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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/*
Copyright 2013 The Perkeep Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package strutil contains string and byte processing functions.
package strutil // import "go4.org/strutil"
import (
"strings"
"unicode"
"unicode/utf8"
)
// Fork of Go's implementation in pkg/strings/strings.go:
// Generic split: splits after each instance of sep,
// including sepSave bytes of sep in the subarrays.
func genSplit(dst []string, s, sep string, sepSave, n int) []string {
if n == 0 {
return nil
}
if sep == "" {
panic("sep is empty")
}
if n < 0 {
n = strings.Count(s, sep) + 1
}
c := sep[0]
start := 0
na := 0
for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ {
if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) {
dst = append(dst, s[start:i+sepSave])
na++
start = i + len(sep)
i += len(sep) - 1
}
}
dst = append(dst, s[start:])
return dst
}
// AppendSplitN is like strings.SplitN but appends to and returns dst.
// Unlike strings.SplitN, an empty separator is not supported.
// The count n determines the number of substrings to return:
// n > 0: at most n substrings; the last substring will be the unsplit remainder.
// n == 0: the result is nil (zero substrings)
// n < 0: all substrings
func AppendSplitN(dst []string, s, sep string, n int) []string {
return genSplit(dst, s, sep, 0, n)
}
// equalFoldRune compares a and b runes whether they fold equally.
//
// The code comes from strings.EqualFold, but shortened to only one rune.
func equalFoldRune(sr, tr rune) bool {
if sr == tr {
return true
}
// Make sr < tr to simplify what follows.
if tr < sr {
sr, tr = tr, sr
}
// Fast check for ASCII.
if tr < utf8.RuneSelf && 'A' <= sr && sr <= 'Z' {
// ASCII, and sr is upper case. tr must be lower case.
if tr == sr+'a'-'A' {
return true
}
return false
}
// General case. SimpleFold(x) returns the next equivalent rune > x
// or wraps around to smaller values.
r := unicode.SimpleFold(sr)
for r != sr && r < tr {
r = unicode.SimpleFold(r)
}
if r == tr {
return true
}
return false
}
// HasPrefixFold is like strings.HasPrefix but uses Unicode case-folding.
func HasPrefixFold(s, prefix string) bool {
if prefix == "" {
return true
}
for _, pr := range prefix {
if s == "" {
return false
}
// step with s, too
sr, size := utf8.DecodeRuneInString(s)
if sr == utf8.RuneError {
return false
}
s = s[size:]
if !equalFoldRune(sr, pr) {
return false
}
}
return true
}
// HasSuffixFold is like strings.HasPrefix but uses Unicode case-folding.
func HasSuffixFold(s, suffix string) bool {
if suffix == "" {
return true
}
// count the runes and bytes in s, but only till rune count of suffix
bo, so := len(s), len(suffix)
for bo > 0 && so > 0 {
r, size := utf8.DecodeLastRuneInString(s[:bo])
if r == utf8.RuneError {
return false
}
bo -= size
sr, size := utf8.DecodeLastRuneInString(suffix[:so])
if sr == utf8.RuneError {
return false
}
so -= size
if !equalFoldRune(r, sr) {
return false
}
}
return so == 0
}
// ContainsFold is like strings.Contains but uses Unicode case-folding.
func ContainsFold(s, substr string) bool {
if substr == "" {
return true
}
if s == "" {
return false
}
firstRune := rune(substr[0])
if firstRune >= utf8.RuneSelf {
firstRune, _ = utf8.DecodeRuneInString(substr)
}
for i, rune := range s {
if equalFoldRune(rune, firstRune) && HasPrefixFold(s[i:], substr) {
return true
}
}
return false
}
// IsPlausibleJSON reports whether s likely contains a JSON object, without
// actually parsing it. It's meant to be a light heuristic.
func IsPlausibleJSON(s string) bool {
return startsWithOpenBrace(s) && endsWithCloseBrace(s)
}
func isASCIIWhite(b byte) bool { return b == ' ' || b == '\n' || b == '\r' || b == '\t' }
func startsWithOpenBrace(s string) bool {
for len(s) > 0 {
switch {
case s[0] == '{':
return true
case isASCIIWhite(s[0]):
s = s[1:]
default:
return false
}
}
return false
}
func endsWithCloseBrace(s string) bool {
for len(s) > 0 {
last := len(s) - 1
switch {
case s[last] == '}':
return true
case isASCIIWhite(s[last]):
s = s[:last]
default:
return false
}
}
return false
}
|