-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance.go
47 lines (38 loc) · 1.52 KB
/
balance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Package balance provides functions for validating parentheses balance of strings
package balance
import "strings"
const (
OpenParentheses = "[({" // OPEN_PARENTHESES keeps default opening elements for checking.
CloseParentheses = "])}" // CLOSE_PARENTHESES keeps default closing elements for checking.
)
// Check validates given string with default parenthesises via CheckCustom.
// Default parenthesises: (), [], {}
func Check(str string) (valid bool, err error) {
return CheckCustom(str, OpenParentheses, CloseParentheses)
}
// CheckCustom validates given string for balance of opening and closing characters.
// It uses stack data structure for validation.
// Basically, it iterates given string, push opening elements to stack and expect to pop closing elements from stack.
// It returns special errors when string is not valid.
// Possible errors: MismatchError, UnclosedParenthesesError, UnknownCharacterError, CustomPairError.
func CheckCustom(str string, opens string, closes string) (valid bool, err error) {
if len(opens) != len(closes) {
return false, &CustomPairError{opens, closes}
}
stack := newStack()
for i, ch := range str {
if pos := strings.IndexRune(opens, ch); pos != -1 {
stack.push(rune(closes[pos]))
} else if pos := strings.IndexRune(closes, ch); pos != -1 {
if ch2 := stack.pop(); ch != ch2 {
return false, &MismatchError{i}
}
} else {
return false, &UnknownCharacterError{i, ch}
}
}
if !stack.isEmpty() {
return false, &UnclosedParenthesesError{stack.size}
}
return true, nil
}