github.com/sandwich-go/boost@v1.3.29/xstrings/escape.go (about)

     1  package xstrings
     2  
     3  // EscapeStringBackslash escape v
     4  func EscapeStringBackslash(v string) string {
     5  	buf := make([]byte, len(v)*2)
     6  	pos := 0
     7  	for i := 0; i < len(v); i++ {
     8  		c := v[i]
     9  		switch c {
    10  		case '\x00':
    11  			buf[pos] = '\\'
    12  			buf[pos+1] = '0'
    13  			pos += 2
    14  		case '\n':
    15  			buf[pos] = '\\'
    16  			buf[pos+1] = 'n'
    17  			pos += 2
    18  		case '\r':
    19  			buf[pos] = '\\'
    20  			buf[pos+1] = 'r'
    21  			pos += 2
    22  		case '\x1a':
    23  			buf[pos] = '\\'
    24  			buf[pos+1] = 'Z'
    25  			pos += 2
    26  		case '\'':
    27  			buf[pos] = '\\'
    28  			buf[pos+1] = '\''
    29  			pos += 2
    30  		case '"':
    31  			buf[pos] = '\\'
    32  			buf[pos+1] = '"'
    33  			pos += 2
    34  		case '\\':
    35  			buf[pos] = '\\'
    36  			buf[pos+1] = '\\'
    37  			pos += 2
    38  		default:
    39  			buf[pos] = c
    40  			pos++
    41  		}
    42  	}
    43  
    44  	return string(buf[:pos])
    45  }