U_INTERNAL int32_t U_EXPORT2 unum_formatUFormattable(const UNumberFormat* fmt, const UFormattable *number, UChar *result, int32_t resultLength, UFieldPosition *pos, /* ignored if 0 */ UErrorCode *status) { if (U_FAILURE(*status)) { return 0; } if (fmt == NULL || number==NULL || (result==NULL ? resultLength!=0 : resultLength<0)) { *status = U_ILLEGAL_ARGUMENT_ERROR; return 0; } UnicodeString res(result, 0, resultLength); FieldPosition fp; if(pos != 0) fp.setField(pos->field); ((const NumberFormat*)fmt)->format(*(Formattable::fromUFormattable(number)), res, fp, *status); if(pos != 0) { pos->beginIndex = fp.getBeginIndex(); pos->endIndex = fp.getEndIndex(); } return res.extract(result, resultLength, *status); }
U_CAPI int32_t U_EXPORT2 udat_format( const UDateFormat* format, UDate dateToFormat, UChar* result, int32_t resultLength, UFieldPosition* position, UErrorCode* status) { if(U_FAILURE(*status)) return -1; UnicodeString res; if(!(result==NULL && resultLength==0)) { // NULL destination for pure preflighting: empty dummy string // otherwise, alias the destination buffer res.setTo(result, 0, resultLength); } FieldPosition fp; if(position != 0) fp.setField(position->field); ((DateFormat*)format)->format(dateToFormat, res, fp); if(position != 0) { position->beginIndex = fp.getBeginIndex(); position->endIndex = fp.getEndIndex(); } return res.extract(result, resultLength, *status); }
static jcharArray formatResult(JNIEnv* env, const UnicodeString& s, FieldPositionIterator* fpi, jobject javaFieldPositionIterator) { static jmethodID gFPI_setData = env->GetMethodID(JniConstants::fieldPositionIteratorClass, "setData", "([I)V"); if (fpi != NULL) { std::vector<int32_t> data; FieldPosition fp; while (fpi->next(fp)) { data.push_back(fp.getField()); data.push_back(fp.getBeginIndex()); data.push_back(fp.getEndIndex()); } jintArray javaData = NULL; if (!data.empty()) { javaData = env->NewIntArray(data.size()); if (javaData == NULL) { return NULL; } ScopedIntArrayRW ints(env, javaData); if (ints.get() == NULL) { return NULL; } memcpy(ints.get(), &data[0], data.size() * sizeof(int32_t)); } env->CallVoidMethod(javaFieldPositionIterator, gFPI_setData, javaData); } jcharArray result = env->NewCharArray(s.length()); if (result != NULL) { env->SetCharArrayRegion(result, 0, s.length(), s.getBuffer()); } return result; }
U_CAPI int32_t U_EXPORT2 unum_formatDouble( const UNumberFormat* fmt, double number, UChar* result, int32_t resultLength, UFieldPosition *pos, /* 0 if ignore */ UErrorCode* status) { if(U_FAILURE(*status)) return -1; UnicodeString res; if(!(result==NULL && resultLength==0)) { // NULL destination for pure preflighting: empty dummy string // otherwise, alias the destination buffer res.setTo(result, 0, resultLength); } FieldPosition fp; if(pos != 0) fp.setField(pos->field); ((const NumberFormat*)fmt)->format(number, res, fp); if(pos != 0) { pos->beginIndex = fp.getBeginIndex(); pos->endIndex = fp.getEndIndex(); } return res.extract(result, resultLength, *status); }
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 FormattedNumber::populateFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) { if (U_FAILURE(status)) { return; } if (fResults == nullptr) { status = fErrorCode; return; } // in case any users were depending on the old behavior: fieldPosition.setBeginIndex(0); fieldPosition.setEndIndex(0); fResults->string.nextFieldPosition(fieldPosition, status); }
UBool FieldPositionIterator::next(FieldPosition& fp) { if (pos == -1) { return FALSE; } fp.setField(data->elementAti(pos++)); fp.setBeginIndex(data->elementAti(pos++)); fp.setEndIndex(data->elementAti(pos++)); if (pos == data->size()) { pos = -1; } return TRUE; }
UnicodeString& RelativeDateFormat::format( Calendar& cal, UnicodeString& appendTo, FieldPosition& pos) const { UErrorCode status = U_ZERO_ERROR; UChar emptyStr = 0; UnicodeString dateString(&emptyStr); // calculate the difference, in days, between 'cal' and now. int dayDiff = dayDifference(cal, status); // look up string int32_t len = 0; const UChar *theString = getStringForDay(dayDiff, len, status); if(U_SUCCESS(status) && (theString!=NULL)) { // found a relative string dateString.setTo(theString, len); } if(fTimeFormat == NULL || fCombinedFormat == 0) { if (dateString.length() > 0) { appendTo.append(dateString); } else if(fDateFormat != NULL) { fDateFormat->format(cal,appendTo,pos); } } else { if (dateString.length() == 0 && fDateFormat != NULL) { fDateFormat->format(cal,dateString,pos); } UnicodeString timeString(&emptyStr); FieldPosition timepos = pos; fTimeFormat->format(cal,timeString,timepos); Formattable timeDateStrings[] = { timeString, dateString }; fCombinedFormat->format(timeDateStrings, 2, appendTo, pos, status); // pos is ignored by this int32_t offset; if (pos.getEndIndex() > 0 && (offset = appendTo.indexOf(dateString)) >= 0) { // pos.field was found in dateString, offset start & end based on final position of dateString pos.setBeginIndex( pos.getBeginIndex() + offset ); pos.setEndIndex( pos.getEndIndex() + offset ); } else if (timepos.getEndIndex() > 0 && (offset = appendTo.indexOf(timeString)) >= 0) { // pos.field was found in timeString, offset start & end based on final position of timeString pos.setBeginIndex( timepos.getBeginIndex() + offset ); pos.setEndIndex( timepos.getEndIndex() + offset ); } } return appendTo; }
void test_FieldPosition( void ) { FieldPosition fp( 7 ); if (fp.getField() == 7) { it_logln("FP constructor(int32_t) and getField tested."); }else{ it_errln("*** FP constructor(int32_t) or getField"); } FieldPosition* fph = new FieldPosition( 3 ); if ( fph->getField() != 3) it_errln("*** FP getField or heap constr."); delete fph; UBool err1 = FALSE; UBool err2 = FALSE; UBool err3 = FALSE; for (int32_t i = -50; i < 50; i++ ) { fp.setField( i+8 ); fp.setBeginIndex( i+6 ); fp.setEndIndex( i+7 ); if (fp.getField() != i+8) err1 = TRUE; if (fp.getBeginIndex() != i+6) err2 = TRUE; if (fp.getEndIndex() != i+7) err3 = TRUE; } if (!err1) { it_logln("FP setField and getField tested."); }else{ it_errln("*** FP setField or getField"); } if (!err2) { it_logln("FP setBeginIndex and getBeginIndex tested."); }else{ it_errln("*** FP setBeginIndex or getBeginIndex"); } if (!err3) { it_logln("FP setEndIndex and getEndIndex tested."); }else{ it_errln("*** FP setEndIndex or getEndIndex"); } it_logln(""); }
void NumberStringBuilder::populateFieldPosition(FieldPosition &fp, int32_t offset, UErrorCode &status) const { int32_t rawField = fp.getField(); if (rawField == FieldPosition::DONT_CARE) { return; } if (rawField < 0 || rawField >= UNUM_FIELD_COUNT) { status = U_ILLEGAL_ARGUMENT_ERROR; return; } auto field = static_cast<Field>(rawField); bool seenStart = false; int32_t fractionStart = -1; for (int i = fZero; i <= fZero + fLength; i++) { Field _field = UNUM_FIELD_COUNT; if (i < fZero + fLength) { _field = getFieldPtr()[i]; } if (seenStart && field != _field) { // Special case: GROUPING_SEPARATOR counts as an INTEGER. if (field == UNUM_INTEGER_FIELD && _field == UNUM_GROUPING_SEPARATOR_FIELD) { continue; } fp.setEndIndex(i - fZero + offset); break; } else if (!seenStart && field == _field) { fp.setBeginIndex(i - fZero + offset); seenStart = true; } if (_field == UNUM_INTEGER_FIELD || _field == UNUM_DECIMAL_SEPARATOR_FIELD) { fractionStart = i - fZero + 1; } } // Backwards compatibility: FRACTION needs to start after INTEGER if empty if (field == UNUM_FRACTION_FIELD && !seenStart) { fp.setBeginIndex(fractionStart + offset); fp.setEndIndex(fractionStart + offset); } }
U_CAPI int32_t U_EXPORT2 unum_formatDecimal(const UNumberFormat* fmt, const char * number, int32_t length, UChar* result, int32_t resultLength, UFieldPosition *pos, /* 0 if ignore */ UErrorCode* status) { if(U_FAILURE(*status)) { return -1; } if ((result == NULL && resultLength != 0) || resultLength < 0) { *status = U_ILLEGAL_ARGUMENT_ERROR; return -1; } FieldPosition fp; if(pos != 0) { fp.setField(pos->field); } if (length < 0) { length = uprv_strlen(number); } StringPiece numSP(number, length); Formattable numFmtbl(numSP, *status); UnicodeString resultStr; if (resultLength > 0) { // Alias the destination buffer. resultStr.setTo(result, 0, resultLength); } ((const NumberFormat*)fmt)->format(numFmtbl, resultStr, fp, *status); if(pos != 0) { pos->beginIndex = fp.getBeginIndex(); pos->endIndex = fp.getEndIndex(); } return resultStr.extract(result, resultLength, *status); }
U_CAPI int32_t U_EXPORT2 udat_formatCalendar(const UDateFormat* format, UCalendar* calendar, UChar* result, int32_t resultLength, UFieldPosition* position, UErrorCode* status) { if(U_FAILURE(*status)) { return -1; } if (result == NULL ? resultLength != 0 : resultLength < 0) { *status = U_ILLEGAL_ARGUMENT_ERROR; return -1; } UnicodeString res; if (result != NULL) { // NULL destination for pure preflighting: empty dummy string // otherwise, alias the destination buffer res.setTo(result, 0, resultLength); } FieldPosition fp; if(position != 0) fp.setField(position->field); ((DateFormat*)format)->format(*(Calendar*)calendar, res, fp); if(position != 0) { position->beginIndex = fp.getBeginIndex(); position->endIndex = fp.getEndIndex(); } return res.extract(result, resultLength, *status); }
U_CAPI int32_t U_EXPORT2 unum_formatDoubleCurrency(const UNumberFormat* fmt, double number, UChar* currency, UChar* result, int32_t resultLength, UFieldPosition* pos, /* ignored if 0 */ UErrorCode* status) { if (U_FAILURE(*status)) return -1; UnicodeString res; if (!(result==NULL && resultLength==0)) { // NULL destination for pure preflighting: empty dummy string // otherwise, alias the destination buffer res.setTo(result, 0, resultLength); } FieldPosition fp; if (pos != 0) { fp.setField(pos->field); } CurrencyAmount *tempCurrAmnt = new CurrencyAmount(number, currency, *status); // Check for null pointer. if (tempCurrAmnt == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; return -1; } Formattable n(tempCurrAmnt); ((const NumberFormat*)fmt)->format(n, res, fp, *status); if (pos != 0) { pos->beginIndex = fp.getBeginIndex(); pos->endIndex = fp.getEndIndex(); } return res.extract(result, resultLength, *status); }
void BeeTask::extract(const string& html, MoneyRule& moneyRule, FieldPosition& fieldPosition) { std::vector<string> numVec; extractNumStr(html, fieldPosition.getFindBegin(), fieldPosition.getFindEnd(), numVec); Rate rate; rate.setSourceNo(moneyRule.getSourceNo()); rate.setMoenyANo(moneyRule.getMoenyANo()); rate.setMoenyBNo(moneyRule.getMoenyBNo()); rate.setTradeUnit(numVec.at(fieldPosition.getTradeUnit())); rate.setMidPrice(numVec.at(fieldPosition.getMidPrice())); rate.setSellPrice(numVec.at(fieldPosition.getSellPrice())); rate.setCashPrice(numVec.at(fieldPosition.getCashPrice())); rate.setSpotPrice(numVec.at(fieldPosition.getSpotPrice())); rate.setUpdateDate(numVec.at(fieldPosition.getUpdateDate())); RateDb::insertRate(rate); }
bool NumberStringBuilder::nextFieldPosition(FieldPosition& fp, UErrorCode& status) const { int32_t rawField = fp.getField(); if (rawField == FieldPosition::DONT_CARE) { return FALSE; } if (rawField < 0 || rawField >= UNUM_FIELD_COUNT) { status = U_ILLEGAL_ARGUMENT_ERROR; return FALSE; } auto field = static_cast<Field>(rawField); bool seenStart = false; int32_t fractionStart = -1; int32_t startIndex = fp.getEndIndex(); for (int i = fZero + startIndex; i <= fZero + fLength; i++) { Field _field = UNUM_FIELD_COUNT; if (i < fZero + fLength) { _field = getFieldPtr()[i]; } if (seenStart && field != _field) { // Special case: GROUPING_SEPARATOR counts as an INTEGER. if (field == UNUM_INTEGER_FIELD && _field == UNUM_GROUPING_SEPARATOR_FIELD) { continue; } fp.setEndIndex(i - fZero); break; } else if (!seenStart && field == _field) { fp.setBeginIndex(i - fZero); seenStart = true; } if (_field == UNUM_INTEGER_FIELD || _field == UNUM_DECIMAL_SEPARATOR_FIELD) { fractionStart = i - fZero + 1; } } // Backwards compatibility: FRACTION needs to start after INTEGER if empty. // Do not return that a field was found, though, since there is not actually a fraction part. if (field == UNUM_FRACTION_FIELD && !seenStart && fractionStart != -1) { fp.setBeginIndex(fractionStart); fp.setEndIndex(fractionStart); } return seenStart; }
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); }
static jstring formatDoubleRBNFImpl(JNIEnv *env, jclass clazz, jint addr, jdouble value, jobject field, jstring fieldType, jobject attributes) { // LOGI("ENTER formatDoubleRBNFImpl"); const char * fieldPositionClassName = "java/text/FieldPosition"; const char * stringBufferClassName = "java/lang/StringBuffer"; jclass fieldPositionClass = env->FindClass(fieldPositionClassName); jclass stringBufferClass = env->FindClass(stringBufferClassName); jmethodID setBeginIndexMethodID = env->GetMethodID(fieldPositionClass, "setBeginIndex", "(I)V"); jmethodID setEndIndexMethodID = env->GetMethodID(fieldPositionClass, "setEndIndex", "(I)V"); jmethodID appendMethodID = env->GetMethodID(stringBufferClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"); const char * fieldName = NULL; if(fieldType != NULL) { fieldName = env->GetStringUTFChars(fieldType, NULL); } uint32_t reslenneeded; double val = value; UChar *result = NULL; FieldPosition fp; fp.setField(FieldPosition::DONT_CARE); UErrorCode status = U_ZERO_ERROR; RuleBasedNumberFormat *fmt = (RuleBasedNumberFormat *)(int)addr; UnicodeString res; fmt->format(val, res, fp); reslenneeded = res.extract(NULL, 0, status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; result = (UChar*)malloc(sizeof(UChar) * (reslenneeded + 1)); res.extract(result, reslenneeded + 1, status); } if (icuError(env, status) != FALSE) { free(result); return NULL; } if(fieldType != NULL) { env->ReleaseStringUTFChars(fieldType, fieldName); } jstring resulting = env->NewString(result, reslenneeded); free(result); return resulting; }