feat support NB StartsWith
This commit is contained in:
parent
724a21e6e2
commit
e4b13e116a
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user