From cece5a421443de92543ec6cf2247f95f1815ed6b Mon Sep 17 00:00:00 2001 From: Brues <5278969+brues-code@users.noreply.github.com> Date: Mon, 26 Oct 2026 23:49:10 -0500 Subject: [PATCH] feat(string): add strlenutf8, strcmputf8i, strsplittable and strconcat The four Blizzard string globals still missing from the base library. strlenutf8 and strconcat are 4.3.4's (a count of non-continuation bytes, and lua_concat over the whole stack); strcmputf8i and strsplittable are the newer clients'. Text::Utf8 mirrors 4.3.4 Storm's SStrLenUTF8 and SStrCmpUTF8I, decoder and case fold included, since 1.12's Storm has neither. strsplit and strsplittable share one scan; the table form never grows the Lua stack. string.concat is the by-value twin of strconcat, as string.split is of strsplit. Adds the lua_concat binding (0x006F44E0). --- README.md | 2 +- docs/API.md | 53 ++++++++++ src/Game.cpp | 3 +- src/Game.h | 4 + src/Offsets.h | 7 ++ src/baselib/StringLib.cpp | 204 +++++++++++++++++++++++++++++++------- src/text/Utf8.cpp | 150 ++++++++++++++++++++++++++++ src/text/Utf8.h | 36 +++++++ 8 files changed, 422 insertions(+), 37 deletions(-) create mode 100644 src/text/Utf8.cpp create mode 100644 src/text/Utf8.h diff --git a/README.md b/README.md index 50104062..0e9caaa6 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ In the Frame rows, a name that starts with `:` is a method of the widget that th | [Loot](docs/API.md#loot) | `C_Loot.GetNearbyLootableUnits`, `C_Loot.GetLastScanResults`, `C_Loot.IsScanInProgress`, `C_Loot.LootAllCorpses`, `C_Loot.LootUnit`, `C_Loot.LootUnitItem`, `C_Loot.ScanNearbyLoot` | | [LootHistory](docs/API.md#loothistory) | `C_LootHistory.GetNumItems`, `C_LootHistory.GetItem`, `C_LootHistory.GetPlayerInfo`, `C_LootHistory.Clear` | | [LossOfControl](docs/API.md#lossofcontrol) | `C_LossOfControl.GetActiveLossOfControlData`, `C_LossOfControl.GetActiveLossOfControlDataCount`, `C_LossOfControl.GetSchoolLockout` | -| [Lua](docs/API.md#lua) | `collectgarbage` (5.1 options), `coroutine.create`, `coroutine.resume`, `coroutine.running`, `coroutine.status`, `coroutine.wrap`, `coroutine.yield`, `CreateFromMixins`, `math.fmod`, `math.huge`, `math.modf`, `Mixin`, `select`, `string.gmatch`, `string.gsub` (table replacement), `string.join`, `string.match`, `string.reverse`, `string.split`, `string.trim`, `strjoin`, `strmatch`, `strreplace`, `strrev`, `strsplit`, `strtrim`, `table.count`, `table.maxn`, `table.wipe`, `unpack` (range args), `xpcall` (argument forwarding) | +| [Lua](docs/API.md#lua) | `collectgarbage` (5.1 options), `coroutine.create`, `coroutine.resume`, `coroutine.running`, `coroutine.status`, `coroutine.wrap`, `coroutine.yield`, `CreateFromMixins`, `math.fmod`, `math.huge`, `math.modf`, `Mixin`, `select`, `strcmputf8i`, `strconcat`, `string.concat`, `string.gmatch`, `string.gsub` (table replacement), `string.join`, `string.match`, `string.reverse`, `string.split`, `string.trim`, `strjoin`, `strlenutf8`, `strmatch`, `strreplace`, `strrev`, `strsplit`, `strsplittable`, `strtrim`, `table.count`, `table.maxn`, `table.wipe`, `unpack` (range args), `xpcall` (argument forwarding) | | [Macros](docs/API.md#macros) | `C_Macro.CreateMacro`, `C_Macro.EditMacro`, `C_Macro.SetMacroDisplay`, `DeleteMacro` / `EditMacro` / `GetMacroInfo` / `PickupMacro` by name, `GetLooseMacroIcons`, `GetLooseMacroItemIcons`, `GetMacroIcons`, `GetMacroItem`, `GetMacroItemIcons`, `GetMacroSpell`, `StopMacro` | | [Mail](docs/API.md#mail) | `GetInboxItemLink`, `GetSendMailItemLink` | | [Map](docs/API.md#map) | `C_Map.CanSetUserWaypointOnMap`, `C_Map.ClearUserWaypoint`, `C_Map.GetAreaInfo`, `C_Map.GetAreas`, `C_Map.GetAreaTriggerInfo`, `C_Map.GetAreaTriggers`, `C_Map.GetBestMapForUnit`, `C_Map.GetFallbackWorldMapID`, `C_Map.GetMapAreaIDs`, `C_Map.GetMapArtLayers`, `C_Map.GetMapArtLayerTextures`, `C_Map.GetMapChildrenInfo`, `C_Map.GetMapInfo`, `C_Map.GetMapInfoAtPosition`, `C_Map.GetMapOverlays`, `C_Map.GetMapPosFromWorldPos`, `C_Map.GetMapRectOnMap`, `C_Map.GetMapWorldSize`, `C_Map.GetPlayerMapPosition`, `C_Map.GetUserWaypoint`, `C_Map.GetUserWaypointFromHyperlink`, `C_Map.GetUserWaypointHyperlink`, `C_Map.GetUserWaypointPositionForMap`, `C_Map.GetWorldPosFromMapPos`, `C_Map.HasUserWaypoint`, `C_Map.MapHasArt`, `C_Map.SetUserWaypoint` | diff --git a/docs/API.md b/docs/API.md index 11883b0c..e5365cbe 100644 --- a/docs/API.md +++ b/docs/API.md @@ -560,6 +560,10 @@ build instructions. - [`strtrim(str [, chars])`](#strtrimstr--chars) - [`strreplace(str, find, replace)`](#strreplacestr-find-replace) - [`string.reverse(s)` / `strrev(s)`](#stringreverses--strrevs) + - [`strlenutf8(s)`](#strlenutf8s) + - [`strcmputf8i(string1, string2)`](#strcmputf8istring1-string2) + - [`strsplittable(sep, str [, pieces])`](#strsplittablesep-str--pieces) + - [`strconcat(...)` / `string.concat(...)`](#strconcat--stringconcat) - [`math.fmod(x, y)`](#mathfmodx-y) - [`math.modf(x)`](#mathmodfx) - [`math.huge`](#mathhuge) @@ -13408,6 +13412,55 @@ string.reverse("abc") -- "cba" strrev("hello") -- "olleh" ``` +### `strlenutf8(s)` + +The number of characters in a UTF-8 string. `strlen` counts bytes, so the +two differ for text with accented or non-Latin letters. + +```lua +strlenutf8("abc") -- 3 +strlen("Ärger") -- 6 (Ä is two bytes) +strlenutf8("Ärger") -- 5 +``` + +### `strcmputf8i(string1, string2)` + +Compares two UTF-8 strings and ignores case. The result is a number: +negative when `string1` sorts first, `0` when the two match, positive when +`string2` sorts first. Only the sign has meaning. + +The case fold covers ASCII letters, the Latin-1 letters `à`..`þ`, `œ`, and +the Cyrillic alphabet. Other characters compare by code point. + +```lua +strcmputf8i("Thrall", "thrall") -- 0 +strcmputf8i("Ärger", "ärger") -- 0 +strcmputf8i("abc", "abd") < 0 -- true +table.sort(names, function(a, b) return strcmputf8i(a, b) < 0 end) +``` + +### `strsplittable(sep, str [, pieces])` + +The same split as `strsplit`, returned as one array table instead of +multiple values. The arguments have the same meaning. The table form has no +stack limit, so a string can split into any number of pieces. + +```lua +strsplittable(":", "hello:world:banana") -- { "hello", "world", "banana" } +strsplittable(",", "a,b,c,d", 2) -- { "a", "b,c,d" } +``` + +### `strconcat(...)` / `string.concat(...)` + +Joins every argument into one string with nothing between them, like the +`..` operator. Numbers convert to text, and any other value is an error. +With no arguments it returns `""`. + +```lua +strconcat("a", "b", 1) -- "ab1" +strconcat() -- "" +``` + ### `math.fmod(x, y)` The floating-point remainder of `x / y` (the quotient truncated toward zero), diff --git a/src/Game.cpp b/src/Game.cpp index 977cb3d9..5b9b3f9a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -96,7 +96,8 @@ namespace Lua { F(ArgError, luaL_argerror, LUAL_ARG_ERROR) \ F(SetN, luaL_setn, LUAL_SETN) \ F(Ref, luaL_ref, LUA_REF_REF) \ - F(CheckStack, lua_checkstack, LUA_CHECK_STACK) + F(CheckStack, lua_checkstack, LUA_CHECK_STACK) \ + F(Concat, lua_concat, LUA_CONCAT) #define CLASSICAPI_BIND_LUA(Name, Typedef, Offset) \ const Typedef##_t Name = reinterpret_cast(Offsets::Offset); diff --git a/src/Game.h b/src/Game.h index 1d2158bb..dcd82a77 100644 --- a/src/Game.h +++ b/src/Game.h @@ -314,6 +314,9 @@ using luaL_setn_t = void(__fastcall *)(void *L, int t, int n); // a value across calls; read it back with RawGetI(L, REGISTRY_INDEX, ref). using luaL_ref_t = int(__fastcall *)(void *L, int t); using lua_checkstack_t = int(__fastcall *)(void *L, int size); +// Pops the top `n` values and pushes their `..` concatenation; n == 0 pushes +// "" and n == 1 leaves the value untouched. +using lua_concat_t = void(__fastcall *)(void *L, int n); extern const lua_isnumber_t IsNumber; extern const lua_isstring_t IsString; @@ -369,6 +372,7 @@ extern const luaL_argerror_t ArgError; extern const luaL_setn_t SetN; extern const luaL_ref_t Ref; extern const lua_checkstack_t CheckStack; +extern const lua_concat_t Concat; // Returns the global `lua_State *` (read on demand from the engine's global). // Callable outside a Lua callback, e.g. during LoadScriptFunctions setup. diff --git a/src/Offsets.h b/src/Offsets.h index 5a2f0c21..b74b2d3a 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -7999,6 +7999,13 @@ enum Offsets { // number of results (e.g. `strsplit`) — pushing past the ~20-slot // C-call guarantee without growing corrupts the stack. LUA_CHECK_STACK = 0x006F2F30, + // `lua_concat(L, n)` — `void __fastcall(L /*ecx*/, n /*edx*/)`. Pops the + // top `n` values and pushes their concatenation: n >= 2 runs + // `luaV_concat(L, n, top-1)` (the `..` operator, so numbers convert and + // anything else raises "attempt to concatenate"), n == 1 leaves the value + // as it is, n == 0 pushes "". A GC step may run first. Decompiled + // 0x006F44E0; catalogued ★ in docs/LuaCAPI.md. Backs `strconcat`. + LUA_CONCAT = 0x006F44E0, // `str_find` — the Lua 5.0 string-library `string.find` C function // (`int __fastcall(void *L)`), entry in the strlib luaL_reg table at // `0x00822dd0`. Returns `1` (nil pushed, no match), `2` (start, end), diff --git a/src/baselib/StringLib.cpp b/src/baselib/StringLib.cpp index 64e25969..9f0cb980 100644 --- a/src/baselib/StringLib.cpp +++ b/src/baselib/StringLib.cpp @@ -22,6 +22,15 @@ // - `strtrim(str [, chars])` — WoW global; trim a char set. // - `strreplace(str, find, replace)` — plain-text replace (see below). // - `string.reverse(s)` / `strrev(s)` — 5.1 string function, missing in 5.0. +// - `strlenutf8(s)` — WoW global; characters, not bytes. +// - `strcmputf8i(a, b)` — WoW global; case-insensitive UTF-8 order. +// - `strsplittable(sep, str [, pieces])`— WoW global; `strsplit` into a table. +// - `strconcat(...)` / `string.concat` — WoW global; `..` over every argument. +// +// The two UTF-8 globals are 4.3.4's `Script_strlenutf8` and the newer +// clients' `strcmputf8i`, each a thin push over a Storm helper 1.12 does not +// have (`SStrLenUTF8` / `SStrCmpUTF8I`) — `Text::Utf8` mirrors those. +// `strconcat` is 4.3.4's: `lua_concat` over the whole stack. // // The two pattern `string.*` ones reuse machinery 5.0 already ships: // - `match` is `find` returning captures / the whole match instead of the @@ -38,6 +47,7 @@ #include "Game.h" #include "Offsets.h" +#include "text/Utf8.h" #include #include @@ -92,33 +102,17 @@ int __fastcall Script_string_match(void *L) { const auto Script_string_gmatch = reinterpret_cast(Offsets::FUN_LUA_STR_GFIND); -// `strsplit(sep, str [, pieces])` — the WoW global (also aliased as -// `string.split`), split `str` on ANY character in `sep` and return the -// pieces as multiple values. `pieces > 0` caps the result count, with the -// unsplit remainder as the final piece; `0` / omitted = unlimited. Ported -// from 3.3.5's `strsplit` (FUN_00816a60) with one deliberate change: we do -// NOT `lua_settop(L, 0)` up front. Popping the args would leave the source -// string unreferenced, so a GC step triggered by a `pushlstring` could free -// the very buffer we're still scanning. Keeping the args on the stack keeps -// the source GC-rooted; Lua returns the top N values (our pieces) regardless -// of the args sitting below them. Each push is guarded by `lua_checkstack` -// (this pushes an unbounded number of results), erroring like 3.3.5 on -// genuine stack exhaustion. -int __fastcall Script_strsplit(void *L) { - const char *sep = Game::Lua::ToString(L, 1); - const char *str = Game::Lua::ToString(L, 2); - if (sep == nullptr || str == nullptr) { - Game::Lua::Error(L, "Usage: strsplit(\"separators\", str [, pieces])"); - return 0; // unreachable - } - const int pieces = - Game::Lua::IsNumber(L, 3) ? static_cast(Game::Lua::ToNumber(L, 3)) : 0; - +// The scan behind `strsplit` and `strsplittable`: splits `str` on ANY +// character in `sep` and hands each piece to `emit(start, length)` in order, +// the unsplit remainder last. `pieces > 0` caps the count, with the +// remainder (delimiters and all) as the final piece; `0` is unlimited. +// Only scans for separators while unlimited or the cap hasn't been reached; +// `pieces == 1` (or <= 0 but non-zero) yields the whole string as a single +// piece — matches 3.3.5's `strsplit` (FUN_00816a60). +template +void SplitPieces(const char *sep, const char *str, int pieces, Emit emit) { const char *segStart = str; int count = 0; - // Only scan for separators while unlimited (pieces == 0) or the cap - // hasn't been reached (pieces > 1). pieces == 1 (or <= 0 but non-zero) - // yields the whole string as a single piece — matches 3.3.5. if (pieces == 0 || pieces > 1) { for (const char *p = str; *p != '\0'; ++p) { bool isSep = false; @@ -131,23 +125,108 @@ int __fastcall Script_strsplit(void *L) { if (!isSep) continue; ++count; - if (Game::Lua::CheckStack(L, count) == 0) { - Game::Lua::Error(L, "strsplit(): Stack overflow"); - return 0; // unreachable - } - Game::Lua::PushLString(L, segStart, - static_cast(p - segStart)); + emit(segStart, static_cast(p - segStart)); segStart = p + 1; if (count == pieces - 1) break; // cap hit; remainder becomes the final piece } } - if (Game::Lua::CheckStack(L, count + 1) == 0) { - Game::Lua::Error(L, "strsplit(): Stack overflow"); + emit(segStart, static_cast(std::strlen(segStart))); +} + +// `strsplit(sep, str [, pieces])` — the WoW global (also aliased as +// `string.split`): the pieces as multiple values. Ported from 3.3.5 with one +// deliberate change: we do NOT `lua_settop(L, 0)` up front. Popping the args +// would leave the source string unreferenced, so a GC step triggered by a +// `pushlstring` could free the very buffer we're still scanning. Keeping the +// args on the stack keeps the source GC-rooted; Lua returns the top N values +// (our pieces) regardless of the args sitting below them. Each push is +// guarded by `lua_checkstack` (this pushes an unbounded number of results), +// erroring like 3.3.5 on genuine stack exhaustion. +int __fastcall Script_strsplit(void *L) { + const char *sep = Game::Lua::ToString(L, 1); + const char *str = Game::Lua::ToString(L, 2); + if (sep == nullptr || str == nullptr) { + Game::Lua::Error(L, "Usage: strsplit(\"separators\", str [, pieces])"); return 0; // unreachable } - Game::Lua::PushString(L, segStart); // final piece (remainder to end) - return count + 1; + const int pieces = + Game::Lua::IsNumber(L, 3) ? static_cast(Game::Lua::ToNumber(L, 3)) : 0; + + int count = 0; + SplitPieces(sep, str, pieces, [L, &count](const char *start, unsigned int len) { + ++count; + if (Game::Lua::CheckStack(L, count) == 0) { + Game::Lua::Error(L, "strsplit(): Stack overflow"); + return; // unreachable + } + Game::Lua::PushLString(L, start, len); + }); + return count; +} + +// `strsplittable(sep, str [, pieces])` — the same split as one array +// table. Every piece goes to the next integer key of a fresh table, so the +// Lua stack never grows and no piece count is too many — the point of the +// table form. The args stay on the stack (GC-rooting the source, as above); +// the table sits above them at index 4. +int __fastcall Script_strsplittable(void *L) { + const char *sep = Game::Lua::ToString(L, 1); + const char *str = Game::Lua::ToString(L, 2); + if (sep == nullptr || str == nullptr) { + Game::Lua::Error(L, "Usage: strsplittable(\"separators\", str [, pieces])"); + return 0; // unreachable + } + const int pieces = + Game::Lua::IsNumber(L, 3) ? static_cast(Game::Lua::ToNumber(L, 3)) : 0; + + Game::Lua::SetTop(L, 3); + Game::Lua::NewTable(L); // 4 + int count = 0; + SplitPieces(sep, str, pieces, [L, &count](const char *start, unsigned int len) { + Game::Lua::PushNumber(L, static_cast(++count)); + Game::Lua::PushLString(L, start, len); + Game::Lua::RawSet(L, 4); + }); + return 1; +} + +// `strlenutf8(s)` — 4.3.4's `Script_strlenutf8`: `SStrLenUTF8` on the +// string, the count of characters (code points) rather than bytes. Same +// usage text and `lua_isstring` gate as the original. +int __fastcall Script_strlenutf8(void *L) { + if (!Game::Lua::IsString(L, 1)) { + Game::Lua::Error(L, "Usage: strlenutf8(string)"); + return 0; // unreachable + } + Game::Lua::PushNumber( + L, static_cast(Text::Utf8::Length(Game::Lua::ToString(L, 1)))); + return 1; +} + +// `strcmputf8i(string1, string2)` — the newer clients' global (its usage +// text is in their binaries) over `SStrCmpUTF8I`: a case-insensitive sort +// order for UTF-8 text. The number is the first folded code-point +// difference, as Storm returns it; FrameXML only ever tests the sign (`< 0` +// in sort comparators, `== 0` for a name match). +int __fastcall Script_strcmputf8i(void *L) { + if (!Game::Lua::IsString(L, 1) || !Game::Lua::IsString(L, 2)) { + Game::Lua::Error(L, "Usage: strcmputf8i(string1, string2)"); + return 0; // unreachable + } + const int result = + Text::Utf8::CompareI(Game::Lua::ToString(L, 1), Game::Lua::ToString(L, 2)); + Game::Lua::PushNumber(L, static_cast(result)); + return 1; +} + +// `strconcat(...)` / `string.concat(...)` — 4.3.4's `Script_strconcat`: +// `lua_concat` over everything on the stack, i.e. the `..` operator applied +// to every argument (numbers convert, anything else raises the concatenate +// error), `""` for no arguments, and a single argument handed back as it is. +int __fastcall Script_strconcat(void *L) { + Game::Lua::Concat(L, Game::Lua::GetTop(L)); + return 1; } // `strjoin(delimiter, ...)` — the inverse of `strsplit`: concatenate the @@ -384,6 +463,55 @@ int __fastcall Script_string_gsub(void *L) { return engineGsub(L); } +// --- Documentation ---------------------------------------------------------- + +const Game::Doc::Field kStrlenutf8Args[] = { + Game::Doc::Req("string", "string", "UTF-8 text."), +}; +const Game::Doc::Field kStrlenutf8Rets[] = { + Game::Doc::Req("length", "number", "The number of characters; strlen counts bytes."), +}; +const Game::Doc::Function kStrlenutf8{ + "The number of characters in a UTF-8 string, not the number of bytes.", + kStrlenutf8Args, kStrlenutf8Rets, "LuaGlobals"}; + +const Game::Doc::Field kStrcmputf8iArgs[] = { + Game::Doc::Req("string1", "string"), + Game::Doc::Req("string2", "string"), +}; +const Game::Doc::Field kStrcmputf8iRets[] = { + Game::Doc::Req("result", "number", + "Negative when string1 sorts first, 0 when the two match ignoring " + "case, positive when string2 sorts first."), +}; +const Game::Doc::Function kStrcmputf8i{ + "Compares two UTF-8 strings ignoring case, for sorting and matching.", + kStrcmputf8iArgs, kStrcmputf8iRets, "LuaGlobals"}; + +const Game::Doc::Field kStrsplittableArgs[] = { + Game::Doc::Req("delimiter", "string", "Each character is a delimiter."), + Game::Doc::Req("str", "string"), + Game::Doc::Opt("pieces", "number", "0", + "Maximum number of pieces; the last one holds the rest of the string. " + "0 is unlimited."), +}; +const Game::Doc::Field kStrsplittableRets[] = { + Game::Doc::Req("chunks", "table", "Array of the pieces, in order."), +}; +const Game::Doc::Function kStrsplittable{ + "Splits a string on delimiter characters and returns the pieces as an array.", + kStrsplittableArgs, kStrsplittableRets, "LuaGlobals"}; + +const Game::Doc::Field kStrconcatArgs[] = { + Game::Doc::Vararg("string", "Strings or numbers."), +}; +const Game::Doc::Field kStrconcatRets[] = { + Game::Doc::Req("result", "string"), +}; +const Game::Doc::Function kStrconcat{ + "Joins every argument into one string with nothing between them.", + kStrconcatArgs, kStrconcatRets, "LuaGlobals"}; + void RegisterFns() { Game::Lua::RegisterTableFunction("string", "match", &Script_string_match); Game::Lua::RegisterTableFunction("string", "gmatch", Script_string_gmatch); @@ -392,6 +520,7 @@ void RegisterFns() { Game::Lua::RegisterTableFunction("string", "split", &Script_strsplit); Game::Lua::RegisterTableFunction("string", "trim", &Script_strtrim); Game::Lua::RegisterTableFunction("string", "join", &Script_strjoin); + Game::Lua::RegisterTableFunction("string", "concat", &Script_strconcat, &kStrconcat); // 4.3.4's compat.lua binds each short global and its string-table twin // to ONE function by assignment (`gsub = str.gsub`, `strmatch = // str.match`, `strrev = str.reverse`, `str.trim = strtrim`, `str.split = @@ -407,7 +536,12 @@ void RegisterFns() { Game::Lua::RegisterGlobalAlias("strjoin", "string", "join"); Game::Lua::RegisterGlobalAlias("strtrim", "string", "trim"); Game::Lua::RegisterGlobalAlias("strrev", "string", "reverse"); + Game::Lua::RegisterGlobalAlias("strconcat", "string", "concat", &kStrconcat); Game::Lua::RegisterGlobalFunction("strreplace", &Script_strreplace); + Game::Lua::RegisterGlobalFunction("strlenutf8", &Script_strlenutf8, &kStrlenutf8); + Game::Lua::RegisterGlobalFunction("strcmputf8i", &Script_strcmputf8i, &kStrcmputf8i); + Game::Lua::RegisterGlobalFunction("strsplittable", &Script_strsplittable, + &kStrsplittable); } // Both states are Lua 5.0 and equally missing these; RegisterTableFunction diff --git a/src/text/Utf8.cpp b/src/text/Utf8.cpp new file mode 100644 index 00000000..7270799b --- /dev/null +++ b/src/text/Utf8.cpp @@ -0,0 +1,150 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +// Mirrors of 4.3.4 Storm's `SStrLenUTF8` (0x004B3320) and `SStrCmpUTF8I` +// (0x004B2F90) with its decoder `SStrCmpGetNextTextUpper` (0x004B2E30), +// transcribed from `Engine/Source/Storm/Source/SStr.cpp` in the 4.3.4 +// decompilation, quirks included: a sequence cut short by the terminator +// decodes as 0, a code point above U+FFFF folds to U+FFFD, and the Cyrillic +// fold shifts А..Е down by one so that Ё and ё land on Е's slot. Whatever +// the newer client answers, this answers. + +#include "text/Utf8.h" + +#include + +namespace Text::Utf8 { + +namespace { + +// Total sequence length by lead byte (0 for the terminator). Continuation +// and invalid lead bytes count as one-byte sequences, as in Storm. +const uint8_t kBytesFromUtf8[256] = { + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, +}; + +// The lead-byte marker bits accumulated by the shift-and-add decode below, +// indexed by sequence length. +const uint32_t kOffsetsFromUtf8[7] = { + 0x00000000, 0x00000000, 0x00003080, 0x000E2080, 0x03C82080, 0xFA082080, 0x82082080, +}; + +// `SStrCmpGetNextTextUpper`: decodes the code point at `*s`, advances `*s` +// past it, and writes the case-folded value to `*upper`. Returns the +// sequence length, or 0 when the terminator cut the sequence short (the +// pointer then rests on the terminator and `*upper` is 0). +int32_t NextUpper(const char **s, uint32_t *upper) { + const int32_t bytes = kBytesFromUtf8[static_cast(**s)]; + uint32_t code = 0; + *upper = 0; + + switch (bytes) { + case 6: + code += static_cast(*(*s)++); + if (**s == '\0') + return 0; + code <<= 6; + [[fallthrough]]; + case 5: + code += static_cast(*(*s)++); + if (**s == '\0') + return 0; + code <<= 6; + [[fallthrough]]; + case 4: + code += static_cast(*(*s)++); + if (**s == '\0') + return 0; + code <<= 6; + [[fallthrough]]; + case 3: + code += static_cast(*(*s)++); + if (**s == '\0') + return 0; + code <<= 6; + [[fallthrough]]; + case 2: + code += static_cast(*(*s)++); + if (**s == '\0') + return 0; + code <<= 6; + [[fallthrough]]; + case 1: + code += static_cast(*(*s)++); + break; + default: + break; + } + + code -= kOffsetsFromUtf8[bytes]; + if (code > 0xFFFF) + code = 0xFFFD; + + uint32_t ch = code; + if (bytes == 1) { + if (ch >= 'a' && ch <= 'z') + ch -= 32; + } else if (bytes == 2) { + if (ch >= 0xE0 && ch <= 0xFE) { + ch -= 32; // Latin-1 à..þ → À..Þ + } else if (ch == 0x153) { + *upper = 0x152; // œ → Œ + return bytes; + } else if (ch == 0x401 || ch == 0x451) { + *upper = 0x415; // Ё / ё → Е's slot + return bytes; + } else if (ch >= 0x410 && ch <= 0x415) { + *upper = ch - 1; // А..Е shift down one, making room for Ё above + return bytes; + } else if (ch >= 0x430 && ch <= 0x44F) { + *upper = ch - (ch <= 0x435 ? 1 : 0) - 32; // а..я → the same slots + return bytes; + } + } + *upper = ch; + return bytes; +} + +} // namespace + +size_t Length(const char *s) { + if (s == nullptr) + return 0; + size_t length = 0; + for (; *s != '\0'; ++s) { + if ((static_cast(*s) & 0xC0) != 0x80) + ++length; + } + return length; +} + +int CompareI(const char *a, const char *b) { + while (*a != '\0' || *b != '\0') { + uint32_t upperA; + NextUpper(&a, &upperA); + uint32_t upperB; + NextUpper(&b, &upperB); + if (upperA != upperB) + return static_cast(upperA - upperB); + } + return 0; +} + +} // namespace Text::Utf8 diff --git a/src/text/Utf8.h b/src/text/Utf8.h new file mode 100644 index 00000000..08659810 --- /dev/null +++ b/src/text/Utf8.h @@ -0,0 +1,36 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU General Public License as published by the Free Software Foundation, either +// version 3 of the License, or (at your option) any later version. +// +// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +#include + +// The two Storm UTF-8 string helpers that 1.12's Storm does not have +// (its only UTF-8 routine is `SUniConvertUTF8to16`), mirrored from 4.3.4's +// `SStr.cpp` so the Lua functions built on them answer exactly as the newer +// clients do. Both stop at the first NUL, as the originals do. +namespace Text::Utf8 { + +// `SStrLenUTF8(string)`: the number of code points — every byte that is not +// a continuation byte (`10xxxxxx`) starts one. +size_t Length(const char *s); + +// `SStrCmpUTF8I(string1, string2, STORM_MAX_STR)`: case-insensitive compare. +// Decodes one code point at a time with 4.3.4's `SStrCmpGetNextTextUpper` +// fold (ASCII and Latin-1 letters, œ, and the Cyrillic alphabet; every other +// code point compares as itself) and returns the first difference of the +// folded values: negative when `a` sorts first, 0 when equal, positive when +// `b` sorts first. Only the sign carries meaning. +int CompareI(const char *a, const char *b); + +} // namespace Text::Utf8