Pascal Bourguignon wrote:
> > I have to split a string from /proc/net/arp of the format
> > "127.0.0.1 0x1 0x2 00:11:22:33:AE:5E * ethx"
> > ^^^
> > to extract the MAC.
>
>
> When I don't need the utmost efficient solution, I merely use:
>
> [69]> (split-string (fourth (delete "" (split-string line " ")
> :test (function string=))) ":")
> ("00" "11" "22" "33" "AE" "5E")
>
>
> with (GPL sources):
>
> (DEFUN SPLIT-STRING (STRING &OPTIONAL (SEPARATORS " "))
> "
> NOTE: current implementation only accepts as separators
> a string containing literal characters.
> "
> (UNLESS (SIMPLE-STRING-P STRING) (SETQ STRING (COPY-SEQ STRING)))
> (UNLESS (SIMPLE-STRING-P SEPARATORS) (SETQ SEPARATORS (COPY-SEQ SEPARATORS)))
> (LET ((CHUNKS '())
> (POSITION 0)
> (NEXTPOS 0)
> (STRLEN (LENGTH STRING)) )
> (DECLARE (TYPE SIMPLE-STRING STRING SEPARATORS))
> (LOOP WHILE (< POSITION STRLEN)
> DO
> (LOOP WHILE (AND (< NEXTPOS STRLEN)
> (not (position (CHAR STRING NEXTPOS) separators)))
> DO (SETQ NEXTPOS (1+ NEXTPOS))
> );;loop
> (PUSH (SUBSEQ STRING POSITION NEXTPOS) CHUNKS)
> (SETQ POSITION (1+ NEXTPOS))
> (SETQ NEXTPOS POSITION)
> );;loop
> (NREVERSE CHUNKS)
> );;let
> );;SPLIT-STRING
(define (split-string str (separators " "))
(define seps (string->list separators))
(define strlen (string-length str))
(let loop ((i 0) (anchor 0) (chunks '()))
(cond ((= i strlen) (reverse (cons (substring str anchor i) chunks)))
((member (string-ref str i) seps)
(loop (+ i 1) (+ i 1) (cons (substring str anchor i) chunks)))
(else (loop (+ i 1) anchor chunks)))))
(require srfi/1) ; delete
(define line
"127.0.0.1 0x1 0x2 00:11:22:33:AE:5E * ethx")
(split-string (fourth (delete "" (split-string line))) ":")
===>
("00" "11" "22" "33" "AE" "5E")
In Forth?
--
Despite its proximity and high level of economic development, Israel has
refused to take any Syrian refugees. "We will not allow Israel to be submerged
by a wave of illegal migrants and terrorist activists," Benjamin Netanyahu has
said.
http://archive.org/details/nolies