#632: M1249352 part 2

This commit is contained in:
Cameron Kaiser 2020-11-22 18:44:30 -08:00
parent 438bdb7265
commit 2c61821e49
1 changed files with 27 additions and 21 deletions

View File

@ -6,6 +6,9 @@
#include "mozilla/RangedPtr.h"
#include <algorithm>
#include <iterator>
#include "nsURLHelper.h"
#include "nsIFile.h"
#include "nsIURLParser.h"
@ -612,33 +615,36 @@ net_IsAbsoluteURL(const nsACString& uri)
void
net_FilterURIString(const nsACString& input, nsACString& result)
{
const char kCharsToStrip[] = "\r\n\t";
result.Truncate();
auto start = input.BeginReading();
auto end = input.EndReading();
nsACString::const_iterator start, end;
input.BeginReading(start);
input.EndReading(end);
// 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();
// Strip C0 and space from begining
while (start != end) {
if ((uint8_t) *start > 0x20) {
break;
}
start++;
// 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;
}
MOZ_ASSERT(!*end, "input should null terminated");
// Strip C0 and space from end
while (end != start) {
end--;
if ((uint8_t) *end > 0x20) {
end++;
break;
}
result.Assign(Substring(newStart, newEnd));
if (needsStrip) {
result.StripChars(kCharsToStrip);
}
nsAutoCString temp(Substring(start, end));
temp.StripChars("\r\n\t");
result.Assign(temp);
}
#if defined(XP_WIN)