string_ascii.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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) String() string {
  100. return string(s)
  101. }
  102. func (s asciiString) ToFloat() float64 {
  103. if s == "" {
  104. return 0
  105. }
  106. if s == "Infinity" || s == "+Infinity" {
  107. return math.Inf(1)
  108. }
  109. if s == "-Infinity" {
  110. return math.Inf(-1)
  111. }
  112. f, err := s._toFloat()
  113. if err != nil {
  114. i, err := s._toInt()
  115. if err == nil {
  116. return float64(i)
  117. }
  118. f = math.NaN()
  119. }
  120. return f
  121. }
  122. func (s asciiString) ToBoolean() bool {
  123. return s != ""
  124. }
  125. func (s asciiString) ToNumber() Value {
  126. if s == "" {
  127. return intToValue(0)
  128. }
  129. if s == "Infinity" || s == "+Infinity" {
  130. return _positiveInf
  131. }
  132. if s == "-Infinity" {
  133. return _negativeInf
  134. }
  135. if i, err := s._toInt(); err == nil {
  136. return intToValue(i)
  137. }
  138. if f, err := s._toFloat(); err == nil {
  139. return floatToValue(f)
  140. }
  141. return _NaN
  142. }
  143. func (s asciiString) ToObject(r *Runtime) *Object {
  144. return r._newString(s)
  145. }
  146. func (s asciiString) SameAs(other Value) bool {
  147. if otherStr, ok := other.(asciiString); ok {
  148. return s == otherStr
  149. }
  150. return false
  151. }
  152. func (s asciiString) Equals(other Value) bool {
  153. if o, ok := other.(asciiString); ok {
  154. return s == o
  155. }
  156. if o, ok := other.assertInt(); ok {
  157. if o1, e := s._toInt(); e == nil {
  158. return o1 == o
  159. }
  160. return false
  161. }
  162. if o, ok := other.assertFloat(); ok {
  163. return s.ToFloat() == o
  164. }
  165. if o, ok := other.(valueBool); ok {
  166. if o1, e := s._toFloat(); e == nil {
  167. return o1 == o.ToFloat()
  168. }
  169. return false
  170. }
  171. if o, ok := other.(*Object); ok {
  172. return s.Equals(o.self.toPrimitive())
  173. }
  174. return false
  175. }
  176. func (s asciiString) StrictEquals(other Value) bool {
  177. if otherStr, ok := other.(asciiString); ok {
  178. return s == otherStr
  179. }
  180. return false
  181. }
  182. func (s asciiString) assertInt() (int64, bool) {
  183. return 0, false
  184. }
  185. func (s asciiString) assertFloat() (float64, bool) {
  186. return 0, false
  187. }
  188. func (s asciiString) assertString() (valueString, bool) {
  189. return s, true
  190. }
  191. func (s asciiString) baseObject(r *Runtime) *Object {
  192. ss := r.stringSingleton
  193. ss.value = s
  194. ss.setLength()
  195. return ss.val
  196. }
  197. func (s asciiString) hash() uint64 {
  198. _, _ = mapHasher.WriteString(string(s))
  199. h := mapHasher.Sum64()
  200. mapHasher.Reset()
  201. return h
  202. }
  203. func (s asciiString) charAt(idx int64) rune {
  204. return rune(s[idx])
  205. }
  206. func (s asciiString) length() int64 {
  207. return int64(len(s))
  208. }
  209. func (s asciiString) concat(other valueString) valueString {
  210. switch other := other.(type) {
  211. case asciiString:
  212. b := make([]byte, len(s)+len(other))
  213. copy(b, s)
  214. copy(b[len(s):], other)
  215. return asciiString(b)
  216. //return asciiString(string(s) + string(other))
  217. case unicodeString:
  218. b := make([]uint16, len(s)+len(other))
  219. for i := 0; i < len(s); i++ {
  220. b[i] = uint16(s[i])
  221. }
  222. copy(b[len(s):], other)
  223. return unicodeString(b)
  224. default:
  225. panic(fmt.Errorf("Unknown string type: %T", other))
  226. }
  227. }
  228. func (s asciiString) substring(start, end int64) valueString {
  229. return asciiString(s[start:end])
  230. }
  231. func (s asciiString) compareTo(other valueString) int {
  232. switch other := other.(type) {
  233. case asciiString:
  234. return strings.Compare(string(s), string(other))
  235. case unicodeString:
  236. return strings.Compare(string(s), other.String())
  237. default:
  238. panic(fmt.Errorf("Unknown string type: %T", other))
  239. }
  240. }
  241. func (s asciiString) index(substr valueString, start int64) int64 {
  242. if substr, ok := substr.(asciiString); ok {
  243. p := int64(strings.Index(string(s[start:]), string(substr)))
  244. if p >= 0 {
  245. return p + start
  246. }
  247. }
  248. return -1
  249. }
  250. func (s asciiString) lastIndex(substr valueString, pos int64) int64 {
  251. if substr, ok := substr.(asciiString); ok {
  252. end := pos + int64(len(substr))
  253. var ss string
  254. if end > int64(len(s)) {
  255. ss = string(s)
  256. } else {
  257. ss = string(s[:end])
  258. }
  259. return int64(strings.LastIndex(ss, string(substr)))
  260. }
  261. return -1
  262. }
  263. func (s asciiString) toLower() valueString {
  264. return asciiString(strings.ToLower(string(s)))
  265. }
  266. func (s asciiString) toUpper() valueString {
  267. return asciiString(strings.ToUpper(string(s)))
  268. }
  269. func (s asciiString) toTrimmedUTF8() string {
  270. return strings.TrimSpace(string(s))
  271. }
  272. func (s asciiString) Export() interface{} {
  273. return string(s)
  274. }
  275. func (s asciiString) ExportType() reflect.Type {
  276. return reflectTypeString
  277. }