2008-11-21 17:13:31 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// Functions and constants to support text encoded in UTF-8.
|
|
|
|
// This package calls a Unicode character a rune for brevity.
|
2008-11-21 17:13:31 -07:00
|
|
|
package utf8
|
|
|
|
|
2009-08-31 14:01:25 -06:00
|
|
|
import "unicode" // only needed for a couple of constants
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// Numbers fundamental to the encoding.
|
2009-01-20 15:40:40 -07:00
|
|
|
const (
|
2009-10-08 16:14:54 -06:00
|
|
|
RuneError = unicode.ReplacementChar; // the "error" Rune or "replacement character".
|
|
|
|
RuneSelf = 0x80; // characters below Runeself are represented as themselves in a single byte.
|
|
|
|
UTFMax = 4; // maximum number of bytes of a UTF-8 encoded Unicode character.
|
2008-11-21 17:13:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2009-10-08 16:14:54 -06:00
|
|
|
_T1 = 0x00; // 0000 0000
|
|
|
|
_Tx = 0x80; // 1000 0000
|
|
|
|
_T2 = 0xC0; // 1100 0000
|
|
|
|
_T3 = 0xE0; // 1110 0000
|
|
|
|
_T4 = 0xF0; // 1111 0000
|
|
|
|
_T5 = 0xF8; // 1111 1000
|
|
|
|
|
|
|
|
_Maskx = 0x3F; // 0011 1111
|
|
|
|
_Mask2 = 0x1F; // 0001 1111
|
|
|
|
_Mask3 = 0x0F; // 0000 1111
|
|
|
|
_Mask4 = 0x07; // 0000 0111
|
|
|
|
|
|
|
|
_Rune1Max = 1<<7 - 1;
|
|
|
|
_Rune2Max = 1<<11 - 1;
|
|
|
|
_Rune3Max = 1<<16 - 1;
|
|
|
|
_Rune4Max = 1<<21 - 1;
|
2008-11-21 17:13:31 -07:00
|
|
|
)
|
|
|
|
|
2009-01-16 13:47:24 -07:00
|
|
|
func decodeRuneInternal(p []byte) (rune, size int, short bool) {
|
2008-11-24 15:51:33 -07:00
|
|
|
n := len(p);
|
|
|
|
if n < 1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 0, true
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
c0 := p[0];
|
|
|
|
|
|
|
|
// 1-byte, 7-bit sequence?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _Tx {
|
2009-11-09 13:07:39 -07:00
|
|
|
return int(c0), 1, false
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// unexpected continuation byte?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _T2 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// need first continuation byte
|
2008-11-24 15:51:33 -07:00
|
|
|
if n < 2 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, true
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
c1 := p[1];
|
2009-01-16 13:47:24 -07:00
|
|
|
if c1 < _Tx || _T2 <= c1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2-byte, 11-bit sequence?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _T3 {
|
|
|
|
rune = int(c0&_Mask2)<<6 | int(c1&_Maskx);
|
|
|
|
if rune <= _Rune1Max {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
2009-10-08 16:14:54 -06:00
|
|
|
return rune, 2, false;
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// need second continuation byte
|
2008-11-24 15:51:33 -07:00
|
|
|
if n < 3 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, true
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
c2 := p[2];
|
2009-01-16 13:47:24 -07:00
|
|
|
if c2 < _Tx || _T2 <= c2 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3-byte, 16-bit sequence?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _T4 {
|
|
|
|
rune = int(c0&_Mask3)<<12 | int(c1&_Maskx)<<6 | int(c2&_Maskx);
|
|
|
|
if rune <= _Rune2Max {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
2009-10-08 16:14:54 -06:00
|
|
|
return rune, 3, false;
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// need third continuation byte
|
2008-11-24 15:51:33 -07:00
|
|
|
if n < 4 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, true
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
c3 := p[3];
|
2009-01-16 13:47:24 -07:00
|
|
|
if c3 < _Tx || _T2 <= c3 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 4-byte, 21-bit sequence?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _T5 {
|
|
|
|
rune = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx);
|
|
|
|
if rune <= _Rune3Max {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
2009-10-08 16:14:54 -06:00
|
|
|
return rune, 4, false;
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// error
|
2009-10-08 16:14:54 -06:00
|
|
|
return RuneError, 1, false;
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
2009-05-11 15:10:34 -06:00
|
|
|
func decodeRuneInStringInternal(s string) (rune, size int, short bool) {
|
|
|
|
n := len(s);
|
2008-11-24 15:51:33 -07:00
|
|
|
if n < 1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 0, true
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-05-11 15:10:34 -06:00
|
|
|
c0 := s[0];
|
2008-11-24 15:51:33 -07:00
|
|
|
|
|
|
|
// 1-byte, 7-bit sequence?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _Tx {
|
2009-11-09 13:07:39 -07:00
|
|
|
return int(c0), 1, false
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// unexpected continuation byte?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _T2 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// need first continuation byte
|
|
|
|
if n < 2 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, true
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-05-11 15:10:34 -06:00
|
|
|
c1 := s[1];
|
2009-01-16 13:47:24 -07:00
|
|
|
if c1 < _Tx || _T2 <= c1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2-byte, 11-bit sequence?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _T3 {
|
|
|
|
rune = int(c0&_Mask2)<<6 | int(c1&_Maskx);
|
|
|
|
if rune <= _Rune1Max {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-10-08 16:14:54 -06:00
|
|
|
return rune, 2, false;
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// need second continuation byte
|
|
|
|
if n < 3 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, true
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-05-11 15:10:34 -06:00
|
|
|
c2 := s[2];
|
2009-01-16 13:47:24 -07:00
|
|
|
if c2 < _Tx || _T2 <= c2 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3-byte, 16-bit sequence?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _T4 {
|
|
|
|
rune = int(c0&_Mask3)<<12 | int(c1&_Maskx)<<6 | int(c2&_Maskx);
|
|
|
|
if rune <= _Rune2Max {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-10-08 16:14:54 -06:00
|
|
|
return rune, 3, false;
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// need third continuation byte
|
|
|
|
if n < 4 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, true
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-05-11 15:10:34 -06:00
|
|
|
c3 := s[3];
|
2009-01-16 13:47:24 -07:00
|
|
|
if c3 < _Tx || _T2 <= c3 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 4-byte, 21-bit sequence?
|
2009-01-16 13:47:24 -07:00
|
|
|
if c0 < _T5 {
|
|
|
|
rune = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx);
|
|
|
|
if rune <= _Rune3Max {
|
2009-11-09 13:07:39 -07:00
|
|
|
return RuneError, 1, false
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
2009-10-08 16:14:54 -06:00
|
|
|
return rune, 4, false;
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// error
|
2009-10-08 16:14:54 -06:00
|
|
|
return RuneError, 1, false;
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune.
|
|
|
|
// An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.
|
2009-01-20 15:40:40 -07:00
|
|
|
func FullRune(p []byte) bool {
|
2009-09-15 10:41:59 -06:00
|
|
|
_, _, short := decodeRuneInternal(p);
|
2009-10-08 16:14:54 -06:00
|
|
|
return !short;
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// FullRuneInString is like FullRune but its input is a string.
|
2009-05-11 15:10:34 -06:00
|
|
|
func FullRuneInString(s string) bool {
|
2009-09-15 10:41:59 -06:00
|
|
|
_, _, short := decodeRuneInStringInternal(s);
|
2009-10-08 16:14:54 -06:00
|
|
|
return !short;
|
2008-11-24 15:51:33 -07:00
|
|
|
}
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and its width in bytes.
|
2009-01-20 15:40:40 -07:00
|
|
|
func DecodeRune(p []byte) (rune, size int) {
|
2009-09-14 18:20:29 -06:00
|
|
|
rune, size, _ = decodeRuneInternal(p);
|
2008-11-21 17:13:31 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// DecodeRuneInString is like DecodeRune but its input is a string.
|
2009-05-11 15:10:34 -06:00
|
|
|
func DecodeRuneInString(s string) (rune, size int) {
|
2009-09-14 18:20:29 -06:00
|
|
|
rune, size, _ = decodeRuneInStringInternal(s);
|
2008-11-24 15:51:33 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// RuneLen returns the number of bytes required to encode the rune.
|
2009-01-20 15:40:40 -07:00
|
|
|
func RuneLen(rune int) int {
|
2008-11-21 17:13:31 -07:00
|
|
|
switch {
|
2009-01-16 13:47:24 -07:00
|
|
|
case rune <= _Rune1Max:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 1
|
2009-01-16 13:47:24 -07:00
|
|
|
case rune <= _Rune2Max:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 2
|
2009-01-16 13:47:24 -07:00
|
|
|
case rune <= _Rune3Max:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 3
|
2009-01-16 13:47:24 -07:00
|
|
|
case rune <= _Rune4Max:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 4
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// EncodeRune writes into p (which must be large enough) the UTF-8 encoding of the rune.
|
|
|
|
// It returns the number of bytes written.
|
2009-01-20 15:40:40 -07:00
|
|
|
func EncodeRune(rune int, p []byte) int {
|
2009-01-16 13:47:24 -07:00
|
|
|
if rune <= _Rune1Max {
|
2008-11-21 17:13:31 -07:00
|
|
|
p[0] = byte(rune);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-16 13:47:24 -07:00
|
|
|
if rune <= _Rune2Max {
|
2009-11-09 22:23:52 -07:00
|
|
|
p[0] = _T2 | byte(rune>>6);
|
2009-01-16 13:47:24 -07:00
|
|
|
p[1] = _Tx | byte(rune)&_Maskx;
|
2008-11-21 17:13:31 -07:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2009-08-31 14:01:25 -06:00
|
|
|
if rune > unicode.MaxRune {
|
2009-11-09 13:07:39 -07:00
|
|
|
rune = RuneError
|
2008-11-21 17:13:31 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 13:47:24 -07:00
|
|
|
if rune <= _Rune3Max {
|
2009-11-09 22:23:52 -07:00
|
|
|
p[0] = _T3 | byte(rune>>12);
|
2009-01-16 13:47:24 -07:00
|
|
|
p[1] = _Tx | byte(rune>>6)&_Maskx;
|
|
|
|
p[2] = _Tx | byte(rune)&_Maskx;
|
2008-11-21 17:13:31 -07:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2009-11-09 22:23:52 -07:00
|
|
|
p[0] = _T4 | byte(rune>>18);
|
2009-01-16 13:47:24 -07:00
|
|
|
p[1] = _Tx | byte(rune>>12)&_Maskx;
|
|
|
|
p[2] = _Tx | byte(rune>>6)&_Maskx;
|
|
|
|
p[3] = _Tx | byte(rune)&_Maskx;
|
2008-11-21 17:13:31 -07:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// RuneCount returns the number of runes in p. Erroneous and short
|
|
|
|
// encodings are treated as single runes of width 1 byte.
|
2009-01-20 15:40:40 -07:00
|
|
|
func RuneCount(p []byte) int {
|
2008-12-04 22:00:34 -07:00
|
|
|
i := 0;
|
|
|
|
var n int;
|
|
|
|
for n = 0; i < len(p); n++ {
|
|
|
|
if p[i] < RuneSelf {
|
2009-11-09 13:07:39 -07:00
|
|
|
i++
|
2008-12-04 22:00:34 -07:00
|
|
|
} else {
|
2009-09-15 10:41:59 -06:00
|
|
|
_, size := DecodeRune(p[i:len(p)]);
|
2008-12-04 22:00:34 -07:00
|
|
|
i += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2009-03-05 20:15:13 -07:00
|
|
|
// RuneCountInString is like RuneCount but its input is a string.
|
2009-05-04 23:12:13 -06:00
|
|
|
func RuneCountInString(s string) int {
|
|
|
|
ei := len(s);
|
|
|
|
i := 0;
|
2009-09-15 10:41:59 -06:00
|
|
|
var n int;
|
2008-12-04 22:00:34 -07:00
|
|
|
for n = 0; i < ei; n++ {
|
|
|
|
if s[i] < RuneSelf {
|
2009-11-09 13:07:39 -07:00
|
|
|
i++
|
2008-12-04 22:00:34 -07:00
|
|
|
} else {
|
2009-09-15 10:41:59 -06:00
|
|
|
_, size, _ := decodeRuneInStringInternal(s[i:ei]);
|
2008-12-04 22:00:34 -07:00
|
|
|
i += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2009-09-01 12:06:28 -06:00
|
|
|
// RuneStart reports whether the byte could be the first byte of
|
|
|
|
// an encoded rune. Second and subsequent bytes always have the top
|
|
|
|
// two bits set to 10.
|
2009-11-06 15:24:38 -07:00
|
|
|
func RuneStart(b byte) bool { return b&0xC0 != 0x80 }
|