diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/check.h | 1 | ||||
-rw-r--r-- | src/util/getuniquepath.cpp | 2 | ||||
-rw-r--r-- | src/util/spanparsing.h | 20 | ||||
-rw-r--r-- | src/util/strencodings.cpp | 33 | ||||
-rw-r--r-- | src/util/string.h | 6 | ||||
-rw-r--r-- | src/util/system.cpp | 4 | ||||
-rw-r--r-- | src/util/system.h | 4 | ||||
-rw-r--r-- | src/util/tokenpipe.cpp | 2 |
8 files changed, 57 insertions, 15 deletions
diff --git a/src/util/check.h b/src/util/check.h index 91d62e262d..aca957925a 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -30,7 +30,6 @@ T&& inline_check_non_fatal(T&& val, const char* file, int line, const char* func throw NonFatalCheckError( format_internal_error(assertion, file, line, func, PACKAGE_BUGREPORT)); } - return std::forward<T>(val); } diff --git a/src/util/getuniquepath.cpp b/src/util/getuniquepath.cpp index 6776e7785b..1d8e511c83 100644 --- a/src/util/getuniquepath.cpp +++ b/src/util/getuniquepath.cpp @@ -9,6 +9,6 @@ fs::path GetUniquePath(const fs::path& base) { FastRandomContext rnd; - fs::path tmpFile = base / HexStr(rnd.randbytes(8)); + fs::path tmpFile = base / fs::u8path(HexStr(rnd.randbytes(8))); return tmpFile; }
\ No newline at end of file diff --git a/src/util/spanparsing.h b/src/util/spanparsing.h index ebec8714a7..51795271de 100644 --- a/src/util/spanparsing.h +++ b/src/util/spanparsing.h @@ -8,6 +8,7 @@ #include <span.h> #include <string> +#include <string_view> #include <vector> namespace spanparsing { @@ -36,7 +37,7 @@ bool Func(const std::string& str, Span<const char>& sp); */ Span<const char> Expr(Span<const char>& sp); -/** Split a string on every instance of sep, returning a vector. +/** Split a string on any char found in separators, returning a vector. * * If sep does not occur in sp, a singleton with the entirety of sp is returned. * @@ -44,13 +45,13 @@ Span<const char> Expr(Span<const char>& sp); * "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}. */ template <typename T = Span<const char>> -std::vector<T> Split(const Span<const char>& sp, char sep) +std::vector<T> Split(const Span<const char>& sp, std::string_view separators) { std::vector<T> ret; auto it = sp.begin(); auto start = it; while (it != sp.end()) { - if (*it == sep) { + if (separators.find(*it) != std::string::npos) { ret.emplace_back(start, it); start = it + 1; } @@ -60,6 +61,19 @@ std::vector<T> Split(const Span<const char>& sp, char sep) return ret; } +/** Split a string on every instance of sep, returning a vector. + * + * If sep does not occur in sp, a singleton with the entirety of sp is returned. + * + * Note that this function does not care about braces, so splitting + * "foo(bar(1),2),3) on ',' will return {"foo(bar(1)", "2)", "3)"}. + */ +template <typename T = Span<const char>> +std::vector<T> Split(const Span<const char>& sp, char sep) +{ + return Split<T>(sp, std::string_view{&sep, 1}); +} + } // namespace spanparsing #endif // BITCOIN_UTIL_SPANPARSING_H diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index 35f62f0422..bcedd4f517 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -9,6 +9,7 @@ #include <tinyformat.h> #include <algorithm> +#include <array> #include <cstdlib> #include <cstring> #include <limits> @@ -452,17 +453,37 @@ std::string Capitalize(std::string str) return str; } +namespace { + +using ByteAsHex = std::array<char, 2>; + +constexpr std::array<ByteAsHex, 256> CreateByteToHexMap() +{ + constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + + std::array<ByteAsHex, 256> byte_to_hex{}; + for (size_t i = 0; i < byte_to_hex.size(); ++i) { + byte_to_hex[i][0] = hexmap[i >> 4]; + byte_to_hex[i][1] = hexmap[i & 15]; + } + return byte_to_hex; +} + +} // namespace + std::string HexStr(const Span<const uint8_t> s) { std::string rv(s.size() * 2, '\0'); - static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - auto it = rv.begin(); + static constexpr auto byte_to_hex = CreateByteToHexMap(); + static_assert(sizeof(byte_to_hex) == 512); + + char* it = rv.data(); for (uint8_t v : s) { - *it++ = hexmap[v >> 4]; - *it++ = hexmap[v & 15]; + std::memcpy(it, byte_to_hex[v].data(), 2); + it += 2; } - assert(it == rv.end()); + + assert(it == rv.data() + rv.size()); return rv; } diff --git a/src/util/string.h b/src/util/string.h index 36b9787db4..2e91347b27 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -14,6 +14,7 @@ #include <locale> #include <sstream> #include <string> +#include <string_view> #include <vector> [[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, char sep) @@ -21,6 +22,11 @@ return spanparsing::Split<std::string>(str, sep); } +[[nodiscard]] inline std::vector<std::string> SplitString(std::string_view str, std::string_view separators) +{ + return spanparsing::Split<std::string>(str, separators); +} + [[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v") { std::string::size_type front = str.find_first_not_of(pattern); diff --git a/src/util/system.cpp b/src/util/system.cpp index 6845e815ed..facf6855cb 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -105,7 +105,7 @@ static Mutex cs_dir_locks; */ static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks GUARDED_BY(cs_dir_locks); -bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only) +bool LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only) { LOCK(cs_dir_locks); fs::path pathLockFile = directory / lockfile_name; @@ -129,7 +129,7 @@ bool LockDirectory(const fs::path& directory, const std::string lockfile_name, b return true; } -void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name) +void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name) { LOCK(cs_dir_locks); dir_locks.erase(fs::PathToString(directory / lockfile_name)); diff --git a/src/util/system.h b/src/util/system.h index a66b597d41..a7f4d16911 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -76,8 +76,8 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length); */ [[nodiscard]] bool RenameOver(fs::path src, fs::path dest); -bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false); -void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name); +bool LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only=false); +void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name); bool DirIsWritable(const fs::path& directory); bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0); diff --git a/src/util/tokenpipe.cpp b/src/util/tokenpipe.cpp index 4c091cd2e6..49456814e2 100644 --- a/src/util/tokenpipe.cpp +++ b/src/util/tokenpipe.cpp @@ -3,7 +3,9 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <util/tokenpipe.h> +#if defined(HAVE_CONFIG_H) #include <config/bitcoin-config.h> +#endif #ifndef WIN32 |