From ee812b37e60d19b34e078f8b7e392667e2b71df8 Mon Sep 17 00:00:00 2001 From: hellerve Date: Tue, 11 Feb 2020 09:09:30 +0100 Subject: [PATCH] core: make subarry/substring slice --- bench/array_subarray.carp | 2 +- core/Array.carp | 16 +++++------ core/Dynamic.carp | 10 +++---- core/Filepath.carp | 2 +- core/Format.carp | 10 +++---- core/Interfaces.carp | 2 ++ core/Pattern.carp | 14 +++++----- core/String.carp | 20 +++++++------- core/carp_string.h | 4 +-- docs/core/Array.html | 58 +++++++++++++++++++-------------------- docs/core/String.html | 54 ++++++++++++++++++------------------ examples/basics.carp | 4 +-- out/keep.txt | 1 - src/StartingEnv.hs | 2 +- test/array.carp | 12 ++++---- test/memory.carp | 14 +++++----- test/string.carp | 12 ++++---- 17 files changed, 119 insertions(+), 118 deletions(-) delete mode 100644 out/keep.txt 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 @@

- -

- prefix-array + +

+ prefix

defn

- (λ [(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.

@@ -1066,6 +1066,26 @@

+
+ +

+ slice +

+
+
+ defn +
+

+ (λ [(Ref (Array a) b), Int, Int] (Array a)) +

+
+                    (slice xs start-index end-index)
+                
+

+

gets a subarray from start-index to end-index.

+ +

+

@@ -1207,29 +1227,9 @@

- -

- subarray -

-
-
- defn -
-

- (λ [(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 + +

+ suffix

@@ -1239,7 +1239,7 @@ (λ [(Ref (Array a) b), Int] (Array a))

-                    (suffix-array xs start-index)
+                    (suffix xs start-index)
                 

gets a suffix array from start-index.

diff --git a/docs/core/String.html b/docs/core/String.html index a9777026..310b324e 100644 --- a/docs/core/String.html +++ b/docs/core/String.html @@ -817,9 +817,9 @@

- -

- prefix-string + +

+ prefix

@@ -829,7 +829,7 @@ (λ [(Ref String a), Int] String)

-                    (prefix-string s a)
+                    (prefix s a)
                 

Return the first a characters of the string s.

@@ -914,6 +914,25 @@

+
+ +

+ slice +

+
+
+ defn +
+

+ (λ [(Ref String a), Int, Int] String) +

+
+                    (slice s a b)
+                
+

+ +

+
- -

- substring -

-
-
- defn -
-

- (λ [(Ref String a), Int, Int] String) -

-
-                    (substring s a b)
-                
-

- -

-
-
- -

- suffix-string + +

+ suffix

@@ -1043,7 +1043,7 @@ (λ [(Ref String a), Int] String)

-                    (suffix-string s b)
+                    (suffix s b)
                 

Return the last b characters of the string s.

diff --git a/examples/basics.carp b/examples/basics.carp index 5e9cc554..fee76dee 100644 --- a/examples/basics.carp +++ b/examples/basics.carp @@ -183,8 +183,8 @@ (defn two-lengths-in-same-func [] (let [a [[1]] b [1 2 3] - c1 (length &a) - c2 (length &b)] + c1 (Array.length &a) + c2 (Array.length &b)] (println* (+ c1 c2)))) (defn changing-target-of-ref [] diff --git a/out/keep.txt b/out/keep.txt deleted file mode 100644 index 251cf3c3..00000000 --- a/out/keep.txt +++ /dev/null @@ -1 +0,0 @@ -Keep this directory. diff --git a/src/StartingEnv.hs b/src/StartingEnv.hs index dde4f487..dcf02098 100644 --- a/src/StartingEnv.hs +++ b/src/StartingEnv.hs @@ -321,7 +321,7 @@ dynamicStringModule = Env { envBindings = bindings , envFunctionNestingLevel = 0 } where bindings = Map.fromList [ addCommand "char-at" 2 commandCharAt , addCommand "index-of" 2 commandIndexOf - , addCommand "substring" 3 commandSubstring + , addCommand "slice" 3 commandSubstring , addCommand "length" 1 commandStringLength , addCommand "join" 1 commandStringJoin , addCommand "directory" 1 commandStringDirectory diff --git a/test/array.carp b/test/array.carp index b62da71d..30848a85 100644 --- a/test/array.carp +++ b/test/array.carp @@ -105,16 +105,16 @@ "sum works as expected") (assert-equal test &[2 3] - &(subarray &(range 1 10 1) 1 3) - "subarray works as expected") + &(slice &(range 1 10 1) 1 3) + "slice works as expected") (assert-equal test &[1 2 3] - &(prefix-array &(range 1 10 1) 3) - "prefix-array works as expected") + &(prefix &(range 1 10 1) 3) + "prefix works as expected") (assert-equal test &[8 9 10] - &(suffix-array &(range 1 10 1) 7) - "suffix-array works as expected") + &(suffix &(range 1 10 1) 7) + "suffix works as expected") (assert-equal test &(Maybe.Nothing) &(nth &a 100) diff --git a/test/memory.carp b/test/memory.carp index 34a59ec8..6a33950b 100644 --- a/test/memory.carp +++ b/test/memory.carp @@ -181,9 +181,9 @@ (defn if-6 [] (branch-in-let false)) -(defn string-substring [] +(defn string-slice [] (let [s1 @"abcde" - s2 (String.substring &s1 0 3)] + s2 (String.slice &s1 0 3)] (assert (= "abc" &s2)))) (defn array-aset [] @@ -241,9 +241,9 @@ (let [xs [10 20 30 40 50]] (assert (= 150 (sum &xs))))) -(defn array-subarray [] +(defn array-slice [] (let [xs [@"a" @"b" @"c" @"d" @"e"]] - (assert (= &[@"c" @"d"] &(Array.subarray &xs 2 4))))) + (assert (= &[@"c" @"d"] &(Array.slice &xs 2 4))))) (defn array-reverse-1 [] (let [xs [@"a" @"b" @"c" @"d" @"e"]] @@ -259,7 +259,7 @@ (assert (= 3 (Array.element-count &xs "a"))))) (defn first-letter [s] - (String.substring s 0 1)) + (String.slice s 0 1)) (defn array-aupdate [] (let [xs [@"abc" @"xyz"] @@ -458,7 +458,7 @@ (assert-no-leak test if-4 "if-4 does not leak") (assert-no-leak test if-5 "if-5 does not leak") (assert-no-leak test if-6 "if-6 does not leak") - (assert-no-leak test string-substring "string-substring does not leak") + (assert-no-leak test string-slice "string-slice does not leak") (assert-no-leak test array-aset "array-aset does not leak") (assert-no-leak test array-reduce "array-reduce does not leak") (assert-no-leak test array-endo-filter "array-endo-filter does not leak") @@ -470,7 +470,7 @@ (assert-no-leak test array-maximum "array-maximum does not leak") (assert-no-leak test array-minimum "array-minimum does not leak") (assert-no-leak test array-sum "array-sum does not leak") - (assert-no-leak test array-subarray "array-subarray does not leak") + (assert-no-leak test array-slice "array-slice does not leak") (assert-no-leak test array-reverse-1 "array-reverse-2 does not leak") (assert-no-leak test array-index-of "array-index-of does not leak") (assert-no-leak test array-element-count "array-element-count does not leak") diff --git a/test/string.carp b/test/string.carp index e91d26ef..a7916d60 100644 --- a/test/string.carp +++ b/test/string.carp @@ -84,16 +84,16 @@ "from-chars works as expected") (assert-equal test "edan" - &(substring "svedang" 2 6) - "substring works as expected") + &(slice "svedang" 2 6) + "slice works as expected") (assert-equal test "sved" - &(prefix-string "svedang" 4) - "prefix-string works as expected") + &(prefix "svedang" 4) + "prefix works as expected") (assert-equal test "dang" - &(suffix-string "svedang" 3) - "suffix-string works as expected") + &(suffix "svedang" 3) + "suffix works as expected") (assert-true test (ends-with? "heller" "ler") "ends-with? works as expected")