Home | History | Annotate | Download | only in i18n
      1 /*
      2  * Copyright (C) 2015, International Business Machines
      3  * Corporation and others.  All Rights Reserved.
      4  *
      5  * file name: digitaffix.cpp
      6  */
      7 
      8 #include "unicode/utypes.h"
      9 
     10 #if !UCONFIG_NO_FORMATTING
     11 
     12 #include "digitaffix.h"
     13 #include "fphdlimp.h"
     14 #include "uassert.h"
     15 #include "unistrappender.h"
     16 
     17 U_NAMESPACE_BEGIN
     18 
     19 DigitAffix::DigitAffix() : fAffix(), fAnnotations() {
     20 }
     21 
     22 DigitAffix::DigitAffix(
     23         const UChar *value, int32_t charCount, int32_t fieldId)
     24         : fAffix(value, charCount),
     25           fAnnotations(charCount, (UChar) fieldId, charCount) {
     26 }
     27 
     28 void
     29 DigitAffix::remove() {
     30     fAffix.remove();
     31     fAnnotations.remove();
     32 }
     33 
     34 void
     35 DigitAffix::appendUChar(UChar value, int32_t fieldId) {
     36     fAffix.append(value);
     37     fAnnotations.append((UChar) fieldId);
     38 }
     39 
     40 void
     41 DigitAffix::append(const UnicodeString &value, int32_t fieldId) {
     42     fAffix.append(value);
     43     {
     44         UnicodeStringAppender appender(fAnnotations);
     45         int32_t len = value.length();
     46         for (int32_t i = 0; i < len; ++i) {
     47             appender.append((UChar) fieldId);
     48         }
     49     }
     50 }
     51 
     52 void
     53 DigitAffix::setTo(const UnicodeString &value, int32_t fieldId) {
     54     fAffix = value;
     55     fAnnotations.remove();
     56     {
     57         UnicodeStringAppender appender(fAnnotations);
     58         int32_t len = value.length();
     59         for (int32_t i = 0; i < len; ++i) {
     60             appender.append((UChar) fieldId);
     61         }
     62     }
     63 }
     64 
     65 void
     66 DigitAffix::append(const UChar *value, int32_t charCount, int32_t fieldId) {
     67     fAffix.append(value, charCount);
     68     {
     69         UnicodeStringAppender appender(fAnnotations);
     70         for (int32_t i = 0; i < charCount; ++i) {
     71             appender.append((UChar) fieldId);
     72         }
     73     }
     74 }
     75 
     76 UnicodeString &
     77 DigitAffix::format(FieldPositionHandler &handler, UnicodeString &appendTo) const {
     78     int32_t len = fAffix.length();
     79     if (len == 0) {
     80         return appendTo;
     81     }
     82     if (!handler.isRecording()) {
     83         return appendTo.append(fAffix);
     84     }
     85     U_ASSERT(fAffix.length() == fAnnotations.length());
     86     int32_t appendToStart = appendTo.length();
     87     int32_t lastId = (int32_t) fAnnotations.charAt(0);
     88     int32_t lastIdStart = 0;
     89     for (int32_t i = 1; i < len; ++i) {
     90         int32_t id = (int32_t) fAnnotations.charAt(i);
     91         if (id != lastId) {
     92             if (lastId != UNUM_FIELD_COUNT) {
     93                 handler.addAttribute(lastId, appendToStart + lastIdStart, appendToStart + i);
     94             }
     95             lastId = id;
     96             lastIdStart = i;
     97         }
     98     }
     99     if (lastId != UNUM_FIELD_COUNT) {
    100         handler.addAttribute(lastId, appendToStart + lastIdStart, appendToStart + len);
    101     }
    102     return appendTo.append(fAffix);
    103 }
    104 
    105 U_NAMESPACE_END
    106 
    107 #endif /* #if !UCONFIG_NO_FORMATTING */
    108