Skip to content

Commit 6953325

Browse files
authored
♻️ Refactor: replace findLastCharsetPosition with strings.LastIndexByte (#3338)
* ♻️ Refactor: replace findLastCharsetPosition with strings.LastIndexByte * 🩹 Fix: correct loop condition in Go benchmark
1 parent a5c7b77 commit 6953325

File tree

2 files changed

+4
-18
lines changed

2 files changed

+4
-18
lines changed

path.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ var (
123123
parameterConstraintSeparatorChars = []byte{paramConstraintSeparator}
124124
// list of parameter constraint data start
125125
parameterConstraintDataStartChars = []byte{paramConstraintDataStart}
126-
// list of parameter constraint data end
127-
parameterConstraintDataEndChars = []byte{paramConstraintDataEnd}
128126
// list of parameter constraint data separator
129127
parameterConstraintDataSeparatorChars = []byte{paramConstraintDataSeparator}
130128
)
@@ -317,7 +315,7 @@ func (routeParser *routeParser) analyseParameterPart(pattern string, customConst
317315
// find constraint part if exists in the parameter part and remove it
318316
if parameterEndPosition > 0 {
319317
parameterConstraintStart = findNextNonEscapedCharsetPosition(pattern[0:parameterEndPosition], parameterConstraintStartChars)
320-
parameterConstraintEnd = findLastCharsetPosition(pattern[0:parameterEndPosition+1], parameterConstraintEndChars)
318+
parameterConstraintEnd = strings.LastIndexByte(pattern[0:parameterEndPosition+1], paramConstraintEnd)
321319
}
322320

323321
// cut params part
@@ -335,7 +333,7 @@ func (routeParser *routeParser) analyseParameterPart(pattern string, customConst
335333

336334
for _, c := range userConstraints {
337335
start := findNextNonEscapedCharsetPosition(c, parameterConstraintDataStartChars)
338-
end := findLastCharsetPosition(c, parameterConstraintDataEndChars)
336+
end := strings.LastIndexByte(c, paramConstraintDataEnd)
339337

340338
// Assign constraint
341339
if start != -1 && end != -1 {
@@ -421,18 +419,6 @@ func findNextCharsetPosition(search string, charset []byte) int {
421419
return nextPosition
422420
}
423421

424-
// findLastCharsetPosition search the last char position from the charset
425-
func findLastCharsetPosition(search string, charset []byte) int {
426-
lastPosition := -1
427-
for _, char := range charset {
428-
if pos := strings.LastIndexByte(search, char); pos != -1 && (pos < lastPosition || lastPosition == -1) {
429-
lastPosition = pos
430-
}
431-
}
432-
433-
return lastPosition
434-
}
435-
436422
// findNextCharsetPositionConstraint search the next char position from the charset
437423
// unlike findNextCharsetPosition, it takes care of constraint start-end chars to parse route pattern
438424
func findNextCharsetPositionConstraint(search string, charset []byte) int {

path_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func Benchmark_Path_matchParams(t *testing.B) {
217217
state = "not match"
218218
}
219219
t.Run(testCollection.pattern+" | "+state+" | "+c.url, func(b *testing.B) {
220-
for i := 0; i <= b.N; i++ {
220+
for i := 0; i < b.N; i++ {
221221
if match := parser.getMatch(c.url, c.url, &ctxParams, c.partialCheck); match {
222222
// Get testCases from the original path
223223
matchRes = true
@@ -250,7 +250,7 @@ func Benchmark_RoutePatternMatch(t *testing.B) {
250250
state = "not match"
251251
}
252252
t.Run(testCollection.pattern+" | "+state+" | "+c.url, func(b *testing.B) {
253-
for i := 0; i <= b.N; i++ {
253+
for i := 0; i < b.N; i++ {
254254
if match := RoutePatternMatch(c.url, testCollection.pattern); match {
255255
// Get testCases from the original path
256256
matchRes = true

0 commit comments

Comments
 (0)