Switch to edit mode.
Name(s): crunch1: "Copied from Encryption Utilities (#111):crunch by Odo (#987)Nov 28 13:11:06 1998"
2: "A simple run-length compression routine, by Odo."
3: "This verb takes any text string, or list of strings as args[1]. It then returns the text either exactly as it arrived, or in a compressed format that strips out repeated runs of single letters. This verb is only usefull on ASCII art."
4: if (!args[1])
5: player:tell("Empty input, no compression done.")
6: return args[1]
7: endif
8: input = typeof(args[1]) == $STR ? {args[1]} | args[1]
9: if (input[1] == $string_utils.tab)
10: player:tell("The text has already been compressed.")
11: return input
12: endif
13: output = {$string_utils.tab}
14: for line in (input)
15: newline = oldletter = ""
16: for index in [1..length(line)]
17: $command_utils:suspend_if_needed()
18: !strcmp(oldletter, letter = line[index]) ? newline[length(newline)] = $string_utils.ascii[repeats = repeats + 1] | (newline = newline + ("^" + letter + $string_utils.ascii[repeats = 1]))
19: oldletter = letter
20: endfor
21: line = ""
22: for index in [1..length(newline) / 3]
23: $command_utils:suspend_if_needed()
24: letter = newline[index * 3 - 2..index * 3]
25: if (letter[2] == "^" || (repeats = index($string_utils.ascii, letter[3], 1)) > 3)
26: line = line + letter
27: else
28: for repeat in [1..repeats]
29: line = line + letter[2]
30: endfor
31: endif
32: endfor
33: output = {@output, line}
34: endfor
35: player:tell("Size changed from ", insize = value_bytes(args[1]), " bytes to ", outsize = value_bytes(output), " bytes. ", 100 * outsize / insize, "%")
36: if (insize <= outsize)
37: player:tell("Compression didn't help, or made things worse. No changes made.")
38: return args[1]
39: endif
40: suspend(0)
41: if (!equal(args[1], this:uncrunch(output)))
42: player:tell("Oh dear! An error has occurred while crunching the text. Please tell a wizard what you just did. The text is being returned intact.")
43: return args[1]
44: endif
45: return output
46: "Last modified by Dax (#789) on Tue May 3 14:03:29 2005 MDT."