Advertisement
Revolucent

Untitled

Apr 18th, 2013
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Copyright (c) 2012 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.  
  16. rebol [
  17.     Author: "Gregory Higley"
  18.     Title: "Revolucent CSV Library"
  19. ;   Type: module
  20. ;   Needs: [2.101.0]
  21. ]
  22.  
  23. whitespace: charset " ^-"
  24.  
  25. csv-decode: funct [
  26.     "Parses a single line of CSV and returns a block of quoted values."
  27.     line [string!]
  28.     /separated-by
  29.         sep-char [char!] "Defaults to comma"
  30.     /quoted-by
  31.         quote-char [char!] "Defaults to double quote"
  32.     /escaped-by
  33.         escape-char [char! none!] "Defaults to none"
  34.     /local
  35.         chunk [string!]
  36.         item [string!]
  37.         items [block!]
  38. ] [
  39.     default quote-char first {""}
  40.     default sep-char #","
  41.     items: copy []
  42.     characters: complement charset rejoin [" ^-" sep-char]
  43.     either escape-char [
  44.         ; If we have an escape char our quoted-item becomes much more complex
  45.         escaped-quote: rejoin [escape-char quote-char]
  46.         quoted-item: [
  47.             (item: copy "")
  48.             quote-char
  49.             any [ copy chunk to escaped-quote escaped-quote (repend item [chunk quote-char]) ]
  50.             copy chunk to quote-char (append item chunk)
  51.             quote-char
  52.         ]
  53.     ] [
  54.         quoted-item: [quote-char copy item to quote-char quote-char]
  55.     ]
  56.     rules: [
  57.         any [
  58.             any whitespace sep-char (append items "")
  59.         |   quoted-item any whitespace sep-char (append items item)
  60.         |   copy item some characters any whitespace sep-char (append items item)
  61.         |   any whitespace
  62.         ]
  63.         opt [
  64.             any whitespace end (append items "")
  65.         |   quoted-item any whitespace end (append items item)
  66.         |   copy item some characters any whitespace end (append items item)
  67.         ]
  68.     ]
  69.     either parse line rules [items] [none]
  70.     items
  71. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement