summaryrefslogtreecommitdiff
path: root/libgo/go/exp/norm/input.go
blob: 42e6f1b79471f8c590edc025e80fd8642f6d9d6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2011 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 norm

import "unicode/utf8"

type input interface {
	skipASCII(p int) int
	skipNonStarter(p int) int
	appendSlice(buf []byte, s, e int) []byte
	copySlice(buf []byte, s, e int)
	charinfo(p int) (uint16, int)
	decomposeNFC(p int) uint16
	decomposeNFKC(p int) uint16
	hangul(p int) uint32
}

type inputString string

func (s inputString) skipASCII(p int) int {
	for ; p < len(s) && s[p] < utf8.RuneSelf; p++ {
	}
	return p
}

func (s inputString) skipNonStarter(p int) int {
	for ; p < len(s) && !utf8.RuneStart(s[p]); p++ {
	}
	return p
}

func (s inputString) appendSlice(buf []byte, b, e int) []byte {
	for i := b; i < e; i++ {
		buf = append(buf, s[i])
	}
	return buf
}

func (s inputString) copySlice(buf []byte, b, e int) {
	copy(buf, s[b:e])
}

func (s inputString) charinfo(p int) (uint16, int) {
	return charInfoTrie.lookupString(string(s[p:]))
}

func (s inputString) decomposeNFC(p int) uint16 {
	return nfcDecompTrie.lookupStringUnsafe(string(s[p:]))
}

func (s inputString) decomposeNFKC(p int) uint16 {
	return nfkcDecompTrie.lookupStringUnsafe(string(s[p:]))
}

func (s inputString) hangul(p int) uint32 {
	if !isHangulString(string(s[p:])) {
		return 0
	}
	rune, _ := utf8.DecodeRuneInString(string(s[p:]))
	return uint32(rune)
}

type inputBytes []byte

func (s inputBytes) skipASCII(p int) int {
	for ; p < len(s) && s[p] < utf8.RuneSelf; p++ {
	}
	return p
}

func (s inputBytes) skipNonStarter(p int) int {
	for ; p < len(s) && !utf8.RuneStart(s[p]); p++ {
	}
	return p
}

func (s inputBytes) appendSlice(buf []byte, b, e int) []byte {
	return append(buf, s[b:e]...)
}

func (s inputBytes) copySlice(buf []byte, b, e int) {
	copy(buf, s[b:e])
}

func (s inputBytes) charinfo(p int) (uint16, int) {
	return charInfoTrie.lookup(s[p:])
}

func (s inputBytes) decomposeNFC(p int) uint16 {
	return nfcDecompTrie.lookupUnsafe(s[p:])
}

func (s inputBytes) decomposeNFKC(p int) uint16 {
	return nfkcDecompTrie.lookupUnsafe(s[p:])
}

func (s inputBytes) hangul(p int) uint32 {
	if !isHangul(s[p:]) {
		return 0
	}
	rune, _ := utf8.DecodeRune(s[p:])
	return uint32(rune)
}