From cc5cd5cc930672edfd6782722f3d3e87a706e473 Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Sat, 22 Apr 2017 17:54:10 -0700 Subject: [PATCH] Simplify string, string*, string-append, string-append*. --- src/runtime/runtime.pkg | 4 +-- src/runtime/symbol.scm | 2 +- src/runtime/ustring.scm | 71 +++++++++++++---------------------------- 3 files changed, 24 insertions(+), 53 deletions(-) diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg index 4b1a24121..767ba31a5 100644 --- a/src/runtime/runtime.pkg +++ b/src/runtime/runtime.pkg @@ -1055,9 +1055,7 @@ USA. substring? vector->string) (export (runtime predicate-metadata) - register-ustring-predicates!) - (export (runtime symbol) - %string*)) + register-ustring-predicates!)) (define-package (runtime bytevector) (files "bytevector") diff --git a/src/runtime/symbol.scm b/src/runtime/symbol.scm index 4b31d7bc5..31a132761 100644 --- a/src/runtime/symbol.scm +++ b/src/runtime/symbol.scm @@ -62,7 +62,7 @@ USA. (else (error "Illegal symbol name:" s))))) (define (symbol . objects) - (string->symbol (%string* objects 'symbol))) + (string->symbol (string* objects))) (define (intern string) ((ucode-primitive string->symbol) (foldcase->utf8 string))) diff --git a/src/runtime/ustring.scm b/src/runtime/ustring.scm index eab35875c..61f48ba77 100644 --- a/src/runtime/ustring.scm +++ b/src/runtime/ustring.scm @@ -84,8 +84,7 @@ USA. '<= mutable-string?) (register-predicate! ustring? 'unicode-string '<= string?) (register-predicate! slice? 'string-slice '<= string?) - (register-predicate! 8-bit-string? '8-bit-string '<= string?) - (register-predicate! ->string-component? '->string-component)) + (register-predicate! 8-bit-string? '8-bit-string '<= string?)) ;;;; Unicode string layout @@ -1584,59 +1583,33 @@ USA. ;;;; Append and general constructor (define (string-append . strings) - (%string-append* strings)) + (string-append* strings)) (define (string-append* strings) - (guarantee list? strings 'string-append*) - (%string-append* strings)) - -(define (%string-append* strings) - (let ((string - (do ((strings strings (cdr strings)) - (n 0 (fix:+ n (string-length (car strings)))) - (8-bit? #t (and 8-bit? (string-8-bit? (car strings))))) - ((not (pair? strings)) - (if 8-bit? - (legacy-string-allocate n) - (mutable-ustring-allocate n)))))) - (let loop ((strings strings) (i 0)) - (if (pair? strings) - (let ((n (string-length (car strings)))) - (string-copy! string i (car strings) 0 n) - (loop (cdr strings) (fix:+ i n))))) - string)) + (let ((builder (string-builder))) + (for-each (lambda (string) + (guarantee string? string 'string-append) + (builder string)) + strings) + (builder))) (define (string . objects) - (%string* objects 'string)) + (string* objects)) (define (string* objects) - (guarantee list? objects 'string*) - (%string* objects 'string*)) - -(define (%string* objects caller) - (%string-append* - (map (lambda (object) - (->string object caller)) - objects))) - -(define (->string object caller) - (cond ((not object) "") - ((bitless-char? object) (char->string object)) - ((string? object) object) - ((symbol? object) (symbol->string object)) - ((pathname? object) (->namestring object)) - ((number? object) (number->string object)) - ((uri? object) (uri->string object)) - (else (error:not-a ->string-component? object caller)))) - -(define (->string-component? object) - (or (not object) - (bitless-char? object) - (string? object) - (symbol? object) - (pathname? object) - (number? object) - (uri? object))) + (let ((builder (string-builder))) + (for-each (lambda (object) + (if object + (builder + (cond ((bitless-char? object) object) + ((string? object) object) + ((symbol? object) (symbol->string object)) + ((pathname? object) (->namestring object)) + ((number? object) (number->string object)) + ((uri? object) (uri->string object)) + (else (error "Unknown string component:" object)))))) + objects) + (builder))) ;;;; Mapping -- 2.25.1