-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
243 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package res | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type group []gpart | ||
|
||
type gpart struct { | ||
str string | ||
idx int | ||
} | ||
|
||
// parseGroup takes a group name and parses it for ${tag} sequences, | ||
// verifying the tags exists as parameter tags in the pattern as well. | ||
// Panics if an error is encountered. | ||
func parseGroup(g, pattern string) group { | ||
if g == "" { | ||
return nil | ||
} | ||
|
||
tokens := splitPattern(pattern) | ||
|
||
var gr group | ||
var c byte | ||
l := len(g) | ||
i := 0 | ||
start := 0 | ||
|
||
StateDefault: | ||
if i == l { | ||
if i > start { | ||
gr = append(gr, gpart{str: g[start:i]}) | ||
} | ||
return gr | ||
} | ||
if g[i] == '$' { | ||
if i > start { | ||
gr = append(gr, gpart{str: g[start:i]}) | ||
} | ||
i++ | ||
if i == l { | ||
goto UnexpectedEnd | ||
} | ||
if g[i] != '{' { | ||
panic(fmt.Sprintf("expected character \"{\" at pos %d", i)) | ||
} | ||
i++ | ||
start = i | ||
goto StateTag | ||
} | ||
i++ | ||
goto StateDefault | ||
|
||
StateTag: | ||
if i == l { | ||
goto UnexpectedEnd | ||
} | ||
c = g[i] | ||
if c == '}' { | ||
if i == start { | ||
panic(fmt.Sprintf("empty group tag at pos %d", i)) | ||
} | ||
tag := "$" + g[start:i] | ||
for j, t := range tokens { | ||
if t == tag { | ||
gr = append(gr, gpart{idx: j}) | ||
goto TokenFound | ||
} | ||
} | ||
panic(fmt.Sprintf("group tag %s not found in pattern", tag)) | ||
TokenFound: | ||
i++ | ||
start = i | ||
goto StateDefault | ||
} | ||
if (c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '_' && c != '-' { | ||
panic(fmt.Sprintf("non alpha-numeric (a-z0-9_-) character in group tag at pos %d", i)) | ||
} | ||
i++ | ||
goto StateTag | ||
|
||
UnexpectedEnd: | ||
panic("unexpected end of group tag") | ||
} | ||
|
||
func (g group) toString(rname string) string { | ||
l := len(g) | ||
if l == 0 { | ||
return rname | ||
} | ||
|
||
if l == 1 && g[0].str != "" { | ||
return g[0].str | ||
} | ||
|
||
var tokens []string | ||
if len(rname) > 0 { | ||
tokens = make([]string, 0, 32) | ||
start := 0 | ||
for i := 0; i < len(rname); i++ { | ||
if rname[i] == btsep { | ||
tokens = append(tokens, rname[start:i]) | ||
start = i + 1 | ||
} | ||
} | ||
tokens = append(tokens, rname[start:]) | ||
} | ||
|
||
var b strings.Builder | ||
for _, gp := range g { | ||
if gp.str == "" { | ||
b.WriteString(tokens[gp.idx]) | ||
} else { | ||
b.WriteString(gp.str) | ||
} | ||
} | ||
|
||
return b.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package res | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// Test parseGroup panics when expected | ||
func TestParseGroup(t *testing.T) { | ||
tbl := []struct { | ||
Group string | ||
Pattern string | ||
Panic bool | ||
}{ | ||
// Valid groups | ||
{"", "test", false}, | ||
{"test", "test", false}, | ||
{"test", "test.$foo", false}, | ||
{"test.${foo}", "test.$foo", false}, | ||
{"${foo}", "test.$foo", false}, | ||
{"${foo}.test", "test.$foo", false}, | ||
{"${foo}${bar}", "test.$foo.$bar", false}, | ||
{"${bar}${foo}", "test.$foo.$bar", false}, | ||
{"${foo}.${bar}", "test.$foo.$bar.>", false}, | ||
{"${foo}${foo}", "test.$foo.$bar", false}, | ||
|
||
// Invalid groups | ||
{"$", "test.$foo", true}, | ||
{"${", "test.$foo", true}, | ||
{"${foo", "test.$foo", true}, | ||
{"${}", "test.$foo", true}, | ||
{"${$foo}", "test.$foo", true}, | ||
{"${bar}", "test.$foo", true}, | ||
} | ||
|
||
for _, l := range tbl { | ||
func() { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
if !l.Panic { | ||
t.Errorf("expected parseGroup not to panic, but it did:\n\tpanic : %s\n\tgroup : %s\n\tpattern : %s", r, l.Group, l.Pattern) | ||
} | ||
} else { | ||
if l.Panic { | ||
t.Errorf("expected parseGroup to panic, but it didn't\n\tgroup : %s\n\tpattern : %s", l.Group, l.Pattern) | ||
} | ||
} | ||
}() | ||
|
||
parseGroup(l.Group, l.Pattern) | ||
}() | ||
} | ||
} | ||
|
||
// Test group toString | ||
func TestGroupToString(t *testing.T) { | ||
tbl := []struct { | ||
Group string | ||
Pattern string | ||
RName string | ||
Expected string | ||
}{ | ||
{"", "test", "test", "test"}, | ||
{"test", "test", "test", "test"}, | ||
{"foo", "test", "test", "foo"}, | ||
{"test", "test.$foo", "test.42", "test"}, | ||
{"test.${foo}", "test.$foo", "test.42", "test.42"}, | ||
{"bar.${foo}", "test.$foo", "test.42", "bar.42"}, | ||
{"${foo}", "test.$foo", "test.42", "42"}, | ||
{"${foo}.test", "test.$foo", "test.42", "42.test"}, | ||
{"${foo}${bar}", "test.$foo.$bar", "test.42.baz", "42baz"}, | ||
{"${bar}${foo}", "test.$foo.$bar", "test.42.baz", "baz42"}, | ||
{"${foo}.${bar}", "test.$foo.$bar.>", "test.42.baz.extra.all", "42.baz"}, | ||
{"${foo}${foo}", "test.$foo.$bar", "test.42.baz", "4242"}, | ||
{"${foo}.test.this.${bar}", "test.$foo.$bar", "test.42.baz", "42.test.this.baz"}, | ||
} | ||
|
||
for _, l := range tbl { | ||
func() { | ||
gr := parseGroup(l.Group, l.Pattern) | ||
wid := gr.toString(l.RName) | ||
if wid != l.Expected { | ||
t.Errorf("expected parseGroup(%#v, %#v).toString(%#v) to return:\n\t%#v\nbut got:\n\t%#v", l.Group, l.Pattern, l.RName, l.Expected, wid) | ||
} | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters