blob: 479e35bc2585b332a9a8806d49cb198d1f2b8576 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package norm
6
7import "unicode/utf8"
8
9type input struct {
10 str string
11 bytes []byte
12}
13
14func inputBytes(str []byte) input {
15 return input{bytes: str}
16}
17
18func inputString(str string) input {
19 return input{str: str}
20}
21
22func (in *input) setBytes(str []byte) {
23 in.str = ""
24 in.bytes = str
25}
26
27func (in *input) setString(str string) {
28 in.str = str
29 in.bytes = nil
30}
31
32func (in *input) _byte(p int) byte {
33 if in.bytes == nil {
34 return in.str[p]
35 }
36 return in.bytes[p]
37}
38
39func (in *input) skipASCII(p, max int) int {
40 if in.bytes == nil {
41 for ; p < max && in.str[p] < utf8.RuneSelf; p++ {
42 }
43 } else {
44 for ; p < max && in.bytes[p] < utf8.RuneSelf; p++ {
45 }
46 }
47 return p
48}
49
50func (in *input) skipContinuationBytes(p int) int {
51 if in.bytes == nil {
52 for ; p < len(in.str) && !utf8.RuneStart(in.str[p]); p++ {
53 }
54 } else {
55 for ; p < len(in.bytes) && !utf8.RuneStart(in.bytes[p]); p++ {
56 }
57 }
58 return p
59}
60
61func (in *input) appendSlice(buf []byte, b, e int) []byte {
62 if in.bytes != nil {
63 return append(buf, in.bytes[b:e]...)
64 }
65 for i := b; i < e; i++ {
66 buf = append(buf, in.str[i])
67 }
68 return buf
69}
70
71func (in *input) copySlice(buf []byte, b, e int) int {
72 if in.bytes == nil {
73 return copy(buf, in.str[b:e])
74 }
75 return copy(buf, in.bytes[b:e])
76}
77
78func (in *input) charinfoNFC(p int) (uint16, int) {
79 if in.bytes == nil {
80 return nfcData.lookupString(in.str[p:])
81 }
82 return nfcData.lookup(in.bytes[p:])
83}
84
85func (in *input) charinfoNFKC(p int) (uint16, int) {
86 if in.bytes == nil {
87 return nfkcData.lookupString(in.str[p:])
88 }
89 return nfkcData.lookup(in.bytes[p:])
90}
91
92func (in *input) hangul(p int) (r rune) {
93 var size int
94 if in.bytes == nil {
95 if !isHangulString(in.str[p:]) {
96 return 0
97 }
98 r, size = utf8.DecodeRuneInString(in.str[p:])
99 } else {
100 if !isHangul(in.bytes[p:]) {
101 return 0
102 }
103 r, size = utf8.DecodeRune(in.bytes[p:])
104 }
105 if size != hangulUTF8Size {
106 return 0
107 }
108 return r
109}