core: remove stringcopy.append

This commit is contained in:
hellerve 2019-05-27 18:05:44 +02:00
parent e575a6aed6
commit e9fcf87ff5
12 changed files with 32 additions and 46 deletions

View File

@ -20,10 +20,10 @@ If your functions takes a large amount of time, experimenting with this might ma
(hidden get-unit) (hidden get-unit)
(defn get-unit [n] (defn get-unit [n]
(cond (cond
(< n 1000.0) (StringCopy.append (Double.str n) @"ns") (< n 1000.0) (String.append &(Double.str n) "ns")
(< n 1000000.0) (StringCopy.append (Double.str (/ n 1000.0)) @"µs") (< n 1000000.0) (String.append &(Double.str (/ n 1000.0)) "µs")
(< n 1000000000.0) (StringCopy.append (Double.str (/ n 1000000.0)) @"ms") (< n 1000000000.0) (String.append &(Double.str (/ n 1000000.0)) "ms")
(StringCopy.append (Double.str (/ n 1000000000.0)) @"s"))) (String.append &(Double.str (/ n 1000000000.0)) "s")))
(private print) (private print)
(hidden print) (hidden print)

View File

@ -6,9 +6,10 @@
(if (= idx -1) (if (= idx -1)
(list 'copy s) ; no more splits found, just return string (list 'copy s) ; no more splits found, just return string
(if (= \% (String.char-at s (inc idx))) ; this is an escaped % (if (= \% (String.char-at s (inc idx))) ; this is an escaped %
(list 'StringCopy.append (list 'ref
(list 'copy "%") (list 'String.append
(fmt-internal (String.substring s (+ idx 2) len) args)) "%"
(fmt-internal (String.substring s (+ idx 2) len) args)))
(if (= 0 (length args)) ; we need to insert something, but have nothing (if (= 0 (length args)) ; we need to insert something, but have nothing
(macro-error "error in format string: not enough arguments to format string") (macro-error "error in format string: not enough arguments to format string")
; okay, this is the meat: ; okay, this is the meat:
@ -17,12 +18,14 @@
(if (= -1 next) (if (= -1 next)
(if (< 1 (length args)) (if (< 1 (length args))
(macro-error "error in format string: too many arguments to format string") (macro-error "error in format string: too many arguments to format string")
(list 'format s (car args))) (list 'ref (list 'format s (car args))))
(let [slice (String.substring s 0 (+ (inc idx) next))] (let [slice (String.substring s 0 (+ (inc idx) next))]
(list 'StringCopy.append (list 'format slice (car args)) (list 'ref
(fmt-internal (String.substring s (+ (inc idx) next) len) (list 'String.append
(cdr args))))))))))) (list 'ref (list 'format slice (car args)))
(fmt-internal (String.substring s (+ (inc idx) next) len)
(cdr args))))))))))))
(doc fmt "formats a string. It supports all of the string interpolations defined in format of the type that should be interpolated (e.g. %d and %x on integers).") (doc fmt "formats a string. It supports all of the string interpolations defined in format of the type that should be interpolated (e.g. %d and %x on integers).")
(defmacro fmt [s :rest args] (defmacro fmt [s :rest args]
(fmt-internal s args)) (list 'copy (fmt-internal s args)))

View File

@ -269,17 +269,17 @@
(if (= (length forms) 0) (if (= (length forms) 0)
(list "") (list "")
(if (= (length forms) 1) (if (= (length forms) 1)
(list 'str (car forms)) (list 'ref (list 'str (car forms)))
(list 'StringCopy.append (list 'str (car forms)) (build-str* (cdr forms)))))) (list 'ref (list 'String.append (list 'ref (list 'str (car forms))) (build-str* (cdr forms)))))))
(defmacro str* [:rest forms] (defmacro str* [:rest forms]
(build-str* forms)) (list 'copy (build-str* forms)))
(defmacro println* [:rest forms] (defmacro println* [:rest forms]
(list 'IO.println (list 'ref (build-str* forms)))) (list 'IO.println (build-str* forms)))
(defmacro print* [:rest forms] (defmacro print* [:rest forms]
(list 'IO.print (list 'ref (build-str* forms)))) (list 'IO.print (build-str* forms)))
(defmacro ignore [form] (defmacro ignore [form]
(list 'let (array '_ form) (list))) (list 'let (array '_ form) (list)))

View File

@ -152,8 +152,6 @@
) )
(defmodule StringCopy (defmodule StringCopy
(register append (Fn [String String] String))
(defn str [s] (the String s)) (defn str [s] (the String s))
(defn = [a b] (defn = [a b]

View File

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

View File

@ -633,11 +633,11 @@ String Pattern_internal_add_value(PatternMatchState *ms, String res, String src,
} }
res = Pattern_internal_add_char(res, tr[i]); res = Pattern_internal_add_char(res, tr[i]);
} }
else if (tr[i] == '0') res = StringCopy_append(res, src); else if (tr[i] == '0') res = String_append(&res, &src);
else { else {
Array a = {.len = 0, .capacity = 0, .data = NULL}; Array a = {.len = 0, .capacity = 0, .data = NULL};
Pattern_internal_push_onecapture(ms, tr[i] - '1', src, e, a); Pattern_internal_push_onecapture(ms, tr[i] - '1', src, e, a);
res = StringCopy_append(res, ((String*)a.data)[0]); /* add capture to accumulated result */ res = String_append(&res, &((String*)a.data)[0]); /* add capture to accumulated result */
} }
} }
} }

View File

@ -105,13 +105,6 @@ String String_append(String *a, String *b) {
return buffer; return buffer;
} }
String StringCopy_append(String a, String b) {
String buffer = String_append(&a, &b);
CARP_FREE(a);
CARP_FREE(b);
return buffer;
}
int String_length(String *s) { int String_length(String *s) {
return strlen(*s); return strlen(*s);
} }

View File

@ -100,7 +100,7 @@ be used at a specific point in your program. In such cases the concept of 'holes
add a hole in your source code and reload (":r") to let the Carp compiler figure out what type goes there. add a hole in your source code and reload (":r") to let the Carp compiler figure out what type goes there.
```clojure ```clojure
(StringCopy.append ?w00t @"!") ;; Will generate a type error telling you that the type of '?w00t' is String (String.append ?w00t "!") ;; Will generate a type error telling you that the type of '?w00t' is &String
``` ```
### Special forms during evaluation of dynamic code ### Special forms during evaluation of dynamic code

View File

@ -19,7 +19,6 @@
** 0.3 ** 0.3
*** Prevent usage of 'private' functions from outside their module. *** Prevent usage of 'private' functions from outside their module.
*** Type annotations should affect the type, not just check it. *** Type annotations should affect the type, not just check it.
*** Remove StringCopy.append, use String.append in all those cases.
*** Unified signatures for struct updaters and aupdate *** Unified signatures for struct updaters and aupdate
*** Allow matching on references, to avoid copying *** Allow matching on references, to avoid copying
** 1.0 ** 1.0

View File

@ -13,10 +13,10 @@
(defmodule Things (defmodule Things
(defn inside [s] (defn inside [s]
(let [msg (StringCopy.append s (String.copy "!"))] (let [msg (String.append s "!")]
(println (ref msg)))) (println (ref msg))))
(defn call [] (defn call []
(inside (String.copy "Hello")))) (inside "Hello")))
(defn use-doubles [] (defn use-doubles []
(println (ref (str (Double.to-int (Double.+ 2.0 3.0)))))) (println (ref (str (Double.to-int (Double.+ 2.0 3.0))))))
@ -132,7 +132,7 @@
(defn print-last-string [] (defn print-last-string []
(println &(get-last-string [(String.copy "NO") (String.copy "NO") (String.copy "YES")]))) (println &(get-last-string [(String.copy "NO") (String.copy "NO") (String.copy "YES")])))
(defn exclaim [x] (StringCopy.append x @"!")) (defn exclaim [x] (String.append &x "!"))
(deftype Simple []) (deftype Simple [])
(deftype Complex [x Int f Float d Double s String c Char]) (deftype Complex [x Int f Float d Double s String c Char])
@ -185,7 +185,7 @@
b [1 2 3] b [1 2 3]
c1 (length &a) c1 (length &a)
c2 (length &b)] c2 (length &b)]
(println* &(str (+ c1 c2))))) (println* (+ c1 c2))))
(defn changing-target-of-ref [] (defn changing-target-of-ref []
(let [s1 @"hello" (let [s1 @"hello"

View File

@ -8,8 +8,8 @@
[4 5 6] [4 5 6]
[7 8 9]]) [7 8 9]])
(defn excl [x] (StringCopy.append x @"!")) (defn excl [x] (String.append &x "!"))
(defn excl-ref [x] (StringCopy.append @x @"!")) (defn excl-ref [x] (String.append x "!"))
(defn inc-ref [x] (+ @x 1)) (defn inc-ref [x] (+ @x 1))

View File

@ -192,7 +192,7 @@
(assert (= &[@"q" @"b" @"c"] &xs))))) (assert (= &[@"q" @"b" @"c"] &xs)))))
(defn append-ref [a b] (defn append-ref [a b]
(StringCopy.append a @b)) (String.append &a b))
(defn array-reduce [] (defn array-reduce []
(let [xs [@"a" @"b" @"c"] (let [xs [@"a" @"b" @"c"]
@ -334,12 +334,6 @@
(let [result (String.append a b)] (let [result (String.append a b)]
(assert (StringCopy.= result @"abcdefghijklmnopqrstuvwxyz"))))) (assert (StringCopy.= result @"abcdefghijklmnopqrstuvwxyz")))))
(defn stringcopy-append-leak-test []
(let [a "abcdef"
b "ghijklmnopqrstuvwxyz"]
(let [result (StringCopy.append @a @b)]
(assert (StringCopy.= result @"abcdefghijklmnopqrstuvwxyz")))))
(defn lambda-1 [] (defn lambda-1 []
(let [s @"X" (let [s @"X"
f (fn [] @&s)] ;; each call needs to produce a new copy of the string f (fn [] @&s)] ;; each call needs to produce a new copy of the string
@ -486,7 +480,6 @@
(assert-no-leak test array-copy-map-1 "array-copy-map-1 does not leak") (assert-no-leak test array-copy-map-1 "array-copy-map-1 does not leak")
(assert-no-leak test array-copy-map-2 "array-copy-map-2 does not leak") (assert-no-leak test array-copy-map-2 "array-copy-map-2 does not leak")
(assert-no-leak test string-append-leak-test "String.append does not leak") (assert-no-leak test string-append-leak-test "String.append does not leak")
(assert-no-leak test stringcopy-append-leak-test "StringCopy.append does not leak")
(assert-no-leak test lambda-1 "lambda-1 does not leak") (assert-no-leak test lambda-1 "lambda-1 does not leak")
(assert-no-leak test lambda-2 "lambda-2 does not leak") (assert-no-leak test lambda-2 "lambda-2 does not leak")
(assert-no-leak test lambda-3 "lambda-3 does not leak") (assert-no-leak test lambda-3 "lambda-3 does not leak")