Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: third_party/WebKit/Source/core/editing/VisibleUnits.cpp

Issue 1604783002: ALL-IN-ONE: Optimization to previousBoundary() and nextBoundary() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@progressive_accumulator
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 22 matching lines...) Expand all
33 #include "core/dom/Text.h" 33 #include "core/dom/Text.h"
34 #include "core/editing/EditingUtilities.h" 34 #include "core/editing/EditingUtilities.h"
35 #include "core/editing/FrameSelection.h" 35 #include "core/editing/FrameSelection.h"
36 #include "core/editing/Position.h" 36 #include "core/editing/Position.h"
37 #include "core/editing/PositionIterator.h" 37 #include "core/editing/PositionIterator.h"
38 #include "core/editing/RenderedPosition.h" 38 #include "core/editing/RenderedPosition.h"
39 #include "core/editing/TextAffinity.h" 39 #include "core/editing/TextAffinity.h"
40 #include "core/editing/VisiblePosition.h" 40 #include "core/editing/VisiblePosition.h"
41 #include "core/editing/iterators/BackwardsCharacterIterator.h" 41 #include "core/editing/iterators/BackwardsCharacterIterator.h"
42 #include "core/editing/iterators/CharacterIterator.h" 42 #include "core/editing/iterators/CharacterIterator.h"
43 #include "core/editing/iterators/ProgressiveTextAccumulator.h"
43 #include "core/editing/iterators/SimplifiedBackwardsTextIterator.h" 44 #include "core/editing/iterators/SimplifiedBackwardsTextIterator.h"
44 #include "core/editing/iterators/TextIterator.h" 45 #include "core/editing/iterators/TextIterator.h"
45 #include "core/frame/LocalFrame.h" 46 #include "core/frame/LocalFrame.h"
46 #include "core/frame/Settings.h" 47 #include "core/frame/Settings.h"
47 #include "core/html/HTMLBRElement.h" 48 #include "core/html/HTMLBRElement.h"
48 #include "core/html/HTMLTextFormControlElement.h" 49 #include "core/html/HTMLTextFormControlElement.h"
49 #include "core/layout/HitTestRequest.h" 50 #include "core/layout/HitTestRequest.h"
50 #include "core/layout/HitTestResult.h" 51 #include "core/layout/HitTestResult.h"
51 #include "core/layout/LayoutBlockFlow.h" 52 #include "core/layout/LayoutBlockFlow.h"
52 #include "core/layout/LayoutInline.h" 53 #include "core/layout/LayoutInline.h"
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 forwardsIterator.copyTextTo(characters); 677 forwardsIterator.copyTextTo(characters);
677 int i = endOfFirstWordBoundaryContext(characters.data(), characters. size()); 678 int i = endOfFirstWordBoundaryContext(characters.data(), characters. size());
678 string.append(characters.data(), i); 679 string.append(characters.data(), i);
679 suffixLength += i; 680 suffixLength += i;
680 if (static_cast<unsigned>(i) < characters.size()) 681 if (static_cast<unsigned>(i) < characters.size())
681 break; 682 break;
682 forwardsIterator.advance(); 683 forwardsIterator.advance();
683 } 684 }
684 } 685 }
685 686
686 SimplifiedBackwardsTextIteratorAlgorithm<Strategy> it(start, end); 687 ProgressiveTextAccumulator<SimplifiedBackwardsTextIteratorAlgorithm<Strategy >> it(start, end);
687 unsigned next = 0; 688 unsigned next = 0;
688 bool needMoreContext = false; 689 bool needMoreContext = false;
689 while (!it.atEnd()) { 690 while (!it.atEnd()) {
690 bool inTextSecurityMode = it.isInTextSecurityMode();
691 // iterate to get chunks until the searchFunction returns a non-zero 691 // iterate to get chunks until the searchFunction returns a non-zero
692 // value. 692 // value.
693 // TODO(xiaochengh): Iterative prepending has quadratic running time 693 it.accumulateTextTo(string);
694 // in the worst case. Should improve it to linear.
695 if (!inTextSecurityMode) {
696 it.copyTextTo(string);
697 } else {
698 // Treat bullets used in the text security mode as regular
699 // characters when looking for boundaries
700 Vector<UChar, 1024> iteratorString;
701 iteratorString.fill('x', it.length());
702 string.prepend(iteratorString.data(), iteratorString.size());
703 }
704 // TODO(xiaochengh): The following line takes O(string.size()) time, 694 // TODO(xiaochengh): The following line takes O(string.size()) time,
705 // which makes the while loop take quadratic time in the worst case. 695 // which makes the while loop take quadratic time in the worst case.
706 // Should improve it in some way. 696 // Should improve it in some way.
707 next = searchFunction(string.data(), string.size(), string.size() - suff ixLength, MayHaveMoreContext, needMoreContext); 697 next = searchFunction(string.data(), string.size(), string.size() - suff ixLength, MayHaveMoreContext, needMoreContext);
708 if (next) 698 if (next)
709 break; 699 break;
710 it.advance(); 700 it.advance();
711 } 701 }
712 if (needMoreContext) { 702 if (needMoreContext) {
713 // The last search returned the beginning of the buffer and asked for 703 // The last search returned the beginning of the buffer and asked for
714 // more context, but there is no earlier text. Force a search with 704 // more context, but there is no earlier text. Force a search with
715 // what's available. 705 // what's available.
716 // TODO(xiaochengh): Do we have to search the whole string? 706 // TODO(xiaochengh): Do we have to search the whole string?
717 next = searchFunction(string.data(), string.size(), string.size() - suff ixLength, DontHaveMoreContext, needMoreContext); 707 next = searchFunction(string.data(), string.size(), string.size() - suff ixLength, DontHaveMoreContext, needMoreContext);
718 ASSERT(!needMoreContext); 708 ASSERT(!needMoreContext);
719 } 709 }
720 710
721 if (!next) 711 if (!next)
722 return createVisiblePosition(it.atEnd() ? it.startPosition() : pos); 712 return createVisiblePosition(it.iterator().atEnd() ? it.iterator().start Position() : pos);
723 713
724 Node* node = it.startContainer(); 714 Node* node = it.iterator().startContainer();
725 if (node->isTextNode() && static_cast<int>(next) <= node->maxCharacterOffset ()) { 715 int boundaryOffset = it.unaccumulatedLengthInCurrentNode() + next;
716 if (node->isTextNode() && boundaryOffset <= node->maxCharacterOffset()) {
726 // The next variable contains a usable index into a text node 717 // The next variable contains a usable index into a text node
727 return createVisiblePosition(PositionTemplate<Strategy>(node, next)); 718 return createVisiblePosition(PositionTemplate<Strategy>(node, boundaryOf fset));
728 } 719 }
729 720
730 // Use the character iterator to translate the next value into a DOM 721 // Use the character iterator to translate the next value into a DOM
731 // position. 722 // position.
732 BackwardsCharacterIteratorAlgorithm<Strategy> charIt(start, end); 723 BackwardsCharacterIteratorAlgorithm<Strategy> charIt(start, end);
733 charIt.advance(string.size() - suffixLength - next); 724 charIt.advance(string.size() - suffixLength - next);
734 // TODO(yosin) charIt can get out of shadow host. 725 // TODO(yosin) charIt can get out of shadow host.
735 return createVisiblePosition(charIt.endPosition()); 726 return createVisiblePosition(charIt.endPosition());
736 } 727 }
737 728
(...skipping 23 matching lines...) Expand all
761 string.prepend(characters.data() + i, length - i); 752 string.prepend(characters.data() + i, length - i);
762 prefixLength += length - i; 753 prefixLength += length - i;
763 if (i > 0) 754 if (i > 0)
764 break; 755 break;
765 backwardsIterator.advance(); 756 backwardsIterator.advance();
766 } 757 }
767 } 758 }
768 759
769 const PositionTemplate<Strategy> searchStart = PositionTemplate<Strategy>::e ditingPositionOf(start.anchorNode(), start.offsetInContainerNode()); 760 const PositionTemplate<Strategy> searchStart = PositionTemplate<Strategy>::e ditingPositionOf(start.anchorNode(), start.offsetInContainerNode());
770 const PositionTemplate<Strategy> searchEnd = PositionTemplate<Strategy>::las tPositionInNode(boundary); 761 const PositionTemplate<Strategy> searchEnd = PositionTemplate<Strategy>::las tPositionInNode(boundary);
771 TextIteratorAlgorithm<Strategy> it(searchStart, searchEnd, TextIteratorEmits CharactersBetweenAllVisiblePositions); 762 ProgressiveTextAccumulator<TextIteratorAlgorithm<Strategy>> it(searchStart, searchEnd, TextIteratorEmitsCharactersBetweenAllVisiblePositions);
yosin_UTC9 2016/01/20 05:21:44 We should have both |accumulator| and |it| rather
772 const unsigned invalidOffset = static_cast<unsigned>(-1); 763 const unsigned invalidOffset = static_cast<unsigned>(-1);
773 unsigned next = invalidOffset; 764 unsigned next = invalidOffset;
774 unsigned offset = prefixLength; 765 unsigned offset = prefixLength;
775 bool needMoreContext = false; 766 bool needMoreContext = false;
776 while (!it.atEnd()) { 767 while (!it.atEnd()) {
777 // Keep asking the iterator for chunks until the search function 768 // Keep asking the iterator for chunks until the search function
778 // returns an end value not equal to the length of the string passed to 769 // returns an end value not equal to the length of the string passed to
779 // it. 770 // it.
780 bool inTextSecurityMode = it.isInTextSecurityMode(); 771 it.accumulateTextTo(string);
781 if (!inTextSecurityMode) {
782 it.copyTextTo(string);
783 } else {
784 // Treat bullets used in the text security mode as regular
785 // characters when looking for boundaries
786 Vector<UChar, 1024> iteratorString;
787 iteratorString.fill('x', it.length());
788 string.append(iteratorString.data(), iteratorString.size());
789 }
790 next = searchFunction(string.data(), string.size(), offset, MayHaveMoreC ontext, needMoreContext); 772 next = searchFunction(string.data(), string.size(), offset, MayHaveMoreC ontext, needMoreContext);
791 if (next != string.size()) 773 if (next != string.size())
792 break; 774 break;
793 it.advance(); 775 it.advance();
794 if (!needMoreContext) { 776 if (!needMoreContext) {
795 // When the search does not need more context, skip all examined 777 // When the search does not need more context, skip all examined
796 // characters except the last one, in case it is a boundary. 778 // characters except the last one, in case it is a boundary.
797 offset = string.size(); 779 offset = string.size();
798 U16_BACK_1(string.data(), 0, offset); 780 U16_BACK_1(string.data(), 0, offset);
799 } 781 }
800 } 782 }
801 if (needMoreContext) { 783 if (needMoreContext) {
802 // The last search returned the end of the buffer and asked for more 784 // The last search returned the end of the buffer and asked for more
803 // context, but there is no further text. Force a search with what's 785 // context, but there is no further text. Force a search with what's
804 // available. 786 // available.
805 // TODO(xiaochengh): Do we still have to search the whole string? 787 // TODO(xiaochengh): Do we still have to search the whole string?
806 next = searchFunction(string.data(), string.size(), prefixLength, DontHa veMoreContext, needMoreContext); 788 next = searchFunction(string.data(), string.size(), prefixLength, DontHa veMoreContext, needMoreContext);
807 ASSERT(!needMoreContext); 789 ASSERT(!needMoreContext);
808 } 790 }
809 791
810 if (it.atEnd() && next == string.size()) { 792 if (it.atEnd() && next == string.size()) {
811 pos = it.startPositionInCurrentContainer(); 793 pos = it.iterator().startPositionInCurrentContainer();
812 } else if (next != invalidOffset && next != prefixLength) { 794 } else if (next != invalidOffset && next != prefixLength) {
813 // Use the character iterator to translate the next value into a DOM 795 // Use the character iterator to translate the next value into a DOM
814 // position. 796 // position.
815 CharacterIteratorAlgorithm<Strategy> charIt(searchStart, searchEnd, Text IteratorEmitsCharactersBetweenAllVisiblePositions); 797 CharacterIteratorAlgorithm<Strategy> charIt(searchStart, searchEnd, Text IteratorEmitsCharactersBetweenAllVisiblePositions);
816 charIt.advance(next - prefixLength - 1); 798 charIt.advance(next - prefixLength - 1);
817 pos = charIt.endPosition(); 799 pos = charIt.endPosition();
818 800
819 if (charIt.characterAt(0) == '\n') { 801 if (charIt.characterAt(0) == '\n') {
820 // TODO(yosin) workaround for collapsed range (where only start 802 // TODO(yosin) workaround for collapsed range (where only start
821 // position is correct) emitted for some emitted newlines 803 // position is correct) emitted for some emitted newlines
(...skipping 2505 matching lines...) Expand 10 before | Expand all | Expand 10 after
3327 { 3309 {
3328 return previousPositionOfAlgorithm<EditingStrategy>(visiblePosition, rule); 3310 return previousPositionOfAlgorithm<EditingStrategy>(visiblePosition, rule);
3329 } 3311 }
3330 3312
3331 VisiblePositionInComposedTree previousPositionOf(const VisiblePositionInComposed Tree& visiblePosition, EditingBoundaryCrossingRule rule) 3313 VisiblePositionInComposedTree previousPositionOf(const VisiblePositionInComposed Tree& visiblePosition, EditingBoundaryCrossingRule rule)
3332 { 3314 {
3333 return previousPositionOfAlgorithm<EditingInComposedTreeStrategy>(visiblePos ition, rule); 3315 return previousPositionOfAlgorithm<EditingInComposedTreeStrategy>(visiblePos ition, rule);
3334 } 3316 }
3335 3317
3336 } // namespace blink 3318 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698