unquote.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package unquote
  17. import (
  18. `unsafe`
  19. `runtime`
  20. `github.com/bytedance/sonic/internal/native`
  21. `github.com/bytedance/sonic/internal/native/types`
  22. `github.com/bytedance/sonic/internal/rt`
  23. )
  24. // String unescapes an escaped string (not including `"` at beginning and end)
  25. // It validates invalid UTF8 and replace with `\ufffd`
  26. func String(s string) (ret string, err types.ParsingError) {
  27. mm := make([]byte, 0, len(s))
  28. err = intoBytesUnsafe(s, &mm, true)
  29. ret = rt.Mem2Str(mm)
  30. return
  31. }
  32. // IntoBytes is same with String besides it output result into a buffer m
  33. func IntoBytes(s string, m *[]byte) types.ParsingError {
  34. if cap(*m) < len(s) {
  35. return types.ERR_EOF
  36. } else {
  37. return intoBytesUnsafe(s, m, true)
  38. }
  39. }
  40. // String unescapes an escaped string (not including `"` at beginning and end)
  41. // - replace enables replacing invalid utf8 escaped char with `\uffd`
  42. func _String(s string, replace bool) (ret string, err error) {
  43. mm := make([]byte, 0, len(s))
  44. err = intoBytesUnsafe(s, &mm, replace)
  45. ret = rt.Mem2Str(mm)
  46. return
  47. }
  48. func intoBytesUnsafe(s string, m *[]byte, replace bool) types.ParsingError {
  49. pos := -1
  50. slv := (*rt.GoSlice)(unsafe.Pointer(m))
  51. str := (*rt.GoString)(unsafe.Pointer(&s))
  52. flags := uint64(0)
  53. if replace {
  54. /* unquote as the default configuration, replace invalid unicode with \ufffd */
  55. flags |= types.F_UNICODE_REPLACE
  56. }
  57. ret := native.Unquote(str.Ptr, str.Len, slv.Ptr, &pos, flags)
  58. /* check for errors */
  59. if ret < 0 {
  60. return types.ParsingError(-ret)
  61. }
  62. /* update the length */
  63. slv.Len = ret
  64. runtime.KeepAlive(s)
  65. return 0
  66. }