| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
(define check-arg |
| 25 |
(lambda (pred val proc) |
| 26 |
(if (pred val) val (error "Bad arg" val pred proc)))) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
(define (add1 n) (+ 1 n)) |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
(define-syntax let-string-start+end |
| 51 |
(syntax-rules () |
| 52 |
((let-string-start+end (start end) proc s-exp args-exp body ...) |
| 53 |
(receive (start end) (string-parse-final-start+end proc s-exp args-exp) |
| 54 |
body ...)) |
| 55 |
((let-string-start+end (start end rest) proc s-exp args-exp body ...) |
| 56 |
(receive (rest start end) (string-parse-start+end proc s-exp args-exp) |
| 57 |
body ...)))) |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
(define-syntax let-string-start+end2 |
| 62 |
(syntax-rules () |
| 63 |
((l-s-s+e2 (start1 end1 start2 end2) proc s1 s2 args body ...) |
| 64 |
(let ((procv proc)) |
| 65 |
(let-string-start+end (start1 end1 rest) procv s1 args |
| 66 |
(let-string-start+end (start2 end2) procv s2 rest |
| 67 |
body ...)))))) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
(define (string-parse-start+end proc s args) |
| 73 |
(if (not (string? s)) (error "Non-string value" proc s)) |
| 74 |
(let ((slen (string-length s))) |
| 75 |
(if (pair? args) |
| 76 |
|
| 77 |
(let ((start (car args)) |
| 78 |
(args (cdr args))) |
| 79 |
(if (and (integer? start) (exact? start) (>= start 0)) |
| 80 |
(receive (end args) |
| 81 |
(if (pair? args) |
| 82 |
(let ((end (car args)) |
| 83 |
(args (cdr args))) |
| 84 |
(if (and (integer? end) (exact? end) (<= end slen)) |
| 85 |
(values end args) |
| 86 |
(error "Illegal substring END spec" proc end s))) |
| 87 |
(values slen args)) |
| 88 |
(if (<= start end) (values args start end) |
| 89 |
(error "Illegal substring START/END spec" |
| 90 |
proc start end s))) |
| 91 |
(error "Illegal substring START spec" proc start s))) |
| 92 |
|
| 93 |
(values '() 0 slen)))) |
| 94 |
|
| 95 |
(define (string-parse-final-start+end proc s args) |
| 96 |
(receive (rest start end) (string-parse-start+end proc s args) |
| 97 |
(if (pair? rest) (error "Extra arguments to procedure" proc rest) |
| 98 |
(values start end)))) |
| 99 |
|
| 100 |
(define (substring-spec-ok? s start end) |
| 101 |
(and (string? s) |
| 102 |
(integer? start) |
| 103 |
(exact? start) |
| 104 |
(integer? end) |
| 105 |
(exact? end) |
| 106 |
(<= 0 start) |
| 107 |
(<= start end) |
| 108 |
(<= end (string-length s)))) |
| 109 |
|
| 110 |
(define (check-substring-spec proc s start end) |
| 111 |
(if (not (substring-spec-ok? s start end)) |
| 112 |
(error "Illegal substring spec." proc s start end))) |
| 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 |
(define (%substring s start end) |
| 139 |
(if (and (zero? start) (= end (string-length s))) s |
| 140 |
(substring s start end))) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
(define (string-fold kons knil s . maybe-start+end) |
| 157 |
(check-arg procedure? kons string-fold) |
| 158 |
(let-string-start+end (start end) string-fold s maybe-start+end |
| 159 |
(let lp ((v knil) (i start)) |
| 160 |
(if (< i end) (lp (kons (string-ref s i) v) (+ i 1)) |
| 161 |
v)))) |
| 162 |
|
| 163 |
(define (string-fold-right kons knil s . maybe-start+end) |
| 164 |
(check-arg procedure? kons string-fold-right) |
| 165 |
(let-string-start+end (start end) string-fold-right s maybe-start+end |
| 166 |
(let lp ((v knil) (i (- end 1))) |
| 167 |
(if (>= i start) (lp (kons (string-ref s i) v) (- i 1)) |
| 168 |
v)))) |
| 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 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
(define (string-unfold p f g seed . base+make-final) |
| 234 |
(check-arg procedure? p string-unfold) |
| 235 |
(check-arg procedure? f string-unfold) |
| 236 |
(check-arg procedure? g string-unfold) |
| 237 |
(let-optionals* base+make-final |
| 238 |
((base "" (string? base)) |
| 239 |
(make-final (lambda (x) "") (procedure? make-final))) |
| 240 |
(let lp ((chunks '()) |
| 241 |
(nchars 0) |
| 242 |
(chunk (make-string 40)) |
| 243 |
(chunk-len 40) |
| 244 |
(i 0) |
| 245 |
(seed seed)) |
| 246 |
(let lp2 ((i i) (seed seed)) |
| 247 |
(if (not (p seed)) |
| 248 |
(let ((c (f seed)) |
| 249 |
(seed (g seed))) |
| 250 |
(if (< i chunk-len) |
| 251 |
(begin (string-set! chunk i c) |
| 252 |
(lp2 (+ i 1) seed)) |
| 253 |
|
| 254 |
(let* ((nchars2 (+ chunk-len nchars)) |
| 255 |
(chunk-len2 (min 4096 nchars2)) |
| 256 |
(new-chunk (make-string chunk-len2))) |
| 257 |
(string-set! new-chunk 0 c) |
| 258 |
(lp (cons chunk chunks) (+ nchars chunk-len) |
| 259 |
new-chunk chunk-len2 1 seed)))) |
| 260 |
|
| 261 |
|
| 262 |
(let* ((final (make-final seed)) |
| 263 |
(flen (string-length final)) |
| 264 |
(base-len (string-length base)) |
| 265 |
(j (+ base-len nchars i)) |
| 266 |
(ans (make-string (+ j flen)))) |
| 267 |
(%string-copy! ans j final 0 flen) |
| 268 |
(let ((j (- j i))) |
| 269 |
(%string-copy! ans j chunk 0 i) |
| 270 |
(let lp ((j j) (chunks chunks)) |
| 271 |
(if (pair? chunks) |
| 272 |
(let* ((chunk (car chunks)) |
| 273 |
(chunks (cdr chunks)) |
| 274 |
(chunk-len (string-length chunk)) |
| 275 |
(j (- j chunk-len))) |
| 276 |
(%string-copy! ans j chunk 0 chunk-len) |
| 277 |
(lp j chunks))))) |
| 278 |
(%string-copy! ans 0 base 0 base-len) |
| 279 |
ans)))))) |
| 280 |
|
| 281 |
(define (string-unfold-right p f g seed . base+make-final) |
| 282 |
(let-optionals* base+make-final |
| 283 |
((base "" (string? base)) |
| 284 |
(make-final (lambda (x) "") (procedure? make-final))) |
| 285 |
(let lp ((chunks '()) |
| 286 |
(nchars 0) |
| 287 |
(chunk (make-string 40)) |
| 288 |
(chunk-len 40) |
| 289 |
(i 40) |
| 290 |
(seed seed)) |
| 291 |
(let lp2 ((i i) (seed seed)) |
| 292 |
(if (not (p seed)) |
| 293 |
(let ((c (f seed)) |
| 294 |
(seed (g seed))) |
| 295 |
(if (> i 0) |
| 296 |
(let ((i (- i 1))) |
| 297 |
(string-set! chunk i c) |
| 298 |
(lp2 i seed)) |
| 299 |
|
| 300 |
(let* ((nchars2 (+ chunk-len nchars)) |
| 301 |
(chunk-len2 (min 4096 nchars2)) |
| 302 |
(new-chunk (make-string chunk-len2)) |
| 303 |
(i (- chunk-len2 1))) |
| 304 |
(string-set! new-chunk i c) |
| 305 |
(lp (cons chunk chunks) (+ nchars chunk-len) |
| 306 |
new-chunk chunk-len2 i seed)))) |
| 307 |
|
| 308 |
|
| 309 |
(let* ((final (make-final seed)) |
| 310 |
(flen (string-length final)) |
| 311 |
(base-len (string-length base)) |
| 312 |
(chunk-used (- chunk-len i)) |
| 313 |
(j (+ base-len nchars chunk-used)) |
| 314 |
(ans (make-string (+ j flen)))) |
| 315 |
(%string-copy! ans 0 final 0 flen) |
| 316 |
(%string-copy! ans flen chunk i chunk-len) |
| 317 |
(let lp ((j (+ flen chunk-used)) |
| 318 |
(chunks chunks)) |
| 319 |
(if (pair? chunks) |
| 320 |
(let* ((chunk (car chunks)) |
| 321 |
(chunks (cdr chunks)) |
| 322 |
(chunk-len (string-length chunk))) |
| 323 |
(%string-copy! ans j chunk 0 chunk-len) |
| 324 |
(lp (+ j chunk-len) chunks)) |
| 325 |
(%string-copy! ans j base 0 base-len))) |
| 326 |
ans)))))) |
| 327 |
|
| 328 |
|
| 329 |
(define (string-every criterion s . maybe-start+end) |
| 330 |
(let-string-start+end (start end) string-every s maybe-start+end |
| 331 |
(or (= start end) |
| 332 |
(let lp ((i start)) |
| 333 |
(let ((c (string-ref s i)) |
| 334 |
(i1 (+ i 1))) |
| 335 |
(if (= i1 end) (criterion c) |
| 336 |
(and (criterion c) (lp i1)))))))) |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
(define (string-any criterion s . maybe-start+end) |
| 341 |
(let-string-start+end (start end) string-any s maybe-start+end |
| 342 |
(and (< start end) |
| 343 |
(let lp ((i start)) |
| 344 |
(let ((c (string-ref s i)) |
| 345 |
(i1 (+ i 1))) |
| 346 |
(if (= i1 end) (criterion c) |
| 347 |
(or (criterion c) (lp i1)))))))) |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
(define (string-tabulate proc len) |
| 352 |
(check-arg procedure? proc string-tabulate) |
| 353 |
(check-arg (lambda (val) (and (integer? val) (exact? val) (<= 0 val))) |
| 354 |
len string-tabulate) |
| 355 |
(let ((s (make-string len))) |
| 356 |
(do ((i (- len 1) (- i 1))) |
| 357 |
((< i 0)) |
| 358 |
(string-set! s i (proc i))) |
| 359 |
s)) |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
(define (%string-prefix-length s1 start1 end1 s2 start2 end2) |
| 373 |
(let* ((delta (min (- end1 start1) (- end2 start2))) |
| 374 |
(end1 (+ start1 delta))) |
| 375 |
|
| 376 |
(if (and (eq? s1 s2) (= start1 start2)) |
| 377 |
delta |
| 378 |
|
| 379 |
(let lp ((i start1) (j start2)) |
| 380 |
(if (or (>= i end1) |
| 381 |
(not (char=? (string-ref s1 i) |
| 382 |
(string-ref s2 j)))) |
| 383 |
(- i start1) |
| 384 |
(lp (+ i 1) (+ j 1))))))) |
| 385 |
|
| 386 |
(define (%string-suffix-length s1 start1 end1 s2 start2 end2) |
| 387 |
(let* ((delta (min (- end1 start1) (- end2 start2))) |
| 388 |
(start1 (- end1 delta))) |
| 389 |
|
| 390 |
(if (and (eq? s1 s2) (= end1 end2)) |
| 391 |
delta |
| 392 |
|
| 393 |
(let lp ((i (- end1 1)) (j (- end2 1))) |
| 394 |
(if (or (< i start1) |
| 395 |
(not (char=? (string-ref s1 i) |
| 396 |
(string-ref s2 j)))) |
| 397 |
(- (- end1 i) 1) |
| 398 |
(lp (- i 1) (- j 1))))))) |
| 399 |
|
| 400 |
(define (string-prefix-length s1 s2 . maybe-starts+ends) |
| 401 |
(let-string-start+end2 (start1 end1 start2 end2) |
| 402 |
string-prefix-length s1 s2 maybe-starts+ends |
| 403 |
(%string-prefix-length s1 start1 end1 s2 start2 end2))) |
| 404 |
|
| 405 |
(define (string-suffix-length s1 s2 . maybe-starts+ends) |
| 406 |
(let-string-start+end2 (start1 end1 start2 end2) |
| 407 |
string-suffix-length s1 s2 maybe-starts+ends |
| 408 |
(%string-suffix-length s1 start1 end1 s2 start2 end2))) |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
(define (string-prefix? s1 s2 . maybe-starts+ends) |
| 416 |
(let-string-start+end2 (start1 end1 start2 end2) |
| 417 |
string-prefix? s1 s2 maybe-starts+ends |
| 418 |
(%string-prefix? s1 start1 end1 s2 start2 end2))) |
| 419 |
|
| 420 |
(define (string-suffix? s1 s2 . maybe-starts+ends) |
| 421 |
(let-string-start+end2 (start1 end1 start2 end2) |
| 422 |
string-suffix? s1 s2 maybe-starts+ends |
| 423 |
(%string-suffix? s1 start1 end1 s2 start2 end2))) |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
(define (%string-prefix? s1 start1 end1 s2 start2 end2) |
| 428 |
(let ((len1 (- end1 start1))) |
| 429 |
(and (<= len1 (- end2 start2)) |
| 430 |
(= (%string-prefix-length s1 start1 end1 |
| 431 |
s2 start2 end2) |
| 432 |
len1)))) |
| 433 |
|
| 434 |
(define (%string-suffix? s1 start1 end1 s2 start2 end2) |
| 435 |
(let ((len1 (- end1 start1))) |
| 436 |
(and (<= len1 (- end2 start2)) |
| 437 |
(= len1 (%string-suffix-length s1 start1 end1 |
| 438 |
s2 start2 end2))))) |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
(define (string-take s n) |
| 461 |
(check-arg string? s string-take) |
| 462 |
(check-arg (lambda (val) (and (integer? n) (exact? n) |
| 463 |
(<= 0 n (string-length s)))) |
| 464 |
n string-take) |
| 465 |
(%substring s 0 n)) |
| 466 |
|
| 467 |
(define (string-take-right s n) |
| 468 |
(check-arg string? s string-take-right) |
| 469 |
(let ((len (string-length s))) |
| 470 |
(check-arg (lambda (val) (and (integer? n) (exact? n) (<= 0 n len))) |
| 471 |
n string-take-right) |
| 472 |
(%substring s (- len n) len))) |
| 473 |
|
| 474 |
(define (string-drop s n) |
| 475 |
(check-arg string? s string-drop) |
| 476 |
(let ((len (string-length s))) |
| 477 |
(check-arg (lambda (val) (and (integer? n) (exact? n) (<= 0 n len))) |
| 478 |
n string-drop) |
| 479 |
(%substring s n len))) |
| 480 |
|
| 481 |
(define (string-drop-right s n) |
| 482 |
(check-arg string? s string-drop-right) |
| 483 |
(let ((len (string-length s))) |
| 484 |
(check-arg (lambda (val) (and (integer? n) (exact? n) (<= 0 n len))) |
| 485 |
n string-drop-right) |
| 486 |
(%substring s 0 (- len n)))) |
| 487 |
|
| 488 |
|
| 489 |
(define (string-trim s . criterion+start+end) |
| 490 |
(let-optionals* criterion+start+end ((criterion char-whitespace?) rest) |
| 491 |
(let-string-start+end (start end) string-trim s rest |
| 492 |
(cond ((string-skip s criterion start end) => |
| 493 |
(lambda (i) (%substring s i end))) |
| 494 |
(else ""))))) |
| 495 |
|
| 496 |
(define (string-trim-right s . criterion+start+end) |
| 497 |
(let-optionals* criterion+start+end ((criterion char-whitespace?) rest) |
| 498 |
(let-string-start+end (start end) string-trim-right s rest |
| 499 |
(cond ((string-skip-right s criterion start end) => |
| 500 |
(lambda (i) (%substring s start (+ 1 i)))) |
| 501 |
(else ""))))) |
| 502 |
|
| 503 |
(define (string-trim-both s . criterion+start+end) |
| 504 |
(let-optionals* criterion+start+end ((criterion char-whitespace?) rest) |
| 505 |
(let-string-start+end (start end) string-trim-both s rest |
| 506 |
(cond ((string-skip s criterion start end) => |
| 507 |
(lambda (i) |
| 508 |
(%substring s i (+ 1 (string-skip-right s criterion i end))))) |
| 509 |
(else ""))))) |
| 510 |
|
| 511 |
|
| 512 |
(define (string-pad-right s n . char+start+end) |
| 513 |
(let-optionals* char+start+end ((char #\space (char? char)) rest) |
| 514 |
(let-string-start+end (start end) string-pad-right s rest |
| 515 |
(check-arg (lambda (n) (and (integer? n) (exact? n) (<= 0 n))) |
| 516 |
n string-pad-right) |
| 517 |
(let ((len (- end start))) |
| 518 |
(if (<= n len) |
| 519 |
(%substring s start (+ start n)) |
| 520 |
(let ((ans (make-string n char))) |
| 521 |
(%string-copy! ans 0 s start end) |
| 522 |
ans)))))) |
| 523 |
|
| 524 |
(define (string-pad s n . char+start+end) |
| 525 |
(let-optionals* char+start+end ((char #\space (char? char)) rest) |
| 526 |
(let-string-start+end (start end) string-pad s rest |
| 527 |
(check-arg (lambda (n) (and (integer? n) (exact? n) (<= 0 n))) |
| 528 |
n string-pad) |
| 529 |
(let ((len (- end start))) |
| 530 |
(if (<= n len) |
| 531 |
(%substring s (- end n) end) |
| 532 |
(let ((ans (make-string n char))) |
| 533 |
(%string-copy! ans (- n len) s start end) |
| 534 |
ans)))))) |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
(define (string-remove criterion s . maybe-start+end) |
| 549 |
(let-string-start+end (start end) string-remove s maybe-start+end |
| 550 |
(let* ((slen (- end start)) |
| 551 |
(temp (make-string slen)) |
| 552 |
(ans-len (string-fold (lambda (c i) |
| 553 |
(if (criterion c) i |
| 554 |
(begin (string-set! temp i c) |
| 555 |
(+ i 1)))) |
| 556 |
0 s start end))) |
| 557 |
(if (= ans-len slen) temp (substring temp 0 ans-len))))) |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
(define (string-filter criterion s . maybe-start+end) |
| 562 |
(let-string-start+end (start end) string-filter s maybe-start+end |
| 563 |
(let* ((slen (- end start)) |
| 564 |
(temp (make-string slen)) |
| 565 |
(ans-len (string-fold (lambda (c i) |
| 566 |
(if (criterion c) |
| 567 |
(begin (string-set! temp i c) |
| 568 |
(+ i 1)) |
| 569 |
i)) |
| 570 |
0 s start end))) |
| 571 |
(if (= ans-len slen) temp (substring temp 0 ans-len))))) |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
(define (string-index str criterion . maybe-start+end) |
| 586 |
(let-string-start+end (start end) string-index str maybe-start+end |
| 587 |
(let lp ((i start)) |
| 588 |
(and (< i end) |
| 589 |
(if (criterion (string-ref str i)) i |
| 590 |
(lp (+ i 1))))))) |
| 591 |
|
| 592 |
(define (string-index-right str criterion . maybe-start+end) |
| 593 |
(let-string-start+end (start end) string-index-right str maybe-start+end |
| 594 |
(let lp ((i (- end 1))) |
| 595 |
(and (>= i start) |
| 596 |
(if (criterion (string-ref str i)) i |
| 597 |
(lp (- i 1))))))) |
| 598 |
|
| 599 |
(define (string-skip str criterion . maybe-start+end) |
| 600 |
(let-string-start+end (start end) string-skip str maybe-start+end |
| 601 |
(let lp ((i start)) |
| 602 |
(and (< i end) |
| 603 |
(if (criterion (string-ref str i)) (lp (+ i 1)) |
| 604 |
i))))) |
| 605 |
|
| 606 |
(define (string-skip-right str criterion . maybe-start+end) |
| 607 |
(let-string-start+end (start end) string-skip-right str maybe-start+end |
| 608 |
(let lp ((i (- end 1))) |
| 609 |
(and (>= i start) |
| 610 |
(if (criterion (string-ref str i)) (lp (- i 1)) |
| 611 |
i))))) |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
(define (string-take-while s criterion . maybe-start+end) |
| 616 |
(let-string-start+end (start end) string-take-while s maybe-start+end |
| 617 |
(let ((idx (string-skip s criterion start end))) |
| 618 |
(if idx |
| 619 |
(%substring s 0 idx) |
| 620 |
"")))) |
| 621 |
|
| 622 |
(define (string-take-while-right s criterion . maybe-start+end) |
| 623 |
(let-string-start+end (start end) string-take-while s maybe-start+end |
| 624 |
(let ((idx (string-skip-right s criterion start end))) |
| 625 |
(if idx |
| 626 |
(%substring s (+ idx 1) (string-length s)) |
| 627 |
"")))) |
| 628 |
|
| 629 |
(define (string-drop-while s criterion . maybe-start+end) |
| 630 |
(let-string-start+end (start end) string-drop-while s maybe-start+end |
| 631 |
(let ((idx (string-skip s criterion start end))) |
| 632 |
(if idx |
| 633 |
(%substring s idx (string-length s)) |
| 634 |
s)))) |
| 635 |
|
| 636 |
(define (string-drop-while-right s criterion . maybe-start+end) |
| 637 |
(let-string-start+end (start end) string-drop-while s maybe-start+end |
| 638 |
(let ((idx (string-skip-right s criterion start end))) |
| 639 |
(if idx |
| 640 |
(%substring s 0 (+ idx 1)) |
| 641 |
s)))) |
| 642 |
|
| 643 |
(define (string-span s criterion . maybe-start+end) |
| 644 |
(let-string-start+end (start end) string-span s maybe-start+end |
| 645 |
(let ((idx (string-skip s criterion start end))) |
| 646 |
(if idx |
| 647 |
(values (%substring s 0 idx) (%substring s idx (string-length s))) |
| 648 |
(values "" s))))) |
| 649 |
|
| 650 |
(define (string-break s criterion . maybe-start+end) |
| 651 |
(let-string-start+end (start end) string-break s maybe-start+end |
| 652 |
(let ((idx (string-index s criterion start end))) |
| 653 |
(if idx |
| 654 |
(values (%substring s 0 idx) (%substring s idx (string-length s))) |
| 655 |
(values s ""))))) |
| 656 |
|
| 657 |
(define (string-count s criterion . maybe-start+end) |
| 658 |
(let-string-start+end (start end) string-count s maybe-start+end |
| 659 |
(do ((i start (+ i 1)) |
| 660 |
(count 0 (if (criterion (string-ref s i)) (+ count 1) count))) |
| 661 |
((>= i end) count)))) |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
(define (%string-copy! to tstart from fstart fend) |
| 671 |
(if (> fstart tstart) |
| 672 |
(do ((i fstart (+ i 1)) |
| 673 |
(j tstart (+ j 1))) |
| 674 |
((>= i fend)) |
| 675 |
(string-set! to j (string-ref from i))) |
| 676 |
|
| 677 |
(do ((i (- fend 1) (- i 1)) |
| 678 |
(j (+ -1 tstart (- fend fstart)) (- j 1))) |
| 679 |
((< i fstart)) |
| 680 |
(string-set! to j (string-ref from i))))) |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
(define (string-contains text pattern . maybe-starts+ends) |
| 705 |
(let-string-start+end2 (t-start t-end p-start p-end) |
| 706 |
string-contains text pattern maybe-starts+ends |
| 707 |
(%kmp-search pattern text char=? p-start p-end t-start t-end))) |
| 708 |
|
| 709 |
(define (string-contains-right text pattern . maybe-starts+ends) |
| 710 |
(let-string-start+end2 (t-start t-end p-start p-end) |
| 711 |
string-contains-right text pattern maybe-starts+ends |
| 712 |
(let* ((t-len (string-length text)) |
| 713 |
(p-len (string-length pattern)) |
| 714 |
(p-size (- p-end p-start)) |
| 715 |
(rt-start (- t-len t-end)) |
| 716 |
(rt-end (- t-len t-start)) |
| 717 |
(rp-start (- p-len p-end)) |
| 718 |
(rp-end (- p-len p-start)) |
| 719 |
(res (%kmp-search (string-reverse pattern) |
| 720 |
(string-reverse text) |
| 721 |
char=? rp-start rp-end rt-start rt-end))) |
| 722 |
(if res |
| 723 |
(- t-len res p-size) |
| 724 |
#f)))) |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
(define (%kmp-search pattern text c= p-start p-end t-start t-end) |
| 745 |
(let ((plen (- p-end p-start)) |
| 746 |
(rv (make-kmp-restart-vector pattern c= p-start p-end))) |
| 747 |
|
| 748 |
|
| 749 |
(let lp ((ti t-start) (pi 0) |
| 750 |
(tj (- t-end t-start)) |
| 751 |
(pj plen)) |
| 752 |
|
| 753 |
(if (= pi plen) |
| 754 |
(- ti plen) |
| 755 |
(and (<= pj tj) |
| 756 |
(if (c= (string-ref text ti) |
| 757 |
(string-ref pattern (+ p-start pi))) |
| 758 |
(lp (+ 1 ti) (+ 1 pi) (- tj 1) (- pj 1)) |
| 759 |
|
| 760 |
(let ((pi (vector-ref rv pi))) |
| 761 |
(if (= pi -1) |
| 762 |
(lp (+ ti 1) 0 (- tj 1) plen) |
| 763 |
(lp ti pi tj (- plen pi)))))))))) |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
c= char=?procedure? c= |
| 796 |
start endlambdaargs |
| 797 |
string-parse-start+end make-kmp-restart-vector |
| 798 |
pattern args |
| 799 |
rvlen- end start |
| 800 |
rvmake-vector rvlen -1 |
| 801 |
if> rvlen 0 |
| 802 |
letrvlen-1- rvlen 1 |
| 803 |
c0string-ref pattern start |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
let lp1i 0j -1k start |
| 808 |
if< i rvlen-1 |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
let lp2j j |
| 813 |
cond= j -1 |
| 814 |
leti1+ 1 i |
| 815 |
ifnotc=string-ref pattern+ k 1c0 |
| 816 |
vector-set! rv i1 0 |
| 817 |
lp1 i1 0+ k 1 |
| 818 |
|
| 819 |
c=string-ref pattern kstring-ref pattern+ j start |
| 820 |
let*i1+ 1 i |
| 821 |
j1+ 1 j |
| 822 |
vector-set! rv i1 j1 |
| 823 |
lp1 i1 j1+ k 1 |
| 824 |
|
| 825 |
elselp2vector-ref rv j |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
(define (make-kmp-restart-vector pattern . maybe-c=+start+end) |
| 830 |
(let-optionals* maybe-c=+start+end |
| 831 |
((c= char=?) rest) |
| 832 |
(receive (rest2 start end) (string-parse-start+end make-kmp-restart-vector pattern rest) |
| 833 |
(let* ((rvlen (- end start)) |
| 834 |
(rv (make-vector rvlen -1))) |
| 835 |
(if (> rvlen 0) |
| 836 |
(let ((rvlen-1 (- rvlen 1)) |
| 837 |
(c0 (string-ref pattern start))) |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
(let lp1 ((i 0) (j -1) (k start)) |
| 842 |
(if (< i rvlen-1) |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
(let lp2 ((j j)) |
| 848 |
|
| 849 |
(cond ((= j -1) |
| 850 |
(let ((i1 (+ i 1)) |
| 851 |
(ck+1 (string-ref pattern (add1 k)))) |
| 852 |
(vector-set! rv i1 (if (c= ck+1 c0) -1 0)) |
| 853 |
(lp1 i1 0 (+ k 1)))) |
| 854 |
|
| 855 |
|
| 856 |
((c= (string-ref pattern k) |
| 857 |
(string-ref pattern (+ j start))) |
| 858 |
(let* ((i1 (+ 1 i)) |
| 859 |
(j1 (+ 1 j))) |
| 860 |
(vector-set! rv i1 j1) |
| 861 |
(lp1 i1 j1 (+ k 1)))) |
| 862 |
|
| 863 |
(else (lp2 (vector-ref rv j))))))))) |
| 864 |
rv)))) |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
(define (kmp-step pat rv c i c= p-start) |
| 880 |
(let lp ((i i)) |
| 881 |
(if (c= c (string-ref pat (+ i p-start))) |
| 882 |
(+ i 1) |
| 883 |
(let ((i (vector-ref rv i))) |
| 884 |
(if (= i -1) 0 |
| 885 |
(lp i)))))) |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
(define (string-kmp-partial-search pat rv s i . c=+p-start+s-start+s-end) |
| 899 |
|
| 900 |
(let-optionals* c=+p-start+s-start+s-end |
| 901 |
((c= char=?) |
| 902 |
(p-start 0) rest) |
| 903 |
(receive (rest2 s-start s-end) (string-parse-start+end string-kmp-partial-search s rest) |
| 904 |
|
| 905 |
(let ((patlen (vector-length rv))) |
| 906 |
(let lp ((si s-start) |
| 907 |
(vi i)) |
| 908 |
(cond ((= vi patlen) (- si)) |
| 909 |
((= si s-end) vi) |
| 910 |
(else |
| 911 |
(let ((c (string-ref s si))) |
| 912 |
(lp (+ si 1) |
| 913 |
(let lp2 ((vi vi)) |
| 914 |
(if (c= c (string-ref pat (+ vi p-start))) |
| 915 |
(+ vi 1) |
| 916 |
(let ((vi (vector-ref rv vi))) |
| 917 |
(if (= vi -1) 0 |
| 918 |
(lp2 vi))))))))))))) ) |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
(define (string-null? s) (zero? (string-length s))) |
| 928 |
|
| 929 |
(define (string-reverse s . maybe-start+end) |
| 930 |
(let-string-start+end (start end) string-reverse s maybe-start+end |
| 931 |
(let* ((len (- end start)) |
| 932 |
(ans (make-string len))) |
| 933 |
(do ((i start (+ i 1)) |
| 934 |
(j (- len 1) (- j 1))) |
| 935 |
((< j 0)) |
| 936 |
(string-set! ans j (string-ref s i))) |
| 937 |
ans))) |
| 938 |
|
| 939 |
(define (reverse-list->string clist) |
| 940 |
(let* ((len (length clist)) |
| 941 |
(s (make-string len))) |
| 942 |
(do ((i (- len 1) (- i 1)) (clist clist (cdr clist))) |
| 943 |
((not (pair? clist))) |
| 944 |
(string-set! s i (car clist))) |
| 945 |
s)) |
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
(define (string-concatenate strings) |
| 960 |
(let* ((total (do ((strings strings (cdr strings)) |
| 961 |
(i 0 (+ i (string-length (car strings))))) |
| 962 |
((not (pair? strings)) i))) |
| 963 |
(ans (make-string total))) |
| 964 |
(let lp ((i 0) (strings strings)) |
| 965 |
(if (pair? strings) |
| 966 |
(let* ((s (car strings)) |
| 967 |
(slen (string-length s))) |
| 968 |
(%string-copy! ans i s 0 slen) |
| 969 |
(lp (+ i slen) (cdr strings))))) |
| 970 |
ans)) |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
(define (string-concatenate-reverse string-list . maybe-final+end) |
| 985 |
(let-optionals* maybe-final+end ((final "" (string? final)) |
| 986 |
(end (string-length final) |
| 987 |
(and (integer? end) |
| 988 |
(exact? end) |
| 989 |
(<= 0 end (string-length final))))) |
| 990 |
(let ((len (let lp ((sum 0) (lis string-list)) |
| 991 |
(if (pair? lis) |
| 992 |
(lp (+ sum (string-length (car lis))) (cdr lis)) |
| 993 |
sum)))) |
| 994 |
|
| 995 |
(%finish-string-concatenate-reverse len string-list final end)))) |
| 996 |
|
| 997 |
(define (%finish-string-concatenate-reverse len string-list final end) |
| 998 |
(let ((ans (make-string (+ end len)))) |
| 999 |
(%string-copy! ans len final 0 end) |
| 1000 |
(let lp ((i len) (lis string-list)) |
| 1001 |
(if (pair? lis) |
| 1002 |
(let* ((s (car lis)) |
| 1003 |
(lis (cdr lis)) |
| 1004 |
(slen (string-length s)) |
| 1005 |
(i (- i slen))) |
| 1006 |
(%string-copy! ans i s 0 slen) |
| 1007 |
(lp i lis)))) |
| 1008 |
ans)) |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
(define (string-replace s1 s2 start1 end1 . maybe-start+end) |
| 1018 |
(check-substring-spec string-replace s1 start1 end1) |
| 1019 |
(let-string-start+end (start2 end2) string-replace s2 maybe-start+end |
| 1020 |
(let* ((slen1 (string-length s1)) |
| 1021 |
(sublen2 (- end2 start2)) |
| 1022 |
(alen (+ (- slen1 (- end1 start1)) sublen2)) |
| 1023 |
(ans (make-string alen))) |
| 1024 |
(%string-copy! ans 0 s1 0 start1) |
| 1025 |
(%string-copy! ans start1 s2 start2 end2) |
| 1026 |
(%string-copy! ans (+ start1 sublen2) s1 end1 slen1) |
| 1027 |
ans))) |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
(define (string-split s delimiter . args) |
| 1054 |
|
| 1055 |
|
| 1056 |
(if (not (string? s)) (error "string expected" s)) |
| 1057 |
(if (not (string? delimiter)) (error "string expected" delimiter)) |
| 1058 |
(let ((slen (string-length s))) |
| 1059 |
(receive (grammar limit no-limit start end) |
| 1060 |
(if (pair? args) |
| 1061 |
(if (pair? (cdr args)) |
| 1062 |
(if (pair? (cddr args)) |
| 1063 |
(if (pair? (cdddr args)) |
| 1064 |
(values (car args) (cadr args) #f (caddr args) (cadddr args)) |
| 1065 |
(values (car args) (cadr args) #f (caddr args) slen)) |
| 1066 |
(values (car args) (cadr args) #f 0 slen)) |
| 1067 |
(values (car args) #f #t 0 slen)) |
| 1068 |
(values 'infix #f #t 0 slen)) |
| 1069 |
(if (not (memq grammar '(infix strict-infix prefix suffix))) |
| 1070 |
(error "grammar must be one of (infix strict-infix prefix suffix)" grammar)) |
| 1071 |
(if (not limit) (set! no-limit #t)) |
| 1072 |
(if (not (or no-limit |
| 1073 |
(and (integer? limit) (exact? limit) (>= limit 0)))) |
| 1074 |
(error "limit must be exact nonnegative integer or #f" limit)) |
| 1075 |
(if (not (and (integer? start) (exact? start))) |
| 1076 |
(error "start argument must be exact integer" start)) |
| 1077 |
(if (not (<= 0 start slen)) |
| 1078 |
(error "start argument out of range" start)) |
| 1079 |
(if (not (<= 0 end slen)) |
| 1080 |
(error "end argument out of range" end)) |
| 1081 |
(if (not (<= start end)) |
| 1082 |
(error "start argument is greater than end argument" (list start end))) |
| 1083 |
|
| 1084 |
(cond ((= start end) |
| 1085 |
(if (eq? grammar 'strict-infix) |
| 1086 |
(error "empty string cannot be spilt with strict-infix grammar") |
| 1087 |
'())) |
| 1088 |
((string-null? delimiter) |
| 1089 |
(%string-split-chars s start end limit)) |
| 1090 |
(else (%string-split s start end delimiter grammar limit)))))) |
| 1091 |
|
| 1092 |
(define (%string-split-chars s start end limit) |
| 1093 |
(if (not limit) |
| 1094 |
(map string (string->list s start end)) |
| 1095 |
(let loop ((r '()) (c start) (n 0)) |
| 1096 |
(cond ((= c end) (reverse r)) |
| 1097 |
((>= n limit) (reverse (cons (substring s c end) r))) |
| 1098 |
(else (loop (cons (string (string-ref s c)) r) |
| 1099 |
(+ c 1) |
| 1100 |
(+ n 1))))))) |
| 1101 |
|
| 1102 |
(define (%string-split s start end delimiter grammar limit) |
| 1103 |
(let ((dlen (string-length delimiter))) |
| 1104 |
(define (finish r c) |
| 1105 |
(let ((rest (substring s c end))) |
| 1106 |
(if (and (eq? grammar 'suffix) (string-null? rest)) |
| 1107 |
(reverse r) |
| 1108 |
(reverse (cons rest r))))) |
| 1109 |
(define (scan r c n) |
| 1110 |
(if (and limit (>= n limit)) |
| 1111 |
(finish r c) |
| 1112 |
(let ((i (string-contains s delimiter c end))) |
| 1113 |
(if i |
| 1114 |
(let ((fragment (substring s c i))) |
| 1115 |
(if (and (= n 0) (eq? grammar 'prefix) (string-null? fragment)) |
| 1116 |
(scan r (+ i dlen) (+ n 1)) |
| 1117 |
(scan (cons fragment r) |
| 1118 |
(+ i dlen) |
| 1119 |
(+ n 1)))) |
| 1120 |
(finish r c))))) |
| 1121 |
(scan '() start 0))) |
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
(define (string-replicate s from . maybe-to+start+end) |
| 1153 |
(check-arg (lambda (val) (and (integer? val) (exact? val))) |
| 1154 |
from string-replicate) |
| 1155 |
(receive (to start end) |
| 1156 |
(if (pair? maybe-to+start+end) |
| 1157 |
(let-string-start+end (start end) string-replicate s (cdr maybe-to+start+end) |
| 1158 |
(let ((to (car maybe-to+start+end))) |
| 1159 |
(check-arg (lambda (val) (and (integer? val) |
| 1160 |
(exact? val) |
| 1161 |
(<= from val))) |
| 1162 |
to string-replicate) |
| 1163 |
(values to start end))) |
| 1164 |
(let ((slen (string-length (check-arg string? s string-replicate)))) |
| 1165 |
(values (+ from slen) 0 slen))) |
| 1166 |
(let ((slen (- end start)) |
| 1167 |
(anslen (- to from))) |
| 1168 |
(cond ((zero? anslen) "") |
| 1169 |
((zero? slen) (error "Cannot replicate empty (sub)string" |
| 1170 |
string-replicate s from to start end)) |
| 1171 |
|
| 1172 |
((= 1 slen) |
| 1173 |
(make-string anslen (string-ref s start))) |
| 1174 |
|
| 1175 |
|
| 1176 |
((= (floor (/ from slen)) (floor (/ to slen))) |
| 1177 |
(substring s (+ start (modulo from slen)) |
| 1178 |
(+ start (modulo to slen)))) |
| 1179 |
|
| 1180 |
|
| 1181 |
(else (let ((ans (make-string anslen))) |
| 1182 |
(%multispan-repcopy! ans 0 s from to start end) |
| 1183 |
ans)))))) |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
(define (%multispan-repcopy! target tstart s sfrom sto start end) |
| 1189 |
(let* ((slen (- end start)) |
| 1190 |
(i0 (+ start (modulo sfrom slen))) |
| 1191 |
(total-chars (- sto sfrom))) |
| 1192 |
|
| 1193 |
|
| 1194 |
(%string-copy! target tstart s i0 end) |
| 1195 |
|
| 1196 |
(let* ((ncopied (- end i0)) |
| 1197 |
(nleft (- total-chars ncopied)) |
| 1198 |
(nspans (quotient nleft slen))) |
| 1199 |
|
| 1200 |
|
| 1201 |
(do ((i (+ tstart ncopied) (+ i slen)) |
| 1202 |
(nspans nspans (- nspans 1))) |
| 1203 |
((zero? nspans) |
| 1204 |
|
| 1205 |
(%string-copy! target i s start (+ start (- total-chars (- i tstart))))) |
| 1206 |
|
| 1207 |
(%string-copy! target i s start end))))) |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
(define (string-join strings . delim+grammar) |
| 1226 |
(let-optionals* delim+grammar ((delim " " (string? delim)) |
| 1227 |
(grammar 'infix)) |
| 1228 |
(let ((buildit (lambda (lis final) |
| 1229 |
(let recur ((lis lis)) |
| 1230 |
(if (pair? lis) |
| 1231 |
(cons delim (cons (car lis) (recur (cdr lis)))) |
| 1232 |
final))))) |
| 1233 |
|
| 1234 |
(cond ((pair? strings) |
| 1235 |
(string-concatenate |
| 1236 |
(case grammar |
| 1237 |
|
| 1238 |
((infix strict-infix) |
| 1239 |
(cons (car strings) (buildit (cdr strings) '()))) |
| 1240 |
|
| 1241 |
((prefix) (buildit strings '())) |
| 1242 |
|
| 1243 |
((suffix) |
| 1244 |
(cons (car strings) (buildit (cdr strings) (list delim)))) |
| 1245 |
|
| 1246 |
(else (error "Illegal join grammar" |
| 1247 |
grammar string-join))))) |
| 1248 |
|
| 1249 |
((not (null? strings)) |
| 1250 |
(error "STRINGS parameter not list." strings string-join)) |
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
((eq? grammar 'strict-infix) |
| 1255 |
(error "Empty list cannot be joined with STRICT-INFIX grammar." |
| 1256 |
string-join)) |
| 1257 |
|
| 1258 |
(else ""))))) |
| 1259 |
|
| 1260 |
(define (string-segment str k) |
| 1261 |
(if (< k 1) (error "minimum segment size is 1" k)) |
| 1262 |
(let ((len (string-length str))) |
| 1263 |
(let loop ((start 0) |
| 1264 |
(result '())) |
| 1265 |
(if (= start len) |
| 1266 |
(reverse result) |
| 1267 |
(let ((end (min (+ start k) len))) |
| 1268 |
(loop end (cons (%substring str start end) result))))))) |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|