module BNFC.Backend.Common.StringUtils where

import BNFC.Prelude
    ( String, isLower, isUpper, toLower, toUpper, panic )


-- | Helper function that escapes characters in strings
-- >>> escapeChars "\\"
-- "\\\\"
-- >>> escapeChars "\""
-- "\\\""
-- >>> escapeChars "'"
-- "\\'"
escapeChars :: String -> String
escapeChars :: String -> String
escapeChars [] = []
escapeChars (Char
'\\':String
xs) = Char
'\\' Char -> String -> String
forall a. a -> [a] -> [a]
: Char
'\\' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
escapeChars String
xs
escapeChars (Char
'\"':String
xs) = Char
'\\' Char -> String -> String
forall a. a -> [a] -> [a]
: Char
'\"' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
escapeChars String
xs
escapeChars (Char
'\'':String
xs) = Char
'\\' Char -> String -> String
forall a. a -> [a] -> [a]
: Char
'\'' Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
escapeChars String
xs
escapeChars (Char
x:String
xs) = Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String -> String
escapeChars String
xs

fstCharUpper :: String -> String
fstCharUpper :: String -> String
fstCharUpper (Char
x:String
xs) =
  if Char -> Bool
isUpper Char
x then Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String
xs else Char -> Char
toUpper Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String
xs
fstCharUpper [] = String -> String
forall a. HasCallStack => String -> a
panic String
"Name string is empty"

fstCharLower :: String -> String
fstCharLower :: String -> String
fstCharLower (Char
x:String
xs) =
  if Char -> Bool
isLower Char
x then Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String
xs else Char -> Char
toLower Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String
xs
fstCharLower [] = String -> String
forall a. HasCallStack => String -> a
panic String
"Name string is empty"