diff --git a/bench/array_subarray.carp b/bench/array_subarray.carp index f8e2f39b..48e9d63c 100644 --- a/bench/array_subarray.carp +++ b/bench/array_subarray.carp @@ -8,7 +8,7 @@ (defn some-subarray [] (let-do [a (replicate n &1) - b (subarray &a 0 (/ n 2))] + b (slice &a 0 (/ n 2))] (assert (= (/ n 2) (length &b))))) (defn perform-bench [new-n] diff --git a/core/Array.carp b/core/Array.carp index 838a894d..7dd7f583 100644 --- a/core/Array.carp +++ b/core/Array.carp @@ -138,20 +138,20 @@ If the array is empty, returns `Nothing`") (defn sum [xs] (Array.reduce &(fn [x y] (+ x @y)) (zero) xs)) - (doc subarray "gets a subarray from `start-index` to `end-index`.") - (defn subarray [xs start-index end-index] + (doc slice "gets a subarray from `start-index` to `end-index`.") + (defn slice [xs start-index end-index] (let-do [result []] (for [i start-index end-index] (set! result (push-back result @(unsafe-nth xs i)))) result)) - (doc prefix-array "gets a prefix array to `end-index`.") - (defn prefix-array [xs end-index] - (subarray xs 0 end-index)) + (doc prefix "gets a prefix array to `end-index`.") + (defn prefix [xs end-index] + (slice xs 0 end-index)) - (doc suffix-array "gets a suffix array from `start-index`.") - (defn suffix-array [xs start-index] - (subarray xs start-index (length xs))) + (doc suffix "gets a suffix array from `start-index`.") + (defn suffix [xs start-index] + (slice xs start-index (length xs))) (doc reverse "reverses an array.") (defn reverse [a] diff --git a/core/Dynamic.carp b/core/Dynamic.carp index 0f2eb93c..5b1c382f 100644 --- a/core/Dynamic.carp +++ b/core/Dynamic.carp @@ -17,14 +17,14 @@ (Project.config "echo-compiler-cmd" false)))) (defmodule String - (defndynamic prefix-string [s to] - (String.substring s 0 to)) + (defndynamic prefix [s to] + (String.slice s 0 to)) - (defndynamic suffix-string [s from] - (String.substring s from (String.length s))) + (defndynamic suffix [s from] + (String.slice s from (String.length s))) (defndynamic tail [s ] - (String.suffix-string s 1)) + (String.suffix s 1)) ) ) diff --git a/core/Filepath.carp b/core/Filepath.carp index 22f59d2f..4271e93d 100644 --- a/core/Filepath.carp +++ b/core/Filepath.carp @@ -6,7 +6,7 @@ (defn dir-from-path [path] (let [segments (split-by path &[\/]) n (dec (length &segments)) - without-last (prefix-array &segments n)] + without-last (prefix &segments n)] (concat &(copy-map &(fn [s] (str* s "/")) &without-last)))) (doc file-from-path "removes the base name part of a path to a file, similar to the `filename` utility in Shell scripting.") diff --git a/core/Format.carp b/core/Format.carp index 0b14b7cf..9f2ad665 100644 --- a/core/Format.carp +++ b/core/Format.carp @@ -11,15 +11,15 @@ (list 'ref (list 'String.append "%" - (fmt-internal (String.substring s (+ idx 2) len) args))) + (fmt-internal (String.slice s (+ idx 2) len) args))) (if (= 0 (length args)) ; we need to insert something, but have nothing (macro-error (str "error in format string: not enough arguments to format string (missing argument for '%" - (String.substring s (inc idx) (inc (inc idx))) + (String.slice s (inc idx) (inc (inc idx))) "')")) ; okay, this is the meat: ; get the next % after our escaper - (let [next (String.index-of (String.substring s (inc idx) len) \%)] + (let [next (String.index-of (String.slice s (inc idx) len) \%)] (if (= -1 next) (if (< 1 (length args)) (macro-error @@ -27,11 +27,11 @@ (cadr args) "')")) (list 'ref (list 'format s (car args)))) - (let [slice (String.substring s 0 (+ (inc idx) next))] + (let [slice (String.slice s 0 (+ (inc idx) next))] (list 'ref (list 'String.append (list 'ref (list 'format slice (car args))) - (fmt-internal (String.substring s (+ (inc idx) next) len) + (fmt-internal (String.slice 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).") diff --git a/core/Interfaces.carp b/core/Interfaces.carp index c77c1910..25697b63 100644 --- a/core/Interfaces.carp +++ b/core/Interfaces.carp @@ -58,3 +58,5 @@ (definterface sqrt (λ [a] a)) (definterface tan (λ [a] a)) (definterface tanh (λ [a] a)) + +(definterface slice (Fn [&a Int Int] a)) diff --git a/core/Pattern.carp b/core/Pattern.carp index aeb173ba..ac2406df 100644 --- a/core/Pattern.carp +++ b/core/Pattern.carp @@ -50,16 +50,16 @@ If you want to replace all occurrences of the pattern, use `-1`.") lidx (Array.length &idx) result (Array.allocate (Int.inc lidx))] (Array.aset-uninitialized! &result 0 - (substring s 0 (if (> lidx 0) @(Array.unsafe-nth &idx 0) (length s)))) + (slice s 0 (if (> lidx 0) @(Array.unsafe-nth &idx 0) (length s)))) (for [i 0 (Int.dec (Array.length &idx))] (let [plen (length (Array.unsafe-nth &strs i))] (Array.aset-uninitialized! &result (Int.inc i) - (substring s (+ @(Array.unsafe-nth &idx i) plen) - @(Array.unsafe-nth &idx (Int.inc i)))))) + (slice s (+ @(Array.unsafe-nth &idx i) plen) + @(Array.unsafe-nth &idx (Int.inc i)))))) (when (> lidx 0) (let [plen (length (Array.unsafe-nth &strs (Int.dec lidx)))] (Array.aset-uninitialized! &result lidx - (suffix-string s (+ @(Array.unsafe-nth &idx (Int.dec lidx)) + (suffix s (+ @(Array.unsafe-nth &idx (Int.dec lidx)) plen))))) result)) @@ -121,13 +121,13 @@ If you want to replace all occurrences of the pattern, use `-1`.") lidx (Array.length &idx) result (Array.allocate (Int.inc lidx))] (Array.aset-uninitialized! &result 0 - (substring s 0 (if (> lidx 0) @(Array.unsafe-nth &idx 0) (length s)))) + (slice s 0 (if (> lidx 0) @(Array.unsafe-nth &idx 0) (length s)))) (for [i 0 (Int.dec (Array.length &idx))] (Array.aset-uninitialized! &result (Int.inc i) - (substring s (Int.inc @(Array.unsafe-nth &idx i)) @(Array.unsafe-nth &idx (Int.inc i))))) + (slice s (Int.inc @(Array.unsafe-nth &idx i)) @(Array.unsafe-nth &idx (Int.inc i))))) (when (> lidx 0) (Array.aset-uninitialized! &result lidx - (suffix-string s (Int.inc @(Array.unsafe-nth &idx (Int.dec lidx)))))) + (suffix s (Int.inc @(Array.unsafe-nth &idx (Int.dec lidx)))))) result)) (doc words "splits a string into words.") diff --git a/core/String.carp b/core/String.carp index b2fdf37f..a2225b4e 100644 --- a/core/String.carp +++ b/core/String.carp @@ -63,24 +63,24 @@ (defn empty? [s] (Int.= (length s) 0)) - (defn substring [s a b] - (from-chars &(Array.subarray &(chars s) a b))) + (defn slice [s a b] + (from-chars &(Array.slice &(chars s) a b))) - (doc prefix-string "Return the first `a` characters of the string `s`.") - (defn prefix-string [s a] - (from-chars &(Array.subarray &(chars s) 0 a))) + (doc prefix "Return the first `a` characters of the string `s`.") + (defn prefix [s a] + (from-chars &(Array.slice &(chars s) 0 a))) - (doc suffix-string "Return the last `b` characters of the string `s`.") - (defn suffix-string [s b] - (from-chars &(Array.subarray &(chars s) b (length s)))) + (doc suffix "Return the last `b` characters of the string `s`.") + (defn suffix [s b] + (from-chars &(Array.slice &(chars s) b (length s)))) (doc starts-with? "Check if the string `s` begins with the string `sub`.") (defn starts-with? [s sub] - (= sub &(prefix-string s (length sub)))) + (= sub &(prefix s (length sub)))) (doc ends-with? "Check if the string `s` ends with the string `sub`.") (defn ends-with? [s sub] - (= sub &(suffix-string s (- (length s) (length sub))))) + (= sub &(suffix s (- (length s) (length sub))))) (doc zero "The empty string.") (defn zero [] @"") diff --git a/core/carp_string.h b/core/carp_string.h index 2bdef0c9..460a7bf7 100644 --- a/core/carp_string.h +++ b/core/carp_string.h @@ -81,8 +81,8 @@ String String_append(const String *a, const String *b) { int total = la + lb + 1; String buffer = CARP_MALLOC(total); memcpy(buffer, *a, la); - memcpy(buffer+la, *b, lb); - buffer[la+lb] = '\0'; + memcpy(buffer + la, *b, lb); + buffer[la + lb] = '\0'; return buffer; } diff --git a/docs/core/Array.html b/docs/core/Array.html index 1c74f4e2..2ae5fc41 100644 --- a/docs/core/Array.html +++ b/docs/core/Array.html @@ -802,19 +802,19 @@
- (λ [(Ref (Array a) b), Int] (Array a)) + (λ [(Ref a b), Int] a)
- (prefix-array xs end-index) + (prefix xs end-index)
gets a prefix array to end-index
.
+ (λ [(Ref (Array a) b), Int, Int] (Array a)) +
++ (slice xs start-index end-index) ++
+
gets a subarray from start-index
to end-index
.
- (λ [(Ref (Array a) b), Int, Int] (Array a)) -
-- (subarray xs start-index end-index) --
-
gets a subarray from start-index
to end-index
.
- (suffix-array xs start-index) + (suffix xs start-index)
gets a suffix array from start-index
.
- (prefix-string s a) + (prefix s a)
Return the first a
characters of the string s
.
- (λ [(Ref String a), Int, Int] String) -
-- (substring s a b) --
- -
-- (suffix-string s b) + (suffix s b)
Return the last b
characters of the string s
.