Commits

chao committed 618f02d

Tweak how split fields are joined together

When the delimiter appears at the beginning of a line, previous code would miss
it after joining the fields together

  • Participants
  • Parent commits 540b488

Comments (0)

Files changed (1)

File src/Command.hs

 import Range
 
 data LineSelection = LineSelection
-    { rangeSel :: !Range -- Pre-regex range seleciton
+    { rangeSel :: !Range -- Pre-regex range selection
     , regexSel :: !(Maybe T.Text)
-    , regexRangeSel :: !Range -- Post-regex range seleciton
+    , regexRangeSel :: !Range -- Post-regex range selection
     } deriving (Show)
 
 data LineCommand
             Just fcs -> T.unlines $ map (execFieldCommands fcs (fieldDelimiter cmd)) selectedLines
             Nothing  -> T.unlines selectedLines
 
+seletLines :: LineCommand -> LineSelection -> [T.Text] -> [T.Text]
 seletLines lnCmd lnSel 
   = execLineCommand lnCmd (regexRangeSel lnSel) 
     . execRegexCommand (regexSel lnSel) 
     . takeRange (rangeSel lnSel)
 
+execLineCommand :: LineCommand -> Range -> [a] -> [a]
 execLineCommand PrintLines r = takeRange r
 execLineCommand DelLines r = dropRange r
 
 execRegexCommand Nothing = id
 
 execFieldCommands :: [FieldCommand] -> CharDelimiter -> T.Text -> T.Text
-execFieldCommands fcs fd text = T.intercalate (T.pack [fd]) (concatMap execFdCmd fcs)
+execFieldCommands fcs fd text = patchedJoin (concatMap execFdCmd fcs)
   where
-    fields = tokeniseText fd text
-    execFdCmd (PrintFields fieldRange) = takeRange fieldRange fields
+     fields = tokeniseText fd text
+     execFdCmd (PrintFields fieldRange) = takeRange fieldRange fields
+     join = T.intercalate (T.pack [fd])
+     -- Prepend the delimiter if necessary
+     patchedJoin fs = if length fs > 1 && not (T.null text) && T.head text == fd
+                        then T.cons fd (join fs)
+                        else join fs
 
 tokeniseText :: CharDelimiter -> T.Text -> [T.Text]
 tokeniseText delimiter = filterEmptyText . splitText