Issue with find and npos
[folly.git] / folly / Uri.cpp
index c2bd85a2fe81db95dc303e583cb9f691eed443ad..9d4ad2acf37535f7ba5487bdeea051b45bf73ec2 100644 (file)
@@ -47,7 +47,7 @@ Uri::Uri(StringPiece str) : port_(0) {
 
   boost::cmatch match;
   if (UNLIKELY(!boost::regex_match(str.begin(), str.end(), match, uriRegex))) {
-    throw std::invalid_argument("invalid URI");
+    throw std::invalid_argument(to<std::string>("invalid URI ", str));
   }
 
   scheme_ = submatch(match, 1);
@@ -74,7 +74,9 @@ Uri::Uri(StringPiece str) : port_(0) {
                             authority.second,
                             authorityMatch,
                             authorityRegex)) {
-      throw std::invalid_argument("invalid URI authority");
+      throw std::invalid_argument(
+          to<std::string>("invalid URI authority ",
+                          StringPiece(authority.first, authority.second)));
     }
 
     StringPiece port(authorityMatch[4].first, authorityMatch[4].second);
@@ -92,23 +94,28 @@ Uri::Uri(StringPiece str) : port_(0) {
   fragment_ = submatch(match, 4);
 }
 
-fbstring
-Uri::authority() const
-{
-  fbstring result(host());
+fbstring Uri::authority() const {
+  fbstring result;
 
-  if (port() != 0) {
-    result += fbstring(":") + to<fbstring>(port());
-  }
+  // Port is 5 characters max and we have up to 3 delimiters.
+  result.reserve(host().size() + username().size() + password().size() + 8);
 
-  if (!username().empty()) {
-    fbstring userInformation(username());
+  if (!username().empty() || !password().empty()) {
+    result.append(username());
 
     if (!password().empty()) {
-      userInformation += fbstring(":") + password();
+      result.push_back(':');
+      result.append(password());
     }
 
-    result = userInformation + "@" + result;
+    result.push_back('@');
+  }
+
+  result.append(host());
+
+  if (port() != 0) {
+    result.push_back(':');
+    toAppend(port(), &result);
   }
 
   return result;