1 /*
2  * Copyright (c) 2008-2010 Mozilla Foundation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #define nsHtml5NamedCharacters_cpp_
24 #include "jArray.h"
25 #include "nscore.h"
26 #include "nsDebug.h"
27 #include "mozilla/ArrayUtils.h"
28 #include "mozilla/Logging.h"
29 
30 #include "nsHtml5NamedCharacters.h"
31 
32 const char16_t nsHtml5NamedCharacters::VALUES[][2] = {
33 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) {VALUE},
34 #include "nsHtml5NamedCharactersInclude.h"
35 #undef NAMED_CHARACTER_REFERENCE
36     {0, 0}};
37 
38 char16_t** nsHtml5NamedCharacters::WINDOWS_1252;
39 static char16_t const WINDOWS_1252_DATA[] = {
40     0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
41     0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F,
42     0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
43     0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178};
44 
45 /**
46  * To avoid having lots of pointers in the |charData| array, below,
47  * which would cause us to have to do lots of relocations at library
48  * load time, store all the string data for the names in one big array.
49  * Then use tricks with enums to help us build an array that contains
50  * the positions of each within the big arrays.
51  */
52 
53 static const int8_t ALL_NAMES[] = {
54 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) CHARS,
55 #include "nsHtml5NamedCharactersInclude.h"
56 #undef NAMED_CHARACTER_REFERENCE
57 };
58 
59 enum NamePositions {
60   DUMMY_INITIAL_NAME_POSITION = 0,
61 /* enums don't take up space, so generate _START and _END */
62 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE)    \
63   NAME_##N##_DUMMY, /* automatically one higher than previous */ \
64       NAME_##N##_START = NAME_##N##_DUMMY - 1,                   \
65       NAME_##N##_END = NAME_##N##_START + LEN + FLAG,
66 #include "nsHtml5NamedCharactersInclude.h"
67 #undef NAMED_CHARACTER_REFERENCE
68   DUMMY_FINAL_NAME_VALUE
69 };
70 
71 static_assert(MOZ_ARRAY_LENGTH(ALL_NAMES) < 0x10000,
72               "Start positions should fit in 16 bits");
73 
74 const nsHtml5CharacterName nsHtml5NamedCharacters::NAMES[] = {
75 #ifdef DEBUG
76 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \
77   {NAME_##N##_START, LEN, N},
78 #else
79 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \
80   {                                                           \
81       NAME_##N##_START,                                       \
82       LEN,                                                    \
83   },
84 #endif
85 #include "nsHtml5NamedCharactersInclude.h"
86 #undef NAMED_CHARACTER_REFERENCE
87 };
88 
length() const89 int32_t nsHtml5CharacterName::length() const { return nameLen; }
90 
charAt(int32_t index) const91 char16_t nsHtml5CharacterName::charAt(int32_t index) const {
92   return static_cast<char16_t>(ALL_NAMES[nameStart + index]);
93 }
94 
initializeStatics()95 void nsHtml5NamedCharacters::initializeStatics() {
96   WINDOWS_1252 = new char16_t*[32];
97   for (int32_t i = 0; i < 32; ++i) {
98     WINDOWS_1252[i] = (char16_t*)&(WINDOWS_1252_DATA[i]);
99   }
100 }
101 
releaseStatics()102 void nsHtml5NamedCharacters::releaseStatics() { delete[] WINDOWS_1252; }
103