parenDeficit returns how many "(" must be prepended and ")" appended to balance s, ignoring parentheses inside string/identifier literals ('...', "...", `...`) and comments (/* */, -- , #).
(s string)
| 796 | // balance s, ignoring parentheses inside string/identifier literals ('...', |
| 797 | // "...", `...`) and comments (/* */, -- , #). |
| 798 | func 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. |
no test coverage detected