fix string trim

This commit is contained in:
tqcq 2025-01-09 11:14:38 +08:00
parent 5e93fa78fb
commit c407aadbb8
4 changed files with 58 additions and 25 deletions

View File

@ -225,6 +225,28 @@ operator+(const Slice &lhs, const Slice &rhs)
return lhs.ToString() + rhs.ToString();
}
static_assert(std::is_constructible<Slice, char>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, char &>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, const char>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, const char &>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, char[]>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, const char[]>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, char[2]>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, const char[2]>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, char *>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, const char *>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, void *, int>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, const void *, int>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, std::string>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, std::string &>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, const std::string>::value, "Can't Build Sliece");
static_assert(std::is_constructible<Slice, const std::string &>::value, "Can't Build Sliece");
}// namespace tile
namespace std {

View File

@ -121,24 +121,6 @@ Split(Slice s, Slice delim, bool keep_empty, std::size_t max_split_parts)
return splited;
}
Slice
TrimLeft(Slice s, Slice cutset)
{
return TrimLeft(s, [&cutset](char c) { return cutset.find(Slice(&c, 1)) != Slice::npos; });
}
Slice
TrimRight(Slice s, Slice cutset)
{
return TrimRight(s, [&cutset](char c) { return cutset.find(Slice(&c, 1)) != Slice::npos; });
}
Slice
Trim(Slice s, Slice cutset)
{
return Trim(s, [&cutset](char c) { return cutset.find(Slice(&c, 1)) != Slice::npos; });
}
template<typename T>
void
JoinImpl(const T &parts, Slice delim, std::string *result)

View File

@ -39,12 +39,8 @@ bool EndsWithIgnoreCase(Slice s, Slice prefix);
void Replace(Slice from, Slice to, std::string *str, std::size_t count = std::numeric_limits<std::size_t>::max());
std::string Replace(Slice str, Slice from, Slice to, std::size_t count = std::numeric_limits<std::size_t>::max());
Slice TrimLeft(Slice s, Slice cutset);
Slice TrimRight(Slice s, Slice cutset);
Slice Trim(Slice s, Slice cutset);
template<typename Pred = int(int)>
Slice
enable_if_t<!std::is_constructible<Slice, Pred>::value, Slice>
TrimLeft(Slice s, Pred pred = isspace)
{
while (!s.empty() && pred(s[0])) { s.RemovePrefix(1); }
@ -52,7 +48,7 @@ TrimLeft(Slice s, Pred pred = isspace)
}
template<typename Pred = int(int)>
Slice
enable_if_t<!std::is_constructible<Slice, Pred>::value, Slice>
TrimRight(Slice s, Pred pred = isspace)
{
while (!s.empty() && pred(s[s.size() - 1])) { s.RemoveSuffix(1); }
@ -60,12 +56,36 @@ TrimRight(Slice s, Pred pred = isspace)
}
template<typename Pred = int(int)>
Slice
enable_if_t<!std::is_constructible<Slice, Pred>::value, Slice>
Trim(Slice s, Pred pred = isspace)
{
return TrimRight(TrimLeft(s, pred), pred);
}
template<typename T>
enable_if_t<std::is_constructible<Slice, T>::value, Slice>
TrimLeft(Slice s, T slice_like)
{
Slice cutset(slice_like);
return TrimLeft(s, [&cutset](char c) { return cutset.find(c) != Slice::npos; });
}
template<typename T>
enable_if_t<std::is_constructible<Slice, T>::value, Slice>
TrimRight(Slice s, T slice_like)
{
Slice cutset(slice_like);
return TrimRight(s, [&cutset](char c) { return cutset.find(c) != Slice::npos; });
}
template<typename T>
enable_if_t<std::is_constructible<Slice, T>::value, Slice>
Trim(Slice s, T slice_like)
{
Slice cutset(slice_like);
return Trim(s, [&cutset](char c) { return cutset.find(c) != Slice::npos; });
}
std::vector<Slice> Split(Slice s,
char delim,
bool keep_empty = false,

View File

@ -184,6 +184,15 @@ TEST(String, Trim)
ASSERT_EQ("a a", Trim(" a a "));
}
TEST(String, TrimByCustset)
{
static_assert(std::is_constructible<Slice, decltype("d")>::value, "");
static_assert(!std::is_constructible<Slice, int(int)>::value, "");
ASSERT_EQ(" ", Trim("abcd abcd", "abcd"));
ASSERT_EQ("abcd abc", Trim("abcd abcd", "d"));
ASSERT_EQ("abcd abcd ", Trim("abcd abcd ", "d"));
}
TEST(String, Split1)
{
auto splited = Split("/a/b/c/d/e/f///g", '/');