-// Intermediate class:
-// Has Normalizer2Impl and does boilerplate argument checking and setup.
-class Normalizer2WithImpl : public Normalizer2 {
-public:
- Normalizer2WithImpl(const Normalizer2Impl &ni) : impl(ni) {}
- virtual ~Normalizer2WithImpl();
-
- // normalize
- virtual UnicodeString &
- normalize(const UnicodeString &src,
- UnicodeString &dest,
- UErrorCode &errorCode) const {
- if(U_FAILURE(errorCode)) {
- dest.setToBogus();
- return dest;
- }
- const UChar *sArray=src.getBuffer();
- if(&dest==&src || sArray==NULL) {
- errorCode=U_ILLEGAL_ARGUMENT_ERROR;
- dest.setToBogus();
- return dest;
- }
- dest.remove();
- ReorderingBuffer buffer(impl, dest);
- if(buffer.init(src.length(), errorCode)) {
- normalize(sArray, sArray+src.length(), buffer, errorCode);
- }
- return dest;
- }
- virtual void
- normalize(const UChar *src, const UChar *limit,
- ReorderingBuffer &buffer, UErrorCode &errorCode) const = 0;
-
- // normalize and append
- virtual UnicodeString &
- normalizeSecondAndAppend(UnicodeString &first,
- const UnicodeString &second,
- UErrorCode &errorCode) const {
- return normalizeSecondAndAppend(first, second, TRUE, errorCode);
- }
- virtual UnicodeString &
- append(UnicodeString &first,
- const UnicodeString &second,
- UErrorCode &errorCode) const {
- return normalizeSecondAndAppend(first, second, FALSE, errorCode);
- }
- UnicodeString &
- normalizeSecondAndAppend(UnicodeString &first,
- const UnicodeString &second,
- UBool doNormalize,
- UErrorCode &errorCode) const {
- uprv_checkCanGetBuffer(first, errorCode);
- if(U_FAILURE(errorCode)) {
- return first;
- }
- const UChar *secondArray=second.getBuffer();
- if(&first==&second || secondArray==NULL) {
- errorCode=U_ILLEGAL_ARGUMENT_ERROR;
- return first;
- }
- int32_t firstLength=first.length();
- UnicodeString safeMiddle;
- {
- ReorderingBuffer buffer(impl, first);
- if(buffer.init(firstLength+second.length(), errorCode)) {
- normalizeAndAppend(secondArray, secondArray+second.length(), doNormalize,
- safeMiddle, buffer, errorCode);
- }
- } // The ReorderingBuffer destructor finalizes the first string.
- if(U_FAILURE(errorCode)) {
- // Restore the modified suffix of the first string.
- first.replace(firstLength-safeMiddle.length(), 0x7fffffff, safeMiddle);
- }
- return first;
- }
- virtual void
- normalizeAndAppend(const UChar *src, const UChar *limit, UBool doNormalize,
- UnicodeString &safeMiddle,
- ReorderingBuffer &buffer, UErrorCode &errorCode) const = 0;
- virtual UBool
- getDecomposition(UChar32 c, UnicodeString &decomposition) const {
- UChar buffer[4];
- int32_t length;
- const UChar *d=impl.getDecomposition(c, buffer, length);
- if(d==NULL) {
- return FALSE;
- }
- if(d==buffer) {
- decomposition.setTo(buffer, length); // copy the string (Jamos from Hangul syllable c)
- } else {
- decomposition.setTo(FALSE, d, length); // read-only alias
- }
- return TRUE;
- }
- virtual UBool
- getRawDecomposition(UChar32 c, UnicodeString &decomposition) const {
- UChar buffer[30];
- int32_t length;
- const UChar *d=impl.getRawDecomposition(c, buffer, length);
- if(d==NULL) {
- return FALSE;
- }
- if(d==buffer) {
- decomposition.setTo(buffer, length); // copy the string (algorithmic decomposition)
- } else {
- decomposition.setTo(FALSE, d, length); // read-only alias
- }
- return TRUE;
- }
- virtual UChar32
- composePair(UChar32 a, UChar32 b) const {
- return impl.composePair(a, b);
- }
-
- virtual uint8_t
- getCombiningClass(UChar32 c) const {
- return impl.getCC(impl.getNorm16(c));
- }
-
- // quick checks
- virtual UBool
- isNormalized(const UnicodeString &s, UErrorCode &errorCode) const {
- if(U_FAILURE(errorCode)) {
- return FALSE;
- }
- const UChar *sArray=s.getBuffer();
- if(sArray==NULL) {
- errorCode=U_ILLEGAL_ARGUMENT_ERROR;
- return FALSE;
- }
- const UChar *sLimit=sArray+s.length();
- return sLimit==spanQuickCheckYes(sArray, sLimit, errorCode);
- }
- virtual UNormalizationCheckResult
- quickCheck(const UnicodeString &s, UErrorCode &errorCode) const {
- return Normalizer2WithImpl::isNormalized(s, errorCode) ? UNORM_YES : UNORM_NO;
- }
- virtual int32_t
- spanQuickCheckYes(const UnicodeString &s, UErrorCode &errorCode) const {
- if(U_FAILURE(errorCode)) {
- return 0;
- }
- const UChar *sArray=s.getBuffer();
- if(sArray==NULL) {
- errorCode=U_ILLEGAL_ARGUMENT_ERROR;
- return 0;
- }
- return (int32_t)(spanQuickCheckYes(sArray, sArray+s.length(), errorCode)-sArray);
- }
- virtual const UChar *
- spanQuickCheckYes(const UChar *src, const UChar *limit, UErrorCode &errorCode) const = 0;
-
- virtual UNormalizationCheckResult getQuickCheck(UChar32) const {
- return UNORM_YES;
- }
-
- const Normalizer2Impl &impl;
-};
-