Skip to content

Commit 71dc3c5

Browse files
committed
Feat: Add ReadOnlySpan<char> extension methods for escaped character checks with tests
1 parent d5be0a0 commit 71dc3c5

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
// ReSharper disable once CheckNamespace
5+
namespace System;
6+
// ---------------------------------------------------------------------------------------------------------------------
7+
// Code
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
public static class ReadOnlySpanOfCharExtensions {
10+
public static bool IsNotEscapedCharacterAtIndex(this scoped ReadOnlySpan<char> source, int atIndex) {
11+
int backslashCount = 0;
12+
for (int i = atIndex - 1; i >= 0 && source[i] is '\\'; i--) {
13+
backslashCount++;
14+
}
15+
return backslashCount % 2 == 0;
16+
}
17+
18+
public static bool IsEscapedCharacterAtIndex(this scoped ReadOnlySpan<char> source, int atIndex) {
19+
return !IsNotEscapedCharacterAtIndex(source, atIndex);
20+
}
21+
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// ---------------------------------------------------------------------------------------------------------------------
2+
// Imports
3+
// ---------------------------------------------------------------------------------------------------------------------
4+
namespace Tests.CodeOfChaos.Extensions;
5+
6+
// ---------------------------------------------------------------------------------------------------------------------
7+
// Code
8+
// ---------------------------------------------------------------------------------------------------------------------
9+
public class ReadOnlySpanOfCharExtensionsTests {
10+
11+
[Test]
12+
[Arguments("", 0, true)]
13+
[Arguments("a", 0, true)]
14+
[Arguments(@"a\", 0, true)]
15+
[Arguments(@"a\b", 2, false)]
16+
[Arguments(@"\a", 1, false)]
17+
public async Task IsNotEscapedCharacterAtIndex_ShouldWork(string input, int index, bool expected) {
18+
// Arrange
19+
ReadOnlySpan<char> span = input.AsSpan();
20+
21+
// Act
22+
bool result = span.IsNotEscapedCharacterAtIndex(index);
23+
24+
// Assert
25+
await Assert.That(result).IsEqualTo(expected);
26+
}
27+
28+
[Test]
29+
[Arguments("", 0, false)]
30+
[Arguments("a", 0, false)]
31+
[Arguments(@"a\", 0, false)]
32+
[Arguments(@"a\b", 2, true)]
33+
[Arguments(@"\a", 1, true)]
34+
public async Task IsEscapedCharacterAtIndex_ShouldWork(string input, int index, bool expected) {
35+
// Arrange
36+
ReadOnlySpan<char> span = input.AsSpan();
37+
38+
// Act
39+
bool result = span.IsEscapedCharacterAtIndex(index);
40+
41+
// Assert
42+
await Assert.That(result).IsEqualTo(expected);
43+
}
44+
}

0 commit comments

Comments
 (0)