From 623cbdf0751270678316230415c8f93cd45ff178 Mon Sep 17 00:00:00 2001 From: Sandeep Gupta Date: Mon, 9 May 2016 14:32:40 +0530 Subject: [PATCH] Updated AdvancedStringReader and corresponding tests --- .../jerry/io/AdvancedStringReader.java | 79 ++++++++++++++++++- .../jerry/io/TestAdvancedStringReader.java | 26 ++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sangupta/jerry/io/AdvancedStringReader.java b/src/main/java/com/sangupta/jerry/io/AdvancedStringReader.java index 0bef73a..37473df 100644 --- a/src/main/java/com/sangupta/jerry/io/AdvancedStringReader.java +++ b/src/main/java/com/sangupta/jerry/io/AdvancedStringReader.java @@ -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 @@ -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 @@ -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 diff --git a/src/test/java/com/sangupta/jerry/io/TestAdvancedStringReader.java b/src/test/java/com/sangupta/jerry/io/TestAdvancedStringReader.java index ce5c7ca..6d9089e 100644 --- a/src/test/java/com/sangupta/jerry/io/TestAdvancedStringReader.java +++ b/src/test/java/com/sangupta/jerry/io/TestAdvancedStringReader.java @@ -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')); + } }