Skip to content

Instantly share code, notes, and snippets.

@flooose
Last active March 21, 2016 08:09
Show Gist options
  • Select an option

  • Save flooose/e8b7f681c268939d4b36 to your computer and use it in GitHub Desktop.

Select an option

Save flooose/e8b7f681c268939d4b36 to your computer and use it in GitHub Desktop.
Playing around with finding balanced parens and such
(defconst parens "()")
(defconst square-bracket "[]")
(defconst curly-bracket "{}")
(defun paired-p (open close)
(cond
((equal open ?\[)
(equal close ?\]))
((equal open ?\{)
(equal close ?\}))
((equal open ?\()
(equal close ?\)))
(t nil)))
(ert-deftest paired-p-round ()
(should (equal (paired-p ?\( ?\)) t)))
(ert-deftest paired-p-square ()
(should (equal (paired-p ?\[ ?\]) t)))
(ert-deftest paired-p-curly ()
(should (equal (paired-p ?\{ ?\}) t)))
(ert-deftest paired-p-unmatched ()
(should (null (paired-p ?\[ ?\}))))
(defun has-open (str)
(string-match "[[({].*" str))
(defun has-close (str)
(string-match "[])}].*" str))
(defun has-open-between-close-p (idx str)
(if (has-close str)
(has-open (substring str (+ 1 (has-open str)) (has-close str)))
nil))
(defun balanced-p (str)
(let ((has-open-p (has-open str))
(has-close-p (has-close (string-reverse str))))
(if (not (or has-open-p has-close-p))
t
(if (and has-open-p has-close-p)
(balanced-p (substring str (+ 1 has-open-p) (* -1 (+ 1 has-close-p))))
nil))))
(defun balanced-p (str)
(if (has-open str)
(if (and (has-close str)
(not (has-open-between-close-p (has-open str) str)))
(balanced-p (substring str (+ 1 (has-close str))))
nil)
t))
(balanced-p "(dsf(s(afd)afds)adf)")
(balanced "")
(ert-deftest smoke-test ()
(should (balanced-p "")))
(ert-deftest single-parenthesis ()
(should (null (balanced-p "("))))
(ert-deftest matched-parenthesis ()
(should (balanced-p "d()")))
(ert-deftest matched-parenthesis-end ()
(should (balanced-p "function()")))
(ert-deftest multiple-matched-parenthesis-end ()
(should (balanced-p "()()")))
(ert-deftest multiple-with-unmatched-parenthesis-end ()
(should (null (balanced-p "()()("))))
(ert-deftest nested-unmatched-parenthesis ()
(should (null (balanced-p "(()))"))))
(ert-deftest nested-matched-parenthesis ()
(should (balanced-p "((()))")))
(ert-deftest nested-matched-parenthesis-with content ()
(should (balanced-p "(dsf(s(afd)afds)adf)")))
(ert-deftest smoke-test ()
(should (equal (balanced "") t)))
(ert-deftest single-parenthesis ()
(should (null (balanced "("))))
(ert-deftest single-square-bracket ()
(should (null (balanced "["))))
(ert-deftest single-curly-bracket ()
(should (null (balanced "{"))))
(ert-deftest matched-parenthesis ()
(should (balanced "()")))
(ert-deftest matched-parenthesis-end ()
(should (balanced "function()")))
(ert-deftest unmatched-nested ()
(should (balanced "{[}")))
(ert-deftest unmatched-multiple-nested ()
(should (balanced "{[(]}")))
(ert-deftest unmatched-multiple-nested-2 ()
(should (balanced "{[)]}")))
(ert-deftest unmatched-multiple-not-nested ()
(should (balanced "{}(")))
(string-match "[[{(]" "fssfs(fsfsfdf")
(substring "fssfs(fsfsfdf" 5)
(has-open-between-close-p 1 "()")
(balanced-p "(")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment