|
| 1 | +// |
| 2 | +// UnicodeCollator_JS.cc |
| 3 | +// |
| 4 | +// Copyright 2017-Present Couchbase, Inc. |
| 5 | +// |
| 6 | +// Use of this software is governed by the Business Source License included |
| 7 | +// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified |
| 8 | +// in that file, in accordance with the Business Source License, use of this |
| 9 | +// software will be governed by the Apache License, Version 2.0, included in |
| 10 | +// the file licenses/APL2.txt. |
| 11 | +// |
| 12 | + |
| 13 | +// This is an UnicodeCollaction implementation based on the JS Intl.Collator API. |
| 14 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator |
| 15 | + |
| 16 | +#include "UnicodeCollator.hh" |
| 17 | +#include "Error.hh" |
| 18 | +#include "emscripten/val.h" |
| 19 | +#include "SQLiteCpp/Exception.h" |
| 20 | +#include <sqlite3.h> |
| 21 | + |
| 22 | +namespace litecore { |
| 23 | + |
| 24 | + using namespace std; |
| 25 | + using namespace emscripten; |
| 26 | + |
| 27 | + class JSCollationContext : public CollationContext { |
| 28 | + public: |
| 29 | + val collator = val::undefined(); |
| 30 | + |
| 31 | + JSCollationContext(const Collation &collation) |
| 32 | + : CollationContext(collation) { |
| 33 | + auto locale = val::undefined(); |
| 34 | + auto options = val::object(); |
| 35 | + |
| 36 | + if (collation.localeName) { |
| 37 | + locale = val(collation.localeName.asString()); |
| 38 | + } |
| 39 | + |
| 40 | + if (collation.diacriticSensitive) { |
| 41 | + if (collation.caseSensitive) { |
| 42 | + options.set("sensitivity", "variant"); |
| 43 | + } else { |
| 44 | + options.set("sensitivity", "accent"); |
| 45 | + } |
| 46 | + } |
| 47 | + else { |
| 48 | + if (collation.caseSensitive) { |
| 49 | + options.set("sensitivity", "case"); |
| 50 | + } else { |
| 51 | + options.set("sensitivity", "base"); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + collator = val::global("Intl")["Collator"].new_(locale, options); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + unique_ptr<CollationContext> CollationContext::create(const Collation &coll) { |
| 60 | + return make_unique<JSCollationContext>(coll); |
| 61 | + } |
| 62 | + |
| 63 | + static inline int compareStringsUnicode(int len1, const void *chars1, |
| 64 | + int len2, const void *chars2, |
| 65 | + const JSCollationContext &ctx) { |
| 66 | + return ctx.collator.call<int>("compare", string((const char *)chars1, len1), |
| 67 | + string((const char *)chars2, len2)); |
| 68 | + } |
| 69 | + |
| 70 | + static int collateUnicodeCallback(void *context, |
| 71 | + int len1, const void *chars1, |
| 72 | + int len2, const void *chars2) { |
| 73 | + auto &coll = *(JSCollationContext *)context; |
| 74 | + if (coll.canCompareASCII) { |
| 75 | + int result = CompareASCII(len1, (const uint8_t *)chars1, |
| 76 | + len2, (const uint8_t *)chars2, coll.caseSensitive); |
| 77 | + if (result != kCompareASCIIGaveUp) |
| 78 | + return result; |
| 79 | + } |
| 80 | + return compareStringsUnicode(len1, chars1, len2, chars2, coll); |
| 81 | + } |
| 82 | + |
| 83 | + int CompareUTF8(slice str1, slice str2, const Collation &coll) { |
| 84 | + return CompareUTF8(str1, str2, JSCollationContext(coll)); |
| 85 | + } |
| 86 | + |
| 87 | + int CompareUTF8(slice str1, slice str2, const CollationContext &ctx) { |
| 88 | + return collateUnicodeCallback((void *)&ctx, (int)str1.size, str1.buf, |
| 89 | + (int)str2.size, str2.buf); |
| 90 | + } |
| 91 | + |
| 92 | + int LikeUTF8(slice str1, slice str2, const Collation &coll) { |
| 93 | + return LikeUTF8(str1, str2, JSCollationContext(coll)); |
| 94 | + } |
| 95 | + |
| 96 | + bool ContainsUTF8(slice str, slice substr, const CollationContext &ctx) { |
| 97 | + return ContainsUTF8_Slow(str, substr, ctx); |
| 98 | + } |
| 99 | + |
| 100 | + unique_ptr<CollationContext> RegisterSQLiteUnicodeCollation(sqlite3 *dbHandle, |
| 101 | + const Collation &coll) { |
| 102 | + unique_ptr<CollationContext> context(new JSCollationContext(coll)); |
| 103 | + int rc = sqlite3_create_collation(dbHandle, |
| 104 | + coll.sqliteName().c_str(), |
| 105 | + SQLITE_UTF8, |
| 106 | + (void *)context.get(), |
| 107 | + collateUnicodeCallback); |
| 108 | + if (rc != SQLITE_OK) |
| 109 | + throw SQLite::Exception(dbHandle, rc); |
| 110 | + return context; |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments