//===-- Utils for wctype classification functions ---------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// DO NOT EDIT MANUALLY.
// This file is generated by libc/utils/wctype_utils scripts.

#ifndef LLVM_LIBC_SRC___SUPPORT_WCTYPE_WCTYPE_CLASSIFICATION_UTILS_H
#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_WCTYPE_CLASSIFICATION_UTILS_H

#include "hdr/stdint_proxy.h"
#include "hdr/types/wchar_t.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/libc_assert.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

// Property flags for Unicode categories
enum PropertyFlag : uint8_t {
  UPPER = 1 << 0,
  LOWER = 1 << 1,
  ALPHA = 1 << 2,
  SPACE = 1 << 3,
  PRINT = 1 << 4,
  BLANK = 1 << 5,
  CNTRL = 1 << 6,
  PUNCT = 1 << 7,
};

static_assert(4352 <= cpp::numeric_limits<unsigned short>::max());
static_assert(39168 <= cpp::numeric_limits<unsigned short>::max());

LIBC_INLINE_VAR constexpr uint16_t LEVEL1_SIZE = 4352;
LIBC_INLINE_VAR constexpr uint16_t LEVEL2_SIZE = 39168;

// Level 1 table: indexed by (codepoint >> 8), stores level2 block offsets
extern const uint16_t LEVEL1[LEVEL1_SIZE];

// Level 2 table: blocks of 256 property flags
extern const uint8_t LEVEL2[LEVEL2_SIZE];

// Returns the Unicode property flag for a given wide character.
LIBC_INLINE uint8_t lookup_properties(const wchar_t wc) {
  // Out of Unicode range
  if (static_cast<uint32_t>(wc) > 0x10FFFF) {
    return 0;
  }

  uint16_t l1_idx = static_cast<uint16_t>(wc >> 8);
  LIBC_ASSERT(l1_idx < LEVEL1_SIZE);

  uint16_t l2_offset = LEVEL1[l1_idx];
  uint16_t l2_idx = l2_offset + (wc & 0xFF);
  LIBC_ASSERT(l2_idx < LEVEL2_SIZE);

  return LEVEL2[l2_idx];
}

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC___SUPPORT_WCTYPE_WCTYPE_CLASSIFICATION_UTILS_H
