1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Util.h"
9 #include "SQLCollations.h"
14 ////////////////////////////////////////////////////////////////////////////////
15 //// Local Helper Functions
20 * Helper function for the UTF-8 locale collations.
23 * The Service that owns the nsICollation used by this collation.
25 * The number of bytes in aStr1.
27 * The string to be compared against aStr2 as provided by SQLite. It
28 * must be a non-null-terminated char* buffer.
30 * The number of bytes in aStr2.
32 * The string to be compared against aStr1 as provided by SQLite. It
33 * must be a non-null-terminated char* buffer.
34 * @param aComparisonStrength
35 * The sorting strength, one of the nsICollation constants.
36 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative number.
37 * If aStr1 > aStr2, returns a positive number. If aStr1 == aStr2,
41 localeCollationHelper8(void *aService
,
46 int32_t aComparisonStrength
)
48 NS_ConvertUTF8toUTF16
str1(static_cast<const char *>(aStr1
), aLen1
);
49 NS_ConvertUTF8toUTF16
str2(static_cast<const char *>(aStr2
), aLen2
);
50 Service
*serv
= static_cast<Service
*>(aService
);
51 return serv
->localeCompareStrings(str1
, str2
, aComparisonStrength
);
55 * Helper function for the UTF-16 locale collations.
58 * The Service that owns the nsICollation used by this collation.
60 * The number of bytes (not characters) in aStr1.
62 * The string to be compared against aStr2 as provided by SQLite. It
63 * must be a non-null-terminated PRUnichar* buffer.
65 * The number of bytes (not characters) in aStr2.
67 * The string to be compared against aStr1 as provided by SQLite. It
68 * must be a non-null-terminated PRUnichar* buffer.
69 * @param aComparisonStrength
70 * The sorting strength, one of the nsICollation constants.
71 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative number.
72 * If aStr1 > aStr2, returns a positive number. If aStr1 == aStr2,
76 localeCollationHelper16(void *aService
,
81 int32_t aComparisonStrength
)
83 const PRUnichar
*buf1
= static_cast<const PRUnichar
*>(aStr1
);
84 const PRUnichar
*buf2
= static_cast<const PRUnichar
*>(aStr2
);
86 // The second argument to the nsDependentSubstring constructor is exclusive:
87 // It points to the PRUnichar immediately following the last one in the target
88 // substring. Since aLen1 and aLen2 are in bytes, divide by sizeof(PRUnichar)
89 // so that the pointer arithmetic is correct.
90 nsDependentSubstring
str1(buf1
, buf1
+ (aLen1
/ sizeof(PRUnichar
)));
91 nsDependentSubstring
str2(buf2
, buf2
+ (aLen2
/ sizeof(PRUnichar
)));
92 Service
*serv
= static_cast<Service
*>(aService
);
93 return serv
->localeCompareStrings(str1
, str2
, aComparisonStrength
);
96 // This struct is used only by registerCollations below, but ISO C++98 forbids
97 // instantiating a template dependent on a locally-defined type. Boo-urns!
101 int(*xCompare
)(void*, int, const void*, int, const void*);
104 } // anonymous namespace
106 ////////////////////////////////////////////////////////////////////////////////
107 //// Exposed Functions
110 registerCollations(sqlite3
*aDB
,
113 Collations collations
[] = {
117 {"locale_case_sensitive",
119 localeCollationCaseSensitive8
},
120 {"locale_accent_sensitive",
122 localeCollationAccentSensitive8
},
123 {"locale_case_accent_sensitive",
125 localeCollationCaseAccentSensitive8
},
129 {"locale_case_sensitive",
131 localeCollationCaseSensitive16
},
132 {"locale_accent_sensitive",
134 localeCollationAccentSensitive16
},
135 {"locale_case_accent_sensitive",
137 localeCollationCaseAccentSensitive16
},
141 for (size_t i
= 0; SQLITE_OK
== rv
&& i
< ArrayLength(collations
); ++i
) {
142 struct Collations
*p
= &collations
[i
];
143 rv
= ::sqlite3_create_collation(aDB
, p
->zName
, p
->enc
, aService
,
150 ////////////////////////////////////////////////////////////////////////////////
154 localeCollation8(void *aService
,
160 return localeCollationHelper8(aService
, aLen1
, aStr1
, aLen2
, aStr2
,
161 nsICollation::kCollationCaseInSensitive
);
165 localeCollationCaseSensitive8(void *aService
,
171 return localeCollationHelper8(aService
, aLen1
, aStr1
, aLen2
, aStr2
,
172 nsICollation::kCollationAccentInsenstive
);
176 localeCollationAccentSensitive8(void *aService
,
182 return localeCollationHelper8(aService
, aLen1
, aStr1
, aLen2
, aStr2
,
183 nsICollation::kCollationCaseInsensitiveAscii
);
187 localeCollationCaseAccentSensitive8(void *aService
,
193 return localeCollationHelper8(aService
, aLen1
, aStr1
, aLen2
, aStr2
,
194 nsICollation::kCollationCaseSensitive
);
198 localeCollation16(void *aService
,
204 return localeCollationHelper16(aService
, aLen1
, aStr1
, aLen2
, aStr2
,
205 nsICollation::kCollationCaseInSensitive
);
209 localeCollationCaseSensitive16(void *aService
,
215 return localeCollationHelper16(aService
, aLen1
, aStr1
, aLen2
, aStr2
,
216 nsICollation::kCollationAccentInsenstive
);
220 localeCollationAccentSensitive16(void *aService
,
226 return localeCollationHelper16(aService
, aLen1
, aStr1
, aLen2
, aStr2
,
227 nsICollation::kCollationCaseInsensitiveAscii
);
231 localeCollationCaseAccentSensitive16(void *aService
,
237 return localeCollationHelper16(aService
, aLen1
, aStr1
, aLen2
, aStr2
,
238 nsICollation::kCollationCaseSensitive
);
241 } // namespace storage
242 } // namespace mozilla