-
Notifications
You must be signed in to change notification settings - Fork 5k
Add whitespace handling methods to MemoryExtensions #111439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexRadch
wants to merge
8
commits into
dotnet:main
Choose a base branch
from
AlexRadch:MemoryExtensions.WhiteSpace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+316
−9
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b35693b
Add whitespace handling methods to MemoryExtensions
AlexRadch ad80463
Remove unused TestUtilities import in tests
AlexRadch e4069c1
Tests bugs have been fixed.
AlexRadch b495165
Apply suggestions from code review
AlexRadch 3f77a0f
Suggestions from code review
AlexRadch 866b91d
Remove unused namespaces in SearchValues.cs
AlexRadch bb9f9f4
Replaced references to `SearchValues.WhiteSpaces` with
AlexRadch 5b45997
Remove unused using directive in MemoryExtensions
AlexRadch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
261 changes: 261 additions & 0 deletions
261
src/libraries/System.Memory/tests/ReadOnlySpan/WhiteSpace.cs
This file contains hidden or 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,261 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
|
||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
namespace System.SpanTests | ||
{ | ||
public static partial class ReadOnlySpanTests | ||
{ | ||
[Fact] | ||
public static void IsWhiteSpace_True() | ||
{ | ||
Assert.True(Span<char>.Empty.IsWhiteSpace()); | ||
|
||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
Assert.True(CollectionsMarshal.AsSpan(chars).IsWhiteSpace()); | ||
} | ||
|
||
[Fact] | ||
public static void IsWhiteSpace_False() | ||
{ | ||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
var index = chars.Count; | ||
chars.AddRange(chars.ToArray()); | ||
chars.Insert(index, ' '); | ||
var span = CollectionsMarshal.AsSpan(chars); | ||
|
||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
{ | ||
chars[index] = (char)i; | ||
Assert.False(span.IsWhiteSpace()); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void ContainsAnyWhiteSpace_Found() | ||
{ | ||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
var index = chars.Count; | ||
chars.AddRange(chars.ToArray()); | ||
chars.Insert(index, ' '); | ||
var span = CollectionsMarshal.AsSpan(chars); | ||
|
||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
{ | ||
chars[index] = (char)i; | ||
Assert.True(span.ContainsAnyWhiteSpace()); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void ContainsAnyWhiteSpace_NotFound() | ||
{ | ||
Assert.False(Span<char>.Empty.ContainsAnyWhiteSpace()); | ||
|
||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
Assert.False(CollectionsMarshal.AsSpan(chars).ContainsAnyWhiteSpace()); | ||
} | ||
|
||
[Fact] | ||
public static void IndexOfAnyWhiteSpace_Found() | ||
{ | ||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
var index = chars.Count; | ||
chars.AddRange(chars.ToArray()); | ||
chars.Insert(index, ' '); | ||
chars.Insert(index, ' '); | ||
var span = CollectionsMarshal.AsSpan(chars); | ||
|
||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
{ | ||
chars[index] = (char)i; | ||
chars[index + 1] = (char)i; | ||
Assert.Equal(index, span.IndexOfAnyWhiteSpace()); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void IndexOfAnyWhiteSpace_NotFound() | ||
{ | ||
Assert.Equal(-1, Span<char>.Empty.IndexOfAnyWhiteSpace()); | ||
|
||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
Assert.Equal(-1, CollectionsMarshal.AsSpan(chars).IndexOfAnyWhiteSpace()); | ||
} | ||
|
||
[Fact] | ||
public static void IndexOfAnyExceptWhiteSpace_Found() | ||
{ | ||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
var index = chars.Count; | ||
chars.AddRange(chars.ToArray()); | ||
chars.Insert(index, ' '); | ||
chars.Insert(index, ' '); | ||
var span = CollectionsMarshal.AsSpan(chars); | ||
|
||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
{ | ||
chars[index] = (char)i; | ||
chars[index + 1] = (char)i; | ||
Assert.Equal(index, span.IndexOfAnyExceptWhiteSpace()); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void IndexOfAnyExceptWhiteSpace_NotFound() | ||
{ | ||
Assert.Equal(-1, Span<char>.Empty.IndexOfAnyExceptWhiteSpace()); | ||
|
||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
Assert.Equal(-1, CollectionsMarshal.AsSpan(chars).IndexOfAnyExceptWhiteSpace()); | ||
} | ||
|
||
[Fact] | ||
public static void LastIndexOfAnyWhiteSpace_Found() | ||
{ | ||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
var index = chars.Count; | ||
chars.AddRange(chars.ToArray()); | ||
chars.Insert(index, ' '); | ||
chars.Insert(index, ' '); | ||
var span = CollectionsMarshal.AsSpan(chars); | ||
|
||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
{ | ||
chars[index] = (char)i; | ||
chars[index + 1] = (char)i; | ||
Assert.Equal(index + 1, span.LastIndexOfAnyWhiteSpace()); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void LastIndexOfAnyWhiteSpace_NotFound() | ||
{ | ||
Assert.Equal(-1, Span<char>.Empty.LastIndexOfAnyWhiteSpace()); | ||
|
||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
Assert.Equal(-1, CollectionsMarshal.AsSpan(chars).LastIndexOfAnyWhiteSpace()); | ||
} | ||
|
||
[Fact] | ||
public static void LastIndexOfAnyExceptWhiteSpace_Found() | ||
{ | ||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
var index = chars.Count; | ||
chars.AddRange(chars.ToArray()); | ||
chars.Insert(index, ' '); | ||
chars.Insert(index, ' '); | ||
var span = CollectionsMarshal.AsSpan(chars); | ||
|
||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (!char.IsWhiteSpace((char)i)) | ||
{ | ||
chars[index] = (char)i; | ||
chars[index + 1] = (char)i; | ||
Assert.Equal(index + 1, span.LastIndexOfAnyExceptWhiteSpace()); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void LastIndexOfAnyExceptWhiteSpace_NotFound() | ||
{ | ||
Assert.Equal(-1, Span<char>.Empty.LastIndexOfAnyExceptWhiteSpace()); | ||
|
||
List<char> chars = []; | ||
for (int i = 0; i <= char.MaxValue; i++) | ||
{ | ||
if (char.IsWhiteSpace((char)i)) | ||
chars.Add((char)i); | ||
} | ||
|
||
Assert.Equal(-1, CollectionsMarshal.AsSpan(chars).LastIndexOfAnyExceptWhiteSpace()); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.