Skip to content

Commit a03a3b5

Browse files
committed
FIX: Munge - a little bit more optimizations
1 parent 7d8c108 commit a03a3b5

File tree

1 file changed

+8
-114
lines changed

1 file changed

+8
-114
lines changed

src/modules/munge3.reb

+8-114
Original file line numberDiff line numberDiff line change
@@ -177,30 +177,13 @@ ctx-munge: context [
177177
"Compress block of file and data pairs"
178178
source [series!]
179179
] compose/deep [
180-
to-short: function [i] [(
181-
either settings/build = 'r2 [[
182-
reverse next next load make string! reduce ["#{" to-hex i "}"]
183-
]] [[
184-
copy/part reverse to binary! i 2
185-
]]
186-
)]
187-
188-
to-long: function [i] [(
189-
either settings/build = 'r2 [[
190-
reverse load make string! reduce ["#{" to-hex i "}"]
191-
]] [[
192-
copy/part reverse to binary! i 4
193-
]]
194-
)]
180+
to-short: function [i] [copy/part reverse to binary! i 2]
181+
to-long: function [i] [copy/part reverse to binary! i 4]
195182

196183
case [
197184
empty? source [none]
198185
not block? source [
199-
join #{1F8B08000000000002FF} next next head either settings/build = 'r2 [
200-
change skip tail compress source -8 to-long crc32 source
201-
] [
202-
reverse/part skip tail compress/gzip source -8 4
203-
]
186+
compress/gzip source
204187
]
205188
true [
206189
bin: copy #{}
@@ -377,7 +360,7 @@ ctx-munge: context [
377360
"Remove leading zeroes from string"
378361
string [string!]
379362
] [
380-
while [string/1 = #"0"] [remove string]
363+
parse string [remove some #"0"]
381364
string
382365
]
383366

@@ -1121,38 +1104,9 @@ ctx-munge: context [
11211104
source [file! url! binary!]
11221105
] compose/deep [
11231106
all [settings/console settings/called/file 'read-string source]
1124-
; (either settings/build = 'r2 [[any [binary? source source: read/binary/direct source]]] [])
1125-
i: 0
1126-
also either binary? source [
1127-
s: make string! length: length? source
1128-
while [i < length] [
1129-
append s latin1-to-utf8 copy/part skip source i 262144
1130-
i: i + 262144
1131-
]
1132-
s
1133-
] [
1134-
(switch settings/build [
1135-
r2 [[
1136-
replace/all trim/with read source null to char! 160 #" "
1137-
]]
1138-
r3 [[
1139-
s: make string! size: size? source
1140-
while [i < size] [
1141-
append s latin1-to-utf8 read/seek/part source i 262144
1142-
i: i + 262144
1143-
]
1144-
s
1145-
]]
1146-
red [[
1147-
s: make string! size: size? source
1148-
while [i < size] [
1149-
append s latin1-to-utf8 read/binary/seek/part source i 262144
1150-
i: i + 262144
1151-
]
1152-
s
1153-
]]
1154-
])
1155-
] all [settings/console settings/exited]
1107+
any [binary? source source: read source]
1108+
also to string! either invalid-utf? source [iconv/to source 'latin1 'utf8][source]
1109+
all [settings/console settings/exited]
11561110
]
11571111

11581112
replace-deep: function [
@@ -1413,62 +1367,6 @@ ctx-munge: context [
14131367
]
14141368
]
14151369

1416-
if settings/build = 'r2 [
1417-
deflate: function [
1418-
"Decompresses a gzip encoding"
1419-
data [binary!]
1420-
][
1421-
any [view? cause-error 'user 'message ["Requires /View"]]
1422-
1423-
set?: func [value bit][not zero? value and to-integer 2 ** bit]
1424-
1425-
any [#{1F8B08} = copy/part data 3 cause-error 'user 'message ["Bad ID or Unknown Method"]]
1426-
1427-
flags: data/4
1428-
1429-
data: skip data 10
1430-
1431-
all [set? flags 1 data: skip data 2] ; crc-16?
1432-
all [set? flags 2 data: skip data 2 data: skip data data/2 * 256 + data/1 + 2] ; extra?
1433-
all [set? flags 3 data: find/tail data #"^@"] ; name?
1434-
1435-
size: to integer! head reverse copy skip tail data -4
1436-
1437-
data: copy/part data skip tail data -8
1438-
1439-
data: load rejoin [
1440-
#{89504E47} #{0D0A1A0A} ; signature
1441-
#{0000000D} ; IHDR length
1442-
"IHDR" ; type: header
1443-
load make string! reduce ["#{" to-hex size "}"] ; width = uncompressed size
1444-
#{00000001} ; height = 1 line
1445-
#{08} ; bit depth
1446-
#{00} ; color type = grayscale
1447-
#{00} ; compression method
1448-
#{00} ; filter method = none
1449-
#{00} ; interlace method = no interlace
1450-
#{00000000} ; no checksum
1451-
load make string! reduce ["#{" to-hex 8 + length? data "}"] ; length
1452-
"IDAT" ; type: data
1453-
#{789C} ; zlib header
1454-
#{00 0100 FEFF 00} ; 0 = no filter for scanline
1455-
data
1456-
#{00000000} ; no checksum
1457-
#{00000000} ; length
1458-
"IEND" ; type: end
1459-
#{00000000} ; no checksum
1460-
]
1461-
1462-
bin: make binary! size
1463-
1464-
repeat i size [
1465-
insert tail bin to char! pick pick data i 1
1466-
]
1467-
1468-
bin
1469-
]
1470-
]
1471-
14721370
unarchive: function [
14731371
"Decompresses archive (only works with compression methods 'store and 'deflate)"
14741372
source [file! url! binary!]
@@ -1485,11 +1383,7 @@ ctx-munge: context [
14851383
either info [
14861384
to integer! reverse skip tail copy source -4
14871385
] [
1488-
switch settings/build [
1489-
r2 [deflate source]
1490-
r3 [decompress/gzip join #{789C} skip head reverse/part skip tail copy source -8 4 10]
1491-
red [decompress source]
1492-
]
1386+
decompress/gzip source
14931387
]
14941388
]
14951389
#{504B0304} <> copy/part source 4 [

0 commit comments

Comments
 (0)