Skip to content

Commit

Permalink
expand: support more format escape sequences
Browse files Browse the repository at this point in the history
Character escape codes like \x and \u remain to be done.

Updates mvdan#338.
  • Loading branch information
mvdan committed Jan 1, 2019
1 parent 1a53a43 commit e66b3a0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 15 additions & 5 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,24 @@ func Format(cfg *Config, format string, args []string) (string, int, error) {
case esc:
esc = false
switch c {
case 'n':
case 'a': // bell
buf.WriteRune('\a')
case 'b': // backspace
buf.WriteRune('\b')
case 'e', 'E': // escape
buf.WriteRune('\x1b')
case 'f': // form feed
buf.WriteRune('\f')
case 'n': // new line
buf.WriteRune('\n')
case 'r':
case 'r': // carriage return
buf.WriteRune('\r')
case 't':
case 't': // horizontal tab
buf.WriteRune('\t')
case '\\':
buf.WriteRune('\\')
case 'v': // vertical tab
buf.WriteRune('\v')
case '\\', '\'', '"', '?': // just the character
buf.WriteRune(c)
default:
buf.WriteRune('\\')
buf.WriteRune(c)
Expand Down
2 changes: 2 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ var fileCases = []struct {
{`echo $'%s'`, "%s\n"},
{`a=$'\r\t\\'; echo "$a"`, "\r\t\\\n"},
{`a=$"foo\nbar"; echo "$a"`, "foo\\nbar\n"},
{`echo $'\a\b\e\E\f\v'`, "\a\b\x1b\x1b\f\v\n"},
{`echo $'\\\'\"\?'`, "\\'\"?\n"},

// escaped chars
{"echo a\\b", "ab\n"},
Expand Down

0 comments on commit e66b3a0

Please sign in to comment.