• Re: cl-ppcre question

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Fri Jun 27 14:33:32 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    Madhu wrote:

    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)))))))

    Gauche Scheme

    (use srfi-13) ;; string-trim-both

    (define (parse-rgb-line line)
    (with-input-from-string line
    (lambda()
    (append
    (list (read) (read) (read))
    (list (string-trim-both (read-line)))))))

    (parse-rgb-line " 255 88 6 yes no ")
    ===>
    (255 88 6 "yes no")
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Fri Jun 27 16:54:38 2025
    From Newsgroup: comp.lang.lisp

    B. Pym wrote:

    B. Pym wrote:

    Madhu wrote:

    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)))))))

    Gauche Scheme

    (use srfi-13) ;; string-trim-both

    (define (parse-rgb-line line)
    (with-input-from-string line
    (lambda()
    (append
    (list (read) (read) (read))
    (list (string-trim-both (read-line)))))))

    (parse-rgb-line " 255 88 6 yes no ")
    ===>
    (255 88 6 "yes no")

    Shorter:

    (define (parse-rgb-line line)
    (with-input-from-string line
    (lambda()
    (list (read) (read) (read) (string-trim-both (read-line))))))


    --- Synchronet 3.21d-Linux NewsLink 1.2