OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // A package of simple functions to manipulate strings. | 5 // A package of simple functions to manipulate strings. |
6 package strings | 6 package strings |
7 | 7 |
8 import ( | 8 import ( |
9 "unicode" | 9 "unicode" |
10 "utf8" | 10 "utf8" |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // SplitAfter splits the string s after each instance of sep, returning an array
of substrings of s. | 165 // SplitAfter splits the string s after each instance of sep, returning an array
of substrings of s. |
166 // If sep is empty, SplitAfter splits s after each UTF-8 sequence. | 166 // If sep is empty, SplitAfter splits s after each UTF-8 sequence. |
167 // If n > 0, SplitAfter splits s into at most n substrings; the last substring w
ill be the unsplit remainder. | 167 // If n > 0, SplitAfter splits s into at most n substrings; the last substring w
ill be the unsplit remainder. |
168 func SplitAfter(s, sep string, n int) []string { | 168 func SplitAfter(s, sep string, n int) []string { |
169 return genSplit(s, sep, len(sep), n) | 169 return genSplit(s, sep, len(sep), n) |
170 } | 170 } |
171 | 171 |
172 // Fields splits the string s around each instance of one or more consecutive wh
ite space | 172 // Fields splits the string s around each instance of one or more consecutive wh
ite space |
173 // characters, returning an array of substrings of s or an empty list if s conta
ins only white space. | 173 // characters, returning an array of substrings of s or an empty list if s conta
ins only white space. |
174 func Fields(s string) []string { | 174 func Fields(s string) []string { |
| 175 return FieldsFunc(s, unicode.IsSpace) |
| 176 } |
| 177 |
| 178 // FieldsFunc splits the string s at each run of Unicode code points c satifying
f(c) |
| 179 // and returns an array of slices of s. If no code points in s satisfy f(c), an
empty slice |
| 180 // is returned. |
| 181 func FieldsFunc(s string, f func(int) bool) []string { |
175 // First count the fields. | 182 // First count the fields. |
176 n := 0 | 183 n := 0 |
177 inField := false | 184 inField := false |
178 for _, rune := range s { | 185 for _, rune := range s { |
179 wasInField := inField | 186 wasInField := inField |
180 » » inField = !unicode.IsSpace(rune) | 187 » » inField = !f(rune) |
181 if inField && !wasInField { | 188 if inField && !wasInField { |
182 n++ | 189 n++ |
183 } | 190 } |
184 } | 191 } |
185 | 192 |
186 // Now create them. | 193 // Now create them. |
187 a := make([]string, n) | 194 a := make([]string, n) |
188 na := 0 | 195 na := 0 |
189 fieldStart := -1 // Set to -1 when looking for start of field. | 196 fieldStart := -1 // Set to -1 when looking for start of field. |
190 for i, rune := range s { | 197 for i, rune := range s { |
191 » » if unicode.IsSpace(rune) { | 198 » » if f(rune) { |
192 if fieldStart >= 0 { | 199 if fieldStart >= 0 { |
193 a[na] = s[fieldStart:i] | 200 a[na] = s[fieldStart:i] |
194 na++ | 201 na++ |
195 fieldStart = -1 | 202 fieldStart = -1 |
196 } | 203 } |
197 } else if fieldStart == -1 { | 204 } else if fieldStart == -1 { |
198 fieldStart = i | 205 fieldStart = i |
199 } | 206 } |
200 } | 207 } |
201 if fieldStart != -1 { // Last field might end at EOF. | 208 if fieldStart != -1 { // Last field might end at EOF. |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 return s | 404 return s |
398 } | 405 } |
399 return TrimRightFunc(s, makeCutsetFunc(cutset)) | 406 return TrimRightFunc(s, makeCutsetFunc(cutset)) |
400 } | 407 } |
401 | 408 |
402 // TrimSpace returns a slice of the string s, with all leading | 409 // TrimSpace returns a slice of the string s, with all leading |
403 // and trailing white space removed, as defined by Unicode. | 410 // and trailing white space removed, as defined by Unicode. |
404 func TrimSpace(s string) string { | 411 func TrimSpace(s string) string { |
405 return TrimFunc(s, unicode.IsSpace) | 412 return TrimFunc(s, unicode.IsSpace) |
406 } | 413 } |
OLD | NEW |