LEFT | RIGHT |
(no file at all) | |
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 // Package strings implements simple functions to manipulate strings. | 5 // Package strings implements simple functions to manipulate strings. |
6 package strings | 6 package strings |
7 | 7 |
8 import ( | 8 import ( |
9 "unicode" | 9 "unicode" |
10 "unicode/utf8" | 10 "unicode/utf8" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 if s[i] == c && s[i:i+l] == sep { | 57 if s[i] == c && s[i:i+l] == sep { |
58 n++ | 58 n++ |
59 i += l - 1 | 59 i += l - 1 |
60 } | 60 } |
61 } | 61 } |
62 return n | 62 return n |
63 } | 63 } |
64 | 64 |
65 // Contains returns true if substr is within s. | 65 // Contains returns true if substr is within s. |
66 func Contains(s, substr string) bool { | 66 func Contains(s, substr string) bool { |
67 » return Index(s, substr) != -1 | 67 » return Index(s, substr) >= 0 |
| 68 } |
| 69 |
| 70 // ContainsAny returns true if any Unicode code points in chars are within s. |
| 71 func ContainsAny(s, chars string) bool { |
| 72 » return IndexAny(s, chars) >= 0 |
| 73 } |
| 74 |
| 75 // ContainsRune returns true if the Unicode code point r is within s. |
| 76 func ContainsRune(s string, r rune) bool { |
| 77 » return IndexRune(s, r) >= 0 |
68 } | 78 } |
69 | 79 |
70 // Index returns the index of the first instance of sep in s, or -1 if sep is no
t present in s. | 80 // Index returns the index of the first instance of sep in s, or -1 if sep is no
t present in s. |
71 func Index(s, sep string) int { | 81 func Index(s, sep string) int { |
72 n := len(sep) | 82 n := len(sep) |
73 if n == 0 { | 83 if n == 0 { |
74 return 0 | 84 return 0 |
75 } | 85 } |
76 c := sep[0] | 86 c := sep[0] |
77 if n == 1 { | 87 if n == 1 { |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 if f(rune) { | 272 if f(rune) { |
263 if fieldStart >= 0 { | 273 if fieldStart >= 0 { |
264 a[na] = s[fieldStart:i] | 274 a[na] = s[fieldStart:i] |
265 na++ | 275 na++ |
266 fieldStart = -1 | 276 fieldStart = -1 |
267 } | 277 } |
268 } else if fieldStart == -1 { | 278 } else if fieldStart == -1 { |
269 fieldStart = i | 279 fieldStart = i |
270 } | 280 } |
271 } | 281 } |
272 » if fieldStart != -1 { // Last field might end at EOF. | 282 » if fieldStart >= 0 { // Last field might end at EOF. |
273 a[na] = s[fieldStart:] | 283 a[na] = s[fieldStart:] |
274 } | 284 } |
275 return a | 285 return a |
276 } | 286 } |
277 | 287 |
278 // Join concatenates the elements of a to create a single string. The separato
r string | 288 // Join concatenates the elements of a to create a single string. The separato
r string |
279 // sep is placed between elements in the resulting string. | 289 // sep is placed between elements in the resulting string. |
280 func Join(a []string, sep string) string { | 290 func Join(a []string, sep string) string { |
281 if len(a) == 0 { | 291 if len(a) == 0 { |
282 return "" | 292 return "" |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 r, size := utf8.DecodeLastRuneInString(s[0:i]) | 515 r, size := utf8.DecodeLastRuneInString(s[0:i]) |
506 i -= size | 516 i -= size |
507 if f(r) == truth { | 517 if f(r) == truth { |
508 return i | 518 return i |
509 } | 519 } |
510 } | 520 } |
511 return -1 | 521 return -1 |
512 } | 522 } |
513 | 523 |
514 func makeCutsetFunc(cutset string) func(rune) bool { | 524 func makeCutsetFunc(cutset string) func(rune) bool { |
515 » return func(r rune) bool { return IndexRune(cutset, r) != -1 } | 525 » return func(r rune) bool { return IndexRune(cutset, r) >= 0 } |
516 } | 526 } |
517 | 527 |
518 // Trim returns a slice of the string s with all leading and | 528 // Trim returns a slice of the string s with all leading and |
519 // trailing Unicode code points contained in cutset removed. | 529 // trailing Unicode code points contained in cutset removed. |
520 func Trim(s string, cutset string) string { | 530 func Trim(s string, cutset string) string { |
521 if s == "" || cutset == "" { | 531 if s == "" || cutset == "" { |
522 return s | 532 return s |
523 } | 533 } |
524 return TrimFunc(s, makeCutsetFunc(cutset)) | 534 return TrimFunc(s, makeCutsetFunc(cutset)) |
525 } | 535 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 } | 642 } |
633 if r == tr { | 643 if r == tr { |
634 continue | 644 continue |
635 } | 645 } |
636 return false | 646 return false |
637 } | 647 } |
638 | 648 |
639 // One string is empty. Are both? | 649 // One string is empty. Are both? |
640 return s == t | 650 return s == t |
641 } | 651 } |
LEFT | RIGHT |