~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/unicode/utf8/utf8.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
        UTFMax    = 4            // maximum number of bytes of a UTF-8 encoded Unicode character.
19
19
)
20
20
 
 
21
// Code points in the surrogate range are not valid for UTF-8.
 
22
const (
 
23
        surrogateMin = 0xD800
 
24
        surrogateMax = 0xDFFF
 
25
)
 
26
 
21
27
const (
22
28
        t1 = 0x00 // 0000 0000
23
29
        tx = 0x80 // 1000 0000
34
40
        rune1Max = 1<<7 - 1
35
41
        rune2Max = 1<<11 - 1
36
42
        rune3Max = 1<<16 - 1
37
 
        rune4Max = 1<<21 - 1
38
43
)
39
44
 
40
45
func decodeRuneInternal(p []byte) (r rune, size int, short bool) {
87
92
                if r <= rune2Max {
88
93
                        return RuneError, 1, false
89
94
                }
 
95
                if surrogateMin <= r && r <= surrogateMax {
 
96
                        return RuneError, 1, false
 
97
                }
90
98
                return r, 3, false
91
99
        }
92
100
 
102
110
        // 4-byte, 21-bit sequence?
103
111
        if c0 < t5 {
104
112
                r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx)
105
 
                if r <= rune3Max {
 
113
                if r <= rune3Max || MaxRune < r {
106
114
                        return RuneError, 1, false
107
115
                }
108
116
                return r, 4, false
162
170
                if r <= rune2Max {
163
171
                        return RuneError, 1, false
164
172
                }
 
173
                if surrogateMin <= r && r <= surrogateMax {
 
174
                        return RuneError, 1, false
 
175
                }
165
176
                return r, 3, false
166
177
        }
167
178
 
177
188
        // 4-byte, 21-bit sequence?
178
189
        if c0 < t5 {
179
190
                r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx)
180
 
                if r <= rune3Max {
 
191
                if r <= rune3Max || MaxRune < r {
181
192
                        return RuneError, 1, false
182
193
                }
183
194
                return r, 4, false
202
213
 
203
214
// DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and its width in bytes.
204
215
// If the encoding is invalid, it returns (RuneError, 1), an impossible result for correct UTF-8.
 
216
// 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
 
218
// value. No other validation is performed.
205
219
func DecodeRune(p []byte) (r rune, size int) {
206
220
        r, size, _ = decodeRuneInternal(p)
207
221
        return
209
223
 
210
224
// DecodeRuneInString is like DecodeRune but its input is a string.
211
225
// If the encoding is invalid, it returns (RuneError, 1), an impossible result for correct UTF-8.
 
226
// 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
 
228
// value. No other validation is performed.
212
229
func DecodeRuneInString(s string) (r rune, size int) {
213
230
        r, size, _ = decodeRuneInStringInternal(s)
214
231
        return
216
233
 
217
234
// DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and its width in bytes.
218
235
// If the encoding is invalid, it returns (RuneError, 1), an impossible result for correct UTF-8.
 
236
// 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
 
238
// value. No other validation is performed.
219
239
func DecodeLastRune(p []byte) (r rune, size int) {
220
240
        end := len(p)
221
241
        if end == 0 {
250
270
 
251
271
// DecodeLastRuneInString is like DecodeLastRune but its input is a string.
252
272
// If the encoding is invalid, it returns (RuneError, 1), an impossible result for correct UTF-8.
 
273
// 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
 
275
// value. No other validation is performed.
253
276
func DecodeLastRuneInString(s string) (r rune, size int) {
254
277
        end := len(s)
255
278
        if end == 0 {
283
306
}
284
307
 
285
308
// RuneLen returns the number of bytes required to encode the rune.
 
309
// It returns -1 if the rune is not a valid value to encode in UTF-8.
286
310
func RuneLen(r rune) int {
287
311
        switch {
 
312
        case r < 0:
 
313
                return -1
288
314
        case r <= rune1Max:
289
315
                return 1
290
316
        case r <= rune2Max:
291
317
                return 2
 
318
        case surrogateMin <= r && r <= surrogateMax:
 
319
                return -1
292
320
        case r <= rune3Max:
293
321
                return 3
294
 
        case r <= rune4Max:
 
322
        case r <= MaxRune:
295
323
                return 4
296
324
        }
297
325
        return -1
316
344
                r = RuneError
317
345
        }
318
346
 
 
347
        if surrogateMin <= r && r <= surrogateMax {
 
348
                r = RuneError
 
349
        }
 
350
 
319
351
        if uint32(r) <= rune3Max {
320
352
                p[0] = t3 | byte(r>>12)
321
353
                p[1] = tx | byte(r>>6)&maskx
368
400
                } else {
369
401
                        _, size := DecodeRune(p[i:])
370
402
                        if size == 1 {
371
 
                                // All valid runes of size of 1 (those
 
403
                                // All valid runes of size 1 (those
372
404
                                // below RuneSelf) were handled above.
373
405
                                // This must be a RuneError.
374
406
                                return false
395
427
        }
396
428
        return true
397
429
}
 
430
 
 
431
// ValidRune reports whether r can be legally encoded as UTF-8.
 
432
// Code points that are out of range or a surrogate half are illegal.
 
433
func ValidRune(r rune) bool {
 
434
        switch {
 
435
        case r < 0:
 
436
                return false
 
437
        case surrogateMin <= r && r <= surrogateMax:
 
438
                return false
 
439
        case r > MaxRune:
 
440
                return false
 
441
        }
 
442
        return true
 
443
}