/*
 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

// THIS FILE IS GENERATED BY build/scripts/make_html_entity_table.py.
// DO NOT EDIT (unless you are a ninja)!

#include "third_party/blink/renderer/core/html/parser/html_entity_table.h"

namespace blink {

namespace {

// The raw character data shared by all entity names. Each entity's name is a
// substring of this storage, identified by (entity_offset, length) in
// kStaticEntityTable below. Substrings are packed: when a new entity is added
// while building this table, the generator first checks whether the name (or a
// prefix of it that matches the tail of the existing storage) is already
// present, and only appends the bytes that are not already covered.
const LChar kStaticEntityStringStorage[] = {
{% for line in storage_lines %}
{{ line }}
{% endfor %}

// One entry per HTML entity name (sorted alphabetically by name).
//   first_value  : primary Unicode code point the entity expands to.
//   second_value : optional second code point (0 if unused).
//   entity_offset: index into kStaticEntityStringStorage where the name starts.
//   length       : number of characters of the name (including any trailing
//                  ';' that is part of the canonical name).
const HTMLEntityTableEntry kStaticEntityTable[{{ entries|length }}] = {
{% for entry in entries %}
    { {{ entry.first_value }}, {{ entry.second_value }}, {{ entry.offset }}, {{ entry.length }} }, // &{{ entry.name }}
{% endfor %}
};

// kUppercaseOffset[i] gives the index of the first entry in kStaticEntityTable
// whose name starts with ('A' + i). The extra final element is the index of
// the first entry starting with 'a', so that
//   [kUppercaseOffset[i], kUppercaseOffset[i + 1])
// is the range of entries beginning with ('A' + i).
const uint16_t kUppercaseOffset[] = {
{% for offset in uppercase_offsets %}
{{ offset }}{{ ',' if not loop.last }}
{% endfor %}};

// Same idea for lowercase letters. The final element is the total entry count
// so that the range for 'z' is well-defined.
const uint16_t kLowercaseOffset[] = {
{% for offset in lowercase_offsets %}
{{ offset }}{{ ',' if not loop.last }}
{% endfor %}};

}  // namespace

base::span<const LChar> HTMLEntityTable::EntityString(const HTMLEntityTableEntry& entry) {
  return base::span(kStaticEntityStringStorage).subspan(entry.entity_offset, entry.length);
}

LChar HTMLEntityTableEntry::LastCharacter() const {
  return HTMLEntityTable::EntityString(*this).back();
}

base::span<const HTMLEntityTableEntry> HTMLEntityTable::EntriesStartingWith(UChar c) {
  if (IsAsciiUpper(c)) {
    const size_t first = kUppercaseOffset[c - 'A'];
    const size_t last = kUppercaseOffset[c - 'A' + 1];
    return AllEntries().subspan(first, last - first);
  }
  if (IsAsciiLower(c)) {
    const size_t first = kLowercaseOffset[c - 'a'];
    const size_t last = kLowercaseOffset[c - 'a' + 1];
    return AllEntries().subspan(first, last - first);
  }
  return {};
}

base::span<const HTMLEntityTableEntry> HTMLEntityTable::AllEntries() {
  return base::span(kStaticEntityTable);
}

}  // namespace blink
