/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#ifndef FOLLY_STRING_INL_H_
-#define FOLLY_STRING_INL_H_
+#pragma once
#include <stdexcept>
#include <iterator>
-#ifndef FOLLY_BASE_STRING_H_
+#ifndef FOLLY_STRING_H_
#error This file may only be included from String.h
#endif
// this is faster than calling push_back repeatedly.
while (p != str.end()) {
char c = *p;
- unsigned char v = static_cast<unsigned char>(v);
switch (c) {
case '%':
{
return *s.start();
}
-/*
- * These output conversion templates allow us to support multiple
- * output string types, even when we are using an arbitrary
- * OutputIterator.
- */
-template<class OutStringT> struct OutputConverter {};
-
-template<> struct OutputConverter<std::string> {
- std::string operator()(StringPiece sp) const {
- return sp.toString();
- }
-};
-
-template<> struct OutputConverter<fbstring> {
- fbstring operator()(StringPiece sp) const {
- return sp.toFbstring();
- }
-};
-
-template<> struct OutputConverter<StringPiece> {
- StringPiece operator()(StringPiece sp) const { return sp; }
-};
-
/*
* Shared implementation for all the split() overloads.
*
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();
const size_t dSize = delimSize(delim);
- OutputConverter<OutStringT> conv;
-
if (dSize > strSize || dSize == 0) {
if (!ignoreEmpty || strSize > 0) {
- *out++ = conv(sp);
+ *out++ = to<OutStringT>(sp);
}
return;
}
ignoreEmpty);
}
- int tokenStartPos = 0;
- int tokenSize = 0;
- for (int i = 0; i <= strSize - dSize; ++i) {
+ size_t tokenStartPos = 0;
+ size_t tokenSize = 0;
+ for (size_t i = 0; i <= strSize - dSize; ++i) {
if (atDelim(&s[i], delim)) {
if (!ignoreEmpty || tokenSize > 0) {
- *out++ = conv(StringPiece(&s[tokenStartPos], tokenSize));
+ *out++ = to<OutStringT>(sp.subpiece(tokenStartPos, tokenSize));
}
tokenStartPos = i + dSize;
++tokenSize;
}
}
-
+ tokenSize = strSize - tokenStartPos;
if (!ignoreEmpty || tokenSize > 0) {
- tokenSize = strSize - tokenStartPos;
- *out++ = conv(StringPiece(&s[tokenStartPos], tokenSize));
+ *out++ = to<OutStringT>(sp.subpiece(tokenStartPos, tokenSize));
}
}
}
inline char prepareDelim(char c) { return c; }
-template<bool exact,
- class Delim>
-bool splitFixed(const Delim& delimiter,
- StringPiece input,
- StringPiece& out) {
+template <bool exact, class Delim, class OutputType>
+bool splitFixed(const Delim& delimiter, StringPiece input, OutputType& output) {
+ static_assert(
+ exact || std::is_same<OutputType, StringPiece>::value ||
+ IsSomeString<OutputType>::value,
+ "split<false>() requires that the last argument be a string type");
if (exact && UNLIKELY(std::string::npos != input.find(delimiter))) {
return false;
}
- out = input;
+ output = folly::to<OutputType>(input);
return true;
}
-template<bool exact,
- class Delim,
- class... StringPieces>
-bool splitFixed(const Delim& delimiter,
- StringPiece input,
- StringPiece& outHead,
- StringPieces&... outTail) {
+template <bool exact, class Delim, class OutputType, class... OutputTypes>
+bool splitFixed(
+ const Delim& delimiter,
+ StringPiece input,
+ OutputType& outHead,
+ OutputTypes&... outTail) {
size_t cut = input.find(delimiter);
if (UNLIKELY(cut == std::string::npos)) {
return false;
StringPiece tail(input.begin() + cut + detail::delimSize(delimiter),
input.end());
if (LIKELY(splitFixed<exact>(delimiter, tail, outTail...))) {
- outHead = head;
+ outHead = folly::to<OutputType>(head);
return true;
}
return false;
ignoreEmpty);
}
-template<bool exact,
- class Delim,
- class... StringPieces>
-bool split(const Delim& delimiter,
- StringPiece input,
- StringPiece& outHead,
- StringPieces&... outTail) {
+template <bool exact, class Delim, class... OutputTypes>
+typename std::enable_if<
+ AllConvertible<OutputTypes...>::value && sizeof...(OutputTypes) >= 1,
+ bool>::type
+split(const Delim& delimiter, StringPiece input, OutputTypes&... outputs) {
return detail::splitFixed<exact>(
- detail::prepareDelim(delimiter),
- input,
- outHead,
- outTail...);
+ detail::prepareDelim(delimiter), input, outputs...);
}
namespace detail {
+/*
+ * If a type can have its string size determined cheaply, we can more
+ * efficiently append it in a loop (see internalJoinAppend). Note that the
+ * struct need not conform to the std::string api completely (ex. does not need
+ * to implement append()).
+ */
+template <class T> struct IsSizableString {
+ enum { value = IsSomeString<T>::value
+ || std::is_same<T, StringPiece>::value };
+};
+
template <class Iterator>
-struct IsStringContainerIterator :
- IsSomeString<typename std::iterator_traits<Iterator>::value_type> {
+struct IsSizableStringContainerIterator :
+ IsSizableString<typename std::iterator_traits<Iterator>::value_type> {
};
template <class Delim, class Iterator, class String>
}
template <class Delim, class Iterator, class String>
-typename std::enable_if<IsStringContainerIterator<Iterator>::value>::type
+typename std::enable_if<IsSizableStringContainerIterator<Iterator>::value>::type
internalJoin(Delim delimiter,
Iterator begin,
Iterator end,
}
template <class Delim, class Iterator, class String>
-typename std::enable_if<!IsStringContainerIterator<Iterator>::value>::type
+typename
+std::enable_if<!IsSizableStringContainerIterator<Iterator>::value>::type
internalJoin(Delim delimiter,
Iterator begin,
Iterator end,
template <class String1, class String2>
void humanify(const String1& input, String2& output) {
- int numUnprintable = 0;
- int numPrintablePrefix = 0;
+ size_t numUnprintable = 0;
+ size_t numPrintablePrefix = 0;
for (unsigned char c : input) {
if (c < 0x20 || c > 0x7e || c == '\\') {
++numUnprintable;
if (!append_output) output.clear();
static char hexValues[] = "0123456789abcdef";
- int j = output.size();
+ auto j = output.size();
output.resize(2 * input.size() + output.size());
- for (int i = 0; i < input.size(); ++i) {
+ for (size_t i = 0; i < input.size(); ++i) {
int ch = input[i];
output[j++] = hexValues[(ch >> 4) & 0xf];
output[j++] = hexValues[ch & 0xf];
}
output.resize(input.size() / 2);
int j = 0;
- auto unhex = [](char c) -> int {
- return c >= '0' && c <= '9' ? c - '0' :
- c >= 'A' && c <= 'F' ? c - 'A' + 10 :
- c >= 'a' && c <= 'f' ? c - 'a' + 10 :
- -1;
- };
-
- for (int i = 0; i < input.size(); i += 2) {
- int highBits = unhex(input[i]);
- int lowBits = unhex(input[i + 1]);
- if (highBits < 0 || lowBits < 0) {
+
+ for (size_t i = 0; i < input.size(); i += 2) {
+ int highBits = detail::hexTable[static_cast<uint8_t>(input[i])];
+ int lowBits = detail::hexTable[static_cast<uint8_t>(input[i + 1])];
+ if ((highBits | lowBits) & 0x10) {
+ // One of the characters wasn't a hex digit
return false;
}
output[j++] = (highBits << 4) + lowBits;
}
} // namespace folly
-
-#endif /* FOLLY_STRING_INL_H_ */
-