Skip to content

Commit a23cbe9

Browse files
committed
修改
1 parent 449b43b commit a23cbe9

File tree

10 files changed

+4788
-9581
lines changed

10 files changed

+4788
-9581
lines changed

csharp/ToolGood.Algorithm2/Internals/CharUtil.cs

Lines changed: 45 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,78 @@ namespace ToolGood.Algorithm.Internals
66
{
77
class CharUtil
88
{
9-
public static char StandardChar(char c)
9+
public static char StandardChar(char o)
1010
{
11-
if (c <= 0) return c;
12-
char o = (char)c;
13-
if (o == '‘') return '\'';
14-
if (o == '’') return '\'';
15-
if (o == '“') return '"';
16-
if (o == '”') return '"';
17-
if (o == '〔') return '(';
18-
if (o == '〕') return ')';
19-
if (o == '=') return '=';
20-
if (o == '+') return '+';
21-
if (o == '-') return '-';
22-
if (o == '×') return '*';
23-
if (o == '÷') return '/';
24-
if (o == '/') return '/';
25-
26-
if (c == 12288) {
27-
o = (char)32;
28-
} else if (c > 65280 && c < 65375) {
29-
o = (char)(c - 65248);
11+
if (o <= 'Z') return o;
12+
if (o > 127) {
13+
if (o == '‘') return '\'';
14+
if (o == '’') return '\'';
15+
if (o == '“') return '"';
16+
if (o == '”') return '"';
17+
if (o == '〔') return '(';
18+
if (o == '〕') return ')';
19+
//if (o == '=') return '=';
20+
//if (o == '+') return '+';
21+
//if (o == '-') return '-';
22+
if (o == '×') return '*';
23+
if (o == '÷') return '/';
24+
//if (o == '/') return '/';
25+
if (o == '【') return '[';
26+
if (o == '】') return ']';
27+
//if (o == '(') return '(';
28+
//if (o == ')') return ')';
29+
//if (o == ',') return ',';
30+
//if (o == '!') return '!';
31+
if (o == 12288) return (char)32;
32+
if (o <= 65280) {
33+
return o;
34+
} else if (o < 65375) {
35+
o = (char)(o - 65248);
36+
}
3037
}
3138
return char.ToUpperInvariant(o);
3239
}
3340

3441
public static string StandardString(string s)
3542
{
36-
var sb = new StringBuilder();
43+
var sb = new StringBuilder(s.Length);
3744
for (int i = 0; i < s.Length; i++) {
38-
var c = s[i];
39-
sb.Append(StandardChar(c));
45+
sb.Append(StandardChar(s[i]));
4046
}
4147
return sb.ToString();
4248
}
4349

44-
private static bool EqualsOnce(string left, string right)
50+
public static bool Equals(string left, char right)
51+
{
52+
if (left.Length != 1) return false;
53+
if (left[0] != right) {
54+
var a = StandardChar(left[0]);
55+
if (a != right) return false;
56+
}
57+
return true;
58+
}
59+
public static bool Equals(string left, string right)
4560
{
4661
if (left.Length != right.Length) return false;
4762
for (int i = 0; i < left.Length; i++) {
4863
if (left[i] != right[i]) {
4964
var a = StandardChar(left[i]);
50-
var b = StandardChar(right[i]);
51-
if (a != b) return false;
65+
if (a != right[i]) return false;
5266
}
5367
}
5468
return true;
5569
}
56-
57-
public static bool Equals(string left, string right)
58-
{
59-
if (left == null) return false;
60-
if (right == null) return false;
61-
return EqualsOnce(left, right);
62-
}
6370
public static bool Equals(string left, string arg1, string arg2)
6471
{
65-
if (left == null) return false;
66-
if (arg1 != null && EqualsOnce(left, arg1))
67-
return true;
68-
if (arg2 != null && EqualsOnce(left, arg2))
69-
return true;
72+
if (Equals(left, arg1)) return true;
73+
if (Equals(left, arg2)) return true;
7074
return false;
7175
}
72-
7376
public static bool Equals(string left, string arg1, string arg2, string arg3)
7477
{
75-
if (left == null) return false;
76-
if (arg1 != null && EqualsOnce(left, arg1))
77-
return true;
78-
if (arg2 != null && EqualsOnce(left, arg2))
79-
return true;
80-
if (arg3 != null && EqualsOnce(left, arg3))
81-
return true;
78+
if (Equals(left, arg1)) return true;
79+
if (Equals(left, arg2)) return true;
80+
if (Equals(left, arg3)) return true;
8281
return false;
8382
}
8483

@@ -139,78 +138,5 @@ public static List<String> SplitFormula(String formula, List<char> splitChars)
139138
}
140139

141140

142-
public static List<string> SplitFormulaForAndOr(string formula)
143-
{
144-
List<String> result = new List<String>();
145-
bool inSquareBrackets = false;
146-
bool inBraceBrackets = false;
147-
int inBracketsCount = 0;
148-
bool inText = false;
149-
char textChar = (char)0;
150-
151-
StringBuilder str = new StringBuilder();
152-
int i = 0;
153-
while (i < formula.Length) {
154-
char c = formula[i];
155-
if (inSquareBrackets) {
156-
str.Append(c);
157-
if (c == ']') inSquareBrackets = false;
158-
} else if (inBraceBrackets) {
159-
str.Append(c);
160-
if (c == '}') inBraceBrackets = false;
161-
} else if (inText) {
162-
str.Append(c);
163-
if (c == '\\') {
164-
i++;
165-
if (i < formula.Length) {
166-
str.Append(formula[i]);
167-
}
168-
} else if (c == textChar) {
169-
inText = false;
170-
}
171-
} else if (c == '&' && inBracketsCount == 0) {
172-
if (i + 1 < formula.Length && formula[i + 1] == '&') {
173-
i++;
174-
result.Add(str.ToString());
175-
str = new StringBuilder();
176-
} else {
177-
result.Add(str.ToString());
178-
}
179-
} else if (c == '|' && inBracketsCount == 0) {
180-
if (i + 1 < formula.Length && formula[i + 1] == '|') {
181-
i++;
182-
result.Add(str.ToString());
183-
str = new StringBuilder();
184-
str.Append(string.Join("&&", result));
185-
str.Append("||");
186-
result.Clear();
187-
} else {
188-
result.Add(str.ToString());
189-
}
190-
} else {
191-
str.Append(c);
192-
if (c == '\'' || c == '"' || c == '`') {
193-
textChar = c;
194-
inText = true;
195-
} else if (c == '[') {
196-
inSquareBrackets = true;
197-
} else if (c == '{') {
198-
inBraceBrackets = true;
199-
} else if (c == '(') {
200-
inBracketsCount++;
201-
} else if (c == ')') {
202-
inBracketsCount--;
203-
}
204-
}
205-
i++;
206-
}
207-
if (str.Length > 0)
208-
result.Add(str.ToString());
209-
return result;
210-
211-
212-
213-
}
214-
215141
}
216142
}

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public virtual Operand VisitMulDiv_fun(mathParser.MulDiv_funContext context)
5757
if (a.IsError == false) secondValue = a;
5858
}
5959
}
60-
if (CharUtil.Equals(t, "*")) {
60+
if (CharUtil.Equals(t, '*')) {
6161
if (secondValue.Type == OperandType.BOOLEAN) {
6262
if (secondValue.BooleanValue)
6363
return firstValue;
@@ -85,7 +85,7 @@ public virtual Operand VisitMulDiv_fun(mathParser.MulDiv_funContext context)
8585
secondValue = secondValue.ToNumber($"Function '{t}' parameter 2 is error!");
8686
if (secondValue.IsError) { return secondValue; }
8787
return Operand.Create(firstValue.NumberValue * secondValue.NumberValue);
88-
} else if (CharUtil.Equals(t, "/")) {
88+
} else if (CharUtil.Equals(t, '/')) {
8989
if (firstValue.Type == OperandType.DATE) {
9090
return Operand.Create(firstValue.DateValue / secondValue.NumberValue);
9191
}
@@ -98,7 +98,7 @@ public virtual Operand VisitMulDiv_fun(mathParser.MulDiv_funContext context)
9898
return Operand.Error($"Function '{t}' parameter 2 is error!");
9999
}
100100
return Operand.Create(firstValue.NumberValue / secondValue.NumberValue);
101-
} else if (CharUtil.Equals(t, "%")) {
101+
} else if (CharUtil.Equals(t, '%')) {
102102
firstValue = firstValue.ToNumber("% fun right value");
103103
if (firstValue.IsError) { return firstValue; }
104104
secondValue = secondValue.ToNumber("% fun right value");
@@ -120,7 +120,7 @@ public virtual Operand VisitAddSub_fun(mathParser.AddSub_funContext context)
120120
var secondValue = args[1];
121121
var t = context.op.Text;
122122

123-
if (CharUtil.Equals(t, "&")) {
123+
if (CharUtil.Equals(t, '&')) {
124124
if (firstValue.IsNull && secondValue.IsNull) {
125125
return firstValue;
126126
} else if (firstValue.IsNull) {
@@ -155,7 +155,7 @@ public virtual Operand VisitAddSub_fun(mathParser.AddSub_funContext context)
155155
if (a.IsError == false) secondValue = a;
156156
}
157157
}
158-
if (CharUtil.Equals(t, "+")) {
158+
if (CharUtil.Equals(t, '+')) {
159159
if (firstValue.Type == OperandType.DATE && secondValue.Type == OperandType.DATE) {
160160
return Operand.Create(firstValue.DateValue + secondValue.DateValue);
161161
} else if (firstValue.Type == OperandType.DATE) {
@@ -172,7 +172,7 @@ public virtual Operand VisitAddSub_fun(mathParser.AddSub_funContext context)
172172
secondValue = secondValue.ToNumber($"Function '{t}' parameter 2 is error!");
173173
if (secondValue.IsError) { return secondValue; }
174174
return Operand.Create(firstValue.NumberValue + secondValue.NumberValue);
175-
} else if (CharUtil.Equals(t, "-")) {
175+
} else if (CharUtil.Equals(t, '-')) {
176176
if (firstValue.Type == OperandType.DATE && secondValue.Type == OperandType.DATE) {
177177
return Operand.Create(firstValue.DateValue - secondValue.DateValue);
178178
} else if (firstValue.Type == OperandType.DATE) {
@@ -182,7 +182,7 @@ public virtual Operand VisitAddSub_fun(mathParser.AddSub_funContext context)
182182
} else if (secondValue.Type == OperandType.DATE) {
183183
firstValue = firstValue.ToNumber($"Function '{t}' parameter 1 is error!");
184184
if (firstValue.IsError) { return firstValue; }
185-
return Operand.Create(secondValue.DateValue - firstValue.NumberValue);
185+
return Operand.Create(firstValue.NumberValue - secondValue.DateValue);
186186
}
187187
firstValue = firstValue.ToNumber(null);
188188
if (firstValue.IsError) { return firstValue; }
@@ -263,11 +263,11 @@ public virtual Operand VisitJudge_fun(mathParser.Judge_funContext context)
263263

264264
r = Compare(firstValue.NumberValue, secondValue.NumberValue);
265265
}
266-
if (CharUtil.Equals(type, "<")) {
266+
if (CharUtil.Equals(type, '<')) {
267267
return Operand.Create(r == -1);
268268
} else if (CharUtil.Equals(type, "<=")) {
269269
return Operand.Create(r <= 0);
270-
} else if (CharUtil.Equals(type, ">")) {
270+
} else if (CharUtil.Equals(type, '>')) {
271271
return Operand.Create(r == 1);
272272
} else if (CharUtil.Equals(type, ">=")) {
273273
return Operand.Create(r >= 0);
@@ -815,7 +815,7 @@ public virtual Operand VisitFIXED_fun(mathParser.FIXED_funContext context)
815815
no = thirdValue.BooleanValue;
816816
}
817817
if (no == false) {
818-
return Operand.Create(s.ToString("N" + num, cultureInfo));
818+
return Operand.Create(s.ToString('N' + num.ToString(), cultureInfo));
819819
}
820820
return Operand.Create(s.ToString(cultureInfo));
821821
}
@@ -1898,7 +1898,7 @@ public virtual Operand VisitDATEDIF_fun(mathParser.DATEDIF_funContext context)
18981898
var endMyDate = (DateTime)secondValue.DateValue;
18991899
var t = thirdValue.TextValue.ToLower();
19001900

1901-
if (CharUtil.Equals(t, "y")) {
1901+
if (CharUtil.Equals(t, 'Y')) {
19021902
#region y
19031903
bool b = false;
19041904
if (startMyDate.Month < endMyDate.Month) {
@@ -1912,7 +1912,7 @@ public virtual Operand VisitDATEDIF_fun(mathParser.DATEDIF_funContext context)
19121912
return Operand.Create((endMyDate.Year - startMyDate.Year - 1));
19131913
}
19141914
#endregion
1915-
} else if (CharUtil.Equals(t, "m")) {
1915+
} else if (CharUtil.Equals(t, 'M')) {
19161916
#region m
19171917
bool b = false;
19181918
if (startMyDate.Day <= endMyDate.Day) b = true;
@@ -1922,9 +1922,9 @@ public virtual Operand VisitDATEDIF_fun(mathParser.DATEDIF_funContext context)
19221922
return Operand.Create((endMyDate.Year * 12 + endMyDate.Month - startMyDate.Year * 12 - startMyDate.Month - 1));
19231923
}
19241924
#endregion
1925-
} else if (CharUtil.Equals(t, "d")) {
1925+
} else if (CharUtil.Equals(t, 'D')) {
19261926
return Operand.Create((endMyDate - startMyDate).Days);
1927-
} else if (CharUtil.Equals(t, "yd")) {
1927+
} else if (CharUtil.Equals(t, "YD")) {
19281928
#region yd
19291929
var day = endMyDate.DayOfYear - startMyDate.DayOfYear;
19301930
if (endMyDate.Year > startMyDate.Year && day < 0) {
@@ -1933,7 +1933,7 @@ public virtual Operand VisitDATEDIF_fun(mathParser.DATEDIF_funContext context)
19331933
}
19341934
return Operand.Create((day));
19351935
#endregion
1936-
} else if (CharUtil.Equals(t, "md")) {
1936+
} else if (CharUtil.Equals(t, "MD")) {
19371937
#region md
19381938
var mo = endMyDate.Day - startMyDate.Day;
19391939
if (mo < 0) {
@@ -1948,7 +1948,7 @@ public virtual Operand VisitDATEDIF_fun(mathParser.DATEDIF_funContext context)
19481948
}
19491949
return Operand.Create((mo));
19501950
#endregion
1951-
} else if (CharUtil.Equals(t, "ym")) {
1951+
} else if (CharUtil.Equals(t, "YM")) {
19521952
#region ym
19531953
var mo = endMyDate.Month - startMyDate.Month;
19541954
if (endMyDate.Day < startMyDate.Day) mo = mo - 1;
@@ -2317,7 +2317,7 @@ public virtual Operand VisitAVERAGEIF_fun(mathParser.AVERAGEIF_funContext contex
23172317
} else {
23182318
if (double.TryParse(args[1].TextValue.Trim(), NumberStyles.Any, cultureInfo, out double d)) {
23192319
count = F_base_countif(list, d);
2320-
sum = F_base_sumif(list, "=" + args[1].TextValue.Trim(), sumdbs);
2320+
sum = F_base_sumif(list, '=' + args[1].TextValue.Trim(), sumdbs);
23212321
} else {
23222322
var sunif = args[1].TextValue.Trim();
23232323
if (sumifRegex.IsMatch(sunif)) {
@@ -2441,7 +2441,7 @@ public virtual Operand VisitSUMIF_fun(mathParser.SUMIF_funContext context)
24412441
sum = F_base_countif(list, args[1].NumberValue) * args[1].NumberValue;
24422442
} else {
24432443
if (double.TryParse(args[1].TextValue.Trim(), NumberStyles.Any, cultureInfo, out _)) {
2444-
sum = F_base_sumif(list, "=" + args[1].TextValue.Trim(), sumdbs);
2444+
sum = F_base_sumif(list, '=' + args[1].TextValue.Trim(), sumdbs);
24452445
} else {
24462446
var sunif = args[1].TextValue.Trim();
24472447
if (sumifRegex.IsMatch(sunif)) {
@@ -2923,11 +2923,11 @@ private double F_base_sumif(List<double> dbs, string s, List<double> sumdbs)
29232923
}
29242924
private bool F_base_compare(double a, double b, string ss)
29252925
{
2926-
if (CharUtil.Equals(ss, "<")) {
2926+
if (CharUtil.Equals(ss, '<')) {
29272927
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) < 0;
29282928
} else if (CharUtil.Equals(ss, "<=")) {
29292929
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) <= 0;
2930-
} else if (CharUtil.Equals(ss, ">")) {
2930+
} else if (CharUtil.Equals(ss, '>')) {
29312931
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) > 0;
29322932
} else if (CharUtil.Equals(ss, ">=")) {
29332933
return Math.Round(a - b, 12, MidpointRounding.AwayFromZero) >= 0;
@@ -3603,7 +3603,7 @@ public virtual Operand VisitJSON_fun(mathParser.JSON_funContext context)
36033603
var firstValue = this.Visit(context.expr()).ToText("Function JSON parameter is error!");
36043604
if (firstValue.IsError) { return firstValue; }
36053605
var txt = firstValue.TextValue;
3606-
if ((txt.StartsWith("{") && txt.EndsWith("}")) || (txt.StartsWith("[") && txt.EndsWith("]"))) {
3606+
if ((txt.StartsWith('{') && txt.EndsWith('}')) || (txt.StartsWith('[') && txt.EndsWith(']'))) {
36073607
try {
36083608
var json = JsonMapper.ToObject(txt);
36093609
return Operand.Create(json);
@@ -3802,7 +3802,7 @@ public virtual Operand VisitPARAMETER_fun(mathParser.PARAMETER_funContext contex
38023802
node = context.PARAMETER2();
38033803
if (node != null) {
38043804
string str = node.GetText();
3805-
if (str.StartsWith("@")) {
3805+
if (str.StartsWith('@')) {
38063806
return GetParameter(str.Substring(1));
38073807
}
38083808
return GetParameter(str.Substring(1, str.Length - 2));

0 commit comments

Comments
 (0)