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 utf8 implements functions and constants to support text encoded in | 5 // Package utf8 implements functions and constants to support text encoded in |
6 // UTF-8. It includes functions to translate between runes and UTF-8 byte sequen
ces. | 6 // UTF-8. It includes functions to translate between runes and UTF-8 byte sequen
ces. |
7 package utf8 | 7 package utf8 |
8 | 8 |
9 // The conditions RuneError==unicode.ReplacementChar and | 9 // The conditions RuneError==unicode.ReplacementChar and |
10 // MaxRune==unicode.MaxRune are verified in the tests. | 10 // MaxRune==unicode.MaxRune are verified in the tests. |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 _, _, short := decodeRuneInternal(p) | 204 _, _, short := decodeRuneInternal(p) |
205 return !short | 205 return !short |
206 } | 206 } |
207 | 207 |
208 // FullRuneInString is like FullRune but its input is a string. | 208 // FullRuneInString is like FullRune but its input is a string. |
209 func FullRuneInString(s string) bool { | 209 func FullRuneInString(s string) bool { |
210 _, _, short := decodeRuneInStringInternal(s) | 210 _, _, short := decodeRuneInStringInternal(s) |
211 return !short | 211 return !short |
212 } | 212 } |
213 | 213 |
214 // DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and its
width in bytes. | 214 // DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and |
215 // If the encoding is invalid, it returns (RuneError, 1), an impossible result f
or correct UTF-8. | 215 // its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if |
| 216 // the encoding is invalid, it returns (RuneError, 1). Both are impossible |
| 217 // results for correct UTF-8. |
| 218 // |
216 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is | 219 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is |
217 // out of range, or is not the shortest possible UTF-8 encoding for the | 220 // out of range, or is not the shortest possible UTF-8 encoding for the |
218 // value. No other validation is performed. | 221 // value. No other validation is performed. |
219 func DecodeRune(p []byte) (r rune, size int) { | 222 func DecodeRune(p []byte) (r rune, size int) { |
220 r, size, _ = decodeRuneInternal(p) | 223 r, size, _ = decodeRuneInternal(p) |
221 return | 224 return |
222 } | 225 } |
223 | 226 |
224 // DecodeRuneInString is like DecodeRune but its input is a string. | 227 // DecodeRuneInString is like DecodeRune but its input is a string. If s is |
225 // If the encoding is invalid, it returns (RuneError, 1), an impossible result f
or correct UTF-8. | 228 // empty it returns (RuneError, 0). Otherwise, if the encoding is invalid, it |
| 229 // returns (RuneError, 1). Both are impossible results for correct UTF-8. |
| 230 // |
226 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is | 231 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is |
227 // out of range, or is not the shortest possible UTF-8 encoding for the | 232 // out of range, or is not the shortest possible UTF-8 encoding for the |
228 // value. No other validation is performed. | 233 // value. No other validation is performed. |
229 func DecodeRuneInString(s string) (r rune, size int) { | 234 func DecodeRuneInString(s string) (r rune, size int) { |
230 r, size, _ = decodeRuneInStringInternal(s) | 235 r, size, _ = decodeRuneInStringInternal(s) |
231 return | 236 return |
232 } | 237 } |
233 | 238 |
234 // DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and
its width in bytes. | 239 // DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and |
235 // If the encoding is invalid, it returns (RuneError, 1), an impossible result f
or correct UTF-8. | 240 // its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if |
| 241 // the encoding is invalid, it returns (RuneError, 1). Both are impossible |
| 242 // results for correct UTF-8. |
| 243 // |
236 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is | 244 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is |
237 // out of range, or is not the shortest possible UTF-8 encoding for the | 245 // out of range, or is not the shortest possible UTF-8 encoding for the |
238 // value. No other validation is performed. | 246 // value. No other validation is performed. |
239 func DecodeLastRune(p []byte) (r rune, size int) { | 247 func DecodeLastRune(p []byte) (r rune, size int) { |
240 end := len(p) | 248 end := len(p) |
241 if end == 0 { | 249 if end == 0 { |
242 return RuneError, 0 | 250 return RuneError, 0 |
243 } | 251 } |
244 start := end - 1 | 252 start := end - 1 |
245 r = rune(p[start]) | 253 r = rune(p[start]) |
(...skipping 15 matching lines...) Expand all Loading... |
261 if start < 0 { | 269 if start < 0 { |
262 start = 0 | 270 start = 0 |
263 } | 271 } |
264 r, size = DecodeRune(p[start:end]) | 272 r, size = DecodeRune(p[start:end]) |
265 if start+size != end { | 273 if start+size != end { |
266 return RuneError, 1 | 274 return RuneError, 1 |
267 } | 275 } |
268 return r, size | 276 return r, size |
269 } | 277 } |
270 | 278 |
271 // DecodeLastRuneInString is like DecodeLastRune but its input is a string. | 279 // DecodeLastRuneInString is like DecodeLastRune but its input is a string. If |
272 // If the encoding is invalid, it returns (RuneError, 1), an impossible result f
or correct UTF-8. | 280 // s is empty it returns (RuneError, 0). Otherwise, if the encoding is invalid, |
| 281 // it returns (RuneError, 1). Both are impossible results for correct UTF-8. |
| 282 // |
273 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is | 283 // An encoding is invalid if it is incorrect UTF-8, encodes a rune that is |
274 // out of range, or is not the shortest possible UTF-8 encoding for the | 284 // out of range, or is not the shortest possible UTF-8 encoding for the |
275 // value. No other validation is performed. | 285 // value. No other validation is performed. |
276 func DecodeLastRuneInString(s string) (r rune, size int) { | 286 func DecodeLastRuneInString(s string) (r rune, size int) { |
277 end := len(s) | 287 end := len(s) |
278 if end == 0 { | 288 if end == 0 { |
279 return RuneError, 0 | 289 return RuneError, 0 |
280 } | 290 } |
281 start := end - 1 | 291 start := end - 1 |
282 r = rune(s[start]) | 292 r = rune(s[start]) |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 switch { | 436 switch { |
427 case r < 0: | 437 case r < 0: |
428 return false | 438 return false |
429 case surrogateMin <= r && r <= surrogateMax: | 439 case surrogateMin <= r && r <= surrogateMax: |
430 return false | 440 return false |
431 case r > MaxRune: | 441 case r > MaxRune: |
432 return false | 442 return false |
433 } | 443 } |
434 return true | 444 return true |
435 } | 445 } |
LEFT | RIGHT |