Skip to content

Commit 06ccd3e

Browse files
committed
Revert "Simplify extractVars, fixes edge cases. (gorilla#185)"
This reverts commit 0b13a92.
1 parent 4198e05 commit 06ccd3e

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
@@ -135,12 +135,12 @@ func TestHost(t *testing.T) {
135135
},
136136
{
137137
title: "Host route with pattern, additional capturing group, match",
138-
route: new(Route).Host("aaa.{v1:[a-z]{2}(?:b|c)}.ccc"),
138+
route: new(Route).Host("aaa.{v1:[a-z]{2}(b|c)}.ccc"),
139139
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
140140
vars: map[string]string{"v1": "bbb"},
141141
host: "aaa.bbb.ccc",
142142
path: "",
143-
hostTemplate: `aaa.{v1:[a-z]{2}(?:b|c)}.ccc`,
143+
hostTemplate: `aaa.{v1:[a-z]{2}(b|c)}.ccc`,
144144
shouldMatch: true,
145145
},
146146
{
@@ -185,12 +185,12 @@ func TestHost(t *testing.T) {
185185
},
186186
{
187187
title: "Host route with hyphenated name and pattern, additional capturing group, match",
188-
route: new(Route).Host("aaa.{v-1:[a-z]{2}(?:b|c)}.ccc"),
188+
route: new(Route).Host("aaa.{v-1:[a-z]{2}(b|c)}.ccc"),
189189
request: newRequest("GET", "http://aaa.bbb.ccc/111/222/333"),
190190
vars: map[string]string{"v-1": "bbb"},
191191
host: "aaa.bbb.ccc",
192192
path: "",
193-
hostTemplate: `aaa.{v-1:[a-z]{2}(?:b|c)}.ccc`,
193+
hostTemplate: `aaa.{v-1:[a-z]{2}(b|c)}.ccc`,
194194
shouldMatch: true,
195195
},
196196
{
@@ -328,17 +328,6 @@ func TestPath(t *testing.T) {
328328
pathRegexp: `^/(?P<v0>[0-9]{3})/(?P<v1>[0-9]{3})/(?P<v2>[0-9]{3})$`,
329329
shouldMatch: false,
330330
},
331-
{
332-
title: "Path route with multiple patterns with pipe, match",
333-
route: new(Route).Path("/{category:a|(?:b/c)}/{product}/{id:[0-9]+}"),
334-
request: newRequest("GET", "http://localhost/a/product_name/1"),
335-
vars: map[string]string{"category": "a", "product": "product_name", "id": "1"},
336-
host: "",
337-
path: "/a/product_name/1",
338-
pathTemplate: `/{category:a|(?:b/c)}/{product}/{id:[0-9]+}`,
339-
pathRegexp: `^/(?P<v0>a|(?:b/c))/(?P<v1>[^/]+)/(?P<v2>[0-9]+)$`,
340-
shouldMatch: true,
341-
},
342331
{
343332
title: "Path route with hyphenated name and pattern, match",
344333
route: new(Route).Path("/111/{v-1:[0-9]{3}}/333"),
@@ -361,17 +350,6 @@ func TestPath(t *testing.T) {
361350
pathRegexp: `^/(?P<v0>[0-9]{3})/(?P<v1>[0-9]{3})/(?P<v2>[0-9]{3})$`,
362351
shouldMatch: true,
363352
},
364-
{
365-
title: "Path route with multiple hyphenated names and patterns with pipe, match",
366-
route: new(Route).Path("/{product-category:a|(?:b/c)}/{product-name}/{product-id:[0-9]+}"),
367-
request: newRequest("GET", "http://localhost/a/product_name/1"),
368-
vars: map[string]string{"product-category": "a", "product-name": "product_name", "product-id": "1"},
369-
host: "",
370-
path: "/a/product_name/1",
371-
pathTemplate: `/{product-category:a|(?:b/c)}/{product-name}/{product-id:[0-9]+}`,
372-
pathRegexp: `^/(?P<v0>a|(?:b/c))/(?P<v1>[^/]+)/(?P<v2>[0-9]+)$`,
373-
shouldMatch: true,
374-
},
375353
{
376354
title: "Path route with multiple hyphenated names and patterns with pipe and case insensitive, match",
377355
route: new(Route).Path("/{type:(?i:daily|mini|variety)}-{date:\\d{4,4}-\\d{2,2}-\\d{2,2}}"),
@@ -383,17 +361,6 @@ func TestPath(t *testing.T) {
383361
pathRegexp: `^/(?P<v0>(?i:daily|mini|variety))-(?P<v1>\d{4,4}-\d{2,2}-\d{2,2})$`,
384362
shouldMatch: true,
385363
},
386-
{
387-
title: "Path route with empty match right after other match",
388-
route: new(Route).Path(`/{v1:[0-9]*}{v2:[a-z]*}/{v3:[0-9]*}`),
389-
request: newRequest("GET", "http://localhost/111/222"),
390-
vars: map[string]string{"v1": "111", "v2": "", "v3": "222"},
391-
host: "",
392-
path: "/111/222",
393-
pathTemplate: `/{v1:[0-9]*}{v2:[a-z]*}/{v3:[0-9]*}`,
394-
pathRegexp: `^/(?P<v0>[0-9]*)(?P<v1>[a-z]*)/(?P<v2>[0-9]*)$`,
395-
shouldMatch: true,
396-
},
397364
{
398365
title: "Path route with single pattern with pipe, match",
399366
route: new(Route).Path("/{category:a|b/c}"),

regexp.go

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

315315
func extractVars(input string, matches []int, names []string, output map[string]string) {
316-
for i, name := range names {
317-
output[name] = input[matches[2*i+2]:matches[2*i+3]]
316+
matchesCount := 0
317+
prevEnd := -1
318+
for i := 2; i < len(matches) && matchesCount < len(names); i += 2 {
319+
if prevEnd < matches[i+1] {
320+
value := input[matches[i]:matches[i+1]]
321+
output[names[matchesCount]] = value
322+
prevEnd = matches[i+1]
323+
matchesCount++
324+
}
318325
}
319326
}

0 commit comments

Comments
 (0)