Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix runtime-extra-platforms failures related to HybridGlobalization #87913

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> string1, ReadOnlySpan<char> string2, CompareOptions options)
{
Debug.Assert(!GlobalizationMode.Invariant);
Expand All @@ -27,7 +34,7 @@ private unsafe int CompareStringNative(ReadOnlySpan<char> 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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
20 changes: 15 additions & 5 deletions src/native/libs/System.Globalization.Native/pal_collation.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be ERROR_COMPARISON_OPTIONS_NOT_FOUND as previously the value was -2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, that was actually the fix in the first place.

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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand Down