void IdnaConfTest::Test(void){ if (!ReadAndConvertFile())return; UnicodeString s; UnicodeString key; UnicodeString value; // skip everything before the first "=====" and "=====" itself do { if (!ReadOneLine(s)) { errln("End of file prematurely found"); break; } } while (s.compare(C_TAG, -1) != 0); //"=====" while(ReadOneLine(s)){ s.trim(); key.remove(); value.remove(); if (s.compare(C_TAG, -1) == 0){ //"=====" Call(); } else { // explain key:value int p = s.indexOf((UChar)0x3A); // : key.setTo(s,0,p).trim(); value.setTo(s,p+1).trim(); if (key.compare(C_TYPE, -1) == 0){ if (value.compare(C_TOASCII, -1) == 0) { type = 0; } else if (value.compare(C_TOUNICODE, -1) == 0){ type = 1; } } else if (key.compare(C_PASSFAIL, -1) == 0){ if (value.compare(C_PASS, -1) == 0){ passfail = 0; } else if (value.compare(C_FAIL, -1) == 0){ passfail = 1; } } else if (key.compare(C_DESC, -1) == 0){ if (value.indexOf(C_USESTD3ASCIIRULES, u_strlen(C_USESTD3ASCIIRULES), 0) == -1){ option = 1; // not found } else { option = 0; } id.setTo(value, 0, value.indexOf((UChar)0x20)); // space } else if (key.compare(C_NAMEZONE, -1) == 0){ ExplainCodePointTag(value); namezone.setTo(value); } else if (key.compare(C_NAMEBASE, -1) == 0){ ExplainCodePointTag(value); namebase.setTo(value); } // just skip other lines } } Call(); // for last record }
/** * Parse an ID into pieces. Take IDs of the form T, T/V, S-T, * S-T/V, or S/V-T. If the source is missing, return a source of * ANY. * @param id the id string, in any of several forms * @return an array of 4 strings: source, target, variant, and * isSourcePresent. If the source is not present, ANY will be * given as the source, and isSourcePresent will be NULL. Otherwise * isSourcePresent will be non-NULL. The target may be empty if the * id is not well-formed. The variant may be empty. */ void TransliteratorIDParser::IDtoSTV(const UnicodeString & id, UnicodeString & source, UnicodeString & target, UnicodeString & variant, UBool & isSourcePresent) { source = ANY; target.truncate(0); variant.truncate(0); int32_t sep = id.indexOf(TARGET_SEP); int32_t var = id.indexOf(VARIANT_SEP); if (var < 0) { var = id.length(); } isSourcePresent = FALSE; if (sep < 0) { // Form: T/V or T (or /V) id.extractBetween(0, var, target); id.extractBetween(var, id.length(), variant); } else if (sep < var) { // Form: S-T/V or S-T (or -T/V or -T) if (sep > 0) { id.extractBetween(0, sep, source); isSourcePresent = TRUE; } id.extractBetween(++sep, var, target); id.extractBetween(var, id.length(), variant); } else { // Form: (S/V-T or /V-T) if (var > 0) { id.extractBetween(0, var, source); isSourcePresent = TRUE; } id.extractBetween(var, sep++, variant); id.extractBetween(sep, id.length(), target); } if (variant.length() > 0) { variant.remove(0, 1); } }
UBool LocaleUtility::isFallbackOf(const UnicodeString& root, const UnicodeString& child) { return child.indexOf(root) == 0 && (child.length() == root.length() || child.charAt(root.length()) == UNDERSCORE_CHAR); }
void TimeZoneBoundaryTest::findDaylightBoundaryUsingDate(UDate d, const char* startMode, UDate expectedBoundary) { UnicodeString str; if (dateToString(d, str).indexOf(startMode) == - 1) { logln(UnicodeString("Error: ") + startMode + " not present in " + str); } UDate min = d; UDate max = min + SIX_MONTHS; while ((max - min) > INTERVAL) { UDate mid = (min + max) / 2; UnicodeString* s = &dateToString(mid, str); if (s->indexOf(startMode) != - 1) { min = mid; } else { max = mid; } } logln("Date Before: " + showDate(min)); logln("Date After: " + showDate(max)); UDate mindelta = expectedBoundary - min; UDate maxdelta = max - expectedBoundary; if (mindelta >= 0 && mindelta <= INTERVAL && maxdelta >= 0 && maxdelta <= INTERVAL) logln(UnicodeString("PASS: Expected boundary at ") + expectedBoundary); else dataerrln(UnicodeString("FAIL: Expected boundary at ") + expectedBoundary); }
UBool U_EXPORT2 DateIntervalFormat::fieldExistsInSkeleton(UCalendarDateFields field, const UnicodeString& skeleton) { const UChar fieldChar = fgCalendarFieldToPatternLetter[field]; return ( (skeleton.indexOf(fieldChar) == -1)?FALSE:TRUE ) ; }
void NormalizerConformanceTest::compare(const UnicodeString& s1, const UnicodeString& s2){ UErrorCode status=U_ZERO_ERROR; // TODO: Re-enable this tests after UTC fixes UAX 21 if(s1.indexOf((UChar32)0x0345)>=0)return; if(Normalizer::compare(s1,s2,U_FOLD_CASE_DEFAULT,status)!=0){ errln("Normalizer::compare() failed for s1: " + prettify(s1) + " s2: " +prettify(s2)); } }
UnicodeString RegExpManager::Encode(const UnicodeString& regexp) const { UnicodeString result = regexp; int32_t pos = 0; // Replace + with \+ while ((pos = result.indexOf('+', pos)) != -1) { result.insert(pos, '\\'); pos += 2; } // Replace ' ' with + while ((pos = result.indexOf(' ')) != -1) { result.setCharAt(pos, '+'); } return result; }
Transliterator* U_EXPORT2 Transliterator::createInstance(const UnicodeString& ID, UTransDirection dir, UParseError& parseError, UErrorCode& status) { if (U_FAILURE(status)) { return 0; } UnicodeString canonID; UVector list(status); if (U_FAILURE(status)) { return NULL; } UnicodeSet* globalFilter; // TODO add code for parseError...currently unused, but // later may be used by parsing code... if (!TransliteratorIDParser::parseCompoundID(ID, dir, canonID, list, globalFilter)) { status = U_INVALID_ID; return NULL; } TransliteratorIDParser::instantiateList(list, status); if (U_FAILURE(status)) { return NULL; } U_ASSERT(list.size() > 0); Transliterator* t = NULL; if (list.size() > 1 || canonID.indexOf(ID_DELIM) >= 0) { // [NOTE: If it's a compoundID, we instantiate a CompoundTransliterator even if it only // has one child transliterator. This is so that toRules() will return the right thing // (without any inactive ID), but our main ID still comes out correct. That is, if we // instantiate "(Lower);Latin-Greek;", we want the rules to come out as "::Latin-Greek;" // even though the ID is "(Lower);Latin-Greek;". t = new CompoundTransliterator(list, parseError, status); } else { t = (Transliterator*)list.elementAt(0); } // Check null pointer if (t != NULL) { t->setID(canonID); if (globalFilter != NULL) { t->adoptFilter(globalFilter); } } else if (U_SUCCESS(status)) { status = U_MEMORY_ALLOCATION_ERROR; } return t; }
int t4p::FinderClass::ReplaceAllMatches(UnicodeString& text) const { int matches = 0; // no check for ReplaceExpression.isEmpty() allow for empty replacements // this allows the user to 'delete' parts of a strin if (IsPrepared) { UnicodeString replacement = ReplaceExpression; RegexMatcher* matcher = NULL; UErrorCode error = U_ZERO_ERROR; UnicodeString dest(text.length(), ' ', 0); int32_t pos = 0; if (EXACT == Mode || (REGULAR_EXPRESSION == Mode && ReplaceExpression.isEmpty())) { pos = text.indexOf(Expression, 0); while (pos >= 0) { text.replaceBetween(pos, pos + Expression.length(), replacement); pos = text.indexOf(Expression, pos + replacement.length()); ++matches; } } else { matcher = Pattern->matcher(text, error); if (U_SUCCESS(error) && matcher) { while (matcher->find()) { if (U_SUCCESS(error)) { matcher->appendReplacement(dest, replacement, error); if (U_SUCCESS(error)) { ++matches; } } } matcher->appendTail(dest); text = dest; } } if (matcher) { delete matcher; } } return matches; }
/** * Joins originalString and nextString using the pattern pat and puts the result in * originalString. */ void ListFormatter::addNewString(const UnicodeString& pat, UnicodeString& originalString, const UnicodeString& nextString, UErrorCode& errorCode) const { if (U_FAILURE(errorCode)) { return; } int32_t p0Offset = pat.indexOf(FIRST_PARAMETER, 3, 0); if (p0Offset < 0) { errorCode = U_ILLEGAL_ARGUMENT_ERROR; return; } int32_t p1Offset = pat.indexOf(SECOND_PARAMETER, 3, 0); if (p1Offset < 0) { errorCode = U_ILLEGAL_ARGUMENT_ERROR; return; } int32_t i, j; const UnicodeString* firstString; const UnicodeString* secondString; if (p0Offset < p1Offset) { i = p0Offset; j = p1Offset; firstString = &originalString; secondString = &nextString; } else { i = p1Offset; j = p0Offset; firstString = &nextString; secondString = &originalString; } UnicodeString result = UnicodeString(pat, 0, i) + *firstString; result += UnicodeString(pat, i+3, j-i-3); result += *secondString; result += UnicodeString(pat, j+3); originalString = result; }
bool t4p::FinderClass::FindNextExact(const UnicodeString& text, int32_t start, bool caseSensitive) { int32_t foundIndex = 0; if (!caseSensitive) { UnicodeString textLower(text); textLower.toLower(); UnicodeString expressionLower(Expression); expressionLower.toLower(); foundIndex = textLower.indexOf(expressionLower, start); } else { foundIndex = text.indexOf(Expression, start); } IsFound = -1 != foundIndex; if (IsFound) { LastPosition = foundIndex; LastLength = Expression.length(); } return IsFound; }
UnicodeString& U_EXPORT2 TimeZoneNamesImpl::getDefaultExemplarLocationName(const UnicodeString& tzID, UnicodeString& name) { if (tzID.isEmpty() || tzID.startsWith(gEtcPrefix, gEtcPrefixLen) || tzID.startsWith(gSystemVPrefix, gSystemVPrefixLen) || tzID.indexOf(gRiyadh8, gRiyadh8Len, 0) > 0) { name.setToBogus(); return name; } int32_t sep = tzID.lastIndexOf((UChar)0x2F /* '/' */); if (sep > 0 && sep + 1 < tzID.length()) { name.setTo(tzID, sep + 1); name.findAndReplace(UnicodeString((UChar)0x5f /* _ */), UnicodeString((UChar)0x20 /* space */)); } else { name.setToBogus(); } return name; }
std::vector<t4p::PhpTagClass> t4p::TagCacheClass::GetTagsAtPosition( const wxString& fileName, const UnicodeString& code, int posToCheck, const std::vector<wxFileName>& sourceDirs, t4p::PhpModuleClass& phpModule, wxString& status) { std::vector<t4p::PhpTagClass> matches; pelet::LexicalAnalyzerClass lexer; pelet::ParserClass parser; t4p::ScopeFinderClass scopeFinder; pelet::ScopeClass variableScope; pelet::VariableClass parsedVariable(variableScope); lexer.SetVersion(phpModule.Environment.Php.Version); parser.SetVersion(phpModule.Environment.Php.Version); scopeFinder.SetVersion(phpModule.Environment.Php.Version); UnicodeString codeUntilPos(code, 0, posToCheck); UnicodeString lastExpression = lexer.LastExpression(codeUntilPos); UnicodeString resourceName; bool doDuckTyping = true; if (!lastExpression.isEmpty()) { scopeFinder.GetScopeString(codeUntilPos, posToCheck, variableScope); if (lastExpression.indexOf(UNICODE_STRING_SIMPLE("\\")) > 0 && variableScope.ClassName.isEmpty() && variableScope.MethodName.isEmpty()) { // the expression is a namespace name outside a class or method. this is // most likely a namespace in the "use" statement // namespace in a use statement is always fully qualified, even if it does // not begin with a backslash lastExpression = UNICODE_STRING_SIMPLE("\\") + lastExpression; } parser.ParseExpression(lastExpression, parsedVariable); t4p::SymbolTableMatchErrorClass error; ResourceMatches(fileName, parsedVariable, variableScope, sourceDirs, matches, doDuckTyping, true, error); if (matches.empty()) { HandleAutoCompletionPhpStatus(error, lastExpression, parsedVariable, variableScope, matches, status); } } return matches; }
// populatePrefixSuffix Adds a specific prefix-suffix pair to result for a // given variant and log10 value. // variant is 'zero', 'one', 'two', 'few', 'many', or 'other'. // formatStr is the format string from which the prefix and suffix are // extracted. It is usually of form 'Pefix 000 suffix'. // populatePrefixSuffix returns the number of 0's found in formatStr // before the decimal point. // In the special case that formatStr contains only spaces for prefix // and suffix, populatePrefixSuffix returns log10Value + 1. static int32_t populatePrefixSuffix( const char* variant, int32_t log10Value, const UnicodeString& formatStr, UHashtable* result, UBool overwrite, UErrorCode& status) { if (U_FAILURE(status)) { return 0; } int32_t firstIdx = formatStr.indexOf(kZero, UPRV_LENGTHOF(kZero), 0); // We must have 0's in format string. if (firstIdx == -1) { status = U_INTERNAL_PROGRAM_ERROR; return 0; } int32_t lastIdx = formatStr.lastIndexOf(kZero, UPRV_LENGTHOF(kZero), firstIdx); CDFUnit* unit = createCDFUnit(variant, log10Value, result, status); if (U_FAILURE(status)) { return 0; } // Return -1 if we are not overwriting an existing value if (unit->isSet() && !overwrite) { return -1; } unit->markAsSet(); // Everything up to first 0 is the prefix unit->prefix = formatStr.tempSubString(0, firstIdx); fixQuotes(unit->prefix); // Everything beyond the last 0 is the suffix unit->suffix = formatStr.tempSubString(lastIdx + 1); fixQuotes(unit->suffix); // If there is effectively no prefix or suffix, ignore the actual number of // 0's and act as if the number of 0's matches the size of the number. if (onlySpaces(unit->prefix) && onlySpaces(unit->suffix)) { return log10Value + 1; } // Calculate number of zeros before decimal point int32_t idx = firstIdx + 1; while (idx <= lastIdx && formatStr.charAt(idx) == u_0) { ++idx; } return (idx - firstIdx); }
UnicodeString RegExpManager::Decode(const UnicodeString& regexp) const { UnicodeString result = regexp; int32_t pos = 0; while ((pos = result.indexOf('+', pos)) != -1) { // If the + is not preceeded by a \ we replace it with a ' ' // Otherwise we remove the \ and leave the + if (pos == 0 || result[pos - 1] != '\\') { result.setCharAt(pos, L' '); } else { result.remove(pos - 1, 1); } ++pos; } return result; }
void MessageImpl::appendReducedApostrophes(const UnicodeString &s, int32_t start, int32_t limit, UnicodeString &sb) { int32_t doubleApos=-1; for(;;) { int32_t i=s.indexOf(u_apos, start); if(i<0 || i>=limit) { sb.append(s, start, limit-start); break; } if(i==doubleApos) { // Double apostrophe at start-1 and start==i, append one. sb.append(u_apos); ++start; doubleApos=-1; } else { // Append text between apostrophes and skip this one. sb.append(s, start, i-start); doubleApos=start=i+1; } } }
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); }
/** * @bug 4103340 * @bug 4138203 * This bug really only works in Locale.US, since that's what the locale * used for Date.toString() is. Bug 4138203 reports that it fails on Korean * NT; it would actually have failed on any non-US locale. Now it should * work on all locales. */ void DateFormatRegressionTest::Test4103340(void) { UErrorCode status = U_ZERO_ERROR; // choose a date that is the FIRST of some month // and some arbitrary time UDate d = date(97, 3, 1, 1, 1, 1); SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status); if (failure(status, "new SimpleDateFormat", TRUE)) return; UnicodeString s; s = dateToString(d, s); UnicodeString s2; FieldPosition pos(FieldPosition::DONT_CARE); s2 = df->format(d, s2, pos); logln("Date=" + s); logln("DF=" + s2); UnicodeString substr; s2.extract(0,2, substr); if (s.indexOf(substr) == -1) errln("Months should match"); delete df; }
void NFRuleSet::parseRules(UnicodeString& description, const RuleBasedNumberFormat* owner, UErrorCode& status) { // start by creating a Vector whose elements are Strings containing // the descriptions of the rules (one rule per element). The rules // are separated by semicolons (there's no escape facility: ALL // semicolons are rule delimiters) if (U_FAILURE(status)) { return; } // ensure we are starting with an empty rule list rules.deleteAll(); // dlf - the original code kept a separate description array for no reason, // so I got rid of it. The loop was too complex so I simplified it. UnicodeString currentDescription; int32_t oldP = 0; while (oldP < description.length()) { int32_t p = description.indexOf(gSemicolon, oldP); if (p == -1) { p = description.length(); } currentDescription.setTo(description, oldP, p - oldP); NFRule::makeRules(currentDescription, this, rules.last(), owner, rules, status); oldP = p + 1; } // for rules that didn't specify a base value, their base values // were initialized to 0. Make another pass through the list and // set all those rules' base values. We also remove any special // rules from the list and put them into their own member variables int64_t defaultBaseValue = 0; // (this isn't a for loop because we might be deleting items from // the vector-- we want to make sure we only increment i when // we _didn't_ delete aything from the vector) uint32_t i = 0; while (i < rules.size()) { NFRule* rule = rules[i]; switch (rule->getType()) { // if the rule's base value is 0, fill in a default // base value (this will be 1 plus the preceding // rule's base value for regular rule sets, and the // same as the preceding rule's base value in fraction // rule sets) case NFRule::kNoBase: rule->setBaseValue(defaultBaseValue, status); if (!isFractionRuleSet()) { ++defaultBaseValue; } ++i; break; // if it's the negative-number rule, copy it into its own // data member and delete it from the list case NFRule::kNegativeNumberRule: if (negativeNumberRule) { delete negativeNumberRule; } negativeNumberRule = rules.remove(i); break; // if it's the improper fraction rule, copy it into the // correct element of fractionRules case NFRule::kImproperFractionRule: if (fractionRules[0]) { delete fractionRules[0]; } fractionRules[0] = rules.remove(i); break; // if it's the proper fraction rule, copy it into the // correct element of fractionRules case NFRule::kProperFractionRule: if (fractionRules[1]) { delete fractionRules[1]; } fractionRules[1] = rules.remove(i); break; // if it's the master rule, copy it into the // correct element of fractionRules case NFRule::kMasterRule: if (fractionRules[2]) { delete fractionRules[2]; } fractionRules[2] = rules.remove(i); break; // if it's a regular rule that already knows its base value, // check to make sure the rules are in order, and update // the default base value for the next rule default: if (rule->getBaseValue() < defaultBaseValue) { // throw new IllegalArgumentException("Rules are not in order"); status = U_PARSE_ERROR; return; } defaultBaseValue = rule->getBaseValue(); if (!isFractionRuleSet()) { ++defaultBaseValue; } ++i; break; } } }
void LocaleDisplayNamesImpl::initialize(void) { LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this; nonConstThis->locale = langData.getLocale() == Locale::getRoot() ? regionData.getLocale() : langData.getLocale(); UnicodeString sep; langData.getNoFallback("localeDisplayPattern", "separator", sep); if (sep.isBogus()) { sep = UnicodeString("{0}, {1}", -1, US_INV); } UErrorCode status = U_ZERO_ERROR; separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status); UnicodeString pattern; langData.getNoFallback("localeDisplayPattern", "pattern", pattern); if (pattern.isBogus()) { pattern = UnicodeString("{0} ({1})", -1, US_INV); } format.applyPatternMinMaxArguments(pattern, 2, 2, status); if (pattern.indexOf((UChar)0xFF08) >= 0) { formatOpenParen.setTo((UChar)0xFF08); // fullwidth ( formatReplaceOpenParen.setTo((UChar)0xFF3B); // fullwidth [ formatCloseParen.setTo((UChar)0xFF09); // fullwidth ) formatReplaceCloseParen.setTo((UChar)0xFF3D); // fullwidth ] } else { formatOpenParen.setTo((UChar)0x0028); // ( formatReplaceOpenParen.setTo((UChar)0x005B); // [ formatCloseParen.setTo((UChar)0x0029); // ) formatReplaceCloseParen.setTo((UChar)0x005D); // ] } UnicodeString ktPattern; langData.get("localeDisplayPattern", "keyTypePattern", ktPattern); if (ktPattern.isBogus()) { ktPattern = UnicodeString("{0}={1}", -1, US_INV); } keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status); uprv_memset(fCapitalization, 0, sizeof(fCapitalization)); #if !UCONFIG_NO_BREAK_ITERATION // The following is basically copied from DateFormatSymbols::initializeData typedef struct { const char * usageName; LocaleDisplayNamesImpl::CapContextUsage usageEnum; } ContextUsageNameToEnum; const ContextUsageNameToEnum contextUsageTypeMap[] = { // Entries must be sorted by usageTypeName; entry with NULL name terminates list. { "key", kCapContextUsageKey }, { "keyValue", kCapContextUsageKeyValue }, { "languages", kCapContextUsageLanguage }, { "script", kCapContextUsageScript }, { "territory", kCapContextUsageTerritory }, { "variant", kCapContextUsageVariant }, { NULL, (CapContextUsage)0 }, }; // Only get the context data if we need it! This is a const object so we know now... // Also check whether we will need a break iterator (depends on the data) UBool needBrkIter = FALSE; if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) { int32_t len = 0; UResourceBundle *localeBundle = ures_open(NULL, locale.getName(), &status); if (U_SUCCESS(status)) { UResourceBundle *contextTransforms = ures_getByKeyWithFallback(localeBundle, "contextTransforms", NULL, &status); if (U_SUCCESS(status)) { UResourceBundle *contextTransformUsage; while ( (contextTransformUsage = ures_getNextResource(contextTransforms, NULL, &status)) != NULL ) { const int32_t * intVector = ures_getIntVector(contextTransformUsage, &len, &status); if (U_SUCCESS(status) && intVector != NULL && len >= 2) { const char* usageKey = ures_getKey(contextTransformUsage); if (usageKey != NULL) { const ContextUsageNameToEnum * typeMapPtr = contextUsageTypeMap; int32_t compResult = 0; // linear search; list is short and we cannot be sure that bsearch is available while ( typeMapPtr->usageName != NULL && (compResult = uprv_strcmp(usageKey, typeMapPtr->usageName)) > 0 ) { ++typeMapPtr; } if (typeMapPtr->usageName != NULL && compResult == 0) { int32_t titlecaseInt = (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU)? intVector[0]: intVector[1]; if (titlecaseInt != 0) { fCapitalization[typeMapPtr->usageEnum] = TRUE;; needBrkIter = TRUE; } } } } status = U_ZERO_ERROR; ures_close(contextTransformUsage); } ures_close(contextTransforms); } ures_close(localeBundle); } } // Get a sentence break iterator if we will need it if (needBrkIter || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) { status = U_ZERO_ERROR; capitalizationBrkIter = BreakIterator::createSentenceInstance(locale, status); if (U_FAILURE(status)) { delete capitalizationBrkIter; capitalizationBrkIter = NULL; } } #endif }
void PluralFormat::parseType(const UnicodeString& source, const NFRule *rbnfLenientScanner, Formattable& result, FieldPosition& pos) const { // If no pattern was applied, return null. if (msgPattern.countParts() == 0) { pos.setBeginIndex(-1); pos.setEndIndex(-1); return; } int partIndex = 0; int currMatchIndex; int count=msgPattern.countParts(); int startingAt = pos.getBeginIndex(); if (startingAt < 0) { startingAt = 0; } // The keyword is null until we need to match against a non-explicit, not-"other" value. // Then we get the keyword from the selector. // (In other words, we never call the selector if we match against an explicit value, // or if the only non-explicit keyword is "other".) UnicodeString keyword; UnicodeString matchedWord; const UnicodeString& pattern = msgPattern.getPatternString(); int matchedIndex = -1; // Iterate over (ARG_SELECTOR ARG_START message ARG_LIMIT) tuples // until the end of the plural-only pattern. while (partIndex < count) { const MessagePattern::Part* partSelector = &msgPattern.getPart(partIndex++); if (partSelector->getType() != UMSGPAT_PART_TYPE_ARG_SELECTOR) { // Bad format continue; } const MessagePattern::Part* partStart = &msgPattern.getPart(partIndex++); if (partStart->getType() != UMSGPAT_PART_TYPE_MSG_START) { // Bad format continue; } const MessagePattern::Part* partLimit = &msgPattern.getPart(partIndex++); if (partLimit->getType() != UMSGPAT_PART_TYPE_MSG_LIMIT) { // Bad format continue; } UnicodeString currArg = pattern.tempSubString(partStart->getLimit(), partLimit->getIndex() - partStart->getLimit()); if (rbnfLenientScanner != NULL) { // If lenient parsing is turned ON, we've got some time consuming parsing ahead of us. int32_t length = -1; currMatchIndex = rbnfLenientScanner->findTextLenient(source, currArg, startingAt, &length); } else { currMatchIndex = source.indexOf(currArg, startingAt); } if (currMatchIndex >= 0 && currMatchIndex >= matchedIndex && currArg.length() > matchedWord.length()) { matchedIndex = currMatchIndex; matchedWord = currArg; keyword = pattern.tempSubString(partStart->getLimit(), partLimit->getIndex() - partStart->getLimit()); } } if (matchedIndex >= 0) { pos.setBeginIndex(matchedIndex); pos.setEndIndex(matchedIndex + matchedWord.length()); result.setString(keyword); return; } // Not found! pos.setBeginIndex(-1); pos.setEndIndex(-1); }
void DateFormatRoundTripTest::test(DateFormat *fmt, const Locale &origLocale, UBool timeOnly) { UnicodeString pat; if(fmt->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) { errln("DateFormat wasn't a SimpleDateFormat"); return; } UBool isGregorian = FALSE; UErrorCode minStatus = U_ZERO_ERROR; UDate minDate = CalendarTest::minDateOfCalendar(*fmt->getCalendar(), isGregorian, minStatus); if(U_FAILURE(minStatus)) { errln((UnicodeString)"Failure getting min date for " + origLocale.getName()); return; } //logln(UnicodeString("Min date is ") + fullFormat(minDate) + " for " + origLocale.getName()); pat = ((SimpleDateFormat*)fmt)->toPattern(pat); // NOTE TO MAINTAINER // This indexOf check into the pattern needs to be refined to ignore // quoted characters. Currently, this isn't a problem with the locale // patterns we have, but it may be a problem later. UBool hasEra = (pat.indexOf(UnicodeString("G")) != -1); UBool hasZoneDisplayName = (pat.indexOf(UnicodeString("z")) != -1) || (pat.indexOf(UnicodeString("v")) != -1) || (pat.indexOf(UnicodeString("V")) != -1); // Because patterns contain incomplete data representing the Date, // we must be careful of how we do the roundtrip. We start with // a randomly generated Date because they're easier to generate. // From this we get a string. The string is our real starting point, // because this string should parse the same way all the time. Note // that it will not necessarily parse back to the original date because // of incompleteness in patterns. For example, a time-only pattern won't // parse back to the same date. //try { for(int i = 0; i < TRIALS; ++i) { UDate *d = new UDate [DEPTH]; UnicodeString *s = new UnicodeString[DEPTH]; if(isGregorian == TRUE) { d[0] = generateDate(); } else { d[0] = generateDate(minDate); } UErrorCode status = U_ZERO_ERROR; // We go through this loop until we achieve a match or until // the maximum loop count is reached. We record the points at // which the date and the string starts to match. Once matching // starts, it should continue. int loop; int dmatch = 0; // d[dmatch].getTime() == d[dmatch-1].getTime() int smatch = 0; // s[smatch].equals(s[smatch-1]) for(loop = 0; loop < DEPTH; ++loop) { if (loop > 0) { d[loop] = fmt->parse(s[loop-1], status); failure(status, "fmt->parse", s[loop-1]+" in locale: " + origLocale.getName() + " with pattern: " + pat); status = U_ZERO_ERROR; /* any error would have been reported */ } s[loop] = fmt->format(d[loop], s[loop]); // For displaying which date is being tested //logln(s[loop] + " = " + fullFormat(d[loop])); if(s[loop].length() == 0) { errln("FAIL: fmt->format gave 0-length string in " + pat + " with number " + d[loop] + " in locale " + origLocale.getName()); } if(loop > 0) { if(smatch == 0) { UBool match = s[loop] == s[loop-1]; if(smatch == 0) { if(match) smatch = loop; } else if( ! match) errln("FAIL: String mismatch after match"); } if(dmatch == 0) { // {sfb} watch out here, this might not work UBool match = d[loop]/*.getTime()*/ == d[loop-1]/*.getTime()*/; if(dmatch == 0) { if(match) dmatch = loop; } else if( ! match) errln("FAIL: Date mismatch after match"); } if(smatch != 0 && dmatch != 0) break; } } // At this point loop == DEPTH if we've failed, otherwise loop is the // max(smatch, dmatch), that is, the index at which we have string and // date matching. // Date usually matches in 2. Exceptions handled below. int maxDmatch = 2; int maxSmatch = 1; if (dmatch > maxDmatch) { // Time-only pattern with zone information and a starting date in PST. if(timeOnly && hasZoneDisplayName) { int32_t startRaw, startDst; fmt->getTimeZone().getOffset(d[0], FALSE, startRaw, startDst, status); failure(status, "TimeZone::getOffset"); // if the start offset is greater than the offset on Jan 1, 1970 // in PST, then need one more round trip. There are two cases // fall into this category. The start date is 1) DST or // 2) LMT (GMT-07:52:58). if (startRaw + startDst > -28800000) { maxDmatch = 3; maxSmatch = 2; } } } // String usually matches in 1. Exceptions are checked for here. if(smatch > maxSmatch) { // Don't compute unless necessary UBool in0; // Starts in BC, with no era in pattern if( ! hasEra && getField(d[0], UCAL_ERA) == GregorianCalendar::BC) maxSmatch = 2; // Starts in DST, no year in pattern else if((in0=fmt->getTimeZone().inDaylightTime(d[0], status)) && ! failure(status, "gettingDaylightTime") && pat.indexOf(UnicodeString("yyyy")) == -1) maxSmatch = 2; // If we start not in DST, but transition into DST else if (!in0 && fmt->getTimeZone().inDaylightTime(d[1], status) && !failure(status, "gettingDaylightTime")) maxSmatch = 2; // Two digit year with no time zone change, // unless timezone isn't used or we aren't close to the DST changover else if (pat.indexOf(UnicodeString("y")) != -1 && pat.indexOf(UnicodeString("yyyy")) == -1 && getField(d[0], UCAL_YEAR) != getField(d[dmatch], UCAL_YEAR) && !failure(status, "error status [smatch>maxSmatch]") && ((hasZoneDisplayName && (fmt->getTimeZone().inDaylightTime(d[0], status) == fmt->getTimeZone().inDaylightTime(d[dmatch], status) || getField(d[0], UCAL_MONTH) == UCAL_APRIL || getField(d[0], UCAL_MONTH) == UCAL_OCTOBER)) || !hasZoneDisplayName) ) { maxSmatch = 2; } // If zone display name is used, fallback format might be used before 1970 else if (hasZoneDisplayName && d[0] < 0) { maxSmatch = 2; } } /* * Special case for Japanese and Buddhist (could have large negative years) * Also, Hebrew calendar need help handling leap month. */ if(dmatch > maxDmatch || smatch > maxSmatch) { const char *type = fmt->getCalendar()->getType(); if(!strcmp(type,"japanese") || (!strcmp(type,"buddhist"))) { maxSmatch = 4; maxDmatch = 4; } else if(!strcmp(type,"hebrew")) { maxSmatch = 3; maxDmatch = 3; } } // Use @v to see verbose results on successful cases UBool fail = (dmatch > maxDmatch || smatch > maxSmatch); if (optionv || fail) { if (fail) { errln(UnicodeString("\nFAIL: Pattern: ") + pat + " in Locale: " + origLocale.getName()); } else { errln(UnicodeString("\nOk: Pattern: ") + pat + " in Locale: " + origLocale.getName()); } logln("Date iters until match=%d (max allowed=%d), string iters until match=%d (max allowed=%d)", dmatch,maxDmatch, smatch, maxSmatch); for(int j = 0; j <= loop && j < DEPTH; ++j) { UnicodeString temp; FieldPosition pos(FieldPosition::DONT_CARE); errln((j>0?" P> ":" ") + fullFormat(d[j]) + " F> " + escape(s[j], temp) + UnicodeString(" d=") + d[j] + (j > 0 && d[j]/*.getTime()*/==d[j-1]/*.getTime()*/?" d==":"") + (j > 0 && s[j] == s[j-1]?" s==":"")); } } delete[] d; delete[] s; } /*} catch (ParseException e) { errln("Exception: " + e.getMessage()); logln(e.toString()); }*/ }
UXMLElement * UXMLParser::parseFile(const char *filename, UErrorCode &errorCode) { char bytes[4096], charsetBuffer[100]; FileStream *f; const char *charset, *pb; UnicodeString src; UConverter *cnv; UChar *buffer, *pu; int32_t fileLength, bytesLength, length, capacity; UBool flush; if(U_FAILURE(errorCode)) { return NULL; } f=T_FileStream_open(filename, "rb"); if(f==NULL) { errorCode=U_FILE_ACCESS_ERROR; return NULL; } bytesLength=T_FileStream_read(f, bytes, (int32_t)sizeof(bytes)); if(bytesLength<(int32_t)sizeof(bytes)) { // we have already read the entire file fileLength=bytesLength; } else { // get the file length fileLength=T_FileStream_size(f); } /* * get the charset: * 1. Unicode signature * 2. treat as ISO-8859-1 and read XML encoding="charser" * 3. default to UTF-8 */ charset=ucnv_detectUnicodeSignature(bytes, bytesLength, NULL, &errorCode); if(U_SUCCESS(errorCode) && charset!=NULL) { // open converter according to Unicode signature cnv=ucnv_open(charset, &errorCode); } else { // read as Latin-1 and parse the XML declaration and encoding cnv=ucnv_open("ISO-8859-1", &errorCode); if(U_FAILURE(errorCode)) { // unexpected error opening Latin-1 converter goto exit; } buffer=src.getBuffer(bytesLength); if(buffer==NULL) { // unexpected failure to reserve some string capacity errorCode=U_MEMORY_ALLOCATION_ERROR; goto exit; } pb=bytes; pu=buffer; ucnv_toUnicode( cnv, &pu, buffer+src.getCapacity(), &pb, bytes+bytesLength, NULL, TRUE, &errorCode); src.releaseBuffer(U_SUCCESS(errorCode) ? (int32_t)(pu-buffer) : 0); ucnv_close(cnv); cnv=NULL; if(U_FAILURE(errorCode)) { // unexpected error in conversion from Latin-1 src.remove(); goto exit; } // parse XML declaration if(mXMLDecl.reset(src).lookingAt(0, errorCode)) { int32_t declEnd=mXMLDecl.end(errorCode); // go beyond <?xml int32_t pos=src.indexOf((UChar)x_l)+1; mAttrValue.reset(src); while(pos<declEnd && mAttrValue.lookingAt(pos, errorCode)) { // loop runs once per attribute on this element. UnicodeString attName = mAttrValue.group(1, errorCode); UnicodeString attValue = mAttrValue.group(2, errorCode); // Trim the quotes from the att value. These are left over from the original regex // that parsed the attribue, which couldn't conveniently strip them. attValue.remove(0,1); // one char from the beginning attValue.truncate(attValue.length()-1); // and one from the end. if(attName==UNICODE_STRING("encoding", 8)) { length=attValue.extract(0, 0x7fffffff, charsetBuffer, (int32_t)sizeof(charsetBuffer)); charset=charsetBuffer; break; } pos = mAttrValue.end(2, errorCode); } if(charset==NULL) { // default to UTF-8 charset="UTF-8"; } cnv=ucnv_open(charset, &errorCode); } } if(U_FAILURE(errorCode)) { // unable to open the converter goto exit; } // convert the file contents capacity=fileLength; // estimated capacity src.getBuffer(capacity); src.releaseBuffer(0); // zero length flush=FALSE; for(;;) { // convert contents of bytes[bytesLength] pb=bytes; for(;;) { length=src.length(); buffer=src.getBuffer(capacity); if(buffer==NULL) { // unexpected failure to reserve some string capacity errorCode=U_MEMORY_ALLOCATION_ERROR; goto exit; } pu=buffer+length; ucnv_toUnicode( cnv, &pu, buffer+src.getCapacity(), &pb, bytes+bytesLength, NULL, FALSE, &errorCode); src.releaseBuffer(U_SUCCESS(errorCode) ? (int32_t)(pu-buffer) : 0); if(errorCode==U_BUFFER_OVERFLOW_ERROR) { errorCode=U_ZERO_ERROR; capacity=(3*src.getCapacity())/2; // increase capacity by 50% } else { break; } } if(U_FAILURE(errorCode)) { break; // conversion error } if(flush) { break; // completely converted the file } // read next block bytesLength=T_FileStream_read(f, bytes, (int32_t)sizeof(bytes)); if(bytesLength==0) { // reached end of file, convert once more to flush the converter flush=TRUE; } }; exit: ucnv_close(cnv); T_FileStream_close(f); if(U_SUCCESS(errorCode)) { return parse(src, errorCode); } else { return NULL; } }
NumberFormat* NumberFormat::makeInstance(const Locale& desiredLocale, UNumberFormatStyle style, UBool mustBeDecimalFormat, UErrorCode& status) { if (U_FAILURE(status)) return NULL; if (style < 0 || style >= UNUM_FORMAT_STYLE_COUNT) { status = U_ILLEGAL_ARGUMENT_ERROR; return NULL; } // Some styles are not supported. This is a result of merging // the @draft ICU 4.2 NumberFormat::EStyles into the long-existing UNumberFormatStyle. // Ticket #8503 is for reviewing/fixing/merging the two relevant implementations: // this one and unum_open(). // The UNUM_PATTERN_ styles are not supported here // because this method does not take a pattern string. if (!isStyleSupported(style)) { status = U_UNSUPPORTED_ERROR; return NULL; } #if U_PLATFORM_USES_ONLY_WIN32_API if (!mustBeDecimalFormat) { char buffer[8]; int32_t count = desiredLocale.getKeywordValue("compat", buffer, sizeof(buffer), status); // if the locale has "@compat=host", create a host-specific NumberFormat if (U_SUCCESS(status) && count > 0 && uprv_strcmp(buffer, "host") == 0) { Win32NumberFormat *f = NULL; UBool curr = TRUE; switch (style) { case UNUM_DECIMAL: curr = FALSE; // fall-through case UNUM_CURRENCY: case UNUM_CURRENCY_ISO: // do not support plural formatting here case UNUM_CURRENCY_PLURAL: f = new Win32NumberFormat(desiredLocale, curr, status); if (U_SUCCESS(status)) { return f; } delete f; break; default: break; } } } #endif // Use numbering system cache hashtable umtx_initOnce(gNSCacheInitOnce, &nscacheInit); // Get cached numbering system LocalPointer<NumberingSystem> ownedNs; NumberingSystem *ns = NULL; if (NumberingSystem_cache != NULL) { // TODO: Bad hash key usage, see ticket #8504. int32_t hashKey = desiredLocale.hashCode(); Mutex lock(&nscacheMutex); ns = (NumberingSystem *)uhash_iget(NumberingSystem_cache, hashKey); if (ns == NULL) { ns = NumberingSystem::createInstance(desiredLocale,status); uhash_iput(NumberingSystem_cache, hashKey, (void*)ns, &status); } } else { ownedNs.adoptInstead(NumberingSystem::createInstance(desiredLocale,status)); ns = ownedNs.getAlias(); } // check results of getting a numbering system if (U_FAILURE(status)) { return NULL; } if (mustBeDecimalFormat && ns->isAlgorithmic()) { status = U_UNSUPPORTED_ERROR; return NULL; } LocalPointer<DecimalFormatSymbols> symbolsToAdopt; UnicodeString pattern; LocalUResourceBundlePointer ownedResource(ures_open(NULL, desiredLocale.getName(), &status)); if (U_FAILURE(status)) { // We don't appear to have resource data available -- use the last-resort data status = U_USING_FALLBACK_WARNING; // When the data is unavailable, and locale isn't passed in, last resort data is used. symbolsToAdopt.adoptInstead(new DecimalFormatSymbols(status)); if (symbolsToAdopt.isNull()) { status = U_MEMORY_ALLOCATION_ERROR; return NULL; } // Creates a DecimalFormat instance with the last resort number patterns. pattern.setTo(TRUE, gLastResortNumberPatterns[style], -1); } else { // Loads the decimal symbols of the desired locale. symbolsToAdopt.adoptInstead(new DecimalFormatSymbols(desiredLocale, status)); if (symbolsToAdopt.isNull()) { status = U_MEMORY_ALLOCATION_ERROR; return NULL; } UResourceBundle *resource = ownedResource.orphan(); UResourceBundle *numElements = ures_getByKeyWithFallback(resource, gNumberElements, NULL, &status); resource = ures_getByKeyWithFallback(numElements, ns->getName(), resource, &status); resource = ures_getByKeyWithFallback(resource, gPatterns, resource, &status); ownedResource.adoptInstead(resource); int32_t patLen = 0; const UChar *patResStr = ures_getStringByKeyWithFallback(resource, gFormatKeys[style], &patLen, &status); // Didn't find a pattern specific to the numbering system, so fall back to "latn" if ( status == U_MISSING_RESOURCE_ERROR && uprv_strcmp(gLatn,ns->getName())) { status = U_ZERO_ERROR; resource = ures_getByKeyWithFallback(numElements, gLatn, resource, &status); resource = ures_getByKeyWithFallback(resource, gPatterns, resource, &status); patResStr = ures_getStringByKeyWithFallback(resource, gFormatKeys[style], &patLen, &status); } ures_close(numElements); // Creates the specified decimal format style of the desired locale. pattern.setTo(TRUE, patResStr, patLen); } if (U_FAILURE(status)) { return NULL; } if(style==UNUM_CURRENCY || style == UNUM_CURRENCY_ISO){ const UChar* currPattern = symbolsToAdopt->getCurrencyPattern(); if(currPattern!=NULL){ pattern.setTo(currPattern, u_strlen(currPattern)); } } NumberFormat *f; if (ns->isAlgorithmic()) { UnicodeString nsDesc; UnicodeString nsRuleSetGroup; UnicodeString nsRuleSetName; Locale nsLoc; URBNFRuleSetTag desiredRulesType = URBNF_NUMBERING_SYSTEM; nsDesc.setTo(ns->getDescription()); int32_t firstSlash = nsDesc.indexOf(gSlash); int32_t lastSlash = nsDesc.lastIndexOf(gSlash); if ( lastSlash > firstSlash ) { CharString nsLocID; nsLocID.appendInvariantChars(nsDesc.tempSubString(0, firstSlash), status); nsRuleSetGroup.setTo(nsDesc,firstSlash+1,lastSlash-firstSlash-1); nsRuleSetName.setTo(nsDesc,lastSlash+1); nsLoc = Locale::createFromName(nsLocID.data()); UnicodeString SpelloutRules = UNICODE_STRING_SIMPLE("SpelloutRules"); if ( nsRuleSetGroup.compare(SpelloutRules) == 0 ) { desiredRulesType = URBNF_SPELLOUT; } } else { nsLoc = desiredLocale; nsRuleSetName.setTo(nsDesc); } RuleBasedNumberFormat *r = new RuleBasedNumberFormat(desiredRulesType,nsLoc,status); if (r == NULL) { status = U_MEMORY_ALLOCATION_ERROR; return NULL; } r->setDefaultRuleSet(nsRuleSetName,status); f = r; } else { // replace single currency sign in the pattern with double currency sign // if the style is UNUM_CURRENCY_ISO if (style == UNUM_CURRENCY_ISO) { pattern.findAndReplace(UnicodeString(TRUE, gSingleCurrencySign, 1), UnicodeString(TRUE, gDoubleCurrencySign, 2)); } // "new DecimalFormat()" does not adopt the symbols if its memory allocation fails. DecimalFormatSymbols *syms = symbolsToAdopt.orphan(); f = new DecimalFormat(pattern, syms, style, status); if (f == NULL) { delete syms; status = U_MEMORY_ALLOCATION_ERROR; return NULL; } } f->setLocaleIDs(ures_getLocaleByType(ownedResource.getAlias(), ULOC_VALID_LOCALE, &status), ures_getLocaleByType(ownedResource.getAlias(), ULOC_ACTUAL_LOCALE, &status)); if (U_FAILURE(status)) { delete f; return NULL; } return f; }
void LocaleDisplayNamesImpl::initialize(void) { LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this; nonConstThis->locale = langData.getLocale() == Locale::getRoot() ? regionData.getLocale() : langData.getLocale(); UnicodeString sep; langData.getNoFallback("localeDisplayPattern", "separator", sep); if (sep.isBogus()) { sep = UnicodeString("{0}, {1}", -1, US_INV); } UErrorCode status = U_ZERO_ERROR; separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status); UnicodeString pattern; langData.getNoFallback("localeDisplayPattern", "pattern", pattern); if (pattern.isBogus()) { pattern = UnicodeString("{0} ({1})", -1, US_INV); } format.applyPatternMinMaxArguments(pattern, 2, 2, status); if (pattern.indexOf((UChar)0xFF08) >= 0) { formatOpenParen.setTo((UChar)0xFF08); // fullwidth ( formatReplaceOpenParen.setTo((UChar)0xFF3B); // fullwidth [ formatCloseParen.setTo((UChar)0xFF09); // fullwidth ) formatReplaceCloseParen.setTo((UChar)0xFF3D); // fullwidth ] } else { formatOpenParen.setTo((UChar)0x0028); // ( formatReplaceOpenParen.setTo((UChar)0x005B); // [ formatCloseParen.setTo((UChar)0x0029); // ) formatReplaceCloseParen.setTo((UChar)0x005D); // ] } UnicodeString ktPattern; langData.get("localeDisplayPattern", "keyTypePattern", ktPattern); if (ktPattern.isBogus()) { ktPattern = UnicodeString("{0}={1}", -1, US_INV); } keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status); uprv_memset(fCapitalization, 0, sizeof(fCapitalization)); #if !UCONFIG_NO_BREAK_ITERATION // Only get the context data if we need it! This is a const object so we know now... // Also check whether we will need a break iterator (depends on the data) UBool needBrkIter = FALSE; if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) { LocalUResourceBundlePointer resource(ures_open(NULL, locale.getName(), &status)); if (U_FAILURE(status)) { return; } CapitalizationContextSink sink(*this); ures_getAllItemsWithFallback(resource.getAlias(), "contextTransforms", sink, status); if (status == U_MISSING_RESOURCE_ERROR) { // Silently ignore. Not every locale has contextTransforms. status = U_ZERO_ERROR; } else if (U_FAILURE(status)) { return; } needBrkIter = sink.hasCapitalizationUsage; } // Get a sentence break iterator if we will need it if (needBrkIter || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) { status = U_ZERO_ERROR; capitalizationBrkIter = BreakIterator::createSentenceInstance(locale, status); if (U_FAILURE(status)) { delete capitalizationBrkIter; capitalizationBrkIter = NULL; } } #endif }
void ChatManagerImplementation::broadcastMessage(CreatureObject* player, const UnicodeString& message, uint64 target, uint32 moodid, uint32 mood2) { Zone* zone = player->getZone(); PlayerObject* myGhost = NULL; bool godMode = false; if (zone == NULL) return; int language = 0; String firstName; if (player->isPlayerCreature() /*|| !((Player *)player)->isChatMuted() */) { CreatureObject* playerCreature = cast<CreatureObject*>(player); if (playerCreature) { firstName = playerCreature->getFirstName().toLowerCase(); myGhost = playerCreature->getPlayerObject(); } if (myGhost) language = myGhost->getLanguageID(); } if (myGhost) { if (myGhost->hasGodMode()) godMode = true; } StringIdChatParameter* param = NULL; if (message[0] == '@' && message.indexOf(":") != -1) { param = new StringIdChatParameter(message.toString()); } CloseObjectsVector* closeObjects = (CloseObjectsVector*) player->getCloseObjects(); SortedVector<QuadTreeEntry*> closeEntryObjects(200, 50); if (closeObjects != NULL) { closeObjects->safeCopyTo(closeEntryObjects); } else { player->info("Null closeobjects vector in ChatManager::broadcastMessage", true); zone->getInRangeObjects(player->getWorldPositionX(), player->getWorldPositionY(), 128, &closeEntryObjects, true); } float range = defaultSpatialChatDistance; float specialRange = spatialChatDistances.get(mood2); if (specialRange != -1) { range = specialRange; } try { for (int i = 0; i < closeEntryObjects.size(); ++i) { SceneObject* object = cast<SceneObject*>(closeEntryObjects.get(i)); if (player->isInRange(object, range)) { //Notify observers that are expecting spatial chat. if (object->getObserverCount(ObserverEventType::SPATIALCHATRECEIVED)) { ManagedReference<ChatMessage*> chatMessage = new ChatMessage(); chatMessage->setString(message.toString()); EXECUTE_TASK_3(object, chatMessage, player, { if (player_p == NULL || object_p == NULL) return; Locker locker(object_p); SortedVector<ManagedReference<Observer*> > observers = object_p->getObservers(ObserverEventType::SPATIALCHATRECEIVED); for (int oc = 0; oc < observers.size(); oc++) { Observer* observer = observers.get(oc); Locker clocker(observer, object_p); if (observer->notifyObserverEvent(ObserverEventType::SPATIALCHATRECEIVED, object_p, chatMessage_p, player_p->getObjectID()) == 1) object_p->dropObserver(ObserverEventType::SPATIALCHATRECEIVED, observer); } }); } if (object->isPlayerCreature()) { CreatureObject* creature = cast<CreatureObject*>(object); PlayerObject* ghost = creature->getPlayerObject(); if (ghost == NULL) continue; if (!ghost->isIgnoring(firstName) || godMode) { SpatialChat* cmsg = NULL; if (param == NULL) { cmsg = new SpatialChat(player->getObjectID(), creature->getObjectID(), message, target, moodid, mood2, language); } else { cmsg = new SpatialChat(player->getObjectID(), creature->getObjectID(), *param, target, moodid, mood2); } creature->sendMessage(cmsg); } } else if( object->isPet() ){ AiAgent* pet = cast<AiAgent*>(object); if (pet == NULL ) continue; if( pet->isDead() || pet->isIncapacitated() ) continue; PetManager* petManager = server->getPetManager(); Locker clocker(pet, player); petManager->handleChat( player, pet, message.toString() ); } } }
NumberFormat* NumberFormat::makeInstance(const Locale& desiredLocale, EStyles style, UErrorCode& status) { if (U_FAILURE(status)) return NULL; if (style < 0 || style >= kStyleCount) { status = U_ILLEGAL_ARGUMENT_ERROR; return NULL; } #ifdef U_WINDOWS char buffer[8]; int32_t count = desiredLocale.getKeywordValue("compat", buffer, sizeof(buffer), status); // if the locale has "@compat=host", create a host-specific NumberFormat if (count > 0 && uprv_strcmp(buffer, "host") == 0) { Win32NumberFormat *f = NULL; UBool curr = TRUE; switch (style) { case kNumberStyle: curr = FALSE; // fall-through case kCurrencyStyle: case kIsoCurrencyStyle: // do not support plural formatting here case kPluralCurrencyStyle: f = new Win32NumberFormat(desiredLocale, curr, status); if (U_SUCCESS(status)) { return f; } delete f; break; default: break; } } #endif NumberFormat* f = NULL; DecimalFormatSymbols* symbolsToAdopt = NULL; UnicodeString pattern; UResourceBundle *resource = ures_open((char *)0, desiredLocale.getName(), &status); UResourceBundle *numberPatterns = ures_getByKey(resource, DecimalFormat::fgNumberPatterns, NULL, &status); NumberingSystem *ns = NULL; UBool deleteSymbols = TRUE; UHashtable * cache = NULL; int32_t hashKey; UBool getCache = FALSE; UBool deleteNS = FALSE; if (U_FAILURE(status)) { // We don't appear to have resource data available -- use the last-resort data status = U_USING_FALLBACK_WARNING; // When the data is unavailable, and locale isn't passed in, last resort data is used. symbolsToAdopt = new DecimalFormatSymbols(status); // Creates a DecimalFormat instance with the last resort number patterns. pattern.setTo(TRUE, gLastResortNumberPatterns[style], -1); } else { // If not all the styled patterns exists for the NumberFormat in this locale, // sets the status code to failure and returns nil. if (ures_getSize(numberPatterns) < (int32_t)(sizeof(gLastResortNumberPatterns)/sizeof(gLastResortNumberPatterns[0])) -2 ) { //minus 2: ISO and plural status = U_INVALID_FORMAT_ERROR; goto cleanup; } // Loads the decimal symbols of the desired locale. symbolsToAdopt = new DecimalFormatSymbols(desiredLocale, status); int32_t patLen = 0; /* for ISOCURRENCYSTYLE and PLURALCURRENCYSTYLE, * the pattern is the same as the pattern of CURRENCYSTYLE * but by replacing the single currency sign with * double currency sign or triple currency sign. */ int styleInNumberPattern = ((style == kIsoCurrencyStyle || style == kPluralCurrencyStyle) ? kCurrencyStyle : style); const UChar *patResStr = ures_getStringByIndex(numberPatterns, (int32_t)styleInNumberPattern, &patLen, &status); // Creates the specified decimal format style of the desired locale. pattern.setTo(TRUE, patResStr, patLen); } if (U_FAILURE(status) || symbolsToAdopt == NULL) { goto cleanup; } if(style==kCurrencyStyle || style == kIsoCurrencyStyle){ const UChar* currPattern = symbolsToAdopt->getCurrencyPattern(); if(currPattern!=NULL){ pattern.setTo(currPattern, u_strlen(currPattern)); } } // Use numbering system cache hashtable UMTX_CHECK(&nscacheMutex, (UBool)(cache != NumberingSystem_cache), getCache); if (getCache) { umtx_lock(&nscacheMutex); cache = NumberingSystem_cache; umtx_unlock(&nscacheMutex); } // Check cache we got, create if non-existant status = U_ZERO_ERROR; if (cache == NULL) { cache = uhash_open(uhash_hashLong, uhash_compareLong, NULL, &status); if (cache == NULL || U_FAILURE(status)) { // cache not created - out of memory cache = NULL; } else { // cache created uhash_setValueDeleter(cache, deleteNumberingSystem); // set final NumberingSystem_cache value UHashtable* h = NULL; UMTX_CHECK(&nscacheMutex, (UBool)(h != NumberingSystem_cache), getCache); if (getCache) { umtx_lock(&nscacheMutex); h = NumberingSystem_cache; umtx_unlock(&nscacheMutex); } if (h == NULL) { umtx_lock(&nscacheMutex); NumberingSystem_cache = h = cache; cache = NULL; ucln_i18n_registerCleanup(UCLN_I18N_NUMFMT, numfmt_cleanup); umtx_unlock(&nscacheMutex); } if(cache != NULL) { uhash_close(cache); } cache = h; } } // Get cached numbering system if (cache != NULL) { hashKey = desiredLocale.hashCode(); umtx_lock(&nscacheMutex); ns = (NumberingSystem *)uhash_iget(cache, hashKey); if (ns == NULL) { ns = NumberingSystem::createInstance(desiredLocale,status); uhash_iput(cache, hashKey, (void*)ns, &status); } umtx_unlock(&nscacheMutex); } else { ns = NumberingSystem::createInstance(desiredLocale,status); deleteNS = TRUE; } // check results of getting a numbering system if ((ns == NULL) || (U_FAILURE(status))) { goto cleanup; } if (ns->isAlgorithmic()) { UnicodeString nsDesc; UnicodeString nsRuleSetGroup; UnicodeString nsRuleSetName; Locale nsLoc; URBNFRuleSetTag desiredRulesType = URBNF_NUMBERING_SYSTEM; nsDesc.setTo(ns->getDescription()); int32_t firstSlash = nsDesc.indexOf(gSlash); int32_t lastSlash = nsDesc.lastIndexOf(gSlash); if ( lastSlash > firstSlash ) { char nsLocID[ULOC_FULLNAME_CAPACITY]; nsDesc.extract(0,firstSlash,nsLocID,ULOC_FULLNAME_CAPACITY,US_INV); nsRuleSetGroup.setTo(nsDesc,firstSlash+1,lastSlash-firstSlash-1); nsRuleSetName.setTo(nsDesc,lastSlash+1); nsLoc = Locale::createFromName(nsLocID); UnicodeString SpelloutRules = UNICODE_STRING_SIMPLE("SpelloutRules"); if ( nsRuleSetGroup.compare(SpelloutRules) == 0 ) { desiredRulesType = URBNF_SPELLOUT; } } else { nsLoc = desiredLocale; nsRuleSetName.setTo(nsDesc); } RuleBasedNumberFormat *r = new RuleBasedNumberFormat(desiredRulesType,nsLoc,status); if (U_FAILURE(status) || r == NULL) { goto cleanup; } r->setDefaultRuleSet(nsRuleSetName,status); f = (NumberFormat *) r; } else { // replace single currency sign in the pattern with double currency sign // if the style is kIsoCurrencyStyle if (style == kIsoCurrencyStyle) { pattern.findAndReplace(gSingleCurrencySign, gDoubleCurrencySign); } f = new DecimalFormat(pattern, symbolsToAdopt, style, status); if (U_FAILURE(status) || f == NULL) { goto cleanup; } deleteSymbols = FALSE; } f->setLocaleIDs(ures_getLocaleByType(numberPatterns, ULOC_VALID_LOCALE, &status), ures_getLocaleByType(numberPatterns, ULOC_ACTUAL_LOCALE, &status)); cleanup: ures_close(numberPatterns); ures_close(resource); if (deleteNS && ns) { delete ns; } if (U_FAILURE(status)) { /* If f exists, then it will delete the symbols */ if (f==NULL) { delete symbolsToAdopt; } else { delete f; } return NULL; } if (f == NULL || symbolsToAdopt == NULL) { status = U_MEMORY_ALLOCATION_ERROR; f = NULL; } if (deleteSymbols && symbolsToAdopt != NULL) { delete symbolsToAdopt; } return f; }
bool has_line_breaks() const { UChar break_char = '\n'; return (text_.indexOf(break_char) >= 0); }
seec::Maybe<IndexedString> IndexedString::from(UnicodeString const &String) { if (String.isBogus()) return seec::Maybe<IndexedString>(); UnicodeString const NeedleStart("@["); UnicodeString const NeedleEscape("@[["); UnicodeString const NeedleEnd("]"); UnicodeString CleanedString; // String with index indicators removed. std::multimap<UnicodeString, Needle> Needles; std::vector<std::pair<UnicodeString, int32_t>> IndexStack; int32_t SearchFrom = 0; // Current offset in String. int32_t FoundStart; // Position of matched index indicator. while ((FoundStart = String.indexOf(NeedleStart, SearchFrom)) != -1) { // Copy all the literal string data. CleanedString.append(String, SearchFrom, FoundStart - SearchFrom); // Check if this is an escape sequence. if (String.compare(FoundStart, NeedleEscape.length(), NeedleEscape) == 0) { CleanedString.append(NeedleStart); SearchFrom = FoundStart + NeedleEscape.length(); continue; } // Find the end of this sequence. int32_t FoundEnd = String.indexOf(NeedleEnd, SearchFrom); if (FoundEnd == -1) return seec::Maybe<IndexedString>(); if (FoundEnd == FoundStart + NeedleStart.length()) { // This is a closing sequence. if (IndexStack.size() == 0) return seec::Maybe<IndexedString>(); // Pop the starting details of the last-opened sequence. auto const Start = IndexStack.back(); IndexStack.pop_back(); // Store the needle for this sequence. Needles.insert(std::make_pair(Start.first, Needle(Start.second, CleanedString.countChar32()))); } else { // This is an opening sequence. int32_t const NameStart = FoundStart + NeedleStart.length(); int32_t const NameLength = FoundEnd - NameStart; IndexStack.emplace_back(UnicodeString(String, NameStart, NameLength), CleanedString.countChar32()); } SearchFrom = FoundEnd + NeedleEnd.length(); } // Copy all remaining literal data. CleanedString.append(String, SearchFrom, String.length() - SearchFrom); return IndexedString(std::move(CleanedString), std::move(Needles)); }
// testConfData - Check each data item from the Unicode confusables.txt file, // verify that it transforms correctly in a skeleton. // void IntlTestSpoof::testConfData() { UErrorCode status = U_ZERO_ERROR; const char *testDataDir = IntlTest::getSourceTestData(status); TEST_ASSERT_SUCCESS(status); char buffer[2000]; uprv_strcpy(buffer, testDataDir); uprv_strcat(buffer, "confusables.txt"); LocalStdioFilePointer f(fopen(buffer, "rb")); if (f.isNull()) { errln("Skipping test spoof/testConfData. File confusables.txt not accessible."); return; } fseek(f.getAlias(), 0, SEEK_END); int32_t fileSize = ftell(f.getAlias()); LocalArray<char> fileBuf(new char[fileSize]); fseek(f.getAlias(), 0, SEEK_SET); int32_t amt_read = fread(fileBuf.getAlias(), 1, fileSize, f.getAlias()); TEST_ASSERT_EQ(amt_read, fileSize); TEST_ASSERT(fileSize>0); if (amt_read != fileSize || fileSize <=0) { return; } UnicodeString confusablesTxt = UnicodeString::fromUTF8(StringPiece(fileBuf.getAlias(), fileSize)); LocalUSpoofCheckerPointer sc(uspoof_open(&status)); TEST_ASSERT_SUCCESS(status); // Parse lines from the confusables.txt file. Example Line: // FF44 ; 0064 ; SL # ( d -> d ) FULLWIDTH .... // Three fields. The hex fields can contain more than one character, // and each character may be more than 4 digits (for supplemntals) // This regular expression matches lines and splits the fields into capture groups. RegexMatcher parseLine("(?m)^([0-9A-F]{4}[^#;]*?);([^#;]*?);([^#]*)", confusablesTxt, 0, status); TEST_ASSERT_SUCCESS(status); while (parseLine.find()) { UnicodeString from = parseHex(parseLine.group(1, status)); if (!Normalizer::isNormalized(from, UNORM_NFD, status)) { // The source character was not NFD. // Skip this case; the first step in obtaining a skeleton is to NFD the input, // so the mapping in this line of confusables.txt will never be applied. continue; } UnicodeString rawExpected = parseHex(parseLine.group(2, status)); UnicodeString expected; Normalizer::decompose(rawExpected, FALSE /*NFD*/, 0, expected, status); TEST_ASSERT_SUCCESS(status); int32_t skeletonType = 0; UnicodeString tableType = parseLine.group(3, status); TEST_ASSERT_SUCCESS(status); if (tableType.indexOf("SL") >= 0) { skeletonType = USPOOF_SINGLE_SCRIPT_CONFUSABLE; } else if (tableType.indexOf("SA") >= 0) { skeletonType = USPOOF_SINGLE_SCRIPT_CONFUSABLE | USPOOF_ANY_CASE; } else if (tableType.indexOf("ML") >= 0) { skeletonType = 0; } else if (tableType.indexOf("MA") >= 0) { skeletonType = USPOOF_ANY_CASE; } UnicodeString actual; uspoof_getSkeletonUnicodeString(sc.getAlias(), skeletonType, from, actual, &status); TEST_ASSERT_SUCCESS(status); TEST_ASSERT(actual == expected); if (actual != expected) { errln(parseLine.group(0, status)); UnicodeString line = "Actual: "; int i = 0; while (i < actual.length()) { appendHexUChar(line, actual.char32At(i)); i = actual.moveIndex32(i, 1); } errln(line); } if (U_FAILURE(status)) { break; } } }