Left: | ||
Right: |
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 // The bytes package implements functions for the manipulation of byte slices. | 5 // The bytes package implements functions for the manipulation of byte slices. |
6 // Analogous to the facilities of the strings package. | 6 // 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 c := sep[0] | 121 c := sep[0] |
122 for i := len(s) - n; i >= 0; i-- { | 122 for i := len(s) - n; i >= 0; i-- { |
123 if s[i] == c && (n == 1 || Equal(s[i:i+n], sep)) { | 123 if s[i] == c && (n == 1 || Equal(s[i:i+n], sep)) { |
124 return i | 124 return i |
125 } | 125 } |
126 } | 126 } |
127 return -1 | 127 return -1 |
128 } | 128 } |
129 | 129 |
130 // IndexRune interprets s as a sequence of UTF-8-encoded Unicode code points. | 130 // IndexRune interprets s as a sequence of UTF-8-encoded Unicode code points. |
131 // It returns the byte index of the first occurrence in s of the Unicode code | 131 // It returns the byte index of the first occurrence in s of the given rune. |
r
2010/08/05 12:55:07
You can just say
of the given rune.
The context
| |
132 // point rune. It returns -1 if rune is not present in s. | 132 // It returns -1 if rune is not present in s. |
133 func IndexRune(s []byte, rune int) int { | 133 func IndexRune(s []byte, rune int) int { |
134 for i := 0; i < len(s); { | 134 for i := 0; i < len(s); { |
135 r, size := utf8.DecodeRune(s[i:]) | 135 r, size := utf8.DecodeRune(s[i:]) |
136 if r == rune { | 136 if r == rune { |
137 return i | 137 return i |
138 } | 138 } |
139 i += size | 139 i += size |
140 } | 140 } |
141 return -1 | 141 return -1 |
142 } | 142 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 return genSplit(s, sep, len(sep), n) | 213 return genSplit(s, sep, len(sep), n) |
214 } | 214 } |
215 | 215 |
216 // Fields splits the array s around each instance of one or more consecutive whi te space | 216 // Fields splits the array s around each instance of one or more consecutive whi te space |
217 // characters, returning a slice of subarrays of s or an empty list if s contain s only white space. | 217 // characters, returning a slice of subarrays of s or an empty list if s contain s only white space. |
218 func Fields(s []byte) [][]byte { | 218 func Fields(s []byte) [][]byte { |
219 return FieldsFunc(s, unicode.IsSpace) | 219 return FieldsFunc(s, unicode.IsSpace) |
220 } | 220 } |
221 | 221 |
222 // FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points. | 222 // FieldsFunc interprets s as a sequence of UTF-8-encoded Unicode code points. |
223 // It splits the array s at each run of Unicode code points c satisfying f(c) | 223 // It splits the array s at each run of code points c satisfying f(c) and |
r
2010/08/05 12:55:07
s/Unicode code points/code points/
| |
224 // and returns a slice of subarrays of s. If no code points in s satisfy f(c), | 224 // returns a slice of subarrays of s. If no code points in s satisfy f(c), an |
225 // an empty slice is returned. | 225 // empty slice is returned. |
226 func FieldsFunc(s []byte, f func(int) bool) [][]byte { | 226 func FieldsFunc(s []byte, f func(int) bool) [][]byte { |
227 n := 0 | 227 n := 0 |
228 inField := false | 228 inField := false |
229 for i := 0; i < len(s); { | 229 for i := 0; i < len(s); { |
230 rune, size := utf8.DecodeRune(s[i:]) | 230 rune, size := utf8.DecodeRune(s[i:]) |
231 wasInField := inField | 231 wasInField := inField |
232 inField = !f(rune) | 232 inField = !f(rune) |
233 if inField && !wasInField { | 233 if inField && !wasInField { |
234 n++ | 234 n++ |
235 } | 235 } |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
640 } else { | 640 } else { |
641 j += Index(s[start:], old) | 641 j += Index(s[start:], old) |
642 } | 642 } |
643 w += copy(t[w:], s[start:j]) | 643 w += copy(t[w:], s[start:j]) |
644 w += copy(t[w:], new) | 644 w += copy(t[w:], new) |
645 start = j + len(old) | 645 start = j + len(old) |
646 } | 646 } |
647 w += copy(t[w:], s[start:]) | 647 w += copy(t[w:], s[start:]) |
648 return t[0:w] | 648 return t[0:w] |
649 } | 649 } |
LEFT | RIGHT |