Skip to content

Commit

Permalink
Added substr function.
Browse files Browse the repository at this point in the history
This closes #86.
  • Loading branch information
skx committed Nov 12, 2022
1 parent 1e8fbf0 commit 8be590b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions stdlib/stdlib/string-substr.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
;;; string-substr.lisp - Fetch substrings from a string

;; String handling is good to have, and here we implement substr in the
;; naive way:
;;
;; Split the string into a list, and take parts of it using "take" and
;; "drop".
;;


(set! substr (fn* (str start &len)
"Return a substring of the given string, by starting index.
The length of the substring is optional."
(if (> start (strlen str)) ; out of bounds?
""
(if (nil? len) ; start at the given offset
(join (drop start (explode str)))
(join (take (car len) (drop start (explode str))))))))

0 comments on commit 8be590b

Please sign in to comment.