summaryrefslogtreecommitdiff
path: root/src/scheme/ao_scheme_string.scheme
blob: 10e6fa4f03833d2f4da75ff5d1818ce8391bcfed (plain) (blame)
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
;
; Copyright © 2018 Keith Packard <keithp@keithp.com>
;
; 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 2 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.
;
; string functions placed in ROM

(define string-map
  (lambda (proc . strings)
					; result length is min of arg lengths
    (let* ((l (apply min (map string-length strings)))
					; create the result
	   (s (make-string l)))
					; walk the strings, doing evaluation
      (define (_m p)
	(if (equal? p l)
	    s
	    (begin
	      (string-set! s p (apply proc (map (lambda (s) (string-ref s p)) strings)))
	      (_m (+ p 1))
	      )
	    )
	)
      (_m 0)
      )
    )
  )

(_??_ (string-map (lambda (x) (+ 1 x)) "HAL") "IBM")

(define string-copy!
  (lambda (t a f . args)
    (let ((l 0)
	  (h (string-length f))
	  (o a)
	  (d 1))
					; handle optional start/end args
      
      (if (not (null? args))
	  (begin
	    (set! l (car args))
	    (if (not (null? (cdr args)))
		(set! h (cadr args)))
	    (set! o (- a l))
	    )
	  )
					; flip copy order if dst is
					; after src
      (if (< l a)
	  (begin
	    (set! d h)
	    (set! h (- l 1))
	    (set! l (- d 1))
	    (set! d -1)
	    )
	  )
					; loop copying one at a time
      (do ((p l (+ p d))
	   )
	  ((= p h) t)
	(string-set! t (+ p o) (string-ref f p))
	)
      )
    )
  )

(_??_ (string-copy! (make-string 10) 0 "hello" 0 5) "hello     ")
(_??_ (string-copy! (make-string 10) 1 "hello" 0 5) " hello    ")
(_??_ (string-copy! (make-string 10) 0 "hello" 0 5) "hello     ")

(define (string-upcase s) (string-map char-upcase s))
(define (string-downcase s) (string-map char-downcase s))
(define string-foldcase string-downcase)

(define string-copy
  (lambda (s . args)
    (let ((l 0)
	  (h (string-length s)))
      (if (not (null? args))
	  (begin
	    (set! l (car args))
	    (if (not (null? (cdr args)))
		(set! h (cadr args)))
	    )
	  )
      (string-copy! (make-string (- h l)) 0 s l h)
      )
    )
  )

(_??_ (string-copy "hello" 0 1) "h")
(_??_ (string-copy "hello" 1) "ello")
(_??_ (string-copy "hello") "hello")

(define substring string-copy)

(define string-fill!
  (lambda (s a . args)
    (let ((l 0)
	  (h (string-length s)))
      (cond ((not (null? args))
	     (set! l (car args))
	     (cond ((not (null? (cdr args)))
		    (set! h (cadr args)))
		   )
	     )
	    )
      (define (_f b)
	(cond ((< b h)
	       (string-set! s b a)
	       (_f (+ b 1))
	       )
	      (else s)
	      )
	)
      (_f l)
      )
    )
  )

(_??_ (string-fill! (make-string 10) #\a) "aaaaaaaaaa")
(_??_ (string-fill! (make-string 10) #\a 1 2) " a        ")

(define string-for-each
  (lambda (proc . strings)
					; result length is min of arg lengths
    (let* ((l (apply min (map string-length strings)))
	   )
					; walk the strings, doing evaluation
      (define (_m p)
	(if (equal? p l)
	    #t
	    (begin
	      (apply proc (map (lambda (s) (string-ref s p)) strings))
	      (_m (+ p 1))
	      )
	    )
	)
      (_m 0)
      )
    )
  )

(_??_ (string-for-each write-char "IBM\n") #t)