Adding a constructor to UTF8Range that uses std::string
authorAditya Muttur <adityamuttur@fb.com>
Tue, 26 Apr 2016 22:53:40 +0000 (15:53 -0700)
committerFacebook Github Bot 0 <facebook-github-bot-0-bot@fb.com>
Tue, 26 Apr 2016 23:05:22 +0000 (16:05 -0700)
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
folly/test/StringTest.cpp

index 78e946c66a67bfe49fb16d64bc5ecf908c9eb842..ba43160d5606609559d123dcda5a847d3cbb6a91 100644 (file)
@@ -649,6 +649,8 @@ class UTF8Range : public Base {
                  baseRange.begin(), baseRange.begin(), baseRange.end()),
              boost::u8_to_u32_iterator<Iterator>(
                  baseRange.end(), baseRange.begin(), baseRange.end())) {}
+  /* implicit */ UTF8Range(const std::string& baseString)
+      : Base(folly::Range<Iterator>(baseString)) {}
 };
 
 using UTF8StringPiece = UTF8Range<const char*>;
index d453ffb20f4a54296131ee2d7425b13a10652068..467141efddee65da4ab3786b82be6773ecbca781 100644 (file)
@@ -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));
+}