Skip to content

Commit

Permalink
Merge pull request #6 from rameel/stringview-enhancement
Browse files Browse the repository at this point in the history
StringView enhancement
  • Loading branch information
rameel authored Jul 11, 2024
2 parents 986fbae + 1d48242 commit 3839884
Show file tree
Hide file tree
Showing 4 changed files with 373 additions and 17 deletions.
5 changes: 5 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
</PropertyGroup>
</Project>
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Ramstack.Structures is a .NET library providing various data structures and utilities.

[![.NET](https://github.com/rameel/ramstack.structures/actions/workflows/test.yml/badge.svg)](https://github.com/rameel/ramstack.structures/actions/workflows/test.yml)

## Installation

To install the `Ramstack.Structures` [NuGet package](https://www.nuget.org/packages/Ramstack.Structures) to your project, use the following command:
Expand Down Expand Up @@ -43,6 +45,16 @@ foreach (ref readonly HeavyStruct s in view)
...
}
```
## Changelog

### 1.2.0
- Add `Trim` overloads to `StringView` class

## Supported versions

| | Version |
|------|---------|
| .NET | 6, 7, 8 |

## Contributions

Expand Down
107 changes: 107 additions & 0 deletions Ramstack.Structures.Tests/Text/StringViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,41 @@ public void TrimStart(string value, int index, int length)
Is.EqualTo(value.AsSpan(index, length).TrimStart().ToString()));
}

[TestCase("", '-')]
[TestCase("--------", '-')]
[TestCase("--++value", '-')]
[TestCase("--++value", '+')]
[TestCase(" value", '-')]
[TestCase(" value", ' ')]
[TestCase("value", '-')]
public void TrimStart_TrimChar(string value, char trimChar)
{
Assert.That(
value.AsView().TrimStart(trimChar).ToString(),
Is.EqualTo(value.TrimStart(trimChar)));
}

[TestCase("", "-+")]
[TestCase("--++--++-", "-+")]
[TestCase("--++value", "-+")]
[TestCase("--++value", "?=")]
[TestCase(" value", "-+")]
[TestCase(" value", "-+")]
[TestCase(" value", "")]
[TestCase(" value", null)]
[TestCase("value", "+-")]
[TestCase("value", null)]
public void TrimStart_TrimChars(string value, string? trimChars)
{
Assert.That(
value.AsView().TrimStart(trimChars?.ToCharArray()).ToString(),
Is.EqualTo(value.TrimStart(trimChars?.ToCharArray())));

Assert.That(
value.AsView().TrimStart(trimChars.AsSpan()).ToString(),
Is.EqualTo(value.TrimStart(trimChars?.ToCharArray())));
}

[TestCase("", 0, 0)]
[TestCase(" ", 0, 4)]
[TestCase(" ", 1, 3)]
Expand All @@ -76,6 +111,42 @@ public void TrimEnd(string value, int index, int length)
Is.EqualTo(value.AsSpan(index, length).TrimEnd().ToString()));
}

[TestCase("", '-')]
[TestCase("---------", '-')]
[TestCase("value--++", '-')]
[TestCase("value--++", '-')]
[TestCase("value--++", '+')]
[TestCase("value ", '-')]
[TestCase("value ", ' ')]
[TestCase("value", '-')]
public void TrimEnd_TrimChar(string value, char trimChar)
{
Assert.That(
value.AsView().TrimEnd(trimChar).ToString(),
Is.EqualTo(value.TrimEnd(trimChar)));
}

[TestCase("", "-+")]
[TestCase("++--++--+", "-+")]
[TestCase("value++--", "-+")]
[TestCase("value++--", "?=")]
[TestCase("value ", "-+")]
[TestCase("value ", "-+")]
[TestCase("value ", "")]
[TestCase("value ", null)]
[TestCase("value", "+-")]
[TestCase("value", null)]
public void TrimEnd_TrimChars(string value, string? trimChars)
{
Assert.That(
value.AsView().TrimEnd(trimChars?.ToCharArray()).ToString(),
Is.EqualTo(value.TrimEnd(trimChars?.ToCharArray())));

Assert.That(
value.AsView().TrimEnd(trimChars.AsSpan()).ToString(),
Is.EqualTo(value.TrimEnd(trimChars?.ToCharArray())));
}

[TestCase("", 0, 0)]
[TestCase(" ", 0, 4)]
[TestCase(" ", 1, 3)]
Expand All @@ -96,6 +167,42 @@ public void Trim(string value, int index, int length)
Is.EqualTo(value.AsSpan(index, length).Trim().ToString()));
}

[TestCase("", '-')]
[TestCase("---------", '-')]
[TestCase("++--value--++", '-')]
[TestCase("++--value--++", '-')]
[TestCase("++--value--++", '+')]
[TestCase(" value ", '-')]
[TestCase(" value ", ' ')]
[TestCase("value", '-')]
public void Trim_TrimChar(string value, char trimChar)
{
Assert.That(
value.AsView().Trim(trimChar).ToString(),
Is.EqualTo(value.Trim(trimChar)));
}

[TestCase("", "-+")]
[TestCase("++--++--+", "-+")]
[TestCase("--++value++--", "-+")]
[TestCase("--++value++--", "?=")]
[TestCase(" value ", "-+")]
[TestCase(" value ", "-+")]
[TestCase(" value ", "")]
[TestCase(" value ", null)]
[TestCase("value", "+-")]
[TestCase("value", null)]
public void Trim_TrimChars(string value, string? trimChars)
{
Assert.That(
value.AsView().Trim(trimChars?.ToCharArray()).ToString(),
Is.EqualTo(value.Trim(trimChars?.ToCharArray())));

Assert.That(
value.AsView().Trim(trimChars.AsSpan()).ToString(),
Is.EqualTo(value.Trim(trimChars?.ToCharArray())));
}

[TestCase("value", 0, 0)]
[TestCase("value", 0, 5)]
[TestCase("value", 1, 4)]
Expand Down
Loading

0 comments on commit 3839884

Please sign in to comment.