-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from snowfrogdev/pvaillancourt/is-acii
Add an IsAscii extension method for string
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,36 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Ardalis.Extensions.StringManipulation; | ||
|
||
public static partial class StringManipulationExtensions | ||
{ | ||
/// <summary> | ||
/// Checks if all characters in this string are within the ASCII range. | ||
/// </summary> | ||
/// <example> | ||
/// <code> | ||
/// var ascii = "hello!\n"; | ||
/// var nonAscii = "Grüße, Jürgen ❤"; | ||
/// | ||
/// Assert.True(ascii.IsAscii()); | ||
/// Assert.False(nonAscii.IsAscii()); | ||
/// </code> | ||
/// </example> | ||
public static bool IsAscii(this string text) | ||
{ | ||
return text.ToCharArray().All(c => IsAscii(c)); | ||
} | ||
|
||
|
||
// Return true for all characters below or equal U+007f, which is ASCII. | ||
|
||
/// <summary> | ||
/// Returns <see langword="true"/> if <paramref name="c"/> is an ASCII | ||
/// character ([ U+0000..U+007F ]). | ||
/// </summary> | ||
/// <remarks> | ||
/// Per http://www.unicode.org/glossary/#ASCII, ASCII is only U+0000..U+007F. | ||
/// </remarks> | ||
private static bool IsAscii(char c) => (uint)c <= '\x007F'; | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Ardalis.Extensions.UnitTests/StringManipulation/IsAsciiTests.cs
This file contains 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,19 @@ | ||
using Ardalis.Extensions.StringManipulation; | ||
using Xunit; | ||
|
||
namespace Ardalis.Extensions.UnitTests.StringManipulation; | ||
|
||
public class IsAsciiTests | ||
{ | ||
[Theory] | ||
[InlineData("", true)] | ||
[InlineData("Hello!\n", true)] | ||
[InlineData("banana\0\x7F", true)] | ||
[InlineData("Vi\xe1\xbb\x87t Nam", false)] | ||
[InlineData("ประเทศไทย中华Việt Nam", false)] | ||
[InlineData("Grüße, Jürgen ❤", false)] | ||
public void ValidatesIfStringIsAscii(string input, bool expected) | ||
{ | ||
Assert.Equal(expected, input.IsAscii()); | ||
} | ||
} |