Demo entry 6657867

lisp_test

   

Submitted by anonymous on Nov 05, 2017 at 11:40
Language: EmacsLisp. Code size: 757 Bytes.

(defun bencode-read-integer ()
  (forward-char)                        ; Skip i
  (assert (looking-at "-?[0-9]+") nil "Can't find integer")
  (prog1 (string-to-number (match-string 0))
    (goto-char (match-end 0))
    (assert (= (following-char) ?e) nil "Can't find the end of integer")
    (forward-char)))

;; 4:spam => "spam"
(defun bencode-read-string ()
  (let (len str)
    (assert (looking-at "[0-9]+") nil "Can't match the length")
    (setq len (string-to-number (match-string 0)))
    (goto-char (match-end 0))           ; Skip length
    (assert (= (following-char) ?:) "Can't find ':'")
    (forward-char)                      ; Skip :
    (setq str (buffer-substring (point) (+ (point) len)))
    (forward-char len)
    str))

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).