Skip to content

Commit 734fd93

Browse files
authored
Move some regexp out of functions (#25430) (#25445)
Partial backport of #25430 Not a bug, but worth backporting for efficiency. Signed-off-by: jolheiser <[email protected]>
1 parent 203fe28 commit 734fd93

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

modules/util/path.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ func isOSWindows() bool {
222222
return runtime.GOOS == "windows"
223223
}
224224

225+
var driveLetterRegexp = regexp.MustCompile("/[A-Za-z]:/")
226+
225227
// FileURLToPath extracts the path information from a file://... url.
226228
func FileURLToPath(u *url.URL) (string, error) {
227229
if u.Scheme != "file" {
@@ -235,8 +237,7 @@ func FileURLToPath(u *url.URL) (string, error) {
235237
}
236238

237239
// If it looks like there's a Windows drive letter at the beginning, strip off the leading slash.
238-
re := regexp.MustCompile("/[A-Za-z]:/")
239-
if re.MatchString(path) {
240+
if driveLetterRegexp.MatchString(path) {
240241
return path[1:], nil
241242
}
242243
return path, nil

services/lfs/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func CheckAcceptMediaType(ctx *context.Context) {
7777
}
7878
}
7979

80+
var rangeHeaderRegexp = regexp.MustCompile(`bytes=(\d+)\-(\d*).*`)
81+
8082
// DownloadHandler gets the content from the content store
8183
func DownloadHandler(ctx *context.Context) {
8284
rc := getRequestContext(ctx)
@@ -92,8 +94,7 @@ func DownloadHandler(ctx *context.Context) {
9294
toByte = meta.Size - 1
9395
statusCode := http.StatusOK
9496
if rangeHdr := ctx.Req.Header.Get("Range"); rangeHdr != "" {
95-
regex := regexp.MustCompile(`bytes=(\d+)\-(\d*).*`)
96-
match := regex.FindStringSubmatch(rangeHdr)
97+
match := rangeHeaderRegexp.FindStringSubmatch(rangeHdr)
9798
if len(match) > 1 {
9899
statusCode = http.StatusPartialContent
99100
fromByte, _ = strconv.ParseInt(match[1], 10, 32)

0 commit comments

Comments
 (0)