Skip to content

Commit d8ceb95

Browse files
committed
Added contains and aligned string.Empty in IndexOf
1 parent 494440e commit d8ceb95

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
###Added
10+
- `Contains` method.
11+
912
### Fixed
1013
- Smaller tweaks in CI/CD
14+
- `IndexOf` and `LastIndexOf` did not return 0 when passing an empty string. Now it is aligned to `string.IndexOf`.
1115

1216
### Removed
1317
- Debug symbol package (snupkg) due to the many constraints of NuGet.org
@@ -76,4 +80,4 @@ This release brings extensions to the `ValueStringBuilder` API. For `v1.0` the `
7680

7781
[1.0.0]: https://github.com/linkdotnet/StringBuilder/compare/0.9.5...1.0.0
7882

79-
[0.9.5]: https://github.com/linkdotnet/StringBuilder/compare/0.9.4...0.9.5
83+
[0.9.5]: https://github.com/linkdotnet/StringBuilder/compare/0.9.4...0.9.5

src/LinkDotNet.StringBuilder/LinkDotNet.StringBuilder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
1717
<PackageIcon>logo.png</PackageIcon>
1818
<PackageIconUrl>https://github.com/linkdotnet/StringBuilder/blob/main/logo.png</PackageIconUrl>
19+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1920
</PropertyGroup>
2021

2122
<ItemGroup>

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ public int IndexOf(ReadOnlySpan<char> word, int startIndex)
172172
throw new ArgumentOutOfRangeException(nameof(startIndex), "Start index can't be smaller than 0.");
173173
}
174174

175+
if (word.IsEmpty)
176+
{
177+
return 0;
178+
}
179+
175180
return NaiveSearch.FindFirst(buffer[startIndex..bufferPosition], word);
176181
}
177182

@@ -196,9 +201,24 @@ public int LastIndexOf(ReadOnlySpan<char> word, int startIndex)
196201
throw new ArgumentOutOfRangeException(nameof(startIndex), "Start index can't be smaller than 0.");
197202
}
198203

204+
if (word.IsEmpty)
205+
{
206+
return 0;
207+
}
208+
199209
return NaiveSearch.FindLast(buffer[startIndex..bufferPosition], word);
200210
}
201211

212+
/// <summary>
213+
/// Returns a value indicating whether a specified substring occurs within this string.
214+
/// </summary>
215+
/// <param name="word">Word to look for in this string.</param>
216+
/// <returns>True if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.</returns>
217+
/// <remarks>
218+
/// This method performs an ordinal (case-sensitive and culture-insensitive) comparison.
219+
/// </remarks>
220+
public bool Contains(ReadOnlySpan<char> word) => IndexOf(word) != -1;
221+
202222
private void Grow(int capacity = 0)
203223
{
204224
var currentSize = buffer.Length;

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,13 @@ public void ShouldReturnMinusOneWordIsLongerThanString()
217217
index.Should().Be(-1);
218218
}
219219

220-
[Theory]
221-
[InlineData("", "word")]
222-
[InlineData("word", "")]
223-
public void ShouldReturnMinusOneIfStringOrWordIsEmpty(string text, string word)
220+
[Fact]
221+
public void ShouldReturnMinusOneIfStringIsEmpty()
224222
{
225223
var stringBuilder = new ValueStringBuilder();
226-
stringBuilder.Append(text);
224+
stringBuilder.Append(string.Empty);
227225

228-
var index = stringBuilder.IndexOf(word);
226+
var index = stringBuilder.IndexOf("word");
229227

230228
index.Should().Be(-1);
231229
}
@@ -289,4 +287,40 @@ public void ShouldFindLastOccurenceInSlice()
289287

290288
index.Should().Be(0);
291289
}
290+
291+
[Fact]
292+
public void ShouldReturnZeroWhenEmptyStringInIndexOf()
293+
{
294+
var builder = new ValueStringBuilder();
295+
builder.Append("Hello");
296+
297+
var index = builder.IndexOf(string.Empty, 6);
298+
299+
index.Should().Be(0);
300+
}
301+
302+
[Fact]
303+
public void ShouldReturnZeroWhenEmptyStringInLastIndexOf()
304+
{
305+
var builder = new ValueStringBuilder();
306+
builder.Append("Hello");
307+
308+
var index = builder.LastIndexOf(string.Empty, 6);
309+
310+
index.Should().Be(0);
311+
}
312+
313+
[Theory]
314+
[InlineData("Hello", true)]
315+
[InlineData("hello", false)]
316+
[InlineData("", true)]
317+
public void ShouldReturnIfStringIsPresent(string word, bool expected)
318+
{
319+
var builder = new ValueStringBuilder();
320+
builder.Append("Hello");
321+
322+
var index = builder.Contains(word);
323+
324+
index.Should().Be(expected);
325+
}
292326
}

0 commit comments

Comments
 (0)