static void appendRange( const UnicodeString &src, int32_t start, int32_t end, UnicodeString &dest) { dest.append(src, start, end - start); }
/* Explain <xxxxx> tag to a native value * * Since <xxxxx> is always larger than the native value, * the operation will replace the tag directly in the buffer, * and, of course, will shift tail elements. */ void IdnaConfTest::ExplainCodePointTag(UnicodeString& buf){ buf.append((UChar)0); // add a terminal NULL UChar* bufBase = buf.getBuffer(buf.length()); UChar* p = bufBase; while (*p != 0){ if ( *p != 0x3C){ // < *bufBase++ = *p++; } else { p++; // skip < UChar32 cp = 0; for ( ;*p != 0x3E; p++){ // > if (0x30 <= *p && *p <= 0x39){ // 0-9 cp = (cp * 16) + (*p - 0x30); } else if (0x61 <= *p && *p <= 0x66){ // a-f cp = (cp * 16) + (*p - 0x61) + 10; } else if (0x41 <= *p && *p <= 0x46) {// A-F cp = (cp * 16) + (*p - 0x41) + 10; } // no else. hope everything is good. } p++; // skip > if (U_IS_BMP(cp)){ *bufBase++ = cp; } else { *bufBase++ = U16_LEAD(cp); *bufBase++ = U16_TRAIL(cp); } } } *bufBase = 0; // close our buffer buf.releaseBuffer(); }
static int32_t _concatenate(const UChar *left, int32_t leftLength, const UChar *right, int32_t rightLength, UChar *dest, int32_t destCapacity, const Normalizer2 *n2, UErrorCode *pErrorCode) { if(U_FAILURE(*pErrorCode)) { return 0; } if(destCapacity<0 || (dest==NULL && destCapacity>0) || left==NULL || leftLength<-1 || right==NULL || rightLength<-1) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* check for overlapping right and destination */ if( dest!=NULL && ((right>=dest && right<(dest+destCapacity)) || (rightLength>0 && dest>=right && dest<(right+rightLength))) ) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* allow left==dest */ UnicodeString destString; if(left==dest) { destString.setTo(dest, leftLength, destCapacity); } else { destString.setTo(dest, 0, destCapacity); destString.append(left, leftLength); } return n2->append(destString, UnicodeString(rightLength<0, right, rightLength), *pErrorCode). extract(dest, destCapacity, *pErrorCode); }
static void doubleToStr( const void *doublePtr, UnicodeString &appendTo) { char buffer[256]; double x = *static_cast<const double *>(doublePtr); sprintf(buffer, "%f", x); appendTo.append(buffer); }
UnicodeString& GLUE_SYM (DateFormat ) :: format( Calendar& cal, UnicodeString& appendTo, FieldPosition& pos) const { #if DATE_FE_DEBUG fprintf(stderr, "VCF " ICUGLUE_VER_STR " - formatting. \n"); #endif int32_t len = appendTo.length(); UChar junk[200]; UErrorCode status = U_ZERO_ERROR; UFieldPosition pos2; int32_t nlen = OICU_udat_format(_this, cal.getTime(status), junk, 200, &pos2, &status); // todo: use pos2 pos.setBeginIndex(len); pos.setEndIndex(len += nlen); appendTo.append(junk, nlen); return appendTo; }
void AlphabeticIndex::hackName(UnicodeString &dest, const UnicodeString &name, const Collator *col) { if (langType_ != kSimplified || !UNIHAN->contains(name.char32At(0))) { dest = name; return; } UErrorCode status = U_ZERO_ERROR; initPinyinBounds(col, status); if (U_FAILURE(status)) { dest = name; return; } // TODO: use binary search int index; for (index=0; ; index++) { if ((*HACK_PINYIN_LOOKUP)[index][0] == (UChar)0xffff) { index--; break; } int32_t compareResult = col->compare(name, UnicodeString(TRUE, (*HACK_PINYIN_LOOKUP)[index], -1)); if (compareResult < 0) { index--; } if (compareResult <= 0) { break; } } UChar c = PINYIN_LOWER_BOUNDS[index]; dest.setTo(c); dest.append(name); return; }
void DateIntervalFormat::concatSingleDate2TimeInterval(const UChar* format, int32_t formatLen, const UnicodeString& datePattern, UCalendarDateFields field, UErrorCode& status) { // following should not set wrong status int32_t itvPtnIndex = DateIntervalInfo::calendarFieldToIntervalIndex(field, status); if ( U_FAILURE(status) ) { return; } PatternInfo& timeItvPtnInfo = fIntervalPatterns[itvPtnIndex]; if ( !timeItvPtnInfo.firstPart.isEmpty() ) { // UnicodeString allocated here is adopted, so no need to delete UnicodeString* timeIntervalPattern = new UnicodeString(timeItvPtnInfo.firstPart); timeIntervalPattern->append(timeItvPtnInfo.secondPart); UnicodeString* dateStr = new UnicodeString(datePattern); Formattable fmtArray[2]; fmtArray[0].adoptString(timeIntervalPattern); fmtArray[1].adoptString(dateStr); UnicodeString combinedPattern; MessageFormat::format(UnicodeString(TRUE, format, formatLen), fmtArray, 2, combinedPattern, status); if ( U_FAILURE(status) ) { return; } setIntervalPattern(field, combinedPattern, timeItvPtnInfo.laterDateFirst); } // else: fall back // it should not happen if the interval format defined is valid }
ssize_t BLocale::FormatDateTime(char* target, size_t maxSize, time_t time, BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle) const { BAutolock lock(fLock); if (!lock.IsLocked()) return B_ERROR; BString format; fConventions.GetDateFormat(dateStyle, format); ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(format)); if (dateFormatter.Get() == NULL) return B_NO_MEMORY; fConventions.GetTimeFormat(timeStyle, format); ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format)); if (timeFormatter.Get() == NULL) return B_NO_MEMORY; UnicodeString icuString; dateFormatter->format((UDate)time * 1000, icuString); icuString.append(UnicodeString::fromUTF8(", ")); timeFormatter->format((UDate)time * 1000, icuString); CheckedArrayByteSink stringConverter(target, maxSize); icuString.toUTF8(stringConverter); if (stringConverter.Overflowed()) return B_BAD_VALUE; return stringConverter.NumberOfBytesWritten(); }
U_CAPI int32_t U_EXPORT2 unorm_concatenate(const UChar * left, int32_t leftLength, const UChar * right, int32_t rightLength, UChar * dest, int32_t destCapacity, UNormalizationMode mode, int32_t options, UErrorCode * pErrorCode) { const Normalizer2 * n2 = Normalizer2Factory::getInstance(mode, *pErrorCode); const UnicodeSet * uni32; if (options & UNORM_UNICODE_3_2) { uni32 = uniset_getUnicode32Instance(*pErrorCode); } else { uni32 = NULL; // unused } FilteredNormalizer2 fn2(*n2, *uni32); if (options & UNORM_UNICODE_3_2) { n2 = &fn2; } if (U_FAILURE(*pErrorCode)) { return 0; } if (destCapacity < 0 || (dest == NULL && destCapacity > 0) || left == NULL || leftLength < -1 || right == NULL || rightLength < -1 ) { *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* check for overlapping right and destination */ if (dest != NULL && ((right >= dest && right < (dest + destCapacity)) || (rightLength > 0 && dest >= right && dest < (right + rightLength))) ) { *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* allow left==dest */ UnicodeString destString; if (left == dest) { destString.setTo(dest, leftLength, destCapacity); } else { destString.setTo(dest, 0, destCapacity); destString.append(left, leftLength); } return n2->append(destString, UnicodeString(rightLength < 0, right, rightLength), *pErrorCode). extract(dest, destCapacity, *pErrorCode); }
UnicodeString &StringSetMonkey::generateAlternative(const UnicodeString &testCase, UnicodeString &alternate) { // find out shortest string for the longest sequence of ces. // needs to be refined to use dynamic programming, but will be roughly right UErrorCode status = U_ZERO_ERROR; CEList ceList(coll, testCase, status); UnicodeString alt; int32_t offset = 0; if (ceList.size() == 0) { return alternate.append(testCase); } while (offset < ceList.size()) { int32_t ce = ceList.get(offset); const StringList *strings = collData->getStringList(ce); if (strings == NULL) { return alternate.append(testCase); } int32_t stringCount = strings->size(); int32_t tries = 0; // find random string that generates the same CEList const CEList *ceList2 = NULL; const UnicodeString *string = NULL; UBool matches = FALSE; do { int32_t s = m_rand() % stringCount; if (tries++ > stringCount) { alternate.append(testCase); return alternate; } string = strings->get(s); ceList2 = collData->getCEList(string); matches = ceList.matchesAt(offset, ceList2); if (! matches) { collData->freeCEList((CEList *) ceList2); } } while (! matches); alt.append(*string); offset += ceList2->size(); collData->freeCEList(ceList2); } const CEList altCEs(coll, alt, status); if (ceList.matchesAt(0, &altCEs)) { return alternate.append(alt); } return alternate.append(testCase); }
/** * Given source, target, and variant strings, concatenate them into a * full ID. If the source is empty, then "Any" will be used for the * source, so the ID will always be of the form s-t/v or s-t. */ void TransliteratorIDParser::STVtoID(const UnicodeString& source, const UnicodeString& target, const UnicodeString& variant, UnicodeString& id) { id = source; if (id.length() == 0) { id.setTo(ANY, 3); } id.append(TARGET_SEP).append(target); if (variant.length() != 0) { id.append(VARIANT_SEP).append(variant); } // NUL-terminate the ID string for getTerminatedBuffer. // This prevents valgrind and Purify warnings. id.append((UChar)0); id.truncate(id.length()-1); }
void UCharsTrieElement::setTo(const UnicodeString &s, int32_t val, UnicodeString &strings, UErrorCode &errorCode) { if(U_FAILURE(errorCode)) { return; } int32_t length=s.length(); if(length>0xffff) { // Too long: We store the length in 1 unit. errorCode=U_INDEX_OUTOFBOUNDS_ERROR; return; } stringOffset=strings.length(); strings.append((UChar)length); value=val; strings.append(s); }
TestReplaceable (const UnicodeString& text, const UnicodeString& newStyles) { chars = text; UnicodeString s; for (int i = 0; i < text.length(); ++i) { if (i < newStyles.length()) { s.append(newStyles.charAt(i)); } else { if (text.charAt(i) == NO_STYLE_MARK) { s.append(NO_STYLE); } else { s.append((UChar)(i + 0x0031)); } } } this->styles = s; }
UnicodeString AlphabeticIndex::separated(const UnicodeString &item) { UnicodeString result; if (item.length() == 0) { return result; } int32_t i = 0; for (;;) { UChar32 cp = item.char32At(i); result.append(cp); i = item.moveIndex32(i, 1); if (i >= item.length()) { break; } result.append(CGJ); } return result; }
// use these for now void ICUServiceTest::confirmStringsEqual(const UnicodeString& message, const UnicodeString& lhs, const UnicodeString& rhs) { UBool equ = lhs == rhs; UnicodeString temp = message; temp.append(" lhs: "); temp.append(lhs); temp.append(" rhs: "); temp.append(rhs); if (equ) { logln(temp); } else { dataerrln(temp); } }
RuleBasedNumberFormat::RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& alocale, UErrorCode& status) : ruleSets(NULL) , ruleSetDescriptions(NULL) , numRuleSets(0) , defaultRuleSet(NULL) , locale(alocale) , collator(NULL) , decimalFormatSymbols(NULL) , lenient(FALSE) , lenientParseRules(NULL) , localizations(NULL) { if (U_FAILURE(status)) { return; } const char* rules_tag = "RBNFRules"; const char* fmt_tag = ""; switch (tag) { case URBNF_SPELLOUT: fmt_tag = "SpelloutRules"; break; case URBNF_ORDINAL: fmt_tag = "OrdinalRules"; break; case URBNF_DURATION: fmt_tag = "DurationRules"; break; case URBNF_NUMBERING_SYSTEM: fmt_tag = "NumberingSystemRules"; break; default: status = U_ILLEGAL_ARGUMENT_ERROR; return; } // TODO: read localization info from resource LocalizationInfo* locinfo = NULL; UResourceBundle* nfrb = ures_open(U_ICUDATA_RBNF, locale.getName(), &status); if (U_SUCCESS(status)) { setLocaleIDs(ures_getLocaleByType(nfrb, ULOC_VALID_LOCALE, &status), ures_getLocaleByType(nfrb, ULOC_ACTUAL_LOCALE, &status)); UResourceBundle* rbnfRules = ures_getByKeyWithFallback(nfrb, rules_tag, NULL, &status); if (U_FAILURE(status)) { ures_close(nfrb); } UResourceBundle* ruleSets = ures_getByKeyWithFallback(rbnfRules, fmt_tag, NULL, &status); if (U_FAILURE(status)) { ures_close(rbnfRules); ures_close(nfrb); return; } UnicodeString desc; while (ures_hasNext(ruleSets)) { desc.append(ures_getNextUnicodeString(ruleSets,NULL,&status)); } UParseError perror; init (desc, locinfo, perror, status); ures_close(ruleSets); ures_close(rbnfRules); } ures_close(nfrb); }
/** * Finish constructing a transliterator: only to be called by * constructors. Before calling init(), set trans and filter to NULL. * @param list a vector of transliterator objects to be adopted. It * should NOT be empty. The list should be in declared order. That * is, it should be in the FORWARD order; if direction is REVERSE then * the list order will be reversed. * @param direction either FORWARD or REVERSE * @param fixReverseID if TRUE, then reconstruct the ID of reverse * entries by calling getID() of component entries. Some constructors * do not require this because they apply a facade ID anyway. * @param status the error code indicating success or failure */ void CompoundTransliterator::init(UVector& list, UTransDirection direction, UBool fixReverseID, UErrorCode& status) { // assert(trans == 0); // Allocate array if (U_SUCCESS(status)) { count = list.size(); trans = (Transliterator **)uprv_malloc(count * sizeof(Transliterator *)); /* test for NULL */ if (trans == 0) { status = U_MEMORY_ALLOCATION_ERROR; return; } } if (U_FAILURE(status) || trans == 0) { // assert(trans == 0); return; } // Move the transliterators from the vector into an array. // Reverse the order if necessary. int32_t i; for (i=0; i<count; ++i) { int32_t j = (direction == UTRANS_FORWARD) ? i : count - 1 - i; trans[i] = (Transliterator*) list.elementAt(j); } // If the direction is UTRANS_REVERSE then we may need to fix the // ID. if (direction == UTRANS_REVERSE && fixReverseID) { UnicodeString newID; for (i=0; i<count; ++i) { if (i > 0) { newID.append(ID_DELIM); } newID.append(trans[i]->getID()); } setID(newID); } computeMaximumContextLength(); }
void LocalizedNumberFormatter::getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result, UErrorCode& status) const { NumberStringBuilder string; auto signum = static_cast<int8_t>(isNegative ? -1 : 1); // Always return affixes for plural form OTHER. static const StandardPlural::Form plural = StandardPlural::OTHER; int32_t prefixLength; if (computeCompiled(status)) { prefixLength = fCompiled->getPrefixSuffix(signum, plural, string, status); } else { prefixLength = NumberFormatterImpl::getPrefixSuffixStatic(fMacros, signum, plural, string, status); } result.remove(); if (isPrefix) { result.append(string.toTempUnicodeString().tempSubStringBetween(0, prefixLength)); } else { result.append(string.toTempUnicodeString().tempSubStringBetween(prefixLength, string.length())); } }
void CSVFormatter::formatRow(const CSVRow& row, UnicodeString& result, UChar32 sepchar, unsigned int varformat) { result.remove(); UnicodeString formatted; int lastcolumn=0; CSVRecordMap::const_iterator iter; for (iter=row.begin(); iter!=row.end(); ++iter) { for (; lastcolumn<iter->first; ++lastcolumn) { result.append(sepchar); } if (iter->second) { iter->second->getValueFormatted(formatted, varformat); // result.append(iter->second->getTypeName()); // result.append(':'); result.append(formatted); //} else { // result.append("[null]"); } } }
/** * Returns a textual description of the substitution * @return A textual description of the substitution. This might * not be identical to the description it was created from, but * it'll produce the same result. */ void NFSubstitution::toString(UnicodeString& text) const { // use tokenChar() to get the character at the beginning and // end of the substitutin token. In between them will go // either the name of the rule set it uses, or the pattern of // the DecimalFormat it uses text.remove(); text.append(tokenChar()); UnicodeString temp; if (ruleSet != NULL) { ruleSet->getName(temp); } else if (numberFormat != NULL) { numberFormat->toPattern(temp); } text.append(temp); text.append(tokenChar()); }
/** * Given a Specs object, return a SingleID representing the * special inverse of that ID. If there is no special inverse * then return NULL. * @return a SingleID or NULL. Returned object always has * 'filter' field of NULL. */ TransliteratorIDParser::SingleID * TransliteratorIDParser::specsToSpecialInverse(const Specs & specs, UErrorCode & status) { if (0 != specs.source.caseCompare(ANY, U_FOLD_CASE_DEFAULT)) { return NULL; } init(status); UnicodeString * inverseTarget; umtx_lock(&LOCK); inverseTarget = (UnicodeString *) SPECIAL_INVERSES->get(specs.target); umtx_unlock(&LOCK); if (inverseTarget != NULL) { // If the original ID contained "Any-" then make the // special inverse "Any-Foo"; otherwise make it "Foo". // So "Any-NFC" => "Any-NFD" but "NFC" => "NFD". UnicodeString buf; if (specs.filter.length() != 0) { buf.append(specs.filter); } if (specs.sawSource) { buf.append(ANY).append(TARGET_SEP); } buf.append(*inverseTarget); UnicodeString basicID(ANY); basicID.append(TARGET_SEP).append(*inverseTarget); if (specs.variant.length() != 0) { buf.append(VARIANT_SEP).append(specs.variant); basicID.append(VARIANT_SEP).append(specs.variant); } return new SingleID(buf, basicID); } return NULL; }
UBool DataDrivenNumberFormatTestSuite::isPass( const NumberFormatTestTuple &tuple, UnicodeString &appendErrorMessage, UErrorCode &status) { if (U_FAILURE(status)) { return FALSE; } UBool result = FALSE; if (tuple.formatFlag && tuple.outputFlag) { ++fFormatTestNumber; result = isFormatPass( tuple, fPreviousFormatters[ fFormatTestNumber % UPRV_LENGTHOF(fPreviousFormatters)], appendErrorMessage, status); } else if (tuple.toPatternFlag || tuple.toLocalizedPatternFlag) { result = isToPatternPass(tuple, appendErrorMessage, status); } else if (tuple.parseFlag && tuple.outputFlag && tuple.outputCurrencyFlag) { result = isParseCurrencyPass(tuple, appendErrorMessage, status); } else if (tuple.parseFlag && tuple.outputFlag) { result = isParsePass(tuple, appendErrorMessage, status); } else if (tuple.pluralFlag) { result = isSelectPass(tuple, appendErrorMessage, status); } else { appendErrorMessage.append("Unrecognized test type."); status = U_ILLEGAL_ARGUMENT_ERROR; } if (!result) { if (appendErrorMessage.length() > 0) { appendErrorMessage.append(": "); } if (U_FAILURE(status)) { appendErrorMessage.append(u_errorName(status)); appendErrorMessage.append(": "); } tuple.toString(appendErrorMessage); } return result; }
static void appendRange( const UnicodeString &src, int32_t start, int32_t end, UnicodeString &dest) { // This check improves performance significantly. if (start == end) { return; } dest.append(src, start, end - start); }
static void fromEnum( const Numberformattesttuple_EnumConversion *table, int32_t tableLength, int32_t val, UnicodeString &appendTo) { for (int32_t i = 0; i < tableLength; ++i) { if (table[i].value == val) { appendTo.append(table[i].str); } } }
void UObjectTest::TestMFCCompatibility() { #if U_HAVE_DEBUG_LOCATION_NEW /* Make sure that it compiles with MFC's debuggable new usage. */ UnicodeString *str = new(__FILE__, __LINE__) UnicodeString(); str->append((UChar)0x0040); // Is it usable? if(str->charAt(0) != 0x0040) { errln("debug new doesn't work."); } UnicodeString::operator delete(str, __FILE__, __LINE__); #endif }
void IntlTestRBNFParse::testfmt(RuleBasedNumberFormat* formatter, int val, UErrorCode& status) { UnicodeString us; formatter->format((const Formattable)(int32_t)val, us, status); if (U_SUCCESS(status)) { us.insert(0, (UChar)'"'); us.append((UChar)'"'); logln(us); } else { logln("error: could not format %d, returned status: %d", val, status); } }
static void appendField( int32_t fieldId, const UnicodeString &value, FieldPositionHandler &handler, UnicodeString &appendTo) { int32_t currentLength = appendTo.length(); appendTo.append(value); handler.addAttribute( fieldId, currentLength, appendTo.length()); }
/** * Givens a Spec object, convert it to a SingleID object. The * Spec object is a more unprocessed parse result. The SingleID * object contains information about canonical and basic IDs. * @return a SingleID; never returns NULL. Returned object always * has 'filter' field of NULL. */ TransliteratorIDParser::SingleID* TransliteratorIDParser::specsToID(const Specs* specs, int32_t dir) { UnicodeString canonID; UnicodeString basicID; UnicodeString basicPrefix; if (specs != NULL) { UnicodeString buf; if (dir == FORWARD) { if (specs->sawSource) { buf.append(specs->source).append(TARGET_SEP); } else { basicPrefix = specs->source; basicPrefix.append(TARGET_SEP); } buf.append(specs->target); } else { buf.append(specs->target).append(TARGET_SEP).append(specs->source); } if (specs->variant.length() != 0) { buf.append(VARIANT_SEP).append(specs->variant); } basicID = basicPrefix; basicID.append(buf); if (specs->filter.length() != 0) { buf.insert(0, specs->filter); } canonID = buf; } return new SingleID(canonID, basicID); }
bool IniFormatter::formatValue(const ValueVariable& value, UnicodeString& result, unsigned int nameformat, unsigned int varformat) { if (value.getName().length()>0) { value.getNameFormatted(result, nameformat); UnicodeString temp; value.getValueFormatted(temp, varformat); result.append('=').append(temp); return true; } else { result.remove(); // clear the result string debug_print("Value name is 0-length"); } return false; }
void RuleBasedNumberFormat::stripWhitespace(UnicodeString& description) { // iterate through the characters... UnicodeString result; int start = 0; while (start != -1 && start < description.length()) { // seek to the first non-whitespace character... while (start < description.length() && PatternProps::isWhiteSpace(description.charAt(start))) { ++start; } // locate the next semicolon in the text and copy the text from // our current position up to that semicolon into the result int32_t p = description.indexOf(gSemiColon, start); if (p == -1) { // or if we don't find a semicolon, just copy the rest of // the string into the result result.append(description, start, description.length() - start); start = -1; } else if (p < description.length()) { result.append(description, start, p + 1 - start); start = p + 1; } // when we get here, we've seeked off the end of the sring, and // we terminate the loop (we continue until *start* is -1 rather // than until *p* is -1, because otherwise we'd miss the last // rule in the description) else { start = -1; } } description.setTo(result); }