From 3878e1780a3ac02e271a018e409691e8f68ad5f9 Mon Sep 17 00:00:00 2001 From: Syn Wee Quek Date: Fri, 9 Nov 2001 22:42:20 +0000 Subject: [PATCH] ICU-1004 jitterbug 1030: Cleaned code. Commented where error status is bing checked in. X-SVN-Rev: 6717 --- icu4c/source/i18n/search.cpp | 12 +++++++ icu4c/source/i18n/stsearch.cpp | 50 +++++++++++++++++++++++------- icu4c/source/i18n/unicode/search.h | 4 +++ icu4c/source/i18n/usearch.cpp | 8 ++++- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/icu4c/source/i18n/search.cpp b/icu4c/source/i18n/search.cpp index 45d7f9c2514..442559da487 100644 --- a/icu4c/source/i18n/search.cpp +++ b/icu4c/source/i18n/search.cpp @@ -161,6 +161,9 @@ UBool SearchIterator::operator==(const SearchIterator &that) const UTextOffset SearchIterator::first(UErrorCode &status) { + if (U_FAILURE(status)) { + return USEARCH_DONE; + } setOffset(0, status); return handleNext(0, status); } @@ -168,12 +171,18 @@ UTextOffset SearchIterator::first(UErrorCode &status) UTextOffset SearchIterator::following(UTextOffset position, UErrorCode &status) { + if (U_FAILURE(status)) { + return USEARCH_DONE; + } setOffset(position, status); return handleNext(position, status); } UTextOffset SearchIterator::last(UErrorCode &status) { + if (U_FAILURE(status)) { + return USEARCH_DONE; + } setOffset(m_search_->textLength, status); return handlePrev(m_search_->textLength, status); } @@ -181,6 +190,9 @@ UTextOffset SearchIterator::last(UErrorCode &status) UTextOffset SearchIterator::preceding(UTextOffset position, UErrorCode &status) { + if (U_FAILURE(status)) { + return USEARCH_DONE; + } setOffset(position, status); return handlePrev(position, status); } diff --git a/icu4c/source/i18n/stsearch.cpp b/icu4c/source/i18n/stsearch.cpp index c9991d91e91..b484b1a607a 100644 --- a/icu4c/source/i18n/stsearch.cpp +++ b/icu4c/source/i18n/stsearch.cpp @@ -24,6 +24,11 @@ StringSearch::StringSearch(const UnicodeString &pattern, m_collator_(), m_pattern_(pattern) { + if (U_FAILURE(status)) { + m_strsrch_ = NULL; + return; + } + m_strsrch_ = usearch_open(m_pattern_.fArray, m_pattern_.fLength, m_text_.fArray, m_text_.fLength, locale.getName(), NULL, &status); @@ -50,6 +55,10 @@ StringSearch::StringSearch(const UnicodeString &pattern, m_collator_(), m_pattern_(pattern) { + if (U_FAILURE(status)) { + m_strsrch_ = NULL; + return; + } if (coll == NULL) { status = U_ILLEGAL_ARGUMENT_ERROR; m_strsrch_ = NULL; @@ -82,6 +91,10 @@ StringSearch::StringSearch(const UnicodeString &pattern, m_collator_(), m_pattern_(pattern) { + if (U_FAILURE(status)) { + m_strsrch_ = NULL; + return; + } m_strsrch_ = usearch_open(m_pattern_.fArray, m_pattern_.fLength, m_text_.fArray, m_text_.fLength, locale.getName(), NULL, &status); @@ -108,6 +121,10 @@ StringSearch::StringSearch(const UnicodeString &pattern, m_collator_(), m_pattern_(pattern) { + if (U_FAILURE(status)) { + m_strsrch_ = NULL; + return; + } if (coll == NULL) { status = U_ILLEGAL_ARGUMENT_ERROR; m_strsrch_ = NULL; @@ -152,7 +169,7 @@ StringSearch::StringSearch(const StringSearch &that) : m_search_ = NULL; if (U_SUCCESS(status)) { - int32_t length; + int32_t length; const UChar *rules = ucol_getRules(m_strsrch_->collator, &length); m_collation_rules_.setTo(rules, length); m_collator_.setUCollator((UCollator *)m_strsrch_->collator, @@ -214,6 +231,7 @@ UBool StringSearch::operator==(const SearchIterator &that) const void StringSearch::setOffset(UTextOffset position, UErrorCode &status) { + // status checked in usearch_setOffset usearch_setOffset(m_strsrch_, position, &status); } @@ -224,14 +242,18 @@ UTextOffset StringSearch::getOffset(void) const void StringSearch::setText(const UnicodeString &text, UErrorCode &status) { - m_text_ = text; - usearch_setText(m_strsrch_, text.fArray, text.fLength, &status); + if (U_SUCCESS(status)) { + m_text_ = text; + usearch_setText(m_strsrch_, text.fArray, text.fLength, &status); + } } void StringSearch::setText(CharacterIterator &text, UErrorCode &status) { - text.getText(m_text_); - usearch_setText(m_strsrch_, m_text_.fArray, m_text_.fLength, &status); + if (U_SUCCESS(status)) { + text.getText(m_text_); + usearch_setText(m_strsrch_, m_text_.fArray, m_text_.fLength, &status); + } } RuleBasedCollator * StringSearch::getCollator() const @@ -241,18 +263,22 @@ RuleBasedCollator * StringSearch::getCollator() const void StringSearch::setCollator(RuleBasedCollator *coll, UErrorCode &status) { - usearch_setCollator(m_strsrch_, coll->getUCollator(), &status); - m_collation_rules_.setTo(coll->getRules()); - m_collator_.setUCollator((UCollator *)m_strsrch_->collator, - &m_collation_rules_); + if (U_SUCCESS(status)) { + usearch_setCollator(m_strsrch_, coll->getUCollator(), &status); + m_collation_rules_.setTo(coll->getRules()); + m_collator_.setUCollator((UCollator *)m_strsrch_->collator, + &m_collation_rules_); + } } void StringSearch::setPattern(const UnicodeString &pattern, UErrorCode &status) { - m_pattern_ = pattern; - usearch_setPattern(m_strsrch_, m_pattern_.fArray, m_pattern_.fLength, - &status); + if (U_SUCCESS(status)) { + m_pattern_ = pattern; + usearch_setPattern(m_strsrch_, m_pattern_.fArray, m_pattern_.fLength, + &status); + } } const UnicodeString & StringSearch::getPattern() const diff --git a/icu4c/source/i18n/unicode/search.h b/icu4c/source/i18n/unicode/search.h index 9ddfbeb375b..989f750d14a 100644 --- a/icu4c/source/i18n/unicode/search.h +++ b/icu4c/source/i18n/unicode/search.h @@ -445,6 +445,8 @@ protected: * @param position The index in the target text at which the search * should start. * @param status for error codes if it occurs. + * @return index at which the match starts, else if match is not found + * USEARCH_DONE is returned */ virtual UTextOffset handleNext(UTextOffset position, UErrorCode &status) = 0; @@ -463,6 +465,8 @@ protected: * @param position The index in the target text at which the search * should start. * @param status for error codes if it occurs. + * @return index at which the match starts, else if match is not found + * USEARCH_DONE is returned */ virtual UTextOffset handlePrev(UTextOffset position, UErrorCode &status) = 0; diff --git a/icu4c/source/i18n/usearch.cpp b/icu4c/source/i18n/usearch.cpp index 88d8485df0b..cbbb6d554b2 100644 --- a/icu4c/source/i18n/usearch.cpp +++ b/icu4c/source/i18n/usearch.cpp @@ -2343,6 +2343,9 @@ U_CAPI UStringSearch * U_EXPORT2 usearch_open(const UChar *pattern, UBreakIterator *breakiter, UErrorCode *status) { + if (U_FAILURE(*status)) { + return NULL; + } if (locale) { // ucol_open internally checks for status UCollator *collator = ucol_open(locale, status); @@ -2375,6 +2378,9 @@ U_CAPI UStringSearch * U_EXPORT2 usearch_openFromCollator( UBreakIterator *breakiter, UErrorCode *status) { + if (U_FAILURE(*status)) { + return NULL; + } if (pattern == NULL || text == NULL || collator == NULL) { *status = U_ILLEGAL_ARGUMENT_ERROR; } @@ -2614,7 +2620,7 @@ U_CAPI void U_EXPORT2 usearch_setBreakIterator(UStringSearch *strsrch, UBreakIterator *breakiter, UErrorCode *status) { - if (strsrch) { + if (U_SUCCESS(*status) && strsrch) { strsrch->search->breakIter = breakiter; if (breakiter) { ubrk_setText(breakiter, strsrch->search->text,