string_ascii.go 6.0 KB

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