]> gitweb.factorcode.org Git - factor.git/blob - basis/simple-flat-file/simple-flat-file.factor
simple-flat-file: rename ``data`` to ``load-data-file``.
[factor.git] / basis / simple-flat-file / simple-flat-file.factor
1 ! Copyright (C) 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays ascii assocs biassocs interval-maps
4 io.encodings.utf8 io.files kernel math.parser sequences sets
5 splitting ;
6 IN: simple-flat-file
7
8 : drop-comments ( seq -- newseq )
9     [ dup [ "#@" member? ] find drop [ head ] when* ] map harvest ;
10
11 : split-column ( line -- columns )
12     " \t" split harvest 2 short head 2 f pad-tail ;
13
14 : parse-hex ( s -- n )
15     dup [
16         "0x" ?head [ "U+" ?head [ "Missing 0x or U+" throw ] unless ] unless
17         hex>
18     ] when ;
19
20 : parse-line ( line -- code-unicode )
21     split-column [ parse-hex ] map! ;
22
23 : process-codetable-lines ( lines -- assoc )
24     drop-comments [ parse-line ] map! ;
25
26 : flat-file>biassoc ( filename -- biassoc )
27     utf8 file-lines process-codetable-lines >biassoc ;
28
29 : split-; ( line -- array )
30     ";" split [ [ blank? ] trim ] map! ;
31
32 : load-data-file ( filename -- data )
33     utf8 file-lines drop-comments [ split-; ] map! ;
34
35 : expand-range ( range -- range' )
36     ".." split1 [ hex> ] bi@ [ 2array ] when* ;
37
38 : expand-ranges ( ranges -- table )
39     [ [ expand-range ] dip ] assoc-map <interval-map> ;
40
41 : intern ( value values -- value' )
42     [ = ] with find nip ;
43
44 : intern-values ( assoc -- assoc' )
45     dup values members [ intern ] curry assoc-map ;
46
47 : load-interval-file ( filename -- table )
48     load-data-file intern-values expand-ranges ;