diff options
| author | 2024-12-28 22:29:28 -0500 | |
|---|---|---|
| committer | 2024-12-28 22:29:28 -0500 | |
| commit | d80be95f5e58f761f2a215256150c85fea8c9d75 (patch) | |
| tree | 98bd3893ba5b40ecb8043d4cbcc12c3288062e84 /mcgoron.iterator.string.scm | |
| parent | refactor iterators to be closure objects (diff) | |
refactor exceptions to be less verbose
Diffstat (limited to 'mcgoron.iterator.string.scm')
| -rw-r--r-- | mcgoron.iterator.string.scm | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/mcgoron.iterator.string.scm b/mcgoron.iterator.string.scm index 62cb960..762b79d 100644 --- a/mcgoron.iterator.string.scm +++ b/mcgoron.iterator.string.scm @@ -16,18 +16,15 @@ (define (string-cursor-valid-movement? str cursor spaces) ;; Return #T if moving CURSOR forwards or backwards SPACES is well ;; defined. - ;; - ;; Will return an error if SPACES is not an integer. (cond - ((not (integer? spaces)) - (raise (non-integer-movement-exception spaces))) + ((zero? spaces) #t) ((negative? spaces) (<= (- spaces) (string-cursor-diff str (string-cursor-start str) cursor))) ((positive? spaces) (<= spaces (string-cursor-diff str cursor (string-cursor-end str)))) - (else #t))) + (else #f))) (define-invocation string-iterator-str) (define-invocation string-iterator->cursor) @@ -42,18 +39,23 @@ (string-cursor-back str cursor (- spaces))) (else (string-cursor-forward str cursor spaces)))) -(define-iterator-implementation (string-iterator str cursor) +(define-iterator-implementation (string-iterator str cursor) self ((iterator-at-start?) (string-cursor=? (string-cursor-start str) cursor)) ((iterator-at-end?) (string-cursor=? (string-cursor-end str) cursor)) ((iterator-advance spaces) - (if (string-cursor-valid-movement? str cursor spaces) - (let ((cursor (string-cursor-advance str cursor spaces))) - (string-iterator str cursor)) - #f)) + (cond + ((not (integer? spaces)) + (raise (non-integer-movement-exception self spaces))) + ((string-cursor-valid-movement? str cursor spaces) + (let ((cursor (string-cursor-advance str cursor spaces))) + (string-iterator str cursor))) + (else #f))) ((iterator-ref) - (string-ref/cursor str cursor)) + (if (iterator-at-end? self) + (raise (ref-at-end-exception self)) + (string-ref/cursor str cursor))) ((iterator->index) (string-cursor->index str cursor)) ((string-iterator-str) str) |
