U_CAPI UCollationElements* U_EXPORT2 ucol_openElements(const UCollator *coll, const UChar *text, int32_t textLength, UErrorCode *status) { UCollationElements *result; if (U_FAILURE(*status)) { return NULL; } result = (UCollationElements *)uprv_malloc(sizeof(UCollationElements)); /* test for NULL */ if (result == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; return NULL; } result->reset_ = TRUE; result->isWritable = FALSE; if (text == NULL) { textLength = 0; } uprv_init_collIterate(coll, text, textLength, &result->iteratordata_); return result; }
U_CAPI void U_EXPORT2 ucol_setText( UCollationElements *elems, const UChar *text, int32_t textLength, UErrorCode *status) { if (U_FAILURE(*status)) { return; } if (elems->isWritable && elems->iteratordata_.string != NULL) { uprv_free(elems->iteratordata_.string); } if (text == NULL) { textLength = 0; } elems->isWritable = FALSE; uprv_init_collIterate(elems->iteratordata_.coll, text, textLength, &elems->iteratordata_); elems->reset_ = TRUE; }
// Sets the source to the new character iterator. void CollationElementIterator::setText(CharacterIterator& source, UErrorCode& status) { if (U_FAILURE(status)) return; int32_t length = source.getLength(); UChar *buffer = NULL; if (length == 0) { buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR); /* test for NULL */ if (buffer == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } *buffer = 0; } else { buffer = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length); /* test for NULL */ if (buffer == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } /* Using this constructor will prevent buffer from being removed when string gets removed */ UnicodeString string; source.getText(string); u_memcpy(buffer, string.getBuffer(), length); } if (m_data_->isWritable && m_data_->iteratordata_.string != NULL) { uprv_free((UChar *)m_data_->iteratordata_.string); } m_data_->isWritable = TRUE; /* Free offsetBuffer before initializing it. */ ucol_freeOffsetBuffer(&(m_data_->iteratordata_)); uprv_init_collIterate(m_data_->iteratordata_.coll, buffer, length, &m_data_->iteratordata_, &status); m_data_->reset_ = TRUE; }
/** * Sets the source to the new source string. */ void CollationElementIterator::setText(const UnicodeString& source, UErrorCode& status) { if (U_FAILURE(status)) { return; } int32_t length = source.length(); UChar *string = NULL; if (m_data_->isWritable && m_data_->iteratordata_.string != NULL) { uprv_free((UChar *)m_data_->iteratordata_.string); } m_data_->isWritable = TRUE; if (length > 0) { string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR * length); /* test for NULL */ if (string == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } u_memcpy(string, source.getBuffer(), length); } else { string = (UChar *)uprv_malloc(U_SIZEOF_UCHAR); /* test for NULL */ if (string == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return; } *string = 0; } /* Free offsetBuffer before initializing it. */ ucol_freeOffsetBuffer(&(m_data_->iteratordata_)); uprv_init_collIterate(m_data_->iteratordata_.coll, string, length, &m_data_->iteratordata_, &status); m_data_->reset_ = TRUE; }