string_ascii.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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() (int64, error) {
  93. return stringToInt(strings.TrimSpace(string(s)))
  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() (float64, error) {
  102. ss := strings.ToLower(strings.TrimSpace(string(s)))
  103. if ss == "" {
  104. return 0, nil
  105. }
  106. if ss == "-0" {
  107. var f float64
  108. return -f, nil
  109. }
  110. f, err := strconv.ParseFloat(ss, 64)
  111. if err == nil && math.IsInf(f, 0) {
  112. if strings.HasPrefix(ss, "inf") || strings.HasPrefix(ss, "-inf") || strings.HasPrefix(ss, "+inf") {
  113. // We handle "Infinity" separately, prevent from being parsed as Infinity due to strconv.ParseFloat() permissive syntax
  114. return 0, strconv.ErrSyntax
  115. }
  116. }
  117. if isRangeErr(err) {
  118. err = nil
  119. }
  120. return f, err
  121. }
  122. func (s asciiString) ToInteger() int64 {
  123. if s == "" {
  124. return 0
  125. }
  126. if s == "Infinity" || s == "+Infinity" {
  127. return math.MaxInt64
  128. }
  129. if s == "-Infinity" {
  130. return math.MinInt64
  131. }
  132. i, err := s._toInt()
  133. if err != nil {
  134. f, err := s._toFloat()
  135. if err == nil {
  136. return int64(f)
  137. }
  138. }
  139. return i
  140. }
  141. func (s asciiString) toString() String {
  142. return s
  143. }
  144. func (s asciiString) ToString() Value {
  145. return s
  146. }
  147. func (s asciiString) String() string {
  148. return string(s)
  149. }
  150. func (s asciiString) ToFloat() float64 {
  151. if s == "" {
  152. return 0
  153. }
  154. if s == "Infinity" || s == "+Infinity" {
  155. return math.Inf(1)
  156. }
  157. if s == "-Infinity" {
  158. return math.Inf(-1)
  159. }
  160. f, err := s._toFloat()
  161. if err != nil {
  162. i, err := s._toInt()
  163. if err == nil {
  164. return float64(i)
  165. }
  166. f = math.NaN()
  167. }
  168. return f
  169. }
  170. func (s asciiString) ToBoolean() bool {
  171. return s != ""
  172. }
  173. func (s asciiString) ToNumber() Value {
  174. if s == "" {
  175. return intToValue(0)
  176. }
  177. if s == "Infinity" || s == "+Infinity" {
  178. return _positiveInf
  179. }
  180. if s == "-Infinity" {
  181. return _negativeInf
  182. }
  183. if i, err := s._toInt(); err == nil {
  184. return intToValue(i)
  185. }
  186. if f, err := s._toFloat(); err == nil {
  187. return floatToValue(f)
  188. }
  189. return _NaN
  190. }
  191. func (s asciiString) ToObject(r *Runtime) *Object {
  192. return r._newString(s, r.getStringPrototype())
  193. }
  194. func (s asciiString) SameAs(other Value) bool {
  195. return s.StrictEquals(other)
  196. }
  197. func (s asciiString) Equals(other Value) bool {
  198. if s.StrictEquals(other) {
  199. return true
  200. }
  201. if o, ok := other.(valueInt); ok {
  202. if o1, e := s._toInt(); e == nil {
  203. return o1 == int64(o)
  204. }
  205. return false
  206. }
  207. if o, ok := other.(valueFloat); ok {
  208. return s.ToFloat() == float64(o)
  209. }
  210. if o, ok := other.(valueBool); ok {
  211. if o1, e := s._toFloat(); e == nil {
  212. return o1 == o.ToFloat()
  213. }
  214. return false
  215. }
  216. if o, ok := other.(*valueBigInt); ok {
  217. bigInt, err := stringToBigInt(s.toTrimmedUTF8())
  218. if err != nil {
  219. return false
  220. }
  221. return bigInt.Cmp((*big.Int)(o)) == 0
  222. }
  223. if o, ok := other.(*Object); ok {
  224. return s.Equals(o.toPrimitive())
  225. }
  226. return false
  227. }
  228. func (s asciiString) StrictEquals(other Value) bool {
  229. if otherStr, ok := other.(asciiString); ok {
  230. return s == otherStr
  231. }
  232. if otherStr, ok := other.(*importedString); ok {
  233. if otherStr.u == nil {
  234. return string(s) == otherStr.s
  235. }
  236. }
  237. return false
  238. }
  239. func (s asciiString) baseObject(r *Runtime) *Object {
  240. ss := r.getStringSingleton()
  241. ss.value = s
  242. ss.setLength()
  243. return ss.val
  244. }
  245. func (s asciiString) hash(hash *maphash.Hash) uint64 {
  246. _, _ = hash.WriteString(string(s))
  247. h := hash.Sum64()
  248. hash.Reset()
  249. return h
  250. }
  251. func (s asciiString) CharAt(idx int) uint16 {
  252. return uint16(s[idx])
  253. }
  254. func (s asciiString) Length() int {
  255. return len(s)
  256. }
  257. func (s asciiString) Concat(other String) String {
  258. a, u := devirtualizeString(other)
  259. if u != nil {
  260. b := make([]uint16, len(s)+len(u))
  261. b[0] = unistring.BOM
  262. for i := 0; i < len(s); i++ {
  263. b[i+1] = uint16(s[i])
  264. }
  265. copy(b[len(s)+1:], u[1:])
  266. return unicodeString(b)
  267. }
  268. return s + a
  269. }
  270. func (s asciiString) Substring(start, end int) String {
  271. return s[start:end]
  272. }
  273. func (s asciiString) CompareTo(other String) int {
  274. switch other := other.(type) {
  275. case asciiString:
  276. return strings.Compare(string(s), string(other))
  277. case unicodeString:
  278. return strings.Compare(string(s), other.String())
  279. case *importedString:
  280. return strings.Compare(string(s), other.s)
  281. default:
  282. panic(newTypeError("Internal bug: unknown string type: %T", other))
  283. }
  284. }
  285. func (s asciiString) index(substr String, start int) int {
  286. a, u := devirtualizeString(substr)
  287. if u == nil {
  288. if start > len(s) {
  289. return -1
  290. }
  291. p := strings.Index(string(s[start:]), string(a))
  292. if p >= 0 {
  293. return p + start
  294. }
  295. }
  296. return -1
  297. }
  298. func (s asciiString) lastIndex(substr String, pos int) int {
  299. a, u := devirtualizeString(substr)
  300. if u == nil {
  301. end := pos + len(a)
  302. var ss string
  303. if end > len(s) {
  304. ss = string(s)
  305. } else {
  306. ss = string(s[:end])
  307. }
  308. return strings.LastIndex(ss, string(a))
  309. }
  310. return -1
  311. }
  312. func (s asciiString) toLower() String {
  313. return asciiString(strings.ToLower(string(s)))
  314. }
  315. func (s asciiString) toUpper() String {
  316. return asciiString(strings.ToUpper(string(s)))
  317. }
  318. func (s asciiString) toTrimmedUTF8() string {
  319. return strings.TrimSpace(string(s))
  320. }
  321. func (s asciiString) string() unistring.String {
  322. return unistring.String(s)
  323. }
  324. func (s asciiString) Export() interface{} {
  325. return string(s)
  326. }
  327. func (s asciiString) ExportType() reflect.Type {
  328. return reflectTypeString
  329. }