From 1c3ede2638e50fc870c2cfa386e8206d782fcb5a Mon Sep 17 00:00:00 2001 From: Chris Wiebe Date: Wed, 20 Jul 2005 23:35:04 +0000 Subject: [PATCH] 2005-07-21 Chris Wiebe Fix PR 104442 * src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java --- core/org.eclipse.cdt.ui/ChangeLog | 4 ++ .../ui/text/FastCPartitionScanner.java | 70 +++++++++++++++---- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index c15a22d09e1..bd44d548a83 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -1,3 +1,7 @@ +2005-07-21 Chris Wiebe + Fix PR 104442 + * src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java + 2005-07-09 Alain Magloire Fix PR 102324: Fire event in the property page for change contentType * src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPreferenceBlock.java diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java index 38bf435bc61..7f68ba777b3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java @@ -33,7 +33,6 @@ public class FastCPartitionScanner implements IPartitionTokenScanner, ICPartitio private static final int SLASH_STAR= 3; // prefix for MULTI_LINE_COMMENT private static final int STAR= 4; // postfix for MULTI_LINE_COMMENT private static final int CARRIAGE_RETURN=5; // postfix for STRING, CHARACTER and SINGLE_LINE_COMMENT - private static final int ESCAPED_CR=6; // for win32 system where termination string is \r\n save the backslash /** The scanner. */ // private final BufferedRuleBasedScanner fScanner= new BufferedRuleBasedScanner(1000); @@ -66,12 +65,12 @@ public class FastCPartitionScanner implements IPartitionTokenScanner, ICPartitio fTokenOffset += fTokenLength; fTokenLength= fPrefixLength; + final char[][] delimiters = fScanner.getLegalLineDelimiters(); + while (true) { final int ch= fScanner.read(); - - // characters - switch (ch) { - case ICharacterScanner.EOF: + + if (ch == ICharacterScanner.EOF) { if (fTokenLength > 0) { fLast= NONE; // ignore last return preFix(fState, CCODE, NONE, 0); @@ -80,20 +79,23 @@ public class FastCPartitionScanner implements IPartitionTokenScanner, ICPartitio fLast= NONE; fPrefixLength= 0; return Token.EOF; - - case '\r': - fLast= (fLast == BACKSLASH) ? ESCAPED_CR : CARRIAGE_RETURN; - fTokenLength++; - continue; - - case '\n': + } + + // detect if we're at the end of the line + final int delim = detectLineDelimiter(ch, fScanner, delimiters); + if (delim != -1) { + final int len = delimiters[delim].length; + if (len > 1) { + // adjust the token length if the delimiter was 2 or more chars + fTokenLength += (len - 1); + } switch (fState) { case SINGLE_LINE_COMMENT: case CHARACTER: //case STRING: // assert(fTokenLength > 0); - boolean escapedLine = (fLast == BACKSLASH || fLast == ESCAPED_CR); - if (!escapedLine) { + // if last char was a backslash then we have an escaped line + if (fLast != BACKSLASH) { return postFix(fState); } // FALLTHROUGH @@ -242,6 +244,46 @@ public class FastCPartitionScanner implements IPartitionTokenScanner, ICPartitio } } + /** + * returns index of longest matching element in delimiters + */ + private static final int detectLineDelimiter(final int ch, final ICharacterScanner scanner, final char[][] delimiters) { + int longestDelimiter = -1; + int maxLen = 0; + for (int i = 0; i < delimiters.length; i++) { + final char[] delim = delimiters[i]; + if (ch == delim[0]) { + final int len = delim.length; + if (len > maxLen && (len == 1 || sequenceDetected(scanner, delim, 1, len))) { + maxLen = len; + longestDelimiter = i; + } + } + } + return longestDelimiter; + } + + /** + * true if sequence matches between beginIndex (inclusive) and endIndex (exclusive) + */ + private static final boolean sequenceDetected(final ICharacterScanner scanner, final char[] sequence, final int beginIndex, final int endIndex) { + int charsRead = 0; + for (int i = beginIndex; i < endIndex; ++i) { + final int c = scanner.read(); + if (c != ICharacterScanner.EOF) { + ++charsRead; + } + if (c != sequence[i]) { + // Non-matching character detected, rewind the scanner back to the start. + for (; charsRead > 0; --charsRead) { + scanner.unread(); + } + return false; + } + } + return true; + } + private static final int getLastLength(int last) { switch (last) { default: