Skip to content

Commit

Permalink
Updated AdvancedStringReader and corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed May 9, 2016
1 parent a2ae9a8 commit 623cbdf
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
79 changes: 78 additions & 1 deletion src/main/java/com/sangupta/jerry/io/AdvancedStringReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ public boolean hasNext() {
public String readTillNext(char separator) {
return this.readTillNext(String.valueOf(separator), 1);
}

public char peekAhead(int position) {
int pos = this.current + position;
if(pos > this.length) {
return 0;
}

return this.str.charAt(pos);
}

public String readTillPosition(int position) {
int pos = this.current + position;
if(pos > this.length) {
pos = this.length;
}

String result = this.str.substring(this.current, pos);
this.current = pos;
return result;
}

/**
* Read the string from current position to the next nth occurrence of the
Expand All @@ -103,7 +123,17 @@ public String readTillNext(char separator, int occurence) {
return this.readTillNext(String.valueOf(separator), occurence);
}

/**
public int peekIndex(char c) {
for(int index = this.current; index < this.length; index++) {
if(this.str.charAt(index) == c) {
return index - this.current;
}
}

return -1;
}

/**
*
* @param separator
* @return
Expand Down Expand Up @@ -193,10 +223,57 @@ public String readNext(int numCharacters) {
if(end > this.length) {
end = this.length;
}

String result = this.str.substring(this.current, end);
this.current = end;
return result;
}

public String readAfter(char character) {
if(!this.hasNext()) {
return null;
}

int index;
for(index = this.current; index < this.length; index++) {
if(this.str.charAt(index) == character) {
break;
}
}

index++;
this.current = this.length;
return this.str.substring(index);
}

public String readFrom(char character) {
if(!this.hasNext()) {
return null;
}

int index;
for(index = this.current; index < this.length; index++) {
if(this.str.charAt(index) == character) {
break;
}
}

this.current = this.length;
return this.str.substring(index);
}

public String peekNext(int numCharacters) {
if(!this.hasNext()) {
return null;
}

int end = this.current + numCharacters;
if(end > this.length) {
end = this.length;
}

return this.str.substring(this.current, end);
}

/**
* Read the string between the first occurrence of starting character, and
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/sangupta/jerry/io/TestAdvancedStringReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,30 @@ public void testSkipNext() {
Assert.assertEquals(2, reader.skipNext(3));
Assert.assertEquals(0, reader.skipNext(3));
}

@Test
public void testReadAfter() {
AdvancedStringReader reader = new AdvancedStringReader("hello world");
Assert.assertEquals("orld", reader.readAfter('w'));

reader = new AdvancedStringReader("hello world, its my world");
Assert.assertEquals("orld, its my world", reader.readAfter('w'));

reader = new AdvancedStringReader("hello world, its my world");
Assert.assertEquals("hello world", reader.readTillNext(','));
Assert.assertEquals("y world", reader.readAfter('m'));
}

@Test
public void testReadFrom() {
AdvancedStringReader reader = new AdvancedStringReader("hello world");
Assert.assertEquals("world", reader.readFrom('w'));

reader = new AdvancedStringReader("hello world, its my world");
Assert.assertEquals("world, its my world", reader.readFrom('w'));

reader = new AdvancedStringReader("hello world, its my world");
Assert.assertEquals("hello world", reader.readTillNext(','));
Assert.assertEquals("my world", reader.readFrom('m'));
}
}

0 comments on commit 623cbdf

Please sign in to comment.