Skip to content

Commit 034b187

Browse files
committed
支持半角全角
1 parent 431fd30 commit 034b187

File tree

9 files changed

+99
-57
lines changed

9 files changed

+99
-57
lines changed

csharp/ToolGood.Algorithm2.Test/AlgorithmEngine/AlgorithmEngineTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public void Test()
125125
t1 = engine.TryEvaluate("1>2?1:2", 0);
126126
Assert.AreEqual(t1, 2);
127127

128-
128+
t1 = engine.TryEvaluate("1!=2?1:2", 0);
129+
Assert.AreEqual(t1, 1);
129130

130131
var t2 = engine.TryEvaluate("Asc('abcABC123')", "");
131132
Assert.AreEqual(t2, "abcABC123");

csharp/ToolGood.Algorithm2.Test/AlgorithmEngineEx/PriceAlgorithm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public PriceAlgorithm(ConditionCache multiConditionCache, Desk desk) : base(mult
1010
_disk = desk;
1111
}
1212

13-
protected override Operand GetParameter(string parameter)
13+
public override Operand GetParameter(string parameter)
1414
{
1515
if (parameter == "长") {
1616
return Operand.Create(_disk.Length);

csharp/ToolGood.Algorithm2/AlgorithmEngineEx.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using ToolGood.Algorithm.Internals;
56
using ToolGood.Algorithm.LitJson;
67
using static mathParser;
78

@@ -49,7 +50,7 @@ public AlgorithmEngineEx(ConditionCache multiConditionCache)
4950
/// </summary>
5051
/// <param name="parameter"></param>
5152
/// <returns></returns>
52-
protected virtual Operand GetParameter(string parameter)
53+
public virtual Operand GetParameter(string parameter)
5354
{
5455
Operand operand;
5556
if (_dict.TryGetValue(parameter, out operand)) {

csharp/ToolGood.Algorithm2/Internals/Base64.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Text;
33

4-
namespace ToolGood.Algorithm
4+
namespace ToolGood.Algorithm.Internals
55
{
66
/// <summary>
77
/// Modified Base64 for URL applications ('base64url' encoding)
@@ -11,7 +11,6 @@ namespace ToolGood.Algorithm
1111
/// </summary>
1212
static class Base64
1313
{
14-
1514
public static string ToBase64String(byte[] input)
1615
{
1716
return Convert.ToBase64String(input);
@@ -22,7 +21,6 @@ public static byte[] FromBase64String(string base64)
2221
return FromBase64ForUrlString(base64);
2322
}
2423

25-
2624
/// <summary>
2725
/// Modified Base64 for URL applications ('base64url' encoding)
2826
///

csharp/ToolGood.Algorithm2/Internals/CaseChangingCharStream.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Antlr4.Runtime;
66
using Antlr4.Runtime.Misc;
77

8-
namespace ToolGood.Algorithm
8+
namespace ToolGood.Algorithm.Internals
99
{
1010
/// <summary>
1111
/// This class supports case-insensitive lexing by wrapping an existing
@@ -67,29 +67,7 @@ public int LA(int i)
6767
if (c <= 0) {
6868
return c;
6969
}
70-
71-
char o = (char)c;
72-
if (o == '‘') {
73-
o = '\'';
74-
} else if (o == '’') {
75-
o = '\'';
76-
} else if (o == '“') {
77-
o = '"';
78-
} else if (o == '”') {
79-
o = '"';
80-
} else if (o == '〔') {
81-
o = '(';
82-
} else if (o == '〕') {
83-
o = ')';
84-
}
85-
86-
if (c == 12288) {
87-
o = (char)32;
88-
} else if (c > 65280 && c < 65375) {
89-
o = (char)(c - 65248);
90-
}
91-
92-
return (int)char.ToUpperInvariant(o);
70+
return CharUtil.StandardChar((char)c);
9371
}
9472

9573
public int Mark()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace ToolGood.Algorithm.Internals
7+
{
8+
internal class CharUtil
9+
{
10+
public static char StandardChar(char c)
11+
{
12+
if (c <= 0) return c;
13+
char o = (char)c;
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+
27+
if (c == 12288) {
28+
o = (char)32;
29+
} else if (c > 65280 && c < 65375) {
30+
o = (char)(c - 65248);
31+
}
32+
return char.ToUpperInvariant(o);
33+
}
34+
35+
public static bool Equals(string left, string right)
36+
{
37+
if (left == null) return false;
38+
if (right == null) return false;
39+
if (left.Length != right.Length) return false;
40+
for (int i = 0; i < left.Length; i++) {
41+
var a = StandardChar(left[i]);
42+
var b = StandardChar(right[i]);
43+
if (a != b) {
44+
return false;
45+
}
46+
}
47+
return true;
48+
}
49+
public static bool Equals(string left, string arg1, string arg2)
50+
{
51+
if (Equals(left, arg1))
52+
return true;
53+
return Equals(left, arg2);
54+
}
55+
56+
public static bool Equals(string left, string arg1, string arg2,string arg3)
57+
{
58+
if (Equals(left, arg1))
59+
return true;
60+
if (Equals(left, arg2))
61+
return true;
62+
return Equals(left, arg3);
63+
}
64+
65+
}
66+
}

csharp/ToolGood.Algorithm2/Internals/Hash.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Security.Cryptography;
33

4-
namespace ToolGood.Algorithm
4+
namespace ToolGood.Algorithm.Internals
55
{
66
static class Hash
77
{

csharp/ToolGood.Algorithm2/Internals/MathVisitor.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using ToolGood.Algorithm.LitJson;
1111
using ToolGood.Algorithm.MathNet.Numerics;
1212

13-
namespace ToolGood.Algorithm
13+
namespace ToolGood.Algorithm.Internals
1414
{
1515
class MathVisitor : AbstractParseTreeVisitor<Operand>, ImathVisitor<Operand>
1616
{
@@ -58,7 +58,7 @@ public Operand VisitMulDiv_fun(mathParser.MulDiv_funContext context)
5858
if (a.IsError == false) secondValue = a;
5959
}
6060
}
61-
if (t == "*") {
61+
if (CharUtil.Equals(t, "*")) {
6262
if (secondValue.Type == OperandType.BOOLEAN) {
6363
if (secondValue.BooleanValue)
6464
return firstValue;
@@ -86,7 +86,7 @@ public Operand VisitMulDiv_fun(mathParser.MulDiv_funContext context)
8686
secondValue = secondValue.ToNumber($"Function '{t}' parameter 2 is error!");
8787
if (secondValue.IsError) { return secondValue; }
8888
return Operand.Create(firstValue.NumberValue * secondValue.NumberValue);
89-
} else if (t == "/") {
89+
} else if (CharUtil.Equals(t, "/")) {
9090
if (firstValue.Type == OperandType.DATE) {
9191
return Operand.Create(firstValue.DateValue / secondValue.NumberValue);
9292
}
@@ -99,7 +99,7 @@ public Operand VisitMulDiv_fun(mathParser.MulDiv_funContext context)
9999
return Operand.Error($"Function '{t}' parameter 2 is error!");
100100
}
101101
return Operand.Create(firstValue.NumberValue / secondValue.NumberValue);
102-
} else if (t == "%") {
102+
} else if (CharUtil.Equals(t, "%")) {
103103
firstValue = firstValue.ToNumber("% fun right value");
104104
if (firstValue.IsError) { return firstValue; }
105105
secondValue = secondValue.ToNumber("% fun right value");
@@ -121,7 +121,7 @@ public Operand VisitAddSub_fun(mathParser.AddSub_funContext context)
121121
var secondValue = args[1];
122122
var t = context.op.Text;
123123

124-
if (t == "&") {
124+
if (CharUtil.Equals(t, "&")) {
125125
if (firstValue.IsNull && secondValue.IsNull) {
126126
return firstValue;
127127
} else if (firstValue.IsNull) {
@@ -156,7 +156,7 @@ public Operand VisitAddSub_fun(mathParser.AddSub_funContext context)
156156
if (a.IsError == false) secondValue = a;
157157
}
158158
}
159-
if (t == "+") {
159+
if (CharUtil.Equals(t, "+")) {
160160
if (firstValue.Type == OperandType.DATE && secondValue.Type == OperandType.DATE) {
161161
return Operand.Create(firstValue.DateValue + secondValue.DateValue);
162162
} else if (firstValue.Type == OperandType.DATE) {
@@ -173,7 +173,7 @@ public Operand VisitAddSub_fun(mathParser.AddSub_funContext context)
173173
secondValue = secondValue.ToNumber($"Function '{t}' parameter 2 is error!");
174174
if (secondValue.IsError) { return secondValue; }
175175
return Operand.Create(firstValue.NumberValue + secondValue.NumberValue);
176-
} else if (t == "-") {
176+
} else if (CharUtil.Equals(t, "-")) {
177177
if (firstValue.Type == OperandType.DATE && secondValue.Type == OperandType.DATE) {
178178
return Operand.Create(firstValue.DateValue - secondValue.DateValue);
179179
} else if (firstValue.Type == OperandType.DATE) {
@@ -204,14 +204,14 @@ public Operand VisitJudge_fun(mathParser.Judge_funContext context)
204204
string type = context.op.Text;
205205

206206
if (firstValue.IsNull) {
207-
if (secondValue.IsNull && (type == "==" || type == "=")) {
207+
if (secondValue.IsNull && CharUtil.Equals(type, "=", "==", "===")) {
208208
return Operand.True;
209-
} else if (secondValue.IsNull == false && (type == "<>" || type == "!=")) {
209+
} else if (secondValue.IsNull == false && CharUtil.Equals(type, "<>", "!=")) {
210210
return Operand.True;
211211
}
212212
return Operand.False;
213213
} else if (secondValue.IsNull) {
214-
if (type == "==" || type == "=") {
214+
if (CharUtil.Equals(type, "=", "==", "===")) {
215215
return Operand.False;
216216
}
217217
return Operand.True;
@@ -264,15 +264,15 @@ public Operand VisitJudge_fun(mathParser.Judge_funContext context)
264264

265265
r = Compare(firstValue.NumberValue, secondValue.NumberValue);
266266
}
267-
if (type == "<") {
267+
if (CharUtil.Equals(type, "<")) {
268268
return Operand.Create(r == -1);
269-
} else if (type == "<=") {
269+
} else if (CharUtil.Equals(type, "<=")) {
270270
return Operand.Create(r <= 0);
271-
} else if (type == ">") {
271+
} else if (CharUtil.Equals(type, ">")) {
272272
return Operand.Create(r == 1);
273-
} else if (type == ">=") {
273+
} else if (CharUtil.Equals(type, ">=")) {
274274
return Operand.Create(r >= 0);
275-
} else if (type == "=" || type == "==") {
275+
} else if (CharUtil.Equals(type, "=", "==", "===")) {
276276
return Operand.Create(r == 0);
277277
}
278278
return Operand.Create(r != 0);
@@ -298,7 +298,7 @@ public Operand VisitAndOr_fun(mathParser.AndOr_funContext context)
298298
var t = context.op.Text;
299299
var first = this.Visit(context.expr(0)).ToBoolean($"Function '{t}' parameter 1 is error!");
300300
if (first.IsError) { return first; }
301-
if (t == "&&" || t.Equals("and", StringComparison.OrdinalIgnoreCase)) {
301+
if (CharUtil.Equals(t, "&&") || t.Equals("and", StringComparison.OrdinalIgnoreCase)) {
302302
if (first.BooleanValue == false) return Operand.False;
303303
} else {
304304
if (first.BooleanValue) return Operand.True;

csharp/ToolGood.Algorithm2/Operand.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ namespace ToolGood.Algorithm
1111
public abstract class Operand : IDisposable
1212
{
1313
private static readonly CultureInfo cultureInfo = CultureInfo.GetCultureInfo("en-US");
14-
/// <summary>
15-
/// True 值
16-
/// </summary>
17-
public static readonly Operand True = Operand.Create(true);
18-
/// <summary>
19-
/// False 值
20-
/// </summary>
21-
public static readonly Operand False = Operand.Create(false);
14+
15+
public static readonly Operand True = new OperandBoolean(true);
16+
public static readonly Operand False = new OperandBoolean(false);
17+
public static readonly Operand One = Operand.Create(1);
18+
public static readonly Operand Zero = Operand.Create(0);
19+
2220
/// <summary>
2321
/// 是否为空
2422
/// </summary>
@@ -70,7 +68,7 @@ public abstract class Operand : IDisposable
7068
/// <returns></returns>
7169
public static Operand Create(bool obj)
7270
{
73-
return new OperandBoolean(obj);
71+
return obj ? True : False;
7472
}
7573

7674
#region number
@@ -310,7 +308,7 @@ public Operand ToNumber(string errorMessage = null)
310308
{
311309
if (Type == OperandType.NUMBER) { return this; }
312310
if (IsError) { return this; }
313-
if (Type == OperandType.BOOLEAN) { return Create(BooleanValue ? 1.0 : 0.0); }
311+
if (Type == OperandType.BOOLEAN) { return BooleanValue ? One : Zero; }
314312
if (Type == OperandType.DATE) { return Create((double)DateValue); }
315313
if (Type == OperandType.STRING) {
316314
if (double.TryParse(TextValue, NumberStyles.Any, cultureInfo, out double d)) {

0 commit comments

Comments
 (0)