Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/python/bytes.x
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lib/python/runtime-num.x
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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)))))
Expand Down
2 changes: 2 additions & 0 deletions lib/python/runtime-str.x
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
27 changes: 24 additions & 3 deletions tests/specs/41-string-repeat.spec.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
```
Loading