LCOV - code coverage report
Current view: top level - src/util - string.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 25 25 100.0 %
Date: 2020-09-26 01:30:44 Functions: 20 20 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2019-2020 The Bitcoin Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #ifndef BITCOIN_UTIL_STRING_H
       6             : #define BITCOIN_UTIL_STRING_H
       7             : 
       8             : #include <attributes.h>
       9             : 
      10             : #include <cstring>
      11             : #include <locale>
      12             : #include <sstream>
      13             : #include <string>
      14             : #include <vector>
      15             : 
      16      328701 : NODISCARD inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v")
      17             : {
      18      328701 :     std::string::size_type front = str.find_first_not_of(pattern);
      19      328701 :     if (front == std::string::npos) {
      20          11 :         return std::string();
      21             :     }
      22      328690 :     std::string::size_type end = str.find_last_not_of(pattern);
      23      328690 :     return str.substr(front, end - front + 1);
      24      328701 : }
      25             : 
      26             : /**
      27             :  * Join a list of items
      28             :  *
      29             :  * @param list       The list to join
      30             :  * @param separator  The separator
      31             :  * @param unary_op   Apply this operator to each item in the list
      32             :  */
      33             : template <typename T, typename BaseType, typename UnaryOp>
      34       11627 : auto Join(const std::vector<T>& list, const BaseType& separator, UnaryOp unary_op)
      35             :     -> decltype(unary_op(list.at(0)))
      36             : {
      37       11627 :     decltype(unary_op(list.at(0))) ret;
      38      124972 :     for (size_t i = 0; i < list.size(); ++i) {
      39      113345 :         if (i > 0) ret += separator;
      40      113345 :         ret += unary_op(list.at(i));
      41             :     }
      42             :     return ret;
      43       11627 : }
      44             : 
      45             : template <typename T>
      46        2652 : T Join(const std::vector<T>& list, const T& separator)
      47             : {
      48       16443 :     return Join(list, separator, [](const T& i) { return i; });
      49             : }
      50             : 
      51             : // Explicit overload needed for c_str arguments, which would otherwise cause a substitution failure in the template above.
      52        2059 : inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
      53             : {
      54        2059 :     return Join<std::string>(list, separator);
      55             : }
      56             : 
      57             : /**
      58             :  * Check if a string does not contain any embedded NUL (\0) characters
      59             :  */
      60     1140373 : NODISCARD inline bool ValidAsCString(const std::string& str) noexcept
      61             : {
      62     1140373 :     return str.size() == strlen(str.c_str());
      63             : }
      64             : 
      65             : /**
      66             :  * Locale-independent version of std::to_string
      67             :  */
      68             : template <typename T>
      69      288247 : std::string ToString(const T& t)
      70             : {
      71      288247 :     std::ostringstream oss;
      72      288247 :     oss.imbue(std::locale::classic());
      73      288247 :     oss << t;
      74      288247 :     return oss.str();
      75      288247 : }
      76             : 
      77             : #endif // BITCOIN_UTIL_STRENCODINGS_H

Generated by: LCOV version 1.15