/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
template<class OutStringT, class DelimT, class OutputIterator>
void internalSplit(DelimT delim, StringPiece sp, OutputIterator out,
bool ignoreEmpty) {
- assert(sp.start() != nullptr);
+ assert(sp.empty() || sp.start() != nullptr);
const char* s = sp.start();
const size_t strSize = sp.size();
}
inline char prepareDelim(char c) { return c; }
+template<bool exact,
+ class Delim>
+bool splitFixed(const Delim& delimiter,
+ StringPiece input,
+ StringPiece& out) {
+ if (exact && UNLIKELY(std::string::npos != input.find(delimiter))) {
+ return false;
+ }
+ out = input;
+ return true;
+}
+
+template<bool exact,
+ class Delim,
+ class... StringPieces>
+bool splitFixed(const Delim& delimiter,
+ StringPiece input,
+ StringPiece& outHead,
+ StringPieces&... outTail) {
+ size_t cut = input.find(delimiter);
+ if (UNLIKELY(cut == std::string::npos)) {
+ return false;
+ }
+ StringPiece head(input.begin(), input.begin() + cut);
+ StringPiece tail(input.begin() + cut + detail::delimSize(delimiter),
+ input.end());
+ if (LIKELY(splitFixed<exact>(delimiter, tail, outTail...))) {
+ outHead = head;
+ return true;
+ }
+ return false;
+}
+
}
//////////////////////////////////////////////////////////////////////
ignoreEmpty);
}
+template<bool exact,
+ class Delim,
+ class... StringPieces>
+bool split(const Delim& delimiter,
+ StringPiece input,
+ StringPiece& outHead,
+ StringPieces&... outTail) {
+ return detail::splitFixed<exact>(
+ detail::prepareDelim(delimiter),
+ input,
+ outHead,
+ outTail...);
+}
+
namespace detail {
template <class Iterator>
} // namespace folly
#endif /* FOLLY_STRING_INL_H_ */
-