]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ********************************************************************** | |
3 | * Copyright (C) 2001, International Business Machines | |
4 | * Corporation and others. All Rights Reserved. | |
5 | ********************************************************************** | |
6 | * Date Name Description | |
7 | * 05/24/01 aliu Creation. | |
8 | ********************************************************************** | |
9 | */ | |
10 | ||
11 | #include "unicode/utypes.h" | |
12 | ||
13 | #if !UCONFIG_NO_TRANSLITERATION | |
14 | ||
15 | #include "unicode/uchar.h" | |
16 | #include "unicode/ustring.h" | |
17 | #include "tolowtrn.h" | |
18 | #include "ustr_imp.h" | |
19 | #include "cpputils.h" | |
20 | ||
21 | U_NAMESPACE_BEGIN | |
22 | ||
23 | const char LowercaseTransliterator::fgClassID=0; | |
24 | ||
25 | const char LowercaseTransliterator::_ID[] = "Any-Lower"; | |
26 | ||
27 | /** | |
28 | * Constructs a transliterator. | |
29 | */ | |
30 | LowercaseTransliterator::LowercaseTransliterator(const Locale& theLoc) : Transliterator(_ID, 0), | |
31 | loc(theLoc) , buffer(0) | |
32 | { | |
33 | buffer = (UChar *)uprv_malloc(u_getMaxCaseExpansion()*sizeof(buffer[0])); | |
34 | } | |
35 | ||
36 | /** | |
37 | * Destructor. | |
38 | */ | |
39 | LowercaseTransliterator::~LowercaseTransliterator() { | |
40 | uprv_free(buffer); | |
41 | } | |
42 | ||
43 | /** | |
44 | * Copy constructor. | |
45 | */ | |
46 | LowercaseTransliterator::LowercaseTransliterator(const LowercaseTransliterator& o) : | |
47 | Transliterator(o), | |
48 | loc(o.loc), buffer(0) | |
49 | { | |
50 | buffer = (UChar *)uprv_malloc(u_getMaxCaseExpansion()*sizeof(buffer[0])); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Assignment operator. | |
55 | */ | |
56 | LowercaseTransliterator& LowercaseTransliterator::operator=( | |
57 | const LowercaseTransliterator& o) { | |
58 | Transliterator::operator=(o); | |
59 | loc = o.loc; | |
60 | uprv_arrayCopy((const UChar*)o.buffer, 0, this->buffer, 0, u_getMaxCaseExpansion()); | |
61 | return *this; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Transliterator API. | |
66 | */ | |
67 | Transliterator* LowercaseTransliterator::clone(void) const { | |
68 | return new LowercaseTransliterator(*this); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Implements {@link Transliterator#handleTransliterate}. | |
73 | */ | |
74 | void LowercaseTransliterator::handleTransliterate(Replaceable& text, | |
75 | UTransPosition& offsets, | |
76 | UBool isIncremental) const | |
77 | { | |
78 | int32_t textPos = offsets.start; | |
79 | if (textPos >= offsets.limit) return; | |
80 | ||
81 | // get string for context | |
82 | ||
83 | UnicodeString original; | |
84 | text.extractBetween(offsets.contextStart, offsets.contextLimit, original); | |
85 | ||
86 | UCharIterator iter; | |
87 | uiter_setReplaceable(&iter, &text); | |
88 | iter.start = offsets.contextStart; | |
89 | iter.limit = offsets.contextLimit; | |
90 | ||
91 | // Walk through original string | |
92 | // If there is a case change, modify corresponding position in replaceable | |
93 | ||
94 | int32_t i = textPos - offsets.contextStart; | |
95 | int32_t limit = offsets.limit - offsets.contextStart; | |
96 | UChar32 cp; | |
97 | int32_t oldLen; | |
98 | ||
99 | for (; i < limit; ) { | |
100 | UTF_GET_CHAR(original.getBuffer(), 0, i, original.length(), cp); | |
101 | oldLen = UTF_CHAR_LENGTH(cp); | |
102 | i += oldLen; | |
103 | iter.index = i; // Point _past_ current char | |
104 | int32_t newLen = u_internalToLower(cp, &iter, buffer, u_getMaxCaseExpansion(), loc.getName()); | |
105 | if (newLen >= 0) { | |
106 | UnicodeString temp(buffer, newLen); | |
107 | text.handleReplaceBetween(textPos, textPos + oldLen, temp); | |
108 | if (newLen != oldLen) { | |
109 | textPos += newLen; | |
110 | offsets.limit += newLen - oldLen; | |
111 | offsets.contextLimit += newLen - oldLen; | |
112 | continue; | |
113 | } | |
114 | } | |
115 | textPos += oldLen; | |
116 | } | |
117 | offsets.start = offsets.limit; | |
118 | } | |
119 | U_NAMESPACE_END | |
120 | ||
121 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |