Created
June 14, 2013 16:21
-
-
Save valvallow/5783251 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
#!/usr/local/bin/gosh | |
(use gauche.parseopt) | |
(use gauche.uvector) | |
(use srfi-13) | |
(define (usage cmd) | |
(print "usage: " cmd " [option ...] string length") | |
(print " options:") | |
(print " h|help print this help") | |
(print " r|rest print rest string") | |
(exit)) | |
(define (string-take-byte str n) | |
(call-with-input-string str | |
(^p (let rec ((i n)(acc '())) | |
(if (zero? i) | |
(u8vector->string (list->u8vector (reverse acc))) | |
(rec (- i 1)(cons (read-byte p) acc))))))) | |
(define (string-split-byte str n) | |
(let1 taken (string-incomplete->complete (string-take-byte str n) :omit) | |
(values taken (string-drop str (string-length taken))))) | |
(define (main args) | |
(let-args (cdr args) | |
((help "h|help" => (cut usage (car args))) | |
(rest "r|rest") | |
(else (opt . _) | |
(print "Unknown option : " opt) | |
(usage (car args))) | |
. rest-args) | |
(if (or (null? rest-args) | |
(null? (cdr rest-args))) | |
(usage (car args)) | |
(let* ((str (car rest-args)) | |
(bytes (clamp (x->integer (cadr rest-args)) | |
0 | |
(u8vector-length (string->u8vector str))))) | |
(receive (taken tail) | |
(string-split-byte str bytes) | |
(display (if rest tail taken))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment