EncodeEscapedChar is used internally to write out a character from a larger string that needs to be escaped to a buffer.
( buf *bytes.Buffer, entireString string, currentRune rune, currentByte byte, currentIdx int, quoteChar byte, )
| 94 | // EncodeEscapedChar is used internally to write out a character from a larger |
| 95 | // string that needs to be escaped to a buffer. |
| 96 | func EncodeEscapedChar( |
| 97 | buf *bytes.Buffer, |
| 98 | entireString string, |
| 99 | currentRune rune, |
| 100 | currentByte byte, |
| 101 | currentIdx int, |
| 102 | quoteChar byte, |
| 103 | ) { |
| 104 | ln := utf8.RuneLen(currentRune) |
| 105 | if currentRune == utf8.RuneError { |
| 106 | // Errors are due to invalid unicode points, so escape the bytes. |
| 107 | // Make sure this is run at least once in case ln == -1. |
| 108 | buf.Write(HexMap[entireString[currentIdx]]) |
| 109 | for ri := 1; ri < ln; ri++ { |
| 110 | if currentIdx+ri < len(entireString) { |
| 111 | buf.Write(HexMap[entireString[currentIdx+ri]]) |
| 112 | } |
| 113 | } |
| 114 | } else if ln == 1 { |
| 115 | // For single-byte runes, do the same as encodeSQLBytes. |
| 116 | if encodedChar := EncodeMap[currentByte]; encodedChar != DontEscape { |
| 117 | buf.WriteByte('\\') |
| 118 | buf.WriteByte(encodedChar) |
| 119 | } else if currentByte == quoteChar { |
| 120 | buf.WriteByte('\\') |
| 121 | buf.WriteByte(quoteChar) |
| 122 | } else { |
| 123 | // Escape non-printable characters. |
| 124 | buf.Write(HexMap[currentByte]) |
| 125 | } |
| 126 | } else if ln == 2 { |
| 127 | // For multi-byte runes, print them based on their width. |
| 128 | fmt.Fprintf(buf, `\u%04X`, currentRune) |
| 129 | } else { |
| 130 | fmt.Fprintf(buf, `\U%08X`, currentRune) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func writeHexDigit(buf *bytes.Buffer, v int) { |
| 135 | if v < 10 { |
no test coverage detected