Skip to content

Commit 194d98c

Browse files
Jim8yshargon
andauthored
Regex: add couple of regex methods to the devpack (#796)
* add couple of regex methods to the devpack * Rename methods * Clean code * update regex methods * add comments --------- Co-authored-by: Fernando Diaz Toledano <[email protected]>
1 parent fd14cb2 commit 194d98c

File tree

5 files changed

+237
-2
lines changed

5 files changed

+237
-2
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using Neo.SmartContract.Framework.Native;
2+
namespace Neo.SmartContract.Framework;
3+
4+
public static class ByteStringExtension
5+
{
6+
/// <summary>
7+
/// Denotes whether provided character is a number.
8+
/// </summary>
9+
/// <param name="byteString">Input to check</param>
10+
/// <returns>True if is number</returns>
11+
public static bool IsNumber(this ByteString byteString)
12+
{
13+
foreach (var value in byteString)
14+
{
15+
if (value is < 48 or > 57)
16+
return false;
17+
}
18+
return true;
19+
}
20+
21+
/// <summary>
22+
/// Denotes whether provided character is a lowercase letter.
23+
/// </summary>
24+
/// <param name="byteString">Input to check</param>
25+
/// <returns>True if is Alpha character</returns>
26+
public static bool IsLowerAlphabet(this ByteString byteString)
27+
{
28+
foreach (var value in byteString)
29+
{
30+
if (value is < 97 or > 122)
31+
return false;
32+
}
33+
return true;
34+
}
35+
36+
/// <summary>
37+
/// Denotes whether provided character is a lowercase letter.
38+
/// </summary>
39+
/// <param name="byteString">Input to check</param>
40+
/// <returns>True if is Alpha character</returns>
41+
public static bool IsUpperAlphabet(this ByteString byteString)
42+
{
43+
foreach (var value in byteString)
44+
{
45+
if (value is < 65 or > 90)
46+
return false;
47+
}
48+
return true;
49+
}
50+
51+
/// <summary>
52+
/// Denotes whether provided character is a lowercase letter.
53+
/// </summary>
54+
/// <param name="byteString">Input to check</param>
55+
/// <returns>True if is Alpha character</returns>
56+
public static bool IsAlphabet(this ByteString byteString)
57+
{
58+
foreach (var value in byteString)
59+
{
60+
if (!((value >= 65 && value <= 90) || (value >= 97 && value <= 122)))
61+
return false;
62+
}
63+
return true;
64+
}
65+
66+
/// <summary>
67+
/// Returns the index of the first occurrence of a given value in an array.
68+
/// </summary>
69+
/// <param name="byteString">Array where to search.</param>
70+
/// <param name="byteToFind">Array to search.</param>
71+
/// <returns>Index where it is located or -1</returns>
72+
public static int IndexOf(this ByteString byteString, ByteString byteToFind)
73+
{
74+
return StdLib.MemorySearch(byteString, byteToFind);
75+
}
76+
77+
/// <summary>
78+
/// Determines whether the beginning of this string instance matches the specified string when compared using the specified culture.
79+
/// </summary>
80+
/// <param name="byteString">Array where to search.</param>
81+
/// <param name="byteToFind">Array to search.</param>
82+
/// <returns>True if start with</returns>
83+
public static bool StartWith(this ByteString byteString, ByteString byteToFind)
84+
{
85+
return StdLib.MemorySearch(byteString, byteToFind) == 0;
86+
}
87+
88+
/// <summary>
89+
/// Determines whether the end of this string instance matches a specified string.
90+
/// </summary>
91+
/// <param name="byteString">Array where to search.</param>
92+
/// <param name="byteToFind">Array to search.</param>
93+
/// <returns>True if ends with</returns>
94+
public static bool EndsWith(this ByteString byteString, ByteString byteToFind)
95+
{
96+
return StdLib.MemorySearch(byteString, byteToFind) + byteToFind.Length == byteString.Length;
97+
}
98+
99+
/// <summary>
100+
/// Checks if the <see cref="ByteString"/> contains the given <see cref="ByteString"/>.
101+
/// </summary>
102+
/// <param name="byteString"><see cref="ByteString"/> to search.</param>
103+
/// <param name="byteToFind"><see cref="ByteString"/> to be searched.</param>
104+
/// <returns></returns>
105+
public static bool Contains(this ByteString byteString, ByteString byteToFind)
106+
{
107+
return StdLib.MemorySearch(byteString, byteToFind) != -1;
108+
}
109+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Neo.SmartContract.Framework.UnitTests.TestClasses
2+
{
3+
public class Contract_Regex : SmartContract
4+
{
5+
public static bool TestStartWith()
6+
{
7+
return ((ByteString)"Hello World").StartWith("Hello");
8+
}
9+
10+
public static int TestIndexOf()
11+
{
12+
return ((ByteString)"Hello World").IndexOf("o");
13+
}
14+
15+
public static bool TestEndWith()
16+
{
17+
return ((ByteString)"Hello World").EndsWith("World");
18+
}
19+
20+
public static bool TestContains()
21+
{
22+
return ((ByteString)"Hello World").Contains("ll");
23+
}
24+
25+
public static bool TestNumberOnly()
26+
{
27+
return ((ByteString)"0123456789").IsNumber();
28+
}
29+
30+
public static bool TestAlphabetOnly()
31+
{
32+
return ((ByteString)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").IsAlphabet();
33+
}
34+
35+
public static bool TestLowerAlphabetOnly()
36+
{
37+
return ((ByteString)"abcdefghijklmnopqrstuvwxyz").IsAlphabet();
38+
}
39+
40+
public static bool TestUpperAlphabetOnly()
41+
{
42+
return ((ByteString)"ABCDEFGHIJKLMNOPQRSTUVWXYZ").IsAlphabet();
43+
}
44+
}
45+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Neo.VM.Types;
3+
using Neo.SmartContract.TestEngine;
4+
5+
namespace Neo.SmartContract.Framework.UnitTests
6+
{
7+
[TestClass]
8+
public class RegexTest
9+
{
10+
private TestEngine.TestEngine _engine;
11+
12+
[TestInitialize]
13+
public void Init()
14+
{
15+
_engine = new TestEngine.TestEngine(snapshot: new TestDataCache());
16+
_engine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_Regex.cs");
17+
}
18+
19+
[TestMethod]
20+
public void TestStartWith()
21+
{
22+
var result = _engine.ExecuteTestCaseStandard("testStartWith");
23+
Assert.AreEqual(1, result.Count);
24+
var item = result.Pop<Boolean>();
25+
Assert.IsTrue(item.GetBoolean());
26+
}
27+
28+
[TestMethod]
29+
public void TestIndexOf()
30+
{
31+
_engine.Reset();
32+
var result = _engine.ExecuteTestCaseStandard("testIndexOf");
33+
Assert.AreEqual(1, result.Count);
34+
var item = result.Pop<Integer>();
35+
Assert.AreEqual(4, item.GetInteger());
36+
}
37+
38+
[TestMethod]
39+
public void TestEndWith()
40+
{
41+
var result = _engine.ExecuteTestCaseStandard("testEndWith");
42+
Assert.AreEqual(1, result.Count);
43+
var item = result.Pop<Boolean>();
44+
Assert.IsTrue(item.GetBoolean());
45+
}
46+
[TestMethod]
47+
public void TestContains()
48+
{
49+
var result = _engine.ExecuteTestCaseStandard("testContains");
50+
Assert.AreEqual(1, result.Count);
51+
var item = result.Pop<Boolean>();
52+
Assert.IsTrue(item.GetBoolean());
53+
}
54+
55+
[TestMethod]
56+
public void TestNumberOnly()
57+
{
58+
var result = _engine.ExecuteTestCaseStandard("testNumberOnly");
59+
Assert.AreEqual(1, result.Count);
60+
var item = result.Pop<Boolean>();
61+
Assert.IsTrue(item.GetBoolean());
62+
}
63+
64+
[TestMethod]
65+
public void TestAlphabetOnly()
66+
{
67+
var result = _engine.ExecuteTestCaseStandard("testAlphabetOnly");
68+
Assert.AreEqual(1, result.Count);
69+
Assert.IsTrue(result.Pop<Boolean>().GetBoolean());
70+
71+
_engine.Reset();
72+
result = _engine.ExecuteTestCaseStandard("testLowerAlphabetOnly");
73+
Assert.AreEqual(1, result.Count);
74+
Assert.IsTrue(result.Pop<Boolean>().GetBoolean());
75+
76+
_engine.Reset();
77+
result = _engine.ExecuteTestCaseStandard("testUpperAlphabetOnly");
78+
Assert.AreEqual(1, result.Count);
79+
Assert.IsTrue(result.Pop<Boolean>().GetBoolean());
80+
}
81+
}
82+
}

tests/Neo.SmartContract.Framework.UnitTests/Services/ContractTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Neo.VM;
66
using Neo.VM.Types;
77
using System;
8-
using Neo.Persistence;
98
using Neo.SmartContract.TestEngine;
109
using Array = Neo.VM.Types.Array;
1110

tests/Neo.SmartContract.Template.UnitTests/templates/neocontractoracle/OracleRequestTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Neo.SmartContract.Template.UnitTests.templates.neocontractowner
1010
{
1111
/// <summary>
12-
/// You need to build the solution to resolve Ownable class.
12+
/// You need to build the solution to resolve OracleRequest class.
1313
/// </summary>
1414
[TestClass]
1515
public class OracleRequestTests : TestBase<OracleRequest>

0 commit comments

Comments
 (0)