string_ascii.go 5.8 KB

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