Skip to content

Commit ad288fb

Browse files
committed
Make list compression ratio more pessimistic
With the recently added h2_huge.lzh, I noticed that the compressed file was listed as being 0.0% of the original size, since the compression ratio is so high. This happens because we were using the regular %f printf formatting, which presumably rounds to the nearest 0.1%. However, it nonetheless seems rather misleading. This instead changes the output to always round upwards to the next 0.1%. This is more pessimistic than the old rounding but seems more honest. With this change, we never show any file as having a 0.0% ratio, 0.1% is the best that can ever be achieved. A bunch of the expected outputs from the list command had to be updated to match the new logic.
1 parent f6155c5 commit ad288fb

File tree

231 files changed

+470
-456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

231 files changed

+470
-456
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
* Support was added for the 64-bit file sizes header, allowing huge
1616
files (>=4GiB) generated by the MorphOS port of lha to be
1717
extracted correctly.
18+
* The compression ratio shown in list output now always rounds up to
19+
the next 0.1%, which is a more pessimistic but more honest. For
20+
example, very highly compressed files might be shown as "0.1%" of
21+
their original size, but never "0.0%".
1822
* The manual page now includes more detailed information about the
1923
different list subcommands.
2024
* The liblhasa headers are now installed into a directory with a name

doc/lha.1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,13 @@ like \fB-lh0-\fR is used, this will be equal to SIZE.
124124
Size of the file in bytes before it was compressed.
125125
.TP
126126
\fBFile compression ratio (RATIO)\fR
127-
Compressed size divided by uncompressed size. This measures the effectiveness
128-
of the compression format; the smaller the percentage, the better. If an
127+
Compressed size as a percentage of uncompressed size. This measures the
128+
effectiveness of the compression format; the smaller the percentage, the
129+
better. The value is (pessimistically) rounded up to the next 0.1%. If an
129130
uncompressed format like \fB-lh0-\fR is used, this will be equal to 100%. For
130-
directories, "******" is shown.
131-
Note that the value here is backwards relative to \fBunzip\fR(1), which instead
132-
shows the percentage of the original file size reduced.
131+
directories, "******" is shown. Note that the value here is backwards relative
132+
to \fBunzip\fR(1), which instead shows the percentage of the original file size
133+
reduced.
133134
.TP
134135
\fBCompression format (METHOD)\fR
135136
The compression format used to store this file. See the \fBCOMPRESSION

src/list.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,31 @@ static const ListColumn size_column = {
242242

243243
// Compression ratio
244244

245-
static float compression_percent(size_t compressed, size_t uncompressed)
245+
static const char *compression_percent(size_t compressed, size_t uncompressed)
246246
{
247+
static char buf[10];
248+
int permille;
249+
250+
// We pessimistically round the compression ratio up to the next 0.1%,
251+
// so that even if eg. a 10,000:1 ratio was achieved, it will be shown
252+
// as "0.1%", not "0.0%". This is marginally more honest.
247253
if (uncompressed > 0) {
248-
return ((float) compressed * 100.0f) / (float) uncompressed;
254+
permille = (compressed * 1000 + uncompressed - 1) / uncompressed;
249255
} else {
250-
return 100.0f;
256+
permille = 1000;
251257
}
258+
259+
snprintf(buf, sizeof(buf), "%3d.%1d%%", permille / 10, permille % 10);
260+
return buf;
252261
}
253262

254263
static void ratio_column_print(LHAFileHeader *header)
255264
{
256265
if (!strcmp(header->compress_method, "-lhd-")) {
257266
printf("******");
258267
} else {
259-
printf("%5.1f%%", compression_percent(header->compressed_length,
260-
header->length));
268+
printf("%s", compression_percent(header->compressed_length,
269+
header->length));
261270
}
262271
}
263272

@@ -266,8 +275,8 @@ static void ratio_column_footer(FileStatistics *stats)
266275
if (stats->length == 0) {
267276
printf("******");
268277
} else {
269-
printf("%5.1f%%", compression_percent(stats->compressed_length,
270-
stats->length));
278+
printf("%s", compression_percent(stats->compressed_length,
279+
stats->length));
271280
}
272281
}
273282

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PERMSSN UID GID SIZE RATIO STAMP NAME
22
---------- ----------- ------- ------ ------------ --------------------
3-
[generic] *****/***** 18092 70.0% May 6 2010 gpl-2
3+
[generic] *****/***** 18092 70.1% May 6 2010 gpl-2
44
---------- ----------- ------- ------ ------------ --------------------
5-
Total 1 file 18092 70.0% Jan 1 2000
5+
Total 1 file 18092 70.1% Jan 1 2000
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PERMSSN UID GID SIZE RATIO STAMP LV
22
---------- ----------- ------- ------ ------------ ---
33
gpl-2
4-
[generic] *****/***** 18092 70.0% May 6 2010 [0]
4+
[generic] *****/***** 18092 70.1% May 6 2010 [0]
55
---------- ----------- ------- ------ ------------ ---
6-
Total 1 file 18092 70.0% Jan 1 2000
6+
Total 1 file 18092 70.1% Jan 1 2000
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PERMSSN UID GID PACKED SIZE RATIO METHOD CRC STAMP NAME
22
---------- ----------- ------- ------- ------ ---------- ------------ -------------
3-
[generic] *****/***** 12667 18092 70.0% -lzs- a33a May 6 2010 gpl-2
3+
[generic] *****/***** 12667 18092 70.1% -lzs- a33a May 6 2010 gpl-2
44
---------- ----------- ------- ------- ------ ---------- ------------ -------------
5-
Total 1 file 12667 18092 70.0% Jan 1 2000
5+
Total 1 file 12667 18092 70.1% Jan 1 2000
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PERMSSN UID GID PACKED SIZE RATIO METHOD CRC STAMP LV
22
---------- ----------- ------- ------- ------ ---------- ------------------- ---
33
gpl-2
4-
[generic] *****/***** 12667 18092 70.0% -lzs- a33a 2010-05-06 23:17:54 [0]
4+
[generic] *****/***** 12667 18092 70.1% -lzs- a33a 2010-05-06 23:17:54 [0]
55
---------- ----------- ------- ------- ------ ---------- ------------------- ---
6-
Total 1 file 12667 18092 70.0% 2000-01-01 00:00:00
6+
Total 1 file 12667 18092 70.1% 2000-01-01 00:00:00

test/output/larc333/long.lzs-l.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PERMSSN UID GID SIZE RATIO STAMP NAME
22
---------- ----------- ------- ------ ------------ --------------------
3-
[generic] *****/***** 1241658 18.2% Jun 9 2011 long.txt
3+
[generic] *****/***** 1241658 18.3% Jun 9 2011 long.txt
44
---------- ----------- ------- ------ ------------ --------------------
5-
Total 1 file 1241658 18.2% Jan 1 2000
5+
Total 1 file 1241658 18.3% Jan 1 2000
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PERMSSN UID GID SIZE RATIO STAMP LV
22
---------- ----------- ------- ------ ------------ ---
33
long.txt
4-
[generic] *****/***** 1241658 18.2% Jun 9 2011 [0]
4+
[generic] *****/***** 1241658 18.3% Jun 9 2011 [0]
55
---------- ----------- ------- ------ ------------ ---
6-
Total 1 file 1241658 18.2% Jan 1 2000
6+
Total 1 file 1241658 18.3% Jan 1 2000

test/output/larc333/long.lzs-v.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PERMSSN UID GID PACKED SIZE RATIO METHOD CRC STAMP NAME
22
---------- ----------- ------- ------- ------ ---------- ------------ -------------
3-
[generic] *****/***** 226557 1241658 18.2% -lz5- 6a7c Jun 9 2011 long.txt
3+
[generic] *****/***** 226557 1241658 18.3% -lz5- 6a7c Jun 9 2011 long.txt
44
---------- ----------- ------- ------- ------ ---------- ------------ -------------
5-
Total 1 file 226557 1241658 18.2% Jan 1 2000
5+
Total 1 file 226557 1241658 18.3% Jan 1 2000

0 commit comments

Comments
 (0)