diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index 88bf2d21..22f0d810 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -623,6 +623,22 @@ namespace simgear { *p = tolower(*p); } } + + +bool iequals(const std::string& a, const std::string& b) +{ + const auto lenA = a.length(); + const auto lenB = b.length(); + if (lenA != lenB) return false; + + const char* aPtr = a.data(); + const char* bPtr = b.data(); + for (size_t i = 0; i < lenA; ++i) { + if (tolower(*aPtr++) != tolower(*bPtr++)) return false; + } + + return true; +} #if defined(SG_WINDOWS) static std::wstring convertMultiByteToWString(DWORD encoding, const std::string& a) diff --git a/simgear/misc/strutils.hxx b/simgear/misc/strutils.hxx index 74fb7f39..d8fe9fbb 100644 --- a/simgear/misc/strutils.hxx +++ b/simgear/misc/strutils.hxx @@ -264,6 +264,11 @@ namespace simgear { */ void lowercase(std::string &s); + /** + * case-insensitive string comparisom + */ + bool iequals(const std::string& a, const std::string& b); + /** * convert a string in the local Windows 8-bit encoding to UTF-8 * (no-op on other platforms) diff --git a/simgear/misc/strutils_test.cxx b/simgear/misc/strutils_test.cxx index d9559ad3..b78b9a63 100644 --- a/simgear/misc/strutils_test.cxx +++ b/simgear/misc/strutils_test.cxx @@ -99,6 +99,16 @@ void test_to_int() SG_CHECK_EQUAL(strutils::to_int("-10000"), -10000); } +void test_iequals() +{ + SG_VERIFY(strutils::iequals("abcdef", "AbCDeF")); + SG_VERIFY(strutils::iequals("", "")); + SG_VERIFY(!strutils::iequals("abcdE", "ABCD")); + SG_VERIFY(strutils::iequals("%$abcdef12", "%$AbCDeF12")); + SG_VERIFY(strutils::iequals("VOR-DME", "vor-dme")); + SG_VERIFY(!strutils::iequals("VOR-DME", "vor_dme")); +} + // Auxiliary function for test_readNonNegativeInt() void aux_readNonNegativeInt_setUpOStringStream(std::ostringstream& oss, int base) { @@ -737,6 +747,7 @@ int main(int argc, char* argv[]) test_utf8Convert(); test_parseGeod(); test_formatGeod(); + test_iequals(); return EXIT_SUCCESS; }