@@ -29,7 +29,7 @@ import Language.PureScript.Comments
2929import Language.PureScript.Crash
3030import Language.PureScript.Names (Ident)
3131import Language.PureScript.Pretty.Common
32−import Language.PureScript.PSString (PSString, decodeString, mkString, prettyPrintString)
32+import Language.PureScript.PSString (PSString, decodeString, mkString)
3333import CodeGen.Cpp.Common
3434
3535import qualified Language.PureScript.Constants as C
@@ -45,13 +45,9 @@ literals = mkPattern' match'
4545 match :: (Emit gen) => AST -> StateT PrinterState Maybe gen
4646 match (NumericLiteral _ n) = return $ emit $ T.pack $ either show show n
4747 match (StringLiteral _ s)
48− | Just s' <- decodeString s = return $ emit $
49− if T.all isAscii s'
50− then stringLiteral s'
51− else "u8" <> stringLiteral s'
52− match (StringLiteral _ s)
53− | Just s' <- decodeString s = return $ emit $ "u8" <> stringLiteral s'
54− match (StringLiteral _ s) = error $ "Bad character in string literal " ++ show s
48+ | Just s' <- decodeString s
49+ , T.all isAscii s' = return $ emit $ stringLiteral s
50+ match (StringLiteral _ s) = return $ emit $ "u8" <> stringLiteral s
5551 match (BooleanLiteral _ True) = return $ emit "true"
5652 match (BooleanLiteral _ False) = return $ emit "false"
5753 -- match (ArrayLiteral _ []) = return $ emit "std::initializer_list<boxed>{}"
@@ -75,7 +71,7 @@ literals = mkPattern' match'
7571 ]
7672 where
7773 objectPropertyToString :: (Emit gen) => PSString -> gen
78− objectPropertyToString s = emit $ prettyPrintString s <> ", "
74+ objectPropertyToString s = emit $ stringLiteral s <> ", "
7975 match (Block _ sts) = mconcat <$> sequence
8076 [ return $ emit "{\n"
8177 , withIndent $ prettyStatements sts
@@ -381,8 +377,21 @@ prettyPrintCpp' = A.runKleisli $ runPattern matchValue
381377prettyPrintCpp1 :: AST -> Text
382378prettyPrintCpp1 = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyPrintCpp'
383379
384−stringLiteral :: Text -> Text
385−stringLiteral s = "\"" <> (T.replace "\\" "\\\\" s) <> "\""
380+stringLiteral :: PSString -> Text
381+stringLiteral pss | Just s <- decodeString pss = stringLiteral' s
382+ where
383+ stringLiteral' :: Text -> Text
384+ stringLiteral' s = "\"" <> T.concatMap encodeChar s <> "\""
385+ encodeChar :: Char -> Text
386+ encodeChar '\b' = "\\b"
387+ encodeChar '\t' = "\\t"
388+ encodeChar '\n' = "\\n"
389+ encodeChar '\v' = "\\v"
390+ encodeChar '\f' = "\\f"
391+ encodeChar '\r' = "\\r"
392+ encodeChar '"' = "\\\""
393+ encodeChar '\\' = "\\\\"
394+ encodeChar c = T.singleton $ c
386395
387396unbox :: Text -> AST -> Text
388397unbox _ v@(NumericLiteral{}) = prettyPrintCpp1 v