Last active
December 18, 2015 02:38
-
-
Save yamasushi/5712069 to your computer and use it in GitHub Desktop.
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-03 19:24:53.622756718 +0900 | |
+++ csv.scm 2013-06-05 15:31:30.180567414 +0900 | |
@@ -31,7 +31,7 @@ | |
;;; 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) | |
@@ -39,7 +39,7 @@ | |
make-csv-reader | |
make-csv-writer) | |
) | |
-(select-module text.csv) | |
+(select-module csv.csv) | |
;; Parameters: | |
;; separator - a character to be used to separate fields. | |
@@ -67,14 +67,14 @@ | |
(define (make-csv-reader separator :optional (quote-char #\")) | |
(^[:optional (port #f)] | |
(if port | |
- (with-input-from-port port (^[] (csv-reader separator quote-char))) | |
- (csv-reader separator quote-char)))) | |
+ (csv-reader separator quote-char port) | |
+ (csv-reader separator quote-char (current-input-port) )))) | |
-(define (csv-reader sep quo) | |
+(define (csv-reader sep quo port) | |
(define (eor? ch) (or (eqv? ch #\newline) (eof-object? ch))) | |
(define (start fields) | |
- (let1 ch (read-char) | |
+ (let1 ch (read-char port) | |
(cond [(eor? ch) (reverse! (cons "" fields))] | |
[(eqv? ch sep) (start (cons "" fields))] | |
[(eqv? ch quo) (quoted fields)] | |
@@ -82,31 +82,31 @@ | |
[else (unquoted (list ch) fields)]))) | |
(define (unquoted chs fields) | |
- (let loop ([ch (read-char)] [last chs] [chs chs]) | |
+ (let loop ([ch (read-char port)] [last chs] [chs chs]) | |
(cond [(eor? ch) (reverse! (cons (finish last) fields))] | |
[(eqv? ch sep) (start (cons (finish last) fields))] | |
- [(char-whitespace? ch) (loop (read-char) last (cons ch chs))] | |
+ [(char-whitespace? ch) (loop (read-char port) last (cons ch chs))] | |
[else (let1 chs (cons ch chs) | |
- (loop (read-char) chs chs))]))) | |
+ (loop (read-char port) chs chs))]))) | |
(define (finish rchrs) (list->string (reverse! rchrs))) | |
(define (quoted fields) | |
- (let loop ([ch (read-char)] [chs '()]) | |
+ (let loop ([ch (read-char port)] [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))) | |
+ (if (eqv? (peek-char port) quo) | |
+ (begin (read-char port) (loop (read-char port) (cons quo chs))) | |
(quoted-tail (cons (finish chs) fields)))] | |
- [else (loop (read-char) (cons ch chs))]))) | |
+ [else (loop (read-char port) (cons ch chs))]))) | |
(define (quoted-tail fields) | |
- (let loop ([ch (read-char)]) | |
+ (let loop ([ch (read-char port)]) | |
(cond [(eor? ch) (reverse! fields)] | |
[(eqv? ch sep) (start fields)] | |
- [else (loop (read-char))]))) | |
+ [else (loop (read-char port))]))) | |
- (if (eof-object? (peek-char)) | |
+ (if (eof-object? (peek-char port)) | |
(eof-object) | |
(start '()))) | |
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
memory 2G | |
Intel® Celeron(R) M processor 1.60GHz | |
Ubuntu 12.04 32bit | |
$ gosh csv/test-csv ken_all.csv | |
;(time (read-data reader (cadr args))) | |
; real 8.223 | |
; user 7.470 | |
; sys 0.240 | |
$ gosh csv/test-csv ken_all.csv | |
;(time (read-data kaizou:reader (cadr args))) | |
; real 7.333 | |
; user 6.900 | |
; sys 0.270 | |
ken_all を四倍にしたファイル | |
$ gosh csv/test-csv big.csv | |
;(time (read-data reader (cadr args))) | |
; real 34.266 | |
; user 32.330 | |
; sys 1.020 | |
$ gosh csv/test-csv big.csv | |
;(time (read-data kaizou:reader (cadr args))) | |
; real 31.257 | |
; user 29.040 | |
; sys 1.150 |
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
#!/usr/bin/env gosh | |
;;; -*- mode: scheme; coding: utf-8 -*- | |
(use text.csv) | |
(use csv.csv :prefix kaizou:) | |
(define sep #\,) | |
(define quo #\') | |
(define reader (make-csv-reader sep quo)) | |
(define kaizou:reader (kaizou:make-csv-reader sep quo)) | |
#| | |
(define line " ,,f oo ,\nbar,''' ,' ,,") | |
#?=(port->list reader (open-input-string line) ) | |
#?=(port->list kaizou:reader (open-input-string line) ) | |
(exit) | |
|# | |
(define (main args) | |
(define (read-data reader fname) | |
(call-with-input-file fname | |
(^ [in] (port->list reader in) ) | |
:encoding "Shift_JIS") ) | |
;(with-output-to-file "csv-org.txt" (^[] ($ for-each write $ read-data reader (cadr args) ) ) ) | |
;(with-output-to-file "csv-test.txt" (^[] ($ for-each write $ read-data kaizou:reader (cadr args) ) ) ) | |
($ time $ read-data reader (cadr args) ) | |
;($ time $ read-data kaizou:reader (cadr args) ) | |
0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment