LCOV - code coverage report
Current view: top level - foo/src - string_utils.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 53 61 86.9 %
Date: 2025-12-18 10:07:43 Functions: 22 23 95.7 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2025 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : #pragma once
      18             : 
      19             : #include <cstdint>
      20             : #include <string>
      21             : #include <vector>
      22             : #include <set>
      23             : #include <algorithm>
      24             : #include <regex>
      25             : #include <iterator>
      26             : #include <charconv>
      27             : #include <string_view>
      28             : 
      29             : #ifdef _WIN32
      30             : #include <WTypes.h>
      31             : #endif
      32             : #if defined(__APPLE__)
      33             : #include <TargetConditionals.h>
      34             : #endif
      35             : 
      36             : namespace jami {
      37             : 
      38             : constexpr static const char TRUE_STR[] = "true";
      39             : constexpr static const char FALSE_STR[] = "false";
      40             : 
      41             : constexpr static const char*
      42        1043 : bool_to_str(bool b) noexcept
      43             : {
      44        1043 :     return b ? TRUE_STR : FALSE_STR;
      45             : }
      46             : 
      47             : const std::string& userAgent();
      48             : 
      49             : constexpr inline std::string_view
      50         843 : platform()
      51             : {
      52             :     using namespace std::literals;
      53             : #ifdef __linux__
      54             : #if defined(__ANDROID__)
      55             :     return "Android"sv;
      56             : #else
      57         843 :     return "Linux"sv;
      58             : #endif
      59             : #elif defined(_WIN32)
      60             :     return "Windows"sv;
      61             : #elif defined(__APPLE__)
      62             : #if TARGET_OS_IOS
      63             :     return "iOS"sv;
      64             : #else
      65             :     return "macOS"sv;
      66             : #endif
      67             : #else
      68             :     return "unknown"sv;
      69             : #endif
      70             : }
      71             : 
      72             : constexpr inline std::string_view
      73         803 : arch()
      74             : {
      75             :     using namespace std::literals;
      76             : #if defined(__x86_64__) || defined(_M_X64)
      77         803 :     return "x86_64"sv;
      78             : #elif defined(__i386__) || defined(_M_IX86)
      79             :     return "x86"sv;
      80             : #elif defined(__aarch64__) || defined(_M_ARM64)
      81             :     return "arm64"sv;
      82             : #elif defined(__arm__) || defined(_M_ARM)
      83             :     return "arm"sv;
      84             : #else
      85             :     return "unknown"sv;
      86             : #endif
      87             : }
      88             : 
      89             : std::string to_string(double value);
      90             : 
      91             : #ifdef _WIN32
      92             : std::wstring to_wstring(const std::string& str, int codePage = CP_UTF8);
      93             : std::string to_string(const std::wstring& wstr, int codePage = CP_UTF8);
      94             : #endif
      95             : 
      96             : std::string to_hex_string(uint64_t id);
      97             : uint64_t from_hex_string(const std::string& str);
      98             : 
      99             : template<typename T>
     100             : inline T
     101        1493 : to_int(std::string_view str, T defaultValue)
     102             : {
     103             :     T result;
     104        1493 :     auto [p, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
     105        1493 :     if (ec == std::errc())
     106        1493 :         return result;
     107             :     else
     108           0 :         return defaultValue;
     109             : }
     110             : 
     111             : template<typename T>
     112             : T
     113        5258 : to_int(std::string_view str)
     114             : {
     115             :     T result;
     116        5258 :     auto [p, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
     117        5258 :     if (ec == std::errc())
     118        5258 :         return result;
     119           0 :     if (ec == std::errc::invalid_argument)
     120           0 :         throw std::invalid_argument("Unable to parse integer: invalid_argument");
     121           0 :     else if (ec == std::errc::result_out_of_range)
     122           0 :         throw std::out_of_range("Unable to parse integer: out of range");
     123           0 :     throw std::system_error(std::make_error_code(ec));
     124             : }
     125             : 
     126             : template<typename T>
     127             : T
     128             : to_enum(std::string_view str, T defaultValue)
     129             : {
     130             :     return static_cast<T>(to_int<std::underlying_type_t<T>>(str, static_cast<std::underlying_type_t<T>>(defaultValue)));
     131             : }
     132             : 
     133             : template<typename T>
     134             : T
     135         124 : to_enum(std::string_view str)
     136             : {
     137         124 :     return static_cast<T>(to_int<std::underlying_type_t<T>>(str));
     138             : }
     139             : 
     140             : static inline bool
     141          14 : starts_with(std::string_view str, std::string_view prefix)
     142             : {
     143          14 :     return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0;
     144             : }
     145             : 
     146             : template<typename... Args>
     147             : std::string
     148        5974 : concat(Args&&... args)
     149             : {
     150             :     static_assert((std::is_constructible_v<std::string_view, Args&&> && ...));
     151        5974 :     std::string s;
     152        5974 :     s.reserve((std::string_view {args}.size() + ...));
     153        5974 :     (s.append(std::forward<Args>(args)), ...);
     154        5974 :     return s;
     155           0 : }
     156             : 
     157             : std::string_view trim(std::string_view s);
     158             : 
     159             : /**
     160             :  * Split a string_view with an API similar to std::getline.
     161             :  * @param str The input string stream to iterate on, trimed of line during iteration.
     162             :  * @param line The output substring.
     163             :  * @param delim The delimiter.
     164             :  * @return True if line was set, false if the end of the input was reached.
     165             :  */
     166             : inline bool
     167       13578 : getline_full(std::string_view& str, std::string_view& line, char delim = '\n')
     168             : {
     169       13578 :     if (str.empty())
     170        3960 :         return false;
     171        9617 :     auto pos = str.find(delim);
     172        9617 :     line = str.substr(0, pos);
     173        9617 :     str.remove_prefix(pos < str.size() ? pos + 1 : str.size());
     174        9618 :     return true;
     175             : }
     176             : 
     177             : /**
     178             :  * Similar to @getline_full but skips empty results.
     179             :  */
     180             : inline bool
     181       12621 : getline(std::string_view& str, std::string_view& line, char delim = '\n')
     182             : {
     183             :     do {
     184       12621 :         if (!getline_full(str, line, delim))
     185        3612 :             return false;
     186        9009 :     } while (line.empty());
     187        8957 :     return true;
     188             : }
     189             : 
     190             : inline std::vector<std::string_view>
     191        3244 : split_string(std::string_view str, char delim)
     192             : {
     193        3244 :     std::vector<std::string_view> output;
     194        9049 :     for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last;
     195        5805 :          first = second + 1) {
     196        5805 :         second = std::find(first, last, delim);
     197        5805 :         if (first != second)
     198        5803 :             output.emplace_back(first, second - first);
     199             :     }
     200        3244 :     return output;
     201           0 : }
     202             : 
     203             : inline std::vector<std::string_view>
     204             : split_string(std::string_view str, std::string_view delims = " ")
     205             : {
     206             :     std::vector<std::string_view> output;
     207             :     for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last;
     208             :          first = second + 1) {
     209             :         second = std::find_first_of(first, last, std::cbegin(delims), std::cend(delims));
     210             :         if (first != second)
     211             :             output.emplace_back(first, second - first);
     212             :     }
     213             :     return output;
     214             : }
     215             : 
     216             : std::vector<unsigned> split_string_to_unsigned(std::string_view s, char sep);
     217             : 
     218             : void string_replace(std::string& str, const std::string& from, const std::string& to);
     219             : 
     220             : std::string_view string_remove_suffix(std::string_view str, char separator);
     221             : 
     222             : std::string string_join(const std::set<std::string>& set, std::string_view separator = "/");
     223             : 
     224             : std::set<std::string> string_split_set(std::string& str, std::string_view separator = "/");
     225             : 
     226             : /**
     227             :  * Percent-encode a string according to RFC 3986 unreserved characters.
     228             :  *
     229             :  * Only [0-9A-Za-z], '-' , '_' , '.' , '~' remain unencoded.
     230             :  * Everything else (including non-ASCII) becomes '%XX'.
     231             :  */
     232             : std::string urlEncode(std::string_view input);
     233             : 
     234             : } // namespace jami
     235             : 
     236             : // Add string operators crucially missing from standard
     237             : // see https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/1RcShRhrmRc
     238             : namespace std {
     239             : inline string
     240        4101 : operator+(const string& s, const string_view& sv)
     241             : {
     242        4101 :     return jami::concat(s, sv);
     243             : }
     244             : inline string
     245         502 : operator+(const string_view& sv, const string& s)
     246             : {
     247         502 :     return jami::concat(sv, s);
     248             : }
     249             : using svmatch = match_results<string_view::const_iterator>;
     250             : using svsub_match = sub_match<string_view::const_iterator>;
     251             : constexpr string_view
     252          20 : svsub_match_view(const svsub_match& submatch) noexcept
     253             : {
     254          20 :     return string_view(&*submatch.first, submatch.second - submatch.first);
     255             : }
     256             : inline bool
     257           8 : regex_match(string_view sv,
     258             :             svmatch& m,
     259             :             const regex& e,
     260             :             regex_constants::match_flag_type flags = regex_constants::match_default)
     261             : {
     262           8 :     return regex_match(sv.begin(), sv.end(), m, e, flags);
     263             : }
     264             : inline bool
     265             : regex_match(string_view sv, const regex& e, regex_constants::match_flag_type flags = regex_constants::match_default)
     266             : {
     267             :     return regex_match(sv.begin(), sv.end(), e, flags);
     268             : }
     269             : inline bool
     270        8377 : regex_search(string_view sv,
     271             :              svmatch& m,
     272             :              const regex& e,
     273             :              regex_constants::match_flag_type flags = regex_constants::match_default)
     274             : {
     275        8377 :     return regex_search(sv.begin(), sv.end(), m, e, flags);
     276             : }
     277             : } // namespace std

Generated by: LCOV version 1.14