util/nocasemaps: add AppendSliceElem method to nocasemaps (#10871)

Updates #7667

Signed-off-by: Anishka Singh <anishkasingh66@gmail.com>
This commit is contained in:
as2643
2024-01-19 15:30:12 -08:00
committed by GitHub
parent 2ce596ea7a
commit 832e5c781d
2 changed files with 33 additions and 0 deletions

View File

@ -74,6 +74,22 @@ func Delete[K ~string, V any](m map[K]V, k K) {
delete(m, K(appendToLower(a[:0], string(k))))
}
// AppendSliceElem is equivalent to:
//
// append(m[strings.ToLower(k)], v)
func AppendSliceElem[K ~string, S []E, E any](m map[K]S, k K, vs ...E) {
// if the key is already lowercased
if isLowerASCII(string(k)) {
m[k] = append(m[k], vs...)
return
}
// if key needs to become lowercase, uses appendToLower
var a [stackArraySize]byte
s := appendToLower(a[:0], string(k))
m[K(s)] = append(m[K(s)], vs...)
}
func isLowerASCII(s string) bool {
for i := 0; i < len(s); i++ {
if c := s[i]; c >= utf8.RuneSelf || ('A' <= c && c <= 'Z') {