LCOV - code coverage report
Current view: top level - src/util - string.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 44 44 100.0 %
Date: 2022-04-21 14:51:19 Functions: 31 31 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 56 64 87.5 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2019-2021 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 <algorithm>
#      11                 :            : #include <array>
#      12                 :            : #include <cstring>
#      13                 :            : #include <locale>
#      14                 :            : #include <sstream>
#      15                 :            : #include <string>
#      16                 :            : #include <vector>
#      17                 :            : 
#      18                 :            : [[nodiscard]] inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v")
#      19                 :    1127324 : {
#      20                 :    1127324 :     std::string::size_type front = str.find_first_not_of(pattern);
#      21         [ +  + ]:    1127324 :     if (front == std::string::npos) {
#      22                 :         52 :         return std::string();
#      23                 :         52 :     }
#      24                 :    1127272 :     std::string::size_type end = str.find_last_not_of(pattern);
#      25                 :    1127272 :     return str.substr(front, end - front + 1);
#      26                 :    1127324 : }
#      27                 :            : 
#      28                 :            : [[nodiscard]] inline std::string RemovePrefix(const std::string& str, const std::string& prefix)
#      29                 :    2723698 : {
#      30         [ +  + ]:    2723698 :     if (str.substr(0, prefix.size()) == prefix) {
#      31                 :     138483 :         return str.substr(prefix.size());
#      32                 :     138483 :     }
#      33                 :    2585215 :     return str;
#      34                 :    2723698 : }
#      35                 :            : 
#      36                 :            : /**
#      37                 :            :  * Join a list of items
#      38                 :            :  *
#      39                 :            :  * @param list       The list to join
#      40                 :            :  * @param separator  The separator
#      41                 :            :  * @param unary_op   Apply this operator to each item in the list
#      42                 :            :  */
#      43                 :            : template <typename T, typename BaseType, typename UnaryOp>
#      44                 :            : auto Join(const std::vector<T>& list, const BaseType& separator, UnaryOp unary_op)
#      45                 :            :     -> decltype(unary_op(list.at(0)))
#      46                 :      73634 : {
#      47                 :      73634 :     decltype(unary_op(list.at(0))) ret;
#      48 [ +  + ][ +  + ]:     540008 :     for (size_t i = 0; i < list.size(); ++i) {
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#                 [ +  + ]
#      49 [ +  + ][ +  + ]:     466374 :         if (i > 0) ret += separator;
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#                 [ +  + ]
#      50                 :     466374 :         ret += unary_op(list.at(i));
#      51                 :     466374 :     }
#      52                 :      73634 :     return ret;
#      53                 :      73634 : }
#      54                 :            : 
#      55                 :            : template <typename T>
#      56                 :            : T Join(const std::vector<T>& list, const T& separator)
#      57                 :      47274 : {
#      58                 :     307562 :     return Join(list, separator, [](const T& i) { return i; });
#      59                 :      47274 : }
#      60                 :            : 
#      61                 :            : // Explicit overload needed for c_str arguments, which would otherwise cause a substitution failure in the template above.
#      62                 :            : inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
#      63                 :      46453 : {
#      64                 :      46453 :     return Join<std::string>(list, separator);
#      65                 :      46453 : }
#      66                 :            : 
#      67                 :            : /**
#      68                 :            :  * Create an unordered multi-line list of items.
#      69                 :            :  */
#      70                 :            : inline std::string MakeUnorderedList(const std::vector<std::string>& items)
#      71                 :       1598 : {
#      72                 :       9573 :     return Join(items, "\n", [](const std::string& item) { return "- " + item; });
#      73                 :       1598 : }
#      74                 :            : 
#      75                 :            : /**
#      76                 :            :  * Check if a string does not contain any embedded NUL (\0) characters
#      77                 :            :  */
#      78                 :            : [[nodiscard]] inline bool ValidAsCString(const std::string& str) noexcept
#      79                 :    2446257 : {
#      80                 :    2446257 :     return str.size() == strlen(str.c_str());
#      81                 :    2446257 : }
#      82                 :            : 
#      83                 :            : /**
#      84                 :            :  * Locale-independent version of std::to_string
#      85                 :            :  */
#      86                 :            : template <typename T>
#      87                 :            : std::string ToString(const T& t)
#      88                 :    3052306 : {
#      89                 :    3052306 :     std::ostringstream oss;
#      90                 :    3052306 :     oss.imbue(std::locale::classic());
#      91                 :    3052306 :     oss << t;
#      92                 :    3052306 :     return oss.str();
#      93                 :    3052306 : }
#      94                 :            : 
#      95                 :            : /**
#      96                 :            :  * Check whether a container begins with the given prefix.
#      97                 :            :  */
#      98                 :            : template <typename T1, size_t PREFIX_LEN>
#      99                 :            : [[nodiscard]] inline bool HasPrefix(const T1& obj,
#     100                 :            :                                 const std::array<uint8_t, PREFIX_LEN>& prefix)
#     101                 :    2907546 : {
#     102 [ +  - ][ +  - ]:    2907546 :     return obj.size() >= PREFIX_LEN &&
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#     103 [ +  + ][ +  + ]:    2907546 :            std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#     104                 :    2907546 : }
#     105                 :            : 
#     106                 :            : #endif // BITCOIN_UTIL_STRING_H

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a