/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <type_traits>
#include <vector>
#include <boost/range/concepts.hpp>
+#include <boost/algorithm/string/trim.hpp>
#include <gtest/gtest.h>
-namespace folly { namespace detail {
-
-// declaration of functions in Range.cpp
-size_t qfind_first_byte_of_memchr(const StringPiece haystack,
- const StringPiece needles);
-
-size_t qfind_first_byte_of_byteset(const StringPiece haystack,
- const StringPiece needles);
-
-}} // namespaces
-
using namespace folly;
using namespace std;
EXPECT_THROW(a.subpiece(6), std::out_of_range);
}
-#if FOLLY_HAVE_CONSTEXPR_STRLEN
constexpr char helloArray[] = "hello";
TEST(StringPiece, Constexpr) {
constexpr StringPiece hello2(helloArray);
EXPECT_EQ("hello", hello2);
}
-#endif
TEST(StringPiece, Prefix) {
StringPiece a("hello");
EXPECT_EQ("", a);
}
+TEST(StringPiece, erase) {
+ StringPiece a("hello");
+ auto b = a.begin();
+ auto e = b + 1;
+ a.erase(b, e);
+ EXPECT_EQ("ello", a);
+
+ e = a.end();
+ b = e - 1;
+ a.erase(b, e);
+ EXPECT_EQ("ell", a);
+
+ b = a.end() - 1;
+ e = a.end() - 1;
+ EXPECT_THROW(a.erase(b, e), std::out_of_range);
+
+ b = a.begin();
+ e = a.end();
+ a.erase(b, e);
+ EXPECT_EQ("", a);
+
+ a = "hello";
+ b = a.begin();
+ e = b + 2;
+ a.erase(b, e);
+ EXPECT_EQ("llo", a);
+
+ b = a.end() - 2;
+ e = a.end();
+ a.erase(b, e);
+ EXPECT_EQ("l", a);
+
+ a = " hello ";
+ boost::algorithm::trim(a);
+ EXPECT_EQ(a, "hello");
+}
+
TEST(StringPiece, split_step_char_delimiter) {
// 0 1 2
// 012345678901234567890123456
}
};
-struct MemchrNeedleFinder {
- static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
- return detail::qfind_first_byte_of_memchr(haystack, needles);
- }
-};
-
struct ByteSetNeedleFinder {
static size_t find_first_byte_of(StringPiece haystack, StringPiece needles) {
return detail::qfind_first_byte_of_byteset(haystack, needles);
}
};
-typedef ::testing::Types<SseNeedleFinder, NoSseNeedleFinder, MemchrNeedleFinder,
+typedef ::testing::Types<SseNeedleFinder,
+ NoSseNeedleFinder,
ByteSetNeedleFinder> NeedleFinders;
TYPED_TEST_CASE(NeedleFinderTest, NeedleFinders);
string s = "0123456789ABCDEFGH";
for (size_t i = 0; i < s.size(); ++i) {
StringPiece a(s.c_str() + i);
- for (int j = 0; j < s.size(); ++j) {
+ for (size_t j = 0; j < s.size(); ++j) {
StringPiece b(s.c_str() + j);
EXPECT_EQ((i > j) ? 0 : j - i, this->find_first_byte_of(a, b));
}