Commit 6f38cd40 authored by Jaroslaw Kasperkiewicz's avatar Jaroslaw Kasperkiewicz Committed by Facebook GitHub Bot

Enabling support for folly::splitTo<std::string_view>

Summary:
Adding new `parseTo` overload for `std::string_view`, and related tests.

Facilitates migration from `folly::StringPiece` to `std::string_view` by supporting:

```lang=c++
splitTo<string_view>(...)
```

Reviewed By: yfeldblum

Differential Revision: D27971000

fbshipit-source-id: 732f968eb6797447ae12bdc70dbb82d5fc6a8a33
parent ea1cdd1a
......@@ -1355,6 +1355,15 @@ inline Expected<StringPiece, ConversionCode> parseTo(
return detail::parseToStr(in, out);
}
#if FOLLY_HAS_STRING_VIEW
FOLLY_NODISCARD
inline Expected<StringPiece, ConversionCode> parseTo(
StringPiece in, std::string_view& out) {
out = std::string_view(in.data(), in.size());
return StringPiece{in.end(), in.end()};
}
#endif
FOLLY_NODISCARD
inline Expected<StringPiece, ConversionCode> parseTo(
StringPiece in, fbstring& out) {
......
......@@ -380,6 +380,7 @@ void testString2Integral() {
TEST(Conv, String2Integral) {
testString2Integral<const char*, int8_t, int16_t, int32_t, int64_t>();
testString2Integral<std::string, int8_t, int16_t, int32_t, int64_t>();
testString2Integral<std::string_view, int8_t, int16_t, int32_t, int64_t>();
testString2Integral<fbstring, int8_t, int16_t, int32_t, int64_t>();
// Testing the behavior of the StringPiece* API
......@@ -500,10 +501,10 @@ TEST(Conv, VariadicToDelim) {
template <class String>
void testDoubleToString() {
EXPECT_EQ(to<string>(0.0), "0");
EXPECT_EQ(to<string>(0.5), "0.5");
EXPECT_EQ(to<string>(10.25), "10.25");
EXPECT_EQ(to<string>(1.123e10), "11230000000");
EXPECT_EQ(to<String>(0.0), "0");
EXPECT_EQ(to<String>(0.5), "0.5");
EXPECT_EQ(to<String>(10.25), "10.25");
EXPECT_EQ(to<String>(1.123e10), "11230000000");
}
TEST(Conv, DoubleToString) {
......@@ -1175,10 +1176,11 @@ TEST(Conv, TryStringToEnum) {
EXPECT_EQ(static_cast<A>(50), rv3.value());
}
TEST(Conv, TryStringToFloat) {
auto rv1 = folly::tryTo<float>("");
template <class String>
void tryStringToFloat() {
auto rv1 = folly::tryTo<float>(String(""));
EXPECT_FALSE(rv1.hasValue());
auto rv2 = folly::tryTo<float>("3.14");
auto rv2 = folly::tryTo<float>(String("3.14"));
EXPECT_TRUE(rv2.hasValue());
EXPECT_NEAR(rv2.value(), 3.14, 1e-5);
// No trailing '\0' to expose 1-byte buffer over-read
......@@ -1187,16 +1189,16 @@ TEST(Conv, TryStringToFloat) {
EXPECT_FALSE(rv3.hasValue());
// Exact conversion at numeric limits (8+ decimal digits)
auto rv4 = folly::tryTo<float>("-3.4028235E38");
auto rv4 = folly::tryTo<float>(String("-3.4028235E38"));
EXPECT_TRUE(rv4.hasValue());
EXPECT_EQ(rv4.value(), numeric_limits<float>::lowest());
auto rv5 = folly::tryTo<float>("3.40282346E38");
auto rv5 = folly::tryTo<float>(String("3.40282346E38"));
EXPECT_TRUE(rv5.hasValue());
EXPECT_EQ(rv5.value(), numeric_limits<float>::max());
// Beyond numeric limits
// numeric_limits<float>::lowest() ~= -3.402823466E38
const std::array<folly::StringPiece, 4> kOversizedInputs{{
const std::array<String, 4> kOversizedInputs{{
"-3.403E38",
"-3.4029E38",
"-3.402824E38",
......@@ -1208,7 +1210,7 @@ TEST(Conv, TryStringToFloat) {
}
// NaN
const std::array<folly::StringPiece, 6> kNanInputs{{
const std::array<String, 6> kNanInputs{{
"nan",
"NaN",
"NAN",
......@@ -1222,10 +1224,17 @@ TEST(Conv, TryStringToFloat) {
}
}
TEST(Conv, TryStringToDouble) {
auto rv1 = folly::tryTo<double>("");
TEST(Conv, TryStringToFloat) {
tryStringToFloat<std::string>();
tryStringToFloat<std::string_view>();
tryStringToFloat<folly::StringPiece>();
}
template <class String>
void tryToDouble() {
auto rv1 = folly::tryTo<double>(String(""));
EXPECT_FALSE(rv1.hasValue());
auto rv2 = folly::tryTo<double>("3.14");
auto rv2 = folly::tryTo<double>(String("3.14"));
EXPECT_TRUE(rv2.hasValue());
EXPECT_NEAR(rv2.value(), 3.14, 1e-10);
// No trailing '\0' to expose 1-byte buffer over-read
......@@ -1234,6 +1243,12 @@ TEST(Conv, TryStringToDouble) {
EXPECT_FALSE(rv4.hasValue());
}
TEST(Conv, TryStringToDouble) {
tryToDouble<std::string>();
tryToDouble<std::string_view>();
tryToDouble<folly::StringPiece>();
}
TEST(Conv, TryIntToInt) {
auto rv1 = folly::tryTo<uint8_t>(256);
EXPECT_FALSE(rv1.hasValue());
......@@ -1266,24 +1281,30 @@ TEST(Conv, TryIntToFloat) {
EXPECT_EQ(rv2.value(), 1000.0f);
}
TEST(Conv, TryPtrPairToInt) {
StringPiece sp1("1000000000000000000000000000000");
template <class String>
void tryTo() {
String sp1("1000000000000000000000000000000");
auto rv1 = folly::tryTo<int>(sp1.begin(), sp1.end());
EXPECT_FALSE(rv1.hasValue());
StringPiece sp2("4711");
String sp2("4711");
auto rv2 = folly::tryTo<int>(sp2.begin(), sp2.end());
EXPECT_TRUE(rv2.hasValue());
EXPECT_EQ(rv2.value(), 4711);
StringPiece sp3("-4711");
String sp3("-4711");
auto rv3 = folly::tryTo<int>(sp3.begin(), sp3.end());
EXPECT_TRUE(rv3.hasValue());
EXPECT_EQ(rv3.value(), -4711);
StringPiece sp4("4711");
String sp4("4711");
auto rv4 = folly::tryTo<uint16_t>(sp4.begin(), sp4.end());
EXPECT_TRUE(rv4.hasValue());
EXPECT_EQ(rv4.value(), 4711);
}
TEST(Conv, TryPtrPairToInt) {
tryTo<string_view>();
tryTo<StringPiece>();
}
TEST(Conv, allocate_size) {
std::string str1 = "meh meh meh";
std::string str2 = "zdech zdech zdech";
......
......@@ -673,10 +673,10 @@ void splitTest() {
EXPECT_EQ(parts[3], "");
}
template <template <class, class> class VectorType>
template <class Str, template <class, class> class VectorType>
void piecesTest() {
VectorType<StringPiece, std::allocator<StringPiece>> pieces;
VectorType<StringPiece, std::allocator<StringPiece>> pieces2;
VectorType<Str, std::allocator<Str>> pieces;
VectorType<Str, std::allocator<Str>> pieces2;
folly::split(',', "a,b,c", pieces);
EXPECT_EQ(pieces.size(), 3);
......@@ -799,15 +799,15 @@ void piecesTest() {
pieces.clear();
const char* str = "a,b";
folly::split(',', StringPiece(str), pieces);
folly::split(',', Str(str), pieces);
EXPECT_EQ(pieces.size(), 2);
EXPECT_EQ(pieces[0], "a");
EXPECT_EQ(pieces[1], "b");
EXPECT_EQ(pieces[0].start(), str);
EXPECT_EQ(pieces[1].start(), str + 2);
EXPECT_EQ(pieces[0].begin(), str);
EXPECT_EQ(pieces[1].begin(), str + 2);
std::set<StringPiece> unique;
folly::splitTo<StringPiece>(
std::set<Str> unique;
folly::splitTo<Str>(
":",
"asd:bsd:asd:asd:bsd:csd::asd",
std::inserter(unique, unique.begin()),
......@@ -831,11 +831,18 @@ TEST(Split, split_vector) {
TEST(Split, split_fbvector) {
splitTest<folly::fbvector>();
}
TEST(Split, pieces_vector) {
piecesTest<std::vector>();
piecesTest<StringPiece, std::vector>();
}
TEST(Split, pieces_fbvector) {
piecesTest<folly::fbvector>();
piecesTest<StringPiece, folly::fbvector>();
}
TEST(Split, view_vector) {
piecesTest<std::string_view, std::vector>();
}
TEST(Split, view_fbvector) {
piecesTest<std::string_view, folly::fbvector>();
}
TEST(Split, fixed) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment