nsHtml5NamedCharacters.cpp (3971B)
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 "mozilla/Logging.h" 26 #include "nsDebug.h" 27 #include "nscore.h" 28 29 #include "nsHtml5NamedCharacters.h" 30 31 const char16_t nsHtml5NamedCharacters::VALUES[][2] = { 32 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) {VALUE}, 33 #include "nsHtml5NamedCharactersInclude.h" 34 #undef NAMED_CHARACTER_REFERENCE 35 {0, 0}}; 36 37 char16_t** nsHtml5NamedCharacters::WINDOWS_1252; 38 static char16_t const WINDOWS_1252_DATA[] = { 39 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 40 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F, 41 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 42 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178}; 43 44 /** 45 * To avoid having lots of pointers in the |charData| array, below, 46 * which would cause us to have to do lots of relocations at library 47 * load time, store all the string data for the names in one big array. 48 * Then use tricks with enums to help us build an array that contains 49 * the positions of each within the big arrays. 50 */ 51 52 static const int8_t ALL_NAMES[] = { 53 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) CHARS, 54 #include "nsHtml5NamedCharactersInclude.h" 55 #undef NAMED_CHARACTER_REFERENCE 56 }; 57 58 enum NamePositions { 59 DUMMY_INITIAL_NAME_POSITION = 0, 60 /* enums don't take up space, so generate _START and _END */ 61 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \ 62 NAME_##N##_DUMMY, /* automatically one higher than previous */ \ 63 NAME_##N##_START = NAME_##N##_DUMMY - 1, \ 64 NAME_##N##_END = NAME_##N##_START + LEN + FLAG, 65 #include "nsHtml5NamedCharactersInclude.h" 66 #undef NAMED_CHARACTER_REFERENCE 67 DUMMY_FINAL_NAME_VALUE 68 }; 69 70 static_assert(std::size(ALL_NAMES) < 0x10000, 71 "Start positions should fit in 16 bits"); 72 73 const nsHtml5CharacterName nsHtml5NamedCharacters::NAMES[] = { 74 #ifdef DEBUG 75 # define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \ 76 {NAME_##N##_START, LEN, N}, 77 #else 78 # define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \ 79 { \ 80 NAME_##N##_START, \ 81 LEN, \ 82 }, 83 #endif 84 #include "nsHtml5NamedCharactersInclude.h" 85 #undef NAMED_CHARACTER_REFERENCE 86 }; 87 88 int32_t nsHtml5CharacterName::length() const { return nameLen; } 89 90 char16_t nsHtml5CharacterName::charAt(int32_t index) const { 91 return static_cast<char16_t>(ALL_NAMES[nameStart + index]); 92 } 93 94 void nsHtml5NamedCharacters::initializeStatics() { 95 WINDOWS_1252 = new char16_t*[32]; 96 for (int32_t i = 0; i < 32; ++i) { 97 WINDOWS_1252[i] = (char16_t*)&(WINDOWS_1252_DATA[i]); 98 } 99 } 100 101 void nsHtml5NamedCharacters::releaseStatics() { delete[] WINDOWS_1252; }