| 1 |
;;; |
| 2 |
;;;libstr.scm - built-in string library |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(select-module gauche.internal) |
| 35 |
|
| 36 |
(inline-stub |
| 37 |
(.include "gauche/priv/configP.h" |
| 38 |
"gauche/vminsn.h" |
| 39 |
"gauche/priv/bignumP.h" |
| 40 |
"gauche/priv/stringP.h" |
| 41 |
"gauche/priv/writerP.h")) |
| 42 |
|
| 43 |
;; Ellipsis, used when output is truncated. |
| 44 |
(select-module gauche.internal) |
| 45 |
(define-cproc string-ellipsis () |
| 46 |
(let* ([elli::(static ScmObj) SCM_UNDEFINED]) |
| 47 |
(when (SCM_UNDEFINEDP elli) |
| 48 |
(set! elli (SCM_MAKE_STR_IMMUTABLE SCM_WRITTEN_ELLIPSIS))) |
| 49 |
(return elli))) |
| 50 |
|
| 51 |
;; |
| 52 |
;; Predicates |
| 53 |
;; |
| 54 |
|
| 55 |
(select-module scheme) |
| 56 |
(define-cproc string? (obj) ::<boolean> :fast-flonum :constant |
| 57 |
(inliner STRINGP) SCM_STRINGP) |
| 58 |
|
| 59 |
(select-module gauche) |
| 60 |
(define-cproc string-incomplete? (obj) ::<boolean> |
| 61 |
(return (and (SCM_STRINGP obj) (SCM_STRING_INCOMPLETE_P obj)))) |
| 62 |
(define-cproc string-immutable? (obj) ::<boolean> |
| 63 |
(return (and (SCM_STRINGP obj) (SCM_STRING_IMMUTABLE_P obj)))) |
| 64 |
|
| 65 |
;; |
| 66 |
;; Constructors |
| 67 |
;; |
| 68 |
|
| 69 |
(select-module scheme) |
| 70 |
(define-cproc make-string (len::<fixnum> :optional (c::<char> #\space)) |
| 71 |
Scm_MakeFillString) |
| 72 |
(define-cproc string (:rest chars) Scm_ListToString) |
| 73 |
(define-cproc string-copy (str::<string> :optional start end) |
| 74 |
(return (Scm_CopyString (SCM_STRING (Scm_MaybeSubstring str start end))))) |
| 75 |
|
| 76 |
(define-cproc string-append (:rest args) Scm_StringAppend) |
| 77 |
|
| 78 |
(select-module gauche) |
| 79 |
(define-cproc string-copy-immutable (str::<string> :optional start end) |
| 80 |
(let* ([s (Scm_MaybeSubstring str start end)]) |
| 81 |
(if (SCM_STRING_IMMUTABLE_P s) |
| 82 |
(return s) |
| 83 |
(return (Scm_CopyStringWithFlags (SCM_STRING s) |
| 84 |
SCM_STRING_IMMUTABLE |
| 85 |
SCM_STRING_IMMUTABLE))))) |
| 86 |
|
| 87 |
(define-cproc string-join (strs::<list> |
| 88 |
:optional (delim::<string> " ") (grammar infix)) |
| 89 |
(let* ([gm::int 0]) |
| 90 |
(cond |
| 91 |
[(SCM_EQ grammar 'infix) (set! gm SCM_STRING_JOIN_INFIX)] |
| 92 |
[(SCM_EQ grammar 'strict-infix) (set! gm SCM_STRING_JOIN_STRICT_INFIX)] |
| 93 |
[(SCM_EQ grammar 'suffix) (set! gm SCM_STRING_JOIN_SUFFIX)] |
| 94 |
[(SCM_EQ grammar 'prefix) (set! gm SCM_STRING_JOIN_PREFIX)] |
| 95 |
[else (SCM_TYPE_ERROR grammar "one of the symbols infix, strict-infix, \ |
| 96 |
suffix, or prefix")]) |
| 97 |
(return (Scm_StringJoin strs delim gm)))) |
| 98 |
|
| 99 |
(define-reader-ctor 'string-interpolate |
| 100 |
(^ args |
| 101 |
(apply string-interpolate args))) ;;lambda is required to delay loading |
| 102 |
|
| 103 |
;; |
| 104 |
;; Conversions |
| 105 |
;; |
| 106 |
|
| 107 |
(select-module scheme) |
| 108 |
(define-cproc string->list (str::<string> :optional start end) |
| 109 |
(return (Scm_StringToList (SCM_STRING (Scm_MaybeSubstring str start end))))) |
| 110 |
(define-cproc list->string (list::<list>) Scm_ListToString) |
| 111 |
|
| 112 |
;; |
| 113 |
;; Accessors |
| 114 |
;; |
| 115 |
|
| 116 |
(select-module scheme) |
| 117 |
(define-cproc string-length (str::<string>) ::<fixnum> :constant |
| 118 |
(return (SCM_STRING_BODY_LENGTH (SCM_STRING_BODY str)))) |
| 119 |
(define-cproc string-ref (str::<string> k :optional fallback) |
| 120 |
:constant |
| 121 |
(let* ([r::ScmChar (Scm_StringRefCursor str k (SCM_UNBOUNDP fallback))]) |
| 122 |
(return (?: (== r SCM_CHAR_INVALID) fallback (SCM_MAKE_CHAR r))))) |
| 123 |
(define-cproc substring (str::<string> start end) |
| 124 |
(return (Scm_SubstringCursor str start end))) |
| 125 |
|
| 126 |
(select-module gauche) |
| 127 |
(define-cproc string-size (str::<string>) ::<fixnum> :constant |
| 128 |
(return (SCM_STRING_BODY_SIZE (SCM_STRING_BODY str)))) |
| 129 |
|
| 130 |
(define-cproc opt-substring (str::<string> :optional start end) |
| 131 |
Scm_MaybeSubstring) |
| 132 |
|
| 133 |
;; bound argument is for SRFI-13 |
| 134 |
(define-cproc %hash-string (str::<string> :optional bound) ::<ulong> |
| 135 |
(let* ([modulo::u_long 0]) |
| 136 |
(cond [(or (SCM_UNBOUNDP bound) (SCM_UNDEFINEDP bound)) |
| 137 |
(set! modulo 0)] |
| 138 |
[(SCM_INTP bound) (set! modulo (SCM_INT_VALUE bound))] |
| 139 |
[(SCM_BIGNUMP bound) |
| 140 |
(set! modulo |
| 141 |
(Scm_BignumToUI (SCM_BIGNUM bound) SCM_CLAMP_BOTH NULL))] |
| 142 |
[else (Scm_Error "argument out of domain: %S" bound)]) |
| 143 |
(return (Scm_HashString str modulo)))) |
| 144 |
|
| 145 |
(select-module gauche) |
| 146 |
(inline-stub |
| 147 |
(define-cfn string-scan-mode (mode) ::int |
| 148 |
(let* ([rmode::int 0]) |
| 149 |
(cond |
| 150 |
[(SCM_EQ mode 'index) (set! rmode SCM_STRING_SCAN_INDEX)] |
| 151 |
[(SCM_EQ mode 'cursor) (set! rmode SCM_STRING_SCAN_CURSOR)] |
| 152 |
[(SCM_EQ mode 'before) (set! rmode SCM_STRING_SCAN_BEFORE)] |
| 153 |
[(SCM_EQ mode 'after) (set! rmode SCM_STRING_SCAN_AFTER)] |
| 154 |
[(SCM_EQ mode 'before*) (set! rmode SCM_STRING_SCAN_BEFORE2)] |
| 155 |
[(SCM_EQ mode 'after*) (set! rmode SCM_STRING_SCAN_AFTER2)] |
| 156 |
[(SCM_EQ mode 'both) (set! rmode SCM_STRING_SCAN_BOTH)] |
| 157 |
[else (Scm_Error "bad value in mode argumet: %S, must be one of \ |
| 158 |
'index, 'before, 'after, 'before*, 'after* or 'both." mode)]) |
| 159 |
(return rmode)))) |
| 160 |
|
| 161 |
;; primitive scanner |
| 162 |
(define-cproc string-scan (s1::<string> s2 :optional (mode index)) |
| 163 |
(let* ([rmode::int (string-scan-mode mode)]) |
| 164 |
(cond |
| 165 |
[(SCM_STRINGP s2) (return (Scm_StringScan s1 (SCM_STRING s2) rmode))] |
| 166 |
[(SCM_CHARP s2) |
| 167 |
(return (Scm_StringScanChar s1 (SCM_CHAR_VALUE s2) rmode))] |
| 168 |
[else (Scm_Error "bad type of argument for s2: %S, must be \ |
| 169 |
either string or character" s2) |
| 170 |
(return SCM_UNDEFINED)]))) |
| 171 |
|
| 172 |
(define-cproc string-scan-right (s1::<string> s2 :optional (mode index)) |
| 173 |
(let* ([rmode::int (string-scan-mode mode)]) |
| 174 |
(cond |
| 175 |
[(SCM_STRINGP s2) (return (Scm_StringScanRight s1 (SCM_STRING s2) rmode))] |
| 176 |
[(SCM_CHARP s2) |
| 177 |
(return (Scm_StringScanCharRight s1 (SCM_CHAR_VALUE s2) rmode))] |
| 178 |
[else (Scm_Error "bad type of argument for s2: %S, must be \ |
| 179 |
either string or character" s2) |
| 180 |
(return SCM_UNDEFINED)]))) |
| 181 |
|
| 182 |
;; string-split |
| 183 |
;; Generic string-split |
| 184 |
;; splitter can be a character, a char-set, a string, or a regexp. |
| 185 |
;; NB: To adapt to SRFI-152, we changed the optional argument - now the |
| 186 |
;; 'grammar' argument comes before 'limit'. For the backward compatibility, |
| 187 |
;; we recognize an integer argument in 'grammar' as 'limit'. |
| 188 |
(define (string-split string splitter |
| 189 |
:optional (grammar 'infix) (limit #f) start end) |
| 190 |
(if (or (not grammar) (integer? grammar)) |
| 191 |
((with-module gauche.internal %string-split) |
| 192 |
string splitter 'infix grammar limit start) |
| 193 |
((with-module gauche.internal %string-split) |
| 194 |
string splitter grammar limit start end))) |
| 195 |
|
| 196 |
(select-module gauche.internal) |
| 197 |
(define-cproc %string-split-by-char (s::<string> ch::<char> |
| 198 |
:optional (limit::<int> -1)) |
| 199 |
Scm_StringSplitByCharWithLimit) |
| 200 |
|
| 201 |
(define (%string-split string splitter grammar limit start end) |
| 202 |
(unless (memq grammar '(infix strict-infix prefix suffix)) |
| 203 |
(error "grammar argument must be one of (infix strict-infix prefix suffix), but got" grammar)) |
| 204 |
(unless (or (not limit) (and (integer? limit) (>= limit 0))) |
| 205 |
(error "limit argument must be a nonnegative integer or #f, but got" limit)) |
| 206 |
(let1 s (opt-substring string start end) |
| 207 |
(if (equal? s "") |
| 208 |
(if (eq? grammar 'strict-infix) |
| 209 |
(error "string must not be empty with strict-infix grammar") |
| 210 |
'()) |
| 211 |
(let1 r (if (char? splitter) |
| 212 |
(%string-split-by-char s splitter (or limit -1)) |
| 213 |
(%string-split-by-scanner s (%string-split-scanner splitter) |
| 214 |
(or limit -1))) |
| 215 |
(case grammar |
| 216 |
[(prefix) (if (and (pair? r) (equal? (car r) "")) |
| 217 |
(cdr r) |
| 218 |
r)] |
| 219 |
[(suffix) (if (and (pair? r) (equal? (car (last-pair r)) "")) |
| 220 |
(drop-right r 1) |
| 221 |
r)] |
| 222 |
[else r]))))) |
| 223 |
|
| 224 |
;; aux fns |
| 225 |
(define (%string-split-scanner splitter) |
| 226 |
(cond [(string? splitter) |
| 227 |
(if (string=? splitter "") |
| 228 |
(^s (if (<= (string-length s) 1) |
| 229 |
(values s #f) |
| 230 |
(values (string-copy s 0 1) (string-copy s 1)))) |
| 231 |
(^s (receive (before after) (string-scan s splitter 'both) |
| 232 |
(if before (values before after) (values s #f)))))] |
| 233 |
[(char-set? splitter) |
| 234 |
(%string-split-scanner-each-char |
| 235 |
(cut char-set-contains? splitter <>))] |
| 236 |
[(regexp? splitter) |
| 237 |
(^s (cond [(rxmatch splitter s) |
| 238 |
=> (^m (let ([before (m 'before)] |
| 239 |
[after (m 'after)]) |
| 240 |
(if (string=? s after) |
| 241 |
(if (<= (string-length s) 1) |
| 242 |
(values s #f) |
| 243 |
(values (string-copy s 0 1) |
| 244 |
(string-copy s 1))) |
| 245 |
(values before after))))] |
| 246 |
[else (values s #f)]))] |
| 247 |
[else ;; assume splitter is a predicate |
| 248 |
(%string-split-scanner-each-char splitter)])) |
| 249 |
|
| 250 |
(define (%string-split-scanner-each-char pred) |
| 251 |
(define (cursor-substr s cur) |
| 252 |
(substring s (string-cursor-start s) cur)) |
| 253 |
|
| 254 |
(define (scan-in s cur end) |
| 255 |
(cond [(string-cursor=? cur end) |
| 256 |
(values (cursor-substr s cur) #f)] |
| 257 |
[(pred (string-ref s cur)) |
| 258 |
(scan-out s (string-cursor-next s cur) end (cursor-substr s cur))] |
| 259 |
[else |
| 260 |
(scan-in s (string-cursor-next s cur) end)])) |
| 261 |
|
| 262 |
(define (scan-out s cur end before) |
| 263 |
(cond [(string-cursor=? cur end) |
| 264 |
(values before "")] |
| 265 |
[(pred (string-ref s cur)) |
| 266 |
(scan-out s (string-cursor-next s cur) end before)] |
| 267 |
[else |
| 268 |
(values before (substring s cur end))])) |
| 269 |
|
| 270 |
(^s (scan-in s (string-cursor-start s) (string-cursor-end s)))) |
| 271 |
|
| 272 |
(define (%string-split-by-scanner string scanner limit) |
| 273 |
(let loop ([s string] |
| 274 |
[r '()] |
| 275 |
[limit limit]) |
| 276 |
(if (zero? limit) |
| 277 |
(reverse! (cons s r)) |
| 278 |
(receive (before after) (scanner s) |
| 279 |
(if after |
| 280 |
(loop after (cons before r) (- limit 1)) |
| 281 |
(reverse! (cons before r))))))) |
| 282 |
|
| 283 |
;; Modifying string |
| 284 |
;; They are just for backward compatibility, and they are expensive |
| 285 |
;; anyway, so we don't care their performance. |
| 286 |
|
| 287 |
(select-module gauche.internal) |
| 288 |
(define-cproc %string-replace-body! (target::<string> source::<string>) |
| 289 |
(return (Scm_StringReplaceBody target (SCM_STRING_BODY source)))) |
| 290 |
|
| 291 |
(define-in-module scheme (string-set! str k ch) |
| 292 |
(check-arg string? str) |
| 293 |
(check-arg char? ch) |
| 294 |
(let1 cur (string-index->cursor str k) |
| 295 |
(%string-replace-body! str |
| 296 |
(string-append (substring str 0 cur) |
| 297 |
(string ch) |
| 298 |
(string-copy str (string-cursor-next str k)))))) |
| 299 |
|
| 300 |
(set! (setter string-ref) string-set!) |
| 301 |
|
| 302 |
(define-in-module gauche (string-byte-set! str k b) |
| 303 |
(check-arg string? str) |
| 304 |
(check-arg integer? k) |
| 305 |
(check-arg exact? k) |
| 306 |
(check-arg integer? b) |
| 307 |
(let ([siz (string-size str)] |
| 308 |
[out (open-output-string :private? #t)]) |
| 309 |
(when (or (< k 0) (<= siz k)) |
| 310 |
(error "string index out of range:" k)) |
| 311 |
(display (byte-substring str 0 k) out) |
| 312 |
(write-byte b out) |
| 313 |
(display (byte-substring str (+ k 1) siz) out) |
| 314 |
(%string-replace-body! str (get-output-byte-string out)))) |
| 315 |
|
| 316 |
(set! (setter string-byte-ref) string-byte-set!) |
| 317 |
|
| 318 |
(define-in-module scheme (string-fill! str c |
| 319 |
:optional (start 0) |
| 320 |
(end (string-length str))) |
| 321 |
(let ([start (string-index->cursor str start)] |
| 322 |
[end (string-index->cursor str end)] |
| 323 |
[istart (string-cursor->index str start)] |
| 324 |
[iend (string-cursor->index str end)] |
| 325 |
[len (string-length str)]) |
| 326 |
(when (< iend istart) |
| 327 |
(errorf "end index ~s is smaller than start index ~s" iend istart)) |
| 328 |
(if (and (= istart 0) (= iend len)) |
| 329 |
(%string-replace-body! str (make-string len c)) |
| 330 |
(%string-replace-body! str |
| 331 |
(string-append (substring str 0 start) |
| 332 |
(make-string (- iend istart) c) |
| 333 |
(string-copy str end)))))) |
| 334 |
|
| 335 |
;; Build index. |
| 336 |
;; Technically, string-build-index mutates StringBody, but it's an idempotent |
| 337 |
;; operation and can be applied on immutable strings. |
| 338 |
(select-module gauche) |
| 339 |
(define-cproc string-build-index! (s::<string>) ::<string> |
| 340 |
(Scm_StringBodyBuildIndex (cast ScmStringBody* (SCM_STRING_BODY s))) |
| 341 |
(return s)) |
| 342 |
|
| 343 |
(define-cproc string-fast-indexable? (s::<string>) ::<boolean> |
| 344 |
(return (Scm_StringBodyFastIndexableP (SCM_STRING_BODY s)))) |
| 345 |
|
| 346 |
(select-module gauche.internal) |
| 347 |
(define-cproc %string-index-dump (s::<string> :optional (p::<port> (current-output-port))) ::<void> |
| 348 |
(Scm_StringBodyIndexDump (SCM_STRING_BODY s) p)) |
| 349 |
|
| 350 |
;; |
| 351 |
;; Comparison |
| 352 |
;; |
| 353 |
|
| 354 |
(select-module scheme) |
| 355 |
(inline-stub |
| 356 |
(define-cise-expr strcmp [(_ op) `(,op (Scm_StringCmp s1 s2) 0)]) |
| 357 |
(define-cise-expr strcmpi [(_ op) `(,op (Scm_StringCiCmp s1 s2) 0)]) |
| 358 |
|
| 359 |
(define-cise-stmt strcmp-multiarg |
| 360 |
[(_ cmp) |
| 361 |
`(while TRUE |
| 362 |
(if ,cmp |
| 363 |
(cond [(SCM_NULLP ss) (return TRUE) break] |
| 364 |
[(not (SCM_STRINGP (SCM_CAR ss))) |
| 365 |
(SCM_TYPE_ERROR (SCM_CAR ss) "string")] |
| 366 |
[else (set! s1 s2) (set! s2 (SCM_STRING (SCM_CAR ss))) |
| 367 |
(set! ss (SCM_CDR ss))]) |
| 368 |
(begin (return FALSE) break)))]) |
| 369 |
) |
| 370 |
|
| 371 |
(define-cproc string=? (s1::<string> s2::<string> :rest ss) |
| 372 |
::<boolean> :constant |
| 373 |
(strcmp-multiarg (Scm_StringEqual s1 s2))) |
| 374 |
(define-cproc string<? (s1::<string> s2::<string> :rest ss) |
| 375 |
::<boolean> :constant |
| 376 |
(strcmp-multiarg (strcmp <))) |
| 377 |
(define-cproc string>? (s1::<string> s2::<string> :rest ss) |
| 378 |
::<boolean> :constant |
| 379 |
(strcmp-multiarg (strcmp >))) |
| 380 |
(define-cproc string<=? (s1::<string> s2::<string> :rest ss) |
| 381 |
::<boolean> :constant |
| 382 |
(strcmp-multiarg (strcmp <=))) |
| 383 |
(define-cproc string>=? (s1::<string> s2::<string> :rest ss) |
| 384 |
::<boolean> :constant |
| 385 |
(strcmp-multiarg (strcmp >=))) |
| 386 |
|
| 387 |
(define-cproc string-ci=? (s1::<string> s2::<string> :rest ss) |
| 388 |
::<boolean> :constant |
| 389 |
(strcmp-multiarg (strcmpi ==))) |
| 390 |
(define-cproc string-ci<? (s1::<string> s2::<string> :rest ss) |
| 391 |
::<boolean> :constant |
| 392 |
(strcmp-multiarg (strcmpi <))) |
| 393 |
(define-cproc string-ci>? (s1::<string> s2::<string> :rest ss) |
| 394 |
::<boolean> :constant |
| 395 |
(strcmp-multiarg (strcmpi >))) |
| 396 |
(define-cproc string-ci<=? (s1::<string> s2::<string> :rest ss) |
| 397 |
::<boolean> :constant |
| 398 |
(strcmp-multiarg (strcmpi <=))) |
| 399 |
(define-cproc string-ci>=? (s1::<string> s2::<string> :rest ss) |
| 400 |
::<boolean> :constant |
| 401 |
(strcmp-multiarg (strcmpi >=))) |
| 402 |
|
| 403 |
;; |
| 404 |
;; Mapping |
| 405 |
;; |
| 406 |
|
| 407 |
;; We have two kinds of string-{map|for-each}. SRFI-13 takes one string |
| 408 |
;; with optional start/end arguments. R7RS takes one or more string arguments. |
| 409 |
;; In retrospect, R7RS is more consistent, but SRFI-13 is very frequently |
| 410 |
;; imported and it is bothersome to avoid name conflicts. So we have |
| 411 |
;; single implementation that handles both. |
| 412 |
|
| 413 |
(select-module gauche.internal) |
| 414 |
(define (%string-map-dispatch name proc str rest proc-single proc-multi) |
| 415 |
(cond [(null? rest) (proc-single proc str)] |
| 416 |
[(or (integer? (car rest)) |
| 417 |
(string-cursor? (car rest))) |
| 418 |
(if (null? (cdr rest)) |
| 419 |
(proc-single proc (opt-substring str (car rest) (undefined))) |
| 420 |
(if (or (integer? (cadr rest)) |
| 421 |
(string-cursor? (cadr rest))) |
| 422 |
(if (null? (cddr rest)) |
| 423 |
(proc-single proc (opt-substring str (car rest) (cadr rest))) |
| 424 |
(errorf "Too many arguments for SRFI-13 style ~a" name)) |
| 425 |
(error "Integer or string-cursor expected, but got:" (cadr rest))))] |
| 426 |
[(string? (car rest)) |
| 427 |
(if-let1 bad (find (^s (not (string? s))) (cdr rest)) |
| 428 |
(error "string expected, but got:" bad) |
| 429 |
(apply proc-multi proc str rest))] |
| 430 |
[else (error "string expected, but got:" (car rest))])) |
| 431 |
|
| 432 |
(select-module gauche) |
| 433 |
(define (string-map proc str . rest) |
| 434 |
(define (string-map-1 proc str) |
| 435 |
;; go reverse so we don't have to reverse the final list |
| 436 |
;; slightly slower than iterating forward, but less temp. |
| 437 |
;; objects |
| 438 |
(let ([start (string-cursor-start str)]) |
| 439 |
(let loop ([cur (string-cursor-end str)] |
| 440 |
[lst '()]) |
| 441 |
(if (string-cursor=? cur start) |
| 442 |
(list->string lst) |
| 443 |
(let1 cur (string-cursor-prev str cur) |
| 444 |
(loop cur (cons (proc (string-ref str cur)) lst))))))) |
| 445 |
(define (string-map-n proc . strs) |
| 446 |
;; the multi-strs version is already more complicated/less |
| 447 |
;; efficient with lots of lists, just go from left to right |
| 448 |
(let1 ends (map string-cursor-end strs) |
| 449 |
(let loop ([curs (map string-cursor-start strs)] |
| 450 |
[lst '()]) |
| 451 |
(if (any string-cursor=? curs ends) |
| 452 |
(list->string (reverse lst)) |
| 453 |
(loop (map string-cursor-next strs curs) |
| 454 |
(cons (apply proc (map string-ref strs curs)) lst)))))) |
| 455 |
((with-module gauche.internal %string-map-dispatch) |
| 456 |
'string-map proc str rest string-map-1 string-map-n)) |
| 457 |
|
| 458 |
(define (string-for-each proc str . rest) |
| 459 |
(define (string-for-each-1 proc str) |
| 460 |
(let ([end (string-cursor-end str)]) |
| 461 |
(let loop ([cur (string-cursor-start str)]) |
| 462 |
(if (string-cursor=? cur end) |
| 463 |
(undefined) |
| 464 |
(begin (proc (string-ref str cur)) |
| 465 |
(loop (string-cursor-next str cur))))))) |
| 466 |
(define (string-for-each-n proc . strs) |
| 467 |
(let1 ends (map string-cursor-end strs) |
| 468 |
(let loop ([curs (map string-cursor-start strs)]) |
| 469 |
(if (any string-cursor=? curs ends) |
| 470 |
(undefined) |
| 471 |
(begin (apply proc (map string-ref strs curs)) |
| 472 |
(loop (map string-cursor-next strs curs))))))) |
| 473 |
((with-module gauche.internal %string-map-dispatch) |
| 474 |
'string-for-each proc str rest string-for-each-1 string-for-each-n)) |
| 475 |
|
| 476 |
;; |
| 477 |
;; Byte string |
| 478 |
;; |
| 479 |
|
| 480 |
(select-module gauche) |
| 481 |
;; DEPRECATED, only kept for backward compatibility. |
| 482 |
;; We allocate a new string and swap the body, in order to avoid MT-hazard. |
| 483 |
;; (So it is _not_ allocation-free, and we no longer have reason to keep |
| 484 |
;; this procedure.) |
| 485 |
(define (string-incomplete->complete! str) |
| 486 |
(and-let1 s (string-incomplete->complete str) |
| 487 |
((with-module gauche.internal %string-replace-body!) str s) |
| 488 |
s)) |
| 489 |
|
| 490 |
(define-cproc string-complete->incomplete (str::<string>) |
| 491 |
(return (Scm_CopyStringWithFlags str |
| 492 |
SCM_STRING_INCOMPLETE |
| 493 |
SCM_STRING_INCOMPLETE))) |
| 494 |
|
| 495 |
;; handling : #f | :omit | :replace | :escape |
| 496 |
;; filler : #f | <char> | <string> |
| 497 |
(define-cproc string-incomplete->complete (str::<string> |
| 498 |
:optional (handling #f) |
| 499 |
(filler #f)) |
| 500 |
;; backward compatibility |
| 501 |
(when (SCM_CHARP handling) |
| 502 |
(set! filler handling |
| 503 |
handling ':replace)) |
| 504 |
;; argument check |
| 505 |
(unless (or (SCM_FALSEP handling) |
| 506 |
(Scm_Memq handling '(:omit :replace :escape))) |
| 507 |
(Scm_Error "Either #f, :omit, :replace or :escape required, \ |
| 508 |
but got:" handling)) |
| 509 |
(unless (or (SCM_FALSEP filler) (SCM_CHARP filler)) |
| 510 |
(Scm_Error "Either a character or #f required, but got:" filler)) |
| 511 |
(let* ([b::(const ScmStringBody*) (SCM_STRING_BODY str)]) |
| 512 |
(unless (SCM_STRING_BODY_INCOMPLETE_P b) |
| 513 |
;; We do simple copy |
| 514 |
(return (Scm_CopyString str))) |
| 515 |
(let* ([s::(const char*) (SCM_STRING_BODY_START b)] |
| 516 |
[siz::ScmSmallInt (SCM_STRING_BODY_SIZE b)] |
| 517 |
[len::ScmSmallInt (Scm_MBLen s (+ s siz))]) |
| 518 |
(when (>= len 0) |
| 519 |
;; The body can be interpreted as a complete string. |
| 520 |
(return (Scm_MakeString s siz len 0))) |
| 521 |
(when (SCM_FALSEP handling) |
| 522 |
;; Return #f for string that can't be complete |
| 523 |
(return SCM_FALSE)) |
| 524 |
(let* ([p::(const char*) s] |
| 525 |
[ds::ScmDString] |
| 526 |
[echar::ScmChar (?: (SCM_CHARP filler) |
| 527 |
(SCM_CHAR_VALUE filler) |
| 528 |
#\?)]) |
| 529 |
(Scm_DStringInit (& ds)) |
| 530 |
(while (< p (+ s siz)) |
| 531 |
(let* ([ch::ScmChar]) |
| 532 |
(if (>= (+ p (SCM_CHAR_NFOLLOWS (* p))) (+ s siz)) |
| 533 |
(set! ch SCM_CHAR_INVALID) |
| 534 |
(SCM_CHAR_GET p ch)) |
| 535 |
(cond |
| 536 |
[(and (SCM_EQ handling ':escape) ; escaping escape char |
| 537 |
(== ch echar)) |
| 538 |
(Scm_DStringPutc (& ds) ch) |
| 539 |
(Scm_DStringPutc (& ds) ch) |
| 540 |
(+= p (SCM_CHAR_NBYTES ch))] |
| 541 |
[(!= ch SCM_CHAR_INVALID) ; ok |
| 542 |
(Scm_DStringPutc (& ds) ch) |
| 543 |
(+= p (SCM_CHAR_NBYTES ch))] |
| 544 |
[(SCM_EQ handling ':omit) (inc! p)] ;skip |
| 545 |
[(SCM_EQ handling ':replace) ;subs with echar |
| 546 |
(Scm_DStringPutc (& ds) echar) |
| 547 |
(inc! p)] |
| 548 |
[(SCM_EQ handling ':escape) ;escape |
| 549 |
(let* ([octet::u_char (* p)]) |
| 550 |
(Scm_DStringPutc (& ds) echar) |
| 551 |
(Scm_DStringPutc (& ds) (Scm_IntToDigit (>> octet 4) 16 0 0)) |
| 552 |
(Scm_DStringPutc (& ds) (Scm_IntToDigit (logand octet #xf) 16 0 0)) |
| 553 |
(inc! p))]))) |
| 554 |
(return (Scm_DStringGet (& ds) 0)))))) |
| 555 |
|
| 556 |
(define-cproc make-byte-string (size::<int32> :optional (byte::<uint8> 0)) |
| 557 |
(let* ([s::char*]) |
| 558 |
(when (< size 0) (Scm_Error "size out of bound: %d" size)) |
| 559 |
(set! s (SCM_NEW_ATOMIC2 (C: char*) size)) |
| 560 |
(memset s byte size) |
| 561 |
(return (Scm_MakeString s size size SCM_STRING_INCOMPLETE)))) |
| 562 |
|
| 563 |
(define-cproc string-byte-ref (str::<string> k::<fixnum> :optional fallback) |
| 564 |
(let* ([r::int (Scm_StringByteRef str k (SCM_UNBOUNDP fallback))]) |
| 565 |
(return (?: (< r 0) fallback (SCM_MAKE_INT r))))) |
| 566 |
|
| 567 |
(define-cproc byte-substring (str::<string> start::<fixnum> end::<fixnum>) |
| 568 |
(return (Scm_Substring str start end TRUE))) |
| 569 |
|
| 570 |
;; |
| 571 |
;; Complete-sexp? |
| 572 |
;; |
| 573 |
|
| 574 |
;; Check if the given string can be parsed as a complete sexp. |
| 575 |
;; Note that this test doesn't rule out all invalid sexprs. |
| 576 |
;; |
| 577 |
;; This used to be in gauche.listener, but is also used in |
| 578 |
;; gauche.interactive.editable-reader so moved here. |
| 579 |
;; |
| 580 |
;; This should eventually be folded into build-in read, so that |
| 581 |
;; any nontrivial syntax can be handled consistently. |
| 582 |
|
| 583 |
(select-module gauche) |
| 584 |
(define (complete-sexp? str) |
| 585 |
(with-input-from-string str |
| 586 |
(^[] |
| 587 |
;; charset that delimits token |
| 588 |
(define special-chars #[\u0000-\u0020\"\'()\,\;\[\\\]\`{|}\u007f]) |
| 589 |
|
| 590 |
;; main loop |
| 591 |
(define (rec closer) |
| 592 |
(let1 ch (read-char) |
| 593 |
(cond [(eof-object? ch) (if closer #f #t)] |
| 594 |
[(eqv? closer ch) #t] |
| 595 |
[(eqv? #\( ch) (and (rec #\) ) (rec closer))] |
| 596 |
[(eqv? #\[ ch) (and (rec #\] ) (rec closer))] |
| 597 |
[(eqv? #\{ ch) (and (rec #\} ) (rec closer))] |
| 598 |
[(eqv? #\" ch) (and (rec-escaped #\") (rec closer))] |
| 599 |
[(eqv? #\| ch) (and (rec-escaped #\|) (rec closer))] |
| 600 |
[(eqv? #\; ch) (skip-to-nl) (rec closer)] |
| 601 |
[(eqv? #\# ch) |
| 602 |
(let1 c2 (read-char) |
| 603 |
(cond [(eof-object? c2) #f] |
| 604 |
[(eqv? c2 #\\) |
| 605 |
(and (not (eof-object? (read-char))) |
| 606 |
(begin (skip-token) (rec closer)))] |
| 607 |
[(eqv? c2 #\") (and (rec-escaped #\") (rec closer))] |
| 608 |
[(eqv? c2 #\/) (and (rec-escaped #\/) (rec closer))] |
| 609 |
[(eqv? c2 #\[) (and (rec-escaped #\]) (rec closer))] |
| 610 |
[(eqv? c2 #\,) |
| 611 |
(let1 c3 (skip-ws) |
| 612 |
(cond [(eof-object? c3) #f] |
| 613 |
[(eqv? #\( c3) (and (rec #\) ) (rec closer))] |
| 614 |
[(eqv? #\[ c3) (and (rec #\] ) (rec closer))] |
| 615 |
[(eqv? #\{ c3) (and (rec #\} ) (rec closer))] |
| 616 |
[else (skip-token) (rec closer)]))] |
| 617 |
[(eqv? c2 #\() (and (rec #\)) (rec closer))] |
| 618 |
[(eqv? c2 #\<) |
| 619 |
(errorf "unreadable sequence #<~a..." |
| 620 |
(read-block 10))] |
| 621 |
[(eqv? c2 closer) #t] |
| 622 |
[else (rec closer)]))] |
| 623 |
[else (rec closer)]))) |
| 624 |
|
| 625 |
(define (rec-escaped closer) |
| 626 |
(let1 ch (read-char) |
| 627 |
(cond [(eof-object? ch) #f] |
| 628 |
[(eqv? closer ch) #t] |
| 629 |
[(eqv? #\\ ch) (read-char) (rec-escaped closer)] |
| 630 |
[else (rec-escaped closer)]))) |
| 631 |
|
| 632 |
(define (skip-token) |
| 633 |
(let loop ([ch (peek-char)]) |
| 634 |
(unless (or (eof-object? ch) |
| 635 |
(char-set-contains? special-chars ch)) |
| 636 |
(read-char) |
| 637 |
(loop (peek-char))))) |
| 638 |
|
| 639 |
(define (skip-ws) |
| 640 |
(let loop ([ch (read-char)]) |
| 641 |
(if (or (eof-object? ch) |
| 642 |
(char-set-contains? #[\S] ch)) |
| 643 |
ch |
| 644 |
(loop (read-char))))) |
| 645 |
|
| 646 |
(define (skip-to-nl) |
| 647 |
(let loop ([ch (read-char)]) |
| 648 |
(unless (or (eof-object? ch) |
| 649 |
(eqv? ch #\newline)) |
| 650 |
(loop (read-char))))) |
| 651 |
|
| 652 |
;; body |
| 653 |
(rec #f) |
| 654 |
))) |
| 655 |
|
| 656 |
;; |
| 657 |
;; String cursors |
| 658 |
;; |
| 659 |
|
| 660 |
(select-module gauche) |
| 661 |
|
| 662 |
(define-cproc string-cursor? (obj) ::<boolean> |
| 663 |
SCM_STRING_CURSOR_P) |
| 664 |
(define-cproc string-cursor-start (s::<string>) |
| 665 |
(return (Scm_MakeStringCursorFromIndex s 0))) |
| 666 |
(define-cproc string-cursor-end (s::<string>) |
| 667 |
Scm_MakeStringCursorEnd) |
| 668 |
(define-cproc string-cursor-next (s::<string> cursor) |
| 669 |
(return (Scm_StringCursorForward s cursor 1))) |
| 670 |
(define-cproc string-cursor-prev (s::<string> cursor) |
| 671 |
(return (Scm_StringCursorBack s cursor 1))) |
| 672 |
(define-cproc string-cursor-forward (s::<string> cursor nchars::<fixnum>) |
| 673 |
Scm_StringCursorForward) |
| 674 |
(define-cproc string-cursor-back (s::<string> cursor nchars::<fixnum>) |
| 675 |
Scm_StringCursorBack) |
| 676 |
(define-cproc string-index->cursor (s::<string> index) |
| 677 |
(if (SCM_STRING_CURSOR_P index) |
| 678 |
(return index) |
| 679 |
(return (Scm_MakeStringCursorFromIndex s (Scm_GetInteger index))))) |
| 680 |
(define-cproc string-cursor->index (s::<string> cursor) |
| 681 |
Scm_StringCursorIndex) |
| 682 |
|
| 683 |
(define-cproc string-cursor=? (cursor1 cursor2) ::<boolean> |
| 684 |
(return (Scm_StringCursorCompare cursor1 cursor2 Scm_NumEq))) |
| 685 |
|
| 686 |
(define-cproc string-cursor<? (cursor1 cursor2) ::<boolean> |
| 687 |
(return (Scm_StringCursorCompare cursor1 cursor2 Scm_NumLT))) |
| 688 |
|
| 689 |
(define-cproc string-cursor>? (cursor1 cursor2) ::<boolean> |
| 690 |
(return (Scm_StringCursorCompare cursor1 cursor2 Scm_NumGT))) |
| 691 |
|
| 692 |
(define-cproc string-cursor<=? (cursor1 cursor2) ::<boolean> |
| 693 |
(return (Scm_StringCursorCompare cursor1 cursor2 Scm_NumLE))) |
| 694 |
|
| 695 |
(define-cproc string-cursor>=? (cursor1 cursor2) ::<boolean> |
| 696 |
(return (Scm_StringCursorCompare cursor1 cursor2 Scm_NumGE))) |
| 697 |
|
| 698 |
(define-cproc string-cursor-diff (s::<string> start end) |
| 699 |
(return (Scm_Sub (Scm_StringCursorIndex s end) |
| 700 |
(Scm_StringCursorIndex s start)))) |