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 // Package bytes implements functions for the manipulation of byte slices. | 5 // Package bytes implements functions for the manipulation of byte slices. |
6 // It is analogous to the facilities of the strings package. | 6 // It is analogous to the facilities of the strings package. |
7 package bytes | 7 package bytes |
8 | 8 |
9 import ( | 9 import ( |
10 "unicode" | 10 "unicode" |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 r, l := utf8.DecodeRune(s) | 598 r, l := utf8.DecodeRune(s) |
599 t[i] = r | 599 t[i] = r |
600 i++ | 600 i++ |
601 s = s[l:] | 601 s = s[l:] |
602 } | 602 } |
603 return t | 603 return t |
604 } | 604 } |
605 | 605 |
606 // Replace returns a copy of the slice s with the first n | 606 // Replace returns a copy of the slice s with the first n |
607 // non-overlapping instances of old replaced by new. | 607 // non-overlapping instances of old replaced by new. |
| 608 // If old is empty, it matches at the beginning of the slice |
| 609 // and after each UTF-8 sequence, yielding up to k+1 replacements |
| 610 // for a k-rune slice. |
608 // If n < 0, there is no limit on the number of replacements. | 611 // If n < 0, there is no limit on the number of replacements. |
609 func Replace(s, old, new []byte, n int) []byte { | 612 func Replace(s, old, new []byte, n int) []byte { |
610 m := 0 | 613 m := 0 |
611 if n != 0 { | 614 if n != 0 { |
612 // Compute number of replacements. | 615 // Compute number of replacements. |
613 m = Count(s, old) | 616 m = Count(s, old) |
614 } | 617 } |
615 if m == 0 { | 618 if m == 0 { |
616 // Just return a copy. | 619 // Just return a copy. |
617 return append([]byte(nil), s...) | 620 return append([]byte(nil), s...) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
689 } | 692 } |
690 if r == tr { | 693 if r == tr { |
691 continue | 694 continue |
692 } | 695 } |
693 return false | 696 return false |
694 } | 697 } |
695 | 698 |
696 // One string is empty. Are both? | 699 // One string is empty. Are both? |
697 return len(s) == len(t) | 700 return len(s) == len(t) |
698 } | 701 } |
OLD | NEW |