int main () { test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); test ( "ABCDE"); test ( "a" ); test ( "" ); test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); test ( L"ABCDE" ); test ( L"a" ); test ( L"" ); #if __cplusplus >= 201103L test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); test ( u"ABCDE" ); test ( u"a" ); test ( u"" ); test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); test ( U"ABCDE" ); test ( U"a" ); test ( U"" ); #endif #if _LIBCPP_STD_VER > 11 { constexpr std::experimental::string_view sv1 { "ABCDE", 5 }; { constexpr std::experimental::string_view sv2 = sv1.substr ( 0, 3 ); static_assert ( sv2.size() == 3, "" ); static_assert ( sv2[0] == 'A', "" ); static_assert ( sv2[1] == 'B', "" ); static_assert ( sv2[2] == 'C', "" ); } { constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 0 ); static_assert ( sv2.size() == 0, "" ); } { constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 3 ); static_assert ( sv2.size() == 2, "" ); static_assert ( sv2[0] == 'D', "" ); static_assert ( sv2[1] == 'E', "" ); } } #endif }
int main () { test ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); test ( "ABCDE"); test ( "a" ); test ( "" ); test ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); test ( L"ABCDE" ); test ( L"a" ); test ( L"" ); test ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); test ( u"ABCDE" ); test ( u"a" ); test ( u"" ); test ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE" ); test ( U"ABCDE" ); test ( U"a" ); test ( U"" ); { constexpr std::experimental::string_view sv1 { "ABCDE", 5 }; { constexpr std::experimental::string_view sv2 = sv1.substr ( 0, 3 ); static_assert ( sv2.size() == 3, "" ); static_assert ( sv2[0] == 'A', "" ); static_assert ( sv2[1] == 'B', "" ); static_assert ( sv2[2] == 'C', "" ); } { constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 0 ); static_assert ( sv2.size() == 0, "" ); } { constexpr std::experimental::string_view sv2 = sv1.substr ( 3, 3 ); static_assert ( sv2.size() == 2, "" ); static_assert ( sv2[0] == 'D', "" ); static_assert ( sv2[1] == 'E', "" ); } } }
static OutputContainer splitted(const std::experimental::string_view &collection, const Element &element) { OutputContainer output; if(!collection.empty()) { unsigned int start = 0, i = 0; for(; i != collection.size();) { if(collection[i] == element) { output.push_back(collection.substr(start, i-start)); ++i; start = i; } else ++i; } output.push_back(collection.substr(start, i)); } return output; }
std::string uri::encode(const std::experimental::string_view input) { static const std::array<char,16> hex = {{ '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' }}; std::string res; res.reserve(input.size()); for (const auto chr : input) if (is_unreserved(chr)) { res += chr; } else { res += '%'; res += hex[ chr >> 4 ]; res += hex[ chr & 0xf ]; } return res; }
std::string uri::decode(const std::experimental::string_view input) { std::string res; res.reserve(input.size()); for (auto it = input.cbegin(), e = input.cend(); it not_eq e; ++it) { if (*it == '%') { if (++it >= e) return decode_error(std::move(res)); const uint8_t nibble1 = (*it); if (++it >= e) return decode_error(std::move(res)); const uint8_t nibble2 = (*it); res += static_cast<char>(from_hex(nibble1, nibble2)); } else { if (is_reserved(*it) or is_unreserved(*it)) res += *it; else return decode_error(std::move(res)); } } return res; }