From 2d0f4f76e2fe2bd66d3612f7ee6847d3b4126e0c Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 16 Mar 2019 10:30:04 -0600 Subject: [PATCH] More FileBuffer functions --- examples/parsing/whitespace.txt | 4 ++++ hank/FileBuffer.hx | 9 +++++++++ tests/FileBufferTest.hx | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 examples/parsing/whitespace.txt diff --git a/examples/parsing/whitespace.txt b/examples/parsing/whitespace.txt new file mode 100644 index 0000000..7c077ab --- /dev/null +++ b/examples/parsing/whitespace.txt @@ -0,0 +1,4 @@ + Just give me this output. +/*comment*/ and on the next line, this output. + Here, just this // and not this +I only want this stuff // not all those spaces! \ No newline at end of file diff --git a/hank/FileBuffer.hx b/hank/FileBuffer.hx index 73c9cdc..23dca8a 100644 --- a/hank/FileBuffer.hx +++ b/hank/FileBuffer.hx @@ -209,4 +209,13 @@ class FileBuffer { public function takeLine(trimmed = ''): Option { return getLine(trimmed, takeUntil); } + + public function skipWhitespace() { + var whitespace = cleanBuffer.substr(0, cleanBuffer.length - StringTools.ltrim(cleanBuffer).length); + drop(whitespace); + } + + public function isEmpty() { + return cleanBuffer.length == 0; + } } \ No newline at end of file diff --git a/tests/FileBufferTest.hx b/tests/FileBufferTest.hx index 3bc99f3..e483007 100644 --- a/tests/FileBufferTest.hx +++ b/tests/FileBufferTest.hx @@ -100,4 +100,24 @@ class FileBufferTest extends utest.Test { // EOF HankAssert.equals(None, file.takeLine()); } + + function testGetLineTrimming() { + file = new FileBuffer('examples/parsing/whitespace.txt'); + + HankAssert.equals(Some("Just give me this output."), file.peekLine("lr")); + HankAssert.equals(Some(" Just give me this output."), file.peekLine("r")); + HankAssert.equals(Some(" Just give me this output. "), file.peekLine("")); + HankAssert.equals(Some("Just give me this output. "), file.takeLine("l")); + + HankAssert.equals(Some("and on the next line, this output."), file.takeLine("lr")); + HankAssert.equals(Some("Here, just this"), file.takeLine("lr")); + HankAssert.equals(Some("I only want this stuff"), file.takeLine("lr")); + } + + function testSkipWhitespace() { + file = new FileBuffer('examples/parsing/whitespace.txt'); + + file.skipWhitespace(); + HankAssert.equals("Just", file.take(4)); + } } \ No newline at end of file