azure-core
strings.hpp
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // SPDX-License-Identifier: MIT
3 
9 #pragma once
10 
11 #include <algorithm>
12 #include <cstring>
13 #include <string>
14 
15 namespace Azure { namespace Core { namespace _internal {
16 
21  struct StringExtensions final
22  {
23  static constexpr char ToUpper(char c) noexcept
24  {
25  return (c < 'a' || c > 'z') ? c : c - ('a' - 'A');
26  }
27 
28  static constexpr char ToLower(char c) noexcept
29  {
30  return (c < 'A' || c > 'Z') ? c : c + ('a' - 'A');
31  }
32 
33  struct CaseInsensitiveComparator final
34  {
35  bool operator()(std::string const& lhs, std::string const& rhs) const
36  {
37  return std::lexicographical_compare(
38  lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](auto l, auto r) {
39  return ToLower(l) < ToLower(r);
40  });
41  }
42  };
43 
44  static bool LocaleInvariantCaseInsensitiveEqual(
45  std::string const& lhs,
46  std::string const& rhs) noexcept
47  {
48  auto const rhsSize = rhs.size();
49  if (lhs.size() != rhsSize)
50  {
51  return false;
52  }
53 
54  auto const lhsData = lhs.c_str();
55  auto const rhsData = rhs.c_str();
56  for (size_t i = 0; i < rhsSize; ++i)
57  {
58  if (ToLower(lhsData[i]) != ToLower(rhsData[i]))
59  {
60  return false;
61  }
62  }
63 
64  return true;
65  }
66 
67  static std::string ToLower(std::string src)
68  {
69  std::transform(src.begin(), src.end(), src.begin(), [](auto c) { return ToLower(c); });
70  return src;
71  }
72 
73  static std::string ToUpper(std::string src)
74  {
75  std::transform(src.begin(), src.end(), src.begin(), [](auto c) { return ToUpper(c); });
76  return src;
77  }
78  };
79 
80 }}} // namespace Azure::Core::_internal
Azure SDK abstractions.
Definition: azure_assert.hpp:55