]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/numparse_stringsegment.cpp
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / numparse_stringsegment.cpp
CommitLineData
0f5d89e8
A
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
8// Allow implicit conversion from char16_t* to UnicodeString for this file:
9// Helpful in toString methods and elsewhere.
10#define UNISTR_FROM_STRING_EXPLICIT
11
12#include "numparse_types.h"
13#include "numparse_stringsegment.h"
14#include "putilimp.h"
15#include "unicode/utf16.h"
16#include "unicode/uniset.h"
17
18using namespace icu;
19using namespace icu::numparse;
20using namespace icu::numparse::impl;
21
22
23StringSegment::StringSegment(const UnicodeString& str, bool ignoreCase)
24 : fStr(str), fStart(0), fEnd(str.length()),
25 fFoldCase(ignoreCase) {}
26
27int32_t StringSegment::getOffset() const {
28 return fStart;
29}
30
31void StringSegment::setOffset(int32_t start) {
32 fStart = start;
33}
34
35void StringSegment::adjustOffset(int32_t delta) {
36 fStart += delta;
37}
38
39void StringSegment::adjustOffsetByCodePoint() {
40 fStart += U16_LENGTH(getCodePoint());
41}
42
43void StringSegment::setLength(int32_t length) {
44 fEnd = fStart + length;
45}
46
47void StringSegment::resetLength() {
48 fEnd = fStr.length();
49}
50
51int32_t StringSegment::length() const {
52 return fEnd - fStart;
53}
54
55char16_t StringSegment::charAt(int32_t index) const {
56 return fStr.charAt(index + fStart);
57}
58
59UChar32 StringSegment::codePointAt(int32_t index) const {
60 return fStr.char32At(index + fStart);
61}
62
63UnicodeString StringSegment::toUnicodeString() const {
64 return UnicodeString(fStr.getBuffer() + fStart, fEnd - fStart);
65}
66
67const UnicodeString StringSegment::toTempUnicodeString() const {
68 // Use the readonly-aliasing constructor for efficiency.
69 return UnicodeString(FALSE, fStr.getBuffer() + fStart, fEnd - fStart);
70}
71
72UChar32 StringSegment::getCodePoint() const {
73 char16_t lead = fStr.charAt(fStart);
74 if (U16_IS_LEAD(lead) && fStart + 1 < fEnd) {
75 return fStr.char32At(fStart);
76 } else if (U16_IS_SURROGATE(lead)) {
77 return -1;
78 } else {
79 return lead;
80 }
81}
82
83bool StringSegment::startsWith(UChar32 otherCp) const {
84 return codePointsEqual(getCodePoint(), otherCp, fFoldCase);
85}
86
87bool StringSegment::startsWith(const UnicodeSet& uniset) const {
88 // TODO: Move UnicodeSet case-folding logic here.
89 // TODO: Handle string matches here instead of separately.
90 UChar32 cp = getCodePoint();
91 if (cp == -1) {
92 return false;
93 }
94 return uniset.contains(cp);
95}
96
97bool StringSegment::startsWith(const UnicodeString& other) const {
98 if (other.isBogus() || other.length() == 0 || length() == 0) {
99 return false;
100 }
101 int cp1 = getCodePoint();
102 int cp2 = other.char32At(0);
103 return codePointsEqual(cp1, cp2, fFoldCase);
104}
105
106int32_t StringSegment::getCommonPrefixLength(const UnicodeString& other) {
107 return getPrefixLengthInternal(other, fFoldCase);
108}
109
110int32_t StringSegment::getCaseSensitivePrefixLength(const UnicodeString& other) {
111 return getPrefixLengthInternal(other, false);
112}
113
114int32_t StringSegment::getPrefixLengthInternal(const UnicodeString& other, bool foldCase) {
115 U_ASSERT(other.length() > 0);
116 int32_t offset = 0;
117 for (; offset < uprv_min(length(), other.length());) {
118 // TODO: case-fold code points, not chars
119 char16_t c1 = charAt(offset);
120 char16_t c2 = other.charAt(offset);
121 if (!codePointsEqual(c1, c2, foldCase)) {
122 break;
123 }
124 offset++;
125 }
126 return offset;
127}
128
129bool StringSegment::codePointsEqual(UChar32 cp1, UChar32 cp2, bool foldCase) {
130 if (cp1 == cp2) {
131 return true;
132 }
133 if (!foldCase) {
134 return false;
135 }
136 cp1 = u_foldCase(cp1, TRUE);
137 cp2 = u_foldCase(cp2, TRUE);
138 return cp1 == cp2;
139}
140
141bool StringSegment::operator==(const UnicodeString& other) const {
142 return toTempUnicodeString() == other;
143}
144
145
146#endif /* #if !UCONFIG_NO_FORMATTING */