| Index: base/strings/utf_offset_string_conversions.cc
|
| diff --git a/base/strings/utf_offset_string_conversions.cc b/base/strings/utf_offset_string_conversions.cc
|
| index 79c00f03834ea80e58216787eee50dbfa47508b9..5cdbaf9de0b4b11097d8afb57543b23df36d5be5 100644
|
| --- a/base/strings/utf_offset_string_conversions.cc
|
| +++ b/base/strings/utf_offset_string_conversions.cc
|
| @@ -15,6 +15,23 @@
|
|
|
| namespace base {
|
|
|
| +namespace {
|
| +
|
| +// Limiting function callable by std::for_each which will replace any value
|
| +// which is greater than |limit| with npos.
|
| +struct LimitOffset {
|
| + explicit LimitOffset(size_t limit) : limit_(limit) {}
|
| +
|
| + void operator()(size_t& offset) {
|
| + if (offset > limit_)
|
| + offset = string16::npos;
|
| + }
|
| +
|
| + size_t limit_;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| OffsetAdjuster::Adjustment::Adjustment(size_t original_offset,
|
| size_t original_length,
|
| size_t output_length)
|
| @@ -24,19 +41,20 @@ OffsetAdjuster::Adjustment::Adjustment(size_t original_offset,
|
| }
|
|
|
| // static
|
| -void OffsetAdjuster::AdjustOffsets(
|
| - const Adjustments& adjustments,
|
| - std::vector<size_t>* offsets_for_adjustment) {
|
| - if (!offsets_for_adjustment || adjustments.empty())
|
| - return;
|
| +void OffsetAdjuster::AdjustOffsets(const Adjustments& adjustments,
|
| + std::vector<size_t>* offsets_for_adjustment,
|
| + size_t limit) {
|
| + DCHECK(offsets_for_adjustment);
|
| for (std::vector<size_t>::iterator i(offsets_for_adjustment->begin());
|
| i != offsets_for_adjustment->end(); ++i)
|
| - AdjustOffset(adjustments, &(*i));
|
| + AdjustOffset(adjustments, &(*i), limit);
|
| }
|
|
|
| // static
|
| void OffsetAdjuster::AdjustOffset(const Adjustments& adjustments,
|
| - size_t* offset) {
|
| + size_t* offset,
|
| + size_t limit) {
|
| + DCHECK(offset);
|
| if (*offset == string16::npos)
|
| return;
|
| int adjustment = 0;
|
| @@ -51,6 +69,9 @@ void OffsetAdjuster::AdjustOffset(const Adjustments& adjustments,
|
| adjustment += static_cast<int>(i->original_length - i->output_length);
|
| }
|
| *offset -= adjustment;
|
| +
|
| + if (*offset > limit)
|
| + *offset = string16::npos;
|
| }
|
|
|
| // static
|
| @@ -236,9 +257,8 @@ string16 UTF8ToUTF16WithAdjustments(
|
| string16 UTF8ToUTF16AndAdjustOffsets(
|
| const base::StringPiece& utf8,
|
| std::vector<size_t>* offsets_for_adjustment) {
|
| - std::for_each(offsets_for_adjustment->begin(),
|
| - offsets_for_adjustment->end(),
|
| - LimitOffset<base::StringPiece>(utf8.length()));
|
| + std::for_each(offsets_for_adjustment->begin(), offsets_for_adjustment->end(),
|
| + LimitOffset(utf8.length()));
|
| OffsetAdjuster::Adjustments adjustments;
|
| string16 result = UTF8ToUTF16WithAdjustments(utf8, &adjustments);
|
| OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment);
|
| @@ -248,9 +268,8 @@ string16 UTF8ToUTF16AndAdjustOffsets(
|
| std::string UTF16ToUTF8AndAdjustOffsets(
|
| const base::StringPiece16& utf16,
|
| std::vector<size_t>* offsets_for_adjustment) {
|
| - std::for_each(offsets_for_adjustment->begin(),
|
| - offsets_for_adjustment->end(),
|
| - LimitOffset<base::StringPiece16>(utf16.length()));
|
| + std::for_each(offsets_for_adjustment->begin(), offsets_for_adjustment->end(),
|
| + LimitOffset(utf16.length()));
|
| std::string result;
|
| PrepareForUTF8Output(utf16.data(), utf16.length(), &result);
|
| OffsetAdjuster::Adjustments adjustments;
|
|
|