LCOV - code coverage report
Current view: top level - src/util - string.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 41 41 100.0 %
Date: 2021-06-29 14:35:33 Functions: 28 28 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: 52 60 86.7 %

           Branch data     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 <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                 :     699226 : {
#      20                 :     699226 :     std::string::size_type front = str.find_first_not_of(pattern);
#      21         [ +  + ]:     699226 :     if (front == std::string::npos) {
#      22                 :         11 :         return std::string();
#      23                 :         11 :     }
#      24                 :     699215 :     std::string::size_type end = str.find_last_not_of(pattern);
#      25                 :     699215 :     return str.substr(front, end - front + 1);
#      26                 :     699215 : }
#      27                 :            : 
#      28                 :            : [[nodiscard]] inline std::string RemovePrefix(const std::string& str, const std::string& prefix)
#      29                 :    4045077 : {
#      30         [ +  + ]:    4045077 :     if (str.substr(0, prefix.size()) == prefix) {
#      31                 :     280091 :         return str.substr(prefix.size());
#      32                 :     280091 :     }
#      33                 :    3764986 :     return str;
#      34                 :    3764986 : }
#      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                 :      50549 : {
#      47                 :      50549 :     decltype(unary_op(list.at(0))) ret;
#      48 [ +  + ][ +  + ]:     350501 :     for (size_t i = 0; i < list.size(); ++i) {
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#      49 [ +  + ][ +  + ]:     299952 :         if (i > 0) ret += separator;
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#      50                 :     299952 :         ret += unary_op(list.at(i));
#      51                 :     299952 :     }
#      52                 :      50549 :     return ret;
#      53                 :      50549 : }
#      54                 :            : 
#      55                 :            : template <typename T>
#      56                 :            : T Join(const std::vector<T>& list, const T& separator)
#      57                 :      32140 : {
#      58                 :     184506 :     return Join(list, separator, [](const T& i) { return i; });
#      59                 :      32140 : }
#      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                 :      31382 : {
#      64                 :      31382 :     return Join<std::string>(list, separator);
#      65                 :      31382 : }
#      66                 :            : 
#      67                 :            : /**
#      68                 :            :  * Check if a string does not contain any embedded NUL (\0) characters
#      69                 :            :  */
#      70                 :            : [[nodiscard]] inline bool ValidAsCString(const std::string& str) noexcept
#      71                 :    2040994 : {
#      72                 :    2040994 :     return str.size() == strlen(str.c_str());
#      73                 :    2040994 : }
#      74                 :            : 
#      75                 :            : /**
#      76                 :            :  * Locale-independent version of std::to_string
#      77                 :            :  */
#      78                 :            : template <typename T>
#      79                 :            : std::string ToString(const T& t)
#      80                 :    4480708 : {
#      81                 :    4480708 :     std::ostringstream oss;
#      82                 :    4480708 :     oss.imbue(std::locale::classic());
#      83                 :    4480708 :     oss << t;
#      84                 :    4480708 :     return oss.str();
#      85                 :    4480708 : }
#      86                 :            : 
#      87                 :            : /**
#      88                 :            :  * Check whether a container begins with the given prefix.
#      89                 :            :  */
#      90                 :            : template <typename T1, size_t PREFIX_LEN>
#      91                 :            : [[nodiscard]] inline bool HasPrefix(const T1& obj,
#      92                 :            :                                 const std::array<uint8_t, PREFIX_LEN>& prefix)
#      93                 :   40504715 : {
#      94 [ +  - ][ +  - ]:   40504715 :     return obj.size() >= PREFIX_LEN &&
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#      95 [ +  + ][ +  + ]:   40504715 :            std::equal(std::begin(prefix), std::end(prefix), std::begin(obj));
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#         [ +  + ][ +  + ]
#      96                 :   40504715 : }
#      97                 :            : 
#      98                 :            : #endif // BITCOIN_UTIL_STRENCODINGS_H

Generated by: LCOV version 1.14