changeset 20:4391afcf6bd0

Fix more bugs, correct more bad tests.
author David Barts <n5jrn@me.com>
date Sun, 30 Jun 2024 22:28:52 -0700
parents 7d80cbcb67bb
children ea65ab890f66
files src/main/kotlin/name/blackcap/passman/Shplitter.kt src/test/kotlin/name/blackcap/passman/ShplitterTest.kt
diffstat 2 files changed, 9 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/Shplitter.kt	Sun Jun 30 20:37:36 2024 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Shplitter.kt	Sun Jun 30 22:28:52 2024 -0700
@@ -31,13 +31,11 @@
 
     fun split(): Iterable<String> {
         if (complete) {
-            if (current.isNotEmpty()) {
+            if (state == ::nonspace) {
+                popState()
                 accum.add(current.toString())
                 current.clear()
             }
-            if (state == ::nonspace) {
-                popState()
-            }
             return accum
         } else {
             throw IllegalStateException("incomplete quoted expression")
@@ -57,14 +55,6 @@
 
     private fun lastState(): State = oldStates.last()
 
-    private fun endQuote(): Unit {
-        if (lastState() == ::space) {
-            accum.add(current.toString())
-            current.clear()
-        }
-        popState()
-    }
-
     // States. A state is represented by a function that accepts the
     // character currently being processed, and returns whether it should
     // immediately transition to the next state without reading a new
@@ -73,9 +63,9 @@
     private fun space(ch: Char): Boolean =
         when (ch) {
             in WHITESPACE -> { false }
-            '\'' -> { pushState(::inSingle); false }
-            '"' -> { pushState(::inDouble); false }
-            '\\' -> { pushState(::backslash); false }
+            '\'' -> { pushState(::nonspace); pushState(::inSingle); false }
+            '"' -> { pushState(::nonspace); pushState(::inDouble); false }
+            '\\' -> { pushState(::nonspace); pushState(::backslash); false }
             else -> { pushState(::nonspace); true }
         }
 
@@ -107,14 +97,14 @@
 
     private fun inSingle(ch: Char): Boolean =
         when (ch) {
-            '\'' -> { endQuote(); false }
+            '\'' -> { popState(); false }
             else -> { current.append(ch); false }
         }
 
     private fun inDouble(ch: Char): Boolean =
         when (ch) {
             '\\' -> { pushState(::backslash); false }
-            '"' -> { endQuote(); false }
+            '"' -> { popState(); false }
             else -> { current.append(ch); false }
         }
 
--- a/src/test/kotlin/name/blackcap/passman/ShplitterTest.kt	Sun Jun 30 20:37:36 2024 -0700
+++ b/src/test/kotlin/name/blackcap/passman/ShplitterTest.kt	Sun Jun 30 22:28:52 2024 -0700
@@ -91,12 +91,12 @@
         TestDatum("\"foo\\ x\\x\\\\\"", arrayOf<String>("foo xx\\")),
         TestDatum("\"foo\\ x\\x\\\\\"\"foobar\"", arrayOf<String>("foo xx\\foobar")),
         TestDatum("\"foo\\ x\\x\\\\\"\\'\"foobar\"", arrayOf<String>("foo xx\\'foobar")),
-        TestDatum("\"foo\\ x\\x\\\\\"\\'\"fo'obar\" 'don'\\''t'", arrayOf<String>("foo xx\\'foobar\" don\\t")),
+        TestDatum("\"foo\\ x\\x\\\\\"\\'\"fo'obar\" 'don'\\''t'", arrayOf<String>("foo xx\\'fo'obar", "don't")),
         TestDatum("'foo\\ bar'", arrayOf<String>("foo\\ bar")),
         TestDatum("'foo\\\\ bar'", arrayOf<String>("foo\\\\ bar")),
         TestDatum("foo\\ bar", arrayOf<String>("foo bar")),
         TestDatum("foo#bar\\nbaz", arrayOf<String>("foo#barnbaz")),
-        TestDatum(":-) ;-)", arrayOf<String>(":-)", ":-)")),
+        TestDatum(":-) ;-)", arrayOf<String>(":-)", ";-)")),
         TestDatum("áéíóú", arrayOf<String>("áéíóú"))
     )