wire.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2023 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package jsonwire implements stateless functionality for handling JSON text.
  5. package jsonwire
  6. import (
  7. "cmp"
  8. "errors"
  9. "strconv"
  10. "strings"
  11. "unicode"
  12. "unicode/utf16"
  13. "unicode/utf8"
  14. )
  15. // TrimSuffixWhitespace trims JSON from the end of b.
  16. func TrimSuffixWhitespace(b []byte) []byte {
  17. // NOTE: The arguments and logic are kept simple to keep this inlinable.
  18. n := len(b) - 1
  19. for n >= 0 && (b[n] == ' ' || b[n] == '\t' || b[n] == '\r' || b[n] == '\n') {
  20. n--
  21. }
  22. return b[:n+1]
  23. }
  24. // TrimSuffixString trims a valid JSON string at the end of b.
  25. // The behavior is undefined if there is not a valid JSON string present.
  26. func TrimSuffixString(b []byte) []byte {
  27. // NOTE: The arguments and logic are kept simple to keep this inlinable.
  28. if len(b) > 0 && b[len(b)-1] == '"' {
  29. b = b[:len(b)-1]
  30. }
  31. for len(b) >= 2 && !(b[len(b)-1] == '"' && b[len(b)-2] != '\\') {
  32. b = b[:len(b)-1] // trim all characters except an unescaped quote
  33. }
  34. if len(b) > 0 && b[len(b)-1] == '"' {
  35. b = b[:len(b)-1]
  36. }
  37. return b
  38. }
  39. // HasSuffixByte reports whether b ends with c.
  40. func HasSuffixByte(b []byte, c byte) bool {
  41. // NOTE: The arguments and logic are kept simple to keep this inlinable.
  42. return len(b) > 0 && b[len(b)-1] == c
  43. }
  44. // TrimSuffixByte removes c from the end of b if it is present.
  45. func TrimSuffixByte(b []byte, c byte) []byte {
  46. // NOTE: The arguments and logic are kept simple to keep this inlinable.
  47. if len(b) > 0 && b[len(b)-1] == c {
  48. return b[:len(b)-1]
  49. }
  50. return b
  51. }
  52. // QuoteRune quotes the first rune in the input.
  53. func QuoteRune[Bytes ~[]byte | ~string](b Bytes) string {
  54. r, n := utf8.DecodeRuneInString(string(truncateMaxUTF8(b)))
  55. if r == utf8.RuneError && n == 1 {
  56. return `'\x` + strconv.FormatUint(uint64(b[0]), 16) + `'`
  57. }
  58. return strconv.QuoteRune(r)
  59. }
  60. // CompareUTF16 lexicographically compares x to y according
  61. // to the UTF-16 codepoints of the UTF-8 encoded input strings.
  62. // This implements the ordering specified in RFC 8785, section 3.2.3.
  63. func CompareUTF16[Bytes ~[]byte | ~string](x, y Bytes) int {
  64. // NOTE: This is an optimized, mostly allocation-free implementation
  65. // of CompareUTF16Simple in wire_test.go. FuzzCompareUTF16 verifies that the
  66. // two implementations agree on the result of comparing any two strings.
  67. isUTF16Self := func(r rune) bool {
  68. return ('\u0000' <= r && r <= '\uD7FF') || ('\uE000' <= r && r <= '\uFFFF')
  69. }
  70. for {
  71. if len(x) == 0 || len(y) == 0 {
  72. return cmp.Compare(len(x), len(y))
  73. }
  74. // ASCII fast-path.
  75. if x[0] < utf8.RuneSelf || y[0] < utf8.RuneSelf {
  76. if x[0] != y[0] {
  77. return cmp.Compare(x[0], y[0])
  78. }
  79. x, y = x[1:], y[1:]
  80. continue
  81. }
  82. // Decode next pair of runes as UTF-8.
  83. rx, nx := utf8.DecodeRuneInString(string(truncateMaxUTF8(x)))
  84. ry, ny := utf8.DecodeRuneInString(string(truncateMaxUTF8(y)))
  85. selfx := isUTF16Self(rx)
  86. selfy := isUTF16Self(ry)
  87. switch {
  88. // The x rune is a single UTF-16 codepoint, while
  89. // the y rune is a surrogate pair of UTF-16 codepoints.
  90. case selfx && !selfy:
  91. ry, _ = utf16.EncodeRune(ry)
  92. // The y rune is a single UTF-16 codepoint, while
  93. // the x rune is a surrogate pair of UTF-16 codepoints.
  94. case selfy && !selfx:
  95. rx, _ = utf16.EncodeRune(rx)
  96. }
  97. if rx != ry {
  98. return cmp.Compare(rx, ry)
  99. }
  100. // Check for invalid UTF-8, in which case,
  101. // we just perform a byte-for-byte comparison.
  102. if isInvalidUTF8(rx, nx) || isInvalidUTF8(ry, ny) {
  103. if x[0] != y[0] {
  104. return cmp.Compare(x[0], y[0])
  105. }
  106. }
  107. x, y = x[nx:], y[ny:]
  108. }
  109. }
  110. // truncateMaxUTF8 truncates b such it contains at least one rune.
  111. //
  112. // The utf8 package currently lacks generic variants, which complicates
  113. // generic functions that operates on either []byte or string.
  114. // As a hack, we always call the utf8 function operating on strings,
  115. // but always truncate the input such that the result is identical.
  116. //
  117. // Example usage:
  118. //
  119. // utf8.DecodeRuneInString(string(truncateMaxUTF8(b)))
  120. //
  121. // Converting a []byte to a string is stack allocated since
  122. // truncateMaxUTF8 guarantees that the []byte is short.
  123. func truncateMaxUTF8[Bytes ~[]byte | ~string](b Bytes) Bytes {
  124. // TODO(https://go.dev/issue/56948): Remove this function and
  125. // instead directly call generic utf8 functions wherever used.
  126. if len(b) > utf8.UTFMax {
  127. return b[:utf8.UTFMax]
  128. }
  129. return b
  130. }
  131. // TODO(https://go.dev/issue/70547): Use utf8.ErrInvalid instead.
  132. var ErrInvalidUTF8 = errors.New("invalid UTF-8")
  133. func NewInvalidCharacterError[Bytes ~[]byte | ~string](prefix Bytes, where string) error {
  134. what := QuoteRune(prefix)
  135. return errors.New("invalid character " + what + " " + where)
  136. }
  137. func NewInvalidEscapeSequenceError[Bytes ~[]byte | ~string](what Bytes) error {
  138. label := "escape sequence"
  139. if len(what) > 6 {
  140. label = "surrogate pair"
  141. }
  142. needEscape := strings.IndexFunc(string(what), func(r rune) bool {
  143. return r == '`' || r == utf8.RuneError || unicode.IsSpace(r) || !unicode.IsPrint(r)
  144. }) >= 0
  145. if needEscape {
  146. return errors.New("invalid " + label + " " + strconv.Quote(string(what)) + " in string")
  147. } else {
  148. return errors.New("invalid " + label + " `" + string(what) + "` in string")
  149. }
  150. }
  151. // TruncatePointer optionally truncates the JSON pointer,
  152. // enforcing that the length roughly does not exceed n.
  153. func TruncatePointer(s string, n int) string {
  154. if len(s) <= n {
  155. return s
  156. }
  157. i := n / 2
  158. j := len(s) - n/2
  159. // Avoid truncating a name if there are multiple names present.
  160. if k := strings.LastIndexByte(s[:i], '/'); k > 0 {
  161. i = k
  162. }
  163. if k := strings.IndexByte(s[j:], '/'); k >= 0 {
  164. j += k + len("/")
  165. }
  166. // Avoid truncation in the middle of a UTF-8 rune.
  167. for i > 0 && isInvalidUTF8(utf8.DecodeLastRuneInString(s[:i])) {
  168. i--
  169. }
  170. for j < len(s) && isInvalidUTF8(utf8.DecodeRuneInString(s[j:])) {
  171. j++
  172. }
  173. // Determine the right middle fragment to use.
  174. var middle string
  175. switch strings.Count(s[i:j], "/") {
  176. case 0:
  177. middle = "…"
  178. case 1:
  179. middle = "…/…"
  180. default:
  181. middle = "…/…/…"
  182. }
  183. if strings.HasPrefix(s[i:j], "/") && middle != "…" {
  184. middle = strings.TrimPrefix(middle, "…")
  185. }
  186. if strings.HasSuffix(s[i:j], "/") && middle != "…" {
  187. middle = strings.TrimSuffix(middle, "…")
  188. }
  189. return s[:i] + middle + s[j:]
  190. }
  191. func isInvalidUTF8(r rune, rn int) bool {
  192. return r == utf8.RuneError && rn == 1
  193. }