string_ascii.go 5.7 KB

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