cgen: reduce memory allocation and improve performance of cescape_nonascii (#13141)

pull/13143/head
jeffmikels 2022-01-12 03:58:37 -05:00 committed by GitHub
parent c98af3c526
commit f99b79480d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 1 deletions

View File

@ -2514,7 +2514,13 @@ fn cescape_nonascii(original string) string {
mut b := strings.new_builder(original.len)
for c in original {
if c < 32 || c > 126 {
b.write_string('\\${c:03o}')
// Encode with a 3 digit octal escape code, which has the
// advantage to be limited/non dependant on what character
// will follow next, unlike hex escapes:
b.write_b(92) // \
b.write_b(48 + (c >> 6)) // oct digit 2
b.write_b(48 + (c >> 3) & 7) // oct digit 1
b.write_b(48 + c & 7) // oct digit 0
continue
}
b.write_b(c)