Skip to content

Commit e0641c2

Browse files
hasufellBodigrim
authored andcommitted
Reduce the use of Char8 module
1 parent 7321307 commit e0641c2

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

Codec/Archive/Tar/Write.hs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ putHeader entry =
6464
LBS.fromStrict
6565
$ BS.take 148 block
6666
<> putOct 7 checksum
67-
<> BS.Char8.cons ' ' (BS.drop 156 block)
67+
<> BS.cons 0x20 (BS.drop 156 block)
6868
where
6969
block = putHeaderNoChkSum entry
70-
checksum = BS.Char8.foldl' (\x y -> x + ord y) 0 block
70+
checksum :: Int
71+
checksum = BS.foldl' (\x y -> x + fromIntegral y) 0 block
7172

7273
putHeaderNoChkSum :: Entry -> BS.ByteString
7374
putHeaderNoChkSum Entry {
@@ -86,33 +87,33 @@ putHeaderNoChkSum Entry {
8687
, putOct 8 $ groupId ownership
8788
, numField 12 contentSize
8889
, putOct 12 modTime
89-
, BS.Char8.replicate 8 ' ' -- dummy checksum
90+
, BS.replicate 8 0x20 -- dummy checksum
9091
, putChar8 typeCode
9192
, putPosixString 100 linkTarget
9293
] <>
9394
case format of
9495
V7Format ->
95-
BS.Char8.replicate 255 '\NUL'
96-
UstarFormat -> BS.Char8.concat
96+
BS.replicate 255 0x00
97+
UstarFormat -> BS.concat
9798
[ putBString 8 ustarMagic
9899
, putString 32 $ ownerName ownership
99100
, putString 32 $ groupName ownership
100101
, putOct 8 deviceMajor
101102
, putOct 8 deviceMinor
102103
, putPosixString 155 prefix
103-
, BS.Char8.replicate 12 '\NUL'
104+
, BS.replicate 12 0x00
104105
]
105-
GnuFormat -> BS.Char8.concat
106+
GnuFormat -> BS.concat
106107
[ putBString 8 gnuMagic
107108
, putString 32 $ ownerName ownership
108109
, putString 32 $ groupName ownership
109110
, putGnuDev 8 deviceMajor
110111
, putGnuDev 8 deviceMinor
111112
, putPosixString 155 prefix
112-
, BS.Char8.replicate 12 '\NUL'
113+
, BS.replicate 12 0x00
113114
]
114115
where
115-
numField :: FieldWidth -> Int64 -> BS.Char8.ByteString
116+
numField :: FieldWidth -> Int64 -> BS.ByteString
116117
numField w n
117118
| n >= 0 && n < 1 `shiftL` (3 * (w - 1))
118119
= putOct w n
@@ -133,24 +134,24 @@ putHeaderNoChkSum Entry {
133134
putGnuDev w n = case content of
134135
CharacterDevice _ _ -> putOct w n
135136
BlockDevice _ _ -> putOct w n
136-
_ -> BS.Char8.replicate w '\NUL'
137+
_ -> BS.replicate w 0x00
137138

138139
ustarMagic, gnuMagic :: BS.ByteString
139-
ustarMagic = BS.Char8.pack "ustar\NUL00"
140-
gnuMagic = BS.Char8.pack "ustar \NUL"
140+
ustarMagic = BS.pack [0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30] -- ustar\NUL00
141+
gnuMagic = BS.pack [0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00] -- ustar \NUL
141142

142143
-- * TAR format primitive output
143144

144145
type FieldWidth = Int
145146

146147
putBString :: FieldWidth -> BS.ByteString -> BS.ByteString
147-
putBString n s = BS.take n s <> BS.Char8.replicate (n - BS.length s) '\NUL'
148+
putBString n s = BS.take n s <> BS.replicate (n - BS.length s) 0x00
148149

149150
putPosixString :: FieldWidth -> PosixString -> BS.ByteString
150-
putPosixString n s = posixToByteString (PS.take n s) <> BS.Char8.replicate (n - PS.length s) '\NUL'
151+
putPosixString n s = posixToByteString (PS.take n s) <> BS.replicate (n - PS.length s) 0x00
151152

152153
putString :: FieldWidth -> String -> BS.ByteString
153-
putString n s = BS.take n (packAscii s) <> BS.Char8.replicate (n - length s) '\NUL'
154+
putString n s = BS.take n (packAscii s) <> BS.replicate (n - length s) 0x00
154155

155156
{-# SPECIALISE putLarge :: FieldWidth -> Int64 -> BS.ByteString #-}
156157
putLarge :: (Bits a, Integral a) => FieldWidth -> a -> BS.ByteString
@@ -161,9 +162,9 @@ putLarge n0 x0 = BS.Char8.pack $ '\x80' : reverse (go (n0-1) x0)
161162
putOct :: (Integral a, Show a) => FieldWidth -> a -> BS.ByteString
162163
putOct n x =
163164
let octStr = BS.take (n-1) $ BS.Char8.pack $ showOct x ""
164-
in BS.Char8.replicate (n - BS.length octStr - 1) '0'
165+
in BS.replicate (n - BS.length octStr - 1) 0x30
165166
<> octStr
166-
<> putChar8 '\NUL'
167+
<> BS.singleton 0x00
167168

168169
putChar8 :: Char -> BS.ByteString
169170
putChar8 = BS.Char8.singleton

0 commit comments

Comments
 (0)