void net_FilterURIString(const nsACString& input, nsACString& result) { const char kCharsToStrip[] = "\r\n\t"; result.Truncate(); auto start = input.BeginReading(); auto end = input.EndReading(); // Trim off leading and trailing invalid chars. auto charFilter = [](char c) { return static_cast<uint8_t>(c) > 0x20; }; auto newStart = std::find_if(start, end, charFilter); auto newEnd = std::find_if( std::reverse_iterator<decltype(end)>(end), std::reverse_iterator<decltype(newStart)>(newStart), charFilter).base(); // Check if chars need to be stripped. auto itr = std::find_first_of( newStart, newEnd, std::begin(kCharsToStrip), std::end(kCharsToStrip)); const bool needsStrip = itr != newEnd; // Just use the passed in string rather than creating new copies if no // changes are necessary. if (newStart == start && newEnd == end && !needsStrip) { result = input; return; } result.Assign(Substring(newStart, newEnd)); if (needsStrip) { result.StripChars(kCharsToStrip); } }
/* Extract URI-Scheme if possible */ nsresult net_ExtractURLScheme(const nsACString &inURI, nsACString& scheme) { nsACString::const_iterator start, end; inURI.BeginReading(start); inURI.EndReading(end); // Strip C0 and space from begining while (start != end) { if ((uint8_t) *start > 0x20) { break; } start++; } Tokenizer p(Substring(start, end), "\r\n\t"); p.Record(); if (!p.CheckChar(isAsciiAlpha)) { // First char must be alpha return NS_ERROR_MALFORMED_URI; } while (p.CheckChar(net_IsValidSchemeChar) || p.CheckWhite()) { // Skip valid scheme characters or \r\n\t } if (!p.CheckChar(':')) { return NS_ERROR_MALFORMED_URI; } p.Claim(scheme); scheme.StripChars("\r\n\t"); return NS_OK; }