This function returns the index of the first position that matches the regular expression AngelScript signature: int string::regexFind(const string &in regex, uint start = 0, uint &out lengthOfMatch = void)
| 381 | // AngelScript signature: |
| 382 | // int string::regexFind(const string &in regex, uint start = 0, uint &out lengthOfMatch = void) |
| 383 | static int StringRegexFind(const string& rex, asUINT start, asUINT& outLengthOfMatch, const string& str) |
| 384 | { |
| 385 | if (start >= str.length()) |
| 386 | { |
| 387 | outLengthOfMatch = 0; |
| 388 | return -1; |
| 389 | } |
| 390 | |
| 391 | // TODO: If possible add support for matching utf8 characters |
| 392 | // However on with MSVC it doesn't seem that std::regex works with utf8 |
| 393 | // This works with MSVC, but I don't want to have to convert the string to UTF-16 first because the position and length will not work |
| 394 | // https://www.regular-expressions.info/stdregex.html |
| 395 | // |
| 396 | // std::wregex pattern(L"[[:alpha:]]+"); |
| 397 | // bool result = std::regex_match(std::wstring(L"abcd�fg"), pattern); |
| 398 | // |
| 399 | // The solution from stack overflow doesn't work with MSVC |
| 400 | // https://stackoverflow.com/questions/11254232/do-c11-regular-expressions-work-with-utf-8-strings |
| 401 | // |
| 402 | // std::locale old; |
| 403 | // std::locale::global(std::locale("en_US.UTF-8")); |
| 404 | // std::regex pattern("[[:alpha:]]+", std::regex_constants::extended); |
| 405 | // bool result = std::regex_match(std::string(u8"abcd�fg"), pattern); |
| 406 | // |
| 407 | // I've tried setting the manifest to use utf8 code page but it also doesn't work with MSVC |
| 408 | // https://learn.microsoft.com/en-us/windows/apps/design/globalizing/use-utf8-code-page |
| 409 | |
| 410 | std::regex pattern(rex, std::regex_constants::ECMAScript | std::regex_constants::collate); |
| 411 | std::cmatch match; |
| 412 | bool result = std::regex_search(str.c_str() + start, str.c_str()+str.length(), match, pattern); |
| 413 | |
| 414 | if (!result) |
| 415 | { |
| 416 | outLengthOfMatch = 0; |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | outLengthOfMatch = (asUINT)match[0].length(); |
| 421 | return (int)match.prefix().length(); |
| 422 | } |
| 423 | |
| 424 | // This function returns the index of the first position where the one of the bytes in substring |
| 425 | // exists in the input string. If the characters in the substring doesn't exist in the input |
no test coverage detected