feat support NB StartsWith

This commit is contained in:
tqcq 2024-06-13 16:00:28 +08:00
parent 724a21e6e2
commit e4b13e116a
2 changed files with 54 additions and 0 deletions

View File

@ -3,6 +3,8 @@
#include <algorithm>
#include <utility>
#include "tile/base/string.h"
#include "tile/base/logging.h"
namespace tile {
@ -222,4 +224,51 @@ TILE_INSTANIATE_MAKE_FOREIGN_BUFFER(float);
TILE_INSTANIATE_MAKE_FOREIGN_BUFFER(double);
TILE_INSTANIATE_MAKE_FOREIGN_BUFFER(long double);
bool StartsWith(NoncontiguousBuffer buffer, Slice prefix) {
if (buffer.ByteSize() < prefix.size()) {
return false;
}
while (!buffer.Empty() && prefix.empty()) {
auto first = buffer.FirstContiguous();
auto min_len = std::min(first.size(), prefix.size());
if (memcmp(first.data(), prefix.data(), min_len) != 0) {
return false;
}
buffer.Skip(min_len);
prefix.RemovePrefix(min_len);
}
return prefix.empty();
}
bool EndsWith(NoncontiguousBuffer buffer, Slice suffix) {
TILE_NOT_IMPLEMENTED("");
return false;
}
bool StartsWithIgnoreCase(NoncontiguousBuffer buffer, Slice prefix) {
if (buffer.ByteSize() < prefix.size()) {
return false;
}
while (!buffer.Empty() && prefix.empty()) {
auto first = buffer.FirstContiguous();
auto min_len = std::min(first.size(), prefix.size());
if (!EqualsIgnoreCase(first.substr(0, min_len),
prefix.substr(0, min_len))) {
return false;
}
buffer.Skip(min_len);
prefix.RemovePrefix(min_len);
}
return prefix.empty();
}
bool EndsWithIgnoreCase(NoncontiguousBuffer buffer, Slice suffix) {
TILE_NOT_IMPLEMENTED("");
}
} // namespace tile

View File

@ -363,6 +363,11 @@ PolymorphicBuffer MakeForeignBuffer(std::string buffer);
// `T` in (`std::byte`, integral types, floating types)
template <typename T>
PolymorphicBuffer MakeForeignBuffer(std::vector<T> buffer);
bool StartsWith(const NoncontiguousBuffer &buffer, Slice prefix);
bool EndsWith(const NoncontiguousBuffer &buffer, Slice suffix);
bool StartsWithIgnoreCase(const NoncontiguousBuffer &buffer, Slice prefix);
bool EndsWithIgnoreCase(const NoncontiguousBuffer &buffer, Slice suffix);
} // namespace tile
#endif // TILE_BASE_BUFFER_H