summaryrefslogtreecommitdiff
path: root/src/pkg/exp/norm/input.go
blob: 9c564d67718b5f7a6be2b374cce59cae2b415c75 (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
// 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, max int) int
	skipNonStarter(p int) int
	appendSlice(buf []byte, s, e int) []byte
	copySlice(buf []byte, s, e int)
	charinfoNFC(p int) (uint16, int)
	charinfoNFKC(p int) (uint16, int)
	hangul(p int) rune
}

type inputString string

func (s inputString) skipASCII(p, max int) int {
	for ; p < max && 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) charinfoNFC(p int) (uint16, int) {
	return nfcTrie.lookupString(string(s[p:]))
}

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

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

type inputBytes []byte

func (s inputBytes) skipASCII(p, max int) int {
	for ; p < max && 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) charinfoNFC(p int) (uint16, int) {
	return nfcTrie.lookup(s[p:])
}

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

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