aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/csc/strings-test.csc18
-rw-r--r--lib/csc/strings.csc8
2 files changed, 26 insertions, 0 deletions
diff --git a/lib/csc/strings-test.csc b/lib/csc/strings-test.csc
index 1f981ad..95dbb59 100644
--- a/lib/csc/strings-test.csc
+++ b/lib/csc/strings-test.csc
@@ -42,6 +42,24 @@
(index "def" "a")))
+ (test last-index-last
+ (assert-equal
+ 4
+ (last-index "ababa" "a")))
+
+
+ (test last-index-penultimate
+ (assert-equal
+ 4
+ (last-index "ababab" "a")))
+
+
+ (test last-index-notfound
+ (assert-equal
+ -1
+ (last-index "abcdfads" ";")))
+
+
(test contains
(assert-equal
#t
diff --git a/lib/csc/strings.csc b/lib/csc/strings.csc
index 255a2fd..373f9be 100644
--- a/lib/csc/strings.csc
+++ b/lib/csc/strings.csc
@@ -4,6 +4,7 @@
has-prefix?
index
join
+ last-index
split
str-quote)
(import (scheme base)
@@ -36,6 +37,13 @@
finally (return -1)))
+ (define (last-index s substr)
+ (loop for i downfrom (- (string-length s) (string-length substr)) to 0
+ if (has-prefix? (string-copy s i) substr)
+ return i
+ finally (return -1)))
+
+
(define (contains? s substr)
(if (string=? "" substr)
#t