Index roster names with Unicode case folding
This commit is contained in:
@@ -263,7 +263,12 @@ the complexity without a benchmark.
|
||||
|
||||
**Priority:** P3
|
||||
|
||||
**Status:** For consideration
|
||||
**Status:** Complete
|
||||
|
||||
Names, aliases, and classes use indexed Unicode simple-fold membership; sorted
|
||||
character traversal and forwarded strings are unchanged. Verified existing
|
||||
roster/family checks plus Unicode ambiguity across aliases, characters, and
|
||||
classes with `go test ./internal/config`.
|
||||
|
||||
**Stage:** 4.
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -2,12 +2,34 @@ package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestPartyAmbiguityUsesUnicodeSimpleFold(t *testing.T) {
|
||||
for _, pair := range [][2]string{{"Σ", "ς"}, {"K", "K"}, {"Arannis", "ARANNIS"}} {
|
||||
for _, location := range []string{"alias", "character", "class"} {
|
||||
details := fmt.Sprintf("name: %q, classes: [{name: wizard}]", pair[0])
|
||||
extra := ""
|
||||
switch location {
|
||||
case "alias":
|
||||
details += fmt.Sprintf(", aliases: [%q]", pair[1])
|
||||
case "character":
|
||||
extra = fmt.Sprintf(" second:\n player: {name: Player}\n character: {name: %q, classes: [{name: wizard}]}\n", pair[1])
|
||||
case "class":
|
||||
details = fmt.Sprintf("name: First, classes: [{name: %q}, {name: %q}]", pair[0], pair[1])
|
||||
}
|
||||
data := fmt.Sprintf("schema_version: maximumdirect.rpg.party.v1\ncharacters:\n first:\n player: {name: Player}\n character: {%s}\n%s", details, extra)
|
||||
if _, err := ParseParty([]byte(data)); err == nil {
|
||||
t.Fatalf("accepted %s ambiguity for %q", location, pair)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartyPreservesSharedExtensionsAndAliasPresence(t *testing.T) {
|
||||
input := []byte(`schema_version: maximumdirect.rpg.party.v1
|
||||
custom: {setting: synthetic}
|
||||
|
||||
Reference in New Issue
Block a user