選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

52 行
1.1 KiB

  1. #pragma once
  2. #include "compare.h"
  3. namespace cppcore
  4. {
  5. /* op_invariant_string_less */
  6. bool op_invariant_string_less
  7. ::operator()(const std::string& lhs, const std::string& rhs) const
  8. { return op_invariant_string_compare { } (lhs, rhs) < 0; }
  9. /* op_invariant_string_compare */
  10. int op_invariant_string_compare::operator()(const std::string& lhs, const std::string& rhs) const
  11. {
  12. auto c1 = lhs.c_str();
  13. auto c2 = rhs.c_str();
  14. auto l1 = lhs.size();
  15. auto l2 = rhs.size();
  16. while (l1 > 0 && l2 > 0 && std::tolower(*c1) == std::tolower(*c2))
  17. {
  18. ++c1;
  19. ++c2;
  20. --l1;
  21. --l2;
  22. }
  23. if (l1 > 0 && l2 > 0)
  24. {
  25. auto x1 = std::tolower(*c1);
  26. auto x2 = std::tolower(*c2);
  27. return
  28. x1 < x2 ? -1 :
  29. x1 > x2 ? 1 :
  30. 0;
  31. }
  32. else
  33. {
  34. return
  35. l1 < l2 ? -1 :
  36. l1 > l2 ? 1 :
  37. 0;
  38. }
  39. }
  40. }