1 /*
2  *******************************************************************************
3  *
4  *   Copyright (c) 1995-2013 International Business Machines Corporation and others
5  *
6  *   All rights reserved.
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining a copy of
9  *   this software and associated documentation files (the "Software"), to deal in
10  *   the Software without restriction, including without limitation the rights to
11  *   use, copy, modify, merge, publish, distribute, and/or sell copies of the
12  *   Software, and to permit persons to whom the Software is furnished to do so,
13  *   provided that the above copyright notice(s) and this permission notice appear
14  *   in all copies of the Software and that both the above copyright notice(s) and
15  *   this permission notice appear in supporting documentation.
16  *
17  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
20  *   NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE
21  *   LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY
22  *   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23  *   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
24  *   CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25  *
26  *   Except as contained in this notice, the name of a copyright holder shall not be
27  *   used in advertising or otherwise to promote the sale, use or other dealings in
28  *   this Software without prior written authorization of the copyright holder.
29  *
30  *******************************************************************************
31  *   file name:  scrptrun.h
32  *
33  *   created on: 10/17/2001
34  *   created by: Eric R. Mader
35  */
36 
37 #ifndef SCRPTRUN_H
38 #define SCRPTRUN_H
39 
40 #include <unicode/utypes.h>
41 #include <unicode/uobject.h>
42 #include <unicode/uscript.h>
43 #include <vector>
44 
45 using namespace icu;
46 
47 struct ScriptRecord
48 {
49     UChar32 startChar;
50     UChar32 endChar;
51     UScriptCode scriptCode;
52 };
53 
54 struct ParenStackEntry
55 {
56     int32_t pairIndex;
57     UScriptCode scriptCode;
ParenStackEntryParenStackEntry58     ParenStackEntry()
59       : pairIndex(0)
60       , scriptCode(USCRIPT_INVALID_CODE)
61     {
62     }
63 };
64 
65 class ScriptRun : public UObject {
66 public:
67     ScriptRun();
68 
69     ScriptRun(const UChar chars[], int32_t length);
70 
71     ScriptRun(const UChar chars[], int32_t start, int32_t length);
72 
73     void reset();
74 
75     void reset(int32_t start, int32_t count);
76 
77     void reset(const UChar chars[], int32_t start, int32_t length);
78 
79     int32_t getScriptStart();
80 
81     int32_t getScriptEnd();
82 
83     UScriptCode getScriptCode();
84 
85     UBool next();
86 
87     /**
88 s     * ICU "poor man's RTTI", returns a UClassID for the actual class.
89      *
90      * @stable ICU 2.2
91      */
getDynamicClassID()92     virtual inline UClassID getDynamicClassID() const { return getStaticClassID(); }
93 
94     /**
95      * ICU "poor man's RTTI", returns a UClassID for this class.
96      *
97      * @stable ICU 2.2
98      */
getStaticClassID()99     static inline UClassID getStaticClassID() { return static_cast<UClassID>(const_cast<char *>(&fgClassID)); }
100 
101 private:
102 
103     int32_t charStart;
104     int32_t charLimit;
105     const UChar *charArray;
106 
107     int32_t scriptStart;
108     int32_t scriptEnd;
109     UScriptCode scriptCode;
110 
111     std::vector<ParenStackEntry> parenStack;
112     int32_t parenSP;
113 
114     /**
115      * The address of this static class variable serves as this class's ID
116      * for ICU "poor man's RTTI".
117      */
118     static const char fgClassID;
119 };
120 
ScriptRun()121 inline ScriptRun::ScriptRun()
122 {
123     reset(NULL, 0, 0);
124 }
125 
ScriptRun(const UChar chars[],int32_t length)126 inline ScriptRun::ScriptRun(const UChar chars[], int32_t length)
127 {
128     reset(chars, 0, length);
129 }
130 
ScriptRun(const UChar chars[],int32_t start,int32_t length)131 inline ScriptRun::ScriptRun(const UChar chars[], int32_t start, int32_t length)
132 {
133     reset(chars, start, length);
134 }
135 
getScriptStart()136 inline int32_t ScriptRun::getScriptStart()
137 {
138     return scriptStart;
139 }
140 
getScriptEnd()141 inline int32_t ScriptRun::getScriptEnd()
142 {
143     return scriptEnd;
144 }
145 
getScriptCode()146 inline UScriptCode ScriptRun::getScriptCode()
147 {
148     return scriptCode;
149 }
150 
reset()151 inline void ScriptRun::reset()
152 {
153     scriptStart = charStart;
154     scriptEnd   = charStart;
155     scriptCode  = USCRIPT_INVALID_CODE;
156     parenSP     = -1;
157     parenStack.resize(128);
158 }
159 
reset(int32_t start,int32_t length)160 inline void ScriptRun::reset(int32_t start, int32_t length)
161 {
162     charStart = start;
163     charLimit = start + length;
164 
165     reset();
166 }
167 
reset(const UChar chars[],int32_t start,int32_t length)168 inline void ScriptRun::reset(const UChar chars[], int32_t start, int32_t length)
169 {
170     charArray = chars;
171 
172     reset(start, length);
173 }
174 
175 #endif
176