input.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. package norm
  5. import "unicode/utf8"
  6. type input struct {
  7. str string
  8. bytes []byte
  9. }
  10. func inputBytes(str []byte) input {
  11. return input{bytes: str}
  12. }
  13. func inputString(str string) input {
  14. return input{str: str}
  15. }
  16. func (in *input) setBytes(str []byte) {
  17. in.str = ""
  18. in.bytes = str
  19. }
  20. func (in *input) setString(str string) {
  21. in.str = str
  22. in.bytes = nil
  23. }
  24. func (in *input) _byte(p int) byte {
  25. if in.bytes == nil {
  26. return in.str[p]
  27. }
  28. return in.bytes[p]
  29. }
  30. func (in *input) skipASCII(p, max int) int {
  31. if in.bytes == nil {
  32. for ; p < max && in.str[p] < utf8.RuneSelf; p++ {
  33. }
  34. } else {
  35. for ; p < max && in.bytes[p] < utf8.RuneSelf; p++ {
  36. }
  37. }
  38. return p
  39. }
  40. func (in *input) skipContinuationBytes(p int) int {
  41. if in.bytes == nil {
  42. for ; p < len(in.str) && !utf8.RuneStart(in.str[p]); p++ {
  43. }
  44. } else {
  45. for ; p < len(in.bytes) && !utf8.RuneStart(in.bytes[p]); p++ {
  46. }
  47. }
  48. return p
  49. }
  50. func (in *input) appendSlice(buf []byte, b, e int) []byte {
  51. if in.bytes != nil {
  52. return append(buf, in.bytes[b:e]...)
  53. }
  54. for i := b; i < e; i++ {
  55. buf = append(buf, in.str[i])
  56. }
  57. return buf
  58. }
  59. func (in *input) copySlice(buf []byte, b, e int) int {
  60. if in.bytes == nil {
  61. return copy(buf, in.str[b:e])
  62. }
  63. return copy(buf, in.bytes[b:e])
  64. }
  65. func (in *input) charinfoNFC(p int) (uint16, int) {
  66. if in.bytes == nil {
  67. return nfcData.lookupString(in.str[p:])
  68. }
  69. return nfcData.lookup(in.bytes[p:])
  70. }
  71. func (in *input) charinfoNFKC(p int) (uint16, int) {
  72. if in.bytes == nil {
  73. return nfkcData.lookupString(in.str[p:])
  74. }
  75. return nfkcData.lookup(in.bytes[p:])
  76. }
  77. func (in *input) hangul(p int) (r rune) {
  78. var size int
  79. if in.bytes == nil {
  80. if !isHangulString(in.str[p:]) {
  81. return 0
  82. }
  83. r, size = utf8.DecodeRuneInString(in.str[p:])
  84. } else {
  85. if !isHangul(in.bytes[p:]) {
  86. return 0
  87. }
  88. r, size = utf8.DecodeRune(in.bytes[p:])
  89. }
  90. if size != hangulUTF8Size {
  91. return 0
  92. }
  93. return r
  94. }