1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Unicode case conversion helpers.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corp..
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Alec Flett <alecf@netscape.com>
24 : * Benjamin Smedberg <benjamin@smedbergs.us>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef nsUnicharUtils_h__
41 : #define nsUnicharUtils_h__
42 :
43 : #include "nsStringGlue.h"
44 :
45 : /* (0x3131u <= (u) && (u) <= 0x318eu) => Hangul Compatibility Jamo */
46 : /* (0xac00u <= (u) && (u) <= 0xd7a3u) => Hangul Syllables */
47 : #define IS_CJ_CHAR(u) \
48 : ((0x2e80u <= (u) && (u) <= 0x312fu) || \
49 : (0x3190u <= (u) && (u) <= 0xabffu) || \
50 : (0xf900u <= (u) && (u) <= 0xfaffu) || \
51 : (0xff00u <= (u) && (u) <= 0xffefu) )
52 :
53 : void ToLowerCase(nsAString&);
54 : void ToUpperCase(nsAString&);
55 :
56 : void ToLowerCase(const nsAString& aSource, nsAString& aDest);
57 : void ToUpperCase(const nsAString& aSource, nsAString& aDest);
58 :
59 : PRUnichar ToLowerCase(PRUnichar);
60 : PRUnichar ToUpperCase(PRUnichar);
61 : PRUnichar ToTitleCase(PRUnichar);
62 :
63 : void ToLowerCase(const PRUnichar*, PRUnichar*, PRUint32);
64 : void ToUpperCase(const PRUnichar*, PRUnichar*, PRUint32);
65 :
66 0 : inline bool IsUpperCase(PRUnichar c) {
67 0 : return ToLowerCase(c) != c;
68 : }
69 :
70 : inline bool IsLowerCase(PRUnichar c) {
71 : return ToUpperCase(c) != c;
72 : }
73 :
74 : #ifdef MOZILLA_INTERNAL_API
75 :
76 : class nsCaseInsensitiveStringComparator : public nsStringComparator
77 22662 : {
78 : public:
79 : virtual PRInt32 operator() (const PRUnichar*,
80 : const PRUnichar*,
81 : PRUint32,
82 : PRUint32) const;
83 : };
84 :
85 : class nsCaseInsensitiveUTF8StringComparator : public nsCStringComparator
86 : {
87 : public:
88 : virtual PRInt32 operator() (const char*,
89 : const char*,
90 : PRUint32,
91 : PRUint32) const;
92 : };
93 :
94 : class nsCaseInsensitiveStringArrayComparator
95 : {
96 : public:
97 : template<class A, class B>
98 : bool Equals(const A& a, const B& b) const {
99 : return a.Equals(b, nsCaseInsensitiveStringComparator());
100 : }
101 : };
102 :
103 : class nsASCIICaseInsensitiveStringComparator : public nsStringComparator
104 : {
105 : public:
106 0 : nsASCIICaseInsensitiveStringComparator() {}
107 : virtual int operator() (const PRUnichar*,
108 : const PRUnichar*,
109 : PRUint32,
110 : PRUint32) const;
111 : };
112 :
113 : inline bool
114 0 : CaseInsensitiveFindInReadable(const nsAString& aPattern,
115 : nsAString::const_iterator& aSearchStart,
116 : nsAString::const_iterator& aSearchEnd)
117 : {
118 : return FindInReadable(aPattern, aSearchStart, aSearchEnd,
119 0 : nsCaseInsensitiveStringComparator());
120 : }
121 :
122 : inline bool
123 219 : CaseInsensitiveFindInReadable(const nsAString& aPattern,
124 : const nsAString& aHay)
125 : {
126 219 : nsAString::const_iterator searchBegin, searchEnd;
127 219 : return FindInReadable(aPattern, aHay.BeginReading(searchBegin),
128 219 : aHay.EndReading(searchEnd),
129 438 : nsCaseInsensitiveStringComparator());
130 : }
131 :
132 : #endif // MOZILLA_INTERNAL_API
133 :
134 : PRInt32
135 : CaseInsensitiveCompare(const PRUnichar *a, const PRUnichar *b, PRUint32 len);
136 :
137 : PRInt32
138 : CaseInsensitiveCompare(const char* aLeft, const char* aRight,
139 : PRUint32 aLeftBytes, PRUint32 aRightBytes);
140 :
141 : /**
142 : * This function determines whether the UTF-8 sequence pointed to by aLeft is
143 : * case-insensitively-equal to the UTF-8 sequence pointed to by aRight.
144 : *
145 : * aLeftEnd marks the first memory location past aLeft that is not part of
146 : * aLeft; aRightEnd similarly marks the end of aRight.
147 : *
148 : * The function assumes that aLeft < aLeftEnd and aRight < aRightEnd.
149 : *
150 : * The function stores the addresses of the next characters in the sequence
151 : * into aLeftNext and aRightNext. It's up to the caller to make sure that the
152 : * returned pointers are valid -- i.e. the function may return aLeftNext >=
153 : * aLeftEnd or aRightNext >= aRightEnd.
154 : *
155 : * If the function encounters invalid text, it sets aErr to true and returns
156 : * false, possibly leaving aLeftNext and aRightNext uninitialized. If the
157 : * function returns true, aErr is guaranteed to be false and both aLeftNext and
158 : * aRightNext are guaranteed to be initialized.
159 : */
160 : bool
161 : CaseInsensitiveUTF8CharsEqual(const char* aLeft, const char* aRight,
162 : const char* aLeftEnd, const char* aRightEnd,
163 : const char** aLeftNext, const char** aRightNext,
164 : bool* aErr);
165 :
166 : namespace mozilla {
167 :
168 : /**
169 : * Hash a UTF8 string as though it were a UTF16 string.
170 : *
171 : * The value returned is the same as if we converted the string to UTF16 and
172 : * then ran HashString() on the result.
173 : *
174 : * The given |length| is in bytes.
175 : */
176 : PRUint32
177 : HashUTF8AsUTF16(const char* aUTF8, PRUint32 aLength, bool* aErr);
178 :
179 : } // namespace mozilla
180 :
181 : #endif /* nsUnicharUtils_h__ */
|