Commit 38fb7acd authored by Lucian Grijincu's avatar Lucian Grijincu Committed by Tudor Bosman

folly: StringPiece: add skipWhitespace

Test Plan: copied from folly::json

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

FB internal diff: D1417992

Tasks: 4527315
parent 368266c3
......@@ -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
......@@ -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
......
......@@ -19,9 +19,10 @@
#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();
}
......
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