Index roster names with Unicode case folding
This commit is contained in:
@@ -263,7 +263,12 @@ the complexity without a benchmark.
|
|||||||
|
|
||||||
**Priority:** P3
|
**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.
|
**Stage:** 4.
|
||||||
|
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ func firstPartyDomainError(document *partyView) *partyDomainError {
|
|||||||
ids = append(ids, id)
|
ids = append(ids, id)
|
||||||
}
|
}
|
||||||
sort.Strings(ids)
|
sort.Strings(ids)
|
||||||
seenNames := make([]string, 0, len(ids)*2)
|
seenNames := make(map[string]struct{}, len(ids)*2)
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
path := charactersPath.Key(id)
|
path := charactersPath.Key(id)
|
||||||
character := document.Characters[id]
|
character := document.Characters[id]
|
||||||
@@ -160,39 +160,41 @@ func firstPartyDomainError(document *partyView) *partyDomainError {
|
|||||||
if err := validatePartyDisplayName(character.Character.Name); err != "" {
|
if err := validatePartyDisplayName(character.Character.Name); err != "" {
|
||||||
return &partyDomainError{path: path.Key("character").Key("name"), message: 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"}
|
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 {
|
for index, alias := range character.Character.Aliases {
|
||||||
aliasPath := path.Key("character").Key("aliases").Index(index)
|
aliasPath := path.Key("character").Key("aliases").Index(index)
|
||||||
if err := validatePartyDisplayName(alias); err != "" {
|
if err := validatePartyDisplayName(alias); err != "" {
|
||||||
return &partyDomainError{path: aliasPath, message: 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"}
|
return &partyDomainError{path: aliasPath, message: "name or alias is ambiguous"}
|
||||||
}
|
}
|
||||||
visibleNames = append(visibleNames, alias)
|
seenNames[key] = struct{}{}
|
||||||
}
|
}
|
||||||
classesPath := path.Key("character").Key("classes")
|
classesPath := path.Key("character").Key("classes")
|
||||||
if len(character.Character.Classes) == 0 {
|
if len(character.Character.Classes) == 0 {
|
||||||
return &partyDomainError{path: classesPath, message: "classes must be a non-empty list"}
|
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 {
|
for index, class := range character.Character.Classes {
|
||||||
classPath := classesPath.Index(index)
|
classPath := classesPath.Index(index)
|
||||||
if err := validatePartyDisplayName(class.Name); err != "" {
|
if err := validatePartyDisplayName(class.Name); err != "" {
|
||||||
return &partyDomainError{path: classPath.Key("name"), message: 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"}
|
return &partyDomainError{path: classPath.Key("name"), message: "duplicate class"}
|
||||||
}
|
}
|
||||||
classNames = append(classNames, class.Name)
|
classNames[key] = struct{}{}
|
||||||
if class.Level != nil && *class.Level <= 0 {
|
if class.Level != nil && *class.Level <= 0 {
|
||||||
return &partyDomainError{path: classPath.Key("level"), message: "level must be a positive integer"}
|
return &partyDomainError{path: classPath.Key("level"), message: "level must be a positive integer"}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
seenNames = visibleNames
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -209,13 +211,21 @@ func validatePartyDisplayName(value string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func partyNameAmbiguous(name string, existing []string) bool {
|
// partyFoldKey selects the least rune in each Unicode simple-fold cycle,
|
||||||
for _, candidate := range existing {
|
// preserving strings.EqualFold equivalence without changing forwarded names.
|
||||||
if strings.EqualFold(name, candidate) {
|
func partyFoldKey(name string) string {
|
||||||
return true
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
key.WriteRune(least)
|
||||||
|
}
|
||||||
|
return key.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func interpretParty(document map[string]any) partyView {
|
func interpretParty(document map[string]any) partyView {
|
||||||
|
|||||||
@@ -2,12 +2,34 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go.maximumdirect.net/configkit"
|
"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) {
|
func TestPartyPreservesSharedExtensionsAndAliasPresence(t *testing.T) {
|
||||||
input := []byte(`schema_version: maximumdirect.rpg.party.v1
|
input := []byte(`schema_version: maximumdirect.rpg.party.v1
|
||||||
custom: {setting: synthetic}
|
custom: {setting: synthetic}
|
||||||
|
|||||||
Reference in New Issue
Block a user