Files
FFF_vtt_frozen/server/text.nim
T

40 lines
2.1 KiB
Nim

## The one copy of the text rules of DESIGN 6.5: a stored text field is trimmed, 1..maxLen code
## points and free of control characters. Shared by the character/token names (tokens.nim,
## characters.nim), the property keys (props.nim) and the player names (auth.nim), so they cannot
## drift apart.
##
## Player names are the one exception to "refuse untrimmed input": they are typed by a human at
## sign-in, so the server trims the value (and requires the trimmed form, which then is what it
## stores) and applies the same length/control rules (6.5). Character and plain-token names are
## GM-authored world text and must already be in that form.
##
## Deviation, recorded FIXED (owner-delegated) in DESIGN 6.5: 6.5 also asks for NFC normalisation of
## a player name. Nim 2.2.12's stdlib carries no Unicode normalisation (it lives in an external
## package that the pinned dependency set does not include), so a name is stored as typed, trimmed,
## and compared case-insensitively. The practical consequence is that two visually identical names
## written with precomposed vs combining accents count as different names; nothing else changes.
import std/strutils
import std/unicode as uni
proc hasControlChar*(s: string): bool =
for r in s.runes:
if r.int < 0x20 or r.int == 0x7f: return true
proc validText*(s: string, maxLen: int): bool =
## Trimmed, 1..maxLen code points, no control characters.
s.len > 0 and s == s.strip and s.runeLen in 1 .. maxLen and not hasControlChar(s)
proc normalizedPlayerName*(raw: string): string =
## DESIGN 6.5: the form a player name is stored in. Trimming is the normalisation the server can
## apply without a Unicode table (see the module comment).
raw.strip
proc validPlayerName*(normalized: string): bool =
## The rule of 6.5 for a player's display name: 1..32 code points, no control characters.
normalized.len > 0 and normalized.runeLen in 1 .. 32 and not hasControlChar(normalized)
proc sameName*(a, b: string): bool =
## Names are unique regardless of case (6.5 `name-taken`); Latin case folding is enough for Q17's
## glyph set.
uni.toLower(a) == uni.toLower(b)