Merge pull request #606 from hellerve/better-repeat

Reduce allocations in String.repeat
This commit is contained in:
Erik Svedäng 2019-10-30 10:09:51 +01:00 committed by GitHub
commit c90d11b7ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -29,10 +29,11 @@
(doc repeat "Returns a new string which is `inpt` repeated `n` times.")
(defn repeat [n inpt]
(let [str @""]
(let [l (length inpt)
str (String.allocate (* n l) \0)]
(do
(for [i 0 n]
(set! str (append &str inpt)))
(string-set-at! &str (* i l) inpt))
str)))
(doc pad-left "Pads the left of a string with len bytes using the padding pad.")

View File

@ -91,11 +91,11 @@
(if (Int.> (Int.+ passed failed) 0)
(do
(IO.color (Green))
(if (Int.> passed 0) (IO.print &(String.append "\t|" &(String.repeat passed "="))) ())
(if (Int.= failed 0) (IO.print "|") ())
(when (Int.> passed 0) (IO.print &(String.append "\t|" &(String.allocate passed \=))))
(when (Int.= failed 0) (IO.print "|"))
(IO.color (Red))
(if (Int.= passed 0) (IO.print "\t|") ())
(if (Int.> failed 0) (IO.print &(String.append &(String.repeat failed "=") "|")) ())
(when (Int.= passed 0) (IO.print "\t|"))
(when (Int.> failed 0) (IO.print &(String.append &(String.allocate failed \=) "|")))
(IO.println ""))
())
(IO.color (Green))