0

I try to check if variable ends with expression.

My current code is this:

(strchr(variable.data(), tolower(expression[0])) || strchr(variable.data(), toupper(expression[0]))) && _stricmp((strrchr(variable.data(), tolower(expression[0]))? strrchr(variable.data(), tolower(expression[0])) : strrchr(variable.data(), toupper(expression[0]))), expression.data()) == 0

It works. But with a case-insensitive version of strrchr and strchr I can simlpify it. I also need the case-insensitive versions of wcsrchr and wcsrchr for UTF16-strings:

(wcschr(variable.data(), tolower(expression[0])) || wcschr(variable.data(), toupper(expression[0]))) && _wcsicmp((wcsrchr(variable.data(), tolower(expression[0])) ? wcsrchr(variable.data(), tolower(expression[0])) : wcsrchr(variable.data(), toupper(expression[0]))), expression.data()) == 0

I´m open for other simplifications.

I can´t write additional methods because the mainprogram is a vs-extension written in c#.

ojesseus1
  • 43
  • 5
  • 1
    Your expression would be simplified if both the variable and expression were converted to the same case first. However, you could consider constructing a regular expression match and specify matching to be case insensitive. https://en.cppreference.com/w/cpp/regex/basic_regex – jxh Aug 27 '21 at 08:52
  • such a long line is terrible. No one likes horizontal scrolling. Cut it short also helps readability – phuclv Aug 27 '21 at 09:14
  • @jxh You should be aware, that std::regex is extremely slow, compared to almost any other regex library. Converting both expressions to the same case will be much, much faster. – Kaznov Aug 27 '21 at 09:14
  • @Kaznov Was not aware the C++ regex library was its own implementation. I would have though it would be wrapped around existing POSIX regex library APIs, which themselves could be replaced with alternative implementations. – jxh Aug 27 '21 at 18:04

0 Answers0