From 545134d11e72181e9df4bdcbd17ba6ccc8196c07 Mon Sep 17 00:00:00 2001 From: Aditya Muttur Date: Tue, 26 Apr 2016 15:53:40 -0700 Subject: [PATCH] Adding a constructor to UTF8Range that uses std::string Summary: Currently UTF8Range has a constructor that allows you construct an object of type UTF8Range using only an object of type folly::Range. Adding a constructor so that we can construct an UTF8Range object using a std::string. Currently, void sampleMethod(UTF8StringPiece sp) {...} /* ... */ std::string str = "example"; folly::UTF8Range utf8Range(str); folly::StringPiece sp(str); sampleMethod(utf8Range); // works sampleMethod(sp); // works sampleMethod(str); // doesn't work This diff hopes to fix this issue. Reviewed By: ddrcoder Differential Revision: D3221144 fb-gh-sync-id: dd6ec4d7790d4602dccb3b63a4861d358aed3608 fbshipit-source-id: dd6ec4d7790d4602dccb3b63a4861d358aed3608 --- folly/String.h | 2 ++ folly/test/StringTest.cpp | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/folly/String.h b/folly/String.h index 78e946c6..ba43160d 100644 --- a/folly/String.h +++ b/folly/String.h @@ -649,6 +649,8 @@ class UTF8Range : public Base { baseRange.begin(), baseRange.begin(), baseRange.end()), boost::u8_to_u32_iterator( baseRange.end(), baseRange.begin(), baseRange.end())) {} + /* implicit */ UTF8Range(const std::string& baseString) + : Base(folly::Range(baseString)) {} }; using UTF8StringPiece = UTF8Range; diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index d453ffb2..467141ef 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -1345,3 +1345,11 @@ TEST(UTF8StringPiece, empty_mid_codepoint) { TEST(UTF8StringPiece, invalid_mid_codepoint) { EXPECT_THROW(UTF8StringPiece(kTestUTF8.subpiece(9, 1)), std::out_of_range); } + +TEST(UTF8StringPiece, valid_implicit_conversion) { + std::string input = "\U0001F602\U0001F602\U0001F602"; + auto checkImplicitCtor = [](UTF8StringPiece implicitCtor) { + return implicitCtor.walk_size(); + }; + EXPECT_EQ(3, checkImplicitCtor(input)); +} -- 2.34.1