MCPcopy Create free account
hub / github.com/bytebase/bytebase / parenDeficit

Function parenDeficit

backend/plugin/parser/tidb/backup.go:798–822  ·  view source on GitHub ↗

parenDeficit returns how many "(" must be prepended and ")" appended to balance s, ignoring parentheses inside string/identifier literals ('...', "...", `...`) and comments (/* */, -- , #).

(s string)

Source from the content-addressed store, hash-verified

796// balance s, ignoring parentheses inside string/identifier literals ('...',
797// "...", `...`) and comments (/* */, -- , #).
798func parenDeficit(s string) (leading int, trailing int) {
799 open := 0
800 for i := 0; i < len(s); i++ {
801 switch c := s[i]; {
802 case c == '\'' || c == '"' || c == '`':
803 i = skipQuoted(s, i)
804 case c == '/' && i+1 < len(s) && s[i+1] == '*':
805 i = skipBlockComment(s, i)
806 case c == '-' && i+1 < len(s) && s[i+1] == '-' && (i+2 >= len(s) || isASCIISpace(s[i+2])):
807 i = skipLineComment(s, i)
808 case c == '#':
809 i = skipLineComment(s, i)
810 case c == '(':
811 open++
812 case c == ')':
813 if open > 0 {
814 open--
815 } else {
816 leading++
817 }
818 default:
819 }
820 }
821 return leading, open
822}
823
824// skipQuoted returns the index of the closing quote of the string/identifier
825// literal opened at i (s[i] is the opening quote), or len(s)-1 if unterminated.

Callers 1

balancedTableRefsFunction · 0.85

Calls 4

skipQuotedFunction · 0.85
isASCIISpaceFunction · 0.85
skipBlockCommentFunction · 0.70
skipLineCommentFunction · 0.70

Tested by

no test coverage detected