23 return (c >= L
'a' && c <= L
'z') || (c >= L
'A' && c <= L
'Z') || (c >= L
'0' && c <= L
'9') ||
24 c == L
'\'' || (c >= 0xC0 && c < 0x2E80);
27inline wchar_t LuxFoldCase(
wchar_t c) {
return (c >= L
'A' && c <= L
'Z') ? (wchar_t)(c - L
'A' + L
'a') : c; }
29inline bool LuxLongerFirst(
const std::wstring& a,
const std::wstring& b) {
return a.size() > b.size(); }
34 std::vector<std::wstring> vOut;
36 while(lStart <= asList.size())
38 size_t lEnd = asList.find(L
'|', lStart);
39 if(lEnd == std::wstring::npos) lEnd = asList.size();
40 std::wstring s = asList.substr(lStart, lEnd - lStart);
41 size_t a = s.find_first_not_of(L
" \t\r\n"), b = s.find_last_not_of(L
" \t\r\n");
42 if(a != std::wstring::npos) vOut.push_back(s.substr(a, b - a + 1));
50inline size_t LuxKeywordMatchAt(
const std::wstring& asText,
size_t alPos,
const std::wstring& asWord)
52 if(asWord.empty() || alPos + asWord.size() > asText.size())
return 0;
53 for(
size_t i = 0; i < asWord.size(); ++i)
57 size_t lEnd = alPos + asWord.size();
58 if(lEnd < asText.size() &&
LuxFoldCase(asText[lEnd]) == L
's') ++lEnd;
59 else if(lEnd + 1 < asText.size() &&
LuxFoldCase(asText[lEnd]) == L
'e' &&
LuxFoldCase(asText[lEnd + 1]) == L
's') lEnd += 2;
60 if(lEnd < asText.size() &&
LuxIsWordChar(asText[lEnd]))
return 0;
65inline std::vector<cLuxTextSpan>
LuxKeywordSpans(
const std::wstring& asText,
const std::vector<std::wstring>& avKeywords)
67 std::vector<cLuxTextSpan> vSpans;
68 size_t lPlain = 0, i = 0;
69 while(i < asText.size())
72 for(
size_t k = 0; k < avKeywords.size() && lLen == 0; ++k) lLen =
LuxKeywordMatchAt(asText, i, avKeywords[k]);
73 if(lLen == 0) { ++i;
continue; }
74 if(i > lPlain) {
cLuxTextSpan p = { lPlain, i - lPlain,
false }; vSpans.push_back(p); }
80 if(lPlain < asText.size()) {
cLuxTextSpan p = { lPlain, asText.size() - lPlain,
false }; vSpans.push_back(p); }
bool LuxLongerFirst(const std::wstring &a, const std::wstring &b)
wchar_t LuxFoldCase(wchar_t c)
std::vector< std::wstring > LuxParseKeywords(const std::wstring &asList)
"fuse box| winch |" -> {"fuse box", "winch"}, longest first.
size_t LuxKeywordMatchAt(const std::wstring &asText, size_t alPos, const std::wstring &asWord)
Length of the keyword match at alPos (0: none).
bool LuxIsWordChar(wchar_t c)
std::vector< cLuxTextSpan > LuxKeywordSpans(const std::wstring &asText, const std::vector< std::wstring > &avKeywords)
The text split into plain and keyword runs, in order, covering all of it.