Created
June 3, 2013 07:33
-
-
Save yamasushi/5696605 to your computer and use it in GitHub Desktop.
use queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- /home/shuji/gauche-dev/Gauche/lib/text/csv.scm 2013-06-01 15:43:19.339012570 +0900 | |
+++ csv.scm 2013-06-03 16:05:43.100572304 +0900 | |
@@ -31,16 +31,18 @@ | |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
;;; | |
-(define-module text.csv | |
+(define-module csv.csv | |
(use srfi-1) | |
(use srfi-11) | |
(use srfi-13) | |
(use gauche.regexp) | |
+ ; | |
+ (use util.queue) | |
(export <csv> | |
make-csv-reader | |
make-csv-writer) | |
) | |
-(select-module text.csv) | |
+(select-module csv.csv) | |
;; Parameters: | |
;; separator - a character to be used to separate fields. | |
@@ -73,43 +75,48 @@ | |
(define (csv-reader sep quo) | |
(define (eor? ch) (or (eqv? ch #\newline) (eof-object? ch))) | |
- | |
- (define (start fields) | |
+ (define fields (make-queue)) | |
+ (define (field-enqueue! x) (enqueue! fields x) ) | |
+ (define (field-dequeue-all!) (dequeue-all! fields)) | |
+ ; | |
+ (define (start) | |
(let1 ch (read-char) | |
- (cond [(eor? ch) (reverse (cons "" fields))] | |
- [(eqv? ch sep) (start (cons "" fields))] | |
- [(eqv? ch quo) (quoted fields)] | |
- [(char-whitespace? ch) (start fields)] | |
- [else (unquoted (list ch) fields)]))) | |
+ (cond [(eor? ch) (field-enqueue! "") (field-dequeue-all!) ] | |
+ ; | |
+ [(eqv? ch sep) (field-enqueue! "") (start) ] | |
+ ; | |
+ [(eqv? ch quo) (quoted) ] | |
+ [(char-whitespace? ch) (start)] | |
+ [else (unquoted (list ch) )]))) | |
- (define (unquoted chs fields) | |
+ (define (unquoted chs) | |
(let loop ([ch (read-char)] [last chs] [chs chs]) | |
- (cond [(eor? ch) (reverse (cons (finish last) fields))] | |
- [(eqv? ch sep) (start (cons (finish last) fields))] | |
+ (cond [(eor? ch) (field-enqueue! (finish last)) (field-dequeue-all!) ] | |
+ [(eqv? ch sep) (field-enqueue! (finish last)) (start)] | |
[(char-whitespace? ch) (loop (read-char) last (cons ch chs))] | |
[else (let1 chs (cons ch chs) | |
(loop (read-char) chs chs))]))) | |
- (define (finish rchrs) (list->string (reverse rchrs))) | |
+ (define (finish rchrs) (list->string (reverse! rchrs))) | |
- (define (quoted fields) | |
+ (define (quoted) | |
(let loop ([ch (read-char)] [chs '()]) | |
(cond [(eof-object? ch) (error "unterminated quoted field")] | |
[(eqv? ch quo) | |
(if (eqv? (peek-char) quo) | |
(begin (read-char) (loop (read-char) (cons quo chs))) | |
- (quoted-tail (cons (finish chs) fields)))] | |
+ (begin (field-enqueue! (finish chs)) (quoted-tail) ) )] | |
[else (loop (read-char) (cons ch chs))]))) | |
- (define (quoted-tail fields) | |
+ (define (quoted-tail) | |
(let loop ([ch (read-char)]) | |
- (cond [(eor? ch) (reverse fields)] | |
- [(eq? ch sep) (start fields)] | |
+ (cond [(eor? ch) (field-dequeue-all!)] | |
+ [(eq? ch sep) (start)] | |
[else (loop (read-char))]))) | |
(if (eof-object? (peek-char)) | |
(eof-object) | |
- (start '()))) | |
+ (begin (field-dequeue-all!) (start) ) )) | |
(define (make-csv-writer separator :optional (newline "\n") (quote-char #\")) | |
(let* ((quote-string (string quote-char)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment