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, )
| 80 | // EncodeEscapedChar is used internally to write out a character from a larger |
| 81 | // string that needs to be escaped to a buffer. |
| 82 | func EncodeEscapedChar( |
| 83 | buf *bytes.Buffer, |
| 84 | entireString string, |
| 85 | currentRune rune, |
| 86 | currentByte byte, |
| 87 | currentIdx int, |
| 88 | quoteChar byte, |
| 89 | ) { |
| 90 | ln := utf8.RuneLen(currentRune) |
| 91 | if currentRune == utf8.RuneError { |
| 92 | // Errors are due to invalid unicode points, so escape the bytes. |
| 93 | // Make sure this is run at least once in case ln == -1. |
| 94 | buf.Write(HexMap[entireString[currentIdx]]) |
| 95 | for ri := 1; ri < ln; ri++ { |
| 96 | if currentIdx+ri < len(entireString) { |
| 97 | buf.Write(HexMap[entireString[currentIdx+ri]]) |
| 98 | } |
| 99 | } |
| 100 | } else if ln == 1 { |
| 101 | // For single-byte runes, do the same as encodeSQLBytes. |
| 102 | if encodedChar := EncodeMap[currentByte]; encodedChar != DontEscape { |
| 103 | buf.WriteByte('\\') |
| 104 | buf.WriteByte(encodedChar) |
| 105 | } else if currentByte == quoteChar { |
| 106 | buf.WriteByte('\\') |
| 107 | buf.WriteByte(quoteChar) |
| 108 | } else { |
| 109 | // Escape non-printable characters. |
| 110 | buf.Write(HexMap[currentByte]) |
| 111 | } |
| 112 | } else if ln == 2 { |
| 113 | // For multi-byte runes, print them based on their width. |
| 114 | fmt.Fprintf(buf, `\u%04X`, currentRune) |
| 115 | } else { |
| 116 | fmt.Fprintf(buf, `\U%08X`, currentRune) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func writeHexDigit(buf *bytes.Buffer, v int) { |
| 121 | if v < 10 { |
no test coverage detected
searching dependent graphs…