Files
2026-11-23 03:28:32 +02:00

38 lines
1.5 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
// Tortoise (be0706) name case folding: normalizePlayerName lowers with shared/Util.h wcharToLower
// (ASCII, Latin-1, Latin Extended-A even/odd pairs, U+1E9E, Cyrillic). Names are compared by
// folding both sides with exactly those tables; invalid UTF-8 never matches.
#pragma once
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
namespace gateway {
inline char32_t tortoise_wchar_to_lower(char32_t c){
if(c>='A' && c<='Z')return c+0x20;
if(c>=0x00C0 && c<=0x00D6)return c+0x20;
if(c>=0x00D8 && c<=0x00DE)return c+0x20;
if(c>=0x0100 && c<=0x012E && c%2==0)return c+1;
if(c==0x1E9E)return 0x00DF;
if(c==0x0401)return 0x0451;
if(c>=0x0410 && c<=0x042F)return c+0x20;
return c;
}
// Folded code points, or nullopt for malformed/overlong/non-BMP UTF-8 (Tortoise works in 16-bit wchar).
inline std::optional<std::u32string> tortoise_fold_name(std::string_view s){
std::u32string out;
for(size_t i=0;i<s.size();){
const auto b=uint8_t(s[i]);char32_t c;size_t n;
if(b<0x80){c=b;n=1;}
else if((b&0xE0)==0xC0){c=b&0x1F;n=2;}
else if((b&0xF0)==0xE0){c=b&0x0F;n=3;}
else return std::nullopt;
if(i+n>s.size())return std::nullopt;
for(size_t k=1;k<n;++k){const auto x=uint8_t(s[i+k]);if((x&0xC0)!=0x80)return std::nullopt;c=(c<<6)|(x&0x3F);}
if((n==2 && c<0x80) || (n==3 && c<0x800) || (c>=0xD800 && c<=0xDFFF))return std::nullopt;
out.push_back(tortoise_wchar_to_lower(c));i+=n;
}
return out;
}
}