storage/src/SQLCollations.cpp

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "mozilla/ArrayUtils.h"
michael@0 8
michael@0 9 #include "SQLCollations.h"
michael@0 10
michael@0 11 namespace mozilla {
michael@0 12 namespace storage {
michael@0 13
michael@0 14 ////////////////////////////////////////////////////////////////////////////////
michael@0 15 //// Local Helper Functions
michael@0 16
michael@0 17 namespace {
michael@0 18
michael@0 19 /**
michael@0 20 * Helper function for the UTF-8 locale collations.
michael@0 21 *
michael@0 22 * @param aService
michael@0 23 * The Service that owns the nsICollation used by this collation.
michael@0 24 * @param aLen1
michael@0 25 * The number of bytes in aStr1.
michael@0 26 * @param aStr1
michael@0 27 * The string to be compared against aStr2 as provided by SQLite. It
michael@0 28 * must be a non-null-terminated char* buffer.
michael@0 29 * @param aLen2
michael@0 30 * The number of bytes in aStr2.
michael@0 31 * @param aStr2
michael@0 32 * The string to be compared against aStr1 as provided by SQLite. It
michael@0 33 * must be a non-null-terminated char* buffer.
michael@0 34 * @param aComparisonStrength
michael@0 35 * The sorting strength, one of the nsICollation constants.
michael@0 36 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative number.
michael@0 37 * If aStr1 > aStr2, returns a positive number. If aStr1 == aStr2,
michael@0 38 * returns 0.
michael@0 39 */
michael@0 40 int
michael@0 41 localeCollationHelper8(void *aService,
michael@0 42 int aLen1,
michael@0 43 const void *aStr1,
michael@0 44 int aLen2,
michael@0 45 const void *aStr2,
michael@0 46 int32_t aComparisonStrength)
michael@0 47 {
michael@0 48 NS_ConvertUTF8toUTF16 str1(static_cast<const char *>(aStr1), aLen1);
michael@0 49 NS_ConvertUTF8toUTF16 str2(static_cast<const char *>(aStr2), aLen2);
michael@0 50 Service *serv = static_cast<Service *>(aService);
michael@0 51 return serv->localeCompareStrings(str1, str2, aComparisonStrength);
michael@0 52 }
michael@0 53
michael@0 54 /**
michael@0 55 * Helper function for the UTF-16 locale collations.
michael@0 56 *
michael@0 57 * @param aService
michael@0 58 * The Service that owns the nsICollation used by this collation.
michael@0 59 * @param aLen1
michael@0 60 * The number of bytes (not characters) in aStr1.
michael@0 61 * @param aStr1
michael@0 62 * The string to be compared against aStr2 as provided by SQLite. It
michael@0 63 * must be a non-null-terminated char16_t* buffer.
michael@0 64 * @param aLen2
michael@0 65 * The number of bytes (not characters) in aStr2.
michael@0 66 * @param aStr2
michael@0 67 * The string to be compared against aStr1 as provided by SQLite. It
michael@0 68 * must be a non-null-terminated char16_t* buffer.
michael@0 69 * @param aComparisonStrength
michael@0 70 * The sorting strength, one of the nsICollation constants.
michael@0 71 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative number.
michael@0 72 * If aStr1 > aStr2, returns a positive number. If aStr1 == aStr2,
michael@0 73 * returns 0.
michael@0 74 */
michael@0 75 int
michael@0 76 localeCollationHelper16(void *aService,
michael@0 77 int aLen1,
michael@0 78 const void *aStr1,
michael@0 79 int aLen2,
michael@0 80 const void *aStr2,
michael@0 81 int32_t aComparisonStrength)
michael@0 82 {
michael@0 83 const char16_t *buf1 = static_cast<const char16_t *>(aStr1);
michael@0 84 const char16_t *buf2 = static_cast<const char16_t *>(aStr2);
michael@0 85
michael@0 86 // The second argument to the nsDependentSubstring constructor is exclusive:
michael@0 87 // It points to the char16_t immediately following the last one in the target
michael@0 88 // substring. Since aLen1 and aLen2 are in bytes, divide by sizeof(char16_t)
michael@0 89 // so that the pointer arithmetic is correct.
michael@0 90 nsDependentSubstring str1(buf1, buf1 + (aLen1 / sizeof(char16_t)));
michael@0 91 nsDependentSubstring str2(buf2, buf2 + (aLen2 / sizeof(char16_t)));
michael@0 92 Service *serv = static_cast<Service *>(aService);
michael@0 93 return serv->localeCompareStrings(str1, str2, aComparisonStrength);
michael@0 94 }
michael@0 95
michael@0 96 // This struct is used only by registerCollations below, but ISO C++98 forbids
michael@0 97 // instantiating a template dependent on a locally-defined type. Boo-urns!
michael@0 98 struct Collations {
michael@0 99 const char *zName;
michael@0 100 int enc;
michael@0 101 int(*xCompare)(void*, int, const void*, int, const void*);
michael@0 102 };
michael@0 103
michael@0 104 } // anonymous namespace
michael@0 105
michael@0 106 ////////////////////////////////////////////////////////////////////////////////
michael@0 107 //// Exposed Functions
michael@0 108
michael@0 109 int
michael@0 110 registerCollations(sqlite3 *aDB,
michael@0 111 Service *aService)
michael@0 112 {
michael@0 113 Collations collations[] = {
michael@0 114 {"locale",
michael@0 115 SQLITE_UTF8,
michael@0 116 localeCollation8},
michael@0 117 {"locale_case_sensitive",
michael@0 118 SQLITE_UTF8,
michael@0 119 localeCollationCaseSensitive8},
michael@0 120 {"locale_accent_sensitive",
michael@0 121 SQLITE_UTF8,
michael@0 122 localeCollationAccentSensitive8},
michael@0 123 {"locale_case_accent_sensitive",
michael@0 124 SQLITE_UTF8,
michael@0 125 localeCollationCaseAccentSensitive8},
michael@0 126 {"locale",
michael@0 127 SQLITE_UTF16,
michael@0 128 localeCollation16},
michael@0 129 {"locale_case_sensitive",
michael@0 130 SQLITE_UTF16,
michael@0 131 localeCollationCaseSensitive16},
michael@0 132 {"locale_accent_sensitive",
michael@0 133 SQLITE_UTF16,
michael@0 134 localeCollationAccentSensitive16},
michael@0 135 {"locale_case_accent_sensitive",
michael@0 136 SQLITE_UTF16,
michael@0 137 localeCollationCaseAccentSensitive16},
michael@0 138 };
michael@0 139
michael@0 140 int rv = SQLITE_OK;
michael@0 141 for (size_t i = 0; SQLITE_OK == rv && i < ArrayLength(collations); ++i) {
michael@0 142 struct Collations *p = &collations[i];
michael@0 143 rv = ::sqlite3_create_collation(aDB, p->zName, p->enc, aService,
michael@0 144 p->xCompare);
michael@0 145 }
michael@0 146
michael@0 147 return rv;
michael@0 148 }
michael@0 149
michael@0 150 ////////////////////////////////////////////////////////////////////////////////
michael@0 151 //// SQL Collations
michael@0 152
michael@0 153 int
michael@0 154 localeCollation8(void *aService,
michael@0 155 int aLen1,
michael@0 156 const void *aStr1,
michael@0 157 int aLen2,
michael@0 158 const void *aStr2)
michael@0 159 {
michael@0 160 return localeCollationHelper8(aService, aLen1, aStr1, aLen2, aStr2,
michael@0 161 nsICollation::kCollationCaseInSensitive);
michael@0 162 }
michael@0 163
michael@0 164 int
michael@0 165 localeCollationCaseSensitive8(void *aService,
michael@0 166 int aLen1,
michael@0 167 const void *aStr1,
michael@0 168 int aLen2,
michael@0 169 const void *aStr2)
michael@0 170 {
michael@0 171 return localeCollationHelper8(aService, aLen1, aStr1, aLen2, aStr2,
michael@0 172 nsICollation::kCollationAccentInsenstive);
michael@0 173 }
michael@0 174
michael@0 175 int
michael@0 176 localeCollationAccentSensitive8(void *aService,
michael@0 177 int aLen1,
michael@0 178 const void *aStr1,
michael@0 179 int aLen2,
michael@0 180 const void *aStr2)
michael@0 181 {
michael@0 182 return localeCollationHelper8(aService, aLen1, aStr1, aLen2, aStr2,
michael@0 183 nsICollation::kCollationCaseInsensitiveAscii);
michael@0 184 }
michael@0 185
michael@0 186 int
michael@0 187 localeCollationCaseAccentSensitive8(void *aService,
michael@0 188 int aLen1,
michael@0 189 const void *aStr1,
michael@0 190 int aLen2,
michael@0 191 const void *aStr2)
michael@0 192 {
michael@0 193 return localeCollationHelper8(aService, aLen1, aStr1, aLen2, aStr2,
michael@0 194 nsICollation::kCollationCaseSensitive);
michael@0 195 }
michael@0 196
michael@0 197 int
michael@0 198 localeCollation16(void *aService,
michael@0 199 int aLen1,
michael@0 200 const void *aStr1,
michael@0 201 int aLen2,
michael@0 202 const void *aStr2)
michael@0 203 {
michael@0 204 return localeCollationHelper16(aService, aLen1, aStr1, aLen2, aStr2,
michael@0 205 nsICollation::kCollationCaseInSensitive);
michael@0 206 }
michael@0 207
michael@0 208 int
michael@0 209 localeCollationCaseSensitive16(void *aService,
michael@0 210 int aLen1,
michael@0 211 const void *aStr1,
michael@0 212 int aLen2,
michael@0 213 const void *aStr2)
michael@0 214 {
michael@0 215 return localeCollationHelper16(aService, aLen1, aStr1, aLen2, aStr2,
michael@0 216 nsICollation::kCollationAccentInsenstive);
michael@0 217 }
michael@0 218
michael@0 219 int
michael@0 220 localeCollationAccentSensitive16(void *aService,
michael@0 221 int aLen1,
michael@0 222 const void *aStr1,
michael@0 223 int aLen2,
michael@0 224 const void *aStr2)
michael@0 225 {
michael@0 226 return localeCollationHelper16(aService, aLen1, aStr1, aLen2, aStr2,
michael@0 227 nsICollation::kCollationCaseInsensitiveAscii);
michael@0 228 }
michael@0 229
michael@0 230 int
michael@0 231 localeCollationCaseAccentSensitive16(void *aService,
michael@0 232 int aLen1,
michael@0 233 const void *aStr1,
michael@0 234 int aLen2,
michael@0 235 const void *aStr2)
michael@0 236 {
michael@0 237 return localeCollationHelper16(aService, aLen1, aStr1, aLen2, aStr2,
michael@0 238 nsICollation::kCollationCaseSensitive);
michael@0 239 }
michael@0 240
michael@0 241 } // namespace storage
michael@0 242 } // namespace mozilla

mercurial