UTF16Encode utf16 encodes s into chars. It returns the resulting length in units of uint16. It is assumed that the chars slice has enough room for the encoded string.
(s string, chars []uint16)
| 28 | // length in units of uint16. It is assumed that the chars slice |
| 29 | // has enough room for the encoded string. |
| 30 | func UTF16Encode(s string, chars []uint16) int { |
| 31 | n := 0 |
| 32 | for _, v := range s { |
| 33 | switch { |
| 34 | case v < 0, surr1 <= v && v < surr3, v > maxRune: |
| 35 | v = replacementChar |
| 36 | fallthrough |
| 37 | case v < surrSelf: |
| 38 | chars[n] = uint16(v) |
| 39 | n += 1 |
| 40 | default: |
| 41 | // surrogate pair, two uint16 values |
| 42 | r1, r2 := utf16.EncodeRune(v) |
| 43 | chars[n] = uint16(r1) |
| 44 | chars[n+1] = uint16(r2) |
| 45 | n += 2 |
| 46 | } |
| 47 | } |
| 48 | return n |
| 49 | } |