Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Move GetRuneAt / TryGetRuneAt to System.String
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks committed Nov 12, 2018
1 parent 415cd7c commit 904718e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/System.Runtime/ref/System.Runtime.cs
Expand Up @@ -2300,6 +2300,7 @@ public sealed partial class String : System.Collections.Generic.IEnumerable<char
public override int GetHashCode() { throw null; }
public int GetHashCode(System.StringComparison comparisonType) { throw null; }
public System.TypeCode GetTypeCode() { throw null; }
public System.Text.Rune GetRuneAt(int index) { throw null; }
public int IndexOf(char value) { throw null; }
public int IndexOf(char value, int startIndex) { throw null; }
public int IndexOf(char value, int startIndex, int count) { throw null; }
Expand Down Expand Up @@ -2408,6 +2409,7 @@ public sealed partial class String : System.Collections.Generic.IEnumerable<char
public System.String TrimStart() { throw null; }
public System.String TrimStart(char trimChar) { throw null; }
public System.String TrimStart(params char[] trimChars) { throw null; }
public bool TryGetRuneAt(int index, out System.Text.Rune value) { throw null; }
}
public enum StringComparison
{
Expand Down Expand Up @@ -7602,7 +7604,6 @@ public enum NormalizationForm
public override bool Equals(object obj) { throw null; }
public bool Equals(Rune other) { throw null; }
public override int GetHashCode() { throw null; }
public static Rune GetRuneAt(string input, int index) { throw null; }
public static bool IsValid(int value) { throw null; }
[CLSCompliant(false)]
public static bool IsValid(uint value) { throw null; }
Expand All @@ -7612,7 +7613,6 @@ public enum NormalizationForm
[CLSCompliant(false)]
public static bool TryCreate(uint value, out Rune result) { throw null; }
public bool TryEncode(Span<char> destination, out int charsWritten) { throw null; }
public static bool TryGetRuneAt(string input, int index, out Rune value) { throw null; }
public static double GetNumericValue(Rune value) { throw null; }
public static System.Globalization.UnicodeCategory GetUnicodeCategory(Rune value) { throw null; }
public static bool IsControl(Rune value) { throw null; }
Expand Down
47 changes: 47 additions & 0 deletions src/System.Runtime/tests/System/StringTests.netcoreapp.cs
Expand Up @@ -6,6 +6,7 @@
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Xunit;

namespace System.Tests
Expand Down Expand Up @@ -912,5 +913,51 @@ public static void IndexOf_Invalid_Char()
AssertExtensions.Throws<ArgumentException>("comparisonType", () => "foo".IndexOf('o', StringComparison.CurrentCulture - 1));
AssertExtensions.Throws<ArgumentException>("comparisonType", () => "foo".IndexOf('o', StringComparison.OrdinalIgnoreCase + 1));
}

[Theory]
[InlineData("a", 0, (int)'a')]
[InlineData("ab", 1, (int)'b')]
[InlineData("x\U0001F46Ey", 3, (int)'y')]
[InlineData("x\U0001F46Ey", 1, 0x1F46E)] // U+1F46E POLICE OFFICER
public static void GetRuneAt_TryGetRuneAt_Success(string inputString, int index, int expectedScalarValue)
{
// GetRuneAt
Assert.Equal(expectedScalarValue, inputString.GetRuneAt(index).Value);

// TryGetRuneAt
Assert.True(inputString.TryGetRuneAt(index, out Rune rune));
Assert.Equal(expectedScalarValue, rune.Value);
}

// Our unit test runner doesn't deal well with malformed literal strings, so
// we smuggle it as a char[] and turn it into a string within the test itself.
[Theory]
[InlineData(new char[] { 'x', '\uD83D', '\uDC6E', 'y' }, 2)] // attempt to index into the middle of a UTF-16 surrogate pair
[InlineData(new char[] { 'x', '\uD800', 'y' }, 1)] // high surrogate not followed by low surrogate
[InlineData(new char[] { 'x', '\uDFFF', '\uDFFF' }, 1)] // attempt to start at a low surrogate
[InlineData(new char[] { 'x', '\uD800' }, 1)] // end of string reached before could complete surrogate pair
public static void GetRuneAt_TryGetRuneAt_InvalidData(char[] inputCharArray, int index)
{
string inputString = new string(inputCharArray);

// GetRuneAt
Assert.Throws<ArgumentException>("index", () => inputString.GetRuneAt(index));

// TryGetRuneAt
Assert.False(inputString.TryGetRuneAt(index, out Rune rune));
Assert.Equal(0, rune.Value);
}

[Theory]
[InlineData("hello", -1)]
[InlineData("", 0)]
public static void GetRuneAt_TryGetRuneAt_BadArgs(string input, int index)
{
// negative index specified
Assert.Throws<ArgumentOutOfRangeException>("index", () => input.GetRuneAt(index));

// index goes past end of string
Assert.Throws<ArgumentOutOfRangeException>("index", () => input.TryGetRuneAt(index, out _));
}
}
}
47 changes: 0 additions & 47 deletions src/System.Runtime/tests/System/Text/RuneTests.netcoreapp.cs
Expand Up @@ -176,53 +176,6 @@ public static void GetHashCodeTests(int scalarValue)
Assert.Equal(scalarValue, new Rune(scalarValue).GetHashCode());
}

[Theory]
[InlineData("a", 0, (int)'a')]
[InlineData("ab", 1, (int)'b')]
[InlineData("x\U0001F46Ey", 3, (int)'y')]
[InlineData("x\U0001F46Ey", 1, 0x1F46E)] // U+1F46E POLICE OFFICER
public static void GetRuneAt_TryGetRuneAt_Utf16_Success(string inputString, int index, int expectedScalarValue)
{
// GetRuneAt
Assert.Equal(expectedScalarValue, Rune.GetRuneAt(inputString, index).Value);

// TryGetRuneAt
Assert.True(Rune.TryGetRuneAt(inputString, index, out Rune rune));
Assert.Equal(expectedScalarValue, rune.Value);
}

// Our unit test runner doesn't deal well with malformed literal strings, so
// we smuggle it as a char[] and turn it into a string within the test itself.
[Theory]
[InlineData(new char[] { 'x', '\uD83D', '\uDC6E', 'y' }, 2)] // attempt to index into the middle of a UTF-16 surrogate pair
[InlineData(new char[] { 'x', '\uD800', 'y' }, 1)] // high surrogate not followed by low surrogate
[InlineData(new char[] { 'x', '\uDFFF', '\uDFFF' }, 1)] // attempt to start at a low surrogate
[InlineData(new char[] { 'x', '\uD800' }, 1)] // end of string reached before could complete surrogate pair
public static void GetRuneAt_TryGetRuneAt_Utf16_InvalidData(char[] inputCharArray, int index)
{
string inputString = new string(inputCharArray);

// GetRuneAt
Assert.Throws<ArgumentException>("index", () => Rune.GetRuneAt(inputString, index));

// TryGetRuneAt
Assert.False(Rune.TryGetRuneAt(inputString, index, out Rune rune));
Assert.Equal(0, rune.Value);
}

[Fact]
public static void GetRuneAt_TryGetRuneAt_Utf16_BadArgs()
{
// null input
Assert.Throws<ArgumentNullException>("input", () => Rune.GetRuneAt(null, 0));

// negative index specified
Assert.Throws<ArgumentOutOfRangeException>("index", () => Rune.GetRuneAt("hello", -1));

// index goes past end of string
Assert.Throws<ArgumentOutOfRangeException>("index", () => Rune.GetRuneAt(string.Empty, 0));
}

[Theory]
[MemberData(nameof(UnicodeInfoTestData_Latin1AndSelectOthers))]
public static void GetNumericValue(UnicodeInfoTestData testData)
Expand Down

0 comments on commit 904718e

Please sign in to comment.