2023-04-20 14:30:24 +02:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2025-01-10 11:49:29 +01:00
|
|
|
"unicode"
|
2023-04-20 14:30:24 +02:00
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
2025-01-10 11:49:29 +01:00
|
|
|
// foldName returns a folded string such that foldName(x) == foldName(y)
|
|
|
|
// is identical to bytes.EqualFold(x, y).
|
|
|
|
func foldName(in []byte) []byte {
|
|
|
|
// This is inlinable to take advantage of "function outlining".
|
|
|
|
var arr [32]byte // large enough for most JSON names
|
|
|
|
return appendFoldedName(arr[:0], in)
|
2023-04-20 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2025-01-10 11:49:29 +01:00
|
|
|
func appendFoldedName(out, in []byte) []byte {
|
|
|
|
for i := 0; i < len(in); {
|
|
|
|
// Handle single-byte ASCII.
|
|
|
|
if c := in[i]; c < utf8.RuneSelf {
|
|
|
|
if 'a' <= c && c <= 'z' {
|
|
|
|
c -= 'a' - 'A'
|
2023-04-20 14:30:24 +02:00
|
|
|
}
|
2025-01-10 11:49:29 +01:00
|
|
|
out = append(out, c)
|
|
|
|
i++
|
2023-04-20 14:30:24 +02:00
|
|
|
continue
|
|
|
|
}
|
2025-01-10 11:49:29 +01:00
|
|
|
// Handle multi-byte Unicode.
|
|
|
|
r, n := utf8.DecodeRune(in[i:])
|
|
|
|
out = utf8.AppendRune(out, foldRune(r))
|
|
|
|
i += n
|
2023-04-20 14:30:24 +02:00
|
|
|
}
|
2025-01-10 11:49:29 +01:00
|
|
|
return out
|
2023-04-20 14:30:24 +02:00
|
|
|
}
|
|
|
|
|
2025-01-10 11:49:29 +01:00
|
|
|
// foldRune is returns the smallest rune for all runes in the same fold set.
|
|
|
|
func foldRune(r rune) rune {
|
|
|
|
for {
|
|
|
|
r2 := unicode.SimpleFold(r)
|
|
|
|
if r2 <= r {
|
|
|
|
return r2
|
2023-04-20 14:30:24 +02:00
|
|
|
}
|
2025-01-10 11:49:29 +01:00
|
|
|
r = r2
|
2023-04-20 14:30:24 +02:00
|
|
|
}
|
|
|
|
}
|