我正在尝试创建一个返回给定左括号的右括号的函数,我已经可以做到这一点,但是在我的第一次尝试中,我不明白我的错误在哪里,我想知道在未来,这是我的第二个功能,它运行良好(看到它可以让我在第一次尝试中尝试的内容更轻松):
(defun properties-without-spaces (s)
(let ((char-list nil)
(char-remove-p nil))
(loop for c across s do
(when (or (char-equal c #\:)
(char-equal c #\;))
(push c char-list)
(setf char-remove-p t))
(when (and char-remove-p
(not (or (char-equal c #\:)
(char-equal c #\;)))
(not (or (char-equal c #\Space)
(char-equal c #\Newline)
(char-equal c #\Tab))))
(setf char-remove-p nil))
(unless char-remove-p
(push c char-list)))
(trim (make-string-from-chars (reverse char-list)))))
(equal (end-bracket-index "div,ul,li:focus {outline:none; margin: 5px 0 0 1px;} body {background-color: blue;}" 16)
51) ;; => T
(equal (end-bracket-index "div,ul,li:focus {outline:none; margin: 5px 0 0 1px;} body {background-color: blue;} @media (min-width: 30em) {div {margin: 3px 0 1px 3px;} body {background-color: blue;}}" 109)
169) ;; => T
(equal (end-bracket-index "div,ul,li:focus {/*By Umaui*/
outline:none; /*something*/
margin: 5px 0 0 1px;
}
body {
background-color: pink;
}
@media (min-width: 30em) {
div {
margin: 3px 0 1px 3px;
}
body {
background-color: blue;
}
}" 154)
236) ;; => T
我知道有其他方法可以做到这一点,我可以使用 cl-ppcre,但这不是我的问题,我的问题是我在下面的递归函数中的错误在哪里:
(defun end-bracket-index (css start-bracket &optional (open-bracket-level 1))
(let* ((css-before-start-bracket (subseq css 0 (+ start-bracket 1)))
(css-after-start-bracket (subseq css (+ start-bracket 1)))
(next-start-bracket
(search "{" css-after-start-bracket))
(next-end-bracket
(search "}" css-after-start-bracket)))
(cond ((and next-start-bracket
next-end-bracket)
(if (< next-start-bracket next-end-bracket)
(incf open-bracket-level)
(decf open-bracket-level)))
((and (null next-start-bracket)
next-end-bracket)
(decf open-bracket-level)))
(when (zerop open-bracket-level)
(return-from end-bracket-index
(+ next-end-bracket
(length css-before-start-bracket))))
(end-bracket-index css
(+ next-start-bracket
(length css-before-start-bracket))
open-bracket-level)))
(equal (end-bracket-index "div,ul,li:focus {outline:none; margin: 5px 0 0 1px;} body {background-color: blue;}" 16)
51) ;; => T
(equal (end-bracket-index "div,ul,li:focus {outline:none; margin: 5px 0 0 1px;} body {background-color: blue;} @media (min-width: 30em) {div {margin: 3px 0 1px 3px;} body {background-color: blue;}}" 109)
169) ;; => NIL because 168 not equal 169
(equal (end-bracket-index "div,ul,li:focus {/*By Umaui*/
outline:none; /*something*/
margin: 5px 0 0 1px;
}
body {
background-color: pink;
}
@media (min-width: 30em) {
div {
margin: 3px 0 1px 3px;
}
body {
background-color: blue;
}
}" 154)
236) ;; NIL because because 234 not equal 236