1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 17:26:01 +02:00

2005-07-21 Chris Wiebe

Fix PR 104442
	* src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java
This commit is contained in:
Chris Wiebe 2005-07-20 23:35:04 +00:00
parent 37b6935570
commit 1c3ede2638
2 changed files with 60 additions and 14 deletions

View file

@ -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 2005-07-09 Alain Magloire
Fix PR 102324: Fire event in the property page for change contentType Fix PR 102324: Fire event in the property page for change contentType
* src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPreferenceBlock.java * src/org/eclipse/cdt/internal/ui/preferences/CFileTypesPreferenceBlock.java

View file

@ -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 SLASH_STAR= 3; // prefix for MULTI_LINE_COMMENT
private static final int STAR= 4; // postfix 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 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. */ /** The scanner. */
// private final BufferedRuleBasedScanner fScanner= new BufferedRuleBasedScanner(1000); // private final BufferedRuleBasedScanner fScanner= new BufferedRuleBasedScanner(1000);
@ -66,12 +65,12 @@ public class FastCPartitionScanner implements IPartitionTokenScanner, ICPartitio
fTokenOffset += fTokenLength; fTokenOffset += fTokenLength;
fTokenLength= fPrefixLength; fTokenLength= fPrefixLength;
final char[][] delimiters = fScanner.getLegalLineDelimiters();
while (true) { while (true) {
final int ch= fScanner.read(); final int ch= fScanner.read();
// characters if (ch == ICharacterScanner.EOF) {
switch (ch) {
case ICharacterScanner.EOF:
if (fTokenLength > 0) { if (fTokenLength > 0) {
fLast= NONE; // ignore last fLast= NONE; // ignore last
return preFix(fState, CCODE, NONE, 0); return preFix(fState, CCODE, NONE, 0);
@ -80,20 +79,23 @@ public class FastCPartitionScanner implements IPartitionTokenScanner, ICPartitio
fLast= NONE; fLast= NONE;
fPrefixLength= 0; fPrefixLength= 0;
return Token.EOF; return Token.EOF;
}
case '\r': // detect if we're at the end of the line
fLast= (fLast == BACKSLASH) ? ESCAPED_CR : CARRIAGE_RETURN; final int delim = detectLineDelimiter(ch, fScanner, delimiters);
fTokenLength++; if (delim != -1) {
continue; final int len = delimiters[delim].length;
if (len > 1) {
case '\n': // adjust the token length if the delimiter was 2 or more chars
fTokenLength += (len - 1);
}
switch (fState) { switch (fState) {
case SINGLE_LINE_COMMENT: case SINGLE_LINE_COMMENT:
case CHARACTER: case CHARACTER:
//case STRING: //case STRING:
// assert(fTokenLength > 0); // assert(fTokenLength > 0);
boolean escapedLine = (fLast == BACKSLASH || fLast == ESCAPED_CR); // if last char was a backslash then we have an escaped line
if (!escapedLine) { if (fLast != BACKSLASH) {
return postFix(fState); return postFix(fState);
} }
// FALLTHROUGH // 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;
}
/**
* <code>true</code> 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) { private static final int getLastLength(int last) {
switch (last) { switch (last) {
default: default: