OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [unicode/] [utf16/] [utf16.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2010 The Go Authors.  All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// Package utf16 implements encoding and decoding of UTF-16 sequences.
6
package utf16
7
 
8
import "unicode"
9
 
10
const (
11
        // 0xd800-0xdc00 encodes the high 10 bits of a pair.
12
        // 0xdc00-0xe000 encodes the low 10 bits of a pair.
13
        // the value is those 20 bits plus 0x10000.
14
        surr1 = 0xd800
15
        surr2 = 0xdc00
16
        surr3 = 0xe000
17
 
18
        surrSelf = 0x10000
19
)
20
 
21
// IsSurrogate returns true if the specified Unicode code point
22
// can appear in a surrogate pair.
23
func IsSurrogate(r rune) bool {
24
        return surr1 <= r && r < surr3
25
}
26
 
27
// DecodeRune returns the UTF-16 decoding of a surrogate pair.
28
// If the pair is not a valid UTF-16 surrogate pair, DecodeRune returns
29
// the Unicode replacement code point U+FFFD.
30
func DecodeRune(r1, r2 rune) rune {
31
        if surr1 <= r1 && r1 < surr2 && surr2 <= r2 && r2 < surr3 {
32
                return (rune(r1)-surr1)<<10 | (rune(r2) - surr2) + 0x10000
33
        }
34
        return unicode.ReplacementChar
35
}
36
 
37
// EncodeRune returns the UTF-16 surrogate pair r1, r2 for the given rune.
38
// If the rune is not a valid Unicode code point or does not need encoding,
39
// EncodeRune returns U+FFFD, U+FFFD.
40
func EncodeRune(r rune) (r1, r2 rune) {
41
        if r < surrSelf || r > unicode.MaxRune || IsSurrogate(r) {
42
                return unicode.ReplacementChar, unicode.ReplacementChar
43
        }
44
        r -= surrSelf
45
        return surr1 + (r>>10)&0x3ff, surr2 + r&0x3ff
46
}
47
 
48
// Encode returns the UTF-16 encoding of the Unicode code point sequence s.
49
func Encode(s []rune) []uint16 {
50
        n := len(s)
51
        for _, v := range s {
52
                if v >= surrSelf {
53
                        n++
54
                }
55
        }
56
 
57
        a := make([]uint16, n)
58
        n = 0
59
        for _, v := range s {
60
                switch {
61
                case v < 0, surr1 <= v && v < surr3, v > unicode.MaxRune:
62
                        v = unicode.ReplacementChar
63
                        fallthrough
64
                case v < surrSelf:
65
                        a[n] = uint16(v)
66
                        n++
67
                default:
68
                        r1, r2 := EncodeRune(v)
69
                        a[n] = uint16(r1)
70
                        a[n+1] = uint16(r2)
71
                        n += 2
72
                }
73
        }
74
        return a[0:n]
75
}
76
 
77
// Decode returns the Unicode code point sequence represented
78
// by the UTF-16 encoding s.
79
func Decode(s []uint16) []rune {
80
        a := make([]rune, len(s))
81
        n := 0
82
        for i := 0; i < len(s); i++ {
83
                switch r := s[i]; {
84
                case surr1 <= r && r < surr2 && i+1 < len(s) &&
85
                        surr2 <= s[i+1] && s[i+1] < surr3:
86
                        // valid surrogate sequence
87
                        a[n] = DecodeRune(rune(r), rune(s[i+1]))
88
                        i++
89
                        n++
90
                case surr1 <= r && r < surr3:
91
                        // invalid surrogate sequence
92
                        a[n] = unicode.ReplacementChar
93
                        n++
94
                default:
95
                        // normal rune
96
                        a[n] = rune(r)
97
                        n++
98
                }
99
        }
100
        return a[0:n]
101
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.