alstring.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "config.h"
  2. #include "alstring.h"
  3. #include <algorithm>
  4. #include <cctype>
  5. #include <cwctype>
  6. #include <cstring>
  7. namespace al {
  8. int case_compare(const std::string_view str0, const std::string_view str1) noexcept
  9. {
  10. using Traits = std::string_view::traits_type;
  11. auto ch0 = str0.cbegin();
  12. auto ch1 = str1.cbegin();
  13. auto ch1end = ch1 + std::min(str0.size(), str1.size());
  14. while(ch1 != ch1end)
  15. {
  16. const int u0{std::toupper(Traits::to_int_type(*ch0))};
  17. const int u1{std::toupper(Traits::to_int_type(*ch1))};
  18. if(const int diff{u0-u1}) return diff;
  19. ++ch0; ++ch1;
  20. }
  21. if(str0.size() < str1.size()) return -1;
  22. if(str0.size() > str1.size()) return 1;
  23. return 0;
  24. }
  25. int case_compare(const std::wstring_view str0, const std::wstring_view str1) noexcept
  26. {
  27. using Traits = std::wstring_view::traits_type;
  28. auto ch0 = str0.cbegin();
  29. auto ch1 = str1.cbegin();
  30. auto ch1end = ch1 + std::min(str0.size(), str1.size());
  31. while(ch1 != ch1end)
  32. {
  33. const auto u0 = std::towupper(Traits::to_int_type(*ch0));
  34. const auto u1 = std::towupper(Traits::to_int_type(*ch1));
  35. if(const auto diff = static_cast<int>(u0-u1)) return diff;
  36. ++ch0; ++ch1;
  37. }
  38. if(str0.size() < str1.size()) return -1;
  39. if(str0.size() > str1.size()) return 1;
  40. return 0;
  41. }
  42. } // namespace al