Skip to content

Commit

Permalink
Remove IO from String
Browse files Browse the repository at this point in the history
  • Loading branch information
reinerp committed Mar 27, 2012
1 parent 0c79d4f commit dd6dded
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CoreFoundation/System/CoreFoundation/Preferences.chs
Expand Up @@ -59,7 +59,7 @@ instance Preference Number where
-- TODO: CFDate and CFBoolean also

instance Preference Prelude.String where
toPreference p = toPreference p `thenMaybe` (fmap Just . getChars)
toPreference p = toPreference p `thenMaybe` (return . Just . getChars)

instance Preference Int8 where
toPreference = fmap (fmap value) . toPreference
Expand Down
25 changes: 17 additions & 8 deletions CoreFoundation/System/CoreFoundation/String.chs
Expand Up @@ -3,9 +3,10 @@ module System.CoreFoundation.String(
String,
StringRef,
-- * Conversion to/from 'Data'
newExternalRepresentation,
newStringFromExternalRepresentation,
encode,
decode,
StringEncoding(..),
ReplacementChar,
-- * Conversion to/from 'Prelude.String'
fromChars,
getChars,
Expand Down Expand Up @@ -77,20 +78,27 @@ import Language.Haskell.TH
`Bool'
} -> `StringRef' id #}

{#fun unsafe CFStringCreateExternalRepresentation as newExternalRepresentation
-- | Replacement character to use on invalid input
type ReplacementChar = Word8

{#fun unsafe CFStringCreateExternalRepresentation as c_newExternalRepresentation
{ withDefaultAllocator- `AllocatorPtr',
withObject* `String',
cvtEnum `StringEncoding',
cvtEnum `Word8'
} -> `DataRef' id #}

{#fun unsafe CFStringCreateFromExternalRepresentation as newStringFromExternalRepresentation
encode :: String -> StringEncoding -> ReplacementChar -> Data
encode str enc rep = unsafePerformIO $ getOwned $ c_newExternalRepresentation str enc rep

{#fun unsafe CFStringCreateFromExternalRepresentation as c_newStringFromExternalRepresentation
{ withDefaultAllocator- `AllocatorPtr',
withObject* `Data',
cvtEnum `StringEncoding'
} -> `StringRef' id #}


decode :: Data -> StringEncoding -> String
decode dat enc = unsafePerformIO $ getOwned $ c_newStringFromExternalRepresentation dat enc

-- | Create a new immutable @CoreFoundation.String@ which contains a copy of the given 'Text'.
fromText :: Text.Text -> String
Expand All @@ -104,8 +112,9 @@ fromChars :: Prelude.String -> String
fromChars = fromText . Text.pack

-- | Extract a 'Text' copy of the given @CoreFoundation.String@.
getText :: String -> IO Text.Text
getText :: String -> Text.Text
getText str =
unsafePerformIO $
withObject str $ \str_p -> do
len <- {#call unsafe CFStringGetLength as ^ #} str_p
ptr <- {#call unsafe CFStringGetCharactersPtr as ^ #} str_p
Expand All @@ -116,7 +125,7 @@ getText str =
fromPtr (castPtr out_ptr) (fromIntegral len)

-- | Extract a 'Prelude.String' copy of the given @CoreFoundation.String@.
getChars :: String -> IO Prelude.String
getChars = fmap Text.unpack . getText
getChars :: String -> Prelude.String
getChars = Text.unpack . getText


0 comments on commit dd6dded

Please sign in to comment.