Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / dns / IDNBlocklistUtils.cpp
blobc8b0f876339bbbf35df84b8366294da5a81b4fdd
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"
9 namespace mozilla {
10 namespace net {
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) {
20 return;
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);
28 return;
31 // If the character matches the first element in the range, just update
32 // the range.
33 if (aChar == pair.first) {
34 pair.first = pair.first + 1;
35 return;
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;
41 return;
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) {
54 aBlocklist.Clear();
55 for (auto const& arr : sBlocklistPairs) {
56 // The hardcoded pairs are already sorted.
57 aBlocklist.AppendElement(std::make_pair(arr[0], arr[1]));
61 } // namespace net
62 } // namespace mozilla