LEFT | RIGHT |
(no file at all) | |
| 1 // © 2017 and later: Unicode, Inc. and others. |
| 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
| 3 package com.ibm.icu.impl.number.parse; |
| 4 |
| 5 import com.ibm.icu.text.UnicodeSet; |
| 6 |
| 7 /** |
| 8 * @author sffc |
| 9 * |
| 10 */ |
| 11 public abstract class SymbolMatcher implements NumberParseMatcher { |
| 12 protected final String string; |
| 13 protected final UnicodeSet uniSet; |
| 14 |
| 15 // TODO: Implement this class using only UnicodeSet and not String? |
| 16 // How to deal with case folding? |
| 17 |
| 18 protected SymbolMatcher(String symbolString, UnicodeSet symbolUniSet) { |
| 19 string = symbolString; |
| 20 uniSet = symbolUniSet; |
| 21 } |
| 22 |
| 23 protected SymbolMatcher(UnicodeSetStaticCache.Key key) { |
| 24 string = ""; |
| 25 uniSet = UnicodeSetStaticCache.get(key); |
| 26 } |
| 27 |
| 28 @Override |
| 29 public boolean match(StringSegment segment, ParsedNumber result) { |
| 30 // Smoke test first; this matcher might be disabled. |
| 31 if (isDisabled(result)) { |
| 32 return false; |
| 33 } |
| 34 |
| 35 int cp = segment.getCodePoint(); |
| 36 if (cp != -1 && uniSet.contains(cp)) { |
| 37 segment.adjustOffset(Character.charCount(cp)); |
| 38 accept(segment, result); |
| 39 return false; |
| 40 } |
| 41 |
| 42 if (string.isEmpty()) { |
| 43 return false; |
| 44 } |
| 45 int overlap = segment.getCommonPrefixLength(string); |
| 46 if (overlap == string.length()) { |
| 47 segment.adjustOffset(string.length()); |
| 48 accept(segment, result); |
| 49 return false; |
| 50 } |
| 51 return overlap == segment.length(); |
| 52 } |
| 53 |
| 54 @Override |
| 55 public UnicodeSet getLeadCodePoints() { |
| 56 if (string == null || string.isEmpty()) { |
| 57 // Assumption: for sets from UnicodeSetStaticCache, uniSet == leadCo
dePoints. |
| 58 return uniSet; |
| 59 } |
| 60 |
| 61 UnicodeSet leadCodePoints = new UnicodeSet(); |
| 62 ParsingUtils.putLeadCodePoints(uniSet, leadCodePoints); |
| 63 ParsingUtils.putLeadCodePoint(string, leadCodePoints); |
| 64 return leadCodePoints.freeze(); |
| 65 } |
| 66 |
| 67 @Override |
| 68 public void postProcess(ParsedNumber result) { |
| 69 // No-op |
| 70 } |
| 71 |
| 72 protected abstract boolean isDisabled(ParsedNumber result); |
| 73 |
| 74 protected abstract void accept(StringSegment segment, ParsedNumber result); |
| 75 } |
LEFT | RIGHT |