Skip to content

Commit

Permalink
Allow escape characters \a \b \f \v (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmos authored Jan 11, 2022
1 parent e885412 commit 93a0fe9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
14 changes: 13 additions & 1 deletion build/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ import (

// unesc maps single-letter chars following \ to their actual values.
var unesc = [256]byte{
'a': '\a',
'b': '\b',
'f': '\f',
'n': '\n',
'r': '\r',
't': '\t',
'v': '\v',
'\\': '\\',
'\'': '\'',
'"': '"',
}

// esc maps escape-worthy bytes to the char that should follow \.
var esc = [256]byte{
'\a': 'a',
'\b': 'b',
'\f': 'f',
'\n': 'n',
'\r': 'r',
'\t': 't',
'\v': 'v',
'\\': '\\',
'\'': '\'',
'"': '"',
Expand All @@ -49,9 +57,13 @@ var esc = [256]byte{
// in a string literal
var escapable = [256]bool{
'\n': true,
'a': true,
'b': true,
'f': true,
'n': true,
'r': true,
't': true,
'v': true,
'x': true,
'\'': true,
'\\': true,
Expand Down Expand Up @@ -138,7 +150,7 @@ func Unquote(quoted string) (s string, triple bool, err error) {
// Ignore the escape and the line break.
quoted = quoted[2:]

case 'n', 'r', 't', '\\', '\'', '"':
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', '\'', '"':
// One-char escape
buf.WriteByte(unesc[quoted[1]])
quoted = quoted[2:]
Expand Down
14 changes: 9 additions & 5 deletions build/testdata/060.golden
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ strings = [
r"\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x43\y\z\0\1\2\3\4\5\6\7\8\9\\n",

# contain incorrect escape sequences
"\\a'",
"\\a\"",
"""\\a'""",
"""\\a\"""",
"\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\k\\l\\m\n\\o\\p\\q\r\\s\t\\u\\v\\wC\\y\\z\000\001\002\003\004\005\006\007\\8\\9",
"\\c'",
"\\c\"",
"""\\c'""",
"""\\c\"""",
"\a\b\\c\\d\\e\f\\g\\h\\i\\j\\k\\l\\m\n\\o\\p\\q\r\\s\t\\u\v\\wC\\y\\z\000\001\002\003\004\005\006\a\\8\\9",

# correct escape sequences
"""\a\b\f\n\r\t\v\007\010\
""",
"""\a\b\f
\r\t\v\a\b """,
""" aa\\bb\n \
""",
"\000\111\222",
Expand Down
12 changes: 8 additions & 4 deletions build/testdata/060.in
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ strings = [
r"\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x43\y\z\0\1\2\3\4\5\6\7\8\9\\n",

# contain incorrect escape sequences
"\a'",
'\a"',
"""\a'""",
'''\a"''',
"\c'",
'\c"',
"""\c'""",
'''\c"''',
"\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x43\y\z\0\1\2\3\4\5\6\7\8\9",

# correct escape sequences
"""\a\b\f\n\r\t\v\007\010\
""",
'''\a\b\f\n\r\t\v\007\010\
''',
""" aa\\bb\n \
""",
"\000\111\222",
Expand Down

0 comments on commit 93a0fe9

Please sign in to comment.