ssa: string range; string convert bytes/rune; op eql/less
This commit is contained in:
Binary file not shown.
132
internal/runtime/utf8.go
Normal file
132
internal/runtime/utf8.go
Normal file
@@ -0,0 +1,132 @@
|
||||
// Copyright 2016 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.
|
||||
|
||||
package runtime
|
||||
|
||||
// Numbers fundamental to the encoding.
|
||||
const (
|
||||
runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
|
||||
runeSelf = 0x80 // characters below runeSelf are represented as themselves in a single byte.
|
||||
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
|
||||
)
|
||||
|
||||
// Code points in the surrogate range are not valid for UTF-8.
|
||||
const (
|
||||
surrogateMin = 0xD800
|
||||
surrogateMax = 0xDFFF
|
||||
)
|
||||
|
||||
const (
|
||||
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
|
||||
|
||||
// The default lowest and highest continuation byte.
|
||||
locb = 0x80 // 1000 0000
|
||||
hicb = 0xBF // 1011 1111
|
||||
)
|
||||
|
||||
// countrunes returns the number of runes in s.
|
||||
// func countrunes(s string) int {
|
||||
// n := 0
|
||||
// for range s {
|
||||
// n++
|
||||
// }
|
||||
// return n
|
||||
// }
|
||||
|
||||
// decoderune returns the non-ASCII rune at the start of
|
||||
// s[k:] and the index after the rune in s.
|
||||
//
|
||||
// decoderune assumes that caller has checked that
|
||||
// the to be decoded rune is a non-ASCII rune.
|
||||
//
|
||||
// If the string appears to be incomplete or decoding problems
|
||||
// are encountered (runeerror, k + 1) is returned to ensure
|
||||
// progress when decoderune is used to iterate over a string.
|
||||
func decoderune(s string, k int) (r rune, pos int) {
|
||||
pos = k
|
||||
|
||||
if k >= len(s) {
|
||||
return runeError, k + 1
|
||||
}
|
||||
|
||||
s = s[k:]
|
||||
|
||||
switch {
|
||||
case t2 <= s[0] && s[0] < t3:
|
||||
// 0080-07FF two byte sequence
|
||||
if len(s) > 1 && (locb <= s[1] && s[1] <= hicb) {
|
||||
r = rune(s[0]&mask2)<<6 | rune(s[1]&maskx)
|
||||
pos += 2
|
||||
if rune1Max < r {
|
||||
return
|
||||
}
|
||||
}
|
||||
case t3 <= s[0] && s[0] < t4:
|
||||
// 0800-FFFF three byte sequence
|
||||
if len(s) > 2 && (locb <= s[1] && s[1] <= hicb) && (locb <= s[2] && s[2] <= hicb) {
|
||||
r = rune(s[0]&mask3)<<12 | rune(s[1]&maskx)<<6 | rune(s[2]&maskx)
|
||||
pos += 3
|
||||
if rune2Max < r && !(surrogateMin <= r && r <= surrogateMax) {
|
||||
return
|
||||
}
|
||||
}
|
||||
case t4 <= s[0] && s[0] < t5:
|
||||
// 10000-1FFFFF four byte sequence
|
||||
if len(s) > 3 && (locb <= s[1] && s[1] <= hicb) && (locb <= s[2] && s[2] <= hicb) && (locb <= s[3] && s[3] <= hicb) {
|
||||
r = rune(s[0]&mask4)<<18 | rune(s[1]&maskx)<<12 | rune(s[2]&maskx)<<6 | rune(s[3]&maskx)
|
||||
pos += 4
|
||||
if rune3Max < r && r <= maxRune {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return runeError, k + 1
|
||||
}
|
||||
|
||||
// encoderune writes into p (which must be large enough) the UTF-8 encoding of the rune.
|
||||
// It returns the number of bytes written.
|
||||
func encoderune(p []byte, r rune) int {
|
||||
// Negative values are erroneous. Making it unsigned addresses the problem.
|
||||
switch i := uint32(r); {
|
||||
case i <= rune1Max:
|
||||
p[0] = byte(r)
|
||||
return 1
|
||||
case i <= rune2Max:
|
||||
_ = p[1] // eliminate bounds checks
|
||||
p[0] = t2 | byte(r>>6)
|
||||
p[1] = tx | byte(r)&maskx
|
||||
return 2
|
||||
case i > maxRune, surrogateMin <= i && i <= surrogateMax:
|
||||
r = runeError
|
||||
fallthrough
|
||||
case i <= rune3Max:
|
||||
_ = p[2] // eliminate bounds checks
|
||||
p[0] = t3 | byte(r>>12)
|
||||
p[1] = tx | byte(r>>6)&maskx
|
||||
p[2] = tx | byte(r)&maskx
|
||||
return 3
|
||||
default:
|
||||
_ = p[3] // eliminate bounds checks
|
||||
p[0] = t4 | byte(r>>18)
|
||||
p[1] = tx | byte(r>>12)&maskx
|
||||
p[2] = tx | byte(r>>6)&maskx
|
||||
p[3] = tx | byte(r)&maskx
|
||||
return 4
|
||||
}
|
||||
}
|
||||
@@ -70,4 +70,119 @@ func NewStringSlice(base String, i, j int) String {
|
||||
return String{nil, 0}
|
||||
}
|
||||
|
||||
type StringIter struct {
|
||||
s string
|
||||
pos int
|
||||
}
|
||||
|
||||
func NewStringIter(s string) *StringIter {
|
||||
return &StringIter{s, 0}
|
||||
}
|
||||
|
||||
func StringIterNext(it *StringIter) (ok bool, k int, v rune) {
|
||||
if it.pos >= len(it.s) {
|
||||
return false, 0, 0
|
||||
}
|
||||
k = it.pos
|
||||
if c := it.s[it.pos]; c < runeSelf {
|
||||
it.pos++
|
||||
v = rune(c)
|
||||
} else {
|
||||
v, it.pos = decoderune(it.s, it.pos)
|
||||
}
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
|
||||
func StringToBytes(s String) []byte {
|
||||
if s.len == 0 {
|
||||
return nil
|
||||
}
|
||||
data := make([]byte, s.len)
|
||||
c.Memcpy(unsafe.Pointer(&data[0]), s.data, uintptr(s.len))
|
||||
return data
|
||||
}
|
||||
|
||||
func StringToRunes(s string) []rune {
|
||||
if len(s) == 0 {
|
||||
return nil
|
||||
}
|
||||
data := make([]rune, len(s))
|
||||
var index uint
|
||||
for i := 0; i < len(s); {
|
||||
if c := s[i]; c < runeSelf {
|
||||
data[index] = rune(c)
|
||||
i++
|
||||
} else {
|
||||
data[index], i = decoderune(s, i)
|
||||
}
|
||||
index++
|
||||
}
|
||||
return data[:index:index]
|
||||
}
|
||||
|
||||
func StringFromBytes(b Slice) (s String) {
|
||||
if b.len == 0 {
|
||||
return
|
||||
}
|
||||
s.len = b.len
|
||||
s.data = AllocU(uintptr(s.len))
|
||||
c.Memcpy(s.data, b.data, uintptr(b.len))
|
||||
return
|
||||
}
|
||||
|
||||
func StringFromRunes(rs []rune) (s String) {
|
||||
if len(rs) == 0 {
|
||||
return
|
||||
}
|
||||
data := make([]byte, len(rs)*4)
|
||||
var index int
|
||||
for _, r := range rs {
|
||||
n := encoderune(data[index:], r)
|
||||
index += n
|
||||
}
|
||||
s.len = index
|
||||
s.data = unsafe.Pointer(&data[0])
|
||||
return
|
||||
}
|
||||
|
||||
func StringFromRune(r rune) (s String) {
|
||||
var buf [4]byte
|
||||
n := encoderune(buf[:], r)
|
||||
s.len = n
|
||||
s.data = unsafe.Pointer(&buf[0])
|
||||
return
|
||||
}
|
||||
|
||||
func StringEqual(x, y String) bool {
|
||||
if x.len != y.len {
|
||||
return false
|
||||
}
|
||||
if x.data != y.data {
|
||||
for i := 0; i < x.len; i++ {
|
||||
if *(*byte)(c.Advance(x.data, i)) != *(*byte)(c.Advance(y.data, i)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func StringLess(x, y String) bool {
|
||||
n := x.len
|
||||
if n > y.len {
|
||||
n = y.len
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
ix := *(*byte)(c.Advance(x.data, i))
|
||||
iy := *(*byte)(c.Advance(y.data, i))
|
||||
if ix < iy {
|
||||
return true
|
||||
} else if ix > iy {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return x.len < y.len
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user