blob: 882d4e9df34732df6994b3e58d572273000df1aa [file] [log] [blame]
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/wtf/text/text_offset_map.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace WTF {
std::ostream& operator<<(std::ostream& stream,
const TextOffsetMap::Entry& entry) {
return stream << "{" << entry.source << ", " << entry.target << "}";
}
void TextOffsetMap::Append(wtf_size_t source, wtf_size_t target) {
DCHECK(IsEmpty() ||
(source > entries_.back().source && target > entries_.back().target));
entries_.emplace_back(source, target);
}
void TextOffsetMap::Append(const icu::Edits& edits) {
DCHECK(IsEmpty());
UErrorCode error = U_ZERO_ERROR;
auto edit = edits.getFineChangesIterator();
while (edit.next(error)) {
if (!edit.hasChange() || edit.oldLength() == edit.newLength())
continue;
entries_.emplace_back(edit.sourceIndex() + edit.oldLength(),
edit.destinationIndex() + edit.newLength());
}
DCHECK(U_SUCCESS(error));
}
} // namespace WTF