From fcb5759cb7d10bcdfe5a1c642a26801abe51ffaf Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Mon, 27 Feb 2017 22:20:05 -0800 Subject: [PATCH] Implement string-trim-X using string-trimmer. --- src/runtime/runtime.pkg | 9 +++++---- src/runtime/string.scm | 35 ----------------------------------- src/runtime/ustring.scm | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg index 3f100ce9c..b4c23e955 100644 --- a/src/runtime/runtime.pkg +++ b/src/runtime/runtime.pkg @@ -982,9 +982,6 @@ USA. string-search-all string-search-backward string-search-forward - string-trim - string-trim-left - string-trim-right substring-search-all substring-search-backward substring-search-forward @@ -994,7 +991,7 @@ USA. (define-package (runtime ustring) (files "ustring") (parent (runtime)) - (export () ;export-deprecated + (export () deprecated:ustring (string-hash-mod string-hash) (substring->list string->list) (substring-move-left! substring-move!) @@ -1009,6 +1006,9 @@ USA. string-find-previous-char-ci string-find-previous-char-in-set string-move! + string-trim + string-trim-left + string-trim-right substring-cichar ascii)))) -;;;; Trim - -(define (string-trim-left string #!optional char-set) - (let ((index - (string-find-next-char-in-set string - (if (default-object? char-set) - char-set:not-whitespace - char-set)))) - (if index - (substring string index (string-length string)) - ""))) - -(define (string-trim-right string #!optional char-set) - (let ((index - (string-find-previous-char-in-set string - (if (default-object? char-set) - char-set:not-whitespace - char-set)))) - (if index - (substring string 0 (fix:+ index 1)) - ""))) - -(define (string-trim string #!optional char-set) - (let* ((char-set - (if (default-object? char-set) - char-set:not-whitespace - char-set)) - (index (string-find-next-char-in-set string char-set))) - (if index - (substring string - index - (fix:+ (string-find-previous-char-in-set string char-set) - 1)) - ""))) - ;;;; Pad (define (string-pad-right string n #!optional char) diff --git a/src/runtime/ustring.scm b/src/runtime/ustring.scm index 4a5ebed92..fc4198a5d 100644 --- a/src/runtime/ustring.scm +++ b/src/runtime/ustring.scm @@ -1358,4 +1358,18 @@ USA. (legacy-string-allocate 1) (full-string-allocate 1)))) (string-set! s 0 char) - s)) \ No newline at end of file + s)) + +(define (legacy-string-trimmer where) + (lambda (string #!optional char-set) + ((string-trimmer 'where where + 'trim-char? + (char-set-predicate + (if (default-object? char-set) + char-set:whitespace + (char-set-invert char-set)))) + string))) + +(define string-trim-left (legacy-string-trimmer 'leading)) +(define string-trim-right (legacy-string-trimmer 'trailing)) +(define string-trim (legacy-string-trimmer 'both)) \ No newline at end of file -- 2.25.1