Skip to content

Commit ad6c18f

Browse files
authored
[iOS] [HybridGlobalization] Throw PNSE when methods with matchLength are used (#107390)
* Throw PlatformNotSupportedException for matchLength
1 parent a709d73 commit ad6c18f

File tree

5 files changed

+51
-39
lines changed

5 files changed

+51
-39
lines changed

src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ private unsafe int IndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value,
10571057
{
10581058
Debug.Assert(matchLengthPtr != null);
10591059
*matchLengthPtr = 0;
1060-
#if TARGET_BROWSER
1060+
#if TARGET_BROWSER || TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
10611061
if (GlobalizationMode.Hybrid)
10621062
{
10631063
throw new PlatformNotSupportedException(SR.PlatformNotSupported_HybridGlobalizationWithMatchLength);

src/libraries/System.Runtime/tests/System.Globalization.Tests/CompareInfo/CompareInfoTests.IndexOf.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static IEnumerable<object[]> IndexOf_TestData()
138138
}
139139

140140
// Inputs where matched length does not equal value string length
141-
if (!PlatformDetection.IsHybridGlobalizationOnBrowser)
141+
if (!PlatformDetection.IsHybridGlobalizationOnBrowser && PlatformDetection.IsNotHybridGlobalizationOnApplePlatform)
142142
{
143143
yield return new object[] { s_germanCompare, "abc Strasse Strasse xyz", "stra\u00DFe", 0, 23, supportedIgnoreCaseIgnoreNonSpaceOptions, 4, 7 };
144144
yield return new object[] { s_germanCompare, "abc stra\u00DFe stra\u00DFe xyz", "Strasse", 0, 21, supportedIgnoreCaseIgnoreNonSpaceOptions, 4, 6 };
@@ -246,7 +246,7 @@ static void RunSpanIndexOfTest(CompareInfo compareInfo, ReadOnlySpan<char> sourc
246246
valueBoundedMemory.MakeReadonly();
247247

248248
Assert.Equal(expected, compareInfo.IndexOf(sourceBoundedMemory.Span, valueBoundedMemory.Span, options));
249-
if (!PlatformDetection.IsHybridGlobalizationOnBrowser)
249+
if (!PlatformDetection.IsHybridGlobalizationOnBrowser && PlatformDetection.IsNotHybridGlobalizationOnApplePlatform)
250250
{
251251
Assert.Equal(expected, compareInfo.IndexOf(sourceBoundedMemory.Span, valueBoundedMemory.Span, options, out int actualMatchLength));
252252
Assert.Equal(expectedMatchLength, actualMatchLength);
@@ -334,7 +334,7 @@ public void IndexOf_Invalid()
334334
AssertExtensions.Throws<ArgumentException>("options", () => s_invariantCompare.IndexOf("Test's", 'b', 0, CompareOptions.StringSort));
335335
AssertExtensions.Throws<ArgumentException>("options", () => s_invariantCompare.IndexOf("Test's", 'c', 0, 2, CompareOptions.StringSort));
336336
AssertExtensions.Throws<ArgumentException>("options", () => s_invariantCompare.IndexOf("Test's".AsSpan(), "b".AsSpan(), CompareOptions.StringSort));
337-
if (PlatformDetection.IsHybridGlobalizationOnBrowser)
337+
if (PlatformDetection.IsHybridGlobalizationOnBrowser || PlatformDetection.IsHybridGlobalizationOnApplePlatform)
338338
{
339339
Assert.Throws<PlatformNotSupportedException>(() => s_invariantCompare.IndexOf("Test's".AsSpan(), "b".AsSpan(), CompareOptions.StringSort, out _));
340340
Assert.Throws<PlatformNotSupportedException>(() => s_invariantCompare.IndexOf("Test's".AsSpan(), "b".AsSpan(), CompareOptions.Ordinal | CompareOptions.IgnoreWidth, out _));

src/libraries/System.Runtime/tests/System.Globalization.Tests/CompareInfo/CompareInfoTests.LastIndexOf.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static void RunSpanLastIndexOfTest(CompareInfo compareInfo, ReadOnlySpan<char> s
253253
valueBoundedMemory.MakeReadonly();
254254

255255
Assert.Equal(expected, compareInfo.LastIndexOf(sourceBoundedMemory.Span, valueBoundedMemory.Span, options));
256-
if (!PlatformDetection.IsHybridGlobalizationOnBrowser)
256+
if (!PlatformDetection.IsHybridGlobalizationOnBrowser && PlatformDetection.IsNotHybridGlobalizationOnApplePlatform)
257257
{
258258
Assert.Equal(expected, compareInfo.LastIndexOf(sourceBoundedMemory.Span, valueBoundedMemory.Span, options, out int actualMatchLength));
259259
Assert.Equal(expectedMatchLength, actualMatchLength);
@@ -348,7 +348,7 @@ public void LastIndexOf_Invalid()
348348
AssertExtensions.Throws<ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, CompareOptions.StringSort));
349349
AssertExtensions.Throws<ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, 1, CompareOptions.StringSort));
350350
AssertExtensions.Throws<ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "a".AsSpan(), CompareOptions.StringSort));
351-
if (PlatformDetection.IsHybridGlobalizationOnBrowser)
351+
if (PlatformDetection.IsHybridGlobalizationOnBrowser || PlatformDetection.IsHybridGlobalizationOnApplePlatform)
352352
{
353353
Assert.Throws<PlatformNotSupportedException>(() => s_invariantCompare.LastIndexOf("Test's", "a".AsSpan(), CompareOptions.StringSort, out int matchLength));
354354
Assert.Throws<PlatformNotSupportedException>(() => s_invariantCompare.LastIndexOf("Test's", "a".AsSpan(), CompareOptions.Ordinal | CompareOptions.IgnoreWidth, out int matchLength));

src/libraries/System.Runtime/tests/System.Globalization.Tests/Invariant/InvariantMode.cs

+40-28
Original file line numberDiff line numberDiff line change
@@ -898,14 +898,17 @@ static void TestCore(CompareInfo compareInfo, string source, string value, int s
898898

899899
Assert.Equal(offsetResult, sourceBoundedSpan.IndexOf(valueBoundedSpan, GetStringComparison(options)));
900900
Assert.Equal(offsetResult, compareInfo.IndexOf(sourceBoundedSpan, valueBoundedSpan, options));
901-
Assert.Equal(offsetResult, compareInfo.IndexOf(sourceBoundedSpan, valueBoundedSpan, options, out int matchLength));
902-
if (offsetResult >= 0)
903-
{
904-
Assert.Equal(valueBoundedSpan.Length, matchLength); // Invariant mode should perform non-linguistic comparisons
905-
}
906-
else
901+
if (PlatformDetection.IsNotHybridGlobalizationOnApplePlatform)
907902
{
908-
Assert.Equal(0, matchLength); // not found
903+
Assert.Equal(offsetResult, compareInfo.IndexOf(sourceBoundedSpan, valueBoundedSpan, options, out int matchLength));
904+
if (offsetResult >= 0)
905+
{
906+
Assert.Equal(valueBoundedSpan.Length, matchLength); // Invariant mode should perform non-linguistic comparisons
907+
}
908+
else
909+
{
910+
Assert.Equal(0, matchLength); // not found
911+
}
909912
}
910913
}
911914
}
@@ -958,14 +961,17 @@ static void TestCore(CompareInfo compareInfo, string source, string value, int s
958961

959962
Assert.Equal(result, sourceBoundedSpan.LastIndexOf(valueBoundedSpan, GetStringComparison(options)));
960963
Assert.Equal(result, compareInfo.LastIndexOf(sourceBoundedSpan, valueBoundedSpan, options));
961-
Assert.Equal(result, compareInfo.LastIndexOf(sourceBoundedSpan, valueBoundedSpan, options, out int matchLength));
962-
if (result >= 0)
963-
{
964-
Assert.Equal(valueBoundedSpan.Length, matchLength); // Invariant mode should perform non-linguistic comparisons
965-
}
966-
else
964+
if (PlatformDetection.IsNotHybridGlobalizationOnApplePlatform)
967965
{
968-
Assert.Equal(0, matchLength); // not found
966+
Assert.Equal(result, compareInfo.LastIndexOf(sourceBoundedSpan, valueBoundedSpan, options, out int matchLength));
967+
if (result >= 0)
968+
{
969+
Assert.Equal(valueBoundedSpan.Length, matchLength); // Invariant mode should perform non-linguistic comparisons
970+
}
971+
else
972+
{
973+
Assert.Equal(0, matchLength); // not found
974+
}
969975
}
970976
}
971977
}
@@ -993,14 +999,17 @@ public void TestIsPrefix(string source, string value, CompareOptions options, bo
993999

9941000
Assert.Equal(result, sourceBoundedSpan.StartsWith(valueBoundedSpan, GetStringComparison(options)));
9951001
Assert.Equal(result, compareInfo.IsPrefix(sourceBoundedSpan, valueBoundedSpan, options));
996-
Assert.Equal(result, compareInfo.IsPrefix(sourceBoundedSpan, valueBoundedSpan, options, out int matchLength));
997-
if (result)
998-
{
999-
Assert.Equal(valueBoundedSpan.Length, matchLength); // Invariant mode should perform non-linguistic comparisons
1000-
}
1001-
else
1002+
if (PlatformDetection.IsNotHybridGlobalizationOnApplePlatform)
10021003
{
1003-
Assert.Equal(0, matchLength); // not found
1004+
Assert.Equal(result, compareInfo.IsPrefix(sourceBoundedSpan, valueBoundedSpan, options, out int matchLength));
1005+
if (result)
1006+
{
1007+
Assert.Equal(valueBoundedSpan.Length, matchLength); // Invariant mode should perform non-linguistic comparisons
1008+
}
1009+
else
1010+
{
1011+
Assert.Equal(0, matchLength); // not found
1012+
}
10041013
}
10051014
}
10061015
}
@@ -1028,14 +1037,17 @@ public void TestIsSuffix(string source, string value, CompareOptions options, bo
10281037

10291038
Assert.Equal(result, sourceBoundedSpan.EndsWith(valueBoundedSpan, GetStringComparison(options)));
10301039
Assert.Equal(result, compareInfo.IsSuffix(sourceBoundedSpan, valueBoundedSpan, options));
1031-
Assert.Equal(result, compareInfo.IsSuffix(sourceBoundedSpan, valueBoundedSpan, options, out int matchLength));
1032-
if (result)
1033-
{
1034-
Assert.Equal(valueBoundedSpan.Length, matchLength); // Invariant mode should perform non-linguistic comparisons
1035-
}
1036-
else
1040+
if (PlatformDetection.IsNotHybridGlobalizationOnApplePlatform)
10371041
{
1038-
Assert.Equal(0, matchLength); // not found
1042+
Assert.Equal(result, compareInfo.IsSuffix(sourceBoundedSpan, valueBoundedSpan, options, out int matchLength));
1043+
if (result)
1044+
{
1045+
Assert.Equal(valueBoundedSpan.Length, matchLength); // Invariant mode should perform non-linguistic comparisons
1046+
}
1047+
else
1048+
{
1049+
Assert.Equal(0, matchLength); // not found
1050+
}
10391051
}
10401052
}
10411053
}

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/StringTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -804,14 +804,14 @@ public static IEnumerable<object[]> Replace_StringComparison_TestData()
804804
yield return new object[] { "", "\0", "y", StringComparison.InvariantCulture, "" };
805805
}
806806

807-
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotHybridGlobalizationOnBrowser))]
807+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotHybridGlobalizationOnBrowser), nameof(PlatformDetection.IsNotHybridGlobalizationOnApplePlatform))]
808808
[MemberData(nameof(Replace_StringComparison_TestData))]
809809
public void Replace_StringComparison_ReturnsExpected(string original, string oldValue, string newValue, StringComparison comparisonType, string expected)
810810
{
811811
Assert.Equal(expected, original.Replace(oldValue, newValue, comparisonType));
812812
}
813813

814-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInvariantGlobalization))]
814+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInvariantGlobalization), nameof(PlatformDetection.IsNotHybridGlobalizationOnApplePlatform))]
815815
[ActiveIssue("https://github.com/dotnet/runtime/issues/60568", TestPlatforms.Android | TestPlatforms.LinuxBionic)]
816816
[ActiveIssue("https://github.com/dotnet/runtime/issues/95503", typeof(PlatformDetection), nameof(PlatformDetection.IsHybridGlobalizationOnBrowser))]
817817
public void Replace_StringComparison_TurkishI()
@@ -871,7 +871,7 @@ public static IEnumerable<object[]> Replace_StringComparisonCulture_TestData()
871871
}
872872
}
873873

874-
[Theory]
874+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotHybridGlobalizationOnApplePlatform))]
875875
[MemberData(nameof(Replace_StringComparisonCulture_TestData))]
876876
[ActiveIssue("https://github.com/dotnet/runtime/issues/95503", typeof(PlatformDetection), nameof(PlatformDetection.IsHybridGlobalizationOnBrowser))]
877877
public void Replace_StringComparisonCulture_ReturnsExpected(string original, string oldValue, string newValue, bool ignoreCase, CultureInfo culture, string expected)
@@ -897,15 +897,15 @@ public void Replace_StringComparison_EmptyOldValue_ThrowsArgumentException()
897897
AssertExtensions.Throws<ArgumentException>("oldValue", () => "abc".Replace("", "def", true, CultureInfo.CurrentCulture));
898898
}
899899

900-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInvariantGlobalization), nameof(PlatformDetection.IsNotHybridGlobalizationOnBrowser))]
900+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInvariantGlobalization), nameof(PlatformDetection.IsNotHybridGlobalizationOnBrowser), nameof(PlatformDetection.IsNotHybridGlobalizationOnApplePlatform))]
901901
public void Replace_StringComparison_WeightlessOldValue_WithOrdinalComparison_Succeeds()
902902
{
903903
Assert.Equal("abcdef", ("abc" + ZeroWidthJoiner).Replace(ZeroWidthJoiner, "def"));
904904
Assert.Equal("abcdef", ("abc" + ZeroWidthJoiner).Replace(ZeroWidthJoiner, "def", StringComparison.Ordinal));
905905
Assert.Equal("abcdef", ("abc" + ZeroWidthJoiner).Replace(ZeroWidthJoiner, "def", StringComparison.OrdinalIgnoreCase));
906906
}
907907

908-
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInvariantGlobalization), nameof(PlatformDetection.IsNotHybridGlobalizationOnBrowser))]
908+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotInvariantGlobalization), nameof(PlatformDetection.IsNotHybridGlobalizationOnBrowser), nameof(PlatformDetection.IsNotHybridGlobalizationOnApplePlatform))]
909909
public void Replace_StringComparison_WeightlessOldValue_WithLinguisticComparison_TerminatesReplacement()
910910
{
911911
Assert.Equal("abc" + ZeroWidthJoiner + "def", ("abc" + ZeroWidthJoiner + "def").Replace(ZeroWidthJoiner, "xyz", StringComparison.CurrentCulture));

0 commit comments

Comments
 (0)