LCOV - code coverage report
Current view: top level - src - string_utils.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 54 62 87.1 %
Date: 2024-11-15 09:04:49 Functions: 21 22 95.5 %

          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        1109 : bool_to_str(bool b) noexcept
      43             : {
      44        1109 :     return b ? TRUE_STR : FALSE_STR;
      45             : }
      46             : 
      47             : std::string_view userAgent();
      48             : 
      49             : constexpr inline std::string_view
      50         860 : platform() {
      51             :     using namespace std::literals;
      52             : #ifdef __linux__
      53             :     #if defined(__ANDROID__)
      54             :         return "Android"sv;
      55             :     #else
      56         860 :         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         816 : arch() {
      73             :     using namespace std::literals;
      74             : #if defined(__x86_64__) || defined(_M_X64)
      75         816 :     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        1592 : to_int(std::string_view str, T defaultValue)
     100             : {
     101             :     T result;
     102        1592 :     auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result);
     103        1592 :     if (ec == std::errc())
     104        1592 :         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 int
     125           1 : stoi(const std::string& str)
     126             : {
     127           1 :     return std::stoi(str);
     128             : }
     129             : 
     130             : static inline double
     131          69 : stod(const std::string& str)
     132             : {
     133          69 :     return std::stod(str);
     134             : }
     135             : 
     136             : template<typename... Args>
     137        2466 : std::string concat(Args &&... args){
     138             :     static_assert((std::is_constructible_v<std::string_view, Args&&> && ...));
     139        2466 :     std::string s;
     140        2466 :     s.reserve((std::string_view{ args }.size() + ...));
     141        2466 :     (s.append(std::forward<Args>(args)), ...);
     142        2466 :     return s;
     143           0 : }
     144             : 
     145             : std::string_view trim(std::string_view s);
     146             : 
     147             : /**
     148             :  * Split a string_view with an API similar to std::getline.
     149             :  * @param str The input string stream to iterate on, trimed of line during iteration.
     150             :  * @param line The output substring.
     151             :  * @param delim The delimiter.
     152             :  * @return True if line was set, false if the end of the input was reached.
     153             :  */
     154             : inline bool
     155       13711 : getline_full(std::string_view& str, std::string_view& line, char delim = '\n')
     156             : {
     157       13711 :     if (str.empty())
     158        3999 :         return false;
     159        9712 :     auto pos = str.find(delim);
     160        9712 :     line = str.substr(0, pos);
     161        9712 :     str.remove_prefix(pos < str.size() ? pos + 1 : str.size());
     162        9712 :     return true;
     163             : }
     164             : 
     165             : /**
     166             :  * Similar to @getline_full but skips empty results.
     167             :  */
     168             : inline bool
     169       12688 : getline(std::string_view& str, std::string_view& line, char delim = '\n')
     170             : {
     171             :     do {
     172       12688 :         if (!getline_full(str, line, delim))
     173        3627 :             return false;
     174        9060 :     } while (line.empty());
     175        9005 :     return true;
     176             : }
     177             : 
     178             : inline std::vector<std::string_view>
     179        3288 : split_string(std::string_view str, char delim)
     180             : {
     181        3288 :     std::vector<std::string_view> output;
     182        3288 :     for (auto first = str.data(), second = str.data(), last = first + str.size();
     183        9154 :          second != last && first != last;
     184        5867 :          first = second + 1) {
     185        5866 :         second = std::find(first, last, delim);
     186        5868 :         if (first != second)
     187        5866 :             output.emplace_back(first, second - first);
     188             :     }
     189        3288 :     return output;
     190           0 : }
     191             : 
     192             : inline std::vector<std::string_view>
     193             : split_string(std::string_view str, std::string_view delims = " ")
     194             : {
     195             :     std::vector<std::string_view> output;
     196             :     for (auto first = str.data(), second = str.data(), last = first + str.size();
     197             :          second != last && first != last;
     198             :          first = second + 1) {
     199             :         second = std::find_first_of(first, last, std::cbegin(delims), std::cend(delims));
     200             :         if (first != second)
     201             :             output.emplace_back(first, second - first);
     202             :     }
     203             :     return output;
     204             : }
     205             : 
     206             : std::vector<unsigned> split_string_to_unsigned(std::string_view s, char sep);
     207             : 
     208             : void string_replace(std::string& str, const std::string& from, const std::string& to);
     209             : 
     210             : std::string_view string_remove_suffix(std::string_view str, char separator);
     211             : 
     212             : std::string string_join(const std::set<std::string>& set, std::string_view separator = "/");
     213             : 
     214             : std::set<std::string> string_split_set(std::string& str, std::string_view separator = "/");
     215             : 
     216             : } // namespace jami
     217             : 
     218             : // Add string operators crucially missing from standard
     219             : // see https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/1RcShRhrmRc
     220             : namespace std {
     221             : inline string
     222        1434 : operator+(const string& s, const string_view& sv)
     223             : {
     224        1434 :     return jami::concat(s, sv);
     225             : }
     226             : inline string
     227        1032 : operator+(const string_view& sv, const string& s)
     228             : {
     229        1032 :     return jami::concat(sv, s);
     230             : }
     231             : using svmatch = match_results<string_view::const_iterator>;
     232             : using svsub_match = sub_match<string_view::const_iterator>;
     233          50 : constexpr string_view svsub_match_view(const svsub_match& submatch) noexcept {
     234          50 :     return string_view(&*submatch.first, submatch.second - submatch.first);
     235             : }
     236             : inline bool
     237          13 : regex_match(string_view sv,
     238             :             svmatch& m,
     239             :             const regex& e,
     240             :             regex_constants::match_flag_type flags = regex_constants::match_default)
     241             : {
     242          13 :     return regex_match(sv.begin(), sv.end(), m, e, flags);
     243             : }
     244             : inline bool
     245             : regex_match(string_view sv,
     246             :             const regex& e,
     247             :             regex_constants::match_flag_type flags = regex_constants::match_default)
     248             : {
     249             :     return regex_match(sv.begin(), sv.end(), e, flags);
     250             : }
     251             : inline bool
     252        8497 : regex_search(string_view sv,
     253             :              svmatch& m,
     254             :              const regex& e,
     255             :              regex_constants::match_flag_type flags = regex_constants::match_default)
     256             : {
     257        8497 :     return regex_search(sv.begin(), sv.end(), m, e, flags);
     258             : }
     259             : } // namespace std

Generated by: LCOV version 1.14