@@ -152,18 +152,13 @@ module Distribution.Simple.Utils (
152
152
-- * FilePath stuff
153
153
isAbsoluteOnAnyPlatform ,
154
154
isRelativeOnAnyPlatform ,
155
-
156
- -- * 'ShortText' type
157
- ShortText ,
158
- toShortText ,
159
- fromShortText ,
160
155
) where
161
156
162
157
import Prelude ()
163
158
import Distribution.Compat.Prelude
164
- import Data.String (IsString (.. ))
165
159
166
160
import Distribution.Text
161
+ import Distribution.Utils.String
167
162
import Distribution.Package
168
163
import Distribution.ModuleName as ModuleName
169
164
import Distribution.System
@@ -188,7 +183,6 @@ import Distribution.Verbosity
188
183
import qualified Paths_Cabal (version )
189
184
#endif
190
185
191
- import Data.Word (Word8 )
192
186
import Control.Concurrent.MVar
193
187
( newEmptyMVar , putMVar , takeMVar )
194
188
import Data.Bits
@@ -205,16 +199,6 @@ import qualified Data.Set as Set
205
199
206
200
import qualified Data.ByteString as SBS
207
201
208
- #if defined(MIN_VERSION_bytestring)
209
- # if MIN_VERSION_bytestring(0,10,4)
210
- # define HAVE_SHORTBYTESTRING 1
211
- # endif
212
- #endif
213
-
214
- #if HAVE_SHORTBYTESTRING
215
- import qualified Data.ByteString.Short as BS.Short
216
- #endif
217
-
218
202
import System.Directory
219
203
( Permissions (executable ), getDirectoryContents , getPermissions
220
204
, doesDirectoryExist , doesFileExist , removeFile , findExecutable
@@ -1390,55 +1374,10 @@ fromUTF8 (c:cs)
1390
1374
replacementChar = ' \xfffd '
1391
1375
1392
1376
fromUTF8BS :: SBS. ByteString -> String
1393
- fromUTF8BS = fromUTF8BSImpl . SBS. unpack
1377
+ fromUTF8BS = decodeStringUtf8 . SBS. unpack
1394
1378
1395
1379
fromUTF8LBS :: BS. ByteString -> String
1396
- fromUTF8LBS = fromUTF8BSImpl . BS. unpack
1397
-
1398
- fromUTF8BSImpl :: [Word8 ] -> String
1399
- fromUTF8BSImpl = go
1400
- where
1401
- go :: [Word8 ] -> String
1402
- go [] = []
1403
- go (c : cs)
1404
- | c <= 0x7F = chr (fromIntegral c) : go cs
1405
- | c <= 0xBF = replacementChar : go cs
1406
- | c <= 0xDF = twoBytes c cs
1407
- | c <= 0xEF = moreBytes 3 0x800 cs (fromIntegral $ c .&. 0xF )
1408
- | c <= 0xF7 = moreBytes 4 0x10000 cs (fromIntegral $ c .&. 0x7 )
1409
- | c <= 0xFB = moreBytes 5 0x200000 cs (fromIntegral $ c .&. 0x3 )
1410
- | c <= 0xFD = moreBytes 6 0x4000000 cs (fromIntegral $ c .&. 0x1 )
1411
- | otherwise = replacementChar : go cs
1412
-
1413
- twoBytes :: Word8 -> [Word8 ] -> String
1414
- twoBytes c0 (c1: cs')
1415
- | c1 .&. 0xC0 == 0x80
1416
- = let d = ((c0 .&. 0x1F ) `shiftL` 6 )
1417
- .|. (c1 .&. 0x3F )
1418
- in if d >= 0x80
1419
- then chr (fromIntegral d) : go cs'
1420
- else replacementChar : go cs'
1421
- twoBytes _ cs' = replacementChar : go cs'
1422
-
1423
- moreBytes :: Int -> Int -> [Word8 ] -> Int -> [Char ]
1424
- moreBytes 1 overlong cs' acc
1425
- | overlong <= acc && acc <= 0x10FFFF
1426
- && (acc < 0xD800 || 0xDFFF < acc)
1427
- && (acc < 0xFFFE || 0xFFFF < acc)
1428
- = chr acc : go cs'
1429
-
1430
- | otherwise
1431
- = replacementChar : go cs'
1432
-
1433
- moreBytes byteCount overlong (cn: cs') acc
1434
- | cn .&. 0xC0 == 0x80
1435
- = moreBytes (byteCount- 1 ) overlong cs'
1436
- ((acc `shiftL` 6 ) .|. fromIntegral cn .&. 0x3F )
1437
-
1438
- moreBytes _ _ cs' _
1439
- = replacementChar : go cs'
1440
-
1441
- replacementChar = ' \xfffd '
1380
+ fromUTF8LBS = decodeStringUtf8 . BS. unpack
1442
1381
1443
1382
toUTF8 :: String -> String
1444
1383
toUTF8 [] = []
@@ -1459,26 +1398,6 @@ toUTF8 (c:cs)
1459
1398
: toUTF8 cs
1460
1399
where w = ord c
1461
1400
1462
- -- | Variant of 'toUTF8' operating on 'Word8's directly
1463
- toUTF8BSImpl :: String -> [Word8 ]
1464
- toUTF8BSImpl [] = []
1465
- toUTF8BSImpl (c: cs)
1466
- | c <= ' \x07F ' = w
1467
- : toUTF8BSImpl cs
1468
- | c <= ' \x7FF ' = (0xC0 .|. (w `shiftR` 6 ))
1469
- : (0x80 .|. (w .&. 0x3F ))
1470
- : toUTF8BSImpl cs
1471
- | c <= ' \xFFFF ' = (0xE0 .|. (w `shiftR` 12 ))
1472
- : (0x80 .|. ((w `shiftR` 6 ) .&. 0x3F ))
1473
- : (0x80 .|. (w .&. 0x3F ))
1474
- : toUTF8BSImpl cs
1475
- | otherwise = (0xf0 .|. (w `shiftR` 18 ))
1476
- : (0x80 .|. ((w `shiftR` 12 ) .&. 0x3F ))
1477
- : (0x80 .|. ((w `shiftR` 6 ) .&. 0x3F ))
1478
- : (0x80 .|. (w .&. 0x3F ))
1479
- : toUTF8BSImpl cs
1480
- where w = fromIntegral (ord c) :: Word8
1481
-
1482
1401
-- | Whether BOM is at the beginning of the input
1483
1402
startsWithBOM :: String -> Bool
1484
1403
startsWithBOM (' \xFEFF ' : _) = True
@@ -1519,7 +1438,7 @@ withUTF8FileContents name action =
1519
1438
-- Uses 'writeFileAtomic', so provides the same guarantees.
1520
1439
--
1521
1440
writeUTF8File :: FilePath -> String -> NoCallStackIO ()
1522
- writeUTF8File path = writeFileAtomic path . BS.Char8. pack . toUTF8
1441
+ writeUTF8File path = writeFileAtomic path . BS. pack . encodeStringUtf8
1523
1442
1524
1443
-- | Fix different systems silly line ending conventions
1525
1444
normaliseLineEndings :: String -> String
@@ -1664,78 +1583,3 @@ isAbsoluteOnAnyPlatform _ = False
1664
1583
-- | @isRelativeOnAnyPlatform = not . 'isAbsoluteOnAnyPlatform'@
1665
1584
isRelativeOnAnyPlatform :: FilePath -> Bool
1666
1585
isRelativeOnAnyPlatform = not . isAbsoluteOnAnyPlatform
1667
-
1668
- -- ------------------------------------------------------------
1669
- -- * 'ShortText' type
1670
- -- ------------------------------------------------------------
1671
-
1672
- -- TODO: if we start using this internally for more opaque types in
1673
- -- Cabal then we will likely need to promote it to it's own module in
1674
- -- Distribution.* to avoid cycles, or just to maintain the sanity of
1675
- -- the Distribution.* vs Distribution.Simple.* distinction.
1676
-
1677
- -- | Construct 'ShortText' from 'String'
1678
- toShortText :: String -> ShortText
1679
-
1680
- -- | Convert 'ShortText' to 'String'
1681
- fromShortText :: ShortText -> String
1682
-
1683
- -- | Compact representation of short 'Strings'
1684
- --
1685
- -- The data is stored internally as UTF8 in an
1686
- -- 'BS.Short.ShortByteString' when compiled against @bytestring >=
1687
- -- 0.10.4@, and otherwise the fallback is to use plain old non-compat
1688
- -- '[Char]'.
1689
- --
1690
- -- Note: This type is for internal uses (such as e.g. 'PackageName')
1691
- -- and shall not be exposed in Cabal's API
1692
- --
1693
- -- @since 2.0.0
1694
- #if HAVE_SHORTBYTESTRING
1695
- newtype ShortText = ST { unST :: BS.Short. ShortByteString }
1696
- deriving (Eq ,Ord ,Generic )
1697
-
1698
- # if MIN_VERSION_binary(0,8,1)
1699
- instance Binary ShortText where
1700
- put = put . unST
1701
- get = fmap ST get
1702
- # else
1703
- instance Binary ShortText where
1704
- put = put . BS.Short. fromShort . unST
1705
- get = fmap (ST . BS.Short. toShort) get
1706
- # endif
1707
-
1708
- toShortText = ST . BS.Short. pack . toUTF8BSImpl
1709
-
1710
- fromShortText = fromUTF8BSImpl . BS.Short. unpack . unST
1711
- #else
1712
- newtype ShortText = ST { unST :: String }
1713
- deriving (Eq ,Ord ,Generic )
1714
-
1715
- instance Binary ShortText where
1716
- put = put . toUTF8BSImpl . unST
1717
- get = fmap (ST . fromUTF8BSImpl) get
1718
-
1719
- toShortText = ST
1720
-
1721
- fromShortText = unST
1722
- #endif
1723
-
1724
- instance NFData ShortText where
1725
- rnf = rnf . unST
1726
-
1727
- instance Show ShortText where
1728
- show = show . fromShortText
1729
-
1730
- instance Read ShortText where
1731
- readsPrec p = map (first toShortText) . readsPrec p
1732
-
1733
- instance Semigroup ShortText where
1734
- ST a <> ST b = ST (mappend a b)
1735
-
1736
- instance Monoid ShortText where
1737
- mempty = ST mempty
1738
- mappend = (<>)
1739
-
1740
- instance IsString ShortText where
1741
- fromString = toShortText
0 commit comments