aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-channel/src
diff options
context:
space:
mode:
authorfmallet <freddy.mallet@gmail.com>2010-11-11 16:48:10 +0000
committerfmallet <freddy.mallet@gmail.com>2010-11-11 16:48:10 +0000
commit92e61d65dc65e01338e472615a846e3fc9620426 (patch)
tree2ebaea5c043cc8b561d1a30ef89689cc37a15621 /sonar-channel/src
parentbd980b0ccd13cb60d5afeec9140b878353a9c86a (diff)
downloadsonarqube-92e61d65dc65e01338e472615a846e3fc9620426.tar.gz
sonarqube-92e61d65dc65e01338e472615a846e3fc9620426.zip
Add a new push(CharSequence characters) to the CodeBuffer class to be able to push back a character sequence at any time.
Diffstat (limited to 'sonar-channel/src')
-rw-r--r--sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java45
-rw-r--r--sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java16
2 files changed, 52 insertions, 9 deletions
diff --git a/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java b/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java
index a8433264b7a..34cdb51ad1d 100644
--- a/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java
+++ b/sonar-channel/src/main/java/org/sonar/channel/CodeBuffer.java
@@ -85,6 +85,15 @@ public class CodeBuffer implements CharSequence {
return -1;
}
int character = buffer[bufferPosition++];
+ updateCursorPosition(character);
+ if (recordingMode) {
+ recordedCharacters.append((char) character);
+ }
+ lastChar = character;
+ return character;
+ }
+
+ private final void updateCursorPosition(int character) {
if (character == LF || character == CR) {
if ((lastChar != LF && lastChar != CR) || lastChar == character || lastChar == LF) {
cursor.line++;
@@ -95,11 +104,6 @@ public class CodeBuffer implements CharSequence {
} else {
cursor.column++;
}
- if (recordingMode) {
- recordedCharacters.append((char) character);
- }
- lastChar = character;
- return character;
}
private int fillBuffer() {
@@ -121,7 +125,7 @@ public class CodeBuffer implements CharSequence {
}
/**
- * Get the last consumed character
+ * Looks at the last consumed character
*
* @return the last character or -1 if the no character has been yet consumed
*/
@@ -130,15 +134,40 @@ public class CodeBuffer implements CharSequence {
}
/**
- * Read without consuming the next character
+ * Looks at the next character without consuming it
*
- * @return the next character or -1 if the end of the stream is reached
+ * @return the next character or -1 if the end of the stream has been reached
*/
public final int peek() {
return intAt(0);
}
/**
+ * Pushes a character sequence onto the top of this CodeBuffer. This characters will be then the first to be read.
+ *
+ * @param chars
+ * the character sequences to push into the CodeBuffer
+ */
+ public void push(CharSequence chars) {
+ int length = chars.length();
+ if (bufferPosition >= length) {
+ for (int index = 0; index < length; index++) {
+ buffer[bufferPosition + index - length] = chars.charAt(index);
+ }
+ bufferPosition -= length;
+ } else {
+ char[] extendedBuffer = new char[buffer.length - bufferPosition + length];
+ for (int index = 0; index < length; index++) {
+ extendedBuffer[index] = chars.charAt(index);
+ }
+ System.arraycopy(buffer, bufferPosition, extendedBuffer, length, bufferSize - bufferPosition);
+ buffer = extendedBuffer;
+ bufferSize = bufferSize + length - bufferPosition;
+ bufferPosition = 0;
+ }
+ }
+
+ /**
* Close the stream
*/
public final void close() {
diff --git a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
index d9ce7fe570b..98ffb19af42 100644
--- a/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
+++ b/sonar-channel/src/test/java/org/sonar/channel/CodeBufferTest.java
@@ -31,7 +31,7 @@ import org.junit.Test;
public class CodeBufferTest {
private CodeReaderConfiguration defaulConfiguration = new CodeReaderConfiguration();
-
+
@Test
public void testPop() {
CodeBuffer code = new CodeBuffer("pa", defaulConfiguration);
@@ -117,6 +117,20 @@ public class CodeBufferTest {
}
@Test
+ public void testPush() {
+ CodeReader reader = new CodeReader("12", defaulConfiguration);
+ assertEquals('1', (char) reader.pop());
+ reader.push("a");
+ assertEquals('a', (char) reader.peek());
+ reader.push("45");
+ assertEquals("45a2", new String(reader.peek(4)));
+ for (int i = 0; i < 4; i++) {
+ reader.pop();
+ }
+ assertEquals( -1, reader.pop());
+ }
+
+ @Test
public void testCharAtIndexOutOfBoundsException() {
CodeBuffer reader = new CodeBuffer("12345", defaulConfiguration);
assertEquals(reader.charAt(5), (char) -1);