string_ascii.go 5.7 KB

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