File: string-utilities.thb

package info (click to toggle)
theme-d 7.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,036 kB
  • sloc: lisp: 9,625; sh: 5,321; makefile: 715; ansic: 477
file content (216 lines) | stat: -rw-r--r-- 8,397 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
;; -*-theme-d-*-

;; Copyright (C) 2008-2019, 2021, 2025 Tommi Höynälänmaa
;; Distributed under GNU Lesser General Public License version 3,
;; see file doc/LGPL-3.

;; The result type safety of the following procedures has been checked:
;;   string-match

(define-body (standard-library string-utilities)
  
  (import (standard-library list-utilities))
  
  (add-method string-append
    (unchecked-prim-proc string-append ((rest <string>)) <string> pure))
  
  (add-method string
    (unchecked-prim-proc string ((rest <character>)) <string> pure))
  
  (add-method string-length
    (unchecked-prim-proc string-length (<string>) <integer> pure))
  
  (add-method string-ref
    (unchecked-prim-proc string-ref (<string> <integer>) <character> pure))
  
  (add-method string-empty?
    (unchecked-prim-proc string-null? (<string>) <boolean> pure))
  
  (define-simple-method substring
      (((str <string>) (i-start <integer>) (i-end <integer>))
       <string>
       force-pure)
    (let ((i-len (string-length str)))
      (if (or (< i-start 0) (< i-end 0)
              (> i-start i-len)
              (> i-end i-len))
          (raise-simple 'substring:index-out-of-range)
          (let-mutable ((result <string> ""))
            (do ((i <integer> i-start (+ i 1))) ((>= i i-end))
              (set! result (string-append result (string (string-ref str i)))))
            result))))
  
  (define-simple-method string-last-char
      (((str <string>)) <character> pure)
    (let ((i-len <integer> (string-length str)))
      (if (> i-len 0)
          (string-ref str (- i-len 1))
          (raise-simple 'string-last-char:empty-string))))
  
  (define-simple-method replace-char
      (((str <string>) (ch-src <character>) (ch-dest <character>))
       <string>
       force-pure)
    (let ((i-len (string-length str)))
      (let-mutable ((str-result <string> ""))
        (do ((i <integer> 0 (+ i 1))) ((>= i i-len))
          (let* ((ch-cur (string-ref str i))
                 (ch-new
                  (if (equal? ch-cur ch-src) ch-dest ch-cur)))
            (set! str-result (string-append str-result (string ch-new)))))
        str-result)))
  
  (define-simple-method replace-char-with-string
      (((str <string>) (ch-src <character>) (str-dest <string>))
       <string>
       force-pure)
    (let ((i-len (string-length str)))
      (let-mutable ((str-result <string> ""))
        (do ((i <integer> 0 (+ i 1))) ((>= i i-len))
          (let* ((ch-cur (string-ref str i))
                 (str-new
                  (if (equal? ch-cur ch-src) str-dest (string ch-cur))))
            (set! str-result (string-append str-result str-new))))
        str-result)))
  
  (define-simple-method split-string (((str <string>)
                                       (ch-split <character>))
                                      (:uniform-list <string>)
                                      force-pure)
    (let ((i-len <integer> (string-length str)))
      (let-mutable ((lst-result (:uniform-list <string>) null)
                    (str-cur <string> ""))
        (do ((i <integer> 0 (+ i 1)))
            ((>= i i-len))
          (let ((ch-cur (string-ref str i)))
            (if (equal? ch-cur ch-split)
                (begin
                 (set! lst-result (append lst-result
                                          (list str-cur)))
                 (set! str-cur ""))
                (set! str-cur (string-append str-cur (string ch-cur))))))
        (append lst-result (list str-cur)))))
  
  (define-simple-method join-strings-with-sep
      (((lst (:uniform-list <string>)) (str-sep <string>)) <string> pure)
    (match-type lst
      ((<null>) "")
      ((lst1 (:nonempty-uniform-list <string>))
       (let ((str-head (car lst1))
             (l-tail (cdr lst1)))
         (if (null? l-tail)
             str-head
             (string-append str-head str-sep
                            (join-strings-with-sep l-tail str-sep)))))))
  
  (define-simple-method string-contains-char?
      (((str <string>) (ch <character>)) <boolean> force-pure)
    (let ((i-len (string-length str)))
      (let-mutable ((found? <boolean> #f))
        (do ((i <integer> 0 (+ i 1))) ((or (>= i i-len) found?))
          (if (equal? (string-ref str i) ch)
              (set! found? #t)))
        found?)))
  
  (define-simple-method string-char-index
      (((str <string>) (ch <character>)) <integer> force-pure)
    (let ((i-len (string-length str)))
      (let-mutable ((i-found <integer> -1))
        (do ((i <integer> 0 (+ i 1))) ((or (>= i i-len) (>= i-found 0)))
          (if (equal? (string-ref str i) ch)
              (set! i-found i)))
        i-found)))
  
  (define-simple-method string-char-index-right
      (((str <string>) (ch <character>))
       <integer>
       pure)
    (let ((i-len (string-length str)))
      (let-mutable ((i-result <integer> -1))
        (do ((i-cur <integer> (- i-len 1) (- i-cur 1)))
            ((or (< i-cur 0) (>= i-result 0)) i-result)
          (if (equal? (string-ref str i-cur) ch)
              (set! i-result i-cur))))))
  
  (define-simple-method string-take
      (((str <string>) (i-count <integer>))
       <string>
       pure)
    (substring str 0 i-count))
  
  (define-simple-method string-drop
      (((str <string>) (i-count <integer>))
       <string>
       pure)
    (let ((i-len (string-length str)))
      (substring str i-count i-len)))
  
  (define-simple-method string-take-right
      (((str <string>) (i-count <integer>))
       <string>
       pure)
    (let ((i-len (string-length str)))
      (if (> i-count i-len)
          (raise-simple 'string-index-out-of-range)
          (substring str (- i-len i-count) i-len))))
  
  (define-simple-method string-drop-right
      (((str <string>) (i-count <integer>))
       <string>
       pure)
    (let ((i-len (string-length str)))
      (if (> i-count i-len)
          (raise-simple 'string-index-out-of-range)
          (substring str 0 (- i-len i-count)))))
  
  (define-simple-method search-substring
      (((str <string>) (str-match <string>)) <integer> pure)
    (let* ((i-len (string-length str))
           (i-match-len (string-length str-match))
           (i-limit (- i-len i-match-len)))
      (if (> i-match-len i-len)
          (raise-simple 'search-substring:index-out-of-range)
          (let-mutable ((i-found <integer> -1))
            (do ((i1 <integer> 0 (+ i1 1)))
                ((or (>= i1 i-limit) (>= i-found 0)) i-found)
              (if
               (let-mutable ((success2? <boolean> #t))
                 (do ((i2 <integer> 0 (+ i2 1)))
                     ((or (>= i2 i-match-len) (not success2?)) success2?)
                   (if (not (character=? (string-ref str (+ i1 i2))
                                         (string-ref str-match i2)))
                       (set! success2? #f))))
               (set! i-found i1)))))))
  
  (define-simple-method search-substring-from-end
      (((str <string>) (str-match <string>)) <integer> pure)
    (let ((i-len (string-length str))
          (i-match-len (string-length str-match)))
      (if (> i-match-len i-len)
          (raise-simple 'search-substring-from-end:index-out-of-range)
          (let-mutable ((i-found <integer> -1))
            (do ((i1 <integer> (- i-len i-match-len) (- i1 1)))
                ((or (< i1 0) (>= i-found 0)) i-found)
              (if
               (let-mutable ((success2? <boolean> #t))
                 (do ((i2 <integer> 0 (+ i2 1)))
                     ((or (>= i2 i-match-len) (not success2?)) success2?)
                   (if (not (character=? (string-ref str (+ i1 i2))
                                         (string-ref str-match i2)))
                       (set! success2? #f))))
               (set! i-found i1)))))))
  
  (add-method string-match
    (unchecked-prim-proc theme-string-match (<string> <string>)
                         <string-match-result>
                         pure))
  
  (define-simple-method string-exact-match?
      (((str-pattern <string>) (str-source <string>)) <boolean> pure)
    (let ((tp-match <string-match-result>
                    (string-match str-pattern str-source)))
      (match-type tp-match
        ((tp1 (:tuple <string> <integer> <integer>))
         (and (equal? (tuple-ref tp1 1) 0)
              (equal? (tuple-ref tp1 2) (string-length str-source))))
        (else #f)))))