Skip to content

Commit adbb514

Browse files
author
linzhijun
committed
fix
1 parent 3ee58fc commit adbb514

5 files changed

Lines changed: 37 additions & 23 deletions

File tree

csharp/ToolGood.Algorithm.Test/AlgorithmEngine/StringTest.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using PetaTest;
1+
using PetaTest;
22

33
namespace ToolGood.Algorithm.Test.String
44
{
@@ -141,7 +141,10 @@ public void LEFT_test()
141141
AlgorithmEngine engine = new AlgorithmEngine();
142142
var t = engine.TryEvaluate("LEFT('123222',3)", "");
143143
Assert.AreEqual(t, "123");
144-
}
144+
145+
t = engine.TryEvaluate("LEFT('123222',30)", "");
146+
Assert.AreEqual(t, "123222");
147+
}
145148

146149
[Test]
147150
public void LEN_test()
@@ -200,7 +203,10 @@ public void RIGHT_test()
200203
AlgorithmEngine engine = new AlgorithmEngine();
201204
var t = engine.TryEvaluate("RIGHT(\"123q\",3)", "");
202205
Assert.AreEqual(t, "23q");
203-
}
206+
207+
t = engine.TryEvaluate("RIGHT(\"123q\",30)", "");
208+
Assert.AreEqual(t, "123q");
209+
}
204210

205211
[Test]
206212
public void RMB_test()

csharp/ToolGood.Algorithm/Internals/Functions/String/Function_LEFT.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using ToolGood.Algorithm.Enums;
44

@@ -27,8 +27,10 @@ public override Operand Evaluate(AlgorithmEngine engine, Func<AlgorithmEngine, s
2727
if (args2.IntValue < 0) {
2828
return ParameterError(2);
2929
}
30-
int length = Math.Min(args2.IntValue, args1.TextValue.Length);
31-
return Operand.Create(args1.TextValue.Substring(0, length));
30+
if(args2.IntValue>= args1.TextValue.Length) {
31+
return args1;
32+
}
33+
return Operand.Create(args1.TextValue.Substring(0, args2.IntValue));
3234
}
3335
public override OperandType GetResultType()
3436
{

csharp/ToolGood.Algorithm/Internals/Functions/String/Function_MID.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using ToolGood.Algorithm.Enums;
44

@@ -15,26 +15,29 @@ public Function_MID(FunctionBase[] funcs) : base(funcs)
1515
public override Operand Evaluate(AlgorithmEngine engine, Func<AlgorithmEngine, string, Operand> tempParameter)
1616
{
1717
var args1 = GetText_1(engine, tempParameter);
18-
if (args1.IsErrorOrNone) { return args1; }
18+
if(args1.IsErrorOrNone) { return args1; }
1919
var args2 = GetNumber_2(engine, tempParameter);
20-
if (args2.IsErrorOrNone) { return args2; }
20+
if(args2.IsErrorOrNone) { return args2; }
2121
var args3 = GetNumber_3(engine, tempParameter);
22-
if (args3.IsErrorOrNone) { return args3; }
22+
if(args3.IsErrorOrNone) { return args3; }
2323

2424
var text = args1.TextValue;
2525
var startIndex = args2.IntValue - engine.ExcelIndex;
2626
var length = args3.IntValue;
2727

28-
if (startIndex < 0) {
28+
if(startIndex < 0) {
2929
return ParameterError(2);
3030
}
31-
if (length < 0) {
31+
if(length < 0) {
3232
return ParameterError(3);
3333
}
34-
if (startIndex >= text.Length) {
34+
if(startIndex == 0 && length >= text.Length) {
35+
return args1;
36+
}
37+
if(startIndex >= text.Length) {
3538
return Operand.Create(string.Empty);
3639
}
37-
if (startIndex + length > text.Length) {
40+
if(startIndex + length > text.Length) {
3841
length = text.Length - startIndex;
3942
}
4043
return Operand.Create(text.Substring(startIndex, length));

csharp/ToolGood.Algorithm/Internals/Functions/String/Function_RIGHT.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using ToolGood.Algorithm.Enums;
44

@@ -15,21 +15,24 @@ public Function_RIGHT(FunctionBase[] funcs) : base(funcs)
1515
public override Operand Evaluate(AlgorithmEngine engine, Func<AlgorithmEngine, string, Operand> tempParameter)
1616
{
1717
var args1 = GetText_1(engine, tempParameter);
18-
if (args1.IsErrorOrNone) { return args1; }
18+
if(args1.IsErrorOrNone) { return args1; }
1919

20-
if (args1.TextValue.Length == 0) {
20+
if(args1.TextValue.Length == 0) {
2121
return Operand.Create(string.Empty);
2222
}
23-
if (func2 == null) {
23+
if(func2 == null) {
2424
return Operand.Create(args1.TextValue.Substring(args1.TextValue.Length - 1, 1));
2525
}
2626
var args2 = GetNumber_2(engine, tempParameter);
27-
if (args2.IsErrorOrNone) { return args2; }
28-
if (args2.IntValue < 0) {
27+
if(args2.IsErrorOrNone) { return args2; }
28+
if(args2.IntValue < 0) {
2929
return ParameterError(2);
3030
}
31-
int length = Math.Min(args2.IntValue, args1.TextValue.Length);
32-
int start = args1.TextValue.Length - length;
31+
if(args2.IntValue >= args1.TextValue.Length) {
32+
return args1;
33+
}
34+
int length = args2.IntValue;
35+
int start = args1.TextValue.Length - args2.IntValue;
3336
return Operand.Create(args1.TextValue.Substring(start, length));
3437
}
3538
public override OperandType GetResultType()

csharp/ToolGood.Algorithm/ToolGood.Algorithm.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<Product>ToolGood.Algorithm</Product>
2020
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2121
<SignAssembly>true</SignAssembly>
22-
<Version>6.2.5.26</Version>
22+
<Version>6.2.5.27</Version>
2323
<AssemblyOriginatorKeyFile>ToolGood.Algorithm.snk</AssemblyOriginatorKeyFile>
2424
<DelaySign>false</DelaySign>
2525
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\ToolGood.Algorithm.xml</DocumentationFile>

0 commit comments

Comments
 (0)