Skip to content

Commit 8afc1b1

Browse files
authored
Move some regexp out of functions (#25430)
/cc @KN4CK3R #25294 (comment) I also searched the codebase and found a few more. --------- Signed-off-by: jolheiser <[email protected]>
1 parent 25455bc commit 8afc1b1

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

modules/repository/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,12 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
372372
return generateRepo, nil
373373
}
374374

375+
var fileNameSanitizeRegexp = regexp.MustCompile(`(?i)\.\.|[<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`)
376+
375377
// Sanitize user input to valid OS filenames
376378
//
377379
// Based on https://github.com/sindresorhus/filename-reserved-regex
378380
// Adds ".." to prevent directory traversal
379381
func fileNameSanitize(s string) string {
380-
re := regexp.MustCompile(`(?i)\.\.|[<>:\"/\\|?*\x{0000}-\x{001F}]|^(con|prn|aux|nul|com\d|lpt\d)$`)
381-
382-
return strings.TrimSpace(re.ReplaceAllString(s, "_"))
382+
return strings.TrimSpace(fileNameSanitizeRegexp.ReplaceAllString(s, "_"))
383383
}

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)