From fad3d54acc0ab48231690a54975db4a012c5cb5f Mon Sep 17 00:00:00 2001 From: Meri Khamoyan <96171496+mkhamoyan@users.noreply.github.com> Date: Thu, 22 Jun 2023 22:25:25 +0400 Subject: [PATCH] Fix runtime-extra-platforms failures related to HybridGlobalization (#87913) Add ErrorCodes and return -2 only when options are not found --- .../src/System/Globalization/CompareInfo.OSX.cs | 17 ++++++++++++----- .../libs/System.Globalization.Native/pal_collation.m | 20 +++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.OSX.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.OSX.cs index 6d72fbc..45ffb5a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.OSX.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.OSX.cs @@ -11,6 +11,13 @@ namespace System.Globalization { public partial class CompareInfo { + private enum ErrorCodes + { + ERROR_INDEX_NOT_FOUND = -1, + ERROR_COMPARISON_OPTIONS_NOT_FOUND = -2, + ERROR_MIXED_COMPOSITION_NOT_FOUND = -3, + } + private unsafe int CompareStringNative(ReadOnlySpan string1, ReadOnlySpan string2, CompareOptions options) { Debug.Assert(!GlobalizationMode.Invariant); @@ -27,7 +34,7 @@ namespace System.Globalization result = Interop.Globalization.CompareStringNative(m_name, m_name.Length, pString1, string1.Length, pString2, string2.Length, options); } - Debug.Assert(result != -2); + Debug.Assert(result != (int)ErrorCodes.ERROR_COMPARISON_OPTIONS_NOT_FOUND); return result; } @@ -37,8 +44,8 @@ namespace System.Globalization AssertComparisonSupported(options); Interop.Range result = Interop.Globalization.IndexOfNative(m_name, m_name.Length, target, cwTargetLength, pSource, cwSourceLength, options, fromBeginning); - Debug.Assert(result.Location != -2); - if (result.Location == -3) + Debug.Assert(result.Location != (int)ErrorCodes.ERROR_COMPARISON_OPTIONS_NOT_FOUND); + if (result.Location == (int)ErrorCodes.ERROR_MIXED_COMPOSITION_NOT_FOUND) throw new PlatformNotSupportedException(SR.PlatformNotSupported_HybridGlobalizationWithMixedCompositions); if (matchLengthPtr != null) *matchLengthPtr = result.Length; @@ -51,7 +58,7 @@ namespace System.Globalization AssertComparisonSupported(options); int result = Interop.Globalization.StartsWithNative(m_name, m_name.Length, pPrefix, cwPrefixLength, pSource, cwSourceLength, options); - Debug.Assert(result != -2); + Debug.Assert(result != (int)ErrorCodes.ERROR_COMPARISON_OPTIONS_NOT_FOUND); return result > 0 ? true : false; } @@ -61,7 +68,7 @@ namespace System.Globalization AssertComparisonSupported(options); int result = Interop.Globalization.EndsWithNative(m_name, m_name.Length, pSuffix, cwSuffixLength, pSource, cwSourceLength, options); - Debug.Assert(result != -2); + Debug.Assert(result != (int)ErrorCodes.ERROR_COMPARISON_OPTIONS_NOT_FOUND); return result > 0 ? true : false; } diff --git a/src/native/libs/System.Globalization.Native/pal_collation.m b/src/native/libs/System.Globalization.Native/pal_collation.m index e6410f7..4c755ab 100644 --- a/src/native/libs/System.Globalization.Native/pal_collation.m +++ b/src/native/libs/System.Globalization.Native/pal_collation.m @@ -19,6 +19,13 @@ typedef enum StringSort = 536870912, } CompareOptions; +typedef enum +{ + ERROR_INDEX_NOT_FOUND = -1, + ERROR_COMPARISON_OPTIONS_NOT_FOUND = -2, + ERROR_MIXED_COMPOSITION_NOT_FOUND = -3, +} ErrorCodes; + static NSLocale* GetCurrentLocale(const uint16_t* localeName, int32_t lNameLength) { NSLocale *currentLocale; @@ -74,7 +81,7 @@ int32_t GlobalizationNative_CompareStringNative(const uint16_t* localeName, int3 // in case mapping is not found if (options == 0) - return -2; + return ERROR_COMPARISON_OPTIONS_NOT_FOUND; return [sourceStrPrecomposed compare:targetStrPrecomposed options:options @@ -114,12 +121,15 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam const uint16_t* lpSource, int32_t cwSourceLength, int32_t comparisonOptions, int32_t fromBeginning) { assert(cwTargetLength >= 0); - Range result = {-2, 0}; + Range result = {ERROR_INDEX_NOT_FOUND, 0}; NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions); // in case mapping is not found if (options == 0) + { + result.location = ERROR_COMPARISON_OPTIONS_NOT_FOUND; return result; + } NSString *searchString = [NSString stringWithCharacters: lpTarget length: cwTargetLength]; NSString *searchStrCleaned = RemoveWeightlessCharacters(searchString); @@ -152,7 +162,7 @@ Range GlobalizationNative_IndexOfNative(const uint16_t* localeName, int32_t lNam return result; // in case search string is inside source string but we can't find the index return -3 - result.location = -3; + result.location = ERROR_MIXED_COMPOSITION_NOT_FOUND; // sourceString and searchString possibly have the same composition of characters rangeOfReceiverToSearch = NSMakeRange(0, sourceStrCleaned.length); NSRange nsRange = [sourceStrCleaned rangeOfString:searchStrCleaned @@ -225,7 +235,7 @@ int32_t GlobalizationNative_StartsWithNative(const uint16_t* localeName, int32_t // in case mapping is not found if (options == 0) - return -2; + return ERROR_COMPARISON_OPTIONS_NOT_FOUND; NSLocale *currentLocale = GetCurrentLocale(localeName, lNameLength); NSString *prefixString = [NSString stringWithCharacters: lpPrefix length: cwPrefixLength]; @@ -252,7 +262,7 @@ int32_t GlobalizationNative_EndsWithNative(const uint16_t* localeName, int32_t l // in case mapping is not found if (options == 0) - return -2; + return ERROR_COMPARISON_OPTIONS_NOT_FOUND; NSLocale *currentLocale = GetCurrentLocale(localeName, lNameLength); NSString *suffixString = [NSString stringWithCharacters: lpSuffix length: cwSuffixLength]; -- 2.7.4