Skip to content

Commit

Permalink
ICU-22641 Change const refs in the class to const pointers
Browse files Browse the repository at this point in the history
The function may return nullptr, we cannot just deref the return value.
Change them to const pointer instead.
  • Loading branch information
FrankYFTang committed Jan 23, 2024
1 parent e81b872 commit f7f9dbb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
19 changes: 10 additions & 9 deletions icu4c/source/common/caniter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ U_NAMESPACE_BEGIN

UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CanonicalIterator)


/**
*@param source string to get results for
*/
Expand All @@ -73,10 +74,10 @@ CanonicalIterator::CanonicalIterator(const UnicodeString &sourceStr, UErrorCode
pieces_lengths(nullptr),
current(nullptr),
current_length(0),
nfd(*Normalizer2::getNFDInstance(status)),
nfcImpl(*Normalizer2Factory::getNFCImpl(status))
nfd(Normalizer2::getNFDInstance(status)),
nfcImpl(Normalizer2Factory::getNFCImpl(status))
{
if(U_SUCCESS(status) && nfcImpl.ensureCanonIterData(status)) {
if(U_SUCCESS(status) && nfcImpl->ensureCanonIterData(status)) {
setSource(sourceStr, status);
}
}
Expand Down Expand Up @@ -172,7 +173,7 @@ void CanonicalIterator::setSource(const UnicodeString &newSource, UErrorCode &st
int32_t i = 0;
UnicodeString *list = nullptr;

nfd.normalize(newSource, source, status);
nfd->normalize(newSource, source, status);
if(U_FAILURE(status)) {
return;
}
Expand Down Expand Up @@ -219,7 +220,7 @@ void CanonicalIterator::setSource(const UnicodeString &newSource, UErrorCode &st
// on the NFD form - see above).
for (; i < source.length(); i += U16_LENGTH(cp)) {
cp = source.char32At(i);
if (nfcImpl.isCanonSegmentStarter(cp)) {
if (nfcImpl->isCanonSegmentStarter(cp)) {
source.extract(start, i-start, list[list_length++]); // add up to i
start = i;
}
Expand Down Expand Up @@ -390,7 +391,7 @@ UnicodeString* CanonicalIterator::getEquivalents(const UnicodeString &segment, i
//UnicodeString *possible = new UnicodeString(*((UnicodeString *)(ne2->value.pointer)));
UnicodeString possible(*((UnicodeString *)(ne2->value.pointer)));
UnicodeString attempt;
nfd.normalize(possible, attempt, status);
nfd->normalize(possible, attempt, status);

// TODO: check if operator == is semanticaly the same as attempt.equals(segment)
if (attempt==segment) {
Expand Down Expand Up @@ -457,7 +458,7 @@ Hashtable *CanonicalIterator::getEquivalents2(Hashtable *fillinResult, const cha
for (int32_t i = 0; i < segLen; i += U16_LENGTH(cp)) {
// see if any character is at the start of some decomposition
U16_GET(segment, 0, i, segLen, cp);
if (!nfcImpl.getCanonStartSet(cp, starts)) {
if (!nfcImpl->getCanonStartSet(cp, starts)) {
continue;
}
// if so, see which decompositions match
Expand Down Expand Up @@ -518,7 +519,7 @@ Hashtable *CanonicalIterator::extract(Hashtable *fillinResult, UChar32 comp, con
UnicodeString temp(comp);
int32_t inputLen=temp.length();
UnicodeString decompString;
nfd.normalize(temp, decompString, status);
nfd->normalize(temp, decompString, status);
if (U_FAILURE(status)) {
return nullptr;
}
Expand Down Expand Up @@ -582,7 +583,7 @@ Hashtable *CanonicalIterator::extract(Hashtable *fillinResult, UChar32 comp, con
// brute force approach
// check to make sure result is canonically equivalent
UnicodeString trial;
nfd.normalize(temp, trial, status);
nfd->normalize(temp, trial, status);
if(U_FAILURE(status) || trial.compare(segment+segmentPos, segLen - segmentPos) != 0) {
return nullptr;
}
Expand Down
4 changes: 2 additions & 2 deletions icu4c/source/common/unicode/caniter.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ class U_COMMON_API CanonicalIterator final : public UObject {
// transient fields
UnicodeString buffer;

const Normalizer2 &nfd;
const Normalizer2Impl &nfcImpl;
const Normalizer2 *nfd;
const Normalizer2Impl *nfcImpl;

// we have a segment, in NFD. Find all the strings that are canonically equivalent to it.
UnicodeString *getEquivalents(const UnicodeString &segment, int32_t &result_len, UErrorCode &status); //private String[] getEquivalents(String segment)
Expand Down
8 changes: 8 additions & 0 deletions icu4c/source/test/intltest/apicoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,13 @@ void CollationAPITest::TestGapTooSmall() {
}
}

void CollationAPITest::TestNFCNull() {
IcuTestErrorCode errorCode(*this, "TestNFCNull");
RuleBasedCollator coll(
u"&†<†\u0f81\u0f81\u0f81\u0f81\u0f81|†", errorCode);
errorCode.expectErrorAndReset(U_UNSUPPORTED_ERROR);
}

void CollationAPITest::dump(UnicodeString msg, RuleBasedCollator* c, UErrorCode& status) {
const char* bigone = "One";
const char* littleone = "one";
Expand Down Expand Up @@ -2560,6 +2567,7 @@ void CollationAPITest::runIndexedTest( int32_t index, UBool exec, const char* &n
TESTCASE_AUTO(TestIterNumeric);
TESTCASE_AUTO(TestBadKeywords);
TESTCASE_AUTO(TestGapTooSmall);
TESTCASE_AUTO(TestNFCNull);
TESTCASE_AUTO_END;
}

Expand Down
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/apicoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class CollationAPITest: public IntlTestCollator {
void TestIterNumeric();
void TestBadKeywords();
void TestGapTooSmall();
void TestNFCNull();

private:
// If this is too small for the test data, just increase it.
Expand Down

0 comments on commit f7f9dbb

Please sign in to comment.