Skip to content

Instantly share code, notes, and snippets.

@aw
Last active May 14, 2018 06:24
Show Gist options
  • Save aw/3305f2298c1141b4ae8e1c1bd85b198a to your computer and use it in GitHub Desktop.
Save aw/3305f2298c1141b4ae8e1c1bd85b198a to your computer and use it in GitHub Desktop.
CSV parsing in PicoLisp

Parse a CSV in PicoLisp:

Examples:

: (make-csv "Testing,csv,\"parsing,a,csv\",test")
-> ("Testing" "csv" "\"parsing,a,csv\"" "test")
: (make-lst '("n" "g" "," "\"" "p" "a" "," "a" "," "c" "s" "v" "\"" "," "t") ",")
-> (("n" "g") (("\"" "p" "a" "," "a" "," "c" "s" "v" "\"")) ("t"))
: (make-quot '("n" "g" "," "\"" "p" "a" "," "a" "," "c" "s" "v" "\"" "," "t"))
-> ("n" "g" "," ("\"" "p" "a" "," "a" "," "c" "s" "v" "\"") "," "t")

Naive code

[de make-csv (S)
  (mapcar pack (make-lst (chop S) ",")) ]

[de make-lst (L D)
  (split (make-quot L) D) ]

[de make-quot (L)
  (when (bit? 1 (cnt = L '("\"" .))) (quit "Unmatched double \"quotes\""))
  (make
    (while (pop 'L) # (while (++ L) # as of 16.12
      (let C @
        (ifn  (= "\"" C)
              (link C)
              (link (make (link C) (until (= "\"" (link (pop 'L) ] # (link (++ L) ] # as of 16.12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment