vendor: Add golang-text dependency
This commit is contained in:
parent
d6a5b7e63c
commit
53b9fa02ee
|
@ -0,0 +1,19 @@
|
||||||
|
Copyright 2012 Keith Rarick
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
|
@ -0,0 +1,3 @@
|
||||||
|
This is a Go package for manipulating paragraphs of text.
|
||||||
|
|
||||||
|
See http://go.pkgdoc.org/github.com/kr/text for full documentation.
|
|
@ -0,0 +1,3 @@
|
||||||
|
// Package text provides rudimentary functions for manipulating text in
|
||||||
|
// paragraphs.
|
||||||
|
package text
|
|
@ -0,0 +1,74 @@
|
||||||
|
package text
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Indent inserts prefix at the beginning of each non-empty line of s. The
|
||||||
|
// end-of-line marker is NL.
|
||||||
|
func Indent(s, prefix string) string {
|
||||||
|
return string(IndentBytes([]byte(s), []byte(prefix)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IndentBytes inserts prefix at the beginning of each non-empty line of b.
|
||||||
|
// The end-of-line marker is NL.
|
||||||
|
func IndentBytes(b, prefix []byte) []byte {
|
||||||
|
var res []byte
|
||||||
|
bol := true
|
||||||
|
for _, c := range b {
|
||||||
|
if bol && c != '\n' {
|
||||||
|
res = append(res, prefix...)
|
||||||
|
}
|
||||||
|
res = append(res, c)
|
||||||
|
bol = c == '\n'
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writer indents each line of its input.
|
||||||
|
type indentWriter struct {
|
||||||
|
w io.Writer
|
||||||
|
bol bool
|
||||||
|
pre [][]byte
|
||||||
|
sel int
|
||||||
|
off int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewIndentWriter makes a new write filter that indents the input
|
||||||
|
// lines. Each line is prefixed in order with the corresponding
|
||||||
|
// element of pre. If there are more lines than elements, the last
|
||||||
|
// element of pre is repeated for each subsequent line.
|
||||||
|
func NewIndentWriter(w io.Writer, pre ...[]byte) io.Writer {
|
||||||
|
return &indentWriter{
|
||||||
|
w: w,
|
||||||
|
pre: pre,
|
||||||
|
bol: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The only errors returned are from the underlying indentWriter.
|
||||||
|
func (w *indentWriter) Write(p []byte) (n int, err error) {
|
||||||
|
for _, c := range p {
|
||||||
|
if w.bol {
|
||||||
|
var i int
|
||||||
|
i, err = w.w.Write(w.pre[w.sel][w.off:])
|
||||||
|
w.off += i
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, err = w.w.Write([]byte{c})
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
n++
|
||||||
|
w.bol = c == '\n'
|
||||||
|
if w.bol {
|
||||||
|
w.off = 0
|
||||||
|
if w.sel < len(w.pre)-1 {
|
||||||
|
w.sel++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package text
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
nl = []byte{'\n'}
|
||||||
|
sp = []byte{' '}
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultPenalty = 1e5
|
||||||
|
|
||||||
|
// Wrap wraps s into a paragraph of lines of length lim, with minimal
|
||||||
|
// raggedness.
|
||||||
|
func Wrap(s string, lim int) string {
|
||||||
|
return string(WrapBytes([]byte(s), lim))
|
||||||
|
}
|
||||||
|
|
||||||
|
// WrapBytes wraps b into a paragraph of lines of length lim, with minimal
|
||||||
|
// raggedness.
|
||||||
|
func WrapBytes(b []byte, lim int) []byte {
|
||||||
|
words := bytes.Split(bytes.Replace(bytes.TrimSpace(b), nl, sp, -1), sp)
|
||||||
|
var lines [][]byte
|
||||||
|
for _, line := range WrapWords(words, 1, lim, defaultPenalty) {
|
||||||
|
lines = append(lines, bytes.Join(line, sp))
|
||||||
|
}
|
||||||
|
return bytes.Join(lines, nl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WrapWords is the low-level line-breaking algorithm, useful if you need more
|
||||||
|
// control over the details of the text wrapping process. For most uses, either
|
||||||
|
// Wrap or WrapBytes will be sufficient and more convenient.
|
||||||
|
//
|
||||||
|
// WrapWords splits a list of words into lines with minimal "raggedness",
|
||||||
|
// treating each byte as one unit, accounting for spc units between adjacent
|
||||||
|
// words on each line, and attempting to limit lines to lim units. Raggedness
|
||||||
|
// is the total error over all lines, where error is the square of the
|
||||||
|
// difference of the length of the line and lim. Too-long lines (which only
|
||||||
|
// happen when a single word is longer than lim units) have pen penalty units
|
||||||
|
// added to the error.
|
||||||
|
func WrapWords(words [][]byte, spc, lim, pen int) [][][]byte {
|
||||||
|
n := len(words)
|
||||||
|
|
||||||
|
length := make([][]int, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
length[i] = make([]int, n)
|
||||||
|
length[i][i] = len(words[i])
|
||||||
|
for j := i + 1; j < n; j++ {
|
||||||
|
length[i][j] = length[i][j-1] + spc + len(words[j])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nbrk := make([]int, n)
|
||||||
|
cost := make([]int, n)
|
||||||
|
for i := range cost {
|
||||||
|
cost[i] = math.MaxInt32
|
||||||
|
}
|
||||||
|
for i := n - 1; i >= 0; i-- {
|
||||||
|
if length[i][n-1] <= lim {
|
||||||
|
cost[i] = 0
|
||||||
|
nbrk[i] = n
|
||||||
|
} else {
|
||||||
|
for j := i + 1; j < n; j++ {
|
||||||
|
d := lim - length[i][j-1]
|
||||||
|
c := d*d + cost[j]
|
||||||
|
if length[i][j-1] > lim {
|
||||||
|
c += pen // too-long lines get a worse penalty
|
||||||
|
}
|
||||||
|
if c < cost[i] {
|
||||||
|
cost[i] = c
|
||||||
|
nbrk[i] = j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var lines [][][]byte
|
||||||
|
i := 0
|
||||||
|
for i < n {
|
||||||
|
lines = append(lines, words[i:nbrk[i]])
|
||||||
|
i = nbrk[i]
|
||||||
|
}
|
||||||
|
return lines
|
||||||
|
}
|
|
@ -740,6 +740,12 @@
|
||||||
"revision": "bb4de0191aa41b5507caa14b0650cdbddcd9280b",
|
"revision": "bb4de0191aa41b5507caa14b0650cdbddcd9280b",
|
||||||
"revisionTime": "2016-09-30T03:27:40Z"
|
"revisionTime": "2016-09-30T03:27:40Z"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "t24KnvC9jRxiANVhpw2pqFpmEu8=",
|
||||||
|
"path": "github.com/tonnerre/golang-text",
|
||||||
|
"revision": "048ed3d792f7104850acbc8cfc01e5a6070f4c04",
|
||||||
|
"revisionTime": "2013-09-25T19:58:46Z"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "9jjO5GjLa0XF/nfWihF02RoH4qc=",
|
"checksumSHA1": "9jjO5GjLa0XF/nfWihF02RoH4qc=",
|
||||||
"path": "golang.org/x/net/context",
|
"path": "golang.org/x/net/context",
|
||||||
|
|
Loading…
Reference in New Issue