Skip to content

Commit

Permalink
Revert "URI parsing in folly"
Browse files Browse the repository at this point in the history
Summary: Unbreak build.  third-party link not yet updated after https://phabricator.fb.com/D776457

Test Plan: no

Reviewed By: chip@fb.com

FB internal diff: D778669
  • Loading branch information
tudor authored and jdelong committed Apr 21, 2013
1 parent 9ff6903 commit cd884e6
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 742 deletions.
89 changes: 0 additions & 89 deletions folly/String-inl.h
Expand Up @@ -149,95 +149,6 @@ void cUnescape(StringPiece str, String& out, bool strict) {
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
extern const unsigned char uriEscapeTable[];
} // namespace detail

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();
auto last = p; // last regular character
// 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) {
out.append(&*last, p - last);
out.push_back('+');
++p;
last = p;
} else {
out.append(&*last, p - last);
esc[1] = hexValues[v >> 4];
esc[2] = hexValues[v & 0x0f];
out.append(esc, 3);
++p;
last = p;
}
}
out.append(&*last, p - last);
}

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;
unsigned char v = static_cast<unsigned char>(v);
switch (c) {
case '%':
{
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");
}
out.append(&*last, p - last);
out.push_back((h1 << 4) | h2);
p += 3;
last = p;
break;
}
case '+':
if (mode == UriEscapeMode::QUERY) {
out.append(&*last, p - last);
out.push_back(' ');
++p;
last = p;
break;
}
// else fallthrough
default:
++p;
break;
}
}
out.append(&*last, p - last);
}

namespace detail {

/*
Expand Down
50 changes: 0 additions & 50 deletions folly/String.h
Expand Up @@ -112,56 +112,6 @@ String cUnescape(StringPiece str, bool strict = true) {
return out;
}

/**
* URI-escape a string. Appends the result to the output string.
*
* Alphanumeric characters and other characters marked as "unreserved" in RFC
* 3986 ( -_.~ ) are left unchanged. In PATH mode, the forward slash (/) is
* also left unchanged. In QUERY mode, spaces are replaced by '+'. All other
* characters are percent-encoded.
*/
enum class UriEscapeMode : unsigned char {
// The values are meaningful, see generate_escape_tables.py
ALL = 0,
QUERY = 1,
PATH = 2
};
template <class String>
void uriEscape(StringPiece str,
String& out,
UriEscapeMode mode = UriEscapeMode::ALL);

/**
* Similar to uriEscape above, but returns the escaped string.
*/
template <class String>
String uriEscape(StringPiece str, UriEscapeMode mode = UriEscapeMode::ALL) {
String out;
uriEscape(str, out, mode);
return out;
}

/**
* URI-unescape a string. Appends the result to the output string.
*
* In QUERY mode, '+' are replaced by space. %XX sequences are decoded if
* XX is a valid hex sequence, otherwise we throw invalid_argument.
*/
template <class String>
void uriUnescape(StringPiece str,
String& out,
UriEscapeMode mode = UriEscapeMode::ALL);

/**
* Similar to uriUnescape above, but returns the unescaped string.
*/
template <class String>
String uriUnescape(StringPiece str, UriEscapeMode mode = UriEscapeMode::ALL) {
String out;
uriUnescape(str, out, mode);
return out;
}

/**
* stringPrintf is much like printf but deposits its result into a
* string. Two signatures are supported: the first simply returns the
Expand Down
49 changes: 0 additions & 49 deletions folly/Uri-inl.h

This file was deleted.

95 changes: 0 additions & 95 deletions folly/Uri.cpp

This file was deleted.

77 changes: 0 additions & 77 deletions folly/Uri.h

This file was deleted.

0 comments on commit cd884e6

Please sign in to comment.