string_ascii.go 5.7 KB

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