Skip to content

Commit

Permalink
Improve performance of scanner by avoiding using String#substring in
Browse files Browse the repository at this point in the history
large string (see eclipse-lemminx#48)
  • Loading branch information
angelozerr authored and NikolasKomonen committed Sep 7, 2018
1 parent 4dd6cdd commit cc9d9b0
Showing 1 changed file with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018 Angelo ZERR, Daniel Dekany.
* Copyright (c) 2018 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,11 +11,11 @@
package org.eclipse.lsp4xml.internal.parser;

import static org.eclipse.lsp4xml.internal.parser.Constants._CAR;
import static org.eclipse.lsp4xml.internal.parser.Constants._LAN;
import static org.eclipse.lsp4xml.internal.parser.Constants._LFD;
import static org.eclipse.lsp4xml.internal.parser.Constants._NWL;
import static org.eclipse.lsp4xml.internal.parser.Constants._TAB;
import static org.eclipse.lsp4xml.internal.parser.Constants._WSP;
import static org.eclipse.lsp4xml.internal.parser.Constants._LAN;

import java.util.function.Predicate;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -65,19 +65,19 @@ public void goToEnd() {
this.position = len;
}

/*public int nextChar() {
// return this.source.codePointAt(this.position++) || 0;
int next = this.source.codePointAt(this.position++);
return next >= 0 ? next : 0;
}*/
/*
* public int nextChar() { // return this.source.codePointAt(this.position++) ||
* 0; int next = this.source.codePointAt(this.position++); return next >= 0 ?
* next : 0; }
*/

public int peekChar() {
return peekChar(0);
}

public int peekChar(int n) {
public int peekChar(int n) {
int pos = this.position + n;
if (pos >= len ) {
if (pos >= len) {
return -1;
}
return this.source.codePointAt(pos);
Expand Down Expand Up @@ -106,12 +106,12 @@ public boolean advanceIfChars(int... ch) {
}

public String advanceIfRegExp(Pattern regex) {
String str = this.source.substring(this.position);
Matcher match = regex.matcher(str);
Matcher match = regex.matcher(source);
// Initialize start region where search must be started.
match.region(this.position, this.len);
if (match.find()) {
String s = match.group(0);
this.position = this.position + match.start() + s.length();
return s;
this.position = match.end();
return match.group(0);
}
return "";
}
Expand All @@ -128,7 +128,7 @@ public String advanceUntilRegExp(Pattern regex) {
/**
* Advances stream.position no matter what until it hits ch or eof(this.len)
*
* @return boolean: was the char found
* @return boolean: was the char found
*/
public boolean advanceUntilChar(int ch) {
while (this.position < this.len) {
Expand All @@ -142,12 +142,12 @@ public boolean advanceUntilChar(int ch) {

public boolean advanceUntilAnyOfChars(int... ch) {
while (this.position < this.len) {
for(int i = 0; i < ch.length; i ++) {
for (int i = 0; i < ch.length; i++) {
if (peekChar() == ch[i]) {
return true;
}
}

this.advance(1);
}
return false;
Expand Down Expand Up @@ -181,15 +181,13 @@ public boolean advanceUntilChars(int... ch) {
}

/**
* Advances until it matches int[] ch OR it hits '<'
* If this returns true, peek if next char is '<' to
* check which case was hit
* Advances until it matches int[] ch OR it hits '<' If this returns true, peek
* if next char is '<' to check which case was hit
*/
public boolean advanceUntilCharsOrNewTag(int... ch) {

while (this.position + ch.length <= this.len) {
int i = 0;
if(peekChar(0) == _LAN) { // <
if (peekChar(0) == _LAN) { // <
return true;
}
for (; i < ch.length && peekChar(i) == ch[i]; i++) {
Expand Down

0 comments on commit cc9d9b0

Please sign in to comment.