]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unitohex.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / unitohex.h
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
3* Copyright (C) 1999-2003, International Business Machines Corporation and others. All Rights Reserved.
4**********************************************************************
5* Date Name Description
6* 11/17/99 aliu Creation.
7**********************************************************************
8*/
9#ifndef UNITOHEX_H
10#define UNITOHEX_H
11
12#include "unicode/utypes.h"
13
14#if !UCONFIG_NO_TRANSLITERATION
15
16#include "unicode/translit.h"
17#include "unicode/unistr.h"
18
19U_NAMESPACE_BEGIN
20
21class UnicodeFilter;
22
23/**
24 * A transliterator that converts from Unicode characters to
25 * hexadecimal Unicode escape sequences. It outputs a
26 * prefix specified in the constructor and optionally converts the hex
27 * digits to uppercase.
28 *
29 * <p>The format of the output is set by a pattern. This pattern
30 * follows the same syntax as <code>HexToUnicodeTransliterator</code>,
31 * except it does not allow multiple specifications. The pattern sets
32 * the prefix string, suffix string, and minimum and maximum digit
33 * count. There are no setters or getters for these attributes; they
34 * are set only through the pattern.
35 *
36 * <p>The setUppercase() and isUppercase() methods control whether 'a'
37 * through 'f' or 'A' through 'F' are output as hex digits. This is
38 * not controlled through the pattern; only through the methods. The
39 * default is uppercase.
40 *
41 * @author Alan Liu
42 * @internal Use transliterator factory methods instead since this class will be removed in that release.
43 */
44class U_I18N_API UnicodeToHexTransliterator : public Transliterator {
45
46private:
47
48 // Character constants defined here to avoid ASCII dependency
49 enum {
50 ZERO = 0x0030, // '0'
51 POUND = 0x0023, // '#'
52 BACKSLASH = 0x005C // '\\'
53 };
54
55 static const UChar HEX_DIGITS[32];
56
57 /**
58 * ID for this transliterator.
59 */
60 static const char _ID[];
61
62 /**
63 * The pattern set by applyPattern() and returned by toPattern().
64 */
65 UnicodeString pattern;
66
67 /**
68 * The string preceding the hex digits, parsed from the pattern.
69 */
70 UnicodeString prefix;
71
72 /**
73 * The string following the hex digits, parsed from the pattern.
74 */
75 UnicodeString suffix;
76
77 /**
78 * The minimum number of hex digits to output, between 1 and 4,
79 * inclusive. Parsed from the pattern.
80 */
81 int8_t minDigits;
82
83 /**
84 * If TRUE, output uppercase hex digits; otherwise output
85 * lowercase. Set by setUppercase() and returned by isUppercase().
86 */
87 UBool uppercase;
88
89 /**
90 * The address of this static class variable serves as this class's ID
91 * for ICU "poor man's RTTI".
92 */
93 static const char fgClassID;
94
95public:
96
97 /**
98 * Constructs a transliterator.
99 * @param pattern The pattern for this transliterator. See
100 * applyPattern() for pattern syntax.
101 * @param isUppercase if true, the four hex digits will be
102 * converted to uppercase; otherwise they will be lowercase.
103 * @param adoptedFilter the filter for this transliterator, or
104 * NULL if none. Adopted by this transliterator.
105 * @param status Error code indicating success or failure
106 * to parse pattern.
107 * @internal Use transliterator factory methods instead since this class will be removed in that release.
108 */
109 UnicodeToHexTransliterator(const UnicodeString& pattern,
110 UBool isUppercase,
111 UnicodeFilter* adoptedFilter,
112 UErrorCode& status);
113
114 /**
115 * Constructs an uppercase transliterator with no filter.
116 * @param pattern The pattern for this transliterator. See
117 * applyPattern() for pattern syntax.
118 * @param status Error code indicating success or failure
119 * to parse pattern.
120 * @internal Use transliterator factory methods instead since this class will be removed in that release.
121 */
122 UnicodeToHexTransliterator(const UnicodeString& pattern,
123 UErrorCode& status);
124
125 /**
126 * Constructs a transliterator with the default prefix "\u"
127 * that outputs uppercase hex digits.
128 * @internal Use transliterator factory methods instead since this class will be removed in that release.
129 */
130 UnicodeToHexTransliterator(UnicodeFilter* adoptedFilter = 0);
131
132 /**
133 * Destructor.
134 * @internal Use transliterator factory methods instead since this class will be removed in that release.
135 */
136 virtual ~UnicodeToHexTransliterator();
137
138 /**
139 * Copy constructor.
140 * @internal Use transliterator factory methods instead since this class will be removed in that release.
141 */
142 UnicodeToHexTransliterator(const UnicodeToHexTransliterator&);
143
144 /**
145 * Assignment operator.
146 * @stable ICU 2.0
147 */
148 UnicodeToHexTransliterator& operator=(const UnicodeToHexTransliterator&);
149
150 /**
151 * Transliterator API.
152 * @internal Use transliterator factory methods instead since this class will be removed in that release.
153 */
154 virtual Transliterator* clone(void) const;
155
156 /**
157 * Set the pattern recognized by this transliterator. The pattern
158 * must contain zero or more prefix characters, one or more digit
159 * characters, and zero or more suffix characters. The digit
160 * characters indicates optional digits ('#') followed by required
161 * digits ('0'). The total number of digits cannot exceed 4, and
162 * must be at least 1 required digit. Use a backslash ('\\') to
163 * escape any of the special characters. An empty pattern is not
164 * allowed.
165 *
166 * <p>Example: "U+0000" specifies a prefix of "U+", exactly four
167 * digits, and no suffix. "<###0>" has a prefix of "<", between
168 * one and four digits, and a suffix of ">".
169 *
170 * <p><pre>
171 * pattern := prefix-char* digit-spec suffix-char*
172 * digit-spec := '#'* '0'+
173 * prefix-char := [^special-char] | '\\' special-char
174 * suffix-char := [^special-char] | '\\' special-char
175 * special-char := ';' | '0' | '#' | '\\'
176 * </pre>
177 *
178 * <p>Limitations: There is no way to set the uppercase attribute
179 * in the pattern. (applyPattern() does not alter the uppercase
180 * attribute.)
181 * @internal Use transliterator factory methods instead since this class will be removed in that release.
182 */
183 void applyPattern(const UnicodeString& thePattern, UErrorCode& status);
184
185 /**
186 * Return this transliterator's pattern.
187 * @internal Use transliterator factory methods instead since this class will be removed in that release.
188 */
189 const UnicodeString& toPattern(void) const;
190
191 /**
192 * Returns true if this transliterator outputs uppercase hex digits.
193 * @internal Use transliterator factory methods instead since this class will be removed in that release.
194 */
195 virtual UBool isUppercase(void) const;
196
197 /**
198 * Sets if this transliterator outputs uppercase hex digits.
199 * @internal Use transliterator factory methods instead since this class will be removed in that release.
200 */
201 virtual void setUppercase(UBool outputUppercase);
202
203 /**
204 * Implements {@link Transliterator#handleTransliterate}.
205 * @internal Use transliterator factory methods instead since this class will be removed in that release.
206 */
207 virtual void handleTransliterate(Replaceable& text, UTransPosition& offsets,
208 UBool isIncremental) const;
209
210 /**
211 * ICU "poor man's RTTI", returns a UClassID for the actual class.
212 *
213 * @draft ICU 2.2
214 */
215 virtual inline UClassID getDynamicClassID() const;
216
217 /**
218 * ICU "poor man's RTTI", returns a UClassID for this class.
219 *
220 * @draft ICU 2.2
221 */
222 static inline UClassID getStaticClassID();
223};
224
225inline UnicodeToHexTransliterator::~UnicodeToHexTransliterator() {}
226
227inline UClassID
228UnicodeToHexTransliterator::getStaticClassID()
229{ return (UClassID)&fgClassID; }
230
231inline UClassID
232UnicodeToHexTransliterator::getDynamicClassID() const
233{ return UnicodeToHexTransliterator::getStaticClassID(); }
234
235U_NAMESPACE_END
236#endif /* #if !UCONFIG_NO_TRANSLITERATION */
237
238#endif