folly: StringPiece: add skipWhitespace
authorLucian Grijincu <lucian@fb.com>
Thu, 26 Jun 2014 23:26:33 +0000 (16:26 -0700)
committerTudor Bosman <tudorb@fb.com>
Mon, 7 Jul 2014 15:42:12 +0000 (08:42 -0700)
Test Plan: copied from folly::json

Reviewed By: philipp@fb.com, soren@fb.com

FB internal diff: D1417992

Tasks: 4527315

folly/String.cpp
folly/String.h
folly/json.cpp

index c80c6821c511157400c541177a3ee57223164d3a..612db8cb71eb16e98099aac22c4c5c0e969bb21d 100644 (file)
@@ -203,7 +203,7 @@ const PrettySuffix kPrettySISuffixes[] = {
   { "z", 1e-21L },
   { "y", 1e-24L },
   { " ", 0 },
-  { 0, 0} 
+  { 0, 0}
 };
 
 const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = {
@@ -247,7 +247,7 @@ std::string prettyPrint(double val, PrettyType type, bool addSpace) {
 
 //TODO:
 //1) Benchmark & optimize
-double prettyToDouble(folly::StringPiece *const prettyString, 
+double prettyToDouble(folly::StringPiece *const prettyString,
                       const PrettyType type) {
   double value = folly::to<double>(prettyString);
   while (prettyString->size() > 0 && std::isspace(prettyString->front())) {
@@ -278,13 +278,13 @@ double prettyToDouble(folly::StringPiece *const prettyString,
             prettyString->toString(), "\""));
   }
   prettyString->advance(longestPrefixLen);
-  return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val : 
+  return suffixes[bestPrefixId].val ? value * suffixes[bestPrefixId].val :
                                       value;
 }
 
 double prettyToDouble(folly::StringPiece prettyString, const PrettyType type){
   double result = prettyToDouble(&prettyString, type);
-  detail::enforceWhitespace(prettyString.data(), 
+  detail::enforceWhitespace(prettyString.data(),
                             prettyString.data() + prettyString.size());
   return result;
 }
@@ -329,6 +329,25 @@ fbstring errnoStr(int err) {
   return result;
 }
 
+StringPiece skipWhitespace(StringPiece sp) {
+  // Spaces other than ' ' characters are less common but should be
+  // checked.  This configuration where we loop on the ' '
+  // separately from oddspaces was empirically fastest.
+  auto oddspace = [] (char c) {
+    return c == '\n' || c == '\t' || c == '\r';
+  };
+
+loop:
+  for (; !sp.empty() && sp.front() == ' '; sp.pop_front()) {
+  }
+  if (!sp.empty() && oddspace(sp.front())) {
+    sp.pop_front();
+    goto loop;
+  }
+
+  return sp;
+}
+
 namespace detail {
 
 size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
@@ -385,4 +404,3 @@ size_t hexDumpLine(const void* ptr, size_t offset, size_t size,
 # undef DMGL_TYPES
 # undef DMGL_RET_POSTFIX
 #endif
-
index f597291faa548ad6afe1ca25d0f211c5ebea857f..9a44f5459116c36784a3830e48c807ae0711508a 100644 (file)
@@ -503,6 +503,12 @@ std::string join(const Delim& delimiter,
   return output;
 }
 
+/**
+ * Returns a subpiece with all whitespace removed from the front of @sp.
+ * Whitespace means any of [' ', '\n', '\r', '\t'].
+ */
+StringPiece skipWhitespace(StringPiece sp);
+
 } // namespace folly
 
 // Hash functions to make std::string usable with e.g. hash_map
index f6bf8f2c574666ab308818dcca18ed7590941fc6..cd83b5adffb68209b8edb695a93ce14986f1aa92 100644 (file)
 #include <boost/next_prior.hpp>
 #include <boost/algorithm/string.hpp>
 
+#include <folly/Conv.h>
 #include <folly/Range.h>
+#include <folly/String.h>
 #include <folly/Unicode.h>
-#include <folly/Conv.h>
 
 namespace folly {
 
@@ -323,20 +324,7 @@ struct Input {
   }
 
   void skipWhitespace() {
-    // Spaces other than ' ' characters are less common but should be
-    // checked.  This configuration where we loop on the ' '
-    // separately from oddspaces was empirically fastest.
-    auto oddspace = [] (char c) {
-      return c == '\n' || c == '\t' || c == '\r';
-    };
-
-  loop:
-    for (; !range_.empty() && range_.front() == ' '; range_.pop_front()) {
-    }
-    if (!range_.empty() && oddspace(range_.front())) {
-      range_.pop_front();
-      goto loop;
-    }
+    range_ = folly::skipWhitespace(range_);
     storeCurrent();
   }