View difference between Paste ID: xDngrD07 and ZQ2ayPag
SHOW: | | - or go back to the newest paste.
1-
; Copyright (c) 2012 Gregory Higley
1+
; Copyright (c) 2013 Gregory Higley
2
3
; Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
4
; files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
5
; modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
6
; is furnished to do so, subject to the following conditions:
7
8
; The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
10
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
11
; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
12
; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
13
; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
15
REBOL [
16
	Author: "Gregory Higley"
17
	Title: "Revolucent CSV Library"
18
;	Type: module
19
;	Needs: [2.101.0]
20
]
21
22
whitespace: charset " ^-"
23
24
csv-decode: funct [
25
	"Parses a single line of CSV and returns a block of quoted values."
26
	line [string!]
27
	/separated-by
28
		sep-char [char!] "Defaults to comma"
29
	/quoted-by
30
		quote-char [char!] "Defaults to double quote"
31
	/escaped-by
32
		escape-char [char! none!] "Defaults to none"
33
	/local
34
		chunk [string!]
35
		item [string!]
36
		items [block!]
37
] [
38
	default quote-char first {""}
39
	default sep-char #","
40
	items: copy []
41
	characters: complement charset rejoin [" ^-" sep-char]
42
	either escape-char [
43
		; If we have an escape char our quoted-item becomes much more complex
44
		escaped-quote: rejoin [escape-char quote-char]
45
		quoted-item: [
46
			(item: copy "")
47
			quote-char 
48
			any [ copy chunk to escaped-quote escaped-quote (repend item [chunk quote-char]) ]
49
			copy chunk to quote-char (append item chunk)
50
			quote-char
51
		]
52
	] [
53
		quoted-item: [quote-char copy item to quote-char quote-char]
54
	]
55
	rules: [
56
		some [
57-
		any [
57+
58
		|	any whitespace quoted-item any whitespace sep-char (append items item)
59-
		|	quoted-item any whitespace sep-char (append items item)
59+
		|	any whitespace copy item some characters any whitespace sep-char (append items item)
60-
		|	copy item some characters any whitespace sep-char (append items item)
60+
		][
61-
		|	any whitespace
61+
62
		|	any whitespace quoted-item any whitespace end (append items item)
63-
		opt [
63+
		|	any whitespace copy item some characters any whitespace end (append items item)
64
		]
65-
		|	quoted-item any whitespace end (append items item)
65+
66-
		|	copy item some characters any whitespace end (append items item)
66+
67
]