fix auto-replace breaking byte arrays

also sneak some wording changes in
This commit is contained in:
bleck9999 2022-04-30 19:57:49 +01:00
parent 09cd6e2bd1
commit 6881075fc8
No known key found for this signature in database
GPG Key ID: D0CA0C41DB616843
1 changed files with 11 additions and 8 deletions

View File

@ -1,7 +1,6 @@
# Copyright (c) 2021 bleck9999
# https://github.com/bleck9999/ts-minifier
# Version: e276c417
# Version: 49befe92
import argparse
import itertools
import logging
@ -153,12 +152,12 @@ def minify(script: Code, userobjects, usages):
# minus the amount of characters it would take to define an alias (len(alias)+len(func)+2), with the 2 being for the
# equals and the whitespace needed for a definition
# the same principle also applies to introducing a variable for string literals, though since a literal requires
# having "s around it then it's uses*(len(str)+2) - (len(minName)+len(str)+4)
# having "s around it then it's uses*(len(str)+2) - (len(minName)+len(str)+4) instead
# ^ 2 for = and whitespace, 2 for ""
#
# obviously for a rename you're already defining it so it's just the difference between lengths multiplied by uses
short_idents = [x for x in (ascii_letters + '_')] + [x[0] + x[1] for x in
itertools.product(ascii_letters + '_', repeat=2)]
short_idents = [x for x in (ascii_letters + '_')] + \
[x[0] + x[1] for x in itertools.product(ascii_letters + '_', repeat=2)]
short_idents.pop(short_idents.index("if"))
mcode = script.rawcode
aliases = []
@ -267,6 +266,10 @@ def minify(script: Code, userobjects, usages):
logging.info("Introducing variables for reused literals" if auto_replace else
"Checking for reused literals")
for string in str_reuse:
if string == '"BYTE[]"':
# the type specifier for byte arrays is special because it has to be a literal
# if it's a variable then it tries to treat it as a string array then shits the bed if the other items are not strings
continue
tmpcode = ""
candidates = short_idents
minName = ""
@ -341,13 +344,13 @@ def minify(script: Code, userobjects, usages):
mcode = tmpcode + mcode[bound + diff + len(minName):]
aliases.append(f"{minName}={uint} ")
else:
logging.warning(f"Not introducing variable for string {uint} reused {uses} times "
logging.warning(f"Not introducing variable for integer {uint} reused {uses} times "
f"(would save {savings} bytes)")
else:
logging.info(f"Not introducing variable for string {uint} reused {uses} times "
logging.info(f"Not introducing variable for integer {uint} reused {uses} times "
f"(would save {savings} bytes)")
else:
logging.info(f"Not introducing variable for int {uint} (only used once)")
logging.info(f"Not introducing variable for integer {uint} (only used once)")
logging.info("Reintroducing REQUIREs")
mcode = "".join([x[2] for x in script.comments]) + "".join(aliases) + mcode