-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
5 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/Neo.SmartContract.Framework/ByteString.Extension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using Neo.SmartContract.Framework.Native; | ||
namespace Neo.SmartContract.Framework; | ||
|
||
public static class ByteStringExtension | ||
{ | ||
/// <summary> | ||
/// Denotes whether provided character is a number. | ||
/// </summary> | ||
/// <param name="byteString">Input to check</param> | ||
/// <returns>True if is number</returns> | ||
public static bool IsNumber(this ByteString byteString) | ||
{ | ||
foreach (var value in byteString) | ||
{ | ||
if (value is < 48 or > 57) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Denotes whether provided character is a lowercase letter. | ||
/// </summary> | ||
/// <param name="byteString">Input to check</param> | ||
/// <returns>True if is Alpha character</returns> | ||
public static bool IsLowerAlphabet(this ByteString byteString) | ||
{ | ||
foreach (var value in byteString) | ||
{ | ||
if (value is < 97 or > 122) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Denotes whether provided character is a lowercase letter. | ||
/// </summary> | ||
/// <param name="byteString">Input to check</param> | ||
/// <returns>True if is Alpha character</returns> | ||
public static bool IsUpperAlphabet(this ByteString byteString) | ||
{ | ||
foreach (var value in byteString) | ||
{ | ||
if (value is < 65 or > 90) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Denotes whether provided character is a lowercase letter. | ||
/// </summary> | ||
/// <param name="byteString">Input to check</param> | ||
/// <returns>True if is Alpha character</returns> | ||
public static bool IsAlphabet(this ByteString byteString) | ||
{ | ||
foreach (var value in byteString) | ||
{ | ||
if (!((value >= 65 && value <= 90) || (value >= 97 && value <= 122))) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the index of the first occurrence of a given value in an array. | ||
/// </summary> | ||
/// <param name="byteString">Array where to search.</param> | ||
/// <param name="byteToFind">Array to search.</param> | ||
/// <returns>Index where it is located or -1</returns> | ||
public static int IndexOf(this ByteString byteString, ByteString byteToFind) | ||
{ | ||
return StdLib.MemorySearch(byteString, byteToFind); | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the beginning of this string instance matches the specified string when compared using the specified culture. | ||
/// </summary> | ||
/// <param name="byteString">Array where to search.</param> | ||
/// <param name="byteToFind">Array to search.</param> | ||
/// <returns>True if start with</returns> | ||
public static bool StartWith(this ByteString byteString, ByteString byteToFind) | ||
{ | ||
return StdLib.MemorySearch(byteString, byteToFind) == 0; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the end of this string instance matches a specified string. | ||
/// </summary> | ||
/// <param name="byteString">Array where to search.</param> | ||
/// <param name="byteToFind">Array to search.</param> | ||
/// <returns>True if ends with</returns> | ||
public static bool EndsWith(this ByteString byteString, ByteString byteToFind) | ||
{ | ||
return StdLib.MemorySearch(byteString, byteToFind) + byteToFind.Length == byteString.Length; | ||
} | ||
|
||
/// <summary> | ||
/// Checks if the <see cref="ByteString"/> contains the given <see cref="ByteString"/>. | ||
/// </summary> | ||
/// <param name="byteString"><see cref="ByteString"/> to search.</param> | ||
/// <param name="byteToFind"><see cref="ByteString"/> to be searched.</param> | ||
/// <returns></returns> | ||
public static bool Contains(this ByteString byteString, ByteString byteToFind) | ||
{ | ||
return StdLib.MemorySearch(byteString, byteToFind) != -1; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
tests/Neo.SmartContract.Framework.TestContracts/Contract_Regex.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
namespace Neo.SmartContract.Framework.UnitTests.TestClasses | ||
{ | ||
public class Contract_Regex : SmartContract | ||
{ | ||
public static bool TestStartWith() | ||
{ | ||
return ((ByteString)"Hello World").StartWith("Hello"); | ||
} | ||
|
||
public static int TestIndexOf() | ||
{ | ||
return ((ByteString)"Hello World").IndexOf("o"); | ||
} | ||
|
||
public static bool TestEndWith() | ||
{ | ||
return ((ByteString)"Hello World").EndsWith("World"); | ||
} | ||
|
||
public static bool TestContains() | ||
{ | ||
return ((ByteString)"Hello World").Contains("ll"); | ||
} | ||
|
||
public static bool TestNumberOnly() | ||
{ | ||
return ((ByteString)"0123456789").IsNumber(); | ||
} | ||
|
||
public static bool TestAlphabetOnly() | ||
{ | ||
return ((ByteString)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").IsAlphabet(); | ||
} | ||
|
||
public static bool TestLowerAlphabetOnly() | ||
{ | ||
return ((ByteString)"abcdefghijklmnopqrstuvwxyz").IsAlphabet(); | ||
} | ||
|
||
public static bool TestUpperAlphabetOnly() | ||
{ | ||
return ((ByteString)"ABCDEFGHIJKLMNOPQRSTUVWXYZ").IsAlphabet(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.VM.Types; | ||
using Neo.SmartContract.TestEngine; | ||
|
||
namespace Neo.SmartContract.Framework.UnitTests | ||
{ | ||
[TestClass] | ||
public class RegexTest | ||
{ | ||
private TestEngine.TestEngine _engine; | ||
|
||
[TestInitialize] | ||
public void Init() | ||
{ | ||
_engine = new TestEngine.TestEngine(snapshot: new TestDataCache()); | ||
_engine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_Regex.cs"); | ||
} | ||
|
||
[TestMethod] | ||
public void TestStartWith() | ||
{ | ||
var result = _engine.ExecuteTestCaseStandard("testStartWith"); | ||
Assert.AreEqual(1, result.Count); | ||
var item = result.Pop<Boolean>(); | ||
Assert.IsTrue(item.GetBoolean()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestIndexOf() | ||
{ | ||
_engine.Reset(); | ||
var result = _engine.ExecuteTestCaseStandard("testIndexOf"); | ||
Assert.AreEqual(1, result.Count); | ||
var item = result.Pop<Integer>(); | ||
Assert.AreEqual(4, item.GetInteger()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestEndWith() | ||
{ | ||
var result = _engine.ExecuteTestCaseStandard("testEndWith"); | ||
Assert.AreEqual(1, result.Count); | ||
var item = result.Pop<Boolean>(); | ||
Assert.IsTrue(item.GetBoolean()); | ||
} | ||
[TestMethod] | ||
public void TestContains() | ||
{ | ||
var result = _engine.ExecuteTestCaseStandard("testContains"); | ||
Assert.AreEqual(1, result.Count); | ||
var item = result.Pop<Boolean>(); | ||
Assert.IsTrue(item.GetBoolean()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestNumberOnly() | ||
{ | ||
var result = _engine.ExecuteTestCaseStandard("testNumberOnly"); | ||
Assert.AreEqual(1, result.Count); | ||
var item = result.Pop<Boolean>(); | ||
Assert.IsTrue(item.GetBoolean()); | ||
} | ||
|
||
[TestMethod] | ||
public void TestAlphabetOnly() | ||
{ | ||
var result = _engine.ExecuteTestCaseStandard("testAlphabetOnly"); | ||
Assert.AreEqual(1, result.Count); | ||
Assert.IsTrue(result.Pop<Boolean>().GetBoolean()); | ||
|
||
_engine.Reset(); | ||
result = _engine.ExecuteTestCaseStandard("testLowerAlphabetOnly"); | ||
Assert.AreEqual(1, result.Count); | ||
Assert.IsTrue(result.Pop<Boolean>().GetBoolean()); | ||
|
||
_engine.Reset(); | ||
result = _engine.ExecuteTestCaseStandard("testUpperAlphabetOnly"); | ||
Assert.AreEqual(1, result.Count); | ||
Assert.IsTrue(result.Pop<Boolean>().GetBoolean()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters