string_ascii.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package goja
  2. import (
  3. "hash/maphash"
  4. "io"
  5. "math"
  6. "math/big"
  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. type asciiUtf16Reader struct {
  28. s asciiString
  29. pos int
  30. }
  31. func (rr *asciiUtf16Reader) readChar() (c uint16, err error) {
  32. if rr.pos < len(rr.s) {
  33. c = uint16(rr.s[rr.pos])
  34. rr.pos++
  35. } else {
  36. err = io.EOF
  37. }
  38. return
  39. }
  40. func (rr *asciiUtf16Reader) ReadRune() (r rune, size int, err error) {
  41. if rr.pos < len(rr.s) {
  42. r = rune(rr.s[rr.pos])
  43. rr.pos++
  44. size = 1
  45. } else {
  46. err = io.EOF
  47. }
  48. return
  49. }
  50. func (s asciiString) Reader() io.RuneReader {
  51. return &asciiRuneReader{
  52. s: s,
  53. }
  54. }
  55. func (s asciiString) utf16Reader() utf16Reader {
  56. return &asciiUtf16Reader{
  57. s: s,
  58. }
  59. }
  60. func (s asciiString) utf16RuneReader() io.RuneReader {
  61. return &asciiUtf16Reader{
  62. s: s,
  63. }
  64. }
  65. func (s asciiString) utf16Runes() []rune {
  66. runes := make([]rune, len(s))
  67. for i := 0; i < len(s); i++ {
  68. runes[i] = rune(s[i])
  69. }
  70. return runes
  71. }
  72. // ss must be trimmed
  73. func stringToInt(ss string) (int64, error) {
  74. if ss == "" {
  75. return 0, nil
  76. }
  77. if ss == "-0" {
  78. return 0, strconv.ErrSyntax
  79. }
  80. if len(ss) > 2 {
  81. switch ss[:2] {
  82. case "0x", "0X":
  83. return strconv.ParseInt(ss[2:], 16, 64)
  84. case "0b", "0B":
  85. return strconv.ParseInt(ss[2:], 2, 64)
  86. case "0o", "0O":
  87. return strconv.ParseInt(ss[2:], 8, 64)
  88. }
  89. }
  90. return strconv.ParseInt(ss, 10, 64)
  91. }
  92. func (s asciiString) _toInt(trimmed string) (int64, error) {
  93. return stringToInt(trimmed)
  94. }
  95. func isRangeErr(err error) bool {
  96. if err, ok := err.(*strconv.NumError); ok {
  97. return err.Err == strconv.ErrRange
  98. }
  99. return false
  100. }
  101. func (s asciiString) _toFloat(trimmed string) (float64, error) {
  102. if trimmed == "" {
  103. return 0, nil
  104. }
  105. if trimmed == "-0" {
  106. var f float64
  107. return -f, nil
  108. }
  109. // Go allows underscores in numbers, when parsed as floats, but ECMAScript expect them to be interpreted as NaN.
  110. if strings.ContainsRune(trimmed, '_') {
  111. return 0, strconv.ErrSyntax
  112. }
  113. // Hexadecimal floats are not supported by ECMAScript.
  114. if len(trimmed) >= 2 {
  115. var prefix string
  116. if trimmed[0] == '-' || trimmed[0] == '+' {
  117. prefix = trimmed[1:]
  118. } else {
  119. prefix = trimmed
  120. }
  121. if len(prefix) >= 2 && prefix[0] == '0' && (prefix[1] == 'x' || prefix[1] == 'X') {
  122. return 0, strconv.ErrSyntax
  123. }
  124. }
  125. f, err := strconv.ParseFloat(trimmed, 64)
  126. if err == nil && math.IsInf(f, 0) {
  127. ss := strings.ToLower(trimmed)
  128. if strings.HasPrefix(ss, "inf") || strings.HasPrefix(ss, "-inf") || strings.HasPrefix(ss, "+inf") {
  129. // We handle "Infinity" separately, prevent from being parsed as Infinity due to strconv.ParseFloat() permissive syntax
  130. return 0, strconv.ErrSyntax
  131. }
  132. }
  133. if isRangeErr(err) {
  134. err = nil
  135. }
  136. return f, err
  137. }
  138. func (s asciiString) ToInteger() int64 {
  139. ss := strings.TrimSpace(string(s))
  140. if ss == "" {
  141. return 0
  142. }
  143. if ss == "Infinity" || ss == "+Infinity" {
  144. return math.MaxInt64
  145. }
  146. if ss == "-Infinity" {
  147. return math.MinInt64
  148. }
  149. i, err := s._toInt(ss)
  150. if err != nil {
  151. f, err := s._toFloat(ss)
  152. if err == nil {
  153. return int64(f)
  154. }
  155. }
  156. return i
  157. }
  158. func (s asciiString) toString() String {
  159. return s
  160. }
  161. func (s asciiString) ToString() Value {
  162. return s
  163. }
  164. func (s asciiString) String() string {
  165. return string(s)
  166. }
  167. func (s asciiString) ToFloat() float64 {
  168. ss := strings.TrimSpace(string(s))
  169. if ss == "" {
  170. return 0
  171. }
  172. if ss == "Infinity" || ss == "+Infinity" {
  173. return math.Inf(1)
  174. }
  175. if ss == "-Infinity" {
  176. return math.Inf(-1)
  177. }
  178. f, err := s._toFloat(ss)
  179. if err != nil {
  180. i, err := s._toInt(ss)
  181. if err == nil {
  182. return float64(i)
  183. }
  184. f = math.NaN()
  185. }
  186. return f
  187. }
  188. func (s asciiString) ToBoolean() bool {
  189. return s != ""
  190. }
  191. func (s asciiString) ToNumber() Value {
  192. ss := strings.TrimSpace(string(s))
  193. if ss == "" {
  194. return intToValue(0)
  195. }
  196. if ss == "Infinity" || ss == "+Infinity" {
  197. return _positiveInf
  198. }
  199. if ss == "-Infinity" {
  200. return _negativeInf
  201. }
  202. if i, err := s._toInt(ss); err == nil {
  203. return intToValue(i)
  204. }
  205. if f, err := s._toFloat(ss); err == nil {
  206. return floatToValue(f)
  207. }
  208. return _NaN
  209. }
  210. func (s asciiString) ToObject(r *Runtime) *Object {
  211. return r._newString(s, r.getStringPrototype())
  212. }
  213. func (s asciiString) SameAs(other Value) bool {
  214. return s.StrictEquals(other)
  215. }
  216. func (s asciiString) Equals(other Value) bool {
  217. if s.StrictEquals(other) {
  218. return true
  219. }
  220. if o, ok := other.(valueInt); ok {
  221. if o1, e := s._toInt(strings.TrimSpace(string(s))); e == nil {
  222. return o1 == int64(o)
  223. }
  224. return false
  225. }
  226. if o, ok := other.(valueFloat); ok {
  227. return s.ToFloat() == float64(o)
  228. }
  229. if o, ok := other.(valueBool); ok {
  230. if o1, e := s._toFloat(strings.TrimSpace(string(s))); e == nil {
  231. return o1 == o.ToFloat()
  232. }
  233. return false
  234. }
  235. if o, ok := other.(*valueBigInt); ok {
  236. bigInt, err := stringToBigInt(s.toTrimmedUTF8())
  237. if err != nil {
  238. return false
  239. }
  240. return bigInt.Cmp((*big.Int)(o)) == 0
  241. }
  242. if o, ok := other.(*Object); ok {
  243. return s.Equals(o.toPrimitive())
  244. }
  245. return false
  246. }
  247. func (s asciiString) StrictEquals(other Value) bool {
  248. if otherStr, ok := other.(asciiString); ok {
  249. return s == otherStr
  250. }
  251. if otherStr, ok := other.(*importedString); ok {
  252. if otherStr.u == nil {
  253. return string(s) == otherStr.s
  254. }
  255. }
  256. return false
  257. }
  258. func (s asciiString) baseObject(r *Runtime) *Object {
  259. ss := r.getStringSingleton()
  260. ss.value = s
  261. ss.setLength()
  262. return ss.val
  263. }
  264. func (s asciiString) hash(hash *maphash.Hash) uint64 {
  265. _, _ = hash.WriteString(string(s))
  266. h := hash.Sum64()
  267. hash.Reset()
  268. return h
  269. }
  270. func (s asciiString) CharAt(idx int) uint16 {
  271. return uint16(s[idx])
  272. }
  273. func (s asciiString) Length() int {
  274. return len(s)
  275. }
  276. func (s asciiString) Concat(other String) String {
  277. a, u := devirtualizeString(other)
  278. if u != nil {
  279. b := make([]uint16, len(s)+len(u))
  280. b[0] = unistring.BOM
  281. for i := 0; i < len(s); i++ {
  282. b[i+1] = uint16(s[i])
  283. }
  284. copy(b[len(s)+1:], u[1:])
  285. return unicodeString(b)
  286. }
  287. return s + a
  288. }
  289. func (s asciiString) Substring(start, end int) String {
  290. return s[start:end]
  291. }
  292. func (s asciiString) CompareTo(other String) int {
  293. switch other := other.(type) {
  294. case asciiString:
  295. return strings.Compare(string(s), string(other))
  296. case unicodeString:
  297. return strings.Compare(string(s), other.String())
  298. case *importedString:
  299. return strings.Compare(string(s), other.s)
  300. default:
  301. panic(newTypeError("Internal bug: unknown string type: %T", other))
  302. }
  303. }
  304. func (s asciiString) index(substr String, start int) int {
  305. a, u := devirtualizeString(substr)
  306. if u == nil {
  307. if start > len(s) {
  308. return -1
  309. }
  310. p := strings.Index(string(s[start:]), string(a))
  311. if p >= 0 {
  312. return p + start
  313. }
  314. }
  315. return -1
  316. }
  317. func (s asciiString) lastIndex(substr String, pos int) int {
  318. a, u := devirtualizeString(substr)
  319. if u == nil {
  320. end := pos + len(a)
  321. var ss string
  322. if end > len(s) {
  323. ss = string(s)
  324. } else {
  325. ss = string(s[:end])
  326. }
  327. return strings.LastIndex(ss, string(a))
  328. }
  329. return -1
  330. }
  331. func (s asciiString) toLower() String {
  332. return asciiString(strings.ToLower(string(s)))
  333. }
  334. func (s asciiString) toUpper() String {
  335. return asciiString(strings.ToUpper(string(s)))
  336. }
  337. func (s asciiString) toTrimmedUTF8() string {
  338. return strings.TrimSpace(string(s))
  339. }
  340. func (s asciiString) string() unistring.String {
  341. return unistring.String(s)
  342. }
  343. func (s asciiString) Export() interface{} {
  344. return string(s)
  345. }
  346. func (s asciiString) ExportType() reflect.Type {
  347. return reflectTypeString
  348. }