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
14 changes: 10 additions & 4 deletions lib/python/bytes.x
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@
(fn (self l b) (if (null? l) b (self (rest l) (pair (first l) b)))))
(def %pb-cat (fn (_ a b) (%pb-onto (List reverse a) b)))

; EACH COPY GOES ON THE FRONT. Every copy is the same list, so the order is
; the same either way -- and on the front a copy costs L, where on the end it
; cost everything made so far: 'a' * 8000 was 32 million pairs.
(def %pb-repeat
(fn (self l n acc)
(if (<= n 0) acc (self l (- n 1) (%pb-cat acc l)))))
(if (<= n 0) acc (self l (- n 1) (%pb-cat l acc)))))

(def %pb-eq?
(fn (self a b)
Expand Down Expand Up @@ -362,11 +365,14 @@
(self (rest l) keep (pair (first l) cur) acc))))))
(def %pb-splitlines (fn (_ l keep) (%pb-splitlines-go l keep () ())))

; The result is built backwards, each piece and separator pushed onto it, and
; turned round once at the end: one pass, where appending to it copied it
; again for every piece.
(def %pb-join-go
(fn (self sep ps acc first?)
(if (null? ps) acc
(fn (self sep ps racc first?)
(if (null? ps) (List reverse racc)
(self sep (rest ps)
(if first? (first ps) (%pb-cat acc (%pb-cat sep (first ps))))
(%pb-onto (first ps) (if first? racc (%pb-onto sep racc)))
#f))))
(def %pb-join (fn (_ sep ps) (%pb-join-go sep ps () #t)))

Expand Down
4 changes: 3 additions & 1 deletion lib/python/runtime-seq.x
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,10 @@
(def %py-dvals (fn (self entries) (if (null? entries) () (pair (rest (first entries)) (self (rest entries))))))

; --- The list method surface -------------------------------------------------
; Each copy on the front, as %pb-repeat does and for its reason: on the end
; every copy copied all the copies before it.
(def python-els-repeat
(fn (self l k acc) (if (<= k 0) acc (self l (- k 1) (python-list-cat acc l)))))
(fn (self l k acc) (if (<= k 0) acc (self l (- k 1) (python-list-cat l acc)))))
(def python-els-drop-at
(fn (self l i) (if (null? l) () (if (= i 0) (rest l) (pair (first l) (self (rest l) (- i 1)))))))
(def python-els-insert-at
Expand Down
24 changes: 24 additions & 0 deletions tests/specs/41-string-repeat.spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.

## string repeat

### a long repeat, a long join and a wide pad, each built in one pass

```python
(python-run "s = 'ab' * 10000\nprint(len(s), s[:5], s[-3:])\nt = 'x' * 0 + 'y' * -3 + 3 * 'z'\nprint(repr(t), 'q' * 1)\nb = b'\\x00\\xff' * 8000\nprint(len(b), b[:4], b[-1])\nl = [1, [2]] * 6000\nprint(len(l), l[:3], l[-1] is l[1])\nprint(len((0, 1, 2) * 6000), len(bytearray(b'ab') * 4000))\nj = ','.join(['ab'] * 4000)\nprint(len(j), j[:8], j[-4:])\nprint(len(b'-'.join([b'xyz'] * 4000)), '-'.join(['a', 'b', 'c']), ''.join([]), '/'.join(['only']))\nprint(len(' '.center(12000)), len('x'.ljust(12000, '.')), 'x'.rjust(5, '*'))\n")
```
---
```output
20000 ababa bab
'zzz' q
16000 b'\x00\xff\x00\xff' 255
12000 [1, [2], 1] True
18000 8000
11999 ab,ab,ab b,ab
15999 a-b-c only
12000 12000 ****x
```
Loading