• .Re: cl-ppcre question

    From Robert L.@21:1/5 to Madhu on Wed Feb 23 14:59:57 2022
    Madhu wrote:

    * Tamas Papp <87hcnwaky1.fsf@pu100877.student.princeton.edu> :

    | Hi,
    |
    | I would like to parse color names in X11's rgb.txt. They look like this:
    |
    | 119 136 153 light slate gray
    | 119 136 153 LightSlateGray
    |
    | When parsing each line, I would like to get the color triplet, and the
    | name. I tried it like this:
    |
    | (let ((color-scanner
    | (cl-ppcre:create-scanner
    | "^ +([0-9]{0,2}) +([0-9]{0,2}) +([0-9]{0,2}) +([ a-zA-Z0-9]+)")))
    | (cl-ppcre:scan-to-strings color-scanner " 1 2 3 foo bar baz "))
    |
    | This gives #("1" "2" "3" "foo bar baz ") for the substrings. How can
    | I get rid of the trailing spaces?

    Use the right tool for the job :)

    (defun parse-rgb-line (string &key (start 0) end)
    (multiple-value-bind (r endr)
    (parse-integer string :start start :end end :junk-allowed t)
    (multiple-value-bind (g endg)
    (parse-integer string :start (1+ endr) :end end :junk-allowed t)
    (multiple-value-bind (b endb)
    (parse-integer string :start (1+ endg) :end end :junk-allowed t)
    (let ((name-startpos
    (position-if-not (lambda (c) (case c ((#\Tab #\Space) t)))
    string :start (1+ endb))))
    (values (format nil "#~2,'0X~2,'0X~2,'0X" r g b)
    (subseq string name-startpos end)))))))


    Testing under SBCL:

    * (parse-rgb-line " 62 128 255 light blue ")

    "#3E80FF"
    "light blue "

    Pure crap.

    Gauche Scheme:

    (use gauche.generator)

    (define (parse-rgb-line line)
    (define tokens
    (with-input-from-string line (cut generator->list read)))
    (list (take tokens 3)
    (string-join (map symbol->string (drop tokens 3)) " ")))

    (parse-rgb-line " 62 128 255 light blue ")
    ===>
    ((62 128 255) "light blue")

    (parse-rgb-line "62 128 255 LightBlue")
    ===>
    ((62 128 255) "LightBlue")

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Zyni=20Mo=C3=AB?=@21:1/5 to Robert L. on Wed Feb 23 16:03:46 2022
    Robert L. <No_spamming@noWhere_7073.org> wrote:

    (define (parse-rgb-line line)
    (define tokens
    (with-input-from-string line (cut generator->list read)))
    (list (take tokens 3)
    (string-join (map symbol->string (drop tokens 3)) " ")))


    He is using read. Because that is a good idea.
    --
    the small snake

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)