parser/html/nsHtml5NamedCharacters.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     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  */
    23 #define nsHtml5NamedCharacters_cpp_
    24 #include "jArray.h"
    25 #include "nscore.h"
    26 #include "nsDebug.h"
    27 #include "mozilla/ArrayUtils.h"
    28 #include "prlog.h"
    30 #include "nsHtml5NamedCharacters.h"
    32 const char16_t nsHtml5NamedCharacters::VALUES[][2] = {
    33 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \
    34 { VALUE },
    35 #include "nsHtml5NamedCharactersInclude.h"
    36 #undef NAMED_CHARACTER_REFERENCE
    37 {0, 0} };
    39 char16_t** nsHtml5NamedCharacters::WINDOWS_1252;
    40 static char16_t const WINDOWS_1252_DATA[] = {
    41   0x20AC,
    42   0x0081,
    43   0x201A,
    44   0x0192,
    45   0x201E,
    46   0x2026,
    47   0x2020,
    48   0x2021,
    49   0x02C6,
    50   0x2030,
    51   0x0160,
    52   0x2039,
    53   0x0152,
    54   0x008D,
    55   0x017D,
    56   0x008F,
    57   0x0090,
    58   0x2018,
    59   0x2019,
    60   0x201C,
    61   0x201D,
    62   0x2022,
    63   0x2013,
    64   0x2014,
    65   0x02DC,
    66   0x2122,
    67   0x0161,
    68   0x203A,
    69   0x0153,
    70   0x009D,
    71   0x017E,
    72   0x0178
    73 };
    75 /**
    76  * To avoid having lots of pointers in the |charData| array, below,
    77  * which would cause us to have to do lots of relocations at library
    78  * load time, store all the string data for the names in one big array.
    79  * Then use tricks with enums to help us build an array that contains
    80  * the positions of each within the big arrays.
    81  */
    83 static const int8_t ALL_NAMES[] = {
    84 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \
    85 CHARS ,
    86 #include "nsHtml5NamedCharactersInclude.h"
    87 #undef NAMED_CHARACTER_REFERENCE
    88 };
    90 enum NamePositions {
    91   DUMMY_INITIAL_NAME_POSITION = 0,
    92 /* enums don't take up space, so generate _START and _END */
    93 #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \
    94 NAME_##N##_DUMMY, /* automatically one higher than previous */ \
    95 NAME_##N##_START = NAME_##N##_DUMMY - 1, \
    96 NAME_##N##_END = NAME_##N##_START + LEN + FLAG,
    97 #include "nsHtml5NamedCharactersInclude.h"
    98 #undef NAMED_CHARACTER_REFERENCE
    99   DUMMY_FINAL_NAME_VALUE
   100 };
   102 /* check that the start positions will fit in 16 bits */
   103 PR_STATIC_ASSERT(MOZ_ARRAY_LENGTH(ALL_NAMES) < 0x10000);
   105 const nsHtml5CharacterName nsHtml5NamedCharacters::NAMES[] = {
   106 #ifdef DEBUG
   107   #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \
   108 { NAME_##N##_START, LEN, N },
   109 #else
   110   #define NAMED_CHARACTER_REFERENCE(N, CHARS, LEN, FLAG, VALUE) \
   111 { NAME_##N##_START, LEN, },
   112 #endif
   113 #include "nsHtml5NamedCharactersInclude.h"
   114 #undef NAMED_CHARACTER_REFERENCE
   115 };
   117 int32_t
   118 nsHtml5CharacterName::length() const
   119 {
   120   return nameLen;
   121 }
   123 char16_t
   124 nsHtml5CharacterName::charAt(int32_t index) const
   125 {
   126   return static_cast<char16_t> (ALL_NAMES[nameStart + index]);
   127 }
   129 void
   130 nsHtml5NamedCharacters::initializeStatics()
   131 {
   132   WINDOWS_1252 = new char16_t*[32];
   133   for (int32_t i = 0; i < 32; ++i) {
   134     WINDOWS_1252[i] = (char16_t*)&(WINDOWS_1252_DATA[i]);
   135   }
   136 }
   138 void
   139 nsHtml5NamedCharacters::releaseStatics()
   140 {
   141   delete[] WINDOWS_1252;
   142 }

mercurial