From 729ee8b86c450e610214e174ba4faed29c0942be Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:05:50 +0800 Subject: [PATCH] fix(string) Trim error --- CMakeLists.txt | 10 +++++----- tile/base/string.cc | 20 -------------------- tile/base/string.h | 27 ++++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 80a5fb2..c2a1bee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,11 +75,11 @@ include(cmake/BuildInfo.cmake) check_symbol_exists("getifaddrs" "ifaddrs.h" TILE_HAVE_GETIFADDRS) check_symbol_exists("freeifaddrs" "ifaddrs.h" TILE_HAVE_FREEIFADDRS) -find_package(Git REQUIRED) - -get_git_commit_hash(GIT_COMMIT_HASH) -get_git_commit_date(GIT_COMMIT_DATE) -get_git_commit_subject(GIT_COMMIT_SUBJECT) +# find_package(Git REQUIRED) +# +# get_git_commit_hash(GIT_COMMIT_HASH) +# get_git_commit_date(GIT_COMMIT_DATE) +# get_git_commit_subject(GIT_COMMIT_SUBJECT) set(THIRD_PARTY_INCLUDE_DIRS # "third_party/json" "third_party/inja" diff --git a/tile/base/string.cc b/tile/base/string.cc index 82c3b15..deb2c90 100644 --- a/tile/base/string.cc +++ b/tile/base/string.cc @@ -139,26 +139,6 @@ Trim(Slice s, Slice cutset) return Trim(s, [&cutset](char c) { return cutset.find(Slice(&c, 1)) != Slice::npos; }); } -Slice -TrimLeft(Slice s, std::function pred) -{ - while (!s.empty() && pred(s[0])) { s.RemovePrefix(1); } - return s; -} - -Slice -TrimRight(Slice s, std::function pred) -{ - while (!s.empty() && pred(s[s.size() - 1])) { s.RemoveSuffix(1); } - return s; -} - -Slice -Trim(Slice s, std::function pred) -{ - return TrimRight(TrimLeft(s, pred), pred); -} - template void JoinImpl(const T &parts, Slice delim, std::string *result) diff --git a/tile/base/string.h b/tile/base/string.h index 267ed5d..067e737 100644 --- a/tile/base/string.h +++ b/tile/base/string.h @@ -8,6 +8,7 @@ #include "tile/base/optional.h" #include "tile/base/slice.h" +#include #include #include #include @@ -41,9 +42,29 @@ std::string Replace(Slice str, Slice from, Slice to, std::size_t count = std::nu Slice TrimLeft(Slice s, Slice cutset); Slice TrimRight(Slice s, Slice cutset); Slice Trim(Slice s, Slice cutset); -Slice TrimLeft(Slice s, std::function pred = isspace); -Slice TrimRight(Slice s, std::function pred = isspace); -Slice Trim(Slice s, std::function pred = isspace); + +template +Slice +TrimLeft(Slice s, Pred pred = isspace) +{ + while (!s.empty() && pred(s[0])) { s.RemovePrefix(1); } + return s; +} + +template +Slice +TrimRight(Slice s, Pred pred = isspace) +{ + while (!s.empty() && pred(s[s.size() - 1])) { s.RemoveSuffix(1); } + return s; +} + +template +Slice +Trim(Slice s, Pred pred = isspace) +{ + return TrimRight(TrimLeft(s, pred), pred); +} std::vector Split(Slice s, char delim,