Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

utils: implement string_view::find #128

Merged
merged 3 commits into from
Jul 12, 2018
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
7 changes: 7 additions & 0 deletions include/dsn/utility/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@ class string_view
return substr(pos1, count1).compare(string_view(s, count2));
}

// string_view::find()
//
// Finds the first occurrence of the substring `s` within the `string_view`,
// returning the position of the first character's match, or `npos` if no
// match was found.
size_type find(string_view s, size_type pos = 0) const noexcept;

private:
const char *ptr_;
size_type length_;
Expand Down
25 changes: 25 additions & 0 deletions src/core/core/memutil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstddef>

namespace dsn {
namespace strings_internal {

// This is significantly faster for case-sensitive matches with very
// few possible matches. See unit test for benchmarks.
const char *memmatch(const char *phaystack, size_t haylen, const char *pneedle, size_t neelen);

} // namespace strings_internal
} // namespace dsn
62 changes: 62 additions & 0 deletions src/core/core/string_view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <dsn/utility/string_view.h>

#include "memutil.h"

namespace dsn {

namespace strings_internal {

// This is significantly faster for case-sensitive matches with very
// few possible matches. See unit test for benchmarks.
const char *memmatch(const char *phaystack, size_t haylen, const char *pneedle, size_t neelen)
{
if (0 == neelen) {
return phaystack; // even if haylen is 0
}
if (haylen < neelen)
return nullptr;

const char *match;
const char *hayend = phaystack + haylen - neelen + 1;
// A static cast is used here to work around the fact that memchr returns
// a void* on Posix-compliant systems and const void* on Windows.
while ((match = static_cast<const char *>(memchr(phaystack, pneedle[0], hayend - phaystack)))) {
if (memcmp(match, pneedle, neelen) == 0)
return match;
else
phaystack = match + 1;
}
return nullptr;
}

} // namespace strings_internal

string_view::size_type string_view::find(string_view s, size_type pos) const noexcept
{
if (empty() || pos > length_) {
if (empty() && pos == 0 && s.empty())
return 0;
return npos;
}
const char *result = strings_internal::memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_);
return result ? result - ptr_ : npos;
}

constexpr string_view::size_type string_view::npos;

} // namespace dsn
31 changes: 31 additions & 0 deletions src/core/tests/memutil_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2017 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Unit test for memutil.cc

#include "memutil.h"

#include <gtest/gtest.h>

TEST(MemUtilTest, memmatch)
{
const char kHaystack[] = "0123456789";
EXPECT_EQ(dsn::strings_internal::memmatch(kHaystack, 0, "", 0), kHaystack);
EXPECT_EQ(dsn::strings_internal::memmatch(kHaystack, 10, "012", 3), kHaystack);
EXPECT_EQ(dsn::strings_internal::memmatch(kHaystack, 10, "0xx", 1), kHaystack);
EXPECT_EQ(dsn::strings_internal::memmatch(kHaystack, 10, "789", 3), kHaystack + 7);
EXPECT_EQ(dsn::strings_internal::memmatch(kHaystack, 10, "9xx", 1), kHaystack + 9);
EXPECT_TRUE(dsn::strings_internal::memmatch(kHaystack, 10, "9xx", 3) == nullptr);
EXPECT_TRUE(dsn::strings_internal::memmatch(kHaystack, 10, "xxx", 1) == nullptr);
}
92 changes: 91 additions & 1 deletion src/core/tests/string_view_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,59 @@

namespace {

// Separated from STL1() because some compilers produce an overly
// large stack frame for the combined function.
TEST(StringViewTest, STL2)
{
const dsn::string_view a("abcdefghijklmnopqrstuvwxyz");
const dsn::string_view b("abc");
const dsn::string_view c("xyz");
dsn::string_view d("foobar");
const dsn::string_view e;
const dsn::string_view f("123"
"\0"
"456",
7);

d = dsn::string_view();
EXPECT_EQ(d.size(), 0);
EXPECT_TRUE(d.empty());
EXPECT_TRUE(d.data() == nullptr);
EXPECT_TRUE(d.begin() == d.end());

EXPECT_EQ(a.find(b), 0);
EXPECT_EQ(a.find(b, 1), dsn::string_view::npos);
EXPECT_EQ(a.find(c), 23);
EXPECT_EQ(a.find(c, 9), 23);
EXPECT_EQ(a.find(c, dsn::string_view::npos), dsn::string_view::npos);
EXPECT_EQ(b.find(c), dsn::string_view::npos);
EXPECT_EQ(b.find(c, dsn::string_view::npos), dsn::string_view::npos);
EXPECT_EQ(a.find(d), 0);
EXPECT_EQ(a.find(e), 0);
EXPECT_EQ(a.find(d, 12), 12);
EXPECT_EQ(a.find(e, 17), 17);
dsn::string_view g("xx not found bb");
EXPECT_EQ(a.find(g), dsn::string_view::npos);
// empty std::string nonsense
EXPECT_EQ(d.find(b), dsn::string_view::npos);
EXPECT_EQ(e.find(b), dsn::string_view::npos);
EXPECT_EQ(d.find(b, 4), dsn::string_view::npos);
EXPECT_EQ(e.find(b, 7), dsn::string_view::npos);

size_t empty_search_pos = std::string().find(std::string());
EXPECT_EQ(d.find(d), empty_search_pos);
EXPECT_EQ(d.find(e), empty_search_pos);
EXPECT_EQ(e.find(d), empty_search_pos);
EXPECT_EQ(e.find(e), empty_search_pos);
EXPECT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
EXPECT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
EXPECT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
EXPECT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
}

// Continued from STL2
TEST(StringViewTest, STL2Substr) {
TEST(StringViewTest, STL2Substr)
{
const dsn::string_view a("abcdefghijklmnopqrstuvwxyz");
const dsn::string_view b("abc");
const dsn::string_view c("xyz");
Expand Down Expand Up @@ -281,6 +332,7 @@ TEST(StringViewTest, Noexcept)
EXPECT_TRUE(noexcept(sp.empty()));
EXPECT_TRUE(noexcept(sp.data()));
EXPECT_TRUE(noexcept(sp.compare(sp)));
EXPECT_TRUE(noexcept(sp.find(sp)));
}

TEST(StringViewTest, HeterogenousStringViewEquals)
Expand All @@ -289,4 +341,42 @@ TEST(StringViewTest, HeterogenousStringViewEquals)
EXPECT_EQ("hello", dsn::string_view("hello"));
}

TEST(StringViewTest, FindConformance)
{
struct
{
std::string haystack;
std::string needle;
} specs[] = {
{"", ""},
{"", "a"},
{"a", ""},
{"a", "a"},
{"a", "b"},
{"aa", ""},
{"aa", "a"},
{"aa", "b"},
{"ab", "a"},
{"ab", "b"},
{"abcd", ""},
{"abcd", "a"},
{"abcd", "d"},
{"abcd", "ab"},
{"abcd", "bc"},
{"abcd", "cd"},
{"abcd", "abcd"},
};
for (const auto &s : specs) {
SCOPED_TRACE(s.haystack);
SCOPED_TRACE(s.needle);
std::string st = s.haystack;
dsn::string_view sp = s.haystack;
for (size_t i = 0; i <= sp.size(); ++i) {
size_t pos = (i == sp.size()) ? dsn::string_view::npos : i;
SCOPED_TRACE(pos);
EXPECT_EQ(sp.find(s.needle, pos), st.find(s.needle, pos));
}
}
}

} // namespace dsn