Skip to content

Commit 6249e2e

Browse files
author
thisismypassport
committed
do not fail on over-huge carts without fail_on_error, and added --no-count-compress/tokenize
1 parent 5c63db3 commit 6249e2e

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ You can combine counting with other operations, in which case the counts are of
329329

330330
In such cases, you can also use `--input-count` to count the number of tokens, characters, and compressed bytes (if applicable) of the input cart.
331331

332+
If you're not interested in the number of tokens or in the compressed size, you can use `--no-count-tokenize` or `--no-count-compress` to avoid tokenizing or compressing the cart just to get the count. (You will still see the count if the tokenize/compress had to be done anyway, though)
333+
332334
# Format Conversion
333335

334336
Shrinko8 supports multiple cart formats, and allows converting between them:

pico_compress.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def compress_code(w, code, size_handler=None, force_compress=False, fail_on_erro
268268
if len(code) >= k_code_size or force_compress: # (>= due to null)
269269
start_pos = w.pos()
270270
w.bytes(k_new_compressed_code_header if is_new else k_compressed_code_header)
271-
w.u16(len(code) & 0xffff)
271+
w.u16(len(code) & 0xffff) # only throw under fail_on_error below
272272
len_pos = w.pos()
273273
w.u16(0) # revised below
274274

@@ -460,7 +460,7 @@ def write_literal(ch):
460460

461461
if is_new:
462462
w.setpos(len_pos)
463-
w.u16(size)
463+
w.u16(size & 0xffff) # only throw under fail_on_error above
464464

465465
else:
466466
w.bytes(bytes(ord(c) for c in code))

pico_process.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,14 @@ def __str__(m):
133133
def print_token_count(num_tokens, **kwargs):
134134
print_size("tokens", num_tokens, 8192, **kwargs)
135135

136-
def process_code(ctxt, source, input_count=False, count=False, lint=False, minify=False, rename=False, fail=True):
136+
def process_code(ctxt, source, input_count=False, count=False, lint=False, minify=False, rename=False, fail=True, want_count=True):
137137
need_lint = lint not in (None, False)
138138
need_minify = minify not in (None, False)
139139
need_rename = rename not in (None, False)
140140

141+
if not need_lint and not need_minify and not (want_count and (count or input_count)):
142+
return True, ()
143+
141144
ok = False
142145
tokens, errors = tokenize(source, ctxt)
143146
if not errors and (need_lint or need_minify):

shrinko8.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def ParsableCountHandler(prefix, name, size, limit):
4646
pgroup.add_argument("-c", "--count", action="store_true", help="enable printing token count, character count & compressed size")
4747
pgroup.add_argument("--input-count", action="store_true", help="enable printing input token count, character count & compressed size")
4848
pgroup.add_argument("--parsable-count", action="store_true", help="output counts in a stable, parsable format")
49+
pgroup.add_argument("--no-count-compress", action="store_true", help="do not compress the cart just to print the compressed size")
50+
pgroup.add_argument("--no-count-tokenize", action="store_true", help="do not tokenize the cart just to print the token count")
4951

5052
pgroup = parser.add_argument_group("script options")
5153
pgroup.add_argument("-s", "--script", help="manipulate the cart via a custom python script - see README for api details")
@@ -175,7 +177,8 @@ def fail(msg):
175177
preproc_cb(cart=cart, src=src, ctxt=ctxt, args=args, res_path=None) # (res_path is obsolete)
176178

177179
ok, errors = process_code(ctxt, src, input_count=args.input_count, count=args.count,
178-
lint=args.lint, minify=args.minify, rename=args.rename, fail=False)
180+
lint=args.lint, minify=args.minify, rename=args.rename,
181+
fail=False, want_count=not args.no_count_tokenize)
179182
if errors:
180183
print("Lint errors:" if ok else "Compilation errors:")
181184
for error in errors:
@@ -191,7 +194,7 @@ def fail(msg):
191194

192195
if args.count:
193196
write_code_size(cart, handler=args.count)
194-
if not (args.output and str(args.format) not in CartFormat._src_names): # else, will be done in write_cart
197+
if not (args.output and str(args.format) not in CartFormat._src_names) and not args.no_count_compress: # else, will be done in write_cart
195198
write_compressed_size(cart, handler=args.count, fast_compress=args.fast_compression)
196199

197200
if args.output:

0 commit comments

Comments
 (0)