2018-02-08 08:49:50 +00:00
|
|
|
// © 2018 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING && !UPRV_INCOMPLETE_CPP11_SUPPORT
|
|
|
|
|
2018-02-09 06:30:40 +00:00
|
|
|
// Allow implicit conversion from char16_t* to UnicodeString for this file
|
|
|
|
#define UNISTR_FROM_STRING_EXPLICIT
|
|
|
|
|
2018-02-08 08:49:50 +00:00
|
|
|
#include "number_types.h"
|
|
|
|
#include "number_patternstring.h"
|
|
|
|
#include "numparse_types.h"
|
|
|
|
#include "numparse_impl.h"
|
|
|
|
#include "numparse_symbols.h"
|
|
|
|
#include "numparse_decimal.h"
|
|
|
|
#include "unicode/numberformatter.h"
|
|
|
|
|
2018-02-08 09:59:35 +00:00
|
|
|
#include <typeinfo>
|
|
|
|
|
2018-02-08 08:49:50 +00:00
|
|
|
using namespace icu;
|
|
|
|
using namespace icu::number;
|
|
|
|
using namespace icu::number::impl;
|
|
|
|
using namespace icu::numparse;
|
|
|
|
using namespace icu::numparse::impl;
|
|
|
|
|
|
|
|
|
|
|
|
NumberParserImpl*
|
|
|
|
NumberParserImpl::createSimpleParser(const Locale& locale, const UnicodeString& patternString,
|
|
|
|
parse_flags_t parseFlags, UErrorCode& status) {
|
|
|
|
|
|
|
|
auto* parser = new NumberParserImpl(parseFlags, true);
|
|
|
|
DecimalFormatSymbols symbols(locale, status);
|
|
|
|
|
2018-02-10 06:36:07 +00:00
|
|
|
parser->fLocalMatchers.ignorables = std::move(IgnorablesMatcher(unisets::DEFAULT_IGNORABLES));
|
2018-02-09 02:35:02 +00:00
|
|
|
|
2018-02-08 08:49:50 +00:00
|
|
|
// MatcherFactory factory = new MatcherFactory();
|
|
|
|
// factory.currency = Currency.getInstance("USD");
|
|
|
|
// factory.symbols = symbols;
|
|
|
|
// factory.ignorables = ignorables;
|
|
|
|
// factory.locale = locale;
|
|
|
|
// factory.parseFlags = parseFlags;
|
|
|
|
|
|
|
|
ParsedPatternInfo patternInfo;
|
|
|
|
PatternParser::parseToPatternInfo(patternString, patternInfo, status);
|
|
|
|
// AffixMatcher.createMatchers(patternInfo, parser, factory, ignorables, parseFlags);
|
|
|
|
|
|
|
|
Grouper grouper = Grouper::forStrategy(UNUM_GROUPING_AUTO);
|
|
|
|
grouper.setLocaleData(patternInfo, locale);
|
|
|
|
|
2018-02-09 05:47:49 +00:00
|
|
|
parser->addMatcher(parser->fLocalMatchers.ignorables);
|
|
|
|
parser->addMatcher(parser->fLocalMatchers.decimal = {symbols, grouper, parseFlags});
|
|
|
|
parser->addMatcher(parser->fLocalMatchers.minusSign = {symbols, false});
|
|
|
|
parser->addMatcher(parser->fLocalMatchers.plusSign = {symbols, false});
|
|
|
|
parser->addMatcher(parser->fLocalMatchers.percent = {symbols});
|
|
|
|
parser->addMatcher(parser->fLocalMatchers.permille = {symbols});
|
|
|
|
parser->addMatcher(parser->fLocalMatchers.nan = {symbols});
|
2018-02-09 06:30:40 +00:00
|
|
|
parser->addMatcher(parser->fLocalMatchers.infinity = {symbols});
|
|
|
|
parser->addMatcher(parser->fLocalMatchers.padding = {u"@"});
|
2018-02-09 06:57:37 +00:00
|
|
|
parser->addMatcher(parser->fLocalMatchers.scientific = {symbols, grouper});
|
2018-02-10 02:59:49 +00:00
|
|
|
parser->addMatcher(parser->fLocalMatchers.currencyNames = {locale, status});
|
2018-02-08 08:49:50 +00:00
|
|
|
// parser.addMatcher(new RequireNumberMatcher());
|
|
|
|
|
|
|
|
parser->freeze();
|
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumberParserImpl::NumberParserImpl(parse_flags_t parseFlags, bool computeLeads)
|
|
|
|
: fParseFlags(parseFlags), fComputeLeads(computeLeads) {
|
|
|
|
}
|
|
|
|
|
|
|
|
NumberParserImpl::~NumberParserImpl() {
|
2018-02-09 05:47:49 +00:00
|
|
|
if (fComputeLeads) {
|
|
|
|
for (int32_t i = 0; i < fNumMatchers; i++) {
|
2018-02-08 08:49:50 +00:00
|
|
|
delete (fLeads[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fNumMatchers = 0;
|
|
|
|
}
|
|
|
|
|
2018-02-10 06:36:07 +00:00
|
|
|
void NumberParserImpl::addMatcher(NumberParseMatcher& matcher) {
|
2018-02-08 08:49:50 +00:00
|
|
|
if (fNumMatchers + 1 > fMatchers.getCapacity()) {
|
|
|
|
fMatchers.resize(fNumMatchers * 2, fNumMatchers);
|
|
|
|
if (fComputeLeads) {
|
|
|
|
// The two arrays should grow in tandem:
|
|
|
|
U_ASSERT(fNumMatchers >= fLeads.getCapacity());
|
|
|
|
fLeads.resize(fNumMatchers * 2, fNumMatchers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:47:49 +00:00
|
|
|
fMatchers[fNumMatchers] = &matcher;
|
2018-02-08 08:49:50 +00:00
|
|
|
|
|
|
|
if (fComputeLeads) {
|
2018-02-10 02:59:49 +00:00
|
|
|
addLeadCodePointsForMatcher(matcher);
|
2018-02-08 08:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fNumMatchers++;
|
|
|
|
}
|
|
|
|
|
2018-02-10 06:36:07 +00:00
|
|
|
void NumberParserImpl::addLeadCodePointsForMatcher(NumberParseMatcher& matcher) {
|
|
|
|
const UnicodeSet& leadCodePoints = matcher.getLeadCodePoints();
|
2018-02-10 02:59:49 +00:00
|
|
|
// TODO: Avoid the clone operation here.
|
|
|
|
if (0 != (fParseFlags & PARSE_FLAG_IGNORE_CASE)) {
|
2018-02-10 06:36:07 +00:00
|
|
|
auto* copy = dynamic_cast<UnicodeSet*>(leadCodePoints.cloneAsThawed());
|
2018-02-10 02:59:49 +00:00
|
|
|
copy->closeOver(USET_ADD_CASE_MAPPINGS);
|
|
|
|
copy->freeze();
|
|
|
|
fLeads[fNumMatchers] = copy;
|
|
|
|
} else {
|
2018-02-10 06:36:07 +00:00
|
|
|
// FIXME: new here because we still take ownership
|
|
|
|
fLeads[fNumMatchers] = new UnicodeSet(leadCodePoints);
|
2018-02-10 02:59:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 08:49:50 +00:00
|
|
|
void NumberParserImpl::freeze() {
|
|
|
|
fFrozen = true;
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:59:35 +00:00
|
|
|
void NumberParserImpl::parse(const UnicodeString& input, bool greedy, ParsedNumber& result,
|
|
|
|
UErrorCode& status) const {
|
|
|
|
return parse(input, 0, greedy, result, status);
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:47:49 +00:00
|
|
|
void NumberParserImpl::parse(const UnicodeString& input, int32_t start, bool greedy, ParsedNumber& result,
|
|
|
|
UErrorCode& status) const {
|
2018-02-08 09:59:35 +00:00
|
|
|
U_ASSERT(fFrozen);
|
|
|
|
// TODO: Check start >= 0 and start < input.length()
|
|
|
|
StringSegment segment(input, fParseFlags);
|
|
|
|
segment.adjustOffset(start);
|
|
|
|
if (greedy) {
|
|
|
|
parseGreedyRecursive(segment, result, status);
|
|
|
|
} else {
|
|
|
|
parseLongestRecursive(segment, result, status);
|
|
|
|
}
|
|
|
|
for (int32_t i = 0; i < fNumMatchers; i++) {
|
|
|
|
fMatchers[i]->postProcess(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberParserImpl::parseGreedyRecursive(StringSegment& segment, ParsedNumber& result,
|
|
|
|
UErrorCode& status) const {
|
|
|
|
// Base Case
|
|
|
|
if (segment.length() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int initialOffset = segment.getOffset();
|
|
|
|
int leadCp = segment.getCodePoint();
|
|
|
|
for (int32_t i = 0; i < fNumMatchers; i++) {
|
|
|
|
if (fComputeLeads && !fLeads[i]->contains(leadCp)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const NumberParseMatcher* matcher = fMatchers[i];
|
|
|
|
matcher->match(segment, result, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (segment.getOffset() != initialOffset) {
|
|
|
|
// In a greedy parse, recurse on only the first match.
|
|
|
|
parseGreedyRecursive(segment, result, status);
|
|
|
|
// The following line resets the offset so that the StringSegment says the same across
|
|
|
|
// the function
|
|
|
|
// call boundary. Since we recurse only once, this line is not strictly necessary.
|
|
|
|
segment.setOffset(initialOffset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: If we get here, the greedy parse completed without consuming the entire string.
|
|
|
|
}
|
|
|
|
|
|
|
|
void NumberParserImpl::parseLongestRecursive(StringSegment& segment, ParsedNumber& result,
|
|
|
|
UErrorCode& status) const {
|
|
|
|
// Base Case
|
|
|
|
if (segment.length() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Give a nice way for the matcher to reset the ParsedNumber?
|
|
|
|
ParsedNumber initial(result);
|
|
|
|
ParsedNumber candidate;
|
|
|
|
|
|
|
|
int initialOffset = segment.getOffset();
|
|
|
|
for (int32_t i = 0; i < fNumMatchers; i++) {
|
|
|
|
// TODO: Check leadChars here?
|
|
|
|
const NumberParseMatcher* matcher = fMatchers[i];
|
|
|
|
|
|
|
|
// In a non-greedy parse, we attempt all possible matches and pick the best.
|
|
|
|
for (int32_t charsToConsume = 0; charsToConsume < segment.length();) {
|
|
|
|
charsToConsume += U16_LENGTH(segment.codePointAt(charsToConsume));
|
|
|
|
|
|
|
|
// Run the matcher on a segment of the current length.
|
|
|
|
candidate = initial;
|
|
|
|
segment.setLength(charsToConsume);
|
|
|
|
bool maybeMore = matcher->match(segment, candidate, status);
|
|
|
|
segment.resetLength();
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the entire segment was consumed, recurse.
|
|
|
|
if (segment.getOffset() - initialOffset == charsToConsume) {
|
|
|
|
parseLongestRecursive(segment, candidate, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (candidate.isBetterThan(result)) {
|
|
|
|
result = candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the segment can be re-used, reset the offset.
|
|
|
|
// This does not have an effect if the matcher did not consume any chars.
|
|
|
|
segment.setOffset(initialOffset);
|
|
|
|
|
|
|
|
// Unless the matcher wants to see the next char, continue to the next matcher.
|
|
|
|
if (!maybeMore) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString NumberParserImpl::toString() const {
|
|
|
|
UnicodeString result(u"<NumberParserImpl matchers:[");
|
|
|
|
for (int32_t i = 0; i < fNumMatchers; i++) {
|
|
|
|
result.append(u' ');
|
|
|
|
result.append(UnicodeString(typeid(*fMatchers[i]).name()));
|
|
|
|
}
|
|
|
|
result.append(u" ]>", -1);
|
|
|
|
return result;
|
|
|
|
}
|
2018-02-08 08:49:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|