xref: /openbmc/bmcweb/include/str_utility.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include <algorithm>
6 #include <cstddef>
7 #include <ranges>
8 #include <string>
9 #include <string_view>
10 #include <vector>
11 
12 namespace bmcweb
13 {
14 // This is a naive replacement for boost::split until
15 // https://github.com/llvm/llvm-project/issues/40486
16 // is resolved
split(std::vector<std::string> & strings,std::string_view str,char delim)17 inline void split(std::vector<std::string>& strings, std::string_view str,
18                   char delim)
19 {
20     size_t start = 0;
21     size_t end = 0;
22     while (end <= str.size())
23     {
24         end = str.find(delim, start);
25         strings.emplace_back(str.substr(start, end - start));
26         start = end + 1;
27     }
28 }
29 
asciiToLower(char c)30 inline char asciiToLower(char c)
31 {
32     // Converts a character to lower case without relying on std::locale
33     if ('A' <= c && c <= 'Z')
34     {
35         c -= ('A' - 'a');
36     }
37     return c;
38 }
39 
asciiIEquals(std::string_view left,std::string_view right)40 inline bool asciiIEquals(std::string_view left, std::string_view right)
41 {
42     return std::ranges::equal(left, right, [](char lChar, char rChar) {
43         return asciiToLower(lChar) == asciiToLower(rChar);
44     });
45 }
46 
47 } // namespace bmcweb
48