-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rkt
55 lines (48 loc) · 1.68 KB
/
utils.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
;; Copyright 2014, 2015 Hamish Ivey-Law
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;; This file contains utility functions of general interest.
;;
#lang racket
(provide string-index-of
string-empty?
string-starts-with?
string-reverse)
(define (string-index-of str ch)
(for/or ([idx (in-range (string-length str))]
#:when (eq? (string-ref str idx) ch))
idx))
(define (string-empty? s)
(string=? s ""))
(define (string-starts-with? s ch)
(if (string-empty? s)
#f
(char=? ch (string-ref s 0))))
;; Somewhat strangely, the implementation used is faster than this
;; commented-out one by around 10-30% on average.
;;
;; (define (string-reverse-2 s)
;; (let* ([len (string-length s)]
;; [t (make-string len)])
;; (for ([i (in-range len)])
;; (string-set! t i (string-ref s (- len i 1))))
;; t))
(define (string-reverse s)
(let ([t (string-copy s)]
[len (string-length s)])
(for ([i (in-range (/ len 2))])
(string-set! t i (string-ref s (- len i 1)))
(string-set! t (- len i 1) (string-ref s i)))
t))