From 25c7c9b5811ca53abdc1023b60612e72dcca5387 Mon Sep 17 00:00:00 2001 From: Jon Ruttan Date: Sat, 28 Nov 2026 11:07:35 -0500 Subject: [PATCH] feat: add zfill to str, bytes and bytearray zfill pads on the left with zeros to the given width, keeping a leading sign in front of the padding. A missing or extra argument raises the TypeError CPython gives. Co-Authored-By: Claude Opus 5.5 --- lib/python/bytes.x | 12 ++++++++++++ lib/python/runtime-num.x | 15 +++++++++++++++ lib/python/runtime-str.x | 2 ++ tests/specs/41-string-repeat.spec.md | 27 ++++++++++++++++++++++++--- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/lib/python/bytes.x b/lib/python/bytes.x index 5894ced..d3126a5 100644 --- a/lib/python/bytes.x +++ b/lib/python/bytes.x @@ -291,6 +291,18 @@ (fn (_ l w c) (let ((n (List length l))) (if (<= w n) l (%pb-cat (%pb-fill (- w n) c) l))))) +; zfill: zeros on the left to width W, after a leading + or - when there is +; one, so '-42' is '-0042' and not '00-42'. Only the first is a sign: +; '--1' is '-00-1'. +(def %pb-zfill + (fn (_ l w) + (let ((n (List length l))) + (if (<= w n) l + (let ((pad (%pb-fill (- w n) 48))) + (if (if (null? l) #f (if (= (first l) 43) #t (= (first l) 45))) + (pair (first l) (%pb-cat pad (rest l))) + (%pb-cat pad l))))))) + ; --- split, join, replace ---------------------------------------------------- ; ; A nil separator splits on RUNS of whitespace and drops the empties, which diff --git a/lib/python/runtime-num.x b/lib/python/runtime-num.x index 297188b..611133a 100644 --- a/lib/python/runtime-num.x +++ b/lib/python/runtime-num.x @@ -1405,6 +1405,9 @@ ((Str8 =? name "center") (fn (_ . a) (mk (%pb-center l (first a) (%py-b-fill a))))) ((Str8 =? name "ljust") (fn (_ . a) (mk (%pb-ljust l (first a) (%py-b-fill a))))) ((Str8 =? name "rjust") (fn (_ . a) (mk (%pb-rjust l (first a) (%py-b-fill a))))) + ((Str8 =? name "zfill") + (fn (_ . a) + (mk (%pb-zfill l (%py-zfill-width (if (python-barr-is v) "bytearray" "bytes") a))))) ((Str8 =? name "isspace") (fn (_ . a) (%pb-isspace l))) ((Str8 =? name "isalpha") (fn (_ . a) (%pb-isalpha l))) ((Str8 =? name "isdigit") (fn (_ . a) (%pb-isdigit l))) @@ -1442,6 +1445,18 @@ (if (null? a) () (if (null? (first a)) () (%py-b-sep (first a)))))) ; the pad byte, space unless one was given +; zfill takes one argument, an int -- a bool or an int subclass as the int it +; is -- and says which type's method it is when given another count. +(def %py-zfill-width + (fn (_ cname a) + (if (if (null? a) #f (null? (rest a))) + (%py-range-int (first a)) + (Err raise (lit type) + (Str8 append cname + (Str8 append ".zfill() takes exactly one argument (" + (Str8 append (python-str (python-length a)) " given)"))) + ())))) + (def %py-b-fill (fn (_ args) (let ((c (%py-b-opt args 1 ()))) (if (null? c) 32 (first c))))) diff --git a/lib/python/runtime-str.x b/lib/python/runtime-str.x index 5a95041..e882600 100644 --- a/lib/python/runtime-str.x +++ b/lib/python/runtime-str.x @@ -547,6 +547,8 @@ (fn (_ . a) (python-str-new (%pb-ljust l (first a) (%py-s-fill a 1))))) ((Str8 =? name "rjust") (fn (_ . a) (python-str-new (%pb-rjust l (first a) (%py-s-fill a 1))))) + ((Str8 =? name "zfill") + (fn (_ . a) (python-str-new (%pb-zfill l (%py-zfill-width "str" a))))) ((Str8 =? name "expandtabs") (fn (_ . a) (python-str-new (%pb-expandtabs l (%py-s-opt a 0 8))))) ((Str8 =? name "isspace") (fn (_ . a) (%pb-isspace l))) diff --git a/tests/specs/41-string-repeat.spec.md b/tests/specs/41-string-repeat.spec.md index 183e46e..4cc8502 100644 --- a/tests/specs/41-string-repeat.spec.md +++ b/tests/specs/41-string-repeat.spec.md @@ -1,8 +1,8 @@ Repetition and joining at a size (`python/bytes.x`, `python/runtime-seq.x`): str, bytes, bytearray, list and tuple times a count, str.join and bytes.join, -and the padding methods, which repeat their fill. Each copy used to go on the -END of what was built, copying it again, so `'a' * 8000` took 32 million pairs -and passed the guard. Every expectation is a real CPython output. +and the padding methods, which repeat their fill -- zfill among them. Each +copy used to go on the END of what was built, copying it again, so +`'a' * 8000` took 32 million pairs and passed the guard. Every expectation is a real CPython output. ## string repeat @@ -22,3 +22,24 @@ and passed the guard. Every expectation is a real CPython output. 15999 a-b-c only 12000 12000 ****x ``` + +### zfill pads with zeros after a sign, for str, bytes and bytearray + +```python +(python-run "print('42'.zfill(5), '-42'.zfill(5), '+7'.zfill(4), '-'.zfill(3), repr(''.zfill(2)), 'abc'.zfill(2), '--1'.zfill(5))\nprint(repr('x'.zfill(True)), repr('x'.zfill(-3)), repr('x'.zfill(False)), len('9'.zfill(5000)))\nclass I(int):\n pass\nprint('5'.zfill(I(3)), str.zfill('7', 3), ascii('\\xe9'.zfill(3)))\nprint(b'-9'.zfill(4), bytearray(b'+a').zfill(4), b'12'.zfill(1), type(bytearray(b'1').zfill(1)).__name__)\nb = bytearray(b'7')\nprint(b.zfill(3), b)\nfor f in (lambda: 'x'.zfill(2.0), lambda: 'x'.zfill(), lambda: 'x'.zfill('3'), lambda: 'x'.zfill(1, 2), lambda: b'x'.zfill(), lambda: bytearray(b'x').zfill(1, 2), lambda: b'5'.zfill(3.5)):\n try:\n f()\n except TypeError as e:\n print('TypeError', e)\n") +``` +--- +```output +00042 -0042 +007 -00 '00' abc -00-1 +'x' 'x' 'x' 5000 +005 007 '00\xe9' +b'-009' bytearray(b'+00a') b'12' bytearray +bytearray(b'007') bytearray(b'7') +TypeError 'float' object cannot be interpreted as an integer +TypeError str.zfill() takes exactly one argument (0 given) +TypeError 'str' object cannot be interpreted as an integer +TypeError str.zfill() takes exactly one argument (2 given) +TypeError bytes.zfill() takes exactly one argument (0 given) +TypeError bytearray.zfill() takes exactly one argument (2 given) +TypeError 'float' object cannot be interpreted as an integer +```