From 6953325df5d5cb4d22eb1f5373dceaad6353b9bc Mon Sep 17 00:00:00 2001 From: Kashiwa <13825170+ksw2000@users.noreply.github.com> Date: Thu, 6 Mar 2025 16:00:18 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor:=20replace=20find?= =?UTF-8?q?LastCharsetPosition=20with=20strings.LastIndexByte=20(#3338)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ Refactor: replace findLastCharsetPosition with strings.LastIndexByte * 🩹 Fix: correct loop condition in Go benchmark --- path.go | 18 ++---------------- path_test.go | 4 ++-- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/path.go b/path.go index e8b90eaf..fdd61e39 100644 --- a/path.go +++ b/path.go @@ -123,8 +123,6 @@ var ( parameterConstraintSeparatorChars = []byte{paramConstraintSeparator} // list of parameter constraint data start parameterConstraintDataStartChars = []byte{paramConstraintDataStart} - // list of parameter constraint data end - parameterConstraintDataEndChars = []byte{paramConstraintDataEnd} // list of parameter constraint data separator parameterConstraintDataSeparatorChars = []byte{paramConstraintDataSeparator} ) @@ -317,7 +315,7 @@ func (routeParser *routeParser) analyseParameterPart(pattern string, customConst // find constraint part if exists in the parameter part and remove it if parameterEndPosition > 0 { parameterConstraintStart = findNextNonEscapedCharsetPosition(pattern[0:parameterEndPosition], parameterConstraintStartChars) - parameterConstraintEnd = findLastCharsetPosition(pattern[0:parameterEndPosition+1], parameterConstraintEndChars) + parameterConstraintEnd = strings.LastIndexByte(pattern[0:parameterEndPosition+1], paramConstraintEnd) } // cut params part @@ -335,7 +333,7 @@ func (routeParser *routeParser) analyseParameterPart(pattern string, customConst for _, c := range userConstraints { start := findNextNonEscapedCharsetPosition(c, parameterConstraintDataStartChars) - end := findLastCharsetPosition(c, parameterConstraintDataEndChars) + end := strings.LastIndexByte(c, paramConstraintDataEnd) // Assign constraint if start != -1 && end != -1 { @@ -421,18 +419,6 @@ func findNextCharsetPosition(search string, charset []byte) int { return nextPosition } -// findLastCharsetPosition search the last char position from the charset -func findLastCharsetPosition(search string, charset []byte) int { - lastPosition := -1 - for _, char := range charset { - if pos := strings.LastIndexByte(search, char); pos != -1 && (pos < lastPosition || lastPosition == -1) { - lastPosition = pos - } - } - - return lastPosition -} - // findNextCharsetPositionConstraint search the next char position from the charset // unlike findNextCharsetPosition, it takes care of constraint start-end chars to parse route pattern func findNextCharsetPositionConstraint(search string, charset []byte) int { diff --git a/path_test.go b/path_test.go index 14eda46c..076066a4 100644 --- a/path_test.go +++ b/path_test.go @@ -217,7 +217,7 @@ func Benchmark_Path_matchParams(t *testing.B) { state = "not match" } t.Run(testCollection.pattern+" | "+state+" | "+c.url, func(b *testing.B) { - for i := 0; i <= b.N; i++ { + for i := 0; i < b.N; i++ { if match := parser.getMatch(c.url, c.url, &ctxParams, c.partialCheck); match { // Get testCases from the original path matchRes = true @@ -250,7 +250,7 @@ func Benchmark_RoutePatternMatch(t *testing.B) { state = "not match" } t.Run(testCollection.pattern+" | "+state+" | "+c.url, func(b *testing.B) { - for i := 0; i <= b.N; i++ { + for i := 0; i < b.N; i++ { if match := RoutePatternMatch(c.url, testCollection.pattern); match { // Get testCases from the original path matchRes = true