2018-12-05 12:29:49 +03:00
|
|
|
/*
|
2019-05-29 14:32:35 +03:00
|
|
|
* Copyright 2012-present Facebook, Inc.
|
2018-12-05 12:29:49 +03:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iterator>
|
2019-05-29 14:32:35 +03:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include <folly/CppAttributes.h>
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
#ifndef FOLLY_STRING_H_
|
|
|
|
#error This file may only be included from String.h
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace folly {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
// Map from character code to value of one-character escape sequence
|
|
|
|
// ('\n' = 10 maps to 'n'), 'O' if the character should be printed as
|
|
|
|
// an octal escape sequence, or 'P' if the character is printable and
|
|
|
|
// should be printed as is.
|
2019-05-29 14:32:35 +03:00
|
|
|
extern const std::array<char, 256> cEscapeTable;
|
|
|
|
} // namespace detail
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
template <class String>
|
|
|
|
void cEscape(StringPiece str, String& out) {
|
|
|
|
char esc[4];
|
|
|
|
esc[0] = '\\';
|
|
|
|
out.reserve(out.size() + str.size());
|
|
|
|
auto p = str.begin();
|
2019-05-29 14:32:35 +03:00
|
|
|
auto last = p; // last regular character
|
2018-12-05 12:29:49 +03:00
|
|
|
// We advance over runs of regular characters (printable, not double-quote or
|
|
|
|
// backslash) and copy them in one go; this is faster than calling push_back
|
|
|
|
// repeatedly.
|
|
|
|
while (p != str.end()) {
|
|
|
|
char c = *p;
|
|
|
|
unsigned char v = static_cast<unsigned char>(c);
|
|
|
|
char e = detail::cEscapeTable[v];
|
2019-05-29 14:32:35 +03:00
|
|
|
if (e == 'P') { // printable
|
2018-12-05 12:29:49 +03:00
|
|
|
++p;
|
2019-05-29 14:32:35 +03:00
|
|
|
} else if (e == 'O') { // octal
|
|
|
|
out.append(&*last, size_t(p - last));
|
2018-12-05 12:29:49 +03:00
|
|
|
esc[1] = '0' + ((v >> 6) & 7);
|
|
|
|
esc[2] = '0' + ((v >> 3) & 7);
|
|
|
|
esc[3] = '0' + (v & 7);
|
|
|
|
out.append(esc, 4);
|
|
|
|
++p;
|
|
|
|
last = p;
|
2019-05-29 14:32:35 +03:00
|
|
|
} else { // special 1-character escape
|
|
|
|
out.append(&*last, size_t(p - last));
|
2018-12-05 12:29:49 +03:00
|
|
|
esc[1] = e;
|
|
|
|
out.append(esc, 2);
|
|
|
|
++p;
|
|
|
|
last = p;
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
out.append(&*last, size_t(p - last));
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
// Map from the character code of the character following a backslash to
|
|
|
|
// the unescaped character if a valid one-character escape sequence
|
|
|
|
// ('n' maps to 10 = '\n'), 'O' if this is the first character of an
|
|
|
|
// octal escape sequence, 'X' if this is the first character of a
|
|
|
|
// hexadecimal escape sequence, or 'I' if this escape sequence is invalid.
|
2019-05-29 14:32:35 +03:00
|
|
|
extern const std::array<char, 256> cUnescapeTable;
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
// Map from the character code to the hex value, or 16 if invalid hex char.
|
2019-05-29 14:32:35 +03:00
|
|
|
extern const std::array<unsigned char, 256> hexTable;
|
|
|
|
} // namespace detail
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
template <class String>
|
|
|
|
void cUnescape(StringPiece str, String& out, bool strict) {
|
|
|
|
out.reserve(out.size() + str.size());
|
|
|
|
auto p = str.begin();
|
2019-05-29 14:32:35 +03:00
|
|
|
auto last = p; // last regular character (not part of an escape sequence)
|
2018-12-05 12:29:49 +03:00
|
|
|
// We advance over runs of regular characters (not backslash) and copy them
|
|
|
|
// in one go; this is faster than calling push_back repeatedly.
|
|
|
|
while (p != str.end()) {
|
|
|
|
char c = *p;
|
2019-05-29 14:32:35 +03:00
|
|
|
if (c != '\\') { // normal case
|
2018-12-05 12:29:49 +03:00
|
|
|
++p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out.append(&*last, p - last);
|
2019-05-29 14:32:35 +03:00
|
|
|
++p;
|
|
|
|
if (p == str.end()) { // backslash at end of string
|
2018-12-05 12:29:49 +03:00
|
|
|
if (strict) {
|
|
|
|
throw std::invalid_argument("incomplete escape sequence");
|
|
|
|
}
|
|
|
|
out.push_back('\\');
|
|
|
|
last = p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char e = detail::cUnescapeTable[static_cast<unsigned char>(*p)];
|
2019-05-29 14:32:35 +03:00
|
|
|
if (e == 'O') { // octal
|
2018-12-05 12:29:49 +03:00
|
|
|
unsigned char val = 0;
|
|
|
|
for (int i = 0; i < 3 && p != str.end() && *p >= '0' && *p <= '7';
|
|
|
|
++i, ++p) {
|
2019-05-29 14:32:35 +03:00
|
|
|
val <<= 3;
|
|
|
|
val |= (*p - '0');
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
out.push_back(val);
|
|
|
|
last = p;
|
2019-05-29 14:32:35 +03:00
|
|
|
} else if (e == 'X') { // hex
|
2018-12-05 12:29:49 +03:00
|
|
|
++p;
|
2019-05-29 14:32:35 +03:00
|
|
|
if (p == str.end()) { // \x at end of string
|
2018-12-05 12:29:49 +03:00
|
|
|
if (strict) {
|
|
|
|
throw std::invalid_argument("incomplete hex escape sequence");
|
|
|
|
}
|
|
|
|
out.append("\\x");
|
|
|
|
last = p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
unsigned char val = 0;
|
|
|
|
unsigned char h;
|
|
|
|
for (; (p != str.end() &&
|
|
|
|
(h = detail::hexTable[static_cast<unsigned char>(*p)]) < 16);
|
|
|
|
++p) {
|
2019-05-29 14:32:35 +03:00
|
|
|
val <<= 4;
|
|
|
|
val |= h;
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
out.push_back(val);
|
|
|
|
last = p;
|
2019-05-29 14:32:35 +03:00
|
|
|
} else if (e == 'I') { // invalid
|
2018-12-05 12:29:49 +03:00
|
|
|
if (strict) {
|
|
|
|
throw std::invalid_argument("invalid escape sequence");
|
|
|
|
}
|
|
|
|
out.push_back('\\');
|
|
|
|
out.push_back(*p);
|
|
|
|
++p;
|
|
|
|
last = p;
|
2019-05-29 14:32:35 +03:00
|
|
|
} else { // standard escape sequence, \' etc
|
2018-12-05 12:29:49 +03:00
|
|
|
out.push_back(e);
|
|
|
|
++p;
|
|
|
|
last = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.append(&*last, p - last);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
// Map from character code to escape mode:
|
|
|
|
// 0 = pass through
|
|
|
|
// 1 = unused
|
|
|
|
// 2 = pass through in PATH mode
|
|
|
|
// 3 = space, replace with '+' in QUERY mode
|
|
|
|
// 4 = percent-encode
|
2019-05-29 14:32:35 +03:00
|
|
|
extern const std::array<unsigned char, 256> uriEscapeTable;
|
|
|
|
} // namespace detail
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
template <class String>
|
|
|
|
void uriEscape(StringPiece str, String& out, UriEscapeMode mode) {
|
|
|
|
static const char hexValues[] = "0123456789abcdef";
|
|
|
|
char esc[3];
|
|
|
|
esc[0] = '%';
|
|
|
|
// Preallocate assuming that 25% of the input string will be escaped
|
|
|
|
out.reserve(out.size() + str.size() + 3 * (str.size() / 4));
|
|
|
|
auto p = str.begin();
|
2019-05-29 14:32:35 +03:00
|
|
|
auto last = p; // last regular character
|
2018-12-05 12:29:49 +03:00
|
|
|
// We advance over runs of passthrough characters and copy them in one go;
|
|
|
|
// this is faster than calling push_back repeatedly.
|
|
|
|
unsigned char minEncode = static_cast<unsigned char>(mode);
|
|
|
|
while (p != str.end()) {
|
|
|
|
char c = *p;
|
|
|
|
unsigned char v = static_cast<unsigned char>(c);
|
|
|
|
unsigned char discriminator = detail::uriEscapeTable[v];
|
|
|
|
if (LIKELY(discriminator <= minEncode)) {
|
|
|
|
++p;
|
|
|
|
} else if (mode == UriEscapeMode::QUERY && discriminator == 3) {
|
2019-05-29 14:32:35 +03:00
|
|
|
out.append(&*last, size_t(p - last));
|
2018-12-05 12:29:49 +03:00
|
|
|
out.push_back('+');
|
|
|
|
++p;
|
|
|
|
last = p;
|
|
|
|
} else {
|
2019-05-29 14:32:35 +03:00
|
|
|
out.append(&*last, size_t(p - last));
|
2018-12-05 12:29:49 +03:00
|
|
|
esc[1] = hexValues[v >> 4];
|
|
|
|
esc[2] = hexValues[v & 0x0f];
|
|
|
|
out.append(esc, 3);
|
|
|
|
++p;
|
|
|
|
last = p;
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
out.append(&*last, size_t(p - last));
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class String>
|
|
|
|
void uriUnescape(StringPiece str, String& out, UriEscapeMode mode) {
|
|
|
|
out.reserve(out.size() + str.size());
|
|
|
|
auto p = str.begin();
|
|
|
|
auto last = p;
|
|
|
|
// We advance over runs of passthrough characters and copy them in one go;
|
|
|
|
// this is faster than calling push_back repeatedly.
|
|
|
|
while (p != str.end()) {
|
|
|
|
char c = *p;
|
|
|
|
switch (c) {
|
2019-05-29 14:32:35 +03:00
|
|
|
case '%': {
|
2018-12-05 12:29:49 +03:00
|
|
|
if (UNLIKELY(std::distance(p, str.end()) < 3)) {
|
|
|
|
throw std::invalid_argument("incomplete percent encode sequence");
|
|
|
|
}
|
|
|
|
auto h1 = detail::hexTable[static_cast<unsigned char>(p[1])];
|
|
|
|
auto h2 = detail::hexTable[static_cast<unsigned char>(p[2])];
|
|
|
|
if (UNLIKELY(h1 == 16 || h2 == 16)) {
|
|
|
|
throw std::invalid_argument("invalid percent encode sequence");
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
out.append(&*last, size_t(p - last));
|
2018-12-05 12:29:49 +03:00
|
|
|
out.push_back((h1 << 4) | h2);
|
|
|
|
p += 3;
|
|
|
|
last = p;
|
|
|
|
break;
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
case '+':
|
|
|
|
if (mode == UriEscapeMode::QUERY) {
|
|
|
|
out.append(&*last, size_t(p - last));
|
|
|
|
out.push_back(' ');
|
|
|
|
++p;
|
|
|
|
last = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// else fallthrough
|
|
|
|
FOLLY_FALLTHROUGH;
|
|
|
|
default:
|
2018-12-05 12:29:49 +03:00
|
|
|
++p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
out.append(&*last, size_t(p - last));
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following functions are type-overloaded helpers for
|
|
|
|
* internalSplit().
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
inline size_t delimSize(char) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
inline size_t delimSize(StringPiece s) {
|
|
|
|
return s.size();
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
inline bool atDelim(const char* s, char c) {
|
2019-05-29 14:32:35 +03:00
|
|
|
return *s == c;
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
inline bool atDelim(const char* s, StringPiece sp) {
|
|
|
|
return !std::memcmp(s, sp.start(), sp.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are used to short-circuit internalSplit() in the case of
|
|
|
|
// 1-character strings.
|
|
|
|
inline char delimFront(char c) {
|
|
|
|
// This one exists only for compile-time; it should never be called.
|
|
|
|
std::abort();
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
inline char delimFront(StringPiece s) {
|
|
|
|
assert(!s.empty() && s.start() != nullptr);
|
|
|
|
return *s.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shared implementation for all the split() overloads.
|
|
|
|
*
|
|
|
|
* This uses some external helpers that are overloaded to let this
|
|
|
|
* algorithm be more performant if the deliminator is a single
|
|
|
|
* character instead of a whole string.
|
|
|
|
*
|
|
|
|
* @param ignoreEmpty iff true, don't copy empty segments to output
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
template <class OutStringT, class DelimT, class OutputIterator>
|
|
|
|
void internalSplit(
|
|
|
|
DelimT delim,
|
|
|
|
StringPiece sp,
|
|
|
|
OutputIterator out,
|
2018-12-05 12:29:49 +03:00
|
|
|
bool ignoreEmpty) {
|
|
|
|
assert(sp.empty() || sp.start() != nullptr);
|
|
|
|
|
|
|
|
const char* s = sp.start();
|
|
|
|
const size_t strSize = sp.size();
|
|
|
|
const size_t dSize = delimSize(delim);
|
|
|
|
|
|
|
|
if (dSize > strSize || dSize == 0) {
|
|
|
|
if (!ignoreEmpty || strSize > 0) {
|
|
|
|
*out++ = to<OutStringT>(sp);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
if (std::is_same<DelimT, StringPiece>::value && dSize == 1) {
|
2018-12-05 12:29:49 +03:00
|
|
|
// Call the char version because it is significantly faster.
|
2019-05-29 14:32:35 +03:00
|
|
|
return internalSplit<OutStringT>(delimFront(delim), sp, out, ignoreEmpty);
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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++ = to<OutStringT>(sp.subpiece(tokenStartPos, tokenSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenStartPos = i + dSize;
|
|
|
|
tokenSize = 0;
|
|
|
|
i += dSize - 1;
|
|
|
|
} else {
|
|
|
|
++tokenSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tokenSize = strSize - tokenStartPos;
|
|
|
|
if (!ignoreEmpty || tokenSize > 0) {
|
|
|
|
*out++ = to<OutStringT>(sp.subpiece(tokenStartPos, tokenSize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
template <class String>
|
|
|
|
StringPiece prepareDelim(const String& s) {
|
2018-12-05 12:29:49 +03:00
|
|
|
return StringPiece(s);
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
inline char prepareDelim(char c) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class OutputType>
|
|
|
|
void toOrIgnore(StringPiece input, OutputType& output) {
|
|
|
|
output = folly::to<OutputType>(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void toOrIgnore(StringPiece, decltype(std::ignore)&) {}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
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 ||
|
2019-05-29 14:32:35 +03:00
|
|
|
IsSomeString<OutputType>::value ||
|
|
|
|
std::is_same<OutputType, decltype(std::ignore)>::value,
|
|
|
|
"split<false>() requires that the last argument be a string type "
|
|
|
|
"or std::ignore");
|
2018-12-05 12:29:49 +03:00
|
|
|
if (exact && UNLIKELY(std::string::npos != input.find(delimiter))) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-29 14:32:35 +03:00
|
|
|
toOrIgnore(input, output);
|
2018-12-05 12:29:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 head(input.begin(), input.begin() + cut);
|
2019-05-29 14:32:35 +03:00
|
|
|
StringPiece tail(
|
|
|
|
input.begin() + cut + detail::delimSize(delimiter), input.end());
|
2018-12-05 12:29:49 +03:00
|
|
|
if (LIKELY(splitFixed<exact>(delimiter, tail, outTail...))) {
|
2019-05-29 14:32:35 +03:00
|
|
|
toOrIgnore(head, outHead);
|
2018-12-05 12:29:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
} // namespace detail
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
template <class Delim, class String, class OutputType>
|
|
|
|
void split(
|
|
|
|
const Delim& delimiter,
|
|
|
|
const String& input,
|
|
|
|
std::vector<OutputType>& out,
|
|
|
|
bool ignoreEmpty) {
|
2018-12-05 12:29:49 +03:00
|
|
|
detail::internalSplit<OutputType>(
|
2019-05-29 14:32:35 +03:00
|
|
|
detail::prepareDelim(delimiter),
|
|
|
|
StringPiece(input),
|
|
|
|
std::back_inserter(out),
|
|
|
|
ignoreEmpty);
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
template <class Delim, class String, class OutputType>
|
|
|
|
void split(
|
|
|
|
const Delim& delimiter,
|
|
|
|
const String& input,
|
|
|
|
fbvector<OutputType>& out,
|
|
|
|
bool ignoreEmpty) {
|
2018-12-05 12:29:49 +03:00
|
|
|
detail::internalSplit<OutputType>(
|
2019-05-29 14:32:35 +03:00
|
|
|
detail::prepareDelim(delimiter),
|
|
|
|
StringPiece(input),
|
|
|
|
std::back_inserter(out),
|
|
|
|
ignoreEmpty);
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
template <
|
|
|
|
class OutputValueType,
|
|
|
|
class Delim,
|
|
|
|
class String,
|
|
|
|
class OutputIterator>
|
|
|
|
void splitTo(
|
|
|
|
const Delim& delimiter,
|
|
|
|
const String& input,
|
|
|
|
OutputIterator out,
|
|
|
|
bool ignoreEmpty) {
|
2018-12-05 12:29:49 +03:00
|
|
|
detail::internalSplit<OutputValueType>(
|
2019-05-29 14:32:35 +03:00
|
|
|
detail::prepareDelim(delimiter), StringPiece(input), out, ignoreEmpty);
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool exact, class Delim, class... OutputTypes>
|
|
|
|
typename std::enable_if<
|
2019-05-29 14:32:35 +03:00
|
|
|
StrictConjunction<IsConvertible<OutputTypes>...>::value &&
|
|
|
|
sizeof...(OutputTypes) >= 1,
|
2018-12-05 12:29:49 +03:00
|
|
|
bool>::type
|
|
|
|
split(const Delim& delimiter, StringPiece input, OutputTypes&... outputs) {
|
|
|
|
return detail::splitFixed<exact>(
|
|
|
|
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()).
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
template <class T>
|
|
|
|
struct IsSizableString {
|
|
|
|
enum {
|
|
|
|
value = IsSomeString<T>::value || std::is_same<T, StringPiece>::value
|
|
|
|
};
|
2018-12-05 12:29:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Iterator>
|
2019-05-29 14:32:35 +03:00
|
|
|
struct IsSizableStringContainerIterator
|
|
|
|
: IsSizableString<typename std::iterator_traits<Iterator>::value_type> {};
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
template <class Delim, class Iterator, class String>
|
2019-05-29 14:32:35 +03:00
|
|
|
void internalJoinAppend(
|
|
|
|
Delim delimiter,
|
|
|
|
Iterator begin,
|
|
|
|
Iterator end,
|
|
|
|
String& output) {
|
2018-12-05 12:29:49 +03:00
|
|
|
assert(begin != end);
|
2019-05-29 14:32:35 +03:00
|
|
|
if (std::is_same<Delim, StringPiece>::value && delimSize(delimiter) == 1) {
|
2018-12-05 12:29:49 +03:00
|
|
|
internalJoinAppend(delimFront(delimiter), begin, end, output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
toAppend(*begin, &output);
|
|
|
|
while (++begin != end) {
|
|
|
|
toAppend(delimiter, *begin, &output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Delim, class Iterator, class String>
|
|
|
|
typename std::enable_if<IsSizableStringContainerIterator<Iterator>::value>::type
|
2019-05-29 14:32:35 +03:00
|
|
|
internalJoin(Delim delimiter, Iterator begin, Iterator end, String& output) {
|
2018-12-05 12:29:49 +03:00
|
|
|
output.clear();
|
|
|
|
if (begin == end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const size_t dsize = delimSize(delimiter);
|
|
|
|
Iterator it = begin;
|
|
|
|
size_t size = it->size();
|
|
|
|
while (++it != end) {
|
|
|
|
size += dsize + it->size();
|
|
|
|
}
|
|
|
|
output.reserve(size);
|
|
|
|
internalJoinAppend(delimiter, begin, end, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Delim, class Iterator, class String>
|
2019-05-29 14:32:35 +03:00
|
|
|
typename std::enable_if<
|
|
|
|
!IsSizableStringContainerIterator<Iterator>::value>::type
|
|
|
|
internalJoin(Delim delimiter, Iterator begin, Iterator end, String& output) {
|
2018-12-05 12:29:49 +03:00
|
|
|
output.clear();
|
|
|
|
if (begin == end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
internalJoinAppend(delimiter, begin, end, output);
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
} // namespace detail
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
template <class Delim, class Iterator, class String>
|
2019-05-29 14:32:35 +03:00
|
|
|
void join(
|
|
|
|
const Delim& delimiter,
|
|
|
|
Iterator begin,
|
|
|
|
Iterator end,
|
|
|
|
String& output) {
|
|
|
|
detail::internalJoin(detail::prepareDelim(delimiter), begin, end, output);
|
2018-12-05 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
template <class OutputString>
|
|
|
|
void backslashify(
|
|
|
|
folly::StringPiece input,
|
|
|
|
OutputString& output,
|
|
|
|
bool hex_style) {
|
2018-12-05 12:29:49 +03:00
|
|
|
static const char hexValues[] = "0123456789abcdef";
|
|
|
|
output.clear();
|
|
|
|
output.reserve(3 * input.size());
|
|
|
|
for (unsigned char c : input) {
|
|
|
|
// less than space or greater than '~' are considered unprintable
|
|
|
|
if (c < 0x20 || c > 0x7e || c == '\\') {
|
|
|
|
bool hex_append = false;
|
|
|
|
output.push_back('\\');
|
|
|
|
if (hex_style) {
|
|
|
|
hex_append = true;
|
|
|
|
} else {
|
2019-05-29 14:32:35 +03:00
|
|
|
if (c == '\r') {
|
|
|
|
output += 'r';
|
|
|
|
} else if (c == '\n') {
|
|
|
|
output += 'n';
|
|
|
|
} else if (c == '\t') {
|
|
|
|
output += 't';
|
|
|
|
} else if (c == '\a') {
|
|
|
|
output += 'a';
|
|
|
|
} else if (c == '\b') {
|
|
|
|
output += 'b';
|
|
|
|
} else if (c == '\0') {
|
|
|
|
output += '0';
|
|
|
|
} else if (c == '\\') {
|
|
|
|
output += '\\';
|
|
|
|
} else {
|
2018-12-05 12:29:49 +03:00
|
|
|
hex_append = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hex_append) {
|
|
|
|
output.push_back('x');
|
|
|
|
output.push_back(hexValues[(c >> 4) & 0xf]);
|
|
|
|
output.push_back(hexValues[c & 0xf]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class String1, class String2>
|
|
|
|
void humanify(const String1& input, String2& output) {
|
|
|
|
size_t numUnprintable = 0;
|
|
|
|
size_t numPrintablePrefix = 0;
|
|
|
|
for (unsigned char c : input) {
|
|
|
|
if (c < 0x20 || c > 0x7e || c == '\\') {
|
|
|
|
++numUnprintable;
|
|
|
|
}
|
|
|
|
if (numUnprintable == 0) {
|
|
|
|
++numPrintablePrefix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// hexlify doubles a string's size; backslashify can potentially
|
|
|
|
// explode it by 4x. Now, the printable range of the ascii
|
|
|
|
// "spectrum" is around 95 out of 256 values, so a "random" binary
|
|
|
|
// string should be around 60% unprintable. We use a 50% hueristic
|
|
|
|
// here, so if a string is 60% unprintable, then we just use hex
|
|
|
|
// output. Otherwise we backslash.
|
|
|
|
//
|
|
|
|
// UTF8 is completely ignored; as a result, utf8 characters will
|
|
|
|
// likely be \x escaped (since most common glyphs fit in two bytes).
|
|
|
|
// This is a tradeoff of complexity/speed instead of a convenience
|
|
|
|
// that likely would rarely matter. Moreover, this function is more
|
|
|
|
// about displaying underlying bytes, not about displaying glyphs
|
|
|
|
// from languages.
|
|
|
|
if (numUnprintable == 0) {
|
|
|
|
output = input;
|
|
|
|
} else if (5 * numUnprintable >= 3 * input.size()) {
|
|
|
|
// However! If we have a "meaningful" prefix of printable
|
|
|
|
// characters, say 20% of the string, we backslashify under the
|
|
|
|
// assumption viewing the prefix as ascii is worth blowing the
|
|
|
|
// output size up a bit.
|
|
|
|
if (5 * numPrintablePrefix >= input.size()) {
|
|
|
|
backslashify(input, output);
|
|
|
|
} else {
|
|
|
|
output = "0x";
|
|
|
|
hexlify(input, output, true /* append output */);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
backslashify(input, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
template <class InputString, class OutputString>
|
|
|
|
bool hexlify(
|
|
|
|
const InputString& input,
|
|
|
|
OutputString& output,
|
|
|
|
bool append_output) {
|
|
|
|
if (!append_output) {
|
|
|
|
output.clear();
|
|
|
|
}
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
static char hexValues[] = "0123456789abcdef";
|
|
|
|
auto j = output.size();
|
|
|
|
output.resize(2 * input.size() + output.size());
|
|
|
|
for (size_t i = 0; i < input.size(); ++i) {
|
|
|
|
int ch = input[i];
|
|
|
|
output[j++] = hexValues[(ch >> 4) & 0xf];
|
|
|
|
output[j++] = hexValues[ch & 0xf];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
template <class InputString, class OutputString>
|
2018-12-05 12:29:49 +03:00
|
|
|
bool unhexlify(const InputString& input, OutputString& output) {
|
|
|
|
if (input.size() % 2 != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
output.resize(input.size() / 2);
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < input.size(); i += 2) {
|
2019-05-29 14:32:35 +03:00
|
|
|
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
|
2018-12-05 12:29:49 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
output[j++] = (highBits << 4) + lowBits;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
/**
|
|
|
|
* Hex-dump at most 16 bytes starting at offset from a memory area of size
|
|
|
|
* bytes. Return the number of bytes actually dumped.
|
|
|
|
*/
|
2019-05-29 14:32:35 +03:00
|
|
|
size_t
|
|
|
|
hexDumpLine(const void* ptr, size_t offset, size_t size, std::string& line);
|
|
|
|
} // namespace detail
|
2018-12-05 12:29:49 +03:00
|
|
|
|
|
|
|
template <class OutIt>
|
|
|
|
void hexDump(const void* ptr, size_t size, OutIt out) {
|
|
|
|
size_t offset = 0;
|
|
|
|
std::string line;
|
|
|
|
while (offset < size) {
|
|
|
|
offset += detail::hexDumpLine(ptr, offset, size, line);
|
|
|
|
*out++ = line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 14:32:35 +03:00
|
|
|
} // namespace folly
|