azure-core
Loading...
Searching...
No Matches
strings.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9#pragma once
10
11#include <algorithm>
12#include <cstring>
13#include <string>
14#include <vector>
15
16namespace Azure { namespace Core { namespace _internal {
17
22 struct StringExtensions final
23 {
24 static constexpr char ToUpper(char c) noexcept
25 {
26 return (c < 'a' || c > 'z') ? c : c - ('a' - 'A');
27 }
28
29 static constexpr char ToLower(char c) noexcept
30 {
31 return (c < 'A' || c > 'Z') ? c : c + ('a' - 'A');
32 }
33
34 static constexpr bool IsDigit(char c) noexcept { return c >= '0' && c <= '9'; }
35
36 static constexpr bool IsHexDigit(char c) noexcept
37 {
38 return IsDigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
39 }
40
41 static constexpr bool IsAlphaNumeric(char c) noexcept
42 {
43 return IsDigit(c) || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
44 }
45
46 static constexpr bool IsSpace(char c) noexcept { return c == ' ' || (c >= '\t' && c <= '\r'); }
47
48 static constexpr bool IsPrintable(char c) noexcept { return c >= ' ' && c <= '~'; }
49
50 struct CaseInsensitiveComparator final
51 {
52 bool operator()(std::string const& lhs, std::string const& rhs) const
53 {
54 return std::lexicographical_compare(
55 lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), [](auto l, auto r) {
56 return ToLower(l) < ToLower(r);
57 });
58 }
59 };
60
61 static bool LocaleInvariantCaseInsensitiveEqual(
62 std::string const& lhs,
63 std::string const& rhs) noexcept
64 {
65 auto const rhsSize = rhs.size();
66 if (lhs.size() != rhsSize)
67 {
68 return false;
69 }
70
71 auto const lhsData = lhs.c_str();
72 auto const rhsData = rhs.c_str();
73 for (size_t i = 0; i < rhsSize; ++i)
74 {
75 if (ToLower(lhsData[i]) != ToLower(rhsData[i]))
76 {
77 return false;
78 }
79 }
80
81 return true;
82 }
83
84 static std::string ToLower(std::string src)
85 {
86 std::transform(src.begin(), src.end(), src.begin(), [](auto c) { return ToLower(c); });
87 return src;
88 }
89
90 static std::string ToUpper(std::string src)
91 {
92 std::transform(src.begin(), src.end(), src.begin(), [](auto c) { return ToUpper(c); });
93 return src;
94 }
95
96 static std::vector<std::string> Split(
97 const std::string& s,
98 char separator,
99 bool removeEmptyEntries = false)
100 {
101 std::vector<std::string> result;
102
103 const auto len = s.size();
104 size_t start = 0;
105 while (start < len)
106 {
107 auto end = s.find(separator, start);
108 if (end == std::string::npos)
109 {
110 end = len;
111 }
112 if (!removeEmptyEntries || start < end)
113 {
114 result.push_back(s.substr(start, end - start));
115 }
116
117 start = end + 1;
118 }
119
120 return result;
121 }
122 };
123
124}}} // namespace Azure::Core::_internal
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57