spec_compat.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // +build !amd64,!arm64 go1.25 !go1.16 arm64,!go1.20
  2. /**
  3. * Copyright 2024 ByteDance Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package alg
  18. import (
  19. _ "unsafe"
  20. "unicode/utf8"
  21. "strconv"
  22. "bytes"
  23. "encoding/json"
  24. "github.com/bytedance/sonic/internal/rt"
  25. )
  26. // Valid validates json and returns first non-blank character position,
  27. // if it is only one valid json value.
  28. // Otherwise returns invalid character position using start.
  29. //
  30. // Note: it does not check for the invalid UTF-8 characters.
  31. func Valid(data []byte) (ok bool, start int) {
  32. ok = json.Valid(data)
  33. return ok, 0
  34. }
  35. var typeByte = rt.UnpackEface(byte(0)).Type
  36. func Quote(e []byte, s string, double bool) []byte {
  37. if len(s) == 0 {
  38. if double {
  39. return append(e, `"\"\""`...)
  40. }
  41. return append(e, `""`...)
  42. }
  43. b := e
  44. ss := len(e)
  45. e = append(e, '"')
  46. start := 0
  47. for i := 0; i < len(s); {
  48. if b := s[i]; b < utf8.RuneSelf {
  49. if rt.SafeSet[b] {
  50. i++
  51. continue
  52. }
  53. if start < i {
  54. e = append(e, s[start:i]...)
  55. }
  56. e = append(e, '\\')
  57. switch b {
  58. case '\\', '"':
  59. e = append(e, b)
  60. case '\n':
  61. e = append(e, 'n')
  62. case '\r':
  63. e = append(e, 'r')
  64. case '\t':
  65. e = append(e, 't')
  66. default:
  67. // This encodes bytes < 0x20 except for \t, \n and \r.
  68. // If escapeHTML is set, it also escapes <, >, and &
  69. // because they can lead to security holes when
  70. // user-controlled strings are rendered into JSON
  71. // and served to some browsers.
  72. e = append(e, `u00`...)
  73. e = append(e, rt.Hex[b>>4])
  74. e = append(e, rt.Hex[b&0xF])
  75. }
  76. i++
  77. start = i
  78. continue
  79. }
  80. c, size := utf8.DecodeRuneInString(s[i:])
  81. // if correct && c == utf8.RuneError && size == 1 {
  82. // if start < i {
  83. // e = append(e, s[start:i]...)
  84. // }
  85. // e = append(e, `\ufffd`...)
  86. // i += size
  87. // start = i
  88. // continue
  89. // }
  90. if c == '\u2028' || c == '\u2029' {
  91. if start < i {
  92. e = append(e, s[start:i]...)
  93. }
  94. e = append(e, `\u202`...)
  95. e = append(e, rt.Hex[c&0xF])
  96. i += size
  97. start = i
  98. continue
  99. }
  100. i += size
  101. }
  102. if start < len(s) {
  103. e = append(e, s[start:]...)
  104. }
  105. e = append(e, '"')
  106. if double {
  107. return strconv.AppendQuote(b, string(e[ss:]))
  108. } else {
  109. return e
  110. }
  111. }
  112. func HtmlEscape(dst []byte, src []byte) []byte {
  113. buf := bytes.NewBuffer(dst)
  114. json.HTMLEscape(buf, src)
  115. return buf.Bytes()
  116. }
  117. func F64toa(buf []byte, v float64) ([]byte) {
  118. bs := bytes.NewBuffer(buf)
  119. _ = json.NewEncoder(bs).Encode(v)
  120. return bs.Bytes()
  121. }
  122. func F32toa(buf []byte, v float32) ([]byte) {
  123. bs := bytes.NewBuffer(buf)
  124. _ = json.NewEncoder(bs).Encode(v)
  125. return bs.Bytes()
  126. }
  127. func I64toa(buf []byte, v int64) ([]byte) {
  128. return strconv.AppendInt(buf, int64(v), 10)
  129. }
  130. func U64toa(buf []byte, v uint64) ([]byte) {
  131. return strconv.AppendUint(buf, v, 10)
  132. }