| 1 |
// © 2018 and later: Unicode, Inc. and others. |
| 2 |
// License & terms of use: http://www.unicode.org/copyright.html |
| 3 |
|
| 4 |
#include "unicode/utypes.h" |
| 5 |
|
| 6 |
#if !UCONFIG_NO_FORMATTING |
| 7 |
#ifndef __NUMPARSE_TYPES_H__ |
| 8 |
#define __NUMPARSE_TYPES_H__ |
| 9 |
|
| 10 |
#include "unicode/uobject.h" |
| 11 |
#include "number_decimalquantity.h" |
| 12 |
#include "string_segment.h" |
| 13 |
|
| 14 |
U_NAMESPACE_BEGIN |
| 15 |
namespace numparse::impl { |
| 16 |
|
| 17 |
// Forward-declarations |
| 18 |
class ParsedNumber; |
| 19 |
|
| 20 |
typedef int32_t result_flags_t; |
| 21 |
typedef int32_t parse_flags_t; |
| 22 |
|
| 23 |
/** Flags for the type result_flags_t */ |
| 24 |
enum ResultFlags { |
| 25 |
FLAG_NEGATIVE = 0x0001, |
| 26 |
FLAG_PERCENT = 0x0002, |
| 27 |
FLAG_PERMILLE = 0x0004, |
| 28 |
FLAG_HAS_EXPONENT = 0x0008, |
| 29 |
// FLAG_HAS_DEFAULT_CURRENCY = 0x0010, // no longer used |
| 30 |
FLAG_HAS_DECIMAL_SEPARATOR = 0x0020, |
| 31 |
FLAG_NAN = 0x0040, |
| 32 |
FLAG_INFINITY = 0x0080, |
| 33 |
FLAG_FAIL = 0x0100, |
| 34 |
}; |
| 35 |
|
| 36 |
/** Flags for the type parse_flags_t */ |
| 37 |
enum ParseFlags { |
| 38 |
PARSE_FLAG_IGNORE_CASE = 0x0001, |
| 39 |
PARSE_FLAG_MONETARY_SEPARATORS = 0x0002, |
| 40 |
PARSE_FLAG_STRICT_SEPARATORS = 0x0004, |
| 41 |
PARSE_FLAG_STRICT_GROUPING_SIZE = 0x0008, |
| 42 |
PARSE_FLAG_INTEGER_ONLY = 0x0010, |
| 43 |
PARSE_FLAG_GROUPING_DISABLED = 0x0020, |
| 44 |
// PARSE_FLAG_FRACTION_GROUPING_ENABLED = 0x0040, // see #10794 |
| 45 |
PARSE_FLAG_INCLUDE_UNPAIRED_AFFIXES = 0x0080, |
| 46 |
PARSE_FLAG_USE_FULL_AFFIXES = 0x0100, |
| 47 |
PARSE_FLAG_EXACT_AFFIX = 0x0200, |
| 48 |
PARSE_FLAG_PLUS_SIGN_ALLOWED = 0x0400, |
| 49 |
// PARSE_FLAG_OPTIMIZE = 0x0800, // no longer used |
| 50 |
// PARSE_FLAG_FORCE_BIG_DECIMAL = 0x1000, // not used in ICU4C |
| 51 |
PARSE_FLAG_NO_FOREIGN_CURRENCY = 0x2000, |
| 52 |
PARSE_FLAG_ALLOW_INFINITE_RECURSION = 0x4000, |
| 53 |
PARSE_FLAG_STRICT_IGNORABLES = 0x8000, |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
// TODO: Is this class worthwhile? |
| 58 |
template<int32_t stackCapacity> |
| 59 |
class CompactUnicodeString { |
| 60 |
public: |
| 61 |
CompactUnicodeString() { |
| 62 |
static_assert(stackCapacity > 0, "cannot have zero space on stack"); |
| 63 |
fBuffer[0] = 0; |
| 64 |
} |
| 65 |
|
| 66 |
CompactUnicodeString(const UnicodeString& text, UErrorCode& status) |
| 67 |
: fBuffer(text.length() + 1, status) { |
| 68 |
if (U_FAILURE(status)) { return; } |
| 69 |
uprv_memcpy(fBuffer.getAlias(), text.getBuffer(), sizeof(char16_t) * text.length()); |
| 70 |
fBuffer[text.length()] = 0; |
| 71 |
} |
| 72 |
|
| 73 |
inline UnicodeString toAliasedUnicodeString() const { |
| 74 |
return UnicodeString(true, fBuffer.getAlias(), -1); |
| 75 |
} |
| 76 |
|
| 77 |
bool operator==(const CompactUnicodeString& other) const { |
| 78 |
// Use the alias-only constructor and then call UnicodeString operator== |
| 79 |
return toAliasedUnicodeString() == other.toAliasedUnicodeString(); |
| 80 |
} |
| 81 |
|
| 82 |
private: |
| 83 |
MaybeStackArray<char16_t, stackCapacity> fBuffer; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Struct-like class to hold the results of a parsing routine. |
| 89 |
* |
| 90 |
* @author sffc |
| 91 |
*/ |
| 92 |
// Exported as U_I18N_API for tests |
| 93 |
class U_I18N_API ParsedNumber { |
| 94 |
public: |
| 95 |
|
| 96 |
/** |
| 97 |
* The numerical value that was parsed. |
| 98 |
*/ |
| 99 |
::icu::number::impl::DecimalQuantity quantity; |
| 100 |
|
| 101 |
/** |
| 102 |
* The index of the last char consumed during parsing. If parsing started at index 0, this is equal |
| 103 |
* to the number of chars consumed. This is NOT necessarily the same as the StringSegment offset; |
| 104 |
* "weak" chars, like whitespace, change the offset, but the charsConsumed is not touched until a |
| 105 |
* "strong" char is encountered. |
| 106 |
*/ |
| 107 |
int32_t charEnd; |
| 108 |
|
| 109 |
/** |
| 110 |
* Boolean flags (see constants above). |
| 111 |
*/ |
| 112 |
result_flags_t flags; |
| 113 |
|
| 114 |
/** |
| 115 |
* The pattern string corresponding to the prefix that got consumed. |
| 116 |
*/ |
| 117 |
UnicodeString prefix; |
| 118 |
|
| 119 |
/** |
| 120 |
* The pattern string corresponding to the suffix that got consumed. |
| 121 |
*/ |
| 122 |
UnicodeString suffix; |
| 123 |
|
| 124 |
/** |
| 125 |
* The currency that got consumed. |
| 126 |
*/ |
| 127 |
char16_t currencyCode[4]; |
| 128 |
|
| 129 |
ParsedNumber(); |
| 130 |
|
| 131 |
ParsedNumber(const ParsedNumber& other) = default; |
| 132 |
|
| 133 |
ParsedNumber& operator=(const ParsedNumber& other) = default; |
| 134 |
|
| 135 |
void clear(); |
| 136 |
|
| 137 |
/** |
| 138 |
* Call this method to register that a "strong" char was consumed. This should be done after calling |
| 139 |
* {@link StringSegment#setOffset} or {@link StringSegment#adjustOffset} except when the char is |
| 140 |
* "weak", like whitespace. |
| 141 |
* |
| 142 |
* <p> |
| 143 |
* <strong>What is a strong versus weak char?</strong> The behavior of number parsing is to "stop" |
| 144 |
* after reading the number, even if there is other content following the number. For example, after |
| 145 |
* parsing the string "123 " (123 followed by a space), the cursor should be set to 3, not 4, even |
| 146 |
* though there are matchers that accept whitespace. In this example, the digits are strong, whereas |
| 147 |
* the whitespace is weak. Grouping separators are weak, whereas decimal separators are strong. Most |
| 148 |
* other chars are strong. |
| 149 |
* |
| 150 |
* @param segment |
| 151 |
* The current StringSegment, usually immediately following a call to setOffset. |
| 152 |
*/ |
| 153 |
void setCharsConsumed(const StringSegment& segment); |
| 154 |
|
| 155 |
/** Apply certain number-related flags to the DecimalQuantity. */ |
| 156 |
void postProcess(); |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns whether this the parse was successful. To be successful, at least one char must have been |
| 160 |
* consumed, and the failure flag must not be set. |
| 161 |
*/ |
| 162 |
bool success() const; |
| 163 |
|
| 164 |
bool seenNumber() const; |
| 165 |
|
| 166 |
double getDouble(UErrorCode& status) const; |
| 167 |
|
| 168 |
void populateFormattable(Formattable& output, parse_flags_t parseFlags) const; |
| 169 |
|
| 170 |
bool isBetterThan(const ParsedNumber& other); |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* The core interface implemented by all matchers used for number parsing. |
| 176 |
* |
| 177 |
* Given a string, there should NOT be more than one way to consume the string with the same matcher |
| 178 |
* applied multiple times. If there is, the non-greedy parsing algorithm will be unhappy and may enter an |
| 179 |
* exponential-time loop. For example, consider the "A Matcher" that accepts "any number of As". Given |
| 180 |
* the string "AAAA", there are 2^N = 8 ways to apply the A Matcher to this string: you could have the A |
| 181 |
* Matcher apply 4 times to each character; you could have it apply just once to all the characters; you |
| 182 |
* could have it apply to the first 2 characters and the second 2 characters; and so on. A better version |
| 183 |
* of the "A Matcher" would be for it to accept exactly one A, and allow the algorithm to run it |
| 184 |
* repeatedly to consume a string of multiple As. The A Matcher can implement the Flexible interface |
| 185 |
* below to signal that it can be applied multiple times in a row. |
| 186 |
* |
| 187 |
* @author sffc |
| 188 |
*/ |
| 189 |
// Exported as U_I18N_API for tests |
| 190 |
class U_I18N_API NumberParseMatcher { |
| 191 |
public: |
| 192 |
virtual ~NumberParseMatcher(); |
| 193 |
|
| 194 |
/** |
| 195 |
* Matchers can override this method to return true to indicate that they are optional and can be run |
| 196 |
* repeatedly. Used by SeriesMatcher, primarily in the context of IgnorablesMatcher. |
| 197 |
*/ |
| 198 |
virtual bool isFlexible() const { |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Runs this matcher starting at the beginning of the given StringSegment. If this matcher finds |
| 204 |
* something interesting in the StringSegment, it should update the offset of the StringSegment |
| 205 |
* corresponding to how many chars were matched. |
| 206 |
* |
| 207 |
* This method is thread-safe. |
| 208 |
* |
| 209 |
* @param segment |
| 210 |
* The StringSegment to match against. Matches always start at the beginning of the |
| 211 |
* segment. The segment is guaranteed to contain at least one char. |
| 212 |
* @param result |
| 213 |
* The data structure to store results if the match succeeds. |
| 214 |
* @return Whether this matcher thinks there may be more interesting chars beyond the end of the |
| 215 |
* string segment. |
| 216 |
*/ |
| 217 |
virtual bool match(StringSegment& segment, ParsedNumber& result, UErrorCode& status) const = 0; |
| 218 |
|
| 219 |
/** |
| 220 |
* Performs a fast "smoke check" for whether or not this matcher could possibly match against the |
| 221 |
* given string segment. The test should be as fast as possible but also as restrictive as possible. |
| 222 |
* For example, matchers can maintain a UnicodeSet of all code points that count possibly start a |
| 223 |
* match. Matchers should use the {@link StringSegment#startsWith} method in order to correctly |
| 224 |
* handle case folding. |
| 225 |
* |
| 226 |
* @param segment |
| 227 |
* The segment to check against. |
| 228 |
* @return true if the matcher might be able to match against this segment; false if it definitely |
| 229 |
* will not be able to match. |
| 230 |
*/ |
| 231 |
virtual bool smokeTest(const StringSegment& segment) const = 0; |
| 232 |
|
| 233 |
/** |
| 234 |
* Method called at the end of a parse, after all matchers have failed to consume any more chars. |
| 235 |
* Allows a matcher to make final modifications to the result given the knowledge that no more |
| 236 |
* matches are possible. |
| 237 |
* |
| 238 |
* @param result |
| 239 |
* The data structure to store results. |
| 240 |
*/ |
| 241 |
virtual void postProcess(ParsedNumber&) const { |
| 242 |
// Default implementation: no-op |
| 243 |
} |
| 244 |
|
| 245 |
// String for debugging |
| 246 |
virtual UnicodeString toString() const = 0; |
| 247 |
|
| 248 |
protected: |
| 249 |
// No construction except by subclasses! |
| 250 |
NumberParseMatcher() = default; |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Interface for use in arguments. |
| 256 |
*/ |
| 257 |
// Exported as U_I18N_API for tests |
| 258 |
class U_I18N_API MutableMatcherCollection { |
| 259 |
public: |
| 260 |
virtual ~MutableMatcherCollection() = default; |
| 261 |
|
| 262 |
virtual void addMatcher(NumberParseMatcher& matcher) = 0; |
| 263 |
}; |
| 264 |
|
| 265 |
} // namespace numparse::impl |
| 266 |
U_NAMESPACE_END |
| 267 |
|
| 268 |
#endif //__NUMPARSE_TYPES_H__ |
| 269 |
#endif /* #if !UCONFIG_NO_FORMATTING */ |