Skip to content

Commit dc86f1d

Browse files
ldezjuliens
authored andcommitted
Revert "Simplify extractVars, fixes edge cases. (gorilla#185)"
This reverts commit 0b13a92.
1 parent 280b3d5 commit dc86f1d

File tree

2 files changed

+13
-39
lines changed

2 files changed

+13
-39
lines changed

mux_test.go

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ func TestHost(t *testing.T) {
148148
},
149149
{
150150
title: "Host route with pattern, additional capturing group, match",
151-
route: new(Route).Host("aaa.{v1:[a-z]{2}(?:b|c)}.ccc"),
151+
route: new(Route).Host("aaa.{v1:[a-z]{2}(b|c)}.ccc"),
152152
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
153153
vars: map[string]string{"v1": "bbb"},
154154
host: "aaa.bbb.ccc",
155155
path: "",
156-
hostTemplate: `aaa.{v1:[a-z]{2}(?:b|c)}.ccc`,
156+
hostTemplate: `aaa.{v1:[a-z]{2}(b|c)}.ccc`,
157157
shouldMatch: true,
158158
},
159159
{
@@ -198,12 +198,12 @@ func TestHost(t *testing.T) {
198198
},
199199
{
200200
title: "Host route with hyphenated name and pattern, additional capturing group, match",
201-
route: new(Route).Host("aaa.{v-1:[a-z]{2}(?:b|c)}.ccc"),
201+
route: new(Route).Host("aaa.{v-1:[a-z]{2}(b|c)}.ccc"),
202202
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
203203
vars: map[string]string{"v-1": "bbb"},
204204
host: "aaa.bbb.ccc",
205205
path: "",
206-
hostTemplate: `aaa.{v-1:[a-z]{2}(?:b|c)}.ccc`,
206+
hostTemplate: `aaa.{v-1:[a-z]{2}(b|c)}.ccc`,
207207
shouldMatch: true,
208208
},
209209
{
@@ -343,17 +343,6 @@ func TestPath(t *testing.T) {
343343
pathRegexp: `^/(?P<v0>[0-9]{3})/(?P<v1>[0-9]{3})/(?P<v2>[0-9]{3})$`,
344344
shouldMatch: false,
345345
},
346-
{
347-
title: "Path route with multiple patterns with pipe, match",
348-
route: new(Route).Path("/{category:a|(?:b/c)}/{product}/{id:[0-9]+}"),
349-
request: newRequest("GET", "http://localhost/a/product_name/1"),
350-
vars: map[string]string{"category": "a", "product": "product_name", "id": "1"},
351-
host: "",
352-
path: "/a/product_name/1",
353-
pathTemplate: `/{category:a|(?:b/c)}/{product}/{id:[0-9]+}`,
354-
pathRegexp: `^/(?P<v0>a|(?:b/c))/(?P<v1>[^/]+)/(?P<v2>[0-9]+)$`,
355-
shouldMatch: true,
356-
},
357346
{
358347
title: "Path route with hyphenated name and pattern, match",
359348
route: new(Route).Path("/111/{v-1:[0-9]{3}}/333"),
@@ -376,17 +365,6 @@ func TestPath(t *testing.T) {
376365
pathRegexp: `^/(?P<v0>[0-9]{3})/(?P<v1>[0-9]{3})/(?P<v2>[0-9]{3})$`,
377366
shouldMatch: true,
378367
},
379-
{
380-
title: "Path route with multiple hyphenated names and patterns with pipe, match",
381-
route: new(Route).Path("/{product-category:a|(?:b/c)}/{product-name}/{product-id:[0-9]+}"),
382-
request: newRequest("GET", "http://localhost/a/product_name/1"),
383-
vars: map[string]string{"product-category": "a", "product-name": "product_name", "product-id": "1"},
384-
host: "",
385-
path: "/a/product_name/1",
386-
pathTemplate: `/{product-category:a|(?:b/c)}/{product-name}/{product-id:[0-9]+}`,
387-
pathRegexp: `^/(?P<v0>a|(?:b/c))/(?P<v1>[^/]+)/(?P<v2>[0-9]+)$`,
388-
shouldMatch: true,
389-
},
390368
{
391369
title: "Path route with multiple hyphenated names and patterns with pipe and case insensitive, match",
392370
route: new(Route).Path("/{type:(?i:daily|mini|variety)}-{date:\\d{4,4}-\\d{2,2}-\\d{2,2}}"),
@@ -398,17 +376,6 @@ func TestPath(t *testing.T) {
398376
pathRegexp: `^/(?P<v0>(?i:daily|mini|variety))-(?P<v1>\d{4,4}-\d{2,2}-\d{2,2})$`,
399377
shouldMatch: true,
400378
},
401-
{
402-
title: "Path route with empty match right after other match",
403-
route: new(Route).Path(`/{v1:[0-9]*}{v2:[a-z]*}/{v3:[0-9]*}`),
404-
request: newRequest("GET", "http://localhost/111/222"),
405-
vars: map[string]string{"v1": "111", "v2": "", "v3": "222"},
406-
host: "",
407-
path: "/111/222",
408-
pathTemplate: `/{v1:[0-9]*}{v2:[a-z]*}/{v3:[0-9]*}`,
409-
pathRegexp: `^/(?P<v0>[0-9]*)(?P<v1>[a-z]*)/(?P<v2>[0-9]*)$`,
410-
shouldMatch: true,
411-
},
412379
{
413380
title: "Path route with single pattern with pipe, match",
414381
route: new(Route).Path("/{category:a|b/c}"),

regexp.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,14 @@ func getHost(r *http.Request) string {
375375
}
376376

377377
func extractVars(input string, matches []int, names []string, output map[string]string) {
378-
for i, name := range names {
379-
output[name] = input[matches[2*i+2]:matches[2*i+3]]
378+
matchesCount := 0
379+
prevEnd := -1
380+
for i := 2; i < len(matches) && matchesCount < len(names); i += 2 {
381+
if prevEnd < matches[i+1] {
382+
value := input[matches[i]:matches[i+1]]
383+
output[names[matchesCount]] = value
384+
prevEnd = matches[i+1]
385+
matchesCount++
386+
}
380387
}
381388
}

0 commit comments

Comments
 (0)