]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/string_segment.h
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
7 #ifndef __NUMPARSE_STRINGSEGMENT_H__
8 #define __NUMPARSE_STRINGSEGMENT_H__
10 #include "unicode/unistr.h"
11 #include "unicode/uniset.h"
17 * A mutable UnicodeString wrapper with a variable offset and length and
18 * support for case folding. The charAt, length, and subSequence methods all
19 * operate relative to the fixed offset into the UnicodeString.
21 * Intended to be useful for parsing.
23 * CAUTION: Since this class is mutable, it must not be used anywhere that an
24 * immutable object is required, like in a cache or as the key of a hash map.
26 * @author sffc (Shane Carr)
28 // Exported as U_I18N_API for tests
29 class U_I18N_API StringSegment
: public UMemory
{
31 StringSegment(const UnicodeString
& str
, bool ignoreCase
);
33 int32_t getOffset() const;
35 void setOffset(int32_t start
);
38 * Equivalent to <code>setOffset(getOffset()+delta)</code>.
41 * This method is usually called by a Matcher to register that a char was consumed. If the char is
42 * strong (it usually is, except for things like whitespace), follow this with a call to
43 * {@link ParsedNumber#setCharsConsumed}. For more information on strong chars, see that method.
45 void adjustOffset(int32_t delta
);
48 * Adjusts the offset by the width of the current code point, either 1 or 2 chars.
50 void adjustOffsetByCodePoint();
52 void setLength(int32_t length
);
56 int32_t length() const;
58 char16_t charAt(int32_t index
) const;
60 UChar32
codePointAt(int32_t index
) const;
62 UnicodeString
toUnicodeString() const;
64 const UnicodeString
toTempUnicodeString() const;
67 * Returns the first code point in the string segment, or -1 if the string starts with an invalid
71 * <strong>Important:</strong> Most of the time, you should use {@link #startsWith}, which handles case
72 * folding logic, instead of this method.
74 UChar32
getCodePoint() const;
77 * Returns true if the first code point of this StringSegment equals the given code point.
80 * This method will perform case folding if case folding is enabled for the parser.
82 bool startsWith(UChar32 otherCp
) const;
85 * Returns true if the first code point of this StringSegment is in the given UnicodeSet.
87 bool startsWith(const UnicodeSet
& uniset
) const;
90 * Returns true if there is at least one code point of overlap between this StringSegment and the
91 * given UnicodeString.
93 bool startsWith(const UnicodeString
& other
) const;
96 * Returns the length of the prefix shared by this StringSegment and the given UnicodeString. For
97 * example, if this string segment is "aab", and the char sequence is "aac", this method returns 2,
98 * since the first 2 characters are the same.
101 * This method only returns offsets along code point boundaries.
104 * This method will perform case folding if case folding was enabled in the constructor.
107 * IMPORTANT: The given UnicodeString must not be empty! It is the caller's responsibility to check.
109 int32_t getCommonPrefixLength(const UnicodeString
& other
);
112 * Like {@link #getCommonPrefixLength}, but never performs case folding, even if case folding is
113 * enabled for the parser.
115 int32_t getCaseSensitivePrefixLength(const UnicodeString
& other
);
117 bool operator==(const UnicodeString
& other
) const;
120 const UnicodeString
& fStr
;
125 int32_t getPrefixLengthInternal(const UnicodeString
& other
, bool foldCase
);
127 static bool codePointsEqual(UChar32 cp1
, UChar32 cp2
, bool foldCase
);
133 #endif //__NUMPARSE_STRINGSEGMENT_H__
134 #endif /* #if !UCONFIG_NO_FORMATTING */