diff src/main/kotlin/name/blackcap/passman/Shplitter.kt @ 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
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 }
         }