LEFT | RIGHT |
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 } | 68 } |
69 | 69 |
70 // ContainsAny returns true if any Unicode code points in chars are within s. | 70 // ContainsAny returns true if any Unicode code points in chars are within s. |
71 func ContainsAny(s, chars string) bool { | 71 func ContainsAny(s, chars string) bool { |
72 » return IndexAny(s, chars) != -1 | 72 » return IndexAny(s, chars) >= 0 |
73 } | |
74 | |
75 // ContainsFunc returns true if any unicode code points within s satisfy f(c). | |
76 func ContainsFunc(s string, f func(rune) bool) bool { | |
77 » return IndexFunc(s, f) != -1 | |
78 } | 73 } |
79 | 74 |
80 // ContainsRune returns true if the Unicode code point r is within s. | 75 // ContainsRune returns true if the Unicode code point r is within s. |
81 func ContainsRune(s string, r rune) bool { | 76 func ContainsRune(s string, r rune) bool { |
82 » return IndexRune(s, r) != -1 | 77 » return IndexRune(s, r) >= 0 |
83 } | 78 } |
84 | 79 |
85 // 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. |
86 func Index(s, sep string) int { | 81 func Index(s, sep string) int { |
87 n := len(sep) | 82 n := len(sep) |
88 if n == 0 { | 83 if n == 0 { |
89 return 0 | 84 return 0 |
90 } | 85 } |
91 c := sep[0] | 86 c := sep[0] |
92 if n == 1 { | 87 if n == 1 { |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 if f(rune) { | 272 if f(rune) { |
278 if fieldStart >= 0 { | 273 if fieldStart >= 0 { |
279 a[na] = s[fieldStart:i] | 274 a[na] = s[fieldStart:i] |
280 na++ | 275 na++ |
281 fieldStart = -1 | 276 fieldStart = -1 |
282 } | 277 } |
283 } else if fieldStart == -1 { | 278 } else if fieldStart == -1 { |
284 fieldStart = i | 279 fieldStart = i |
285 } | 280 } |
286 } | 281 } |
287 » if fieldStart != -1 { // Last field might end at EOF. | 282 » if fieldStart >= 0 { // Last field might end at EOF. |
288 a[na] = s[fieldStart:] | 283 a[na] = s[fieldStart:] |
289 } | 284 } |
290 return a | 285 return a |
291 } | 286 } |
292 | 287 |
293 // 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 |
294 // sep is placed between elements in the resulting string. | 289 // sep is placed between elements in the resulting string. |
295 func Join(a []string, sep string) string { | 290 func Join(a []string, sep string) string { |
296 if len(a) == 0 { | 291 if len(a) == 0 { |
297 return "" | 292 return "" |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 r, size := utf8.DecodeLastRuneInString(s[0:i]) | 515 r, size := utf8.DecodeLastRuneInString(s[0:i]) |
521 i -= size | 516 i -= size |
522 if f(r) == truth { | 517 if f(r) == truth { |
523 return i | 518 return i |
524 } | 519 } |
525 } | 520 } |
526 return -1 | 521 return -1 |
527 } | 522 } |
528 | 523 |
529 func makeCutsetFunc(cutset string) func(rune) bool { | 524 func makeCutsetFunc(cutset string) func(rune) bool { |
530 » return func(r rune) bool { return IndexRune(cutset, r) != -1 } | 525 » return func(r rune) bool { return IndexRune(cutset, r) >= 0 } |
531 } | 526 } |
532 | 527 |
533 // 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 |
534 // trailing Unicode code points contained in cutset removed. | 529 // trailing Unicode code points contained in cutset removed. |
535 func Trim(s string, cutset string) string { | 530 func Trim(s string, cutset string) string { |
536 if s == "" || cutset == "" { | 531 if s == "" || cutset == "" { |
537 return s | 532 return s |
538 } | 533 } |
539 return TrimFunc(s, makeCutsetFunc(cutset)) | 534 return TrimFunc(s, makeCutsetFunc(cutset)) |
540 } | 535 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 } | 642 } |
648 if r == tr { | 643 if r == tr { |
649 continue | 644 continue |
650 } | 645 } |
651 return false | 646 return false |
652 } | 647 } |
653 | 648 |
654 // One string is empty. Are both? | 649 // One string is empty. Are both? |
655 return s == t | 650 return s == t |
656 } | 651 } |
LEFT | RIGHT |