tags: don't allow tags with unbalanced parentheses.

Don't allow tags to have unbalanced parentheses, except for a few
emoticon tags as special exceptions to the rule.
This commit is contained in:
evazion
2022-04-07 03:06:36 -05:00
parent 3e8e33e663
commit 5f1c296011
4 changed files with 42 additions and 0 deletions

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Danbooru
module Extensions
module String
@@ -55,6 +57,22 @@ module Danbooru
text
end
# @return [Boolean] True if the string contains only balanced parentheses; false if the string contains unbalanced parentheses.
def has_balanced_parens?(open = "(", close = ")")
parens = 0
chars.each do |char|
if char == open
parens += 1
elsif char == close
parens -= 1
return false if parens < 0
end
end
parens == 0
end
end
end
end