commit ffea32ebb7d94db893c87aa9eb1abf8d34e340cd
parent eea32057ab434149cd47c50f99cc898c1ac7f586
Author: Andres Navarro <canavarro82@gmail.com>
Date: Thu, 27 Oct 2011 15:00:17 -0300
Bugfix: substring now follows the mutability of the source. Revised some test cases.
Diffstat:
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/src/kgstrings.c b/src/kgstrings.c
@@ -239,7 +239,7 @@ bool kstring_ci_gep(TValue str1, TValue str2)
}
/* 13.2.5? substring */
-/* TEMP: at least for now this always returns mutable strings */
+/* Note: This will return an mutable string iff the source string is mutable */
void substring(klisp_State *K, TValue *xparams, TValue ptree, TValue denv)
{
UNUSED(xparams);
@@ -276,8 +276,10 @@ void substring(klisp_State *K, TValue *xparams, TValue ptree, TValue denv)
/* the if isn't strictly necessary but it's clearer this way */
if (size == 0) {
new_str = K->empty_string;
- } else {
+ } else if (kstring_mutablep(str)) {
new_str = kstring_new_bs(K, kstring_buf(str)+start, size);
+ } else {
+ new_str = kstring_new_bs_imm(K, kstring_buf(str)+start, size);
}
kapply_cc(K, new_str);
}
diff --git a/src/tests/strings.k b/src/tests/strings.k
@@ -129,10 +129,19 @@
($check-error (substring "abcdef" 3 10))
($check-error (substring "abcdef" 4 2))
-($check-not-predicate
+
+;; immutable strings are eq? iff string=?
+;; Andres Navarro
+($check-predicate
($let* ((p "abc") (q (substring p 0 3)))
(eq? p q)))
+;; string-copy always generate mutable strings
+;; Andres Navarro
+($check-not-predicate
+ ($let* ((p (string-copy "abc")) (q (substring p 0 3)))
+ (eq? p q)))
+
($check-predicate (immutable-string? (substring "abc" 0 0)))
($check-predicate (immutable-string? (substring "abc" 0 1)))