Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix the string search behavior when using ICU #57078

Merged
merged 8 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -417,7 +417,7 @@ static int CanIgnoreAllCollationElements(const UCollator* pColl, const UChar* lp

static void CreateSortHandle(SortHandle** ppSortHandle)
{
*ppSortHandle = (SortHandle*)malloc(sizeof(SortHandle));
*ppSortHandle = (SortHandle*)calloc(1, sizeof(SortHandle));
if ((*ppSortHandle) == NULL)
{
return;
Expand Down Expand Up @@ -513,7 +513,6 @@ static const char* BreakIteratorRuleNew = // supported on newer ICU versions li
".;";

static UChar* s_breakIteratorRules = NULL;
static int32_t s_breakIteratorRulesLength = 0;

// When doing string search operations using ICU, it is internally using a break iterator which doesn't allow breaking between some characters according to
// the Grapheme Cluster Boundary Rules specified in http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundary_Rules.
Expand All @@ -530,8 +529,7 @@ static UBreakIterator* CreateCustomizedBreakIterator()
UErrorCode status = U_ZERO_ERROR;
if (s_breakIteratorRules != NULL)
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
{
assert(s_breakIteratorRulesLength > 0);
breaker = ubrk_openRules(s_breakIteratorRules, s_breakIteratorRulesLength, emptyString, 0, NULL, &status);
breaker = ubrk_openRules(s_breakIteratorRules, -1, emptyString, 0, NULL, &status);
return U_FAILURE(status) ? NULL : breaker;
}

Expand All @@ -540,25 +538,22 @@ static UBreakIterator* CreateCustomizedBreakIterator()

int32_t breakIteratorRulesLength = newRulesLength > oldRulesLength ? newRulesLength : oldRulesLength;

UChar* rules = (UChar*)malloc((breakIteratorRulesLength + 1) * sizeof(UChar));
UChar* rules = (UChar*)calloc((breakIteratorRulesLength + 1), sizeof(UChar));
if (rules == NULL)
{
return NULL;
}

u_uastrcpy(rules, BreakIteratorRuleNew);
u_uastrncpy(rules, BreakIteratorRuleNew, newRulesLength);
rules[newRulesLength] = '\0';

breaker = ubrk_openRules(rules, newRulesLength, emptyString, 0, NULL, &status);
if (U_FAILURE(status))
{
status = U_ZERO_ERROR;
u_uastrcpy(rules, BreakIteratorRuleOld);
u_uastrncpy(rules, BreakIteratorRuleOld, oldRulesLength);
rules[oldRulesLength] = '\0';
breaker = ubrk_openRules(rules, oldRulesLength, emptyString, 0, NULL, &status);
s_breakIteratorRulesLength = oldRulesLength;
}
else
{
s_breakIteratorRulesLength = newRulesLength;
}

if (U_FAILURE(status))
Expand Down Expand Up @@ -667,7 +662,7 @@ static const UCollator* GetCollatorFromSortHandle(SortHandle* pSortHandle, int32
// CreateNewSearchNode will create a new node in the linked list and mark this node search handle as borrowed handle.
static inline int32_t CreateNewSearchNode(SortHandle* pSortHandle, int32_t options)
{
SearchIteratorNode* node = (SearchIteratorNode*) malloc(sizeof(SearchIteratorNode));
SearchIteratorNode* node = (SearchIteratorNode*)calloc(1, sizeof(SearchIteratorNode));
if (node == NULL)
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ U_CAPI int32_t U_EXPORT2 ucal_getWindowsTimeZoneID(const UChar* id, int32_t len,
PER_FUNCTION_BLOCK(u_strncpy, libicuuc, true) \
PER_FUNCTION_BLOCK(u_tolower, libicuuc, true) \
PER_FUNCTION_BLOCK(u_toupper, libicuuc, true) \
PER_FUNCTION_BLOCK(u_uastrcpy, libicuuc, true) \
PER_FUNCTION_BLOCK(u_uastrncpy, libicuuc, true) \
PER_FUNCTION_BLOCK(ubrk_close, libicui18n, true) \
PER_FUNCTION_BLOCK(ubrk_openRules, libicui18n, true) \
PER_FUNCTION_BLOCK(ucal_add, libicui18n, true) \
Expand Down Expand Up @@ -218,7 +218,7 @@ FOR_ALL_ICU_FUNCTIONS
#define u_strncpy(...) u_strncpy_ptr(__VA_ARGS__)
#define u_tolower(...) u_tolower_ptr(__VA_ARGS__)
#define u_toupper(...) u_toupper_ptr(__VA_ARGS__)
#define u_uastrcpy(...) u_uastrcpy_ptr(__VA_ARGS__)
#define u_uastrncpy(...) u_uastrncpy_ptr(__VA_ARGS__)
#define ubrk_close(...) ubrk_close_ptr(__VA_ARGS__)
#define ubrk_openRules(...) ubrk_openRules_ptr(__VA_ARGS__)
#define ucal_add(...) ucal_add_ptr(__VA_ARGS__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ UChar * u_strcpy(UChar * dst, const UChar * src);
UChar * u_strncpy(UChar * dst, const UChar * src, int32_t n);
UChar32 u_tolower(UChar32 c);
UChar32 u_toupper(UChar32 c);
UChar* u_uastrcpy(UChar * dst, const char * src);
UChar* u_uastrncpy(UChar * dst, const char * src, int32_t n);
void ubrk_close(UBreakIterator * bi);
UBreakIterator* ubrk_openRules(const UChar * rules, int32_t rulesLength, const UChar * text, int32_t textLength, UParseError * parseErr, UErrorCode * status);
void ucal_add(UCalendar * cal, UCalendarDateFields field, int32_t amount, UErrorCode * status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "pal_errors_internal.h"
#include "pal_locale_internal.h"
Expand Down Expand Up @@ -223,7 +224,7 @@ static void FixupTimeZoneGenericDisplayName(const char* locale, const UChar* tim
}

// Make a UChar[] version of the test time zone id for use in the API calls.
u_uastrcpy(testTimeZoneId, testId);
u_uastrncpy(testTimeZoneId, testId, (int32_t)strlen(testId));
tarekgh marked this conversation as resolved.
Show resolved Hide resolved

// Get the standard name from the test time zone.
GetTimeZoneDisplayName_FromCalendar(locale, testTimeZoneId, timestamp, UCAL_STANDARD, testDisplayName, DISPLAY_NAME_LENGTH, err);
Expand Down