2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE DeriveGeneric #-}
5 -- | Compact representation of short 'Strings'
7 -- This module is designed to be import qualified
10 -- import Distribution.Utils.ShortText (ShortText)
11 -- import qualified Distribution.Utils.ShortText as ShortText
13 module Distribution
.Utils
.ShortText
14 ( -- * 'ShortText' type
24 -- * internal utilities
29 import Distribution
.Compat
.Prelude
hiding (length, null)
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
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
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
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
64 import Distribution
.Utils
.Generic
(fromUTF8BS
)
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'.
76 unsafeFromUTF8BS
:: BS
.ByteString
-> ShortText
78 -- | Text whether 'ShortText' is empty.
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
90 -- Note: This type is for internal uses (such as e.g. 'PackageName')
91 -- and shall not be exposed in Cabal's API
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
103 instance Binary ShortText
where
104 put
= put
. BS
.Short
.fromShort
. unST
105 get
= fmap (ST
. BS
.Short
.toShort
) get
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
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
128 unsafeFromUTF8BS
= ST
. fromUTF8BS
130 null = List
.null . unST
133 instance Structured ShortText
where structure
= nominalStructure
135 instance NFData ShortText
where
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
151 instance IsString ShortText
where
152 fromString
= toShortText
154 -- | /O(n)/. Length in characters. /Slow/ as converts to string.
157 length :: ShortText
-> Int
158 length = List
.length . fromShortText
160 -- Note: avoid using it, we use it @cabal check@ implementation, where it's ok.