git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
extract Unicode replacement into PSString.decodeStringWithReplacement
commitd1a88d7e3159f6a57df89d25f0e3bf4295b8270a authorMichael Ficarra <git@michael.ficarra.me> dateFri 20 Jan 2017, 09:35 (10 years ago) parent3941bd1
2 files changed+12−2
src/Language/PureScript/Errors.hs+2−2 src/Language/PureScript/PSString.hs+10
src/Language/PureScript/Errors.hs+2−2
@@ -33,7 +33,7 @@ import Language.PureScript.Label (Label(..))
3333import Language.PureScript.Names
3434import Language.PureScript.Pretty
3535import Language.PureScript.Pretty.Common (endWith)
36−import Language.PureScript.PSString (PSString, decodeStringEither)
36+import Language.PureScript.PSString (PSString, decodeStringWithReplacement)
3737import Language.PureScript.Traversals
3838import Language.PureScript.Types
3939import qualified Language.PureScript.Publish.BoxesHelpers as BoxHelpers
@@ -1266,7 +1266,7 @@ renderBox = unlines
12661266 whiteSpace = all isSpace
12671267
12681268toTypelevelString :: Type -> Maybe Box.Box
1269−toTypelevelString t = (Box.text . map (either (const '\xFFFD') id) . decodeStringEither) <$> toTypelevelString' t
1269+toTypelevelString t = (Box.text . decodeStringWithReplacement) <$> toTypelevelString' t
12701270 where
12711271 toTypelevelString' :: Type -> Maybe PSString
12721272 toTypelevelString' (TypeLevelString s) = Just s
src/Language/PureScript/PSString.hs+10
@@ -5,6 +5,7 @@ module Language.PureScript.PSString
55 , toUTF16CodeUnits
66 , decodeString
77 , decodeStringEither
8+ , decodeStringWithReplacement
89 , prettyPrintString
910 , prettyPrintStringJS
1011 , mkString
@@ -52,15 +53,24 @@ newtype PSString = PSString { toUTF16CodeUnits :: [Word16] }
5253instance Show PSString where
5354 show = show . codePoints
5455
56+-- |
5557-- Decode a PSString to a String, representing any lone surrogates as the
5658-- reserved code point with that index. Warning: if there are any lone
5759-- surrogates, converting the result to Text via Data.Text.pack will result in
5860-- loss of information as those lone surrogates will be replaced with U+FFFD
5961-- REPLACEMENT CHARACTER. Because this function requires care to use correctly,
6062-- we do not export it.
63+--
6164codePoints :: PSString -> String
6265codePoints = map (either (chr . fromIntegral) id) . decodeStringEither
6366
67+-- |
68+-- Decode a PSString as UTF-16 text. Lone surrogates will be replaced with
69+-- U+FFFD REPLACEMENT CHARACTER
70+--
71+decodeStringWithReplacement :: PSString -> String
72+decodeStringWithReplacement = map (either (const '\xFFFD') id) . decodeStringEither
73+
6474-- |
6575-- Decode a PSString as UTF-16. Lone surrogates in the input are represented in
6676-- the output with the Left constructor; characters which were successfully