Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/editing/markers/SpellCheckMarkerList.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 namespace blink { | |
| 10 | |
| 11 SpellCheckMarkerList::SpellCheckMarkerList(DocumentMarker::MarkerType type) | |
| 12 : m_type(type) {} | |
| 13 | |
| 14 DocumentMarker::MarkerType SpellCheckMarkerList::allowedMarkerType() const { | |
| 15 return m_type; | |
| 16 } | |
| 17 | |
| 18 bool SpellCheckMarkerList::isSpellCheckMarkerList() const { | |
| 19 return true; | |
| 20 } | |
| 21 | |
| 22 void SpellCheckMarkerList::add(DocumentMarker* marker) { | |
| 23 DCHECK(marker->type() == allowedMarkerType()); | |
|
yosin_UTC9
2017/03/30 01:33:06
nit: Let's use DCHECK_EQ(). If we need to introduc
| |
| 24 // Optimize case where (non-merging) markers are being added in order so we | |
| 25 // can add n markers in O(n) time instead of O(n log n) | |
| 26 if (m_markers.isEmpty() || | |
| 27 m_markers.back()->endOffset() < marker->startOffset()) { | |
| 28 m_markers.push_back(marker); | |
| 29 return; | |
| 30 } | |
| 31 | |
| 32 auto firstOverlappingIt = std::lower_bound( | |
|
yosin_UTC9
2017/03/30 01:33:06
nit: s/auto/const auto&/
| |
| 33 m_markers.begin(), m_markers.end(), marker, | |
| 34 [](const Member<DocumentMarker>& lhv, const DocumentMarker* rhv) { | |
| 35 return lhv->endOffset() < rhv->startOffset(); | |
| 36 }); | |
| 37 | |
| 38 size_t index = firstOverlappingIt - m_markers.begin(); | |
| 39 m_markers.insert(index, marker); | |
| 40 auto insertedIt = m_markers.begin() + index; | |
|
yosin_UTC9
2017/03/30 01:33:06
nit: s/auto/const auto&/
| |
| 41 | |
| 42 unsigned newStart = marker->startOffset(); | |
| 43 unsigned newEnd = marker->endOffset(); | |
| 44 | |
| 45 iterator it; | |
| 46 for (it = insertedIt + 1; | |
| 47 it != m_markers.end() && (*it)->startOffset() <= newEnd; ++it) { | |
| 48 newStart = std::min(newStart, (*it)->startOffset()); | |
| 49 newEnd = std::max(newEnd, (*it)->endOffset()); | |
| 50 } | |
| 51 m_markers.erase(index + 1, it - (insertedIt + 1)); | |
| 52 | |
| 53 (*insertedIt)->setStartOffset(newStart); | |
| 54 (*insertedIt)->setEndOffset(newEnd); | |
| 55 } | |
| 56 | |
| 57 void SpellCheckMarkerList::removeMarkersForWords(const String& nodeText, | |
| 58 const Vector<String>& words) { | |
| 59 // Build a second vector and swap with m_markers to avoid O(n^2) performance | |
| 60 HeapVector<Member<DocumentMarker>> newMarkerList; | |
| 61 | |
| 62 for (Member<DocumentMarker> marker : m_markers) { | |
|
yosin_UTC9
2017/03/30 01:33:05
Can we use std::copy_if()[1]?
[1] http://en.cppre
| |
| 63 unsigned start = marker->startOffset(); | |
| 64 unsigned length = marker->endOffset() - marker->startOffset(); | |
| 65 String markerText = nodeText.substring(start, length); | |
|
yosin_UTC9
2017/03/30 01:33:05
nit: s/String/const String&/
| |
| 66 if (!words.contains(markerText)) | |
| 67 newMarkerList.push_back(marker); | |
| 68 } | |
| 69 | |
| 70 std::swap(m_markers, newMarkerList); | |
| 71 } | |
| 72 | |
| 73 } // namespace blink | |
| OLD | NEW |