aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
authorEduardo Julian2018-07-04 18:53:03 -0400
committerEduardo Julian2018-07-04 18:53:03 -0400
commit9ba7b6416a34d9f031b113aa48b1663b14dcb0aa (patch)
treeff2dbd0267a1945540a89d8eac04d59b866eea6d /stdlib
parent01ca61865cf816808151fdecccd84bc6da8194ff (diff)
- "lux/data/text/buffer" module, with platform-specific implementations of code using text-buffer abstractions for fast text concatenation.
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/data/text/buffer.lux72
1 files changed, 72 insertions, 0 deletions
diff --git a/stdlib/source/lux/data/text/buffer.lux b/stdlib/source/lux/data/text/buffer.lux
new file mode 100644
index 000000000..423a1ba3c
--- /dev/null
+++ b/stdlib/source/lux/data/text/buffer.lux
@@ -0,0 +1,72 @@
+(.module:
+ lux
+ (lux (data [product]
+ text/format
+ (coll [row #+ Row "row/" Fold<Row>]))
+ (lang ["_" host])
+ (type abstract)
+ [host #+ import:])
+ [//])
+
+(`` (for {(~~ (static _.jvm))
+ (as-is (import: java/lang/CharSequence)
+
+ (import: java/lang/Appendable
+ (append [CharSequence] Appendable))
+
+ (import: java/lang/StringBuilder
+ (new [int])
+ (toString [] String)))}))
+
+(`` (abstract: #export Buffer
+ {#.doc "Immutable text buffer for efficient text concatenation."}
+
+ (for {(~~ (static _.jvm))
+ [Nat (-> StringBuilder StringBuilder)]}
+ ## default
+ (Row Text))
+
+ (def: #export empty
+ Buffer
+ (:abstraction (for {(~~ (static _.jvm))
+ [+0 id]}
+ ## default
+ row.empty)))
+
+ (def: #export (append chunk buffer)
+ (-> Text Buffer Buffer)
+ (for {(~~ (static _.jvm))
+ (let [[capacity transform] (:representation buffer)
+ append! (: (-> Text StringBuilder StringBuilder)
+ (function (_ chunk builder)
+ (exec (Appendable::append [(:coerce CharSequence chunk)]
+ builder)
+ builder)))]
+ (:abstraction [(n/+ (//.size chunk) capacity)
+ (|>> transform (append! chunk))]))}
+ ## default
+ (|> buffer :representation (row.add chunk) :abstraction)))
+
+ (def: #export (size buffer)
+ (-> Buffer Nat)
+ (for {(~~ (static _.jvm))
+ (|> buffer :representation product.left)}
+ ## default
+ (row/fold (function (_ chunk total)
+ (n/+ (//.size chunk) total))
+ +0
+ (:representation buffer))))
+
+ (def: #export (text buffer)
+ (-> Buffer Text)
+ (for {(~~ (static _.jvm))
+ (let [[capacity transform] (:representation buffer)]
+ (|> (StringBuilder::new [(.int capacity)])
+ transform
+ (StringBuilder::toString [])))}
+ ## default
+ (row/fold (function (_ chunk total)
+ (format total chunk))
+ ""
+ (:representation buffer))))
+ ))