Bug 1249352 - Part 2: Modify net_FilterURIString behavior to avoid unnecessary copying. r=valentin

Previously we avoided copying the input string if modification was not
necessary. This remimplements that behavior.
This commit is contained in:
Eric Rahm 2016-06-27 11:10:44 -07:00
Родитель 161df1419b
Коммит acc0ac63c4
1 изменённых файлов: 28 добавлений и 21 удалений

Просмотреть файл

@ -6,6 +6,9 @@
#include "mozilla/RangedPtr.h"
#include <algorithm>
#include <iterator>
#include "nsURLHelper.h"
#include "nsIFile.h"
#include "nsIURLParser.h"
@ -572,33 +575,37 @@ net_IsAbsoluteURL(const nsACString& uri)
void
net_FilterURIString(const nsACString& input, nsACString& result)
{
const char kCharsToStrip[] = "\r\n\t";
result.Truncate();
nsACString::const_iterator start, end;
input.BeginReading(start);
input.EndReading(end);
auto start = input.BeginReading();
auto end = input.EndReading();
// Strip C0 and space from begining
while (start != end) {
if ((uint8_t) *start > 0x20) {
break;
}
start++;
// 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;
}
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);
}