zoukankan      html  css  js  c++  java
  • 示例:字串代换

    CL-USER> (defstruct buf
           vec (start -1) (used -1) (new -1) (end -1))
    BUF


    CL-USER> (defun bref (buf n)
           (svref (buf-vec buf)
              (mod n (length (buf-vec buf)))))
    BREF


    CL-USER> (defun (setf bref) (val buf n)
           (setf (svref (buf-vec buf)
                (mod n (length (buf-vec buf))))
             val))
    (SETF BREF)


    CL-USER> (defun new-buf (len)
           (make-buf :vec (make-array len)))
    NEW-BUF


    CL-USER> (defun buf-insert (x b)
           (setf (bref b (incf (buf-end b))) x))
    BUF-INSERT


    CL-USER> (defun buf-pop (b)
            (prog1
            (bref b (incf (buf-start b)))
              (setf (buf-used b) (buf-start b)
                (buf-new b) (buf-end b))))
    BUF-POP


    CL-USER> (defun buf-next (b)
           (when (< (buf-used b) (buf-new b))
             (bref b (incf (buf-used b)))))
    BUF-NEXT


    CL-USER> (defun buf-reset (b)
           (setf (buf-used b) (buf-start b)
             (buf-new b) (buf-end b)))
    BUF-RESET


    CL-USER> (defun buf-clear (b)
           (setf (buf-start b) -1 (buf-used b) -1
             (buf-new b) -1 (buf-end b) -1))
    BUF-CLEAR


    CL-USER> (defun buf-flush (b str)
           (do ((i (1+ (buf-used b)) (1+ i)))
               ((> i (buf-end b)))
             (princ (bref b i) str)))
    BUF-FLUSH


    CL-USER> (defun file-subst (old new file1 file2)
           (with-open-file (in file1 :direction :input)
             (with-open-file (out file2 :direction :output
                      :if-exists :supersede)
               (stream-subst old new in out))))
    ;Compiler warnings :
    ;   In FILE-SUBST: Undefined function STREAM-SUBST
    FILE-SUBST


    CL-USER> (defun stream-subst (old new in out)
           (let* ((pos 0)
              (len (length old))
              (buf (new-buf len))
              (from-buf nil))
             (do ((c (read-char in nil :eof)
                 (or (setf from-buf (buf-next buf))
                 (read-char in nil :eof))))
             ((eql c :eof))
               (cond ((char= c (char old pos))
                  (incf pos)
                  (cond ((= pos len)
                     (princ new out)
                     (setf pos 0)
                     (buf-clear buf))               

                    ((not from-buf)
                     (buf-insert c buf))))
                 ((zerop pos)
                  (princ c out)
                  (when from-buf
                (buf-pop buf)
                (buf-reset buf)))
                 (t
                  (unless from-buf
                (buf-insert c buf))
                  (princ (buf-pop buf) out)
                  (buf-reset buf)
                  (setf pos 0))))
             (buf-flush buf out)))
    STREAM-SUBST

    然后在”C:\lispbox-0.7”路径下新建文件test1,test2

    test1中存储的内容:

    The struggle between Liberty and Authority is the most conspicuous feature
    in the portions of history with which we are earliest familiar, particularly
    in that of Greece, Rome, and England.
    然后在REPL中输入如下代码:

    CL-USER> (file-subst " th" " z" "test1" "test2")
    NIL

    打开test2,其内容为:

    The struggle between Liberty and Authority is ze most conspicuous feature
    in ze portions of history with which we are earliest familiar, particularly
    in zat of Greece, Rome, and England.

  • 相关阅读:
    PHP用腾讯地图api根据地址获取经纬度的方法
    Tomcat实现负载均衡
    命令行备忘神器 Navi
    Bashtop – Linux的资源监视工具
    使用jinfo出现“can't determine target's VM version”问题解决方法
    axios 封装【满足常规数据上传及file上传】
    前端基础之设计模式篇
    使用Chrome插件拦截广告的原理
    前端基础之算法篇
    一道面试题引发的思考
  • 原文地址:https://www.cnblogs.com/flowjacky/p/2842994.html
Copyright © 2011-2022 走看看