Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use starts_with and ends_with member functions in std::string. #32171

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eval/src/vespa/eval/eval/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ Function::dump_as_lambda() const
}
lambda += ")";
vespalib::string expr = dump();
if (starts_with(expr, "(")) {
if (expr.starts_with("(")) {
lambda += expr;
} else {
lambda += "(";
Expand Down
4 changes: 2 additions & 2 deletions eval/src/vespa/eval/eval/tensor_nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ vespalib::string
TensorLambda::dump(DumpContext &) const {
vespalib::string str = _type.to_spec();
vespalib::string expr = _lambda->dump();
if (starts_with(expr, "(")) {
if (expr.starts_with("(")) {
str += expr;
} else {
str += "(";
Expand All @@ -173,7 +173,7 @@ TensorPeek::dump(DumpContext &ctx) const {
str += ":";
if (dim.second.is_expr()) {
vespalib::string expr = dim.second.expr->dump(ctx);
if (starts_with(expr, "(")) {
if (expr.starts_with("(")) {
str += expr;
} else {
str += "(";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void decode_json(const vespalib::string &path, Slime &slime) {
if (!file.valid()) {
LOG(warning, "could not read file: %s", path.c_str());
} else {
if (ends_with(path, ".lz4")) {
if (path.ends_with(".lz4")) {
size_t buffer_size = 64_Ki;
Lz4InputDecoder lz4_decoder(file, buffer_size);
decode_json(path, lz4_decoder, slime);
Expand All @@ -279,7 +279,7 @@ ConstantTensorLoader::create(const vespalib::string &path, const vespalib::strin
LOG(warning, "invalid type specification: %s", type.c_str());
return std::make_unique<BadConstantValue>();
}
if (ends_with(path, ".tbf")) {
if (path.ends_with(".tbf")) {
vespalib::MappedFileInput file(path);
vespalib::Memory content = file.get();
vespalib::nbostream stream(content.data, content.size);
Expand Down
10 changes: 5 additions & 5 deletions searchlib/src/apps/vespa-query-analyzer/vespa-query-analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,19 @@ struct Sample {
size_t count = 0;
explicit Sample(const Inspector &sample) {
auto name = sample["name"].asString().make_stringview();
if (ends_with(name, "/init")) {
if (name.ends_with("/init")) {
type = Type::INIT;
}
if (ends_with(name, "/seek")) {
if (name.ends_with("/seek")) {
type = Type::SEEK;
}
if (ends_with(name, "/unpack")) {
if (name.ends_with("/unpack")) {
type = Type::UNPACK;
}
if (ends_with(name, "/termwise")) {
if (name.ends_with("/termwise")) {
type = Type::TERMWISE;
}
if (starts_with(name, "/")) {
if (name.starts_with("/")) {
size_t child = 0;
for (size_t pos = 1; pos < name.size(); ++pos) {
char c = name[pos];
Expand Down
24 changes: 11 additions & 13 deletions vespalib/src/tests/stllike/string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,25 +372,23 @@ TEST("require that contains works") {
EXPECT_FALSE(contains(s, "not in there"));
}

using vespalib::starts_with;
TEST("require that starts_with works") {
vespalib::string s("require that starts_with works");
EXPECT_TRUE(starts_with(s, "require"));
EXPECT_TRUE(starts_with(s, "require that starts_with work"));
EXPECT_TRUE(starts_with(s, "require that starts_with works"));
EXPECT_FALSE(starts_with(s, "equire"));
EXPECT_FALSE(starts_with(s, "not in there"));
EXPECT_TRUE(s.starts_with("require"));
EXPECT_TRUE(s.starts_with("require that starts_with work"));
EXPECT_TRUE(s.starts_with("require that starts_with works"));
EXPECT_FALSE(s.starts_with("equire"));
EXPECT_FALSE(s.starts_with("not in there"));
}

using vespalib::ends_with;
TEST("require that ends_with works") {
vespalib::string s("require that ends_with works");
EXPECT_FALSE(ends_with(s, "require"));
EXPECT_TRUE(ends_with(s, "works"));
EXPECT_TRUE(ends_with(s, "equire that ends_with works"));
EXPECT_TRUE(ends_with(s, "require that ends_with works"));
EXPECT_FALSE(ends_with(s, "work"));
EXPECT_FALSE(ends_with(s, "not in there"));
EXPECT_FALSE(s.ends_with("require"));
EXPECT_TRUE(s.ends_with("works"));
EXPECT_TRUE(s.ends_with("equire that ends_with works"));
EXPECT_TRUE(s.ends_with("require that ends_with works"));
EXPECT_FALSE(s.ends_with("work"));
EXPECT_FALSE(s.ends_with("not in there"));
}

TEST("test that small_string::pop_back works") {
Expand Down
6 changes: 3 additions & 3 deletions vespalib/src/vespa/vespalib/net/socket_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ const SocketSpec SocketSpec::invalid;
SocketSpec::SocketSpec(const vespalib::string &spec)
: SocketSpec()
{
if (starts_with(spec, ipc_path_prefix)) {
if (spec.starts_with(ipc_path_prefix)) {
_node = spec.substr(ipc_path_prefix.size());
_type = Type::PATH;
} else if (starts_with(spec, ipc_name_prefix)) {
} else if (spec.starts_with(ipc_name_prefix)) {
_node = spec.substr(ipc_name_prefix.size());
_type = Type::NAME;
} else if (starts_with(spec, tcp_prefix)) {
} else if (spec.starts_with(tcp_prefix)) {
bool with_host = (spec.find(':') != spec.npos);
const char *port_str = spec.c_str() + (with_host
? (spec.rfind(':') + 1)
Expand Down
2 changes: 1 addition & 1 deletion vespalib/src/vespa/vespalib/portal/portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Portal::lookup_get_handler(const vespalib::string &uri, GetHandler *&handler)
{
std::lock_guard guard(_lock);
for (const auto &entry: _bind_list) {
if (starts_with(uri, entry.prefix)) {
if (uri.starts_with(entry.prefix)) {
auto handle_guard = _handle_manager.lock(entry.handle);
if (handle_guard.valid()) {
handler = entry.handler;
Expand Down
14 changes: 0 additions & 14 deletions vespalib/src/vespa/vespalib/stllike/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ inline bool contains(std::string_view text, std::string_view key) noexcept {
return text.find(key) != std::string_view::npos;
}

inline bool starts_with(std::string_view text, std::string_view key) noexcept {
if (text.size() >= key.size()) {
return memcmp(text.begin(), key.begin(), key.size()) == 0;
}
return false;
}

inline bool ends_with(std::string_view text, std::string_view key) noexcept {
if (text.size() >= key.size()) {
return memcmp(text.end()-key.size(), key.begin(), key.size()) == 0;
}
return false;
}

// returns a reference to a shared empty string
const string &empty_string() noexcept;

Expand Down