Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept multiple hex escapes in the same [[]] block #53

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ var replacementTestCases = []stringTestCase{
{`www[[\x0D]]`, "www\x0d"},
{`www[[\xff]]`, "www\xff"},
{`www[[\xFF]]`, "www\xff"},
{`www[[\xFF\x00]]`, "www\xff\x00"},
{`www[[\xFF\x00\x01]]01`, "www\xff\x00\x0101"},
{`www[[\xaa\xBB\xcc\x35]]01`, "www\xaa\xbb\xcc\x3501"},
{`www[[\xzz]]`, "www[[\\xzz]]"}, // invalid hex is not an escape
{`[[notanescape]]`, "[[notanescape]]"},
{`[[[U]]]`, "[\x1f]"},
}
Expand Down
16 changes: 10 additions & 6 deletions lib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (

var (
// e.g., [[\x00]] for \x00, [[\xFF]] or [[\xff]] for \xff
hexEscapeRegex = regexp.MustCompile(`^\[\[\\x[0-9a-fA-F]{2}\]\]`)
// multiple escapes are accepted within the same block, e.g. [[\x00\x01\x03]]
hexEscapeRegex = regexp.MustCompile(`^\[\[(\\x[0-9a-fA-F]{2})+\]\]`)
)

var controlCodeReplacements = []struct {
Expand Down Expand Up @@ -49,12 +50,15 @@ LineLoop:
continue LineLoop
}
}
if hexEscapeRegex.MatchString(line) {
if val, err := strconv.ParseUint(strings.ToLower(line[4:6]), 16, 8); err == nil {
buf.WriteByte(byte(val))
line = line[8:]
continue LineLoop
if matched := hexEscapeRegex.FindString(line); matched != "" {
// [[\x01\x02]]
for i := 2; i < len(matched)-2; i += 4 {
if val, err := strconv.ParseUint(line[i+2:i+4], 16, 8); err == nil {
buf.WriteByte(byte(val))
}
}
line = line[len(matched):]
continue LineLoop
}
}
buf.WriteByte(line[0])
Expand Down
Loading