-
Notifications
You must be signed in to change notification settings - Fork 29
/
text.go
111 lines (102 loc) · 2.58 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package toolbox
import (
"bufio"
"github.com/viant/toolbox/format"
"io"
"unicode"
)
//IsASCIIText return true if supplied string does not have binary data
func IsASCIIText(candidate string) bool {
for _, r := range candidate {
if r == '\n' || r == '\r' || r == '\t' {
continue
}
if r > unicode.MaxASCII || !unicode.IsPrint(r) || r == '`' {
return false
}
}
return true
}
//IsPrintText return true if all candidate characters are printable (unicode.IsPrintText)
func IsPrintText(candidate string) bool {
for _, r := range candidate {
if !unicode.IsPrint(r) {
if r == '\n' || r == '\r' || r == '\t' || r == '`' {
continue
}
return false
}
}
return true
}
//TerminatedSplitN split supplied text into n fragmentCount, each terminated with supplied terminator
func TerminatedSplitN(text string, fragmentCount int, terminator string) []string {
var result = make([]string, 0)
if fragmentCount == 0 {
fragmentCount = 1
}
fragmentSize := len(text) / fragmentCount
lowerBound := 0
for i := fragmentSize - 1; i < len(text); i++ {
isLast := i+1 == len(text)
isAtLeastOfFragmentSize := i-lowerBound >= fragmentSize
isNewLine := string(text[i:i+len(terminator)]) == terminator
if (isAtLeastOfFragmentSize && isNewLine) || isLast {
result = append(result, string(text[lowerBound:i+1]))
lowerBound = i + 1
}
}
return result
}
//SplitTextStream divides reader supplied text by number of specified line
func SplitTextStream(reader io.Reader, writerProvider func() io.WriteCloser, elementCount int) error {
scanner := bufio.NewScanner(reader)
var writer io.WriteCloser
counter := 0
var err error
if elementCount == 0 {
elementCount = 1
}
for scanner.Scan() {
if writer == nil {
writer = writerProvider()
}
data := scanner.Bytes()
if err = scanner.Err(); err != nil {
return err
}
if counter > 0 {
if _, err = writer.Write([]byte{'\n'}); err != nil {
return err
}
}
if _, err = writer.Write(data); err != nil {
return err
}
counter++
if counter == elementCount {
if err := writer.Close(); err != nil {
return err
}
counter = 0
writer = nil
}
}
if writer != nil {
return writer.Close()
}
return nil
}
const (
CaseUpper = iota
CaseLower
CaseUpperCamel
CaseLowerCamel
CaseUpperUnderscore
CaseLowerUnderscore
)
//ToCaseFormat format text, from, to are const: CaseLower, CaseUpperCamel, CaseLowerCamel, CaseUpperUnderscore, CaseLowerUnderscore,
// Deprecated: please use format.Case instead
func ToCaseFormat(text string, from, to int) string {
return format.Case(from).Format(text, format.Case(to))
}