SHOW:
|
|
- or go back to the newest paste.
1 | public static class BracketsChecker { | |
2 | ||
3 | - | public class BracketMismatchException : ApplicationException { |
3 | + | public class BracketErrorException : ApplicationException { |
4 | ||
5 | public BracketErrorException(string message) | |
6 | : base(message) { | |
7 | } | |
8 | ||
9 | } | |
10 | ||
11 | public class BracketMismatchException : BracketErrorException { | |
12 | ||
13 | public readonly char openChar; | |
14 | public readonly char closeChar; | |
15 | public readonly int closePosition; | |
16 | ||
17 | public BracketMismatchException(char openChar, char closeChar, int closePosition) | |
18 | - | public class ExtraOpeningBracketException : ApplicationException { |
18 | + | |
19 | this.openChar = openChar; | |
20 | this.closeChar = closeChar; | |
21 | this.closePosition = closePosition; | |
22 | } | |
23 | ||
24 | } | |
25 | ||
26 | - | public class ExtraClosingBracketException : ApplicationException { |
26 | + | public class ExtraOpeningBracketException : BracketErrorException { |
27 | ||
28 | public ExtraOpeningBracketException() | |
29 | : base("Extra opening bracket") { | |
30 | } | |
31 | ||
32 | } | |
33 | ||
34 | public class ExtraClosingBracketException : BracketErrorException { | |
35 | ||
36 | public readonly char closeChar; | |
37 | public readonly int closePosition; | |
38 | ||
39 | public ExtraClosingBracketException(char closeChar, int closePosition) | |
40 | : base(string.Format("Extra closing bracket at {1}: \"{0}\"", closeChar, closePosition)) { | |
41 | this.closeChar = closeChar; | |
42 | this.closePosition = closePosition; | |
43 | } | |
44 | ||
45 | } | |
46 | ||
47 | private class Bracket { | |
48 | ||
49 | char[] openChars; | |
50 | char[] closeChars; | |
51 | ||
52 | public Bracket(char[] openChars, char[] closeChars) { | |
53 | this.openChars = openChars; | |
54 | this.closeChars = closeChars; | |
55 | } | |
56 | ||
57 | } | |
58 | ||
59 | private static readonly Bracket[] Brackets = new[] { | |
60 | new Bracket(new[] { '(' }, new[] { ')' }), | |
61 | new Bracket(new[] { '[' }, new[] { ']' }), | |
62 | new Bracket(new[] { '{' }, new[] { '}' }), | |
63 | } | |
64 | ||
65 | private static readonly Dictionary<char, Bracket> BracketsByOpenChar = | |
66 | ( | |
67 | from bracket in Brackets | |
68 | from openChar in bracket.openChars | |
69 | select new KeyValuePair<char, Bracket>(openChar, bracket) | |
70 | ).ToDictionary(); | |
71 | ||
72 | private static readonly HashSet<char> CloseChars = | |
73 | new HashSet<char>( | |
74 | from bracket in Brackets | |
75 | from closeChar in bracket.CloseChars | |
76 | select closeChar | |
77 | ); | |
78 | ||
79 | public static void CheckString(string str) { | |
80 | Stack<Bracket> currentState = new Stack<Bracket>(); | |
81 | for(int i=0; i<str.length; i++) { | |
82 | if(BracketsByOpenChar.ContainsKey(str[i])) { | |
83 | currentState.Push(BracketsByOpenChar[str[i]]); | |
84 | } else if(CloseChars.Contains(str[i])) { | |
85 | if(currentState.IsEmpty) { | |
86 | throw new ExtraClosingBracketException(); | |
87 | } | |
88 | if(!currentState.Peek().closeChars.Contains(str[i])) { | |
89 | throw new BracketsMismatchException(); | |
90 | } | |
91 | currentState.Pop(); | |
92 | } | |
93 | } | |
94 | if(!currentState.IsEmpty) { | |
95 | throw new ExtraOpeningBracketException(); | |
96 | } | |
97 | } | |
98 | ||
99 | } |