This source file includes following definitions.
- IsAllASCII
- AppendRaw8BitQueryString
- RunConverter
- RunConverter
- DoConvertToQueryEncoding
- DoCanonicalizeQuery
- CanonicalizeQuery
- CanonicalizeQuery
- ConvertUTF16ToQueryEncoding
#include "url/url_canon.h"
#include "url/url_canon_internal.h"
namespace url_canon {
namespace {
template<typename CHAR, typename UCHAR>
bool IsAllASCII(const CHAR* spec, const url_parse::Component& query) {
int end = query.end();
for (int i = query.begin; i < end; i++) {
if (static_cast<UCHAR>(spec[i]) >= 0x80)
return false;
}
return true;
}
template<typename CHAR>
void AppendRaw8BitQueryString(const CHAR* source, int length,
CanonOutput* output) {
for (int i = 0; i < length; i++) {
if (!IsQueryChar(static_cast<unsigned char>(source[i])))
AppendEscapedChar(static_cast<unsigned char>(source[i]), output);
else
output->push_back(static_cast<char>(source[i]));
}
}
void RunConverter(const char* spec,
const url_parse::Component& query,
CharsetConverter* converter,
CanonOutput* output) {
RawCanonOutputW<1024> utf16;
ConvertUTF8ToUTF16(&spec[query.begin], query.len, &utf16);
converter->ConvertFromUTF16(utf16.data(), utf16.length(), output);
}
void RunConverter(const base::char16* spec,
const url_parse::Component& query,
CharsetConverter* converter,
CanonOutput* output) {
converter->ConvertFromUTF16(&spec[query.begin], query.len, output);
}
template<typename CHAR, typename UCHAR>
void DoConvertToQueryEncoding(const CHAR* spec,
const url_parse::Component& query,
CharsetConverter* converter,
CanonOutput* output) {
if (IsAllASCII<CHAR, UCHAR>(spec, query)) {
AppendRaw8BitQueryString(&spec[query.begin], query.len, output);
} else {
if (converter) {
RawCanonOutput<1024> eight_bit;
RunConverter(spec, query, converter, &eight_bit);
AppendRaw8BitQueryString(eight_bit.data(), eight_bit.length(), output);
} else {
AppendStringOfType(&spec[query.begin], query.len, CHAR_QUERY, output);
}
}
}
template<typename CHAR, typename UCHAR>
void DoCanonicalizeQuery(const CHAR* spec,
const url_parse::Component& query,
CharsetConverter* converter,
CanonOutput* output,
url_parse::Component* out_query) {
if (query.len < 0) {
*out_query = url_parse::Component();
return;
}
output->push_back('?');
out_query->begin = output->length();
DoConvertToQueryEncoding<CHAR, UCHAR>(spec, query, converter, output);
out_query->len = output->length() - out_query->begin;
}
}
void CanonicalizeQuery(const char* spec,
const url_parse::Component& query,
CharsetConverter* converter,
CanonOutput* output,
url_parse::Component* out_query) {
DoCanonicalizeQuery<char, unsigned char>(spec, query, converter,
output, out_query);
}
void CanonicalizeQuery(const base::char16* spec,
const url_parse::Component& query,
CharsetConverter* converter,
CanonOutput* output,
url_parse::Component* out_query) {
DoCanonicalizeQuery<base::char16, base::char16>(spec, query, converter,
output, out_query);
}
void ConvertUTF16ToQueryEncoding(const base::char16* input,
const url_parse::Component& query,
CharsetConverter* converter,
CanonOutput* output) {
DoConvertToQueryEncoding<base::char16, base::char16>(input, query,
converter, output);
}
}