make LTS branch pre-releases
[cabal.git] / Cabal-syntax / src / Distribution / Utils / ShortText.hs
blob0b128de96983953d68ad6e2d810d339331a38afb
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE DeriveGeneric #-}
5 -- | Compact representation of short 'Strings'
6 --
7 -- This module is designed to be import qualified
8 --
9 -- @
10 -- import Distribution.Utils.ShortText (ShortText)
11 -- import qualified Distribution.Utils.ShortText as ShortText
12 -- @
13 module Distribution.Utils.ShortText
14 ( -- * 'ShortText' type
15 ShortText
16 , toShortText
17 , fromShortText
18 , unsafeFromUTF8BS
20 -- * Operations
21 , null
22 , length
24 -- * internal utilities
25 , decodeStringUtf8
26 , encodeStringUtf8
27 ) where
29 import Distribution.Compat.Prelude hiding (length, null)
30 import Prelude ()
32 import Distribution.Utils.String (decodeStringUtf8, encodeStringUtf8)
33 import Distribution.Utils.Structured (Structured (..), nominalStructure)
35 #if defined(MIN_VERSION_bytestring)
36 # if MIN_VERSION_bytestring(0,10,4)
37 # define HAVE_SHORTBYTESTRING 1
38 # endif
39 #endif
41 -- Hack for GHC bootstrapping
43 -- Currently (as of GHC 8.1), GHC bootstraps Cabal by building
44 -- binary and Cabal in one giant ghc --make command. This
45 -- means no MIN_VERSION_binary macro is available.
47 -- We could try to cleverly figure something out in this case,
48 -- but there is a better plan: just use the unoptimized version
49 -- of the Binary instance. We're not going to use it for anything
50 -- real in any case.
52 -- WARNING: Don't use MIN_VERSION_binary to smooth over a BC-break!
54 #ifndef MIN_VERSION_binary
55 #define MIN_VERSION_binary(x, y, z) 0
56 #endif
58 import qualified Data.ByteString as BS
59 import qualified Data.List as List
61 #if HAVE_SHORTBYTESTRING
62 import qualified Data.ByteString.Short as BS.Short
63 #else
64 import Distribution.Utils.Generic (fromUTF8BS)
65 #endif
67 -- | Construct 'ShortText' from 'String'
68 toShortText :: String -> ShortText
70 -- | Convert 'ShortText' to 'String'
71 fromShortText :: ShortText -> String
73 -- | Convert from UTF-8 encoded strict 'ByteString'.
75 -- @since 3.2.0.0
76 unsafeFromUTF8BS :: BS.ByteString -> ShortText
78 -- | Text whether 'ShortText' is empty.
80 -- @since 3.2.0.0
81 null :: ShortText -> Bool
83 -- | Compact representation of short 'Strings'
85 -- The data is stored internally as UTF8 in an
86 -- 'BS.Short.ShortByteString' when compiled against @bytestring >=
87 -- 0.10.4@, and otherwise the fallback is to use plain old non-compat
88 -- '[Char]'.
90 -- Note: This type is for internal uses (such as e.g. 'PackageName')
91 -- and shall not be exposed in Cabal's API
93 -- @since 2.0.0.2
94 #if HAVE_SHORTBYTESTRING
95 newtype ShortText = ST { unST :: BS.Short.ShortByteString }
96 deriving (Eq,Ord,Generic,Data,Typeable)
98 # if MIN_VERSION_binary(0,8,1)
99 instance Binary ShortText where
100 put = put . unST
101 get = fmap ST get
102 # else
103 instance Binary ShortText where
104 put = put . BS.Short.fromShort . unST
105 get = fmap (ST . BS.Short.toShort) get
106 # endif
109 toShortText = ST . BS.Short.pack . encodeStringUtf8
111 fromShortText = decodeStringUtf8 . BS.Short.unpack . unST
113 unsafeFromUTF8BS = ST . BS.Short.toShort
115 null = BS.Short.null . unST
116 #else
117 newtype ShortText = ST { unST :: String }
118 deriving (Eq,Ord,Generic,Data,Typeable)
120 instance Binary ShortText where
121 put = put . encodeStringUtf8 . unST
122 get = fmap (ST . decodeStringUtf8) get
124 toShortText = ST
126 fromShortText = unST
128 unsafeFromUTF8BS = ST . fromUTF8BS
130 null = List.null . unST
131 #endif
133 instance Structured ShortText where structure = nominalStructure
135 instance NFData ShortText where
136 rnf = rnf . unST
138 instance Show ShortText where
139 show = show . fromShortText
141 instance Read ShortText where
142 readsPrec p = map (first toShortText) . readsPrec p
144 instance Semigroup ShortText where
145 ST a <> ST b = ST (mappend a b)
147 instance Monoid ShortText where
148 mempty = ST mempty
149 mappend = (<>)
151 instance IsString ShortText where
152 fromString = toShortText
154 -- | /O(n)/. Length in characters. /Slow/ as converts to string.
156 -- @since 3.2.0.0
157 length :: ShortText -> Int
158 length = List.length . fromShortText
160 -- Note: avoid using it, we use it @cabal check@ implementation, where it's ok.