{-# LANGUAGE RecordWildCards #-}
module System.Console.CmdArgs.Explicit.SplitJoin(splitArgs, joinArgs) where
import Data.Char
import Data.Maybe
joinArgs :: [String] -> String
joinArgs :: [String] -> String
joinArgs = [String] -> String
unwords forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map String -> String
f
where
f :: String -> String
f String
x = String
q forall a. [a] -> [a] -> [a]
++ String -> String
g String
x forall a. [a] -> [a] -> [a]
++ String
q
where
hasSpace :: Bool
hasSpace = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Char -> Bool
isSpace String
x
q :: String
q = [Char
'\"' | Bool
hasSpace Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
x]
g :: String -> String
g (Char
'\\':Char
'\"':String
xs) = Char
'\\'forall a. a -> [a] -> [a]
:Char
'\\'forall a. a -> [a] -> [a]
:Char
'\\'forall a. a -> [a] -> [a]
:Char
'\"'forall a. a -> [a] -> [a]
: String -> String
g String
xs
g String
"\\" | Bool
hasSpace = String
"\\\\"
g (Char
'\"':String
xs) = Char
'\\'forall a. a -> [a] -> [a]
:Char
'\"'forall a. a -> [a] -> [a]
: String -> String
g String
xs
g (Char
x:String
xs) = Char
x forall a. a -> [a] -> [a]
: String -> String
g String
xs
g [] = []
data State = Init
| Norm
| Quot
splitArgs :: String -> [String]
splitArgs :: String -> [String]
splitArgs = [Maybe Char] -> [String]
join forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> String -> [Maybe Char]
f State
Init
where
join :: [Maybe Char] -> [String]
join :: [Maybe Char] -> [String]
join [] = []
join [Maybe Char]
xs = forall a b. (a -> b) -> [a] -> [b]
map forall a. HasCallStack => Maybe a -> a
fromJust [Maybe Char]
a forall a. a -> [a] -> [a]
: [Maybe Char] -> [String]
join (forall a. Int -> [a] -> [a]
drop Int
1 [Maybe Char]
b)
where ([Maybe Char]
a,[Maybe Char]
b) = forall a. (a -> Bool) -> [a] -> ([a], [a])
break forall a. Maybe a -> Bool
isNothing [Maybe Char]
xs
f :: State -> String -> [Maybe Char]
f State
Init (Char
x:String
xs) | Char -> Bool
isSpace Char
x = State -> String -> [Maybe Char]
f State
Init String
xs
f State
Init String
"\"\"" = [forall a. Maybe a
Nothing]
f State
Init String
"\"" = [forall a. Maybe a
Nothing]
f State
Init String
xs = State -> String -> [Maybe Char]
f State
Norm String
xs
f State
m (Char
'\"':Char
'\"':Char
'\"':String
xs) = forall a. a -> Maybe a
Just Char
'\"' forall a. a -> [a] -> [a]
: State -> String -> [Maybe Char]
f State
m String
xs
f State
m (Char
'\\':Char
'\"':String
xs) = forall a. a -> Maybe a
Just Char
'\"' forall a. a -> [a] -> [a]
: State -> String -> [Maybe Char]
f State
m String
xs
f State
m (Char
'\\':Char
'\\':Char
'\"':String
xs) = forall a. a -> Maybe a
Just Char
'\\' forall a. a -> [a] -> [a]
: State -> String -> [Maybe Char]
f State
m (Char
'\"'forall a. a -> [a] -> [a]
:String
xs)
f State
Norm (Char
'\"':String
xs) = State -> String -> [Maybe Char]
f State
Quot String
xs
f State
Quot (Char
'\"':Char
'\"':String
xs) = forall a. a -> Maybe a
Just Char
'\"' forall a. a -> [a] -> [a]
: State -> String -> [Maybe Char]
f State
Norm String
xs
f State
Quot (Char
'\"':String
xs) = State -> String -> [Maybe Char]
f State
Norm String
xs
f State
Norm (Char
x:String
xs) | Char -> Bool
isSpace Char
x = forall a. Maybe a
Nothing forall a. a -> [a] -> [a]
: State -> String -> [Maybe Char]
f State
Init String
xs
f State
m (Char
x:String
xs) = forall a. a -> Maybe a
Just Char
x forall a. a -> [a] -> [a]
: State -> String -> [Maybe Char]
f State
m String
xs
f State
m [] = []