From d7521f4139294516d8fe651d543b9000515d3162 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan Date: Thu, 22 Jun 2023 10:47:35 +0200 Subject: [PATCH 1/2] return -2 only when options are not found --- src/native/libs/System.Globalization.Native/pal_collation.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/native/libs/System.Globalization.Native/pal_collation.m b/src/native/libs/System.Globalization.Native/pal_collation.m index e6410f7a9de210..025f2b8bf7a9fe 100644 --- a/src/native/libs/System.Globalization.Native/pal_collation.m +++ b/src/native/libs/System.Globalization.Native/pal_collation.m @@ -114,12 +114,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 = {-1, 0}; NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions); // in case mapping is not found if (options == 0) + { + result.location = -2; return result; + } NSString *searchString = [NSString stringWithCharacters: lpTarget length: cwTargetLength]; NSString *searchStrCleaned = RemoveWeightlessCharacters(searchString); From 34109cd5580d7b35981f44d393119ed2850c65a5 Mon Sep 17 00:00:00 2001 From: Meri Khamoyan Date: Thu, 22 Jun 2023 14:07:39 +0200 Subject: [PATCH 2/2] Add ErrorCodes enum --- .../System/Globalization/CompareInfo.OSX.cs | 17 ++++++++++++----- .../pal_collation.m | 19 +++++++++++++------ 2 files changed, 25 insertions(+), 11 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 6d72fbcc015305..45ffb5ad31ec04 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 @@ private unsafe int CompareStringNative(ReadOnlySpan string1, ReadOnlySpan< 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 @@ private unsafe int IndexOfCoreNative(char* target, int cwTargetLength, char* pSo 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 @@ private unsafe bool NativeStartsWith(char* pPrefix, int cwPrefixLength, char* pS 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 @@ private unsafe bool NativeEndsWith(char* pSuffix, int cwSuffixLength, char* pSou 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 025f2b8bf7a9fe..4c755ab475685d 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 @@ 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,13 +121,13 @@ 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 = {-1, 0}; + Range result = {ERROR_INDEX_NOT_FOUND, 0}; NSStringCompareOptions options = ConvertFromCompareOptionsToNSStringCompareOptions(comparisonOptions); // in case mapping is not found if (options == 0) { - result.location = -2; + result.location = ERROR_COMPARISON_OPTIONS_NOT_FOUND; return result; } @@ -155,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 @@ -228,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]; @@ -255,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];