xref: /openbmc/bmcweb/include/str_utility.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
350ebd4afSEd Tanous #pragma once
450ebd4afSEd Tanous 
518f8f608SEd Tanous #include <algorithm>
6*d7857201SEd Tanous #include <cstddef>
718f8f608SEd Tanous #include <ranges>
850ebd4afSEd Tanous #include <string>
950ebd4afSEd Tanous #include <string_view>
1050ebd4afSEd Tanous #include <vector>
1150ebd4afSEd Tanous 
1250ebd4afSEd Tanous namespace bmcweb
1350ebd4afSEd Tanous {
1450ebd4afSEd Tanous // This is a naive replacement for boost::split until
1550ebd4afSEd Tanous // https://github.com/llvm/llvm-project/issues/40486
1650ebd4afSEd Tanous // is resolved
split(std::vector<std::string> & strings,std::string_view str,char delim)1750ebd4afSEd Tanous inline void split(std::vector<std::string>& strings, std::string_view str,
1850ebd4afSEd Tanous                   char delim)
1950ebd4afSEd Tanous {
2050ebd4afSEd Tanous     size_t start = 0;
2150ebd4afSEd Tanous     size_t end = 0;
22b64c6262SEd Tanous     while (end <= str.size())
2350ebd4afSEd Tanous     {
2450ebd4afSEd Tanous         end = str.find(delim, start);
2550ebd4afSEd Tanous         strings.emplace_back(str.substr(start, end - start));
26b64c6262SEd Tanous         start = end + 1;
2750ebd4afSEd Tanous     }
2850ebd4afSEd Tanous }
2918f8f608SEd Tanous 
asciiToLower(char c)3018f8f608SEd Tanous inline char asciiToLower(char c)
3118f8f608SEd Tanous {
3218f8f608SEd Tanous     // Converts a character to lower case without relying on std::locale
3318f8f608SEd Tanous     if ('A' <= c && c <= 'Z')
3418f8f608SEd Tanous     {
3518f8f608SEd Tanous         c -= ('A' - 'a');
3618f8f608SEd Tanous     }
3718f8f608SEd Tanous     return c;
3818f8f608SEd Tanous }
3918f8f608SEd Tanous 
asciiIEquals(std::string_view left,std::string_view right)4018f8f608SEd Tanous inline bool asciiIEquals(std::string_view left, std::string_view right)
4118f8f608SEd Tanous {
4218f8f608SEd Tanous     return std::ranges::equal(left, right, [](char lChar, char rChar) {
4318f8f608SEd Tanous         return asciiToLower(lChar) == asciiToLower(rChar);
4418f8f608SEd Tanous     });
4518f8f608SEd Tanous }
4618f8f608SEd Tanous 
4750ebd4afSEd Tanous } // namespace bmcweb
48