Release 1.24.0
[openal-soft.git] / common / alstring.cpp
blobd609823dfc489286a14b3d8050d97d9870fad765
2 #include "config.h"
4 #include "alstring.h"
6 #include <algorithm>
7 #include <cctype>
8 #include <cwctype>
9 #include <cstring>
12 namespace al {
14 int case_compare(const std::string_view str0, const std::string_view str1) noexcept
16 using Traits = std::string_view::traits_type;
18 auto ch0 = str0.cbegin();
19 auto ch1 = str1.cbegin();
20 auto ch1end = ch1 + std::min(str0.size(), str1.size());
21 while(ch1 != ch1end)
23 const int u0{std::toupper(Traits::to_int_type(*ch0))};
24 const int u1{std::toupper(Traits::to_int_type(*ch1))};
25 if(const int diff{u0-u1}) return diff;
26 ++ch0; ++ch1;
29 if(str0.size() < str1.size()) return -1;
30 if(str0.size() > str1.size()) return 1;
31 return 0;
34 int case_compare(const std::wstring_view str0, const std::wstring_view str1) noexcept
36 using Traits = std::wstring_view::traits_type;
38 auto ch0 = str0.cbegin();
39 auto ch1 = str1.cbegin();
40 auto ch1end = ch1 + std::min(str0.size(), str1.size());
41 while(ch1 != ch1end)
43 const auto u0 = std::towupper(Traits::to_int_type(*ch0));
44 const auto u1 = std::towupper(Traits::to_int_type(*ch1));
45 if(const auto diff = static_cast<int>(u0-u1)) return diff;
46 ++ch0; ++ch1;
49 if(str0.size() < str1.size()) return -1;
50 if(str0.size() > str1.size()) return 1;
51 return 0;
54 } // namespace al