2323#include < cstring>
2424#include < array>
2525#include < algorithm>
26+ #include < set>
2627
2728#include < pugixml.hpp>
2829
@@ -480,8 +481,8 @@ namespace tivars::TypeHandlers
480481 struct TokenNames
481482 {
482483 std::array<std::string, TH_Tokenized::LANG_MAX> display{};
483- std::array<std::vector <std::string>, TH_Tokenized::LANG_MAX> variants{};
484- std::array<std::vector <std::string>, TH_Tokenized::LANG_MAX> accessibles{};
484+ std::array<std::set <std::string>, TH_Tokenized::LANG_MAX> variants{};
485+ std::array<std::set <std::string>, TH_Tokenized::LANG_MAX> accessibles{};
485486 };
486487
487488 std::unordered_map<uint16_t , TokenNames> tokens_BytesToNames;
@@ -548,11 +549,11 @@ namespace tivars::TypeHandlers
548549 continue ;
549550 }
550551
551- if (strCursorPos + 1 < str.length () && str[strCursorPos + 1 ] == ' \\ ' && tokens_NameToBytes.contains (backslashStr))
552+ const auto backslashTokenIt = tokens_NameToBytes.find (backslashStr);
553+ if (strCursorPos + 1 < str.length () && str[strCursorPos + 1 ] == ' \\ ' && backslashTokenIt != tokens_NameToBytes.end ())
552554 {
553- const uint16_t tokenValue = tokens_NameToBytes[backslashStr];
554- onToken (" \\\\ " , tokenValue);
555- state.lastTokenBytes = tokenValue;
555+ onToken (" \\\\ " , backslashTokenIt->second );
556+ state.lastTokenBytes = backslashTokenIt->second ;
556557 strCursorPos++;
557558 } else {
558559 state.isInCustomName = false ;
@@ -623,12 +624,12 @@ namespace tivars::TypeHandlers
623624 break ;
624625 }
625626
626- if (tokens_NameToBytes.contains (currentSubString))
627+ const auto tokenIt = tokens_NameToBytes.find (currentSubString);
628+ if (tokenIt != tokens_NameToBytes.end ())
627629 {
628- uint16_t tokenValue = tokens_NameToBytes[currentSubString];
629- onToken (currentSubString, tokenValue);
630+ onToken (currentSubString, tokenIt->second );
630631 strCursorPos += currentLength - 1 ;
631- state.lastTokenBytes = tokenValue ;
632+ state.lastTokenBytes = tokenIt-> second ;
632633 matched = true ;
633634 break ;
634635 }
@@ -673,19 +674,6 @@ namespace tivars::TypeHandlers
673674 [](const std::string&) {});
674675 }
675676
676- static void append_unique_string (std::vector<std::string>& list, const std::string& value)
677- {
678- if (value.empty ())
679- {
680- return ;
681- }
682-
683- if (std::find (list.begin (), list.end (), value) == list.end ())
684- {
685- list.push_back (value);
686- }
687- }
688-
689677 static bool append_can_merge_with_previous_token (const std::string& existing, const std::string& appended)
690678 {
691679 if (existing.empty () || appended.empty () || lengthOfLongestTokenName < 2 )
@@ -710,7 +698,7 @@ namespace tivars::TypeHandlers
710698 return false ;
711699 }
712700
713- static std::vector <std::string> get_detok_alias_candidates (uint16_t bytesKey, uint8_t langIdx, const std::string& primaryDisplay)
701+ static std::set <std::string> get_detok_alias_candidates (uint16_t bytesKey, uint8_t langIdx, const std::string& primaryDisplay)
714702 {
715703 using TH_Tokenized::LANG_EN;
716704
@@ -721,14 +709,14 @@ namespace tivars::TypeHandlers
721709 }
722710
723711 const TokenNames& tokenNames = it->second ;
724- std::vector <std::string> candidates;
712+ std::set <std::string> candidates;
725713 auto append_aliases = [&](const auto & aliases, uint8_t idx)
726714 {
727715 for (const auto & candidate : aliases[idx])
728716 {
729717 if (candidate != primaryDisplay)
730718 {
731- append_unique_string ( candidates, candidate);
719+ candidates. insert ( candidate);
732720 }
733721 }
734722 };
@@ -763,13 +751,11 @@ namespace tivars::TypeHandlers
763751 return display;
764752 }
765753
766- const std::vector<std::string>* accessibles = &tokenNames.accessibles [langIdx];
767- if (accessibles->empty () && langIdx != LANG_EN)
768- {
769- accessibles = &tokenNames.accessibles [LANG_EN];
770- }
754+ const auto & accessibles = tokenNames.accessibles [langIdx].empty () && langIdx != LANG_EN
755+ ? tokenNames.accessibles [LANG_EN]
756+ : tokenNames.accessibles [langIdx];
771757
772- return accessibles-> empty () ? display : accessibles-> front ();
758+ return accessibles. empty () ? display : * accessibles. begin ();
773759 }
774760
775761 // Shared XML parsing routine used by both standard and Qt/CEmu builds
@@ -826,20 +812,22 @@ namespace tivars::TypeHandlers
826812 const char * code = lang.attribute (" code" ).as_string (" " );
827813 const uint8_t langIdx = std::strcmp (code, " fr" ) == 0 ? LANG_FR : LANG_EN;
828814
829- for (const auto * which : { " variant " , " accessible " } )
815+ auto register_alias_nodes = [&] (const char * nodeName, auto & aliasList )
830816 {
831- for (const xml_node& v : lang.children (which ))
817+ for (const xml_node& v : lang.children (nodeName ))
832818 {
833819 const std::string s = v.child_value ();
834820 if (!s.empty ())
835821 {
836- auto & tokenNames = tokens_BytesToNames[bytes];
837- auto & aliasList = std::strcmp (which, " variant" ) == 0 ? tokenNames.variants [langIdx] : tokenNames.accessibles [langIdx];
838- append_unique_string (aliasList, s);
822+ aliasList.insert (s);
839823 register_token_lookup_name (s, bytes);
840824 }
841825 }
842- }
826+ };
827+
828+ auto & tokenNames = tokens_BytesToNames[bytes];
829+ register_alias_nodes (" accessible" , tokenNames.accessibles [langIdx]);
830+ register_alias_nodes (" variant" , tokenNames.variants [langIdx]);
843831 }
844832 }
845833 };
@@ -1324,13 +1312,7 @@ namespace tivars::TypeHandlers
13241312 bytesKey = nextToken + (currentToken << 8 );
13251313 }
13261314
1327- std::string tokStr;
1328- if (tokens_BytesToNames.contains (bytesKey))
1329- {
1330- tokStr = get_detok_primary_string (bytesKey, langIdx, options);
1331- } else {
1332- tokStr = format_raw_token_escape (bytesKey);
1333- }
1315+ std::string tokStr = get_detok_primary_string (bytesKey, langIdx, options);
13341316 if (fromPrettified)
13351317 {
13361318 tokStr = prettify_token_string (tokStr);
@@ -1347,13 +1329,7 @@ namespace tivars::TypeHandlers
13471329 return " " ;
13481330 }
13491331
1350- std::string tokStr;
1351- if (tokens_BytesToNames.contains (tokenBytes))
1352- {
1353- tokStr = get_detok_primary_string (tokenBytes, LANG_EN, {});
1354- } else {
1355- tokStr = format_raw_token_escape (tokenBytes);
1356- }
1332+ std::string tokStr = get_detok_primary_string (tokenBytes, LANG_EN, {});
13571333
13581334 tokStr = prettify_token_string (tokStr);
13591335
@@ -1414,13 +1390,7 @@ namespace tivars::TypeHandlers
14141390 i++;
14151391 }
14161392
1417- std::string tokStr;
1418- if (tokens_BytesToNames.contains (bytesKey))
1419- {
1420- tokStr = get_detok_primary_string (bytesKey, langIdx, options);
1421- } else {
1422- tokStr = format_raw_token_escape (bytesKey);
1423- }
1393+ std::string tokStr = get_detok_primary_string (bytesKey, langIdx, options);
14241394 if (fromPrettified)
14251395 {
14261396 tokStr = prettify_token_string (tokStr);
0 commit comments