| 1 |
// 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 |
|
| 5 |
package norm |
| 6 |
|
| 7 |
import "unicode/utf8" |
| 8 |
|
| 9 |
type input struct { |
| 10 |
str string |
| 11 |
bytes []byte |
| 12 |
} |
| 13 |
|
| 14 |
func inputBytes(str []byte) input { |
| 15 |
return input{bytes: str} |
| 16 |
} |
| 17 |
|
| 18 |
func inputString(str string) input { |
| 19 |
return input{str: str} |
| 20 |
} |
| 21 |
|
| 22 |
func (in *input) setBytes(str []byte) { |
| 23 |
in.str = "" |
| 24 |
in.bytes = str |
| 25 |
} |
| 26 |
|
| 27 |
func (in *input) setString(str string) { |
| 28 |
in.str = str |
| 29 |
in.bytes = nil |
| 30 |
} |
| 31 |
|
| 32 |
func (in *input) _byte(p int) byte { |
| 33 |
if in.bytes == nil { |
| 34 |
return in.str[p] |
| 35 |
} |
| 36 |
return in.bytes[p] |
| 37 |
} |
| 38 |
|
| 39 |
func (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 |
|
| 50 |
func (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 |
|
| 61 |
func (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 |
|
| 71 |
func (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 |
|
| 78 |
func (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 |
|
| 85 |
func (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 |
|
| 92 |
func (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 |
} |