string_ascii.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package goja
  2. import (
  3. "fmt"
  4. "hash/maphash"
  5. "io"
  6. "math"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "github.com/dop251/goja/unistring"
  11. )
  12. type asciiString string
  13. type asciiRuneReader struct {
  14. s asciiString
  15. pos int
  16. }
  17. func (rr *asciiRuneReader) ReadRune() (r rune, size int, err error) {
  18. if rr.pos < len(rr.s) {
  19. r = rune(rr.s[rr.pos])
  20. size = 1
  21. rr.pos++
  22. } else {
  23. err = io.EOF
  24. }
  25. return
  26. }
  27. func (s asciiString) reader(start int) io.RuneReader {
  28. return &asciiRuneReader{
  29. s: s[start:],
  30. }
  31. }
  32. func (s asciiString) utf16Reader(start int) io.RuneReader {
  33. return s.reader(start)
  34. }
  35. func (s asciiString) runes() []rune {
  36. runes := make([]rune, len(s))
  37. for i := 0; i < len(s); i++ {
  38. runes[i] = rune(s[i])
  39. }
  40. return runes
  41. }
  42. func (s asciiString) utf16Runes() []rune {
  43. return s.runes()
  44. }
  45. // ss must be trimmed
  46. func strToInt(ss string) (int64, error) {
  47. if ss == "" {
  48. return 0, nil
  49. }
  50. if ss == "-0" {
  51. return 0, strconv.ErrSyntax
  52. }
  53. if len(ss) > 2 {
  54. switch ss[:2] {
  55. case "0x", "0X":
  56. return strconv.ParseInt(ss[2:], 16, 64)
  57. case "0b", "0B":
  58. return strconv.ParseInt(ss[2:], 2, 64)
  59. case "0o", "0O":
  60. return strconv.ParseInt(ss[2:], 8, 64)
  61. }
  62. }
  63. return strconv.ParseInt(ss, 10, 64)
  64. }
  65. func (s asciiString) _toInt() (int64, error) {
  66. return strToInt(strings.TrimSpace(string(s)))
  67. }
  68. func isRangeErr(err error) bool {
  69. if err, ok := err.(*strconv.NumError); ok {
  70. return err.Err == strconv.ErrRange
  71. }
  72. return false
  73. }
  74. func (s asciiString) _toFloat() (float64, error) {
  75. ss := strings.TrimSpace(string(s))
  76. if ss == "" {
  77. return 0, nil
  78. }
  79. if ss == "-0" {
  80. var f float64
  81. return -f, nil
  82. }
  83. f, err := strconv.ParseFloat(ss, 64)
  84. if isRangeErr(err) {
  85. err = nil
  86. }
  87. return f, err
  88. }
  89. func (s asciiString) ToInteger() int64 {
  90. if s == "" {
  91. return 0
  92. }
  93. if s == "Infinity" || s == "+Infinity" {
  94. return math.MaxInt64
  95. }
  96. if s == "-Infinity" {
  97. return math.MinInt64
  98. }
  99. i, err := s._toInt()
  100. if err != nil {
  101. f, err := s._toFloat()
  102. if err == nil {
  103. return int64(f)
  104. }
  105. }
  106. return i
  107. }
  108. func (s asciiString) toString() valueString {
  109. return s
  110. }
  111. func (s asciiString) ToString() Value {
  112. return s
  113. }
  114. func (s asciiString) String() string {
  115. return string(s)
  116. }
  117. func (s asciiString) ToFloat() float64 {
  118. if s == "" {
  119. return 0
  120. }
  121. if s == "Infinity" || s == "+Infinity" {
  122. return math.Inf(1)
  123. }
  124. if s == "-Infinity" {
  125. return math.Inf(-1)
  126. }
  127. f, err := s._toFloat()
  128. if err != nil {
  129. i, err := s._toInt()
  130. if err == nil {
  131. return float64(i)
  132. }
  133. f = math.NaN()
  134. }
  135. return f
  136. }
  137. func (s asciiString) ToBoolean() bool {
  138. return s != ""
  139. }
  140. func (s asciiString) ToNumber() Value {
  141. if s == "" {
  142. return intToValue(0)
  143. }
  144. if s == "Infinity" || s == "+Infinity" {
  145. return _positiveInf
  146. }
  147. if s == "-Infinity" {
  148. return _negativeInf
  149. }
  150. if i, err := s._toInt(); err == nil {
  151. return intToValue(i)
  152. }
  153. if f, err := s._toFloat(); err == nil {
  154. return floatToValue(f)
  155. }
  156. return _NaN
  157. }
  158. func (s asciiString) ToObject(r *Runtime) *Object {
  159. return r._newString(s, r.global.StringPrototype)
  160. }
  161. func (s asciiString) SameAs(other Value) bool {
  162. if otherStr, ok := other.(asciiString); ok {
  163. return s == otherStr
  164. }
  165. return false
  166. }
  167. func (s asciiString) Equals(other Value) bool {
  168. if o, ok := other.(asciiString); ok {
  169. return s == o
  170. }
  171. if o, ok := other.(valueInt); ok {
  172. if o1, e := s._toInt(); e == nil {
  173. return o1 == int64(o)
  174. }
  175. return false
  176. }
  177. if o, ok := other.(valueFloat); ok {
  178. return s.ToFloat() == float64(o)
  179. }
  180. if o, ok := other.(valueBool); ok {
  181. if o1, e := s._toFloat(); e == nil {
  182. return o1 == o.ToFloat()
  183. }
  184. return false
  185. }
  186. if o, ok := other.(*Object); ok {
  187. return s.Equals(o.toPrimitive())
  188. }
  189. return false
  190. }
  191. func (s asciiString) StrictEquals(other Value) bool {
  192. if otherStr, ok := other.(asciiString); ok {
  193. return s == otherStr
  194. }
  195. return false
  196. }
  197. func (s asciiString) baseObject(r *Runtime) *Object {
  198. ss := r.stringSingleton
  199. ss.value = s
  200. ss.setLength()
  201. return ss.val
  202. }
  203. func (s asciiString) hash(hash *maphash.Hash) uint64 {
  204. _, _ = hash.WriteString(string(s))
  205. h := hash.Sum64()
  206. hash.Reset()
  207. return h
  208. }
  209. func (s asciiString) charAt(idx int) rune {
  210. return rune(s[idx])
  211. }
  212. func (s asciiString) length() int {
  213. return len(s)
  214. }
  215. func (s asciiString) concat(other valueString) valueString {
  216. switch other := other.(type) {
  217. case asciiString:
  218. b := make([]byte, len(s)+len(other))
  219. copy(b, s)
  220. copy(b[len(s):], other)
  221. return asciiString(b)
  222. case unicodeString:
  223. b := make([]uint16, len(s)+len(other))
  224. b[0] = unistring.BOM
  225. for i := 0; i < len(s); i++ {
  226. b[i+1] = uint16(s[i])
  227. }
  228. copy(b[len(s)+1:], other[1:])
  229. return unicodeString(b)
  230. default:
  231. panic(fmt.Errorf("unknown string type: %T", other))
  232. }
  233. }
  234. func (s asciiString) substring(start, end int) valueString {
  235. return s[start:end]
  236. }
  237. func (s asciiString) compareTo(other valueString) int {
  238. switch other := other.(type) {
  239. case asciiString:
  240. return strings.Compare(string(s), string(other))
  241. case unicodeString:
  242. return strings.Compare(string(s), other.String())
  243. default:
  244. panic(fmt.Errorf("unknown string type: %T", other))
  245. }
  246. }
  247. func (s asciiString) index(substr valueString, start int) int {
  248. if substr, ok := substr.(asciiString); ok {
  249. p := strings.Index(string(s[start:]), string(substr))
  250. if p >= 0 {
  251. return p + start
  252. }
  253. }
  254. return -1
  255. }
  256. func (s asciiString) lastIndex(substr valueString, pos int) int {
  257. if substr, ok := substr.(asciiString); ok {
  258. end := pos + len(substr)
  259. var ss string
  260. if end > len(s) {
  261. ss = string(s)
  262. } else {
  263. ss = string(s[:end])
  264. }
  265. return strings.LastIndex(ss, string(substr))
  266. }
  267. return -1
  268. }
  269. func (s asciiString) toLower() valueString {
  270. return asciiString(strings.ToLower(string(s)))
  271. }
  272. func (s asciiString) toUpper() valueString {
  273. return asciiString(strings.ToUpper(string(s)))
  274. }
  275. func (s asciiString) toTrimmedUTF8() string {
  276. return strings.TrimSpace(string(s))
  277. }
  278. func (s asciiString) string() unistring.String {
  279. return unistring.String(s)
  280. }
  281. func (s asciiString) Export() interface{} {
  282. return string(s)
  283. }
  284. func (s asciiString) ExportType() reflect.Type {
  285. return reflectTypeString
  286. }