...

Source file src/github.com/rs/zerolog/internal/json/string.go

Documentation: github.com/rs/zerolog/internal/json

     1  package json
     2  
     3  import (
     4  	"fmt"
     5  	"unicode/utf8"
     6  )
     7  
     8  const hex = "0123456789abcdef"
     9  
    10  var noEscapeTable = [256]bool{}
    11  
    12  func init() {
    13  	for i := 0; i <= 0x7e; i++ {
    14  		noEscapeTable[i] = i >= 0x20 && i != '\\' && i != '"'
    15  	}
    16  }
    17  
    18  // AppendStrings encodes the input strings to json and
    19  // appends the encoded string list to the input byte slice.
    20  func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
    21  	if len(vals) == 0 {
    22  		return append(dst, '[', ']')
    23  	}
    24  	dst = append(dst, '[')
    25  	dst = e.AppendString(dst, vals[0])
    26  	if len(vals) > 1 {
    27  		for _, val := range vals[1:] {
    28  			dst = e.AppendString(append(dst, ','), val)
    29  		}
    30  	}
    31  	dst = append(dst, ']')
    32  	return dst
    33  }
    34  
    35  // AppendString encodes the input string to json and appends
    36  // the encoded string to the input byte slice.
    37  //
    38  // The operation loops though each byte in the string looking
    39  // for characters that need json or utf8 encoding. If the string
    40  // does not need encoding, then the string is appended in its
    41  // entirety to the byte slice.
    42  // If we encounter a byte that does need encoding, switch up
    43  // the operation and perform a byte-by-byte read-encode-append.
    44  func (Encoder) AppendString(dst []byte, s string) []byte {
    45  	// Start with a double quote.
    46  	dst = append(dst, '"')
    47  	// Loop through each character in the string.
    48  	for i := 0; i < len(s); i++ {
    49  		// Check if the character needs encoding. Control characters, slashes,
    50  		// and the double quote need json encoding. Bytes above the ascii
    51  		// boundary needs utf8 encoding.
    52  		if !noEscapeTable[s[i]] {
    53  			// We encountered a character that needs to be encoded. Switch
    54  			// to complex version of the algorithm.
    55  			dst = appendStringComplex(dst, s, i)
    56  			return append(dst, '"')
    57  		}
    58  	}
    59  	// The string has no need for encoding and therefore is directly
    60  	// appended to the byte slice.
    61  	dst = append(dst, s...)
    62  	// End with a double quote
    63  	return append(dst, '"')
    64  }
    65  
    66  // AppendStringers encodes the provided Stringer list to json and
    67  // appends the encoded Stringer list to the input byte slice.
    68  func (e Encoder) AppendStringers(dst []byte, vals []fmt.Stringer) []byte {
    69  	if len(vals) == 0 {
    70  		return append(dst, '[', ']')
    71  	}
    72  	dst = append(dst, '[')
    73  	dst = e.AppendStringer(dst, vals[0])
    74  	if len(vals) > 1 {
    75  		for _, val := range vals[1:] {
    76  			dst = e.AppendStringer(append(dst, ','), val)
    77  		}
    78  	}
    79  	return append(dst, ']')
    80  }
    81  
    82  // AppendStringer encodes the input Stringer to json and appends the
    83  // encoded Stringer value to the input byte slice.
    84  func (e Encoder) AppendStringer(dst []byte, val fmt.Stringer) []byte {
    85  	if val == nil {
    86  		return e.AppendInterface(dst, nil)
    87  	}
    88  	return e.AppendString(dst, val.String())
    89  }
    90  
    91  //// appendStringComplex is used by appendString to take over an in
    92  // progress JSON string encoding that encountered a character that needs
    93  // to be encoded.
    94  func appendStringComplex(dst []byte, s string, i int) []byte {
    95  	start := 0
    96  	for i < len(s) {
    97  		b := s[i]
    98  		if b >= utf8.RuneSelf {
    99  			r, size := utf8.DecodeRuneInString(s[i:])
   100  			if r == utf8.RuneError && size == 1 {
   101  				// In case of error, first append previous simple characters to
   102  				// the byte slice if any and append a replacement character code
   103  				// in place of the invalid sequence.
   104  				if start < i {
   105  					dst = append(dst, s[start:i]...)
   106  				}
   107  				dst = append(dst, `\ufffd`...)
   108  				i += size
   109  				start = i
   110  				continue
   111  			}
   112  			i += size
   113  			continue
   114  		}
   115  		if noEscapeTable[b] {
   116  			i++
   117  			continue
   118  		}
   119  		// We encountered a character that needs to be encoded.
   120  		// Let's append the previous simple characters to the byte slice
   121  		// and switch our operation to read and encode the remainder
   122  		// characters byte-by-byte.
   123  		if start < i {
   124  			dst = append(dst, s[start:i]...)
   125  		}
   126  		switch b {
   127  		case '"', '\\':
   128  			dst = append(dst, '\\', b)
   129  		case '\b':
   130  			dst = append(dst, '\\', 'b')
   131  		case '\f':
   132  			dst = append(dst, '\\', 'f')
   133  		case '\n':
   134  			dst = append(dst, '\\', 'n')
   135  		case '\r':
   136  			dst = append(dst, '\\', 'r')
   137  		case '\t':
   138  			dst = append(dst, '\\', 't')
   139  		default:
   140  			dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF])
   141  		}
   142  		i++
   143  		start = i
   144  	}
   145  	if start < len(s) {
   146  		dst = append(dst, s[start:]...)
   147  	}
   148  	return dst
   149  }
   150  

View as plain text