-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.go
More file actions
96 lines (74 loc) · 2.3 KB
/
utils.go
File metadata and controls
96 lines (74 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-FileCopyrightText: 2026 The SonicWeb contributors.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"os"
"regexp"
"slices"
"strconv"
"strings"
"time"
)
type LangPref struct {
Lang string
Variant string
Pref float32
}
// acceptLanguageHeaderRegex is a regular expression for parsing the HTTP "Accept-Language" header values
// and quality factors.
var acceptLanguageHeaderRegex = regexp.MustCompile(
`^(([A-Za-z]+)(?:-[A-Za-z0-9]+)*|\*)(?:;q=(1(?:\.0)?|0(?:\.[0-9]+)?))?$`)
// parseLanguageHeader parses the Accept-Language header and returns a sorted slice of LangPref by preference value.
func parseLanguageHeader(langHeader string) []LangPref {
const AverageLangLength = 5
const LangPrefMatch = 4
langHeader = strings.ReplaceAll(langHeader, " ", "")
langs := make([]LangPref, 0, max(1, len(langHeader)/AverageLangLength))
for part := range strings.SplitSeq(langHeader, ",") {
matches := acceptLanguageHeaderRegex.FindStringSubmatch(part)
if len(matches) == LangPrefMatch {
pref, prefErr := strconv.ParseFloat(matches[3], 32)
// this occurs in the case of an empty match
if prefErr != nil {
pref = 1
}
langs = append(langs, LangPref{
Lang: matches[2],
Variant: matches[1],
Pref: float32(pref),
})
}
}
slices.SortStableFunc(langs, func(a, b LangPref) int {
if a.Pref > b.Pref {
return -1
} else if a.Pref < b.Pref {
return 1
}
return 0
})
return langs
}
// cutLog truncates a string to ensure it does not exceed a specified length, appending a suffix if truncation occurs.
func cutLog(s string) string {
const MaxLogStringLength = 64 // must be smaller than the MaxPathPartLength!!!
const EndFill = "..."
if len([]rune(s)) > MaxLogStringLength {
return string(append([]rune(s)[:max(1, MaxLogStringLength-len(EndFill))], []rune(EndFill)...))
}
return s
}
// executableTime retrieves the modification time of the current executable and
// returns it formatted as an RFC3339 string.
// If the executable lookup fails, the time since the start of the program is returned instead.
func executableTime() string {
exe, err := os.Executable()
if err != nil {
return startTime.Format(time.RFC3339)
}
info, err := os.Stat(exe)
if err != nil {
return startTime.Format(time.RFC3339)
}
return info.ModTime().Format(time.RFC3339)
}