From 08c687b4a3599553ab98dbb07fa829cecf57bbf2 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Tue, 26 Dec 2023 23:44:45 +0800 Subject: [PATCH] Add IO-free variants of some functions Fixes #4 --- System/OsString/Common.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/System/OsString/Common.hs b/System/OsString/Common.hs index ba9ab19..f585364 100644 --- a/System/OsString/Common.hs +++ b/System/OsString/Common.hs @@ -33,7 +33,13 @@ module System.OsString.MODULE_NAME , unsafeEncodeUtf , encodeWith , encodeFS +#ifdef WINDOWS + , fromString +#endif , fromBytes +#ifndef WINDOWS + , fromBytestring +#endif , pstr , singleton , empty @@ -254,6 +260,18 @@ encodeFS = fmap WindowsString . encodeWithBaseWindows encodeFS = fmap PosixString . encodeWithBasePosix #endif +#ifdef WINDOWS +-- | Like 'encodeFS', but not in IO. +-- +-- 'encodeFS' was designed to have a symmetric type signature +-- on unix and windows, but morally the function has no IO effects on windows, +-- so we provide this variant without breaking existing API. +-- +-- This function does not exist on unix. +fromString :: String -> WindowsString +fromString = unsafePerformIO . fmap WindowsString . encodeWithBaseWindows +#endif + #ifdef WINDOWS_DOC -- | Partial unicode friendly decoding. @@ -346,6 +364,18 @@ fromBytes bs = fromBytes = pure . PosixString . BSP.toShort #endif +#ifndef WINDOWS +-- | Like 'fromBytes', but not in IO. +-- +-- 'fromBytes' was designed to have a symmetric type signature +-- on unix and windows, but morally the function has no IO effects on unix, +-- so we provide this variant without breaking existing API. +-- +-- This function does not exist on windows. +fromBytestring :: ByteString -> PosixString +fromBytestring = PosixString . BSP.toShort +#endif + #ifdef WINDOWS_DOC -- | QuasiQuote a 'WindowsString'. This accepts Unicode characters