MCPcopy Create free account
hub / github.com/pingcap/tidb / jsonMarshalStringTo

Function jsonMarshalStringTo

pkg/types/json_binary.go:397–467  ·  view source on GitHub ↗
(buf, s []byte)

Source from the content-addressed store, hash-verified

395}
396
397func jsonMarshalStringTo(buf, s []byte) []byte {
398 // NOTE: copied from Go standard library.
399 // NOTE: keep in sync with string above.
400 buf = append(buf, '"')
401 start := 0
402 for i := 0; i < len(s); {
403 if b := s[i]; b < utf8.RuneSelf {
404 if jsonSafeSet[b] {
405 i++
406 continue
407 }
408 if start < i {
409 buf = append(buf, s[start:i]...)
410 }
411 switch b {
412 case '\\', '"':
413 buf = append(buf, '\\', b)
414 case '\n':
415 buf = append(buf, '\\', 'n')
416 case '\r':
417 buf = append(buf, '\\', 'r')
418 case '\t':
419 buf = append(buf, '\\', 't')
420 default:
421 // This encodes bytes < 0x20 except for \t, \n and \r.
422 // If escapeHTML is set, it also escapes <, >, and &
423 // because they can lead to security holes when
424 // user-controlled strings are rendered into JSON
425 // and served to some browsers.
426 buf = append(buf, `\u00`...)
427 buf = append(buf, jsonHexChars[b>>4], jsonHexChars[b&0xF])
428 }
429 i++
430 start = i
431 continue
432 }
433 c, size := utf8.DecodeRune(s[i:])
434 if c == utf8.RuneError && size == 1 {
435 if start < i {
436 buf = append(buf, s[start:i]...)
437 }
438 buf = append(buf, `\ufffd`...)
439 i += size
440 start = i
441 continue
442 }
443 // U+2028 is LINE SEPARATOR.
444 // U+2029 is PARAGRAPH SEPARATOR.
445 // They are both technically valid characters in JSON strings,
446 // but don't work in JSONP, which has to be evaluated as JavaScript,
447 // and can lead to security holes there. It is valid JSON to
448 // escape them, so we do so unconditionally.
449 // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
450 if c == '\u2028' || c == '\u2029' {
451 if start < i {
452 buf = append(buf, s[start:i]...)
453 }
454 buf = append(buf, `\u202`...)

Callers 2

marshalToMethod · 0.85
marshalObjToMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…