1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "IDNBlocklistUtils.h"
7 #include "nsStringFwd.h"
12 static constexpr char16_t sBlocklistPairs
[][2] = {
13 #include "IDNCharacterBlocklist.inc"
16 void RemoveCharFromBlocklist(char16_t aChar
,
17 nsTArray
<BlocklistRange
>& aBlocklist
) {
18 auto pos
= aBlocklist
.BinaryIndexOf(aChar
, BlocklistPairToCharComparator());
19 if (pos
== nsTArray
<BlocklistRange
>::NoIndex
) {
23 auto& pair
= aBlocklist
[pos
];
25 // If the matched range has a length of one, we can just remove it
26 if (pair
.second
== pair
.first
) {
27 aBlocklist
.RemoveElementAt(pos
);
31 // If the character matches the first element in the range, just update
33 if (aChar
== pair
.first
) {
34 pair
.first
= pair
.first
+ 1;
38 // Also if it matches the last character in the range, we just update it.
39 if (aChar
== pair
.second
) {
40 pair
.second
= pair
.second
- 1;
44 // Our character is in the middle of the range, splitting it in two.
45 // We update the matched range to reflect the values before the character,
46 // and insert a new range that represents the values after.
47 char16_t lastElement
= pair
.second
;
48 pair
.second
= aChar
- 1;
49 aBlocklist
.InsertElementAt(pos
+ 1,
50 std::make_pair(char16_t(aChar
+ 1), lastElement
));
53 void InitializeBlocklist(nsTArray
<BlocklistRange
>& aBlocklist
) {
55 for (auto const& arr
: sBlocklistPairs
) {
56 // The hardcoded pairs are already sorted.
57 aBlocklist
.AppendElement(std::make_pair(arr
[0], arr
[1]));
62 } // namespace mozilla