Skip to content

Commit 3147ceb

Browse files
committed
Added TryGetHexString tests.
1 parent 28515e2 commit 3147ceb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

ReClass.NET/Extensions/StringExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ public static string LimitLength(this string s, int length)
9797
return s.Substring(0, length);
9898
}
9999

100-
private static readonly Regex HexRegex = new Regex("(0x|h)?([0-9A-F]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
100+
private static readonly Regex hexadecimalValueRegex = new Regex("^(0x|h)?([0-9A-F]+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
101101
public static bool TryGetHexString(this string s, out string value)
102102
{
103-
var match = HexRegex.Match(s);
103+
Contract.Requires(s != null);
104+
105+
var match = hexadecimalValueRegex.Match(s);
104106
value = match.Success ? match.Groups[2].Value : null;
105107

106108
return match.Success;

ReClass.NET_Tests/Extensions/StringExtensionTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,24 @@ public void TestIsNotLikelyPrintableData(params char[] sut)
146146
{
147147
Check.That(sut.IsPrintableData()).IsFalse();
148148
}
149+
150+
[Theory]
151+
[InlineData("", false, null)]
152+
[InlineData("-", false, null)]
153+
[InlineData("-0", false, null)]
154+
[InlineData("-0x0", false, null)]
155+
[InlineData("-h0", false, null)]
156+
[InlineData("0", true, "0")]
157+
[InlineData("h0", true, "0")]
158+
[InlineData("0x0", true, "0")]
159+
[InlineData("0123456789abcdef", true, "0123456789abcdef")]
160+
[InlineData("h0123456789abcdef", true, "0123456789abcdef")]
161+
[InlineData("0x0123456789abcdef", true, "0123456789abcdef")]
162+
[InlineData("0123456789ABCDEF", true, "0123456789ABCDEF")]
163+
public void TestTryGetHexString(string input, bool expectedResult, string expectedValue)
164+
{
165+
Check.That(input.TryGetHexString(out var value)).IsEqualTo(expectedResult);
166+
Check.That(value).IsEqualTo(expectedValue);
167+
}
149168
}
150169
}

0 commit comments

Comments
 (0)