Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Copyright (c) 2012 Gregory Higley
- ; Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
- ; files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
- ; modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
- ; is furnished to do so, subject to the following conditions:
- ; The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- ; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- ; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- ; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- rebol [
- Author: "Gregory Higley"
- Title: "Revolucent CSV Library"
- ; Type: module
- ; Needs: [2.101.0]
- ]
- whitespace: charset " ^-"
- csv-decode: funct [
- "Parses a single line of CSV and returns a block of quoted values."
- line [string!]
- /separated-by
- sep-char [char!] "Defaults to comma"
- /quoted-by
- quote-char [char!] "Defaults to double quote"
- /escaped-by
- escape-char [char! none!] "Defaults to none"
- /local
- chunk [string!]
- item [string!]
- items [block!]
- ] [
- default quote-char first {""}
- default sep-char #","
- items: copy []
- characters: complement charset rejoin [" ^-" sep-char]
- either escape-char [
- ; If we have an escape char our quoted-item becomes much more complex
- escaped-quote: rejoin [escape-char quote-char]
- quoted-item: [
- (item: copy "")
- quote-char
- any [ copy chunk to escaped-quote escaped-quote (repend item [chunk quote-char]) ]
- copy chunk to quote-char (append item chunk)
- quote-char
- ]
- ] [
- quoted-item: [quote-char copy item to quote-char quote-char]
- ]
- rules: [
- any [
- any whitespace sep-char (append items "")
- | quoted-item any whitespace sep-char (append items item)
- | copy item some characters any whitespace sep-char (append items item)
- | any whitespace
- ]
- opt [
- any whitespace end (append items "")
- | quoted-item any whitespace end (append items item)
- | copy item some characters any whitespace end (append items item)
- ]
- ]
- either parse line rules [items] [none]
- items
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement