MCPcopy Create free account
hub / github.com/Aghajari/MathParser / Utils

Class Utils

MathParser/src/com/aghajari/math/Utils.java:25–215  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23import java.util.regex.Pattern;
24
25public class Utils {
26
27 /* used to check if parentheses are balanced */
28 //public final static Pattern balancedParentheses = Pattern.compile("\\((?:[^)(]+|\\((?:[^)(]+|\\([^)(]*\\))*\\))*\\)");
29 /* used to find the innermost parentheses */
30 public final static Pattern innermostParentheses = Pattern.compile("(\\([^\\(]*?\\))");
31 /* used to split function arguments by comma */
32 public final static Pattern splitParameters = Pattern.compile(",(?=(?:[^()]*\\([^()]*\\))*[^\\()]*$)");
33 /* used to split if condition to two comparable part */
34 public final static Pattern splitIf = Pattern.compile("(.*?)(!=|<>|>=|<=|==|>|=|<)(.*?$)");
35 /* used to simplify double type values */
36 public final static Pattern doubleType = Pattern.compile("\\(([\\d.]+([eE])[\\d+-]+)\\)");
37 /* used to simplify binary values */
38 public final static Pattern binary = Pattern.compile("\\(0b[01]+\\)");
39 /* used to simplify octal values */
40 public final static Pattern octal = Pattern.compile("\\(0o[0-7]+\\)");
41 /* used to simplify hexadecimal values */
42 public final static Pattern hexadecimal = Pattern.compile("\\(0x[0-9a-fA-F]+\\)");
43
44 /**
45 * @param src the expression to check
46 * @throws BalancedParenthesesException If parentheses aren't balanced
47 */
48 public static void validateBalancedParentheses(String src) throws BalancedParenthesesException {
49 /*String dest = src.replaceAll(balancedParentheses.pattern(), "");
50 if (dest.contains(")"))
51 throw new BalancedParenthesesException(src, src.indexOf(dest.substring(dest.indexOf(")"))) + 1);
52 else if (dest.contains("("))
53 throw new BalancedParenthesesException(src, src.indexOf(dest.substring(dest.indexOf("("))) + 1);*/
54
55 if (Utils.realTrim(src).contains("()"))
56 throw new BalancedParenthesesException(null, -1);
57
58 int opened = 0;
59 for (int i = 0; i < src.length(); ++i)
60 if (src.charAt(i) == '(')
61 opened++;
62 else if (src.charAt(i) == ')') {
63 opened--;
64 if (opened < 0)
65 throw new BalancedParenthesesException(src, i + 1);
66 }
67
68 if (opened != 0)
69 throw new BalancedParenthesesException(src, src.length());
70 }
71
72 /**
73 * @see jdk.internal.joptsimple.internal.Strings#repeat(char, int)
74 */
75 public static String repeat(char ch, int count) {
76 StringBuilder buffer = new StringBuilder();
77
78 for (int i = 0; i < count; ++i)
79 buffer.append(ch);
80
81 return buffer.toString();
82 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected