diff --git a/lib/python/bytes.x b/lib/python/bytes.x index efa01b4..5894ced 100644 --- a/lib/python/bytes.x +++ b/lib/python/bytes.x @@ -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) @@ -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))) diff --git a/lib/python/runtime-seq.x b/lib/python/runtime-seq.x index e7b1626..2bde34c 100644 --- a/lib/python/runtime-seq.x +++ b/lib/python/runtime-seq.x @@ -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 diff --git a/tests/specs/41-string-repeat.spec.md b/tests/specs/41-string-repeat.spec.md new file mode 100644 index 0000000..183e46e --- /dev/null +++ b/tests/specs/41-string-repeat.spec.md @@ -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 +```