Simplify wildcard replacement in route patterns#1012
Simplify wildcard replacement in route patterns#1012VojtechVitek merged 1 commit intogo-chi:masterfrom
Conversation
| for strings.Contains(p, "/*/") { | ||
| p = strings.ReplaceAll(p, "/*/", "/") |
There was a problem hiding this comment.
Can this ever run more than two times?
Do we need the for loop?
There was a problem hiding this comment.
I used a loop because the original implementation was recursive — I assumed /*/ could appear multiple times, so I kept the behavior but replaced recursion with a loop
There was a problem hiding this comment.
@srpvpn I hear you, it makes sense.
Can you please find out if this needs to be done once, twice or many more times please? I'd rather remove the for loop unless necessary.
There was a problem hiding this comment.
@VojtechVitek
ReplaceAll doesn’t handle overlapping patterns — for example, /// becomes /*/, so we need to run it multiple times.
Since paths can have several wildcards in a row, a loop ensures they're all collapsed. The test with three wildcards confirms this.
There was a problem hiding this comment.
Sorry. My question was if if it ever overlaps more than two times, since you introduced ReplaceAll()?
There was a problem hiding this comment.
@VojtechVitek
Docs say “ReplaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new”.
Because the scan resumes after the piece it just wrote, any overlap is skipped:
/// --1st pass--> /*/ --2nd pass--> /
So if a router tacks on three-plus /* segments, one call isn’t enough.
VojtechVitek
left a comment
There was a problem hiding this comment.
LGTM. Thank you for you contribution!
Replace recursive wildcard collapsing with iterative strings.ReplaceAll
Add tests for consecutive wildcard segments and trailing wildcard behavior