LCOV - code coverage report
Current view: top level - src - string_utils.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 52 60 86.7 %
Date: 2024-12-21 08:56:24 Functions: 20 21 95.2 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 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        1115 : bool_to_str(bool b) noexcept
      43             : {
      44        1115 :     return b ? TRUE_STR : FALSE_STR;
      45             : }
      46             : 
      47             : std::string_view userAgent();
      48             : 
      49             : constexpr inline std::string_view
      50         859 : platform() {
      51             :     using namespace std::literals;
      52             : #ifdef __linux__
      53             :     #if defined(__ANDROID__)
      54             :         return "Android"sv;
      55             :     #else
      56         859 :         return "Linux"sv;
      57             :     #endif
      58             : #elif defined(_WIN32)
      59             :     return "Windows"sv;
      60             : #elif defined(__APPLE__)
      61             :     #if TARGET_OS_IOS
      62             :         return "iOS"sv;
      63             :     #else
      64             :         return "macOS"sv;
      65             :     #endif
      66             : #else
      67             :     return "unknown"sv;
      68             : #endif
      69             : }
      70             : 
      71             : constexpr inline std::string_view
      72         815 : arch() {
      73             :     using namespace std::literals;
      74             : #if defined(__x86_64__) || defined(_M_X64)
      75         815 :     return "x86_64"sv;
      76             : #elif defined(__i386__) || defined(_M_IX86)
      77             :     return "x86"sv;
      78             : #elif defined(__aarch64__) || defined(_M_ARM64)
      79             :     return "arm64"sv;
      80             : #elif defined(__arm__) || defined(_M_ARM)
      81             :     return "arm"sv;
      82             : #else
      83             :     return "unknown"sv;
      84             : #endif
      85             : }
      86             : 
      87             : std::string to_string(double value);
      88             : 
      89             : #ifdef _WIN32
      90             : std::wstring to_wstring(const std::string& str, int codePage = CP_UTF8);
      91             : std::string to_string(const std::wstring& wstr, int codePage = CP_UTF8);
      92             : #endif
      93             : 
      94             : std::string to_hex_string(uint64_t id);
      95             : uint64_t from_hex_string(const std::string& str);
      96             : 
      97             : template<typename T>
      98             : inline T
      99        1558 : to_int(std::string_view str, T defaultValue)
     100             : {
     101             :     T result;
     102        1558 :     auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result);
     103        1558 :     if (ec == std::errc())
     104        1558 :         return result;
     105             :     else
     106           0 :         return defaultValue;
     107             : }
     108             : 
     109             : template<typename T>
     110             : T
     111        5170 : to_int(std::string_view str)
     112             : {
     113             :     T result;
     114        5170 :     auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result);
     115        5170 :     if (ec == std::errc())
     116        5170 :         return result;
     117           0 :     if (ec == std::errc::invalid_argument)
     118           0 :         throw std::invalid_argument("Unable to parse integer: invalid_argument");
     119           0 :     else if (ec == std::errc::result_out_of_range)
     120           0 :         throw std::out_of_range("Unable to parse integer: out of range");
     121           0 :     throw std::system_error(std::make_error_code(ec));
     122             : }
     123             : 
     124             : static inline bool
     125           4 : starts_with(std::string_view str, std::string_view prefix)
     126             : {
     127           4 :     return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0;
     128             : }
     129             : 
     130             : template<typename... Args>
     131        2498 : std::string concat(Args &&... args){
     132             :     static_assert((std::is_constructible_v<std::string_view, Args&&> && ...));
     133        2498 :     std::string s;
     134        2498 :     s.reserve((std::string_view{ args }.size() + ...));
     135        2498 :     (s.append(std::forward<Args>(args)), ...);
     136        2498 :     return s;
     137           0 : }
     138             : 
     139             : std::string_view trim(std::string_view s);
     140             : 
     141             : /**
     142             :  * Split a string_view with an API similar to std::getline.
     143             :  * @param str The input string stream to iterate on, trimed of line during iteration.
     144             :  * @param line The output substring.
     145             :  * @param delim The delimiter.
     146             :  * @return True if line was set, false if the end of the input was reached.
     147             :  */
     148             : inline bool
     149       13636 : getline_full(std::string_view& str, std::string_view& line, char delim = '\n')
     150             : {
     151       13636 :     if (str.empty())
     152        3974 :         return false;
     153        9662 :     auto pos = str.find(delim);
     154        9662 :     line = str.substr(0, pos);
     155        9662 :     str.remove_prefix(pos < str.size() ? pos + 1 : str.size());
     156        9662 :     return true;
     157             : }
     158             : 
     159             : /**
     160             :  * Similar to @getline_full but skips empty results.
     161             :  */
     162             : inline bool
     163       12635 : getline(std::string_view& str, std::string_view& line, char delim = '\n')
     164             : {
     165             :     do {
     166       12635 :         if (!getline_full(str, line, delim))
     167        3610 :             return false;
     168        9025 :     } while (line.empty());
     169        8973 :     return true;
     170             : }
     171             : 
     172             : inline std::vector<std::string_view>
     173        3276 : split_string(std::string_view str, char delim)
     174             : {
     175        3276 :     std::vector<std::string_view> output;
     176        3276 :     for (auto first = str.data(), second = str.data(), last = first + str.size();
     177        9126 :          second != last && first != last;
     178        5850 :          first = second + 1) {
     179        5850 :         second = std::find(first, last, delim);
     180        5850 :         if (first != second)
     181        5848 :             output.emplace_back(first, second - first);
     182             :     }
     183        3276 :     return output;
     184           0 : }
     185             : 
     186             : inline std::vector<std::string_view>
     187             : split_string(std::string_view str, std::string_view delims = " ")
     188             : {
     189             :     std::vector<std::string_view> output;
     190             :     for (auto first = str.data(), second = str.data(), last = first + str.size();
     191             :          second != last && first != last;
     192             :          first = second + 1) {
     193             :         second = std::find_first_of(first, last, std::cbegin(delims), std::cend(delims));
     194             :         if (first != second)
     195             :             output.emplace_back(first, second - first);
     196             :     }
     197             :     return output;
     198             : }
     199             : 
     200             : std::vector<unsigned> split_string_to_unsigned(std::string_view s, char sep);
     201             : 
     202             : void string_replace(std::string& str, const std::string& from, const std::string& to);
     203             : 
     204             : std::string_view string_remove_suffix(std::string_view str, char separator);
     205             : 
     206             : std::string string_join(const std::set<std::string>& set, std::string_view separator = "/");
     207             : 
     208             : std::set<std::string> string_split_set(std::string& str, std::string_view separator = "/");
     209             : 
     210             : } // namespace jami
     211             : 
     212             : // Add string operators crucially missing from standard
     213             : // see https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/1RcShRhrmRc
     214             : namespace std {
     215             : inline string
     216        1454 : operator+(const string& s, const string_view& sv)
     217             : {
     218        1454 :     return jami::concat(s, sv);
     219             : }
     220             : inline string
     221        1044 : operator+(const string_view& sv, const string& s)
     222             : {
     223        1044 :     return jami::concat(sv, s);
     224             : }
     225             : using svmatch = match_results<string_view::const_iterator>;
     226             : using svsub_match = sub_match<string_view::const_iterator>;
     227          34 : constexpr string_view svsub_match_view(const svsub_match& submatch) noexcept {
     228          34 :     return string_view(&*submatch.first, submatch.second - submatch.first);
     229             : }
     230             : inline bool
     231           9 : regex_match(string_view sv,
     232             :             svmatch& m,
     233             :             const regex& e,
     234             :             regex_constants::match_flag_type flags = regex_constants::match_default)
     235             : {
     236           9 :     return regex_match(sv.begin(), sv.end(), m, e, flags);
     237             : }
     238             : inline bool
     239             : regex_match(string_view sv,
     240             :             const regex& e,
     241             :             regex_constants::match_flag_type flags = regex_constants::match_default)
     242             : {
     243             :     return regex_match(sv.begin(), sv.end(), e, flags);
     244             : }
     245             : inline bool
     246        8479 : regex_search(string_view sv,
     247             :              svmatch& m,
     248             :              const regex& e,
     249             :              regex_constants::match_flag_type flags = regex_constants::match_default)
     250             : {
     251        8479 :     return regex_search(sv.begin(), sv.end(), m, e, flags);
     252             : }
     253             : } // namespace std

Generated by: LCOV version 1.14