Index roster names with Unicode case folding
This commit is contained in:
@@ -147,7 +147,7 @@ func firstPartyDomainError(document *partyView) *partyDomainError {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
seenNames := make([]string, 0, len(ids)*2)
|
||||
seenNames := make(map[string]struct{}, len(ids)*2)
|
||||
for _, id := range ids {
|
||||
path := charactersPath.Key(id)
|
||||
character := document.Characters[id]
|
||||
@@ -160,39 +160,41 @@ func firstPartyDomainError(document *partyView) *partyDomainError {
|
||||
if err := validatePartyDisplayName(character.Character.Name); err != "" {
|
||||
return &partyDomainError{path: path.Key("character").Key("name"), message: err}
|
||||
}
|
||||
if partyNameAmbiguous(character.Character.Name, seenNames) {
|
||||
nameKey := partyFoldKey(character.Character.Name)
|
||||
if _, exists := seenNames[nameKey]; exists {
|
||||
return &partyDomainError{path: path.Key("character").Key("name"), message: "name or alias is ambiguous"}
|
||||
}
|
||||
visibleNames := append(append([]string(nil), seenNames...), character.Character.Name)
|
||||
seenNames[nameKey] = struct{}{}
|
||||
for index, alias := range character.Character.Aliases {
|
||||
aliasPath := path.Key("character").Key("aliases").Index(index)
|
||||
if err := validatePartyDisplayName(alias); err != "" {
|
||||
return &partyDomainError{path: aliasPath, message: err}
|
||||
}
|
||||
if partyNameAmbiguous(alias, visibleNames) {
|
||||
key := partyFoldKey(alias)
|
||||
if _, exists := seenNames[key]; exists {
|
||||
return &partyDomainError{path: aliasPath, message: "name or alias is ambiguous"}
|
||||
}
|
||||
visibleNames = append(visibleNames, alias)
|
||||
seenNames[key] = struct{}{}
|
||||
}
|
||||
classesPath := path.Key("character").Key("classes")
|
||||
if len(character.Character.Classes) == 0 {
|
||||
return &partyDomainError{path: classesPath, message: "classes must be a non-empty list"}
|
||||
}
|
||||
classNames := make([]string, 0, len(character.Character.Classes))
|
||||
classNames := make(map[string]struct{}, len(character.Character.Classes))
|
||||
for index, class := range character.Character.Classes {
|
||||
classPath := classesPath.Index(index)
|
||||
if err := validatePartyDisplayName(class.Name); err != "" {
|
||||
return &partyDomainError{path: classPath.Key("name"), message: err}
|
||||
}
|
||||
if partyNameAmbiguous(class.Name, classNames) {
|
||||
key := partyFoldKey(class.Name)
|
||||
if _, exists := classNames[key]; exists {
|
||||
return &partyDomainError{path: classPath.Key("name"), message: "duplicate class"}
|
||||
}
|
||||
classNames = append(classNames, class.Name)
|
||||
classNames[key] = struct{}{}
|
||||
if class.Level != nil && *class.Level <= 0 {
|
||||
return &partyDomainError{path: classPath.Key("level"), message: "level must be a positive integer"}
|
||||
}
|
||||
}
|
||||
seenNames = visibleNames
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -209,13 +211,21 @@ func validatePartyDisplayName(value string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func partyNameAmbiguous(name string, existing []string) bool {
|
||||
for _, candidate := range existing {
|
||||
if strings.EqualFold(name, candidate) {
|
||||
return true
|
||||
// partyFoldKey selects the least rune in each Unicode simple-fold cycle,
|
||||
// preserving strings.EqualFold equivalence without changing forwarded names.
|
||||
func partyFoldKey(name string) string {
|
||||
var key strings.Builder
|
||||
key.Grow(len(name))
|
||||
for _, r := range name {
|
||||
least := r
|
||||
for folded := unicode.SimpleFold(r); folded != r; folded = unicode.SimpleFold(folded) {
|
||||
if folded < least {
|
||||
least = folded
|
||||
}
|
||||
}
|
||||
key.WriteRune(least)
|
||||
}
|
||||
return false
|
||||
return key.String()
|
||||
}
|
||||
|
||||
func interpretParty(document map[string]any) partyView {
|
||||
|
||||
Reference in New Issue
Block a user