2018-02-09 06:57:37 +00:00
|
|
|
// © 2018 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2018-04-23 23:02:26 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2018-02-09 06:57:37 +00:00
|
|
|
|
2018-02-13 02:23:52 +00:00
|
|
|
// Allow implicit conversion from char16_t* to UnicodeString for this file:
|
|
|
|
// Helpful in toString methods and elsewhere.
|
|
|
|
#define UNISTR_FROM_STRING_EXPLICIT
|
|
|
|
|
2018-02-09 06:57:37 +00:00
|
|
|
#include "numparse_types.h"
|
|
|
|
#include "numparse_scientific.h"
|
2018-05-16 22:46:40 +00:00
|
|
|
#include "static_unicode_sets.h"
|
2019-04-05 23:43:19 +00:00
|
|
|
#include "string_segment.h"
|
2018-02-09 06:57:37 +00:00
|
|
|
|
|
|
|
using namespace icu;
|
|
|
|
using namespace icu::numparse;
|
|
|
|
using namespace icu::numparse::impl;
|
|
|
|
|
|
|
|
|
2018-03-31 03:10:44 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline const UnicodeSet& minusSignSet() {
|
|
|
|
return *unisets::get(unisets::MINUS_SIGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const UnicodeSet& plusSignSet() {
|
|
|
|
return *unisets::get(unisets::PLUS_SIGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2018-02-09 06:57:37 +00:00
|
|
|
ScientificMatcher::ScientificMatcher(const DecimalFormatSymbols& dfs, const Grouper& grouper)
|
|
|
|
: fExponentSeparatorString(dfs.getConstSymbol(DecimalFormatSymbols::kExponentialSymbol)),
|
2019-05-21 22:36:25 +00:00
|
|
|
fExponentMatcher(dfs, grouper, PARSE_FLAG_INTEGER_ONLY | PARSE_FLAG_GROUPING_DISABLED),
|
|
|
|
fIgnorablesMatcher(PARSE_FLAG_STRICT_IGNORABLES) {
|
2018-03-31 03:10:44 +00:00
|
|
|
|
|
|
|
const UnicodeString& minusSign = dfs.getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
|
|
|
|
if (minusSignSet().contains(minusSign)) {
|
|
|
|
fCustomMinusSign.setToBogus();
|
|
|
|
} else {
|
|
|
|
fCustomMinusSign = minusSign;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString& plusSign = dfs.getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
|
|
|
|
if (plusSignSet().contains(plusSign)) {
|
|
|
|
fCustomPlusSign.setToBogus();
|
|
|
|
} else {
|
|
|
|
fCustomPlusSign = plusSign;
|
|
|
|
}
|
2018-02-09 06:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScientificMatcher::match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const {
|
|
|
|
// Only accept scientific notation after the mantissa.
|
2018-04-17 08:05:20 +00:00
|
|
|
if (!result.seenNumber()) {
|
2018-02-09 06:57:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-19 01:10:38 +00:00
|
|
|
|
|
|
|
// Only accept one exponent per string.
|
|
|
|
if (0 != (result.flags & FLAG_HAS_EXPONENT)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-09 06:57:37 +00:00
|
|
|
|
|
|
|
// First match the scientific separator, and then match another number after it.
|
2018-06-01 00:31:54 +00:00
|
|
|
// NOTE: This is guarded by the smoke test; no need to check fExponentSeparatorString length again.
|
2019-05-21 22:36:25 +00:00
|
|
|
int32_t initialOffset = segment.getOffset();
|
|
|
|
int32_t overlap = segment.getCommonPrefixLength(fExponentSeparatorString);
|
|
|
|
if (overlap == fExponentSeparatorString.length()) {
|
2018-02-09 06:57:37 +00:00
|
|
|
// Full exponent separator match.
|
|
|
|
|
|
|
|
// First attempt to get a code point, returning true if we can't get one.
|
2019-05-21 22:36:25 +00:00
|
|
|
if (segment.length() == overlap) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
segment.adjustOffset(overlap);
|
|
|
|
|
|
|
|
// Allow ignorables before the sign.
|
|
|
|
// Note: call site is guarded by the segment.length() check above.
|
|
|
|
// Note: the ignorables matcher should not touch the result.
|
|
|
|
fIgnorablesMatcher.match(segment, result, status);
|
|
|
|
if (segment.length() == 0) {
|
|
|
|
segment.setOffset(initialOffset);
|
2018-02-09 06:57:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow a sign, and then try to match digits.
|
|
|
|
int8_t exponentSign = 1;
|
2018-03-31 03:10:44 +00:00
|
|
|
if (segment.startsWith(minusSignSet())) {
|
2018-02-09 06:57:37 +00:00
|
|
|
exponentSign = -1;
|
|
|
|
segment.adjustOffsetByCodePoint();
|
2018-03-31 03:10:44 +00:00
|
|
|
} else if (segment.startsWith(plusSignSet())) {
|
2018-02-09 06:57:37 +00:00
|
|
|
segment.adjustOffsetByCodePoint();
|
2018-03-31 03:10:44 +00:00
|
|
|
} else if (segment.startsWith(fCustomMinusSign)) {
|
2019-05-21 22:36:25 +00:00
|
|
|
overlap = segment.getCommonPrefixLength(fCustomMinusSign);
|
|
|
|
if (overlap != fCustomMinusSign.length()) {
|
|
|
|
// Partial custom sign match
|
|
|
|
segment.setOffset(initialOffset);
|
2018-03-31 03:10:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
exponentSign = -1;
|
2019-05-21 22:36:25 +00:00
|
|
|
segment.adjustOffset(overlap);
|
2018-03-31 03:10:44 +00:00
|
|
|
} else if (segment.startsWith(fCustomPlusSign)) {
|
2019-05-21 22:36:25 +00:00
|
|
|
overlap = segment.getCommonPrefixLength(fCustomPlusSign);
|
|
|
|
if (overlap != fCustomPlusSign.length()) {
|
|
|
|
// Partial custom sign match
|
|
|
|
segment.setOffset(initialOffset);
|
2018-03-31 03:10:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-21 22:36:25 +00:00
|
|
|
segment.adjustOffset(overlap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the segment is empty.
|
|
|
|
if (segment.length() == 0) {
|
|
|
|
segment.setOffset(initialOffset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow ignorables after the sign.
|
|
|
|
// Note: call site is guarded by the segment.length() check above.
|
|
|
|
// Note: the ignorables matcher should not touch the result.
|
|
|
|
fIgnorablesMatcher.match(segment, result, status);
|
|
|
|
if (segment.length() == 0) {
|
|
|
|
segment.setOffset(initialOffset);
|
|
|
|
return true;
|
2018-02-09 06:57:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:05:20 +00:00
|
|
|
// We are supposed to accept E0 after NaN, so we need to make sure result.quantity is available.
|
|
|
|
bool wasBogus = result.quantity.bogus;
|
|
|
|
result.quantity.bogus = false;
|
2018-02-09 06:57:37 +00:00
|
|
|
int digitsOffset = segment.getOffset();
|
|
|
|
bool digitsReturnValue = fExponentMatcher.match(segment, result, exponentSign, status);
|
2018-04-17 08:05:20 +00:00
|
|
|
result.quantity.bogus = wasBogus;
|
|
|
|
|
2018-02-09 06:57:37 +00:00
|
|
|
if (segment.getOffset() != digitsOffset) {
|
|
|
|
// At least one exponent digit was matched.
|
|
|
|
result.flags |= FLAG_HAS_EXPONENT;
|
|
|
|
} else {
|
2019-05-21 22:36:25 +00:00
|
|
|
// No exponent digits were matched
|
|
|
|
segment.setOffset(initialOffset);
|
2018-02-09 06:57:37 +00:00
|
|
|
}
|
|
|
|
return digitsReturnValue;
|
|
|
|
|
2019-05-21 22:36:25 +00:00
|
|
|
} else if (overlap == segment.length()) {
|
2018-02-09 06:57:37 +00:00
|
|
|
// Partial exponent separator match
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-21 06:30:29 +00:00
|
|
|
bool ScientificMatcher::smokeTest(const StringSegment& segment) const {
|
|
|
|
return segment.startsWith(fExponentSeparatorString);
|
2018-02-09 06:57:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 02:23:52 +00:00
|
|
|
UnicodeString ScientificMatcher::toString() const {
|
|
|
|
return u"<Scientific>";
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:57:37 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|