codekingpro/portable-devtools
114k
1// Copyright 2010 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4 5// Package utf16 implements encoding and decoding of UTF-16 sequences.6package utf167 8// The conditions replacementChar==unicode.ReplacementChar and9// maxRune==unicode.MaxRune are verified in the tests.10// Defining them locally avoids this package depending on package unicode.11 12const (13 replacementChar = '\uFFFD' // Unicode replacement character14 maxRune = '\U0010FFFF' // Maximum valid Unicode code point.15)16 17const (18 // 0xd800-0xdc00 encodes the high 10 bits of a pair.19 // 0xdc00-0xe000 encodes the low 10 bits of a pair.20 // the value is those 20 bits plus 0x10000.21 surr1 = 0xd80022 surr2 = 0xdc0023 surr3 = 0xe00024 25 surrSelf = 0x1000026)27 28// IsSurrogate reports whether the specified Unicode code point29// can appear in a surrogate pair.30func IsSurrogate(r rune) bool {31 return surr1 <= r && r < surr332}33 34// DecodeRune returns the UTF-16 decoding of a surrogate pair.35// If the pair is not a valid UTF-16 surrogate pair, DecodeRune returns36// the Unicode replacement code point U+FFFD.37func DecodeRune(r1, r2 rune) rune {38 if surr1 <= r1 && r1 < surr2 && surr2 <= r2 && r2 < surr3 {39 return (r1-surr1)<<10 | (r2 - surr2) + surrSelf40 }41 return replacementChar42}43 44// EncodeRune returns the UTF-16 surrogate pair r1, r2 for the given rune.45// If the rune is not a valid Unicode code point or does not need encoding,46// EncodeRune returns U+FFFD, U+FFFD.47func EncodeRune(r rune) (r1, r2 rune) {48 if r < surrSelf || r > maxRune {49 return replacementChar, replacementChar50 }51 r -= surrSelf52 return surr1 + (r>>10)&0x3ff, surr2 + r&0x3ff53}54 55// RuneLen returns the number of 16-bit words in the UTF-16 encoding of the rune.56// It returns -1 if the rune is not a valid value to encode in UTF-16.57func RuneLen(r rune) int {58 switch {59 case 0 <= r && r < surr1, surr3 <= r && r < surrSelf:60 return 161 case surrSelf <= r && r <= maxRune:62 return 263 default:64 return -165 }66}67 68// Encode returns the UTF-16 encoding of the Unicode code point sequence s.69func Encode(s []rune) []uint16 {70 n := len(s)71 for _, v := range s {72 if v >= surrSelf {73 n++74 }75 }76 77 a := make([]uint16, n)78 n = 079 for _, v := range s {80 switch RuneLen(v) {81 case 1: // normal rune82 a[n] = uint16(v)83 n++84 case 2: // needs surrogate sequence85 r1, r2 := EncodeRune(v)86 a[n] = uint16(r1)87 a[n+1] = uint16(r2)88 n += 289 default:90 a[n] = uint16(replacementChar)91 n++92 }93 }94 return a[:n]95}96 97// AppendRune appends the UTF-16 encoding of the Unicode code point r98// to the end of p and returns the extended buffer. If the rune is not99// a valid Unicode code point, it appends the encoding of U+FFFD.100func AppendRune(a []uint16, r rune) []uint16 {101 // This function is inlineable for fast handling of ASCII.102 switch {103 case 0 <= r && r < surr1, surr3 <= r && r < surrSelf:104 // normal rune105 return append(a, uint16(r))106 case surrSelf <= r && r <= maxRune:107 // needs surrogate sequence108 r1, r2 := EncodeRune(r)109 return append(a, uint16(r1), uint16(r2))110 }111 return append(a, replacementChar)112}113 114// Decode returns the Unicode code point sequence represented115// by the UTF-16 encoding s.116func Decode(s []uint16) []rune {117 // Preallocate capacity to hold up to 64 runes.118 // Decode inlines, so the allocation can live on the stack.119 buf := make([]rune, 0, 64)120 return decode(s, buf)121}122 123// decode appends to buf the Unicode code point sequence represented124// by the UTF-16 encoding s and return the extended buffer.125func decode(s []uint16, buf []rune) []rune {126 for i := 0; i < len(s); i++ {127 var ar rune128 switch r := s[i]; {129 case r < surr1, surr3 <= r:130 // normal rune131 ar = rune(r)132 case surr1 <= r && r < surr2 && i+1 < len(s) &&133 surr2 <= s[i+1] && s[i+1] < surr3:134 // valid surrogate sequence135 ar = DecodeRune(rune(r), rune(s[i+1]))136 i++137 default:138 // invalid surrogate sequence139 ar = replacementChar140 }141 buf = append(buf, ar)142 }143 return buf144}145 