1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker ******************************************************************************
5*0e209d39SAndroid Build Coastguard Worker *
6*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 2007, International Business Machines
7*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved.
8*0e209d39SAndroid Build Coastguard Worker *
9*0e209d39SAndroid Build Coastguard Worker ******************************************************************************
10*0e209d39SAndroid Build Coastguard Worker * file name: unisetspan.h
11*0e209d39SAndroid Build Coastguard Worker * encoding: UTF-8
12*0e209d39SAndroid Build Coastguard Worker * tab size: 8 (not used)
13*0e209d39SAndroid Build Coastguard Worker * indentation:4
14*0e209d39SAndroid Build Coastguard Worker *
15*0e209d39SAndroid Build Coastguard Worker * created on: 2007mar01
16*0e209d39SAndroid Build Coastguard Worker * created by: Markus W. Scherer
17*0e209d39SAndroid Build Coastguard Worker */
18*0e209d39SAndroid Build Coastguard Worker
19*0e209d39SAndroid Build Coastguard Worker #ifndef __UNISETSPAN_H__
20*0e209d39SAndroid Build Coastguard Worker #define __UNISETSPAN_H__
21*0e209d39SAndroid Build Coastguard Worker
22*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
23*0e209d39SAndroid Build Coastguard Worker #include "unicode/uniset.h"
24*0e209d39SAndroid Build Coastguard Worker
25*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
26*0e209d39SAndroid Build Coastguard Worker
27*0e209d39SAndroid Build Coastguard Worker /*
28*0e209d39SAndroid Build Coastguard Worker * Implement span() etc. for a set with strings.
29*0e209d39SAndroid Build Coastguard Worker * Avoid recursion because of its exponential complexity.
30*0e209d39SAndroid Build Coastguard Worker * Instead, try multiple paths at once and track them with an IndexList.
31*0e209d39SAndroid Build Coastguard Worker */
32*0e209d39SAndroid Build Coastguard Worker class UnicodeSetStringSpan : public UMemory {
33*0e209d39SAndroid Build Coastguard Worker public:
34*0e209d39SAndroid Build Coastguard Worker /*
35*0e209d39SAndroid Build Coastguard Worker * Which span() variant will be used?
36*0e209d39SAndroid Build Coastguard Worker * The object is either built for one variant and used once,
37*0e209d39SAndroid Build Coastguard Worker * or built for all and may be used many times.
38*0e209d39SAndroid Build Coastguard Worker */
39*0e209d39SAndroid Build Coastguard Worker enum {
40*0e209d39SAndroid Build Coastguard Worker FWD = 0x20,
41*0e209d39SAndroid Build Coastguard Worker BACK = 0x10,
42*0e209d39SAndroid Build Coastguard Worker UTF16 = 8,
43*0e209d39SAndroid Build Coastguard Worker UTF8 = 4,
44*0e209d39SAndroid Build Coastguard Worker CONTAINED = 2,
45*0e209d39SAndroid Build Coastguard Worker NOT_CONTAINED = 1,
46*0e209d39SAndroid Build Coastguard Worker
47*0e209d39SAndroid Build Coastguard Worker ALL = 0x3f,
48*0e209d39SAndroid Build Coastguard Worker
49*0e209d39SAndroid Build Coastguard Worker FWD_UTF16_CONTAINED = FWD | UTF16 | CONTAINED,
50*0e209d39SAndroid Build Coastguard Worker FWD_UTF16_NOT_CONTAINED = FWD | UTF16 | NOT_CONTAINED,
51*0e209d39SAndroid Build Coastguard Worker FWD_UTF8_CONTAINED = FWD | UTF8 | CONTAINED,
52*0e209d39SAndroid Build Coastguard Worker FWD_UTF8_NOT_CONTAINED = FWD | UTF8 | NOT_CONTAINED,
53*0e209d39SAndroid Build Coastguard Worker BACK_UTF16_CONTAINED = BACK | UTF16 | CONTAINED,
54*0e209d39SAndroid Build Coastguard Worker BACK_UTF16_NOT_CONTAINED= BACK | UTF16 | NOT_CONTAINED,
55*0e209d39SAndroid Build Coastguard Worker BACK_UTF8_CONTAINED = BACK | UTF8 | CONTAINED,
56*0e209d39SAndroid Build Coastguard Worker BACK_UTF8_NOT_CONTAINED = BACK | UTF8 | NOT_CONTAINED
57*0e209d39SAndroid Build Coastguard Worker };
58*0e209d39SAndroid Build Coastguard Worker
59*0e209d39SAndroid Build Coastguard Worker UnicodeSetStringSpan(const UnicodeSet &set, const UVector &setStrings, uint32_t which);
60*0e209d39SAndroid Build Coastguard Worker
61*0e209d39SAndroid Build Coastguard Worker // Copy constructor. Assumes which==ALL for a frozen set.
62*0e209d39SAndroid Build Coastguard Worker UnicodeSetStringSpan(const UnicodeSetStringSpan &otherStringSpan, const UVector &newParentSetStrings);
63*0e209d39SAndroid Build Coastguard Worker
64*0e209d39SAndroid Build Coastguard Worker ~UnicodeSetStringSpan();
65*0e209d39SAndroid Build Coastguard Worker
66*0e209d39SAndroid Build Coastguard Worker /*
67*0e209d39SAndroid Build Coastguard Worker * Do the strings need to be checked in span() etc.?
68*0e209d39SAndroid Build Coastguard Worker * @return true if strings need to be checked (call span() here),
69*0e209d39SAndroid Build Coastguard Worker * false if not (use a BMPSet for best performance).
70*0e209d39SAndroid Build Coastguard Worker */
71*0e209d39SAndroid Build Coastguard Worker inline UBool needsStringSpanUTF16();
72*0e209d39SAndroid Build Coastguard Worker inline UBool needsStringSpanUTF8();
73*0e209d39SAndroid Build Coastguard Worker
74*0e209d39SAndroid Build Coastguard Worker // For fast UnicodeSet::contains(c).
75*0e209d39SAndroid Build Coastguard Worker inline UBool contains(UChar32 c) const;
76*0e209d39SAndroid Build Coastguard Worker
77*0e209d39SAndroid Build Coastguard Worker int32_t span(const char16_t *s, int32_t length, USetSpanCondition spanCondition) const;
78*0e209d39SAndroid Build Coastguard Worker
79*0e209d39SAndroid Build Coastguard Worker int32_t spanBack(const char16_t *s, int32_t length, USetSpanCondition spanCondition) const;
80*0e209d39SAndroid Build Coastguard Worker
81*0e209d39SAndroid Build Coastguard Worker int32_t spanUTF8(const uint8_t *s, int32_t length, USetSpanCondition spanCondition) const;
82*0e209d39SAndroid Build Coastguard Worker
83*0e209d39SAndroid Build Coastguard Worker int32_t spanBackUTF8(const uint8_t *s, int32_t length, USetSpanCondition spanCondition) const;
84*0e209d39SAndroid Build Coastguard Worker
85*0e209d39SAndroid Build Coastguard Worker private:
86*0e209d39SAndroid Build Coastguard Worker // Special spanLength byte values.
87*0e209d39SAndroid Build Coastguard Worker enum {
88*0e209d39SAndroid Build Coastguard Worker // The spanLength is >=0xfe.
89*0e209d39SAndroid Build Coastguard Worker LONG_SPAN=0xfe,
90*0e209d39SAndroid Build Coastguard Worker // All code points in the string are contained in the parent set.
91*0e209d39SAndroid Build Coastguard Worker ALL_CP_CONTAINED=0xff
92*0e209d39SAndroid Build Coastguard Worker };
93*0e209d39SAndroid Build Coastguard Worker
94*0e209d39SAndroid Build Coastguard Worker // Add a starting or ending string character to the spanNotSet
95*0e209d39SAndroid Build Coastguard Worker // so that a character span ends before any string.
96*0e209d39SAndroid Build Coastguard Worker void addToSpanNotSet(UChar32 c);
97*0e209d39SAndroid Build Coastguard Worker
98*0e209d39SAndroid Build Coastguard Worker int32_t spanNot(const char16_t *s, int32_t length) const;
99*0e209d39SAndroid Build Coastguard Worker int32_t spanNotBack(const char16_t *s, int32_t length) const;
100*0e209d39SAndroid Build Coastguard Worker int32_t spanNotUTF8(const uint8_t *s, int32_t length) const;
101*0e209d39SAndroid Build Coastguard Worker int32_t spanNotBackUTF8(const uint8_t *s, int32_t length) const;
102*0e209d39SAndroid Build Coastguard Worker
103*0e209d39SAndroid Build Coastguard Worker // Set for span(). Same as parent but without strings.
104*0e209d39SAndroid Build Coastguard Worker UnicodeSet spanSet;
105*0e209d39SAndroid Build Coastguard Worker
106*0e209d39SAndroid Build Coastguard Worker // Set for span(not contained).
107*0e209d39SAndroid Build Coastguard Worker // Same as spanSet, plus characters that start or end strings.
108*0e209d39SAndroid Build Coastguard Worker UnicodeSet *pSpanNotSet;
109*0e209d39SAndroid Build Coastguard Worker
110*0e209d39SAndroid Build Coastguard Worker // The strings of the parent set.
111*0e209d39SAndroid Build Coastguard Worker const UVector &strings;
112*0e209d39SAndroid Build Coastguard Worker
113*0e209d39SAndroid Build Coastguard Worker // Pointer to the UTF-8 string lengths.
114*0e209d39SAndroid Build Coastguard Worker // Also pointer to further allocated storage for meta data and
115*0e209d39SAndroid Build Coastguard Worker // UTF-8 string contents as necessary.
116*0e209d39SAndroid Build Coastguard Worker int32_t *utf8Lengths;
117*0e209d39SAndroid Build Coastguard Worker
118*0e209d39SAndroid Build Coastguard Worker // Pointer to the part of the (utf8Lengths) memory block that stores
119*0e209d39SAndroid Build Coastguard Worker // the lengths of span(), spanBack() etc. for each string.
120*0e209d39SAndroid Build Coastguard Worker uint8_t *spanLengths;
121*0e209d39SAndroid Build Coastguard Worker
122*0e209d39SAndroid Build Coastguard Worker // Pointer to the part of the (utf8Lengths) memory block that stores
123*0e209d39SAndroid Build Coastguard Worker // the UTF-8 versions of the parent set's strings.
124*0e209d39SAndroid Build Coastguard Worker uint8_t *utf8;
125*0e209d39SAndroid Build Coastguard Worker
126*0e209d39SAndroid Build Coastguard Worker // Number of bytes for all UTF-8 versions of strings together.
127*0e209d39SAndroid Build Coastguard Worker int32_t utf8Length;
128*0e209d39SAndroid Build Coastguard Worker
129*0e209d39SAndroid Build Coastguard Worker // Maximum lengths of relevant strings.
130*0e209d39SAndroid Build Coastguard Worker int32_t maxLength16;
131*0e209d39SAndroid Build Coastguard Worker int32_t maxLength8;
132*0e209d39SAndroid Build Coastguard Worker
133*0e209d39SAndroid Build Coastguard Worker // Set up for all variants of span()?
134*0e209d39SAndroid Build Coastguard Worker UBool all;
135*0e209d39SAndroid Build Coastguard Worker
136*0e209d39SAndroid Build Coastguard Worker // Memory for small numbers and lengths of strings.
137*0e209d39SAndroid Build Coastguard Worker // For example, for 8 strings:
138*0e209d39SAndroid Build Coastguard Worker // 8 UTF-8 lengths, 8*4 bytes span lengths, 8*2 3-byte UTF-8 characters
139*0e209d39SAndroid Build Coastguard Worker // = 112 bytes = int32_t[28].
140*0e209d39SAndroid Build Coastguard Worker int32_t staticLengths[32];
141*0e209d39SAndroid Build Coastguard Worker };
142*0e209d39SAndroid Build Coastguard Worker
needsStringSpanUTF16()143*0e209d39SAndroid Build Coastguard Worker UBool UnicodeSetStringSpan::needsStringSpanUTF16() {
144*0e209d39SAndroid Build Coastguard Worker return (UBool)(maxLength16!=0);
145*0e209d39SAndroid Build Coastguard Worker }
146*0e209d39SAndroid Build Coastguard Worker
needsStringSpanUTF8()147*0e209d39SAndroid Build Coastguard Worker UBool UnicodeSetStringSpan::needsStringSpanUTF8() {
148*0e209d39SAndroid Build Coastguard Worker return (UBool)(maxLength8!=0);
149*0e209d39SAndroid Build Coastguard Worker }
150*0e209d39SAndroid Build Coastguard Worker
contains(UChar32 c)151*0e209d39SAndroid Build Coastguard Worker UBool UnicodeSetStringSpan::contains(UChar32 c) const {
152*0e209d39SAndroid Build Coastguard Worker return spanSet.contains(c);
153*0e209d39SAndroid Build Coastguard Worker }
154*0e209d39SAndroid Build Coastguard Worker
155*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
156*0e209d39SAndroid Build Coastguard Worker
157*0e209d39SAndroid Build Coastguard Worker #endif
158