diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 12:54:13 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-09-29 12:54:13 +0200 |
| commit | 91871be6ece9317c5d49353f6474583e7e28168d (patch) | |
| tree | 8cabf2546322a9e1b724bf8413f32de54378824f | |
| parent | 35ab9c12ba4d396e6cba99d6ea947e26becedad4 (diff) | |
| download | langdet-91871be6ece9317c5d49353f6474583e7e28168d.tar.gz langdet-91871be6ece9317c5d49353f6474583e7e28168d.zip | |
Add native CPU language detection CLI
43 files changed, 34390 insertions, 64 deletions
@@ -1,2 +1,4 @@ /model/ /__pycache__/ +/build/ +/langdetect diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e8a19e2 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +CC = cc +CPPFLAGS += -Ilibffwd -Ideps/yyjson -DUSE_BLAS -DACCELERATE_NEW_LAPACK +CFLAGS ?= -O3 -march=native +CFLAGS += -std=c11 -Wall -Wextra -Werror=implicit-function-declaration -pthread +LDLIBS += -framework Accelerate -lm -pthread + +CPU_SOURCES = model.c model_blas.c forward.c alloc.c config.c vec.c \ + tokenize.c bpe.c sentencepiece.c wordpiece.c safetensors.c \ + act.c gemm.c norm.c rope.c attn.c threadpool.c generic.c neon.c avx.c +SOURCES = langdetect.c $(addprefix libffwd/,$(CPU_SOURCES)) deps/yyjson/yyjson.c +OBJECTS = $(SOURCES:%.c=build/%.o) + +.PHONY: all clean +all: langdetect + +langdetect: $(OBJECTS) + $(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LDLIBS) + +build/%.o: %.c + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c $< -o $@ + +clean: + rm -rf build langdetect + +-include $(OBJECTS:.o=.d) @@ -1,65 +1,56 @@ -# XLM-R language detection: ffwd compatibility +# XLM-R language detection on macOS CPU -`papluca/xlm-roberta-base-language-detection` is **not supported as a language -classifier by the inspected ffwd implementation**. Its XLM-R encoder is supported, -but ffwd does not load or execute the classification head. Loading the checkpoint -as an embedding model would produce 768 numbers, not 20 language scores. +A native C CLI for `papluca/xlm-roberta-base-language-detection`, using ffwd's +CPU encoder, ARM NEON kernels, and Apple Accelerate BLAS. It returns language +predictions for the checkpoint's 20 languages. No MLX, Metal, PyTorch, or Python +runtime is required for inference. -## Compatibility findings +## Build and run -The reference source is `/Users/ogz/x/ffwd`. +Use an Apple Silicon Mac with Xcode Command Line Tools installed +(`xcode-select --install` if needed). Python 3 is required only for downloading +the model. -- `README.md` lists XLM-R support for text embeddings. -- `libffwd/config.c` recognizes `xlm-roberta`, sets the correct position offset, - and defaults to mean pooling. It does not interpret the classification - architecture or `id2label`. -- `libffwd/model.c`, in `load_bert_weights`, accepts the checkpoint's `roberta.` - tensor prefix and loads the encoder, but not `classifier.*`. -- `libffwd/forward.c` implements the encoder and embedding pooling, including - first-token pooling (`FFWD_POOL_CLS`), but no classification head. -- `libffwd/ffwd.h` exposes embeddings and reranking, not sequence classification. -- `libffwd/tokenize.c` supports the checkpoint's `sentencepiece.bpe.model` format - and wraps text with `<s>` and `</s>`. +```sh +python3 download_model.py +make -j8 +./langdetect 'This is an English sentence.' 'Bugün hava çok güzel.' +printf '%s\n' 'Bonjour tout le monde.' 'これは日本語です。' | ./langdetect +``` -The checkpoint declares `XLMRobertaForSequenceClassification`. Its safetensors -header contains these F32 classifier tensors: +Run these commands from this directory. The default model path is `./model`; +use `-d DIRECTORY` for a different path. -| Tensor | Shape | -| --- | --- | -| `classifier.dense.weight` | `[768, 768]` | -| `classifier.dense.bias` | `[768]` | -| `classifier.out_proj.weight` | `[20, 768]` | -| `classifier.out_proj.bias` | `[20]` | +The model is loaded once per process. Each text argument, or each stdin line +when there are no text arguments, produces one JSON line in input order: -## Required inference work +- `label` is the predicted language code. +- `score` is its softmax probability. +- `tokens` includes the leading `<s>` and trailing `</s>`. +- `truncated` indicates whether the input exceeded 512 tokens. +- `ms` measures tokenization and inference, excluding model loading and output. -1. Reuse ffwd's CPU encoder, SentencePiece tokenizer, safetensors reader, and - ARM NEON kernels. On macOS ARM64, matrix multiplication uses Apple's Accelerate - framework; Metal and MLX are not required. -2. Load and validate the four classifier tensors, plus `id2label` from - `config.json`. -3. Take the final hidden state of the first token (`<s>`), not the mean of all - tokens. Do not apply L2 normalization. Compute, with column vectors: +`--all` includes a `scores` object with probabilities for every language. +`-t N` selects 1 to 16 ffwd kernel threads; the default is the detected CPU count, +capped at 16. Accelerate manages its own BLAS threads. `-h` prints help. +Use `--` before text that begins with a hyphen. Diagnostics go to stderr. - ```text - h = final_hidden_state[0] # 768 values - z = tanh(classifier.dense.weight @ h + classifier.dense.bias) - logits = classifier.out_proj.weight @ z + classifier.out_proj.bias - probabilities = softmax(logits) # 20 values - language = id2label[argmax(probabilities)] - ``` +## Inference - Dropout is disabled during inference. Stable softmax subtracts the largest - logit before exponentiation. -4. Add a classification runner that returns labels and scores. Keep each input - within 512 tokens, including both special tokens; any truncation must retain - the final `</s>` token. -5. Compare token IDs, logits, and probabilities against Hugging Face Transformers - on CPU, including all 20 supported languages, Unicode, empty input, mixed - sequence lengths, and the length limit. +`langdetect.c` loads and validates the four F32 classifier tensors and the +`id2label` mapping. The encoder uses first-token pooling without L2 normalization. +The classifier applies a 768-to-768 linear layer, `tanh`, a 768-to-20 linear layer, +and stable softmax. Dropout is disabled during inference. -This requires additional classification code, not retraining or a new transformer -architecture. A configuration change alone cannot execute the missing head. +The CPU encoder and tokenizer are in `libffwd/`; the JSON parser is in +`deps/yyjson/`. Their license files are included. The build uses only local source +files and the macOS SDK, with no dependency on another repository. + +Inputs are processed individually, not as a parallel batch. Long inputs are +truncated to 512 tokens while preserving the final `</s>`. Empty inputs are +accepted, but their predictions are not useful. The model always chooses among +its 20 labels; it has no unknown-language result, and scores are not a guarantee +of accuracy on ambiguous or unsupported text. ## Download @@ -85,16 +76,13 @@ byte offset. ## Verification status -- The host reports `Darwin arm64`. -- All seven files were downloaded and their sizes and hashes verified. A second - script run verified and reused every file. -- The local safetensors header was checked for the four classifier shapes and - F32 types, and its final data offset matches the file size. -- The classification formula matches Transformers 4.12.5, the version recorded - in the model configuration. -- No ffwd inference code is included or built here. No language predictions or - numerical comparisons against Transformers have been run. Encoder compatibility - is based on source and checkpoint inspection, not a runtime test. - -The remaining work is the classification runner and the numerical verification -listed above. The reference ffwd source is unchanged. +- The executable builds and runs as native macOS ARM64, linked to Accelerate and + the system C library, with no GPU runtime. +- CLI smoke checks pass for one sample in each of the 20 supported languages, + normalized scores, stdin and argument input, empty and whitespace-only input, + single-thread inference, and 512-token truncation. +- Invalid thread counts, a missing model directory, and NUL bytes in stdin + produce errors. +- The downloaded checkpoint files match Hugging Face's sizes and hashes. +- Numerical equivalence with Hugging Face Transformers has not been established. + These smoke checks are not an accuracy benchmark or exhaustive tokenizer test. diff --git a/deps/yyjson/LICENSE b/deps/yyjson/LICENSE new file mode 100644 index 0000000..a09bff3 --- /dev/null +++ b/deps/yyjson/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 YaoYuan <ibireme@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/deps/yyjson/yyjson.c b/deps/yyjson/yyjson.c new file mode 100644 index 0000000..c16d925 --- /dev/null +++ b/deps/yyjson/yyjson.c @@ -0,0 +1,11065 @@ +/*============================================================================== + Copyright (c) 2020 YaoYuan <ibireme@gmail.com> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + *============================================================================*/ + +#include "yyjson.h" +#include <math.h> /* for `HUGE_VAL/INFINIY/NAN` macros, no libm required */ + + + +/*============================================================================== + * MARK: - Warning Suppress (Private) + *============================================================================*/ + +#if defined(__clang__) +# pragma clang diagnostic ignored "-Wunused-function" +# pragma clang diagnostic ignored "-Wunused-parameter" +# pragma clang diagnostic ignored "-Wunused-label" +# pragma clang diagnostic ignored "-Wunused-macros" +# pragma clang diagnostic ignored "-Wunused-variable" +#elif defined(__GNUC__) +# pragma GCC diagnostic ignored "-Wunused-function" +# pragma GCC diagnostic ignored "-Wunused-parameter" +# pragma GCC diagnostic ignored "-Wunused-label" +# pragma GCC diagnostic ignored "-Wunused-macros" +# pragma GCC diagnostic ignored "-Wunused-variable" +#elif defined(_MSC_VER) +# pragma warning(disable:4100) /* unreferenced formal parameter */ +# pragma warning(disable:4101) /* unreferenced variable */ +# pragma warning(disable:4102) /* unreferenced label */ +# pragma warning(disable:4127) /* conditional expression is constant */ +# pragma warning(disable:4706) /* assignment within conditional expression */ +#endif + + + +/*============================================================================== + * MARK: - Version (Public) + *============================================================================*/ + +uint32_t yyjson_version(void) { + return YYJSON_VERSION_HEX; +} + + + +/*============================================================================== + * MARK: - Flags (Private) + *============================================================================*/ + +/* msvc intrinsic */ +#if YYJSON_MSC_VER >= 1400 +# include <intrin.h> +# if defined(_M_AMD64) || defined(_M_ARM64) +# define MSC_HAS_BIT_SCAN_64 1 +# pragma intrinsic(_BitScanForward64) +# pragma intrinsic(_BitScanReverse64) +# else +# define MSC_HAS_BIT_SCAN_64 0 +# endif +# if defined(_M_AMD64) || defined(_M_ARM64) || \ + defined(_M_IX86) || defined(_M_ARM) +# define MSC_HAS_BIT_SCAN 1 +# pragma intrinsic(_BitScanForward) +# pragma intrinsic(_BitScanReverse) +# else +# define MSC_HAS_BIT_SCAN 0 +# endif +# if defined(_M_AMD64) +# define MSC_HAS_UMUL128 1 +# pragma intrinsic(_umul128) +# else +# define MSC_HAS_UMUL128 0 +# endif +#else +# define MSC_HAS_BIT_SCAN_64 0 +# define MSC_HAS_BIT_SCAN 0 +# define MSC_HAS_UMUL128 0 +#endif + +/* gcc builtin */ +#if yyjson_has_builtin(__builtin_clzll) || yyjson_gcc_available(3, 4, 0) +# define GCC_HAS_CLZLL 1 +#else +# define GCC_HAS_CLZLL 0 +#endif + +#if yyjson_has_builtin(__builtin_ctzll) || yyjson_gcc_available(3, 4, 0) +# define GCC_HAS_CTZLL 1 +#else +# define GCC_HAS_CTZLL 0 +#endif + +/* int128 type */ +#if defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16) && \ + (defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)) +# define YYJSON_HAS_INT128 1 +#else +# define YYJSON_HAS_INT128 0 +#endif + +/* IEEE 754 floating-point binary representation */ +#if defined(__STDC_IEC_559__) || defined(__STDC_IEC_60559_BFP__) +# define YYJSON_HAS_IEEE_754 1 +#elif FLT_RADIX == 2 && \ + FLT_MANT_DIG == 24 && FLT_DIG == 6 && \ + FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128 && \ + FLT_MIN_10_EXP == -37 && FLT_MAX_10_EXP == 38 && \ + DBL_MANT_DIG == 53 && DBL_DIG == 15 && \ + DBL_MIN_EXP == -1021 && DBL_MAX_EXP == 1024 && \ + DBL_MIN_10_EXP == -307 && DBL_MAX_10_EXP == 308 +# define YYJSON_HAS_IEEE_754 1 +#else +# define YYJSON_HAS_IEEE_754 0 +# undef YYJSON_DISABLE_FAST_FP_CONV +# define YYJSON_DISABLE_FAST_FP_CONV 1 +#endif + +/* + Correct rounding in double number computations. + + On the x86 architecture, some compilers may use x87 FPU instructions for + floating-point arithmetic. The x87 FPU loads all floating point number as + 80-bit double-extended precision internally, then rounds the result to original + precision, which may produce inaccurate results. For a more detailed + explanation, see the paper: https://arxiv.org/abs/cs/0701192 + + Here are some examples of double precision calculation error: + + 2877.0 / 1e6 == 0.002877, but x87 returns 0.0028770000000000002 + 43683.0 * 1e21 == 4.3683e25, but x87 returns 4.3683000000000004e25 + + Here are some examples of compiler flags to generate x87 instructions on x86: + + clang -m32 -mno-sse + gcc/icc -m32 -mfpmath=387 + msvc /arch:SSE or /arch:IA32 + + If we are sure that there's no similar error described above, we can define the + YYJSON_DOUBLE_MATH_CORRECT as 1 to enable the fast path calculation. This is + not an accurate detection, it's just try to avoid the error at compile-time. + An accurate detection can be done at run-time: + + bool is_double_math_correct(void) { + volatile double r = 43683.0; + r *= 1e21; + return r == 4.3683e25; + } + + See also: utils.h in https://github.com/google/double-conversion/ + */ +#if !defined(FLT_EVAL_METHOD) && defined(__FLT_EVAL_METHOD__) +# define FLT_EVAL_METHOD __FLT_EVAL_METHOD__ +#endif + +#if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1 +# define YYJSON_DOUBLE_MATH_CORRECT 0 +#elif defined(i386) || defined(__i386) || defined(__i386__) || \ + defined(_X86_) || defined(__X86__) || defined(_M_IX86) || \ + defined(__I86__) || defined(__IA32__) || defined(__THW_INTEL) +# if (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 2) || \ + (defined(__SSE2_MATH__) && __SSE2_MATH__) +# define YYJSON_DOUBLE_MATH_CORRECT 1 +# else +# define YYJSON_DOUBLE_MATH_CORRECT 0 +# endif +#elif defined(__mc68000__) || defined(__pnacl__) || defined(__native_client__) +# define YYJSON_DOUBLE_MATH_CORRECT 0 +#else +# define YYJSON_DOUBLE_MATH_CORRECT 1 +#endif + +/* + Detect the endianness at compile-time. + YYJSON_ENDIAN == YYJSON_BIG_ENDIAN + YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN + */ +#define YYJSON_BIG_ENDIAN 4321 +#define YYJSON_LITTLE_ENDIAN 1234 + +#if yyjson_has_include(<sys/types.h>) +# include <sys/types.h> /* POSIX */ +#endif +#if yyjson_has_include(<endian.h>) +# include <endian.h> /* Linux */ +#elif yyjson_has_include(<sys/endian.h>) +# include <sys/endian.h> /* BSD, Android */ +#elif yyjson_has_include(<machine/endian.h>) +# include <machine/endian.h> /* BSD, Darwin */ +#endif + +#if defined(BYTE_ORDER) && BYTE_ORDER +# if defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN) +# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN +# elif defined(LITTLE_ENDIAN) && (BYTE_ORDER == LITTLE_ENDIAN) +# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN +# endif +#elif defined(__BYTE_ORDER) && __BYTE_ORDER +# if defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) +# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN +# elif defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) +# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN +# endif +#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ +# if defined(__ORDER_BIG_ENDIAN__) && \ + (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN +# elif defined(__ORDER_LITTLE_ENDIAN__) && \ + (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN +# endif +#elif (defined(__LITTLE_ENDIAN__) && __LITTLE_ENDIAN__ == 1) || \ + defined(__i386) || defined(__i386__) || \ + defined(_X86_) || defined(__X86__) || \ + defined(_M_IX86) || defined(__THW_INTEL__) || \ + defined(__x86_64) || defined(__x86_64__) || \ + defined(__amd64) || defined(__amd64__) || \ + defined(_M_AMD64) || defined(_M_X64) || \ + defined(_M_ARM) || defined(_M_ARM64) || \ + defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \ + defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \ + defined(__EMSCRIPTEN__) || defined(__wasm__) || \ + defined(__loongarch__) +# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN +#elif (defined(__BIG_ENDIAN__) && __BIG_ENDIAN__ == 1) || \ + defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \ + defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || \ + defined(__or1k__) || defined(__OR1K__) +# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN +#else +# define YYJSON_ENDIAN 0 /* unknown endian, detect at run-time */ +#endif + +/* + This macro controls how yyjson handles unaligned memory accesses. + + By default, yyjson uses `memcpy()` for memory copying. This allows the compiler + to optimize the code and emit unaligned memory access instructions when + supported by the target architecture. + + However, on some older compilers or architectures where `memcpy()` is not + well-optimized and may result in unnecessary function calls, defining this + macro as 1 may help. In such cases, yyjson switches to manual byte-by-byte + access, which can potentially improve performance. + + An example of the generated assembly code for ARM can be found here: + https://godbolt.org/z/334jjhxPT + + This flag is already enabled for common architectures in the following code, + so manual configuration is usually unnecessary. If unsure, you can check the + generated assembly or run benchmarks to make an informed decision. + */ +#ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS +# if defined(__ia64) || defined(_IA64) || defined(__IA64__) || \ + defined(__ia64__) || defined(_M_IA64) || defined(__itanium__) +# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* Itanium */ +# elif (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) && \ + (defined(__GNUC__) || defined(__clang__)) && \ + (!defined(__ARM_FEATURE_UNALIGNED) || !__ARM_FEATURE_UNALIGNED) +# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* ARM */ +# elif defined(__sparc) || defined(__sparc__) +# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* SPARC */ +# elif defined(__mips) || defined(__mips__) || defined(__MIPS__) +# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* MIPS */ +# elif defined(__m68k__) || defined(M68000) +# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* M68K */ +# else +# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 0 +# endif +#endif + +/* + Estimated initial ratio of the JSON data (data_size / value_count). + For example: + + data: {"id":12345678,"name":"Harry"} + data_size: 30 + value_count: 5 + ratio: 6 + + yyjson uses dynamic memory with a growth factor of 1.5 when reading and writing + JSON, the ratios below are used to determine the initial memory size. + + A too large ratio will waste memory, and a too small ratio will cause multiple + memory growths and degrade performance. Currently, these ratios are generated + with some commonly used JSON datasets. + */ +#define YYJSON_READER_ESTIMATED_PRETTY_RATIO 16 +#define YYJSON_READER_ESTIMATED_MINIFY_RATIO 6 +#define YYJSON_WRITER_ESTIMATED_PRETTY_RATIO 32 +#define YYJSON_WRITER_ESTIMATED_MINIFY_RATIO 18 + +/* The initial and maximum size of the memory pool's chunk in yyjson_mut_doc. */ +#define YYJSON_MUT_DOC_STR_POOL_INIT_SIZE 0x100 +#define YYJSON_MUT_DOC_STR_POOL_MAX_SIZE 0x10000000 +#define YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE (0x10 * sizeof(yyjson_mut_val)) +#define YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE (0x1000000 * sizeof(yyjson_mut_val)) + +/* The minimum size of the dynamic allocator's chunk. */ +#define YYJSON_ALC_DYN_MIN_SIZE 0x1000 + +/* Default value for compile-time options. */ +#ifndef YYJSON_DISABLE_READER +#define YYJSON_DISABLE_READER 0 +#endif +#ifndef YYJSON_DISABLE_WRITER +#define YYJSON_DISABLE_WRITER 0 +#endif +#ifndef YYJSON_DISABLE_INCR_READER +#define YYJSON_DISABLE_INCR_READER 0 +#endif +#ifndef YYJSON_DISABLE_UTILS +#define YYJSON_DISABLE_UTILS 0 +#endif +#ifndef YYJSON_DISABLE_FAST_FP_CONV +#define YYJSON_DISABLE_FAST_FP_CONV 0 +#endif +#ifndef YYJSON_DISABLE_NON_STANDARD +#define YYJSON_DISABLE_NON_STANDARD 0 +#endif +#ifndef YYJSON_DISABLE_UTF8_VALIDATION +#define YYJSON_DISABLE_UTF8_VALIDATION 0 +#endif + + + +/*============================================================================== + * MARK: - Macros (Private) + *============================================================================*/ + +/* Macros used for loop unrolling and other purpose. */ +#define repeat2(x) { x x } +#define repeat4(x) { x x x x } +#define repeat8(x) { x x x x x x x x } +#define repeat16(x) { x x x x x x x x x x x x x x x x } + +#define repeat2_incr(x) { x(0) x(1) } +#define repeat4_incr(x) { x(0) x(1) x(2) x(3) } +#define repeat8_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) } +#define repeat16_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \ + x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) } +#define repeat_in_1_18(x) { x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) \ + x(9) x(10) x(11) x(12) x(13) x(14) x(15) x(16) \ + x(17) x(18) } + +/* Macros used to provide branch prediction information for compiler. */ +#undef likely +#define likely(x) yyjson_likely(x) +#undef unlikely +#define unlikely(x) yyjson_unlikely(x) + +/* Macros used to provide inline information for compiler. */ +#undef static_inline +#define static_inline static yyjson_inline +#undef static_noinline +#define static_noinline static yyjson_noinline + +/* Macros for min and max. */ +#undef yyjson_min +#define yyjson_min(x, y) ((x) < (y) ? (x) : (y)) +#undef yyjson_max +#define yyjson_max(x, y) ((x) > (y) ? (x) : (y)) + +/* Used to write u64 literal for C89 which doesn't support "ULL" suffix. */ +#undef U64 +#define U64(hi, lo) ((((u64)hi##UL) << 32U) + lo##UL) +#undef U32 +#define U32(hi) ((u32)(hi##UL)) + +/* Used to cast away (remove) const qualifier. */ +#define constcast(type) (type)(void *)(size_t)(const void *) + +/* + Compiler barriers for single variables. + + These macros inform GCC that a read or write access to the given memory + location will occur, preventing certain compiler optimizations or reordering + around the access to 'val'. They do not emit any actual instructions. + + This is useful when GCC's default optimization strategies are suboptimal and + precise control over memory access patterns is required. + These barriers are not needed when using Clang or MSVC. + */ +#if YYJSON_IS_REAL_GCC +# define gcc_load_barrier(val) __asm__ volatile(""::"m"(val)) +# define gcc_store_barrier(val) __asm__ volatile("":"=m"(val)) +# define gcc_full_barrier(val) __asm__ volatile("":"=m"(val):"m"(val)) +#else +# define gcc_load_barrier(val) +# define gcc_store_barrier(val) +# define gcc_full_barrier(val) +#endif + + + +/*============================================================================== + * MARK: - Constants (Private) + *============================================================================*/ + +/* Common error messages. */ +#define MSG_FOPEN "failed to open file" +#define MSG_FREAD "failed to read file" +#define MSG_FWRITE "failed to write file" +#define MSG_FCLOSE "failed to close file" +#define MSG_MALLOC "failed to allocate memory" +#define MSG_CHAR_T "invalid literal, expected 'true'" +#define MSG_CHAR_F "invalid literal, expected 'false'" +#define MSG_CHAR_N "invalid literal, expected 'null'" +#define MSG_CHAR "unexpected character, expected a JSON value" +#define MSG_ARR_END "unexpected character, expected ',' or ']'" +#define MSG_OBJ_KEY "unexpected character, expected a string key" +#define MSG_OBJ_SEP "unexpected character, expected ':' after key" +#define MSG_OBJ_END "unexpected character, expected ',' or '}'" +#define MSG_GARBAGE "unexpected content after document" +#define MSG_NOT_END "unexpected end of data" +#define MSG_COMMENT "unclosed multiline comment" +#define MSG_COMMA "trailing comma is not allowed" +#define MSG_NAN_INF "nan or inf number is not allowed" +#define MSG_ERR_TYPE "invalid JSON value type" +#define MSG_ERR_BOM "UTF-8 byte order mark (BOM) is not supported" +#define MSG_ERR_UTF8 "invalid utf-8 encoding in string" +#define MSG_ERR_UTF16 "UTF-16 encoding is not supported" +#define MSG_ERR_UTF32 "UTF-32 encoding is not supported" + +/* U64 constant values */ +#undef U64_MAX +#define U64_MAX U64(0xFFFFFFFF, 0xFFFFFFFF) +#undef I64_MAX +#define I64_MAX U64(0x7FFFFFFF, 0xFFFFFFFF) +#undef USIZE_MAX +#define USIZE_MAX ((usize)(~(usize)0)) + +/* Maximum number of digits for reading u32/u64/usize safety (not overflow). */ +#undef U32_SAFE_DIG +#define U32_SAFE_DIG 9 /* u32 max is 4294967295, 10 digits */ +#undef U64_SAFE_DIG +#define U64_SAFE_DIG 19 /* u64 max is 18446744073709551615, 20 digits */ +#undef USIZE_SAFE_DIG +#define USIZE_SAFE_DIG (sizeof(usize) == 8 ? U64_SAFE_DIG : U32_SAFE_DIG) + +/* Inf bits (positive) */ +#define F64_BITS_INF U64(0x7FF00000, 0x00000000) + +/* NaN bits (quiet NaN, no payload, no sign) */ +#if defined(__hppa__) || (defined(__mips__) && !defined(__mips_nan2008)) +#define F64_BITS_NAN U64(0x7FF7FFFF, 0xFFFFFFFF) +#else +#define F64_BITS_NAN U64(0x7FF80000, 0x00000000) +#endif + +/* maximum significant digits count in decimal when reading double number */ +#define F64_MAX_DEC_DIG 768 + +/* maximum decimal power of double number (1.7976931348623157e308) */ +#define F64_MAX_DEC_EXP 308 + +/* minimum decimal power of double number (4.9406564584124654e-324) */ +#define F64_MIN_DEC_EXP (-324) + +/* maximum binary power of double number */ +#define F64_MAX_BIN_EXP 1024 + +/* minimum binary power of double number */ +#define F64_MIN_BIN_EXP (-1021) + +/* float/double number bits */ +#define F32_BITS 32 +#define F64_BITS 64 + +/* float/double number exponent part bits */ +#define F32_EXP_BITS 8 +#define F64_EXP_BITS 11 + +/* float/double number significand part bits */ +#define F32_SIG_BITS 23 +#define F64_SIG_BITS 52 + +/* float/double number significand part bits (with 1 hidden bit) */ +#define F32_SIG_FULL_BITS 24 +#define F64_SIG_FULL_BITS 53 + +/* float/double number significand bit mask */ +#define F32_SIG_MASK U32(0x007FFFFF) +#define F64_SIG_MASK U64(0x000FFFFF, 0xFFFFFFFF) + +/* float/double number exponent bit mask */ +#define F32_EXP_MASK U32(0x7F800000) +#define F64_EXP_MASK U64(0x7FF00000, 0x00000000) + +/* float/double number exponent bias */ +#define F32_EXP_BIAS 127 +#define F64_EXP_BIAS 1023 + +/* float/double number significant digits count in decimal */ +#define F32_DEC_DIG 9 +#define F64_DEC_DIG 17 + +/* buffer length required for float/double number writer */ +#define FP_BUF_LEN 40 + +/* maximum length of a number in incremental parsing */ +#define INCR_NUM_MAX_LEN 1024 + + + +/*============================================================================== + * MARK: - Types (Private) + *============================================================================*/ + +/** Type define for primitive types. */ +typedef float f32; +typedef double f64; +typedef int8_t i8; +typedef uint8_t u8; +typedef int16_t i16; +typedef uint16_t u16; +typedef int32_t i32; +typedef uint32_t u32; +typedef int64_t i64; +typedef uint64_t u64; +typedef size_t usize; + +/** 128-bit integer, used by floating-point number reader and writer. */ +#if YYJSON_HAS_INT128 +__extension__ typedef __int128 i128; +__extension__ typedef unsigned __int128 u128; +#endif + +/** 16/32/64-bit vector */ +typedef struct v16 { char c[2]; } v16; +typedef struct v32 { char c[4]; } v32; +typedef struct v64 { char c[8]; } v64; + +/** 16/32/64-bit vector union */ +typedef union v16_uni { v16 v; u16 u; } v16_uni; +typedef union v32_uni { v32 v; u32 u; } v32_uni; +typedef union v64_uni { v64 v; u64 u; } v64_uni; + + + +/*============================================================================== + * MARK: - Load/Store Utils (Private) + *============================================================================*/ + +#define byte_move_idx(x) ((char *)dst)[x] = ((const char *)src)[x]; +#define byte_move_src(x) ((char *)tmp)[x] = ((const char *)src)[x]; +#define byte_move_dst(x) ((char *)dst)[x] = ((const char *)tmp)[x]; + +/** Same as `memcpy(dst, src, 2)`, no overlap. */ +static_inline void byte_copy_2(void *dst, const void *src) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + memcpy(dst, src, 2); +#else + repeat2_incr(byte_move_idx) +#endif +} + +/** Same as `memcpy(dst, src, 4)`, no overlap. */ +static_inline void byte_copy_4(void *dst, const void *src) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + memcpy(dst, src, 4); +#else + repeat4_incr(byte_move_idx) +#endif +} + +/** Same as `memcpy(dst, src, 8)`, no overlap. */ +static_inline void byte_copy_8(void *dst, const void *src) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + memcpy(dst, src, 8); +#else + repeat8_incr(byte_move_idx) +#endif +} + +/** Same as `memcpy(dst, src, 16)`, no overlap. */ +static_inline void byte_copy_16(void *dst, const void *src) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + memcpy(dst, src, 16); +#else + repeat16_incr(byte_move_idx) +#endif +} + +/** Same as `memmove(dst, src, 2)`, allows overlap. */ +static_inline void byte_move_2(void *dst, const void *src) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + u16 tmp; + memcpy(&tmp, src, 2); + memcpy(dst, &tmp, 2); +#else + char tmp[2]; + repeat2_incr(byte_move_src) + repeat2_incr(byte_move_dst) +#endif +} + +/** Same as `memmove(dst, src, 4)`, allows overlap. */ +static_inline void byte_move_4(void *dst, const void *src) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + u32 tmp; + memcpy(&tmp, src, 4); + memcpy(dst, &tmp, 4); +#else + char tmp[4]; + repeat4_incr(byte_move_src) + repeat4_incr(byte_move_dst) +#endif +} + +/** Same as `memmove(dst, src, 8)`, allows overlap. */ +static_inline void byte_move_8(void *dst, const void *src) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + u64 tmp; + memcpy(&tmp, src, 8); + memcpy(dst, &tmp, 8); +#else + char tmp[8]; + repeat8_incr(byte_move_src) + repeat8_incr(byte_move_dst) +#endif +} + +/** Same as `memmove(dst, src, 16)`, allows overlap. */ +static_inline void byte_move_16(void *dst, const void *src) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + char *pdst = (char *)dst; + const char *psrc = (const char *)src; + u64 tmp1, tmp2; + memcpy(&tmp1, psrc, 8); + memcpy(&tmp2, psrc + 8, 8); + memcpy(pdst, &tmp1, 8); + memcpy(pdst + 8, &tmp2, 8); +#else + char tmp[16]; + repeat16_incr(byte_move_src) + repeat16_incr(byte_move_dst) +#endif +} + +/** Same as `memmove(dst, src, n)`, but only `dst <= src` and `n <= 16`. */ +static_inline void byte_move_forward(void *dst, void *src, usize n) { + char *d = (char *)dst, *s = (char *)src; + n += (n % 2); /* round up to even */ + if (n == 16) { byte_move_16(d, s); return; } + if (n >= 8) { byte_move_8(d, s); n -= 8; d += 8; s += 8; } + if (n >= 4) { byte_move_4(d, s); n -= 4; d += 4; s += 4; } + if (n >= 2) { byte_move_2(d, s); } +} + +/** Same as `memcmp(buf, pat, 2) == 0`. */ +static_inline bool byte_match_2(void *buf, const char *pat) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + v16_uni u1, u2; + memcpy(&u1, buf, 2); + memcpy(&u2, pat, 2); + return u1.u == u2.u; +#else + return ((char *)buf)[0] == ((const char *)pat)[0] && + ((char *)buf)[1] == ((const char *)pat)[1]; +#endif +} + +/** Same as `memcmp(buf, pat, 4) == 0`. */ +static_inline bool byte_match_4(void *buf, const char *pat) { +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + v32_uni u1, u2; + memcpy(&u1, buf, 4); + memcpy(&u2, pat, 4); + return u1.u == u2.u; +#else + return ((char *)buf)[0] == ((const char *)pat)[0] && + ((char *)buf)[1] == ((const char *)pat)[1] && + ((char *)buf)[2] == ((const char *)pat)[2] && + ((char *)buf)[3] == ((const char *)pat)[3]; +#endif +} + +/** Loads 2 bytes from `src` as a u16 (native-endian). */ +static_inline u16 byte_load_2(const void *src) { + v16_uni uni; +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + memcpy(&uni, src, 2); +#else + uni.v.c[0] = ((const char *)src)[0]; + uni.v.c[1] = ((const char *)src)[1]; +#endif + return uni.u; +} + +/** Loads 3 bytes from `src` as a u32 (native-endian). */ +static_inline u32 byte_load_3(const void *src) { + v32_uni uni; +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + memcpy(&uni, src, 2); + uni.v.c[2] = ((const char *)src)[2]; + uni.v.c[3] = 0; +#else + uni.v.c[0] = ((const char *)src)[0]; + uni.v.c[1] = ((const char *)src)[1]; + uni.v.c[2] = ((const char *)src)[2]; + uni.v.c[3] = 0; +#endif + return uni.u; +} + +/** Loads 4 bytes from `src` as a u32 (native-endian). */ +static_inline u32 byte_load_4(const void *src) { + v32_uni uni; +#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS + memcpy(&uni, src, 4); +#else + uni.v.c[0] = ((const char *)src)[0]; + uni.v.c[1] = ((const char *)src)[1]; + uni.v.c[2] = ((const char *)src)[2]; + uni.v.c[3] = ((const char *)src)[3]; +#endif + return uni.u; +} + + + +/*============================================================================== + * MARK: - Character Utils (Private) + * These lookup tables were generated by `misc/make_tables.c`. + *============================================================================*/ + +/* char_table1 */ +#define CHAR_TYPE_ASCII (1 << 0) /* Except: ["\], [0x00-0x1F, 0x80-0xFF] */ +#define CHAR_TYPE_ASCII_SQ (1 << 1) /* Except: ['\], [0x00-0x1F, 0x80-0xFF] */ +#define CHAR_TYPE_SPACE (1 << 2) /* Whitespace: [ \t\n\r] */ +#define CHAR_TYPE_SPACE_EXT (1 << 3) /* Whitespace: [ \t\n\r\v\f], JSON5 */ +#define CHAR_TYPE_NUM (1 << 4) /* Number: [.-+0-9] */ +#define CHAR_TYPE_COMMENT (1 << 5) /* Comment: [/] */ + +/* char_table2 */ +#define CHAR_TYPE_EOL (1 << 0) /* End of line: [\r\n] */ +#define CHAR_TYPE_EOL_EXT (1 << 1) /* End of line: [\r\n], JSON5 */ +#define CHAR_TYPE_ID_START (1 << 2) /* ID start: [_$A-Za-z\], U+0080+ */ +#define CHAR_TYPE_ID_NEXT (1 << 3) /* ID next: [_$A-Za-z0-9\], U+0080+ */ +#define CHAR_TYPE_ID_ASCII (1 << 4) /* ID next ASCII: [_$A-Za-z0-9] */ + +/* char_table3 */ +#define CHAR_TYPE_SIGN (1 << 0) /* [-+] */ +#define CHAR_TYPE_DIGIT (1 << 1) /* [0-9] */ +#define CHAR_TYPE_NONZERO (1 << 2) /* [1-9] */ +#define CHAR_TYPE_EXP (1 << 3) /* [eE] */ +#define CHAR_TYPE_DOT (1 << 4) /* [.] */ + +static const u8 char_table1[256] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x0C, 0x0C, 0x08, 0x08, 0x0C, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0F, 0x03, 0x02, 0x03, 0x03, 0x03, 0x03, 0x01, + 0x03, 0x03, 0x03, 0x13, 0x03, 0x13, 0x13, 0x23, + 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, + 0x13, 0x13, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x00, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +static const u8 char_table2[256] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, + 0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, + 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, + 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, + 0x1C, 0x1C, 0x1C, 0x00, 0x0C, 0x00, 0x00, 0x1C, + 0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, + 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, + 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, + 0x1C, 0x1C, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, + 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C +}; + +static const u8 char_table3[256] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x10, 0x00, + 0x02, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, + 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +/** Match a whitespace: [ \t\n\r]. */ +static_inline bool char_is_space(u8 c) { + return !!(char_table1[c] & CHAR_TYPE_SPACE); +} + +/** Match an extended whitespace: [ \t\n\r\\v\\f], JSON5 whitespace. */ +static_inline bool char_is_space_ext(u8 c) { + return !!(char_table1[c] & CHAR_TYPE_SPACE_EXT); +} + +/** Match a JSON number: [.-+0-9]. */ +static_inline bool char_is_num(u8 c) { + return !!(char_table1[c] & CHAR_TYPE_NUM); +} + +/** Match an ASCII character in string: ["\], [0x00-0x1F, 0x80-0xFF]. */ +static_inline bool char_is_ascii_skip(u8 c) { + return !!(char_table1[c] & CHAR_TYPE_ASCII); +} + +/** Match an ASCII character single-quoted: ['\], [0x00-0x1F, 0x80-0xFF]. */ +static_inline bool char_is_ascii_skip_sq(u8 c) { + return !!(char_table1[c] & CHAR_TYPE_ASCII_SQ); +} + +/** Match a trivia character: extended whitespace or comment. */ +static_inline bool char_is_trivia(u8 c) { + return !!(char_table1[c] & (CHAR_TYPE_SPACE_EXT | CHAR_TYPE_COMMENT)); +} + +/** Match a line end character: [\r\n]. */ +static_inline bool char_is_eol(u8 c) { + return !!(char_table2[c] & CHAR_TYPE_EOL); +} + +/** Match an extended line end character: [\r\n], JSON5 line terminator. */ +static_inline bool char_is_eol_ext(u8 c) { + return !!(char_table2[c] & CHAR_TYPE_EOL_EXT); +} + +/** Match an identifier name start: [_$A-Za-z\], U+0080+. */ +static_inline bool char_is_id_start(u8 c) { + return !!(char_table2[c] & CHAR_TYPE_ID_START); +} + +/** Match an identifier name next: [_$A-Za-z0-9\], U+0080+. */ +static_inline bool char_is_id_next(u8 c) { + return !!(char_table2[c] & CHAR_TYPE_ID_NEXT); +} + +/** Match an identifier name ASCII: [_$A-Za-z0-9]. */ +static_inline bool char_is_id_ascii(u8 c) { + return !!(char_table2[c] & CHAR_TYPE_ID_ASCII); +} + +/** Match a sign: [+-] */ +static_inline bool char_is_sign(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_SIGN); +} + +/** Match a none-zero digit: [1-9] */ +static_inline bool char_is_nonzero(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_NONZERO); +} + +/** Match a digit: [0-9] */ +static_inline bool char_is_digit(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_DIGIT); +} + +/** Match an exponent sign: [eE]. */ +static_inline bool char_is_exp(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_EXP); +} + +/** Match a floating point indicator: [.eE]. */ +static_inline bool char_is_fp(u8 d) { + return !!(char_table3[d] & (CHAR_TYPE_DOT | CHAR_TYPE_EXP)); +} + +/** Match a digit or floating point indicator: [0-9.eE]. */ +static_inline bool char_is_digit_or_fp(u8 d) { + return !!(char_table3[d] & (CHAR_TYPE_DIGIT | CHAR_TYPE_DOT | + CHAR_TYPE_EXP)); +} + +/** Match a JSON container: `{` or `[`. */ +static_inline bool char_is_ctn(u8 c) { + return (c & 0xDF) == 0x5B; /* '[': 0x5B, '{': 0x7B */ +} + +/** Convert ASCII letter to lowercase; valid only for [A-Za-z]. */ +static_inline u8 char_to_lower(u8 c) { + return c | 0x20; +} + +/** Match UTF-8 byte order mask. */ +static_inline bool is_utf8_bom(const u8 *cur) { + return byte_load_3(cur) == byte_load_3("\xEF\xBB\xBF"); +} + +/** Match UTF-16 byte order mask. */ +static_inline bool is_utf16_bom(const u8 *cur) { + return byte_load_2(cur) == byte_load_2("\xFE\xFF") || + byte_load_2(cur) == byte_load_2("\xFF\xFE"); +} + +/** Match UTF-32 byte order mask, need length check to avoid zero padding. */ +static_inline bool is_utf32_bom(const u8 *cur) { + return byte_load_4(cur) == byte_load_4("\x00\x00\xFE\xFF") || + byte_load_4(cur) == byte_load_4("\xFF\xFE\x00\x00"); +} + +/** Get the extended line end length. Used with `char_is_eol_ext`. */ +static_inline usize ext_eol_len(const u8 *cur) { + if (cur[0] < 0x80) return 1; + if (cur[1] == 0x80 && (cur[2] == 0xA8 || cur[2] == 0xA9)) return 3; + return 0; +} + +/** Get the extended whitespace length. Used with `char_is_space_ext`. */ +static_inline usize ext_space_len(const u8 *cur) { + if (cur[0] < 0x80) { + return 1; + } else if (byte_load_2(cur) == byte_load_2("\xC2\xA0")) { + return 2; + } else if (byte_load_2(cur) == byte_load_2("\xE2\x80")) { + if (cur[2] >= 0x80 && cur[2] <= 0x8A) return 3; + if (cur[2] == 0xA8 || cur[2] == 0xA9 || cur[2] == 0xAF) return 3; + } else { + u32 uni = byte_load_3(cur); + if (uni == byte_load_3("\xE1\x9A\x80") || + uni == byte_load_3("\xE2\x81\x9F") || + uni == byte_load_3("\xE3\x80\x80") || + uni == byte_load_3("\xEF\xBB\xBF")) return 3; + } + return 0; +} + + + +/*============================================================================== + * MARK: - Hex Character Reader (Private) + * This function is used by JSON reader to read escaped characters. + *============================================================================*/ + +/** + This table is used to convert 4 hex character sequence to a number. + A valid hex character [0-9A-Fa-f] will mapped to it's raw number [0x00, 0x0F], + an invalid hex character will mapped to [0xF0]. + (generate with misc/make_tables.c) + */ +static const u8 hex_conv_table[256] = { + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0 +}; + +/** Load 4 hex characters to `u16`, return true on valid input. */ +static_inline bool hex_load_4(const u8 *src, u16 *dst) { + u16 c0 = hex_conv_table[src[0]]; + u16 c1 = hex_conv_table[src[1]]; + u16 c2 = hex_conv_table[src[2]]; + u16 c3 = hex_conv_table[src[3]]; + u16 t0 = (u16)((c0 << 8) | c2); + u16 t1 = (u16)((c1 << 8) | c3); + *dst = (u16)((t0 << 4) | t1); + return ((t0 | t1) & (u16)0xF0F0) == 0; +} + +/** Load 2 hex characters to `u8`, return true on valid input. */ +static_inline bool hex_load_2(const u8 *src, u8 *dst) { + u8 c0 = hex_conv_table[src[0]]; + u8 c1 = hex_conv_table[src[1]]; + *dst = (u8)((c0 << 4) | c1); + return ((c0 | c1) & 0xF0) == 0; +} + +/** Match a hexadecimal numeric character: [0-9a-fA-F]. */ +static_inline bool char_is_hex(u8 c) { + return hex_conv_table[c] != 0xF0; +} + + + +/*============================================================================== + * MARK: - UTF8 Validation (Private) + * Each Unicode code point is encoded using 1 to 4 bytes in UTF-8. + * Validation is performed using a 4-byte mask and pattern-based approach, + * which requires the input data to be padded with four zero bytes at the end. + *============================================================================*/ + +/* Macro for concatenating four u8 into a u32 and keeping the byte order. */ +#if YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN +# define utf8_seq_def(name, a, b, c, d) \ + static const u32 utf8_seq_##name = 0x##d##c##b##a##UL; +# define utf8_seq(name) utf8_seq_##name +#elif YYJSON_ENDIAN == YYJSON_BIG_ENDIAN +# define utf8_seq_def(name, a, b, c, d) \ + static const u32 utf8_seq_##name = 0x##a##b##c##d##UL; +# define utf8_seq(name) utf8_seq_##name +#else +# define utf8_seq_def(name, a, b, c, d) \ + static const v32_uni utf8_uni_##name = {{ 0x##a, 0x##b, 0x##c, 0x##d }}; +# define utf8_seq(name) utf8_uni_##name.u +#endif + +/* + 1-byte sequence (U+0000 to U+007F) + bit min [.......0] (U+0000) + bit max [.1111111] (U+007F) + bit mask [x.......] (80) + bit pattern [0.......] (00) + */ +utf8_seq_def(b1_mask, 80, 00, 00, 00) +utf8_seq_def(b1_patt, 00, 00, 00, 00) +#define is_utf8_seq1(uni) ( \ + ((uni & utf8_seq(b1_mask)) == utf8_seq(b1_patt)) ) + +/* + 2-byte sequence (U+0080 to U+07FF) + bit min [......10 ..000000] (U+0080) + bit max [...11111 ..111111] (U+07FF) + bit mask [xxx..... xx......] (E0 C0) + bit pattern [110..... 10......] (C0 80) + bit require [...xxxx. ........] (1E 00) + */ +utf8_seq_def(b2_mask, E0, C0, 00, 00) +utf8_seq_def(b2_patt, C0, 80, 00, 00) +utf8_seq_def(b2_requ, 1E, 00, 00, 00) +#define is_utf8_seq2(uni) ( \ + ((uni & utf8_seq(b2_mask)) == utf8_seq(b2_patt)) && \ + ((uni & utf8_seq(b2_requ))) ) + +/* + 3-byte sequence (U+0800 to U+FFFF) + bit min [........ ..100000 ..000000] (U+0800) + bit max [....1111 ..111111 ..111111] (U+FFFF) + bit mask [xxxx.... xx...... xx......] (F0 C0 C0) + bit pattern [1110.... 10...... 10......] (E0 80 80) + bit require [....xxxx ..x..... ........] (0F 20 00) + + 3-byte invalid sequence, reserved for surrogate halves (U+D800 to U+DFFF) + bit min [....1101 ..100000 ..000000] (U+D800) + bit max [....1101 ..111111 ..111111] (U+DFFF) + bit mask [....xxxx ..x..... ........] (0F 20 00) + bit pattern [....1101 ..1..... ........] (0D 20 00) + */ +utf8_seq_def(b3_mask, F0, C0, C0, 00) +utf8_seq_def(b3_patt, E0, 80, 80, 00) +utf8_seq_def(b3_requ, 0F, 20, 00, 00) +utf8_seq_def(b3_erro, 0D, 20, 00, 00) +#define is_utf8_seq3(uni) ( \ + ((uni & utf8_seq(b3_mask)) == utf8_seq(b3_patt)) && \ + ((tmp = (uni & utf8_seq(b3_requ)))) && \ + ((tmp != utf8_seq(b3_erro))) ) + +/* + 4-byte sequence (U+10000 to U+10FFFF) + bit min [........ ...10000 ..000000 ..000000] (U+10000) + bit max [.....100 ..001111 ..111111 ..111111] (U+10FFFF) + bit mask [xxxxx... xx...... xx...... xx......] (F8 C0 C0 C0) + bit pattern [11110... 10...... 10...... 10......] (F0 80 80 80) + bit require [.....xxx ..xx.... ........ ........] (07 30 00 00) + bit require 1 [.....x.. ........ ........ ........] (04 00 00 00) + bit require 2 [......xx ..xx.... ........ ........] (03 30 00 00) + */ +utf8_seq_def(b4_mask, F8, C0, C0, C0) +utf8_seq_def(b4_patt, F0, 80, 80, 80) +utf8_seq_def(b4_requ, 07, 30, 00, 00) +utf8_seq_def(b4_req1, 04, 00, 00, 00) +utf8_seq_def(b4_req2, 03, 30, 00, 00) +#define is_utf8_seq4(uni) ( \ + ((uni & utf8_seq(b4_mask)) == utf8_seq(b4_patt)) && \ + ((tmp = (uni & utf8_seq(b4_requ)))) && \ + ((tmp & utf8_seq(b4_req1)) == 0 || (tmp & utf8_seq(b4_req2)) == 0) ) + + + +/*============================================================================== + * MARK: - Power10 Lookup Table (Private) + * These data are used by the floating-point number reader and writer. + *============================================================================*/ + +#if !YYJSON_DISABLE_FAST_FP_CONV + +/** Maximum pow10 exponent that can be represented exactly as a float64. */ +#define F64_POW10_MAX_EXACT_EXP 22 + +/** Cached pow10 table. */ +static const f64 f64_pow10_table[F64_POW10_MAX_EXACT_EXP + 1] = { + 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, + 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 +}; + +/** Maximum pow10 exponent that can be represented exactly as a uint64. */ +#define U64_POW10_MAX_EXACT_EXP 19 + +/** Table: [ 10^0, ..., 10^19 ] (generate with misc/make_tables.c) */ +static const u64 u64_pow10_table[U64_POW10_MAX_EXACT_EXP + 1] = { + U64(0x00000000, 0x00000001), U64(0x00000000, 0x0000000A), + U64(0x00000000, 0x00000064), U64(0x00000000, 0x000003E8), + U64(0x00000000, 0x00002710), U64(0x00000000, 0x000186A0), + U64(0x00000000, 0x000F4240), U64(0x00000000, 0x00989680), + U64(0x00000000, 0x05F5E100), U64(0x00000000, 0x3B9ACA00), + U64(0x00000002, 0x540BE400), U64(0x00000017, 0x4876E800), + U64(0x000000E8, 0xD4A51000), U64(0x00000918, 0x4E72A000), + U64(0x00005AF3, 0x107A4000), U64(0x00038D7E, 0xA4C68000), + U64(0x002386F2, 0x6FC10000), U64(0x01634578, 0x5D8A0000), + U64(0x0DE0B6B3, 0xA7640000), U64(0x8AC72304, 0x89E80000) +}; + +/** Minimum decimal exponent in pow10_sig_table. */ +#define POW10_SIG_TABLE_MIN_EXP -343 + +/** Maximum decimal exponent in pow10_sig_table. */ +#define POW10_SIG_TABLE_MAX_EXP 324 + +/** Minimum exact decimal exponent in pow10_sig_table */ +#define POW10_SIG_TABLE_MIN_EXACT_EXP 0 + +/** Maximum exact decimal exponent in pow10_sig_table */ +#define POW10_SIG_TABLE_MAX_EXACT_EXP 55 + +/** Normalized significant 128 bits of pow10, no rounded up (size: 10.4KB). + This lookup table is used by both the double number reader and writer. + (generate with misc/make_tables.c) */ +static const u64 pow10_sig_table[] = { + U64(0xBF29DCAB, 0xA82FDEAE), U64(0x7432EE87, 0x3880FC33), /* ~= 10^-343 */ + U64(0xEEF453D6, 0x923BD65A), U64(0x113FAA29, 0x06A13B3F), /* ~= 10^-342 */ + U64(0x9558B466, 0x1B6565F8), U64(0x4AC7CA59, 0xA424C507), /* ~= 10^-341 */ + U64(0xBAAEE17F, 0xA23EBF76), U64(0x5D79BCF0, 0x0D2DF649), /* ~= 10^-340 */ + U64(0xE95A99DF, 0x8ACE6F53), U64(0xF4D82C2C, 0x107973DC), /* ~= 10^-339 */ + U64(0x91D8A02B, 0xB6C10594), U64(0x79071B9B, 0x8A4BE869), /* ~= 10^-338 */ + U64(0xB64EC836, 0xA47146F9), U64(0x9748E282, 0x6CDEE284), /* ~= 10^-337 */ + U64(0xE3E27A44, 0x4D8D98B7), U64(0xFD1B1B23, 0x08169B25), /* ~= 10^-336 */ + U64(0x8E6D8C6A, 0xB0787F72), U64(0xFE30F0F5, 0xE50E20F7), /* ~= 10^-335 */ + U64(0xB208EF85, 0x5C969F4F), U64(0xBDBD2D33, 0x5E51A935), /* ~= 10^-334 */ + U64(0xDE8B2B66, 0xB3BC4723), U64(0xAD2C7880, 0x35E61382), /* ~= 10^-333 */ + U64(0x8B16FB20, 0x3055AC76), U64(0x4C3BCB50, 0x21AFCC31), /* ~= 10^-332 */ + U64(0xADDCB9E8, 0x3C6B1793), U64(0xDF4ABE24, 0x2A1BBF3D), /* ~= 10^-331 */ + U64(0xD953E862, 0x4B85DD78), U64(0xD71D6DAD, 0x34A2AF0D), /* ~= 10^-330 */ + U64(0x87D4713D, 0x6F33AA6B), U64(0x8672648C, 0x40E5AD68), /* ~= 10^-329 */ + U64(0xA9C98D8C, 0xCB009506), U64(0x680EFDAF, 0x511F18C2), /* ~= 10^-328 */ + U64(0xD43BF0EF, 0xFDC0BA48), U64(0x0212BD1B, 0x2566DEF2), /* ~= 10^-327 */ + U64(0x84A57695, 0xFE98746D), U64(0x014BB630, 0xF7604B57), /* ~= 10^-326 */ + U64(0xA5CED43B, 0x7E3E9188), U64(0x419EA3BD, 0x35385E2D), /* ~= 10^-325 */ + U64(0xCF42894A, 0x5DCE35EA), U64(0x52064CAC, 0x828675B9), /* ~= 10^-324 */ + U64(0x818995CE, 0x7AA0E1B2), U64(0x7343EFEB, 0xD1940993), /* ~= 10^-323 */ + U64(0xA1EBFB42, 0x19491A1F), U64(0x1014EBE6, 0xC5F90BF8), /* ~= 10^-322 */ + U64(0xCA66FA12, 0x9F9B60A6), U64(0xD41A26E0, 0x77774EF6), /* ~= 10^-321 */ + U64(0xFD00B897, 0x478238D0), U64(0x8920B098, 0x955522B4), /* ~= 10^-320 */ + U64(0x9E20735E, 0x8CB16382), U64(0x55B46E5F, 0x5D5535B0), /* ~= 10^-319 */ + U64(0xC5A89036, 0x2FDDBC62), U64(0xEB2189F7, 0x34AA831D), /* ~= 10^-318 */ + U64(0xF712B443, 0xBBD52B7B), U64(0xA5E9EC75, 0x01D523E4), /* ~= 10^-317 */ + U64(0x9A6BB0AA, 0x55653B2D), U64(0x47B233C9, 0x2125366E), /* ~= 10^-316 */ + U64(0xC1069CD4, 0xEABE89F8), U64(0x999EC0BB, 0x696E840A), /* ~= 10^-315 */ + U64(0xF148440A, 0x256E2C76), U64(0xC00670EA, 0x43CA250D), /* ~= 10^-314 */ + U64(0x96CD2A86, 0x5764DBCA), U64(0x38040692, 0x6A5E5728), /* ~= 10^-313 */ + U64(0xBC807527, 0xED3E12BC), U64(0xC6050837, 0x04F5ECF2), /* ~= 10^-312 */ + U64(0xEBA09271, 0xE88D976B), U64(0xF7864A44, 0xC633682E), /* ~= 10^-311 */ + U64(0x93445B87, 0x31587EA3), U64(0x7AB3EE6A, 0xFBE0211D), /* ~= 10^-310 */ + U64(0xB8157268, 0xFDAE9E4C), U64(0x5960EA05, 0xBAD82964), /* ~= 10^-309 */ + U64(0xE61ACF03, 0x3D1A45DF), U64(0x6FB92487, 0x298E33BD), /* ~= 10^-308 */ + U64(0x8FD0C162, 0x06306BAB), U64(0xA5D3B6D4, 0x79F8E056), /* ~= 10^-307 */ + U64(0xB3C4F1BA, 0x87BC8696), U64(0x8F48A489, 0x9877186C), /* ~= 10^-306 */ + U64(0xE0B62E29, 0x29ABA83C), U64(0x331ACDAB, 0xFE94DE87), /* ~= 10^-305 */ + U64(0x8C71DCD9, 0xBA0B4925), U64(0x9FF0C08B, 0x7F1D0B14), /* ~= 10^-304 */ + U64(0xAF8E5410, 0x288E1B6F), U64(0x07ECF0AE, 0x5EE44DD9), /* ~= 10^-303 */ + U64(0xDB71E914, 0x32B1A24A), U64(0xC9E82CD9, 0xF69D6150), /* ~= 10^-302 */ + U64(0x892731AC, 0x9FAF056E), U64(0xBE311C08, 0x3A225CD2), /* ~= 10^-301 */ + U64(0xAB70FE17, 0xC79AC6CA), U64(0x6DBD630A, 0x48AAF406), /* ~= 10^-300 */ + U64(0xD64D3D9D, 0xB981787D), U64(0x092CBBCC, 0xDAD5B108), /* ~= 10^-299 */ + U64(0x85F04682, 0x93F0EB4E), U64(0x25BBF560, 0x08C58EA5), /* ~= 10^-298 */ + U64(0xA76C5823, 0x38ED2621), U64(0xAF2AF2B8, 0x0AF6F24E), /* ~= 10^-297 */ + U64(0xD1476E2C, 0x07286FAA), U64(0x1AF5AF66, 0x0DB4AEE1), /* ~= 10^-296 */ + U64(0x82CCA4DB, 0x847945CA), U64(0x50D98D9F, 0xC890ED4D), /* ~= 10^-295 */ + U64(0xA37FCE12, 0x6597973C), U64(0xE50FF107, 0xBAB528A0), /* ~= 10^-294 */ + U64(0xCC5FC196, 0xFEFD7D0C), U64(0x1E53ED49, 0xA96272C8), /* ~= 10^-293 */ + U64(0xFF77B1FC, 0xBEBCDC4F), U64(0x25E8E89C, 0x13BB0F7A), /* ~= 10^-292 */ + U64(0x9FAACF3D, 0xF73609B1), U64(0x77B19161, 0x8C54E9AC), /* ~= 10^-291 */ + U64(0xC795830D, 0x75038C1D), U64(0xD59DF5B9, 0xEF6A2417), /* ~= 10^-290 */ + U64(0xF97AE3D0, 0xD2446F25), U64(0x4B057328, 0x6B44AD1D), /* ~= 10^-289 */ + U64(0x9BECCE62, 0x836AC577), U64(0x4EE367F9, 0x430AEC32), /* ~= 10^-288 */ + U64(0xC2E801FB, 0x244576D5), U64(0x229C41F7, 0x93CDA73F), /* ~= 10^-287 */ + U64(0xF3A20279, 0xED56D48A), U64(0x6B435275, 0x78C1110F), /* ~= 10^-286 */ + U64(0x9845418C, 0x345644D6), U64(0x830A1389, 0x6B78AAA9), /* ~= 10^-285 */ + U64(0xBE5691EF, 0x416BD60C), U64(0x23CC986B, 0xC656D553), /* ~= 10^-284 */ + U64(0xEDEC366B, 0x11C6CB8F), U64(0x2CBFBE86, 0xB7EC8AA8), /* ~= 10^-283 */ + U64(0x94B3A202, 0xEB1C3F39), U64(0x7BF7D714, 0x32F3D6A9), /* ~= 10^-282 */ + U64(0xB9E08A83, 0xA5E34F07), U64(0xDAF5CCD9, 0x3FB0CC53), /* ~= 10^-281 */ + U64(0xE858AD24, 0x8F5C22C9), U64(0xD1B3400F, 0x8F9CFF68), /* ~= 10^-280 */ + U64(0x91376C36, 0xD99995BE), U64(0x23100809, 0xB9C21FA1), /* ~= 10^-279 */ + U64(0xB5854744, 0x8FFFFB2D), U64(0xABD40A0C, 0x2832A78A), /* ~= 10^-278 */ + U64(0xE2E69915, 0xB3FFF9F9), U64(0x16C90C8F, 0x323F516C), /* ~= 10^-277 */ + U64(0x8DD01FAD, 0x907FFC3B), U64(0xAE3DA7D9, 0x7F6792E3), /* ~= 10^-276 */ + U64(0xB1442798, 0xF49FFB4A), U64(0x99CD11CF, 0xDF41779C), /* ~= 10^-275 */ + U64(0xDD95317F, 0x31C7FA1D), U64(0x40405643, 0xD711D583), /* ~= 10^-274 */ + U64(0x8A7D3EEF, 0x7F1CFC52), U64(0x482835EA, 0x666B2572), /* ~= 10^-273 */ + U64(0xAD1C8EAB, 0x5EE43B66), U64(0xDA324365, 0x0005EECF), /* ~= 10^-272 */ + U64(0xD863B256, 0x369D4A40), U64(0x90BED43E, 0x40076A82), /* ~= 10^-271 */ + U64(0x873E4F75, 0xE2224E68), U64(0x5A7744A6, 0xE804A291), /* ~= 10^-270 */ + U64(0xA90DE353, 0x5AAAE202), U64(0x711515D0, 0xA205CB36), /* ~= 10^-269 */ + U64(0xD3515C28, 0x31559A83), U64(0x0D5A5B44, 0xCA873E03), /* ~= 10^-268 */ + U64(0x8412D999, 0x1ED58091), U64(0xE858790A, 0xFE9486C2), /* ~= 10^-267 */ + U64(0xA5178FFF, 0x668AE0B6), U64(0x626E974D, 0xBE39A872), /* ~= 10^-266 */ + U64(0xCE5D73FF, 0x402D98E3), U64(0xFB0A3D21, 0x2DC8128F), /* ~= 10^-265 */ + U64(0x80FA687F, 0x881C7F8E), U64(0x7CE66634, 0xBC9D0B99), /* ~= 10^-264 */ + U64(0xA139029F, 0x6A239F72), U64(0x1C1FFFC1, 0xEBC44E80), /* ~= 10^-263 */ + U64(0xC9874347, 0x44AC874E), U64(0xA327FFB2, 0x66B56220), /* ~= 10^-262 */ + U64(0xFBE91419, 0x15D7A922), U64(0x4BF1FF9F, 0x0062BAA8), /* ~= 10^-261 */ + U64(0x9D71AC8F, 0xADA6C9B5), U64(0x6F773FC3, 0x603DB4A9), /* ~= 10^-260 */ + U64(0xC4CE17B3, 0x99107C22), U64(0xCB550FB4, 0x384D21D3), /* ~= 10^-259 */ + U64(0xF6019DA0, 0x7F549B2B), U64(0x7E2A53A1, 0x46606A48), /* ~= 10^-258 */ + U64(0x99C10284, 0x4F94E0FB), U64(0x2EDA7444, 0xCBFC426D), /* ~= 10^-257 */ + U64(0xC0314325, 0x637A1939), U64(0xFA911155, 0xFEFB5308), /* ~= 10^-256 */ + U64(0xF03D93EE, 0xBC589F88), U64(0x793555AB, 0x7EBA27CA), /* ~= 10^-255 */ + U64(0x96267C75, 0x35B763B5), U64(0x4BC1558B, 0x2F3458DE), /* ~= 10^-254 */ + U64(0xBBB01B92, 0x83253CA2), U64(0x9EB1AAED, 0xFB016F16), /* ~= 10^-253 */ + U64(0xEA9C2277, 0x23EE8BCB), U64(0x465E15A9, 0x79C1CADC), /* ~= 10^-252 */ + U64(0x92A1958A, 0x7675175F), U64(0x0BFACD89, 0xEC191EC9), /* ~= 10^-251 */ + U64(0xB749FAED, 0x14125D36), U64(0xCEF980EC, 0x671F667B), /* ~= 10^-250 */ + U64(0xE51C79A8, 0x5916F484), U64(0x82B7E127, 0x80E7401A), /* ~= 10^-249 */ + U64(0x8F31CC09, 0x37AE58D2), U64(0xD1B2ECB8, 0xB0908810), /* ~= 10^-248 */ + U64(0xB2FE3F0B, 0x8599EF07), U64(0x861FA7E6, 0xDCB4AA15), /* ~= 10^-247 */ + U64(0xDFBDCECE, 0x67006AC9), U64(0x67A791E0, 0x93E1D49A), /* ~= 10^-246 */ + U64(0x8BD6A141, 0x006042BD), U64(0xE0C8BB2C, 0x5C6D24E0), /* ~= 10^-245 */ + U64(0xAECC4991, 0x4078536D), U64(0x58FAE9F7, 0x73886E18), /* ~= 10^-244 */ + U64(0xDA7F5BF5, 0x90966848), U64(0xAF39A475, 0x506A899E), /* ~= 10^-243 */ + U64(0x888F9979, 0x7A5E012D), U64(0x6D8406C9, 0x52429603), /* ~= 10^-242 */ + U64(0xAAB37FD7, 0xD8F58178), U64(0xC8E5087B, 0xA6D33B83), /* ~= 10^-241 */ + U64(0xD5605FCD, 0xCF32E1D6), U64(0xFB1E4A9A, 0x90880A64), /* ~= 10^-240 */ + U64(0x855C3BE0, 0xA17FCD26), U64(0x5CF2EEA0, 0x9A55067F), /* ~= 10^-239 */ + U64(0xA6B34AD8, 0xC9DFC06F), U64(0xF42FAA48, 0xC0EA481E), /* ~= 10^-238 */ + U64(0xD0601D8E, 0xFC57B08B), U64(0xF13B94DA, 0xF124DA26), /* ~= 10^-237 */ + U64(0x823C1279, 0x5DB6CE57), U64(0x76C53D08, 0xD6B70858), /* ~= 10^-236 */ + U64(0xA2CB1717, 0xB52481ED), U64(0x54768C4B, 0x0C64CA6E), /* ~= 10^-235 */ + U64(0xCB7DDCDD, 0xA26DA268), U64(0xA9942F5D, 0xCF7DFD09), /* ~= 10^-234 */ + U64(0xFE5D5415, 0x0B090B02), U64(0xD3F93B35, 0x435D7C4C), /* ~= 10^-233 */ + U64(0x9EFA548D, 0x26E5A6E1), U64(0xC47BC501, 0x4A1A6DAF), /* ~= 10^-232 */ + U64(0xC6B8E9B0, 0x709F109A), U64(0x359AB641, 0x9CA1091B), /* ~= 10^-231 */ + U64(0xF867241C, 0x8CC6D4C0), U64(0xC30163D2, 0x03C94B62), /* ~= 10^-230 */ + U64(0x9B407691, 0xD7FC44F8), U64(0x79E0DE63, 0x425DCF1D), /* ~= 10^-229 */ + U64(0xC2109436, 0x4DFB5636), U64(0x985915FC, 0x12F542E4), /* ~= 10^-228 */ + U64(0xF294B943, 0xE17A2BC4), U64(0x3E6F5B7B, 0x17B2939D), /* ~= 10^-227 */ + U64(0x979CF3CA, 0x6CEC5B5A), U64(0xA705992C, 0xEECF9C42), /* ~= 10^-226 */ + U64(0xBD8430BD, 0x08277231), U64(0x50C6FF78, 0x2A838353), /* ~= 10^-225 */ + U64(0xECE53CEC, 0x4A314EBD), U64(0xA4F8BF56, 0x35246428), /* ~= 10^-224 */ + U64(0x940F4613, 0xAE5ED136), U64(0x871B7795, 0xE136BE99), /* ~= 10^-223 */ + U64(0xB9131798, 0x99F68584), U64(0x28E2557B, 0x59846E3F), /* ~= 10^-222 */ + U64(0xE757DD7E, 0xC07426E5), U64(0x331AEADA, 0x2FE589CF), /* ~= 10^-221 */ + U64(0x9096EA6F, 0x3848984F), U64(0x3FF0D2C8, 0x5DEF7621), /* ~= 10^-220 */ + U64(0xB4BCA50B, 0x065ABE63), U64(0x0FED077A, 0x756B53A9), /* ~= 10^-219 */ + U64(0xE1EBCE4D, 0xC7F16DFB), U64(0xD3E84959, 0x12C62894), /* ~= 10^-218 */ + U64(0x8D3360F0, 0x9CF6E4BD), U64(0x64712DD7, 0xABBBD95C), /* ~= 10^-217 */ + U64(0xB080392C, 0xC4349DEC), U64(0xBD8D794D, 0x96AACFB3), /* ~= 10^-216 */ + U64(0xDCA04777, 0xF541C567), U64(0xECF0D7A0, 0xFC5583A0), /* ~= 10^-215 */ + U64(0x89E42CAA, 0xF9491B60), U64(0xF41686C4, 0x9DB57244), /* ~= 10^-214 */ + U64(0xAC5D37D5, 0xB79B6239), U64(0x311C2875, 0xC522CED5), /* ~= 10^-213 */ + U64(0xD77485CB, 0x25823AC7), U64(0x7D633293, 0x366B828B), /* ~= 10^-212 */ + U64(0x86A8D39E, 0xF77164BC), U64(0xAE5DFF9C, 0x02033197), /* ~= 10^-211 */ + U64(0xA8530886, 0xB54DBDEB), U64(0xD9F57F83, 0x0283FDFC), /* ~= 10^-210 */ + U64(0xD267CAA8, 0x62A12D66), U64(0xD072DF63, 0xC324FD7B), /* ~= 10^-209 */ + U64(0x8380DEA9, 0x3DA4BC60), U64(0x4247CB9E, 0x59F71E6D), /* ~= 10^-208 */ + U64(0xA4611653, 0x8D0DEB78), U64(0x52D9BE85, 0xF074E608), /* ~= 10^-207 */ + U64(0xCD795BE8, 0x70516656), U64(0x67902E27, 0x6C921F8B), /* ~= 10^-206 */ + U64(0x806BD971, 0x4632DFF6), U64(0x00BA1CD8, 0xA3DB53B6), /* ~= 10^-205 */ + U64(0xA086CFCD, 0x97BF97F3), U64(0x80E8A40E, 0xCCD228A4), /* ~= 10^-204 */ + U64(0xC8A883C0, 0xFDAF7DF0), U64(0x6122CD12, 0x8006B2CD), /* ~= 10^-203 */ + U64(0xFAD2A4B1, 0x3D1B5D6C), U64(0x796B8057, 0x20085F81), /* ~= 10^-202 */ + U64(0x9CC3A6EE, 0xC6311A63), U64(0xCBE33036, 0x74053BB0), /* ~= 10^-201 */ + U64(0xC3F490AA, 0x77BD60FC), U64(0xBEDBFC44, 0x11068A9C), /* ~= 10^-200 */ + U64(0xF4F1B4D5, 0x15ACB93B), U64(0xEE92FB55, 0x15482D44), /* ~= 10^-199 */ + U64(0x99171105, 0x2D8BF3C5), U64(0x751BDD15, 0x2D4D1C4A), /* ~= 10^-198 */ + U64(0xBF5CD546, 0x78EEF0B6), U64(0xD262D45A, 0x78A0635D), /* ~= 10^-197 */ + U64(0xEF340A98, 0x172AACE4), U64(0x86FB8971, 0x16C87C34), /* ~= 10^-196 */ + U64(0x9580869F, 0x0E7AAC0E), U64(0xD45D35E6, 0xAE3D4DA0), /* ~= 10^-195 */ + U64(0xBAE0A846, 0xD2195712), U64(0x89748360, 0x59CCA109), /* ~= 10^-194 */ + U64(0xE998D258, 0x869FACD7), U64(0x2BD1A438, 0x703FC94B), /* ~= 10^-193 */ + U64(0x91FF8377, 0x5423CC06), U64(0x7B6306A3, 0x4627DDCF), /* ~= 10^-192 */ + U64(0xB67F6455, 0x292CBF08), U64(0x1A3BC84C, 0x17B1D542), /* ~= 10^-191 */ + U64(0xE41F3D6A, 0x7377EECA), U64(0x20CABA5F, 0x1D9E4A93), /* ~= 10^-190 */ + U64(0x8E938662, 0x882AF53E), U64(0x547EB47B, 0x7282EE9C), /* ~= 10^-189 */ + U64(0xB23867FB, 0x2A35B28D), U64(0xE99E619A, 0x4F23AA43), /* ~= 10^-188 */ + U64(0xDEC681F9, 0xF4C31F31), U64(0x6405FA00, 0xE2EC94D4), /* ~= 10^-187 */ + U64(0x8B3C113C, 0x38F9F37E), U64(0xDE83BC40, 0x8DD3DD04), /* ~= 10^-186 */ + U64(0xAE0B158B, 0x4738705E), U64(0x9624AB50, 0xB148D445), /* ~= 10^-185 */ + U64(0xD98DDAEE, 0x19068C76), U64(0x3BADD624, 0xDD9B0957), /* ~= 10^-184 */ + U64(0x87F8A8D4, 0xCFA417C9), U64(0xE54CA5D7, 0x0A80E5D6), /* ~= 10^-183 */ + U64(0xA9F6D30A, 0x038D1DBC), U64(0x5E9FCF4C, 0xCD211F4C), /* ~= 10^-182 */ + U64(0xD47487CC, 0x8470652B), U64(0x7647C320, 0x0069671F), /* ~= 10^-181 */ + U64(0x84C8D4DF, 0xD2C63F3B), U64(0x29ECD9F4, 0x0041E073), /* ~= 10^-180 */ + U64(0xA5FB0A17, 0xC777CF09), U64(0xF4681071, 0x00525890), /* ~= 10^-179 */ + U64(0xCF79CC9D, 0xB955C2CC), U64(0x7182148D, 0x4066EEB4), /* ~= 10^-178 */ + U64(0x81AC1FE2, 0x93D599BF), U64(0xC6F14CD8, 0x48405530), /* ~= 10^-177 */ + U64(0xA21727DB, 0x38CB002F), U64(0xB8ADA00E, 0x5A506A7C), /* ~= 10^-176 */ + U64(0xCA9CF1D2, 0x06FDC03B), U64(0xA6D90811, 0xF0E4851C), /* ~= 10^-175 */ + U64(0xFD442E46, 0x88BD304A), U64(0x908F4A16, 0x6D1DA663), /* ~= 10^-174 */ + U64(0x9E4A9CEC, 0x15763E2E), U64(0x9A598E4E, 0x043287FE), /* ~= 10^-173 */ + U64(0xC5DD4427, 0x1AD3CDBA), U64(0x40EFF1E1, 0x853F29FD), /* ~= 10^-172 */ + U64(0xF7549530, 0xE188C128), U64(0xD12BEE59, 0xE68EF47C), /* ~= 10^-171 */ + U64(0x9A94DD3E, 0x8CF578B9), U64(0x82BB74F8, 0x301958CE), /* ~= 10^-170 */ + U64(0xC13A148E, 0x3032D6E7), U64(0xE36A5236, 0x3C1FAF01), /* ~= 10^-169 */ + U64(0xF18899B1, 0xBC3F8CA1), U64(0xDC44E6C3, 0xCB279AC1), /* ~= 10^-168 */ + U64(0x96F5600F, 0x15A7B7E5), U64(0x29AB103A, 0x5EF8C0B9), /* ~= 10^-167 */ + U64(0xBCB2B812, 0xDB11A5DE), U64(0x7415D448, 0xF6B6F0E7), /* ~= 10^-166 */ + U64(0xEBDF6617, 0x91D60F56), U64(0x111B495B, 0x3464AD21), /* ~= 10^-165 */ + U64(0x936B9FCE, 0xBB25C995), U64(0xCAB10DD9, 0x00BEEC34), /* ~= 10^-164 */ + U64(0xB84687C2, 0x69EF3BFB), U64(0x3D5D514F, 0x40EEA742), /* ~= 10^-163 */ + U64(0xE65829B3, 0x046B0AFA), U64(0x0CB4A5A3, 0x112A5112), /* ~= 10^-162 */ + U64(0x8FF71A0F, 0xE2C2E6DC), U64(0x47F0E785, 0xEABA72AB), /* ~= 10^-161 */ + U64(0xB3F4E093, 0xDB73A093), U64(0x59ED2167, 0x65690F56), /* ~= 10^-160 */ + U64(0xE0F218B8, 0xD25088B8), U64(0x306869C1, 0x3EC3532C), /* ~= 10^-159 */ + U64(0x8C974F73, 0x83725573), U64(0x1E414218, 0xC73A13FB), /* ~= 10^-158 */ + U64(0xAFBD2350, 0x644EEACF), U64(0xE5D1929E, 0xF90898FA), /* ~= 10^-157 */ + U64(0xDBAC6C24, 0x7D62A583), U64(0xDF45F746, 0xB74ABF39), /* ~= 10^-156 */ + U64(0x894BC396, 0xCE5DA772), U64(0x6B8BBA8C, 0x328EB783), /* ~= 10^-155 */ + U64(0xAB9EB47C, 0x81F5114F), U64(0x066EA92F, 0x3F326564), /* ~= 10^-154 */ + U64(0xD686619B, 0xA27255A2), U64(0xC80A537B, 0x0EFEFEBD), /* ~= 10^-153 */ + U64(0x8613FD01, 0x45877585), U64(0xBD06742C, 0xE95F5F36), /* ~= 10^-152 */ + U64(0xA798FC41, 0x96E952E7), U64(0x2C481138, 0x23B73704), /* ~= 10^-151 */ + U64(0xD17F3B51, 0xFCA3A7A0), U64(0xF75A1586, 0x2CA504C5), /* ~= 10^-150 */ + U64(0x82EF8513, 0x3DE648C4), U64(0x9A984D73, 0xDBE722FB), /* ~= 10^-149 */ + U64(0xA3AB6658, 0x0D5FDAF5), U64(0xC13E60D0, 0xD2E0EBBA), /* ~= 10^-148 */ + U64(0xCC963FEE, 0x10B7D1B3), U64(0x318DF905, 0x079926A8), /* ~= 10^-147 */ + U64(0xFFBBCFE9, 0x94E5C61F), U64(0xFDF17746, 0x497F7052), /* ~= 10^-146 */ + U64(0x9FD561F1, 0xFD0F9BD3), U64(0xFEB6EA8B, 0xEDEFA633), /* ~= 10^-145 */ + U64(0xC7CABA6E, 0x7C5382C8), U64(0xFE64A52E, 0xE96B8FC0), /* ~= 10^-144 */ + U64(0xF9BD690A, 0x1B68637B), U64(0x3DFDCE7A, 0xA3C673B0), /* ~= 10^-143 */ + U64(0x9C1661A6, 0x51213E2D), U64(0x06BEA10C, 0xA65C084E), /* ~= 10^-142 */ + U64(0xC31BFA0F, 0xE5698DB8), U64(0x486E494F, 0xCFF30A62), /* ~= 10^-141 */ + U64(0xF3E2F893, 0xDEC3F126), U64(0x5A89DBA3, 0xC3EFCCFA), /* ~= 10^-140 */ + U64(0x986DDB5C, 0x6B3A76B7), U64(0xF8962946, 0x5A75E01C), /* ~= 10^-139 */ + U64(0xBE895233, 0x86091465), U64(0xF6BBB397, 0xF1135823), /* ~= 10^-138 */ + U64(0xEE2BA6C0, 0x678B597F), U64(0x746AA07D, 0xED582E2C), /* ~= 10^-137 */ + U64(0x94DB4838, 0x40B717EF), U64(0xA8C2A44E, 0xB4571CDC), /* ~= 10^-136 */ + U64(0xBA121A46, 0x50E4DDEB), U64(0x92F34D62, 0x616CE413), /* ~= 10^-135 */ + U64(0xE896A0D7, 0xE51E1566), U64(0x77B020BA, 0xF9C81D17), /* ~= 10^-134 */ + U64(0x915E2486, 0xEF32CD60), U64(0x0ACE1474, 0xDC1D122E), /* ~= 10^-133 */ + U64(0xB5B5ADA8, 0xAAFF80B8), U64(0x0D819992, 0x132456BA), /* ~= 10^-132 */ + U64(0xE3231912, 0xD5BF60E6), U64(0x10E1FFF6, 0x97ED6C69), /* ~= 10^-131 */ + U64(0x8DF5EFAB, 0xC5979C8F), U64(0xCA8D3FFA, 0x1EF463C1), /* ~= 10^-130 */ + U64(0xB1736B96, 0xB6FD83B3), U64(0xBD308FF8, 0xA6B17CB2), /* ~= 10^-129 */ + U64(0xDDD0467C, 0x64BCE4A0), U64(0xAC7CB3F6, 0xD05DDBDE), /* ~= 10^-128 */ + U64(0x8AA22C0D, 0xBEF60EE4), U64(0x6BCDF07A, 0x423AA96B), /* ~= 10^-127 */ + U64(0xAD4AB711, 0x2EB3929D), U64(0x86C16C98, 0xD2C953C6), /* ~= 10^-126 */ + U64(0xD89D64D5, 0x7A607744), U64(0xE871C7BF, 0x077BA8B7), /* ~= 10^-125 */ + U64(0x87625F05, 0x6C7C4A8B), U64(0x11471CD7, 0x64AD4972), /* ~= 10^-124 */ + U64(0xA93AF6C6, 0xC79B5D2D), U64(0xD598E40D, 0x3DD89BCF), /* ~= 10^-123 */ + U64(0xD389B478, 0x79823479), U64(0x4AFF1D10, 0x8D4EC2C3), /* ~= 10^-122 */ + U64(0x843610CB, 0x4BF160CB), U64(0xCEDF722A, 0x585139BA), /* ~= 10^-121 */ + U64(0xA54394FE, 0x1EEDB8FE), U64(0xC2974EB4, 0xEE658828), /* ~= 10^-120 */ + U64(0xCE947A3D, 0xA6A9273E), U64(0x733D2262, 0x29FEEA32), /* ~= 10^-119 */ + U64(0x811CCC66, 0x8829B887), U64(0x0806357D, 0x5A3F525F), /* ~= 10^-118 */ + U64(0xA163FF80, 0x2A3426A8), U64(0xCA07C2DC, 0xB0CF26F7), /* ~= 10^-117 */ + U64(0xC9BCFF60, 0x34C13052), U64(0xFC89B393, 0xDD02F0B5), /* ~= 10^-116 */ + U64(0xFC2C3F38, 0x41F17C67), U64(0xBBAC2078, 0xD443ACE2), /* ~= 10^-115 */ + U64(0x9D9BA783, 0x2936EDC0), U64(0xD54B944B, 0x84AA4C0D), /* ~= 10^-114 */ + U64(0xC5029163, 0xF384A931), U64(0x0A9E795E, 0x65D4DF11), /* ~= 10^-113 */ + U64(0xF64335BC, 0xF065D37D), U64(0x4D4617B5, 0xFF4A16D5), /* ~= 10^-112 */ + U64(0x99EA0196, 0x163FA42E), U64(0x504BCED1, 0xBF8E4E45), /* ~= 10^-111 */ + U64(0xC06481FB, 0x9BCF8D39), U64(0xE45EC286, 0x2F71E1D6), /* ~= 10^-110 */ + U64(0xF07DA27A, 0x82C37088), U64(0x5D767327, 0xBB4E5A4C), /* ~= 10^-109 */ + U64(0x964E858C, 0x91BA2655), U64(0x3A6A07F8, 0xD510F86F), /* ~= 10^-108 */ + U64(0xBBE226EF, 0xB628AFEA), U64(0x890489F7, 0x0A55368B), /* ~= 10^-107 */ + U64(0xEADAB0AB, 0xA3B2DBE5), U64(0x2B45AC74, 0xCCEA842E), /* ~= 10^-106 */ + U64(0x92C8AE6B, 0x464FC96F), U64(0x3B0B8BC9, 0x0012929D), /* ~= 10^-105 */ + U64(0xB77ADA06, 0x17E3BBCB), U64(0x09CE6EBB, 0x40173744), /* ~= 10^-104 */ + U64(0xE5599087, 0x9DDCAABD), U64(0xCC420A6A, 0x101D0515), /* ~= 10^-103 */ + U64(0x8F57FA54, 0xC2A9EAB6), U64(0x9FA94682, 0x4A12232D), /* ~= 10^-102 */ + U64(0xB32DF8E9, 0xF3546564), U64(0x47939822, 0xDC96ABF9), /* ~= 10^-101 */ + U64(0xDFF97724, 0x70297EBD), U64(0x59787E2B, 0x93BC56F7), /* ~= 10^-100 */ + U64(0x8BFBEA76, 0xC619EF36), U64(0x57EB4EDB, 0x3C55B65A), /* ~= 10^-99 */ + U64(0xAEFAE514, 0x77A06B03), U64(0xEDE62292, 0x0B6B23F1), /* ~= 10^-98 */ + U64(0xDAB99E59, 0x958885C4), U64(0xE95FAB36, 0x8E45ECED), /* ~= 10^-97 */ + U64(0x88B402F7, 0xFD75539B), U64(0x11DBCB02, 0x18EBB414), /* ~= 10^-96 */ + U64(0xAAE103B5, 0xFCD2A881), U64(0xD652BDC2, 0x9F26A119), /* ~= 10^-95 */ + U64(0xD59944A3, 0x7C0752A2), U64(0x4BE76D33, 0x46F0495F), /* ~= 10^-94 */ + U64(0x857FCAE6, 0x2D8493A5), U64(0x6F70A440, 0x0C562DDB), /* ~= 10^-93 */ + U64(0xA6DFBD9F, 0xB8E5B88E), U64(0xCB4CCD50, 0x0F6BB952), /* ~= 10^-92 */ + U64(0xD097AD07, 0xA71F26B2), U64(0x7E2000A4, 0x1346A7A7), /* ~= 10^-91 */ + U64(0x825ECC24, 0xC873782F), U64(0x8ED40066, 0x8C0C28C8), /* ~= 10^-90 */ + U64(0xA2F67F2D, 0xFA90563B), U64(0x72890080, 0x2F0F32FA), /* ~= 10^-89 */ + U64(0xCBB41EF9, 0x79346BCA), U64(0x4F2B40A0, 0x3AD2FFB9), /* ~= 10^-88 */ + U64(0xFEA126B7, 0xD78186BC), U64(0xE2F610C8, 0x4987BFA8), /* ~= 10^-87 */ + U64(0x9F24B832, 0xE6B0F436), U64(0x0DD9CA7D, 0x2DF4D7C9), /* ~= 10^-86 */ + U64(0xC6EDE63F, 0xA05D3143), U64(0x91503D1C, 0x79720DBB), /* ~= 10^-85 */ + U64(0xF8A95FCF, 0x88747D94), U64(0x75A44C63, 0x97CE912A), /* ~= 10^-84 */ + U64(0x9B69DBE1, 0xB548CE7C), U64(0xC986AFBE, 0x3EE11ABA), /* ~= 10^-83 */ + U64(0xC24452DA, 0x229B021B), U64(0xFBE85BAD, 0xCE996168), /* ~= 10^-82 */ + U64(0xF2D56790, 0xAB41C2A2), U64(0xFAE27299, 0x423FB9C3), /* ~= 10^-81 */ + U64(0x97C560BA, 0x6B0919A5), U64(0xDCCD879F, 0xC967D41A), /* ~= 10^-80 */ + U64(0xBDB6B8E9, 0x05CB600F), U64(0x5400E987, 0xBBC1C920), /* ~= 10^-79 */ + U64(0xED246723, 0x473E3813), U64(0x290123E9, 0xAAB23B68), /* ~= 10^-78 */ + U64(0x9436C076, 0x0C86E30B), U64(0xF9A0B672, 0x0AAF6521), /* ~= 10^-77 */ + U64(0xB9447093, 0x8FA89BCE), U64(0xF808E40E, 0x8D5B3E69), /* ~= 10^-76 */ + U64(0xE7958CB8, 0x7392C2C2), U64(0xB60B1D12, 0x30B20E04), /* ~= 10^-75 */ + U64(0x90BD77F3, 0x483BB9B9), U64(0xB1C6F22B, 0x5E6F48C2), /* ~= 10^-74 */ + U64(0xB4ECD5F0, 0x1A4AA828), U64(0x1E38AEB6, 0x360B1AF3), /* ~= 10^-73 */ + U64(0xE2280B6C, 0x20DD5232), U64(0x25C6DA63, 0xC38DE1B0), /* ~= 10^-72 */ + U64(0x8D590723, 0x948A535F), U64(0x579C487E, 0x5A38AD0E), /* ~= 10^-71 */ + U64(0xB0AF48EC, 0x79ACE837), U64(0x2D835A9D, 0xF0C6D851), /* ~= 10^-70 */ + U64(0xDCDB1B27, 0x98182244), U64(0xF8E43145, 0x6CF88E65), /* ~= 10^-69 */ + U64(0x8A08F0F8, 0xBF0F156B), U64(0x1B8E9ECB, 0x641B58FF), /* ~= 10^-68 */ + U64(0xAC8B2D36, 0xEED2DAC5), U64(0xE272467E, 0x3D222F3F), /* ~= 10^-67 */ + U64(0xD7ADF884, 0xAA879177), U64(0x5B0ED81D, 0xCC6ABB0F), /* ~= 10^-66 */ + U64(0x86CCBB52, 0xEA94BAEA), U64(0x98E94712, 0x9FC2B4E9), /* ~= 10^-65 */ + U64(0xA87FEA27, 0xA539E9A5), U64(0x3F2398D7, 0x47B36224), /* ~= 10^-64 */ + U64(0xD29FE4B1, 0x8E88640E), U64(0x8EEC7F0D, 0x19A03AAD), /* ~= 10^-63 */ + U64(0x83A3EEEE, 0xF9153E89), U64(0x1953CF68, 0x300424AC), /* ~= 10^-62 */ + U64(0xA48CEAAA, 0xB75A8E2B), U64(0x5FA8C342, 0x3C052DD7), /* ~= 10^-61 */ + U64(0xCDB02555, 0x653131B6), U64(0x3792F412, 0xCB06794D), /* ~= 10^-60 */ + U64(0x808E1755, 0x5F3EBF11), U64(0xE2BBD88B, 0xBEE40BD0), /* ~= 10^-59 */ + U64(0xA0B19D2A, 0xB70E6ED6), U64(0x5B6ACEAE, 0xAE9D0EC4), /* ~= 10^-58 */ + U64(0xC8DE0475, 0x64D20A8B), U64(0xF245825A, 0x5A445275), /* ~= 10^-57 */ + U64(0xFB158592, 0xBE068D2E), U64(0xEED6E2F0, 0xF0D56712), /* ~= 10^-56 */ + U64(0x9CED737B, 0xB6C4183D), U64(0x55464DD6, 0x9685606B), /* ~= 10^-55 */ + U64(0xC428D05A, 0xA4751E4C), U64(0xAA97E14C, 0x3C26B886), /* ~= 10^-54 */ + U64(0xF5330471, 0x4D9265DF), U64(0xD53DD99F, 0x4B3066A8), /* ~= 10^-53 */ + U64(0x993FE2C6, 0xD07B7FAB), U64(0xE546A803, 0x8EFE4029), /* ~= 10^-52 */ + U64(0xBF8FDB78, 0x849A5F96), U64(0xDE985204, 0x72BDD033), /* ~= 10^-51 */ + U64(0xEF73D256, 0xA5C0F77C), U64(0x963E6685, 0x8F6D4440), /* ~= 10^-50 */ + U64(0x95A86376, 0x27989AAD), U64(0xDDE70013, 0x79A44AA8), /* ~= 10^-49 */ + U64(0xBB127C53, 0xB17EC159), U64(0x5560C018, 0x580D5D52), /* ~= 10^-48 */ + U64(0xE9D71B68, 0x9DDE71AF), U64(0xAAB8F01E, 0x6E10B4A6), /* ~= 10^-47 */ + U64(0x92267121, 0x62AB070D), U64(0xCAB39613, 0x04CA70E8), /* ~= 10^-46 */ + U64(0xB6B00D69, 0xBB55C8D1), U64(0x3D607B97, 0xC5FD0D22), /* ~= 10^-45 */ + U64(0xE45C10C4, 0x2A2B3B05), U64(0x8CB89A7D, 0xB77C506A), /* ~= 10^-44 */ + U64(0x8EB98A7A, 0x9A5B04E3), U64(0x77F3608E, 0x92ADB242), /* ~= 10^-43 */ + U64(0xB267ED19, 0x40F1C61C), U64(0x55F038B2, 0x37591ED3), /* ~= 10^-42 */ + U64(0xDF01E85F, 0x912E37A3), U64(0x6B6C46DE, 0xC52F6688), /* ~= 10^-41 */ + U64(0x8B61313B, 0xBABCE2C6), U64(0x2323AC4B, 0x3B3DA015), /* ~= 10^-40 */ + U64(0xAE397D8A, 0xA96C1B77), U64(0xABEC975E, 0x0A0D081A), /* ~= 10^-39 */ + U64(0xD9C7DCED, 0x53C72255), U64(0x96E7BD35, 0x8C904A21), /* ~= 10^-38 */ + U64(0x881CEA14, 0x545C7575), U64(0x7E50D641, 0x77DA2E54), /* ~= 10^-37 */ + U64(0xAA242499, 0x697392D2), U64(0xDDE50BD1, 0xD5D0B9E9), /* ~= 10^-36 */ + U64(0xD4AD2DBF, 0xC3D07787), U64(0x955E4EC6, 0x4B44E864), /* ~= 10^-35 */ + U64(0x84EC3C97, 0xDA624AB4), U64(0xBD5AF13B, 0xEF0B113E), /* ~= 10^-34 */ + U64(0xA6274BBD, 0xD0FADD61), U64(0xECB1AD8A, 0xEACDD58E), /* ~= 10^-33 */ + U64(0xCFB11EAD, 0x453994BA), U64(0x67DE18ED, 0xA5814AF2), /* ~= 10^-32 */ + U64(0x81CEB32C, 0x4B43FCF4), U64(0x80EACF94, 0x8770CED7), /* ~= 10^-31 */ + U64(0xA2425FF7, 0x5E14FC31), U64(0xA1258379, 0xA94D028D), /* ~= 10^-30 */ + U64(0xCAD2F7F5, 0x359A3B3E), U64(0x096EE458, 0x13A04330), /* ~= 10^-29 */ + U64(0xFD87B5F2, 0x8300CA0D), U64(0x8BCA9D6E, 0x188853FC), /* ~= 10^-28 */ + U64(0x9E74D1B7, 0x91E07E48), U64(0x775EA264, 0xCF55347D), /* ~= 10^-27 */ + U64(0xC6120625, 0x76589DDA), U64(0x95364AFE, 0x032A819D), /* ~= 10^-26 */ + U64(0xF79687AE, 0xD3EEC551), U64(0x3A83DDBD, 0x83F52204), /* ~= 10^-25 */ + U64(0x9ABE14CD, 0x44753B52), U64(0xC4926A96, 0x72793542), /* ~= 10^-24 */ + U64(0xC16D9A00, 0x95928A27), U64(0x75B7053C, 0x0F178293), /* ~= 10^-23 */ + U64(0xF1C90080, 0xBAF72CB1), U64(0x5324C68B, 0x12DD6338), /* ~= 10^-22 */ + U64(0x971DA050, 0x74DA7BEE), U64(0xD3F6FC16, 0xEBCA5E03), /* ~= 10^-21 */ + U64(0xBCE50864, 0x92111AEA), U64(0x88F4BB1C, 0xA6BCF584), /* ~= 10^-20 */ + U64(0xEC1E4A7D, 0xB69561A5), U64(0x2B31E9E3, 0xD06C32E5), /* ~= 10^-19 */ + U64(0x9392EE8E, 0x921D5D07), U64(0x3AFF322E, 0x62439FCF), /* ~= 10^-18 */ + U64(0xB877AA32, 0x36A4B449), U64(0x09BEFEB9, 0xFAD487C2), /* ~= 10^-17 */ + U64(0xE69594BE, 0xC44DE15B), U64(0x4C2EBE68, 0x7989A9B3), /* ~= 10^-16 */ + U64(0x901D7CF7, 0x3AB0ACD9), U64(0x0F9D3701, 0x4BF60A10), /* ~= 10^-15 */ + U64(0xB424DC35, 0x095CD80F), U64(0x538484C1, 0x9EF38C94), /* ~= 10^-14 */ + U64(0xE12E1342, 0x4BB40E13), U64(0x2865A5F2, 0x06B06FB9), /* ~= 10^-13 */ + U64(0x8CBCCC09, 0x6F5088CB), U64(0xF93F87B7, 0x442E45D3), /* ~= 10^-12 */ + U64(0xAFEBFF0B, 0xCB24AAFE), U64(0xF78F69A5, 0x1539D748), /* ~= 10^-11 */ + U64(0xDBE6FECE, 0xBDEDD5BE), U64(0xB573440E, 0x5A884D1B), /* ~= 10^-10 */ + U64(0x89705F41, 0x36B4A597), U64(0x31680A88, 0xF8953030), /* ~= 10^-9 */ + U64(0xABCC7711, 0x8461CEFC), U64(0xFDC20D2B, 0x36BA7C3D), /* ~= 10^-8 */ + U64(0xD6BF94D5, 0xE57A42BC), U64(0x3D329076, 0x04691B4C), /* ~= 10^-7 */ + U64(0x8637BD05, 0xAF6C69B5), U64(0xA63F9A49, 0xC2C1B10F), /* ~= 10^-6 */ + U64(0xA7C5AC47, 0x1B478423), U64(0x0FCF80DC, 0x33721D53), /* ~= 10^-5 */ + U64(0xD1B71758, 0xE219652B), U64(0xD3C36113, 0x404EA4A8), /* ~= 10^-4 */ + U64(0x83126E97, 0x8D4FDF3B), U64(0x645A1CAC, 0x083126E9), /* ~= 10^-3 */ + U64(0xA3D70A3D, 0x70A3D70A), U64(0x3D70A3D7, 0x0A3D70A3), /* ~= 10^-2 */ + U64(0xCCCCCCCC, 0xCCCCCCCC), U64(0xCCCCCCCC, 0xCCCCCCCC), /* ~= 10^-1 */ + U64(0x80000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^0 */ + U64(0xA0000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^1 */ + U64(0xC8000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^2 */ + U64(0xFA000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^3 */ + U64(0x9C400000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^4 */ + U64(0xC3500000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^5 */ + U64(0xF4240000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^6 */ + U64(0x98968000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^7 */ + U64(0xBEBC2000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^8 */ + U64(0xEE6B2800, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^9 */ + U64(0x9502F900, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^10 */ + U64(0xBA43B740, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^11 */ + U64(0xE8D4A510, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^12 */ + U64(0x9184E72A, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^13 */ + U64(0xB5E620F4, 0x80000000), U64(0x00000000, 0x00000000), /* == 10^14 */ + U64(0xE35FA931, 0xA0000000), U64(0x00000000, 0x00000000), /* == 10^15 */ + U64(0x8E1BC9BF, 0x04000000), U64(0x00000000, 0x00000000), /* == 10^16 */ + U64(0xB1A2BC2E, 0xC5000000), U64(0x00000000, 0x00000000), /* == 10^17 */ + U64(0xDE0B6B3A, 0x76400000), U64(0x00000000, 0x00000000), /* == 10^18 */ + U64(0x8AC72304, 0x89E80000), U64(0x00000000, 0x00000000), /* == 10^19 */ + U64(0xAD78EBC5, 0xAC620000), U64(0x00000000, 0x00000000), /* == 10^20 */ + U64(0xD8D726B7, 0x177A8000), U64(0x00000000, 0x00000000), /* == 10^21 */ + U64(0x87867832, 0x6EAC9000), U64(0x00000000, 0x00000000), /* == 10^22 */ + U64(0xA968163F, 0x0A57B400), U64(0x00000000, 0x00000000), /* == 10^23 */ + U64(0xD3C21BCE, 0xCCEDA100), U64(0x00000000, 0x00000000), /* == 10^24 */ + U64(0x84595161, 0x401484A0), U64(0x00000000, 0x00000000), /* == 10^25 */ + U64(0xA56FA5B9, 0x9019A5C8), U64(0x00000000, 0x00000000), /* == 10^26 */ + U64(0xCECB8F27, 0xF4200F3A), U64(0x00000000, 0x00000000), /* == 10^27 */ + U64(0x813F3978, 0xF8940984), U64(0x40000000, 0x00000000), /* == 10^28 */ + U64(0xA18F07D7, 0x36B90BE5), U64(0x50000000, 0x00000000), /* == 10^29 */ + U64(0xC9F2C9CD, 0x04674EDE), U64(0xA4000000, 0x00000000), /* == 10^30 */ + U64(0xFC6F7C40, 0x45812296), U64(0x4D000000, 0x00000000), /* == 10^31 */ + U64(0x9DC5ADA8, 0x2B70B59D), U64(0xF0200000, 0x00000000), /* == 10^32 */ + U64(0xC5371912, 0x364CE305), U64(0x6C280000, 0x00000000), /* == 10^33 */ + U64(0xF684DF56, 0xC3E01BC6), U64(0xC7320000, 0x00000000), /* == 10^34 */ + U64(0x9A130B96, 0x3A6C115C), U64(0x3C7F4000, 0x00000000), /* == 10^35 */ + U64(0xC097CE7B, 0xC90715B3), U64(0x4B9F1000, 0x00000000), /* == 10^36 */ + U64(0xF0BDC21A, 0xBB48DB20), U64(0x1E86D400, 0x00000000), /* == 10^37 */ + U64(0x96769950, 0xB50D88F4), U64(0x13144480, 0x00000000), /* == 10^38 */ + U64(0xBC143FA4, 0xE250EB31), U64(0x17D955A0, 0x00000000), /* == 10^39 */ + U64(0xEB194F8E, 0x1AE525FD), U64(0x5DCFAB08, 0x00000000), /* == 10^40 */ + U64(0x92EFD1B8, 0xD0CF37BE), U64(0x5AA1CAE5, 0x00000000), /* == 10^41 */ + U64(0xB7ABC627, 0x050305AD), U64(0xF14A3D9E, 0x40000000), /* == 10^42 */ + U64(0xE596B7B0, 0xC643C719), U64(0x6D9CCD05, 0xD0000000), /* == 10^43 */ + U64(0x8F7E32CE, 0x7BEA5C6F), U64(0xE4820023, 0xA2000000), /* == 10^44 */ + U64(0xB35DBF82, 0x1AE4F38B), U64(0xDDA2802C, 0x8A800000), /* == 10^45 */ + U64(0xE0352F62, 0xA19E306E), U64(0xD50B2037, 0xAD200000), /* == 10^46 */ + U64(0x8C213D9D, 0xA502DE45), U64(0x4526F422, 0xCC340000), /* == 10^47 */ + U64(0xAF298D05, 0x0E4395D6), U64(0x9670B12B, 0x7F410000), /* == 10^48 */ + U64(0xDAF3F046, 0x51D47B4C), U64(0x3C0CDD76, 0x5F114000), /* == 10^49 */ + U64(0x88D8762B, 0xF324CD0F), U64(0xA5880A69, 0xFB6AC800), /* == 10^50 */ + U64(0xAB0E93B6, 0xEFEE0053), U64(0x8EEA0D04, 0x7A457A00), /* == 10^51 */ + U64(0xD5D238A4, 0xABE98068), U64(0x72A49045, 0x98D6D880), /* == 10^52 */ + U64(0x85A36366, 0xEB71F041), U64(0x47A6DA2B, 0x7F864750), /* == 10^53 */ + U64(0xA70C3C40, 0xA64E6C51), U64(0x999090B6, 0x5F67D924), /* == 10^54 */ + U64(0xD0CF4B50, 0xCFE20765), U64(0xFFF4B4E3, 0xF741CF6D), /* == 10^55 */ + U64(0x82818F12, 0x81ED449F), U64(0xBFF8F10E, 0x7A8921A4), /* ~= 10^56 */ + U64(0xA321F2D7, 0x226895C7), U64(0xAFF72D52, 0x192B6A0D), /* ~= 10^57 */ + U64(0xCBEA6F8C, 0xEB02BB39), U64(0x9BF4F8A6, 0x9F764490), /* ~= 10^58 */ + U64(0xFEE50B70, 0x25C36A08), U64(0x02F236D0, 0x4753D5B4), /* ~= 10^59 */ + U64(0x9F4F2726, 0x179A2245), U64(0x01D76242, 0x2C946590), /* ~= 10^60 */ + U64(0xC722F0EF, 0x9D80AAD6), U64(0x424D3AD2, 0xB7B97EF5), /* ~= 10^61 */ + U64(0xF8EBAD2B, 0x84E0D58B), U64(0xD2E08987, 0x65A7DEB2), /* ~= 10^62 */ + U64(0x9B934C3B, 0x330C8577), U64(0x63CC55F4, 0x9F88EB2F), /* ~= 10^63 */ + U64(0xC2781F49, 0xFFCFA6D5), U64(0x3CBF6B71, 0xC76B25FB), /* ~= 10^64 */ + U64(0xF316271C, 0x7FC3908A), U64(0x8BEF464E, 0x3945EF7A), /* ~= 10^65 */ + U64(0x97EDD871, 0xCFDA3A56), U64(0x97758BF0, 0xE3CBB5AC), /* ~= 10^66 */ + U64(0xBDE94E8E, 0x43D0C8EC), U64(0x3D52EEED, 0x1CBEA317), /* ~= 10^67 */ + U64(0xED63A231, 0xD4C4FB27), U64(0x4CA7AAA8, 0x63EE4BDD), /* ~= 10^68 */ + U64(0x945E455F, 0x24FB1CF8), U64(0x8FE8CAA9, 0x3E74EF6A), /* ~= 10^69 */ + U64(0xB975D6B6, 0xEE39E436), U64(0xB3E2FD53, 0x8E122B44), /* ~= 10^70 */ + U64(0xE7D34C64, 0xA9C85D44), U64(0x60DBBCA8, 0x7196B616), /* ~= 10^71 */ + U64(0x90E40FBE, 0xEA1D3A4A), U64(0xBC8955E9, 0x46FE31CD), /* ~= 10^72 */ + U64(0xB51D13AE, 0xA4A488DD), U64(0x6BABAB63, 0x98BDBE41), /* ~= 10^73 */ + U64(0xE264589A, 0x4DCDAB14), U64(0xC696963C, 0x7EED2DD1), /* ~= 10^74 */ + U64(0x8D7EB760, 0x70A08AEC), U64(0xFC1E1DE5, 0xCF543CA2), /* ~= 10^75 */ + U64(0xB0DE6538, 0x8CC8ADA8), U64(0x3B25A55F, 0x43294BCB), /* ~= 10^76 */ + U64(0xDD15FE86, 0xAFFAD912), U64(0x49EF0EB7, 0x13F39EBE), /* ~= 10^77 */ + U64(0x8A2DBF14, 0x2DFCC7AB), U64(0x6E356932, 0x6C784337), /* ~= 10^78 */ + U64(0xACB92ED9, 0x397BF996), U64(0x49C2C37F, 0x07965404), /* ~= 10^79 */ + U64(0xD7E77A8F, 0x87DAF7FB), U64(0xDC33745E, 0xC97BE906), /* ~= 10^80 */ + U64(0x86F0AC99, 0xB4E8DAFD), U64(0x69A028BB, 0x3DED71A3), /* ~= 10^81 */ + U64(0xA8ACD7C0, 0x222311BC), U64(0xC40832EA, 0x0D68CE0C), /* ~= 10^82 */ + U64(0xD2D80DB0, 0x2AABD62B), U64(0xF50A3FA4, 0x90C30190), /* ~= 10^83 */ + U64(0x83C7088E, 0x1AAB65DB), U64(0x792667C6, 0xDA79E0FA), /* ~= 10^84 */ + U64(0xA4B8CAB1, 0xA1563F52), U64(0x577001B8, 0x91185938), /* ~= 10^85 */ + U64(0xCDE6FD5E, 0x09ABCF26), U64(0xED4C0226, 0xB55E6F86), /* ~= 10^86 */ + U64(0x80B05E5A, 0xC60B6178), U64(0x544F8158, 0x315B05B4), /* ~= 10^87 */ + U64(0xA0DC75F1, 0x778E39D6), U64(0x696361AE, 0x3DB1C721), /* ~= 10^88 */ + U64(0xC913936D, 0xD571C84C), U64(0x03BC3A19, 0xCD1E38E9), /* ~= 10^89 */ + U64(0xFB587849, 0x4ACE3A5F), U64(0x04AB48A0, 0x4065C723), /* ~= 10^90 */ + U64(0x9D174B2D, 0xCEC0E47B), U64(0x62EB0D64, 0x283F9C76), /* ~= 10^91 */ + U64(0xC45D1DF9, 0x42711D9A), U64(0x3BA5D0BD, 0x324F8394), /* ~= 10^92 */ + U64(0xF5746577, 0x930D6500), U64(0xCA8F44EC, 0x7EE36479), /* ~= 10^93 */ + U64(0x9968BF6A, 0xBBE85F20), U64(0x7E998B13, 0xCF4E1ECB), /* ~= 10^94 */ + U64(0xBFC2EF45, 0x6AE276E8), U64(0x9E3FEDD8, 0xC321A67E), /* ~= 10^95 */ + U64(0xEFB3AB16, 0xC59B14A2), U64(0xC5CFE94E, 0xF3EA101E), /* ~= 10^96 */ + U64(0x95D04AEE, 0x3B80ECE5), U64(0xBBA1F1D1, 0x58724A12), /* ~= 10^97 */ + U64(0xBB445DA9, 0xCA61281F), U64(0x2A8A6E45, 0xAE8EDC97), /* ~= 10^98 */ + U64(0xEA157514, 0x3CF97226), U64(0xF52D09D7, 0x1A3293BD), /* ~= 10^99 */ + U64(0x924D692C, 0xA61BE758), U64(0x593C2626, 0x705F9C56), /* ~= 10^100 */ + U64(0xB6E0C377, 0xCFA2E12E), U64(0x6F8B2FB0, 0x0C77836C), /* ~= 10^101 */ + U64(0xE498F455, 0xC38B997A), U64(0x0B6DFB9C, 0x0F956447), /* ~= 10^102 */ + U64(0x8EDF98B5, 0x9A373FEC), U64(0x4724BD41, 0x89BD5EAC), /* ~= 10^103 */ + U64(0xB2977EE3, 0x00C50FE7), U64(0x58EDEC91, 0xEC2CB657), /* ~= 10^104 */ + U64(0xDF3D5E9B, 0xC0F653E1), U64(0x2F2967B6, 0x6737E3ED), /* ~= 10^105 */ + U64(0x8B865B21, 0x5899F46C), U64(0xBD79E0D2, 0x0082EE74), /* ~= 10^106 */ + U64(0xAE67F1E9, 0xAEC07187), U64(0xECD85906, 0x80A3AA11), /* ~= 10^107 */ + U64(0xDA01EE64, 0x1A708DE9), U64(0xE80E6F48, 0x20CC9495), /* ~= 10^108 */ + U64(0x884134FE, 0x908658B2), U64(0x3109058D, 0x147FDCDD), /* ~= 10^109 */ + U64(0xAA51823E, 0x34A7EEDE), U64(0xBD4B46F0, 0x599FD415), /* ~= 10^110 */ + U64(0xD4E5E2CD, 0xC1D1EA96), U64(0x6C9E18AC, 0x7007C91A), /* ~= 10^111 */ + U64(0x850FADC0, 0x9923329E), U64(0x03E2CF6B, 0xC604DDB0), /* ~= 10^112 */ + U64(0xA6539930, 0xBF6BFF45), U64(0x84DB8346, 0xB786151C), /* ~= 10^113 */ + U64(0xCFE87F7C, 0xEF46FF16), U64(0xE6126418, 0x65679A63), /* ~= 10^114 */ + U64(0x81F14FAE, 0x158C5F6E), U64(0x4FCB7E8F, 0x3F60C07E), /* ~= 10^115 */ + U64(0xA26DA399, 0x9AEF7749), U64(0xE3BE5E33, 0x0F38F09D), /* ~= 10^116 */ + U64(0xCB090C80, 0x01AB551C), U64(0x5CADF5BF, 0xD3072CC5), /* ~= 10^117 */ + U64(0xFDCB4FA0, 0x02162A63), U64(0x73D9732F, 0xC7C8F7F6), /* ~= 10^118 */ + U64(0x9E9F11C4, 0x014DDA7E), U64(0x2867E7FD, 0xDCDD9AFA), /* ~= 10^119 */ + U64(0xC646D635, 0x01A1511D), U64(0xB281E1FD, 0x541501B8), /* ~= 10^120 */ + U64(0xF7D88BC2, 0x4209A565), U64(0x1F225A7C, 0xA91A4226), /* ~= 10^121 */ + U64(0x9AE75759, 0x6946075F), U64(0x3375788D, 0xE9B06958), /* ~= 10^122 */ + U64(0xC1A12D2F, 0xC3978937), U64(0x0052D6B1, 0x641C83AE), /* ~= 10^123 */ + U64(0xF209787B, 0xB47D6B84), U64(0xC0678C5D, 0xBD23A49A), /* ~= 10^124 */ + U64(0x9745EB4D, 0x50CE6332), U64(0xF840B7BA, 0x963646E0), /* ~= 10^125 */ + U64(0xBD176620, 0xA501FBFF), U64(0xB650E5A9, 0x3BC3D898), /* ~= 10^126 */ + U64(0xEC5D3FA8, 0xCE427AFF), U64(0xA3E51F13, 0x8AB4CEBE), /* ~= 10^127 */ + U64(0x93BA47C9, 0x80E98CDF), U64(0xC66F336C, 0x36B10137), /* ~= 10^128 */ + U64(0xB8A8D9BB, 0xE123F017), U64(0xB80B0047, 0x445D4184), /* ~= 10^129 */ + U64(0xE6D3102A, 0xD96CEC1D), U64(0xA60DC059, 0x157491E5), /* ~= 10^130 */ + U64(0x9043EA1A, 0xC7E41392), U64(0x87C89837, 0xAD68DB2F), /* ~= 10^131 */ + U64(0xB454E4A1, 0x79DD1877), U64(0x29BABE45, 0x98C311FB), /* ~= 10^132 */ + U64(0xE16A1DC9, 0xD8545E94), U64(0xF4296DD6, 0xFEF3D67A), /* ~= 10^133 */ + U64(0x8CE2529E, 0x2734BB1D), U64(0x1899E4A6, 0x5F58660C), /* ~= 10^134 */ + U64(0xB01AE745, 0xB101E9E4), U64(0x5EC05DCF, 0xF72E7F8F), /* ~= 10^135 */ + U64(0xDC21A117, 0x1D42645D), U64(0x76707543, 0xF4FA1F73), /* ~= 10^136 */ + U64(0x899504AE, 0x72497EBA), U64(0x6A06494A, 0x791C53A8), /* ~= 10^137 */ + U64(0xABFA45DA, 0x0EDBDE69), U64(0x0487DB9D, 0x17636892), /* ~= 10^138 */ + U64(0xD6F8D750, 0x9292D603), U64(0x45A9D284, 0x5D3C42B6), /* ~= 10^139 */ + U64(0x865B8692, 0x5B9BC5C2), U64(0x0B8A2392, 0xBA45A9B2), /* ~= 10^140 */ + U64(0xA7F26836, 0xF282B732), U64(0x8E6CAC77, 0x68D7141E), /* ~= 10^141 */ + U64(0xD1EF0244, 0xAF2364FF), U64(0x3207D795, 0x430CD926), /* ~= 10^142 */ + U64(0x8335616A, 0xED761F1F), U64(0x7F44E6BD, 0x49E807B8), /* ~= 10^143 */ + U64(0xA402B9C5, 0xA8D3A6E7), U64(0x5F16206C, 0x9C6209A6), /* ~= 10^144 */ + U64(0xCD036837, 0x130890A1), U64(0x36DBA887, 0xC37A8C0F), /* ~= 10^145 */ + U64(0x80222122, 0x6BE55A64), U64(0xC2494954, 0xDA2C9789), /* ~= 10^146 */ + U64(0xA02AA96B, 0x06DEB0FD), U64(0xF2DB9BAA, 0x10B7BD6C), /* ~= 10^147 */ + U64(0xC83553C5, 0xC8965D3D), U64(0x6F928294, 0x94E5ACC7), /* ~= 10^148 */ + U64(0xFA42A8B7, 0x3ABBF48C), U64(0xCB772339, 0xBA1F17F9), /* ~= 10^149 */ + U64(0x9C69A972, 0x84B578D7), U64(0xFF2A7604, 0x14536EFB), /* ~= 10^150 */ + U64(0xC38413CF, 0x25E2D70D), U64(0xFEF51385, 0x19684ABA), /* ~= 10^151 */ + U64(0xF46518C2, 0xEF5B8CD1), U64(0x7EB25866, 0x5FC25D69), /* ~= 10^152 */ + U64(0x98BF2F79, 0xD5993802), U64(0xEF2F773F, 0xFBD97A61), /* ~= 10^153 */ + U64(0xBEEEFB58, 0x4AFF8603), U64(0xAAFB550F, 0xFACFD8FA), /* ~= 10^154 */ + U64(0xEEAABA2E, 0x5DBF6784), U64(0x95BA2A53, 0xF983CF38), /* ~= 10^155 */ + U64(0x952AB45C, 0xFA97A0B2), U64(0xDD945A74, 0x7BF26183), /* ~= 10^156 */ + U64(0xBA756174, 0x393D88DF), U64(0x94F97111, 0x9AEEF9E4), /* ~= 10^157 */ + U64(0xE912B9D1, 0x478CEB17), U64(0x7A37CD56, 0x01AAB85D), /* ~= 10^158 */ + U64(0x91ABB422, 0xCCB812EE), U64(0xAC62E055, 0xC10AB33A), /* ~= 10^159 */ + U64(0xB616A12B, 0x7FE617AA), U64(0x577B986B, 0x314D6009), /* ~= 10^160 */ + U64(0xE39C4976, 0x5FDF9D94), U64(0xED5A7E85, 0xFDA0B80B), /* ~= 10^161 */ + U64(0x8E41ADE9, 0xFBEBC27D), U64(0x14588F13, 0xBE847307), /* ~= 10^162 */ + U64(0xB1D21964, 0x7AE6B31C), U64(0x596EB2D8, 0xAE258FC8), /* ~= 10^163 */ + U64(0xDE469FBD, 0x99A05FE3), U64(0x6FCA5F8E, 0xD9AEF3BB), /* ~= 10^164 */ + U64(0x8AEC23D6, 0x80043BEE), U64(0x25DE7BB9, 0x480D5854), /* ~= 10^165 */ + U64(0xADA72CCC, 0x20054AE9), U64(0xAF561AA7, 0x9A10AE6A), /* ~= 10^166 */ + U64(0xD910F7FF, 0x28069DA4), U64(0x1B2BA151, 0x8094DA04), /* ~= 10^167 */ + U64(0x87AA9AFF, 0x79042286), U64(0x90FB44D2, 0xF05D0842), /* ~= 10^168 */ + U64(0xA99541BF, 0x57452B28), U64(0x353A1607, 0xAC744A53), /* ~= 10^169 */ + U64(0xD3FA922F, 0x2D1675F2), U64(0x42889B89, 0x97915CE8), /* ~= 10^170 */ + U64(0x847C9B5D, 0x7C2E09B7), U64(0x69956135, 0xFEBADA11), /* ~= 10^171 */ + U64(0xA59BC234, 0xDB398C25), U64(0x43FAB983, 0x7E699095), /* ~= 10^172 */ + U64(0xCF02B2C2, 0x1207EF2E), U64(0x94F967E4, 0x5E03F4BB), /* ~= 10^173 */ + U64(0x8161AFB9, 0x4B44F57D), U64(0x1D1BE0EE, 0xBAC278F5), /* ~= 10^174 */ + U64(0xA1BA1BA7, 0x9E1632DC), U64(0x6462D92A, 0x69731732), /* ~= 10^175 */ + U64(0xCA28A291, 0x859BBF93), U64(0x7D7B8F75, 0x03CFDCFE), /* ~= 10^176 */ + U64(0xFCB2CB35, 0xE702AF78), U64(0x5CDA7352, 0x44C3D43E), /* ~= 10^177 */ + U64(0x9DEFBF01, 0xB061ADAB), U64(0x3A088813, 0x6AFA64A7), /* ~= 10^178 */ + U64(0xC56BAEC2, 0x1C7A1916), U64(0x088AAA18, 0x45B8FDD0), /* ~= 10^179 */ + U64(0xF6C69A72, 0xA3989F5B), U64(0x8AAD549E, 0x57273D45), /* ~= 10^180 */ + U64(0x9A3C2087, 0xA63F6399), U64(0x36AC54E2, 0xF678864B), /* ~= 10^181 */ + U64(0xC0CB28A9, 0x8FCF3C7F), U64(0x84576A1B, 0xB416A7DD), /* ~= 10^182 */ + U64(0xF0FDF2D3, 0xF3C30B9F), U64(0x656D44A2, 0xA11C51D5), /* ~= 10^183 */ + U64(0x969EB7C4, 0x7859E743), U64(0x9F644AE5, 0xA4B1B325), /* ~= 10^184 */ + U64(0xBC4665B5, 0x96706114), U64(0x873D5D9F, 0x0DDE1FEE), /* ~= 10^185 */ + U64(0xEB57FF22, 0xFC0C7959), U64(0xA90CB506, 0xD155A7EA), /* ~= 10^186 */ + U64(0x9316FF75, 0xDD87CBD8), U64(0x09A7F124, 0x42D588F2), /* ~= 10^187 */ + U64(0xB7DCBF53, 0x54E9BECE), U64(0x0C11ED6D, 0x538AEB2F), /* ~= 10^188 */ + U64(0xE5D3EF28, 0x2A242E81), U64(0x8F1668C8, 0xA86DA5FA), /* ~= 10^189 */ + U64(0x8FA47579, 0x1A569D10), U64(0xF96E017D, 0x694487BC), /* ~= 10^190 */ + U64(0xB38D92D7, 0x60EC4455), U64(0x37C981DC, 0xC395A9AC), /* ~= 10^191 */ + U64(0xE070F78D, 0x3927556A), U64(0x85BBE253, 0xF47B1417), /* ~= 10^192 */ + U64(0x8C469AB8, 0x43B89562), U64(0x93956D74, 0x78CCEC8E), /* ~= 10^193 */ + U64(0xAF584166, 0x54A6BABB), U64(0x387AC8D1, 0x970027B2), /* ~= 10^194 */ + U64(0xDB2E51BF, 0xE9D0696A), U64(0x06997B05, 0xFCC0319E), /* ~= 10^195 */ + U64(0x88FCF317, 0xF22241E2), U64(0x441FECE3, 0xBDF81F03), /* ~= 10^196 */ + U64(0xAB3C2FDD, 0xEEAAD25A), U64(0xD527E81C, 0xAD7626C3), /* ~= 10^197 */ + U64(0xD60B3BD5, 0x6A5586F1), U64(0x8A71E223, 0xD8D3B074), /* ~= 10^198 */ + U64(0x85C70565, 0x62757456), U64(0xF6872D56, 0x67844E49), /* ~= 10^199 */ + U64(0xA738C6BE, 0xBB12D16C), U64(0xB428F8AC, 0x016561DB), /* ~= 10^200 */ + U64(0xD106F86E, 0x69D785C7), U64(0xE13336D7, 0x01BEBA52), /* ~= 10^201 */ + U64(0x82A45B45, 0x0226B39C), U64(0xECC00246, 0x61173473), /* ~= 10^202 */ + U64(0xA34D7216, 0x42B06084), U64(0x27F002D7, 0xF95D0190), /* ~= 10^203 */ + U64(0xCC20CE9B, 0xD35C78A5), U64(0x31EC038D, 0xF7B441F4), /* ~= 10^204 */ + U64(0xFF290242, 0xC83396CE), U64(0x7E670471, 0x75A15271), /* ~= 10^205 */ + U64(0x9F79A169, 0xBD203E41), U64(0x0F0062C6, 0xE984D386), /* ~= 10^206 */ + U64(0xC75809C4, 0x2C684DD1), U64(0x52C07B78, 0xA3E60868), /* ~= 10^207 */ + U64(0xF92E0C35, 0x37826145), U64(0xA7709A56, 0xCCDF8A82), /* ~= 10^208 */ + U64(0x9BBCC7A1, 0x42B17CCB), U64(0x88A66076, 0x400BB691), /* ~= 10^209 */ + U64(0xC2ABF989, 0x935DDBFE), U64(0x6ACFF893, 0xD00EA435), /* ~= 10^210 */ + U64(0xF356F7EB, 0xF83552FE), U64(0x0583F6B8, 0xC4124D43), /* ~= 10^211 */ + U64(0x98165AF3, 0x7B2153DE), U64(0xC3727A33, 0x7A8B704A), /* ~= 10^212 */ + U64(0xBE1BF1B0, 0x59E9A8D6), U64(0x744F18C0, 0x592E4C5C), /* ~= 10^213 */ + U64(0xEDA2EE1C, 0x7064130C), U64(0x1162DEF0, 0x6F79DF73), /* ~= 10^214 */ + U64(0x9485D4D1, 0xC63E8BE7), U64(0x8ADDCB56, 0x45AC2BA8), /* ~= 10^215 */ + U64(0xB9A74A06, 0x37CE2EE1), U64(0x6D953E2B, 0xD7173692), /* ~= 10^216 */ + U64(0xE8111C87, 0xC5C1BA99), U64(0xC8FA8DB6, 0xCCDD0437), /* ~= 10^217 */ + U64(0x910AB1D4, 0xDB9914A0), U64(0x1D9C9892, 0x400A22A2), /* ~= 10^218 */ + U64(0xB54D5E4A, 0x127F59C8), U64(0x2503BEB6, 0xD00CAB4B), /* ~= 10^219 */ + U64(0xE2A0B5DC, 0x971F303A), U64(0x2E44AE64, 0x840FD61D), /* ~= 10^220 */ + U64(0x8DA471A9, 0xDE737E24), U64(0x5CEAECFE, 0xD289E5D2), /* ~= 10^221 */ + U64(0xB10D8E14, 0x56105DAD), U64(0x7425A83E, 0x872C5F47), /* ~= 10^222 */ + U64(0xDD50F199, 0x6B947518), U64(0xD12F124E, 0x28F77719), /* ~= 10^223 */ + U64(0x8A5296FF, 0xE33CC92F), U64(0x82BD6B70, 0xD99AAA6F), /* ~= 10^224 */ + U64(0xACE73CBF, 0xDC0BFB7B), U64(0x636CC64D, 0x1001550B), /* ~= 10^225 */ + U64(0xD8210BEF, 0xD30EFA5A), U64(0x3C47F7E0, 0x5401AA4E), /* ~= 10^226 */ + U64(0x8714A775, 0xE3E95C78), U64(0x65ACFAEC, 0x34810A71), /* ~= 10^227 */ + U64(0xA8D9D153, 0x5CE3B396), U64(0x7F1839A7, 0x41A14D0D), /* ~= 10^228 */ + U64(0xD31045A8, 0x341CA07C), U64(0x1EDE4811, 0x1209A050), /* ~= 10^229 */ + U64(0x83EA2B89, 0x2091E44D), U64(0x934AED0A, 0xAB460432), /* ~= 10^230 */ + U64(0xA4E4B66B, 0x68B65D60), U64(0xF81DA84D, 0x5617853F), /* ~= 10^231 */ + U64(0xCE1DE406, 0x42E3F4B9), U64(0x36251260, 0xAB9D668E), /* ~= 10^232 */ + U64(0x80D2AE83, 0xE9CE78F3), U64(0xC1D72B7C, 0x6B426019), /* ~= 10^233 */ + U64(0xA1075A24, 0xE4421730), U64(0xB24CF65B, 0x8612F81F), /* ~= 10^234 */ + U64(0xC94930AE, 0x1D529CFC), U64(0xDEE033F2, 0x6797B627), /* ~= 10^235 */ + U64(0xFB9B7CD9, 0xA4A7443C), U64(0x169840EF, 0x017DA3B1), /* ~= 10^236 */ + U64(0x9D412E08, 0x06E88AA5), U64(0x8E1F2895, 0x60EE864E), /* ~= 10^237 */ + U64(0xC491798A, 0x08A2AD4E), U64(0xF1A6F2BA, 0xB92A27E2), /* ~= 10^238 */ + U64(0xF5B5D7EC, 0x8ACB58A2), U64(0xAE10AF69, 0x6774B1DB), /* ~= 10^239 */ + U64(0x9991A6F3, 0xD6BF1765), U64(0xACCA6DA1, 0xE0A8EF29), /* ~= 10^240 */ + U64(0xBFF610B0, 0xCC6EDD3F), U64(0x17FD090A, 0x58D32AF3), /* ~= 10^241 */ + U64(0xEFF394DC, 0xFF8A948E), U64(0xDDFC4B4C, 0xEF07F5B0), /* ~= 10^242 */ + U64(0x95F83D0A, 0x1FB69CD9), U64(0x4ABDAF10, 0x1564F98E), /* ~= 10^243 */ + U64(0xBB764C4C, 0xA7A4440F), U64(0x9D6D1AD4, 0x1ABE37F1), /* ~= 10^244 */ + U64(0xEA53DF5F, 0xD18D5513), U64(0x84C86189, 0x216DC5ED), /* ~= 10^245 */ + U64(0x92746B9B, 0xE2F8552C), U64(0x32FD3CF5, 0xB4E49BB4), /* ~= 10^246 */ + U64(0xB7118682, 0xDBB66A77), U64(0x3FBC8C33, 0x221DC2A1), /* ~= 10^247 */ + U64(0xE4D5E823, 0x92A40515), U64(0x0FABAF3F, 0xEAA5334A), /* ~= 10^248 */ + U64(0x8F05B116, 0x3BA6832D), U64(0x29CB4D87, 0xF2A7400E), /* ~= 10^249 */ + U64(0xB2C71D5B, 0xCA9023F8), U64(0x743E20E9, 0xEF511012), /* ~= 10^250 */ + U64(0xDF78E4B2, 0xBD342CF6), U64(0x914DA924, 0x6B255416), /* ~= 10^251 */ + U64(0x8BAB8EEF, 0xB6409C1A), U64(0x1AD089B6, 0xC2F7548E), /* ~= 10^252 */ + U64(0xAE9672AB, 0xA3D0C320), U64(0xA184AC24, 0x73B529B1), /* ~= 10^253 */ + U64(0xDA3C0F56, 0x8CC4F3E8), U64(0xC9E5D72D, 0x90A2741E), /* ~= 10^254 */ + U64(0x88658996, 0x17FB1871), U64(0x7E2FA67C, 0x7A658892), /* ~= 10^255 */ + U64(0xAA7EEBFB, 0x9DF9DE8D), U64(0xDDBB901B, 0x98FEEAB7), /* ~= 10^256 */ + U64(0xD51EA6FA, 0x85785631), U64(0x552A7422, 0x7F3EA565), /* ~= 10^257 */ + U64(0x8533285C, 0x936B35DE), U64(0xD53A8895, 0x8F87275F), /* ~= 10^258 */ + U64(0xA67FF273, 0xB8460356), U64(0x8A892ABA, 0xF368F137), /* ~= 10^259 */ + U64(0xD01FEF10, 0xA657842C), U64(0x2D2B7569, 0xB0432D85), /* ~= 10^260 */ + U64(0x8213F56A, 0x67F6B29B), U64(0x9C3B2962, 0x0E29FC73), /* ~= 10^261 */ + U64(0xA298F2C5, 0x01F45F42), U64(0x8349F3BA, 0x91B47B8F), /* ~= 10^262 */ + U64(0xCB3F2F76, 0x42717713), U64(0x241C70A9, 0x36219A73), /* ~= 10^263 */ + U64(0xFE0EFB53, 0xD30DD4D7), U64(0xED238CD3, 0x83AA0110), /* ~= 10^264 */ + U64(0x9EC95D14, 0x63E8A506), U64(0xF4363804, 0x324A40AA), /* ~= 10^265 */ + U64(0xC67BB459, 0x7CE2CE48), U64(0xB143C605, 0x3EDCD0D5), /* ~= 10^266 */ + U64(0xF81AA16F, 0xDC1B81DA), U64(0xDD94B786, 0x8E94050A), /* ~= 10^267 */ + U64(0x9B10A4E5, 0xE9913128), U64(0xCA7CF2B4, 0x191C8326), /* ~= 10^268 */ + U64(0xC1D4CE1F, 0x63F57D72), U64(0xFD1C2F61, 0x1F63A3F0), /* ~= 10^269 */ + U64(0xF24A01A7, 0x3CF2DCCF), U64(0xBC633B39, 0x673C8CEC), /* ~= 10^270 */ + U64(0x976E4108, 0x8617CA01), U64(0xD5BE0503, 0xE085D813), /* ~= 10^271 */ + U64(0xBD49D14A, 0xA79DBC82), U64(0x4B2D8644, 0xD8A74E18), /* ~= 10^272 */ + U64(0xEC9C459D, 0x51852BA2), U64(0xDDF8E7D6, 0x0ED1219E), /* ~= 10^273 */ + U64(0x93E1AB82, 0x52F33B45), U64(0xCABB90E5, 0xC942B503), /* ~= 10^274 */ + U64(0xB8DA1662, 0xE7B00A17), U64(0x3D6A751F, 0x3B936243), /* ~= 10^275 */ + U64(0xE7109BFB, 0xA19C0C9D), U64(0x0CC51267, 0x0A783AD4), /* ~= 10^276 */ + U64(0x906A617D, 0x450187E2), U64(0x27FB2B80, 0x668B24C5), /* ~= 10^277 */ + U64(0xB484F9DC, 0x9641E9DA), U64(0xB1F9F660, 0x802DEDF6), /* ~= 10^278 */ + U64(0xE1A63853, 0xBBD26451), U64(0x5E7873F8, 0xA0396973), /* ~= 10^279 */ + U64(0x8D07E334, 0x55637EB2), U64(0xDB0B487B, 0x6423E1E8), /* ~= 10^280 */ + U64(0xB049DC01, 0x6ABC5E5F), U64(0x91CE1A9A, 0x3D2CDA62), /* ~= 10^281 */ + U64(0xDC5C5301, 0xC56B75F7), U64(0x7641A140, 0xCC7810FB), /* ~= 10^282 */ + U64(0x89B9B3E1, 0x1B6329BA), U64(0xA9E904C8, 0x7FCB0A9D), /* ~= 10^283 */ + U64(0xAC2820D9, 0x623BF429), U64(0x546345FA, 0x9FBDCD44), /* ~= 10^284 */ + U64(0xD732290F, 0xBACAF133), U64(0xA97C1779, 0x47AD4095), /* ~= 10^285 */ + U64(0x867F59A9, 0xD4BED6C0), U64(0x49ED8EAB, 0xCCCC485D), /* ~= 10^286 */ + U64(0xA81F3014, 0x49EE8C70), U64(0x5C68F256, 0xBFFF5A74), /* ~= 10^287 */ + U64(0xD226FC19, 0x5C6A2F8C), U64(0x73832EEC, 0x6FFF3111), /* ~= 10^288 */ + U64(0x83585D8F, 0xD9C25DB7), U64(0xC831FD53, 0xC5FF7EAB), /* ~= 10^289 */ + U64(0xA42E74F3, 0xD032F525), U64(0xBA3E7CA8, 0xB77F5E55), /* ~= 10^290 */ + U64(0xCD3A1230, 0xC43FB26F), U64(0x28CE1BD2, 0xE55F35EB), /* ~= 10^291 */ + U64(0x80444B5E, 0x7AA7CF85), U64(0x7980D163, 0xCF5B81B3), /* ~= 10^292 */ + U64(0xA0555E36, 0x1951C366), U64(0xD7E105BC, 0xC332621F), /* ~= 10^293 */ + U64(0xC86AB5C3, 0x9FA63440), U64(0x8DD9472B, 0xF3FEFAA7), /* ~= 10^294 */ + U64(0xFA856334, 0x878FC150), U64(0xB14F98F6, 0xF0FEB951), /* ~= 10^295 */ + U64(0x9C935E00, 0xD4B9D8D2), U64(0x6ED1BF9A, 0x569F33D3), /* ~= 10^296 */ + U64(0xC3B83581, 0x09E84F07), U64(0x0A862F80, 0xEC4700C8), /* ~= 10^297 */ + U64(0xF4A642E1, 0x4C6262C8), U64(0xCD27BB61, 0x2758C0FA), /* ~= 10^298 */ + U64(0x98E7E9CC, 0xCFBD7DBD), U64(0x8038D51C, 0xB897789C), /* ~= 10^299 */ + U64(0xBF21E440, 0x03ACDD2C), U64(0xE0470A63, 0xE6BD56C3), /* ~= 10^300 */ + U64(0xEEEA5D50, 0x04981478), U64(0x1858CCFC, 0xE06CAC74), /* ~= 10^301 */ + U64(0x95527A52, 0x02DF0CCB), U64(0x0F37801E, 0x0C43EBC8), /* ~= 10^302 */ + U64(0xBAA718E6, 0x8396CFFD), U64(0xD3056025, 0x8F54E6BA), /* ~= 10^303 */ + U64(0xE950DF20, 0x247C83FD), U64(0x47C6B82E, 0xF32A2069), /* ~= 10^304 */ + U64(0x91D28B74, 0x16CDD27E), U64(0x4CDC331D, 0x57FA5441), /* ~= 10^305 */ + U64(0xB6472E51, 0x1C81471D), U64(0xE0133FE4, 0xADF8E952), /* ~= 10^306 */ + U64(0xE3D8F9E5, 0x63A198E5), U64(0x58180FDD, 0xD97723A6), /* ~= 10^307 */ + U64(0x8E679C2F, 0x5E44FF8F), U64(0x570F09EA, 0xA7EA7648), /* ~= 10^308 */ + U64(0xB201833B, 0x35D63F73), U64(0x2CD2CC65, 0x51E513DA), /* ~= 10^309 */ + U64(0xDE81E40A, 0x034BCF4F), U64(0xF8077F7E, 0xA65E58D1), /* ~= 10^310 */ + U64(0x8B112E86, 0x420F6191), U64(0xFB04AFAF, 0x27FAF782), /* ~= 10^311 */ + U64(0xADD57A27, 0xD29339F6), U64(0x79C5DB9A, 0xF1F9B563), /* ~= 10^312 */ + U64(0xD94AD8B1, 0xC7380874), U64(0x18375281, 0xAE7822BC), /* ~= 10^313 */ + U64(0x87CEC76F, 0x1C830548), U64(0x8F229391, 0x0D0B15B5), /* ~= 10^314 */ + U64(0xA9C2794A, 0xE3A3C69A), U64(0xB2EB3875, 0x504DDB22), /* ~= 10^315 */ + U64(0xD433179D, 0x9C8CB841), U64(0x5FA60692, 0xA46151EB), /* ~= 10^316 */ + U64(0x849FEEC2, 0x81D7F328), U64(0xDBC7C41B, 0xA6BCD333), /* ~= 10^317 */ + U64(0xA5C7EA73, 0x224DEFF3), U64(0x12B9B522, 0x906C0800), /* ~= 10^318 */ + U64(0xCF39E50F, 0xEAE16BEF), U64(0xD768226B, 0x34870A00), /* ~= 10^319 */ + U64(0x81842F29, 0xF2CCE375), U64(0xE6A11583, 0x00D46640), /* ~= 10^320 */ + U64(0xA1E53AF4, 0x6F801C53), U64(0x60495AE3, 0xC1097FD0), /* ~= 10^321 */ + U64(0xCA5E89B1, 0x8B602368), U64(0x385BB19C, 0xB14BDFC4), /* ~= 10^322 */ + U64(0xFCF62C1D, 0xEE382C42), U64(0x46729E03, 0xDD9ED7B5), /* ~= 10^323 */ + U64(0x9E19DB92, 0xB4E31BA9), U64(0x6C07A2C2, 0x6A8346D1) /* ~= 10^324 */ +}; + +/** + Get the cached pow10 value from `pow10_sig_table`. + @param exp10 The exponent of pow(10, e). This value must in range + `POW10_SIG_TABLE_MIN_EXP` to `POW10_SIG_TABLE_MAX_EXP`. + @param hi The highest 64 bits of pow(10, e). + @param lo The lower 64 bits after `hi`. + */ +static_inline void pow10_table_get_sig(i32 exp10, u64 *hi, u64 *lo) { + i32 idx = exp10 - (POW10_SIG_TABLE_MIN_EXP); + *hi = pow10_sig_table[idx * 2]; + *lo = pow10_sig_table[idx * 2 + 1]; +} + +/** + Get the exponent (base 2) for highest 64 bits significand in `pow10_sig_table`. + */ +static_inline void pow10_table_get_exp(i32 exp10, i32 *exp2) { + /* e2 = floor(log2(pow(10, e))) - 64 + 1 */ + /* = floor(e * log2(10) - 63) */ + *exp2 = (exp10 * 217706 - 4128768) >> 16; +} + +#endif + + + +/*============================================================================== + * MARK: - Number and Bit Utils (Private) + *============================================================================*/ + +/** Convert bits to double. */ +static_inline f64 f64_from_bits(u64 u) { + f64 f; + memcpy(&f, &u, sizeof(u)); + return f; +} + +/** Convert double to bits. */ +static_inline u64 f64_to_bits(f64 f) { + u64 u; + memcpy(&u, &f, sizeof(u)); + return u; +} + +/** Convert double to bits. */ +static_inline u32 f32_to_bits(f32 f) { + u32 u; + memcpy(&u, &f, sizeof(u)); + return u; +} + +/** Get 'infinity' bits with sign. */ +static_inline u64 f64_bits_inf(bool sign) { +#if YYJSON_HAS_IEEE_754 + return F64_BITS_INF | ((u64)sign << 63); +#elif defined(INFINITY) + return f64_to_bits(sign ? -INFINITY : INFINITY); +#else + return f64_to_bits(sign ? -HUGE_VAL : HUGE_VAL); +#endif +} + +/** Get 'nan' bits with sign. */ +static_inline u64 f64_bits_nan(bool sign) { +#if YYJSON_HAS_IEEE_754 + return F64_BITS_NAN | ((u64)sign << 63); +#elif defined(NAN) + return f64_to_bits(sign ? (f64)-NAN : (f64)NAN); +#else + return f64_to_bits((sign ? -0.0 : 0.0) / 0.0); +#endif +} + +/** Casting double to float, allow overflow. */ +#if yyjson_has_attribute(no_sanitize) +__attribute__((no_sanitize("undefined"))) +#elif yyjson_gcc_available(4, 9, 0) +__attribute__((__no_sanitize_undefined__)) +#endif +static_inline f32 f64_to_f32(f64 val) { + return (f32)val; +} + +/** Returns the number of leading 0-bits in value (input should not be 0). */ +static_inline u32 u64_lz_bits(u64 v) { +#if GCC_HAS_CLZLL + return (u32)__builtin_clzll(v); +#elif MSC_HAS_BIT_SCAN_64 + unsigned long r; + _BitScanReverse64(&r, v); + return (u32)63 - (u32)r; +#elif MSC_HAS_BIT_SCAN + unsigned long hi, lo; + bool hi_set = _BitScanReverse(&hi, (u32)(v >> 32)) != 0; + _BitScanReverse(&lo, (u32)v); + hi |= 32; + return (u32)63 - (u32)(hi_set ? hi : lo); +#else + /* branchless, use De Bruijn sequence */ + /* see: https://www.chessprogramming.org/BitScan */ + const u8 table[64] = { + 63, 16, 62, 7, 15, 36, 61, 3, 6, 14, 22, 26, 35, 47, 60, 2, + 9, 5, 28, 11, 13, 21, 42, 19, 25, 31, 34, 40, 46, 52, 59, 1, + 17, 8, 37, 4, 23, 27, 48, 10, 29, 12, 43, 20, 32, 41, 53, 18, + 38, 24, 49, 30, 44, 33, 54, 39, 50, 45, 55, 51, 56, 57, 58, 0 + }; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v |= v >> 32; + return table[(v * U64(0x03F79D71, 0xB4CB0A89)) >> 58]; +#endif +} + +/** Returns the number of trailing 0-bits in value (input should not be 0). */ +static_inline u32 u64_tz_bits(u64 v) { +#if GCC_HAS_CTZLL + return (u32)__builtin_ctzll(v); +#elif MSC_HAS_BIT_SCAN_64 + unsigned long r; + _BitScanForward64(&r, v); + return (u32)r; +#elif MSC_HAS_BIT_SCAN + unsigned long lo, hi; + bool lo_set = _BitScanForward(&lo, (u32)(v)) != 0; + _BitScanForward(&hi, (u32)(v >> 32)); + hi += 32; + return lo_set ? lo : hi; +#else + /* branchless, use De Bruijn sequence */ + /* see: https://www.chessprogramming.org/BitScan */ + const u8 table[64] = { + 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, + 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, + 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, + 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12 + }; + return table[((v & (~v + 1)) * U64(0x022FDD63, 0xCC95386D)) >> 58]; +#endif +} + +/** Multiplies two 64-bit unsigned integers (a * b), + returns the 128-bit result as 'hi' and 'lo'. */ +static_inline void u128_mul(u64 a, u64 b, u64 *hi, u64 *lo) { +#if YYJSON_HAS_INT128 + u128 m = (u128)a * b; + *hi = (u64)(m >> 64); + *lo = (u64)(m); +#elif MSC_HAS_UMUL128 + *lo = _umul128(a, b, hi); +#else + u32 a0 = (u32)(a), a1 = (u32)(a >> 32); + u32 b0 = (u32)(b), b1 = (u32)(b >> 32); + u64 p00 = (u64)a0 * b0, p01 = (u64)a0 * b1; + u64 p10 = (u64)a1 * b0, p11 = (u64)a1 * b1; + u64 m0 = p01 + (p00 >> 32); + u32 m00 = (u32)(m0), m01 = (u32)(m0 >> 32); + u64 m1 = p10 + m00; + u32 m10 = (u32)(m1), m11 = (u32)(m1 >> 32); + *hi = p11 + m01 + m11; + *lo = ((u64)m10 << 32) | (u32)p00; +#endif +} + +/** Multiplies two 64-bit unsigned integers and add a value (a * b + c), + returns the 128-bit result as 'hi' and 'lo'. */ +static_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) { +#if YYJSON_HAS_INT128 + u128 m = (u128)a * b + c; + *hi = (u64)(m >> 64); + *lo = (u64)(m); +#else + u64 h, l, t; + u128_mul(a, b, &h, &l); + t = l + c; + h += (u64)(((t < l) | (t < c))); + *hi = h; + *lo = t; +#endif +} + + + +/*============================================================================== + * MARK: - File Utils (Private) + * These functions are used to read and write JSON files. + *============================================================================*/ + +#define YYJSON_FOPEN_E +#if !defined(_MSC_VER) && defined(__GLIBC__) && defined(__GLIBC_PREREQ) +# if __GLIBC_PREREQ(2, 7) +# undef YYJSON_FOPEN_E +# define YYJSON_FOPEN_E "e" /* glibc extension to enable O_CLOEXEC */ +# endif +#endif + +static_inline FILE *fopen_safe(const char *path, const char *mode) { +#if YYJSON_MSC_VER >= 1400 + FILE *file = NULL; + if (fopen_s(&file, path, mode) != 0) return NULL; + return file; +#else + return fopen(path, mode); +#endif +} + +static_inline FILE *fopen_readonly(const char *path) { + return fopen_safe(path, "rb" YYJSON_FOPEN_E); +} + +static_inline FILE *fopen_writeonly(const char *path) { + return fopen_safe(path, "wb" YYJSON_FOPEN_E); +} + +static_inline usize fread_safe(void *buf, usize size, FILE *file) { +#if YYJSON_MSC_VER >= 1400 + return fread_s(buf, size, 1, size, file); +#else + return fread(buf, 1, size, file); +#endif +} + + + +/*============================================================================== + * MARK: - Size Utils (Private) + * These functions are used for memory allocation. + *============================================================================*/ + +/** Returns whether the size is overflow after increment. */ +static_inline bool size_add_is_overflow(usize size, usize add) { + return size > (size + add); +} + +/** Returns whether the size is power of 2 (size should not be 0). */ +static_inline bool size_is_pow2(usize size) { + return (size & (size - 1)) == 0; +} + +/** Align size upwards (may overflow). */ +static_inline usize size_align_up(usize size, usize align) { + if (size_is_pow2(align)) { + return (size + (align - 1)) & ~(align - 1); + } else { + return size + align - (size + align - 1) % align - 1; + } +} + +/** Align size downwards. */ +static_inline usize size_align_down(usize size, usize align) { + if (size_is_pow2(align)) { + return size & ~(align - 1); + } else { + return size - (size % align); + } +} + +/** Align address upwards (may overflow). */ +static_inline void *mem_align_up(void *mem, usize align) { + usize size; + memcpy(&size, &mem, sizeof(usize)); + size = size_align_up(size, align); + memcpy(&mem, &size, sizeof(usize)); + return mem; +} + + + +/*============================================================================== + * MARK: - Default Memory Allocator (Private) + * This is a simple libc memory allocator wrapper. + *============================================================================*/ + +static void *default_malloc(void *ctx, usize size) { + return malloc(size); +} + +static void *default_realloc(void *ctx, void *ptr, usize old_size, usize size) { + return realloc(ptr, size); +} + +static void default_free(void *ctx, void *ptr) { + free(ptr); +} + +static const yyjson_alc YYJSON_DEFAULT_ALC = { + default_malloc, default_realloc, default_free, NULL +}; + + + +/*============================================================================== + * MARK: - Null Memory Allocator (Private) + * This allocator is just a placeholder to ensure that the internal + * malloc/realloc/free function pointers are not null. + *============================================================================*/ + +static void *null_malloc(void *ctx, usize size) { + return NULL; +} + +static void *null_realloc(void *ctx, void *ptr, usize old_size, usize size) { + return NULL; +} + +static void null_free(void *ctx, void *ptr) { + return; +} + +static const yyjson_alc YYJSON_NULL_ALC = { + null_malloc, null_realloc, null_free, NULL +}; + + + +/*============================================================================== + * MARK: - Pool Memory Allocator (Public) + * This allocator is initialized with a fixed-size buffer. + * The buffer is split into multiple memory chunks for memory allocation. + *============================================================================*/ + +/** memory chunk header */ +typedef struct pool_chunk { + usize size; /* chunk memory size, include chunk header */ + struct pool_chunk *next; /* linked list, nullable */ + /* char mem[]; flexible array member */ +} pool_chunk; + +/** allocator ctx header */ +typedef struct pool_ctx { + usize size; /* total memory size, include ctx header */ + pool_chunk *free_list; /* linked list, nullable */ + /* pool_chunk chunks[]; flexible array member */ +} pool_ctx; + +/** align up the input size to chunk size */ +static_inline void pool_size_align(usize *size) { + *size = size_align_up(*size, sizeof(pool_chunk)) + sizeof(pool_chunk); +} + +static void *pool_malloc(void *ctx_ptr, usize size) { + /* assert(size != 0) */ + pool_ctx *ctx = (pool_ctx *)ctx_ptr; + pool_chunk *next, *prev = NULL, *cur = ctx->free_list; + + if (unlikely(size >= ctx->size)) return NULL; + pool_size_align(&size); + + while (cur) { + if (cur->size < size) { + /* not enough space, try next chunk */ + prev = cur; + cur = cur->next; + continue; + } + if (cur->size >= size + sizeof(pool_chunk) * 2) { + /* too much space, split this chunk */ + next = (pool_chunk *)(void *)((u8 *)cur + size); + next->size = cur->size - size; + next->next = cur->next; + cur->size = size; + } else { + /* just enough space, use whole chunk */ + next = cur->next; + } + if (prev) prev->next = next; + else ctx->free_list = next; + return (void *)(cur + 1); + } + return NULL; +} + +static void pool_free(void *ctx_ptr, void *ptr) { + /* assert(ptr != NULL) */ + pool_ctx *ctx = (pool_ctx *)ctx_ptr; + pool_chunk *cur = ((pool_chunk *)ptr) - 1; + pool_chunk *prev = NULL, *next = ctx->free_list; + + while (next && next < cur) { + prev = next; + next = next->next; + } + if (prev) prev->next = cur; + else ctx->free_list = cur; + cur->next = next; + + if (next && ((u8 *)cur + cur->size) == (u8 *)next) { + /* merge cur to higher chunk */ + cur->size += next->size; + cur->next = next->next; + } + if (prev && ((u8 *)prev + prev->size) == (u8 *)cur) { + /* merge cur to lower chunk */ + prev->size += cur->size; + prev->next = cur->next; + } +} + +static void *pool_realloc(void *ctx_ptr, void *ptr, + usize old_size, usize size) { + /* assert(ptr != NULL && size != 0 && old_size < size) */ + pool_ctx *ctx = (pool_ctx *)ctx_ptr; + pool_chunk *cur = ((pool_chunk *)ptr) - 1, *prev, *next, *tmp; + + /* check size */ + if (unlikely(size >= ctx->size)) return NULL; + pool_size_align(&old_size); + pool_size_align(&size); + if (unlikely(old_size == size)) return ptr; + + /* find next and prev chunk */ + prev = NULL; + next = ctx->free_list; + while (next && next < cur) { + prev = next; + next = next->next; + } + + if ((u8 *)cur + cur->size == (u8 *)next && cur->size + next->size >= size) { + /* merge to higher chunk if they are contiguous */ + usize free_size = cur->size + next->size - size; + if (free_size > sizeof(pool_chunk) * 2) { + tmp = (pool_chunk *)(void *)((u8 *)cur + size); + if (prev) prev->next = tmp; + else ctx->free_list = tmp; + tmp->next = next->next; + tmp->size = free_size; + cur->size = size; + } else { + if (prev) prev->next = next->next; + else ctx->free_list = next->next; + cur->size += next->size; + } + return ptr; + } else { + /* fallback to malloc and memcpy */ + void *new_ptr = pool_malloc(ctx_ptr, size - sizeof(pool_chunk)); + if (new_ptr) { + memcpy(new_ptr, ptr, cur->size - sizeof(pool_chunk)); + pool_free(ctx_ptr, ptr); + } + return new_ptr; + } +} + +bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, usize size) { + pool_chunk *chunk; + pool_ctx *ctx; + + if (unlikely(!alc)) return false; + *alc = YYJSON_NULL_ALC; + if (size < sizeof(pool_ctx) * 4) return false; + ctx = (pool_ctx *)mem_align_up(buf, sizeof(pool_ctx)); + if (unlikely(!ctx)) return false; + size -= (usize)((u8 *)ctx - (u8 *)buf); + size = size_align_down(size, sizeof(pool_ctx)); + + chunk = (pool_chunk *)(ctx + 1); + chunk->size = size - sizeof(pool_ctx); + chunk->next = NULL; + ctx->size = size; + ctx->free_list = chunk; + + alc->malloc = pool_malloc; + alc->realloc = pool_realloc; + alc->free = pool_free; + alc->ctx = (void *)ctx; + return true; +} + + + +/*============================================================================== + * MARK: - Dynamic Memory Allocator (Public) + * This allocator allocates memory on demand and does not immediately release + * unused memory. Instead, it places the unused memory into a freelist for + * potential reuse in the future. It is only when the entire allocator is + * destroyed that all previously allocated memory is released at once. + *============================================================================*/ + +/** memory chunk header */ +typedef struct dyn_chunk { + usize size; /* chunk size, include header */ + struct dyn_chunk *next; + /* char mem[]; flexible array member */ +} dyn_chunk; + +/** allocator ctx header */ +typedef struct { + dyn_chunk free_list; /* dummy header, sorted from small to large */ + dyn_chunk used_list; /* dummy header */ +} dyn_ctx; + +/** align up the input size to chunk size */ +static_inline bool dyn_size_align(usize *size) { + usize alc_size = *size + sizeof(dyn_chunk); + alc_size = size_align_up(alc_size, YYJSON_ALC_DYN_MIN_SIZE); + if (unlikely(alc_size < *size)) return false; /* overflow */ + *size = alc_size; + return true; +} + +/** remove a chunk from list (the chunk must already be in the list) */ +static_inline void dyn_chunk_list_remove(dyn_chunk *list, dyn_chunk *chunk) { + dyn_chunk *prev = list, *cur; + for (cur = prev->next; cur; cur = cur->next) { + if (cur == chunk) { + prev->next = cur->next; + cur->next = NULL; + return; + } + prev = cur; + } +} + +/** add a chunk to list header (the chunk must not be in the list) */ +static_inline void dyn_chunk_list_add(dyn_chunk *list, dyn_chunk *chunk) { + chunk->next = list->next; + list->next = chunk; +} + +static void *dyn_malloc(void *ctx_ptr, usize size) { + /* assert(size != 0) */ + const yyjson_alc def = YYJSON_DEFAULT_ALC; + dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; + dyn_chunk *chunk, *prev; + if (unlikely(!dyn_size_align(&size))) return NULL; + + /* freelist is empty, create new chunk */ + if (!ctx->free_list.next) { + chunk = (dyn_chunk *)def.malloc(def.ctx, size); + if (unlikely(!chunk)) return NULL; + chunk->size = size; + chunk->next = NULL; + dyn_chunk_list_add(&ctx->used_list, chunk); + return (void *)(chunk + 1); + } + + /* find a large enough chunk, or resize the largest chunk */ + prev = &ctx->free_list; + while (true) { + chunk = prev->next; + if (chunk->size >= size) { /* enough size, reuse this chunk */ + prev->next = chunk->next; + dyn_chunk_list_add(&ctx->used_list, chunk); + return (void *)(chunk + 1); + } + if (!chunk->next) { /* resize the largest chunk */ + chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); + if (unlikely(!chunk)) return NULL; + prev->next = NULL; + chunk->size = size; + dyn_chunk_list_add(&ctx->used_list, chunk); + return (void *)(chunk + 1); + } + prev = chunk; + } +} + +static void *dyn_realloc(void *ctx_ptr, void *ptr, + usize old_size, usize size) { + /* assert(ptr != NULL && size != 0 && old_size < size) */ + const yyjson_alc def = YYJSON_DEFAULT_ALC; + dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; + dyn_chunk *new_chunk, *chunk = (dyn_chunk *)ptr - 1; + if (unlikely(!dyn_size_align(&size))) return NULL; + if (chunk->size >= size) return ptr; + + dyn_chunk_list_remove(&ctx->used_list, chunk); + new_chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); + if (likely(new_chunk)) { + new_chunk->size = size; + chunk = new_chunk; + } + dyn_chunk_list_add(&ctx->used_list, chunk); + return new_chunk ? (void *)(new_chunk + 1) : NULL; +} + +static void dyn_free(void *ctx_ptr, void *ptr) { + /* assert(ptr != NULL) */ + dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; + dyn_chunk *chunk = (dyn_chunk *)ptr - 1, *prev; + + dyn_chunk_list_remove(&ctx->used_list, chunk); + for (prev = &ctx->free_list; prev; prev = prev->next) { + if (!prev->next || prev->next->size >= chunk->size) { + chunk->next = prev->next; + prev->next = chunk; + break; + } + } +} + +yyjson_alc *yyjson_alc_dyn_new(void) { + const yyjson_alc def = YYJSON_DEFAULT_ALC; + usize hdr_len = sizeof(yyjson_alc) + sizeof(dyn_ctx); + yyjson_alc *alc = (yyjson_alc *)def.malloc(def.ctx, hdr_len); + dyn_ctx *ctx = (dyn_ctx *)(void *)(alc + 1); + if (unlikely(!alc)) return NULL; + alc->malloc = dyn_malloc; + alc->realloc = dyn_realloc; + alc->free = dyn_free; + alc->ctx = alc + 1; + memset(ctx, 0, sizeof(*ctx)); + return alc; +} + +void yyjson_alc_dyn_free(yyjson_alc *alc) { + const yyjson_alc def = YYJSON_DEFAULT_ALC; + dyn_ctx *ctx = (dyn_ctx *)(void *)(alc + 1); + dyn_chunk *chunk, *next; + if (unlikely(!alc)) return; + for (chunk = ctx->free_list.next; chunk; chunk = next) { + next = chunk->next; + def.free(def.ctx, chunk); + } + for (chunk = ctx->used_list.next; chunk; chunk = next) { + next = chunk->next; + def.free(def.ctx, chunk); + } + def.free(def.ctx, alc); +} + + + +/*============================================================================== + * MARK: - JSON Struct Utils (Public) + * These functions are used for creating, copying, releasing, and comparing + * JSON documents and values. They are widely used throughout this library. + *============================================================================*/ + +static_inline void unsafe_yyjson_str_pool_release(yyjson_str_pool *pool, + yyjson_alc *alc) { + yyjson_str_chunk *chunk = pool->chunks, *next; + while (chunk) { + next = chunk->next; + alc->free(alc->ctx, chunk); + chunk = next; + } +} + +static_inline void unsafe_yyjson_val_pool_release(yyjson_val_pool *pool, + yyjson_alc *alc) { + yyjson_val_chunk *chunk = pool->chunks, *next; + while (chunk) { + next = chunk->next; + alc->free(alc->ctx, chunk); + chunk = next; + } +} + +bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool, + const yyjson_alc *alc, usize len) { + yyjson_str_chunk *chunk; + usize size, max_len; + + /* create a new chunk */ + max_len = USIZE_MAX - sizeof(yyjson_str_chunk); + if (unlikely(len > max_len)) return false; + size = len + sizeof(yyjson_str_chunk); + size = yyjson_max(pool->chunk_size, size); + chunk = (yyjson_str_chunk *)alc->malloc(alc->ctx, size); + if (unlikely(!chunk)) return false; + + /* insert the new chunk as the head of the linked list */ + chunk->next = pool->chunks; + chunk->chunk_size = size; + pool->chunks = chunk; + pool->cur = (char *)chunk + sizeof(yyjson_str_chunk); + pool->end = (char *)chunk + size; + + /* the next chunk is twice the size of the current one */ + size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max); + if (size < pool->chunk_size) size = pool->chunk_size_max; /* overflow */ + pool->chunk_size = size; + return true; +} + +bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool, + const yyjson_alc *alc, usize count) { + yyjson_val_chunk *chunk; + usize size, max_count; + + /* create a new chunk */ + max_count = USIZE_MAX / sizeof(yyjson_mut_val) - 1; + if (unlikely(count > max_count)) return false; + size = (count + 1) * sizeof(yyjson_mut_val); + size = yyjson_max(pool->chunk_size, size); + chunk = (yyjson_val_chunk *)alc->malloc(alc->ctx, size); + if (unlikely(!chunk)) return false; + + /* insert the new chunk as the head of the linked list */ + chunk->next = pool->chunks; + chunk->chunk_size = size; + pool->chunks = chunk; + pool->cur = (yyjson_mut_val *)(void *)((u8 *)chunk) + 1; + pool->end = (yyjson_mut_val *)(void *)((u8 *)chunk + size); + + /* the next chunk is twice the size of the current one */ + size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max); + if (size < pool->chunk_size) size = pool->chunk_size_max; /* overflow */ + pool->chunk_size = size; + return true; +} + +bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, size_t len) { + usize max_size = USIZE_MAX - sizeof(yyjson_str_chunk); + if (!doc || !len || len > max_size) return false; + doc->str_pool.chunk_size = len + sizeof(yyjson_str_chunk); + return true; +} + +bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, size_t count) { + usize max_count = USIZE_MAX / sizeof(yyjson_mut_val) - 1; + if (!doc || !count || count > max_count) return false; + doc->val_pool.chunk_size = (count + 1) * sizeof(yyjson_mut_val); + return true; +} + +void yyjson_mut_doc_free(yyjson_mut_doc *doc) { + if (doc) { + yyjson_alc alc = doc->alc; + memset(&doc->alc, 0, sizeof(alc)); + unsafe_yyjson_str_pool_release(&doc->str_pool, &alc); + unsafe_yyjson_val_pool_release(&doc->val_pool, &alc); + alc.free(alc.ctx, doc); + } +} + +yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc) { + yyjson_mut_doc *doc; + if (!alc) alc = &YYJSON_DEFAULT_ALC; + doc = (yyjson_mut_doc *)alc->malloc(alc->ctx, sizeof(yyjson_mut_doc)); + if (!doc) return NULL; + memset(doc, 0, sizeof(yyjson_mut_doc)); + + doc->alc = *alc; + doc->str_pool.chunk_size = YYJSON_MUT_DOC_STR_POOL_INIT_SIZE; + doc->str_pool.chunk_size_max = YYJSON_MUT_DOC_STR_POOL_MAX_SIZE; + doc->val_pool.chunk_size = YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE; + doc->val_pool.chunk_size_max = YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE; + return doc; +} + +yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, const yyjson_alc *alc) { + yyjson_mut_doc *m_doc; + yyjson_mut_val *m_val; + + if (!doc || !doc->root) return NULL; + m_doc = yyjson_mut_doc_new(alc); + if (!m_doc) return NULL; + m_val = yyjson_val_mut_copy(m_doc, doc->root); + if (!m_val) { + yyjson_mut_doc_free(m_doc); + return NULL; + } + yyjson_mut_doc_set_root(m_doc, m_val); + return m_doc; +} + +yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc, + const yyjson_alc *alc) { + yyjson_mut_doc *m_doc; + yyjson_mut_val *m_val; + + if (!doc) return NULL; + if (!doc->root) return yyjson_mut_doc_new(alc); + + m_doc = yyjson_mut_doc_new(alc); + if (!m_doc) return NULL; + m_val = yyjson_mut_val_mut_copy(m_doc, doc->root); + if (!m_val) { + yyjson_mut_doc_free(m_doc); + return NULL; + } + yyjson_mut_doc_set_root(m_doc, m_val); + return m_doc; +} + +yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *m_doc, + yyjson_val *i_vals) { + /* + The immutable object or array stores all sub-values in a contiguous memory, + We copy them to another contiguous memory as mutable values, + then reconnect the mutable values with the original relationship. + */ + usize i_vals_len; + yyjson_mut_val *m_vals, *m_val; + yyjson_val *i_val, *i_end; + + if (!m_doc || !i_vals) return NULL; + i_end = unsafe_yyjson_get_next(i_vals); + i_vals_len = (usize)(unsafe_yyjson_get_next(i_vals) - i_vals); + m_vals = unsafe_yyjson_mut_val(m_doc, i_vals_len); + if (!m_vals) return NULL; + i_val = i_vals; + m_val = m_vals; + + for (; i_val < i_end; i_val++, m_val++) { + yyjson_type type = unsafe_yyjson_get_type(i_val); + m_val->tag = i_val->tag; + m_val->uni.u64 = i_val->uni.u64; + if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { + const char *str = i_val->uni.str; + usize str_len = unsafe_yyjson_get_len(i_val); + m_val->uni.str = unsafe_yyjson_mut_strncpy(m_doc, str, str_len); + if (!m_val->uni.str) return NULL; + } else if (type == YYJSON_TYPE_ARR) { + usize len = unsafe_yyjson_get_len(i_val); + if (len > 0) { + yyjson_val *ii_val = i_val + 1, *ii_next; + yyjson_mut_val *mm_val = m_val + 1, *mm_ctn = m_val, *mm_next; + while (len-- > 1) { + ii_next = unsafe_yyjson_get_next(ii_val); + mm_next = mm_val + (ii_next - ii_val); + mm_val->next = mm_next; + ii_val = ii_next; + mm_val = mm_next; + } + mm_val->next = mm_ctn + 1; + mm_ctn->uni.ptr = mm_val; + } + } else if (type == YYJSON_TYPE_OBJ) { + usize len = unsafe_yyjson_get_len(i_val); + if (len > 0) { + yyjson_val *ii_key = i_val + 1, *ii_nextkey; + yyjson_mut_val *mm_key = m_val + 1, *mm_ctn = m_val; + yyjson_mut_val *mm_nextkey; + while (len-- > 1) { + ii_nextkey = unsafe_yyjson_get_next(ii_key + 1); + mm_nextkey = mm_key + (ii_nextkey - ii_key); + mm_key->next = mm_key + 1; + mm_key->next->next = mm_nextkey; + ii_key = ii_nextkey; + mm_key = mm_nextkey; + } + mm_key->next = mm_key + 1; + mm_key->next->next = mm_ctn + 1; + mm_ctn->uni.ptr = mm_key; + } + } + } + return m_vals; +} + +static yyjson_mut_val *unsafe_yyjson_mut_val_mut_copy(yyjson_mut_doc *m_doc, + yyjson_mut_val *m_vals) { + /* + The mutable object or array stores all sub-values in a circular linked + list, so we can traverse them in the same loop. The traversal starts from + the last item, continues with the first item in a list, and ends with the + second to last item, which needs to be linked to the last item to close the + circle. + */ + yyjson_mut_val *m_val = unsafe_yyjson_mut_val(m_doc, 1); + if (unlikely(!m_val)) return NULL; + m_val->tag = m_vals->tag; + + switch (unsafe_yyjson_get_type(m_vals)) { + case YYJSON_TYPE_OBJ: + case YYJSON_TYPE_ARR: + if (unsafe_yyjson_get_len(m_vals) > 0) { + yyjson_mut_val *last = (yyjson_mut_val *)m_vals->uni.ptr; + yyjson_mut_val *next = last->next, *prev; + prev = unsafe_yyjson_mut_val_mut_copy(m_doc, last); + if (!prev) return NULL; + m_val->uni.ptr = (void *)prev; + while (next != last) { + prev->next = unsafe_yyjson_mut_val_mut_copy(m_doc, next); + if (!prev->next) return NULL; + prev = prev->next; + next = next->next; + } + prev->next = (yyjson_mut_val *)m_val->uni.ptr; + } + break; + case YYJSON_TYPE_RAW: + case YYJSON_TYPE_STR: { + const char *str = m_vals->uni.str; + usize str_len = unsafe_yyjson_get_len(m_vals); + m_val->uni.str = unsafe_yyjson_mut_strncpy(m_doc, str, str_len); + if (!m_val->uni.str) return NULL; + break; + } + default: + m_val->uni = m_vals->uni; + break; + } + return m_val; +} + +yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, + yyjson_mut_val *val) { + if (doc && val) return unsafe_yyjson_mut_val_mut_copy(doc, val); + return NULL; +} + +/* Count the number of values and the total length of the strings. */ +static void yyjson_mut_stat(yyjson_mut_val *val, + usize *val_sum, usize *str_sum) { + yyjson_type type = unsafe_yyjson_get_type(val); + *val_sum += 1; + if (type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ) { + yyjson_mut_val *child = (yyjson_mut_val *)val->uni.ptr; + usize len = unsafe_yyjson_get_len(val), i; + len <<= (u8)(type == YYJSON_TYPE_OBJ); + *val_sum += len; + for (i = 0; i < len; i++) { + yyjson_type stype = unsafe_yyjson_get_type(child); + if (stype == YYJSON_TYPE_STR || stype == YYJSON_TYPE_RAW) { + *str_sum += unsafe_yyjson_get_len(child) + 1; + } else if (stype == YYJSON_TYPE_ARR || stype == YYJSON_TYPE_OBJ) { + yyjson_mut_stat(child, val_sum, str_sum); + *val_sum -= 1; + } + child = child->next; + } + } else if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { + *str_sum += unsafe_yyjson_get_len(val) + 1; + } +} + +/* Copy mutable values to immutable value pool. */ +static usize yyjson_imut_copy(yyjson_val **val_ptr, char **buf_ptr, + yyjson_mut_val *mval) { + yyjson_val *val = *val_ptr; + yyjson_type type = unsafe_yyjson_get_type(mval); + if (type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ) { + yyjson_mut_val *child = (yyjson_mut_val *)mval->uni.ptr; + usize len = unsafe_yyjson_get_len(mval), i; + usize val_sum = 1; + if (type == YYJSON_TYPE_OBJ) { + if (len) child = child->next->next; + len <<= 1; + } else { + if (len) child = child->next; + } + *val_ptr = val + 1; + for (i = 0; i < len; i++) { + val_sum += yyjson_imut_copy(val_ptr, buf_ptr, child); + child = child->next; + } + val->tag = mval->tag; + val->uni.ofs = val_sum * sizeof(yyjson_val); + return val_sum; + } else if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { + char *buf = *buf_ptr; + usize len = unsafe_yyjson_get_len(mval); + memcpy((void *)buf, (const void *)mval->uni.str, len); + buf[len] = '\0'; + val->tag = mval->tag; + val->uni.str = buf; + *val_ptr = val + 1; + *buf_ptr = buf + len + 1; + return 1; + } else { + val->tag = mval->tag; + val->uni = mval->uni; + *val_ptr = val + 1; + return 1; + } +} + +yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *mdoc, + const yyjson_alc *alc) { + if (!mdoc) return NULL; + return yyjson_mut_val_imut_copy(mdoc->root, alc); +} + +yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *mval, + const yyjson_alc *alc) { + usize val_num = 0, str_sum = 0, hdr_size, buf_size; + yyjson_doc *doc = NULL; + yyjson_val *val_hdr = NULL; + + /* This value should be NULL here. Setting a non-null value suppresses + warning from the clang analyzer. */ + char *str_hdr = (char *)(void *)&str_sum; + if (!mval) return NULL; + if (!alc) alc = &YYJSON_DEFAULT_ALC; + + /* traverse the input value to get pool size */ + yyjson_mut_stat(mval, &val_num, &str_sum); + + /* create doc and val pool */ + hdr_size = size_align_up(sizeof(yyjson_doc), sizeof(yyjson_val)); + buf_size = hdr_size + val_num * sizeof(yyjson_val); + doc = (yyjson_doc *)alc->malloc(alc->ctx, buf_size); + if (!doc) return NULL; + memset(doc, 0, sizeof(yyjson_doc)); + val_hdr = (yyjson_val *)(void *)((char *)(void *)doc + hdr_size); + doc->root = val_hdr; + doc->alc = *alc; + + /* create str pool */ + if (str_sum > 0) { + str_hdr = (char *)alc->malloc(alc->ctx, str_sum); + doc->str_pool = str_hdr; + if (!str_hdr) { + alc->free(alc->ctx, (void *)doc); + return NULL; + } + } + + /* copy vals and strs */ + doc->val_read = yyjson_imut_copy(&val_hdr, &str_hdr, mval); + doc->dat_read = str_sum + 1; + return doc; +} + +static_inline bool unsafe_yyjson_num_equals(void *lhs, void *rhs) { + yyjson_val_uni *luni = &((yyjson_val *)lhs)->uni; + yyjson_val_uni *runi = &((yyjson_val *)rhs)->uni; + yyjson_subtype lt = unsafe_yyjson_get_subtype(lhs); + yyjson_subtype rt = unsafe_yyjson_get_subtype(rhs); + if (lt == rt) return luni->u64 == runi->u64; + if (lt == YYJSON_SUBTYPE_SINT && rt == YYJSON_SUBTYPE_UINT) { + return luni->i64 >= 0 && luni->u64 == runi->u64; + } + if (lt == YYJSON_SUBTYPE_UINT && rt == YYJSON_SUBTYPE_SINT) { + return runi->i64 >= 0 && luni->u64 == runi->u64; + } + return false; +} + +static_inline bool unsafe_yyjson_str_equals(void *lhs, void *rhs) { + usize len = unsafe_yyjson_get_len(lhs); + if (len != unsafe_yyjson_get_len(rhs)) return false; + return !memcmp(unsafe_yyjson_get_str(lhs), + unsafe_yyjson_get_str(rhs), len); +} + +bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) { + yyjson_type type = unsafe_yyjson_get_type(lhs); + if (type != unsafe_yyjson_get_type(rhs)) return false; + + switch (type) { + case YYJSON_TYPE_OBJ: { + usize len = unsafe_yyjson_get_len(lhs); + if (len != unsafe_yyjson_get_len(rhs)) return false; + if (len > 0) { + yyjson_obj_iter iter; + yyjson_obj_iter_init(rhs, &iter); + lhs = unsafe_yyjson_get_first(lhs); + while (len-- > 0) { + rhs = yyjson_obj_iter_getn(&iter, lhs->uni.str, + unsafe_yyjson_get_len(lhs)); + if (!rhs) return false; + if (!unsafe_yyjson_equals(lhs + 1, rhs)) return false; + lhs = unsafe_yyjson_get_next(lhs + 1); + } + } + /* yyjson allows duplicate keys, so the check may be inaccurate */ + return true; + } + + case YYJSON_TYPE_ARR: { + usize len = unsafe_yyjson_get_len(lhs); + if (len != unsafe_yyjson_get_len(rhs)) return false; + if (len > 0) { + lhs = unsafe_yyjson_get_first(lhs); + rhs = unsafe_yyjson_get_first(rhs); + while (len-- > 0) { + if (!unsafe_yyjson_equals(lhs, rhs)) return false; + lhs = unsafe_yyjson_get_next(lhs); + rhs = unsafe_yyjson_get_next(rhs); + } + } + return true; + } + + case YYJSON_TYPE_NUM: + return unsafe_yyjson_num_equals(lhs, rhs); + + case YYJSON_TYPE_RAW: + case YYJSON_TYPE_STR: + return unsafe_yyjson_str_equals(lhs, rhs); + + case YYJSON_TYPE_NULL: + case YYJSON_TYPE_BOOL: + return lhs->tag == rhs->tag; + + default: + return false; + } +} + +bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, yyjson_mut_val *rhs) { + yyjson_type type = unsafe_yyjson_get_type(lhs); + if (type != unsafe_yyjson_get_type(rhs)) return false; + + switch (type) { + case YYJSON_TYPE_OBJ: { + usize len = unsafe_yyjson_get_len(lhs); + if (len != unsafe_yyjson_get_len(rhs)) return false; + if (len > 0) { + yyjson_mut_obj_iter iter; + yyjson_mut_obj_iter_init(rhs, &iter); + lhs = (yyjson_mut_val *)lhs->uni.ptr; + while (len-- > 0) { + rhs = yyjson_mut_obj_iter_getn(&iter, lhs->uni.str, + unsafe_yyjson_get_len(lhs)); + if (!rhs) return false; + if (!unsafe_yyjson_mut_equals(lhs->next, rhs)) return false; + lhs = lhs->next->next; + } + } + /* yyjson allows duplicate keys, so the check may be inaccurate */ + return true; + } + + case YYJSON_TYPE_ARR: { + usize len = unsafe_yyjson_get_len(lhs); + if (len != unsafe_yyjson_get_len(rhs)) return false; + if (len > 0) { + lhs = (yyjson_mut_val *)lhs->uni.ptr; + rhs = (yyjson_mut_val *)rhs->uni.ptr; + while (len-- > 0) { + if (!unsafe_yyjson_mut_equals(lhs, rhs)) return false; + lhs = lhs->next; + rhs = rhs->next; + } + } + return true; + } + + case YYJSON_TYPE_NUM: + return unsafe_yyjson_num_equals(lhs, rhs); + + case YYJSON_TYPE_RAW: + case YYJSON_TYPE_STR: + return unsafe_yyjson_str_equals(lhs, rhs); + + case YYJSON_TYPE_NULL: + case YYJSON_TYPE_BOOL: + return lhs->tag == rhs->tag; + + default: + return false; + } +} + +bool yyjson_locate_pos(const char *str, size_t len, size_t pos, + size_t *line, size_t *col, size_t *chr) { + usize line_sum = 0, line_pos = 0, chr_sum = 0; + const u8 *cur = (const u8 *)str; + const u8 *end = cur + pos; + + if (!str || pos > len) { + if (line) *line = 0; + if (col) *col = 0; + if (chr) *chr = 0; + return false; + } + + if (pos >= 3 && is_utf8_bom(cur)) cur += 3; /* don't count BOM */ + while (cur < end) { + u8 c = *cur; + chr_sum += 1; + if (likely(c < 0x80)) { /* 0xxxxxxx (0x00-0x7F) ASCII */ + if (c == '\n') { + line_sum += 1; + line_pos = chr_sum; + } + cur += 1; + } + else if (c < 0xC0) cur += 1; /* 10xxxxxx (0x80-0xBF) Invalid */ + else if (c < 0xE0) cur += 2; /* 110xxxxx (0xC0-0xDF) 2-byte UTF-8 */ + else if (c < 0xF0) cur += 3; /* 1110xxxx (0xE0-0xEF) 3-byte UTF-8 */ + else if (c < 0xF8) cur += 4; /* 11110xxx (0xF0-0xF7) 4-byte UTF-8 */ + else cur += 1; /* 11111xxx (0xF8-0xFF) Invalid */ + } + if (line) *line = line_sum + 1; + if (col) *col = chr_sum - line_pos + 1; + if (chr) *chr = chr_sum; + return true; +} + + + +#if !YYJSON_DISABLE_READER /* reader begin */ + +/* Check read flag, avoids `always false` warning when disabled. */ +#define has_flg(_flg) unlikely(has_rflag(flg, YYJSON_READ_##_flg, 0)) +#define has_allow(_flg) unlikely(has_rflag(flg, YYJSON_READ_ALLOW_##_flg, 1)) +#define YYJSON_READ_ALLOW_TRIVIA (YYJSON_READ_ALLOW_COMMENTS | \ + YYJSON_READ_ALLOW_EXT_WHITESPACE) +static_inline bool has_rflag(yyjson_read_flag flg, yyjson_read_flag chk, + bool non_standard) { +#if YYJSON_DISABLE_NON_STANDARD + if (non_standard) return false; +#endif + return (flg & chk) != 0; +} + + + +/*============================================================================== + * MARK: - JSON Reader Utils (Private) + * These functions are used by JSON reader to read literals and comments. + *============================================================================*/ + +/** Read `true` literal, `*ptr[0]` should be `t`. */ +static_inline bool read_true(u8 **ptr, yyjson_val *val) { + u8 *cur = *ptr; + if (likely(byte_match_4(cur, "true"))) { + val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; + *ptr = cur + 4; + return true; + } + return false; +} + +/** Read `false` literal, `*ptr[0]` should be `f`. */ +static_inline bool read_false(u8 **ptr, yyjson_val *val) { + u8 *cur = *ptr; + if (likely(byte_match_4(cur + 1, "alse"))) { + val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; + *ptr = cur + 5; + return true; + } + return false; +} + +/** Read `null` literal, `*ptr[0]` should be `n`. */ +static_inline bool read_null(u8 **ptr, yyjson_val *val) { + u8 *cur = *ptr; + if (likely(byte_match_4(cur, "null"))) { + val->tag = YYJSON_TYPE_NULL; + *ptr = cur + 4; + return true; + } + return false; +} + +/** Read `Inf` or `Infinity` literal (ignoring case). */ +static_inline bool read_inf(u8 **ptr, u8 **pre, + yyjson_read_flag flg, yyjson_val *val) { + u8 *hdr = *ptr; + u8 *cur = *ptr; + u8 **end = ptr; + bool sign = (*cur == '-'); + if (*cur == '+' && !has_allow(EXT_NUMBER)) return false; + cur += char_is_sign(*cur); + if (char_to_lower(cur[0]) == 'i' && + char_to_lower(cur[1]) == 'n' && + char_to_lower(cur[2]) == 'f') { + if (char_to_lower(cur[3]) == 'i') { + if (char_to_lower(cur[4]) == 'n' && + char_to_lower(cur[5]) == 'i' && + char_to_lower(cur[6]) == 't' && + char_to_lower(cur[7]) == 'y') { + cur += 8; + } else { + return false; + } + } else { + cur += 3; + } + *end = cur; + if (has_flg(NUMBER_AS_RAW)) { + **pre = '\0'; /* add null-terminator for previous raw string */ + *pre = cur; /* save end position for current raw string */ + val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; + val->uni.str = (const char *)hdr; + } else { + val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; + val->uni.u64 = f64_bits_inf(sign); + } + return true; + } + return false; +} + +/** Read `NaN` literal (ignoring case). */ +static_inline bool read_nan(u8 **ptr, u8 **pre, + yyjson_read_flag flg, yyjson_val *val) { + u8 *hdr = *ptr; + u8 *cur = *ptr; + u8 **end = ptr; + bool sign = (*cur == '-'); + if (*cur == '+' && !has_allow(EXT_NUMBER)) return false; + cur += char_is_sign(*cur); + if (char_to_lower(cur[0]) == 'n' && + char_to_lower(cur[1]) == 'a' && + char_to_lower(cur[2]) == 'n') { + cur += 3; + *end = cur; + if (has_flg(NUMBER_AS_RAW)) { + **pre = '\0'; /* add null-terminator for previous raw string */ + *pre = cur; /* save end position for current raw string */ + val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; + val->uni.str = (const char *)hdr; + } else { + val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; + val->uni.u64 = f64_bits_nan(sign); + } + return true; + } + return false; +} + +/** Read `Inf`, `Infinity` or `NaN` literal (ignoring case). */ +static_inline bool read_inf_or_nan(u8 **ptr, u8 **pre, + yyjson_read_flag flg, yyjson_val *val) { + if (read_inf(ptr, pre, flg, val)) return true; + if (read_nan(ptr, pre, flg, val)) return true; + return false; +} + +/** Read a JSON number as raw string. */ +static_noinline bool read_num_raw(u8 **ptr, u8 **pre, yyjson_read_flag flg, + yyjson_val *val, const char **msg) { +#define return_err(_pos, _msg) do { \ + *msg = _msg; *end = _pos; return false; \ +} while (false) + +#define return_raw() do { \ + val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \ + val->uni.str = (const char *)hdr; \ + **pre = '\0'; *pre = cur; *end = cur; return true; \ +} while (false) + + u8 *hdr = *ptr; + u8 *cur = *ptr; + u8 **end = ptr; + + /* skip sign */ + cur += (*cur == '-'); + + /* read first digit, check leading zero */ + while (unlikely(!char_is_digit(*cur))) { + if (has_allow(EXT_NUMBER)) { + if (*cur == '+' && cur == hdr) { /* leading `+` sign */ + cur++; + continue; + } + if (*cur == '.' && char_is_digit(cur[1])) { /* e.g. '.123' */ + goto read_double; + } + } + if (has_allow(INF_AND_NAN)) { + if (read_inf_or_nan(ptr, pre, flg, val)) return true; + } + return_err(cur, "no digit after sign"); + } + + /* read integral part */ + if (*cur == '0') { + cur++; + if (unlikely(char_is_digit(*cur))) { + return_err(cur - 1, "number with leading zero is not allowed"); + } + if (!char_is_fp(*cur)) { + if (has_allow(EXT_NUMBER) && char_to_lower(*cur) == 'x') { /* hex */ + if (!char_is_hex(*++cur)) return_err(cur, "invalid hex number"); + while(char_is_hex(*cur)) cur++; + } + return_raw(); + } + } else { + while (char_is_digit(*cur)) cur++; + if (!char_is_fp(*cur)) return_raw(); + } + +read_double: + /* read fraction part */ + if (*cur == '.') { + cur++; + if (!char_is_digit(*cur)) { + if (has_allow(EXT_NUMBER)) { + if (!char_is_exp(*cur)) return_raw(); + } else { + return_err(cur, "no digit after decimal point"); + } + } + while (char_is_digit(*cur)) cur++; + } + + /* read exponent part */ + if (char_is_exp(*cur)) { + cur += 1 + char_is_sign(cur[1]); + if (!char_is_digit(*cur++)) { + return_err(cur, "no digit after exponent sign"); + } + while (char_is_digit(*cur)) cur++; + } + + return_raw(); + +#undef return_err +#undef return_raw +} + +/** Read a hex number. */ +static_noinline bool read_num_hex(u8 **ptr, u8 **pre, yyjson_read_flag flg, + yyjson_val *val, const char **msg) { + u8 *hdr = *ptr; + u8 *cur = *ptr; + u8 **end = ptr; + u64 sig = 0, i = 0; + bool sign; + + /* skip sign and '0x' */ + sign = (*cur == '-'); + cur += (*cur == '-' || *cur == '+') + 2; + + /* read hex */ + for(; i < 16; i++) { + u8 c = hex_conv_table[cur[i]]; + if (c == 0xF0) break; + sig <<= 4; + sig |= c; + } + + /* check error */ + if (unlikely(i == 0)) { + *msg = "invalid hex number"; + return false; + } + + /* check overflow */ + if (unlikely(i == 16)) { + if (char_is_hex(cur[16]) || (sign && sig > ((u64)1 << 63))) { + if (!has_flg(BIGNUM_AS_RAW)) { + *msg = "hex number overflow"; + return false; + } + cur += 16; + while (char_is_hex(*cur)) cur++; + **pre = '\0'; + val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; + val->uni.str = (const char *)hdr; + *pre = cur; *end = cur; + return true; + } + } + + val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); + val->uni.u64 = (u64)(sign ? (u64)(~(sig) + 1) : (u64)(sig)); + *end = cur + i; + return true; +} + +/** + Skip trivia (whitespace and comments). + This function should be used only when `char_is_trivia()` returns true. + @param ptr (inout) Input current position, output end position. + @param eof JSON end position. + @param flg JSON read flags. + @return true if at least one character was skipped. + false if no characters were skipped, + or if a multi-line comment is unterminated; + in the latter case, `ptr` will be set to `eof`. + */ +static_noinline bool skip_trivia(u8 **ptr, u8 *eof, yyjson_read_flag flg) { + u8 *hdr = *ptr, *cur = *ptr; + usize len; + + while (cur < eof) { + u8 *loop_begin = cur; + + /* skip standard whitespace */ + while(char_is_space(*cur)) cur++; + + /* skip extended whitespace */ + if (has_allow(EXT_WHITESPACE)) { + while (char_is_space_ext(*cur)) { + cur += (len = ext_space_len(cur)); + if (!len) break; + } + } + + /* skip comment, do not validate encoding */ + if (has_allow(COMMENTS) && cur[0] == '/') { + if (cur[1] == '/') { /* single-line comment */ + cur += 2; + if (has_allow(EXT_WHITESPACE)) { + while (cur < eof) { + if (char_is_eol_ext(*cur)) { + cur += (len = ext_eol_len(cur)); + if (len) break; + } + cur++; + } + } else { + while (cur < eof && !char_is_eol(*cur)) cur++; + } + } else if (cur[1] == '*') { /* multi-line comment */ + cur += 2; + while (!byte_match_2(cur, "*/") && cur < eof) cur++; + if (cur == eof) { + *ptr = eof; + return false; /* unclosed comment */ + } + cur += 2; + } + } + if (cur == loop_begin) break; + } + *ptr = cur; + return cur > hdr; +} + +/** + Check truncated UTF-8 character. + Return true if `cur` starts a valid UTF-8 sequence that is truncated. + */ +static bool is_truncated_utf8(u8 *cur, u8 *eof) { + u8 c0, c1, c2; + usize len = (usize)(eof - cur); + if (cur >= eof || len >= 4) return false; + c0 = cur[0]; c1 = cur[1]; c2 = cur[2]; + /* 1-byte UTF-8, not truncated */ + if (c0 < 0x80) return false; + if (len == 1) { + /* 2-byte UTF-8, truncated */ + if ((c0 & 0xE0) == 0xC0 && (c0 & 0x1E) != 0x00) return true; + /* 3-byte UTF-8, truncated */ + if ((c0 & 0xF0) == 0xE0) return true; + /* 4-byte UTF-8, truncated */ + if ((c0 & 0xF8) == 0xF0 && (c0 & 0x07) <= 0x04) return true; + } else if (len == 2) { + /* 3-byte UTF-8, truncated */ + if ((c0 & 0xF0) == 0xE0 && (c1 & 0xC0) == 0x80) { + u8 t = (u8)(((c0 & 0x0F) << 1) | ((c1 & 0x20) >> 5)); + return 0x01 <= t && t != 0x1B; + } + /* 4-byte UTF-8, truncated */ + if ((c0 & 0xF8) == 0xF0 && (c1 & 0xC0) == 0x80) { + u8 t = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4)); + return 0x01 <= t && t <= 0x10; + } + } else if (len == 3) { + /* 4 bytes UTF-8, truncated */ + if ((c0 & 0xF8) == 0xF0 && (c1 & 0xC0) == 0x80 && (c2 & 0xC0) == 0x80) { + u8 t = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4)); + return 0x01 <= t && t <= 0x10; + } + } + return false; +} + +/** + Check truncated string. + Returns true if `cur` match `str` but is truncated. + The `str` should be lowercase ASCII letters. + */ +static bool is_truncated_str(u8 *cur, u8 *eof, const char *str, + bool case_sensitive) { + usize len = strlen(str); + if (cur + len <= eof || eof <= cur) return false; + if (case_sensitive) { + return memcmp(cur, str, (usize)(eof - cur)) == 0; + } + for (; cur < eof; cur++, str++) { + if (char_to_lower(*cur) != *(const u8 *)str) return false; + } + return true; +} + +/** + Check truncated JSON on parsing errors. + Returns true if the input is valid but truncated. + */ +static_noinline bool is_truncated_end(u8 *hdr, u8 *cur, u8 *eof, + yyjson_read_code code, + yyjson_read_flag flg) { + if (cur >= eof) return true; + if (code == YYJSON_READ_ERROR_LITERAL) { + if (is_truncated_str(cur, eof, "true", true) || + is_truncated_str(cur, eof, "false", true) || + is_truncated_str(cur, eof, "null", true)) { + return true; + } + } + if (code == YYJSON_READ_ERROR_UNEXPECTED_CHARACTER || + code == YYJSON_READ_ERROR_INVALID_NUMBER || + code == YYJSON_READ_ERROR_LITERAL) { + if (has_allow(INF_AND_NAN)) { + if (*cur == '-') cur++; + if (is_truncated_str(cur, eof, "infinity", false) || + is_truncated_str(cur, eof, "nan", false)) { + return true; + } + } + } + if (code == YYJSON_READ_ERROR_UNEXPECTED_CONTENT) { + if (has_allow(INF_AND_NAN)) { + if (hdr + 3 <= cur && + is_truncated_str(cur - 3, eof, "infinity", false)) { + return true; /* e.g. infin would be read as inf + in */ + } + } + } + if (code == YYJSON_READ_ERROR_INVALID_STRING) { + usize len = (usize)(eof - cur); + + /* unicode escape sequence */ + if (*cur == '\\') { + if (len == 1) return true; + if (len <= 5) { + if (*++cur != 'u') return false; + for (++cur; cur < eof; cur++) { + if (!char_is_hex(*cur)) return false; + } + return true; + } else if (len <= 11) { + /* incomplete surrogate pair? */ + u16 hi; + if (*++cur != 'u') return false; + if (!hex_load_4(++cur, &hi)) return false; + if ((hi & 0xF800) != 0xD800) return false; + cur += 4; + if (cur >= eof) return true; + /* valid low surrogate is DC00...DFFF */ + if (*cur != '\\') return false; + if (++cur >= eof) return true; + if (*cur != 'u') return false; + if (++cur >= eof) return true; + if (*cur != 'd' && *cur != 'D') return false; + if (++cur >= eof) return true; + if ((*cur < 'c' || *cur > 'f') && (*cur < 'C' || *cur > 'F')) + return false; + if (++cur >= eof) return true; + if (!char_is_hex(*cur)) return false; + return true; + } + return false; + } + + /* 2 to 4 bytes UTF-8 */ + if (is_truncated_utf8(cur, eof)) { + return true; + } + } + if (has_allow(COMMENTS)) { + if (code == YYJSON_READ_ERROR_INVALID_COMMENT) { + /* unclosed multiline comment */ + return true; + } + if (code == YYJSON_READ_ERROR_UNEXPECTED_CHARACTER && + *cur == '/' && cur + 1 == eof) { + /* truncated beginning of comment */ + return true; + } + } + if (code == YYJSON_READ_ERROR_UNEXPECTED_CHARACTER && + has_allow(BOM)) { + /* truncated UTF-8 BOM */ + usize len = (usize)(eof - cur); + if (cur == hdr && len < 3 && !memcmp(hdr, "\xEF\xBB\xBF", len)) { + return true; + } + } + return false; +} + + + +#if !YYJSON_DISABLE_FAST_FP_CONV /* FP_READER */ + +/*============================================================================== + * MARK: - BigInt For Floating Point Number Reader (Private) + * + * The bigint algorithm is used by floating-point number reader to get correctly + * rounded result for numbers with lots of digits. This part of code is rarely + * used for common numbers. + *============================================================================*/ + +/** Unsigned arbitrarily large integer */ +typedef struct bigint { + u32 used; /* used chunks count, should not be 0 */ + u64 bits[64]; /* chunks (58 is enough here) */ +} bigint; + +/** + Evaluate 'big += val'. + @param big A big number (can be 0). + @param val An unsigned integer (can be 0). + */ +static_inline void bigint_add_u64(bigint *big, u64 val) { + u32 idx, max; + u64 num = big->bits[0]; + u64 add = num + val; + big->bits[0] = add; + if (likely((add >= num) || (add >= val))) return; + for ((void)(idx = 1), max = big->used; idx < max; idx++) { + if (likely(big->bits[idx] != U64_MAX)) { + big->bits[idx] += 1; + return; + } + big->bits[idx] = 0; + } + big->bits[big->used++] = 1; +} + +/** + Evaluate 'big *= val'. + @param big A big number (can be 0). + @param val An unsigned integer (cannot be 0). + */ +static_inline void bigint_mul_u64(bigint *big, u64 val) { + u32 idx = 0, max = big->used; + u64 hi, lo, carry = 0; + for (; idx < max; idx++) { + if (big->bits[idx]) break; + } + for (; idx < max; idx++) { + u128_mul_add(big->bits[idx], val, carry, &hi, &lo); + big->bits[idx] = lo; + carry = hi; + } + if (carry) big->bits[big->used++] = carry; +} + +/** + Evaluate 'big *= 2^exp'. + @param big A big number (can be 0). + @param exp An exponent integer (can be 0). + */ +static_inline void bigint_mul_pow2(bigint *big, u32 exp) { + u32 shft = exp % 64; + u32 move = exp / 64; + u32 idx = big->used; + if (unlikely(shft == 0)) { + for (; idx > 0; idx--) { + big->bits[idx + move - 1] = big->bits[idx - 1]; + } + big->used += move; + while (move) big->bits[--move] = 0; + } else { + big->bits[idx] = 0; + for (; idx > 0; idx--) { + u64 num = big->bits[idx] << shft; + num |= big->bits[idx - 1] >> (64 - shft); + big->bits[idx + move] = num; + } + big->bits[move] = big->bits[0] << shft; + big->used += move + (big->bits[big->used + move] > 0); + while (move) big->bits[--move] = 0; + } +} + +/** + Evaluate 'big *= 10^exp'. + @param big A big number (can be 0). + @param exp An exponent integer (cannot be 0). + */ +static_inline void bigint_mul_pow10(bigint *big, i32 exp) { + for (; exp >= U64_POW10_MAX_EXACT_EXP; exp -= U64_POW10_MAX_EXACT_EXP) { + bigint_mul_u64(big, u64_pow10_table[U64_POW10_MAX_EXACT_EXP]); + } + if (exp) { + bigint_mul_u64(big, u64_pow10_table[exp]); + } +} + +/** + Compare two bigint. + @return -1 if 'a < b', +1 if 'a > b', 0 if 'a == b'. + */ +static_inline i32 bigint_cmp(bigint *a, bigint *b) { + u32 idx = a->used; + if (a->used < b->used) return -1; + if (a->used > b->used) return +1; + while (idx-- > 0) { + u64 av = a->bits[idx]; + u64 bv = b->bits[idx]; + if (av < bv) return -1; + if (av > bv) return +1; + } + return 0; +} + +/** + Evaluate 'big = val'. + @param big A big number (can be 0). + @param val An unsigned integer (can be 0). + */ +static_inline void bigint_set_u64(bigint *big, u64 val) { + big->used = 1; + big->bits[0] = val; +} + +/** Set a bigint with floating point number string. */ +static_noinline void bigint_set_buf(bigint *big, u64 sig, i32 *exp, + u8 *sig_cut, u8 *sig_end, u8 *dot_pos) { + + if (unlikely(!sig_cut)) { + /* no digit cut, set significant part only */ + bigint_set_u64(big, sig); + return; + + } else { + /* some digits were cut, read them from 'sig_cut' to 'sig_end' */ + u8 *hdr = sig_cut; + u8 *cur = hdr; + u32 len = 0; + u64 val = 0; + bool dig_big_cut = false; + bool has_dot = (hdr < dot_pos) & (dot_pos < sig_end); + u32 dig_len_total = U64_SAFE_DIG + (u32)(sig_end - hdr) - has_dot; + + sig -= (*sig_cut >= '5'); /* sig was rounded before */ + if (dig_len_total > F64_MAX_DEC_DIG) { + dig_big_cut = true; + sig_end -= dig_len_total - (F64_MAX_DEC_DIG + 1); + sig_end -= (dot_pos + 1 == sig_end); + dig_len_total = (F64_MAX_DEC_DIG + 1); + } + *exp -= (i32)dig_len_total - U64_SAFE_DIG; + + big->used = 1; + big->bits[0] = sig; + while (cur < sig_end) { + if (likely(cur != dot_pos)) { + val = val * 10 + (u8)(*cur++ - '0'); + len++; + if (unlikely(cur == sig_end && dig_big_cut)) { + /* The last digit must be non-zero, */ + /* set it to '1' for correct rounding. */ + val = val - (val % 10) + 1; + } + if (len == U64_SAFE_DIG || cur == sig_end) { + bigint_mul_pow10(big, (i32)len); + bigint_add_u64(big, val); + val = 0; + len = 0; + } + } else { + cur++; + } + } + } +} + + + +/*============================================================================== + * MARK: - Diy Floating Point (Private) + *============================================================================*/ + +/** "Do It Yourself Floating Point" struct. */ +typedef struct diy_fp { + u64 sig; /* significand */ + i32 exp; /* exponent, base 2 */ + i32 pad; /* padding, useless */ +} diy_fp; + +/** Get cached rounded diy_fp with pow(10, e) The input value must in range + [POW10_SIG_TABLE_MIN_EXP, POW10_SIG_TABLE_MAX_EXP]. */ +static_inline diy_fp diy_fp_get_cached_pow10(i32 exp10) { + diy_fp fp; + u64 sig_ext; + pow10_table_get_sig(exp10, &fp.sig, &sig_ext); + pow10_table_get_exp(exp10, &fp.exp); + fp.sig += (sig_ext >> 63); + return fp; +} + +/** Returns fp * fp2. */ +static_inline diy_fp diy_fp_mul(diy_fp fp, diy_fp fp2) { + u64 hi, lo; + u128_mul(fp.sig, fp2.sig, &hi, &lo); + fp.sig = hi + (lo >> 63); + fp.exp += fp2.exp + 64; + return fp; +} + +/** Convert diy_fp to IEEE-754 raw value. */ +static_inline u64 diy_fp_to_ieee_raw(diy_fp fp) { + u64 sig = fp.sig; + i32 exp = fp.exp; + u32 lz_bits; + if (unlikely(fp.sig == 0)) return 0; + + lz_bits = u64_lz_bits(sig); + sig <<= lz_bits; + sig >>= F64_BITS - F64_SIG_FULL_BITS; + exp -= (i32)lz_bits; + exp += F64_BITS - F64_SIG_FULL_BITS; + exp += F64_SIG_BITS; + + if (unlikely(exp >= F64_MAX_BIN_EXP)) { + /* overflow */ + return F64_BITS_INF; + } else if (likely(exp >= F64_MIN_BIN_EXP - 1)) { + /* normal */ + exp += F64_EXP_BIAS; + return ((u64)exp << F64_SIG_BITS) | (sig & F64_SIG_MASK); + } else if (likely(exp >= F64_MIN_BIN_EXP - F64_SIG_FULL_BITS)) { + /* subnormal */ + return sig >> (F64_MIN_BIN_EXP - exp - 1); + } else { + /* underflow */ + return 0; + } +} + + + +/*============================================================================== + * MARK: - Number Reader (Private) + *============================================================================*/ + +/** + Read a JSON number. + + 1. This function assume that the floating-point number is in IEEE-754 format. + 2. This function support uint64/int64/double number. If an integer number + cannot fit in uint64/int64, it will returns as a double number. If a double + number is infinite, the return value is based on flag. + 3. This function (with inline attribute) may generate a lot of instructions. + */ +static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, + yyjson_val *val, const char **msg) { +#define return_err(_pos, _msg) do { \ + *msg = _msg; \ + *end = _pos; \ + return false; \ +} while (false) + +#define return_0() do { \ + val->tag = YYJSON_TYPE_NUM | (u8)((u8)sign << 3); \ + val->uni.u64 = 0; \ + *end = cur; return true; \ +} while (false) + +#define return_i64(_v) do { \ + val->tag = YYJSON_TYPE_NUM | (u8)((u8)sign << 3); \ + val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \ + *end = cur; return true; \ +} while (false) + +#define return_f64(_v) do { \ + val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ + val->uni.f64 = sign ? -(f64)(_v) : (f64)(_v); \ + *end = cur; return true; \ +} while (false) + +#define return_f64_bin(_v) do { \ + val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ + val->uni.u64 = ((u64)sign << 63) | (u64)(_v); \ + *end = cur; return true; \ +} while (false) + +#define return_inf() do { \ + if (has_flg(BIGNUM_AS_RAW)) return_raw(); \ + if (has_allow(INF_AND_NAN)) return_f64_bin(F64_BITS_INF); \ + else return_err(hdr, "number is infinity when parsed as double"); \ +} while (false) + +#define return_raw() do { \ + **pre = '\0'; /* add null-terminator for previous raw string */ \ + val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \ + val->uni.str = (const char *)hdr; \ + *pre = cur; *end = cur; return true; \ +} while (false) + + u8 *sig_cut = NULL; /* significant part cutting position for long number */ + u8 *sig_end = NULL; /* significant part ending position */ + u8 *dot_pos = NULL; /* decimal point position */ + + u64 sig = 0; /* significant part of the number */ + i32 exp = 0; /* exponent part of the number */ + + bool exp_sign; /* temporary exponent sign from literal part */ + i64 exp_sig = 0; /* temporary exponent number from significant part */ + i64 exp_lit = 0; /* temporary exponent number from exponent literal part */ + u64 num; /* temporary number for reading */ + u8 *tmp; /* temporary cursor for reading */ + + u8 *hdr = *ptr; + u8 *cur = *ptr; + u8 **end = ptr; + bool sign; + + /* read number as raw string if has `YYJSON_READ_NUMBER_AS_RAW` flag */ + if (has_flg(NUMBER_AS_RAW)) { + return read_num_raw(ptr, pre, flg, val, msg); + } + + sign = (*hdr == '-'); + cur += sign; + + /* begin with a leading zero or non-digit */ + while (unlikely(!char_is_nonzero(*cur))) { /* 0 or non-digit char */ + if (unlikely(*cur != '0')) { /* non-digit char */ + if (has_allow(EXT_NUMBER)) { + if (*cur == '+' && cur == hdr) { /* leading `+` sign */ + cur++; + continue; + } + if (*cur == '.' && char_is_digit(cur[1])) { /* e.g. '.123' */ + goto leading_dot; + } + } + if (has_allow(INF_AND_NAN)) { + if (read_inf_or_nan(ptr, pre, flg, val)) return true; + } + return_err(cur, "no digit after sign"); + } + /* begin with 0 */ + if (likely(!char_is_digit_or_fp(*++cur))) { + if (has_allow(EXT_NUMBER) && char_to_lower(*cur) == 'x') { /* hex */ + return read_num_hex(ptr, pre, flg, val, msg); + } + return_0(); + } + if (likely(*cur == '.')) { +leading_dot: + dot_pos = cur++; + if (unlikely(!char_is_digit(*cur))) { + if (has_allow(EXT_NUMBER)) { + if (char_is_exp(*cur)) { + goto digi_exp_more; + } else { + return_f64_bin(0); + } + } + return_err(cur, "no digit after decimal point"); + } + while (unlikely(*cur == '0')) cur++; + if (likely(char_is_digit(*cur))) { + /* first non-zero digit after decimal point */ + sig = (u64)(*cur - '0'); /* read first digit */ + cur--; + goto digi_frac_1; /* continue read fraction part */ + } + } + if (unlikely(char_is_digit(*cur))) { + return_err(cur - 1, "number with leading zero is not allowed"); + } + if (unlikely(char_is_exp(*cur))) { /* 0 with any exponent is still 0 */ + cur += (usize)1 + char_is_sign(cur[1]); + if (unlikely(!char_is_digit(*cur))) { + return_err(cur, "no digit after exponent sign"); + } + while (char_is_digit(*++cur)); + } + return_f64_bin(0); + } + + /* begin with non-zero digit */ + sig = (u64)(*cur - '0'); + + /* + Read integral part, same as the following code. + + for (int i = 1; i <= 18; i++) { + num = cur[i] - '0'; + if (num <= 9) sig = num + sig * 10; + else goto digi_sepr_i; + } + */ +#define expr_intg(i) \ + if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \ + else { goto digi_sepr_##i; } + repeat_in_1_18(expr_intg) +#undef expr_intg + + + cur += 19; /* skip continuous 19 digits */ + if (!char_is_digit_or_fp(*cur)) { + /* this number is an integer consisting of 19 digits */ + if (sign && (sig > ((u64)1 << 63))) { /* overflow */ + if (has_flg(BIGNUM_AS_RAW)) return_raw(); + return_f64(unsafe_yyjson_u64_to_f64(sig)); + } + return_i64(sig); + } + goto digi_intg_more; /* read more digits in integral part */ + + + /* process first non-digit character */ +#define expr_sepr(i) \ + digi_sepr_##i: \ + if (likely(!char_is_fp(cur[i]))) { cur += i; return_i64(sig); } \ + dot_pos = cur + i; \ + if (likely(cur[i] == '.')) goto digi_frac_##i; \ + cur += i; sig_end = cur; goto digi_exp_more; + repeat_in_1_18(expr_sepr) +#undef expr_sepr + + + /* read fraction part */ +#define expr_frac(i) \ + digi_frac_##i: \ + if (likely((num = (u64)(cur[i + 1] - (u8)'0')) <= 9)) \ + sig = num + sig * 10; \ + else { goto digi_stop_##i; } + repeat_in_1_18(expr_frac) +#undef expr_frac + + cur += 20; /* skip 19 digits and 1 decimal point */ + if (!char_is_digit(*cur)) goto digi_frac_end; /* fraction part end */ + goto digi_frac_more; /* read more digits in fraction part */ + + + /* significant part end */ +#define expr_stop(i) \ + digi_stop_##i: \ + cur += i + 1; \ + goto digi_frac_end; + repeat_in_1_18(expr_stop) +#undef expr_stop + + + /* read more digits in integral part */ +digi_intg_more: + if (char_is_digit(*cur)) { + if (!char_is_digit_or_fp(cur[1])) { + /* this number is an integer consisting of 20 digits */ + num = (u64)(*cur - '0'); + if ((sig < (U64_MAX / 10)) || + (sig == (U64_MAX / 10) && num <= (U64_MAX % 10))) { + sig = num + sig * 10; + cur++; + /* convert to double if overflow */ + if (sign) { + if (has_flg(BIGNUM_AS_RAW)) return_raw(); + return_f64(unsafe_yyjson_u64_to_f64(sig)); + } + return_i64(sig); + } + } + } + + if (char_is_exp(*cur)) { + dot_pos = cur; + goto digi_exp_more; + } + + if (*cur == '.') { + dot_pos = cur++; + if (unlikely(!char_is_digit(*cur))) { + if (has_allow(EXT_NUMBER)) { + goto digi_frac_end; + } + return_err(cur, "no digit after decimal point"); + } + } + + + /* read more digits in fraction part */ +digi_frac_more: + sig_cut = cur; /* too large to fit in u64, excess digits need to be cut */ + sig += (*cur >= '5'); /* round */ + while (char_is_digit(*++cur)); + if (!dot_pos) { + if (!char_is_fp(*cur) && has_flg(BIGNUM_AS_RAW)) { + return_raw(); /* it's a large integer */ + } + dot_pos = cur; + if (*cur == '.') { + if (unlikely(!char_is_digit(*++cur))) { + if (!has_allow(EXT_NUMBER)) { + return_err(cur, "no digit after decimal point"); + } + } + while (char_is_digit(*cur)) cur++; + } + } + exp_sig = (i64)(dot_pos - sig_cut); + exp_sig += (dot_pos < sig_cut); + + /* ignore trailing zeros */ + tmp = cur - 1; + while ((*tmp == '0' || *tmp == '.') && tmp > hdr) tmp--; + if (tmp < sig_cut) { + sig_cut = NULL; + } else { + sig_end = cur; + } + + if (char_is_exp(*cur)) goto digi_exp_more; + goto digi_exp_finish; + + + /* fraction part end */ +digi_frac_end: + if (unlikely(dot_pos + 1 == cur)) { + if (!has_allow(EXT_NUMBER)) { + return_err(cur, "no digit after decimal point"); + } + } + sig_end = cur; + exp_sig = -(i64)((u64)(cur - dot_pos) - 1); + if (likely(!char_is_exp(*cur))) { + if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) { + return_f64_bin(0); /* underflow */ + } + exp = (i32)exp_sig; + goto digi_finish; + } else { + goto digi_exp_more; + } + + + /* read exponent part */ +digi_exp_more: + exp_sign = (*++cur == '-'); + cur += char_is_sign(*cur); + if (unlikely(!char_is_digit(*cur))) { + return_err(cur, "no digit after exponent sign"); + } + while (*cur == '0') cur++; + + /* read exponent literal */ + tmp = cur; + while (char_is_digit(*cur)) { + exp_lit = (i64)((u8)(*cur++ - '0') + (u64)exp_lit * 10); + } + if (unlikely(cur - tmp >= U64_SAFE_DIG)) { + if (exp_sign) { + return_f64_bin(0); /* underflow */ + } else { + return_inf(); /* overflow */ + } + } + exp_sig += exp_sign ? -exp_lit : exp_lit; + + + /* validate exponent value */ +digi_exp_finish: + if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) { + return_f64_bin(0); /* underflow */ + } + if (unlikely(exp_sig > F64_MAX_DEC_EXP)) { + return_inf(); /* overflow */ + } + exp = (i32)exp_sig; + + + /* all digit read finished */ +digi_finish: + + /* + Fast path 1: + + 1. The floating-point number calculation should be accurate, see the + comments of macro `YYJSON_DOUBLE_MATH_CORRECT`. + 2. Correct rounding should be performed (fegetround() == FE_TONEAREST). + 3. The input of floating point number calculation does not lose precision, + which means: 64 - leading_zero(input) - trailing_zero(input) < 53. + + We don't check all available inputs here, because that would make the code + more complicated, and not friendly to branch predictor. + */ +#if YYJSON_DOUBLE_MATH_CORRECT + if (sig < ((u64)1 << 53) && + exp >= -F64_POW10_MAX_EXACT_EXP && + exp <= +F64_POW10_MAX_EXACT_EXP) { + f64 dbl = (f64)sig; + if (exp < 0) { + dbl /= f64_pow10_table[-exp]; + } else { + dbl *= f64_pow10_table[+exp]; + } + return_f64(dbl); + } +#endif + + /* + Fast path 2: + + To keep it simple, we only accept normal number here, + let the slow path to handle subnormal and infinity number. + */ + if (likely(!sig_cut && + exp > -F64_MAX_DEC_EXP + 1 && + exp < +F64_MAX_DEC_EXP - 20)) { + /* + The result value is exactly equal to (sig * 10^exp), + the exponent part (10^exp) can be converted to (sig2 * 2^exp2). + + The sig2 can be an infinite length number, only the highest 128 bits + is cached in the pow10_sig_table. + + Now we have these bits: + sig1 (normalized 64bit) : aaaaaaaa + sig2 (higher 64bit) : bbbbbbbb + sig2_ext (lower 64bit) : cccccccc + sig2_cut (extra unknown bits) : dddddddddddd.... + + And the calculation process is: + ---------------------------------------- + aaaaaaaa * + bbbbbbbbccccccccdddddddddddd.... + ---------------------------------------- + abababababababab + + acacacacacacacac + + adadadadadadadadadad.... + ---------------------------------------- + [hi____][lo____] + + [hi2___][lo2___] + + [unknown___________....] + ---------------------------------------- + + The addition with carry may affect higher bits, but if there is a 0 + in higher bits, the bits higher than 0 will not be affected. + + `lo2` + `unknown` may get a carry bit and may affect `hi2`, the max + value of `hi2` is 0xFFFFFFFFFFFFFFFE, so `hi2` will not overflow. + + `lo` + `hi2` may also get a carry bit and may affect `hi`, but only + the highest significant 53 bits of `hi` is needed. If there is a 0 + in the lower bits of `hi`, then all the following bits can be dropped. + + To convert the result to IEEE-754 double number, we need to perform + correct rounding: + 1. if bit 54 is 0, round down, + 2. if bit 54 is 1 and any bit beyond bit 54 is 1, round up, + 3. if bit 54 is 1 and all bits beyond bit 54 are 0, round to even, + as the extra bits is unknown, this case will not be handled here. + */ + + u64 raw; + u64 sig1, sig2, sig2_ext, hi, lo, hi2, lo2, add, bits; + i32 exp2; + u32 lz; + bool exact = false, carry, round_up; + + /* convert (10^exp) to (sig2 * 2^exp2) */ + pow10_table_get_sig(exp, &sig2, &sig2_ext); + pow10_table_get_exp(exp, &exp2); + + /* normalize and multiply */ + lz = u64_lz_bits(sig); + sig1 = sig << lz; + exp2 -= (i32)lz; + u128_mul(sig1, sig2, &hi, &lo); + + /* + The `hi` is in range [0x4000000000000000, 0xFFFFFFFFFFFFFFFE], + To get normalized value, `hi` should be shifted to the left by 0 or 1. + + The highest significant 53 bits is used by IEEE-754 double number, + and the bit 54 is used to detect rounding direction. + + The lowest (64 - 54 - 1) bits is used to check whether it contains 0. + */ + bits = hi & (((u64)1 << (64 - 54 - 1)) - 1); + if (bits - 1 < (((u64)1 << (64 - 54 - 1)) - 2)) { + /* + (bits != 0 && bits != 0x1FF) => (bits - 1 < 0x1FF - 1) + The `bits` is not zero, so we don't need to check `round to even` + case. The `bits` contains bit `0`, so we can drop the extra bits + after `0`. + */ + exact = true; + + } else { + /* + (bits == 0 || bits == 0x1FF) + The `bits` is filled with all `0` or all `1`, so we need to check + lower bits with another 64-bit multiplication. + */ + u128_mul(sig1, sig2_ext, &hi2, &lo2); + + add = lo + hi2; + if (add + 1 > (u64)1) { + /* + (add != 0 && add != U64_MAX) => (add + 1 > 1) + The `add` is not zero, so we don't need to check `round to + even` case. The `add` contains bit `0`, so we can drop the + extra bits after `0`. The `hi` cannot be U64_MAX, so it will + not overflow. + */ + carry = add < lo || add < hi2; + hi += carry; + exact = true; + } + } + + if (exact) { + /* normalize */ + lz = hi < ((u64)1 << 63); + hi <<= lz; + exp2 -= (i32)lz; + exp2 += 64; + + /* test the bit 54 and get rounding direction */ + round_up = (hi & ((u64)1 << (64 - 54))) > (u64)0; + hi += (round_up ? ((u64)1 << (64 - 54)) : (u64)0); + + /* test overflow */ + if (hi < ((u64)1 << (64 - 54))) { + hi = ((u64)1 << 63); + exp2 += 1; + } + + /* This is a normal number, convert it to IEEE-754 format. */ + hi >>= F64_BITS - F64_SIG_FULL_BITS; + exp2 += F64_BITS - F64_SIG_FULL_BITS + F64_SIG_BITS; + exp2 += F64_EXP_BIAS; + raw = ((u64)exp2 << F64_SIG_BITS) | (hi & F64_SIG_MASK); + return_f64_bin(raw); + } + } + + /* + Slow path: read double number exactly with diyfp. + 1. Use cached diyfp to get an approximation value. + 2. Use bigcomp to check the approximation value if needed. + + This algorithm refers to google's double-conversion project: + https://github.com/google/double-conversion + */ + { + const i32 ERR_ULP_LOG = 3; + const i32 ERR_ULP = 1 << ERR_ULP_LOG; + const i32 ERR_CACHED_POW = ERR_ULP / 2; + const i32 ERR_MUL_FIXED = ERR_ULP / 2; + const i32 DIY_SIG_BITS = 64; + const i32 EXP_BIAS = F64_EXP_BIAS + F64_SIG_BITS; + const i32 EXP_SUBNORMAL = -EXP_BIAS + 1; + + u64 fp_err; + u32 bits; + i32 order_of_magnitude; + i32 effective_significand_size; + i32 precision_digits_count; + u64 precision_bits; + u64 half_way; + + u64 raw; + diy_fp fp, fp_upper; + bigint big_full, big_comp; + i32 cmp; + + fp.sig = sig; + fp.exp = 0; + fp_err = sig_cut ? (u64)(ERR_ULP / 2) : (u64)0; + + /* normalize */ + bits = u64_lz_bits(fp.sig); + fp.sig <<= bits; + fp.exp -= (i32)bits; + fp_err <<= bits; + + /* multiply and add error */ + fp = diy_fp_mul(fp, diy_fp_get_cached_pow10(exp)); + fp_err += (u64)ERR_CACHED_POW + (fp_err != 0) + (u64)ERR_MUL_FIXED; + + /* normalize */ + bits = u64_lz_bits(fp.sig); + fp.sig <<= bits; + fp.exp -= (i32)bits; + fp_err <<= bits; + + /* effective significand */ + order_of_magnitude = DIY_SIG_BITS + fp.exp; + if (likely(order_of_magnitude >= EXP_SUBNORMAL + F64_SIG_FULL_BITS)) { + effective_significand_size = F64_SIG_FULL_BITS; + } else if (order_of_magnitude <= EXP_SUBNORMAL) { + effective_significand_size = 0; + } else { + effective_significand_size = order_of_magnitude - EXP_SUBNORMAL; + } + + /* precision digits count */ + precision_digits_count = DIY_SIG_BITS - effective_significand_size; + if (unlikely(precision_digits_count + ERR_ULP_LOG >= DIY_SIG_BITS)) { + i32 shr = (precision_digits_count + ERR_ULP_LOG) - DIY_SIG_BITS + 1; + fp.sig >>= shr; + fp.exp += shr; + fp_err = (fp_err >> shr) + 1 + (u32)ERR_ULP; + precision_digits_count -= shr; + } + + /* half way */ + precision_bits = fp.sig & (((u64)1 << precision_digits_count) - 1); + precision_bits *= (u32)ERR_ULP; + half_way = (u64)1 << (precision_digits_count - 1); + half_way *= (u32)ERR_ULP; + + /* rounding */ + fp.sig >>= precision_digits_count; + fp.sig += (precision_bits >= half_way + fp_err); + fp.exp += precision_digits_count; + + /* get IEEE double raw value */ + raw = diy_fp_to_ieee_raw(fp); + if (unlikely(raw == F64_BITS_INF)) return_inf(); + if (likely(precision_bits <= half_way - fp_err || + precision_bits >= half_way + fp_err)) { + return_f64_bin(raw); /* number is accurate */ + } + /* now the number is the correct value, or the next lower value */ + + /* upper boundary */ + if (raw & F64_EXP_MASK) { + fp_upper.sig = (raw & F64_SIG_MASK) + ((u64)1 << F64_SIG_BITS); + fp_upper.exp = (i32)((raw & F64_EXP_MASK) >> F64_SIG_BITS); + } else { + fp_upper.sig = (raw & F64_SIG_MASK); + fp_upper.exp = 1; + } + fp_upper.exp -= F64_EXP_BIAS + F64_SIG_BITS; + fp_upper.sig <<= 1; + fp_upper.exp -= 1; + fp_upper.sig += 1; /* add half ulp */ + + /* compare with bigint */ + bigint_set_buf(&big_full, sig, &exp, sig_cut, sig_end, dot_pos); + bigint_set_u64(&big_comp, fp_upper.sig); + if (exp >= 0) { + bigint_mul_pow10(&big_full, +exp); + } else { + bigint_mul_pow10(&big_comp, -exp); + } + if (fp_upper.exp > 0) { + bigint_mul_pow2(&big_comp, (u32)+fp_upper.exp); + } else { + bigint_mul_pow2(&big_full, (u32)-fp_upper.exp); + } + cmp = bigint_cmp(&big_full, &big_comp); + if (likely(cmp != 0)) { + /* round down or round up */ + raw += (cmp > 0); + } else { + /* falls midway, round to even */ + raw += (raw & 1); + } + + if (unlikely(raw == F64_BITS_INF)) return_inf(); + return_f64_bin(raw); + } + +#undef return_err +#undef return_inf +#undef return_0 +#undef return_i64 +#undef return_f64 +#undef return_f64_bin +#undef return_raw +} + + + +#else /* FP_READER */ + +/** + Read a JSON number. + This is a fallback function if the custom number reader is disabled. + This function use libc's strtod() to read floating-point number. + */ +static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, + yyjson_val *val, const char **msg) { +#define return_err(_pos, _msg) do { \ + *msg = _msg; \ + *end = _pos; \ + return false; \ +} while (false) + +#define return_0() do { \ + val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); \ + val->uni.u64 = 0; \ + *end = cur; return true; \ +} while (false) + +#define return_i64(_v) do { \ + val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); \ + val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \ + *end = cur; return true; \ +} while (false) + +#define return_f64(_v) do { \ + val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ + val->uni.f64 = sign ? -(f64)(_v) : (f64)(_v); \ + *end = cur; return true; \ +} while (false) + +#define return_f64_bin(_v) do { \ + val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ + val->uni.u64 = ((u64)sign << 63) | (u64)(_v); \ + *end = cur; return true; \ +} while (false) + +#define return_inf() do { \ + if (has_flg(BIGNUM_AS_RAW)) return_raw(); \ + if (has_allow(INF_AND_NAN)) return_f64_bin(F64_BITS_INF); \ + else return_err(hdr, "number is infinity when parsed as double"); \ +} while (false) + +#define return_raw() do { \ + val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \ + val->uni.str = (const char *)hdr; \ + **pre = '\0'; *pre = cur; *end = cur; return true; \ +} while (false) + + u64 sig, num; + u8 *hdr = *ptr; + u8 *cur = *ptr; + u8 **end = ptr; + u8 *dot = NULL; + u8 *f64_end = NULL; + bool sign; + + /* read number as raw string if has `YYJSON_READ_NUMBER_AS_RAW` flag */ + if (has_flg(NUMBER_AS_RAW)) { + return read_num_raw(ptr, pre, flg, val, msg); + } + + sign = (*hdr == '-'); + cur += sign; + sig = (u8)(*cur - '0'); + + /* read first digit, check leading zero */ + while (unlikely(!char_is_digit(*cur))) { + if (has_allow(EXT_NUMBER)) { + if (*cur == '+' && cur == hdr) { /* leading `+` sign */ + cur++; + sig = (u8)(*cur - '0'); + continue; + } + if (*cur == '.' && char_is_num(cur[1])) { /* no integer part */ + goto read_double; /* e.g. '.123' */ + } + } + if (has_allow(INF_AND_NAN)) { + if (read_inf_or_nan(ptr, pre, flg, val)) return true; + } + return_err(cur, "no digit after sign"); + } + if (*cur == '0') { + cur++; + if (unlikely(char_is_digit(*cur))) { + return_err(cur - 1, "number with leading zero is not allowed"); + } + if (!char_is_fp(*cur)) { + if (has_allow(EXT_NUMBER) && + (*cur == 'x' || *cur == 'X')) { /* hex integer */ + return read_num_hex(ptr, pre, flg, val, msg); + } + return_0(); + } + goto read_double; + } + + /* read continuous digits, up to 19 characters */ +#define expr_intg(i) \ + if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \ + else { cur += i; goto intg_end; } + repeat_in_1_18(expr_intg) +#undef expr_intg + + /* here are 19 continuous digits, skip them */ + cur += 19; + if (char_is_digit(cur[0]) && !char_is_digit_or_fp(cur[1])) { + /* this number is an integer consisting of 20 digits */ + num = (u8)(*cur - '0'); + if ((sig < (U64_MAX / 10)) || + (sig == (U64_MAX / 10) && num <= (U64_MAX % 10))) { + sig = num + sig * 10; + cur++; + if (sign) { + if (has_flg(BIGNUM_AS_RAW)) return_raw(); + return_f64(unsafe_yyjson_u64_to_f64(sig)); + } + return_i64(sig); + } + } + +intg_end: + /* continuous digits ended */ + if (!char_is_digit_or_fp(*cur)) { + /* this number is an integer consisting of 1 to 19 digits */ + if (sign && (sig > ((u64)1 << 63))) { + if (has_flg(BIGNUM_AS_RAW)) return_raw(); + return_f64(unsafe_yyjson_u64_to_f64(sig)); + } + return_i64(sig); + } + +read_double: + /* this number should be read as double */ + while (char_is_digit(*cur)) cur++; + if (!char_is_fp(*cur) && has_flg(BIGNUM_AS_RAW)) { + return_raw(); /* it's a large integer */ + } + while (*cur == '.') { + /* skip fraction part */ + dot = cur; + cur++; + if (!char_is_digit(*cur)) { + if (has_allow(EXT_NUMBER)) { + break; + } else { + return_err(cur, "no digit after decimal point"); + } + } + cur++; + while (char_is_digit(*cur)) cur++; + break; + } + if (char_is_exp(*cur)) { + /* skip exponent part */ + cur += 1 + char_is_sign(cur[1]); + if (!char_is_digit(*cur)) { + return_err(cur, "no digit after exponent sign"); + } + cur++; + while (char_is_digit(*cur)) cur++; + } + + /* + libc's strtod() is used to parse the floating-point number. + + Note that the decimal point character used by strtod() is locale-dependent, + and the rounding direction may affected by fesetround(). + + For currently known locales, (en, zh, ja, ko, am, he, hi) use '.' as the + decimal point, while other locales use ',' as the decimal point. + + Here strtod() is called twice for different locales, but if another thread + happens calls setlocale() between two strtod(), parsing may still fail. + */ + val->uni.f64 = strtod((const char *)hdr, (char **)&f64_end); + if (unlikely(f64_end != cur)) { + /* replace '.' with ',' for locale */ + bool cut = (*cur == ','); + if (cut) *cur = ' '; + if (dot) *dot = ','; + val->uni.f64 = strtod((const char *)hdr, (char **)&f64_end); + /* restore ',' to '.' */ + if (cut) *cur = ','; + if (dot) *dot = '.'; + if (unlikely(f64_end != cur)) { + return_err(hdr, "strtod() failed to parse the number"); + } + } + if (unlikely(val->uni.f64 >= HUGE_VAL || val->uni.f64 <= -HUGE_VAL)) { + return_inf(); + } + val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; + *end = cur; + return true; + +#undef return_err +#undef return_0 +#undef return_i64 +#undef return_f64 +#undef return_f64_bin +#undef return_inf +#undef return_raw +} + +#endif /* FP_READER */ + + + +/*============================================================================== + * MARK: - String Reader (Private) + *============================================================================*/ + +/** Read unicode escape sequence. */ +static_inline bool read_uni_esc(u8 **src_ptr, u8 **dst_ptr, const char **msg) { +#define return_err(_end, _msg) *msg = _msg; *src_ptr = _end; return false + + u8 *src = *src_ptr; + u8 *dst = *dst_ptr; + u16 hi, lo; + u32 uni; + + src += 2; /* skip `\u` */ + if (unlikely(!hex_load_4(src, &hi))) { + return_err(src - 2, "invalid escaped sequence in string"); + } + src += 4; /* skip hex */ + if (likely((hi & 0xF800) != 0xD800)) { + /* a BMP character */ + if (hi >= 0x800) { + *dst++ = (u8)(0xE0 | (hi >> 12)); + *dst++ = (u8)(0x80 | ((hi >> 6) & 0x3F)); + *dst++ = (u8)(0x80 | (hi & 0x3F)); + } else if (hi >= 0x80) { + *dst++ = (u8)(0xC0 | (hi >> 6)); + *dst++ = (u8)(0x80 | (hi & 0x3F)); + } else { + *dst++ = (u8)hi; + } + } else { + /* a non-BMP character, represented as a surrogate pair */ + if (unlikely((hi & 0xFC00) != 0xD800)) { + return_err(src - 6, "invalid high surrogate in string"); + } + if (unlikely(!byte_match_2(src, "\\u"))) { + return_err(src - 6, "no low surrogate in string"); + } + if (unlikely(!hex_load_4(src + 2, &lo))) { + return_err(src - 6, "invalid escape in string"); + } + if (unlikely((lo & 0xFC00) != 0xDC00)) { + return_err(src - 6, "invalid low surrogate in string"); + } + uni = ((((u32)hi - 0xD800) << 10) | + ((u32)lo - 0xDC00)) + 0x10000; + *dst++ = (u8)(0xF0 | (uni >> 18)); + *dst++ = (u8)(0x80 | ((uni >> 12) & 0x3F)); + *dst++ = (u8)(0x80 | ((uni >> 6) & 0x3F)); + *dst++ = (u8)(0x80 | (uni & 0x3F)); + src += 6; + } + *src_ptr = src; + *dst_ptr = dst; + return true; +#undef return_err +} + +/** + Read a JSON string. + @param quo The quote character (single quote or double quote). + @param ptr The head pointer of string before quote (inout). + @param eof JSON end position. + @param flg JSON read flag. + @param val The string value to be written. + @param msg The error message pointer. + @param con Continuation for incremental parsing. + @return Whether success. + */ +static_inline bool read_str_opt(u8 quo, u8 **ptr, u8 *eof, yyjson_read_flag flg, + yyjson_val *val, const char **msg, u8 *con[2]) { + /* + GCC may sometimes load variables into registers too early, causing + unnecessary instructions and performance degradation. This inline assembly + serves as a hint to GCC: 'This variable will be modified, so avoid loading + it too early.' Other compilers like MSVC, Clang, and ICC can generate the + expected instructions without needing this hint. + + Check out this example: https://godbolt.org/z/YG6a5W5Ec + */ +#define return_err(_end, _msg) do { \ + *msg = _msg; \ + *end = _end; \ + if (con) { con[0] = _end; con[1] = dst; } \ + return false; \ +} while (false) + + u8 *hdr = *ptr + 1; + u8 **end = ptr; + u8 *src = hdr, *dst = NULL, *pos; + u16 hi, lo; + u32 uni, tmp; + + /* Resume incremental parsing. */ + if (con && unlikely(con[0])) { + src = con[0]; + dst = con[1]; + if (dst) goto copy_ascii; + } + +skip_ascii: + /* + Most strings have no escaped characters, so we can jump them quickly. + + We want to make loop unrolling, as shown in the following code. Some + compiler may not generate instructions as expected, so we rewrite it with + explicit goto statements. We hope the compiler can generate instructions + like this: https://godbolt.org/z/8vjsYq + + while (true) repeat16({ + if (likely((char_is_ascii_skip(*src)))) src++; + else break; + }) + */ + if (quo == '"') { +#define expr_jump(i) \ + if (likely(char_is_ascii_skip(src[i]))) {} \ + else goto skip_ascii_stop##i; + +#define expr_stop(i) \ + skip_ascii_stop##i: \ + src += i; \ + goto skip_ascii_end; + + repeat16_incr(expr_jump) + src += 16; + goto skip_ascii; + repeat16_incr(expr_stop) + +#undef expr_jump +#undef expr_stop + } else { +#define expr_jump(i) \ + if (likely(char_is_ascii_skip_sq(src[i]))) {} \ + else goto skip_ascii_stop_sq##i; + +#define expr_stop(i) \ + skip_ascii_stop_sq##i: \ + src += i; \ + goto skip_ascii_end; + + repeat16_incr(expr_jump) + src += 16; + goto skip_ascii; + repeat16_incr(expr_stop) + +#undef expr_jump +#undef expr_stop + } + +skip_ascii_end: + gcc_store_barrier(*src); + if (likely(*src == quo)) { + val->tag = ((u64)(src - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR | + (quo == '"' ? YYJSON_SUBTYPE_NOESC : 0); + val->uni.str = (const char *)hdr; + *src = '\0'; + *end = src + 1; + if (con) con[0] = con[1] = NULL; + return true; + } + +skip_utf8: + if (*src & 0x80) { /* non-ASCII character */ + /* + Non-ASCII character appears here, which means that the text is likely + to be written in non-English or emoticons. According to some common + data set statistics, byte sequences of the same length may appear + consecutively. We process the byte sequences of the same length in each + loop, which is more friendly to branch prediction. + */ + pos = src; +#if YYJSON_DISABLE_UTF8_VALIDATION + while (true) repeat8({ + if (likely((*src & 0xF0) == 0xE0)) src += 3; + else break; + }) + if (*src < 0x80) goto skip_ascii; + while (true) repeat8({ + if (likely((*src & 0xE0) == 0xC0)) src += 2; + else break; + }) + while (true) repeat8({ + if (likely((*src & 0xF8) == 0xF0)) src += 4; + else break; + }) +#else + uni = byte_load_4(src); + while (is_utf8_seq3(uni)) { + src += 3; + uni = byte_load_4(src); + } + if (is_utf8_seq1(uni)) goto skip_ascii; + while (is_utf8_seq2(uni)) { + src += 2; + uni = byte_load_4(src); + } + while (is_utf8_seq4(uni)) { + src += 4; + uni = byte_load_4(src); + } +#endif + if (unlikely(pos == src)) { + if (has_allow(INVALID_UNICODE)) ++src; + else return_err(src, "invalid UTF-8 encoding in string"); + } + goto skip_ascii; + } + + /* The escape character appears, we need to copy it. */ + dst = src; +copy_escape: + if (likely(*src == '\\')) { + switch (*++src) { + case '"': *dst++ = '"'; src++; break; + case '\\': *dst++ = '\\'; src++; break; + case '/': *dst++ = '/'; src++; break; + case 'b': *dst++ = '\b'; src++; break; + case 'f': *dst++ = '\f'; src++; break; + case 'n': *dst++ = '\n'; src++; break; + case 'r': *dst++ = '\r'; src++; break; + case 't': *dst++ = '\t'; src++; break; + case 'u': + src--; + if (!read_uni_esc(&src, &dst, msg)) return_err(src, *msg); + break; + default: { + if (has_allow(EXT_ESCAPE)) { + /* read extended escape (non-standard) */ + switch (*src) { + case '\'': *dst++ = '\''; src++; break; + case 'a': *dst++ = '\a'; src++; break; + case 'v': *dst++ = '\v'; src++; break; + case '?': *dst++ = '\?'; src++; break; + case 'e': *dst++ = 0x1B; src++; break; + case '0': + if (!char_is_digit(src[1])) { + *dst++ = '\0'; src++; break; + } + return_err(src - 1, "octal escape is not allowed"); + case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + return_err(src - 1, "invalid number escape"); + case 'x': { + u8 c; + if (hex_load_2(src + 1, &c)) { + src += 3; + if (c <= 0x7F) { /* 1-byte ASCII */ + *dst++ = c; + } else { /* 2-byte UTF-8 */ + *dst++ = (u8)(0xC0 | (c >> 6)); + *dst++ = (u8)(0x80 | (c & 0x3F)); + } + break; + } + return_err(src - 1, "invalid hex escape"); + } + case '\n': src++; break; + case '\r': src++; src += (*src == '\n'); break; + case 0xE2: /* Line terminator: U+2028, U+2029 */ + if ((src[1] == 0x80 && src[2] == 0xA8) || + (src[1] == 0x80 && src[2] == 0xA9)) { + src += 3; + } + break; + default: + break; /* skip */ + } + } else if (quo == '\'' && *src == '\'') { + *dst++ = '\''; src++; break; + } else { + return_err(src - 1, "invalid escaped sequence in string"); + } + } + } + } else if (likely(*src == quo)) { + val->tag = ((u64)(dst - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + val->uni.str = (const char *)hdr; + *dst = '\0'; + *end = src + 1; + if (con) con[0] = con[1] = NULL; + return true; + } else { + if (!has_allow(INVALID_UNICODE)) { + return_err(src, "unexpected control character in string"); + } + if (src >= eof) return_err(src, "unclosed string"); + *dst++ = *src++; + } + +copy_ascii: + /* + Copy continuous ASCII, loop unrolling, same as the following code: + + while (true) repeat16({ + if (char_is_ascii_skip(*src)) *dst++ = *src++; + else break; + }) + */ + if (quo == '"') { +#define expr_jump(i) \ + if (likely((char_is_ascii_skip(src[i])))) {} \ + else { gcc_store_barrier(src[i]); goto copy_ascii_stop_##i; } + repeat16_incr(expr_jump) +#undef expr_jump + } else { +#define expr_jump(i) \ + if (likely((char_is_ascii_skip_sq(src[i])))) {} \ + else { gcc_store_barrier(src[i]); goto copy_ascii_stop_##i; } + repeat16_incr(expr_jump) +#undef expr_jump + } + + byte_move_16(dst, src); + dst += 16; src += 16; + goto copy_ascii; + + /* + The memory is copied forward since `dst < src`. + So it's safe to move one extra byte to reduce instruction count. + */ +#define expr_jump(i) \ + copy_ascii_stop_##i: \ + byte_move_forward(dst, src, i); \ + dst += i; src += i; \ + goto copy_utf8; + repeat16_incr(expr_jump) +#undef expr_jump + +copy_utf8: + if (*src & 0x80) { /* non-ASCII character */ + pos = src; + uni = byte_load_4(src); +#if YYJSON_DISABLE_UTF8_VALIDATION + while (true) repeat4({ + if ((uni & utf8_seq(b3_mask)) == utf8_seq(b3_patt)) { + byte_copy_4(dst, &uni); + dst += 3; src += 3; + uni = byte_load_4(src); + } else break; + }) + if ((uni & utf8_seq(b1_mask)) == utf8_seq(b1_patt)) goto copy_ascii; + while (true) repeat4({ + if ((uni & utf8_seq(b2_mask)) == utf8_seq(b2_patt)) { + byte_copy_2(dst, &uni); + dst += 2; src += 2; + uni = byte_load_4(src); + } else break; + }) + while (true) repeat4({ + if ((uni & utf8_seq(b4_mask)) == utf8_seq(b4_patt)) { + byte_copy_4(dst, &uni); + dst += 4; src += 4; + uni = byte_load_4(src); + } else break; + }) +#else + while (is_utf8_seq3(uni)) { + byte_copy_4(dst, &uni); + dst += 3; src += 3; + uni = byte_load_4(src); + } + if (is_utf8_seq1(uni)) goto copy_ascii; + while (is_utf8_seq2(uni)) { + byte_copy_2(dst, &uni); + dst += 2; src += 2; + uni = byte_load_4(src); + } + while (is_utf8_seq4(uni)) { + byte_copy_4(dst, &uni); + dst += 4; src += 4; + uni = byte_load_4(src); + } +#endif + if (unlikely(pos == src)) { + if (!has_allow(INVALID_UNICODE)) { + return_err(src, MSG_ERR_UTF8); + } + goto copy_ascii_stop_1; + } + goto copy_ascii; + } + goto copy_escape; + +#undef return_err +} + +static_inline bool read_str(u8 **ptr, u8 *eof, yyjson_read_flag flg, + yyjson_val *val, const char **msg) { + return read_str_opt('\"', ptr, eof, flg, val, msg, NULL); +} + +static_inline bool read_str_con(u8 **ptr, u8 *eof, yyjson_read_flag flg, + yyjson_val *val, const char **msg, u8 **con) { + return read_str_opt('\"', ptr, eof, flg, val, msg, con); +} + +static_noinline bool read_str_sq(u8 **ptr, u8 *eof, yyjson_read_flag flg, + yyjson_val *val, const char **msg) { + return read_str_opt('\'', ptr, eof, flg, val, msg, NULL); +} + +/** Read unquoted key (identifier name). */ +static_noinline bool read_str_id(u8 **ptr, u8 *eof, yyjson_read_flag flg, + u8 **pre, yyjson_val *val, const char **msg) { +#define return_err(_end, _msg) do { \ + *msg = _msg; \ + *end = _end; \ + return false; \ +} while (false) + +#define return_suc(_str_end, _cur_end) do { \ + val->tag = ((u64)(_str_end - hdr) << YYJSON_TAG_BIT) | \ + (u64)(YYJSON_TYPE_STR); \ + val->uni.str = (const char *)hdr; \ + *pre = _str_end; *end = _cur_end; \ + return true; \ +} while (false) + + u8 *hdr = *ptr; + u8 **end = ptr; + u8 *src = hdr, *dst = NULL; + u16 hi, lo; + u32 uni, tmp; + + /* add null-terminator for previous raw string */ + **pre = '\0'; + +skip_ascii: +#define expr_jump(i) \ + if (likely(char_is_id_ascii(src[i]))) {} \ + else goto skip_ascii_stop##i; + +#define expr_stop(i) \ + skip_ascii_stop##i: \ + src += i; \ + goto skip_ascii_end; + + repeat16_incr(expr_jump) + src += 16; + goto skip_ascii; + repeat16_incr(expr_stop) + +#undef expr_jump +#undef expr_stop + +skip_ascii_end: + gcc_store_barrier(*src); + if (likely(!char_is_id_next(*src))) { + return_suc(src, src); + } + +skip_utf8: + while (*src >= 0x80) { + if (has_allow(EXT_WHITESPACE)) { + if (char_is_space_ext(*src) && ext_space_len(src)) { + return_suc(src, src); + } + } + uni = byte_load_4(src); + if (is_utf8_seq2(uni)) { + src += 2; + } else if (is_utf8_seq3(uni)) { + src += 3; + } else if (is_utf8_seq4(uni)) { + src += 4; + } else { +#if !YYJSON_DISABLE_UTF8_VALIDATION + if (!has_allow(INVALID_UNICODE)) return_err(src, MSG_ERR_UTF8); +#endif + src += 1; + } + } + if (char_is_id_ascii(*src)) goto skip_ascii; + + /* The escape character appears, we need to copy it. */ + dst = src; +copy_escape: + if (byte_match_2(src, "\\u")) { + if (!read_uni_esc(&src, &dst, msg)) return_err(src, *msg); + } else { + if (!char_is_id_next(*src)) return_suc(dst, src); + return_err(src, "unexpected character in key"); + } + +copy_ascii: + /* + Copy continuous ASCII, loop unrolling, same as the following code: + + while (true) repeat16({ + if (char_is_ascii_skip(*src)) *dst++ = *src++; + else break; + }) + */ +#define expr_jump(i) \ + if (likely((char_is_id_ascii(src[i])))) {} \ + else { gcc_store_barrier(src[i]); goto copy_ascii_stop_##i; } + repeat16_incr(expr_jump) +#undef expr_jump + + byte_move_16(dst, src); + dst += 16; src += 16; + goto copy_ascii; + +#define expr_jump(i) \ + copy_ascii_stop_##i: \ + byte_move_forward(dst, src, i); \ + dst += i; src += i; \ + goto copy_utf8; + repeat16_incr(expr_jump) +#undef expr_jump + +copy_utf8: + while (*src >= 0x80) { /* non-ASCII character */ + if (has_allow(EXT_WHITESPACE)) { + if (char_is_space_ext(*src) && ext_space_len(src)) { + return_suc(dst, src); + } + } + uni = byte_load_4(src); + if (is_utf8_seq2(uni)) { + byte_copy_2(dst, &uni); + dst += 2; src += 2; + } else if (is_utf8_seq3(uni)) { + byte_copy_4(dst, &uni); + dst += 3; src += 3; + } else if (is_utf8_seq4(uni)) { + byte_copy_4(dst, &uni); + dst += 4; src += 4; + } else { +#if !YYJSON_DISABLE_UTF8_VALIDATION + if (!has_allow(INVALID_UNICODE)) return_err(src, MSG_ERR_UTF8); +#endif + *dst = *src; + dst += 1; src += 1; + } + } + if (char_is_id_ascii(*src)) goto copy_ascii; + goto copy_escape; + +#undef return_err +#undef return_suc +} + + + +/*============================================================================== + * MARK: - JSON Reader Implementation (Private) + * + * We use goto statements to build the finite state machine (FSM). + * The FSM's state was held by program counter (PC) and the 'goto' make the + * state transitions. + *============================================================================*/ + +/** Read single value JSON document. */ +static_noinline yyjson_doc *read_root_single(u8 *hdr, u8 *cur, u8 *eof, + yyjson_alc alc, + yyjson_read_flag flg, + yyjson_read_err *err) { +#define return_err(_pos, _code, _msg) do { \ + if (is_truncated_end(hdr, _pos, eof, YYJSON_READ_ERROR_##_code, flg)) { \ + err->pos = (usize)(eof - hdr); \ + err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ + err->msg = MSG_NOT_END; \ + } else { \ + err->pos = (usize)(_pos - hdr); \ + err->code = YYJSON_READ_ERROR_##_code; \ + err->msg = _msg; \ + } \ + if (val_hdr) alc.free(alc.ctx, val_hdr); \ + return NULL; \ +} while (false) + + usize hdr_len; /* value count used by doc */ + usize alc_num; /* value count capacity */ + yyjson_val *val_hdr; /* the head of allocated values */ + yyjson_val *val; /* current value */ + yyjson_doc *doc; /* the JSON document, equals to val_hdr */ + const char *msg; /* error message */ + + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ + + hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); + hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; + alc_num = hdr_len + 1; /* single value */ + + val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_num * sizeof(yyjson_val)); + if (unlikely(!val_hdr)) goto fail_alloc; + val = val_hdr + hdr_len; + + if (char_is_num(*cur)) { + if (likely(read_num(&cur, pre, flg, val, &msg))) goto doc_end; + goto fail_number; + } + if (*cur == '"') { + if (likely(read_str(&cur, eof, flg, val, &msg))) goto doc_end; + goto fail_string; + } + if (*cur == 't') { + if (likely(read_true(&cur, val))) goto doc_end; + goto fail_literal_true; + } + if (*cur == 'f') { + if (likely(read_false(&cur, val))) goto doc_end; + goto fail_literal_false; + } + if (*cur == 'n') { + if (likely(read_null(&cur, val))) goto doc_end; + if (has_allow(INF_AND_NAN)) { + if (read_nan(&cur, pre, flg, val)) goto doc_end; + } + goto fail_literal_null; + } + if (has_allow(INF_AND_NAN)) { + if (read_inf_or_nan(&cur, pre, flg, val)) goto doc_end; + } + if (has_allow(SINGLE_QUOTED_STR) && *cur == '\'') { + if (likely(read_str_sq(&cur, eof, flg, val, &msg))) goto doc_end; + goto fail_string; + } + goto fail_character; + +doc_end: + /* check invalid contents after json document */ + if (unlikely(cur < eof) && !has_flg(STOP_WHEN_DONE)) { + while (char_is_space(*cur)) cur++; + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (!skip_trivia(&cur, eof, flg) && cur == eof) { + goto fail_comment; + } + } + if (unlikely(cur < eof)) goto fail_garbage; + } + + **pre = '\0'; + doc = (yyjson_doc *)val_hdr; + doc->root = val_hdr + hdr_len; + doc->alc = alc; + doc->dat_read = (usize)(cur - hdr); + doc->val_read = 1; + doc->str_pool = has_flg(INSITU) ? NULL : (char *)hdr; + return doc; + +fail_string: return_err(cur, INVALID_STRING, msg); +fail_number: return_err(cur, INVALID_NUMBER, msg); +fail_alloc: return_err(cur, MEMORY_ALLOCATION, MSG_MALLOC); +fail_literal_true: return_err(cur, LITERAL, MSG_CHAR_T); +fail_literal_false: return_err(cur, LITERAL, MSG_CHAR_F); +fail_literal_null: return_err(cur, LITERAL, MSG_CHAR_N); +fail_character: return_err(cur, UNEXPECTED_CHARACTER, MSG_CHAR); +fail_comment: return_err(cur, INVALID_COMMENT, MSG_COMMENT); +fail_garbage: return_err(cur, UNEXPECTED_CONTENT, MSG_GARBAGE); + +#undef return_err +} + +/** Read JSON document (accept all style, but optimized for minify). */ +static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *eof, + yyjson_alc alc, + yyjson_read_flag flg, + yyjson_read_err *err) { +#define return_err(_pos, _code, _msg) do { \ + if (is_truncated_end(hdr, _pos, eof, YYJSON_READ_ERROR_##_code, flg)) { \ + err->pos = (usize)(eof - hdr); \ + err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ + err->msg = MSG_NOT_END; \ + } else { \ + err->pos = (usize)(_pos - hdr); \ + err->code = YYJSON_READ_ERROR_##_code; \ + err->msg = _msg; \ + } \ + if (val_hdr) alc.free(alc.ctx, val_hdr); \ + return NULL; \ +} while (false) + +#define val_incr() do { \ + val++; \ + if (unlikely(val >= val_end)) { \ + usize alc_old = alc_len; \ + usize val_ofs = (usize)(val - val_hdr); \ + usize ctn_ofs = (usize)(ctn - val_hdr); \ + alc_len += alc_len / 2; \ + if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ + val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ + alc_old * sizeof(yyjson_val), \ + alc_len * sizeof(yyjson_val)); \ + if ((!val_tmp)) goto fail_alloc; \ + val = val_tmp + val_ofs; \ + ctn = val_tmp + ctn_ofs; \ + val_hdr = val_tmp; \ + val_end = val_tmp + (alc_len - 2); \ + } \ +} while (false) + + usize dat_len; /* data length in bytes, hint for allocator */ + usize hdr_len; /* value count used by yyjson_doc */ + usize alc_len; /* value count allocated */ + usize alc_max; /* maximum value count for allocator */ + usize ctn_len; /* the number of elements in current container */ + yyjson_val *val_hdr; /* the head of allocated values */ + yyjson_val *val_end; /* the end of allocated values */ + yyjson_val *val_tmp; /* temporary pointer for realloc */ + yyjson_val *val; /* current JSON value */ + yyjson_val *ctn; /* current container */ + yyjson_val *ctn_parent; /* parent of current container */ + yyjson_doc *doc; /* the JSON document, equals to val_hdr */ + const char *msg; /* error message */ + + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ + + dat_len = has_flg(STOP_WHEN_DONE) ? 256 : (usize)(eof - cur); + hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); + hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; + alc_max = USIZE_MAX / sizeof(yyjson_val); + alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_MINIFY_RATIO) + 4; + alc_len = yyjson_min(alc_len, alc_max); + + val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val)); + if (unlikely(!val_hdr)) goto fail_alloc; + val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */ + val = val_hdr + hdr_len; + ctn = val; + ctn_len = 0; + + if (*cur++ == '{') { + ctn->tag = YYJSON_TYPE_OBJ; + ctn->uni.ofs = 0; + goto obj_key_begin; + } else { + ctn->tag = YYJSON_TYPE_ARR; + ctn->uni.ofs = 0; + goto arr_val_begin; + } + +arr_begin: + /* save current container */ + ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | + (ctn->tag & YYJSON_TAG_MASK); + + /* create a new array value, save parent container offset */ + val_incr(); + val->tag = YYJSON_TYPE_ARR; + val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); + + /* push the new array value as current container */ + ctn = val; + ctn_len = 0; + +arr_val_begin: + if (*cur == '{') { + cur++; + goto obj_begin; + } + if (*cur == '[') { + cur++; + goto arr_begin; + } + if (char_is_num(*cur)) { + val_incr(); + ctn_len++; + if (likely(read_num(&cur, pre, flg, val, &msg))) goto arr_val_end; + goto fail_number; + } + if (*cur == '"') { + val_incr(); + ctn_len++; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto arr_val_end; + goto fail_string; + } + if (*cur == 't') { + val_incr(); + ctn_len++; + if (likely(read_true(&cur, val))) goto arr_val_end; + goto fail_literal_true; + } + if (*cur == 'f') { + val_incr(); + ctn_len++; + if (likely(read_false(&cur, val))) goto arr_val_end; + goto fail_literal_false; + } + if (*cur == 'n') { + val_incr(); + ctn_len++; + if (likely(read_null(&cur, val))) goto arr_val_end; + if (has_allow(INF_AND_NAN)) { + if (read_nan(&cur, pre, flg, val)) goto arr_val_end; + } + goto fail_literal_null; + } + if (*cur == ']') { + cur++; + if (likely(ctn_len == 0)) goto arr_end; + if (has_allow(TRAILING_COMMAS)) goto arr_end; + while (*cur != ',') cur--; + goto fail_trailing_comma; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto arr_val_begin; + } + if (has_allow(INF_AND_NAN) && + (*cur == 'i' || *cur == 'I' || *cur == 'N')) { + val_incr(); + ctn_len++; + if (read_inf_or_nan(&cur, pre, flg, val)) goto arr_val_end; + goto fail_character_val; + } + if (has_allow(SINGLE_QUOTED_STR) && *cur == '\'') { + val_incr(); + ctn_len++; + if (likely(read_str_sq(&cur, eof, flg, val, &msg))) goto arr_val_end; + goto fail_string; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto arr_val_begin; + if (cur == eof) goto fail_comment; + } + goto fail_character_val; + +arr_val_end: + if (*cur == ',') { + cur++; + goto arr_val_begin; + } + if (*cur == ']') { + cur++; + goto arr_end; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto arr_val_end; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto arr_val_end; + if (cur == eof) goto fail_comment; + } + goto fail_character_arr_end; + +arr_end: + /* get parent container */ + ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); + + /* save the next sibling value offset */ + ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); + ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; + if (unlikely(ctn == ctn_parent)) goto doc_end; + + /* pop parent as current container */ + ctn = ctn_parent; + ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); + if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { + goto obj_val_end; + } else { + goto arr_val_end; + } + +obj_begin: + /* push container */ + ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | + (ctn->tag & YYJSON_TAG_MASK); + val_incr(); + val->tag = YYJSON_TYPE_OBJ; + /* offset to the parent */ + val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); + ctn = val; + ctn_len = 0; + +obj_key_begin: + if (likely(*cur == '"')) { + val_incr(); + ctn_len++; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto obj_key_end; + goto fail_string; + } + if (likely(*cur == '}')) { + cur++; + if (likely(ctn_len == 0)) goto obj_end; + if (has_allow(TRAILING_COMMAS)) goto obj_end; + while (*cur != ',') cur--; + goto fail_trailing_comma; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_key_begin; + } + if (has_allow(SINGLE_QUOTED_STR) && *cur == '\'') { + val_incr(); + ctn_len++; + if (likely(read_str_sq(&cur, eof, flg, val, &msg))) goto obj_key_end; + goto fail_string; + } + if (has_allow(UNQUOTED_KEY) && char_is_id_start(*cur)) { + val_incr(); + ctn_len++; + if (read_str_id(&cur, eof, flg, pre, val, &msg)) goto obj_key_end; + goto fail_string; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_key_begin; + if (cur == eof) goto fail_comment; + } + goto fail_character_obj_key; + +obj_key_end: + if (*cur == ':') { + cur++; + goto obj_val_begin; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_key_end; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_key_end; + if (cur == eof) goto fail_comment; + } + goto fail_character_obj_sep; + +obj_val_begin: + if (*cur == '"') { + val++; + ctn_len++; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto obj_val_end; + goto fail_string; + } + if (char_is_num(*cur)) { + val++; + ctn_len++; + if (likely(read_num(&cur, pre, flg, val, &msg))) goto obj_val_end; + goto fail_number; + } + if (*cur == '{') { + cur++; + goto obj_begin; + } + if (*cur == '[') { + cur++; + goto arr_begin; + } + if (*cur == 't') { + val++; + ctn_len++; + if (likely(read_true(&cur, val))) goto obj_val_end; + goto fail_literal_true; + } + if (*cur == 'f') { + val++; + ctn_len++; + if (likely(read_false(&cur, val))) goto obj_val_end; + goto fail_literal_false; + } + if (*cur == 'n') { + val++; + ctn_len++; + if (likely(read_null(&cur, val))) goto obj_val_end; + if (has_allow(INF_AND_NAN)) { + if (read_nan(&cur, pre, flg, val)) goto obj_val_end; + } + goto fail_literal_null; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_val_begin; + } + if (has_allow(INF_AND_NAN) && + (*cur == 'i' || *cur == 'I' || *cur == 'N')) { + val++; + ctn_len++; + if (read_inf_or_nan(&cur, pre, flg, val)) goto obj_val_end; + goto fail_character_val; + } + if (has_allow(SINGLE_QUOTED_STR) && *cur == '\'') { + val++; + ctn_len++; + if (likely(read_str_sq(&cur, eof, flg, val, &msg))) goto obj_val_end; + goto fail_string; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_val_begin; + if (cur == eof) goto fail_comment; + } + goto fail_character_val; + +obj_val_end: + if (likely(*cur == ',')) { + cur++; + goto obj_key_begin; + } + if (likely(*cur == '}')) { + cur++; + goto obj_end; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_val_end; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_val_end; + if (cur == eof) goto fail_comment; + } + goto fail_character_obj_end; + +obj_end: + /* pop container */ + ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); + /* point to the next value */ + ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); + ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ; + if (unlikely(ctn == ctn_parent)) goto doc_end; + ctn = ctn_parent; + ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); + if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { + goto obj_val_end; + } else { + goto arr_val_end; + } + +doc_end: + /* check invalid contents after json document */ + if (unlikely(cur < eof) && !has_flg(STOP_WHEN_DONE)) { + while (char_is_space(*cur)) cur++; + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (!skip_trivia(&cur, eof, flg) && cur == eof) { + goto fail_comment; + } + } + if (unlikely(cur < eof)) goto fail_garbage; + } + + **pre = '\0'; + doc = (yyjson_doc *)val_hdr; + doc->root = val_hdr + hdr_len; + doc->alc = alc; + doc->dat_read = (usize)(cur - hdr); + doc->val_read = (usize)((val - doc->root) + 1); + doc->str_pool = has_flg(INSITU) ? NULL : (char *)hdr; + return doc; + +fail_string: return_err(cur, INVALID_STRING, msg); +fail_number: return_err(cur, INVALID_NUMBER, msg); +fail_alloc: return_err(cur, MEMORY_ALLOCATION, MSG_MALLOC); +fail_trailing_comma: return_err(cur, JSON_STRUCTURE, MSG_COMMA); +fail_literal_true: return_err(cur, LITERAL, MSG_CHAR_T); +fail_literal_false: return_err(cur, LITERAL, MSG_CHAR_F); +fail_literal_null: return_err(cur, LITERAL, MSG_CHAR_N); +fail_character_val: return_err(cur, UNEXPECTED_CHARACTER, MSG_CHAR); +fail_character_arr_end: return_err(cur, UNEXPECTED_CHARACTER, MSG_ARR_END); +fail_character_obj_key: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_KEY); +fail_character_obj_sep: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_SEP); +fail_character_obj_end: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_END); +fail_comment: return_err(cur, INVALID_COMMENT, MSG_COMMENT); +fail_garbage: return_err(cur, UNEXPECTED_CONTENT, MSG_GARBAGE); + +#undef val_incr +#undef return_err +} + +/** Read JSON document (accept all style, but optimized for pretty). */ +static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *eof, + yyjson_alc alc, + yyjson_read_flag flg, + yyjson_read_err *err) { +#define return_err(_pos, _code, _msg) do { \ + if (is_truncated_end(hdr, _pos, eof, YYJSON_READ_ERROR_##_code, flg)) { \ + err->pos = (usize)(eof - hdr); \ + err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ + err->msg = MSG_NOT_END; \ + } else { \ + err->pos = (usize)(_pos - hdr); \ + err->code = YYJSON_READ_ERROR_##_code; \ + err->msg = _msg; \ + } \ + if (val_hdr) alc.free(alc.ctx, val_hdr); \ + return NULL; \ +} while (false) + +#define val_incr() do { \ + val++; \ + if (unlikely(val >= val_end)) { \ + usize alc_old = alc_len; \ + usize val_ofs = (usize)(val - val_hdr); \ + usize ctn_ofs = (usize)(ctn - val_hdr); \ + alc_len += alc_len / 2; \ + if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ + val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ + alc_old * sizeof(yyjson_val), \ + alc_len * sizeof(yyjson_val)); \ + if ((!val_tmp)) goto fail_alloc; \ + val = val_tmp + val_ofs; \ + ctn = val_tmp + ctn_ofs; \ + val_hdr = val_tmp; \ + val_end = val_tmp + (alc_len - 2); \ + } \ +} while (false) + + usize dat_len; /* data length in bytes, hint for allocator */ + usize hdr_len; /* value count used by yyjson_doc */ + usize alc_len; /* value count allocated */ + usize alc_max; /* maximum value count for allocator */ + usize ctn_len; /* the number of elements in current container */ + yyjson_val *val_hdr; /* the head of allocated values */ + yyjson_val *val_end; /* the end of allocated values */ + yyjson_val *val_tmp; /* temporary pointer for realloc */ + yyjson_val *val; /* current JSON value */ + yyjson_val *ctn; /* current container */ + yyjson_val *ctn_parent; /* parent of current container */ + yyjson_doc *doc; /* the JSON document, equals to val_hdr */ + const char *msg; /* error message */ + + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ + + dat_len = has_flg(STOP_WHEN_DONE) ? 256 : (usize)(eof - cur); + hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); + hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; + alc_max = USIZE_MAX / sizeof(yyjson_val); + alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_PRETTY_RATIO) + 4; + alc_len = yyjson_min(alc_len, alc_max); + + val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val)); + if (unlikely(!val_hdr)) goto fail_alloc; + val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */ + val = val_hdr + hdr_len; + ctn = val; + ctn_len = 0; + + if (*cur++ == '{') { + ctn->tag = YYJSON_TYPE_OBJ; + ctn->uni.ofs = 0; + if (*cur == '\n') cur++; + goto obj_key_begin; + } else { + ctn->tag = YYJSON_TYPE_ARR; + ctn->uni.ofs = 0; + if (*cur == '\n') cur++; + goto arr_val_begin; + } + +arr_begin: + /* save current container */ + ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | + (ctn->tag & YYJSON_TAG_MASK); + + /* create a new array value, save parent container offset */ + val_incr(); + val->tag = YYJSON_TYPE_ARR; + val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); + + /* push the new array value as current container */ + ctn = val; + ctn_len = 0; + if (*cur == '\n') cur++; + +arr_val_begin: +#if YYJSON_IS_REAL_GCC + while (true) repeat16({ + if (byte_match_2(cur, " ")) cur += 2; + else break; + }) +#else + while (true) repeat16({ + if (likely(byte_match_2(cur, " "))) cur += 2; + else break; + }) +#endif + + if (*cur == '{') { + cur++; + goto obj_begin; + } + if (*cur == '[') { + cur++; + goto arr_begin; + } + if (char_is_num(*cur)) { + val_incr(); + ctn_len++; + if (likely(read_num(&cur, pre, flg, val, &msg))) goto arr_val_end; + goto fail_number; + } + if (*cur == '"') { + val_incr(); + ctn_len++; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto arr_val_end; + goto fail_string; + } + if (*cur == 't') { + val_incr(); + ctn_len++; + if (likely(read_true(&cur, val))) goto arr_val_end; + goto fail_literal_true; + } + if (*cur == 'f') { + val_incr(); + ctn_len++; + if (likely(read_false(&cur, val))) goto arr_val_end; + goto fail_literal_false; + } + if (*cur == 'n') { + val_incr(); + ctn_len++; + if (likely(read_null(&cur, val))) goto arr_val_end; + if (has_allow(INF_AND_NAN)) { + if (read_nan(&cur, pre, flg, val)) goto arr_val_end; + } + goto fail_literal_null; + } + if (*cur == ']') { + cur++; + if (likely(ctn_len == 0)) goto arr_end; + if (has_allow(TRAILING_COMMAS)) goto arr_end; + while (*cur != ',') cur--; + goto fail_trailing_comma; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto arr_val_begin; + } + if (has_allow(INF_AND_NAN) && + (*cur == 'i' || *cur == 'I' || *cur == 'N')) { + val_incr(); + ctn_len++; + if (read_inf_or_nan(&cur, pre, flg, val)) goto arr_val_end; + goto fail_character_val; + } + if (has_allow(SINGLE_QUOTED_STR) && *cur == '\'') { + val_incr(); + ctn_len++; + if (likely(read_str_sq(&cur, eof, flg, val, &msg))) goto arr_val_end; + goto fail_string; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto arr_val_begin; + if (cur == eof) goto fail_comment; + } + goto fail_character_val; + +arr_val_end: + if (byte_match_2(cur, ",\n")) { + cur += 2; + goto arr_val_begin; + } + if (*cur == ',') { + cur++; + goto arr_val_begin; + } + if (*cur == ']') { + cur++; + goto arr_end; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto arr_val_end; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto arr_val_end; + if (cur == eof) goto fail_comment; + } + goto fail_character_arr_end; + +arr_end: + /* get parent container */ + ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); + + /* save the next sibling value offset */ + ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); + ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; + if (unlikely(ctn == ctn_parent)) goto doc_end; + + /* pop parent as current container */ + ctn = ctn_parent; + ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); + if (*cur == '\n') cur++; + if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { + goto obj_val_end; + } else { + goto arr_val_end; + } + +obj_begin: + /* push container */ + ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | + (ctn->tag & YYJSON_TAG_MASK); + val_incr(); + val->tag = YYJSON_TYPE_OBJ; + /* offset to the parent */ + val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); + ctn = val; + ctn_len = 0; + if (*cur == '\n') cur++; + +obj_key_begin: +#if YYJSON_IS_REAL_GCC + while (true) repeat16({ + if (byte_match_2(cur, " ")) cur += 2; + else break; + }) +#else + while (true) repeat16({ + if (likely(byte_match_2(cur, " "))) cur += 2; + else break; + }) +#endif + if (likely(*cur == '"')) { + val_incr(); + ctn_len++; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto obj_key_end; + goto fail_string; + } + if (likely(*cur == '}')) { + cur++; + if (likely(ctn_len == 0)) goto obj_end; + if (has_allow(TRAILING_COMMAS)) goto obj_end; + while (*cur != ',') cur--; + goto fail_trailing_comma; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_key_begin; + } + if (has_allow(SINGLE_QUOTED_STR) && *cur == '\'') { + val_incr(); + ctn_len++; + if (likely(read_str_sq(&cur, eof, flg, val, &msg))) goto obj_key_end; + goto fail_string; + } + if (has_allow(UNQUOTED_KEY) && char_is_id_start(*cur)) { + val_incr(); + ctn_len++; + if (read_str_id(&cur, eof, flg, pre, val, &msg)) goto obj_key_end; + goto fail_string; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_key_begin; + if (cur == eof) goto fail_comment; + } + goto fail_character_obj_key; + +obj_key_end: + if (byte_match_2(cur, ": ")) { + cur += 2; + goto obj_val_begin; + } + if (*cur == ':') { + cur++; + goto obj_val_begin; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_key_end; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_key_end; + if (cur == eof) goto fail_comment; + } + goto fail_character_obj_sep; + +obj_val_begin: + if (*cur == '"') { + val++; + ctn_len++; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto obj_val_end; + goto fail_string; + } + if (char_is_num(*cur)) { + val++; + ctn_len++; + if (likely(read_num(&cur, pre, flg, val, &msg))) goto obj_val_end; + goto fail_number; + } + if (*cur == '{') { + cur++; + goto obj_begin; + } + if (*cur == '[') { + cur++; + goto arr_begin; + } + if (*cur == 't') { + val++; + ctn_len++; + if (likely(read_true(&cur, val))) goto obj_val_end; + goto fail_literal_true; + } + if (*cur == 'f') { + val++; + ctn_len++; + if (likely(read_false(&cur, val))) goto obj_val_end; + goto fail_literal_false; + } + if (*cur == 'n') { + val++; + ctn_len++; + if (likely(read_null(&cur, val))) goto obj_val_end; + if (has_allow(INF_AND_NAN)) { + if (read_nan(&cur, pre, flg, val)) goto obj_val_end; + } + goto fail_literal_null; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_val_begin; + } + if (has_allow(INF_AND_NAN) && + (*cur == 'i' || *cur == 'I' || *cur == 'N')) { + val++; + ctn_len++; + if (read_inf_or_nan(&cur, pre, flg, val)) goto obj_val_end; + goto fail_character_val; + } + if (has_allow(SINGLE_QUOTED_STR) && *cur == '\'') { + val++; + ctn_len++; + if (likely(read_str_sq(&cur, eof, flg, val, &msg))) goto obj_val_end; + goto fail_string; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_val_begin; + if (cur == eof) goto fail_comment; + } + goto fail_character_val; + +obj_val_end: + if (byte_match_2(cur, ",\n")) { + cur += 2; + goto obj_key_begin; + } + if (likely(*cur == ',')) { + cur++; + goto obj_key_begin; + } + if (likely(*cur == '}')) { + cur++; + goto obj_end; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_val_end; + } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_val_end; + if (cur == eof) goto fail_comment; + } + goto fail_character_obj_end; + +obj_end: + /* pop container */ + ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); + /* point to the next value */ + ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); + ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ; + if (unlikely(ctn == ctn_parent)) goto doc_end; + ctn = ctn_parent; + ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); + if (*cur == '\n') cur++; + if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { + goto obj_val_end; + } else { + goto arr_val_end; + } + +doc_end: + /* check invalid contents after json document */ + if (unlikely(cur < eof) && !has_flg(STOP_WHEN_DONE)) { + while (char_is_space(*cur)) cur++; + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (!skip_trivia(&cur, eof, flg) && cur == eof) { + goto fail_comment; + } + } + if (unlikely(cur < eof)) goto fail_garbage; + } + + **pre = '\0'; + doc = (yyjson_doc *)val_hdr; + doc->root = val_hdr + hdr_len; + doc->alc = alc; + doc->dat_read = (usize)(cur - hdr); + doc->val_read = (usize)((val - doc->root) + 1); + doc->str_pool = has_flg(INSITU) ? NULL : (char *)hdr; + return doc; + +fail_string: return_err(cur, INVALID_STRING, msg); +fail_number: return_err(cur, INVALID_NUMBER, msg); +fail_alloc: return_err(cur, MEMORY_ALLOCATION, MSG_MALLOC); +fail_trailing_comma: return_err(cur, JSON_STRUCTURE, MSG_COMMA); +fail_literal_true: return_err(cur, LITERAL, MSG_CHAR_T); +fail_literal_false: return_err(cur, LITERAL, MSG_CHAR_F); +fail_literal_null: return_err(cur, LITERAL, MSG_CHAR_N); +fail_character_val: return_err(cur, UNEXPECTED_CHARACTER, MSG_CHAR); +fail_character_arr_end: return_err(cur, UNEXPECTED_CHARACTER, MSG_ARR_END); +fail_character_obj_key: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_KEY); +fail_character_obj_sep: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_SEP); +fail_character_obj_end: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_END); +fail_comment: return_err(cur, INVALID_COMMENT, MSG_COMMENT); +fail_garbage: return_err(cur, UNEXPECTED_CONTENT, MSG_GARBAGE); + +#undef val_incr +#undef return_err +} + + + +/*============================================================================== + * MARK: - JSON Reader (Public) + *============================================================================*/ + +yyjson_doc *yyjson_read_opts(char *dat, usize len, + yyjson_read_flag flg, + const yyjson_alc *alc_ptr, + yyjson_read_err *err) { +#define return_err(_pos, _code, _msg) do { \ + err->pos = (usize)(_pos); \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_##_code; \ + if (!has_flg(INSITU) && hdr) alc.free(alc.ctx, (void *)hdr); \ + return NULL; \ +} while (false) + + yyjson_read_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + yyjson_doc *doc; + u8 *hdr = NULL, *eof, *cur; + + /* validate input parameters */ + if (!err) err = &tmp_err; + if (unlikely(!dat)) return_err(0, INVALID_PARAMETER, "input data is NULL"); + if (unlikely(!len)) return_err(0, INVALID_PARAMETER, "input length is 0"); + + /* add 4-byte zero padding for input data if necessary */ + if (has_flg(INSITU)) { + hdr = (u8 *)dat; + eof = (u8 *)dat + len; + cur = (u8 *)dat; + } else { + if (unlikely(len >= USIZE_MAX - YYJSON_PADDING_SIZE)) { + return_err(0, MEMORY_ALLOCATION, MSG_MALLOC); + } + hdr = (u8 *)alc.malloc(alc.ctx, len + YYJSON_PADDING_SIZE); + if (unlikely(!hdr)) { + return_err(0, MEMORY_ALLOCATION, MSG_MALLOC); + } + eof = hdr + len; + cur = hdr; + memcpy(hdr, dat, len); + } + memset(eof, 0, YYJSON_PADDING_SIZE); + + if (has_allow(BOM)) { + if (len >= 3 && is_utf8_bom(cur)) cur += 3; + } + + /* skip empty contents before json document */ + if (unlikely(!char_is_ctn(*cur))) { + while (char_is_space(*cur)) cur++; + if (unlikely(!char_is_ctn(*cur))) { + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (!skip_trivia(&cur, eof, flg) && cur == eof) { + return_err(cur - hdr, INVALID_COMMENT, MSG_COMMENT); + } + } + } + if (unlikely(cur >= eof)) { + return_err(0, EMPTY_CONTENT, "input data is empty"); + } + } + + /* read json document */ + if (likely(char_is_ctn(*cur))) { + if (char_is_space(cur[1]) && char_is_space(cur[2])) { + doc = read_root_pretty(hdr, cur, eof, alc, flg, err); + } else { + doc = read_root_minify(hdr, cur, eof, alc, flg, err); + } + } else { + doc = read_root_single(hdr, cur, eof, alc, flg, err); + } + + /* check result */ + if (likely(doc)) { + memset(err, 0, sizeof(yyjson_read_err)); + } else { + /* RFC 8259: JSON text MUST be encoded using UTF-8 */ + if (err->pos == 0 && err->code != YYJSON_READ_ERROR_MEMORY_ALLOCATION) { + if (is_utf8_bom(hdr)) err->msg = MSG_ERR_BOM; + else if (len >= 4 && is_utf32_bom(hdr)) err->msg = MSG_ERR_UTF32; + else if (len >= 2 && is_utf16_bom(hdr)) err->msg = MSG_ERR_UTF16; + } + if (!has_flg(INSITU)) alc.free(alc.ctx, hdr); + } + return doc; + +#undef return_err +} + +yyjson_doc *yyjson_read_file(const char *path, + yyjson_read_flag flg, + const yyjson_alc *alc_ptr, + yyjson_read_err *err) { +#define return_err(_code, _msg) do { \ + err->pos = 0; \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_##_code; \ + return NULL; \ +} while (false) + + yyjson_read_err tmp_err; + yyjson_doc *doc; + FILE *file; + + if (!err) err = &tmp_err; + if (unlikely(!path)) return_err(INVALID_PARAMETER, "input path is NULL"); + + file = fopen_readonly(path); + if (unlikely(!file)) return_err(FILE_OPEN, MSG_FREAD); + + doc = yyjson_read_fp(file, flg, alc_ptr, err); + fclose(file); + return doc; + +#undef return_err +} + +yyjson_doc *yyjson_read_fp(FILE *file, + yyjson_read_flag flg, + const yyjson_alc *alc_ptr, + yyjson_read_err *err) { +#define return_err(_code, _msg) do { \ + err->pos = 0; \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_##_code; \ + if (buf) alc.free(alc.ctx, buf); \ + return NULL; \ +} while (false) + + yyjson_read_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + yyjson_doc *doc; + + long file_size = 0, file_pos; + void *buf = NULL; + usize buf_size = 0; + + /* validate input parameters */ + if (!err) err = &tmp_err; + if (unlikely(!file)) return_err(INVALID_PARAMETER, "input file is NULL"); + + /* get current position */ + file_pos = ftell(file); + if (file_pos != -1) { + /* get total file size, may fail */ + if (fseek(file, 0, SEEK_END) == 0) file_size = ftell(file); + /* reset to original position, may fail */ + if (fseek(file, file_pos, SEEK_SET) != 0) file_size = 0; + /* get file size from current postion to end */ + if (file_size > 0) file_size -= file_pos; + } + + /* read file */ + if (file_size > 0) { + /* read the entire file in one call */ + buf_size = (usize)file_size + YYJSON_PADDING_SIZE; + buf = alc.malloc(alc.ctx, buf_size); + if (buf == NULL) { + return_err(MEMORY_ALLOCATION, MSG_MALLOC); + } + if (fread_safe(buf, (usize)file_size, file) != (usize)file_size) { + return_err(FILE_READ, MSG_FREAD); + } + } else { + /* failed to get file size, read it as a stream */ + usize chunk_min = (usize)64; + usize chunk_max = (usize)512 * 1024 * 1024; + usize chunk_now = chunk_min; + usize read_size; + void *tmp; + + buf_size = YYJSON_PADDING_SIZE; + while (true) { + if (buf_size + chunk_now < buf_size) { /* overflow */ + return_err(MEMORY_ALLOCATION, MSG_MALLOC); + } + buf_size += chunk_now; + if (!buf) { + buf = alc.malloc(alc.ctx, buf_size); + if (!buf) return_err(MEMORY_ALLOCATION, MSG_MALLOC); + } else { + tmp = alc.realloc(alc.ctx, buf, buf_size - chunk_now, buf_size); + if (!tmp) return_err(MEMORY_ALLOCATION, MSG_MALLOC); + buf = tmp; + } + tmp = ((u8 *)buf) + buf_size - YYJSON_PADDING_SIZE - chunk_now; + read_size = fread_safe(tmp, chunk_now, file); + file_size += (long)read_size; + if (read_size != chunk_now) break; + + chunk_now *= 2; + if (chunk_now > chunk_max) chunk_now = chunk_max; + } + } + + /* read JSON */ + memset((u8 *)buf + file_size, 0, YYJSON_PADDING_SIZE); + flg |= YYJSON_READ_INSITU; + doc = yyjson_read_opts((char *)buf, (usize)file_size, flg, &alc, err); + if (doc) { + doc->str_pool = (char *)buf; + return doc; + } else { + alc.free(alc.ctx, buf); + return NULL; + } + +#undef return_err +} + +const char *yyjson_read_number(const char *dat, + yyjson_val *val, + yyjson_read_flag flg, + const yyjson_alc *alc, + yyjson_read_err *err) { +#define return_err(_pos, _code, _msg) do { \ + err->pos = _pos > hdr ? (usize)(_pos - hdr) : 0; \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_##_code; \ + return NULL; \ +} while (false) + + u8 *hdr = constcast(u8 *)dat, *cur = hdr; + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ + const char *msg; + yyjson_read_err tmp_err; + +#if YYJSON_DISABLE_FAST_FP_CONV + u8 buf[128]; + usize dat_len; +#endif + + if (!err) err = &tmp_err; + if (unlikely(!dat)) { + return_err(cur, INVALID_PARAMETER, "input data is NULL"); + } + if (unlikely(!val)) { + return_err(cur, INVALID_PARAMETER, "output value is NULL"); + } + +#if YYJSON_DISABLE_FAST_FP_CONV + if (!alc) alc = &YYJSON_DEFAULT_ALC; + dat_len = strlen(dat); + if (dat_len < sizeof(buf)) { + memcpy(buf, dat, dat_len + 1); + hdr = buf; + cur = hdr; + } else { + hdr = (u8 *)alc->malloc(alc->ctx, dat_len + 1); + cur = hdr; + if (unlikely(!hdr)) { + return_err(cur, MEMORY_ALLOCATION, MSG_MALLOC); + } + memcpy(hdr, dat, dat_len + 1); + } + hdr[dat_len] = 0; +#endif + +#if YYJSON_DISABLE_FAST_FP_CONV + if (!read_num(&cur, pre, flg, val, &msg)) { + if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); + return_err(cur, INVALID_NUMBER, msg); + } + if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); + if (yyjson_is_raw(val)) val->uni.str = dat; + return dat + (cur - hdr); +#else + if (!read_num(&cur, pre, flg, val, &msg)) { + return_err(cur, INVALID_NUMBER, msg); + } + return (const char *)cur; +#endif + +#undef return_err +} + + + +/*============================================================================== + * MARK: - Incremental JSON Reader (Public) + *============================================================================*/ + +#if !YYJSON_DISABLE_INCR_READER + +/* labels within yyjson_incr_read() to resume incremental parsing */ +#define LABEL_doc_begin 0 +#define LABEL_arr_val_begin 1 +#define LABEL_arr_val_end 2 +#define LABEL_obj_key_begin 3 +#define LABEL_obj_key_end 4 +#define LABEL_obj_val_begin 5 +#define LABEL_obj_val_end 6 +#define LABEL_doc_end 7 + +/** State for incremental JSON reader, opaque in the API. */ +struct yyjson_incr_state { + u32 label; /* current parser goto label */ + yyjson_alc alc; /* allocator */ + yyjson_read_flag flg; /* read flags */ + u8 *hdr; /* JSON data header */ + u8 *cur; /* current position in JSON data */ + usize buf_len; /* total buffer length (without padding) */ + usize hdr_len; /* value count used by yyjson_doc */ + usize alc_len; /* value count allocated */ + usize ctn_len; /* the number of elements in current container */ + yyjson_val *val_hdr; /* the head of allocated values */ + yyjson_val *val_end; /* the end of allocated values */ + yyjson_val *val; /* current JSON value */ + yyjson_val *ctn; /* current container */ + u8 *str_con[2]; /* string parser incremental state */ +}; + +yyjson_incr_state *yyjson_incr_new(char *buf, size_t buf_len, + yyjson_read_flag flg, + const yyjson_alc *alc_ptr) { + yyjson_incr_state *state = NULL; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + + /* remove non-standard flags */ + flg &= ~YYJSON_READ_JSON5; + flg &= ~YYJSON_READ_ALLOW_BOM; + flg &= ~YYJSON_READ_ALLOW_INVALID_UNICODE; + + if (unlikely(!buf)) return NULL; + if (unlikely(buf_len >= USIZE_MAX - YYJSON_PADDING_SIZE)) return NULL; + state = (yyjson_incr_state *)alc.malloc(alc.ctx, sizeof(*state)); + if (!state) return NULL; + memset(state, 0, sizeof(yyjson_incr_state)); + state->alc = alc; + state->flg = flg; + state->buf_len = buf_len; + + /* add 4-byte zero padding for input data if necessary */ + if (has_flg(INSITU)) { + state->hdr = (u8 *)buf; + } else { + state->hdr = (u8 *)alc.malloc(alc.ctx, buf_len + YYJSON_PADDING_SIZE); + if (unlikely(!state->hdr)) { + alc.free(alc.ctx, state); + return NULL; + } + memcpy(state->hdr, buf, buf_len); + } + memset(state->hdr + buf_len, 0, YYJSON_PADDING_SIZE); + state->cur = state->hdr; + state->label = LABEL_doc_begin; + return state; +} + +void yyjson_incr_free(yyjson_incr_state *state) { + if (state) { + yyjson_alc alc = state->alc; + memset(&state->alc, 0, sizeof(alc)); + if (state->val_hdr) { + alc.free(alc.ctx, (void *)state->val_hdr); + } + if (state->hdr && !(state->flg & YYJSON_READ_INSITU)) { + alc.free(alc.ctx, state->hdr); + } + alc.free(alc.ctx, state); + } +} + +yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, + yyjson_read_err *err) { +#define return_err_inv_param(_msg) do { \ + err->pos = 0; \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; \ + return NULL; \ +} while (false) + +#define return_err(_pos, _code, _msg) do { \ + if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code, flg)) { \ + goto unexpected_end; \ + } else { \ + err->pos = (usize)(_pos - hdr); \ + err->code = YYJSON_READ_ERROR_##_code; \ + err->msg = _msg; \ + } \ + return NULL; \ +} while (false) + +#define val_incr() do { \ + val++; \ + if (unlikely(val >= val_end)) { \ + usize alc_old = alc_len; \ + alc_len += alc_len / 2; \ + if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ + val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ + alc_old * sizeof(yyjson_val), \ + alc_len * sizeof(yyjson_val)); \ + if ((!val_tmp)) goto fail_alloc; \ + val = val_tmp + (usize)(val - val_hdr); \ + ctn = val_tmp + (usize)(ctn - val_hdr); \ + state->val = val_tmp + (usize)(state->val - val_hdr); \ + state->val_hdr = val_hdr = val_tmp; \ + val_end = val_tmp + (alc_len - 2); \ + state->val_end = val_end; \ + } \ +} while (false) + + /* save position where it's possible to resume incremental parsing */ +#define save_incr_state(_label) do { \ + state->label = LABEL_##_label; \ + state->cur = cur; \ + state->val = val; \ + state->ctn_len = ctn_len; \ + state->hdr_len = hdr_len; \ + if (unlikely(cur >= end)) goto unexpected_end; \ +} while (false) + +#define check_maybe_truncated_number() do { \ + if (unlikely(cur >= end)) { \ + if (unlikely(cur > state->cur + INCR_NUM_MAX_LEN)) { \ + msg = "number too long"; \ + goto fail_number; \ + } \ + goto unexpected_end; \ + } \ +} while (false) + + u8 *hdr = NULL, *end = NULL, *cur = NULL; + yyjson_read_flag flg; + yyjson_alc alc; + usize dat_len; /* data length in bytes, hint for allocator */ + usize hdr_len; /* value count used by yyjson_doc */ + usize alc_len; /* value count allocated */ + usize alc_max; /* maximum value count for allocator */ + usize ctn_len; /* the number of elements in current container */ + yyjson_val *val_hdr; /* the head of allocated values */ + yyjson_val *val_end; /* the end of allocated values */ + yyjson_val *val_tmp; /* temporary pointer for realloc */ + yyjson_val *val; /* current JSON value */ + yyjson_val *ctn; /* current container */ + yyjson_val *ctn_parent; /* parent of current container */ + yyjson_doc *doc; /* the JSON document, equals to val_hdr */ + const char *msg; /* error message */ + + yyjson_read_err tmp_err; + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ + u8 **con = NULL; /* for incremental string parsing */ + u8 saved_end = '\0'; /* saved end char */ + + /* validate input parameters */ + if (!err) err = &tmp_err; + if (unlikely(!state)) { + return_err_inv_param("input state is NULL"); + } + if (unlikely(!len)) { + return_err_inv_param("input length is 0"); + } + if (unlikely(len > state->buf_len)) { + return_err_inv_param("length is greater than total input length"); + } + + /* restore state saved from the previous call */ + hdr = state->hdr; + end = state->hdr + len; + cur = state->cur; + flg = state->flg; + alc = state->alc; + ctn_len = state->ctn_len; + hdr_len = state->hdr_len; + alc_len = state->alc_len; + val = state->val; + val_hdr = state->val_hdr; + val_end = state->val_end; + ctn = state->ctn; + con = state->str_con; + alc_max = USIZE_MAX / sizeof(yyjson_val); + + /* insert null terminator to make us stop at the specified end, even if + the data contains more valid JSON */ + saved_end = *end; + *end = '\0'; + + /* resume parsing from the last save point */ + switch (state->label) { + case LABEL_doc_begin: goto doc_begin; + case LABEL_arr_val_begin: goto arr_val_begin; + case LABEL_arr_val_end: goto arr_val_end; + case LABEL_obj_key_begin: goto obj_key_begin; + case LABEL_obj_key_end: goto obj_key_end; + case LABEL_obj_val_begin: goto obj_val_begin; + case LABEL_obj_val_end: goto obj_val_end; + case LABEL_doc_end: goto doc_end; + default: return_err_inv_param("invalid incremental state"); + } + +doc_begin: + /* skip empty contents before json document */ + if (unlikely(!char_is_ctn(*cur))) { + while (char_is_space(*cur)) cur++; + if (unlikely(cur >= end)) goto unexpected_end; /* input data is empty */ + } + + /* allocate memory for document */ + if (!val_hdr) { + hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); + hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; + if (likely(char_is_ctn(*cur))) { + dat_len = has_flg(STOP_WHEN_DONE) ? 256 : state->buf_len; + alc_len = hdr_len + + (dat_len / YYJSON_READER_ESTIMATED_MINIFY_RATIO) + 4; + alc_len = yyjson_min(alc_len, alc_max); + } else { + alc_len = hdr_len + 1; /* single value */ + } + val_hdr = (yyjson_val *)alc.malloc(alc.ctx, + alc_len * sizeof(yyjson_val)); + if (unlikely(!val_hdr)) goto fail_alloc; + val_end = val_hdr + (alc_len - 2); /* padding for kv pair reading */ + val = val_hdr + hdr_len; + ctn = val; + ctn_len = 0; + state->val_hdr = val_hdr; + state->val_end = val_end; + save_incr_state(doc_begin); + } + + /* read json document */ + if (*cur == '{') { + cur++; + ctn->tag = YYJSON_TYPE_OBJ; + ctn->uni.ofs = 0; + goto obj_key_begin; + } + if (*cur == '[') { + cur++; + ctn->tag = YYJSON_TYPE_ARR; + ctn->uni.ofs = 0; + goto arr_val_begin; + } + if (char_is_num(*cur)) { + if (likely(read_num(&cur, pre, flg, val, &msg))) goto doc_end; + goto fail_number; + } + if (*cur == '"') { + if (likely(read_str_con(&cur, end, flg, val, &msg, con))) goto doc_end; + goto fail_string; + } + if (*cur == 't') { + if (likely(read_true(&cur, val))) goto doc_end; + goto fail_literal_true; + } + if (*cur == 'f') { + if (likely(read_false(&cur, val))) goto doc_end; + goto fail_literal_false; + } + if (*cur == 'n') { + if (likely(read_null(&cur, val))) goto doc_end; + goto fail_literal_null; + } + + msg = "unexpected character, expected a valid root value"; + if (cur == hdr) { + /* RFC 8259: JSON text MUST be encoded using UTF-8 */ + if (is_utf8_bom(hdr)) msg = MSG_ERR_BOM; + else if (len >= 4 && is_utf32_bom(hdr)) msg = MSG_ERR_UTF32; + else if (len >= 2 && is_utf16_bom(hdr)) msg = MSG_ERR_UTF16; + } + return_err(cur, UNEXPECTED_CHARACTER, msg); + +arr_begin: + /* save current container */ + ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | + (ctn->tag & YYJSON_TAG_MASK); + + /* create a new array value, save parent container offset */ + val_incr(); + val->tag = YYJSON_TYPE_ARR; + val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); + + /* push the new array value as current container */ + ctn = val; + ctn_len = 0; + +arr_val_begin: + save_incr_state(arr_val_begin); +arr_val_continue: + if (*cur == '{') { + cur++; + goto obj_begin; + } + if (*cur == '[') { + cur++; + goto arr_begin; + } + if (char_is_num(*cur)) { + val_incr(); + ctn_len++; + if (likely(read_num(&cur, pre, flg, val, &msg))) goto arr_val_maybe_end; + goto fail_number; + } + if (*cur == '"') { + val_incr(); + ctn_len++; + if (likely(read_str_con(&cur, end, flg, val, &msg, con))) + goto arr_val_end; + goto fail_string; + } + if (*cur == 't') { + val_incr(); + ctn_len++; + if (likely(read_true(&cur, val))) goto arr_val_end; + goto fail_literal_true; + } + if (*cur == 'f') { + val_incr(); + ctn_len++; + if (likely(read_false(&cur, val))) goto arr_val_end; + goto fail_literal_false; + } + if (*cur == 'n') { + val_incr(); + ctn_len++; + if (likely(read_null(&cur, val))) goto arr_val_end; + goto fail_literal_null; + } + if (*cur == ']') { + cur++; + if (likely(ctn_len == 0)) goto arr_end; + while (*cur != ',') cur--; + goto fail_trailing_comma; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto arr_val_continue; + } + goto fail_character_val; + +arr_val_maybe_end: + /* if incremental parsing stops in the middle of a number, it may continue + with more digits, so arr val maybe didn't end yet */ + check_maybe_truncated_number(); + +arr_val_end: + save_incr_state(arr_val_end); + if (*cur == ',') { + cur++; + goto arr_val_begin; + } + if (*cur == ']') { + cur++; + goto arr_end; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto arr_val_end; + } + goto fail_character_arr_end; + +arr_end: + /* get parent container */ + ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); + + /* save the next sibling value offset */ + ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); + ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; + if (unlikely(ctn == ctn_parent)) goto doc_end; + + /* pop parent as current container */ + ctn = ctn_parent; + ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); + if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { + goto obj_val_end; + } else { + goto arr_val_end; + } + +obj_begin: + /* push container */ + ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | + (ctn->tag & YYJSON_TAG_MASK); + val_incr(); + val->tag = YYJSON_TYPE_OBJ; + /* offset to the parent */ + val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); + ctn = val; + ctn_len = 0; + +obj_key_begin: + save_incr_state(obj_key_begin); +obj_key_continue: + if (likely(*cur == '"')) { + val_incr(); + ctn_len++; + if (likely(read_str_con(&cur, end, flg, val, &msg, con))) + goto obj_key_end; + goto fail_string; + } + if (likely(*cur == '}')) { + cur++; + if (likely(ctn_len == 0)) goto obj_end; + while (*cur != ',') cur--; + goto fail_trailing_comma; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_key_continue; + } + goto fail_character_obj_key; + +obj_key_end: + save_incr_state(obj_key_end); + if (*cur == ':') { + cur++; + goto obj_val_begin; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_key_end; + } + goto fail_character_obj_sep; + +obj_val_begin: + save_incr_state(obj_val_begin); +obj_val_continue: + if (*cur == '"') { + val++; + ctn_len++; + if (likely(read_str_con(&cur, end, flg, val, &msg, con))) + goto obj_val_end; + goto fail_string; + } + if (char_is_num(*cur)) { + val++; + ctn_len++; + if (likely(read_num(&cur, pre, flg, val, &msg))) goto obj_val_maybe_end; + goto fail_number; + } + if (*cur == '{') { + cur++; + goto obj_begin; + } + if (*cur == '[') { + cur++; + goto arr_begin; + } + if (*cur == 't') { + val++; + ctn_len++; + if (likely(read_true(&cur, val))) goto obj_val_end; + goto fail_literal_true; + } + if (*cur == 'f') { + val++; + ctn_len++; + if (likely(read_false(&cur, val))) goto obj_val_end; + goto fail_literal_false; + } + if (*cur == 'n') { + val++; + ctn_len++; + if (likely(read_null(&cur, val))) goto obj_val_end; + goto fail_literal_null; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_val_continue; + } + goto fail_character_val; + +obj_val_maybe_end: + /* if incremental parsing stops in the middle of a number, it may continue + with more digits, so obj val maybe didn't end yet */ + check_maybe_truncated_number(); + +obj_val_end: + save_incr_state(obj_val_end); + if (likely(*cur == ',')) { + cur++; + goto obj_key_begin; + } + if (likely(*cur == '}')) { + cur++; + goto obj_end; + } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_val_end; + } + goto fail_character_obj_end; + +obj_end: + /* pop container */ + ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); + /* point to the next value */ + ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); + ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ; + if (unlikely(ctn == ctn_parent)) goto doc_end; + ctn = ctn_parent; + ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); + if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { + goto obj_val_end; + } else { + goto arr_val_end; + } + +doc_end: + /* check invalid contents after json document */ + if (unlikely(cur < end) && !has_flg(STOP_WHEN_DONE)) { + save_incr_state(doc_end); + while (char_is_space(*cur)) cur++; + if (unlikely(cur < end)) goto fail_garbage; + } + + **pre = '\0'; + doc = (yyjson_doc *)val_hdr; + doc->root = val_hdr + hdr_len; + doc->alc = alc; + doc->dat_read = (usize)(cur - hdr); + doc->val_read = (usize)((val - doc->root) + 1); + doc->str_pool = has_flg(INSITU) ? NULL : (char *)hdr; + state->hdr = NULL; + state->val_hdr = NULL; + memset(err, 0, sizeof(yyjson_read_err)); + return doc; + +unexpected_end: + err->pos = len; + /* if no nore data, stop the incr read */ + if (unlikely(len >= state->buf_len)) { + err->code = YYJSON_READ_ERROR_UNEXPECTED_END; + err->msg = MSG_NOT_END; + return NULL; + } + /* save parser state in extended error struct, in addition to what was + * stored in the last save_incr_state */ + err->code = YYJSON_READ_ERROR_MORE; + err->msg = "need more data"; + state->val_end = val_end; + state->ctn = ctn; + state->alc_len = alc_len; + /* restore the end where we've inserted a null terminator */ + *end = saved_end; + return NULL; + +fail_string: return_err(cur, INVALID_STRING, msg); +fail_number: return_err(cur, INVALID_NUMBER, msg); +fail_alloc: return_err(cur, MEMORY_ALLOCATION, MSG_MALLOC); +fail_trailing_comma: return_err(cur, JSON_STRUCTURE, MSG_COMMA); +fail_literal_true: return_err(cur, LITERAL, MSG_CHAR_T); +fail_literal_false: return_err(cur, LITERAL, MSG_CHAR_F); +fail_literal_null: return_err(cur, LITERAL, MSG_CHAR_N); +fail_character_val: return_err(cur, UNEXPECTED_CHARACTER, MSG_CHAR); +fail_character_arr_end: return_err(cur, UNEXPECTED_CHARACTER, MSG_ARR_END); +fail_character_obj_key: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_KEY); +fail_character_obj_sep: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_SEP); +fail_character_obj_end: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_END); +fail_garbage: return_err(cur, UNEXPECTED_CONTENT, MSG_GARBAGE); + +#undef val_incr +#undef return_err +#undef return_err_inv_param +#undef save_incr_state +#undef check_maybe_truncated_number +} + +#endif /* YYJSON_DISABLE_INCR_READER */ + +#undef has_flg +#undef has_allow +#endif /* YYJSON_DISABLE_READER */ + + + +#if !YYJSON_DISABLE_WRITER /* writer begin */ + +/* Check write flag, avoids `always false` warning when disabled. */ +#define has_flg(_flg) unlikely(has_wflag(flg, YYJSON_WRITE_##_flg, 0)) +#define has_allow(_flg) unlikely(has_wflag(flg, YYJSON_WRITE_ALLOW_##_flg, 1)) +static_inline bool has_wflag(yyjson_write_flag flg, yyjson_write_flag chk, + bool non_standard) { +#if YYJSON_DISABLE_NON_STANDARD + if (non_standard) return false; +#endif + return (flg & chk) != 0; +} + +/*============================================================================== + * MARK: - Integer Writer (Private) + * + * The maximum value of uint32_t is 4294967295 (10 digits), + * these digits are named as 'aabbccddee' here. + * + * Although most compilers may convert the "division by constant value" into + * "multiply and shift", manual conversion can still help some compilers + * generate fewer and better instructions. + * + * Reference: + * Division by Invariant Integers using Multiplication, 1994. + * https://gmplib.org/~tege/divcnst-pldi94.pdf + * Improved division by invariant integers, 2011. + * https://gmplib.org/~tege/division-paper.pdf + *============================================================================*/ + +/** Digit table from 00 to 99. */ +yyjson_align(2) +static const char digit_table[200] = { + '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', + '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', + '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', + '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', + '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', + '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', + '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', + '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', + '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', + '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', + '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', + '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', + '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', + '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', + '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', + '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', + '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', + '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', + '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', + '9', '5', '9', '6', '9', '7', '9', '8', '9', '9' +}; + +static_inline u8 *write_u32_len_8(u32 val, u8 *buf) { + u32 aa, bb, cc, dd, aabb, ccdd; /* 8 digits: aabbccdd */ + aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ + ccdd = val - aabb * 10000; /* (val % 10000) */ + aa = (aabb * 5243) >> 19; /* (aabb / 100) */ + cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ + bb = aabb - aa * 100; /* (aabb % 100) */ + dd = ccdd - cc * 100; /* (ccdd % 100) */ + byte_copy_2(buf + 0, digit_table + aa * 2); + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + byte_copy_2(buf + 6, digit_table + dd * 2); + return buf + 8; +} + +static_inline u8 *write_u32_len_4(u32 val, u8 *buf) { + u32 aa, bb; /* 4 digits: aabb */ + aa = (val * 5243) >> 19; /* (val / 100) */ + bb = val - aa * 100; /* (val % 100) */ + byte_copy_2(buf + 0, digit_table + aa * 2); + byte_copy_2(buf + 2, digit_table + bb * 2); + return buf + 4; +} + +static_inline u8 *write_u32_len_1_to_8(u32 val, u8 *buf) { + u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; + + if (val < 100) { /* 1-2 digits: aa */ + lz = val < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + val * 2 + lz); + buf -= lz; + return buf + 2; + + } else if (val < 10000) { /* 3-4 digits: aabb */ + aa = (val * 5243) >> 19; /* (val / 100) */ + bb = val - aa * 100; /* (val % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + return buf + 4; + + } else if (val < 1000000) { /* 5-6 digits: aabbcc */ + aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */ + bbcc = val - aa * 10000; /* (val % 10000) */ + bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */ + cc = bbcc - bb * 100; /* (bbcc % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + return buf + 6; + + } else { /* 7-8 digits: aabbccdd */ + aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ + ccdd = val - aabb * 10000; /* (val % 10000) */ + aa = (aabb * 5243) >> 19; /* (aabb / 100) */ + cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ + bb = aabb - aa * 100; /* (aabb % 100) */ + dd = ccdd - cc * 100; /* (ccdd % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + byte_copy_2(buf + 6, digit_table + dd * 2); + return buf + 8; + } +} + +static_inline u8 *write_u32_len_5_to_8(u32 val, u8 *buf) { + u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; + + if (val < 1000000) { /* 5-6 digits: aabbcc */ + aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */ + bbcc = val - aa * 10000; /* (val % 10000) */ + bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */ + cc = bbcc - bb * 100; /* (bbcc % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + return buf + 6; + + } else { /* 7-8 digits: aabbccdd */ + aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ + ccdd = val - aabb * 10000; /* (val % 10000) */ + aa = (aabb * 5243) >> 19; /* (aabb / 100) */ + cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ + bb = aabb - aa * 100; /* (aabb % 100) */ + dd = ccdd - cc * 100; /* (ccdd % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + byte_copy_2(buf + 6, digit_table + dd * 2); + return buf + 8; + } +} + +static_inline u8 *write_u64(u64 val, u8 *buf) { + u64 tmp, hgh; + u32 mid, low; + + if (val < 100000000) { /* 1-8 digits */ + buf = write_u32_len_1_to_8((u32)val, buf); + return buf; + + } else if (val < (u64)100000000 * 100000000) { /* 9-16 digits */ + hgh = val / 100000000; /* (val / 100000000) */ + low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ + buf = write_u32_len_1_to_8((u32)hgh, buf); + buf = write_u32_len_8(low, buf); + return buf; + + } else { /* 17-20 digits */ + tmp = val / 100000000; /* (val / 100000000) */ + low = (u32)(val - tmp * 100000000); /* (val % 100000000) */ + hgh = (u32)(tmp / 10000); /* (tmp / 10000) */ + mid = (u32)(tmp - hgh * 10000); /* (tmp % 10000) */ + buf = write_u32_len_5_to_8((u32)hgh, buf); + buf = write_u32_len_4(mid, buf); + buf = write_u32_len_8(low, buf); + return buf; + } +} + + + +/*============================================================================== + * MARK: - Number Writer (Private) + *============================================================================*/ + +#if !YYJSON_DISABLE_FAST_FP_CONV /* FP_WRITER */ + +/** Trailing zero count table for number 0 to 99. + (generate with misc/make_tables.c) */ +static const u8 dec_trailing_zero_table[] = { + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +static_inline u8 *write_u64_len_1_to_16(u64 val, u8 *buf) { + u64 hgh; + u32 low; + if (val < 100000000) { /* 1-8 digits */ + buf = write_u32_len_1_to_8((u32)val, buf); + return buf; + } else { /* 9-16 digits */ + hgh = val / 100000000; /* (val / 100000000) */ + low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ + buf = write_u32_len_1_to_8((u32)hgh, buf); + buf = write_u32_len_8(low, buf); + return buf; + } +} + +static_inline u8 *write_u64_len_1_to_17(u64 val, u8 *buf) { + u64 hgh; + u32 mid, low, one; + if (val >= (u64)100000000 * 10000000) { /* len: 16 to 17 */ + hgh = val / 100000000; /* (val / 100000000) */ + low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ + one = (u32)(hgh / 100000000); /* (hgh / 100000000) */ + mid = (u32)(hgh - (u64)one * 100000000); /* (hgh % 100000000) */ + *buf = (u8)((u8)one + (u8)'0'); + buf += one > 0; + buf = write_u32_len_8(mid, buf); + buf = write_u32_len_8(low, buf); + return buf; + } else if (val >= (u64)100000000){ /* len: 9 to 15 */ + hgh = val / 100000000; /* (val / 100000000) */ + low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ + buf = write_u32_len_1_to_8((u32)hgh, buf); + buf = write_u32_len_8(low, buf); + return buf; + } else { /* len: 1 to 8 */ + buf = write_u32_len_1_to_8((u32)val, buf); + return buf; + } +} + +/** + Write an unsigned integer with a length of 7 to 9 with trailing zero trimmed. + These digits are named as "abbccddee" here. + For example, input 123456000, output "123456". + */ +static_inline u8 *write_u32_len_7_to_9_trim(u32 val, u8 *buf) { + bool lz; + u32 tz, tz1, tz2; + + u32 abbcc = val / 10000; /* (abbccddee / 10000) */ + u32 ddee = val - abbcc * 10000; /* (abbccddee % 10000) */ + u32 abb = (u32)(((u64)abbcc * 167773) >> 24); /* (abbcc / 100) */ + u32 a = (abb * 41) >> 12; /* (abb / 100) */ + u32 bb = abb - a * 100; /* (abb % 100) */ + u32 cc = abbcc - abb * 100; /* (abbcc % 100) */ + + /* write abbcc */ + buf[0] = (u8)(a + '0'); + buf += a > 0; + lz = bb < 10 && a == 0; + byte_copy_2(buf + 0, digit_table + bb * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + cc * 2); + + if (ddee) { + u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */ + u32 ee = ddee - dd * 100; /* (ddee % 100) */ + byte_copy_2(buf + 4, digit_table + dd * 2); + byte_copy_2(buf + 6, digit_table + ee * 2); + tz1 = dec_trailing_zero_table[dd]; + tz2 = dec_trailing_zero_table[ee]; + tz = ee ? tz2 : (tz1 + 2); + buf += 8 - tz; + return buf; + } else { + tz1 = dec_trailing_zero_table[bb]; + tz2 = dec_trailing_zero_table[cc]; + tz = cc ? tz2 : (tz1 + tz2); + buf += 4 - tz; + return buf; + } +} + +/** + Write an unsigned integer with a length of 16 or 17 with trailing zero trimmed. + These digits are named as "abbccddeeffgghhii" here. + For example, input 1234567890123000, output "1234567890123". + */ +static_inline u8 *write_u64_len_16_to_17_trim(u64 val, u8 *buf) { + u32 tz, tz1, tz2; + + u32 abbccddee = (u32)(val / 100000000); + u32 ffgghhii = (u32)(val - (u64)abbccddee * 100000000); + u32 abbcc = abbccddee / 10000; + u32 ddee = abbccddee - abbcc * 10000; + u32 abb = (u32)(((u64)abbcc * 167773) >> 24); /* (abbcc / 100) */ + u32 a = (abb * 41) >> 12; /* (abb / 100) */ + u32 bb = abb - a * 100; /* (abb % 100) */ + u32 cc = abbcc - abb * 100; /* (abbcc % 100) */ + buf[0] = (u8)(a + '0'); + buf += a > 0; + byte_copy_2(buf + 0, digit_table + bb * 2); + byte_copy_2(buf + 2, digit_table + cc * 2); + + if (ffgghhii) { + u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */ + u32 ee = ddee - dd * 100; /* (ddee % 100) */ + u32 ffgg = (u32)(((u64)ffgghhii * 109951163) >> 40); /* (val / 10000) */ + u32 hhii = ffgghhii - ffgg * 10000; /* (val % 10000) */ + u32 ff = (ffgg * 5243) >> 19; /* (aabb / 100) */ + u32 gg = ffgg - ff * 100; /* (aabb % 100) */ + byte_copy_2(buf + 4, digit_table + dd * 2); + byte_copy_2(buf + 6, digit_table + ee * 2); + byte_copy_2(buf + 8, digit_table + ff * 2); + byte_copy_2(buf + 10, digit_table + gg * 2); + if (hhii) { + u32 hh = (hhii * 5243) >> 19; /* (ccdd / 100) */ + u32 ii = hhii - hh * 100; /* (ccdd % 100) */ + byte_copy_2(buf + 12, digit_table + hh * 2); + byte_copy_2(buf + 14, digit_table + ii * 2); + tz1 = dec_trailing_zero_table[hh]; + tz2 = dec_trailing_zero_table[ii]; + tz = ii ? tz2 : (tz1 + 2); + return buf + 16 - tz; + } else { + tz1 = dec_trailing_zero_table[ff]; + tz2 = dec_trailing_zero_table[gg]; + tz = gg ? tz2 : (tz1 + 2); + return buf + 12 - tz; + } + } else { + if (ddee) { + u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */ + u32 ee = ddee - dd * 100; /* (ddee % 100) */ + byte_copy_2(buf + 4, digit_table + dd * 2); + byte_copy_2(buf + 6, digit_table + ee * 2); + tz1 = dec_trailing_zero_table[dd]; + tz2 = dec_trailing_zero_table[ee]; + tz = ee ? tz2 : (tz1 + 2); + return buf + 8 - tz; + } else { + tz1 = dec_trailing_zero_table[bb]; + tz2 = dec_trailing_zero_table[cc]; + tz = cc ? tz2 : (tz1 + tz2); + return buf + 4 - tz; + } + } +} + +/** Write exponent part in range `e-45` to `e38`. */ +static_inline u8 *write_f32_exp(i32 exp, u8 *buf) { + bool lz; + byte_copy_2(buf, "e-"); + buf += 2 - (exp >= 0); + exp = exp < 0 ? -exp : exp; + lz = exp < 10; + byte_copy_2(buf + 0, digit_table + (u32)exp * 2 + lz); + return buf + 2 - lz; +} + +/** Write exponent part in range `e-324` to `e308`. */ +static_inline u8 *write_f64_exp(i32 exp, u8 *buf) { + byte_copy_2(buf, "e-"); + buf += 2 - (exp >= 0); + exp = exp < 0 ? -exp : exp; + if (exp < 100) { + bool lz = exp < 10; + byte_copy_2(buf + 0, digit_table + (u32)exp * 2 + lz); + return buf + 2 - lz; + } else { + u32 hi = ((u32)exp * 656) >> 16; /* exp / 100 */ + u32 lo = (u32)exp - hi * 100; /* exp % 100 */ + buf[0] = (u8)((u8)hi + (u8)'0'); + byte_copy_2(buf + 1, digit_table + lo * 2); + return buf + 3; + } +} + +/** Magic number for fast `divide by power of 10`. */ +typedef struct { + u64 p10, mul; + u32 shr1, shr2; +} div_pow10_magic; + +/** Generated with llvm, see https://github.com/llvm/llvm-project/ + blob/main/llvm/lib/Support/DivisionByConstantInfo.cpp */ +static const div_pow10_magic div_pow10_table[] = { + { U64(0x00000000, 0x00000001), U64(0x00000000, 0x00000000), 0, 0 }, + { U64(0x00000000, 0x0000000A), U64(0xCCCCCCCC, 0xCCCCCCCD), 0, 3 }, + { U64(0x00000000, 0x00000064), U64(0x28F5C28F, 0x5C28F5C3), 2, 2 }, + { U64(0x00000000, 0x000003E8), U64(0x20C49BA5, 0xE353F7CF), 3, 4 }, + { U64(0x00000000, 0x00002710), U64(0x346DC5D6, 0x3886594B), 0, 11 }, + { U64(0x00000000, 0x000186A0), U64(0x0A7C5AC4, 0x71B47843), 5, 7 }, + { U64(0x00000000, 0x000F4240), U64(0x431BDE82, 0xD7B634DB), 0, 18 }, + { U64(0x00000000, 0x00989680), U64(0xD6BF94D5, 0xE57A42BD), 0, 23 }, + { U64(0x00000000, 0x05F5E100), U64(0xABCC7711, 0x8461CEFD), 0, 26 }, + { U64(0x00000000, 0x3B9ACA00), U64(0x0044B82F, 0xA09B5A53), 9, 11 }, + { U64(0x00000002, 0x540BE400), U64(0xDBE6FECE, 0xBDEDD5BF), 0, 33 }, + { U64(0x00000017, 0x4876E800), U64(0xAFEBFF0B, 0xCB24AAFF), 0, 36 }, + { U64(0x000000E8, 0xD4A51000), U64(0x232F3302, 0x5BD42233), 0, 37 }, + { U64(0x00000918, 0x4E72A000), U64(0x384B84D0, 0x92ED0385), 0, 41 }, + { U64(0x00005AF3, 0x107A4000), U64(0x0B424DC3, 0x5095CD81), 0, 42 }, + { U64(0x00038D7E, 0xA4C68000), U64(0x00024075, 0xF3DCEAC3), 15, 20 }, + { U64(0x002386F2, 0x6FC10000), U64(0x39A5652F, 0xB1137857), 0, 51 }, + { U64(0x01634578, 0x5D8A0000), U64(0x00005C3B, 0xD5191B53), 17, 22 }, + { U64(0x0DE0B6B3, 0xA7640000), U64(0x000049C9, 0x7747490F), 18, 24 }, + { U64(0x8AC72304, 0x89E80000), U64(0x760F253E, 0xDB4AB0d3), 0, 62 }, +}; + +/** Divide a number by power of 10. */ +static_inline void div_pow10(u64 num, u32 exp, u64 *div, u64 *mod, u64 *p10) { + u64 hi, lo; + div_pow10_magic m = div_pow10_table[exp]; + u128_mul(num >> m.shr1, m.mul, &hi, &lo); + *div = hi >> m.shr2; + *mod = num - (*div * m.p10); + *p10 = m.p10; +} + +/** Multiplies 64-bit integer and returns highest 64-bit rounded value. */ +static_inline u32 u64_round_to_odd(u64 u, u32 cp) { + u64 hi, lo; + u32 y_hi, y_lo; + u128_mul(cp, u, &hi, &lo); + y_hi = (u32)hi; + y_lo = (u32)(lo >> 32); + return y_hi | (y_lo > 1); +} + +/** Multiplies 128-bit integer and returns highest 64-bit rounded value. */ +static_inline u64 u128_round_to_odd(u64 hi, u64 lo, u64 cp) { + u64 x_hi, x_lo, y_hi, y_lo; + u128_mul(cp, lo, &x_hi, &x_lo); + u128_mul_add(cp, hi, x_hi, &y_hi, &y_lo); + return y_hi | (y_lo > 1); +} + +/** Convert f32 from binary to decimal (shortest but may have trailing zeros). + The input should not be 0, inf or nan. */ +static_inline void f32_bin_to_dec(u32 sig_raw, u32 exp_raw, + u32 sig_bin, i32 exp_bin, + u32 *sig_dec, i32 *exp_dec) { + + bool is_even, irregular, round_up, trim; + bool u0_inside, u1_inside, w0_inside, w1_inside; + u64 p10_hi, p10_lo, hi, lo; + u32 s, sp, cb, cbl, cbr, vb, vbl, vbr, upper, lower, mid; + i32 k, h; + + /* Fast path, see f64_bin_to_dec(). */ + while (likely(sig_raw)) { + u32 mod, dec, add_1, add_10, s_hi, s_lo; + u32 c, half_ulp, t0, t1; + + /* k = floor(exp_bin * log10(2)); */ + /* h = exp_bin + floor(log2(10) * -k); (h = 0/1/2/3) */ + k = (i32)(exp_bin * 315653) >> 20; + h = exp_bin + ((-k * 217707) >> 16); + pow10_table_get_sig(-k, &p10_hi, &p10_lo); + + /* sig_bin << (1/2/3/4) */ + cb = sig_bin << (h + 1); + u128_mul(cb, p10_hi, &hi, &lo); + s_hi = (u32)(hi); + s_lo = (u32)(lo >> 32); + mod = s_hi % 10; + dec = s_hi - mod; + + /* right shift 4 to fit in u32 */ + c = (mod << (32 - 4)) | (s_lo >> 4); + half_ulp = (u32)(p10_hi >> (32 + 4 - h)); + + /* check w1, u0, w0 range */ + w1_inside = (s_lo >= ((u32)1 << 31)); + if (unlikely(s_lo == ((u32)1 << 31))) break; + u0_inside = (half_ulp >= c); + if (unlikely(half_ulp == c)) break; + t0 = (u32)10 << (32 - 4); + t1 = c + half_ulp; + w0_inside = (t1 >= t0); + if (unlikely(t0 - t1 <= (u32)1)) break; + + trim = (u0_inside | w0_inside); + add_10 = (w0_inside ? 10 : 0); + add_1 = mod + w1_inside; + *sig_dec = dec + (trim ? add_10 : add_1); + *exp_dec = k; + return; + } + + /* Schubfach algorithm, see f64_bin_to_dec(). */ + irregular = (sig_raw == 0 && exp_raw > 1); + is_even = !(sig_bin & 1); + cbl = 4 * sig_bin - 2 + irregular; + cb = 4 * sig_bin; + cbr = 4 * sig_bin + 2; + + /* k = floor(exp_bin * log10(2) + (irregular ? log10(3.0 / 4.0) : 0)); */ + /* h = exp_bin + floor(log2(10) * -k) + 1; (h = 1/2/3/4) */ + k = (i32)(exp_bin * 315653 - (irregular ? 131237 : 0)) >> 20; + h = exp_bin + ((-k * 217707) >> 16) + 1; + pow10_table_get_sig(-k, &p10_hi, &p10_lo); + p10_hi += 1; + + vbl = u64_round_to_odd(p10_hi, cbl << h); + vb = u64_round_to_odd(p10_hi, cb << h); + vbr = u64_round_to_odd(p10_hi, cbr << h); + lower = vbl + !is_even; + upper = vbr - !is_even; + + s = vb / 4; + if (s >= 10) { + sp = s / 10; + u0_inside = (lower <= 40 * sp); + w0_inside = (upper >= 40 * sp + 40); + if (u0_inside != w0_inside) { + *sig_dec = sp * 10 + (w0_inside ? 10 : 0); + *exp_dec = k; + return; + } + } + u1_inside = (lower <= 4 * s); + w1_inside = (upper >= 4 * s + 4); + mid = 4 * s + 2; + round_up = (vb > mid) || (vb == mid && (s & 1) != 0); + *sig_dec = s + ((u1_inside != w1_inside) ? w1_inside : round_up); + *exp_dec = k; +} + +/** Convert f64 from binary to decimal (shortest but may have trailing zeros). + The input should not be 0, inf or nan. */ +static_inline void f64_bin_to_dec(u64 sig_raw, u32 exp_raw, + u64 sig_bin, i32 exp_bin, + u64 *sig_dec, i32 *exp_dec) { + + bool is_even, irregular, round_up, trim; + bool u0_inside, u1_inside, w0_inside, w1_inside; + u64 s, sp, cb, cbl, cbr, vb, vbl, vbr, p10_hi, p10_lo, upper, lower, mid; + i32 k, h; + + /* + Fast path: + For regular spacing significand 'c', there are 4 candidates: + + u0 u1 c w1 w0 + ----|----|----|----|----|-*--|----|----|----|----|----|----|----|---- + 9 0 1 2 3 4 5 6 7 8 9 0 1 + |___________________|___________________| + 1ulp + + The `1ulp` is in the range [1.0, 10.0). + If (c - 0.5ulp < u0), trim the last digit and round down. + If (c + 0.5ulp > w0), trim the last digit and round up. + If (c - 0.5ulp < u1), round down. + If (c + 0.5ulp > w1), round up. + */ + while (likely(sig_raw)) { + u64 mod, dec, add_1, add_10, s_hi, s_lo; + u64 c, half_ulp, t0, t1; + + /* k = floor(exp_bin * log10(2)); */ + /* h = exp_bin + floor(log2(10) * -k); (h = 0/1/2/3) */ + k = (i32)(exp_bin * 315653) >> 20; + h = exp_bin + ((-k * 217707) >> 16); + pow10_table_get_sig(-k, &p10_hi, &p10_lo); + + /* sig_bin << (1/2/3/4) */ + cb = sig_bin << (h + 1); + u128_mul(cb, p10_lo, &s_hi, &s_lo); + u128_mul_add(cb, p10_hi, s_hi, &s_hi, &s_lo); + mod = s_hi % 10; + dec = s_hi - mod; + + /* right shift 4 to fit in u64 */ + c = (mod << (64 - 4)) | (s_lo >> 4); + half_ulp = p10_hi >> (4 - h); + + /* check w1, u0, w0 range */ + w1_inside = (s_lo >= ((u64)1 << 63)); + if (unlikely(s_lo == ((u64)1 << 63))) break; + u0_inside = (half_ulp >= c); + if (unlikely(half_ulp == c)) break; + t0 = ((u64)10 << (64 - 4)); + t1 = c + half_ulp; + w0_inside = (t1 >= t0); + if (unlikely(t0 - t1 <= (u64)1)) break; + + trim = (u0_inside | w0_inside); + add_10 = (w0_inside ? 10 : 0); + add_1 = mod + w1_inside; + *sig_dec = dec + (trim ? add_10 : add_1); + *exp_dec = k; + return; + } + + /* + Schubfach algorithm: + Raffaello Giulietti, The Schubfach way to render doubles, 2022. + https://drive.google.com/file/d/1gp5xv4CAa78SVgCeWfGqqI4FfYYYuNFb (Paper) + https://github.com/openjdk/jdk/pull/3402 (Java implementation) + https://github.com/abolz/Drachennest (C++ implementation) + */ + irregular = (sig_raw == 0 && exp_raw > 1); + is_even = !(sig_bin & 1); + cbl = 4 * sig_bin - 2 + irregular; + cb = 4 * sig_bin; + cbr = 4 * sig_bin + 2; + + /* k = floor(exp_bin * log10(2) + (irregular ? log10(3.0 / 4.0) : 0)); */ + /* h = exp_bin + floor(log2(10) * -k) + 1; (h = 1/2/3/4) */ + k = (i32)(exp_bin * 315653 - (irregular ? 131237 : 0)) >> 20; + h = exp_bin + ((-k * 217707) >> 16) + 1; + pow10_table_get_sig(-k, &p10_hi, &p10_lo); + p10_lo += 1; + + vbl = u128_round_to_odd(p10_hi, p10_lo, cbl << h); + vb = u128_round_to_odd(p10_hi, p10_lo, cb << h); + vbr = u128_round_to_odd(p10_hi, p10_lo, cbr << h); + lower = vbl + !is_even; + upper = vbr - !is_even; + + s = vb / 4; + if (s >= 10) { + sp = s / 10; + u0_inside = (lower <= 40 * sp); + w0_inside = (upper >= 40 * sp + 40); + if (u0_inside != w0_inside) { + *sig_dec = sp * 10 + (w0_inside ? 10 : 0); + *exp_dec = k; + return; + } + } + u1_inside = (lower <= 4 * s); + w1_inside = (upper >= 4 * s + 4); + mid = 4 * s + 2; + round_up = (vb > mid) || (vb == mid && (s & 1) != 0); + *sig_dec = s + ((u1_inside != w1_inside) ? w1_inside : round_up); + *exp_dec = k; +} + +/** Convert f64 from binary to decimal (fast but not the shortest). + The input should not be 0, inf, nan. */ +static_inline void f64_bin_to_dec_fast(u64 sig_raw, u32 exp_raw, + u64 sig_bin, i32 exp_bin, + u64 *sig_dec, i32 *exp_dec, + bool *round_up) { + u64 cb, p10_hi, p10_lo, s_hi, s_lo; + i32 k, h; + bool irregular, u; + + irregular = (sig_raw == 0 && exp_raw > 1); + + /* k = floor(exp_bin * log10(2) + (irregular ? log10(3.0 / 4.0) : 0)); */ + /* h = exp_bin + floor(log2(10) * -k) + 1; (h = 1/2/3/4) */ + k = (i32)(exp_bin * 315653 - (irregular ? 131237 : 0)) >> 20; + h = exp_bin + ((-k * 217707) >> 16); + pow10_table_get_sig(-k, &p10_hi, &p10_lo); + + /* sig_bin << (1/2/3/4) */ + cb = sig_bin << (h + 1); + u128_mul(cb, p10_lo, &s_hi, &s_lo); + u128_mul_add(cb, p10_hi, s_hi, &s_hi, &s_lo); + + /* round up */ + u = s_lo >= (irregular ? U64(0x55555555, 0x55555555) : ((u64)1 << 63)); + + *sig_dec = s_hi + u; + *exp_dec = k; + *round_up = u; + return; +} + +/** Write inf/nan if allowed. */ +static_inline u8 *write_inf_or_nan(u8 *buf, yyjson_write_flag flg, + u64 sig_raw, bool sign) { + if (has_flg(INF_AND_NAN_AS_NULL)) { + byte_copy_4(buf, "null"); + return buf + 4; + } + if (has_allow(INF_AND_NAN)) { + if (sig_raw == 0) { + buf[0] = '-'; + buf += sign; + byte_copy_8(buf, "Infinity"); + return buf + 8; + } else { + byte_copy_4(buf, "NaN"); + return buf + 3; + } + } + return NULL; +} + +/** + Write a float number (requires 40 bytes buffer). + We follow the ECMAScript specification for printing floating-point numbers, + similar to `Number.prototype.toString()`, but with the following changes: + 1. Keep the negative sign of `-0.0` to preserve input information. + 2. Keep decimal point to indicate the number is floating point. + 3. Remove positive sign in the exponent part. + */ +static_noinline u8 *write_f32_raw(u8 *buf, u64 raw_f64, + yyjson_write_flag flg) { + u32 sig_bin, sig_dec, sig_raw; + i32 exp_bin, exp_dec, sig_len, dot_ofs; + u32 exp_raw, raw; + u8 *end; + bool sign; + + /* cast double to float */ + raw = f32_to_bits(f64_to_f32(f64_from_bits(raw_f64))); + + /* decode raw bytes from IEEE-754 double format. */ + sign = (bool)(raw >> (F32_BITS - 1)); + sig_raw = raw & F32_SIG_MASK; + exp_raw = (raw & F32_EXP_MASK) >> F32_SIG_BITS; + + /* return inf or nan */ + if (unlikely(exp_raw == ((u32)1 << F32_EXP_BITS) - 1)) { + return write_inf_or_nan(buf, flg, sig_raw, sign); + } + + /* add sign for all finite number */ + buf[0] = '-'; + buf += sign; + + /* return zero */ + if ((raw << 1) == 0) { + byte_copy_4(buf, "0.0"); + return buf + 3; + } + + if (likely(exp_raw != 0)) { + /* normal number */ + sig_bin = sig_raw | ((u32)1 << F32_SIG_BITS); + exp_bin = (i32)exp_raw - F32_EXP_BIAS - F32_SIG_BITS; + + /* fast path for small integer number without fraction */ + if ((-F32_SIG_BITS <= exp_bin && exp_bin <= 0) && + (u64_tz_bits(sig_bin) >= (u32)-exp_bin)) { + sig_dec = sig_bin >> -exp_bin; /* range: [1, 0xFFFFFF] */ + buf = write_u32_len_1_to_8(sig_dec, buf); + byte_copy_2(buf, ".0"); + return buf + 2; + } + + /* binary to decimal */ + f32_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); + + /* the sig length is 7 or 9 */ + sig_len = 7 + (sig_dec >= (u32)10000000) + (sig_dec >= (u32)100000000); + + /* the decimal point offset relative to the first digit */ + dot_ofs = sig_len + exp_dec; + + if (-6 < dot_ofs && dot_ofs <= 21) { + i32 num_sep_pos, dot_set_pos, pre_ofs; + u8 *num_hdr, *num_end, *num_sep, *dot_end; + bool no_pre_zero; + + /* fill zeros */ + memset(buf, '0', 32); + + /* not prefixed with zero, e.g. 1.234, 1234.0 */ + no_pre_zero = (dot_ofs > 0); + + /* write the number as digits */ + pre_ofs = no_pre_zero ? 0 : (2 - dot_ofs); + num_hdr = buf + pre_ofs; + num_end = write_u32_len_7_to_9_trim(sig_dec, num_hdr); + + /* seperate these digits to leave a space for dot */ + num_sep_pos = no_pre_zero ? dot_ofs : 0; + num_sep = num_hdr + num_sep_pos; + byte_move_8(num_sep + no_pre_zero, num_sep); + num_end += no_pre_zero; + + /* write the dot */ + dot_set_pos = yyjson_max(dot_ofs, 1); + buf[dot_set_pos] = '.'; + + /* return the ending */ + dot_end = buf + dot_ofs + 2; + return yyjson_max(dot_end, num_end); + + } else { + /* write with scientific notation, e.g. 1.234e56 */ + end = write_u32_len_7_to_9_trim(sig_dec, buf + 1); + end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */ + exp_dec += sig_len - 1; + buf[0] = buf[1]; + buf[1] = '.'; + return write_f32_exp(exp_dec, end); + } + + } else { + /* subnormal number */ + sig_bin = sig_raw; + exp_bin = 1 - F32_EXP_BIAS - F32_SIG_BITS; + + /* binary to decimal */ + f32_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); + + /* write significand part */ + end = write_u32_len_1_to_8(sig_dec, buf + 1); + buf[0] = buf[1]; + buf[1] = '.'; + exp_dec += (i32)(end - buf) - 2; + + /* trim trailing zeros */ + end -= *(end - 1) == '0'; /* branchless for last zero */ + end -= *(end - 1) == '0'; /* branchless for second last zero */ + while (*(end - 1) == '0') end--; /* for unlikely more zeros */ + end -= *(end - 1) == '.'; /* remove dot, e.g. 2.e-321 -> 2e-321 */ + + /* write exponent part */ + return write_f32_exp(exp_dec, end); + } +} + +/** + Write a double number (requires 40 bytes buffer). + We follow the ECMAScript specification for printing floating-point numbers, + similar to `Number.prototype.toString()`, but with the following changes: + 1. Keep the negative sign of `-0.0` to preserve input information. + 2. Keep decimal point to indicate the number is floating point. + 3. Remove positive sign in the exponent part. + */ +static_noinline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) { + u64 sig_bin, sig_dec, sig_raw; + i32 exp_bin, exp_dec, sig_len, dot_ofs; + u32 exp_raw; + u8 *end; + bool sign; + + /* decode raw bytes from IEEE-754 double format. */ + sign = (bool)(raw >> (F64_BITS - 1)); + sig_raw = raw & F64_SIG_MASK; + exp_raw = (u32)((raw & F64_EXP_MASK) >> F64_SIG_BITS); + + /* return inf or nan */ + if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) { + return write_inf_or_nan(buf, flg, sig_raw, sign); + } + + /* add sign for all finite number */ + buf[0] = '-'; + buf += sign; + + /* return zero */ + if ((raw << 1) == 0) { + byte_copy_4(buf, "0.0"); + return buf + 3; + } + + if (likely(exp_raw != 0)) { + /* normal number */ + sig_bin = sig_raw | ((u64)1 << F64_SIG_BITS); + exp_bin = (i32)exp_raw - F64_EXP_BIAS - F64_SIG_BITS; + + /* fast path for small integer number without fraction */ + if ((-F64_SIG_BITS <= exp_bin && exp_bin <= 0) && + (u64_tz_bits(sig_bin) >= (u32)-exp_bin)) { + sig_dec = sig_bin >> -exp_bin; /* range: [1, 0x1FFFFFFFFFFFFF] */ + buf = write_u64_len_1_to_16(sig_dec, buf); + byte_copy_2(buf, ".0"); + return buf + 2; + } + + /* binary to decimal */ + f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); + + /* the sig length is 16 or 17 */ + sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); + + /* the decimal point offset relative to the first digit */ + dot_ofs = sig_len + exp_dec; + + if (-6 < dot_ofs && dot_ofs <= 21) { + i32 num_sep_pos, dot_set_pos, pre_ofs; + u8 *num_hdr, *num_end, *num_sep, *dot_end; + bool no_pre_zero; + + /* fill zeros */ + memset(buf, '0', 32); + + /* not prefixed with zero, e.g. 1.234, 1234.0 */ + no_pre_zero = (dot_ofs > 0); + + /* write the number as digits */ + pre_ofs = no_pre_zero ? 0 : (2 - dot_ofs); + num_hdr = buf + pre_ofs; + num_end = write_u64_len_16_to_17_trim(sig_dec, num_hdr); + + /* seperate these digits to leave a space for dot */ + num_sep_pos = no_pre_zero ? dot_ofs : 0; + num_sep = num_hdr + num_sep_pos; + byte_move_16(num_sep + no_pre_zero, num_sep); + num_end += no_pre_zero; + + /* write the dot */ + dot_set_pos = yyjson_max(dot_ofs, 1); + buf[dot_set_pos] = '.'; + + /* return the ending */ + dot_end = buf + dot_ofs + 2; + return yyjson_max(dot_end, num_end); + + } else { + /* write with scientific notation, e.g. 1.234e56 */ + end = write_u64_len_16_to_17_trim(sig_dec, buf + 1); + end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */ + exp_dec += sig_len - 1; + buf[0] = buf[1]; + buf[1] = '.'; + return write_f64_exp(exp_dec, end); + } + + } else { + /* subnormal number */ + sig_bin = sig_raw; + exp_bin = 1 - F64_EXP_BIAS - F64_SIG_BITS; + + /* binary to decimal */ + f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); + + /* write significand part */ + end = write_u64_len_1_to_17(sig_dec, buf + 1); + buf[0] = buf[1]; + buf[1] = '.'; + exp_dec += (i32)(end - buf) - 2; + + /* trim trailing zeros */ + end -= *(end - 1) == '0'; /* branchless for last zero */ + end -= *(end - 1) == '0'; /* branchless for second last zero */ + while (*(end - 1) == '0') end--; /* for unlikely more zeros */ + end -= *(end - 1) == '.'; /* remove dot, e.g. 2.e-321 -> 2e-321 */ + + /* write exponent part */ + return write_f64_exp(exp_dec, end); + } +} + +/** + Write a double number using fixed-point notation (requires 40 bytes buffer). + + We follow the ECMAScript specification for printing floating-point numbers, + similar to `Number.prototype.toFixed(prec)`, but with the following changes: + 1. Keep the negative sign of `-0.0` to preserve input information. + 2. Keep decimal point to indicate the number is floating point. + 3. Remove positive sign in the exponent part. + 4. Remove trailing zeros and reduce unnecessary precision. + */ +static_noinline u8 *write_f64_raw_fixed(u8 *buf, u64 raw, yyjson_write_flag flg, + u32 prec) { + u64 sig_bin, sig_dec, sig_raw; + i32 exp_bin, exp_dec, sig_len, dot_ofs; + u32 exp_raw; + u8 *end; + bool sign; + + /* decode raw bytes from IEEE-754 double format. */ + sign = (bool)(raw >> (F64_BITS - 1)); + sig_raw = raw & F64_SIG_MASK; + exp_raw = (u32)((raw & F64_EXP_MASK) >> F64_SIG_BITS); + + /* return inf or nan */ + if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) { + return write_inf_or_nan(buf, flg, sig_raw, sign); + } + + /* add sign for all finite number */ + buf[0] = '-'; + buf += sign; + + /* return zero */ + if ((raw << 1) == 0) { + byte_copy_4(buf, "0.0"); + return buf + 3; + } + + if (likely(exp_raw != 0)) { + /* normal number */ + sig_bin = sig_raw | ((u64)1 << F64_SIG_BITS); + exp_bin = (i32)exp_raw - F64_EXP_BIAS - F64_SIG_BITS; + + /* fast path for small integer number without fraction */ + if ((-F64_SIG_BITS <= exp_bin && exp_bin <= 0) && + (u64_tz_bits(sig_bin) >= (u32)-exp_bin)) { + sig_dec = sig_bin >> -exp_bin; /* range: [1, 0x1FFFFFFFFFFFFF] */ + buf = write_u64_len_1_to_16(sig_dec, buf); + byte_copy_2(buf, ".0"); + return buf + 2; + } + + /* only `fabs(num) < 1e21` are processed here. */ + if ((raw << 1) < (U64(0x444B1AE4, 0xD6E2EF50) << 1)) { + i32 num_sep_pos, dot_set_pos, pre_ofs; + u8 *num_hdr, *num_end, *num_sep; + bool round_up, no_pre_zero; + + /* binary to decimal */ + f64_bin_to_dec_fast(sig_raw, exp_raw, sig_bin, exp_bin, + &sig_dec, &exp_dec, &round_up); + + /* the sig length is 16 or 17 */ + sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); + + /* limit the length of digits after the decimal point */ + if (exp_dec < -1) { + i32 sig_len_cut = -exp_dec - (i32)prec; + if (sig_len_cut > sig_len) { + byte_copy_4(buf, "0.0"); + return buf + 3; + } + if (sig_len_cut > 0) { + u64 div, mod, p10; + + /* remove round up */ + sig_dec -= round_up; + sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); + + /* cut off some digits */ + div_pow10(sig_dec, (u32)sig_len_cut, &div, &mod, &p10); + + /* add round up */ + sig_dec = div + (mod >= p10 / 2); + + /* update exp and sig length */ + exp_dec += sig_len_cut; + sig_len -= sig_len_cut; + sig_len += (sig_len >= 0) && + (sig_dec >= div_pow10_table[sig_len].p10); + } + if (sig_len <= 0) { + byte_copy_4(buf, "0.0"); + return buf + 3; + } + } + + /* fill zeros */ + memset(buf, '0', 32); + + /* the decimal point offset relative to the first digit */ + dot_ofs = sig_len + exp_dec; + + /* not prefixed with zero, e.g. 1.234, 1234.0 */ + no_pre_zero = (dot_ofs > 0); + + /* write the number as digits */ + pre_ofs = no_pre_zero ? 0 : (1 - dot_ofs); + num_hdr = buf + pre_ofs; + num_end = write_u64_len_1_to_17(sig_dec, num_hdr); + + /* seperate these digits to leave a space for dot */ + num_sep_pos = no_pre_zero ? dot_ofs : -dot_ofs; + num_sep = buf + num_sep_pos; + byte_move_16(num_sep + 1, num_sep); + num_end += (exp_dec < 0); + + /* write the dot */ + dot_set_pos = yyjson_max(dot_ofs, 1); + buf[dot_set_pos] = '.'; + + /* remove trailing zeros */ + buf += dot_set_pos + 2; + buf = yyjson_max(buf, num_end); + buf -= *(buf - 1) == '0'; /* branchless for last zero */ + buf -= *(buf - 1) == '0'; /* branchless for second last zero */ + while (*(buf - 1) == '0') buf--; /* for unlikely more zeros */ + buf += *(buf - 1) == '.'; /* keep a zero after dot */ + return buf; + + } else { + /* binary to decimal */ + f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, + &sig_dec, &exp_dec); + + /* the sig length is 16 or 17 */ + sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); + + /* write with scientific notation, e.g. 1.234e56 */ + end = write_u64_len_16_to_17_trim(sig_dec, buf + 1); + end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */ + exp_dec += sig_len - 1; + buf[0] = buf[1]; + buf[1] = '.'; + return write_f64_exp(exp_dec, end); + } + } else { + /* subnormal number */ + byte_copy_4(buf, "0.0"); + return buf + 3; + } +} + +#else /* FP_WRITER */ + +#if YYJSON_MSC_VER >= 1400 +#define snprintf_num(buf, len, fmt, dig, val) \ + sprintf_s((char *)buf, len, fmt, dig, val) +#elif defined(snprintf) || (YYJSON_STDC_VER >= 199901L) +#define snprintf_num(buf, len, fmt, dig, val) \ + snprintf((char *)buf, len, fmt, dig, val) +#else +#define snprintf_num(buf, len, fmt, dig, val) \ + sprintf((char *)buf, fmt, dig, val) +#endif + +static_noinline u8 *write_fp_reformat(u8 *buf, int len, + yyjson_write_flag flg, bool fixed) { + u8 *cur = buf; + if (unlikely(len < 1)) return NULL; + cur += (*cur == '-'); + if (unlikely(!char_is_digit(*cur))) { + /* nan, inf, or bad output */ + if (has_flg(INF_AND_NAN_AS_NULL)) { + byte_copy_4(buf, "null"); + return buf + 4; + } else if (has_allow(INF_AND_NAN)) { + if (*cur == 'i') { + byte_copy_8(cur, "Infinity"); + return cur + 8; + } else if (*cur == 'n') { + byte_copy_4(buf, "NaN"); + return buf + 3; + } + } + return NULL; + } else { + /* finite number */ + u8 *end = buf + len, *dot = NULL, *exp = NULL; + + /* + The snprintf() function is locale-dependent. For currently known + locales, (en, zh, ja, ko, am, he, hi) use '.' as the decimal point, + while other locales use ',' as the decimal point. we need to replace + ',' with '.' to avoid the locale setting. + */ + for (; cur < end; cur++) { + switch (*cur) { + case ',': *cur = '.'; /* fallthrough */ + case '.': dot = cur; break; + case 'e': exp = cur; break; + default: break; + } + } + if (fixed) { + /* remove trailing zeros */ + while (*(end - 1) == '0') end--; + end += *(end - 1) == '.'; + } else { + if (!dot && !exp) { + /* add decimal point, e.g. 123 -> 123.0 */ + byte_copy_2(end, ".0"); + end += 2; + } else if (exp) { + cur = exp + 1; + /* remove positive sign in the exponent part */ + if (*cur == '+') { + memmove(cur, cur + 1, (usize)(end - cur - 1)); + end--; + } + cur += (*cur == '-'); + /* remove leading zeros in the exponent part */ + if (*cur == '0') { + u8 *hdr = cur++; + while (*cur == '0') cur++; + memmove(hdr, cur, (usize)(end - cur)); + end -= (usize)(cur - hdr); + } + } + } + return end; + } +} + +/** Write a double number (requires 40 bytes buffer). */ +static_noinline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) { +#if defined(DBL_DECIMAL_DIG) && DBL_DECIMAL_DIG < F64_DEC_DIG + int dig = DBL_DECIMAL_DIG; +#else + int dig = F64_DEC_DIG; +#endif + f64 val = f64_from_bits(raw); + int len = snprintf_num(buf, FP_BUF_LEN, "%.*g", dig, val); + return write_fp_reformat(buf, len, flg, false); +} + +/** Write a double number (requires 40 bytes buffer). */ +static_noinline u8 *write_f32_raw(u8 *buf, u64 raw, yyjson_write_flag flg) { +#if defined(FLT_DECIMAL_DIG) && FLT_DECIMAL_DIG < F32_DEC_DIG + int dig = FLT_DECIMAL_DIG; +#else + int dig = F32_DEC_DIG; +#endif + f64 val = (f64)f64_to_f32(f64_from_bits(raw)); + int len = snprintf_num(buf, FP_BUF_LEN, "%.*g", dig, val); + return write_fp_reformat(buf, len, flg, false); +} + +/** Write a double number (requires 40 bytes buffer). */ +static_noinline u8 *write_f64_raw_fixed(u8 *buf, u64 raw, + yyjson_write_flag flg, u32 prec) { + f64 val = (f64)f64_from_bits(raw); + if (-1e21 < val && val < 1e21) { + int len = snprintf_num(buf, FP_BUF_LEN, "%.*f", (int)prec, val); + return write_fp_reformat(buf, len, flg, true); + } else { + return write_f64_raw(buf, raw, flg); + } +} + +#endif /* FP_WRITER */ + +/** Write a JSON number (requires 40 bytes buffer). */ +static_inline u8 *write_num(u8 *cur, yyjson_val *val, yyjson_write_flag flg) { + if (!(val->tag & YYJSON_SUBTYPE_REAL)) { + u64 pos = val->uni.u64; + u64 neg = ~pos + 1; + usize sign = ((val->tag & YYJSON_SUBTYPE_SINT) > 0) & ((i64)pos < 0); + *cur = '-'; + return write_u64(sign ? neg : pos, cur + sign); + } else { + u64 raw = val->uni.u64; + u32 val_fmt = (u32)(val->tag >> 32); + u32 all_fmt = flg; + u32 fmt = val_fmt | all_fmt; + if (likely(!(fmt >> (32 - YYJSON_WRITE_FP_FLAG_BITS)))) { + /* double to shortest */ + return write_f64_raw(cur, raw, flg); + } else if (fmt >> (32 - YYJSON_WRITE_FP_PREC_BITS)) { + /* double to fixed */ + u32 val_prec = val_fmt >> (32 - YYJSON_WRITE_FP_PREC_BITS); + u32 all_prec = all_fmt >> (32 - YYJSON_WRITE_FP_PREC_BITS); + u32 prec = val_prec ? val_prec : all_prec; + return write_f64_raw_fixed(cur, raw, flg, prec); + } else { + if (fmt & YYJSON_WRITE_FP_TO_FLOAT) { + /* float to shortest */ + return write_f32_raw(cur, raw, flg); + } else { + /* double to shortest */ + return write_f64_raw(cur, raw, flg); + } + } + } +} + +char *yyjson_write_number(const yyjson_val *val, char *buf) { + if (unlikely(!val || !buf)) return NULL; + switch (val->tag & YYJSON_TAG_MASK) { + case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT: { + buf = (char *)write_u64(val->uni.u64, (u8 *)buf); + *buf = '\0'; + return buf; + } + case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT: { + u64 pos = val->uni.u64; + u64 neg = ~pos + 1; + usize sign = ((i64)pos < 0); + *buf = '-'; + buf = (char *)write_u64(sign ? neg : pos, (u8 *)buf + sign); + *buf = '\0'; + return buf; + } + case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL: { + u64 raw = val->uni.u64; + u32 fmt = (u32)(val->tag >> 32); + u32 flg = YYJSON_WRITE_ALLOW_INF_AND_NAN; + if (likely(!(fmt >> (32 - YYJSON_WRITE_FP_FLAG_BITS)))) { + buf = (char *)write_f64_raw((u8 *)buf, raw, flg); + } else if (fmt >> (32 - YYJSON_WRITE_FP_PREC_BITS)) { + u32 prec = fmt >> (32 - YYJSON_WRITE_FP_PREC_BITS); + buf = (char *)write_f64_raw_fixed((u8 *)buf, raw, flg, prec); + } else { + if (fmt & YYJSON_WRITE_FP_TO_FLOAT) { + buf = (char *)write_f32_raw((u8 *)buf, raw, flg); + } else { + buf = (char *)write_f64_raw((u8 *)buf, raw, flg); + } + } + if (buf) *buf = '\0'; + return buf; + } + default: return NULL; + } +} + + + +/*============================================================================== + * MARK: - String Writer (Private) + *============================================================================*/ + +/** Character encode type, if (type > CHAR_ENC_ERR_1) bytes = type / 2; */ +typedef u8 char_enc_type; +#define CHAR_ENC_CPY_1 0 /* 1-byte UTF-8, copy. */ +#define CHAR_ENC_ERR_1 1 /* 1-byte UTF-8, error. */ +#define CHAR_ENC_ESC_A 2 /* 1-byte ASCII, escaped as '\x'. */ +#define CHAR_ENC_ESC_1 3 /* 1-byte UTF-8, escaped as '\uXXXX'. */ +#define CHAR_ENC_CPY_2 4 /* 2-byte UTF-8, copy. */ +#define CHAR_ENC_ESC_2 5 /* 2-byte UTF-8, escaped as '\uXXXX'. */ +#define CHAR_ENC_CPY_3 6 /* 3-byte UTF-8, copy. */ +#define CHAR_ENC_ESC_3 7 /* 3-byte UTF-8, escaped as '\uXXXX'. */ +#define CHAR_ENC_CPY_4 8 /* 4-byte UTF-8, copy. */ +#define CHAR_ENC_ESC_4 9 /* 4-byte UTF-8, escaped as '\uXXXX\uXXXX'. */ + +/** Character encode type table: don't escape unicode, don't escape '/'. + (generate with misc/make_tables.c) */ +static const char_enc_type enc_table_cpy[256] = { + 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1 +}; + +/** Character encode type table: don't escape unicode, escape '/'. + (generate with misc/make_tables.c) */ +static const char_enc_type enc_table_cpy_slash[256] = { + 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1 +}; + +/** Character encode type table: escape unicode, don't escape '/'. + (generate with misc/make_tables.c) */ +static const char_enc_type enc_table_esc[256] = { + 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1 +}; + +/** Character encode type table: escape unicode, escape '/'. + (generate with misc/make_tables.c) */ +static const char_enc_type enc_table_esc_slash[256] = { + 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1 +}; + +/** Escaped hex character table: ["00" "01" "02" ... "FD" "FE" "FF"]. + (generate with misc/make_tables.c) */ +yyjson_align(2) +static const u8 esc_hex_char_table[512] = { + '0', '0', '0', '1', '0', '2', '0', '3', + '0', '4', '0', '5', '0', '6', '0', '7', + '0', '8', '0', '9', '0', 'A', '0', 'B', + '0', 'C', '0', 'D', '0', 'E', '0', 'F', + '1', '0', '1', '1', '1', '2', '1', '3', + '1', '4', '1', '5', '1', '6', '1', '7', + '1', '8', '1', '9', '1', 'A', '1', 'B', + '1', 'C', '1', 'D', '1', 'E', '1', 'F', + '2', '0', '2', '1', '2', '2', '2', '3', + '2', '4', '2', '5', '2', '6', '2', '7', + '2', '8', '2', '9', '2', 'A', '2', 'B', + '2', 'C', '2', 'D', '2', 'E', '2', 'F', + '3', '0', '3', '1', '3', '2', '3', '3', + '3', '4', '3', '5', '3', '6', '3', '7', + '3', '8', '3', '9', '3', 'A', '3', 'B', + '3', 'C', '3', 'D', '3', 'E', '3', 'F', + '4', '0', '4', '1', '4', '2', '4', '3', + '4', '4', '4', '5', '4', '6', '4', '7', + '4', '8', '4', '9', '4', 'A', '4', 'B', + '4', 'C', '4', 'D', '4', 'E', '4', 'F', + '5', '0', '5', '1', '5', '2', '5', '3', + '5', '4', '5', '5', '5', '6', '5', '7', + '5', '8', '5', '9', '5', 'A', '5', 'B', + '5', 'C', '5', 'D', '5', 'E', '5', 'F', + '6', '0', '6', '1', '6', '2', '6', '3', + '6', '4', '6', '5', '6', '6', '6', '7', + '6', '8', '6', '9', '6', 'A', '6', 'B', + '6', 'C', '6', 'D', '6', 'E', '6', 'F', + '7', '0', '7', '1', '7', '2', '7', '3', + '7', '4', '7', '5', '7', '6', '7', '7', + '7', '8', '7', '9', '7', 'A', '7', 'B', + '7', 'C', '7', 'D', '7', 'E', '7', 'F', + '8', '0', '8', '1', '8', '2', '8', '3', + '8', '4', '8', '5', '8', '6', '8', '7', + '8', '8', '8', '9', '8', 'A', '8', 'B', + '8', 'C', '8', 'D', '8', 'E', '8', 'F', + '9', '0', '9', '1', '9', '2', '9', '3', + '9', '4', '9', '5', '9', '6', '9', '7', + '9', '8', '9', '9', '9', 'A', '9', 'B', + '9', 'C', '9', 'D', '9', 'E', '9', 'F', + 'A', '0', 'A', '1', 'A', '2', 'A', '3', + 'A', '4', 'A', '5', 'A', '6', 'A', '7', + 'A', '8', 'A', '9', 'A', 'A', 'A', 'B', + 'A', 'C', 'A', 'D', 'A', 'E', 'A', 'F', + 'B', '0', 'B', '1', 'B', '2', 'B', '3', + 'B', '4', 'B', '5', 'B', '6', 'B', '7', + 'B', '8', 'B', '9', 'B', 'A', 'B', 'B', + 'B', 'C', 'B', 'D', 'B', 'E', 'B', 'F', + 'C', '0', 'C', '1', 'C', '2', 'C', '3', + 'C', '4', 'C', '5', 'C', '6', 'C', '7', + 'C', '8', 'C', '9', 'C', 'A', 'C', 'B', + 'C', 'C', 'C', 'D', 'C', 'E', 'C', 'F', + 'D', '0', 'D', '1', 'D', '2', 'D', '3', + 'D', '4', 'D', '5', 'D', '6', 'D', '7', + 'D', '8', 'D', '9', 'D', 'A', 'D', 'B', + 'D', 'C', 'D', 'D', 'D', 'E', 'D', 'F', + 'E', '0', 'E', '1', 'E', '2', 'E', '3', + 'E', '4', 'E', '5', 'E', '6', 'E', '7', + 'E', '8', 'E', '9', 'E', 'A', 'E', 'B', + 'E', 'C', 'E', 'D', 'E', 'E', 'E', 'F', + 'F', '0', 'F', '1', 'F', '2', 'F', '3', + 'F', '4', 'F', '5', 'F', '6', 'F', '7', + 'F', '8', 'F', '9', 'F', 'A', 'F', 'B', + 'F', 'C', 'F', 'D', 'F', 'E', 'F', 'F' +}; + +/** Escaped single character table. (generate with misc/make_tables.c) */ +yyjson_align(2) +static const u8 esc_single_char_table[512] = { + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + '\\', 'b', '\\', 't', '\\', 'n', ' ', ' ', + '\\', 'f', '\\', 'r', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', '\\', '"', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', '\\', '/', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + '\\', '\\', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' +}; + +/** Returns the encode table with options. */ +static_inline const char_enc_type *get_enc_table_with_flag( + yyjson_write_flag flg) { + if (has_flg(ESCAPE_UNICODE)) { + if (has_flg(ESCAPE_SLASHES)) { + return enc_table_esc_slash; + } else { + return enc_table_esc; + } + } else { + if (has_flg(ESCAPE_SLASHES)) { + return enc_table_cpy_slash; + } else { + return enc_table_cpy; + } + } +} + +/** Write raw string. */ +static_inline u8 *write_raw(u8 *cur, const u8 *raw, usize raw_len) { + memcpy(cur, raw, raw_len); + return cur + raw_len; +} + +/** + Write string no-escape. + @param cur Buffer cursor. + @param str A UTF-8 string, null-terminator is not required. + @param str_len Length of string in bytes. + @return The buffer cursor after string. + */ +static_inline u8 *write_str_noesc(u8 *cur, const u8 *str, usize str_len) { + *cur++ = '"'; + while (str_len >= 16) { + byte_copy_16(cur, str); + cur += 16; + str += 16; + str_len -= 16; + } + while (str_len >= 4) { + byte_copy_4(cur, str); + cur += 4; + str += 4; + str_len -= 4; + } + while (str_len) { + *cur++ = *str++; + str_len -= 1; + } + *cur++ = '"'; + return cur; +} + +/** + Write UTF-8 string (requires len * 6 + 2 bytes buffer). + @param cur Buffer cursor. + @param esc Escape unicode. + @param inv Allow invalid unicode. + @param str A UTF-8 string, null-terminator is not required. + @param str_len Length of string in bytes. + @param enc_table Encode type table for character. + @return The buffer cursor after string, or NULL on invalid unicode. + */ +static_inline u8 *write_str(u8 *cur, bool esc, bool inv, + const u8 *str, usize str_len, + const char_enc_type *enc_table) { + /* The replacement character U+FFFD, used to indicate invalid character. */ + const v32 rep = {{ 'F', 'F', 'F', 'D' }}; + const v32 pre = {{ '\\', 'u', '0', '0' }}; + + const u8 *src = str; + const u8 *end = str + str_len; + *cur++ = '"'; + +copy_ascii: + /* + Copy continuous ASCII, loop unrolling, same as the following code: + + while (end > src) ( + if (unlikely(enc_table[*src])) break; + *cur++ = *src++; + ); + */ +#define expr_jump(i) \ + if (unlikely(enc_table[src[i]])) goto stop_char_##i; + +#define expr_stop(i) \ + stop_char_##i: \ + memcpy(cur, src, i); \ + cur += i; src += i; goto copy_utf8; + + while (end - src >= 16) { + repeat16_incr(expr_jump) + byte_copy_16(cur, src); + cur += 16; src += 16; + } + + while (end - src >= 4) { + repeat4_incr(expr_jump) + byte_copy_4(cur, src); + cur += 4; src += 4; + } + + while (end > src) { + expr_jump(0) + *cur++ = *src++; + } + + *cur++ = '"'; + return cur; + + repeat16_incr(expr_stop) + +#undef expr_jump +#undef expr_stop + +copy_utf8: + if (unlikely(src + 4 > end)) { + if (end == src) goto copy_end; + if (end - src < enc_table[*src] / 2) goto err_one; + } + switch (enc_table[*src]) { + case CHAR_ENC_CPY_1: { + *cur++ = *src++; + goto copy_ascii; + } + case CHAR_ENC_CPY_2: { +#if YYJSON_DISABLE_UTF8_VALIDATION + byte_copy_2(cur, src); +#else + u32 uni = 0; + byte_copy_2(&uni, src); + if (unlikely(!is_utf8_seq2(uni))) goto err_cpy; + byte_copy_2(cur, &uni); +#endif + cur += 2; + src += 2; + goto copy_utf8; + } + case CHAR_ENC_CPY_3: { +#if YYJSON_DISABLE_UTF8_VALIDATION + if (likely(src + 4 <= end)) { + byte_copy_4(cur, src); + } else { + byte_copy_2(cur, src); + cur[2] = src[2]; + } +#else + u32 uni, tmp; + if (likely(src + 4 <= end)) { + uni = byte_load_4(src); + if (unlikely(!is_utf8_seq3(uni))) goto err_cpy; + byte_copy_4(cur, src); + } else { + uni = byte_load_3(src); + if (unlikely(!is_utf8_seq3(uni))) goto err_cpy; + byte_copy_4(cur, &uni); + } +#endif + cur += 3; + src += 3; + goto copy_utf8; + } + case CHAR_ENC_CPY_4: { +#if YYJSON_DISABLE_UTF8_VALIDATION + byte_copy_4(cur, src); +#else + u32 uni, tmp; + uni = byte_load_4(src); + if (unlikely(!is_utf8_seq4(uni))) goto err_cpy; + byte_copy_4(cur, src); +#endif + cur += 4; + src += 4; + goto copy_utf8; + } + case CHAR_ENC_ESC_A: { + byte_copy_2(cur, &esc_single_char_table[*src * 2]); + cur += 2; + src += 1; + goto copy_utf8; + } + case CHAR_ENC_ESC_1: { + byte_copy_4(cur + 0, &pre); + byte_copy_2(cur + 4, &esc_hex_char_table[*src * 2]); + cur += 6; + src += 1; + goto copy_utf8; + } + case CHAR_ENC_ESC_2: { + u16 u; +#if !YYJSON_DISABLE_UTF8_VALIDATION + u32 v4 = 0; + u16 v2 = byte_load_2(src); + byte_copy_2(&v4, &v2); + if (unlikely(!is_utf8_seq2(v4))) goto err_esc; +#endif + u = (u16)(((u16)(src[0] & 0x1F) << 6) | + ((u16)(src[1] & 0x3F) << 0)); + byte_copy_2(cur + 0, &pre); + byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]); + byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]); + cur += 6; + src += 2; + goto copy_utf8; + } + case CHAR_ENC_ESC_3: { + u16 u; + u32 v, tmp; +#if !YYJSON_DISABLE_UTF8_VALIDATION + v = byte_load_3(src); + if (unlikely(!is_utf8_seq3(v))) goto err_esc; +#endif + u = (u16)(((u16)(src[0] & 0x0F) << 12) | + ((u16)(src[1] & 0x3F) << 6) | + ((u16)(src[2] & 0x3F) << 0)); + byte_copy_2(cur + 0, &pre); + byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]); + byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]); + cur += 6; + src += 3; + goto copy_utf8; + } + case CHAR_ENC_ESC_4: { + u32 hi, lo, u, v, tmp; +#if !YYJSON_DISABLE_UTF8_VALIDATION + v = byte_load_4(src); + if (unlikely(!is_utf8_seq4(v))) goto err_esc; +#endif + u = ((u32)(src[0] & 0x07) << 18) | + ((u32)(src[1] & 0x3F) << 12) | + ((u32)(src[2] & 0x3F) << 6) | + ((u32)(src[3] & 0x3F) << 0); + u -= 0x10000; + hi = (u >> 10) + 0xD800; + lo = (u & 0x3FF) + 0xDC00; + byte_copy_2(cur + 0, &pre); + byte_copy_2(cur + 2, &esc_hex_char_table[(hi >> 8) * 2]); + byte_copy_2(cur + 4, &esc_hex_char_table[(hi & 0xFF) * 2]); + byte_copy_2(cur + 6, &pre); + byte_copy_2(cur + 8, &esc_hex_char_table[(lo >> 8) * 2]); + byte_copy_2(cur + 10, &esc_hex_char_table[(lo & 0xFF) * 2]); + cur += 12; + src += 4; + goto copy_utf8; + } + case CHAR_ENC_ERR_1: { + goto err_one; + } + default: break; /* unreachable */ + } + +copy_end: + *cur++ = '"'; + return cur; + +err_one: + if (esc) goto err_esc; + else goto err_cpy; + +err_cpy: + if (!inv) return NULL; + *cur++ = *src++; + goto copy_utf8; + +err_esc: + if (!inv) return NULL; + byte_copy_2(cur + 0, &pre); + byte_copy_4(cur + 2, &rep); + cur += 6; + src += 1; + goto copy_utf8; +} + + + +/*============================================================================== + * MARK: - JSON Writer Utilities (Private) + *============================================================================*/ + +/** Write null (requires 8 bytes buffer). */ +static_inline u8 *write_null(u8 *cur) { + v64 v = {{ 'n', 'u', 'l', 'l', ',', '\n', 0, 0 }}; + byte_copy_8(cur, &v); + return cur + 4; +} + +/** Write bool (requires 8 bytes buffer). */ +static_inline u8 *write_bool(u8 *cur, bool val) { + v64 v0 = {{ 'f', 'a', 'l', 's', 'e', ',', '\n', 0 }}; + v64 v1 = {{ 't', 'r', 'u', 'e', ',', '\n', 0, 0 }}; + if (val) { + byte_copy_8(cur, &v1); + } else { + byte_copy_8(cur, &v0); + } + return cur + 5 - val; +} + +/** Write indent (requires level x 4 bytes buffer). + Param spaces should not larger than 4. */ +static_inline u8 *write_indent(u8 *cur, usize level, usize spaces) { + while (level-- > 0) { + byte_copy_4(cur, " "); + cur += spaces; + } + return cur; +} + +/** Write data to file pointer. */ +static bool write_dat_to_fp(FILE *fp, u8 *dat, usize len, + yyjson_write_err *err) { + if (fwrite(dat, len, 1, fp) != 1) { + err->msg = "file writing failed"; + err->code = YYJSON_WRITE_ERROR_FILE_WRITE; + return false; + } + return true; +} + +/** Write data to file. */ +static bool write_dat_to_file(const char *path, u8 *dat, usize len, + yyjson_write_err *err) { +#define return_err(_code, _msg) do { \ + err->msg = _msg; \ + err->code = YYJSON_WRITE_ERROR_##_code; \ + if (file) fclose(file); \ + return false; \ +} while (false) + + FILE *file = fopen_writeonly(path); + if (file == NULL) { + return_err(FILE_OPEN, MSG_FOPEN); + } + if (fwrite(dat, len, 1, file) != 1) { + return_err(FILE_WRITE, MSG_FWRITE); + } + if (fclose(file) != 0) { + file = NULL; + return_err(FILE_WRITE, MSG_FCLOSE); + } + return true; + +#undef return_err +} + + + +/*============================================================================== + * MARK: - JSON Writer Implementation (Private) + *============================================================================*/ + +typedef struct yyjson_write_ctx { + usize tag; +} yyjson_write_ctx; + +static_inline void yyjson_write_ctx_set(yyjson_write_ctx *ctx, + usize size, bool is_obj) { + ctx->tag = (size << 1) | (usize)is_obj; +} + +static_inline void yyjson_write_ctx_get(yyjson_write_ctx *ctx, + usize *size, bool *is_obj) { + usize tag = ctx->tag; + *size = tag >> 1; + *is_obj = (bool)(tag & 1); +} + +/** Write single JSON value. */ +static_inline u8 *yyjson_write_single(yyjson_val *val, + yyjson_write_flag flg, + yyjson_alc alc, + usize *dat_len, + yyjson_write_err *err) { +#define return_err(_code, _msg) do { \ + if (hdr) alc.free(alc.ctx, (void *)hdr); \ + *dat_len = 0; \ + err->code = YYJSON_WRITE_ERROR_##_code; \ + err->msg = _msg; \ + return NULL; \ +} while (false) + +#define incr_len(_len) do { \ + hdr = (u8 *)alc.malloc(alc.ctx, _len); \ + if (!hdr) goto fail_alloc; \ + cur = hdr; \ +} while (false) + +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) + + u8 *hdr = NULL, *cur; + usize str_len; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + bool newline = has_flg(NEWLINE_AT_END) != 0; + const usize end_len = 2; /* '\n' and '\0' */ + + switch (unsafe_yyjson_get_type(val)) { + case YYJSON_TYPE_RAW: + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len + end_len); + cur = write_raw(cur, str_ptr, str_len); + break; + + case YYJSON_TYPE_STR: + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 2 + end_len); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; + } + break; + + case YYJSON_TYPE_NUM: + incr_len(FP_BUF_LEN + end_len); + cur = write_num(cur, val, flg); + if (unlikely(!cur)) goto fail_num; + break; + + case YYJSON_TYPE_BOOL: + incr_len(8); + cur = write_bool(cur, unsafe_yyjson_get_bool(val)); + break; + + case YYJSON_TYPE_NULL: + incr_len(8); + cur = write_null(cur); + break; + + case YYJSON_TYPE_ARR: + incr_len(2 + end_len); + byte_copy_2(cur, "[]"); + cur += 2; + break; + + case YYJSON_TYPE_OBJ: + incr_len(2 + end_len); + byte_copy_2(cur, "{}"); + cur += 2; + break; + + default: + goto fail_type; + } + + if (newline) *cur++ = '\n'; + *cur = '\0'; + *dat_len = (usize)(cur - hdr); + memset(err, 0, sizeof(yyjson_write_err)); + return hdr; + +fail_alloc: return_err(MEMORY_ALLOCATION, MSG_MALLOC); +fail_type: return_err(INVALID_VALUE_TYPE, MSG_ERR_TYPE); +fail_num: return_err(NAN_OR_INF, MSG_NAN_INF); +fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + +#undef return_err +#undef check_str_len +#undef incr_len +} + +/** Write JSON document minify. + The root of this document should be a non-empty container. */ +static_inline u8 *yyjson_write_minify(const yyjson_val *root, + const yyjson_write_flag flg, + const yyjson_alc alc, + usize *dat_len, + yyjson_write_err *err) { +#define return_err(_code, _msg) do { \ + *dat_len = 0; \ + err->code = YYJSON_WRITE_ERROR_##_code; \ + err->msg = _msg; \ + if (hdr) alc.free(alc.ctx, hdr); \ + return NULL; \ +} while (false) + +#define incr_len(_len) do { \ + ext_len = (usize)(_len); \ + if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ + alc_inc = yyjson_max(alc_len / 2, ext_len); \ + alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \ + if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ + goto fail_alloc; \ + alc_len += alc_inc; \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + if (unlikely(!tmp)) goto fail_alloc; \ + ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ + ctx = ctx_tmp; \ + cur = tmp + cur_pos; \ + end = tmp + alc_len; \ + hdr = tmp; \ + } \ +} while (false) + +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) + + yyjson_val *val; + yyjson_type val_type; + usize ctn_len, ctn_len_tmp; + bool ctn_obj, ctn_obj_tmp, is_key; + u8 *hdr, *cur, *end, *tmp; + yyjson_write_ctx *ctx, *ctx_tmp; + usize alc_len, alc_inc, ctx_len, ext_len, str_len; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + bool newline = has_flg(NEWLINE_AT_END) != 0; + + alc_len = root->uni.ofs / sizeof(yyjson_val); + alc_len = alc_len * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; + alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); + if (!hdr) goto fail_alloc; + cur = hdr; + end = hdr + alc_len; + ctx = (yyjson_write_ctx *)(void *)end; + +doc_begin: + val = constcast(yyjson_val *)root; + val_type = unsafe_yyjson_get_type(val); + ctn_obj = (val_type == YYJSON_TYPE_OBJ); + ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + val++; + +val_begin: + val_type = unsafe_yyjson_get_type(val); + if (val_type == YYJSON_TYPE_STR) { + is_key = ((u8)ctn_obj & (u8)~ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 16); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; + } + *cur++ = is_key ? ':' : ','; + goto val_end; + } + if (val_type == YYJSON_TYPE_NUM) { + incr_len(FP_BUF_LEN); + cur = write_num(cur, val, flg); + if (unlikely(!cur)) goto fail_num; + *cur++ = ','; + goto val_end; + } + if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == + (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { + ctn_len_tmp = unsafe_yyjson_get_len(val); + ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); + incr_len(16); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); + *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); + *cur++ = ','; + goto val_end; + } else { + /* push context, setup new container */ + yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj); + ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; + ctn_obj = ctn_obj_tmp; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + val++; + goto val_begin; + } + } + if (val_type == YYJSON_TYPE_BOOL) { + incr_len(16); + cur = write_bool(cur, unsafe_yyjson_get_bool(val)); + cur++; + goto val_end; + } + if (val_type == YYJSON_TYPE_NULL) { + incr_len(16); + cur = write_null(cur); + cur++; + goto val_end; + } + if (val_type == YYJSON_TYPE_RAW) { + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len + 2); + cur = write_raw(cur, str_ptr, str_len); + *cur++ = ','; + goto val_end; + } + goto fail_type; + +val_end: + val++; + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + goto val_begin; + +ctn_end: + cur--; + *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); + *cur++ = ','; + if (unlikely((u8 *)ctx >= end)) goto doc_end; + yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj); + ctn_len--; + if (likely(ctn_len > 0)) { + goto val_begin; + } else { + goto ctn_end; + } + +doc_end: + if (newline) { + incr_len(2); + *(cur - 1) = '\n'; + cur++; + } + *--cur = '\0'; + *dat_len = (usize)(cur - hdr); + memset(err, 0, sizeof(yyjson_write_err)); + return hdr; + +fail_alloc: return_err(MEMORY_ALLOCATION, MSG_MALLOC); +fail_type: return_err(INVALID_VALUE_TYPE, MSG_ERR_TYPE); +fail_num: return_err(NAN_OR_INF, MSG_NAN_INF); +fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + +#undef return_err +#undef incr_len +#undef check_str_len +} + +/** Write JSON document pretty. + The root of this document should be a non-empty container. */ +static_inline u8 *yyjson_write_pretty(const yyjson_val *root, + const yyjson_write_flag flg, + const yyjson_alc alc, + usize *dat_len, + yyjson_write_err *err) { +#define return_err(_code, _msg) do { \ + *dat_len = 0; \ + err->code = YYJSON_WRITE_ERROR_##_code; \ + err->msg = _msg; \ + if (hdr) alc.free(alc.ctx, hdr); \ + return NULL; \ +} while (false) + +#define incr_len(_len) do { \ + ext_len = (usize)(_len); \ + if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ + alc_inc = yyjson_max(alc_len / 2, ext_len); \ + alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \ + if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ + goto fail_alloc; \ + alc_len += alc_inc; \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + if (unlikely(!tmp)) goto fail_alloc; \ + ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ + ctx = ctx_tmp; \ + cur = tmp + cur_pos; \ + end = tmp + alc_len; \ + hdr = tmp; \ + } \ +} while (false) + +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) + + yyjson_val *val; + yyjson_type val_type; + usize ctn_len, ctn_len_tmp; + bool ctn_obj, ctn_obj_tmp, is_key, no_indent; + u8 *hdr, *cur, *end, *tmp; + yyjson_write_ctx *ctx, *ctx_tmp; + usize alc_len, alc_inc, ctx_len, ext_len, str_len, level; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + usize spaces = has_flg(PRETTY_TWO_SPACES) ? 2 : 4; + bool newline = has_flg(NEWLINE_AT_END) != 0; + + alc_len = root->uni.ofs / sizeof(yyjson_val); + alc_len = alc_len * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; + alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); + if (!hdr) goto fail_alloc; + cur = hdr; + end = hdr + alc_len; + ctx = (yyjson_write_ctx *)(void *)end; + +doc_begin: + val = constcast(yyjson_val *)root; + val_type = unsafe_yyjson_get_type(val); + ctn_obj = (val_type == YYJSON_TYPE_OBJ); + ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + *cur++ = '\n'; + val++; + level = 1; + +val_begin: + val_type = unsafe_yyjson_get_type(val); + if (val_type == YYJSON_TYPE_STR) { + is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; + } + *cur++ = is_key ? ':' : ','; + *cur++ = is_key ? ' ' : '\n'; + goto val_end; + } + if (val_type == YYJSON_TYPE_NUM) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(FP_BUF_LEN + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_num(cur, val, flg); + if (unlikely(!cur)) goto fail_num; + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } + if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == + (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + ctn_len_tmp = unsafe_yyjson_get_len(val); + ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); + *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } else { + /* push context, setup new container */ + incr_len(32 + (no_indent ? 0 : level * 4)); + yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj); + ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; + ctn_obj = ctn_obj_tmp; + cur = write_indent(cur, no_indent ? 0 : level, spaces); + level++; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + *cur++ = '\n'; + val++; + goto val_begin; + } + } + if (val_type == YYJSON_TYPE_BOOL) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_bool(cur, unsafe_yyjson_get_bool(val)); + cur += 2; + goto val_end; + } + if (val_type == YYJSON_TYPE_NULL) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_null(cur); + cur += 2; + goto val_end; + } + if (val_type == YYJSON_TYPE_RAW) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len + 3 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_raw(cur, str_ptr, str_len); + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } + goto fail_type; + +val_end: + val++; + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + goto val_begin; + +ctn_end: + cur -= 2; + *cur++ = '\n'; + incr_len(level * 4); + cur = write_indent(cur, --level, spaces); + *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); + if (unlikely((u8 *)ctx >= end)) goto doc_end; + yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj); + ctn_len--; + *cur++ = ','; + *cur++ = '\n'; + if (likely(ctn_len > 0)) { + goto val_begin; + } else { + goto ctn_end; + } + +doc_end: + if (newline) { + incr_len(2); + *cur++ = '\n'; + } + *cur = '\0'; + *dat_len = (usize)(cur - hdr); + memset(err, 0, sizeof(yyjson_write_err)); + return hdr; + +fail_alloc: return_err(MEMORY_ALLOCATION, MSG_MALLOC); +fail_type: return_err(INVALID_VALUE_TYPE, MSG_ERR_TYPE); +fail_num: return_err(NAN_OR_INF, MSG_NAN_INF); +fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + +#undef return_err +#undef incr_len +#undef check_str_len +} + + + +/*============================================================================== + * MARK: - JSON Writer (Public) + *============================================================================*/ + +char *yyjson_val_write_opts(const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + usize tmp_dat_len; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + yyjson_val *root = constcast(yyjson_val *)val; + + if (!err) err = &tmp_err; + if (!dat_len) dat_len = &tmp_dat_len; + + if (unlikely(!root)) { + *dat_len = 0; + err->msg = "input JSON is NULL"; + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; + return NULL; + } + + if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) { + return (char *)yyjson_write_single(root, flg, alc, dat_len, err); + } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) { + return (char *)yyjson_write_pretty(root, flg, alc, dat_len, err); + } else { + return (char *)yyjson_write_minify(root, flg, alc, dat_len, err); + } +} + +char *yyjson_write_opts(const yyjson_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + yyjson_val *root = doc ? doc->root : NULL; + return yyjson_val_write_opts(root, flg, alc_ptr, dat_len, err); +} + +bool yyjson_val_write_file(const char *path, + const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + u8 *dat; + usize dat_len = 0; + yyjson_val *root = constcast(yyjson_val *)val; + bool suc; + + if (!err) err = &tmp_err; + if (unlikely(!path || !*path)) { + err->msg = "input path is invalid"; + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; + return false; + } + + dat = (u8 *)yyjson_val_write_opts(root, flg, &alc, &dat_len, err); + if (unlikely(!dat)) return false; + suc = write_dat_to_file(path, dat, dat_len, err); + alc.free(alc.ctx, dat); + return suc; +} + +bool yyjson_val_write_fp(FILE *fp, + const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + u8 *dat; + usize dat_len = 0; + yyjson_val *root = constcast(yyjson_val *)val; + bool suc; + + if (!err) err = &tmp_err; + if (unlikely(!fp)) { + err->msg = "input fp is invalid"; + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; + return false; + } + + dat = (u8 *)yyjson_val_write_opts(root, flg, &alc, &dat_len, err); + if (unlikely(!dat)) return false; + suc = write_dat_to_fp(fp, dat, dat_len, err); + alc.free(alc.ctx, dat); + return suc; +} + +bool yyjson_write_file(const char *path, + const yyjson_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_val *root = doc ? doc->root : NULL; + return yyjson_val_write_file(path, root, flg, alc_ptr, err); +} + +bool yyjson_write_fp(FILE *fp, + const yyjson_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_val *root = doc ? doc->root : NULL; + return yyjson_val_write_fp(fp, root, flg, alc_ptr, err); +} + + + +/*============================================================================== + * MARK: - Mutable JSON Writer Implementation (Private) + *============================================================================*/ + +typedef struct yyjson_mut_write_ctx { + usize tag; + yyjson_mut_val *ctn; +} yyjson_mut_write_ctx; + +static_inline void yyjson_mut_write_ctx_set(yyjson_mut_write_ctx *ctx, + yyjson_mut_val *ctn, + usize size, bool is_obj) { + ctx->tag = (size << 1) | (usize)is_obj; + ctx->ctn = ctn; +} + +static_inline void yyjson_mut_write_ctx_get(yyjson_mut_write_ctx *ctx, + yyjson_mut_val **ctn, + usize *size, bool *is_obj) { + usize tag = ctx->tag; + *size = tag >> 1; + *is_obj = (bool)(tag & 1); + *ctn = ctx->ctn; +} + +/** Get the estimated number of values for the mutable JSON document. */ +static_inline usize yyjson_mut_doc_estimated_val_num( + const yyjson_mut_doc *doc) { + usize sum = 0; + yyjson_val_chunk *chunk = doc->val_pool.chunks; + while (chunk) { + sum += chunk->chunk_size / sizeof(yyjson_mut_val) - 1; + if (chunk == doc->val_pool.chunks) { + sum -= (usize)(doc->val_pool.end - doc->val_pool.cur); + } + chunk = chunk->next; + } + return sum; +} + +/** Write single JSON value. */ +static_inline u8 *yyjson_mut_write_single(yyjson_mut_val *val, + yyjson_write_flag flg, + yyjson_alc alc, + usize *dat_len, + yyjson_write_err *err) { + return yyjson_write_single((yyjson_val *)val, flg, alc, dat_len, err); +} + +/** Write JSON document minify. + The root of this document should be a non-empty container. */ +static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, + usize estimated_val_num, + yyjson_write_flag flg, + yyjson_alc alc, + usize *dat_len, + yyjson_write_err *err) { +#define return_err(_code, _msg) do { \ + *dat_len = 0; \ + err->code = YYJSON_WRITE_ERROR_##_code; \ + err->msg = _msg; \ + if (hdr) alc.free(alc.ctx, hdr); \ + return NULL; \ +} while (false) + +#define incr_len(_len) do { \ + ext_len = (usize)(_len); \ + if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ + alc_inc = yyjson_max(alc_len / 2, ext_len); \ + alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ + if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ + goto fail_alloc; \ + alc_len += alc_inc; \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + if (unlikely(!tmp)) goto fail_alloc; \ + ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ + ctx = ctx_tmp; \ + cur = tmp + cur_pos; \ + end = tmp + alc_len; \ + hdr = tmp; \ + } \ +} while (false) + +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) + + yyjson_mut_val *val, *ctn; + yyjson_type val_type; + usize ctn_len, ctn_len_tmp; + bool ctn_obj, ctn_obj_tmp, is_key; + u8 *hdr, *cur, *end, *tmp; + yyjson_mut_write_ctx *ctx, *ctx_tmp; + usize alc_len, alc_inc, ctx_len, ext_len, str_len; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + bool newline = has_flg(NEWLINE_AT_END) != 0; + + alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; + alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); + if (!hdr) goto fail_alloc; + cur = hdr; + end = hdr + alc_len; + ctx = (yyjson_mut_write_ctx *)(void *)end; + +doc_begin: + val = constcast(yyjson_mut_val *)root; + val_type = unsafe_yyjson_get_type(val); + ctn_obj = (val_type == YYJSON_TYPE_OBJ); + ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + ctn = val; + val = (yyjson_mut_val *)val->uni.ptr; /* tail */ + val = ctn_obj ? val->next->next : val->next; + +val_begin: + val_type = unsafe_yyjson_get_type(val); + if (val_type == YYJSON_TYPE_STR) { + is_key = ((u8)ctn_obj & (u8)~ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 16); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; + } + *cur++ = is_key ? ':' : ','; + goto val_end; + } + if (val_type == YYJSON_TYPE_NUM) { + incr_len(FP_BUF_LEN); + cur = write_num(cur, (yyjson_val *)val, flg); + if (unlikely(!cur)) goto fail_num; + *cur++ = ','; + goto val_end; + } + if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == + (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { + ctn_len_tmp = unsafe_yyjson_get_len(val); + ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); + incr_len(16); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); + *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); + *cur++ = ','; + goto val_end; + } else { + /* push context, setup new container */ + yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); + ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; + ctn_obj = ctn_obj_tmp; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + ctn = val; + val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ + val = ctn_obj ? val->next->next : val->next; + goto val_begin; + } + } + if (val_type == YYJSON_TYPE_BOOL) { + incr_len(16); + cur = write_bool(cur, unsafe_yyjson_get_bool(val)); + cur++; + goto val_end; + } + if (val_type == YYJSON_TYPE_NULL) { + incr_len(16); + cur = write_null(cur); + cur++; + goto val_end; + } + if (val_type == YYJSON_TYPE_RAW) { + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len + 2); + cur = write_raw(cur, str_ptr, str_len); + *cur++ = ','; + goto val_end; + } + goto fail_type; + +val_end: + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + val = val->next; + goto val_begin; + +ctn_end: + cur--; + *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); + *cur++ = ','; + if (unlikely((u8 *)ctx >= end)) goto doc_end; + val = ctn->next; + yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj); + ctn_len--; + if (likely(ctn_len > 0)) { + goto val_begin; + } else { + goto ctn_end; + } + +doc_end: + if (newline) { + incr_len(2); + *(cur - 1) = '\n'; + cur++; + } + *--cur = '\0'; + *dat_len = (usize)(cur - hdr); + err->code = YYJSON_WRITE_SUCCESS; + err->msg = NULL; + return hdr; + +fail_alloc: return_err(MEMORY_ALLOCATION, MSG_MALLOC); +fail_type: return_err(INVALID_VALUE_TYPE, MSG_ERR_TYPE); +fail_num: return_err(NAN_OR_INF, MSG_NAN_INF); +fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + +#undef return_err +#undef incr_len +#undef check_str_len +} + +/** Write JSON document pretty. + The root of this document should be a non-empty container. */ +static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, + usize estimated_val_num, + yyjson_write_flag flg, + yyjson_alc alc, + usize *dat_len, + yyjson_write_err *err) { +#define return_err(_code, _msg) do { \ + *dat_len = 0; \ + err->code = YYJSON_WRITE_ERROR_##_code; \ + err->msg = _msg; \ + if (hdr) alc.free(alc.ctx, hdr); \ + return NULL; \ +} while (false) + +#define incr_len(_len) do { \ + ext_len = (usize)(_len); \ + if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ + alc_inc = yyjson_max(alc_len / 2, ext_len); \ + alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ + if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ + goto fail_alloc; \ + alc_len += alc_inc; \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + if (unlikely(!tmp)) goto fail_alloc; \ + ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ + ctx = ctx_tmp; \ + cur = tmp + cur_pos; \ + end = tmp + alc_len; \ + hdr = tmp; \ + } \ +} while (false) + +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) + + yyjson_mut_val *val, *ctn; + yyjson_type val_type; + usize ctn_len, ctn_len_tmp; + bool ctn_obj, ctn_obj_tmp, is_key, no_indent; + u8 *hdr, *cur, *end, *tmp; + yyjson_mut_write_ctx *ctx, *ctx_tmp; + usize alc_len, alc_inc, ctx_len, ext_len, str_len, level; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + usize spaces = has_flg(PRETTY_TWO_SPACES) ? 2 : 4; + bool newline = has_flg(NEWLINE_AT_END) != 0; + + alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; + alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); + if (!hdr) goto fail_alloc; + cur = hdr; + end = hdr + alc_len; + ctx = (yyjson_mut_write_ctx *)(void *)end; + +doc_begin: + val = constcast(yyjson_mut_val *)root; + val_type = unsafe_yyjson_get_type(val); + ctn_obj = (val_type == YYJSON_TYPE_OBJ); + ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + *cur++ = '\n'; + ctn = val; + val = (yyjson_mut_val *)val->uni.ptr; /* tail */ + val = ctn_obj ? val->next->next : val->next; + level = 1; + +val_begin: + val_type = unsafe_yyjson_get_type(val); + if (val_type == YYJSON_TYPE_STR) { + is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; + } + *cur++ = is_key ? ':' : ','; + *cur++ = is_key ? ' ' : '\n'; + goto val_end; + } + if (val_type == YYJSON_TYPE_NUM) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(FP_BUF_LEN + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_num(cur, (yyjson_val *)val, flg); + if (unlikely(!cur)) goto fail_num; + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } + if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == + (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + ctn_len_tmp = unsafe_yyjson_get_len(val); + ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); + *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } else { + /* push context, setup new container */ + incr_len(32 + (no_indent ? 0 : level * 4)); + yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); + ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; + ctn_obj = ctn_obj_tmp; + cur = write_indent(cur, no_indent ? 0 : level, spaces); + level++; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + *cur++ = '\n'; + ctn = val; + val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ + val = ctn_obj ? val->next->next : val->next; + goto val_begin; + } + } + if (val_type == YYJSON_TYPE_BOOL) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_bool(cur, unsafe_yyjson_get_bool(val)); + cur += 2; + goto val_end; + } + if (val_type == YYJSON_TYPE_NULL) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_null(cur); + cur += 2; + goto val_end; + } + if (val_type == YYJSON_TYPE_RAW) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len + 3 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_raw(cur, str_ptr, str_len); + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } + goto fail_type; + +val_end: + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + val = val->next; + goto val_begin; + +ctn_end: + cur -= 2; + *cur++ = '\n'; + incr_len(level * 4); + cur = write_indent(cur, --level, spaces); + *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); + if (unlikely((u8 *)ctx >= end)) goto doc_end; + val = ctn->next; + yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj); + ctn_len--; + *cur++ = ','; + *cur++ = '\n'; + if (likely(ctn_len > 0)) { + goto val_begin; + } else { + goto ctn_end; + } + +doc_end: + if (newline) { + incr_len(2); + *cur++ = '\n'; + } + *cur = '\0'; + *dat_len = (usize)(cur - hdr); + err->code = YYJSON_WRITE_SUCCESS; + err->msg = NULL; + return hdr; + +fail_alloc: return_err(MEMORY_ALLOCATION, MSG_MALLOC); +fail_type: return_err(INVALID_VALUE_TYPE, MSG_ERR_TYPE); +fail_num: return_err(NAN_OR_INF, MSG_NAN_INF); +fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + +#undef return_err +#undef incr_len +#undef check_str_len +} + +static char *yyjson_mut_write_opts_impl(const yyjson_mut_val *val, + usize estimated_val_num, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + usize tmp_dat_len; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + yyjson_mut_val *root = constcast(yyjson_mut_val *)val; + + if (!err) err = &tmp_err; + if (!dat_len) dat_len = &tmp_dat_len; + + if (unlikely(!root)) { + *dat_len = 0; + err->msg = "input JSON is NULL"; + err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; + return NULL; + } + + if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) { + return (char *)yyjson_mut_write_single(root, flg, alc, dat_len, err); + } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) { + return (char *)yyjson_mut_write_pretty(root, estimated_val_num, + flg, alc, dat_len, err); + } else { + return (char *)yyjson_mut_write_minify(root, estimated_val_num, + flg, alc, dat_len, err); + } +} + + + +/*============================================================================== + * MARK: - Mutable JSON Writer (Public) + *============================================================================*/ + +char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + return yyjson_mut_write_opts_impl(val, 0, flg, alc_ptr, dat_len, err); +} + +char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + yyjson_mut_val *root; + usize estimated_val_num; + if (likely(doc)) { + root = doc->root; + estimated_val_num = yyjson_mut_doc_estimated_val_num(doc); + } else { + root = NULL; + estimated_val_num = 0; + } + return yyjson_mut_write_opts_impl(root, estimated_val_num, + flg, alc_ptr, dat_len, err); +} + +bool yyjson_mut_val_write_file(const char *path, + const yyjson_mut_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + u8 *dat; + usize dat_len = 0; + yyjson_mut_val *root = constcast(yyjson_mut_val *)val; + bool suc; + + if (!err) err = &tmp_err; + if (unlikely(!path || !*path)) { + err->msg = "input path is invalid"; + err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; + return false; + } + + dat = (u8 *)yyjson_mut_val_write_opts(root, flg, &alc, &dat_len, err); + if (unlikely(!dat)) return false; + suc = write_dat_to_file(path, dat, dat_len, err); + alc.free(alc.ctx, dat); + return suc; +} + +bool yyjson_mut_val_write_fp(FILE *fp, + const yyjson_mut_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + u8 *dat; + usize dat_len = 0; + yyjson_mut_val *root = constcast(yyjson_mut_val *)val; + bool suc; + + if (!err) err = &tmp_err; + if (unlikely(!fp)) { + err->msg = "input fp is invalid"; + err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; + return false; + } + + dat = (u8 *)yyjson_mut_val_write_opts(root, flg, &alc, &dat_len, err); + if (unlikely(!dat)) return false; + suc = write_dat_to_fp(fp, dat, dat_len, err); + alc.free(alc.ctx, dat); + return suc; +} + +bool yyjson_mut_write_file(const char *path, + const yyjson_mut_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_mut_val *root = doc ? doc->root : NULL; + return yyjson_mut_val_write_file(path, root, flg, alc_ptr, err); +} + +bool yyjson_mut_write_fp(FILE *fp, + const yyjson_mut_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_mut_val *root = doc ? doc->root : NULL; + return yyjson_mut_val_write_fp(fp, root, flg, alc_ptr, err); +} + +#undef has_flg +#undef has_allow +#endif /* YYJSON_DISABLE_WRITER */ + + + +#if !YYJSON_DISABLE_UTILS + +/*============================================================================== + * MARK: - JSON Pointer API (RFC 6901) (Public) + *============================================================================*/ + +/** + Get a token from JSON pointer string. + @param ptr [in] string that points to current token prefix `/` + [out] string that points to next token prefix `/`, or string end + @param end [in] end of the entire JSON Pointer string + @param len [out] unescaped token length + @param esc [out] number of escaped characters in this token + @return head of the token, or NULL if syntax error + */ +static_inline const char *ptr_next_token(const char **ptr, const char *end, + usize *len, usize *esc) { + const char *hdr = *ptr + 1; + const char *cur = hdr; + /* skip unescaped characters */ + while (cur < end && *cur != '/' && *cur != '~') cur++; + if (likely(cur == end || *cur != '~')) { + /* no escaped characters, return */ + *ptr = cur; + *len = (usize)(cur - hdr); + *esc = 0; + return hdr; + } else { + /* handle escaped characters */ + usize esc_num = 0; + while (cur < end && *cur != '/') { + if (*cur++ == '~') { + if (cur == end || (*cur != '0' && *cur != '1')) { + *ptr = cur - 1; + return NULL; + } + esc_num++; + } + } + *ptr = cur; + *len = (usize)(cur - hdr) - esc_num; + *esc = esc_num; + return hdr; + } +} + +/** + Convert token string to index. + @param cur [in] token head + @param len [in] token length + @param idx [out] the index number, or USIZE_MAX if token is '-' + @return true if token is a valid array index + */ +static_inline bool ptr_token_to_idx(const char *cur, usize len, usize *idx) { + const char *end = cur + len; + usize num = 0, add; + if (unlikely(len == 0 || len > USIZE_SAFE_DIG)) return false; + if (*cur == '0') { + if (unlikely(len > 1)) return false; + *idx = 0; + return true; + } + if (*cur == '-') { + if (unlikely(len > 1)) return false; + *idx = USIZE_MAX; + return true; + } + for (; cur < end && (add = (usize)((u8)*cur - (u8)'0')) <= 9; cur++) { + num = num * 10 + add; + } + if (unlikely(num == 0 || cur < end)) return false; + *idx = num; + return true; +} + +/** + Compare JSON key with token. + @param key a string key (yyjson_val or yyjson_mut_val) + @param token a JSON pointer token + @param len unescaped token length + @param esc number of escaped characters in this token + @return true if `str` is equals to `token` + */ +static_inline bool ptr_token_eq(void *key, + const char *token, usize len, usize esc) { + yyjson_val *val = (yyjson_val *)key; + if (unsafe_yyjson_get_len(val) != len) return false; + if (likely(!esc)) { + return memcmp(val->uni.str, token, len) == 0; + } else { + const char *str = val->uni.str; + for (; len-- > 0; token++, str++) { + if (*token == '~') { + if (*str != (*++token == '0' ? '~' : '/')) return false; + } else { + if (*str != *token) return false; + } + } + return true; + } +} + +/** + Get a value from array by token. + @param arr an array, should not be NULL or non-array type + @param token a JSON pointer token + @param len unescaped token length + @param esc number of escaped characters in this token + @return value at index, or NULL if token is not index or index is out of range + */ +static_inline yyjson_val *ptr_arr_get(yyjson_val *arr, const char *token, + usize len, usize esc) { + yyjson_val *val = unsafe_yyjson_get_first(arr); + usize num = unsafe_yyjson_get_len(arr), idx = 0; + if (unlikely(num == 0)) return NULL; + if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; + if (unlikely(idx >= num)) return NULL; + if (unsafe_yyjson_arr_is_flat(arr)) { + return val + idx; + } else { + while (idx-- > 0) val = unsafe_yyjson_get_next(val); + return val; + } +} + +/** + Get a value from object by token. + @param obj [in] an object, should not be NULL or non-object type + @param token [in] a JSON pointer token + @param len [in] unescaped token length + @param esc [in] number of escaped characters in this token + @return value associated with the token, or NULL if no value + */ +static_inline yyjson_val *ptr_obj_get(yyjson_val *obj, const char *token, + usize len, usize esc) { + yyjson_val *key = unsafe_yyjson_get_first(obj); + usize num = unsafe_yyjson_get_len(obj); + if (unlikely(num == 0)) return NULL; + for (; num > 0; num--, key = unsafe_yyjson_get_next(key + 1)) { + if (ptr_token_eq(key, token, len, esc)) return key + 1; + } + return NULL; +} + +/** + Get a value from array by token. + @param arr [in] an array, should not be NULL or non-array type + @param token [in] a JSON pointer token + @param len [in] unescaped token length + @param esc [in] number of escaped characters in this token + @param pre [out] previous (sibling) value of the returned value + @param last [out] whether index is last + @return value at index, or NULL if token is not index or index is out of range + */ +static_inline yyjson_mut_val *ptr_mut_arr_get(yyjson_mut_val *arr, + const char *token, + usize len, usize esc, + yyjson_mut_val **pre, + bool *last) { + yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; /* last (tail) */ + usize num = unsafe_yyjson_get_len(arr), idx; + if (last) *last = false; + if (pre) *pre = NULL; + if (unlikely(num == 0)) { + if (last && len == 1 && (*token == '0' || *token == '-')) *last = true; + return NULL; + } + if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; + if (last) *last = (idx == num || idx == USIZE_MAX); + if (unlikely(idx >= num)) return NULL; + while (idx-- > 0) val = val->next; + if (pre) *pre = val; + return val->next; +} + +/** + Get a value from object by token. + @param obj [in] an object, should not be NULL or non-object type + @param token [in] a JSON pointer token + @param len [in] unescaped token length + @param esc [in] number of escaped characters in this token + @param pre [out] previous (sibling) key of the returned value's key + @return value associated with the token, or NULL if no value + */ +static_inline yyjson_mut_val *ptr_mut_obj_get(yyjson_mut_val *obj, + const char *token, + usize len, usize esc, + yyjson_mut_val **pre) { + yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr, *key; + usize num = unsafe_yyjson_get_len(obj); + if (pre) *pre = NULL; + if (unlikely(num == 0)) return NULL; + for (; num > 0; num--, pre_key = key) { + key = pre_key->next->next; + if (ptr_token_eq(key, token, len, esc)) { + if (pre) *pre = pre_key; + return key->next; + } + } + return NULL; +} + +/** + Create a string value with JSON pointer token. + @param token [in] a JSON pointer token + @param len [in] unescaped token length + @param esc [in] number of escaped characters in this token + @param doc [in] used for memory allocation when creating value + @return new string value, or NULL if memory allocation failed + */ +static_inline yyjson_mut_val *ptr_new_key(const char *token, + usize len, usize esc, + yyjson_mut_doc *doc) { + const char *src = token; + if (likely(!esc)) { + return yyjson_mut_strncpy(doc, src, len); + } else { + const char *end = src + len + esc; + char *dst = unsafe_yyjson_mut_str_alc(doc, len + esc); + char *str = dst; + if (unlikely(!dst)) return NULL; + for (; src < end; src++, dst++) { + if (*src != '~') *dst = *src; + else *dst = (*++src == '0' ? '~' : '/'); + } + *dst = '\0'; + return yyjson_mut_strn(doc, str, len); + } +} + +/* macros for yyjson_ptr */ +#define return_err(_ret, _code, _pos, _msg) do { \ + if (err) { \ + err->code = YYJSON_PTR_ERR_##_code; \ + err->msg = _msg; \ + err->pos = (usize)(_pos); \ + } \ + return _ret; \ +} while (false) + +#define return_err_resolve(_ret, _pos) \ + return_err(_ret, RESOLVE, _pos, "JSON pointer cannot be resolved") +#define return_err_syntax(_ret, _pos) \ + return_err(_ret, SYNTAX, _pos, "invalid escaped character") +#define return_err_alloc(_ret) \ + return_err(_ret, MEMORY_ALLOCATION, 0, "failed to create value") + +yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, + const char *ptr, size_t ptr_len, + yyjson_ptr_err *err) { + + const char *hdr = ptr, *end = ptr + ptr_len, *token; + usize len, esc; + yyjson_type type; + + while (true) { + token = ptr_next_token(&ptr, end, &len, &esc); + if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr); + type = unsafe_yyjson_get_type(val); + if (type == YYJSON_TYPE_OBJ) { + val = ptr_obj_get(val, token, len, esc); + } else if (type == YYJSON_TYPE_ARR) { + val = ptr_arr_get(val, token, len, esc); + } else { + val = NULL; + } + if (!val) return_err_resolve(NULL, token - hdr); + if (ptr == end) return val; + } +} + +yyjson_mut_val *unsafe_yyjson_mut_ptr_getx( + yyjson_mut_val *val, const char *ptr, size_t ptr_len, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { + + const char *hdr = ptr, *end = ptr + ptr_len, *token; + usize len, esc; + yyjson_mut_val *ctn, *pre = NULL; + yyjson_type type; + bool idx_is_last = false; + + while (true) { + token = ptr_next_token(&ptr, end, &len, &esc); + if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr); + ctn = val; + type = unsafe_yyjson_get_type(val); + if (type == YYJSON_TYPE_OBJ) { + val = ptr_mut_obj_get(val, token, len, esc, &pre); + } else if (type == YYJSON_TYPE_ARR) { + val = ptr_mut_arr_get(val, token, len, esc, &pre, &idx_is_last); + } else { + val = NULL; + } + if (ctx && (ptr == end)) { + if (type == YYJSON_TYPE_OBJ || + (type == YYJSON_TYPE_ARR && (val || idx_is_last))) { + ctx->ctn = ctn; + ctx->pre = pre; + } + } + if (!val) return_err_resolve(NULL, token - hdr); + if (ptr == end) return val; + } +} + +bool unsafe_yyjson_mut_ptr_putx( + yyjson_mut_val *val, const char *ptr, size_t ptr_len, + yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, + bool insert_new, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { + + const char *hdr = ptr, *end = ptr + ptr_len, *token; + usize token_len, esc, ctn_len; + yyjson_mut_val *ctn, *key, *pre = NULL; + yyjson_mut_val *sep_ctn = NULL, *sep_key = NULL, *sep_val = NULL; + yyjson_type ctn_type; + bool idx_is_last = false; + + /* skip exist parent nodes */ + while (true) { + token = ptr_next_token(&ptr, end, &token_len, &esc); + if (unlikely(!token)) return_err_syntax(false, ptr - hdr); + ctn = val; + ctn_type = unsafe_yyjson_get_type(ctn); + if (ctn_type == YYJSON_TYPE_OBJ) { + val = ptr_mut_obj_get(ctn, token, token_len, esc, &pre); + } else if (ctn_type == YYJSON_TYPE_ARR) { + val = ptr_mut_arr_get(ctn, token, token_len, esc, &pre, + &idx_is_last); + } else return_err_resolve(false, token - hdr); + if (!val) break; + if (ptr == end) break; /* is last token */ + } + + /* create parent nodes if not exist */ + if (unlikely(ptr != end)) { /* not last token */ + if (!create_parent) return_err_resolve(false, token - hdr); + + /* add value at last index if container is array */ + if (ctn_type == YYJSON_TYPE_ARR) { + if (!idx_is_last || !insert_new) { + return_err_resolve(false, token - hdr); + } + val = yyjson_mut_obj(doc); + if (!val) return_err_alloc(false); + + /* delay attaching until all operations are completed */ + sep_ctn = ctn; + sep_key = NULL; + sep_val = val; + + /* move to next token */ + ctn = val; + val = NULL; + ctn_type = YYJSON_TYPE_OBJ; + token = ptr_next_token(&ptr, end, &token_len, &esc); + if (unlikely(!token)) return_err_resolve(false, token - hdr); + } + + /* container is object, create parent nodes */ + while (ptr != end) { /* not last token */ + key = ptr_new_key(token, token_len, esc, doc); + if (!key) return_err_alloc(false); + val = yyjson_mut_obj(doc); + if (!val) return_err_alloc(false); + + /* delay attaching until all operations are completed */ + if (!sep_ctn) { + sep_ctn = ctn; + sep_key = key; + sep_val = val; + } else { + yyjson_mut_obj_add(ctn, key, val); + } + + /* move to next token */ + ctn = val; + val = NULL; + token = ptr_next_token(&ptr, end, &token_len, &esc); + if (unlikely(!token)) return_err_syntax(false, ptr - hdr); + } + } + + /* JSON pointer is resolved, insert or replace target value */ + ctn_len = unsafe_yyjson_get_len(ctn); + if (ctn_type == YYJSON_TYPE_OBJ) { + if (ctx) ctx->ctn = ctn; + if (!val || insert_new) { + /* insert new key-value pair */ + key = ptr_new_key(token, token_len, esc, doc); + if (unlikely(!key)) return_err_alloc(false); + if (ctx) ctx->pre = ctn_len ? (yyjson_mut_val *)ctn->uni.ptr : key; + unsafe_yyjson_mut_obj_add(ctn, key, new_val, ctn_len); + } else { + /* replace exist value */ + key = pre->next->next; + if (ctx) ctx->pre = pre; + if (ctx) ctx->old = val; + yyjson_mut_obj_put(ctn, key, new_val); + } + } else { + /* array */ + if (ctx && (val || idx_is_last)) ctx->ctn = ctn; + if (insert_new) { + /* append new value */ + if (val) { + pre->next = new_val; + new_val->next = val; + if (ctx) ctx->pre = pre; + unsafe_yyjson_set_len(ctn, ctn_len + 1); + } else if (idx_is_last) { + if (ctx) ctx->pre = ctn_len ? + (yyjson_mut_val *)ctn->uni.ptr : new_val; + yyjson_mut_arr_append(ctn, new_val); + } else { + return_err_resolve(false, token - hdr); + } + } else { + /* replace exist value */ + if (!val) return_err_resolve(false, token - hdr); + if (ctn_len > 1) { + new_val->next = val->next; + pre->next = new_val; + if (ctn->uni.ptr == val) ctn->uni.ptr = new_val; + } else { + new_val->next = new_val; + ctn->uni.ptr = new_val; + pre = new_val; + } + if (ctx) ctx->pre = pre; + if (ctx) ctx->old = val; + } + } + + /* all operations are completed, attach the new components to the target */ + if (unlikely(sep_ctn)) { + if (sep_key) yyjson_mut_obj_add(sep_ctn, sep_key, sep_val); + else yyjson_mut_arr_append(sep_ctn, sep_val); + } + return true; +} + +yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex( + yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { + + yyjson_mut_val *cur_val; + yyjson_ptr_ctx cur_ctx; + memset(&cur_ctx, 0, sizeof(cur_ctx)); + if (!ctx) ctx = &cur_ctx; + cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); + if (!cur_val) return NULL; + + if (yyjson_mut_is_obj(ctx->ctn)) { + yyjson_mut_val *key = ctx->pre->next->next; + yyjson_mut_obj_put(ctx->ctn, key, new_val); + } else { + yyjson_ptr_ctx_replace(ctx, new_val); + } + ctx->old = cur_val; + return cur_val; +} + +yyjson_mut_val *unsafe_yyjson_mut_ptr_removex( + yyjson_mut_val *val, const char *ptr, size_t len, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { + + yyjson_mut_val *cur_val; + yyjson_ptr_ctx cur_ctx; + memset(&cur_ctx, 0, sizeof(cur_ctx)); + if (!ctx) ctx = &cur_ctx; + cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); + if (cur_val) { + if (yyjson_mut_is_obj(ctx->ctn)) { + yyjson_mut_val *key = ctx->pre->next->next; + yyjson_mut_obj_put(ctx->ctn, key, NULL); + } else { + yyjson_ptr_ctx_remove(ctx); + } + ctx->pre = NULL; + ctx->old = cur_val; + } + return cur_val; +} + +/* macros for yyjson_ptr */ +#undef return_err +#undef return_err_resolve +#undef return_err_syntax +#undef return_err_alloc + + + +/*============================================================================== + * MARK: - JSON Patch API (RFC 6902) (Public) + *============================================================================*/ + +/* JSON Patch operation */ +typedef enum patch_op { + PATCH_OP_ADD, /* path, value */ + PATCH_OP_REMOVE, /* path */ + PATCH_OP_REPLACE, /* path, value */ + PATCH_OP_MOVE, /* from, path */ + PATCH_OP_COPY, /* from, path */ + PATCH_OP_TEST, /* path, value */ + PATCH_OP_NONE /* invalid */ +} patch_op; + +static patch_op patch_op_get(yyjson_val *op) { + const char *str = op->uni.str; + switch (unsafe_yyjson_get_len(op)) { + case 3: + if (!memcmp(str, "add", 3)) return PATCH_OP_ADD; + return PATCH_OP_NONE; + case 4: + if (!memcmp(str, "move", 4)) return PATCH_OP_MOVE; + if (!memcmp(str, "copy", 4)) return PATCH_OP_COPY; + if (!memcmp(str, "test", 4)) return PATCH_OP_TEST; + return PATCH_OP_NONE; + case 6: + if (!memcmp(str, "remove", 6)) return PATCH_OP_REMOVE; + return PATCH_OP_NONE; + case 7: + if (!memcmp(str, "replace", 7)) return PATCH_OP_REPLACE; + return PATCH_OP_NONE; + default: + return PATCH_OP_NONE; + } +} + +/* macros for yyjson_patch */ +#define return_err(_code, _msg) do { \ + if (err->ptr.code == YYJSON_PTR_ERR_MEMORY_ALLOCATION) { \ + err->code = YYJSON_PATCH_ERROR_MEMORY_ALLOCATION; \ + err->msg = _msg; \ + memset(&err->ptr, 0, sizeof(yyjson_ptr_err)); \ + } else { \ + err->code = YYJSON_PATCH_ERROR_##_code; \ + err->msg = _msg; \ + err->idx = iter.idx ? iter.idx - 1 : 0; \ + } \ + return NULL; \ +} while (false) + +#define return_err_copy() \ + return_err(MEMORY_ALLOCATION, "failed to copy value") +#define return_err_key(_key) \ + return_err(MISSING_KEY, "missing key " _key) +#define return_err_val(_key) \ + return_err(INVALID_MEMBER, "invalid member " _key) + +#define ptr_get(_ptr) yyjson_mut_ptr_getx( \ + root, _ptr->uni.str, _ptr##_len, NULL, &err->ptr) +#define ptr_add(_ptr, _val) yyjson_mut_ptr_addx( \ + root, _ptr->uni.str, _ptr##_len, _val, doc, false, NULL, &err->ptr) +#define ptr_remove(_ptr) yyjson_mut_ptr_removex( \ + root, _ptr->uni.str, _ptr##_len, NULL, &err->ptr) +#define ptr_replace(_ptr, _val)yyjson_mut_ptr_replacex( \ + root, _ptr->uni.str, _ptr##_len, _val, NULL, &err->ptr) + +yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, + yyjson_val *orig, + yyjson_val *patch, + yyjson_patch_err *err) { + + yyjson_mut_val *root; + yyjson_val *obj; + yyjson_arr_iter iter; + yyjson_patch_err err_tmp; + if (!err) err = &err_tmp; + memset(err, 0, sizeof(*err)); + memset(&iter, 0, sizeof(iter)); + + if (unlikely(!doc || !orig || !patch)) { + return_err(INVALID_PARAMETER, "input parameter is NULL"); + } + if (unlikely(!yyjson_is_arr(patch))) { + return_err(INVALID_PARAMETER, "input patch is not array"); + } + root = yyjson_val_mut_copy(doc, orig); + if (unlikely(!root)) return_err_copy(); + + /* iterate through the patch array */ + yyjson_arr_iter_init(patch, &iter); + while ((obj = yyjson_arr_iter_next(&iter))) { + patch_op op_enum; + yyjson_val *op, *path, *from = NULL, *value; + yyjson_mut_val *val = NULL, *test; + usize path_len, from_len = 0; + if (unlikely(!unsafe_yyjson_is_obj(obj))) { + return_err(INVALID_OPERATION, "JSON patch operation is not object"); + } + + /* get required member: op */ + op = yyjson_obj_get(obj, "op"); + if (unlikely(!op)) return_err_key("`op`"); + if (unlikely(!yyjson_is_str(op))) return_err_val("`op`"); + op_enum = patch_op_get(op); + + /* get required member: path */ + path = yyjson_obj_get(obj, "path"); + if (unlikely(!path)) return_err_key("`path`"); + if (unlikely(!yyjson_is_str(path))) return_err_val("`path`"); + path_len = unsafe_yyjson_get_len(path); + + /* get required member: value, from */ + switch ((int)op_enum) { + case PATCH_OP_ADD: case PATCH_OP_REPLACE: case PATCH_OP_TEST: + value = yyjson_obj_get(obj, "value"); + if (unlikely(!value)) return_err_key("`value`"); + val = yyjson_val_mut_copy(doc, value); + if (unlikely(!val)) return_err_copy(); + break; + case PATCH_OP_MOVE: case PATCH_OP_COPY: + from = yyjson_obj_get(obj, "from"); + if (unlikely(!from)) return_err_key("`from`"); + if (unlikely(!yyjson_is_str(from))) return_err_val("`from`"); + from_len = unsafe_yyjson_get_len(from); + break; + default: + break; + } + + /* perform an operation */ + switch ((int)op_enum) { + case PATCH_OP_ADD: /* add(path, val) */ + if (unlikely(path_len == 0)) { root = val; break; } + if (unlikely(!ptr_add(path, val))) { + return_err(POINTER, "failed to add `path`"); + } + break; + case PATCH_OP_REMOVE: /* remove(path) */ + if (unlikely(!ptr_remove(path))) { + return_err(POINTER, "failed to remove `path`"); + } + break; + case PATCH_OP_REPLACE: /* replace(path, val) */ + if (unlikely(path_len == 0)) { root = val; break; } + if (unlikely(!ptr_replace(path, val))) { + return_err(POINTER, "failed to replace `path`"); + } + break; + case PATCH_OP_MOVE: /* val = remove(from), add(path, val) */ + if (unlikely(from_len == 0 && path_len == 0)) break; + val = ptr_remove(from); + if (unlikely(!val)) { + return_err(POINTER, "failed to remove `from`"); + } + if (unlikely(path_len == 0)) { root = val; break; } + if (unlikely(!ptr_add(path, val))) { + return_err(POINTER, "failed to add `path`"); + } + break; + case PATCH_OP_COPY: /* val = get(from).copy, add(path, val) */ + val = ptr_get(from); + if (unlikely(!val)) { + return_err(POINTER, "failed to get `from`"); + } + if (unlikely(path_len == 0)) { root = val; break; } + val = yyjson_mut_val_mut_copy(doc, val); + if (unlikely(!val)) return_err_copy(); + if (unlikely(!ptr_add(path, val))) { + return_err(POINTER, "failed to add `path`"); + } + break; + case PATCH_OP_TEST: /* test = get(path), test.eq(val) */ + test = ptr_get(path); + if (unlikely(!test)) { + return_err(POINTER, "failed to get `path`"); + } + if (unlikely(!yyjson_mut_equals(val, test))) { + return_err(EQUAL, "failed to test equal"); + } + break; + default: + return_err(INVALID_MEMBER, "unsupported `op`"); + } + } + return root; +} + +yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, + yyjson_mut_val *orig, + yyjson_mut_val *patch, + yyjson_patch_err *err) { + yyjson_mut_val *root, *obj; + yyjson_mut_arr_iter iter; + yyjson_patch_err err_tmp; + if (!err) err = &err_tmp; + memset(err, 0, sizeof(*err)); + memset(&iter, 0, sizeof(iter)); + + if (unlikely(!doc || !orig || !patch)) { + return_err(INVALID_PARAMETER, "input parameter is NULL"); + } + if (unlikely(!yyjson_mut_is_arr(patch))) { + return_err(INVALID_PARAMETER, "input patch is not array"); + } + root = yyjson_mut_val_mut_copy(doc, orig); + if (unlikely(!root)) return_err_copy(); + + /* iterate through the patch array */ + yyjson_mut_arr_iter_init(patch, &iter); + while ((obj = yyjson_mut_arr_iter_next(&iter))) { + patch_op op_enum; + yyjson_mut_val *op, *path, *from = NULL, *value; + yyjson_mut_val *val = NULL, *test; + usize path_len, from_len = 0; + if (!unsafe_yyjson_is_obj(obj)) { + return_err(INVALID_OPERATION, "JSON patch operation is not object"); + } + + /* get required member: op */ + op = yyjson_mut_obj_get(obj, "op"); + if (unlikely(!op)) return_err_key("`op`"); + if (unlikely(!yyjson_mut_is_str(op))) return_err_val("`op`"); + op_enum = patch_op_get((yyjson_val *)(void *)op); + + /* get required member: path */ + path = yyjson_mut_obj_get(obj, "path"); + if (unlikely(!path)) return_err_key("`path`"); + if (unlikely(!yyjson_mut_is_str(path))) return_err_val("`path`"); + path_len = unsafe_yyjson_get_len(path); + + /* get required member: value, from */ + switch ((int)op_enum) { + case PATCH_OP_ADD: case PATCH_OP_REPLACE: case PATCH_OP_TEST: + value = yyjson_mut_obj_get(obj, "value"); + if (unlikely(!value)) return_err_key("`value`"); + val = yyjson_mut_val_mut_copy(doc, value); + if (unlikely(!val)) return_err_copy(); + break; + case PATCH_OP_MOVE: case PATCH_OP_COPY: + from = yyjson_mut_obj_get(obj, "from"); + if (unlikely(!from)) return_err_key("`from`"); + if (unlikely(!yyjson_mut_is_str(from))) { + return_err_val("`from`"); + } + from_len = unsafe_yyjson_get_len(from); + break; + default: + break; + } + + /* perform an operation */ + switch ((int)op_enum) { + case PATCH_OP_ADD: /* add(path, val) */ + if (unlikely(path_len == 0)) { root = val; break; } + if (unlikely(!ptr_add(path, val))) { + return_err(POINTER, "failed to add `path`"); + } + break; + case PATCH_OP_REMOVE: /* remove(path) */ + if (unlikely(!ptr_remove(path))) { + return_err(POINTER, "failed to remove `path`"); + } + break; + case PATCH_OP_REPLACE: /* replace(path, val) */ + if (unlikely(path_len == 0)) { root = val; break; } + if (unlikely(!ptr_replace(path, val))) { + return_err(POINTER, "failed to replace `path`"); + } + break; + case PATCH_OP_MOVE: /* val = remove(from), add(path, val) */ + if (unlikely(from_len == 0 && path_len == 0)) break; + val = ptr_remove(from); + if (unlikely(!val)) { + return_err(POINTER, "failed to remove `from`"); + } + if (unlikely(path_len == 0)) { root = val; break; } + if (unlikely(!ptr_add(path, val))) { + return_err(POINTER, "failed to add `path`"); + } + break; + case PATCH_OP_COPY: /* val = get(from).copy, add(path, val) */ + val = ptr_get(from); + if (unlikely(!val)) { + return_err(POINTER, "failed to get `from`"); + } + if (unlikely(path_len == 0)) { root = val; break; } + val = yyjson_mut_val_mut_copy(doc, val); + if (unlikely(!val)) return_err_copy(); + if (unlikely(!ptr_add(path, val))) { + return_err(POINTER, "failed to add `path`"); + } + break; + case PATCH_OP_TEST: /* test = get(path), test.eq(val) */ + test = ptr_get(path); + if (unlikely(!test)) { + return_err(POINTER, "failed to get `path`"); + } + if (unlikely(!yyjson_mut_equals(val, test))) { + return_err(EQUAL, "failed to test equal"); + } + break; + default: + return_err(INVALID_MEMBER, "unsupported `op`"); + } + } + return root; +} + +/* macros for yyjson_patch */ +#undef return_err +#undef return_err_copy +#undef return_err_key +#undef return_err_val +#undef ptr_get +#undef ptr_add +#undef ptr_remove +#undef ptr_replace + + + +/*============================================================================== + * MARK: - JSON Merge-Patch API (RFC 7386) (Public) + *============================================================================*/ + +yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, + yyjson_val *orig, + yyjson_val *patch) { + usize idx, max; + yyjson_val *key, *orig_val, *patch_val, local_orig; + yyjson_mut_val *builder, *mut_key, *mut_val, *merged_val; + + if (unlikely(!yyjson_is_obj(patch))) { + return yyjson_val_mut_copy(doc, patch); + } + + builder = yyjson_mut_obj(doc); + if (unlikely(!builder)) return NULL; + + memset(&local_orig, 0, sizeof(local_orig)); + if (!yyjson_is_obj(orig)) { + orig = &local_orig; + orig->tag = builder->tag; + orig->uni = builder->uni; + } + + /* If orig is contributing, copy any items not modified by the patch */ + if (orig != &local_orig) { + yyjson_obj_foreach(orig, idx, max, key, orig_val) { + patch_val = yyjson_obj_getn(patch, + unsafe_yyjson_get_str(key), + unsafe_yyjson_get_len(key)); + if (!patch_val) { + mut_key = yyjson_val_mut_copy(doc, key); + mut_val = yyjson_val_mut_copy(doc, orig_val); + if (!yyjson_mut_obj_add(builder, mut_key, mut_val)) return NULL; + } + } + } + + /* Merge items modified by the patch. */ + yyjson_obj_foreach(patch, idx, max, key, patch_val) { + /* null indicates the field is removed. */ + if (unsafe_yyjson_is_null(patch_val)) { + continue; + } + mut_key = yyjson_val_mut_copy(doc, key); + orig_val = yyjson_obj_getn(orig, + unsafe_yyjson_get_str(key), + unsafe_yyjson_get_len(key)); + merged_val = yyjson_merge_patch(doc, orig_val, patch_val); + if (!yyjson_mut_obj_add(builder, mut_key, merged_val)) return NULL; + } + + return builder; +} + +yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, + yyjson_mut_val *orig, + yyjson_mut_val *patch) { + usize idx, max; + yyjson_mut_val *key, *orig_val, *patch_val, local_orig; + yyjson_mut_val *builder, *mut_key, *mut_val, *merged_val; + + if (unlikely(!yyjson_mut_is_obj(patch))) { + return yyjson_mut_val_mut_copy(doc, patch); + } + + builder = yyjson_mut_obj(doc); + if (unlikely(!builder)) return NULL; + + memset(&local_orig, 0, sizeof(local_orig)); + if (!yyjson_mut_is_obj(orig)) { + orig = &local_orig; + orig->tag = builder->tag; + orig->uni = builder->uni; + } + + /* If orig is contributing, copy any items not modified by the patch */ + if (orig != &local_orig) { + yyjson_mut_obj_foreach(orig, idx, max, key, orig_val) { + patch_val = yyjson_mut_obj_getn(patch, + unsafe_yyjson_get_str(key), + unsafe_yyjson_get_len(key)); + if (!patch_val) { + mut_key = yyjson_mut_val_mut_copy(doc, key); + mut_val = yyjson_mut_val_mut_copy(doc, orig_val); + if (!yyjson_mut_obj_add(builder, mut_key, mut_val)) return NULL; + } + } + } + + /* Merge items modified by the patch. */ + yyjson_mut_obj_foreach(patch, idx, max, key, patch_val) { + /* null indicates the field is removed. */ + if (unsafe_yyjson_is_null(patch_val)) { + continue; + } + mut_key = yyjson_mut_val_mut_copy(doc, key); + orig_val = yyjson_mut_obj_getn(orig, + unsafe_yyjson_get_str(key), + unsafe_yyjson_get_len(key)); + merged_val = yyjson_mut_merge_patch(doc, orig_val, patch_val); + if (!yyjson_mut_obj_add(builder, mut_key, merged_val)) return NULL; + } + + return builder; +} + +#endif /* YYJSON_DISABLE_UTILS */ diff --git a/deps/yyjson/yyjson.h b/deps/yyjson/yyjson.h new file mode 100644 index 0000000..5eb6d46 --- /dev/null +++ b/deps/yyjson/yyjson.h @@ -0,0 +1,8230 @@ +/*============================================================================== + Copyright (c) 2020 YaoYuan <ibireme@gmail.com> + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + *============================================================================*/ + +/** + @file yyjson.h + @date 2019-03-09 + @author YaoYuan + */ + +#ifndef YYJSON_H +#define YYJSON_H + + + +/*============================================================================== + * MARK: - Header Files + *============================================================================*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stddef.h> +#include <limits.h> +#include <string.h> +#include <float.h> + + + +/*============================================================================== + * MARK: - Compile-time Options + *============================================================================*/ + +/* + Define as 1 to disable JSON reader at compile-time. + This disables functions with "read" in their name. + Reduces binary size by about 60%. + */ +#ifndef YYJSON_DISABLE_READER +#endif + +/* + Define as 1 to disable JSON writer at compile-time. + This disables functions with "write" in their name. + Reduces binary size by about 30%. + */ +#ifndef YYJSON_DISABLE_WRITER +#endif + +/* + Define as 1 to disable JSON incremental reader at compile-time. + This disables functions with "incr" in their name. + */ +#ifndef YYJSON_DISABLE_INCR_READER +#endif + +/* + Define as 1 to disable JSON Pointer, JSON Patch and JSON Merge Patch supports. + This disables functions with "ptr" or "patch" in their name. + */ +#ifndef YYJSON_DISABLE_UTILS +#endif + +/* + Define as 1 to disable the fast floating-point number conversion in yyjson. + Libc's `strtod/snprintf` will be used instead. + + This reduces binary size by about 30%, but significantly slows down the + floating-point read/write speed. + */ +#ifndef YYJSON_DISABLE_FAST_FP_CONV +#endif + +/* + Define as 1 to disable non-standard JSON features support at compile-time, + such as YYJSON_READ_ALLOW_XXX and YYJSON_WRITE_ALLOW_XXX. + + This reduces binary size by about 10%, and slightly improves performance. + */ +#ifndef YYJSON_DISABLE_NON_STANDARD +#endif + +/* + Define as 1 to disable UTF-8 validation at compile-time. + + Use this if all input strings are guaranteed to be valid UTF-8 + (e.g. language-level String types are already validated). + + Disabling UTF-8 validation improves performance for non-ASCII strings by about + 3% to 7%. + + Note: If this flag is enabled while passing illegal UTF-8 strings, + the following errors may occur: + - Escaped characters may be ignored when parsing JSON strings. + - Ending quotes may be ignored when parsing JSON strings, causing the + string to merge with the next value. + - When serializing with `yyjson_mut_val`, the string's end may be accessed + out of bounds, potentially causing a segmentation fault. + */ +#ifndef YYJSON_DISABLE_UTF8_VALIDATION +#endif + +/* + Define as 1 to improve performance on architectures that do not support + unaligned memory access. + + Normally, this does not need to be set manually. See the C file for details. + */ +#ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS +#endif + +/* Define as 1 to export symbols when building this library as a Windows DLL. */ +#ifndef YYJSON_EXPORTS +#endif + +/* Define as 1 to import symbols when using this library as a Windows DLL. */ +#ifndef YYJSON_IMPORTS +#endif + +/* Define as 1 to include <stdint.h> for compilers without C99 support. */ +#ifndef YYJSON_HAS_STDINT_H +#endif + +/* Define as 1 to include <stdbool.h> for compilers without C99 support. */ +#ifndef YYJSON_HAS_STDBOOL_H +#endif + + + +/*============================================================================== + * MARK: - Compiler Macros + *============================================================================*/ + +/** compiler version (MSVC) */ +#ifdef _MSC_VER +# define YYJSON_MSC_VER _MSC_VER +#else +# define YYJSON_MSC_VER 0 +#endif + +/** compiler version (GCC) */ +#ifdef __GNUC__ +# define YYJSON_GCC_VER __GNUC__ +# if defined(__GNUC_PATCHLEVEL__) +# define yyjson_gcc_available(major, minor, patch) \ + ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) \ + >= (major * 10000 + minor * 100 + patch)) +# else +# define yyjson_gcc_available(major, minor, patch) \ + ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) \ + >= (major * 10000 + minor * 100 + patch)) +# endif +#else +# define YYJSON_GCC_VER 0 +# define yyjson_gcc_available(major, minor, patch) 0 +#endif + +/** real gcc check */ +#if defined(__GNUC__) && defined(__GNUC_MINOR__) && \ + !defined(__clang__) && !defined(__llvm__) && \ + !defined(__INTEL_COMPILER) && !defined(__ICC) && \ + !defined(__NVCC__) && !defined(__PGI) && !defined(__TINYC__) +# define YYJSON_IS_REAL_GCC 1 +#else +# define YYJSON_IS_REAL_GCC 0 +#endif + +/** C version (STDC) */ +#if defined(__STDC__) && (__STDC__ >= 1) && defined(__STDC_VERSION__) +# define YYJSON_STDC_VER __STDC_VERSION__ +#else +# define YYJSON_STDC_VER 0 +#endif + +/** C++ version */ +#if defined(__cplusplus) +# define YYJSON_CPP_VER __cplusplus +#else +# define YYJSON_CPP_VER 0 +#endif + +/** compiler builtin check (since gcc 10.0, clang 2.6, icc 2021) */ +#ifndef yyjson_has_builtin +# ifdef __has_builtin +# define yyjson_has_builtin(x) __has_builtin(x) +# else +# define yyjson_has_builtin(x) 0 +# endif +#endif + +/** compiler attribute check (since gcc 5.0, clang 2.9, icc 17) */ +#ifndef yyjson_has_attribute +# ifdef __has_attribute +# define yyjson_has_attribute(x) __has_attribute(x) +# else +# define yyjson_has_attribute(x) 0 +# endif +#endif + +/** compiler feature check (since clang 2.6, icc 17) */ +#ifndef yyjson_has_feature +# ifdef __has_feature +# define yyjson_has_feature(x) __has_feature(x) +# else +# define yyjson_has_feature(x) 0 +# endif +#endif + +/** include check (since gcc 5.0, clang 2.7, icc 16, msvc 2017 15.3) */ +#ifndef yyjson_has_include +# ifdef __has_include +# define yyjson_has_include(x) __has_include(x) +# else +# define yyjson_has_include(x) 0 +# endif +#endif + +/** inline for compiler */ +#ifndef yyjson_inline +# if YYJSON_MSC_VER >= 1200 +# define yyjson_inline __forceinline +# elif defined(_MSC_VER) +# define yyjson_inline __inline +# elif yyjson_has_attribute(always_inline) || YYJSON_GCC_VER >= 4 +# define yyjson_inline __inline__ __attribute__((always_inline)) +# elif defined(__clang__) || defined(__GNUC__) +# define yyjson_inline __inline__ +# elif defined(__cplusplus) || YYJSON_STDC_VER >= 199901L +# define yyjson_inline inline +# else +# define yyjson_inline +# endif +#endif + +/** noinline for compiler */ +#ifndef yyjson_noinline +# if YYJSON_MSC_VER >= 1400 +# define yyjson_noinline __declspec(noinline) +# elif yyjson_has_attribute(noinline) || YYJSON_GCC_VER >= 4 +# define yyjson_noinline __attribute__((noinline)) +# else +# define yyjson_noinline +# endif +#endif + +/** align for compiler */ +#ifndef yyjson_align +# if YYJSON_MSC_VER >= 1300 +# define yyjson_align(x) __declspec(align(x)) +# elif yyjson_has_attribute(aligned) || defined(__GNUC__) +# define yyjson_align(x) __attribute__((aligned(x))) +# elif YYJSON_CPP_VER >= 201103L +# define yyjson_align(x) alignas(x) +# else +# define yyjson_align(x) +# endif +#endif + +/** likely for compiler */ +#ifndef yyjson_likely +# if yyjson_has_builtin(__builtin_expect) || \ + (YYJSON_GCC_VER >= 4 && YYJSON_GCC_VER != 5) +# define yyjson_likely(expr) __builtin_expect(!!(expr), 1) +# else +# define yyjson_likely(expr) (expr) +# endif +#endif + +/** unlikely for compiler */ +#ifndef yyjson_unlikely +# if yyjson_has_builtin(__builtin_expect) || \ + (YYJSON_GCC_VER >= 4 && YYJSON_GCC_VER != 5) +# define yyjson_unlikely(expr) __builtin_expect(!!(expr), 0) +# else +# define yyjson_unlikely(expr) (expr) +# endif +#endif + +/** compile-time constant check for compiler */ +#ifndef yyjson_constant_p +# if yyjson_has_builtin(__builtin_constant_p) || (YYJSON_GCC_VER >= 3) +# define YYJSON_HAS_CONSTANT_P 1 +# define yyjson_constant_p(value) __builtin_constant_p(value) +# else +# define YYJSON_HAS_CONSTANT_P 0 +# define yyjson_constant_p(value) 0 +# endif +#endif + +/** deprecate warning */ +#ifndef yyjson_deprecated +# if YYJSON_MSC_VER >= 1400 +# define yyjson_deprecated(msg) __declspec(deprecated(msg)) +# elif yyjson_has_feature(attribute_deprecated_with_message) || \ + (YYJSON_GCC_VER > 4 || (YYJSON_GCC_VER == 4 && __GNUC_MINOR__ >= 5)) +# define yyjson_deprecated(msg) __attribute__((deprecated(msg))) +# elif YYJSON_GCC_VER >= 3 +# define yyjson_deprecated(msg) __attribute__((deprecated)) +# else +# define yyjson_deprecated(msg) +# endif +#endif + +/** function export */ +#ifndef yyjson_api +# if defined(_WIN32) +# if defined(YYJSON_EXPORTS) && YYJSON_EXPORTS +# define yyjson_api __declspec(dllexport) +# elif defined(YYJSON_IMPORTS) && YYJSON_IMPORTS +# define yyjson_api __declspec(dllimport) +# else +# define yyjson_api +# endif +# elif yyjson_has_attribute(visibility) || YYJSON_GCC_VER >= 4 +# define yyjson_api __attribute__((visibility("default"))) +# else +# define yyjson_api +# endif +#endif + +/** inline function export */ +#ifndef yyjson_api_inline +# define yyjson_api_inline static yyjson_inline +#endif + +/** stdint (C89 compatible) */ +#if (defined(YYJSON_HAS_STDINT_H) && YYJSON_HAS_STDINT_H) || \ + YYJSON_MSC_VER >= 1600 || YYJSON_STDC_VER >= 199901L || \ + defined(_STDINT_H) || defined(_STDINT_H_) || \ + defined(__CLANG_STDINT_H) || defined(_STDINT_H_INCLUDED) || \ + yyjson_has_include(<stdint.h>) +# include <stdint.h> +#elif defined(_MSC_VER) +# if _MSC_VER < 1300 + typedef signed char int8_t; + typedef signed short int16_t; + typedef signed int int32_t; + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; + typedef unsigned int uint32_t; + typedef signed __int64 int64_t; + typedef unsigned __int64 uint64_t; +# else + typedef signed __int8 int8_t; + typedef signed __int16 int16_t; + typedef signed __int32 int32_t; + typedef unsigned __int8 uint8_t; + typedef unsigned __int16 uint16_t; + typedef unsigned __int32 uint32_t; + typedef signed __int64 int64_t; + typedef unsigned __int64 uint64_t; +# endif +#else +# if UCHAR_MAX == 0xFFU + typedef signed char int8_t; + typedef unsigned char uint8_t; +# else +# error cannot find 8-bit integer type +# endif +# if USHRT_MAX == 0xFFFFU + typedef unsigned short uint16_t; + typedef signed short int16_t; +# elif UINT_MAX == 0xFFFFU + typedef unsigned int uint16_t; + typedef signed int int16_t; +# else +# error cannot find 16-bit integer type +# endif +# if UINT_MAX == 0xFFFFFFFFUL + typedef unsigned int uint32_t; + typedef signed int int32_t; +# elif ULONG_MAX == 0xFFFFFFFFUL + typedef unsigned long uint32_t; + typedef signed long int32_t; +# elif USHRT_MAX == 0xFFFFFFFFUL + typedef unsigned short uint32_t; + typedef signed short int32_t; +# else +# error cannot find 32-bit integer type +# endif +# if defined(__INT64_TYPE__) && defined(__UINT64_TYPE__) + typedef __INT64_TYPE__ int64_t; + typedef __UINT64_TYPE__ uint64_t; +# elif defined(__GNUC__) || defined(__clang__) +# if !defined(_SYS_TYPES_H) && !defined(__int8_t_defined) + __extension__ typedef long long int64_t; +# endif + __extension__ typedef unsigned long long uint64_t; +# elif defined(_LONG_LONG) || defined(__MWERKS__) || defined(_CRAYC) || \ + defined(__SUNPRO_C) || defined(__SUNPRO_CC) + typedef long long int64_t; + typedef unsigned long long uint64_t; +# elif (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || \ + defined(__WATCOM_INT64__) || defined (__alpha) || defined (__DECC) + typedef __int64 int64_t; + typedef unsigned __int64 uint64_t; +# else +# error cannot find 64-bit integer type +# endif +#endif + +/** stdbool (C89 compatible) */ +#if (defined(YYJSON_HAS_STDBOOL_H) && YYJSON_HAS_STDBOOL_H) || \ + (yyjson_has_include(<stdbool.h>) && !defined(__STRICT_ANSI__)) || \ + YYJSON_MSC_VER >= 1800 || YYJSON_STDC_VER >= 199901L +# include <stdbool.h> +#elif !defined(__bool_true_false_are_defined) +# define __bool_true_false_are_defined 1 +# if defined(__cplusplus) +# if defined(__GNUC__) && !defined(__STRICT_ANSI__) +# define _Bool bool +# if __cplusplus < 201103L +# define bool bool +# define false false +# define true true +# endif +# endif +# else +# define bool unsigned char +# define true 1 +# define false 0 +# endif +#endif + +/** char bit check */ +#if defined(CHAR_BIT) +# if CHAR_BIT != 8 +# error non 8-bit char is not supported +# endif +#endif + +/** + Microsoft Visual C++ 6.0 doesn't support converting number from u64 to f64: + error C2520: conversion from unsigned __int64 to double not implemented. + */ +#ifndef YYJSON_U64_TO_F64_NO_IMPL +# if (0 < YYJSON_MSC_VER) && (YYJSON_MSC_VER <= 1200) +# define YYJSON_U64_TO_F64_NO_IMPL 1 +# else +# define YYJSON_U64_TO_F64_NO_IMPL 0 +# endif +#endif + + + +/*============================================================================== + * MARK: - Compile Hint Begin + *============================================================================*/ + +/* extern "C" begin */ +#ifdef __cplusplus +extern "C" { +#endif + +/* warning suppress begin */ +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunused-function" +# pragma clang diagnostic ignored "-Wunused-parameter" +#elif defined(__GNUC__) +# if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +# pragma GCC diagnostic push +# endif +# pragma GCC diagnostic ignored "-Wunused-function" +# pragma GCC diagnostic ignored "-Wunused-parameter" +#elif defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable:4800) /* 'int': forcing value to 'true' or 'false' */ +#endif + + + +/*============================================================================== + * MARK: - Version + *============================================================================*/ + +/** The major version of yyjson. */ +#define YYJSON_VERSION_MAJOR 0 + +/** The minor version of yyjson. */ +#define YYJSON_VERSION_MINOR 12 + +/** The patch version of yyjson. */ +#define YYJSON_VERSION_PATCH 0 + +/** The version of yyjson in hex: `(major << 16) | (minor << 8) | (patch)`. */ +#define YYJSON_VERSION_HEX 0x000C00 + +/** The version string of yyjson. */ +#define YYJSON_VERSION_STRING "0.12.0" + +/** The version of yyjson in hex, same as `YYJSON_VERSION_HEX`. */ +yyjson_api uint32_t yyjson_version(void); + + + +/*============================================================================== + * MARK: - JSON Types + *============================================================================*/ + +/** Type of a JSON value (3 bit). */ +typedef uint8_t yyjson_type; +/** No type, invalid. */ +#define YYJSON_TYPE_NONE ((uint8_t)0) /* _____000 */ +/** Raw string type, no subtype. */ +#define YYJSON_TYPE_RAW ((uint8_t)1) /* _____001 */ +/** Null type: `null` literal, no subtype. */ +#define YYJSON_TYPE_NULL ((uint8_t)2) /* _____010 */ +/** Boolean type, subtype: TRUE, FALSE. */ +#define YYJSON_TYPE_BOOL ((uint8_t)3) /* _____011 */ +/** Number type, subtype: UINT, SINT, REAL. */ +#define YYJSON_TYPE_NUM ((uint8_t)4) /* _____100 */ +/** String type, subtype: NONE, NOESC. */ +#define YYJSON_TYPE_STR ((uint8_t)5) /* _____101 */ +/** Array type, no subtype. */ +#define YYJSON_TYPE_ARR ((uint8_t)6) /* _____110 */ +/** Object type, no subtype. */ +#define YYJSON_TYPE_OBJ ((uint8_t)7) /* _____111 */ + +/** Subtype of a JSON value (2 bit). */ +typedef uint8_t yyjson_subtype; +/** No subtype. */ +#define YYJSON_SUBTYPE_NONE ((uint8_t)(0 << 3)) /* ___00___ */ +/** False subtype: `false` literal. */ +#define YYJSON_SUBTYPE_FALSE ((uint8_t)(0 << 3)) /* ___00___ */ +/** True subtype: `true` literal. */ +#define YYJSON_SUBTYPE_TRUE ((uint8_t)(1 << 3)) /* ___01___ */ +/** Unsigned integer subtype: `uint64_t`. */ +#define YYJSON_SUBTYPE_UINT ((uint8_t)(0 << 3)) /* ___00___ */ +/** Signed integer subtype: `int64_t`. */ +#define YYJSON_SUBTYPE_SINT ((uint8_t)(1 << 3)) /* ___01___ */ +/** Real number subtype: `double`. */ +#define YYJSON_SUBTYPE_REAL ((uint8_t)(2 << 3)) /* ___10___ */ +/** String that do not need to be escaped for writing (internal use). */ +#define YYJSON_SUBTYPE_NOESC ((uint8_t)(1 << 3)) /* ___01___ */ + +/** The mask used to extract the type of a JSON value. */ +#define YYJSON_TYPE_MASK ((uint8_t)0x07) /* _____111 */ +/** The number of bits used by the type. */ +#define YYJSON_TYPE_BIT ((uint8_t)3) +/** The mask used to extract the subtype of a JSON value. */ +#define YYJSON_SUBTYPE_MASK ((uint8_t)0x18) /* ___11___ */ +/** The number of bits used by the subtype. */ +#define YYJSON_SUBTYPE_BIT ((uint8_t)2) +/** The mask used to extract the reserved bits of a JSON value. */ +#define YYJSON_RESERVED_MASK ((uint8_t)0xE0) /* 111_____ */ +/** The number of reserved bits. */ +#define YYJSON_RESERVED_BIT ((uint8_t)3) +/** The mask used to extract the tag of a JSON value. */ +#define YYJSON_TAG_MASK ((uint8_t)0xFF) /* 11111111 */ +/** The number of bits used by the tag. */ +#define YYJSON_TAG_BIT ((uint8_t)8) + +/** Padding size for JSON reader. */ +#define YYJSON_PADDING_SIZE 4 + + + +/*============================================================================== + * MARK: - Allocator + *============================================================================*/ + +/** + A memory allocator. + + Typically you don't need to use it, unless you want to customize your own + memory allocator. + */ +typedef struct yyjson_alc { + /** Same as libc's malloc(size), should not be NULL. */ + void *(*malloc)(void *ctx, size_t size); + /** Same as libc's realloc(ptr, size), should not be NULL. */ + void *(*realloc)(void *ctx, void *ptr, size_t old_size, size_t size); + /** Same as libc's free(ptr), should not be NULL. */ + void (*free)(void *ctx, void *ptr); + /** A context for malloc/realloc/free, can be NULL. */ + void *ctx; +} yyjson_alc; + +/** + A pool allocator uses fixed length pre-allocated memory. + + This allocator may be used to avoid malloc/realloc calls. The pre-allocated + memory should be held by the caller. The maximum amount of memory required to + read a JSON can be calculated using the `yyjson_read_max_memory_usage()` + function, but the amount of memory required to write a JSON cannot be directly + calculated. + + This is not a general-purpose allocator. It is designed to handle a single JSON + data at a time. If it is used for overly complex memory tasks, such as parsing + multiple JSON documents using the same allocator but releasing only a few of + them, it may cause memory fragmentation, resulting in performance degradation + and memory waste. + + @param alc The allocator to be initialized. + If this parameter is NULL, the function will fail and return false. + If `buf` or `size` is invalid, this will be set to an empty allocator. + @param buf The buffer memory for this allocator. + If this parameter is NULL, the function will fail and return false. + @param size The size of `buf`, in bytes. + If this parameter is less than 8 words (32/64 bytes on 32/64-bit OS), the + function will fail and return false. + @return true if the `alc` has been successfully initialized. + + @b Example + @code + // parse JSON with stack memory + char buf[1024]; + yyjson_alc alc; + yyjson_alc_pool_init(&alc, buf, 1024); + + const char *json = "{\"name\":\"Helvetica\",\"size\":16}" + yyjson_doc *doc = yyjson_read_opts(json, strlen(json), 0, &alc, NULL); + // the memory of `doc` is on the stack + @endcode + + @warning This Allocator is not thread-safe. + */ +yyjson_api bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, size_t size); + +/** + A dynamic allocator. + + This allocator has a similar usage to the pool allocator above. However, when + there is not enough memory, this allocator will dynamically request more memory + using libc's `malloc` function, and frees it all at once when it is destroyed. + + @return A new dynamic allocator, or NULL if memory allocation failed. + @note The returned value should be freed with `yyjson_alc_dyn_free()`. + + @warning This Allocator is not thread-safe. + */ +yyjson_api yyjson_alc *yyjson_alc_dyn_new(void); + +/** + Free a dynamic allocator which is created by `yyjson_alc_dyn_new()`. + @param alc The dynamic allocator to be destroyed. + */ +yyjson_api void yyjson_alc_dyn_free(yyjson_alc *alc); + + + +/*============================================================================== + * MARK: - Text Locating + *============================================================================*/ + +/** + Locate the line and column number for a byte position in a string. + This can be used to get better description for error position. + + @param str The input string. + @param len The byte length of the input string. + @param pos The byte position within the input string. + @param line A pointer to receive the line number, starting from 1. + @param col A pointer to receive the column number, starting from 1. + @param chr A pointer to receive the character index, starting from 0. + @return true on success, false if `str` is NULL or `pos` is out of bounds. + @note Line/column/character are calculated based on Unicode characters for + compatibility with text editors. For multi-byte UTF-8 characters, + the returned value may not directly correspond to the byte position. + */ +yyjson_api bool yyjson_locate_pos(const char *str, size_t len, size_t pos, + size_t *line, size_t *col, size_t *chr); + + + +/*============================================================================== + * MARK: - JSON Structure + *============================================================================*/ + +/** + An immutable document for reading JSON. + This document holds memory for all its JSON values and strings. When it is no + longer used, the user should call `yyjson_doc_free()` to free its memory. + */ +typedef struct yyjson_doc yyjson_doc; + +/** + An immutable value for reading JSON. + A JSON Value has the same lifetime as its document. The memory is held by its + document and and cannot be freed alone. + */ +typedef struct yyjson_val yyjson_val; + +/** + A mutable document for building JSON. + This document holds memory for all its JSON values and strings. When it is no + longer used, the user should call `yyjson_mut_doc_free()` to free its memory. + */ +typedef struct yyjson_mut_doc yyjson_mut_doc; + +/** + A mutable value for building JSON. + A JSON Value has the same lifetime as its document. The memory is held by its + document and and cannot be freed alone. + */ +typedef struct yyjson_mut_val yyjson_mut_val; + + + +/*============================================================================== + * MARK: - JSON Reader API + *============================================================================*/ + +/** Run-time options for JSON reader. */ +typedef uint32_t yyjson_read_flag; + +/** Default option (RFC 8259 compliant): + - Read positive integer as uint64_t. + - Read negative integer as int64_t. + - Read floating-point number as double with round-to-nearest mode. + - Read integer which cannot fit in uint64_t or int64_t as double. + - Report error if double number is infinity. + - Report error if string contains invalid UTF-8 character or BOM. + - Report error on trailing commas, comments, inf and nan literals. */ +static const yyjson_read_flag YYJSON_READ_NOFLAG = 0; + +/** Read the input data in-situ. + This option allows the reader to modify and use input data to store string + values, which can increase reading speed slightly. + The caller should hold the input data before free the document. + The input data must be padded by at least `YYJSON_PADDING_SIZE` bytes. + For example: `[1,2]` should be `[1,2]\0\0\0\0`, input length should be 5. */ +static const yyjson_read_flag YYJSON_READ_INSITU = 1 << 0; + +/** Stop when done instead of issuing an error if there's additional content + after a JSON document. This option may be used to parse small pieces of JSON + in larger data, such as `NDJSON`. */ +static const yyjson_read_flag YYJSON_READ_STOP_WHEN_DONE = 1 << 1; + +/** Allow single trailing comma at the end of an object or array, + such as `[1,2,3,]`, `{"a":1,"b":2,}` (non-standard). */ +static const yyjson_read_flag YYJSON_READ_ALLOW_TRAILING_COMMAS = 1 << 2; + +/** Allow C-style single-line and mult-line comments (non-standard). */ +static const yyjson_read_flag YYJSON_READ_ALLOW_COMMENTS = 1 << 3; + +/** Allow inf/nan number and literal, case-insensitive, + such as 1e999, NaN, inf, -Infinity (non-standard). */ +static const yyjson_read_flag YYJSON_READ_ALLOW_INF_AND_NAN = 1 << 4; + +/** Read all numbers as raw strings (value with `YYJSON_TYPE_RAW` type), + inf/nan literal is also read as raw with `ALLOW_INF_AND_NAN` flag. */ +static const yyjson_read_flag YYJSON_READ_NUMBER_AS_RAW = 1 << 5; + +/** Allow reading invalid unicode when parsing string values (non-standard). + Invalid characters will be allowed to appear in the string values, but + invalid escape sequences will still be reported as errors. + This flag does not affect the performance of correctly encoded strings. + + @warning Strings in JSON values may contain incorrect encoding when this + option is used, you need to handle these strings carefully to avoid security + risks. */ +static const yyjson_read_flag YYJSON_READ_ALLOW_INVALID_UNICODE = 1 << 6; + +/** Read big numbers as raw strings. These big numbers include integers that + cannot be represented by `int64_t` and `uint64_t`, and floating-point + numbers that cannot be represented by finite `double`. + The flag will be overridden by `YYJSON_READ_NUMBER_AS_RAW` flag. */ +static const yyjson_read_flag YYJSON_READ_BIGNUM_AS_RAW = 1 << 7; + +/** Allow UTF-8 BOM and skip it before parsing if any (non-standard). */ +static const yyjson_read_flag YYJSON_READ_ALLOW_BOM = 1 << 8; + +/** Allow extended number formats (non-standard): + - Hexadecimal numbers, such as `0x7B`. + - Numbers with leading or trailing decimal point, such as `.123`, `123.`. + - Numbers with a leading plus sign, such as `+123`. */ +static const yyjson_read_flag YYJSON_READ_ALLOW_EXT_NUMBER = 1 << 9; + +/** Allow extended escape sequences in strings (non-standard): + - Additional escapes: `\a`, `\e`, `\v`, ``\'``, `\?`, `\0`. + - Hex escapes: `\xNN`, such as `\x7B`. + - Line continuation: backslash followed by line terminator sequences. + - Unknown escape: if backslash is followed by an unsupported character, + the backslash will be removed and the character will be kept as-is. + However, `\1`-`\9` will still trigger an error. */ +static const yyjson_read_flag YYJSON_READ_ALLOW_EXT_ESCAPE = 1 << 10; + +/** Allow extended whitespace characters (non-standard): + - Vertical tab `\v` and form feed `\f`. + - Line separator `\u2028` and paragraph separator `\u2029`. + - Non-breaking space `\xA0`. + - Byte order mark: `\uFEFF`. + - Other Unicode characters in the Zs (Separator, space) category. */ +static const yyjson_read_flag YYJSON_READ_ALLOW_EXT_WHITESPACE = 1 << 11; + +/** Allow strings enclosed in single quotes (non-standard), such as ``'ab'``. */ +static const yyjson_read_flag YYJSON_READ_ALLOW_SINGLE_QUOTED_STR = 1 << 12; + +/** Allow object keys without quotes (non-standard), such as `{a:1,b:2}`. + This extends the ECMAScript IdentifierName rule by allowing any + non-whitespace character with code point above `U+007F`. */ +static const yyjson_read_flag YYJSON_READ_ALLOW_UNQUOTED_KEY = 1 << 13; + +/** Allow JSON5 format, see: [https://json5.org]. + This flag supports all JSON5 features with some additional extensions: + - Accepts more escape sequences than JSON5 (e.g. `\a`, `\e`). + - Unquoted keys are not limited to ECMAScript IdentifierName. + - Allow case-insensitive `NaN`, `Inf` and `Infinity` literals. */ +static const yyjson_read_flag YYJSON_READ_JSON5 = + (1 << 2) | /* YYJSON_READ_ALLOW_TRAILING_COMMAS */ + (1 << 3) | /* YYJSON_READ_ALLOW_COMMENTS */ + (1 << 4) | /* YYJSON_READ_ALLOW_INF_AND_NAN */ + (1 << 9) | /* YYJSON_READ_ALLOW_EXT_NUMBER */ + (1 << 10) | /* YYJSON_READ_ALLOW_EXT_ESCAPE */ + (1 << 11) | /* YYJSON_READ_ALLOW_EXT_WHITESPACE */ + (1 << 12) | /* YYJSON_READ_ALLOW_SINGLE_QUOTED_STR */ + (1 << 13); /* YYJSON_READ_ALLOW_UNQUOTED_KEY */ + + + +/** Result code for JSON reader. */ +typedef uint32_t yyjson_read_code; + +/** Success, no error. */ +static const yyjson_read_code YYJSON_READ_SUCCESS = 0; + +/** Invalid parameter, such as NULL input string or 0 input length. */ +static const yyjson_read_code YYJSON_READ_ERROR_INVALID_PARAMETER = 1; + +/** Memory allocation failed. */ +static const yyjson_read_code YYJSON_READ_ERROR_MEMORY_ALLOCATION = 2; + +/** Input JSON string is empty. */ +static const yyjson_read_code YYJSON_READ_ERROR_EMPTY_CONTENT = 3; + +/** Unexpected content after document, such as `[123]abc`. */ +static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CONTENT = 4; + +/** Unexpected end of input, the parsed part is valid, such as `[123`. */ +static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_END = 5; + +/** Unexpected character inside the document, such as `[abc]`. */ +static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CHARACTER = 6; + +/** Invalid JSON structure, such as `[1,]`. */ +static const yyjson_read_code YYJSON_READ_ERROR_JSON_STRUCTURE = 7; + +/** Invalid comment, deprecated, use `UNEXPECTED_END` for unclosed comment. */ +static const yyjson_read_code YYJSON_READ_ERROR_INVALID_COMMENT = 8; + +/** Invalid number, such as `123.e12`, `000`. */ +static const yyjson_read_code YYJSON_READ_ERROR_INVALID_NUMBER = 9; + +/** Invalid string, such as invalid escaped character inside a string. */ +static const yyjson_read_code YYJSON_READ_ERROR_INVALID_STRING = 10; + +/** Invalid JSON literal, such as `truu`. */ +static const yyjson_read_code YYJSON_READ_ERROR_LITERAL = 11; + +/** Failed to open a file. */ +static const yyjson_read_code YYJSON_READ_ERROR_FILE_OPEN = 12; + +/** Failed to read a file. */ +static const yyjson_read_code YYJSON_READ_ERROR_FILE_READ = 13; + +/** Incomplete input during incremental parsing; parsing state is preserved. */ +static const yyjson_read_code YYJSON_READ_ERROR_MORE = 14; + +/** Error information for JSON reader. */ +typedef struct yyjson_read_err { + /** Error code, see `yyjson_read_code` for all possible values. */ + yyjson_read_code code; + /** Error message, constant, no need to free (NULL if success). */ + const char *msg; + /** Error byte position for input data (0 if success). */ + size_t pos; +} yyjson_read_err; + + + +#if !defined(YYJSON_DISABLE_READER) || !YYJSON_DISABLE_READER + +/** + Read JSON with options. + + This function is thread-safe when: + 1. The `dat` is not modified by other threads. + 2. The `alc` is thread-safe or NULL. + + @param dat The JSON data (UTF-8 without BOM), null-terminator is not required. + If this parameter is NULL, the function will fail and return NULL. + The `dat` will not be modified without the flag `YYJSON_READ_INSITU`, so you + can pass a `const char *` string and case it to `char *` if you don't use + the `YYJSON_READ_INSITU` flag. + @param len The length of JSON data in bytes. + If this parameter is 0, the function will fail and return NULL. + @param flg The JSON read options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON reader. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return A new JSON document, or NULL if an error occurs. + When it's no longer needed, it should be freed with `yyjson_doc_free()`. + */ +yyjson_api yyjson_doc *yyjson_read_opts(char *dat, + size_t len, + yyjson_read_flag flg, + const yyjson_alc *alc, + yyjson_read_err *err); + +/** + Read a JSON file. + + This function is thread-safe when: + 1. The file is not modified by other threads. + 2. The `alc` is thread-safe or NULL. + + @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. + If this path is NULL or invalid, the function will fail and return NULL. + @param flg The JSON read options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON reader. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return A new JSON document, or NULL if an error occurs. + When it's no longer needed, it should be freed with `yyjson_doc_free()`. + + @warning On 32-bit operating system, files larger than 2GB may fail to read. + */ +yyjson_api yyjson_doc *yyjson_read_file(const char *path, + yyjson_read_flag flg, + const yyjson_alc *alc, + yyjson_read_err *err); + +/** + Read JSON from a file pointer. + + @param fp The file pointer. + The data will be read from the current position of the FILE to the end. + If this fp is NULL or invalid, the function will fail and return NULL. + @param flg The JSON read options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON reader. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return A new JSON document, or NULL if an error occurs. + When it's no longer needed, it should be freed with `yyjson_doc_free()`. + + @warning On 32-bit operating system, files larger than 2GB may fail to read. + */ +yyjson_api yyjson_doc *yyjson_read_fp(FILE *fp, + yyjson_read_flag flg, + const yyjson_alc *alc, + yyjson_read_err *err); + +/** + Read a JSON string. + + This function is thread-safe. + + @param dat The JSON data (UTF-8 without BOM), null-terminator is not required. + If this parameter is NULL, the function will fail and return NULL. + @param len The length of JSON data in bytes. + If this parameter is 0, the function will fail and return NULL. + @param flg The JSON read options. + Multiple options can be combined with `|` operator. 0 means no options. + @return A new JSON document, or NULL if an error occurs. + When it's no longer needed, it should be freed with `yyjson_doc_free()`. + */ +yyjson_api_inline yyjson_doc *yyjson_read(const char *dat, + size_t len, + yyjson_read_flag flg) { + flg &= ~YYJSON_READ_INSITU; /* const string cannot be modified */ + return yyjson_read_opts((char *)(void *)(size_t)(const void *)dat, + len, flg, NULL, NULL); +} + + + +#if !defined(YYJSON_DISABLE_INCR_READER) || !YYJSON_DISABLE_INCR_READER + +/** Opaque state for incremental JSON reader. */ +typedef struct yyjson_incr_state yyjson_incr_state; + +/** + Initialize state for incremental read. + + To read a large JSON document incrementally: + 1. Call `yyjson_incr_new()` to create the state for incremental reading. + 2. Call `yyjson_incr_read()` repeatedly. + 3. Call `yyjson_incr_free()` to free the state. + + Note: The incremental JSON reader only supports standard JSON. + Flags for non-standard features (e.g. comments, trailing commas) are ignored. + + @param buf The JSON data, null-terminator is not required. + If this parameter is NULL, the function will fail and return NULL. + @param buf_len The length of the JSON data in `buf`. + If use `YYJSON_READ_INSITU`, `buf_len` should not include the padding size. + @param flg The JSON read options. + Multiple options can be combined with `|` operator. + @param alc The memory allocator used by JSON reader. + Pass NULL to use the libc's default allocator. + @return A state for incremental reading. + It should be freed with `yyjson_incr_free()`. + NULL is returned if memory allocation fails. +*/ +yyjson_api yyjson_incr_state *yyjson_incr_new(char *buf, size_t buf_len, + yyjson_read_flag flg, + const yyjson_alc *alc); + +/** + Performs incremental read of up to `len` bytes. + + If NULL is returned and `err->code` is set to `YYJSON_READ_ERROR_MORE`, it + indicates that more data is required to continue parsing. Then, call this + function again with incremented `len`. Continue until a document is returned or + an error other than `YYJSON_READ_ERROR_MORE` is returned. + + Note: Parsing in very small increments is not efficient. An increment of + several kilobytes or megabytes is recommended. + + @param state The state for incremental reading, created using + `yyjson_incr_new()`. + @param len The number of bytes of JSON data available to parse. + If this parameter is 0, the function will fail and return NULL. + @param err A pointer to receive error information. + @return A new JSON document, or NULL if an error occurs. + When the document is no longer needed, it should be freed with + `yyjson_doc_free()`. +*/ +yyjson_api yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, + yyjson_read_err *err); + +/** Release the incremental read state and free the memory. */ +yyjson_api void yyjson_incr_free(yyjson_incr_state *state); + +#endif /* YYJSON_DISABLE_INCR_READER */ + +/** + Returns the size of maximum memory usage to read a JSON data. + + You may use this value to avoid malloc() or calloc() call inside the reader + to get better performance, or read multiple JSON with one piece of memory. + + @param len The length of JSON data in bytes. + @param flg The JSON read options. + @return The maximum memory size to read this JSON, or 0 if overflow. + + @b Example + @code + // read multiple JSON with same pre-allocated memory + + char *dat1, *dat2, *dat3; // JSON data + size_t len1, len2, len3; // JSON length + size_t max_len = MAX(len1, MAX(len2, len3)); + yyjson_doc *doc; + + // use one allocator for multiple JSON + size_t size = yyjson_read_max_memory_usage(max_len, 0); + void *buf = malloc(size); + yyjson_alc alc; + yyjson_alc_pool_init(&alc, buf, size); + + // no more alloc() or realloc() call during reading + doc = yyjson_read_opts(dat1, len1, 0, &alc, NULL); + yyjson_doc_free(doc); + doc = yyjson_read_opts(dat2, len2, 0, &alc, NULL); + yyjson_doc_free(doc); + doc = yyjson_read_opts(dat3, len3, 0, &alc, NULL); + yyjson_doc_free(doc); + + free(buf); + @endcode + @see yyjson_alc_pool_init() + */ +yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len, + yyjson_read_flag flg) { + /* + 1. The max value count is (json_size / 2 + 1), + for example: "[1,2,3,4]" size is 9, value count is 5. + 2. Some broken JSON may cost more memory during reading, but fail at end, + for example: "[[[[[[[[". + 3. yyjson use 16 bytes per value, see struct yyjson_val. + 4. yyjson use dynamic memory with a growth factor of 1.5. + + The max memory size is (json_size / 2 * 16 * 1.5 + padding). + */ + size_t mul = (size_t)12 + !(flg & YYJSON_READ_INSITU); + size_t pad = 256; + size_t max = (size_t)(~(size_t)0); + if (flg & YYJSON_READ_STOP_WHEN_DONE) len = len < 256 ? 256 : len; + if (len >= (max - pad - mul) / mul) return 0; + return len * mul + pad; +} + +/** + Read a JSON number. + + This function is thread-safe when data is not modified by other threads. + + @param dat The JSON data (UTF-8 without BOM), null-terminator is required. + If this parameter is NULL, the function will fail and return NULL. + @param val The output value where result is stored. + If this parameter is NULL, the function will fail and return NULL. + The value will hold either UINT or SINT or REAL number; + @param flg The JSON read options. + Multiple options can be combined with `|` operator. 0 means no options. + Supports `YYJSON_READ_NUMBER_AS_RAW` and `YYJSON_READ_ALLOW_INF_AND_NAN`. + @param alc The memory allocator used for long number. + It is only used when the built-in floating point reader is disabled. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return If successful, a pointer to the character after the last character + used in the conversion, NULL if an error occurs. + */ +yyjson_api const char *yyjson_read_number(const char *dat, + yyjson_val *val, + yyjson_read_flag flg, + const yyjson_alc *alc, + yyjson_read_err *err); + +/** Same as `yyjson_read_number()`. */ +yyjson_api_inline const char *yyjson_mut_read_number(const char *dat, + yyjson_mut_val *val, + yyjson_read_flag flg, + const yyjson_alc *alc, + yyjson_read_err *err) { + return yyjson_read_number(dat, (yyjson_val *)val, flg, alc, err); +} + +#endif /* YYJSON_DISABLE_READER) */ + + + +/*============================================================================== + * MARK: - JSON Writer API + *============================================================================*/ + +/** Run-time options for JSON writer. */ +typedef uint32_t yyjson_write_flag; + +/** Default option: + - Write JSON minify. + - Report error on inf or nan number. + - Report error on invalid UTF-8 string. + - Do not escape unicode or slash. */ +static const yyjson_write_flag YYJSON_WRITE_NOFLAG = 0; + +/** Write JSON pretty with 4 space indent. */ +static const yyjson_write_flag YYJSON_WRITE_PRETTY = 1 << 0; + +/** Escape unicode as `uXXXX`, make the output ASCII only. */ +static const yyjson_write_flag YYJSON_WRITE_ESCAPE_UNICODE = 1 << 1; + +/** Escape '/' as '\/'. */ +static const yyjson_write_flag YYJSON_WRITE_ESCAPE_SLASHES = 1 << 2; + +/** Write inf and nan number as 'Infinity' and 'NaN' literal (non-standard). */ +static const yyjson_write_flag YYJSON_WRITE_ALLOW_INF_AND_NAN = 1 << 3; + +/** Write inf and nan number as null literal. + This flag will override `YYJSON_WRITE_ALLOW_INF_AND_NAN` flag. */ +static const yyjson_write_flag YYJSON_WRITE_INF_AND_NAN_AS_NULL = 1 << 4; + +/** Allow invalid unicode when encoding string values (non-standard). + Invalid characters in string value will be copied byte by byte. + If `YYJSON_WRITE_ESCAPE_UNICODE` flag is also set, invalid character will be + escaped as `U+FFFD` (replacement character). + This flag does not affect the performance of correctly encoded strings. */ +static const yyjson_write_flag YYJSON_WRITE_ALLOW_INVALID_UNICODE = 1 << 5; + +/** Write JSON pretty with 2 space indent. + This flag will override `YYJSON_WRITE_PRETTY` flag. */ +static const yyjson_write_flag YYJSON_WRITE_PRETTY_TWO_SPACES = 1 << 6; + +/** Adds a newline character `\n` at the end of the JSON. + This can be helpful for text editors or NDJSON. */ +static const yyjson_write_flag YYJSON_WRITE_NEWLINE_AT_END = 1 << 7; + + + +/** The highest 8 bits of `yyjson_write_flag` and real number value's `tag` + are reserved for controlling the output format of floating-point numbers. */ +#define YYJSON_WRITE_FP_FLAG_BITS 8 + +/** The highest 4 bits of flag are reserved for precision value. */ +#define YYJSON_WRITE_FP_PREC_BITS 4 + +/** Write floating-point number using fixed-point notation. + - This is similar to ECMAScript `Number.prototype.toFixed(prec)`, + but with trailing zeros removed. The `prec` ranges from 1 to 15. + - This will produce shorter output but may lose some precision. */ +#define YYJSON_WRITE_FP_TO_FIXED(prec) ((yyjson_write_flag)( \ + (uint32_t)((uint32_t)(prec)) << (32 - 4) )) + +/** Write floating-point numbers using single-precision (float). + - This casts `double` to `float` before serialization. + - This will produce shorter output, but may lose some precision. + - This flag is ignored if `YYJSON_WRITE_FP_TO_FIXED(prec)` is also used. */ +#define YYJSON_WRITE_FP_TO_FLOAT ((yyjson_write_flag)(1 << (32 - 5))) + + + +/** Result code for JSON writer */ +typedef uint32_t yyjson_write_code; + +/** Success, no error. */ +static const yyjson_write_code YYJSON_WRITE_SUCCESS = 0; + +/** Invalid parameter, such as NULL document. */ +static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_PARAMETER = 1; + +/** Memory allocation failure occurs. */ +static const yyjson_write_code YYJSON_WRITE_ERROR_MEMORY_ALLOCATION = 2; + +/** Invalid value type in JSON document. */ +static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_VALUE_TYPE = 3; + +/** NaN or Infinity number occurs. */ +static const yyjson_write_code YYJSON_WRITE_ERROR_NAN_OR_INF = 4; + +/** Failed to open a file. */ +static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_OPEN = 5; + +/** Failed to write a file. */ +static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_WRITE = 6; + +/** Invalid unicode in string. */ +static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_STRING = 7; + +/** Error information for JSON writer. */ +typedef struct yyjson_write_err { + /** Error code, see `yyjson_write_code` for all possible values. */ + yyjson_write_code code; + /** Error message, constant, no need to free (NULL if success). */ + const char *msg; +} yyjson_write_err; + + + +#if !defined(YYJSON_DISABLE_WRITER) || !YYJSON_DISABLE_WRITER + +/*============================================================================== + * MARK: - JSON Document Writer API + *============================================================================*/ + +/** + Write a document to JSON string with options. + + This function is thread-safe when: + The `alc` is thread-safe or NULL. + + @param doc The JSON document. + If this doc is NULL or has no root, the function will fail and return false. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param len A pointer to receive output length in bytes (not including the + null-terminator). Pass NULL if you don't need length information. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return A new JSON string, or NULL if an error occurs. + This string is encoded as UTF-8 with a null-terminator. + When it's no longer needed, it should be freed with free() or alc->free(). + */ +yyjson_api char *yyjson_write_opts(const yyjson_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc, + size_t *len, + yyjson_write_err *err); + +/** + Write a document to JSON file with options. + + This function is thread-safe when: + 1. The file is not accessed by other threads. + 2. The `alc` is thread-safe or NULL. + + @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. + If this path is NULL or invalid, the function will fail and return false. + If this file is not empty, the content will be discarded. + @param doc The JSON document. + If this doc is NULL or has no root, the function will fail and return false. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return true if successful, false if an error occurs. + + @warning On 32-bit operating system, files larger than 2GB may fail to write. + */ +yyjson_api bool yyjson_write_file(const char *path, + const yyjson_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc, + yyjson_write_err *err); + +/** + Write a document to file pointer with options. + + @param fp The file pointer. + The data will be written to the current position of the file. + If this fp is NULL or invalid, the function will fail and return false. + @param doc The JSON document. + If this doc is NULL or has no root, the function will fail and return false. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return true if successful, false if an error occurs. + + @warning On 32-bit operating system, files larger than 2GB may fail to write. + */ +yyjson_api bool yyjson_write_fp(FILE *fp, + const yyjson_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc, + yyjson_write_err *err); + +/** + Write a document to JSON string. + + This function is thread-safe. + + @param doc The JSON document. + If this doc is NULL or has no root, the function will fail and return false. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param len A pointer to receive output length in bytes (not including the + null-terminator). Pass NULL if you don't need length information. + @return A new JSON string, or NULL if an error occurs. + This string is encoded as UTF-8 with a null-terminator. + When it's no longer needed, it should be freed with free(). + */ +yyjson_api_inline char *yyjson_write(const yyjson_doc *doc, + yyjson_write_flag flg, + size_t *len) { + return yyjson_write_opts(doc, flg, NULL, len, NULL); +} + + + +/** + Write a document to JSON string with options. + + This function is thread-safe when: + 1. The `doc` is not modified by other threads. + 2. The `alc` is thread-safe or NULL. + + @param doc The mutable JSON document. + If this doc is NULL or has no root, the function will fail and return false. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param len A pointer to receive output length in bytes (not including the + null-terminator). Pass NULL if you don't need length information. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return A new JSON string, or NULL if an error occurs. + This string is encoded as UTF-8 with a null-terminator. + When it's no longer needed, it should be freed with free() or alc->free(). + */ +yyjson_api char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc, + size_t *len, + yyjson_write_err *err); + +/** + Write a document to JSON file with options. + + This function is thread-safe when: + 1. The file is not accessed by other threads. + 2. The `doc` is not modified by other threads. + 3. The `alc` is thread-safe or NULL. + + @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. + If this path is NULL or invalid, the function will fail and return false. + If this file is not empty, the content will be discarded. + @param doc The mutable JSON document. + If this doc is NULL or has no root, the function will fail and return false. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return true if successful, false if an error occurs. + + @warning On 32-bit operating system, files larger than 2GB may fail to write. + */ +yyjson_api bool yyjson_mut_write_file(const char *path, + const yyjson_mut_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc, + yyjson_write_err *err); + +/** + Write a document to file pointer with options. + + @param fp The file pointer. + The data will be written to the current position of the file. + If this fp is NULL or invalid, the function will fail and return false. + @param doc The mutable JSON document. + If this doc is NULL or has no root, the function will fail and return false. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return true if successful, false if an error occurs. + + @warning On 32-bit operating system, files larger than 2GB may fail to write. + */ +yyjson_api bool yyjson_mut_write_fp(FILE *fp, + const yyjson_mut_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc, + yyjson_write_err *err); + +/** + Write a document to JSON string. + + This function is thread-safe when: + The `doc` is not modified by other threads. + + @param doc The JSON document. + If this doc is NULL or has no root, the function will fail and return false. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param len A pointer to receive output length in bytes (not including the + null-terminator). Pass NULL if you don't need length information. + @return A new JSON string, or NULL if an error occurs. + This string is encoded as UTF-8 with a null-terminator. + When it's no longer needed, it should be freed with free(). + */ +yyjson_api_inline char *yyjson_mut_write(const yyjson_mut_doc *doc, + yyjson_write_flag flg, + size_t *len) { + return yyjson_mut_write_opts(doc, flg, NULL, len, NULL); +} + + + +/*============================================================================== + * MARK: - JSON Value Writer API + *============================================================================*/ + +/** + Write a value to JSON string with options. + + This function is thread-safe when: + The `alc` is thread-safe or NULL. + + @param val The JSON root value. + If this parameter is NULL, the function will fail and return NULL. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param len A pointer to receive output length in bytes (not including the + null-terminator). Pass NULL if you don't need length information. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return A new JSON string, or NULL if an error occurs. + This string is encoded as UTF-8 with a null-terminator. + When it's no longer needed, it should be freed with free() or alc->free(). + */ +yyjson_api char *yyjson_val_write_opts(const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc, + size_t *len, + yyjson_write_err *err); + +/** + Write a value to JSON file with options. + + This function is thread-safe when: + 1. The file is not accessed by other threads. + 2. The `alc` is thread-safe or NULL. + + @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. + If this path is NULL or invalid, the function will fail and return false. + If this file is not empty, the content will be discarded. + @param val The JSON root value. + If this parameter is NULL, the function will fail and return NULL. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return true if successful, false if an error occurs. + + @warning On 32-bit operating system, files larger than 2GB may fail to write. + */ +yyjson_api bool yyjson_val_write_file(const char *path, + const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc, + yyjson_write_err *err); + +/** + Write a value to file pointer with options. + + @param fp The file pointer. + The data will be written to the current position of the file. + If this path is NULL or invalid, the function will fail and return false. + @param val The JSON root value. + If this parameter is NULL, the function will fail and return NULL. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return true if successful, false if an error occurs. + + @warning On 32-bit operating system, files larger than 2GB may fail to write. + */ +yyjson_api bool yyjson_val_write_fp(FILE *fp, + const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc, + yyjson_write_err *err); + +/** + Write a value to JSON string. + + This function is thread-safe. + + @param val The JSON root value. + If this parameter is NULL, the function will fail and return NULL. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param len A pointer to receive output length in bytes (not including the + null-terminator). Pass NULL if you don't need length information. + @return A new JSON string, or NULL if an error occurs. + This string is encoded as UTF-8 with a null-terminator. + When it's no longer needed, it should be freed with free(). + */ +yyjson_api_inline char *yyjson_val_write(const yyjson_val *val, + yyjson_write_flag flg, + size_t *len) { + return yyjson_val_write_opts(val, flg, NULL, len, NULL); +} + +/** + Write a value to JSON string with options. + + This function is thread-safe when: + 1. The `val` is not modified by other threads. + 2. The `alc` is thread-safe or NULL. + + @param val The mutable JSON root value. + If this parameter is NULL, the function will fail and return NULL. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param len A pointer to receive output length in bytes (not including the + null-terminator). Pass NULL if you don't need length information. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return A new JSON string, or NULL if an error occurs. + This string is encoded as UTF-8 with a null-terminator. + When it's no longer needed, it should be freed with free() or alc->free(). + */ +yyjson_api char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc, + size_t *len, + yyjson_write_err *err); + +/** + Write a value to JSON file with options. + + This function is thread-safe when: + 1. The file is not accessed by other threads. + 2. The `val` is not modified by other threads. + 3. The `alc` is thread-safe or NULL. + + @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. + If this path is NULL or invalid, the function will fail and return false. + If this file is not empty, the content will be discarded. + @param val The mutable JSON root value. + If this parameter is NULL, the function will fail and return NULL. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return true if successful, false if an error occurs. + + @warning On 32-bit operating system, files larger than 2GB may fail to write. + */ +yyjson_api bool yyjson_mut_val_write_file(const char *path, + const yyjson_mut_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc, + yyjson_write_err *err); + +/** + Write a value to JSON file with options. + + @param fp The file pointer. + The data will be written to the current position of the file. + If this path is NULL or invalid, the function will fail and return false. + @param val The mutable JSON root value. + If this parameter is NULL, the function will fail and return NULL. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param alc The memory allocator used by JSON writer. + Pass NULL to use the libc's default allocator. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return true if successful, false if an error occurs. + + @warning On 32-bit operating system, files larger than 2GB may fail to write. + */ +yyjson_api bool yyjson_mut_val_write_fp(FILE *fp, + const yyjson_mut_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc, + yyjson_write_err *err); + +/** + Write a value to JSON string. + + This function is thread-safe when: + The `val` is not modified by other threads. + + @param val The JSON root value. + If this parameter is NULL, the function will fail and return NULL. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param len A pointer to receive output length in bytes (not including the + null-terminator). Pass NULL if you don't need length information. + @return A new JSON string, or NULL if an error occurs. + This string is encoded as UTF-8 with a null-terminator. + When it's no longer needed, it should be freed with free(). + */ +yyjson_api_inline char *yyjson_mut_val_write(const yyjson_mut_val *val, + yyjson_write_flag flg, + size_t *len) { + return yyjson_mut_val_write_opts(val, flg, NULL, len, NULL); +} + +/** + Write a JSON number. + + @param val A JSON number value to be converted to a string. + If this parameter is invalid, the function will fail and return NULL. + @param buf A buffer to store the resulting null-terminated string. + If this parameter is NULL, the function will fail and return NULL. + For integer values, the buffer must be at least 21 bytes. + For floating-point values, the buffer must be at least 40 bytes. + @return On success, returns a pointer to the character after the last + written character. On failure, returns NULL. + @note + - This function is thread-safe and does not allocate memory + (when `YYJSON_DISABLE_FAST_FP_CONV` is not defined). + - This function will fail and return NULL only in the following cases: + 1) `val` or `buf` is NULL; + 2) `val` is not a number type; + 3) `val` is `inf` or `nan`, and non-standard JSON is explicitly disabled + via the `YYJSON_DISABLE_NON_STANDARD` flag. + */ +yyjson_api char *yyjson_write_number(const yyjson_val *val, char *buf); + +/** Same as `yyjson_write_number()`. */ +yyjson_api_inline char *yyjson_mut_write_number(const yyjson_mut_val *val, + char *buf) { + return yyjson_write_number((const yyjson_val *)val, buf); +} + +#endif /* YYJSON_DISABLE_WRITER */ + + + +/*============================================================================== + * MARK: - JSON Document API + *============================================================================*/ + +/** Returns the root value of this JSON document. + Returns NULL if `doc` is NULL. */ +yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc); + +/** Returns read size of input JSON data. + Returns 0 if `doc` is NULL. + For example: the read size of `[1,2,3]` is 7 bytes. */ +yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc); + +/** Returns total value count in this JSON document. + Returns 0 if `doc` is NULL. + For example: the value count of `[1,2,3]` is 4. */ +yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc); + +/** Release the JSON document and free the memory. + After calling this function, the `doc` and all values from the `doc` are no + longer available. This function will do nothing if the `doc` is NULL. */ +yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc); + + + +/*============================================================================== + * MARK: - JSON Value Type API + *============================================================================*/ + +/** Returns whether the JSON value is raw. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_raw(yyjson_val *val); + +/** Returns whether the JSON value is `null`. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_null(yyjson_val *val); + +/** Returns whether the JSON value is `true`. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_true(yyjson_val *val); + +/** Returns whether the JSON value is `false`. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_false(yyjson_val *val); + +/** Returns whether the JSON value is bool (true/false). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_bool(yyjson_val *val); + +/** Returns whether the JSON value is unsigned integer (uint64_t). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_uint(yyjson_val *val); + +/** Returns whether the JSON value is signed integer (int64_t). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_sint(yyjson_val *val); + +/** Returns whether the JSON value is integer (uint64_t/int64_t). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_int(yyjson_val *val); + +/** Returns whether the JSON value is real number (double). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_real(yyjson_val *val); + +/** Returns whether the JSON value is number (uint64_t/int64_t/double). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_num(yyjson_val *val); + +/** Returns whether the JSON value is string. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_str(yyjson_val *val); + +/** Returns whether the JSON value is array. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_arr(yyjson_val *val); + +/** Returns whether the JSON value is object. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_obj(yyjson_val *val); + +/** Returns whether the JSON value is container (array/object). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val); + + + +/*============================================================================== + * MARK: - JSON Value Content API + *============================================================================*/ + +/** Returns the JSON value's type. + Returns YYJSON_TYPE_NONE if `val` is NULL. */ +yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val); + +/** Returns the JSON value's subtype. + Returns YYJSON_SUBTYPE_NONE if `val` is NULL. */ +yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val); + +/** Returns the JSON value's tag. + Returns 0 if `val` is NULL. */ +yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val); + +/** Returns the JSON value's type description. + The return value should be one of these strings: "raw", "null", "string", + "array", "object", "true", "false", "uint", "sint", "real", "unknown". */ +yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val); + +/** Returns the content if the value is raw. + Returns NULL if `val` is NULL or type is not raw. */ +yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val); + +/** Returns the content if the value is bool. + Returns false if `val` is NULL or type is not bool. */ +yyjson_api_inline bool yyjson_get_bool(yyjson_val *val); + +/** Returns the content and cast to uint64_t. + Returns 0 if `val` is NULL or type is not integer(sint/uint). */ +yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val); + +/** Returns the content and cast to int64_t. + Returns 0 if `val` is NULL or type is not integer(sint/uint). */ +yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val); + +/** Returns the content and cast to int. + Returns 0 if `val` is NULL or type is not integer(sint/uint). */ +yyjson_api_inline int yyjson_get_int(yyjson_val *val); + +/** Returns the content if the value is real number, or 0.0 on error. + Returns 0.0 if `val` is NULL or type is not real(double). */ +yyjson_api_inline double yyjson_get_real(yyjson_val *val); + +/** Returns the content and typecast to `double` if the value is number. + Returns 0.0 if `val` is NULL or type is not number(uint/sint/real). */ +yyjson_api_inline double yyjson_get_num(yyjson_val *val); + +/** Returns the content if the value is string. + Returns NULL if `val` is NULL or type is not string. */ +yyjson_api_inline const char *yyjson_get_str(yyjson_val *val); + +/** Returns the content length (string length, array size, object size. + Returns 0 if `val` is NULL or type is not string/array/object. */ +yyjson_api_inline size_t yyjson_get_len(yyjson_val *val); + +/** Returns whether the JSON value is equals to a string. + Returns false if input is NULL or type is not string. */ +yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str); + +/** Returns whether the JSON value is equals to a string. + The `str` should be a UTF-8 string, null-terminator is not required. + Returns false if input is NULL or type is not string. */ +yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, + size_t len); + +/** Returns whether two JSON values are equal (deep compare). + Returns false if input is NULL. + @note the result may be inaccurate if object has duplicate keys. + @warning This function is recursive and may cause a stack overflow + if the object level is too deep. */ +yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); + +/** Set the value to raw. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, + const char *raw, size_t len); + +/** Set the value to null. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_null(yyjson_val *val); + +/** Set the value to bool. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num); + +/** Set the value to uint. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num); + +/** Set the value to sint. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num); + +/** Set the value to int. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num); + +/** Set the value to float. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_float(yyjson_val *val, float num); + +/** Set the value to double. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_double(yyjson_val *val, double num); + +/** Set the value to real. + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num); + +/** Set the floating-point number's output format to fixed-point notation. + Returns false if input is NULL or `val` is not real type. + @see YYJSON_WRITE_FP_TO_FIXED flag. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_fp_to_fixed(yyjson_val *val, int prec); + +/** Set the floating-point number's output format to single-precision. + Returns false if input is NULL or `val` is not real type. + @see YYJSON_WRITE_FP_TO_FLOAT flag. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_fp_to_float(yyjson_val *val, bool flt); + +/** Set the value to string (null-terminated). + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str); + +/** Set the value to string (with length). + Returns false if input is NULL or `val` is object or array. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, + const char *str, size_t len); + +/** Marks this string as not needing to be escaped during JSON writing. + This can be used to avoid the overhead of escaping if the string contains + only characters that do not require escaping. + Returns false if input is NULL or `val` is not string. + @see YYJSON_SUBTYPE_NOESC subtype. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_set_str_noesc(yyjson_val *val, bool noesc); + + + +/*============================================================================== + * MARK: - JSON Array API + *============================================================================*/ + +/** Returns the number of elements in this array. + Returns 0 if `arr` is NULL or type is not array. */ +yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr); + +/** Returns the element at the specified position in this array. + Returns NULL if array is NULL/empty or the index is out of bounds. + @warning This function takes a linear search time if array is not flat. + For example: `[1,{},3]` is flat, `[1,[2],3]` is not flat. */ +yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx); + +/** Returns the first element of this array. + Returns NULL if `arr` is NULL/empty or type is not array. */ +yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr); + +/** Returns the last element of this array. + Returns NULL if `arr` is NULL/empty or type is not array. + @warning This function takes a linear search time if array is not flat. + For example: `[1,{},3]` is flat, `[1,[2],3]` is not flat.*/ +yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr); + + + +/*============================================================================== + * MARK: - JSON Array Iterator API + *============================================================================*/ + +/** + A JSON array iterator. + + @b Example + @code + yyjson_val *val; + yyjson_arr_iter iter = yyjson_arr_iter_with(arr); + while ((val = yyjson_arr_iter_next(&iter))) { + your_func(val); + } + @endcode + */ +typedef struct yyjson_arr_iter { + size_t idx; /**< next value's index */ + size_t max; /**< maximum index (arr.size) */ + yyjson_val *cur; /**< next value */ +} yyjson_arr_iter; + +/** + Initialize an iterator for this array. + + @param arr The array to be iterated over. + If this parameter is NULL or not an array, `iter` will be set to empty. + @param iter The iterator to be initialized. + If this parameter is NULL, the function will fail and return false. + @return true if the `iter` has been successfully initialized. + + @note The iterator does not need to be destroyed. + */ +yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, + yyjson_arr_iter *iter); + +/** + Create an iterator with an array , same as `yyjson_arr_iter_init()`. + + @param arr The array to be iterated over. + If this parameter is NULL or not an array, an empty iterator will returned. + @return A new iterator for the array. + + @note The iterator does not need to be destroyed. + */ +yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr); + +/** + Returns whether the iteration has more elements. + If `iter` is NULL, this function will return false. + */ +yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter); + +/** + Returns the next element in the iteration, or NULL on end. + If `iter` is NULL, this function will return NULL. + */ +yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter); + +/** + Macro for iterating over an array. + It works like iterator, but with a more intuitive API. + + @b Example + @code + size_t idx, max; + yyjson_val *val; + yyjson_arr_foreach(arr, idx, max, val) { + your_func(idx, val); + } + @endcode + */ +#define yyjson_arr_foreach(arr, idx, max, val) \ + for ((idx) = 0, \ + (max) = yyjson_arr_size(arr), \ + (val) = yyjson_arr_get_first(arr); \ + (idx) < (max); \ + (idx)++, \ + (val) = unsafe_yyjson_get_next(val)) + + + +/*============================================================================== + * MARK: - JSON Object API + *============================================================================*/ + +/** Returns the number of key-value pairs in this object. + Returns 0 if `obj` is NULL or type is not object. */ +yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj); + +/** Returns the value to which the specified key is mapped. + Returns NULL if this object contains no mapping for the key. + Returns NULL if `obj/key` is NULL, or type is not object. + + The `key` should be a null-terminated UTF-8 string. + + @warning This function takes a linear search time. */ +yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, const char *key); + +/** Returns the value to which the specified key is mapped. + Returns NULL if this object contains no mapping for the key. + Returns NULL if `obj/key` is NULL, or type is not object. + + The `key` should be a UTF-8 string, null-terminator is not required. + The `key_len` should be the length of the key, in bytes. + + @warning This function takes a linear search time. */ +yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, const char *key, + size_t key_len); + + + +/*============================================================================== + * MARK: - JSON Object Iterator API + *============================================================================*/ + +/** + A JSON object iterator. + + @b Example + @code + yyjson_val *key, *val; + yyjson_obj_iter iter = yyjson_obj_iter_with(obj); + while ((key = yyjson_obj_iter_next(&iter))) { + val = yyjson_obj_iter_get_val(key); + your_func(key, val); + } + @endcode + + If the ordering of the keys is known at compile-time, you can use this method + to speed up value lookups: + @code + // {"k1":1, "k2": 3, "k3": 3} + yyjson_val *key, *val; + yyjson_obj_iter iter = yyjson_obj_iter_with(obj); + yyjson_val *v1 = yyjson_obj_iter_get(&iter, "k1"); + yyjson_val *v3 = yyjson_obj_iter_get(&iter, "k3"); + @endcode + @see yyjson_obj_iter_get() and yyjson_obj_iter_getn() + */ +typedef struct yyjson_obj_iter { + size_t idx; /**< next key's index */ + size_t max; /**< maximum key index (obj.size) */ + yyjson_val *cur; /**< next key */ + yyjson_val *obj; /**< the object being iterated */ +} yyjson_obj_iter; + +/** + Initialize an iterator for this object. + + @param obj The object to be iterated over. + If this parameter is NULL or not an object, `iter` will be set to empty. + @param iter The iterator to be initialized. + If this parameter is NULL, the function will fail and return false. + @return true if the `iter` has been successfully initialized. + + @note The iterator does not need to be destroyed. + */ +yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, + yyjson_obj_iter *iter); + +/** + Create an iterator with an object, same as `yyjson_obj_iter_init()`. + + @param obj The object to be iterated over. + If this parameter is NULL or not an object, an empty iterator will returned. + @return A new iterator for the object. + + @note The iterator does not need to be destroyed. + */ +yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(yyjson_val *obj); + +/** + Returns whether the iteration has more elements. + If `iter` is NULL, this function will return false. + */ +yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter); + +/** + Returns the next key in the iteration, or NULL on end. + If `iter` is NULL, this function will return NULL. + */ +yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter); + +/** + Returns the value for key inside the iteration. + If `iter` is NULL, this function will return NULL. + */ +yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key); + +/** + Iterates to a specified key and returns the value. + + This function does the same thing as `yyjson_obj_get()`, but is much faster + if the ordering of the keys is known at compile-time and you are using the same + order to look up the values. If the key exists in this object, then the + iterator will stop at the next key, otherwise the iterator will not change and + NULL is returned. + + @param iter The object iterator, should not be NULL. + @param key The key, should be a UTF-8 string with null-terminator. + @return The value to which the specified key is mapped. + NULL if this object contains no mapping for the key or input is invalid. + + @warning This function takes a linear search time if the key is not nearby. + */ +yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, + const char *key); + +/** + Iterates to a specified key and returns the value. + + This function does the same thing as `yyjson_obj_getn()`, but is much faster + if the ordering of the keys is known at compile-time and you are using the same + order to look up the values. If the key exists in this object, then the + iterator will stop at the next key, otherwise the iterator will not change and + NULL is returned. + + @param iter The object iterator, should not be NULL. + @param key The key, should be a UTF-8 string, null-terminator is not required. + @param key_len The the length of `key`, in bytes. + @return The value to which the specified key is mapped. + NULL if this object contains no mapping for the key or input is invalid. + + @warning This function takes a linear search time if the key is not nearby. + */ +yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, + const char *key, + size_t key_len); + +/** + Macro for iterating over an object. + It works like iterator, but with a more intuitive API. + + @b Example + @code + size_t idx, max; + yyjson_val *key, *val; + yyjson_obj_foreach(obj, idx, max, key, val) { + your_func(key, val); + } + @endcode + */ +#define yyjson_obj_foreach(obj, idx, max, key, val) \ + for ((idx) = 0, \ + (max) = yyjson_obj_size(obj), \ + (key) = (obj) ? unsafe_yyjson_get_first(obj) : NULL, \ + (val) = (key) + 1; \ + (idx) < (max); \ + (idx)++, \ + (key) = unsafe_yyjson_get_next(val), \ + (val) = (key) + 1) + + + +/*============================================================================== + * MARK: - Mutable JSON Document API + *============================================================================*/ + +/** Returns the root value of this JSON document. + Returns NULL if `doc` is NULL. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc); + +/** Sets the root value of this JSON document. + Pass NULL to clear root value of the document. */ +yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, + yyjson_mut_val *root); + +/** + Set the string pool size for a mutable document. + This function does not allocate memory immediately, but uses the size when + the next memory allocation is needed. + + If the caller knows the approximate bytes of strings that the document needs to + store (e.g. copy string with `yyjson_mut_strcpy` function), setting a larger + size can avoid multiple memory allocations and improve performance. + + @param doc The mutable document. + @param len The desired string pool size in bytes (total string length). + @return true if successful, false if size is 0 or overflow. + */ +yyjson_api bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, + size_t len); + +/** + Set the value pool size for a mutable document. + This function does not allocate memory immediately, but uses the size when + the next memory allocation is needed. + + If the caller knows the approximate number of values that the document needs to + store (e.g. create new value with `yyjson_mut_xxx` functions), setting a larger + size can avoid multiple memory allocations and improve performance. + + @param doc The mutable document. + @param count The desired value pool size (number of `yyjson_mut_val`). + @return true if successful, false if size is 0 or overflow. + */ +yyjson_api bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, + size_t count); + +/** Release the JSON document and free the memory. + After calling this function, the `doc` and all values from the `doc` are no + longer available. This function will do nothing if the `doc` is NULL. */ +yyjson_api void yyjson_mut_doc_free(yyjson_mut_doc *doc); + +/** Creates and returns a new mutable JSON document, returns NULL on error. + If allocator is NULL, the default allocator will be used. */ +yyjson_api yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc); + +/** Copies and returns a new mutable document from input, returns NULL on error. + This makes a `deep-copy` on the immutable document. + If allocator is NULL, the default allocator will be used. + @note `imut_doc` -> `mut_doc`. */ +yyjson_api yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, + const yyjson_alc *alc); + +/** Copies and returns a new mutable document from input, returns NULL on error. + This makes a `deep-copy` on the mutable document. + If allocator is NULL, the default allocator will be used. + @note `mut_doc` -> `mut_doc`. */ +yyjson_api yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc, + const yyjson_alc *alc); + +/** Copies and returns a new mutable value from input, returns NULL on error. + This makes a `deep-copy` on the immutable value. + The memory was managed by mutable document. + @note `imut_val` -> `mut_val`. */ +yyjson_api yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *doc, + yyjson_val *val); + +/** Copies and returns a new mutable value from input, returns NULL on error. + This makes a `deep-copy` on the mutable value. + The memory was managed by mutable document. + @note `mut_val` -> `mut_val`. + @warning This function is recursive and may cause a stack overflow + if the object level is too deep. */ +yyjson_api yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, + yyjson_mut_val *val); + +/** Copies and returns a new immutable document from input, + returns NULL on error. This makes a `deep-copy` on the mutable document. + The returned document should be freed with `yyjson_doc_free()`. + @note `mut_doc` -> `imut_doc`. + @warning This function is recursive and may cause a stack overflow + if the object level is too deep. */ +yyjson_api yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *doc, + const yyjson_alc *alc); + +/** Copies and returns a new immutable document from input, + returns NULL on error. This makes a `deep-copy` on the mutable value. + The returned document should be freed with `yyjson_doc_free()`. + @note `mut_val` -> `imut_doc`. + @warning This function is recursive and may cause a stack overflow + if the object level is too deep. */ +yyjson_api yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *val, + const yyjson_alc *alc); + + + +/*============================================================================== + * MARK: - Mutable JSON Value Type API + *============================================================================*/ + +/** Returns whether the JSON value is raw. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val); + +/** Returns whether the JSON value is `null`. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val); + +/** Returns whether the JSON value is `true`. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val); + +/** Returns whether the JSON value is `false`. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val); + +/** Returns whether the JSON value is bool (true/false). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val); + +/** Returns whether the JSON value is unsigned integer (uint64_t). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val); + +/** Returns whether the JSON value is signed integer (int64_t). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val); + +/** Returns whether the JSON value is integer (uint64_t/int64_t). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val); + +/** Returns whether the JSON value is real number (double). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val); + +/** Returns whether the JSON value is number (uint/sint/real). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val); + +/** Returns whether the JSON value is string. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val); + +/** Returns whether the JSON value is array. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val); + +/** Returns whether the JSON value is object. + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val); + +/** Returns whether the JSON value is container (array/object). + Returns false if `val` is NULL. */ +yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val); + + + +/*============================================================================== + * MARK: - Mutable JSON Value Content API + *============================================================================*/ + +/** Returns the JSON value's type. + Returns `YYJSON_TYPE_NONE` if `val` is NULL. */ +yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val); + +/** Returns the JSON value's subtype. + Returns `YYJSON_SUBTYPE_NONE` if `val` is NULL. */ +yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val); + +/** Returns the JSON value's tag. + Returns 0 if `val` is NULL. */ +yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val); + +/** Returns the JSON value's type description. + The return value should be one of these strings: "raw", "null", "string", + "array", "object", "true", "false", "uint", "sint", "real", "unknown". */ +yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val); + +/** Returns the content if the value is raw. + Returns NULL if `val` is NULL or type is not raw. */ +yyjson_api_inline const char *yyjson_mut_get_raw(yyjson_mut_val *val); + +/** Returns the content if the value is bool. + Returns NULL if `val` is NULL or type is not bool. */ +yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val); + +/** Returns the content and cast to uint64_t. + Returns 0 if `val` is NULL or type is not integer(sint/uint). */ +yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val); + +/** Returns the content and cast to int64_t. + Returns 0 if `val` is NULL or type is not integer(sint/uint). */ +yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val); + +/** Returns the content and cast to int. + Returns 0 if `val` is NULL or type is not integer(sint/uint). */ +yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val); + +/** Returns the content if the value is real number. + Returns 0.0 if `val` is NULL or type is not real(double). */ +yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val); + +/** Returns the content and typecast to `double` if the value is number. + Returns 0.0 if `val` is NULL or type is not number(uint/sint/real). */ +yyjson_api_inline double yyjson_mut_get_num(yyjson_mut_val *val); + +/** Returns the content if the value is string. + Returns NULL if `val` is NULL or type is not string. */ +yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val); + +/** Returns the content length (string length, array size, object size. + Returns 0 if `val` is NULL or type is not string/array/object. */ +yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val); + +/** Returns whether the JSON value is equals to a string. + The `str` should be a null-terminated UTF-8 string. + Returns false if input is NULL or type is not string. */ +yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, + const char *str); + +/** Returns whether the JSON value is equals to a string. + The `str` should be a UTF-8 string, null-terminator is not required. + Returns false if input is NULL or type is not string. */ +yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, + const char *str, size_t len); + +/** Returns whether two JSON values are equal (deep compare). + Returns false if input is NULL. + @note the result may be inaccurate if object has duplicate keys. + @warning This function is recursive and may cause a stack overflow + if the object level is too deep. */ +yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, + yyjson_mut_val *rhs); + +/** Set the value to raw. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_raw(yyjson_mut_val *val, + const char *raw, size_t len); + +/** Set the value to null. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val *val); + +/** Set the value to bool. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val *val, bool num); + +/** Set the value to uint. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_uint(yyjson_mut_val *val, uint64_t num); + +/** Set the value to sint. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num); + +/** Set the value to int. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num); + +/** Set the value to float. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_float(yyjson_mut_val *val, float num); + +/** Set the value to double. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_double(yyjson_mut_val *val, double num); + +/** Set the value to real. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val *val, double num); + +/** Set the floating-point number's output format to fixed-point notation. + Returns false if input is NULL or `val` is not real type. + @see YYJSON_WRITE_FP_TO_FIXED flag. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_mut_set_fp_to_fixed(yyjson_mut_val *val, + int prec); + +/** Set the floating-point number's output format to single-precision. + Returns false if input is NULL or `val` is not real type. + @see YYJSON_WRITE_FP_TO_FLOAT flag. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_mut_set_fp_to_float(yyjson_mut_val *val, + bool flt); + +/** Set the value to string (null-terminated). + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_str(yyjson_mut_val *val, const char *str); + +/** Set the value to string (with length). + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, + const char *str, size_t len); + +/** Marks this string as not needing to be escaped during JSON writing. + This can be used to avoid the overhead of escaping if the string contains + only characters that do not require escaping. + Returns false if input is NULL or `val` is not string. + @see YYJSON_SUBTYPE_NOESC subtype. + @warning This will modify the `immutable` value, use with caution. */ +yyjson_api_inline bool yyjson_mut_set_str_noesc(yyjson_mut_val *val, + bool noesc); + +/** Set the value to array. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val *val); + +/** Set the value to array. + Returns false if input is NULL. + @warning This function should not be used on an existing object or array. */ +yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val); + + + +/*============================================================================== + * MARK: - Mutable JSON Value Creation API + *============================================================================*/ + +/** Creates and returns a raw value, returns NULL on error. + The `str` should be a null-terminated UTF-8 string. + + @warning The input string is not copied, you should keep this string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, + const char *str); + +/** Creates and returns a raw value, returns NULL on error. + The `str` should be a UTF-8 string, null-terminator is not required. + + @warning The input string is not copied, you should keep this string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_rawn(yyjson_mut_doc *doc, + const char *str, + size_t len); + +/** Creates and returns a raw value, returns NULL on error. + The `str` should be a null-terminated UTF-8 string. + The input string is copied and held by the document. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_rawcpy(yyjson_mut_doc *doc, + const char *str); + +/** Creates and returns a raw value, returns NULL on error. + The `str` should be a UTF-8 string, null-terminator is not required. + The input string is copied and held by the document. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_rawncpy(yyjson_mut_doc *doc, + const char *str, + size_t len); + +/** Creates and returns a null value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc); + +/** Creates and returns a true value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc); + +/** Creates and returns a false value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc); + +/** Creates and returns a bool value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc, + bool val); + +/** Creates and returns an unsigned integer value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc, + uint64_t num); + +/** Creates and returns a signed integer value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc, + int64_t num); + +/** Creates and returns a signed integer value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc, + int64_t num); + +/** Creates and returns a float number value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_float(yyjson_mut_doc *doc, + float num); + +/** Creates and returns a double number value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_double(yyjson_mut_doc *doc, + double num); + +/** Creates and returns a real number value, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc, + double num); + +/** Creates and returns a string value, returns NULL on error. + The `str` should be a null-terminated UTF-8 string. + @warning The input string is not copied, you should keep this string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc, + const char *str); + +/** Creates and returns a string value, returns NULL on error. + The `str` should be a UTF-8 string, null-terminator is not required. + @warning The input string is not copied, you should keep this string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc, + const char *str, + size_t len); + +/** Creates and returns a string value, returns NULL on error. + The `str` should be a null-terminated UTF-8 string. + The input string is copied and held by the document. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc, + const char *str); + +/** Creates and returns a string value, returns NULL on error. + The `str` should be a UTF-8 string, null-terminator is not required. + The input string is copied and held by the document. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, + const char *str, + size_t len); + + + +/*============================================================================== + * MARK: - Mutable JSON Array API + *============================================================================*/ + +/** Returns the number of elements in this array. + Returns 0 if `arr` is NULL or type is not array. */ +yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr); + +/** Returns the element at the specified position in this array. + Returns NULL if array is NULL/empty or the index is out of bounds. + @warning This function takes a linear search time. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, + size_t idx); + +/** Returns the first element of this array. + Returns NULL if `arr` is NULL/empty or type is not array. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first(yyjson_mut_val *arr); + +/** Returns the last element of this array. + Returns NULL if `arr` is NULL/empty or type is not array. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last(yyjson_mut_val *arr); + + + +/*============================================================================== + * MARK: - Mutable JSON Array Iterator API + *============================================================================*/ + +/** + A mutable JSON array iterator. + + @warning You should not modify the array while iterating over it, but you can + use `yyjson_mut_arr_iter_remove()` to remove current value. + + @b Example + @code + yyjson_mut_val *val; + yyjson_mut_arr_iter iter = yyjson_mut_arr_iter_with(arr); + while ((val = yyjson_mut_arr_iter_next(&iter))) { + your_func(val); + if (your_val_is_unused(val)) { + yyjson_mut_arr_iter_remove(&iter); + } + } + @endcode + */ +typedef struct yyjson_mut_arr_iter { + size_t idx; /**< next value's index */ + size_t max; /**< maximum index (arr.size) */ + yyjson_mut_val *cur; /**< current value */ + yyjson_mut_val *pre; /**< previous value */ + yyjson_mut_val *arr; /**< the array being iterated */ +} yyjson_mut_arr_iter; + +/** + Initialize an iterator for this array. + + @param arr The array to be iterated over. + If this parameter is NULL or not an array, `iter` will be set to empty. + @param iter The iterator to be initialized. + If this parameter is NULL, the function will fail and return false. + @return true if the `iter` has been successfully initialized. + + @note The iterator does not need to be destroyed. + */ +yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, + yyjson_mut_arr_iter *iter); + +/** + Create an iterator with an array , same as `yyjson_mut_arr_iter_init()`. + + @param arr The array to be iterated over. + If this parameter is NULL or not an array, an empty iterator will returned. + @return A new iterator for the array. + + @note The iterator does not need to be destroyed. + */ +yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( + yyjson_mut_val *arr); + +/** + Returns whether the iteration has more elements. + If `iter` is NULL, this function will return false. + */ +yyjson_api_inline bool yyjson_mut_arr_iter_has_next( + yyjson_mut_arr_iter *iter); + +/** + Returns the next element in the iteration, or NULL on end. + If `iter` is NULL, this function will return NULL. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next( + yyjson_mut_arr_iter *iter); + +/** + Removes and returns current element in the iteration. + If `iter` is NULL, this function will return NULL. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( + yyjson_mut_arr_iter *iter); + +/** + Macro for iterating over an array. + It works like iterator, but with a more intuitive API. + + @warning You should not modify the array while iterating over it. + + @b Example + @code + size_t idx, max; + yyjson_mut_val *val; + yyjson_mut_arr_foreach(arr, idx, max, val) { + your_func(idx, val); + } + @endcode + */ +#define yyjson_mut_arr_foreach(arr, idx, max, val) \ + for ((idx) = 0, \ + (max) = yyjson_mut_arr_size(arr), \ + (val) = yyjson_mut_arr_get_first(arr); \ + (idx) < (max); \ + (idx)++, \ + (val) = (val)->next) + + + +/*============================================================================== + * MARK: - Mutable JSON Array Creation API + *============================================================================*/ + +/** + Creates and returns an empty mutable array. + @param doc A mutable document, used for memory allocation only. + @return The new array. NULL if input is NULL or memory allocation failed. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc); + +/** + Creates and returns a new mutable array with the given boolean values. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of boolean values. + @param count The value count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const bool vals[3] = { true, false, true }; + yyjson_mut_val *arr = yyjson_mut_arr_with_bool(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( + yyjson_mut_doc *doc, const bool *vals, size_t count); + +/** + Creates and returns a new mutable array with the given sint numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of sint numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const int64_t vals[3] = { -1, 0, 1 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( + yyjson_mut_doc *doc, const int64_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given uint numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of uint numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const uint64_t vals[3] = { 0, 1, 0 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_uint(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( + yyjson_mut_doc *doc, const uint64_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given real numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of real numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const double vals[3] = { 0.1, 0.2, 0.3 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_real(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( + yyjson_mut_doc *doc, const double *vals, size_t count); + +/** + Creates and returns a new mutable array with the given int8 numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of int8 numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const int8_t vals[3] = { -1, 0, 1 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_sint8(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( + yyjson_mut_doc *doc, const int8_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given int16 numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of int16 numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const int16_t vals[3] = { -1, 0, 1 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_sint16(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( + yyjson_mut_doc *doc, const int16_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given int32 numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of int32 numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const int32_t vals[3] = { -1, 0, 1 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_sint32(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( + yyjson_mut_doc *doc, const int32_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given int64 numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of int64 numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const int64_t vals[3] = { -1, 0, 1 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( + yyjson_mut_doc *doc, const int64_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given uint8 numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of uint8 numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const uint8_t vals[3] = { 0, 1, 0 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_uint8(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( + yyjson_mut_doc *doc, const uint8_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given uint16 numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of uint16 numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const uint16_t vals[3] = { 0, 1, 0 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_uint16(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( + yyjson_mut_doc *doc, const uint16_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given uint32 numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of uint32 numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const uint32_t vals[3] = { 0, 1, 0 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_uint32(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( + yyjson_mut_doc *doc, const uint32_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given uint64 numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of uint64 numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const uint64_t vals[3] = { 0, 1, 0 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_uint64(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( + yyjson_mut_doc *doc, const uint64_t *vals, size_t count); + +/** + Creates and returns a new mutable array with the given float numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of float numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const float vals[3] = { -1.0f, 0.0f, 1.0f }; + yyjson_mut_val *arr = yyjson_mut_arr_with_float(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( + yyjson_mut_doc *doc, const float *vals, size_t count); + +/** + Creates and returns a new mutable array with the given double numbers. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of double numbers. + @param count The number count. If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const double vals[3] = { -1.0, 0.0, 1.0 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_double(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( + yyjson_mut_doc *doc, const double *vals, size_t count); + +/** + Creates and returns a new mutable array with the given strings, these strings + will not be copied. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of UTF-8 null-terminator strings. + If this array contains NULL, the function will fail and return NULL. + @param count The number of values in `vals`. + If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @warning The input strings are not copied, you should keep these strings + unmodified for the lifetime of this JSON document. If these strings will be + modified, you should use `yyjson_mut_arr_with_strcpy()` instead. + + @b Example + @code + const char *vals[3] = { "a", "b", "c" }; + yyjson_mut_val *arr = yyjson_mut_arr_with_str(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( + yyjson_mut_doc *doc, const char **vals, size_t count); + +/** + Creates and returns a new mutable array with the given strings and string + lengths, these strings will not be copied. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of UTF-8 strings, null-terminator is not required. + If this array contains NULL, the function will fail and return NULL. + @param lens A C array of string lengths, in bytes. + @param count The number of strings in `vals`. + If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @warning The input strings are not copied, you should keep these strings + unmodified for the lifetime of this JSON document. If these strings will be + modified, you should use `yyjson_mut_arr_with_strncpy()` instead. + + @b Example + @code + const char *vals[3] = { "a", "bb", "c" }; + const size_t lens[3] = { 1, 2, 1 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_strn(doc, vals, lens, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( + yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count); + +/** + Creates and returns a new mutable array with the given strings, these strings + will be copied. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of UTF-8 null-terminator strings. + If this array contains NULL, the function will fail and return NULL. + @param count The number of values in `vals`. + If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const char *vals[3] = { "a", "b", "c" }; + yyjson_mut_val *arr = yyjson_mut_arr_with_strcpy(doc, vals, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( + yyjson_mut_doc *doc, const char **vals, size_t count); + +/** + Creates and returns a new mutable array with the given strings and string + lengths, these strings will be copied. + + @param doc A mutable document, used for memory allocation only. + If this parameter is NULL, the function will fail and return NULL. + @param vals A C array of UTF-8 strings, null-terminator is not required. + If this array contains NULL, the function will fail and return NULL. + @param lens A C array of string lengths, in bytes. + @param count The number of strings in `vals`. + If this value is 0, an empty array will return. + @return The new array. NULL if input is invalid or memory allocation failed. + + @b Example + @code + const char *vals[3] = { "a", "bb", "c" }; + const size_t lens[3] = { 1, 2, 1 }; + yyjson_mut_val *arr = yyjson_mut_arr_with_strn(doc, vals, lens, 3); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( + yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count); + + + +/*============================================================================== + * MARK: - Mutable JSON Array Modification API + *============================================================================*/ + +/** + Inserts a value into an array at a given index. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param val The value to be inserted. Returns false if it is NULL. + @param idx The index to which to insert the new value. + Returns false if the index is out of range. + @return Whether successful. + @warning This function takes a linear search time. + */ +yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, + yyjson_mut_val *val, size_t idx); + +/** + Inserts a value at the end of the array. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param val The value to be inserted. Returns false if it is NULL. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, + yyjson_mut_val *val); + +/** + Inserts a value at the head of the array. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param val The value to be inserted. Returns false if it is NULL. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, + yyjson_mut_val *val); + +/** + Replaces a value at index and returns old value. + @param arr The array to which the value is to be replaced. + Returns false if it is NULL or not an array. + @param idx The index to which to replace the value. + Returns false if the index is out of range. + @param val The new value to replace. Returns false if it is NULL. + @return Old value, or NULL on error. + @warning This function takes a linear search time. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr, + size_t idx, + yyjson_mut_val *val); + +/** + Removes and returns a value at index. + @param arr The array from which the value is to be removed. + Returns false if it is NULL or not an array. + @param idx The index from which to remove the value. + Returns false if the index is out of range. + @return Old value, or NULL on error. + @warning This function takes a linear search time. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, + size_t idx); + +/** + Removes and returns the first value in this array. + @param arr The array from which the value is to be removed. + Returns false if it is NULL or not an array. + @return The first value, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first( + yyjson_mut_val *arr); + +/** + Removes and returns the last value in this array. + @param arr The array from which the value is to be removed. + Returns false if it is NULL or not an array. + @return The last value, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( + yyjson_mut_val *arr); + +/** + Removes all values within a specified range in the array. + @param arr The array from which the value is to be removed. + Returns false if it is NULL or not an array. + @param idx The start index of the range (0 is the first). + @param len The number of items in the range (can be 0). + @return Whether successful. + @warning This function takes a linear search time. + */ +yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, + size_t idx, size_t len); + +/** + Removes all values in this array. + @param arr The array from which all of the values are to be removed. + Returns false if it is NULL or not an array. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr); + +/** + Rotates values in this array for the given number of times. + For example: `[1,2,3,4,5]` rotate 2 is `[3,4,5,1,2]`. + @param arr The array to be rotated. + @param idx Index (or times) to rotate. + @warning This function takes a linear search time. + */ +yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, + size_t idx); + + + +/*============================================================================== + * MARK: - Mutable JSON Array Modification Convenience API + *============================================================================*/ + +/** + Adds a value at the end of the array. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param val The value to be inserted. Returns false if it is NULL. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, + yyjson_mut_val *val); + +/** + Adds a `null` value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, + yyjson_mut_val *arr); + +/** + Adds a `true` value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, + yyjson_mut_val *arr); + +/** + Adds a `false` value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, + yyjson_mut_val *arr); + +/** + Adds a bool value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param val The bool value to be added. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + bool val); + +/** + Adds an unsigned integer value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param num The number to be added. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + uint64_t num); + +/** + Adds a signed integer value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param num The number to be added. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + int64_t num); + +/** + Adds an integer value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param num The number to be added. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + int64_t num); + +/** + Adds a float value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param num The number to be added. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_float(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + float num); + +/** + Adds a double value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param num The number to be added. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_double(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + double num); + +/** + Adds a double value at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param num The number to be added. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + double num); + +/** + Adds a string value at the end of the array (no copy). + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param str A null-terminated UTF-8 string. + @return Whether successful. + @warning The input string is not copied, you should keep this string unmodified + for the lifetime of this JSON document. + */ +yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + const char *str); + +/** + Adds a string value at the end of the array (no copy). + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param str A UTF-8 string, null-terminator is not required. + @param len The length of the string, in bytes. + @return Whether successful. + @warning The input string is not copied, you should keep this string unmodified + for the lifetime of this JSON document. + */ +yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + const char *str, + size_t len); + +/** + Adds a string value at the end of the array (copied). + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param str A null-terminated UTF-8 string. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + const char *str); + +/** + Adds a string value at the end of the array (copied). + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @param str A UTF-8 string, null-terminator is not required. + @param len The length of the string, in bytes. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + const char *str, + size_t len); + +/** + Creates and adds a new array at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @return The new array, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, + yyjson_mut_val *arr); + +/** + Creates and adds a new object at the end of the array. + @param doc The `doc` is only used for memory allocation. + @param arr The array to which the value is to be inserted. + Returns false if it is NULL or not an array. + @return The new object, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, + yyjson_mut_val *arr); + + + +/*============================================================================== + * MARK: - Mutable JSON Object API + *============================================================================*/ + +/** Returns the number of key-value pairs in this object. + Returns 0 if `obj` is NULL or type is not object. */ +yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj); + +/** Returns the value to which the specified key is mapped. + Returns NULL if this object contains no mapping for the key. + Returns NULL if `obj/key` is NULL, or type is not object. + + The `key` should be a null-terminated UTF-8 string. + + @warning This function takes a linear search time. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj, + const char *key); + +/** Returns the value to which the specified key is mapped. + Returns NULL if this object contains no mapping for the key. + Returns NULL if `obj/key` is NULL, or type is not object. + + The `key` should be a UTF-8 string, null-terminator is not required. + The `key_len` should be the length of the key, in bytes. + + @warning This function takes a linear search time. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, + const char *key, + size_t key_len); + + + +/*============================================================================== + * MARK: - Mutable JSON Object Iterator API + *============================================================================*/ + +/** + A mutable JSON object iterator. + + @warning You should not modify the object while iterating over it, but you can + use `yyjson_mut_obj_iter_remove()` to remove current value. + + @b Example + @code + yyjson_mut_val *key, *val; + yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); + while ((key = yyjson_mut_obj_iter_next(&iter))) { + val = yyjson_mut_obj_iter_get_val(key); + your_func(key, val); + if (your_val_is_unused(key, val)) { + yyjson_mut_obj_iter_remove(&iter); + } + } + @endcode + + If the ordering of the keys is known at compile-time, you can use this method + to speed up value lookups: + @code + // {"k1":1, "k2": 3, "k3": 3} + yyjson_mut_val *key, *val; + yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); + yyjson_mut_val *v1 = yyjson_mut_obj_iter_get(&iter, "k1"); + yyjson_mut_val *v3 = yyjson_mut_obj_iter_get(&iter, "k3"); + @endcode + @see `yyjson_mut_obj_iter_get()` and `yyjson_mut_obj_iter_getn()` + */ +typedef struct yyjson_mut_obj_iter { + size_t idx; /**< next key's index */ + size_t max; /**< maximum key index (obj.size) */ + yyjson_mut_val *cur; /**< current key */ + yyjson_mut_val *pre; /**< previous key */ + yyjson_mut_val *obj; /**< the object being iterated */ +} yyjson_mut_obj_iter; + +/** + Initialize an iterator for this object. + + @param obj The object to be iterated over. + If this parameter is NULL or not an array, `iter` will be set to empty. + @param iter The iterator to be initialized. + If this parameter is NULL, the function will fail and return false. + @return true if the `iter` has been successfully initialized. + + @note The iterator does not need to be destroyed. + */ +yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, + yyjson_mut_obj_iter *iter); + +/** + Create an iterator with an object, same as `yyjson_obj_iter_init()`. + + @param obj The object to be iterated over. + If this parameter is NULL or not an object, an empty iterator will returned. + @return A new iterator for the object. + + @note The iterator does not need to be destroyed. + */ +yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( + yyjson_mut_val *obj); + +/** + Returns whether the iteration has more elements. + If `iter` is NULL, this function will return false. + */ +yyjson_api_inline bool yyjson_mut_obj_iter_has_next( + yyjson_mut_obj_iter *iter); + +/** + Returns the next key in the iteration, or NULL on end. + If `iter` is NULL, this function will return NULL. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next( + yyjson_mut_obj_iter *iter); + +/** + Returns the value for key inside the iteration. + If `iter` is NULL, this function will return NULL. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val( + yyjson_mut_val *key); + +/** + Removes current key-value pair in the iteration, returns the removed value. + If `iter` is NULL, this function will return NULL. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove( + yyjson_mut_obj_iter *iter); + +/** + Iterates to a specified key and returns the value. + + This function does the same thing as `yyjson_mut_obj_get()`, but is much faster + if the ordering of the keys is known at compile-time and you are using the same + order to look up the values. If the key exists in this object, then the + iterator will stop at the next key, otherwise the iterator will not change and + NULL is returned. + + @param iter The object iterator, should not be NULL. + @param key The key, should be a UTF-8 string with null-terminator. + @return The value to which the specified key is mapped. + NULL if this object contains no mapping for the key or input is invalid. + + @warning This function takes a linear search time if the key is not nearby. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( + yyjson_mut_obj_iter *iter, const char *key); + +/** + Iterates to a specified key and returns the value. + + This function does the same thing as `yyjson_mut_obj_getn()` but is much faster + if the ordering of the keys is known at compile-time and you are using the same + order to look up the values. If the key exists in this object, then the + iterator will stop at the next key, otherwise the iterator will not change and + NULL is returned. + + @param iter The object iterator, should not be NULL. + @param key The key, should be a UTF-8 string, null-terminator is not required. + @param key_len The the length of `key`, in bytes. + @return The value to which the specified key is mapped. + NULL if this object contains no mapping for the key or input is invalid. + + @warning This function takes a linear search time if the key is not nearby. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( + yyjson_mut_obj_iter *iter, const char *key, size_t key_len); + +/** + Macro for iterating over an object. + It works like iterator, but with a more intuitive API. + + @warning You should not modify the object while iterating over it. + + @b Example + @code + size_t idx, max; + yyjson_mut_val *key, *val; + yyjson_mut_obj_foreach(obj, idx, max, key, val) { + your_func(key, val); + } + @endcode + */ +#define yyjson_mut_obj_foreach(obj, idx, max, key, val) \ + for ((idx) = 0, \ + (max) = yyjson_mut_obj_size(obj), \ + (key) = (max) ? ((yyjson_mut_val *)(obj)->uni.ptr)->next->next : NULL, \ + (val) = (key) ? (key)->next : NULL; \ + (idx) < (max); \ + (idx)++, \ + (key) = (val)->next, \ + (val) = (key)->next) + + + +/*============================================================================== + * MARK: - Mutable JSON Object Creation API + *============================================================================*/ + +/** Creates and returns a mutable object, returns NULL on error. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc); + +/** + Creates and returns a mutable object with keys and values, returns NULL on + error. The keys and values are not copied. The strings should be a + null-terminated UTF-8 string. + + @warning The input string is not copied, you should keep this string + unmodified for the lifetime of this JSON document. + + @b Example + @code + const char *keys[2] = { "id", "name" }; + const char *vals[2] = { "01", "Harry" }; + yyjson_mut_val *obj = yyjson_mut_obj_with_str(doc, keys, vals, 2); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, + const char **keys, + const char **vals, + size_t count); + +/** + Creates and returns a mutable object with key-value pairs and pair count, + returns NULL on error. The keys and values are not copied. The strings should + be a null-terminated UTF-8 string. + + @warning The input string is not copied, you should keep this string + unmodified for the lifetime of this JSON document. + + @b Example + @code + const char *kv_pairs[4] = { "id", "01", "name", "Harry" }; + yyjson_mut_val *obj = yyjson_mut_obj_with_kv(doc, kv_pairs, 2); + @endcode + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, + const char **kv_pairs, + size_t pair_count); + + + +/*============================================================================== + * MARK: - Mutable JSON Object Modification API + *============================================================================*/ + +/** + Adds a key-value pair at the end of the object. + This function allows duplicated key in one object. + @param obj The object to which the new key-value pair is to be added. + @param key The key, should be a string which is created by `yyjson_mut_str()`, + `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. + @param val The value to add to the object. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val); +/** + Sets a key-value pair at the end of the object. + This function may remove all key-value pairs for the given key before add. + @param obj The object to which the new key-value pair is to be added. + @param key The key, should be a string which is created by `yyjson_mut_str()`, + `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. + @param val The value to add to the object. If this value is null, the behavior + is same as `yyjson_mut_obj_remove()`. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val); + +/** + Inserts a key-value pair to the object at the given position. + This function allows duplicated key in one object. + @param obj The object to which the new key-value pair is to be added. + @param key The key, should be a string which is created by `yyjson_mut_str()`, + `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. + @param val The value to add to the object. + @param idx The index to which to insert the new pair. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val, + size_t idx); + +/** + Removes all key-value pair from the object with given key. + @param obj The object from which the key-value pair is to be removed. + @param key The key, should be a string value. + @return The first matched value, or NULL if no matched value. + @warning This function takes a linear search time. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj, + yyjson_mut_val *key); + +/** + Removes all key-value pair from the object with given key. + @param obj The object from which the key-value pair is to be removed. + @param key The key, should be a UTF-8 string with null-terminator. + @return The first matched value, or NULL if no matched value. + @warning This function takes a linear search time. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_key( + yyjson_mut_val *obj, const char *key); + +/** + Removes all key-value pair from the object with given key. + @param obj The object from which the key-value pair is to be removed. + @param key The key, should be a UTF-8 string, null-terminator is not required. + @param key_len The length of the key. + @return The first matched value, or NULL if no matched value. + @warning This function takes a linear search time. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( + yyjson_mut_val *obj, const char *key, size_t key_len); + +/** + Removes all key-value pairs in this object. + @param obj The object from which all of the values are to be removed. + @return Whether successful. + */ +yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj); + +/** + Replaces value from the object with given key. + If the key is not exist, or the value is NULL, it will fail. + @param obj The object to which the value is to be replaced. + @param key The key, should be a string value. + @param val The value to replace into the object. + @return Whether successful. + @warning This function takes a linear search time. + */ +yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val); + +/** + Rotates key-value pairs in the object for the given number of times. + For example: `{"a":1,"b":2,"c":3,"d":4}` rotate 1 is + `{"b":2,"c":3,"d":4,"a":1}`. + @param obj The object to be rotated. + @param idx Index (or times) to rotate. + @return Whether successful. + @warning This function takes a linear search time. + */ +yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, + size_t idx); + + + +/*============================================================================== + * MARK: - Mutable JSON Object Modification Convenience API + *============================================================================*/ + +/** Adds a `null` value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key); + +/** Adds a `true` value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key); + +/** Adds a `false` value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key); + +/** Adds a bool value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, bool val); + +/** Adds an unsigned integer value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, uint64_t val); + +/** Adds a signed integer value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, int64_t val); + +/** Adds an int value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, int64_t val); + +/** Adds a float value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_float(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, float val); + +/** Adds a double value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_double(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, double val); + +/** Adds a real value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, double val); + +/** Adds a string value at the end of the object. + The `key` and `val` should be null-terminated UTF-8 strings. + This function allows duplicated key in one object. + + @warning The key/value strings are not copied, you should keep these strings + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, const char *val); + +/** Adds a string value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + The `val` should be a UTF-8 string, null-terminator is not required. + The `len` should be the length of the `val`, in bytes. + This function allows duplicated key in one object. + + @warning The key/value strings are not copied, you should keep these strings + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, + const char *val, size_t len); + +/** Adds a string value at the end of the object. + The `key` and `val` should be null-terminated UTF-8 strings. + The value string is copied. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, + const char *val); + +/** Adds a string value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + The `val` should be a UTF-8 string, null-terminator is not required. + The `len` should be the length of the `val`, in bytes. + This function allows duplicated key in one object. + + @warning The key strings are not copied, you should keep these strings + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, + const char *val, size_t len); + +/** + Creates and adds a new array to the target object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep these strings + unmodified for the lifetime of this JSON document. + @return The new array, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_arr(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key); + +/** + Creates and adds a new object to the target object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep these strings + unmodified for the lifetime of this JSON document. + @return The new object, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_obj(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key); + +/** Adds a JSON value at the end of the object. + The `key` should be a null-terminated UTF-8 string. + This function allows duplicated key in one object. + + @warning The key string is not copied, you should keep the string + unmodified for the lifetime of this JSON document. */ +yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, + yyjson_mut_val *val); + +/** Removes all key-value pairs for the given key. + Returns the first value to which the specified key is mapped or NULL if this + object contains no mapping for the key. + The `key` should be a null-terminated UTF-8 string. + + @warning This function takes a linear search time. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str( + yyjson_mut_val *obj, const char *key); + +/** Removes all key-value pairs for the given key. + Returns the first value to which the specified key is mapped or NULL if this + object contains no mapping for the key. + The `key` should be a UTF-8 string, null-terminator is not required. + The `len` should be the length of the key, in bytes. + + @warning This function takes a linear search time. */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( + yyjson_mut_val *obj, const char *key, size_t len); + +/** Replaces all matching keys with the new key. + Returns true if at least one key was renamed. + The `key` and `new_key` should be a null-terminated UTF-8 string. + The `new_key` is copied and held by doc. + + @warning This function takes a linear search time. + If `new_key` already exists, it will cause duplicate keys. + */ +yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, + const char *new_key); + +/** Replaces all matching keys with the new key. + Returns true if at least one key was renamed. + The `key` and `new_key` should be a UTF-8 string, + null-terminator is not required. The `new_key` is copied and held by doc. + + @warning This function takes a linear search time. + If `new_key` already exists, it will cause duplicate keys. + */ +yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, + size_t len, + const char *new_key, + size_t new_len); + + + +#if !defined(YYJSON_DISABLE_UTILS) || !YYJSON_DISABLE_UTILS + +/*============================================================================== + * MARK: - JSON Pointer API (RFC 6901) + * https://tools.ietf.org/html/rfc6901 + *============================================================================*/ + +/** JSON Pointer error code. */ +typedef uint32_t yyjson_ptr_code; + +/** No JSON pointer error. */ +static const yyjson_ptr_code YYJSON_PTR_ERR_NONE = 0; + +/** Invalid input parameter, such as NULL input. */ +static const yyjson_ptr_code YYJSON_PTR_ERR_PARAMETER = 1; + +/** JSON pointer syntax error, such as invalid escape, token no prefix. */ +static const yyjson_ptr_code YYJSON_PTR_ERR_SYNTAX = 2; + +/** JSON pointer resolve failed, such as index out of range, key not found. */ +static const yyjson_ptr_code YYJSON_PTR_ERR_RESOLVE = 3; + +/** Document's root is NULL, but it is required for the function call. */ +static const yyjson_ptr_code YYJSON_PTR_ERR_NULL_ROOT = 4; + +/** Cannot set root as the target is not a document. */ +static const yyjson_ptr_code YYJSON_PTR_ERR_SET_ROOT = 5; + +/** The memory allocation failed and a new value could not be created. */ +static const yyjson_ptr_code YYJSON_PTR_ERR_MEMORY_ALLOCATION = 6; + +/** Error information for JSON pointer. */ +typedef struct yyjson_ptr_err { + /** Error code, see `yyjson_ptr_code` for all possible values. */ + yyjson_ptr_code code; + /** Error message, constant, no need to free (NULL if no error). */ + const char *msg; + /** Error byte position for input JSON pointer (0 if no error). */ + size_t pos; +} yyjson_ptr_err; + +/** + A context for JSON pointer operation. + + This struct stores the context of JSON Pointer operation result. The struct + can be used with three helper functions: `ctx_append()`, `ctx_replace()`, and + `ctx_remove()`, which perform the corresponding operations on the container + without re-parsing the JSON Pointer. + + For example: + @code + // doc before: {"a":[0,1,null]} + // ptr: "/a/2" + val = yyjson_mut_doc_ptr_getx(doc, ptr, strlen(ptr), &ctx, &err); + if (yyjson_is_null(val)) { + yyjson_ptr_ctx_remove(&ctx); + } + // doc after: {"a":[0,1]} + @endcode + */ +typedef struct yyjson_ptr_ctx { + /** + The container (parent) of the target value. It can be either an array or + an object. If the target location has no value, but all its parent + containers exist, and the target location can be used to insert a new + value, then `ctn` is the parent container of the target location. + Otherwise, `ctn` is NULL. + */ + yyjson_mut_val *ctn; + /** + The previous sibling of the target value. It can be either a value in an + array or a key in an object. As the container is a `circular linked list` + of elements, `pre` is the previous node of the target value. If the + operation is `add` or `set`, then `pre` is the previous node of the new + value, not the original target value. If the target value does not exist, + `pre` is NULL. + */ + yyjson_mut_val *pre; + /** + The removed value if the operation is `set`, `replace` or `remove`. It can + be used to restore the original state of the document if needed. + */ + yyjson_mut_val *old; +} yyjson_ptr_ctx; + +/** + Get value by a JSON Pointer. + @param doc The JSON document to be queried. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @return The value referenced by the JSON pointer. + NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, + const char *ptr); + +/** + Get value by a JSON Pointer. + @param doc The JSON document to be queried. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @return The value referenced by the JSON pointer. + NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, + const char *ptr, size_t len); + +/** + Get value by a JSON Pointer. + @param doc The JSON document to be queried. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param err A pointer to store the error information, or NULL if not needed. + @return The value referenced by the JSON pointer. + NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, + const char *ptr, size_t len, + yyjson_ptr_err *err); + +/** + Get value by a JSON Pointer. + @param val The JSON value to be queried. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @return The value referenced by the JSON pointer. + NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, + const char *ptr); + +/** + Get value by a JSON Pointer. + @param val The JSON value to be queried. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @return The value referenced by the JSON pointer. + NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, + const char *ptr, size_t len); + +/** + Get value by a JSON Pointer. + @param val The JSON value to be queried. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param err A pointer to store the error information, or NULL if not needed. + @return The value referenced by the JSON pointer. + NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, + const char *ptr, size_t len, + yyjson_ptr_err *err); + +/** + Get value by a JSON Pointer. + @param doc The JSON document to be queried. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @return The value referenced by the JSON pointer. + NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, + const char *ptr); + +/** + Get value by a JSON Pointer. + @param doc The JSON document to be queried. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @return The value referenced by the JSON pointer. + NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, + const char *ptr, + size_t len); + +/** + Get value by a JSON Pointer. + @param doc The JSON document to be queried. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return The value referenced by the JSON pointer. + NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, + const char *ptr, + size_t len, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/** + Get value by a JSON Pointer. + @param val The JSON value to be queried. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @return The value referenced by the JSON pointer. + NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, + const char *ptr); + +/** + Get value by a JSON Pointer. + @param val The JSON value to be queried. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @return The value referenced by the JSON pointer. + NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, + const char *ptr, + size_t len); + +/** + Get value by a JSON Pointer. + @param val The JSON value to be queried. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return The value referenced by the JSON pointer. + NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(yyjson_mut_val *val, + const char *ptr, + size_t len, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/** + Add (insert) value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @param new_val The value to be added. + @return true if JSON pointer is valid and new value is added, false otherwise. + @note The parent nodes will be created if they do not exist. + */ +yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, + const char *ptr, + yyjson_mut_val *new_val); + +/** + Add (insert) value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The value to be added. + @return true if JSON pointer is valid and new value is added, false otherwise. + @note The parent nodes will be created if they do not exist. + */ +yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, + const char *ptr, size_t len, + yyjson_mut_val *new_val); + +/** + Add (insert) value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The value to be added. + @param create_parent Whether to create parent nodes if not exist. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return true if JSON pointer is valid and new value is added, false otherwise. + */ +yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + bool create_parent, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/** + Add (insert) value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @param doc Only used to create new values when needed. + @param new_val The value to be added. + @return true if JSON pointer is valid and new value is added, false otherwise. + @note The parent nodes will be created if they do not exist. + */ +yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, + const char *ptr, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc); + +/** + Add (insert) value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param doc Only used to create new values when needed. + @param new_val The value to be added. + @return true if JSON pointer is valid and new value is added, false otherwise. + @note The parent nodes will be created if they do not exist. + */ +yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc); + +/** + Add (insert) value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param doc Only used to create new values when needed. + @param new_val The value to be added. + @param create_parent Whether to create parent nodes if not exist. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return true if JSON pointer is valid and new value is added, false otherwise. + */ +yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc, + bool create_parent, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/** + Set value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @param new_val The value to be set, pass NULL to remove. + @return true if JSON pointer is valid and new value is set, false otherwise. + @note The parent nodes will be created if they do not exist. + If the target value already exists, it will be replaced by the new value. + */ +yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, + const char *ptr, + yyjson_mut_val *new_val); + +/** + Set value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The value to be set, pass NULL to remove. + @return true if JSON pointer is valid and new value is set, false otherwise. + @note The parent nodes will be created if they do not exist. + If the target value already exists, it will be replaced by the new value. + */ +yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, + const char *ptr, size_t len, + yyjson_mut_val *new_val); + +/** + Set value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The value to be set, pass NULL to remove. + @param create_parent Whether to create parent nodes if not exist. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return true if JSON pointer is valid and new value is set, false otherwise. + @note If the target value already exists, it will be replaced by the new value. + */ +yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + bool create_parent, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/** + Set value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @param new_val The value to be set, pass NULL to remove. + @param doc Only used to create new values when needed. + @return true if JSON pointer is valid and new value is set, false otherwise. + @note The parent nodes will be created if they do not exist. + If the target value already exists, it will be replaced by the new value. + */ +yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, + const char *ptr, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc); + +/** + Set value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The value to be set, pass NULL to remove. + @param doc Only used to create new values when needed. + @return true if JSON pointer is valid and new value is set, false otherwise. + @note The parent nodes will be created if they do not exist. + If the target value already exists, it will be replaced by the new value. + */ +yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc); + +/** + Set value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The value to be set, pass NULL to remove. + @param doc Only used to create new values when needed. + @param create_parent Whether to create parent nodes if not exist. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return true if JSON pointer is valid and new value is set, false otherwise. + @note If the target value already exists, it will be replaced by the new value. + */ +yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc, + bool create_parent, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/** + Replace value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @param new_val The new value to replace the old one. + @return The old value that was replaced, or NULL if not found. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replace( + yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val); + +/** + Replace value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The new value to replace the old one. + @return The old value that was replaced, or NULL if not found. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( + yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val); + +/** + Replace value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The new value to replace the old one. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return The old value that was replaced, or NULL if not found. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacex( + yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); + +/** + Replace value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @param new_val The new value to replace the old one. + @return The old value that was replaced, or NULL if not found. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replace( + yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val); + +/** + Replace value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The new value to replace the old one. + @return The old value that was replaced, or NULL if not found. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( + yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val); + +/** + Replace value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param new_val The new value to replace the old one. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return The old value that was replaced, or NULL if not found. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacex( + yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); + +/** + Remove value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @return The removed value, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_remove( + yyjson_mut_doc *doc, const char *ptr); + +/** + Remove value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @return The removed value, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( + yyjson_mut_doc *doc, const char *ptr, size_t len); + +/** + Remove value by a JSON pointer. + @param doc The target JSON document. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return The removed value, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removex( + yyjson_mut_doc *doc, const char *ptr, size_t len, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); + +/** + Remove value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8 with null-terminator). + @return The removed value, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_remove(yyjson_mut_val *val, + const char *ptr); + +/** + Remove value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @return The removed value, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removen(yyjson_mut_val *val, + const char *ptr, + size_t len); + +/** + Remove value by a JSON pointer. + @param val The target JSON value. + @param ptr The JSON pointer string (UTF-8, null-terminator is not required). + @param len The length of `ptr` in bytes. + @param ctx A pointer to store the result context, or NULL if not needed. + @param err A pointer to store the error information, or NULL if not needed. + @return The removed value, or NULL on error. + */ +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, + const char *ptr, + size_t len, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/** + Append value by JSON pointer context. + @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. + @param key New key if `ctx->ctn` is object, or NULL if `ctx->ctn` is array. + @param val New value to be added. + @return true on success or false on fail. + */ +yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, + yyjson_mut_val *key, + yyjson_mut_val *val); + +/** + Replace value by JSON pointer context. + @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. + @param val New value to be replaced. + @return true on success or false on fail. + @note If success, the old value will be returned via `ctx->old`. + */ +yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, + yyjson_mut_val *val); + +/** + Remove value by JSON pointer context. + @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. + @return true on success or false on fail. + @note If success, the old value will be returned via `ctx->old`. + */ +yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx); + + + +/*============================================================================== + * MARK: - JSON Patch API (RFC 6902) + * https://tools.ietf.org/html/rfc6902 + *============================================================================*/ + +/** Result code for JSON patch. */ +typedef uint32_t yyjson_patch_code; + +/** Success, no error. */ +static const yyjson_patch_code YYJSON_PATCH_SUCCESS = 0; + +/** Invalid parameter, such as NULL input or non-array patch. */ +static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_PARAMETER = 1; + +/** Memory allocation failure occurs. */ +static const yyjson_patch_code YYJSON_PATCH_ERROR_MEMORY_ALLOCATION = 2; + +/** JSON patch operation is not object type. */ +static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_OPERATION = 3; + +/** JSON patch operation is missing a required key. */ +static const yyjson_patch_code YYJSON_PATCH_ERROR_MISSING_KEY = 4; + +/** JSON patch operation member is invalid. */ +static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_MEMBER = 5; + +/** JSON patch operation `test` not equal. */ +static const yyjson_patch_code YYJSON_PATCH_ERROR_EQUAL = 6; + +/** JSON patch operation failed on JSON pointer. */ +static const yyjson_patch_code YYJSON_PATCH_ERROR_POINTER = 7; + +/** Error information for JSON patch. */ +typedef struct yyjson_patch_err { + /** Error code, see `yyjson_patch_code` for all possible values. */ + yyjson_patch_code code; + /** Index of the error operation (0 if no error). */ + size_t idx; + /** Error message, constant, no need to free (NULL if no error). */ + const char *msg; + /** JSON pointer error if `code == YYJSON_PATCH_ERROR_POINTER`. */ + yyjson_ptr_err ptr; +} yyjson_patch_err; + +/** + Creates and returns a patched JSON value (RFC 6902). + The memory of the returned value is allocated by the `doc`. + The `err` is used to receive error information, pass NULL if not needed. + Returns NULL if the patch could not be applied. + */ +yyjson_api yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, + yyjson_val *orig, + yyjson_val *patch, + yyjson_patch_err *err); + +/** + Creates and returns a patched JSON value (RFC 6902). + The memory of the returned value is allocated by the `doc`. + The `err` is used to receive error information, pass NULL if not needed. + Returns NULL if the patch could not be applied. + */ +yyjson_api yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, + yyjson_mut_val *orig, + yyjson_mut_val *patch, + yyjson_patch_err *err); + + + +/*============================================================================== + * MARK: - JSON Merge-Patch API (RFC 7386) + * https://tools.ietf.org/html/rfc7386 + *============================================================================*/ + +/** + Creates and returns a merge-patched JSON value (RFC 7386). + The memory of the returned value is allocated by the `doc`. + Returns NULL if the patch could not be applied. + + @warning This function is recursive and may cause a stack overflow if the + object level is too deep. + */ +yyjson_api yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, + yyjson_val *orig, + yyjson_val *patch); + +/** + Creates and returns a merge-patched JSON value (RFC 7386). + The memory of the returned value is allocated by the `doc`. + Returns NULL if the patch could not be applied. + + @warning This function is recursive and may cause a stack overflow if the + object level is too deep. + */ +yyjson_api yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, + yyjson_mut_val *orig, + yyjson_mut_val *patch); + +#endif /* YYJSON_DISABLE_UTILS */ + + + +/*============================================================================== + * MARK: - JSON Structure (Implementation) + *============================================================================*/ + +/** Payload of a JSON value (8 bytes). */ +typedef union yyjson_val_uni { + uint64_t u64; + int64_t i64; + double f64; + const char *str; + void *ptr; + size_t ofs; +} yyjson_val_uni; + +/** + Immutable JSON value, 16 bytes. + */ +struct yyjson_val { + uint64_t tag; /**< type, subtype and length */ + yyjson_val_uni uni; /**< payload */ +}; + +struct yyjson_doc { + /** Root value of the document (nonnull). */ + yyjson_val *root; + /** Allocator used by document (nonnull). */ + yyjson_alc alc; + /** The total number of bytes read when parsing JSON (nonzero). */ + size_t dat_read; + /** The total number of value read when parsing JSON (nonzero). */ + size_t val_read; + /** The string pool used by JSON values (nullable). */ + char *str_pool; +}; + + + +/*============================================================================== + * MARK: - Unsafe JSON Value API (Implementation) + *============================================================================*/ + +/* + Whether the string does not need to be escaped for serialization. + This function is used to optimize the writing speed of small constant strings. + This function works only if the compiler can evaluate it at compile time. + + Clang supports it since v8.0, + earlier versions do not support constant_p(strlen) and return false. + GCC supports it since at least v4.4, + earlier versions may compile it as run-time instructions. + ICC supports it since at least v16, + earlier versions are uncertain. + + @param str The C string. + @param len The returnd value from strlen(str). + */ +yyjson_api_inline bool unsafe_yyjson_is_str_noesc(const char *str, size_t len) { +#if YYJSON_HAS_CONSTANT_P && \ + (!YYJSON_IS_REAL_GCC || yyjson_gcc_available(4, 4, 0)) + if (yyjson_constant_p(len) && len <= 32) { + /* + Same as the following loop: + + for (size_t i = 0; i < len; i++) { + char c = str[i]; + if (c < ' ' || c > '~' || c == '"' || c == '\\') return false; + } + + GCC evaluates it at compile time only if the string length is within 17 + and -O3 (which turns on the -fpeel-loops flag) is used. + So the loop is unrolled for GCC. + */ +# define yyjson_repeat32_incr(x) \ + x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \ + x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) \ + x(16) x(17) x(18) x(19) x(20) x(21) x(22) x(23) \ + x(24) x(25) x(26) x(27) x(28) x(29) x(30) x(31) +# define yyjson_check_char_noesc(i) \ + if (i < len) { \ + char c = str[i]; \ + if (c < ' ' || c > '~' || c == '"' || c == '\\') return false; } + yyjson_repeat32_incr(yyjson_check_char_noesc) +# undef yyjson_repeat32_incr +# undef yyjson_check_char_noesc + return true; + } +#else + (void)str; + (void)len; +#endif + return false; +} + +yyjson_api_inline double unsafe_yyjson_u64_to_f64(uint64_t num) { +#if YYJSON_U64_TO_F64_NO_IMPL + uint64_t msb = ((uint64_t)1) << 63; + if ((num & msb) == 0) { + return (double)(int64_t)num; + } else { + return ((double)(int64_t)((num >> 1) | (num & 1))) * (double)2.0; + } +#else + return (double)num; +#endif +} + +yyjson_api_inline yyjson_type unsafe_yyjson_get_type(void *val) { + uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; + return (yyjson_type)(tag & YYJSON_TYPE_MASK); +} + +yyjson_api_inline yyjson_subtype unsafe_yyjson_get_subtype(void *val) { + uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; + return (yyjson_subtype)(tag & YYJSON_SUBTYPE_MASK); +} + +yyjson_api_inline uint8_t unsafe_yyjson_get_tag(void *val) { + uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; + return (uint8_t)(tag & YYJSON_TAG_MASK); +} + +yyjson_api_inline bool unsafe_yyjson_is_raw(void *val) { + return unsafe_yyjson_get_type(val) == YYJSON_TYPE_RAW; +} + +yyjson_api_inline bool unsafe_yyjson_is_null(void *val) { + return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NULL; +} + +yyjson_api_inline bool unsafe_yyjson_is_bool(void *val) { + return unsafe_yyjson_get_type(val) == YYJSON_TYPE_BOOL; +} + +yyjson_api_inline bool unsafe_yyjson_is_num(void *val) { + return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NUM; +} + +yyjson_api_inline bool unsafe_yyjson_is_str(void *val) { + return unsafe_yyjson_get_type(val) == YYJSON_TYPE_STR; +} + +yyjson_api_inline bool unsafe_yyjson_is_arr(void *val) { + return unsafe_yyjson_get_type(val) == YYJSON_TYPE_ARR; +} + +yyjson_api_inline bool unsafe_yyjson_is_obj(void *val) { + return unsafe_yyjson_get_type(val) == YYJSON_TYPE_OBJ; +} + +yyjson_api_inline bool unsafe_yyjson_is_ctn(void *val) { + uint8_t mask = YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ; + return (unsafe_yyjson_get_tag(val) & mask) == mask; +} + +yyjson_api_inline bool unsafe_yyjson_is_uint(void *val) { + const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; + return unsafe_yyjson_get_tag(val) == patt; +} + +yyjson_api_inline bool unsafe_yyjson_is_sint(void *val) { + const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; + return unsafe_yyjson_get_tag(val) == patt; +} + +yyjson_api_inline bool unsafe_yyjson_is_int(void *val) { + const uint8_t mask = YYJSON_TAG_MASK & (~YYJSON_SUBTYPE_SINT); + const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; + return (unsafe_yyjson_get_tag(val) & mask) == patt; +} + +yyjson_api_inline bool unsafe_yyjson_is_real(void *val) { + const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; + return unsafe_yyjson_get_tag(val) == patt; +} + +yyjson_api_inline bool unsafe_yyjson_is_true(void *val) { + const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; + return unsafe_yyjson_get_tag(val) == patt; +} + +yyjson_api_inline bool unsafe_yyjson_is_false(void *val) { + const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; + return unsafe_yyjson_get_tag(val) == patt; +} + +yyjson_api_inline bool unsafe_yyjson_arr_is_flat(yyjson_val *val) { + size_t ofs = val->uni.ofs; + size_t len = (size_t)(val->tag >> YYJSON_TAG_BIT); + return len * sizeof(yyjson_val) + sizeof(yyjson_val) == ofs; +} + +yyjson_api_inline const char *unsafe_yyjson_get_raw(void *val) { + return ((yyjson_val *)val)->uni.str; +} + +yyjson_api_inline bool unsafe_yyjson_get_bool(void *val) { + uint8_t tag = unsafe_yyjson_get_tag(val); + return (bool)((tag & YYJSON_SUBTYPE_MASK) >> YYJSON_TYPE_BIT); +} + +yyjson_api_inline uint64_t unsafe_yyjson_get_uint(void *val) { + return ((yyjson_val *)val)->uni.u64; +} + +yyjson_api_inline int64_t unsafe_yyjson_get_sint(void *val) { + return ((yyjson_val *)val)->uni.i64; +} + +yyjson_api_inline int unsafe_yyjson_get_int(void *val) { + return (int)((yyjson_val *)val)->uni.i64; +} + +yyjson_api_inline double unsafe_yyjson_get_real(void *val) { + return ((yyjson_val *)val)->uni.f64; +} + +yyjson_api_inline double unsafe_yyjson_get_num(void *val) { + uint8_t tag = unsafe_yyjson_get_tag(val); + if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL)) { + return ((yyjson_val *)val)->uni.f64; + } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT)) { + return (double)((yyjson_val *)val)->uni.i64; + } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT)) { + return unsafe_yyjson_u64_to_f64(((yyjson_val *)val)->uni.u64); + } + return 0.0; +} + +yyjson_api_inline const char *unsafe_yyjson_get_str(void *val) { + return ((yyjson_val *)val)->uni.str; +} + +yyjson_api_inline size_t unsafe_yyjson_get_len(void *val) { + return (size_t)(((yyjson_val *)val)->tag >> YYJSON_TAG_BIT); +} + +yyjson_api_inline yyjson_val *unsafe_yyjson_get_first(yyjson_val *ctn) { + return ctn + 1; +} + +yyjson_api_inline yyjson_val *unsafe_yyjson_get_next(yyjson_val *val) { + bool is_ctn = unsafe_yyjson_is_ctn(val); + size_t ctn_ofs = val->uni.ofs; + size_t ofs = (is_ctn ? ctn_ofs : sizeof(yyjson_val)); + return (yyjson_val *)(void *)((uint8_t *)val + ofs); +} + +yyjson_api_inline bool unsafe_yyjson_equals_strn(void *val, const char *str, + size_t len) { + return unsafe_yyjson_get_len(val) == len && + memcmp(((yyjson_val *)val)->uni.str, str, len) == 0; +} + +yyjson_api_inline bool unsafe_yyjson_equals_str(void *val, const char *str) { + return unsafe_yyjson_equals_strn(val, str, strlen(str)); +} + +yyjson_api_inline void unsafe_yyjson_set_type(void *val, yyjson_type type, + yyjson_subtype subtype) { + uint8_t tag = (type | subtype); + uint64_t new_tag = ((yyjson_val *)val)->tag; + new_tag = (new_tag & (~(uint64_t)YYJSON_TAG_MASK)) | (uint64_t)tag; + ((yyjson_val *)val)->tag = new_tag; +} + +yyjson_api_inline void unsafe_yyjson_set_len(void *val, size_t len) { + uint64_t tag = ((yyjson_val *)val)->tag & YYJSON_TAG_MASK; + tag |= (uint64_t)len << YYJSON_TAG_BIT; + ((yyjson_val *)val)->tag = tag; +} + +yyjson_api_inline void unsafe_yyjson_set_tag(void *val, yyjson_type type, + yyjson_subtype subtype, + size_t len) { + uint64_t tag = (uint64_t)len << YYJSON_TAG_BIT; + tag |= (type | subtype); + ((yyjson_val *)val)->tag = tag; +} + +yyjson_api_inline void unsafe_yyjson_inc_len(void *val) { + uint64_t tag = ((yyjson_val *)val)->tag; + tag += (uint64_t)(1 << YYJSON_TAG_BIT); + ((yyjson_val *)val)->tag = tag; +} + +yyjson_api_inline void unsafe_yyjson_set_raw(void *val, const char *raw, + size_t len) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_RAW, YYJSON_SUBTYPE_NONE, len); + ((yyjson_val *)val)->uni.str = raw; +} + +yyjson_api_inline void unsafe_yyjson_set_null(void *val) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_NULL, YYJSON_SUBTYPE_NONE, 0); +} + +yyjson_api_inline void unsafe_yyjson_set_bool(void *val, bool num) { + yyjson_subtype subtype = num ? YYJSON_SUBTYPE_TRUE : YYJSON_SUBTYPE_FALSE; + unsafe_yyjson_set_tag(val, YYJSON_TYPE_BOOL, subtype, 0); +} + +yyjson_api_inline void unsafe_yyjson_set_uint(void *val, uint64_t num) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_UINT, 0); + ((yyjson_val *)val)->uni.u64 = num; +} + +yyjson_api_inline void unsafe_yyjson_set_sint(void *val, int64_t num) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_SINT, 0); + ((yyjson_val *)val)->uni.i64 = num; +} + +yyjson_api_inline void unsafe_yyjson_set_fp_to_fixed(void *val, int prec) { + ((yyjson_val *)val)->tag &= ~((uint64_t)YYJSON_WRITE_FP_TO_FIXED(15) << 32); + ((yyjson_val *)val)->tag |= (uint64_t)YYJSON_WRITE_FP_TO_FIXED(prec) << 32; +} + +yyjson_api_inline void unsafe_yyjson_set_fp_to_float(void *val, bool flt) { + uint64_t flag = (uint64_t)YYJSON_WRITE_FP_TO_FLOAT << 32; + if (flt) ((yyjson_val *)val)->tag |= flag; + else ((yyjson_val *)val)->tag &= ~flag; +} + +yyjson_api_inline void unsafe_yyjson_set_float(void *val, float num) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_REAL, 0); + ((yyjson_val *)val)->tag |= (uint64_t)YYJSON_WRITE_FP_TO_FLOAT << 32; + ((yyjson_val *)val)->uni.f64 = (double)num; +} + +yyjson_api_inline void unsafe_yyjson_set_double(void *val, double num) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_REAL, 0); + ((yyjson_val *)val)->uni.f64 = num; +} + +yyjson_api_inline void unsafe_yyjson_set_real(void *val, double num) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_REAL, 0); + ((yyjson_val *)val)->uni.f64 = num; +} + +yyjson_api_inline void unsafe_yyjson_set_str_noesc(void *val, bool noesc) { + ((yyjson_val *)val)->tag &= ~(uint64_t)YYJSON_SUBTYPE_MASK; + if (noesc) ((yyjson_val *)val)->tag |= (uint64_t)YYJSON_SUBTYPE_NOESC; +} + +yyjson_api_inline void unsafe_yyjson_set_strn(void *val, const char *str, + size_t len) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_STR, YYJSON_SUBTYPE_NONE, len); + ((yyjson_val *)val)->uni.str = str; +} + +yyjson_api_inline void unsafe_yyjson_set_str(void *val, const char *str) { + size_t len = strlen(str); + bool noesc = unsafe_yyjson_is_str_noesc(str, len); + yyjson_subtype subtype = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; + unsafe_yyjson_set_tag(val, YYJSON_TYPE_STR, subtype, len); + ((yyjson_val *)val)->uni.str = str; +} + +yyjson_api_inline void unsafe_yyjson_set_arr(void *val, size_t size) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_ARR, YYJSON_SUBTYPE_NONE, size); +} + +yyjson_api_inline void unsafe_yyjson_set_obj(void *val, size_t size) { + unsafe_yyjson_set_tag(val, YYJSON_TYPE_OBJ, YYJSON_SUBTYPE_NONE, size); +} + + + +/*============================================================================== + * MARK: - JSON Document API (Implementation) + *============================================================================*/ + +yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc) { + return doc ? doc->root : NULL; +} + +yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc) { + return doc ? doc->dat_read : 0; +} + +yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc) { + return doc ? doc->val_read : 0; +} + +yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc) { + if (doc) { + yyjson_alc alc = doc->alc; + memset(&doc->alc, 0, sizeof(alc)); + if (doc->str_pool) alc.free(alc.ctx, doc->str_pool); + alc.free(alc.ctx, doc); + } +} + + + +/*============================================================================== + * MARK: - JSON Value Type API (Implementation) + *============================================================================*/ + +yyjson_api_inline bool yyjson_is_raw(yyjson_val *val) { + return val ? unsafe_yyjson_is_raw(val) : false; +} + +yyjson_api_inline bool yyjson_is_null(yyjson_val *val) { + return val ? unsafe_yyjson_is_null(val) : false; +} + +yyjson_api_inline bool yyjson_is_true(yyjson_val *val) { + return val ? unsafe_yyjson_is_true(val) : false; +} + +yyjson_api_inline bool yyjson_is_false(yyjson_val *val) { + return val ? unsafe_yyjson_is_false(val) : false; +} + +yyjson_api_inline bool yyjson_is_bool(yyjson_val *val) { + return val ? unsafe_yyjson_is_bool(val) : false; +} + +yyjson_api_inline bool yyjson_is_uint(yyjson_val *val) { + return val ? unsafe_yyjson_is_uint(val) : false; +} + +yyjson_api_inline bool yyjson_is_sint(yyjson_val *val) { + return val ? unsafe_yyjson_is_sint(val) : false; +} + +yyjson_api_inline bool yyjson_is_int(yyjson_val *val) { + return val ? unsafe_yyjson_is_int(val) : false; +} + +yyjson_api_inline bool yyjson_is_real(yyjson_val *val) { + return val ? unsafe_yyjson_is_real(val) : false; +} + +yyjson_api_inline bool yyjson_is_num(yyjson_val *val) { + return val ? unsafe_yyjson_is_num(val) : false; +} + +yyjson_api_inline bool yyjson_is_str(yyjson_val *val) { + return val ? unsafe_yyjson_is_str(val) : false; +} + +yyjson_api_inline bool yyjson_is_arr(yyjson_val *val) { + return val ? unsafe_yyjson_is_arr(val) : false; +} + +yyjson_api_inline bool yyjson_is_obj(yyjson_val *val) { + return val ? unsafe_yyjson_is_obj(val) : false; +} + +yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val) { + return val ? unsafe_yyjson_is_ctn(val) : false; +} + + + +/*============================================================================== + * MARK: - JSON Value Content API (Implementation) + *============================================================================*/ + +yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val) { + return val ? unsafe_yyjson_get_type(val) : YYJSON_TYPE_NONE; +} + +yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val) { + return val ? unsafe_yyjson_get_subtype(val) : YYJSON_SUBTYPE_NONE; +} + +yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val) { + return val ? unsafe_yyjson_get_tag(val) : 0; +} + +yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val) { + switch (yyjson_get_tag(val)) { + case YYJSON_TYPE_RAW | YYJSON_SUBTYPE_NONE: return "raw"; + case YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE: return "null"; + case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NONE: return "string"; + case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC: return "string"; + case YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE: return "array"; + case YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE: return "object"; + case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE: return "true"; + case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE: return "false"; + case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT: return "uint"; + case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT: return "sint"; + case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL: return "real"; + default: return "unknown"; + } +} + +yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val) { + return yyjson_is_raw(val) ? unsafe_yyjson_get_raw(val) : NULL; +} + +yyjson_api_inline bool yyjson_get_bool(yyjson_val *val) { + return yyjson_is_bool(val) ? unsafe_yyjson_get_bool(val) : false; +} + +yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val) { + return yyjson_is_int(val) ? unsafe_yyjson_get_uint(val) : 0; +} + +yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val) { + return yyjson_is_int(val) ? unsafe_yyjson_get_sint(val) : 0; +} + +yyjson_api_inline int yyjson_get_int(yyjson_val *val) { + return yyjson_is_int(val) ? unsafe_yyjson_get_int(val) : 0; +} + +yyjson_api_inline double yyjson_get_real(yyjson_val *val) { + return yyjson_is_real(val) ? unsafe_yyjson_get_real(val) : 0.0; +} + +yyjson_api_inline double yyjson_get_num(yyjson_val *val) { + return val ? unsafe_yyjson_get_num(val) : 0.0; +} + +yyjson_api_inline const char *yyjson_get_str(yyjson_val *val) { + return yyjson_is_str(val) ? unsafe_yyjson_get_str(val) : NULL; +} + +yyjson_api_inline size_t yyjson_get_len(yyjson_val *val) { + return val ? unsafe_yyjson_get_len(val) : 0; +} + +yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str) { + if (yyjson_likely(val && str)) { + return unsafe_yyjson_is_str(val) && + unsafe_yyjson_equals_str(val, str); + } + return false; +} + +yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, + size_t len) { + if (yyjson_likely(val && str)) { + return unsafe_yyjson_is_str(val) && + unsafe_yyjson_equals_strn(val, str, len); + } + return false; +} + +yyjson_api bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); + +yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) { + if (yyjson_unlikely(!lhs || !rhs)) return false; + return unsafe_yyjson_equals(lhs, rhs); +} + +yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, + const char *raw, size_t len) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_raw(val, raw, len); + return true; +} + +yyjson_api_inline bool yyjson_set_null(yyjson_val *val) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_null(val); + return true; +} + +yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_bool(val, num); + return true; +} + +yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_uint(val, num); + return true; +} + +yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_sint(val, num); + return true; +} + +yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_sint(val, (int64_t)num); + return true; +} + +yyjson_api_inline bool yyjson_set_float(yyjson_val *val, float num) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_float(val, num); + return true; +} + +yyjson_api_inline bool yyjson_set_double(yyjson_val *val, double num) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_double(val, num); + return true; +} + +yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + unsafe_yyjson_set_real(val, num); + return true; +} + +yyjson_api_inline bool yyjson_set_fp_to_fixed(yyjson_val *val, int prec) { + if (yyjson_unlikely(!yyjson_is_real(val))) return false; + unsafe_yyjson_set_fp_to_fixed(val, prec); + return true; +} + +yyjson_api_inline bool yyjson_set_fp_to_float(yyjson_val *val, bool flt) { + if (yyjson_unlikely(!yyjson_is_real(val))) return false; + unsafe_yyjson_set_fp_to_float(val, flt); + return true; +} + +yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + if (yyjson_unlikely(!str)) return false; + unsafe_yyjson_set_str(val, str); + return true; +} + +yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, + const char *str, size_t len) { + if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; + if (yyjson_unlikely(!str)) return false; + unsafe_yyjson_set_strn(val, str, len); + return true; +} + +yyjson_api_inline bool yyjson_set_str_noesc(yyjson_val *val, bool noesc) { + if (yyjson_unlikely(!yyjson_is_str(val))) return false; + unsafe_yyjson_set_str_noesc(val, noesc); + return true; +} + + + +/*============================================================================== + * MARK: - JSON Array API (Implementation) + *============================================================================*/ + +yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr) { + return yyjson_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0; +} + +yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx) { + if (yyjson_likely(yyjson_is_arr(arr))) { + if (yyjson_likely(unsafe_yyjson_get_len(arr) > idx)) { + yyjson_val *val = unsafe_yyjson_get_first(arr); + if (unsafe_yyjson_arr_is_flat(arr)) { + return val + idx; + } else { + while (idx-- > 0) val = unsafe_yyjson_get_next(val); + return val; + } + } + } + return NULL; +} + +yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr) { + if (yyjson_likely(yyjson_is_arr(arr))) { + if (yyjson_likely(unsafe_yyjson_get_len(arr) > 0)) { + return unsafe_yyjson_get_first(arr); + } + } + return NULL; +} + +yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr) { + if (yyjson_likely(yyjson_is_arr(arr))) { + size_t len = unsafe_yyjson_get_len(arr); + if (yyjson_likely(len > 0)) { + yyjson_val *val = unsafe_yyjson_get_first(arr); + if (unsafe_yyjson_arr_is_flat(arr)) { + return val + (len - 1); + } else { + while (len-- > 1) val = unsafe_yyjson_get_next(val); + return val; + } + } + } + return NULL; +} + + + +/*============================================================================== + * MARK: - JSON Array Iterator API (Implementation) + *============================================================================*/ + +yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, + yyjson_arr_iter *iter) { + if (yyjson_likely(yyjson_is_arr(arr) && iter)) { + iter->idx = 0; + iter->max = unsafe_yyjson_get_len(arr); + iter->cur = unsafe_yyjson_get_first(arr); + return true; + } + if (iter) memset(iter, 0, sizeof(yyjson_arr_iter)); + return false; +} + +yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr) { + yyjson_arr_iter iter; + yyjson_arr_iter_init(arr, &iter); + return iter; +} + +yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter) { + return iter ? iter->idx < iter->max : false; +} + +yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter) { + yyjson_val *val; + if (iter && iter->idx < iter->max) { + val = iter->cur; + iter->cur = unsafe_yyjson_get_next(val); + iter->idx++; + return val; + } + return NULL; +} + + + +/*============================================================================== + * MARK: - JSON Object API (Implementation) + *============================================================================*/ + +yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj) { + return yyjson_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0; +} + +yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, + const char *key) { + return yyjson_obj_getn(obj, key, key ? strlen(key) : 0); +} + +yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, + const char *_key, + size_t key_len) { + if (yyjson_likely(yyjson_is_obj(obj) && _key)) { + size_t len = unsafe_yyjson_get_len(obj); + yyjson_val *key = unsafe_yyjson_get_first(obj); + while (len-- > 0) { + if (unsafe_yyjson_equals_strn(key, _key, key_len)) return key + 1; + key = unsafe_yyjson_get_next(key + 1); + } + } + return NULL; +} + + + +/*============================================================================== + * MARK: - JSON Object Iterator API (Implementation) + *============================================================================*/ + +yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, + yyjson_obj_iter *iter) { + if (yyjson_likely(yyjson_is_obj(obj) && iter)) { + iter->idx = 0; + iter->max = unsafe_yyjson_get_len(obj); + iter->cur = unsafe_yyjson_get_first(obj); + iter->obj = obj; + return true; + } + if (iter) memset(iter, 0, sizeof(yyjson_obj_iter)); + return false; +} + +yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(yyjson_val *obj) { + yyjson_obj_iter iter; + yyjson_obj_iter_init(obj, &iter); + return iter; +} + +yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter) { + return iter ? iter->idx < iter->max : false; +} + +yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter) { + if (iter && iter->idx < iter->max) { + yyjson_val *key = iter->cur; + iter->idx++; + iter->cur = unsafe_yyjson_get_next(key + 1); + return key; + } + return NULL; +} + +yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key) { + return key ? key + 1 : NULL; +} + +yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, + const char *key) { + return yyjson_obj_iter_getn(iter, key, key ? strlen(key) : 0); +} + +yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, + const char *key, + size_t key_len) { + if (iter && key) { + size_t idx = iter->idx; + size_t max = iter->max; + yyjson_val *cur = iter->cur; + if (yyjson_unlikely(idx == max)) { + idx = 0; + cur = unsafe_yyjson_get_first(iter->obj); + } + while (idx++ < max) { + yyjson_val *next = unsafe_yyjson_get_next(cur + 1); + if (unsafe_yyjson_equals_strn(cur, key, key_len)) { + iter->idx = idx; + iter->cur = next; + return cur + 1; + } + cur = next; + if (idx == iter->max && iter->idx < iter->max) { + idx = 0; + max = iter->idx; + cur = unsafe_yyjson_get_first(iter->obj); + } + } + } + return NULL; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Structure (Implementation) + *============================================================================*/ + +/** + Mutable JSON value, 24 bytes. + The 'tag' and 'uni' field is same as immutable value. + The 'next' field links all elements inside the container to be a cycle. + */ +struct yyjson_mut_val { + uint64_t tag; /**< type, subtype and length */ + yyjson_val_uni uni; /**< payload */ + yyjson_mut_val *next; /**< the next value in circular linked list */ +}; + +/** + A memory chunk in string memory pool. + */ +typedef struct yyjson_str_chunk { + struct yyjson_str_chunk *next; /* next chunk linked list */ + size_t chunk_size; /* chunk size in bytes */ + /* char str[]; flexible array member */ +} yyjson_str_chunk; + +/** + A memory pool to hold all strings in a mutable document. + */ +typedef struct yyjson_str_pool { + char *cur; /* cursor inside current chunk */ + char *end; /* the end of current chunk */ + size_t chunk_size; /* chunk size in bytes while creating new chunk */ + size_t chunk_size_max; /* maximum chunk size in bytes */ + yyjson_str_chunk *chunks; /* a linked list of chunks, nullable */ +} yyjson_str_pool; + +/** + A memory chunk in value memory pool. + `sizeof(yyjson_val_chunk)` should not larger than `sizeof(yyjson_mut_val)`. + */ +typedef struct yyjson_val_chunk { + struct yyjson_val_chunk *next; /* next chunk linked list */ + size_t chunk_size; /* chunk size in bytes */ + /* char pad[sizeof(yyjson_mut_val) - sizeof(yyjson_val_chunk)]; padding */ + /* yyjson_mut_val vals[]; flexible array member */ +} yyjson_val_chunk; + +/** + A memory pool to hold all values in a mutable document. + */ +typedef struct yyjson_val_pool { + yyjson_mut_val *cur; /* cursor inside current chunk */ + yyjson_mut_val *end; /* the end of current chunk */ + size_t chunk_size; /* chunk size in bytes while creating new chunk */ + size_t chunk_size_max; /* maximum chunk size in bytes */ + yyjson_val_chunk *chunks; /* a linked list of chunks, nullable */ +} yyjson_val_pool; + +struct yyjson_mut_doc { + yyjson_mut_val *root; /**< root value of the JSON document, nullable */ + yyjson_alc alc; /**< a valid allocator, nonnull */ + yyjson_str_pool str_pool; /**< string memory pool */ + yyjson_val_pool val_pool; /**< value memory pool */ +}; + +/* Ensures the capacity to at least equal to the specified byte length. */ +yyjson_api bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool, + const yyjson_alc *alc, + size_t len); + +/* Ensures the capacity to at least equal to the specified value count. */ +yyjson_api bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool, + const yyjson_alc *alc, + size_t count); + +/* Allocate memory for string. */ +yyjson_api_inline char *unsafe_yyjson_mut_str_alc(yyjson_mut_doc *doc, + size_t len) { + char *mem; + const yyjson_alc *alc = &doc->alc; + yyjson_str_pool *pool = &doc->str_pool; + if (yyjson_unlikely((size_t)(pool->end - pool->cur) <= len)) { + if (yyjson_unlikely(!unsafe_yyjson_str_pool_grow(pool, alc, len + 1))) { + return NULL; + } + } + mem = pool->cur; + pool->cur = mem + len + 1; + return mem; +} + +yyjson_api_inline char *unsafe_yyjson_mut_strncpy(yyjson_mut_doc *doc, + const char *str, size_t len) { + char *mem = unsafe_yyjson_mut_str_alc(doc, len); + if (yyjson_unlikely(!mem)) return NULL; + memcpy((void *)mem, (const void *)str, len); + mem[len] = '\0'; + return mem; +} + +yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_val(yyjson_mut_doc *doc, + size_t count) { + yyjson_mut_val *val; + yyjson_alc *alc = &doc->alc; + yyjson_val_pool *pool = &doc->val_pool; + if (yyjson_unlikely((size_t)(pool->end - pool->cur) < count)) { + if (yyjson_unlikely(!unsafe_yyjson_val_pool_grow(pool, alc, count))) { + return NULL; + } + } + val = pool->cur; + pool->cur += count; + return val; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Document API (Implementation) + *============================================================================*/ + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc) { + return doc ? doc->root : NULL; +} + +yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, + yyjson_mut_val *root) { + if (doc) doc->root = root; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Value Type API (Implementation) + *============================================================================*/ + +yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_raw(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_null(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_true(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_false(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_bool(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_uint(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_sint(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_int(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_real(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_num(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_str(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_arr(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_obj(val) : false; +} + +yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val) { + return val ? unsafe_yyjson_is_ctn(val) : false; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Value Content API (Implementation) + *============================================================================*/ + +yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val) { + return yyjson_get_type((yyjson_val *)val); +} + +yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val) { + return yyjson_get_subtype((yyjson_val *)val); +} + +yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val) { + return yyjson_get_tag((yyjson_val *)val); +} + +yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val) { + return yyjson_get_type_desc((yyjson_val *)val); +} + +yyjson_api_inline const char *yyjson_mut_get_raw(yyjson_mut_val *val) { + return yyjson_get_raw((yyjson_val *)val); +} + +yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val) { + return yyjson_get_bool((yyjson_val *)val); +} + +yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val) { + return yyjson_get_uint((yyjson_val *)val); +} + +yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val) { + return yyjson_get_sint((yyjson_val *)val); +} + +yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val) { + return yyjson_get_int((yyjson_val *)val); +} + +yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val) { + return yyjson_get_real((yyjson_val *)val); +} + +yyjson_api_inline double yyjson_mut_get_num(yyjson_mut_val *val) { + return yyjson_get_num((yyjson_val *)val); +} + +yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val) { + return yyjson_get_str((yyjson_val *)val); +} + +yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val) { + return yyjson_get_len((yyjson_val *)val); +} + +yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, + const char *str) { + return yyjson_equals_str((yyjson_val *)val, str); +} + +yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, + const char *str, size_t len) { + return yyjson_equals_strn((yyjson_val *)val, str, len); +} + +yyjson_api bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, + yyjson_mut_val *rhs); + +yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, + yyjson_mut_val *rhs) { + if (yyjson_unlikely(!lhs || !rhs)) return false; + return unsafe_yyjson_mut_equals(lhs, rhs); +} + +yyjson_api_inline bool yyjson_mut_set_raw(yyjson_mut_val *val, + const char *raw, size_t len) { + if (yyjson_unlikely(!val || !raw)) return false; + unsafe_yyjson_set_raw(val, raw, len); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val *val) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_null(val); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val *val, bool num) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_bool(val, num); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_uint(yyjson_mut_val *val, uint64_t num) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_uint(val, num); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_sint(val, num); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_sint(val, (int64_t)num); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_float(yyjson_mut_val *val, float num) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_float(val, num); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_double(yyjson_mut_val *val, double num) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_double(val, num); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val *val, double num) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_real(val, num); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_fp_to_fixed(yyjson_mut_val *val, + int prec) { + if (yyjson_unlikely(!yyjson_mut_is_real(val))) return false; + unsafe_yyjson_set_fp_to_fixed(val, prec); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_fp_to_float(yyjson_mut_val *val, + bool flt) { + if (yyjson_unlikely(!yyjson_mut_is_real(val))) return false; + unsafe_yyjson_set_fp_to_float(val, flt); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_str(yyjson_mut_val *val, + const char *str) { + if (yyjson_unlikely(!val || !str)) return false; + unsafe_yyjson_set_str(val, str); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, + const char *str, size_t len) { + if (yyjson_unlikely(!val || !str)) return false; + unsafe_yyjson_set_strn(val, str, len); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_str_noesc(yyjson_mut_val *val, + bool noesc) { + if (yyjson_unlikely(!yyjson_mut_is_str(val))) return false; + unsafe_yyjson_set_str_noesc(val, noesc); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val *val) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_arr(val, 0); + return true; +} + +yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val) { + if (yyjson_unlikely(!val)) return false; + unsafe_yyjson_set_obj(val, 0); + return true; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Value Creation API (Implementation) + *============================================================================*/ + +#define yyjson_mut_val_one(func) \ + if (yyjson_likely(doc)) { \ + yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); \ + if (yyjson_likely(val)) { \ + func \ + return val; \ + } \ + } \ + return NULL + +#define yyjson_mut_val_one_str(func) \ + if (yyjson_likely(doc && str)) { \ + yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); \ + if (yyjson_likely(val)) { \ + func \ + return val; \ + } \ + } \ + return NULL + +yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, + const char *str) { + yyjson_mut_val_one_str({ unsafe_yyjson_set_raw(val, str, strlen(str)); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_rawn(yyjson_mut_doc *doc, + const char *str, + size_t len) { + yyjson_mut_val_one_str({ unsafe_yyjson_set_raw(val, str, len); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_rawcpy(yyjson_mut_doc *doc, + const char *str) { + yyjson_mut_val_one_str({ + size_t len = strlen(str); + char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); + if (yyjson_unlikely(!new_str)) return NULL; + unsafe_yyjson_set_raw(val, new_str, len); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_rawncpy(yyjson_mut_doc *doc, + const char *str, + size_t len) { + yyjson_mut_val_one_str({ + char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); + if (yyjson_unlikely(!new_str)) return NULL; + unsafe_yyjson_set_raw(val, new_str, len); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc) { + yyjson_mut_val_one({ unsafe_yyjson_set_null(val); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc) { + yyjson_mut_val_one({ unsafe_yyjson_set_bool(val, true); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc) { + yyjson_mut_val_one({ unsafe_yyjson_set_bool(val, false); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc, + bool _val) { + yyjson_mut_val_one({ unsafe_yyjson_set_bool(val, _val); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc, + uint64_t num) { + yyjson_mut_val_one({ unsafe_yyjson_set_uint(val, num); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc, + int64_t num) { + yyjson_mut_val_one({ unsafe_yyjson_set_sint(val, num); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc, + int64_t num) { + yyjson_mut_val_one({ unsafe_yyjson_set_sint(val, num); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_float(yyjson_mut_doc *doc, + float num) { + yyjson_mut_val_one({ unsafe_yyjson_set_float(val, num); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_double(yyjson_mut_doc *doc, + double num) { + yyjson_mut_val_one({ unsafe_yyjson_set_double(val, num); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc, + double num) { + yyjson_mut_val_one({ unsafe_yyjson_set_real(val, num); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc, + const char *str) { + yyjson_mut_val_one_str({ unsafe_yyjson_set_str(val, str); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc, + const char *str, + size_t len) { + yyjson_mut_val_one_str({ unsafe_yyjson_set_strn(val, str, len); }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc, + const char *str) { + yyjson_mut_val_one_str({ + size_t len = strlen(str); + bool noesc = unsafe_yyjson_is_str_noesc(str, len); + yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; + char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); + if (yyjson_unlikely(!new_str)) return NULL; + unsafe_yyjson_set_tag(val, YYJSON_TYPE_STR, sub, len); + val->uni.str = new_str; + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, + const char *str, + size_t len) { + yyjson_mut_val_one_str({ + char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); + if (yyjson_unlikely(!new_str)) return NULL; + unsafe_yyjson_set_strn(val, new_str, len); + }); +} + +#undef yyjson_mut_val_one +#undef yyjson_mut_val_one_str + + + +/*============================================================================== + * MARK: - Mutable JSON Array API (Implementation) + *============================================================================*/ + +yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr) { + return yyjson_mut_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, + size_t idx) { + if (yyjson_likely(idx < yyjson_mut_arr_size(arr))) { + yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; + while (idx-- > 0) val = val->next; + return val->next; + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first( + yyjson_mut_val *arr) { + if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { + return ((yyjson_mut_val *)arr->uni.ptr)->next; + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last( + yyjson_mut_val *arr) { + if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { + return ((yyjson_mut_val *)arr->uni.ptr); + } + return NULL; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Array Iterator API (Implementation) + *============================================================================*/ + +yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, + yyjson_mut_arr_iter *iter) { + if (yyjson_likely(yyjson_mut_is_arr(arr) && iter)) { + iter->idx = 0; + iter->max = unsafe_yyjson_get_len(arr); + iter->cur = iter->max ? (yyjson_mut_val *)arr->uni.ptr : NULL; + iter->pre = NULL; + iter->arr = arr; + return true; + } + if (iter) memset(iter, 0, sizeof(yyjson_mut_arr_iter)); + return false; +} + +yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( + yyjson_mut_val *arr) { + yyjson_mut_arr_iter iter; + yyjson_mut_arr_iter_init(arr, &iter); + return iter; +} + +yyjson_api_inline bool yyjson_mut_arr_iter_has_next(yyjson_mut_arr_iter *iter) { + return iter ? iter->idx < iter->max : false; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next( + yyjson_mut_arr_iter *iter) { + if (iter && iter->idx < iter->max) { + yyjson_mut_val *val = iter->cur; + iter->pre = val; + iter->cur = val->next; + iter->idx++; + return iter->cur; + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( + yyjson_mut_arr_iter *iter) { + if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) { + yyjson_mut_val *prev = iter->pre; + yyjson_mut_val *cur = iter->cur; + yyjson_mut_val *next = cur->next; + if (yyjson_unlikely(iter->idx == iter->max)) iter->arr->uni.ptr = prev; + iter->idx--; + iter->max--; + unsafe_yyjson_set_len(iter->arr, iter->max); + prev->next = next; + iter->cur = prev; + return cur; + } + return NULL; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Array Creation API (Implementation) + *============================================================================*/ + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc) { + if (yyjson_likely(doc)) { + yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); + if (yyjson_likely(val)) { + val->tag = YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE; + return val; + } + } + return NULL; +} + +#define yyjson_mut_arr_with_func(func) \ + if (yyjson_likely(doc && ((0 < count && count < \ + (~(size_t)0) / sizeof(yyjson_mut_val) && vals) || count == 0))) { \ + yyjson_mut_val *arr = unsafe_yyjson_mut_val(doc, 1 + count); \ + if (yyjson_likely(arr)) { \ + arr->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; \ + if (count > 0) { \ + size_t i; \ + for (i = 0; i < count; i++) { \ + yyjson_mut_val *val = arr + i + 1; \ + func \ + val->next = val + 1; \ + } \ + arr[count].next = arr + 1; \ + arr->uni.ptr = arr + count; \ + } \ + return arr; \ + } \ + } \ + return NULL + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( + yyjson_mut_doc *doc, const bool *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_bool(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( + yyjson_mut_doc *doc, const int64_t *vals, size_t count) { + return yyjson_mut_arr_with_sint64(doc, vals, count); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( + yyjson_mut_doc *doc, const uint64_t *vals, size_t count) { + return yyjson_mut_arr_with_uint64(doc, vals, count); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( + yyjson_mut_doc *doc, const double *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_real(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( + yyjson_mut_doc *doc, const int8_t *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_sint(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( + yyjson_mut_doc *doc, const int16_t *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_sint(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( + yyjson_mut_doc *doc, const int32_t *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_sint(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( + yyjson_mut_doc *doc, const int64_t *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_sint(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( + yyjson_mut_doc *doc, const uint8_t *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_uint(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( + yyjson_mut_doc *doc, const uint16_t *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_uint(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( + yyjson_mut_doc *doc, const uint32_t *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_uint(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( + yyjson_mut_doc *doc, const uint64_t *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_uint(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( + yyjson_mut_doc *doc, const float *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_float(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( + yyjson_mut_doc *doc, const double *vals, size_t count) { + yyjson_mut_arr_with_func({ + unsafe_yyjson_set_double(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( + yyjson_mut_doc *doc, const char **vals, size_t count) { + yyjson_mut_arr_with_func({ + if (yyjson_unlikely(!vals[i])) return NULL; + unsafe_yyjson_set_str(val, vals[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( + yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) { + if (yyjson_unlikely(count > 0 && !lens)) return NULL; + yyjson_mut_arr_with_func({ + if (yyjson_unlikely(!vals[i])) return NULL; + unsafe_yyjson_set_strn(val, vals[i], lens[i]); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( + yyjson_mut_doc *doc, const char **vals, size_t count) { + size_t len; + const char *str, *new_str; + yyjson_mut_arr_with_func({ + str = vals[i]; + if (yyjson_unlikely(!str)) return NULL; + len = strlen(str); + new_str = unsafe_yyjson_mut_strncpy(doc, str, len); + if (yyjson_unlikely(!new_str)) return NULL; + unsafe_yyjson_set_strn(val, new_str, len); + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( + yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) { + size_t len; + const char *str, *new_str; + if (yyjson_unlikely(count > 0 && !lens)) return NULL; + yyjson_mut_arr_with_func({ + str = vals[i]; + if (yyjson_unlikely(!str)) return NULL; + len = lens[i]; + new_str = unsafe_yyjson_mut_strncpy(doc, str, len); + if (yyjson_unlikely(!new_str)) return NULL; + unsafe_yyjson_set_strn(val, new_str, len); + }); +} + +#undef yyjson_mut_arr_with_func + + + +/*============================================================================== + * MARK: - Mutable JSON Array Modification API (Implementation) + *============================================================================*/ + +yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, + yyjson_mut_val *val, size_t idx) { + if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { + size_t len = unsafe_yyjson_get_len(arr); + if (yyjson_likely(idx <= len)) { + unsafe_yyjson_set_len(arr, len + 1); + if (len == 0) { + val->next = val; + arr->uni.ptr = val; + } else { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + yyjson_mut_val *next = prev->next; + if (idx == len) { + prev->next = val; + val->next = next; + arr->uni.ptr = val; + } else { + while (idx-- > 0) { + prev = next; + next = next->next; + } + prev->next = val; + val->next = next; + } + } + return true; + } + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, + yyjson_mut_val *val) { + if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { + size_t len = unsafe_yyjson_get_len(arr); + unsafe_yyjson_set_len(arr, len + 1); + if (len == 0) { + val->next = val; + } else { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + yyjson_mut_val *next = prev->next; + prev->next = val; + val->next = next; + } + arr->uni.ptr = val; + return true; + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, + yyjson_mut_val *val) { + if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { + size_t len = unsafe_yyjson_get_len(arr); + unsafe_yyjson_set_len(arr, len + 1); + if (len == 0) { + val->next = val; + arr->uni.ptr = val; + } else { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + yyjson_mut_val *next = prev->next; + prev->next = val; + val->next = next; + } + return true; + } + return false; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr, + size_t idx, + yyjson_mut_val *val) { + if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { + size_t len = unsafe_yyjson_get_len(arr); + if (yyjson_likely(idx < len)) { + if (yyjson_likely(len > 1)) { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + yyjson_mut_val *next = prev->next; + while (idx-- > 0) { + prev = next; + next = next->next; + } + prev->next = val; + val->next = next->next; + if ((void *)next == arr->uni.ptr) arr->uni.ptr = val; + return next; + } else { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + val->next = val; + arr->uni.ptr = val; + return prev; + } + } + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, + size_t idx) { + if (yyjson_likely(yyjson_mut_is_arr(arr))) { + size_t len = unsafe_yyjson_get_len(arr); + if (yyjson_likely(idx < len)) { + unsafe_yyjson_set_len(arr, len - 1); + if (yyjson_likely(len > 1)) { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + yyjson_mut_val *next = prev->next; + while (idx-- > 0) { + prev = next; + next = next->next; + } + prev->next = next->next; + if ((void *)next == arr->uni.ptr) arr->uni.ptr = prev; + return next; + } else { + return ((yyjson_mut_val *)arr->uni.ptr); + } + } + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first( + yyjson_mut_val *arr) { + if (yyjson_likely(yyjson_mut_is_arr(arr))) { + size_t len = unsafe_yyjson_get_len(arr); + if (len > 1) { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + yyjson_mut_val *next = prev->next; + prev->next = next->next; + unsafe_yyjson_set_len(arr, len - 1); + return next; + } else if (len == 1) { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + unsafe_yyjson_set_len(arr, 0); + return prev; + } + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( + yyjson_mut_val *arr) { + if (yyjson_likely(yyjson_mut_is_arr(arr))) { + size_t len = unsafe_yyjson_get_len(arr); + if (yyjson_likely(len > 1)) { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + yyjson_mut_val *next = prev->next; + unsafe_yyjson_set_len(arr, len - 1); + while (--len > 0) prev = prev->next; + prev->next = next; + next = (yyjson_mut_val *)arr->uni.ptr; + arr->uni.ptr = prev; + return next; + } else if (len == 1) { + yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); + unsafe_yyjson_set_len(arr, 0); + return prev; + } + } + return NULL; +} + +yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, + size_t _idx, size_t _len) { + if (yyjson_likely(yyjson_mut_is_arr(arr))) { + yyjson_mut_val *prev, *next; + bool tail_removed; + size_t len = unsafe_yyjson_get_len(arr); + if (yyjson_unlikely(_idx + _len > len)) return false; + if (yyjson_unlikely(_len == 0)) return true; + unsafe_yyjson_set_len(arr, len - _len); + if (yyjson_unlikely(len == _len)) return true; + tail_removed = (_idx + _len == len); + prev = ((yyjson_mut_val *)arr->uni.ptr); + while (_idx-- > 0) prev = prev->next; + next = prev->next; + while (_len-- > 0) next = next->next; + prev->next = next; + if (yyjson_unlikely(tail_removed)) arr->uni.ptr = prev; + return true; + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr) { + if (yyjson_likely(yyjson_mut_is_arr(arr))) { + unsafe_yyjson_set_len(arr, 0); + return true; + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, + size_t idx) { + if (yyjson_likely(yyjson_mut_is_arr(arr) && + unsafe_yyjson_get_len(arr) > idx)) { + yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; + while (idx-- > 0) val = val->next; + arr->uni.ptr = (void *)val; + return true; + } + return false; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Array Modification Convenience API (Implementation) + *============================================================================*/ + +yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, + yyjson_mut_val *val) { + return yyjson_mut_arr_append(arr, val); +} + +yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, + yyjson_mut_val *arr) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_null(doc); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, + yyjson_mut_val *arr) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_true(doc); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, + yyjson_mut_val *arr) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_false(doc); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + bool _val) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_bool(doc, _val); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + uint64_t num) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_uint(doc, num); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + int64_t num) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_sint(doc, num); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + int64_t num) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_sint(doc, num); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_float(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + float num) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_float(doc, num); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_double(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + double num) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_double(doc, num); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + double num) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_real(doc, num); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + const char *str) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_str(doc, str); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + const char *str, size_t len) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_strn(doc, str, len); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + const char *str) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_strcpy(doc, str); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, + yyjson_mut_val *arr, + const char *str, size_t len) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_strncpy(doc, str, len); + return yyjson_mut_arr_append(arr, val); + } + return false; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, + yyjson_mut_val *arr) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_arr(doc); + return yyjson_mut_arr_append(arr, val) ? val : NULL; + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, + yyjson_mut_val *arr) { + if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { + yyjson_mut_val *val = yyjson_mut_obj(doc); + return yyjson_mut_arr_append(arr, val) ? val : NULL; + } + return NULL; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Object API (Implementation) + *============================================================================*/ + +yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj) { + return yyjson_mut_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj, + const char *key) { + return yyjson_mut_obj_getn(obj, key, key ? strlen(key) : 0); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, + const char *_key, + size_t key_len) { + size_t len = yyjson_mut_obj_size(obj); + if (yyjson_likely(len && _key)) { + yyjson_mut_val *key = ((yyjson_mut_val *)obj->uni.ptr)->next->next; + while (len-- > 0) { + if (unsafe_yyjson_equals_strn(key, _key, key_len)) return key->next; + key = key->next->next; + } + } + return NULL; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Object Iterator API (Implementation) + *============================================================================*/ + +yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, + yyjson_mut_obj_iter *iter) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && iter)) { + iter->idx = 0; + iter->max = unsafe_yyjson_get_len(obj); + iter->cur = iter->max ? (yyjson_mut_val *)obj->uni.ptr : NULL; + iter->pre = NULL; + iter->obj = obj; + return true; + } + if (iter) memset(iter, 0, sizeof(yyjson_mut_obj_iter)); + return false; +} + +yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( + yyjson_mut_val *obj) { + yyjson_mut_obj_iter iter; + yyjson_mut_obj_iter_init(obj, &iter); + return iter; +} + +yyjson_api_inline bool yyjson_mut_obj_iter_has_next(yyjson_mut_obj_iter *iter) { + return iter ? iter->idx < iter->max : false; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next( + yyjson_mut_obj_iter *iter) { + if (iter && iter->idx < iter->max) { + yyjson_mut_val *key = iter->cur; + iter->pre = key; + iter->cur = key->next->next; + iter->idx++; + return iter->cur; + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val( + yyjson_mut_val *key) { + return key ? key->next : NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove( + yyjson_mut_obj_iter *iter) { + if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) { + yyjson_mut_val *prev = iter->pre; + yyjson_mut_val *cur = iter->cur; + yyjson_mut_val *next = cur->next->next; + if (yyjson_unlikely(iter->idx == iter->max)) iter->obj->uni.ptr = prev; + iter->idx--; + iter->max--; + unsafe_yyjson_set_len(iter->obj, iter->max); + prev->next->next = next; + iter->cur = prev; + return cur->next; + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( + yyjson_mut_obj_iter *iter, const char *key) { + return yyjson_mut_obj_iter_getn(iter, key, key ? strlen(key) : 0); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( + yyjson_mut_obj_iter *iter, const char *key, size_t key_len) { + if (iter && key) { + size_t idx = 0; + size_t max = iter->max; + yyjson_mut_val *pre, *cur = iter->cur; + while (idx++ < max) { + pre = cur; + cur = cur->next->next; + if (unsafe_yyjson_equals_strn(cur, key, key_len)) { + iter->idx += idx; + if (iter->idx > max) iter->idx -= max + 1; + iter->pre = pre; + iter->cur = cur; + return cur->next; + } + } + } + return NULL; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Object Creation API (Implementation) + *============================================================================*/ + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc) { + if (yyjson_likely(doc)) { + yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); + if (yyjson_likely(val)) { + val->tag = YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE; + return val; + } + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, + const char **keys, + const char **vals, + size_t count) { + if (yyjson_likely(doc && ((count > 0 && keys && vals) || (count == 0)))) { + yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2); + if (yyjson_likely(obj)) { + obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ; + if (count > 0) { + size_t i; + for (i = 0; i < count; i++) { + yyjson_mut_val *key = obj + (i * 2 + 1); + yyjson_mut_val *val = obj + (i * 2 + 2); + uint64_t key_len = (uint64_t)strlen(keys[i]); + uint64_t val_len = (uint64_t)strlen(vals[i]); + key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + key->uni.str = keys[i]; + val->uni.str = vals[i]; + key->next = val; + val->next = val + 1; + } + obj[count * 2].next = obj + 1; + obj->uni.ptr = obj + (count * 2 - 1); + } + return obj; + } + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, + const char **pairs, + size_t count) { + if (yyjson_likely(doc && ((count > 0 && pairs) || (count == 0)))) { + yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2); + if (yyjson_likely(obj)) { + obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ; + if (count > 0) { + size_t i; + for (i = 0; i < count; i++) { + yyjson_mut_val *key = obj + (i * 2 + 1); + yyjson_mut_val *val = obj + (i * 2 + 2); + const char *key_str = pairs[i * 2 + 0]; + const char *val_str = pairs[i * 2 + 1]; + uint64_t key_len = (uint64_t)strlen(key_str); + uint64_t val_len = (uint64_t)strlen(val_str); + key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + key->uni.str = key_str; + val->uni.str = val_str; + key->next = val; + val->next = val + 1; + } + obj[count * 2].next = obj + 1; + obj->uni.ptr = obj + (count * 2 - 1); + } + return obj; + } + } + return NULL; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Object Modification API (Implementation) + *============================================================================*/ + +yyjson_api_inline void unsafe_yyjson_mut_obj_add(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val, + size_t len) { + if (yyjson_likely(len)) { + yyjson_mut_val *prev_val = ((yyjson_mut_val *)obj->uni.ptr)->next; + yyjson_mut_val *next_key = prev_val->next; + prev_val->next = key; + val->next = next_key; + } else { + val->next = key; + } + key->next = val; + obj->uni.ptr = (void *)key; + unsafe_yyjson_set_len(obj, len + 1); +} + +yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_obj_remove( + yyjson_mut_val *obj, const char *key, size_t key_len) { + size_t obj_len = unsafe_yyjson_get_len(obj); + if (obj_len) { + yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr; + yyjson_mut_val *cur_key = pre_key->next->next; + yyjson_mut_val *removed_item = NULL; + size_t i; + for (i = 0; i < obj_len; i++) { + if (unsafe_yyjson_equals_strn(cur_key, key, key_len)) { + if (!removed_item) removed_item = cur_key->next; + cur_key = cur_key->next->next; + pre_key->next->next = cur_key; + if (i + 1 == obj_len) obj->uni.ptr = pre_key; + i--; + obj_len--; + } else { + pre_key = cur_key; + cur_key = cur_key->next->next; + } + } + unsafe_yyjson_set_len(obj, obj_len); + return removed_item; + } else { + return NULL; + } +} + +yyjson_api_inline bool unsafe_yyjson_mut_obj_replace(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val) { + size_t key_len = unsafe_yyjson_get_len(key); + size_t obj_len = unsafe_yyjson_get_len(obj); + if (obj_len) { + yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr; + yyjson_mut_val *cur_key = pre_key->next->next; + size_t i; + for (i = 0; i < obj_len; i++) { + if (unsafe_yyjson_equals_strn(cur_key, key->uni.str, key_len)) { + cur_key->next->tag = val->tag; + cur_key->next->uni.u64 = val->uni.u64; + return true; + } else { + cur_key = cur_key->next->next; + } + } + } + return false; +} + +yyjson_api_inline void unsafe_yyjson_mut_obj_rotate(yyjson_mut_val *obj, + size_t idx) { + yyjson_mut_val *key = (yyjson_mut_val *)obj->uni.ptr; + while (idx-- > 0) key = key->next->next; + obj->uni.ptr = (void *)key; +} + +yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && + yyjson_mut_is_str(key) && val)) { + unsafe_yyjson_mut_obj_add(obj, key, val, unsafe_yyjson_get_len(obj)); + return true; + } + return false; +} + +yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val) { + bool replaced = false; + size_t key_len; + yyjson_mut_obj_iter iter; + yyjson_mut_val *cur_key; + if (yyjson_unlikely(!yyjson_mut_is_obj(obj) || + !yyjson_mut_is_str(key))) return false; + key_len = unsafe_yyjson_get_len(key); + yyjson_mut_obj_iter_init(obj, &iter); + while ((cur_key = yyjson_mut_obj_iter_next(&iter)) != 0) { + if (unsafe_yyjson_equals_strn(cur_key, key->uni.str, key_len)) { + if (!replaced && val) { + replaced = true; + val->next = cur_key->next->next; + cur_key->next = val; + } else { + yyjson_mut_obj_iter_remove(&iter); + } + } + } + if (!replaced && val) unsafe_yyjson_mut_obj_add(obj, key, val, iter.max); + return true; +} + +yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val, + size_t idx) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && + yyjson_mut_is_str(key) && val)) { + size_t len = unsafe_yyjson_get_len(obj); + if (yyjson_likely(len >= idx)) { + if (len > idx) { + void *ptr = obj->uni.ptr; + unsafe_yyjson_mut_obj_rotate(obj, idx); + unsafe_yyjson_mut_obj_add(obj, key, val, len); + obj->uni.ptr = ptr; + } else { + unsafe_yyjson_mut_obj_add(obj, key, val, len); + } + return true; + } + } + return false; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj, + yyjson_mut_val *key) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && yyjson_mut_is_str(key))) { + return unsafe_yyjson_mut_obj_remove(obj, key->uni.str, + unsafe_yyjson_get_len(key)); + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_key( + yyjson_mut_val *obj, const char *key) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && key)) { + size_t key_len = strlen(key); + return unsafe_yyjson_mut_obj_remove(obj, key, key_len); + } + return NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( + yyjson_mut_val *obj, const char *key, size_t key_len) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && key)) { + return unsafe_yyjson_mut_obj_remove(obj, key, key_len); + } + return NULL; +} + +yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj) { + if (yyjson_likely(yyjson_mut_is_obj(obj))) { + unsafe_yyjson_set_len(obj, 0); + return true; + } + return false; +} + +yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, + yyjson_mut_val *key, + yyjson_mut_val *val) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && + yyjson_mut_is_str(key) && val)) { + return unsafe_yyjson_mut_obj_replace(obj, key, val); + } + return false; +} + +yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, + size_t idx) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && + unsafe_yyjson_get_len(obj) > idx)) { + unsafe_yyjson_mut_obj_rotate(obj, idx); + return true; + } + return false; +} + + + +/*============================================================================== + * MARK: - Mutable JSON Object Modification Convenience API (Implementation) + *============================================================================*/ + +#define yyjson_mut_obj_add_func(func) \ + if (yyjson_likely(doc && yyjson_mut_is_obj(obj) && _key)) { \ + yyjson_mut_val *key = unsafe_yyjson_mut_val(doc, 2); \ + if (yyjson_likely(key)) { \ + size_t len = unsafe_yyjson_get_len(obj); \ + yyjson_mut_val *val = key + 1; \ + size_t key_len = strlen(_key); \ + bool noesc = unsafe_yyjson_is_str_noesc(_key, key_len); \ + key->tag = YYJSON_TYPE_STR; \ + key->tag |= noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; \ + key->tag |= (uint64_t)strlen(_key) << YYJSON_TAG_BIT; \ + key->uni.str = _key; \ + func \ + unsafe_yyjson_mut_obj_add(obj, key, val, len); \ + return true; \ + } \ + } \ + return false + +yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_null(val); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_bool(val, true); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_bool(val, false); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + bool _val) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_bool(val, _val); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + uint64_t _val) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_uint(val, _val); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + int64_t _val) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_sint(val, _val); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + int64_t _val) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_sint(val, _val); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_float(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + float _val) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_float(val, _val); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_double(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + double _val) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_double(val, _val); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + double _val) { + yyjson_mut_obj_add_func({ unsafe_yyjson_set_real(val, _val); }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + const char *_val) { + if (yyjson_unlikely(!_val)) return false; + yyjson_mut_obj_add_func({ + size_t val_len = strlen(_val); + bool val_noesc = unsafe_yyjson_is_str_noesc(_val, val_len); + val->tag = ((uint64_t)strlen(_val) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + val->tag |= val_noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; + val->uni.str = _val; + }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + const char *_val, + size_t _len) { + if (yyjson_unlikely(!_val)) return false; + yyjson_mut_obj_add_func({ + val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + val->uni.str = _val; + }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + const char *_val) { + if (yyjson_unlikely(!_val)) return false; + yyjson_mut_obj_add_func({ + size_t _len = strlen(_val); + val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); + if (yyjson_unlikely(!val->uni.str)) return false; + val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + }); +} + +yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + const char *_val, + size_t _len) { + if (yyjson_unlikely(!_val)) return false; + yyjson_mut_obj_add_func({ + val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); + if (yyjson_unlikely(!val->uni.str)) return false; + val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_arr(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key) { + yyjson_mut_val *key = yyjson_mut_str(doc, _key); + yyjson_mut_val *val = yyjson_mut_arr(doc); + return yyjson_mut_obj_add(obj, key, val) ? val : NULL; +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_obj(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key) { + yyjson_mut_val *key = yyjson_mut_str(doc, _key); + yyjson_mut_val *val = yyjson_mut_obj(doc); + return yyjson_mut_obj_add(obj, key, val) ? val : NULL; +} + +yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *_key, + yyjson_mut_val *_val) { + if (yyjson_unlikely(!_val)) return false; + yyjson_mut_obj_add_func({ + val = _val; + }); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str(yyjson_mut_val *obj, + const char *key) { + return yyjson_mut_obj_remove_strn(obj, key, key ? strlen(key) : 0); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( + yyjson_mut_val *obj, const char *_key, size_t _len) { + if (yyjson_likely(yyjson_mut_is_obj(obj) && _key)) { + yyjson_mut_val *key; + yyjson_mut_obj_iter iter; + yyjson_mut_val *val_removed = NULL; + yyjson_mut_obj_iter_init(obj, &iter); + while ((key = yyjson_mut_obj_iter_next(&iter)) != NULL) { + if (unsafe_yyjson_equals_strn(key, _key, _len)) { + if (!val_removed) val_removed = key->next; + yyjson_mut_obj_iter_remove(&iter); + } + } + return val_removed; + } + return NULL; +} + +yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, + const char *new_key) { + if (!key || !new_key) return false; + return yyjson_mut_obj_rename_keyn(doc, obj, key, strlen(key), + new_key, strlen(new_key)); +} + +yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, + yyjson_mut_val *obj, + const char *key, + size_t len, + const char *new_key, + size_t new_len) { + char *cpy_key = NULL; + yyjson_mut_val *old_key; + yyjson_mut_obj_iter iter; + if (!doc || !obj || !key || !new_key) return false; + yyjson_mut_obj_iter_init(obj, &iter); + while ((old_key = yyjson_mut_obj_iter_next(&iter))) { + if (unsafe_yyjson_equals_strn((void *)old_key, key, len)) { + if (!cpy_key) { + cpy_key = unsafe_yyjson_mut_strncpy(doc, new_key, new_len); + if (!cpy_key) return false; + } + yyjson_mut_set_strn(old_key, cpy_key, new_len); + } + } + return cpy_key != NULL; +} + + + +#if !defined(YYJSON_DISABLE_UTILS) || !YYJSON_DISABLE_UTILS + +/*============================================================================== + * MARK: - JSON Pointer API (Implementation) + *============================================================================*/ + +#define yyjson_ptr_set_err(_code, _msg) do { \ + if (err) { \ + err->code = YYJSON_PTR_ERR_##_code; \ + err->msg = _msg; \ + err->pos = 0; \ + } \ +} while(false) + +/* require: val != NULL, *ptr == '/', len > 0 */ +yyjson_api yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, + const char *ptr, size_t len, + yyjson_ptr_err *err); + +/* require: val != NULL, *ptr == '/', len > 0 */ +yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_getx(yyjson_mut_val *val, + const char *ptr, + size_t len, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/* require: val/new_val/doc != NULL, *ptr == '/', len > 0 */ +yyjson_api bool unsafe_yyjson_mut_ptr_putx(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc, + bool create_parent, bool insert_new, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +/* require: val/err != NULL, *ptr == '/', len > 0 */ +yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex( + yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); + +/* require: val/err != NULL, *ptr == '/', len > 0 */ +yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_removex(yyjson_mut_val *val, + const char *ptr, + size_t len, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err); + +yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, + const char *ptr) { + if (yyjson_unlikely(!ptr)) return NULL; + return yyjson_doc_ptr_getn(doc, ptr, strlen(ptr)); +} + +yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, + const char *ptr, size_t len) { + return yyjson_doc_ptr_getx(doc, ptr, len, NULL); +} + +yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, + const char *ptr, size_t len, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (yyjson_unlikely(!doc || !ptr)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return NULL; + } + if (yyjson_unlikely(!doc->root)) { + yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); + return NULL; + } + if (yyjson_unlikely(len == 0)) { + return doc->root; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return NULL; + } + return unsafe_yyjson_ptr_getx(doc->root, ptr, len, err); +} + +yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, + const char *ptr) { + if (yyjson_unlikely(!ptr)) return NULL; + return yyjson_ptr_getn(val, ptr, strlen(ptr)); +} + +yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, + const char *ptr, size_t len) { + return yyjson_ptr_getx(val, ptr, len, NULL); +} + +yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, + const char *ptr, size_t len, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (yyjson_unlikely(!val || !ptr)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return NULL; + } + if (yyjson_unlikely(len == 0)) { + return val; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return NULL; + } + return unsafe_yyjson_ptr_getx(val, ptr, len, err); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, + const char *ptr) { + if (!ptr) return NULL; + return yyjson_mut_doc_ptr_getn(doc, ptr, strlen(ptr)); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, + const char *ptr, + size_t len) { + return yyjson_mut_doc_ptr_getx(doc, ptr, len, NULL, NULL); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, + const char *ptr, + size_t len, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!doc || !ptr)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return NULL; + } + if (yyjson_unlikely(!doc->root)) { + yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); + return NULL; + } + if (yyjson_unlikely(len == 0)) { + return doc->root; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return NULL; + } + return unsafe_yyjson_mut_ptr_getx(doc->root, ptr, len, ctx, err); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, + const char *ptr) { + if (!ptr) return NULL; + return yyjson_mut_ptr_getn(val, ptr, strlen(ptr)); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, + const char *ptr, + size_t len) { + return yyjson_mut_ptr_getx(val, ptr, len, NULL, NULL); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(yyjson_mut_val *val, + const char *ptr, + size_t len, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!val || !ptr)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return NULL; + } + if (yyjson_unlikely(len == 0)) { + return val; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return NULL; + } + return unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); +} + +yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, + const char *ptr, + yyjson_mut_val *new_val) { + if (yyjson_unlikely(!ptr)) return false; + return yyjson_mut_doc_ptr_addn(doc, ptr, strlen(ptr), new_val); +} + +yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, + const char *ptr, + size_t len, + yyjson_mut_val *new_val) { + return yyjson_mut_doc_ptr_addx(doc, ptr, len, new_val, true, NULL, NULL); +} + +yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + bool create_parent, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!doc || !ptr || !new_val)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return false; + } + if (yyjson_unlikely(len == 0)) { + if (doc->root) { + yyjson_ptr_set_err(SET_ROOT, "cannot set document's root"); + return false; + } else { + doc->root = new_val; + return true; + } + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return false; + } + if (yyjson_unlikely(!doc->root && !create_parent)) { + yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); + return false; + } + if (yyjson_unlikely(!doc->root)) { + yyjson_mut_val *root = yyjson_mut_obj(doc); + if (yyjson_unlikely(!root)) { + yyjson_ptr_set_err(MEMORY_ALLOCATION, "failed to create value"); + return false; + } + if (unsafe_yyjson_mut_ptr_putx(root, ptr, len, new_val, doc, + create_parent, true, ctx, err)) { + doc->root = root; + return true; + } + return false; + } + return unsafe_yyjson_mut_ptr_putx(doc->root, ptr, len, new_val, doc, + create_parent, true, ctx, err); +} + +yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, + const char *ptr, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc) { + if (yyjson_unlikely(!ptr)) return false; + return yyjson_mut_ptr_addn(val, ptr, strlen(ptr), new_val, doc); +} + +yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc) { + return yyjson_mut_ptr_addx(val, ptr, len, new_val, doc, true, NULL, NULL); +} + +yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc, + bool create_parent, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!val || !ptr || !new_val || !doc)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return false; + } + if (yyjson_unlikely(len == 0)) { + yyjson_ptr_set_err(SET_ROOT, "cannot set root"); + return false; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return false; + } + return unsafe_yyjson_mut_ptr_putx(val, ptr, len, new_val, + doc, create_parent, true, ctx, err); +} + +yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, + const char *ptr, + yyjson_mut_val *new_val) { + if (yyjson_unlikely(!ptr)) return false; + return yyjson_mut_doc_ptr_setn(doc, ptr, strlen(ptr), new_val); +} + +yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, + const char *ptr, size_t len, + yyjson_mut_val *new_val) { + return yyjson_mut_doc_ptr_setx(doc, ptr, len, new_val, true, NULL, NULL); +} + +yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + bool create_parent, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!doc || !ptr)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return false; + } + if (yyjson_unlikely(len == 0)) { + if (ctx) ctx->old = doc->root; + doc->root = new_val; + return true; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return false; + } + if (!new_val) { + if (!doc->root) { + yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved"); + return false; + } + return !!unsafe_yyjson_mut_ptr_removex(doc->root, ptr, len, ctx, err); + } + if (yyjson_unlikely(!doc->root && !create_parent)) { + yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); + return false; + } + if (yyjson_unlikely(!doc->root)) { + yyjson_mut_val *root = yyjson_mut_obj(doc); + if (yyjson_unlikely(!root)) { + yyjson_ptr_set_err(MEMORY_ALLOCATION, "failed to create value"); + return false; + } + if (unsafe_yyjson_mut_ptr_putx(root, ptr, len, new_val, doc, + create_parent, false, ctx, err)) { + doc->root = root; + return true; + } + return false; + } + return unsafe_yyjson_mut_ptr_putx(doc->root, ptr, len, new_val, doc, + create_parent, false, ctx, err); +} + +yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, + const char *ptr, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc) { + if (yyjson_unlikely(!ptr)) return false; + return yyjson_mut_ptr_setn(val, ptr, strlen(ptr), new_val, doc); +} + +yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc) { + return yyjson_mut_ptr_setx(val, ptr, len, new_val, doc, true, NULL, NULL); +} + +yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, + const char *ptr, size_t len, + yyjson_mut_val *new_val, + yyjson_mut_doc *doc, + bool create_parent, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!val || !ptr || !doc)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return false; + } + if (yyjson_unlikely(len == 0)) { + yyjson_ptr_set_err(SET_ROOT, "cannot set root"); + return false; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return false; + } + if (!new_val) { + return !!unsafe_yyjson_mut_ptr_removex(val, ptr, len, ctx, err); + } + return unsafe_yyjson_mut_ptr_putx(val, ptr, len, new_val, doc, + create_parent, false, ctx, err); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replace( + yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val) { + if (!ptr) return NULL; + return yyjson_mut_doc_ptr_replacen(doc, ptr, strlen(ptr), new_val); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( + yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val) { + return yyjson_mut_doc_ptr_replacex(doc, ptr, len, new_val, NULL, NULL); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacex( + yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { + + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!doc || !ptr || !new_val)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return NULL; + } + if (yyjson_unlikely(len == 0)) { + yyjson_mut_val *root = doc->root; + if (yyjson_unlikely(!root)) { + yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved"); + return NULL; + } + if (ctx) ctx->old = root; + doc->root = new_val; + return root; + } + if (yyjson_unlikely(!doc->root)) { + yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); + return NULL; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return NULL; + } + return unsafe_yyjson_mut_ptr_replacex(doc->root, ptr, len, new_val, + ctx, err); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replace( + yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val) { + if (!ptr) return NULL; + return yyjson_mut_ptr_replacen(val, ptr, strlen(ptr), new_val); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( + yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val) { + return yyjson_mut_ptr_replacex(val, ptr, len, new_val, NULL, NULL); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacex( + yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { + + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!val || !ptr || !new_val)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return NULL; + } + if (yyjson_unlikely(len == 0)) { + yyjson_ptr_set_err(SET_ROOT, "cannot set root"); + return NULL; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return NULL; + } + return unsafe_yyjson_mut_ptr_replacex(val, ptr, len, new_val, ctx, err); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_remove( + yyjson_mut_doc *doc, const char *ptr) { + if (!ptr) return NULL; + return yyjson_mut_doc_ptr_removen(doc, ptr, strlen(ptr)); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( + yyjson_mut_doc *doc, const char *ptr, size_t len) { + return yyjson_mut_doc_ptr_removex(doc, ptr, len, NULL, NULL); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removex( + yyjson_mut_doc *doc, const char *ptr, size_t len, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { + + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!doc || !ptr)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return NULL; + } + if (yyjson_unlikely(!doc->root)) { + yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); + return NULL; + } + if (yyjson_unlikely(len == 0)) { + yyjson_mut_val *root = doc->root; + if (ctx) ctx->old = root; + doc->root = NULL; + return root; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return NULL; + } + return unsafe_yyjson_mut_ptr_removex(doc->root, ptr, len, ctx, err); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_remove(yyjson_mut_val *val, + const char *ptr) { + if (!ptr) return NULL; + return yyjson_mut_ptr_removen(val, ptr, strlen(ptr)); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removen(yyjson_mut_val *val, + const char *ptr, + size_t len) { + return yyjson_mut_ptr_removex(val, ptr, len, NULL, NULL); +} + +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, + const char *ptr, + size_t len, + yyjson_ptr_ctx *ctx, + yyjson_ptr_err *err) { + yyjson_ptr_set_err(NONE, NULL); + if (ctx) memset(ctx, 0, sizeof(*ctx)); + + if (yyjson_unlikely(!val || !ptr)) { + yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); + return NULL; + } + if (yyjson_unlikely(len == 0)) { + yyjson_ptr_set_err(SET_ROOT, "cannot set root"); + return NULL; + } + if (yyjson_unlikely(*ptr != '/')) { + yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); + return NULL; + } + return unsafe_yyjson_mut_ptr_removex(val, ptr, len, ctx, err); +} + +yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, + yyjson_mut_val *key, + yyjson_mut_val *val) { + yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; + if (!ctx || !ctx->ctn || !val) return false; + ctn = ctx->ctn; + + if (yyjson_mut_is_obj(ctn)) { + if (!key) return false; + key->next = val; + pre_key = ctx->pre; + if (unsafe_yyjson_get_len(ctn) == 0) { + val->next = key; + ctn->uni.ptr = key; + ctx->pre = key; + } else if (!pre_key) { + pre_key = (yyjson_mut_val *)ctn->uni.ptr; + pre_val = pre_key->next; + val->next = pre_val->next; + pre_val->next = key; + ctn->uni.ptr = key; + ctx->pre = pre_key; + } else { + cur_key = pre_key->next->next; + cur_val = cur_key->next; + val->next = cur_val->next; + cur_val->next = key; + if (ctn->uni.ptr == cur_key) ctn->uni.ptr = key; + ctx->pre = cur_key; + } + } else { + pre_val = ctx->pre; + if (unsafe_yyjson_get_len(ctn) == 0) { + val->next = val; + ctn->uni.ptr = val; + ctx->pre = val; + } else if (!pre_val) { + pre_val = (yyjson_mut_val *)ctn->uni.ptr; + val->next = pre_val->next; + pre_val->next = val; + ctn->uni.ptr = val; + ctx->pre = pre_val; + } else { + cur_val = pre_val->next; + val->next = cur_val->next; + cur_val->next = val; + if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; + ctx->pre = cur_val; + } + } + unsafe_yyjson_inc_len(ctn); + return true; +} + +yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, + yyjson_mut_val *val) { + yyjson_mut_val *ctn, *pre_key, *cur_key, *pre_val, *cur_val; + if (!ctx || !ctx->ctn || !ctx->pre || !val) return false; + ctn = ctx->ctn; + if (yyjson_mut_is_obj(ctn)) { + pre_key = ctx->pre; + pre_val = pre_key->next; + cur_key = pre_val->next; + cur_val = cur_key->next; + /* replace current value */ + cur_key->next = val; + val->next = cur_val->next; + ctx->old = cur_val; + } else { + pre_val = ctx->pre; + cur_val = pre_val->next; + /* replace current value */ + if (pre_val != cur_val) { + val->next = cur_val->next; + pre_val->next = val; + if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; + } else { + val->next = val; + ctn->uni.ptr = val; + ctx->pre = val; + } + ctx->old = cur_val; + } + return true; +} + +yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx) { + yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; + size_t len; + if (!ctx || !ctx->ctn || !ctx->pre) return false; + ctn = ctx->ctn; + if (yyjson_mut_is_obj(ctn)) { + pre_key = ctx->pre; + pre_val = pre_key->next; + cur_key = pre_val->next; + cur_val = cur_key->next; + /* remove current key-value */ + pre_val->next = cur_val->next; + if (ctn->uni.ptr == cur_key) ctn->uni.ptr = pre_key; + ctx->pre = NULL; + ctx->old = cur_val; + } else { + pre_val = ctx->pre; + cur_val = pre_val->next; + /* remove current key-value */ + pre_val->next = cur_val->next; + if (ctn->uni.ptr == cur_val) ctn->uni.ptr = pre_val; + ctx->pre = NULL; + ctx->old = cur_val; + } + len = unsafe_yyjson_get_len(ctn) - 1; + if (len == 0) ctn->uni.ptr = NULL; + unsafe_yyjson_set_len(ctn, len); + return true; +} + +#undef yyjson_ptr_set_err + + + +/*============================================================================== + * MARK: - JSON Value at Pointer API (Implementation) + *============================================================================*/ + +/** + Set provided `value` if the JSON Pointer (RFC 6901) exists and is type bool. + Returns true if value at `ptr` exists and is the correct type, otherwise false. + */ +yyjson_api_inline bool yyjson_ptr_get_bool( + yyjson_val *root, const char *ptr, bool *value) { + yyjson_val *val = yyjson_ptr_get(root, ptr); + if (value && yyjson_is_bool(val)) { + *value = unsafe_yyjson_get_bool(val); + return true; + } else { + return false; + } +} + +/** + Set provided `value` if the JSON Pointer (RFC 6901) exists and is an integer + that fits in `uint64_t`. Returns true if successful, otherwise false. + */ +yyjson_api_inline bool yyjson_ptr_get_uint( + yyjson_val *root, const char *ptr, uint64_t *value) { + yyjson_val *val = yyjson_ptr_get(root, ptr); + if (value && val) { + uint64_t ret = val->uni.u64; + if (unsafe_yyjson_is_uint(val) || + (unsafe_yyjson_is_sint(val) && !(ret >> 63))) { + *value = ret; + return true; + } + } + return false; +} + +/** + Set provided `value` if the JSON Pointer (RFC 6901) exists and is an integer + that fits in `int64_t`. Returns true if successful, otherwise false. + */ +yyjson_api_inline bool yyjson_ptr_get_sint( + yyjson_val *root, const char *ptr, int64_t *value) { + yyjson_val *val = yyjson_ptr_get(root, ptr); + if (value && val) { + int64_t ret = val->uni.i64; + if (unsafe_yyjson_is_sint(val) || + (unsafe_yyjson_is_uint(val) && ret >= 0)) { + *value = ret; + return true; + } + } + return false; +} + +/** + Set provided `value` if the JSON Pointer (RFC 6901) exists and is type real. + Returns true if value at `ptr` exists and is the correct type, otherwise false. + */ +yyjson_api_inline bool yyjson_ptr_get_real( + yyjson_val *root, const char *ptr, double *value) { + yyjson_val *val = yyjson_ptr_get(root, ptr); + if (value && yyjson_is_real(val)) { + *value = unsafe_yyjson_get_real(val); + return true; + } else { + return false; + } +} + +/** + Set provided `value` if the JSON Pointer (RFC 6901) exists and is type sint, + uint or real. + Returns true if value at `ptr` exists and is the correct type, otherwise false. + */ +yyjson_api_inline bool yyjson_ptr_get_num( + yyjson_val *root, const char *ptr, double *value) { + yyjson_val *val = yyjson_ptr_get(root, ptr); + if (value && yyjson_is_num(val)) { + *value = unsafe_yyjson_get_num(val); + return true; + } else { + return false; + } +} + +/** + Set provided `value` if the JSON Pointer (RFC 6901) exists and is type string. + Returns true if value at `ptr` exists and is the correct type, otherwise false. + */ +yyjson_api_inline bool yyjson_ptr_get_str( + yyjson_val *root, const char *ptr, const char **value) { + yyjson_val *val = yyjson_ptr_get(root, ptr); + if (value && yyjson_is_str(val)) { + *value = unsafe_yyjson_get_str(val); + return true; + } else { + return false; + } +} + + + +/*============================================================================== + * MARK: - Deprecated + *============================================================================*/ + +/** @deprecated renamed to `yyjson_doc_ptr_get` */ +yyjson_deprecated("renamed to yyjson_doc_ptr_get") +yyjson_api_inline yyjson_val *yyjson_doc_get_pointer(yyjson_doc *doc, + const char *ptr) { + return yyjson_doc_ptr_get(doc, ptr); +} + +/** @deprecated renamed to `yyjson_doc_ptr_getn` */ +yyjson_deprecated("renamed to yyjson_doc_ptr_getn") +yyjson_api_inline yyjson_val *yyjson_doc_get_pointern(yyjson_doc *doc, + const char *ptr, + size_t len) { + return yyjson_doc_ptr_getn(doc, ptr, len); +} + +/** @deprecated renamed to `yyjson_mut_doc_ptr_get` */ +yyjson_deprecated("renamed to yyjson_mut_doc_ptr_get") +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointer( + yyjson_mut_doc *doc, const char *ptr) { + return yyjson_mut_doc_ptr_get(doc, ptr); +} + +/** @deprecated renamed to `yyjson_mut_doc_ptr_getn` */ +yyjson_deprecated("renamed to yyjson_mut_doc_ptr_getn") +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointern( + yyjson_mut_doc *doc, const char *ptr, size_t len) { + return yyjson_mut_doc_ptr_getn(doc, ptr, len); +} + +/** @deprecated renamed to `yyjson_ptr_get` */ +yyjson_deprecated("renamed to yyjson_ptr_get") +yyjson_api_inline yyjson_val *yyjson_get_pointer(yyjson_val *val, + const char *ptr) { + return yyjson_ptr_get(val, ptr); +} + +/** @deprecated renamed to `yyjson_ptr_getn` */ +yyjson_deprecated("renamed to yyjson_ptr_getn") +yyjson_api_inline yyjson_val *yyjson_get_pointern(yyjson_val *val, + const char *ptr, + size_t len) { + return yyjson_ptr_getn(val, ptr, len); +} + +/** @deprecated renamed to `yyjson_mut_ptr_get` */ +yyjson_deprecated("renamed to yyjson_mut_ptr_get") +yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointer(yyjson_mut_val *val, + const char *ptr) { + return yyjson_mut_ptr_get(val, ptr); +} + +/** @deprecated renamed to `yyjson_mut_ptr_getn` */ +yyjson_deprecated("renamed to yyjson_mut_ptr_getn") +yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointern(yyjson_mut_val *val, + const char *ptr, + size_t len) { + return yyjson_mut_ptr_getn(val, ptr, len); +} + +/** @deprecated renamed to `yyjson_mut_ptr_getn` */ +yyjson_deprecated("renamed to unsafe_yyjson_ptr_getn") +yyjson_api_inline yyjson_val *unsafe_yyjson_get_pointer(yyjson_val *val, + const char *ptr, + size_t len) { + yyjson_ptr_err err; + return unsafe_yyjson_ptr_getx(val, ptr, len, &err); +} + +/** @deprecated renamed to `unsafe_yyjson_mut_ptr_getx` */ +yyjson_deprecated("renamed to unsafe_yyjson_mut_ptr_getx") +yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_get_pointer( + yyjson_mut_val *val, const char *ptr, size_t len) { + yyjson_ptr_err err; + return unsafe_yyjson_mut_ptr_getx(val, ptr, len, NULL, &err); +} + +#endif /* YYJSON_DISABLE_UTILS */ + + + +/*============================================================================== + * MARK: - Compiler Hint End + *============================================================================*/ + +#if defined(__clang__) +# pragma clang diagnostic pop +#elif defined(__GNUC__) +# if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +# pragma GCC diagnostic pop +# endif +#elif defined(_MSC_VER) +# pragma warning(pop) +#endif /* warning suppress end */ + +#ifdef __cplusplus +} +#endif /* extern "C" end */ + +#endif /* YYJSON_H */ diff --git a/langdetect.c b/langdetect.c new file mode 100644 index 0000000..5911363 --- /dev/null +++ b/langdetect.c @@ -0,0 +1,269 @@ +#include "model_internal.h" +#include "kernels.h" +#include "threadpool.h" + +#include <yyjson.h> + +#include <errno.h> +#include <getopt.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#define HIDDEN_SIZE 768 +#define NUM_LABELS 20 +#define MAX_TOKENS 512 + +typedef struct { + ffwd_model_t *model; + ffwd_workspace_t *workspace; + ffwd_tok_t *tokenizer; + yyjson_doc *config; + const char *labels[NUM_LABELS]; + const float *dense_weight, *dense_bias; + const float *out_weight, *out_bias; +} detector_t; + +static double now_ms(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_sec * 1000.0 + t.tv_nsec / 1e6; +} + +static const float *load_tensor(detector_t *d, const char *name, const int64_t *shape, int ndim) { + safetensors_file_t *file = NULL; + const safetensor_t *tensor = multi_safetensors_find(d->model->safetensors, name, &file); + if (!tensor) { + fprintf(stderr, "Missing classifier tensor: %s\n", name); + return NULL; + } + if (!tensor_has_supported_shape(file, tensor, name, shape, ndim, 0)) + return NULL; + /* Classifier pointers borrow the model's mapping and remain valid until model teardown. */ + return safetensors_data(file, tensor); +} + +static void detector_free(detector_t *d) { + ffwd_tok_free(d->tokenizer); + ffwd_workspace_free(d->workspace); + ffwd_model_free(d->model); + yyjson_doc_free(d->config); +} + +static int detector_load(detector_t *d, const char *directory) { + char path[1024]; + int length = snprintf(path, sizeof(path), "%s/config.json", directory); + if (length < 0 || (size_t)length >= sizeof(path)) { + fprintf(stderr, "Model path is too long\n"); + return -1; + } + yyjson_read_err error; + d->config = yyjson_read_file(path, 0, NULL, &error); + if (!d->config) { + fprintf(stderr, "Cannot read %s: %s\n", path, error.msg); + return -1; + } + yyjson_val *root = yyjson_doc_get_root(d->config); + if (!yyjson_equals_str(yyjson_obj_get(root, "model_type"), "xlm-roberta") || + !yyjson_equals_str(yyjson_arr_get(yyjson_obj_get(root, "architectures"), 0), + "XLMRobertaForSequenceClassification")) { + fprintf(stderr, "Expected an XLMRobertaForSequenceClassification checkpoint\n"); + return -1; + } + yyjson_val *labels = yyjson_obj_get(root, "id2label"); + if (yyjson_obj_size(labels) != NUM_LABELS) { + fprintf(stderr, "Expected %d language labels in config.json\n", NUM_LABELS); + return -1; + } + for (int i = 0; i < NUM_LABELS; i++) { + char key[16]; + snprintf(key, sizeof(key), "%d", i); + d->labels[i] = yyjson_get_str(yyjson_obj_get(labels, key)); + if (!d->labels[i] || !d->labels[i][0]) { + fprintf(stderr, "Missing language label %d\n", i); + return -1; + } + } + + d->model = ffwd_model_load(directory); + if (!d->model) + return -1; + ffwd_config_t *cfg = &d->model->config; + if (cfg->family != FFWD_FAMILY_BERT || cfg->hidden_size != HIDDEN_SIZE || + cfg->max_position_embeddings - cfg->position_id_offset < MAX_TOKENS) { + fprintf(stderr, "Expected a 768-dimensional XLM-R encoder with 512-token capacity\n"); + return -1; + } + /* The classifier consumes the unnormalized final state of <s>, not a sentence mean. */ + cfg->pooling_mode = FFWD_POOL_CLS; + cfg->normalize_embeddings = 0; + d->dense_weight = load_tensor(d, "classifier.dense.weight", + (int64_t[]){HIDDEN_SIZE, HIDDEN_SIZE}, 2); + d->dense_bias = load_tensor(d, "classifier.dense.bias", (int64_t[]){HIDDEN_SIZE}, 1); + d->out_weight = load_tensor(d, "classifier.out_proj.weight", + (int64_t[]){NUM_LABELS, HIDDEN_SIZE}, 2); + d->out_bias = load_tensor(d, "classifier.out_proj.bias", (int64_t[]){NUM_LABELS}, 1); + if (!d->dense_weight || !d->dense_bias || !d->out_weight || !d->out_bias) + return -1; + + char message[256] = {0}; + d->tokenizer = ffwd_tok_open(directory, 0, message, sizeof(message)); + if (!d->tokenizer) { + fprintf(stderr, "Tokenizer: %s\n", message); + return -1; + } + d->workspace = ffwd_workspace_new(d->model); + if (!d->workspace) { + fprintf(stderr, "Cannot allocate inference workspace\n"); + return -1; + } + return 0; +} + +static int classify(detector_t *d, const char *text, int all_scores) { + double start = now_ms(); + int count = 0; + int *ids = ffwd_tokenize(d->tokenizer, text, NULL, &count); + if (!ids || count < 2) { + fprintf(stderr, "Tokenization failed\n"); + free(ids); + return -1; + } + int truncated = count > MAX_TOKENS; + if (truncated) { + ids[MAX_TOKENS - 1] = ids[count - 1]; + count = MAX_TOKENS; + } + float hidden[HIDDEN_SIZE], dense[HIDDEN_SIZE], scores[NUM_LABELS]; + int result = ffwd_model_encode_into(d->model, d->workspace, ids, count, hidden); + free(ids); + if (result != 0) { + fprintf(stderr, "Encoder inference failed\n"); + return -1; + } + linear(dense, hidden, d->dense_weight, d->dense_bias, 1, HIDDEN_SIZE, HIDDEN_SIZE); + for (int i = 0; i < HIDDEN_SIZE; i++) + dense[i] = tanhf(dense[i]); + linear(scores, dense, d->out_weight, d->out_bias, 1, HIDDEN_SIZE, NUM_LABELS); + softmax(scores, 1, NUM_LABELS); + int best = 0; + for (int i = 0; i < NUM_LABELS; i++) { + if (!isfinite(scores[i])) { + fprintf(stderr, "Classifier produced a non-finite score\n"); + return -1; + } + if (scores[i] > scores[best]) + best = i; + } + double elapsed = now_ms() - start; + + yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); + if (!doc) + return -1; + yyjson_mut_val *root = yyjson_mut_obj(doc); + yyjson_mut_doc_set_root(doc, root); + int ok = root && + yyjson_mut_obj_add_str(doc, root, "label", d->labels[best]) && + yyjson_mut_obj_add_real(doc, root, "score", scores[best]) && + yyjson_mut_obj_add_int(doc, root, "tokens", count) && + yyjson_mut_obj_add_bool(doc, root, "truncated", truncated) && + yyjson_mut_obj_add_real(doc, root, "ms", elapsed); + if (all_scores && ok) { + yyjson_mut_val *all = yyjson_mut_obj(doc); + ok = all && yyjson_mut_obj_add_val(doc, root, "scores", all); + for (int i = 0; i < NUM_LABELS && ok; i++) + ok = yyjson_mut_obj_add_real(doc, all, d->labels[i], scores[i]); + } + char *json = ok ? yyjson_mut_write(doc, 0, NULL) : NULL; + int written = json ? puts(json) : EOF; + free(json); + yyjson_mut_doc_free(doc); + if (written == EOF || fflush(stdout) == EOF) { + fprintf(stderr, "Cannot write classification result\n"); + return -1; + } + return 0; +} + +static void usage(FILE *out, const char *program) { + fprintf(out, + "Usage: %s [-d MODEL_DIR] [-t THREADS] [--all] [--] [TEXT ...]\n" + "Without TEXT arguments, read one UTF-8 text per line from stdin.\n" + "Write one JSON result per text, in input order.\n" + " -d DIR Model directory (default: ./model)\n" + " -t N CPU kernel threads, 1-16 (default: detected CPU count, capped at 16)\n" + " --all Include scores for all 20 languages\n" + " -h Show help\n" + "Inputs longer than 512 tokens are truncated, retaining the final </s>.\n", + program); +} + +int main(int argc, char **argv) { + const char *directory = "model"; + int threads = tp_get_num_cpus(); + if (threads > 16) + threads = 16; + int all_scores = 0; + const struct option options[] = { + {"all", no_argument, NULL, 'a'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0}, + }; + int option; + while ((option = getopt_long(argc, argv, "d:t:ah", options, NULL)) != -1) { + switch (option) { + case 'd': directory = optarg; break; + case 'a': all_scores = 1; break; + case 'h': usage(stdout, argv[0]); return 0; + case 't': { + char *end; + errno = 0; + long n = strtol(optarg, &end, 10); + if (errno || *end || n < 1 || n > 16) { + fprintf(stderr, "Thread count must be an integer between 1 and 16\n"); + return 1; + } + threads = (int)n; + break; + } + default: usage(stderr, argv[0]); return 1; + } + } + detector_t detector = {0}; + if (detector_load(&detector, directory) != 0) { + detector_free(&detector); + return 1; + } + tp_set_threads(threads); + int status = 0; + if (optind < argc) { + for (int i = optind; i < argc && status == 0; i++) + status = classify(&detector, argv[i], all_scores); + } else { + char *line = NULL; + size_t capacity = 0; + ssize_t length; + while ((length = getline(&line, &capacity, stdin)) >= 0) { + if (memchr(line, '\0', (size_t)length)) { + fprintf(stderr, "Input must not contain NUL bytes\n"); + status = -1; + break; + } + while (length > 0 && (line[length - 1] == '\n' || line[length - 1] == '\r')) + line[--length] = '\0'; + status = classify(&detector, line, all_scores); + if (status != 0) + break; + } + if (ferror(stdin) || (!feof(stdin) && status == 0)) { + fprintf(stderr, "Cannot read input\n"); + status = -1; + } + free(line); + } + tp_set_threads(1); + detector_free(&detector); + return status == 0 ? 0 : 1; +} diff --git a/libffwd/LICENSE b/libffwd/LICENSE new file mode 100644 index 0000000..a00caa7 --- /dev/null +++ b/libffwd/LICENSE @@ -0,0 +1,20 @@ +MIT License + +Copyright (c) 2026 The ffwd authors + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/libffwd/act.c b/libffwd/act.c new file mode 100644 index 0000000..2495311 --- /dev/null +++ b/libffwd/act.c @@ -0,0 +1,90 @@ +/* Activation kernels: softmax, SiLU-gated multiply, and the erf and tanh GeLU variants. */ + +#include <math.h> +#include <stdlib.h> +#include <string.h> + +#include "impl.h" +#include "kernels.h" + +#if (defined(__AVX512F__) || defined(__AVX2__)) && \ + (defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)) +# include <immintrin.h> +#endif +#if defined(__ARM_NEON) || defined(__ARM_NEON__) +# include <arm_neon.h> +#endif + +#ifdef USE_BLAS +# ifdef __APPLE__ +# include <Accelerate/Accelerate.h> +# else +# include <cblas.h> +# endif +#endif + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +void silu_mul_inplace(float *gate, const float *up, int n) { +#if defined(__APPLE__) && defined(USE_BLAS) + enum { CHUNK = 4096 }; + float exp_neg[CHUNK]; + + for (int base = 0; base < n; base += CHUNK) { + int len = n - base; + if (len > CHUNK) + len = CHUNK; + + for (int i = 0; i < len; i++) + exp_neg[i] = -gate[base + i]; + vvexpf(exp_neg, exp_neg, &len); + + for (int i = 0; i < len; i++) { + float g = gate[base + i]; + gate[base + i] = (g / (1.0f + exp_neg[i])) * up[base + i]; + } + } +#else + for (int i = 0; i < n; i++) { + float g = gate[i]; + gate[i] = (g / (1.0f + expf(-g))) * up[i]; + } +#endif +} + +void gelu_inplace(float *x, int n) { + /* Exact erf GeLU; 0.70710678... = 1/sqrt(2). */ + for (int i = 0; i < n; i++) + x[i] = 0.5f * x[i] * (1.0f + erff(x[i] * 0.70710678118654752f)); +} + +void gelu_tanh_inplace(float *x, int n) { + /* Tanh-approximation GeLU; 0.79788456... = sqrt(2/pi). */ + for (int i = 0; i < n; i++) { + float v = x[i]; + float inner = 0.79788456080286536f * (v + 0.044715f * v * v * v); + x[i] = 0.5f * v * (1.0f + tanhf(inner)); + } +} + +void softmax(float *x, int rows, int cols) { + for (int r = 0; r < rows; r++) { + float *row = x + r * cols; + float max_val = row[0]; + for (int c = 1; c < cols; c++) { + if (row[c] > max_val) + max_val = row[c]; + } + float sum = 0.0f; + for (int c = 0; c < cols; c++) { + row[c] = expf(row[c] - max_val); + sum += row[c]; + } + float inv_sum = 1.0f / sum; + for (int c = 0; c < cols; c++) { + row[c] *= inv_sum; + } + } +} diff --git a/libffwd/alloc.c b/libffwd/alloc.c new file mode 100644 index 0000000..828258e --- /dev/null +++ b/libffwd/alloc.c @@ -0,0 +1,91 @@ +#include "alloc.h" + +#include <stdlib.h> +#include <stdint.h> +#include <limits.h> + +/* Sequence-capacity growth policy for the forward-pass workspaces: + * For new growth: + * - never allocate less than the minimum required size. + * - round up to the granularity so small length changes do not trigger a realloc on every call. + * - keep existing capacity if it is already larger. */ +#define FFWD_MIN_WORKSPACE_SEQ_CAP 16 +#define FFWD_WORKSPACE_SEQ_GRANULARITY 16 + +int mul_size(size_t a, size_t b, size_t *out) { + if (a != 0 && b > SIZE_MAX / a) + return -1; + *out = a * b; + return 0; +} + +int add_size(size_t a, size_t b, size_t *out) { + if (b > SIZE_MAX - a) + return -1; + *out = a + b; + return 0; +} + +int grow_cap(int current, int needed, int *out) { + if (current < 0 || needed <= 0) + return -1; + + int cap = needed; + if (cap < FFWD_MIN_WORKSPACE_SEQ_CAP) + cap = FFWD_MIN_WORKSPACE_SEQ_CAP; + + int rem = cap % FFWD_WORKSPACE_SEQ_GRANULARITY; + if (rem != 0) { + int pad = FFWD_WORKSPACE_SEQ_GRANULARITY - rem; + if (cap > INT_MAX - pad) + return -1; + cap += pad; + } + + if (cap < current) + cap = current; + + *out = cap; + return 0; +} + +int realloc_floats(float **ptr, size_t count) { + size_t bytes; + if (mul_size(count, sizeof(float), &bytes) != 0) + return -1; + + void *p = realloc(*ptr, bytes); + if (!p && bytes != 0) + return -1; + *ptr = (float *)p; + return 0; +} + +float *malloc_floats(size_t count) { + size_t bytes; + if (mul_size(count, sizeof(float), &bytes) != 0) + return NULL; + return (float *)malloc(bytes); +} + +int realloc_floats_2d(float **ptr, int rows, int cols) { + if (rows < 0 || cols < 0) + return -1; + + size_t count; + if (mul_size((size_t)rows, (size_t)cols, &count) != 0) + return -1; + return realloc_floats(ptr, count); +} + +int realloc_ints(int **ptr, size_t count) { + size_t bytes; + if (mul_size(count, sizeof(int), &bytes) != 0) + return -1; + + void *p = realloc(*ptr, bytes); + if (!p && bytes != 0) + return -1; + *ptr = (int *)p; + return 0; +} diff --git a/libffwd/alloc.h b/libffwd/alloc.h new file mode 100644 index 0000000..a12ee7a --- /dev/null +++ b/libffwd/alloc.h @@ -0,0 +1,31 @@ +#ifndef FFWD_ALLOC_H +#define FFWD_ALLOC_H + +#include <stddef.h> + +/* Checked size arithmetic and typed allocation helpers. + * Return -1 or NULL on overflow/allocation failure. + * realloc_* leave the original pointer unchanged on failure. */ + +/* out = a * b, or -1 if the product overflows size_t. */ +int mul_size(size_t a, size_t b, size_t *out); + +/* out = a + b, or -1 if the sum overflows size_t. */ +int add_size(size_t a, size_t b, size_t *out); + +/* Grow sequence capacity without shrinking. */ +int grow_cap(int current, int needed, int *out); + +/* realloc *ptr to hold count floats; *ptr unchanged on failure. */ +int realloc_floats(float **ptr, size_t count); + +/* malloc count floats, NULL on overflow/failure. */ +float *malloc_floats(size_t count); + +/* realloc *ptr to hold rows*cols floats; -1 on negative dims/overflow/failure. */ +int realloc_floats_2d(float **ptr, int rows, int cols); + +/* realloc *ptr to hold count ints; *ptr unchanged on failure. */ +int realloc_ints(int **ptr, size_t count); + +#endif /* FFWD_ALLOC_H */ diff --git a/libffwd/attn.c b/libffwd/attn.c new file mode 100644 index 0000000..57c665a --- /dev/null +++ b/libffwd/attn.c @@ -0,0 +1,458 @@ +#include "kernels.h" +#include "impl.h" +#include "threadpool.h" + +#include <math.h> +#include <stdlib.h> +#include <string.h> + +#ifdef USE_BLAS +# ifdef __APPLE__ +# include <Accelerate/Accelerate.h> +# else +# include <cblas.h> +# endif +#endif + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +/* Packed block-diagonal attention: causal (Qwen) and bidirectional (BERT/GTE) + * GQA, each with an online-softmax and a BLAS-GEMM path. */ + +/* Attention operations */ + +float dot_f32(const float *a, const float *b, int n) { return dot_f32_impl(a, b, n); } + +static inline float dot_f32_fast(const float *a, const float *b, int n) { + return dot_f32_impl(a, b, n); +} + +/* dst = dst * scale */ +static inline void vec_scale_inplace(float *dst, float scale, int n) { + vec_scale_inplace_impl(dst, scale, n); +} + +/* dst += alpha * src */ +static inline void vec_axpy_inplace(float *dst, const float *src, float alpha, int n) { + vec_axpy_inplace_impl(dst, src, alpha, n); +} + +/* dst = dst * correction + src */ +static inline void vec_scale_add(float *dst, const float *src, float correction, int n) { + vec_scale_add_impl(dst, src, correction, n); +} + +#define PACKED_ATTN_QUERY_TILE 32 +#define PACKED_ATTN_BLAS_MIN_SEQ 128 + +static void bidirectional_gqa_attention_packed_online_rows(float *out, + const float *Q, + const float *K, + const float *V, + int start, + int end, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + int h, + int query_start, + int query_end) { + int heads_per_kv = n_heads / n_kv_heads; + int q_hidden = n_heads * head_dim; + int kv_hidden = n_kv_heads * head_dim; + int kv_h = h / heads_per_kv; + + for (int i = query_start; i < query_end; i++) { + const float *q_row = Q + (size_t)i * q_hidden + h * head_dim; + float *o_row = out + (size_t)i * q_hidden + h * head_dim; + + float max_score = -1e30f; + float sum_exp = 0.0f; + for (int d = 0; d < head_dim; d++) + o_row[d] = 0.0f; + + for (int j = start; j < end; j++) { + const float *k_row = K + (size_t)j * kv_hidden + kv_h * head_dim; + const float *v_row = V + (size_t)j * kv_hidden + kv_h * head_dim; + + float score = dot_f32_fast(q_row, k_row, head_dim) * scale; + + if (score > max_score) { + float correction = expf(max_score - score); + sum_exp = sum_exp * correction + 1.0f; + vec_scale_add(o_row, v_row, correction, head_dim); + max_score = score; + } else { + float wt = expf(score - max_score); + sum_exp += wt; + vec_axpy_inplace(o_row, v_row, wt, head_dim); + } + } + + if (sum_exp > 0.0f) { + float inv_sum = 1.0f / sum_exp; + vec_scale_inplace(o_row, inv_sum, head_dim); + } + } +} + +#ifdef USE_BLAS +static void bidirectional_gqa_attention_packed_blas_rows(float *out, + const float *Q, + const float *K, + const float *V, + int start, + int end, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + int h, + int query_start, + int query_end, + float *scores) { + int heads_per_kv = n_heads / n_kv_heads; + int q_hidden = n_heads * head_dim; + int kv_hidden = n_kv_heads * head_dim; + int kv_h = h / heads_per_kv; + int rows = query_end - query_start; + int keys = end - start; + + const float *q = Q + (size_t)query_start * q_hidden + h * head_dim; + const float *k = K + (size_t)start * kv_hidden + kv_h * head_dim; + const float *v = V + (size_t)start * kv_hidden + kv_h * head_dim; + float *o = out + (size_t)query_start * q_hidden + h * head_dim; + + cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, rows, keys, head_dim, scale, q, q_hidden, k, + kv_hidden, 0.0f, scores, keys); + softmax(scores, rows, keys); + cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, rows, head_dim, keys, 1.0f, scores, keys, + v, kv_hidden, 0.0f, o, q_hidden); +} +#endif + +typedef struct { + float *out; + const float *Q; + const float *K; + const float *V; + const int *offsets; + int batch; + int n_heads, n_kv_heads; + int head_dim; + float scale; + float *scores; + int max_seq; +} packed_gqa_attn_task_t; + +static void packed_gqa_attn_worker(int tid, int n_threads, void *arg) { + packed_gqa_attn_task_t *t = (packed_gqa_attn_task_t *)arg; + int task_id = 0; +#ifdef USE_BLAS + float *scores = t->scores ? t->scores + (size_t)tid * PACKED_ATTN_QUERY_TILE * t->max_seq : NULL; +#endif + + for (int h = 0; h < t->n_heads; h++) { + for (int b = 0; b < t->batch; b++) { + int start = t->offsets[b]; + int end = t->offsets[b + 1]; + for (int q0 = start; q0 < end; q0 += PACKED_ATTN_QUERY_TILE) { + int q1 = q0 + PACKED_ATTN_QUERY_TILE; + if (q1 > end) + q1 = end; + if (task_id++ % n_threads != tid) + continue; +#ifdef USE_BLAS + if (scores && end - start >= PACKED_ATTN_BLAS_MIN_SEQ) { + bidirectional_gqa_attention_packed_blas_rows( + t->out, t->Q, t->K, t->V, start, end, t->n_heads, t->n_kv_heads, t->head_dim, + t->scale, h, q0, q1, scores); + continue; + } +#endif + bidirectional_gqa_attention_packed_online_rows(t->out, t->Q, t->K, t->V, start, end, + t->n_heads, t->n_kv_heads, t->head_dim, + t->scale, h, q0, q1); + } + } + } +} + +size_t bidirectional_gqa_attention_packed_scratch_bytes(const int *offsets, int batch) { +#ifdef USE_BLAS + if (!offsets || batch <= 0) + return 0; + + int max_seq = 0; + for (int b = 0; b < batch; b++) { + int len = offsets[b + 1] - offsets[b]; + if (len > max_seq) + max_seq = len; + } + if (max_seq < PACKED_ATTN_BLAS_MIN_SEQ) + return 0; + if ((size_t)max_seq > + SIZE_MAX / (sizeof(float) * PACKED_ATTN_QUERY_TILE * (size_t)tp_num_threads())) + return 0; + return (size_t)tp_num_threads() * PACKED_ATTN_QUERY_TILE * (size_t)max_seq * sizeof(float); +#else + (void)offsets; + (void)batch; + return 0; +#endif +} + +void bidirectional_gqa_attention_packed_with_scratch(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + float *scratch, + size_t scratch_bytes) { + long long qk_work = 0; + int max_seq = 0; + for (int b = 0; b < batch; b++) { + int len = offsets[b + 1] - offsets[b]; + qk_work += (long long)len * len * n_heads; + if (len > max_seq) + max_seq = len; + } + + float *scores = NULL; +#ifdef USE_BLAS + size_t required = bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + if (required != 0 && scratch && scratch_bytes >= required) + scores = scratch; +#else + (void)scratch; + (void)scratch_bytes; +#endif + packed_gqa_attn_task_t task = {.out = out, + .Q = Q, + .K = K, + .V = V, + .offsets = offsets, + .batch = batch, + .n_heads = n_heads, + .n_kv_heads = n_kv_heads, + .head_dim = head_dim, + .scale = scale, + .scores = scores, + .max_seq = max_seq}; + if (tp_num_threads() > 1 && n_heads >= 2 && qk_work >= 4096) { + parallel_for(packed_gqa_attn_worker, &task); + } else { + packed_gqa_attn_worker(0, 1, &task); + } +} + +void bidirectional_gqa_attention_packed(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale) { + size_t scratch_bytes = bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + float *scores = scratch_bytes ? malloc(scratch_bytes) : NULL; + bidirectional_gqa_attention_packed_with_scratch(out, Q, K, V, offsets, batch, n_heads, n_kv_heads, + head_dim, scale, scores, scratch_bytes); + free(scores); +} + +static void causal_gqa_attention_packed_online_rows(float *out, + const float *Q, + const float *K, + const float *V, + int start, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + int h, + int query_start, + int query_end) { + int heads_per_kv = n_heads / n_kv_heads; + int q_hidden = n_heads * head_dim; + int kv_hidden = n_kv_heads * head_dim; + int kv_h = h / heads_per_kv; + + for (int i = query_start; i < query_end; i++) { + const float *q_row = Q + (size_t)i * q_hidden + h * head_dim; + float *o_row = out + (size_t)i * q_hidden + h * head_dim; + + float max_score = -1e30f; + float sum_exp = 0.0f; + for (int d = 0; d < head_dim; d++) + o_row[d] = 0.0f; + + for (int j = start; j <= i; j++) { + const float *k_row = K + (size_t)j * kv_hidden + kv_h * head_dim; + const float *v_row = V + (size_t)j * kv_hidden + kv_h * head_dim; + float score = dot_f32_fast(q_row, k_row, head_dim) * scale; + + if (score > max_score) { + float correction = expf(max_score - score); + sum_exp = sum_exp * correction + 1.0f; + vec_scale_add(o_row, v_row, correction, head_dim); + max_score = score; + } else { + float wt = expf(score - max_score); + sum_exp += wt; + vec_axpy_inplace(o_row, v_row, wt, head_dim); + } + } + + if (sum_exp > 0.0f) + vec_scale_inplace(o_row, 1.0f / sum_exp, head_dim); + } +} + +#ifdef USE_BLAS +static void causal_gqa_attention_packed_blas_rows(float *out, + const float *Q, + const float *K, + const float *V, + int start, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + int h, + int query_start, + int query_end, + float *scores) { + int heads_per_kv = n_heads / n_kv_heads; + int q_hidden = n_heads * head_dim; + int kv_hidden = n_kv_heads * head_dim; + int kv_h = h / heads_per_kv; + int rows = query_end - query_start; + int keys = query_end - start; + + const float *q = Q + (size_t)query_start * q_hidden + h * head_dim; + const float *k = K + (size_t)start * kv_hidden + kv_h * head_dim; + const float *v = V + (size_t)start * kv_hidden + kv_h * head_dim; + float *o = out + (size_t)query_start * q_hidden + h * head_dim; + + cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, rows, keys, head_dim, scale, q, q_hidden, k, + kv_hidden, 0.0f, scores, keys); + for (int r = 0; r < rows; r++) { + int allowed = query_start + r - start + 1; + float *row = scores + (size_t)r * keys; + for (int j = allowed; j < keys; j++) + row[j] = -INFINITY; + } + softmax(scores, rows, keys); + cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, rows, head_dim, keys, 1.0f, scores, keys, + v, kv_hidden, 0.0f, o, q_hidden); +} +#endif + +static void packed_causal_gqa_attn_worker(int tid, int n_threads, void *arg) { + packed_gqa_attn_task_t *t = (packed_gqa_attn_task_t *)arg; + int task_id = 0; +#ifdef USE_BLAS + float *scores = t->scores ? t->scores + (size_t)tid * PACKED_ATTN_QUERY_TILE * t->max_seq : NULL; +#endif + + for (int h = 0; h < t->n_heads; h++) { + for (int b = 0; b < t->batch; b++) { + int start = t->offsets[b]; + int end = t->offsets[b + 1]; + for (int q0 = start; q0 < end; q0 += PACKED_ATTN_QUERY_TILE) { + int q1 = q0 + PACKED_ATTN_QUERY_TILE; + if (q1 > end) + q1 = end; + if (task_id++ % n_threads != tid) + continue; +#ifdef USE_BLAS + if (scores && end - start >= PACKED_ATTN_BLAS_MIN_SEQ) { + causal_gqa_attention_packed_blas_rows(t->out, t->Q, t->K, t->V, start, t->n_heads, + t->n_kv_heads, t->head_dim, t->scale, h, q0, + q1, scores); + continue; + } +#endif + causal_gqa_attention_packed_online_rows(t->out, t->Q, t->K, t->V, start, t->n_heads, + t->n_kv_heads, t->head_dim, t->scale, h, q0, + q1); + } + } + } +} + +void causal_gqa_attention_packed_with_scratch(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + float *scratch, + size_t scratch_bytes) { + long long qk_work = 0; + int max_seq = 0; + for (int b = 0; b < batch; b++) { + int len = offsets[b + 1] - offsets[b]; + qk_work += (long long)len * (len + 1) / 2 * n_heads; + if (len > max_seq) + max_seq = len; + } + + float *scores = NULL; +#ifdef USE_BLAS + size_t required = bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + if (required != 0 && scratch && scratch_bytes >= required) + scores = scratch; +#else + (void)scratch; + (void)scratch_bytes; +#endif + packed_gqa_attn_task_t task = {.out = out, + .Q = Q, + .K = K, + .V = V, + .offsets = offsets, + .batch = batch, + .n_heads = n_heads, + .n_kv_heads = n_kv_heads, + .head_dim = head_dim, + .scale = scale, + .scores = scores, + .max_seq = max_seq}; + if (tp_num_threads() > 1 && n_heads >= 2 && qk_work >= 4096) + parallel_for(packed_causal_gqa_attn_worker, &task); + else + packed_causal_gqa_attn_worker(0, 1, &task); +} + +void causal_gqa_attention_packed(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale) { + size_t scratch_bytes = bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + float *scores = scratch_bytes ? malloc(scratch_bytes) : NULL; + causal_gqa_attention_packed_with_scratch(out, Q, K, V, offsets, batch, n_heads, n_kv_heads, + head_dim, scale, scores, scratch_bytes); + free(scores); +} diff --git a/libffwd/avx.c b/libffwd/avx.c new file mode 100644 index 0000000..7cbbd5a --- /dev/null +++ b/libffwd/avx.c @@ -0,0 +1,271 @@ +/* x86 SIMD hot kernels: AVX2+FMA, plus AVX-512 when available. + * + * BF16 -> F32 conversion: + * load 16 uint16 values, zero-extend to 32-bit, then shift left 16. + * + * Uses AVX-512F+BW for 16-wide BF16 matvec, dominant when seq_len == 1. + * Uses AVX2+FMA / AVX-512F for F32 attention helpers, which operate on + * cache-resident data. + * + * BF16 matvec processes 4 output rows at once to reduce instruction overhead + * and improve out-of-order execution on memory-bound workloads. + */ + +#include "impl.h" + +#if defined(__AVX2__) && defined(__FMA__) + +# include <immintrin.h> +# include <string.h> + +/* BF16 matvec: uses AVX-512F+BW when available, AVX2 fallback otherwise */ + +# if defined(__AVX512F__) && defined(__AVX512BW__) + +void bf16_matvec_fused_avx( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { + int o = 0; + + /* Process 4 output rows at a time */ + for (; o + 3 < out_dim; o += 4) { + const uint16_t *w0 = W_bf16 + (size_t)o * in_dim; + const uint16_t *w1 = w0 + in_dim; + const uint16_t *w2 = w1 + in_dim; + const uint16_t *w3 = w2 + in_dim; + + __m512 a0 = _mm512_setzero_ps(), a1 = _mm512_setzero_ps(); + __m512 a2 = _mm512_setzero_ps(), a3 = _mm512_setzero_ps(); + __m512 a4 = _mm512_setzero_ps(), a5 = _mm512_setzero_ps(); + __m512 a6 = _mm512_setzero_ps(), a7 = _mm512_setzero_ps(); + int k = 0; + + for (; k + 32 <= in_dim; k += 32) { + __m512 xv0 = _mm512_loadu_ps(x + k); + __m512 xv1 = _mm512_loadu_ps(x + k + 16); + + /* Row 0 */ + __m256i r0a = _mm256_loadu_si256((const __m256i *)(w0 + k)); + __m256i r0b = _mm256_loadu_si256((const __m256i *)(w0 + k + 16)); + a0 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0a), 16)), xv0, a0); + a1 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0b), 16)), xv1, a1); + + /* Row 1 */ + __m256i r1a = _mm256_loadu_si256((const __m256i *)(w1 + k)); + __m256i r1b = _mm256_loadu_si256((const __m256i *)(w1 + k + 16)); + a2 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1a), 16)), xv0, a2); + a3 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1b), 16)), xv1, a3); + + /* Row 2 */ + __m256i r2a = _mm256_loadu_si256((const __m256i *)(w2 + k)); + __m256i r2b = _mm256_loadu_si256((const __m256i *)(w2 + k + 16)); + a4 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r2a), 16)), xv0, a4); + a5 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r2b), 16)), xv1, a5); + + /* Row 3 */ + __m256i r3a = _mm256_loadu_si256((const __m256i *)(w3 + k)); + __m256i r3b = _mm256_loadu_si256((const __m256i *)(w3 + k + 16)); + a6 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r3a), 16)), xv0, a6); + a7 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r3b), 16)), xv1, a7); + } + + for (; k + 16 <= in_dim; k += 16) { + __m512 xv = _mm512_loadu_ps(x + k); + __m256i r0 = _mm256_loadu_si256((const __m256i *)(w0 + k)); + __m256i r1 = _mm256_loadu_si256((const __m256i *)(w1 + k)); + __m256i r2 = _mm256_loadu_si256((const __m256i *)(w2 + k)); + __m256i r3 = _mm256_loadu_si256((const __m256i *)(w3 + k)); + a0 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0), 16)), xv, a0); + a2 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1), 16)), xv, a2); + a4 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r2), 16)), xv, a4); + a6 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r3), 16)), xv, a6); + } + + float s0 = _mm512_reduce_add_ps(_mm512_add_ps(a0, a1)) + (bias ? bias[o] : 0.0f); + float s1 = _mm512_reduce_add_ps(_mm512_add_ps(a2, a3)) + (bias ? bias[o + 1] : 0.0f); + float s2 = _mm512_reduce_add_ps(_mm512_add_ps(a4, a5)) + (bias ? bias[o + 2] : 0.0f); + float s3 = _mm512_reduce_add_ps(_mm512_add_ps(a6, a7)) + (bias ? bias[o + 3] : 0.0f); + + for (; k < in_dim; k++) { + float xk = x[k]; + uint32_t b0 = ((uint32_t)w0[k]) << 16, b1 = ((uint32_t)w1[k]) << 16; + uint32_t b2 = ((uint32_t)w2[k]) << 16, b3 = ((uint32_t)w3[k]) << 16; + float v0, v1, v2, v3; + memcpy(&v0, &b0, 4); + memcpy(&v1, &b1, 4); + memcpy(&v2, &b2, 4); + memcpy(&v3, &b3, 4); + s0 += v0 * xk; + s1 += v1 * xk; + s2 += v2 * xk; + s3 += v3 * xk; + } + y[o] = s0; + y[o + 1] = s1; + y[o + 2] = s2; + y[o + 3] = s3; + } + + /* Handle 2-row remainder */ + for (; o + 1 < out_dim; o += 2) { + const uint16_t *w0 = W_bf16 + (size_t)o * in_dim; + const uint16_t *w1 = w0 + in_dim; + __m512 a0 = _mm512_setzero_ps(), a1 = _mm512_setzero_ps(); + __m512 a2 = _mm512_setzero_ps(), a3 = _mm512_setzero_ps(); + int k = 0; + + for (; k + 32 <= in_dim; k += 32) { + __m512 xv0 = _mm512_loadu_ps(x + k); + __m512 xv1 = _mm512_loadu_ps(x + k + 16); + __m256i r0a = _mm256_loadu_si256((const __m256i *)(w0 + k)); + __m256i r0b = _mm256_loadu_si256((const __m256i *)(w0 + k + 16)); + __m256i r1a = _mm256_loadu_si256((const __m256i *)(w1 + k)); + __m256i r1b = _mm256_loadu_si256((const __m256i *)(w1 + k + 16)); + a0 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0a), 16)), xv0, a0); + a1 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0b), 16)), xv1, a1); + a2 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1a), 16)), xv0, a2); + a3 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1b), 16)), xv1, a3); + } + for (; k + 16 <= in_dim; k += 16) { + __m512 xv = _mm512_loadu_ps(x + k); + __m256i r0 = _mm256_loadu_si256((const __m256i *)(w0 + k)); + __m256i r1 = _mm256_loadu_si256((const __m256i *)(w1 + k)); + a0 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r0), 16)), xv, a0); + a2 = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(r1), 16)), xv, a2); + } + + float s0 = _mm512_reduce_add_ps(_mm512_add_ps(a0, a1)) + (bias ? bias[o] : 0.0f); + float s1 = _mm512_reduce_add_ps(_mm512_add_ps(a2, a3)) + (bias ? bias[o + 1] : 0.0f); + + for (; k < in_dim; k++) { + uint32_t b0 = ((uint32_t)w0[k]) << 16, b1 = ((uint32_t)w1[k]) << 16; + float v0, v1; + memcpy(&v0, &b0, 4); + memcpy(&v1, &b1, 4); + s0 += v0 * x[k]; + s1 += v1 * x[k]; + } + y[o] = s0; + y[o + 1] = s1; + } + + /* Single remaining row */ + for (; o < out_dim; o++) { + const uint16_t *w = W_bf16 + (size_t)o * in_dim; + __m512 acc = _mm512_setzero_ps(); + int k = 0; + for (; k + 16 <= in_dim; k += 16) { + __m256i raw = _mm256_loadu_si256((const __m256i *)(w + k)); + acc = _mm512_fmadd_ps( + _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(raw), 16)), + _mm512_loadu_ps(x + k), acc); + } + float sum = _mm512_reduce_add_ps(acc) + (bias ? bias[o] : 0.0f); + for (; k < in_dim; k++) { + uint32_t bits = ((uint32_t)w[k]) << 16; + float wv; + memcpy(&wv, &bits, 4); + sum += wv * x[k]; + } + y[o] = sum; + } +} + +# else /* AVX2 only (or AVX-512F without BW) */ + +/* Helper: bf16 to f32 for 8 elements */ +static inline __m256 bf16x8_to_f32(__m128i raw) { + return _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_cvtepu16_epi32(raw), 16)); +} + +static inline void bf16x16_to_f32(const uint16_t *src, __m256 *lo, __m256 *hi) { + __m256i raw = _mm256_loadu_si256((const __m256i *)src); + *lo = bf16x8_to_f32(_mm256_castsi256_si128(raw)); + *hi = bf16x8_to_f32(_mm256_extracti128_si256(raw, 1)); +} + +void bf16_matvec_fused_avx( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { + int o = 0; + + for (; o + 1 < out_dim; o += 2) { + const uint16_t *w0 = W_bf16 + (size_t)o * in_dim; + const uint16_t *w1 = w0 + in_dim; + __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps(); + __m256 a2 = _mm256_setzero_ps(), a3 = _mm256_setzero_ps(); + int k = 0; + for (; k + 16 <= in_dim; k += 16) { + __m256 xlo = _mm256_loadu_ps(x + k); + __m256 xhi = _mm256_loadu_ps(x + k + 8); + __m256 wlo, whi; + bf16x16_to_f32(w0 + k, &wlo, &whi); + a0 = _mm256_fmadd_ps(wlo, xlo, a0); + a1 = _mm256_fmadd_ps(whi, xhi, a1); + bf16x16_to_f32(w1 + k, &wlo, &whi); + a2 = _mm256_fmadd_ps(wlo, xlo, a2); + a3 = _mm256_fmadd_ps(whi, xhi, a3); + } + a0 = _mm256_add_ps(a0, a1); + a2 = _mm256_add_ps(a2, a3); + __m128 r0 = _mm_add_ps(_mm256_castps256_ps128(a0), _mm256_extractf128_ps(a0, 1)); + __m128 r1 = _mm_add_ps(_mm256_castps256_ps128(a2), _mm256_extractf128_ps(a2, 1)); + r0 = _mm_hadd_ps(r0, r1); + r0 = _mm_hadd_ps(r0, r0); + float s0 = _mm_cvtss_f32(r0) + (bias ? bias[o] : 0.0f); + float s1 = _mm_cvtss_f32(_mm_shuffle_ps(r0, r0, 1)) + (bias ? bias[o + 1] : 0.0f); + for (; k < in_dim; k++) { + uint32_t b0 = ((uint32_t)w0[k]) << 16, b1 = ((uint32_t)w1[k]) << 16; + float v0, v1; + memcpy(&v0, &b0, 4); + memcpy(&v1, &b1, 4); + s0 += v0 * x[k]; + s1 += v1 * x[k]; + } + y[o] = s0; + y[o + 1] = s1; + } + for (; o < out_dim; o++) { + const uint16_t *w = W_bf16 + (size_t)o * in_dim; + __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps(); + int k = 0; + for (; k + 16 <= in_dim; k += 16) { + __m256 wlo, whi; + bf16x16_to_f32(w + k, &wlo, &whi); + a0 = _mm256_fmadd_ps(wlo, _mm256_loadu_ps(x + k), a0); + a1 = _mm256_fmadd_ps(whi, _mm256_loadu_ps(x + k + 8), a1); + } + a0 = _mm256_add_ps(a0, a1); + __m128 r = _mm_add_ps(_mm256_castps256_ps128(a0), _mm256_extractf128_ps(a0, 1)); + r = _mm_hadd_ps(r, r); + r = _mm_hadd_ps(r, r); + float sum = _mm_cvtss_f32(r) + (bias ? bias[o] : 0.0f); + for (; k < in_dim; k++) { + uint32_t bits = ((uint32_t)w[k]) << 16; + float wv; + memcpy(&wv, &bits, 4); + sum += wv * x[k]; + } + y[o] = sum; + } +} + +# endif /* AVX-512F+BW vs AVX2 for bf16 */ + +#endif /* __AVX2__ && __FMA__ */ diff --git a/libffwd/bpe.c b/libffwd/bpe.c new file mode 100644 index 0000000..5a4d184 --- /dev/null +++ b/libffwd/bpe.c @@ -0,0 +1,1898 @@ +#include "bpe.h" + +#include <yyjson.h> + +#include <limits.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* GPT-2 bytes-to-unicode mapping */ + +static int gpt2_byte_to_unicode[256]; +static int gpt2_unicode_to_byte[512]; /* codepoints up to ~384 */ +static int gpt2_mapping_initialized = 0; + +static void init_gpt2_mapping(void) { + if (gpt2_mapping_initialized) + return; + + memset(gpt2_unicode_to_byte, -1, sizeof(gpt2_unicode_to_byte)); + + int n = 0; + for (int b = 0; b < 256; b++) { + int is_normal = 0; + if (b >= 33 && b <= 126) + is_normal = 1; /* '!'..'~' */ + if (b >= 161 && b <= 172) + is_normal = 1; /* '¡'..'¬' */ + if (b >= 174 && b <= 255) + is_normal = 1; /* '®'..'ÿ' */ + + if (is_normal) + gpt2_byte_to_unicode[b] = b; + else + gpt2_byte_to_unicode[b] = 256 + n++; + } + + for (int b = 0; b < 256; b++) { + int cp = gpt2_byte_to_unicode[b]; + if (cp < 512) + gpt2_unicode_to_byte[cp] = b; + } + + gpt2_mapping_initialized = 1; +} + +/* Convert one Unicode codepoint to UTF-8 bytes. Returns bytes written. */ +static int utf8_encode_cp(int cp, char out[4]) { + if (cp < 0x80) { + out[0] = (char)cp; + return 1; + } + if (cp < 0x800) { + out[0] = (char)(0xC0 | (cp >> 6)); + out[1] = (char)(0x80 | (cp & 0x3F)); + return 2; + } + out[0] = (char)(0xE0 | (cp >> 12)); + out[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); + out[2] = (char)(0x80 | (cp & 0x3F)); + return 3; +} + +/* + * Decode a GPT-2 encoded token string (vocab key) to raw bytes/UTF-8 text. + * Returns allocated string (caller must free). + */ +static char *decode_gpt2_token(const char *token_str) { + init_gpt2_mapping(); + + size_t len = strlen(token_str); + unsigned char *bytes = (unsigned char *)malloc(len + 1); + if (!bytes) + return NULL; + + int byte_count = 0; + const unsigned char *p = (const unsigned char *)token_str; + const unsigned char *end = p + len; + + while (p < end) { + int cp = 0; + int nbytes = 1; + + if ((*p & 0x80) == 0) { + cp = *p; + nbytes = 1; + } else if ((*p & 0xE0) == 0xC0) { + cp = (*p & 0x1F) << 6; + if (p + 1 < end) + cp |= (p[1] & 0x3F); + nbytes = 2; + } else if ((*p & 0xF0) == 0xE0) { + cp = (*p & 0x0F) << 12; + if (p + 1 < end) + cp |= (p[1] & 0x3F) << 6; + if (p + 2 < end) + cp |= (p[2] & 0x3F); + nbytes = 3; + } else if ((*p & 0xF8) == 0xF0) { + cp = (*p & 0x07) << 18; + if (p + 1 < end) + cp |= (p[1] & 0x3F) << 12; + if (p + 2 < end) + cp |= (p[2] & 0x3F) << 6; + if (p + 3 < end) + cp |= (p[3] & 0x3F); + nbytes = 4; + } else { + cp = *p; + nbytes = 1; + } + p += nbytes; + + if (cp < 512 && gpt2_unicode_to_byte[cp] >= 0) { + bytes[byte_count++] = (unsigned char)gpt2_unicode_to_byte[cp]; + } else { + bytes[byte_count++] = '?'; + } + } + bytes[byte_count] = '\0'; + return (char *)bytes; +} + +/* String->int hash map (open addressing) */ + +typedef struct { + char *key; /* NULL means empty slot */ + int value; +} str_int_entry_t; + +static uint64_t fnv1a_hash(const char *s) { + uint64_t h = 1469598103934665603ULL; + while (*s) { + h ^= (unsigned char)*s++; + h *= 1099511628211ULL; + } + return h; +} + +static uint64_t fnv1a_hash_pair(const char *a, const char *b) { + uint64_t h = 1469598103934665603ULL; + while (*a) { + h ^= (unsigned char)*a++; + h *= 1099511628211ULL; + } + h ^= (unsigned char)' '; + h *= 1099511628211ULL; + while (*b) { + h ^= (unsigned char)*b++; + h *= 1099511628211ULL; + } + return h; +} + +static int pair_key_eq(const char *key, const char *a, const char *b) { + while (*a) { + if (*key++ != *a++) + return 0; + } + if (*key++ != ' ') + return 0; + while (*b) { + if (*key++ != *b++) + return 0; + } + return *key == '\0'; +} + +static int next_pow2(int x) { + int p = 1; + while (p < x) + p <<= 1; + return p; +} + +static int map_insert(str_int_entry_t *map, int cap, char *key, int value) { + if (!map || cap <= 0 || !key) + return -1; + int mask = cap - 1; + int pos = (int)(fnv1a_hash(key) & (uint64_t)mask); + for (int i = 0; i < cap; i++) { + int idx = (pos + i) & mask; + if (!map[idx].key) { + map[idx].key = key; + map[idx].value = value; + return 0; + } + if (strcmp(map[idx].key, key) == 0) { + map[idx].value = value; + return 0; + } + } + return -1; +} + +static int map_insert_owned(str_int_entry_t *map, int cap, char **keyp, int value) { + if (!keyp) + return -1; + char *key = *keyp; + if (!map || cap <= 0 || !key) + return -1; + int mask = cap - 1; + int pos = (int)(fnv1a_hash(key) & (uint64_t)mask); + for (int i = 0; i < cap; i++) { + int idx = (pos + i) & mask; + if (!map[idx].key) { + map[idx].key = key; + map[idx].value = value; + *keyp = NULL; + return 0; + } + if (strcmp(map[idx].key, key) == 0) { + map[idx].value = value; + return 0; + } + } + return -1; +} + +static int map_get(const str_int_entry_t *map, int cap, const char *key) { + if (!map || cap <= 0 || !key) + return -1; + int mask = cap - 1; + int pos = (int)(fnv1a_hash(key) & (uint64_t)mask); + for (int i = 0; i < cap; i++) { + int idx = (pos + i) & mask; + if (!map[idx].key) + return -1; + if (strcmp(map[idx].key, key) == 0) + return map[idx].value; + } + return -1; +} + +static int map_get_pair(const str_int_entry_t *map, int cap, const char *a, const char *b) { + if (!map || cap <= 0 || !a || !b) + return -1; + int mask = cap - 1; + int pos = (int)(fnv1a_hash_pair(a, b) & (uint64_t)mask); + for (int i = 0; i < cap; i++) { + int idx = (pos + i) & mask; + if (!map[idx].key) + return -1; + if (pair_key_eq(map[idx].key, a, b)) + return map[idx].value; + } + return -1; +} + +/* BPE helpers */ + +static int utf8_char_len(unsigned char c) { + if ((c & 0x80) == 0) + return 1; + if ((c & 0xE0) == 0xC0) + return 2; + if ((c & 0xF0) == 0xE0) + return 3; + if ((c & 0xF8) == 0xF0) + return 4; + return 1; +} + +static int split_utf8_symbols(const char *s, char ***out_syms, int *out_n) { + *out_syms = NULL; + *out_n = 0; + if (!s || !*s) + return 0; + + int n = 0; + for (const unsigned char *p = (const unsigned char *)s; *p;) { + int l = utf8_char_len(*p); + p += l; + n++; + } + if (n <= 0) + return 0; + + char **syms = (char **)calloc((size_t)n, sizeof(char *)); + if (!syms) + return -1; + + int i = 0; + for (const unsigned char *p = (const unsigned char *)s; *p;) { + int l = utf8_char_len(*p); + syms[i] = (char *)malloc((size_t)l + 1); + if (!syms[i]) { + for (int j = 0; j < i; j++) + free(syms[j]); + free(syms); + return -1; + } + memcpy(syms[i], p, (size_t)l); + syms[i][l] = '\0'; + p += l; + i++; + } + + *out_syms = syms; + *out_n = n; + return 0; +} + +static char *str_concat2(const char *a, const char *b) { + size_t la = strlen(a), lb = strlen(b); + char *r = (char *)malloc(la + lb + 1); + if (!r) + return NULL; + memcpy(r, a, la); + memcpy(r + la, b, lb); + r[la + lb] = '\0'; + return r; +} + +/* Integer-pair BPE merges: (left_id, right_id) -> (rank, merged_id) */ + +typedef struct { + uint64_t key; /* (left_id << 32) | right_id; UINT64_MAX = empty */ + int rank; + int merged_id; +} pair_merge_entry_t; + +static uint64_t pair_hash(uint64_t k) { + /* splitmix64 finalizer */ + k ^= k >> 30; + k *= 0xbf58476d1ce4e5b9ULL; + k ^= k >> 27; + k *= 0x94d049bb133111ebULL; + k ^= k >> 31; + return k; +} + +static void pair_map_insert(pair_merge_entry_t *map, int cap, uint64_t key, int rank, int merged_id) { + int mask = cap - 1; + int idx = (int)(pair_hash(key) & (uint64_t)mask); + for (int probe = 0; probe < cap; probe++, idx = (idx + 1) & mask) { + if (map[idx].key == UINT64_MAX) { + map[idx].key = key; + map[idx].rank = rank; + map[idx].merged_id = merged_id; + return; + } + if (map[idx].key == key) + return; /* first (lowest) rank wins */ + } +} + +/* Returns the rank and sets *merged_id, or INT_MAX when the pair never + * merges. */ +static int pair_merge_lookup(const tok_bpe_t *tok, int left, int right, int *merged_id) { + const pair_merge_entry_t *map = (const pair_merge_entry_t *)tok->int_merges; + uint64_t key = ((uint64_t)(uint32_t)left << 32) | (uint32_t)right; + int mask = tok->int_merges_cap - 1; + int idx = (int)(pair_hash(key) & (uint64_t)mask); + for (int probe = 0; probe < tok->int_merges_cap; probe++, idx = (idx + 1) & mask) { + if (map[idx].key == UINT64_MAX) + return INT_MAX; + if (map[idx].key == key) { + *merged_id = map[idx].merged_id; + return map[idx].rank; + } + } + return INT_MAX; +} + +static int merge_rank(const tok_bpe_t *tok, const char *a, const char *b) { + if (!tok->merge_map || tok->merge_map_cap <= 0) + return INT_MAX; + + int rank = map_get_pair((const str_int_entry_t *)tok->merge_map, tok->merge_map_cap, a, b); + return rank >= 0 ? rank : INT_MAX; +} + +static int append_id(int **arr, int *n, int *cap, int id) { + if (*n >= *cap) { + int new_cap = (*cap == 0) ? 16 : (*cap * 2); + int *tmp = (int *)realloc(*arr, (size_t)new_cap * sizeof(int)); + if (!tmp) + return -1; + *arr = tmp; + *cap = new_cap; + } + (*arr)[(*n)++] = id; + return 0; +} + +/* Convert UTF-8 bytes to GPT-2 byte-level unicode string. */ +static char *text_to_bpe_unicode(const char *text) { + init_gpt2_mapping(); + size_t len = strlen(text); + /* Every input byte becomes one codepoint up to 3 UTF-8 bytes. */ + char *out = (char *)malloc(len * 3 + 1); + if (!out) + return NULL; + + size_t w = 0; + for (size_t i = 0; i < len; i++) { + int cp = gpt2_byte_to_unicode[(unsigned char)text[i]]; + char tmp[4]; + int n = utf8_encode_cp(cp, tmp); + for (int j = 0; j < n; j++) + out[w++] = tmp[j]; + } + out[w] = '\0'; + return out; +} + +static int encode_bpe_word(const tok_bpe_t *tok, const char *mapped, int **out_ids, int *out_n); + +static int +utf8_decode_cp_len(const unsigned char *p, const unsigned char *end, int *cp_out, int *len_out) { + if (p >= end || *p == '\0') + return 0; + + unsigned char c = *p; + if ((c & 0x80) == 0) { + *cp_out = c; + *len_out = 1; + return 1; + } + if ((c & 0xE0) == 0xC0 && p + 1 < end && (p[1] & 0xC0) == 0x80) { + *cp_out = ((c & 0x1F) << 6) | (p[1] & 0x3F); + *len_out = 2; + return 1; + } + if ((c & 0xF0) == 0xE0 && p + 2 < end && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80) { + *cp_out = ((c & 0x0F) << 12) | ((p[1] & 0x3F) << 6) | (p[2] & 0x3F); + *len_out = 3; + return 1; + } + if ((c & 0xF8) == 0xF0 && p + 3 < end && (p[1] & 0xC0) == 0x80 && (p[2] & 0xC0) == 0x80 && + (p[3] & 0xC0) == 0x80) { + *cp_out = ((c & 0x07) << 18) | ((p[1] & 0x3F) << 12) | ((p[2] & 0x3F) << 6) | (p[3] & 0x3F); + *len_out = 4; + return 1; + } + + *cp_out = c; + *len_out = 1; + return 1; +} + +/* The pretokenizer classifies every codepoint, several times each, and the + * input is overwhelmingly ASCII; one table lookup replaces the range chains + * for cp < 0x80. Non-ASCII codepoints fall through to the full checks. + * ASCII has no symbolish codepoints, so that class needs no bit. */ +#define CPC_SPACE 1 +#define CPC_NUMBER 2 +#define CPC_LETTER 4 + +static const unsigned char ascii_cp_class[128] = { + ['\t'] = CPC_SPACE, ['\n'] = CPC_SPACE, ['\v'] = CPC_SPACE, + ['\f'] = CPC_SPACE, ['\r'] = CPC_SPACE, [' '] = CPC_SPACE, + ['0' ... '9'] = CPC_NUMBER, ['A' ... 'Z'] = CPC_LETTER, ['a' ... 'z'] = CPC_LETTER, +}; + +static int is_number_cp(int cp) { + if ((unsigned)cp < 0x80) + return ascii_cp_class[cp] & CPC_NUMBER; + if (cp >= 0x0660 && cp <= 0x0669) + return 1; + if (cp >= 0x06F0 && cp <= 0x06F9) + return 1; + if (cp >= 0x0966 && cp <= 0x096F) + return 1; + if (cp >= 0xFF10 && cp <= 0xFF19) + return 1; + return 0; +} + +static int is_space_cp(int cp) { + if ((unsigned)cp < 0x80) + return ascii_cp_class[cp] & CPC_SPACE; + return cp == 0x85 || cp == 0xA0 || cp == 0x1680 || (cp >= 0x2000 && cp <= 0x200A) || + cp == 0x2028 || cp == 0x2029 || cp == 0x202F || cp == 0x205F || cp == 0x3000; +} + +static int is_symbolish_cp(int cp) { + if (cp < 0x80) + return 0; + if (cp >= 0x0300 && cp <= 0x036F) + return 1; + if (cp >= 0x00A1 && cp <= 0x00BF && cp != 0x00AA && cp != 0x00B5 && cp != 0x00BA) + return 1; + if (cp == 0x00D7 || cp == 0x00F7) + return 1; + if (cp >= 0x064B && cp <= 0x065F) + return 1; + if ((cp >= 0x0900 && cp <= 0x0903) || (cp >= 0x093A && cp <= 0x094F) || + (cp >= 0x0951 && cp <= 0x0957) || (cp >= 0x0962 && cp <= 0x0963)) + return 1; + if (cp >= 0x2000 && cp <= 0x206F) + return 1; + if (cp >= 0x2190 && cp <= 0x27BF) + return 1; + if (cp >= 0x2E00 && cp <= 0x2E7F) + return 1; + if (cp >= 0x3000 && cp <= 0x303F) + return 1; + if (cp >= 0x1F000 && cp <= 0x1FAFF) + return 1; + return 0; +} + +static int compose_latin_mark(int base, int mark) { + switch (mark) { + case 0x0300: + switch (base) { + case 'A': + return 0x00C0; + case 'E': + return 0x00C8; + case 'I': + return 0x00CC; + case 'O': + return 0x00D2; + case 'U': + return 0x00D9; + case 'a': + return 0x00E0; + case 'e': + return 0x00E8; + case 'i': + return 0x00EC; + case 'o': + return 0x00F2; + case 'u': + return 0x00F9; + } + break; + case 0x0301: + switch (base) { + case 'A': + return 0x00C1; + case 'C': + return 0x0106; + case 'E': + return 0x00C9; + case 'I': + return 0x00CD; + case 'L': + return 0x0139; + case 'N': + return 0x0143; + case 'O': + return 0x00D3; + case 'R': + return 0x0154; + case 'S': + return 0x015A; + case 'U': + return 0x00DA; + case 'Y': + return 0x00DD; + case 'Z': + return 0x0179; + case 'a': + return 0x00E1; + case 'c': + return 0x0107; + case 'e': + return 0x00E9; + case 'i': + return 0x00ED; + case 'l': + return 0x013A; + case 'n': + return 0x0144; + case 'o': + return 0x00F3; + case 'r': + return 0x0155; + case 's': + return 0x015B; + case 'u': + return 0x00FA; + case 'y': + return 0x00FD; + case 'z': + return 0x017A; + } + break; + case 0x0302: + switch (base) { + case 'A': + return 0x00C2; + case 'E': + return 0x00CA; + case 'I': + return 0x00CE; + case 'O': + return 0x00D4; + case 'U': + return 0x00DB; + case 'a': + return 0x00E2; + case 'e': + return 0x00EA; + case 'i': + return 0x00EE; + case 'o': + return 0x00F4; + case 'u': + return 0x00FB; + } + break; + case 0x0303: + switch (base) { + case 'A': + return 0x00C3; + case 'N': + return 0x00D1; + case 'O': + return 0x00D5; + case 'a': + return 0x00E3; + case 'n': + return 0x00F1; + case 'o': + return 0x00F5; + } + break; + case 0x0308: + switch (base) { + case 'A': + return 0x00C4; + case 'E': + return 0x00CB; + case 'I': + return 0x00CF; + case 'O': + return 0x00D6; + case 'U': + return 0x00DC; + case 'Y': + return 0x0178; + case 'a': + return 0x00E4; + case 'e': + return 0x00EB; + case 'i': + return 0x00EF; + case 'o': + return 0x00F6; + case 'u': + return 0x00FC; + case 'y': + return 0x00FF; + } + break; + case 0x030A: + switch (base) { + case 'A': + return 0x00C5; + case 'a': + return 0x00E5; + } + break; + case 0x0327: + switch (base) { + case 'C': + return 0x00C7; + case 'c': + return 0x00E7; + } + break; + } + return 0; +} + +static char *normalize_nfc_latin(const char *text) { + const unsigned char *p = (const unsigned char *)text; + const unsigned char *end = p + strlen(text); + char *out = (char *)malloc((size_t)(end - p) * 2 + 1); + if (!out) + return NULL; + + size_t w = 0; + while (p < end) { + const unsigned char *cur = p; + int cp = 0, len = 0; + if (!utf8_decode_cp_len(p, end, &cp, &len)) + break; + p += len; + + if (p < end) { + int mark = 0, mark_len = 0; + utf8_decode_cp_len(p, end, &mark, &mark_len); + int composed = compose_latin_mark(cp, mark); + if (composed) { + char tmp[4]; + int n = utf8_encode_cp(composed, tmp); + for (int i = 0; i < n; i++) + out[w++] = tmp[i]; + p += mark_len; + continue; + } + } + + memcpy(out + w, cur, (size_t)len); + w += (size_t)len; + } + + out[w] = '\0'; + return out; +} + +static int is_letter_cp(int cp) { + if ((unsigned)cp < 0x80) + return ascii_cp_class[cp] & CPC_LETTER; + if (is_space_cp(cp) || is_number_cp(cp) || is_symbolish_cp(cp)) + return 0; + return 1; +} + +static int is_word_cp(int cp) { return is_letter_cp(cp) || is_number_cp(cp); } + +static int is_newline_cp(int cp) { return cp == '\n' || cp == '\r'; } + +static int ascii_lower(int c) { return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; } + +static int match_contraction(const unsigned char *p, const unsigned char *end) { + if (p >= end || *p != '\'') + return 0; + int remain = (int)(end - p); + if (remain >= 2) { + int c1 = ascii_lower(p[1]); + if (c1 == 's' || c1 == 't' || c1 == 'm' || c1 == 'd') + return 2; + } + if (remain >= 3) { + int c1 = ascii_lower(p[1]); + int c2 = ascii_lower(p[2]); + if ((c1 == 'r' && c2 == 'e') || (c1 == 'v' && c2 == 'e') || (c1 == 'l' && c2 == 'l')) + return 3; + } + return 0; +} + +static int append_encoded_piece( + const tok_bpe_t *tok, const char *piece, size_t piece_len, int **ids, int *n_ids, int *cap) { + if (piece_len == 0) + return 0; + + char *tmp = (char *)malloc(piece_len + 1); + if (!tmp) + return -1; + memcpy(tmp, piece, piece_len); + tmp[piece_len] = '\0'; + + char *mapped = text_to_bpe_unicode(tmp); + free(tmp); + if (!mapped) + return -1; + + int *piece_ids = NULL; + int n_piece = 0; + if (encode_bpe_word(tok, mapped, &piece_ids, &n_piece) != 0) { + free(mapped); + free(piece_ids); + return -1; + } + free(mapped); + + for (int i = 0; i < n_piece; i++) { + if (append_id(ids, n_ids, cap, piece_ids[i]) != 0) { + free(piece_ids); + return -1; + } + } + free(piece_ids); + return 0; +} + +static int encode_pretokenized(const tok_bpe_t *tok, const char *text, int **out_ids, int *out_n) { + const unsigned char *start = (const unsigned char *)text; + const unsigned char *p = start; + const unsigned char *end = start + strlen(text); + int *ids = NULL; + int n_ids = 0, cap = 0; + + while (p < end) { + const unsigned char *piece = p; + int cp = 0, len = 0; + if (!utf8_decode_cp_len(p, end, &cp, &len)) + break; + + int clen = match_contraction(p, end); + if (clen > 0) { + p += clen; + } else { + const unsigned char *next = p + len; + int next_cp = 0, next_len = 0; + int has_next = utf8_decode_cp_len(next, end, &next_cp, &next_len); + + if (!is_newline_cp(cp) && !is_word_cp(cp) && has_next && is_letter_cp(next_cp)) { + p = next + next_len; + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (!is_letter_cp(c)) + break; + p += l; + } + } else if (is_letter_cp(cp)) { + p += len; + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (!is_letter_cp(c)) + break; + p += l; + } + } else if (is_number_cp(cp)) { + p += len; + } else if (is_space_cp(cp)) { + const unsigned char *q = p; + int saw_newline = 0; + while (q < end) { + int c = 0, l = 0; + utf8_decode_cp_len(q, end, &c, &l); + if (!is_space_cp(c)) + break; + if (is_newline_cp(c)) + saw_newline = 1; + q += l; + if (saw_newline) { + while (q < end) { + int c2 = 0, l2 = 0; + utf8_decode_cp_len(q, end, &c2, &l2); + if (!is_newline_cp(c2)) + break; + q += l2; + } + break; + } + } + + if (saw_newline) { + p = q; + } else if (cp == ' ' && has_next && !is_space_cp(next_cp) && !is_word_cp(next_cp)) { + p = next + next_len; + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (is_space_cp(c) || is_word_cp(c)) + break; + p += l; + } + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (!is_newline_cp(c)) + break; + p += l; + } + } else { + const unsigned char *last = p; + const unsigned char *q2 = p; + int count = 0; + while (q2 < end) { + int c = 0, l = 0; + utf8_decode_cp_len(q2, end, &c, &l); + if (!is_space_cp(c)) + break; + last = q2; + q2 += l; + count++; + } + if (q2 < end && count > 1) + p = last; + else + p = q2; + } + } else { + p += len; + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (is_space_cp(c) || is_word_cp(c)) + break; + p += l; + } + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (!is_newline_cp(c)) + break; + p += l; + } + } + } + + if (append_encoded_piece(tok, (const char *)piece, (size_t)(p - piece), &ids, &n_ids, &cap) != + 0) { + free(ids); + return -1; + } + } + + *out_ids = ids; + *out_n = n_ids; + return 0; +} + +/* Encode one mapped BPE unicode string to token IDs. */ +static int encode_bpe_word(const tok_bpe_t *tok, const char *mapped, int **out_ids, int *out_n) { + *out_ids = NULL; + *out_n = 0; + if (!mapped || !*mapped) + return 0; + + char **syms = NULL; + int n_syms = 0; + if (split_utf8_symbols(mapped, &syms, &n_syms) != 0) + return -1; + if (n_syms <= 0) { + free(syms); + return 0; + } + + while (n_syms > 1) { + int best_rank = INT_MAX; + int best_i = -1; + for (int i = 0; i < n_syms - 1; i++) { + int r = merge_rank(tok, syms[i], syms[i + 1]); + if (r < best_rank) { + best_rank = r; + best_i = i; + } + } + if (best_i < 0 || best_rank == INT_MAX) + break; + + char *merged = str_concat2(syms[best_i], syms[best_i + 1]); + if (!merged) { + for (int i = 0; i < n_syms; i++) + free(syms[i]); + free(syms); + return -1; + } + free(syms[best_i]); + free(syms[best_i + 1]); + syms[best_i] = merged; + for (int j = best_i + 1; j < n_syms - 1; j++) + syms[j] = syms[j + 1]; + n_syms--; + } + + int *ids = NULL; + int n_ids = 0, cap = 0; + for (int i = 0; i < n_syms; i++) { + int id = map_get((const str_int_entry_t *)tok->vocab_map, tok->vocab_map_cap, syms[i]); + if (id < 0) { + /* Should not happen with valid vocab + merges + byte-level mapping. */ + for (int k = 0; k < n_syms; k++) + free(syms[k]); + free(syms); + free(ids); + return -1; + } + if (append_id(&ids, &n_ids, &cap, id) != 0) { + for (int k = 0; k < n_syms; k++) + free(syms[k]); + free(syms); + free(ids); + return -1; + } + } + + for (int i = 0; i < n_syms; i++) + free(syms[i]); + free(syms); + + *out_ids = ids; + *out_n = n_ids; + return 0; +} + +typedef struct { + size_t off; +} bpe_sym_ref_t; + +struct tok_bpe_workspace { + char *normalized; + size_t normalized_cap; + char *mapped; + size_t mapped_cap; + char *arena; + size_t arena_cap; + size_t arena_len; + bpe_sym_ref_t *syms; + int syms_cap; + int *ids; + int ids_cap; + /* Integer-pair BPE merge scratch: token id, doubly-linked live order, + * and the cached (rank, merged id) of the pair starting at each live + * position. */ + int *bpe_ids; + int *bpe_prv; + int *bpe_nxt; + int *bpe_rnk; + int *bpe_mrg; + int bpe_cap; +}; + +static int reserve_bpe(tok_bpe_workspace_t *ws, int need) { + if (ws->bpe_cap >= need) + return 0; + int new_cap = ws->bpe_cap ? ws->bpe_cap : 64; + while (new_cap < need) { + if (new_cap > INT_MAX / 2) + return -1; + new_cap *= 2; + } + int *p; +#define R(field) \ + p = (int *)realloc(ws->field, (size_t)new_cap * sizeof(int)); \ + if (!p) \ + return -1; \ + ws->field = p; + R(bpe_ids) + R(bpe_prv) + R(bpe_nxt) + R(bpe_rnk) + R(bpe_mrg) +#undef R + ws->bpe_cap = new_cap; + return 0; +} + +static int reserve_bytes(char **buf, size_t *cap, size_t need) { + if (*cap >= need) + return 0; + size_t new_cap = *cap ? *cap : 256; + while (new_cap < need) { + if (new_cap > SIZE_MAX / 2) + return -1; + new_cap *= 2; + } + char *p = (char *)realloc(*buf, new_cap); + if (!p) + return -1; + *buf = p; + *cap = new_cap; + return 0; +} + +static int reserve_ints(int **buf, int *cap, int need) { + if (*cap >= need) + return 0; + int new_cap = *cap ? *cap : 64; + while (new_cap < need) { + if (new_cap > INT_MAX / 2) + return -1; + new_cap *= 2; + } + int *p = (int *)realloc(*buf, (size_t)new_cap * sizeof(int)); + if (!p) + return -1; + *buf = p; + *cap = new_cap; + return 0; +} + +static int reserve_syms(tok_bpe_workspace_t *ws, int need) { + if (ws->syms_cap >= need) + return 0; + int new_cap = ws->syms_cap ? ws->syms_cap : 64; + while (new_cap < need) { + if (new_cap > INT_MAX / 2) + return -1; + new_cap *= 2; + } + bpe_sym_ref_t *p = (bpe_sym_ref_t *)realloc(ws->syms, (size_t)new_cap * sizeof(*ws->syms)); + if (!p) + return -1; + ws->syms = p; + ws->syms_cap = new_cap; + return 0; +} + +static int arena_append(tok_bpe_workspace_t *ws, const char *s, size_t len, size_t *off) { + if (ws->arena_len > SIZE_MAX - len - 1) + return -1; + size_t need = ws->arena_len + len + 1; + if (reserve_bytes(&ws->arena, &ws->arena_cap, need) != 0) + return -1; + *off = ws->arena_len; + memcpy(ws->arena + ws->arena_len, s, len); + ws->arena_len += len; + ws->arena[ws->arena_len++] = '\0'; + return 0; +} + +static const char *arena_str(const tok_bpe_workspace_t *ws, bpe_sym_ref_t sym) { + return ws->arena + sym.off; +} + +static int normalize_nfc_latin_into(tok_bpe_workspace_t *ws, const char *text, const char **out) { + const unsigned char *p = (const unsigned char *)text; + const unsigned char *end = p + strlen(text); + size_t in_len = (size_t)(end - p); + if (in_len > (SIZE_MAX - 1) / 2) + return -1; + if (reserve_bytes(&ws->normalized, &ws->normalized_cap, in_len * 2 + 1) != 0) + return -1; + + size_t w = 0; + while (p < end) { + const unsigned char *cur = p; + int cp = 0, len = 0; + if (!utf8_decode_cp_len(p, end, &cp, &len)) + break; + p += len; + + if (p < end) { + int mark = 0, mark_len = 0; + utf8_decode_cp_len(p, end, &mark, &mark_len); + int composed = compose_latin_mark(cp, mark); + if (composed) { + char tmp[4]; + int n = utf8_encode_cp(composed, tmp); + memcpy(ws->normalized + w, tmp, (size_t)n); + w += (size_t)n; + p += mark_len; + continue; + } + } + + memcpy(ws->normalized + w, cur, (size_t)len); + w += (size_t)len; + } + + ws->normalized[w] = '\0'; + *out = ws->normalized; + return 0; +} + +static int +text_to_bpe_unicode_into(tok_bpe_workspace_t *ws, const char *text, size_t len, const char **out) { + init_gpt2_mapping(); + if (len > (SIZE_MAX - 1) / 3) + return -1; + if (reserve_bytes(&ws->mapped, &ws->mapped_cap, len * 3 + 1) != 0) + return -1; + + size_t w = 0; + for (size_t i = 0; i < len; i++) { + int cp = gpt2_byte_to_unicode[(unsigned char)text[i]]; + char tmp[4]; + int n = utf8_encode_cp(cp, tmp); + memcpy(ws->mapped + w, tmp, (size_t)n); + w += (size_t)n; + } + ws->mapped[w] = '\0'; + *out = ws->mapped; + return 0; +} + +static int output_append_id(int *out_ids, int out_cap, int *n_ids, int *overflow, int id) { + if (*n_ids < out_cap && out_ids) + out_ids[*n_ids] = id; + else + *overflow = 1; + (*n_ids)++; + return 0; +} + +static int encode_bpe_word_into_slow(const tok_bpe_t *tok, + tok_bpe_workspace_t *ws, + const char *mapped, + int *out_ids, + int out_cap, + int *n_ids, + int *overflow) { + if (!mapped || !*mapped) + return 0; + + int n_syms = 0; + for (const unsigned char *p = (const unsigned char *)mapped; *p;) { + int l = utf8_char_len(*p); + p += l; + n_syms++; + } + if (n_syms <= 0) + return 0; + if (reserve_syms(ws, n_syms) != 0) + return -1; + + ws->arena_len = 0; + int i = 0; + for (const unsigned char *p = (const unsigned char *)mapped; *p;) { + int l = utf8_char_len(*p); + size_t off = 0; + if (arena_append(ws, (const char *)p, (size_t)l, &off) != 0) + return -1; + ws->syms[i++].off = off; + p += l; + } + + while (n_syms > 1) { + int best_rank = INT_MAX; + int best_i = -1; + for (int j = 0; j < n_syms - 1; j++) { + int r = merge_rank(tok, arena_str(ws, ws->syms[j]), arena_str(ws, ws->syms[j + 1])); + if (r < best_rank) { + best_rank = r; + best_i = j; + } + } + if (best_i < 0 || best_rank == INT_MAX) + break; + + const char *a = arena_str(ws, ws->syms[best_i]); + const char *b = arena_str(ws, ws->syms[best_i + 1]); + size_t la = strlen(a), lb = strlen(b); + if (la > SIZE_MAX - lb) + return -1; + if (reserve_bytes(&ws->arena, &ws->arena_cap, ws->arena_len + la + lb + 1) != 0) + return -1; + + a = arena_str(ws, ws->syms[best_i]); + b = arena_str(ws, ws->syms[best_i + 1]); + size_t off = ws->arena_len; + memcpy(ws->arena + ws->arena_len, a, la); + ws->arena_len += la; + memcpy(ws->arena + ws->arena_len, b, lb); + ws->arena_len += lb; + ws->arena[ws->arena_len++] = '\0'; + ws->syms[best_i].off = off; + for (int j = best_i + 1; j < n_syms - 1; j++) + ws->syms[j] = ws->syms[j + 1]; + n_syms--; + } + + for (int j = 0; j < n_syms; j++) { + int id = map_get((const str_int_entry_t *)tok->vocab_map, tok->vocab_map_cap, + arena_str(ws, ws->syms[j])); + if (id < 0) + return -1; + output_append_id(out_ids, out_cap, n_ids, overflow, id); + } + return 0; +} + +/* Integer-pair BPE merge. + * Uses token ids in a doubly-linked array with cached pair ranks. + * Each merge updates only the two adjacent pairs. + * + * Falls back to the string-based loop when int tables are unavailable. + * + * Output is identical: initial ids are byte-level single-char token ids, and + * each merge result is the vocab id of the concatenated pair. */ +static int encode_bpe_word_into(const tok_bpe_t *tok, + tok_bpe_workspace_t *ws, + const char *mapped, + int *out_ids, + int out_cap, + int *n_ids, + int *overflow) { + if (!mapped || !*mapped) + return 0; + if (!tok->int_merges || !tok->cp_to_id) + return encode_bpe_word_into_slow(tok, ws, mapped, out_ids, out_cap, n_ids, overflow); + + int n = 0; + for (const unsigned char *p = (const unsigned char *)mapped; *p;) { + p += utf8_char_len(*p); + n++; + } + if (n <= 0) + return 0; + if (reserve_bpe(ws, n) != 0) + return -1; + + int *ids = ws->bpe_ids, *prv = ws->bpe_prv, *nxt = ws->bpe_nxt; + int *rnk = ws->bpe_rnk, *mrg = ws->bpe_mrg; + + int i = 0; + for (const unsigned char *p = (const unsigned char *)mapped; *p;) { + int len = utf8_char_len(*p); + int cp; + if (len == 1) + cp = *p; + else if (len == 2) + cp = ((*p & 0x1F) << 6) | (p[1] & 0x3F); + else + cp = 512; /* byte-level alphabet never exceeds two bytes */ + int id = cp < 512 ? tok->cp_to_id[cp] : -1; + if (id < 0) + return encode_bpe_word_into_slow(tok, ws, mapped, out_ids, out_cap, n_ids, overflow); + ids[i] = id; + prv[i] = i - 1; + nxt[i] = i + 1 < n ? i + 1 : -1; + i++; + p += len; + } + + for (int j = 0; j < n - 1; j++) + rnk[j] = pair_merge_lookup(tok, ids[j], ids[j + 1], &mrg[j]); + if (n > 0) + rnk[n - 1] = INT_MAX; + + for (;;) { + int best = -1, best_rank = INT_MAX; + for (int j = 0; j != -1 && nxt[j] != -1; j = nxt[j]) { + if (rnk[j] < best_rank) { + best_rank = rnk[j]; + best = j; + } + } + if (best < 0) + break; + + ids[best] = mrg[best]; + int dead = nxt[best]; + nxt[best] = nxt[dead]; + if (nxt[dead] != -1) + prv[nxt[dead]] = best; + + if (prv[best] != -1) + rnk[prv[best]] = pair_merge_lookup(tok, ids[prv[best]], ids[best], &mrg[prv[best]]); + rnk[best] = + nxt[best] != -1 ? pair_merge_lookup(tok, ids[best], ids[nxt[best]], &mrg[best]) : INT_MAX; + } + + for (int j = 0; j != -1; j = nxt[j]) + output_append_id(out_ids, out_cap, n_ids, overflow, ids[j]); + return 0; +} + +static int append_encoded_piece_into(const tok_bpe_t *tok, + tok_bpe_workspace_t *ws, + const char *piece, + size_t piece_len, + int *out_ids, + int out_cap, + int *n_ids, + int *overflow) { + if (piece_len == 0) + return 0; + + const char *mapped = NULL; + if (text_to_bpe_unicode_into(ws, piece, piece_len, &mapped) != 0) + return -1; + return encode_bpe_word_into(tok, ws, mapped, out_ids, out_cap, n_ids, overflow); +} + +static int encode_pretokenized_into(const tok_bpe_t *tok, + tok_bpe_workspace_t *ws, + const char *text, + int *out_ids, + int out_cap, + int *out_n) { + const unsigned char *start = (const unsigned char *)text; + const unsigned char *p = start; + const unsigned char *end = start + strlen(text); + int n_ids = 0; + int overflow = 0; + + while (p < end) { + const unsigned char *piece = p; + int cp = 0, len = 0; + if (!utf8_decode_cp_len(p, end, &cp, &len)) + break; + + int clen = match_contraction(p, end); + if (clen > 0) { + p += clen; + } else { + const unsigned char *next = p + len; + int next_cp = 0, next_len = 0; + int has_next = utf8_decode_cp_len(next, end, &next_cp, &next_len); + + if (!is_newline_cp(cp) && !is_word_cp(cp) && has_next && is_letter_cp(next_cp)) { + p = next + next_len; + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (!is_letter_cp(c)) + break; + p += l; + } + } else if (is_letter_cp(cp)) { + p += len; + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (!is_letter_cp(c)) + break; + p += l; + } + } else if (is_number_cp(cp)) { + p += len; + } else if (is_space_cp(cp)) { + const unsigned char *q = p; + int saw_newline = 0; + while (q < end) { + int c = 0, l = 0; + utf8_decode_cp_len(q, end, &c, &l); + if (!is_space_cp(c)) + break; + if (is_newline_cp(c)) + saw_newline = 1; + q += l; + if (saw_newline) { + while (q < end) { + int c2 = 0, l2 = 0; + utf8_decode_cp_len(q, end, &c2, &l2); + if (!is_newline_cp(c2)) + break; + q += l2; + } + break; + } + } + + if (saw_newline) { + p = q; + } else if (cp == ' ' && has_next && !is_space_cp(next_cp) && !is_word_cp(next_cp)) { + p = next + next_len; + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (is_space_cp(c) || is_word_cp(c)) + break; + p += l; + } + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (!is_newline_cp(c)) + break; + p += l; + } + } else { + const unsigned char *last = p; + const unsigned char *q2 = p; + int count = 0; + while (q2 < end) { + int c = 0, l = 0; + utf8_decode_cp_len(q2, end, &c, &l); + if (!is_space_cp(c)) + break; + last = q2; + q2 += l; + count++; + } + if (q2 < end && count > 1) + p = last; + else + p = q2; + } + } else { + p += len; + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (is_space_cp(c) || is_word_cp(c)) + break; + p += l; + } + while (p < end) { + int c = 0, l = 0; + utf8_decode_cp_len(p, end, &c, &l); + if (!is_newline_cp(c)) + break; + p += l; + } + } + } + + if (append_encoded_piece_into(tok, ws, (const char *)piece, (size_t)(p - piece), out_ids, + out_cap, &n_ids, &overflow) != 0) + return -1; + } + + *out_n = n_ids; + return overflow ? -2 : 0; +} + +static int derive_merges_path(const char *vocab_path, char *out_path, size_t out_cap) { + const char *slash = strrchr(vocab_path, '/'); + if (!slash) { + if (snprintf(out_path, out_cap, "merges.txt") >= (int)out_cap) + return -1; + return 0; + } + size_t dir_len = (size_t)(slash - vocab_path); + if (dir_len + 12 >= out_cap) + return -1; + memcpy(out_path, vocab_path, dir_len); + out_path[dir_len] = '\0'; + snprintf(out_path + dir_len, out_cap - dir_len, "/merges.txt"); + return 0; +} + +static void trim_newline(char *line) { + size_t n = strlen(line); + while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r')) + line[--n] = '\0'; +} + +static int parse_merge_pair(char *line, char **a, char **b) { + *a = NULL; + *b = NULL; + trim_newline(line); + if (line[0] == '\0') + return -1; + if (line[0] == '#') + return -1; + + char *sp = strchr(line, ' '); + if (!sp) + return -1; + *sp = '\0'; + char *p2 = sp + 1; + while (*p2 == ' ') + p2++; + if (*line == '\0' || *p2 == '\0') + return -1; + *a = line; + *b = p2; + return 0; +} + +static int load_merges_map(tok_bpe_t *tok, const char *merges_path) { + FILE *f = fopen(merges_path, "rb"); + if (!f) + return -1; + + int n_pairs = 0; + char line[8192]; + while (fgets(line, sizeof(line), f)) { + char *a = NULL, *b = NULL; + if (parse_merge_pair(line, &a, &b) == 0) + n_pairs++; + } + if (n_pairs <= 0) { + fclose(f); + return -1; + } + + tok->merge_map_cap = next_pow2(n_pairs * 2); + tok->merge_map = calloc((size_t)tok->merge_map_cap, sizeof(str_int_entry_t)); + if (!tok->merge_map) { + fclose(f); + tok->merge_map_cap = 0; + return -1; + } + + if (fseek(f, 0, SEEK_SET) != 0) { + fclose(f); + return -1; + } + tok->int_merges_cap = next_pow2(n_pairs * 2); + pair_merge_entry_t *pm = + (pair_merge_entry_t *)calloc((size_t)tok->int_merges_cap, sizeof(pair_merge_entry_t)); + if (pm) { + for (int i = 0; i < tok->int_merges_cap; i++) { + pm[i].key = UINT64_MAX; + pm[i].rank = INT_MAX; + pm[i].merged_id = -1; + } + } + tok->int_merges = pm; + + int rank = 0; + while (fgets(line, sizeof(line), f)) { + char *a = NULL, *b = NULL; + if (parse_merge_pair(line, &a, &b) != 0) + continue; + + size_t la = strlen(a), lb = strlen(b); + char *key = (char *)malloc(la + 1 + lb + 1); + if (!key) + continue; + memcpy(key, a, la); + key[la] = ' '; + memcpy(key + la + 1, b, lb); + key[la + 1 + lb] = '\0'; + + if (tok->int_merges) { + /* Resolve (a, b) and the merged token a+b to vocab ids; if any + * merge is unresolvable, drop the whole int table and keep the + * string-based loop as the only path. */ + const str_int_entry_t *vm = (const str_int_entry_t *)tok->vocab_map; + int left = map_get(vm, tok->vocab_map_cap, a); + int right = map_get(vm, tok->vocab_map_cap, b); + char ab[8192]; + int merged = -1; + if (la + lb < sizeof(ab)) { + memcpy(ab, a, la); + memcpy(ab + la, b, lb); + ab[la + lb] = '\0'; + merged = map_get(vm, tok->vocab_map_cap, ab); + } + if (left < 0 || right < 0 || merged < 0) { + free(tok->int_merges); + tok->int_merges = NULL; + tok->int_merges_cap = 0; + } else { + pair_map_insert((pair_merge_entry_t *)tok->int_merges, tok->int_merges_cap, + ((uint64_t)(uint32_t)left << 32) | (uint32_t)right, rank, merged); + } + } + (void)map_insert_owned((str_int_entry_t *)tok->merge_map, tok->merge_map_cap, &key, rank); + free(key); + rank++; + } + fclose(f); + + /* Byte-level unicode codepoint -> single-char token id table. */ + if (tok->int_merges) { + init_gpt2_mapping(); + tok->cp_to_id = (int *)malloc(512 * sizeof(int)); + if (tok->cp_to_id) { + for (int i = 0; i < 512; i++) + tok->cp_to_id[i] = -1; + for (int byte = 0; byte < 256 && tok->cp_to_id; byte++) { + int cp = gpt2_byte_to_unicode[byte]; + char buf[4]; + int n; + if (cp < 0x80) { + buf[0] = (char)cp; + n = 1; + } else { + buf[0] = (char)(0xC0 | (cp >> 6)); + buf[1] = (char)(0x80 | (cp & 0x3F)); + n = 2; + } + buf[n] = '\0'; + int id = map_get((const str_int_entry_t *)tok->vocab_map, tok->vocab_map_cap, buf); + if (id < 0) { + free(tok->cp_to_id); + tok->cp_to_id = NULL; + } else { + tok->cp_to_id[cp] = id; + } + } + } + } + return 0; +} + +/* Public API */ + +tok_bpe_t *tok_bpe_load(const char *vocab_json_path) { + FILE *f = fopen(vocab_json_path, "rb"); + if (!f) { + fprintf(stderr, "tok_bpe_load: cannot open %s\n", vocab_json_path); + return NULL; + } + + if (fseek(f, 0, SEEK_END) != 0) { + fclose(f); + return NULL; + } + long file_size = ftell(f); + if (file_size < 0 || fseek(f, 0, SEEK_SET) != 0) { + fclose(f); + return NULL; + } + + char *json = (char *)malloc((size_t)file_size + 1); + if (!json || fread(json, 1, (size_t)file_size, f) != (size_t)file_size) { + fclose(f); + free(json); + return NULL; + } + fclose(f); + json[file_size] = '\0'; + + yyjson_doc *doc = yyjson_read(json, (size_t)file_size, 0); + yyjson_val *root = yyjson_doc_get_root(doc); + if (!yyjson_is_obj(root)) { + yyjson_doc_free(doc); + free(json); + return NULL; + } + + /* vocab.json is a flat { token: id } object. + * Walk it once to find the max id and size the tables, then again to fill them. + * yyjson unescapes each key, including \uXXXX, to the same byte-level GPT-2 form + * the old scanner produced. + * decode_gpt2_token then maps that to the raw token text. */ + int max_id = 0; + size_t idx, n; + yyjson_val *key, *val; + yyjson_obj_foreach(root, idx, n, key, val) { + int64_t id = yyjson_get_int(val); + if (id > max_id) + max_id = (int)id; + } + + int vocab_size = max_id + 1; + tok_bpe_t *tok = (tok_bpe_t *)calloc(1, sizeof(tok_bpe_t)); + if (!tok) { + yyjson_doc_free(doc); + free(json); + return NULL; + } + tok->vocab_size = vocab_size; + tok->id_to_text = (char **)calloc((size_t)vocab_size, sizeof(char *)); + tok->id_to_bpe = (char **)calloc((size_t)vocab_size, sizeof(char *)); + if (!tok->id_to_text || !tok->id_to_bpe) { + tok_bpe_free(tok); + yyjson_doc_free(doc); + free(json); + return NULL; + } + + yyjson_obj_foreach(root, idx, n, key, val) { + const char *kstr = yyjson_get_str(key); + int64_t id = yyjson_get_int(val); + if (kstr && id >= 0 && id < vocab_size) { + free(tok->id_to_bpe[id]); + free(tok->id_to_text[id]); + tok->id_to_bpe[id] = strdup(kstr); + tok->id_to_text[id] = decode_gpt2_token(kstr); + } + } + yyjson_doc_free(doc); + free(json); + + int n_vocab_entries = 0; + for (int i = 0; i < vocab_size; i++) + if (tok->id_to_bpe[i]) + n_vocab_entries++; + tok->vocab_map_cap = next_pow2(n_vocab_entries * 2 + 1); + tok->vocab_map = calloc((size_t)tok->vocab_map_cap, sizeof(str_int_entry_t)); + if (!tok->vocab_map) { + tok_bpe_free(tok); + return NULL; + } + for (int i = 0; i < vocab_size; i++) { + if (tok->id_to_bpe[i]) { + map_insert((str_int_entry_t *)tok->vocab_map, tok->vocab_map_cap, tok->id_to_bpe[i], i); + } + } + + char merges_path[1024]; + if (derive_merges_path(vocab_json_path, merges_path, sizeof(merges_path)) == 0) { + if (load_merges_map(tok, merges_path) != 0) { + fprintf(stderr, + "Tokenizer: merges not loaded from %s (encoding falls back to byte-level)\n", + merges_path); + } + } + + return tok; +} + +const char *tok_bpe_decode(const tok_bpe_t *tok, int token_id) { + if (!tok || token_id < 0 || token_id >= tok->vocab_size) + return ""; + return tok->id_to_text[token_id] ? tok->id_to_text[token_id] : ""; +} + +int tok_bpe_token_id(const tok_bpe_t *tok, const char *token) { + if (!tok || !token || !tok->vocab_map) + return -1; + return map_get((const str_int_entry_t *)tok->vocab_map, tok->vocab_map_cap, token); +} + +int *tok_bpe_encode(const tok_bpe_t *tok, const char *text, int *out_n_tokens) { + if (out_n_tokens) + *out_n_tokens = 0; + if (!tok || !text || text[0] == '\0') + return NULL; + + char *normalized = normalize_nfc_latin(text); + if (!normalized) + return NULL; + + int *ids = NULL; + int n_ids = 0; + if (encode_pretokenized(tok, normalized, &ids, &n_ids) != 0) { + free(normalized); + free(ids); + return NULL; + } + free(normalized); + + if (out_n_tokens) + *out_n_tokens = n_ids; + return ids; +} + +tok_bpe_workspace_t *tok_bpe_workspace_new(void) { + return (tok_bpe_workspace_t *)calloc(1, sizeof(tok_bpe_workspace_t)); +} + +void tok_bpe_workspace_free(tok_bpe_workspace_t *ws) { + if (!ws) + return; + free(ws->normalized); + free(ws->mapped); + free(ws->arena); + free(ws->syms); + free(ws->ids); + free(ws->bpe_ids); + free(ws->bpe_prv); + free(ws->bpe_nxt); + free(ws->bpe_rnk); + free(ws->bpe_mrg); + free(ws); +} + +int tok_bpe_encode_into(const tok_bpe_t *tok, + tok_bpe_workspace_t *ws, + const char *text, + int *out_ids, + int out_cap, + int *out_n_tokens) { + if (out_n_tokens) + *out_n_tokens = 0; + if (!tok || !ws || !text || out_cap < 0) + return -1; + if (text[0] == '\0') + return 0; + + const char *normalized = NULL; + if (normalize_nfc_latin_into(ws, text, &normalized) != 0) + return -1; + + int n_ids = 0; + int rc = encode_pretokenized_into(tok, ws, normalized, out_ids, out_cap, &n_ids); + if (out_n_tokens) + *out_n_tokens = n_ids; + return rc; +} + +int *tok_bpe_encode_with_workspace(const tok_bpe_t *tok, + tok_bpe_workspace_t *ws, + const char *text, + int *out_n_tokens) { + if (out_n_tokens) + *out_n_tokens = 0; + if (!tok || !ws || !text || text[0] == '\0') + return NULL; + + size_t len = strlen(text); + size_t cap_sz = len > (SIZE_MAX - 8) / 2 ? SIZE_MAX : len * 2 + 8; + if (cap_sz > (size_t)INT_MAX) + return NULL; + int cap = (int)cap_sz; + + if (reserve_ints(&ws->ids, &ws->ids_cap, cap) != 0) + return NULL; + + int n_ids = 0; + int rc = tok_bpe_encode_into(tok, ws, text, ws->ids, ws->ids_cap, &n_ids); + if (rc == -2) { + if (reserve_ints(&ws->ids, &ws->ids_cap, n_ids) != 0) + return NULL; + rc = tok_bpe_encode_into(tok, ws, text, ws->ids, ws->ids_cap, &n_ids); + } + if (rc != 0 || n_ids <= 0) + return NULL; + + int *ids = (int *)malloc((size_t)n_ids * sizeof(int)); + if (!ids) + return NULL; + memcpy(ids, ws->ids, (size_t)n_ids * sizeof(int)); + if (out_n_tokens) + *out_n_tokens = n_ids; + return ids; +} + +void tok_bpe_free(tok_bpe_t *tok) { + if (!tok) + return; + + if (tok->id_to_text) { + for (int i = 0; i < tok->vocab_size; i++) + free(tok->id_to_text[i]); + free(tok->id_to_text); + } + if (tok->id_to_bpe) { + for (int i = 0; i < tok->vocab_size; i++) + free(tok->id_to_bpe[i]); + free(tok->id_to_bpe); + } + + if (tok->merge_map) { + str_int_entry_t *m = (str_int_entry_t *)tok->merge_map; + for (int i = 0; i < tok->merge_map_cap; i++) + free(m[i].key); + free(tok->merge_map); + } + free(tok->vocab_map); + free(tok->int_merges); + free(tok->cp_to_id); + free(tok); +} diff --git a/libffwd/bpe.h b/libffwd/bpe.h new file mode 100644 index 0000000..5199fd0 --- /dev/null +++ b/libffwd/bpe.h @@ -0,0 +1,74 @@ +/* + * Qwen2/Qwen3 BPE tokenizer (GPT-2 byte-level). + * + * Supports: + * - decode token ID -> UTF-8 text + * - encode UTF-8 text -> token IDs (vocab.json + merges.txt) + */ + +#ifndef TOK_BPE_H +#define TOK_BPE_H + +typedef struct { + char **id_to_text; /* [vocab_size] decoded text strings */ + char **id_to_bpe; /* [vocab_size] raw BPE token strings from vocab.json */ + int vocab_size; + + /* Internal hash maps (opaque to callers) */ + void *vocab_map; + int vocab_map_cap; + void *merge_map; + int merge_map_cap; + /* Integer-pair BPE merge table: (left_id, right_id) -> (rank, merged_id). + * NULL when the vocab could not resolve every merge; encoding then falls + * back to the string-based merge loop. */ + void *int_merges; + int int_merges_cap; + /* Byte-level unicode codepoint -> single-character token id (512 slots), + * or NULL when any byte token is missing from the vocab. */ + int *cp_to_id; +} tok_bpe_t; + +typedef struct tok_bpe_workspace tok_bpe_workspace_t; + +/* Load tokenizer from vocab.json in model directory */ +tok_bpe_t *tok_bpe_load(const char *vocab_json_path); + +/* Decode a single token ID to text. Returns pointer to internal string. */ +const char *tok_bpe_decode(const tok_bpe_t *tok, int token_id); + +/* Look up the id of a raw vocab.json token string (e.g. "<|endoftext|>"). + * Returns -1 when the token is not in the vocab. */ +int tok_bpe_token_id(const tok_bpe_t *tok, const char *token); + +/* Encode UTF-8 text into token IDs using BPE. + * Returns malloc'd array of token IDs and sets *out_n_tokens. + * Returns NULL on error (and sets *out_n_tokens to 0). */ +int *tok_bpe_encode(const tok_bpe_t *tok, const char *text, int *out_n_tokens); + +/* Reusable scratch for allocation-light repeated encoding. */ +tok_bpe_workspace_t *tok_bpe_workspace_new(void); +void tok_bpe_workspace_free(tok_bpe_workspace_t *ws); + +/* Encode into caller-provided out_ids[0..out_cap-1]. + * Returns 0 on success, -1 on error, -2 if out_cap is too small. + * In all cases *out_n_tokens is set to the number of tokens needed/emitted + * when out_n_tokens is non-NULL. */ +int tok_bpe_encode_into(const tok_bpe_t *tok, + tok_bpe_workspace_t *ws, + const char *text, + int *out_ids, + int out_cap, + int *out_n_tokens); + +/* Same tokenization as tok_bpe_encode(), but reuses ws internally and + * performs only the final returned int[] allocation. */ +int *tok_bpe_encode_with_workspace(const tok_bpe_t *tok, + tok_bpe_workspace_t *ws, + const char *text, + int *out_n_tokens); + +/* Free tokenizer */ +void tok_bpe_free(tok_bpe_t *tok); + +#endif /* TOK_BPE_H */ diff --git a/libffwd/config.c b/libffwd/config.c new file mode 100644 index 0000000..d9efb4d --- /dev/null +++ b/libffwd/config.c @@ -0,0 +1,341 @@ +#include "config.h" + +#include <yyjson.h> + +#include <errno.h> +#include <limits.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* Verbosity flag for optional stderr diagnostics across the codebase. + * Defined in config.c because every backend links it. */ +int ffwd_verbose = 0; + +typedef enum { + JSON_BOOL_MISSING = 0, + JSON_BOOL_FALSE = 1, + JSON_BOOL_TRUE = 2, + JSON_BOOL_INVALID = -1, +} json_bool_t; + +/* Read fields from a parsed JSON object. + * yyjson getters tolerate NULL, so missing keys or wrong types cleanly return + * the fallback. + * + * This replaces substring scanning, which could match a key anywhere in the + * document, including inside nested sub-configs. A real parser avoids that + * failure mode. */ +static int json_get_int(yyjson_val *obj, const char *key, int fallback) { + yyjson_val *v = yyjson_obj_get(obj, key); + if (yyjson_is_int(v)) + return (int)yyjson_get_int(v); + if (yyjson_is_real(v)) + return (int)yyjson_get_real(v); + return fallback; +} + +static double json_get_double(yyjson_val *obj, const char *key, double fallback) { + yyjson_val *v = yyjson_obj_get(obj, key); + return yyjson_is_num(v) ? yyjson_get_num(v) : fallback; +} + +static int json_string_equals(yyjson_val *obj, const char *key, const char *expected) { + const char *s = yyjson_get_str(yyjson_obj_get(obj, key)); + return s && strcmp(s, expected) == 0; +} + +static json_bool_t json_get_bool(yyjson_val *obj, const char *key) { + yyjson_val *v = yyjson_obj_get(obj, key); + if (!v) + return JSON_BOOL_MISSING; + if (yyjson_is_bool(v)) + return yyjson_get_bool(v) ? JSON_BOOL_TRUE : JSON_BOOL_FALSE; + return JSON_BOOL_INVALID; +} + +/* Return 1 with a NUL-terminated buffer, 0 when an optional file is absent, + * and -1 for an existing file that could not be read. */ +static int read_optional_file(const char *model_dir, const char *relative, char **out) { + char path[1024]; + int n = snprintf(path, sizeof(path), "%s/%s", model_dir, relative); + if (n < 0 || (size_t)n >= sizeof(path)) { + fprintf(stderr, "ffwd_config: model path too long\n"); + return -1; + } + + FILE *f = fopen(path, "rb"); + if (!f && errno == ENOENT) + return 0; + if (!f) { + fprintf(stderr, "ffwd_config: cannot open %s\n", path); + return -1; + } + if (fseek(f, 0, SEEK_END) != 0) { + fclose(f); + return -1; + } + long sz = ftell(f); + if (sz < 0 || (size_t)sz == SIZE_MAX || fseek(f, 0, SEEK_SET) != 0) { + fclose(f); + return -1; + } + char *buf = (char *)malloc((size_t)sz + 1); + if (!buf || fread(buf, 1, (size_t)sz, f) != (size_t)sz) { + fclose(f); + free(buf); + return -1; + } + fclose(f); + buf[sz] = '\0'; + *out = buf; + return 1; +} + +static int parse_sentence_transformers_metadata(ffwd_config_t *cfg, const char *model_dir) { + char *buf = NULL; + int rc = read_optional_file(model_dir, "1_Pooling/config.json", &buf); + if (rc < 0) + return -1; + if (rc > 0) { + yyjson_doc *doc = yyjson_read(buf, strlen(buf), 0); + yyjson_val *root = yyjson_doc_get_root(doc); + json_bool_t mean = json_get_bool(root, "pooling_mode_mean_tokens"); + json_bool_t last = json_get_bool(root, "pooling_mode_lasttoken"); + json_bool_t cls = json_get_bool(root, "pooling_mode_cls_token"); + json_bool_t max = json_get_bool(root, "pooling_mode_max_tokens"); + json_bool_t sqrt_len = json_get_bool(root, "pooling_mode_mean_sqrt_len_tokens"); + json_bool_t weighted = json_get_bool(root, "pooling_mode_weightedmean_tokens"); + if (mean < 0 || last < 0 || cls < 0 || max < 0 || sqrt_len < 0 || weighted < 0) { + fprintf(stderr, "ffwd_config: invalid pooling metadata\n"); + yyjson_doc_free(doc); + free(buf); + return -1; + } + int supported = (mean == JSON_BOOL_TRUE) + (last == JSON_BOOL_TRUE) + (cls == JSON_BOOL_TRUE); + int unsupported = + max == JSON_BOOL_TRUE || sqrt_len == JSON_BOOL_TRUE || weighted == JSON_BOOL_TRUE; + if (supported != 1 || unsupported) { + fprintf(stderr, "ffwd_config: unsupported Sentence Transformers pooling mode\n"); + yyjson_doc_free(doc); + free(buf); + return -1; + } + cfg->pooling_mode = cls == JSON_BOOL_TRUE ? FFWD_POOL_CLS + : last == JSON_BOOL_TRUE ? FFWD_POOL_LAST_TOKEN + : FFWD_POOL_MEAN; + yyjson_doc_free(doc); + free(buf); + buf = NULL; + } + + rc = read_optional_file(model_dir, "modules.json", &buf); + if (rc < 0) + return -1; + if (rc > 0) { + /* modules.json is an array of module objects; the embeddings are + * normalized iff one module's "type" is the Normalize class. Walking the + * array is exact, unlike a substring match that any field could trip. */ + yyjson_doc *doc = yyjson_read(buf, strlen(buf), 0); + yyjson_val *root = yyjson_doc_get_root(doc); + int normalize = 0; + size_t idx, n; + yyjson_val *mod; + yyjson_arr_foreach(root, idx, n, mod) { + if (json_string_equals(mod, "type", "sentence_transformers.models.Normalize")) { + normalize = 1; + break; + } + } + cfg->normalize_embeddings = normalize; + yyjson_doc_free(doc); + free(buf); + buf = NULL; + } + + rc = read_optional_file(model_dir, "tokenizer_config.json", &buf); + if (rc < 0) + return -1; + if (rc > 0) { + yyjson_doc *doc = yyjson_read(buf, strlen(buf), 0); + yyjson_val *root = yyjson_doc_get_root(doc); + json_bool_t add_eos = json_get_bool(root, "add_eos_token"); + if (add_eos < 0) { + fprintf(stderr, "ffwd_config: invalid add_eos_token metadata\n"); + yyjson_doc_free(doc); + free(buf); + return -1; + } + if (add_eos != JSON_BOOL_MISSING) + cfg->append_terminal_token = add_eos == JSON_BOOL_TRUE; + yyjson_doc_free(doc); + free(buf); + } + return 0; +} + +int ffwd_config_parse(ffwd_config_t *cfg, const char *model_dir) { + char path[1024]; + int n = snprintf(path, sizeof(path), "%s/config.json", model_dir); + if (n < 0 || (size_t)n >= sizeof(path)) { + fprintf(stderr, "ffwd_config: model path too long\n"); + return -1; + } + + FILE *f = fopen(path, "rb"); + if (!f) { + fprintf(stderr, "ffwd_config: cannot open %s\n", path); + return -1; + } + + if (fseek(f, 0, SEEK_END) != 0) { + fclose(f); + return -1; + } + long sz = ftell(f); + if (sz < 0 || (size_t)sz == SIZE_MAX || fseek(f, 0, SEEK_SET) != 0) { + fclose(f); + return -1; + } + char *buf = (char *)malloc((size_t)sz + 1); + if (!buf || fread(buf, 1, (size_t)sz, f) != (size_t)sz) { + fclose(f); + free(buf); + return -1; + } + fclose(f); + buf[sz] = '\0'; + + yyjson_doc *doc = yyjson_read(buf, (size_t)sz, 0); + yyjson_val *root = yyjson_doc_get_root(doc); + if (!yyjson_is_obj(root)) { + fprintf(stderr, "ffwd_config: invalid JSON in %s\n", path); + yyjson_doc_free(doc); + free(buf); + return -1; + } + + cfg->hidden_size = json_get_int(root, "hidden_size", 0); + cfg->n_layers = json_get_int(root, "num_hidden_layers", 0); + cfg->n_heads = json_get_int(root, "num_attention_heads", 0); + cfg->n_kv_heads = json_get_int(root, "num_key_value_heads", 0); + cfg->head_dim = json_get_int(root, "head_dim", FFWD_HEAD_DIM); + cfg->intermediate_size = json_get_int(root, "intermediate_size", 0); + cfg->vocab_size = json_get_int(root, "vocab_size", FFWD_VOCAB_SIZE); + cfg->rms_norm_eps = (float)json_get_double(root, "rms_norm_eps", 1e-6); + cfg->layer_norm_eps = (float)json_get_double(root, "layer_norm_eps", 1e-12); + cfg->rope_theta = (float)json_get_double(root, "rope_theta", 1000000.0); + cfg->max_position_embeddings = json_get_int(root, "max_position_embeddings", 0); + cfg->position_id_offset = 0; + cfg->type_vocab_size = json_get_int(root, "type_vocab_size", 2); + cfg->family = FFWD_FAMILY_QWEN3; + cfg->qk_norm = 1; + cfg->qkv_bias = 0; + cfg->attention_mode = FFWD_ATTENTION_BIDIRECTIONAL; + cfg->pooling_mode = FFWD_POOL_MEAN; + cfg->normalize_embeddings = 0; + cfg->append_terminal_token = 0; + + if (json_string_equals(root, "model_type", "qwen3")) { + cfg->attention_mode = FFWD_ATTENTION_CAUSAL; + cfg->pooling_mode = FFWD_POOL_LAST_TOKEN; + cfg->normalize_embeddings = 1; + cfg->append_terminal_token = 1; + } else if (json_string_equals(root, "model_type", "qwen2")) { + cfg->qk_norm = 0; + cfg->qkv_bias = 1; + cfg->attention_mode = FFWD_ATTENTION_CAUSAL; + } else if (json_string_equals(root, "model_type", "bert") || + json_string_equals(root, "model_type", "roberta") || + json_string_equals(root, "model_type", "xlm-roberta")) { + cfg->family = FFWD_FAMILY_BERT; + cfg->qk_norm = 0; + cfg->qkv_bias = 1; /* q/k/v/o and both dense layers carry bias */ + cfg->attention_mode = FFWD_ATTENTION_BIDIRECTIONAL; + cfg->n_kv_heads = cfg->n_heads; /* full multi-head attention, no GQA */ + cfg->head_dim = cfg->n_heads > 0 ? cfg->hidden_size / cfg->n_heads : 0; + if (json_string_equals(root, "model_type", "roberta") || + json_string_equals(root, "model_type", "xlm-roberta")) { + int pad_id = json_get_int(root, "pad_token_id", 1); + if (pad_id < 0 || pad_id == INT_MAX) { + fprintf(stderr, "ffwd_config: invalid RoBERTa pad_token_id\n"); + yyjson_doc_free(doc); + free(buf); + return -1; + } + cfg->position_id_offset = pad_id + 1; + } + /* hidden_act selects the feed-forward GeLU curve. gelu_new and + * gelu_pytorch_tanh are the same tanh approximation; everything else + * (gelu, absent) is the exact erf GeLU the released encoders use. */ + if (json_string_equals(root, "hidden_act", "gelu_new") || + json_string_equals(root, "hidden_act", "gelu_pytorch_tanh")) { + cfg->ffn_act = FFWD_ACT_GELU_TANH; + } + if (cfg->max_position_embeddings <= 0 || + cfg->position_id_offset >= cfg->max_position_embeddings || cfg->type_vocab_size <= 0 || + !isfinite(cfg->layer_norm_eps) || cfg->layer_norm_eps <= 0.0f) { + fprintf(stderr, "ffwd_config: invalid BERT layer_norm_eps/max_position_embeddings\n"); + yyjson_doc_free(doc); + free(buf); + return -1; + } + } + + json_bool_t is_causal = json_get_bool(root, "is_causal"); + if (is_causal < 0) { + fprintf(stderr, "ffwd_config: invalid is_causal in %s\n", path); + yyjson_doc_free(doc); + free(buf); + return -1; + } + if (is_causal != JSON_BOOL_MISSING) { + cfg->attention_mode = + is_causal == JSON_BOOL_TRUE ? FFWD_ATTENTION_CAUSAL : FFWD_ATTENTION_BIDIRECTIONAL; + } + + cfg->q_dim = 0; + cfg->kv_dim = 0; + if (cfg->n_heads > 0 && cfg->head_dim > 0 && cfg->n_heads <= INT_MAX / cfg->head_dim) + cfg->q_dim = cfg->n_heads * cfg->head_dim; + if (cfg->n_kv_heads > 0 && cfg->head_dim > 0 && cfg->n_kv_heads <= INT_MAX / cfg->head_dim) + cfg->kv_dim = cfg->n_kv_heads * cfg->head_dim; + + yyjson_doc_free(doc); + free(buf); + + if (parse_sentence_transformers_metadata(cfg, model_dir) != 0) + return -1; + + if (cfg->hidden_size <= 0 || cfg->n_layers <= 0 || cfg->n_heads <= 0 || cfg->n_kv_heads <= 0 || + cfg->head_dim <= 0 || cfg->intermediate_size <= 0 || cfg->vocab_size <= 0 || + cfg->q_dim <= 0 || cfg->kv_dim <= 0 || (cfg->head_dim & 1) || + cfg->n_heads % cfg->n_kv_heads != 0 || !isfinite(cfg->rms_norm_eps) || + cfg->rms_norm_eps <= 0.0f || !isfinite(cfg->rope_theta) || cfg->rope_theta <= 0.0f) { + fprintf(stderr, + "ffwd_config: invalid config in %s " + "(hidden=%d, layers=%d, heads=%d/%d, head_dim=%d, inter=%d)\n", + path, cfg->hidden_size, cfg->n_layers, cfg->n_heads, cfg->n_kv_heads, cfg->head_dim, + cfg->intermediate_size); + return -1; + } + if (cfg->n_layers > FFWD_MAX_LAYERS) { + fprintf(stderr, "ffwd_config: too many layers (%d > %d)\n", cfg->n_layers, FFWD_MAX_LAYERS); + return -1; + } + + if (ffwd_verbose >= 1) + fprintf(stderr, + "config: hidden=%d, layers=%d, heads=%d/%d, " + "inter=%d, head_dim=%d, attention=%s, pooling=%s, qk_norm=%s, qkv_bias=%s\n", + cfg->hidden_size, cfg->n_layers, cfg->n_heads, cfg->n_kv_heads, + cfg->intermediate_size, cfg->head_dim, + cfg->attention_mode == FFWD_ATTENTION_CAUSAL ? "causal" : "bidirectional", + cfg->pooling_mode == FFWD_POOL_LAST_TOKEN ? "last-token" + : cfg->pooling_mode == FFWD_POOL_CLS ? "cls" + : "mean", + cfg->qk_norm ? "yes" : "no", cfg->qkv_bias ? "yes" : "no"); + + return 0; +} diff --git a/libffwd/config.h b/libffwd/config.h new file mode 100644 index 0000000..b3e2f67 --- /dev/null +++ b/libffwd/config.h @@ -0,0 +1,8 @@ +#ifndef FFWD_CONFIG_H +#define FFWD_CONFIG_H + +#include "ffwd.h" + +int ffwd_config_parse(ffwd_config_t *cfg, const char *model_dir); + +#endif /* FFWD_CONFIG_H */ diff --git a/libffwd/dtype.h b/libffwd/dtype.h new file mode 100644 index 0000000..7a7a2ad --- /dev/null +++ b/libffwd/dtype.h @@ -0,0 +1,55 @@ +#ifndef FFWD_DTYPE_H +#define FFWD_DTYPE_H + +#include <stdint.h> +#include <string.h> + +/* BF16 is the upper 16 bits of an IEEE-754 binary32, so widening one value is a + * left shift reinterpreted as a float. Shared by every host-side caller (the + * CPU loader, the safetensors reader); the CUDA backend keeps its own + * __nv_bfloat16 device kernels, and kernels.c keeps the SIMD buffer variant + * bf16_to_f32_buf for hot, contiguous conversions. */ +static inline float ffwd_bf16_to_f32(uint16_t bf16) { + uint32_t bits = (uint32_t)bf16 << 16; + float f; + memcpy(&f, &bits, sizeof(f)); + return f; +} + +/* IEEE-754 binary16 -> binary32. Unlike BF16, F16 has a 5-bit exponent and a + * 10-bit mantissa, so widening is a field re-pack, not a shift: rebias the + * exponent (15 -> 127), shift the mantissa up by 13, and special-case zeros, + * subnormals, and inf/NaN. Used by the loaders to accept F16 safetensors + * snapshots (e.g. the public multilingual-e5 weights); there is no fused F16 + * kernel, so weights are widened to F32 once at load. */ +static inline float ffwd_f16_to_f32(uint16_t f16) { + uint32_t sign = (uint32_t)(f16 & 0x8000u) << 16; + uint32_t exp = (f16 >> 10) & 0x1fu; + uint32_t mant = f16 & 0x3ffu; + uint32_t bits; + if (exp == 0) { + if (mant == 0) { + bits = sign; /* signed zero */ + } else { + /* Subnormal half: renormalize into a normal float. Shift the + * mantissa left until its leading 1 reaches the hidden-bit + * position, decrementing the exponent for each shift. */ + exp = 127 - 15 + 1; + while ((mant & 0x400u) == 0) { + mant <<= 1; + exp--; + } + mant &= 0x3ffu; + bits = sign | (exp << 23) | (mant << 13); + } + } else if (exp == 0x1fu) { + bits = sign | 0x7f800000u | (mant << 13); /* inf or NaN */ + } else { + bits = sign | ((exp + (127 - 15)) << 23) | (mant << 13); + } + float f; + memcpy(&f, &bits, sizeof(f)); + return f; +} + +#endif /* FFWD_DTYPE_H */ diff --git a/libffwd/ffwd.h b/libffwd/ffwd.h new file mode 100644 index 0000000..c4708fa --- /dev/null +++ b/libffwd/ffwd.h @@ -0,0 +1,281 @@ +#ifndef FFWD_H +#define FFWD_H + +/* The libraries are built with -fvisibility=hidden, so only declarations carrying + * FFWD_API are exported from the shared library; everything else stays internal. */ +#ifndef FFWD_API +# if defined(__GNUC__) +# define FFWD_API __attribute__((visibility("default"))) +# else +# define FFWD_API +# endif +#endif + +#include <stddef.h> +#include <stdint.h> + +/* Shared constants and defaults */ + +#define FFWD_VOCAB_SIZE 151936 +#define FFWD_HEAD_DIM 128 +#define FFWD_MAX_LAYERS 64 /* upper bound for stack arrays */ +#define FFWD_CONTEXT_SEPARATOR_TOKEN_ID 151643 /* <|endoftext|> */ + +/* Late-interaction (ColBERT) fallback special-token ids and query pad length. + * Used when the model tokenizer does not define these tokens itself. */ +#define FFWD_LATE_QUERY_TOKENS 32 +#define FFWD_LATE_MASK_TOKEN_ID 151642 +#define FFWD_LATE_QUERY_PREFIX_ID 151669 +#define FFWD_LATE_DOCUMENT_PREFIX_ID 151670 + +/* Model Configuration (populated from config.json at load time) */ + +typedef enum { + FFWD_ATTENTION_BIDIRECTIONAL = 0, + FFWD_ATTENTION_CAUSAL = 1, +} ffwd_attention_mode_t; + +typedef enum { + FFWD_POOL_MEAN = 0, + FFWD_POOL_LAST_TOKEN = 1, + FFWD_POOL_CLS = 2, /* first token; the encoder-family [CLS] sentence vector */ +} ffwd_pooling_mode_t; + +typedef enum { + FFWD_FAMILY_QWEN3 = 0, /* RMSNorm + RoPE + SwiGLU decoder block (Qwen2/3, pplx-embed) */ + FFWD_FAMILY_BERT = 1, /* LayerNorm + learned positions + GeLU encoder block */ +} ffwd_family_t; + +/* BERT feed-forward activation. The released MiniLM/BGE encoders use exact erf + * GeLU; some encoders publish the tanh approximation (hidden_act gelu_new or + * gelu_pytorch_tanh), which is a different curve and must be matched exactly. */ +typedef enum { + FFWD_ACT_GELU_ERF = 0, /* 0.5 x (1 + erf(x / sqrt(2))) */ + FFWD_ACT_GELU_TANH = 1, /* 0.5 x (1 + tanh(sqrt(2/pi) (x + 0.044715 x^3))) */ +} ffwd_act_t; + +typedef struct { + ffwd_family_t family; + ffwd_act_t ffn_act; /* BERT feed-forward GeLU variant; ERF for Qwen/SwiGLU */ + int hidden_size; + int n_layers; + int n_heads; + int n_kv_heads; + int head_dim; + int q_dim; /* n_heads * head_dim */ + int kv_dim; /* n_kv_heads * head_dim */ + int intermediate_size; + int vocab_size; + int max_position_embeddings; /* BERT learned position table size */ + int position_id_offset; /* RoBERTa/XLM-R positions start at pad_id + 1 */ + int type_vocab_size; /* BERT token-type embedding rows; XLM-R uses 1 */ + float rms_norm_eps; + float layer_norm_eps; /* BERT LayerNorm epsilon */ + float rope_theta; + int qk_norm; + int qkv_bias; + ffwd_attention_mode_t attention_mode; + ffwd_pooling_mode_t pooling_mode; + int normalize_embeddings; + /* Qwen3-Embedding pools the last token: the tokenizer's <|endoftext|> suffix + * added by its post-processor, not the model's chat eos_token_id (<|im_end|>). + * + * Text frontends append it and resolve its id from the tokenizer, just like the + * contextual separator. FFWD_CONTEXT_SEPARATOR_TOKEN_ID is the released-model + * fallback. + * + * Model forward consumes token ids exactly and never appends. */ + int append_terminal_token; +} ffwd_config_t; + +typedef struct { + const int *ids; + int n_tokens; +} ffwd_input_t; + +typedef struct { + int start; + int n_tokens; +} ffwd_span_t; + +typedef struct { + ffwd_input_t input; + const ffwd_span_t *spans; + int n_spans; +} ffwd_context_input_t; + +/* + * Backend-selected inference handle. + * + * This is the library's main public interface and the full supported surface of + * the shared library. Exactly one backend is linked at build time: CPU, Apple Metal, + * or NVIDIA CUDA. That backend implements these functions, so callers never branch + * on backend. + * + * The CLI, server, and language bindings use only these functions plus the math + * helpers below. + * + * Model loading has two phases so a GPU context can be created on the thread + * that owns it. + * + * ffwd_open runs on the caller's thread. It loads a CPU model immediately; in a + * GPU build, it only records what to load. + * + * ffwd_activate runs on the inference thread and creates the GPU context. It is + * a no-op for CPU. + * + * Teardown mirrors activation: ffwd_worker_free releases the GPU context from + * the inference thread, and ffwd_free releases the rest. + */ + +/* Opaque inference handle; layout is private to the linked backend. */ +typedef struct embed ffwd_t; + +/* Backend tuning, parsed from the command line. A backend reads the fields it + * supports and ignores the rest, so the front-ends parse them with no backend + * branch. */ +typedef struct { + int n_threads; /* CPU worker threads; <= 0 auto-detects */ + int gpu_quant_bits; /* GPU load-time weight quantization (0 or 8) */ + int gpu_quant_group_size; /* GPU quantization group size (> 0) */ + const char *gpu_gemm_mode; /* GPU GEMM compute mode, or NULL */ + const char *gpu_weight_dtype; /* GPU weight storage dtype, or NULL */ + double memory_utilization; /* GPU memory budget fraction; 0 = default */ +} ffwd_options_t; + +/* One late-interaction rerank: a query and N candidate documents, each given as + * token ids plus the kept-token indices. The vector dimension is the loaded + * late model's token_dim. */ +typedef struct { + const int *query_ids; + int query_n_tokens; + int query_n_keep; + const int *const *doc_ids; + const int *doc_n_tokens; + const int *const *doc_keep; + const int *doc_n_keep; + int n_docs; +} ffwd_rerank_input_t; + +/* Apply process-wide settings before any load (CPU thread count, GPU GEMM and + * weight settings, GPU option checks) and print the startup banner. Returns -1 and + * writes a reason into err on a rejected option value. */ +FFWD_API int ffwd_init(const ffwd_options_t *opts, char *err, size_t errlen); + +/* First load phase, on the caller's thread: load a CPU model now, or record what + * a GPU build will load in ffwd_activate. is_late selects a late-interaction + * model. NULL on failure with a reason in err. */ +FFWD_API ffwd_t * +ffwd_open(const char *model_dir, int is_late, const ffwd_options_t *opts, char *err, size_t errlen); +/* Second load phase, on the inference thread: create the GPU context. CPU + * no-op. Returns -1 with a reason in err on failure. */ +FFWD_API int ffwd_activate(ffwd_t *e, char *err, size_t errlen); +/* Release the inference-thread-owned GPU context; CPU no-op. Idempotent. */ +FFWD_API void ffwd_worker_free(ffwd_t *e); +/* Release CPU state and the handle; call after ffwd_worker_free. */ +FFWD_API void ffwd_free(ffwd_t *e); + +/* Available once the model is loaded (after open for CPU, after activate for + * GPU); config is NULL until then. */ +FFWD_API const ffwd_config_t *ffwd_config(const ffwd_t *e); +/* Late projection dimension, or 0 for a standard model. */ +FFWD_API int ffwd_token_dim(const ffwd_t *e); +/* Whether the backend pads batches to a common length rather than packing them. */ +FFWD_API int ffwd_uses_dense_batches(const ffwd_t *e); + +/* Encode a packed batch into out[batch, hidden_size]. Returns 0 on success. */ +FFWD_API int ffwd_encode_batch(ffwd_t *e, const ffwd_input_t *inputs, int batch, float *out); +/* Encode a contextual document batch, pooling each selected span in order. */ +FFWD_API int +ffwd_encode_spans_batch(ffwd_t *e, const ffwd_context_input_t *inputs, int batch, float *out); +/* Score one query against its candidate documents into scores[n_docs]. */ +FFWD_API int ffwd_rerank(ffwd_t *e, const ffwd_rerank_input_t *in, float *scores); +/* Scratch memory the model currently holds, for diagnostics; 0 on GPU. */ +FFWD_API size_t ffwd_scratch_nbytes(const ffwd_t *e); + +/* Accelerator label for --build-info and the startup banner: "Apple Metal", + * "NVIDIA CUDA", or "" on a CPU build. A function, not a macro, so a shared + * object does not bake in one backend's label and stays correct across a + * rebuild. */ +FFWD_API const char *ffwd_capability(void); + +/* First-arrival micro-batch wait the backend prefers, in microseconds. */ +FFWD_API int ffwd_default_batch_wait_us(void); +/* Reject a model set that would not fit host memory (GPU backends); 0 otherwise. On + * rejection returns -1 with a reason in err. paths are the model directories. */ +FFWD_API int ffwd_preflight( + const char *const *paths, int n_paths, const ffwd_options_t *opts, char *err, size_t errlen); + +/* + * L2-normalize vec[dim] in place. + * + * Returns 0 on success. Returns -1 for invalid arguments or a zero-length + * vector, which cannot be normalized. + */ +FFWD_API int ffwd_l2_normalize(float *vec, int dim); + +/* + * Cosine similarity between two vectors. + * Returns 0 for invalid arguments or if either vector has zero length. + */ +FFWD_API float ffwd_cosine_similarity(const float *a, const float *b, int dim); + +/* + * Tokenization: text -> token ids the model expects. + * + * Tokenization is backend-independent because text -> ids is the same on CPU + * and GPU, so it has its own handle, separate from ffwd_t. + * + * ffwd_tok_open selects the tokenizer from files in model_dir: WordPiece + * vocab.txt, SentencePiece .model, or byte-level vocab.json. It also resolves + * the model's special-token ids. + * + * ffwd_tokenize applies the model's token layout: BERT and XLM-R wrap ids with + * [CLS]..[SEP] (<s>..</s>), while Qwen appends its terminal token. + */ + +/* Opaque tokenizer handle; owns the tokenizer and its resolved special tokens. */ +typedef struct ffwd_tok ffwd_tok_t; + +/* Late-interaction tokens: + * token ids plus the document positions to score. + * For queries, keep is NULL because every position is kept, and + * n_keep == n_tokens. */ +typedef struct { + int *ids; + int n_tokens; + int *keep; + int n_keep; +} ffwd_late_tokens_t; + +/* Load the tokenizer for the model in model_dir and resolve its special-token + * ids. is_late additionally resolves the late-interaction [Q]/[D]/[MASK] ids. + * NULL on failure with a reason in err. */ +FFWD_API ffwd_tok_t *ffwd_tok_open(const char *model_dir, int is_late, char *err, size_t errlen); +FFWD_API void ffwd_tok_free(ffwd_tok_t *t); + +/* Tokenize text for this model: applies the model's special-token layout and + * optionally prepends a query instruction. Returns a malloc'd id array (caller + * frees) and sets *n_out. NULL on error. */ +FFWD_API int *ffwd_tokenize(ffwd_tok_t *t, const char *text, const char *query_instruct, int *n_out); + +/* Late-interaction tokenization: [Q]/[D] prefix, [MASK]-pad queries to a fixed + * length, and mark the document positions to score. Returns 0 on success. */ +FFWD_API int +ffwd_tokenize_late(ffwd_tok_t *t, const char *text, int is_query, ffwd_late_tokens_t *out); +FFWD_API void ffwd_late_tokens_free(ffwd_late_tokens_t *out); + +/* The contextual chunk-separator id (the tokenizer's <|endoftext|>), for + * frontends that split a document into chunks. */ +FFWD_API int ffwd_tok_context_separator_id(const ffwd_tok_t *t); + +/* Whether ffwd_tokenize appends the model's terminal token (<|endoftext|>) for + * last-token pooling. Off until set; the caller sets it from the loaded model's + * config (ffwd_config(...)->append_terminal_token), since it is model-specific + * and only known after the backend is open. */ +FFWD_API void ffwd_tok_set_append_terminal(ffwd_tok_t *t, int on); + +/* Verbose level: 0=quiet, 1=info, 2=debug */ +FFWD_API extern int ffwd_verbose; + +#endif /* FFWD_H */ diff --git a/libffwd/forward.c b/libffwd/forward.c new file mode 100644 index 0000000..d5d2442 --- /dev/null +++ b/libffwd/forward.c @@ -0,0 +1,721 @@ +#include "ffwd.h" +#include "model_internal.h" +#include "dtype.h" +#include "kernels.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <limits.h> + +/* For packed sequences below these lengths, fused BF16 row kernels beat + * widening the weight to F32 and using BLAS. For longer packed sequences, widen once. */ +#define FFWD_BF16_QKV_FUSE_MAX_SEQ 16 +#define FFWD_BF16_PAIR_FUSE_MAX_SEQ 4 + +/* Linear projections (weight-dtype dispatch) and workspace scratch growth */ + +static void bf16_row_to_f32(float *dst, const uint16_t *src, int n) { + for (int i = 0; i < n; i++) + dst[i] = ffwd_bf16_to_f32(src[i]); +} + +static void copy_weight_row(float *dst, const ffwd_weight_ref_t *w, size_t row, int row_width) { + if (w->dtype == DTYPE_F32) { + const float *src = (const float *)w->data + row * (size_t)row_width; + memcpy(dst, src, (size_t)row_width * sizeof(float)); + } else { + const uint16_t *src = (const uint16_t *)w->data + row * (size_t)row_width; + bf16_row_to_f32(dst, src, row_width); + } +} + +static int ensure_bf16_widen(ffwd_workspace_t *ws, size_t count) { + if (ws->bf16_widen_count >= count) + return 0; + float *p = (float *)realloc(ws->bf16_widen, count * sizeof(float)); + if (!p) + return -1; + ws->bf16_widen = p; + ws->bf16_widen_count = count; + return 0; +} + +void linear_nobias_weight(ffwd_workspace_t *ws, + float *y, + const float *x, + const ffwd_weight_ref_t *w, + int seq_len, + int in_dim, + int out_dim) { + if (w->dtype == DTYPE_F32) { + linear_nobias(y, x, (const float *)w->data, seq_len, in_dim, out_dim); + return; + } + if (seq_len > 16) { + /* Widen the weight matrix once into workspace scratch and use the + * BLAS-backed F32 path; below that the fused BF16 row kernels win. */ + size_t count = (size_t)out_dim * (size_t)in_dim; + if (ensure_bf16_widen(ws, count) == 0) { + bf16_to_f32_buf(ws->bf16_widen, (const uint16_t *)w->data, count); + linear_nobias(y, x, ws->bf16_widen, seq_len, in_dim, out_dim); + return; + } + } + linear_nobias_bf16(y, x, (const uint16_t *)w->data, seq_len, in_dim, out_dim); +} + +static void linear_qkv_weight(ffwd_workspace_t *ws, + float *q, + float *k, + float *v, + const float *x, + const ffwd_weight_ref_t *wq, + const ffwd_weight_ref_t *wk, + const ffwd_weight_ref_t *wv, + int seq_len, + int in_dim, + int q_dim, + int kv_dim) { + if (wq->dtype == DTYPE_BF16 && wk->dtype == DTYPE_BF16 && wv->dtype == DTYPE_BF16 && + seq_len <= FFWD_BF16_QKV_FUSE_MAX_SEQ) { + linear_nobias_bf16_qkv(q, k, v, x, (const uint16_t *)wq->data, (const uint16_t *)wk->data, + (const uint16_t *)wv->data, seq_len, in_dim, q_dim, kv_dim); + return; + } + + linear_nobias_weight(ws, q, x, wq, seq_len, in_dim, q_dim); + linear_nobias_weight(ws, k, x, wk, seq_len, in_dim, kv_dim); + linear_nobias_weight(ws, v, x, wv, seq_len, in_dim, kv_dim); +} + +static void add_bias_rows(float *x, const float *bias, int rows, int dim) { + if (!bias) + return; + for (int row = 0; row < rows; row++) { + float *dst = x + (size_t)row * dim; + for (int d = 0; d < dim; d++) + dst[d] += bias[d]; + } +} + +static void linear_pair_weight(ffwd_workspace_t *ws, + float *a, + float *b, + const float *x, + const ffwd_weight_ref_t *wa, + const ffwd_weight_ref_t *wb, + int seq_len, + int in_dim, + int a_dim, + int b_dim) { + if (wa->dtype == DTYPE_BF16 && wb->dtype == DTYPE_BF16 && + seq_len <= FFWD_BF16_PAIR_FUSE_MAX_SEQ) { + linear_nobias_bf16_pair(a, b, x, (const uint16_t *)wa->data, (const uint16_t *)wb->data, + seq_len, in_dim, a_dim, b_dim); + return; + } + + linear_nobias_weight(ws, a, x, wa, seq_len, in_dim, a_dim); + linear_nobias_weight(ws, b, x, wb, seq_len, in_dim, b_dim); +} + +/* Packed forward pass. + * + * Workspace-growth helpers called here live in model_blas.c beside the + * workspace lifecycle and ffwd_workspace_nbytes: + * ensure_buffers, ensure_rope_cache, ensure_attention_scores, ensure_offsets. + * + * ffwd_workspace_nbytes mirrors their size accounting. + */ + +static ffwd_attention_fn attention_for_config(const ffwd_config_t *cfg) { + return cfg->attention_mode == FFWD_ATTENTION_CAUSAL + ? causal_gqa_attention_packed_with_scratch + : bidirectional_gqa_attention_packed_with_scratch; +} + +int build_offsets(const ffwd_input_t *inputs, + int batch, + int require_ids, + int *offsets, + int *total_out, + int *max_out) { + if (!inputs || batch <= 0 || !offsets || !total_out || !max_out) + return -1; + + int total = 0; + int max_seq = 0; + for (int b = 0; b < batch; b++) { + if ((require_ids && !inputs[b].ids) || inputs[b].n_tokens <= 0) + return -1; + if (total > INT32_MAX - inputs[b].n_tokens) + return -1; + offsets[b] = total; + total += inputs[b].n_tokens; + if (inputs[b].n_tokens > max_seq) + max_seq = inputs[b].n_tokens; + } + offsets[batch] = total; + *total_out = total; + *max_out = max_seq; + return 0; +} + +static int forward_packed_slice_inplace(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const ffwd_input_t *inputs, + int batch, + const int *offsets, + int total_seq, + int max_seq, + const float *input_states, + int layer_start, + int layer_end, + int apply_final_norm) { + if (!model || !ws || ws->model != model || !inputs || batch <= 0 || !offsets || total_seq <= 0 || + max_seq <= 0) + return -1; + + const ffwd_config_t *cfg = &model->config; + const ffwd_weights_t *w = &model->weights; + if (layer_start < 0 || layer_start > layer_end || layer_end > cfg->n_layers || + layer_start < model->layer_start || layer_end > model->layer_end || + (layer_start == 0 && input_states) || (layer_start != 0 && !input_states) || + (layer_start == 0 && !w->embed_tokens.data) || + (apply_final_norm && (layer_end != cfg->n_layers || !w->norm))) + return -1; + + int hidden = cfg->hidden_size; + int n_heads = cfg->n_heads; + int n_kv_heads = cfg->n_kv_heads; + int head_dim = cfg->head_dim; + int q_dim = cfg->q_dim; + int kv_dim = cfg->kv_dim; + int inter = cfg->intermediate_size; + float eps = cfg->rms_norm_eps; + + if (ensure_buffers(ws, cfg, total_seq) != 0) + return -1; + if (ensure_rope_cache(ws, cfg, max_seq) != 0) + return -1; + ensure_attention_scores(ws, offsets, batch); + + const float *rope_cos = ws->rope_cos; + const float *rope_sin = ws->rope_sin; + + float *x = ws->x; + float *x_norm = ws->x_norm; + float *q_buf = ws->q; + float *k_buf = ws->k; + float *v_buf = ws->v; + float *attn_out = ws->attn_out; + float *proj_out = ws->proj_out; + float *ffn_gate = ws->ffn_gate; + float *ffn_up = ws->ffn_up; + + /* A first stage performs embedding lookup. Later stages resume from a + * packed hidden-state tensor received from the preceding stage. */ + if (layer_start == 0) { + for (int b = 0; b < batch; b++) { + int start = offsets[b]; + for (int i = 0; i < inputs[b].n_tokens; i++) { + int id = inputs[b].ids[i]; + if (id < 0 || id >= cfg->vocab_size) { + fprintf(stderr, "ffwd: invalid token id %d at input %d position %d\n", id, b, i); + return -1; + } + copy_weight_row(x + (size_t)(start + i) * hidden, &w->embed_tokens, (size_t)id, + hidden); + } + } + } else { + memcpy(x, input_states, (size_t)total_seq * hidden * sizeof(float)); + } + + float scale = 1.0f / sqrtf((float)head_dim); + + /* 2. Transformer layers. Linear/MLP ops run over all packed tokens; + * attention is explicitly block-diagonal by sequence offsets. */ + for (int layer = layer_start; layer < layer_end; layer++) { + const ffwd_layer_t *l = &w->layers[layer]; + + rms_norm(x_norm, x, l->input_norm, total_seq, hidden, eps); + + linear_qkv_weight(ws, q_buf, k_buf, v_buf, x_norm, &l->wq, &l->wk, &l->wv, total_seq, hidden, + q_dim, kv_dim); + + add_bias_rows(q_buf, l->q_bias, total_seq, q_dim); + add_bias_rows(k_buf, l->k_bias, total_seq, kv_dim); + add_bias_rows(v_buf, l->v_bias, total_seq, kv_dim); + if (cfg->qk_norm) { + rms_norm_per_head(q_buf, l->q_norm, total_seq, n_heads, head_dim, eps); + rms_norm_per_head(k_buf, l->k_norm, total_seq, n_kv_heads, head_dim, eps); + } + + for (int b = 0; b < batch; b++) { + int start = offsets[b]; + int len = offsets[b + 1] - start; + apply_rope_neox(q_buf + (size_t)start * q_dim, rope_cos, rope_sin, len, n_heads, + head_dim); + apply_rope_neox(k_buf + (size_t)start * kv_dim, rope_cos, rope_sin, len, n_kv_heads, + head_dim); + } + + ffwd_attention_fn attention = attention_for_config(cfg); + attention(attn_out, q_buf, k_buf, v_buf, offsets, batch, n_heads, n_kv_heads, head_dim, scale, + ws->attn_scores, ws->attn_scores_bytes); + + linear_nobias_weight(ws, proj_out, attn_out, &l->wo, total_seq, q_dim, hidden); + add_inplace(x, proj_out, total_seq * hidden); + + rms_norm(x_norm, x, l->post_attn_norm, total_seq, hidden, eps); + + linear_pair_weight(ws, ffn_gate, ffn_up, x_norm, &l->gate_proj, &l->up_proj, total_seq, + hidden, inter, inter); + silu_mul_inplace(ffn_gate, ffn_up, total_seq * inter); + linear_nobias_weight(ws, proj_out, ffn_gate, &l->down_proj, total_seq, inter, hidden); + add_inplace(x, proj_out, total_seq * hidden); + } + + if (apply_final_norm) + rms_norm(x, x, w->norm, total_seq, hidden, eps); + return 0; +} + +int forward_packed_inplace(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const ffwd_input_t *inputs, + int batch, + const int *offsets, + int total_seq, + int max_seq, + int apply_final_norm) { + return forward_packed_slice_inplace(model, ws, inputs, batch, offsets, total_seq, max_seq, NULL, + 0, model->config.n_layers, apply_final_norm); +} + +/* BERT-family forward pass. + * + * Uses token, learned-position, and token-type embeddings, then embedding + * LayerNorm. + * + * Encoder layers are post-norm, with bias on every projection, GeLU + * feed-forward, no RoPE, and no per-head norm. + * + * Bidirectional attention is block-diagonal over the packed batch. Each + * sequence's positions restart at cfg->position_id_offset: 0 for BERT, or + * pad_id + 1 for RoBERTa/XLM-R. + * + * Leaves final hidden states in ws->x. Pooling reads them as-is because the last + * layer's LayerNorm is already the model's final normalization. + */ +static int forward_packed_bert(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const ffwd_input_t *inputs, + int batch, + const int *offsets, + int total_seq, + int max_seq) { + if (!model || !ws || ws->model != model || !inputs || batch <= 0 || !offsets || total_seq <= 0 || + max_seq <= 0) + return -1; + + const ffwd_config_t *cfg = &model->config; + const ffwd_weights_t *w = &model->weights; + int hidden = cfg->hidden_size; + int n_heads = cfg->n_heads; + int head_dim = cfg->head_dim; + int inter = cfg->intermediate_size; + float eps = cfg->layer_norm_eps; + + if (!w->embed_tokens.data || !w->position_embeddings || !w->token_type_embeddings || + !w->ffwd_ln_w || !w->ffwd_ln_b) + return -1; + if (max_seq > cfg->max_position_embeddings - cfg->position_id_offset) { + fprintf(stderr, + "ffwd: sequence length %d plus position offset %d exceeds max_position_embeddings " + "%d\n", + max_seq, cfg->position_id_offset, cfg->max_position_embeddings); + return -1; + } + if (ensure_buffers(ws, cfg, total_seq) != 0) + return -1; + ensure_attention_scores(ws, offsets, batch); + + float *x = ws->x; + float *q_buf = ws->q, *k_buf = ws->k, *v_buf = ws->v; + float *attn_out = ws->attn_out, *proj_out = ws->proj_out, *ffn_up = ws->ffn_up; + + /* Embeddings: token + learned absolute position + token-type[0], then the + * embedding LayerNorm over every packed token. */ + for (int b = 0; b < batch; b++) { + int start = offsets[b]; + for (int i = 0; i < inputs[b].n_tokens; i++) { + int id = inputs[b].ids[i]; + if (id < 0 || id >= cfg->vocab_size) { + fprintf(stderr, "ffwd: invalid token id %d at input %d position %d\n", id, b, i); + return -1; + } + float *row = x + (size_t)(start + i) * hidden; + copy_weight_row(row, &w->embed_tokens, (size_t)id, hidden); + int pos = cfg->position_id_offset + i; + add_inplace(row, w->position_embeddings + (size_t)pos * hidden, hidden); + add_inplace(row, w->token_type_embeddings, hidden); + } + } + layer_norm(x, x, w->ffwd_ln_w, w->ffwd_ln_b, total_seq, hidden, eps); + + float scale = 1.0f / sqrtf((float)head_dim); + + for (int layer = 0; layer < cfg->n_layers; layer++) { + const ffwd_layer_t *l = &w->layers[layer]; + + /* Self-attention (post-norm): projections read x directly. */ + linear_qkv_weight(ws, q_buf, k_buf, v_buf, x, &l->wq, &l->wk, &l->wv, total_seq, hidden, + hidden, hidden); + add_bias_rows(q_buf, l->q_bias, total_seq, hidden); + add_bias_rows(k_buf, l->k_bias, total_seq, hidden); + add_bias_rows(v_buf, l->v_bias, total_seq, hidden); + + ffwd_attention_fn attention = attention_for_config(cfg); + attention(attn_out, q_buf, k_buf, v_buf, offsets, batch, n_heads, n_heads, head_dim, scale, + ws->attn_scores, ws->attn_scores_bytes); + + linear_nobias_weight(ws, proj_out, attn_out, &l->wo, total_seq, hidden, hidden); + add_bias_rows(proj_out, l->o_bias, total_seq, hidden); + add_inplace(x, proj_out, total_seq * hidden); + layer_norm(x, x, l->input_norm, l->attn_ln_bias, total_seq, hidden, eps); + + /* Feed-forward (post-norm): dense -> GeLU -> dense. */ + linear_nobias_weight(ws, ffn_up, x, &l->up_proj, total_seq, hidden, inter); + add_bias_rows(ffn_up, l->ffn_inter_bias, total_seq, inter); + if (cfg->ffn_act == FFWD_ACT_GELU_TANH) + gelu_tanh_inplace(ffn_up, total_seq * inter); + else + gelu_inplace(ffn_up, total_seq * inter); + linear_nobias_weight(ws, proj_out, ffn_up, &l->down_proj, total_seq, inter, hidden); + add_bias_rows(proj_out, l->ffn_out_bias, total_seq, hidden); + add_inplace(x, proj_out, total_seq * hidden); + layer_norm(x, x, l->post_attn_norm, l->ffn_ln_bias, total_seq, hidden, eps); + } + return 0; +} + +static int pool_embeddings(const ffwd_model_t *model, + const ffwd_workspace_t *ws, + const int *offsets, + int batch, + float *out_embeddings) { + const ffwd_config_t *cfg = &model->config; + int hidden = cfg->hidden_size; + float eps = cfg->rms_norm_eps; + /* Qwen pools pre-final-norm states, then applies final RMSNorm here. + * BERT pools already-normalized states, so a NULL norm means no final norm. */ + const float *norm_weight = model->weights.norm; + const float *x = ws->x; + if (hidden <= 0 || !x) + return -1; + + for (int b = 0; b < batch; b++) { + int start = offsets[b]; + int end = offsets[b + 1]; + int len = end - start; + float *emb = out_embeddings + (size_t)b * hidden; + + if (cfg->pooling_mode == FFWD_POOL_LAST_TOKEN || cfg->pooling_mode == FFWD_POOL_CLS) { + /* Single-token pooling: CLS uses the first token; last-token uses the final token. */ + int idx = cfg->pooling_mode == FFWD_POOL_CLS ? start : end - 1; + const float *row = x + (size_t)idx * hidden; + if (norm_weight) { + float sum_sq = 0.0f; + for (int d = 0; d < hidden; d++) + sum_sq += row[d] * row[d]; + float rms_inv = 1.0f / sqrtf(sum_sq / (float)hidden + eps); + for (int d = 0; d < hidden; d++) + emb[d] = row[d] * rms_inv * norm_weight[d]; + } else { + memcpy(emb, row, (size_t)hidden * sizeof(float)); + } + } else { + memset(emb, 0, (size_t)hidden * sizeof(float)); + for (int i = start; i < end; i++) { + const float *row = x + (size_t)i * hidden; + if (norm_weight) { + float sum_sq = 0.0f; + for (int d = 0; d < hidden; d++) + sum_sq += row[d] * row[d]; + float rms_inv = 1.0f / sqrtf(sum_sq / (float)hidden + eps); + for (int d = 0; d < hidden; d++) + emb[d] += row[d] * rms_inv * norm_weight[d]; + } else { + for (int d = 0; d < hidden; d++) + emb[d] += row[d]; + } + } + float inv_len = 1.0f / (float)len; + for (int d = 0; d < hidden; d++) + emb[d] *= inv_len; + } + + if (cfg->normalize_embeddings && ffwd_l2_normalize(emb, hidden) != 0) + return -1; + } + return 0; +} + +/* Forward/embed APIs */ + +int ffwd_model_forward_into(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens, + float *out_states) { + if (!model || !ws || !token_ids || n_tokens <= 0 || !out_states) + return -1; + + size_t count, bytes; + if (mul_size((size_t)n_tokens, (size_t)model->config.hidden_size, &count) != 0 || + mul_size(count, sizeof(float), &bytes) != 0) + return -1; + + ffwd_input_t input = {token_ids, n_tokens}; + int offsets[2] = {0, n_tokens}; + if (forward_packed_inplace(model, ws, &input, 1, offsets, n_tokens, n_tokens, 1) != 0) + return -1; + + memcpy(out_states, ws->x, bytes); + return 0; +} + +int ffwd_pool_spans(const ffwd_config_t *cfg, + const float *states, + int n_tokens, + const ffwd_span_t *spans, + int n_spans, + float *out_embeddings) { + if (!cfg || !states || n_tokens <= 0 || !spans || n_spans <= 0 || !out_embeddings) + return -1; + + int hidden = cfg->hidden_size; + if (hidden <= 0) + return -1; + + for (int s = 0; s < n_spans; s++) { + int start = spans[s].start; + int len = spans[s].n_tokens; + if (start < 0 || len <= 0 || start > n_tokens || len > n_tokens - start) + return -1; + + float *emb = out_embeddings + (size_t)s * hidden; + memset(emb, 0, (size_t)hidden * sizeof(float)); + + for (int t = 0; t < len; t++) { + const float *row = states + (size_t)(start + t) * hidden; + for (int d = 0; d < hidden; d++) + emb[d] += row[d]; + } + + float inv_len = 1.0f / (float)len; + for (int d = 0; d < hidden; d++) { + emb[d] *= inv_len; + } + } + return 0; +} + +int ffwd_model_encode_spans_batch(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const ffwd_context_input_t *inputs, + int batch, + float *out_embeddings) { + if (!model || !ws || !inputs || batch <= 0 || !out_embeddings) + return -1; + + if (ensure_offsets(ws, batch) != 0) + return -1; + if ((size_t)batch > SIZE_MAX / sizeof(ffwd_input_t)) + return -1; + ffwd_input_t *packed_inputs = malloc((size_t)batch * sizeof(*packed_inputs)); + if (!packed_inputs) + return -1; + int *offsets = ws->offsets; + int total_seq = 0; + int max_seq = 0; + int total_spans = 0; + for (int b = 0; b < batch; b++) { + const ffwd_context_input_t *input = &inputs[b]; + int n_tokens = input->input.n_tokens; + if (!input->input.ids || n_tokens <= 0 || !input->spans || input->n_spans <= 0 || + total_seq > INT_MAX - n_tokens || total_spans > INT_MAX - input->n_spans) { + free(packed_inputs); + return -1; + } + packed_inputs[b] = input->input; + offsets[b] = total_seq; + total_seq += n_tokens; + total_spans += input->n_spans; + if (n_tokens > max_seq) + max_seq = n_tokens; + for (int s = 0; s < input->n_spans; s++) { + int start = input->spans[s].start; + int len = input->spans[s].n_tokens; + if (start < 0 || len <= 0 || start > n_tokens || len > n_tokens - start) { + free(packed_inputs); + return -1; + } + } + } + offsets[batch] = total_seq; + + if (forward_packed_inplace(model, ws, packed_inputs, batch, offsets, total_seq, max_seq, 1) != + 0) { + free(packed_inputs); + return -1; + } + free(packed_inputs); + + int span_offset = 0; + int hidden = model->config.hidden_size; + for (int b = 0; b < batch; b++) { + const ffwd_context_input_t *input = &inputs[b]; + if (ffwd_pool_spans(&model->config, ws->x + (size_t)offsets[b] * hidden, + input->input.n_tokens, input->spans, input->n_spans, + out_embeddings + (size_t)span_offset * hidden) != 0) + return -1; + span_offset += input->n_spans; + } + return 0; +} + +int ffwd_model_encode_spans(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens, + const ffwd_span_t *spans, + int n_spans, + float *out_embeddings) { + ffwd_context_input_t input = { + .input = {token_ids, n_tokens}, + .spans = spans, + .n_spans = n_spans, + }; + return ffwd_model_encode_spans_batch(model, ws, &input, 1, out_embeddings); +} + +float *ffwd_model_forward(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens) { + if (!model || !ws || !token_ids || n_tokens <= 0) + return NULL; + + size_t n; + if (mul_size((size_t)n_tokens, (size_t)model->config.hidden_size, &n) != 0) + return NULL; + float *out = malloc_floats(n); + if (!out) + return NULL; + + if (ffwd_model_forward_into(model, ws, token_ids, n_tokens, out) != 0) { + free(out); + return NULL; + } + return out; +} + +int ffwd_model_encode_batch(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const ffwd_input_t *inputs, + int batch, + float *out_embeddings) { + if (!model || !ws || !inputs || batch <= 0 || !out_embeddings) + return -1; + + if (ensure_offsets(ws, batch) != 0) + return -1; + int *offsets = ws->offsets; + + int total_seq = 0; + int max_seq = 0; + if (build_offsets(inputs, batch, 1, offsets, &total_seq, &max_seq) != 0) + return -1; + + int rc = model->config.family == FFWD_FAMILY_BERT + ? forward_packed_bert(model, ws, inputs, batch, offsets, total_seq, max_seq) + : forward_packed_inplace(model, ws, inputs, batch, offsets, total_seq, max_seq, 0); + if (rc == 0) + rc = pool_embeddings(model, ws, offsets, batch, out_embeddings); + + return rc; +} + +int ffwd_pool_batch(const ffwd_config_t *cfg, + const float *states, + const int *seq_lengths, + int batch, + float *out_embeddings) { + if (!cfg || !states || !seq_lengths || batch <= 0 || !out_embeddings || cfg->hidden_size <= 0) + return -1; + + int offset = 0; + for (int b = 0; b < batch; b++) { + int len = seq_lengths[b]; + if (len <= 0 || offset > INT_MAX - len) + return -1; + + float *emb = out_embeddings + (size_t)b * cfg->hidden_size; + if (cfg->pooling_mode == FFWD_POOL_LAST_TOKEN || cfg->pooling_mode == FFWD_POOL_CLS) { + /* CLS takes the first token, last-token the final one; these states + * already carry the model's final RMSNorm. */ + int idx = cfg->pooling_mode == FFWD_POOL_CLS ? offset : offset + len - 1; + const float *row = states + (size_t)idx * cfg->hidden_size; + memcpy(emb, row, (size_t)cfg->hidden_size * sizeof(float)); + } else { + memset(emb, 0, (size_t)cfg->hidden_size * sizeof(float)); + for (int t = 0; t < len; t++) { + const float *row = states + (size_t)(offset + t) * cfg->hidden_size; + for (int d = 0; d < cfg->hidden_size; d++) + emb[d] += row[d]; + } + float inv_len = 1.0f / (float)len; + for (int d = 0; d < cfg->hidden_size; d++) + emb[d] *= inv_len; + } + if (cfg->normalize_embeddings && ffwd_l2_normalize(emb, cfg->hidden_size) != 0) + return -1; + offset += len; + } + return 0; +} + +int ffwd_model_encode_into(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens, + float *out_embedding) { + ffwd_input_t input = {token_ids, n_tokens}; + return ffwd_model_encode_batch(model, ws, &input, 1, out_embedding); +} + +float *ffwd_model_encode(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens) { + if (!model || !ws || !token_ids || n_tokens <= 0) + return NULL; + + int hidden = model->config.hidden_size; + if (hidden <= 0) + return NULL; + float *emb = malloc_floats((size_t)hidden); + if (!emb) + return NULL; + + if (ffwd_model_encode_into(model, ws, token_ids, n_tokens, emb) != 0) { + free(emb); + return NULL; + } + return emb; +} diff --git a/libffwd/gemm.c b/libffwd/gemm.c new file mode 100644 index 0000000..ab664f1 --- /dev/null +++ b/libffwd/gemm.c @@ -0,0 +1,429 @@ +/* + * General matrix multiplication. + */ + +#include "kernels.h" +#include "impl.h" +#include "threadpool.h" + +#include <math.h> +#include <stdlib.h> +#include <string.h> + +#if (defined(__AVX512F__) || defined(__AVX2__)) && \ + (defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)) +# include <immintrin.h> +#endif +#if defined(__ARM_NEON) || defined(__ARM_NEON__) +# include <arm_neon.h> +#endif + +#ifdef USE_BLAS +# ifdef __APPLE__ +# include <Accelerate/Accelerate.h> +# else +# include <cblas.h> +# endif +#endif + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +/* Matrix and vector ops: + * GEMM, dtype-dispatch linear kernels, BF16-fused matvec/QKV/pair workers, + * and element-wise residual add. */ + +/* Basic element-wise operations */ + +void add_inplace(float *a, const float *b, int n) { + for (int i = 0; i < n; i++) + a[i] += b[i]; +} + +/* Matrix operations */ + +void matmul_t(float *C, const float *A, const float *B, int M, int K, int N) { +#ifdef USE_BLAS + cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, M, N, K, 1.0f, A, K, B, K, 0.0f, C, N); +#else + for (int m = 0; m < M; m++) { + for (int n = 0; n < N; n++) { + float sum = 0.0f; + for (int k = 0; k < K; k++) { + sum += A[m * K + k] * B[n * K + k]; + } + C[m * N + n] = sum; + } + } +#endif +} + +void linear( + float *y, const float *x, const float *W, const float *b, int seq_len, int in_dim, int out_dim) { +#ifdef USE_BLAS + if (seq_len == 1) { + cblas_sgemv(CblasRowMajor, CblasNoTrans, out_dim, in_dim, 1.0f, W, in_dim, x, 1, 0.0f, y, 1); + } else { + cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, seq_len, out_dim, in_dim, 1.0f, x, + in_dim, W, in_dim, 0.0f, y, out_dim); + } + if (b != NULL) { + for (int s = 0; s < seq_len; s++) { + for (int o = 0; o < out_dim; o++) { + y[s * out_dim + o] += b[o]; + } + } + } +#else + for (int s = 0; s < seq_len; s++) { + const float *x_row = x + s * in_dim; + float *y_row = y + s * out_dim; + for (int o = 0; o < out_dim; o++) { + const float *w_row = W + o * in_dim; + float sum = (b != NULL) ? b[o] : 0.0f; + for (int i = 0; i < in_dim; i++) { + sum += x_row[i] * w_row[i]; + } + y_row[o] = sum; + } + } +#endif +} + +void linear_nobias(float *y, const float *x, const float *W, int seq_len, int in_dim, int out_dim) { + linear(y, x, W, NULL, seq_len, in_dim, out_dim); +} + +/* Convert bf16 buffer to f32 buffer */ +void bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n) { +#if defined(__ARM_NEON) || defined(__ARM_NEON__) + size_t i = 0; + uint32_t *d = (uint32_t *)(void *)dst; + for (; i + 8 <= n; i += 8) { + uint16x8_t v = vld1q_u16(src + i); + uint32x4_t lo = vshlq_n_u32(vmovl_u16(vget_low_u16(v)), 16); + uint32x4_t hi = vshlq_n_u32(vmovl_u16(vget_high_u16(v)), 16); + vst1q_u32(d + i, lo); + vst1q_u32(d + i + 4, hi); + } + for (; i < n; i++) + d[i] = ((uint32_t)src[i]) << 16; +#else + uint32_t *d = (uint32_t *)(void *)dst; + for (size_t i = 0; i < n; i++) + d[i] = ((uint32_t)src[i]) << 16; +#endif +} + +/* + * Fused BF16 matvec: y[out_dim] = W_bf16[out_dim, in_dim] @ x[in_dim] + bias + * Processes 2 output rows at a time to amortize x vector loads. + */ +static void bf16_matvec_fused( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { + bf16_matvec_fused_impl(y, x, W_bf16, bias, in_dim, out_dim); +} + +/* Threaded matvec: split output rows across threads */ +typedef struct { + float *y; + const float *x; + const uint16_t *W_bf16; + const float *bias; + int in_dim; + int out_dim; +} matvec_task_t; + +static void matvec_worker(int tid, int n_threads, void *arg) { + matvec_task_t *t = (matvec_task_t *)arg; + int chunk = (t->out_dim + n_threads - 1) / n_threads; + int start = tid * chunk; + int end = start + chunk; + if (end > t->out_dim) + end = t->out_dim; + if (start >= end) + return; + + bf16_matvec_fused(t->y + start, t->x, t->W_bf16 + (size_t)start * t->in_dim, + t->bias ? t->bias + start : NULL, t->in_dim, end - start); +} + +static void bf16_matvec_threaded( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { + if (tp_num_threads() <= 1) { + bf16_matvec_fused(y, x, W_bf16, bias, in_dim, out_dim); + return; + } + matvec_task_t task = {y, x, W_bf16, bias, in_dim, out_dim}; + parallel_for(matvec_worker, &task); +} + +typedef struct { + float *y; + const float *x; + const uint16_t *W_bf16; + const float *bias; + int seq_len; + int in_dim; + int out_dim; +} bf16_linear_rows_task_t; + +static void bf16_linear_rows_worker(int tid, int n_threads, void *arg) { + bf16_linear_rows_task_t *t = (bf16_linear_rows_task_t *)arg; + int chunk = (t->seq_len + n_threads - 1) / n_threads; + int start = tid * chunk; + int end = start + chunk; + if (end > t->seq_len) + end = t->seq_len; + if (start >= end) + return; + + for (int s = start; s < end; s++) { + bf16_matvec_fused(t->y + (size_t)s * t->out_dim, t->x + (size_t)s * t->in_dim, t->W_bf16, + t->bias, t->in_dim, t->out_dim); + } +} + +static void bf16_linear_rows(float *y, + const float *x, + const uint16_t *W_bf16, + const float *bias, + int seq_len, + int in_dim, + int out_dim) { + bf16_linear_rows_task_t task = { + .y = y, + .x = x, + .W_bf16 = W_bf16, + .bias = bias, + .seq_len = seq_len, + .in_dim = in_dim, + .out_dim = out_dim, + }; + + if (tp_num_threads() > 1 && seq_len >= 2) + parallel_for(bf16_linear_rows_worker, &task); + else + bf16_linear_rows_worker(0, 1, &task); +} + +typedef struct { + float *a; + float *b; + const float *x; + const uint16_t *Wa_bf16; + const uint16_t *Wb_bf16; + int seq_len; + int in_dim; + int a_dim; + int b_dim; + int total_dim; +} pair_matvec_task_t; + +static void pair_matvec_range(const pair_matvec_task_t *t, int row, int start, int end) { + const float *x_row = t->x + (size_t)row * t->in_dim; + int a_end = t->a_dim; + int b_end = a_end + t->b_dim; + + if (start < a_end) { + int s = start; + int e = end < a_end ? end : a_end; + if (s < e) { + bf16_matvec_fused(t->a + (size_t)row * t->a_dim + s, x_row, + t->Wa_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s); + } + } + + if (end > a_end && start < b_end) { + int s = start > a_end ? start - a_end : 0; + int e_abs = end < b_end ? end : b_end; + int e = e_abs - a_end; + if (s < e) { + bf16_matvec_fused(t->b + (size_t)row * t->b_dim + s, x_row, + t->Wb_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s); + } + } +} + +static void pair_matvec_worker(int tid, int n_threads, void *arg) { + pair_matvec_task_t *t = (pair_matvec_task_t *)arg; + int total_work = t->seq_len * t->total_dim; + int chunk = (total_work + n_threads - 1) / n_threads; + int start = tid * chunk; + int end = start + chunk; + if (end > total_work) + end = total_work; + if (start >= end) + return; + + while (start < end) { + int row = start / t->total_dim; + int pos = start - row * t->total_dim; + int row_end = (row + 1) * t->total_dim; + int stop = end < row_end ? end : row_end; + pair_matvec_range(t, row, pos, stop - row * t->total_dim); + start = stop; + } +} + +void linear_nobias_bf16_pair(float *a, + float *b, + const float *x, + const uint16_t *Wa_bf16, + const uint16_t *Wb_bf16, + int seq_len, + int in_dim, + int a_dim, + int b_dim) { + if (seq_len <= 0) + return; + + if (tp_num_threads() <= 1) { + for (int s = 0; s < seq_len; s++) { + const float *x_row = x + (size_t)s * in_dim; + bf16_matvec_fused(a + (size_t)s * a_dim, x_row, Wa_bf16, NULL, in_dim, a_dim); + bf16_matvec_fused(b + (size_t)s * b_dim, x_row, Wb_bf16, NULL, in_dim, b_dim); + } + return; + } + + pair_matvec_task_t task = { + .a = a, + .b = b, + .x = x, + .Wa_bf16 = Wa_bf16, + .Wb_bf16 = Wb_bf16, + .seq_len = seq_len, + .in_dim = in_dim, + .a_dim = a_dim, + .b_dim = b_dim, + .total_dim = a_dim + b_dim, + }; + parallel_for(pair_matvec_worker, &task); +} + +typedef struct { + float *q; + float *k; + float *v; + const float *x; + const uint16_t *Wq_bf16; + const uint16_t *Wk_bf16; + const uint16_t *Wv_bf16; + int in_dim; + int q_dim; + int kv_dim; + int seq_len; + int total_dim; +} qkv_matvec_task_t; + +static void qkv_matvec_range(const qkv_matvec_task_t *t, int row, int start, int end) { + const float *x_row = t->x + (size_t)row * t->in_dim; + int q_end = t->q_dim; + int k_end = q_end + t->kv_dim; + int v_end = k_end + t->kv_dim; + + if (start < q_end) { + int s = start; + int e = end < q_end ? end : q_end; + if (s < e) { + bf16_matvec_fused(t->q + (size_t)row * t->q_dim + s, x_row, + t->Wq_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s); + } + } + + if (end > q_end && start < k_end) { + int s = start > q_end ? start - q_end : 0; + int e_abs = end < k_end ? end : k_end; + int e = e_abs - q_end; + if (s < e) { + bf16_matvec_fused(t->k + (size_t)row * t->kv_dim + s, x_row, + t->Wk_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s); + } + } + + if (end > k_end && start < v_end) { + int s = start > k_end ? start - k_end : 0; + int e_abs = end < v_end ? end : v_end; + int e = e_abs - k_end; + if (s < e) { + bf16_matvec_fused(t->v + (size_t)row * t->kv_dim + s, x_row, + t->Wv_bf16 + (size_t)s * t->in_dim, NULL, t->in_dim, e - s); + } + } +} + +static void qkv_matvec_worker(int tid, int n_threads, void *arg) { + qkv_matvec_task_t *t = (qkv_matvec_task_t *)arg; + int total_work = t->seq_len * t->total_dim; + int chunk = (total_work + n_threads - 1) / n_threads; + int start = tid * chunk; + int end = start + chunk; + if (end > total_work) + end = total_work; + if (start >= end) + return; + + while (start < end) { + int row = start / t->total_dim; + int pos = start - row * t->total_dim; + int row_end = (row + 1) * t->total_dim; + int stop = end < row_end ? end : row_end; + qkv_matvec_range(t, row, pos, stop - row * t->total_dim); + start = stop; + } +} + +void linear_nobias_bf16_qkv(float *q, + float *k, + float *v, + const float *x, + const uint16_t *Wq_bf16, + const uint16_t *Wk_bf16, + const uint16_t *Wv_bf16, + int seq_len, + int in_dim, + int q_dim, + int kv_dim) { + if (seq_len <= 0) + return; + + if (tp_num_threads() <= 1) { + for (int s = 0; s < seq_len; s++) { + const float *x_row = x + (size_t)s * in_dim; + bf16_matvec_fused(q + (size_t)s * q_dim, x_row, Wq_bf16, NULL, in_dim, q_dim); + bf16_matvec_fused(k + (size_t)s * kv_dim, x_row, Wk_bf16, NULL, in_dim, kv_dim); + bf16_matvec_fused(v + (size_t)s * kv_dim, x_row, Wv_bf16, NULL, in_dim, kv_dim); + } + return; + } + + qkv_matvec_task_t task = { + .q = q, + .k = k, + .v = v, + .x = x, + .Wq_bf16 = Wq_bf16, + .Wk_bf16 = Wk_bf16, + .Wv_bf16 = Wv_bf16, + .in_dim = in_dim, + .q_dim = q_dim, + .kv_dim = kv_dim, + .seq_len = seq_len, + .total_dim = q_dim + 2 * kv_dim, + }; + parallel_for(qkv_matvec_worker, &task); +} + +void linear_nobias_bf16( + float *y, const float *x, const uint16_t *W_bf16, int seq_len, int in_dim, int out_dim) { + if (seq_len == 1) { + bf16_matvec_threaded(y, x, W_bf16, NULL, in_dim, out_dim); + return; + } + /* Callers send longer sequences through an F32-widened weight matrix in + * caller-owned scratch. + * This per-row path remains correct for any length. */ + bf16_linear_rows(y, x, W_bf16, NULL, seq_len, in_dim, out_dim); +} diff --git a/libffwd/generic.c b/libffwd/generic.c new file mode 100644 index 0000000..5425247 --- /dev/null +++ b/libffwd/generic.c @@ -0,0 +1,44 @@ +/* + * Architecture-generic hot kernels / non-SIMD fallback. + */ + +#include "impl.h" + +#include <string.h> + +void bf16_matvec_fused_generic( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { + for (int o = 0; o < out_dim; o++) { + const uint16_t *w_row = W_bf16 + (size_t)o * in_dim; + float sum = bias ? bias[o] : 0.0f; + for (int k = 0; k < in_dim; k++) { + uint32_t f32_bits = ((uint32_t)w_row[k]) << 16; + float w_val; + memcpy(&w_val, &f32_bits, sizeof(float)); + sum += w_val * x[k]; + } + y[o] = sum; + } +} + +float dot_f32_generic(const float *a, const float *b, int n) { + float sum = 0.0f; + for (int i = 0; i < n; i++) + sum += a[i] * b[i]; + return sum; +} + +void vec_scale_inplace_generic(float *dst, float scale, int n) { + for (int i = 0; i < n; i++) + dst[i] *= scale; +} + +void vec_axpy_inplace_generic(float *dst, const float *src, float alpha, int n) { + for (int i = 0; i < n; i++) + dst[i] += alpha * src[i]; +} + +void vec_scale_add_generic(float *dst, const float *src, float correction, int n) { + for (int i = 0; i < n; i++) + dst[i] = dst[i] * correction + src[i]; +} diff --git a/libffwd/impl.h b/libffwd/impl.h new file mode 100644 index 0000000..2149cf2 --- /dev/null +++ b/libffwd/impl.h @@ -0,0 +1,280 @@ +/* + * Internal architecture dispatch for hot kernels. + * + * Small vector primitives (dot product, scale, axpy, scale-add, scaled + * multiply) are static inline here so they fold into the caller's loop. + * For example, an RMSNorm or attention row has no call overhead, and the + * compiler can keep the row in registers across reduction and write-back. + * + * Generic scalar versions stay out of line in generic.c. They are the reference + * for kernel tests and the fallback when no SIMD path is available. + * + * bf16_matvec_fused is a large full matrix-vector product, called once per + * output tile rather than inside a tight loop, so it stays out of line. + */ + +#ifndef KERNELS_IMPL_H +#define KERNELS_IMPL_H + +#include <stdint.h> + +/* Scalar references (generic.c): the kernel tests compare the SIMD results + * against these, and the no-SIMD build dispatches to them directly. */ +void bf16_matvec_fused_generic( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim); +float dot_f32_generic(const float *a, const float *b, int n); +void vec_scale_inplace_generic(float *dst, float scale, int n); +void vec_axpy_inplace_generic(float *dst, const float *src, float alpha, int n); +void vec_scale_add_generic(float *dst, const float *src, float correction, int n); + +#ifdef __ARM_NEON + +# include <arm_neon.h> + +void bf16_matvec_fused_neon( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim); +# define bf16_matvec_fused_impl bf16_matvec_fused_neon + +static inline float dot_f32_impl(const float *a, const float *b, int n) { + int i = 0; + float32x4_t acc0 = vdupq_n_f32(0.0f); + float32x4_t acc1 = vdupq_n_f32(0.0f); + for (; i + 8 <= n; i += 8) { + float32x4_t a0 = vld1q_f32(a + i); + float32x4_t b0 = vld1q_f32(b + i); + float32x4_t a1 = vld1q_f32(a + i + 4); + float32x4_t b1 = vld1q_f32(b + i + 4); + acc0 = vfmaq_f32(acc0, a0, b0); + acc1 = vfmaq_f32(acc1, a1, b1); + } + float sum = vaddvq_f32(vaddq_f32(acc0, acc1)); + for (; i < n; i++) + sum += a[i] * b[i]; + return sum; +} + +static inline void vec_scale_inplace_impl(float *dst, float scale, int n) { + int i = 0; + float32x4_t s = vdupq_n_f32(scale); + for (; i + 8 <= n; i += 8) { + float32x4_t d0 = vld1q_f32(dst + i); + float32x4_t d1 = vld1q_f32(dst + i + 4); + vst1q_f32(dst + i, vfmaq_f32(vdupq_n_f32(0.0f), d0, s)); + vst1q_f32(dst + i + 4, vfmaq_f32(vdupq_n_f32(0.0f), d1, s)); + } + for (; i < n; i++) + dst[i] *= scale; +} + +static inline void vec_axpy_inplace_impl(float *dst, const float *src, float alpha, int n) { + int i = 0; + float32x4_t a = vdupq_n_f32(alpha); + for (; i + 8 <= n; i += 8) { + float32x4_t d0 = vld1q_f32(dst + i); + float32x4_t s0 = vld1q_f32(src + i); + float32x4_t d1 = vld1q_f32(dst + i + 4); + float32x4_t s1 = vld1q_f32(src + i + 4); + vst1q_f32(dst + i, vfmaq_f32(d0, s0, a)); + vst1q_f32(dst + i + 4, vfmaq_f32(d1, s1, a)); + } + for (; i < n; i++) + dst[i] += alpha * src[i]; +} + +static inline void vec_scale_add_impl(float *dst, const float *src, float correction, int n) { + int i = 0; + float32x4_t c = vdupq_n_f32(correction); + for (; i + 8 <= n; i += 8) { + float32x4_t d0 = vld1q_f32(dst + i); + float32x4_t s0 = vld1q_f32(src + i); + float32x4_t d1 = vld1q_f32(dst + i + 4); + float32x4_t s1 = vld1q_f32(src + i + 4); + vst1q_f32(dst + i, vfmaq_f32(s0, d0, c)); + vst1q_f32(dst + i + 4, vfmaq_f32(s1, d1, c)); + } + for (; i < n; i++) + dst[i] = dst[i] * correction + src[i]; +} + +#elif defined(__AVX2__) && defined(__FMA__) + +# include <immintrin.h> + +void bf16_matvec_fused_avx( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim); +# define bf16_matvec_fused_impl bf16_matvec_fused_avx + +static inline float dot_f32_impl(const float *a, const float *b, int n) { +# if defined(__AVX512F__) + int i = 0; + __m512 acc0 = _mm512_setzero_ps(); + __m512 acc1 = _mm512_setzero_ps(); + __m512 acc2 = _mm512_setzero_ps(); + __m512 acc3 = _mm512_setzero_ps(); + for (; i + 64 <= n; i += 64) { + acc0 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i), _mm512_loadu_ps(b + i), acc0); + acc1 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 16), _mm512_loadu_ps(b + i + 16), acc1); + acc2 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 32), _mm512_loadu_ps(b + i + 32), acc2); + acc3 = _mm512_fmadd_ps(_mm512_loadu_ps(a + i + 48), _mm512_loadu_ps(b + i + 48), acc3); + } + __m512 acc = _mm512_add_ps(_mm512_add_ps(acc0, acc1), _mm512_add_ps(acc2, acc3)); + for (; i + 16 <= n; i += 16) { + acc = _mm512_fmadd_ps(_mm512_loadu_ps(a + i), _mm512_loadu_ps(b + i), acc); + } + float sum = _mm512_reduce_add_ps(acc); + for (; i < n; i++) + sum += a[i] * b[i]; + return sum; +# else + int i = 0; + __m256 acc0 = _mm256_setzero_ps(); + __m256 acc1 = _mm256_setzero_ps(); + __m256 acc2 = _mm256_setzero_ps(); + __m256 acc3 = _mm256_setzero_ps(); + for (; i + 32 <= n; i += 32) { + acc0 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i), _mm256_loadu_ps(b + i), acc0); + acc1 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 8), _mm256_loadu_ps(b + i + 8), acc1); + acc2 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 16), _mm256_loadu_ps(b + i + 16), acc2); + acc3 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i + 24), _mm256_loadu_ps(b + i + 24), acc3); + } + acc0 = _mm256_add_ps(_mm256_add_ps(acc0, acc1), _mm256_add_ps(acc2, acc3)); + for (; i + 8 <= n; i += 8) { + acc0 = _mm256_fmadd_ps(_mm256_loadu_ps(a + i), _mm256_loadu_ps(b + i), acc0); + } + __m128 r = _mm_add_ps(_mm256_castps256_ps128(acc0), _mm256_extractf128_ps(acc0, 1)); + r = _mm_hadd_ps(r, r); + r = _mm_hadd_ps(r, r); + float sum = _mm_cvtss_f32(r); + for (; i < n; i++) + sum += a[i] * b[i]; + return sum; +# endif +} + +static inline void vec_scale_inplace_impl(float *dst, float scale, int n) { +# if defined(__AVX512F__) + int i = 0; + __m512 s = _mm512_set1_ps(scale); + for (; i + 64 <= n; i += 64) { + _mm512_storeu_ps(dst + i, _mm512_mul_ps(_mm512_loadu_ps(dst + i), s)); + _mm512_storeu_ps(dst + i + 16, _mm512_mul_ps(_mm512_loadu_ps(dst + i + 16), s)); + _mm512_storeu_ps(dst + i + 32, _mm512_mul_ps(_mm512_loadu_ps(dst + i + 32), s)); + _mm512_storeu_ps(dst + i + 48, _mm512_mul_ps(_mm512_loadu_ps(dst + i + 48), s)); + } + for (; i + 16 <= n; i += 16) { + _mm512_storeu_ps(dst + i, _mm512_mul_ps(_mm512_loadu_ps(dst + i), s)); + } + for (; i < n; i++) + dst[i] *= scale; +# else + int i = 0; + __m256 s = _mm256_set1_ps(scale); + for (; i + 32 <= n; i += 32) { + _mm256_storeu_ps(dst + i, _mm256_mul_ps(_mm256_loadu_ps(dst + i), s)); + _mm256_storeu_ps(dst + i + 8, _mm256_mul_ps(_mm256_loadu_ps(dst + i + 8), s)); + _mm256_storeu_ps(dst + i + 16, _mm256_mul_ps(_mm256_loadu_ps(dst + i + 16), s)); + _mm256_storeu_ps(dst + i + 24, _mm256_mul_ps(_mm256_loadu_ps(dst + i + 24), s)); + } + for (; i + 8 <= n; i += 8) { + _mm256_storeu_ps(dst + i, _mm256_mul_ps(_mm256_loadu_ps(dst + i), s)); + } + for (; i < n; i++) + dst[i] *= scale; +# endif +} + +static inline void vec_axpy_inplace_impl(float *dst, const float *src, float alpha, int n) { +# if defined(__AVX512F__) + int i = 0; + __m512 a = _mm512_set1_ps(alpha); + for (; i + 64 <= n; i += 64) { + _mm512_storeu_ps(dst + i, + _mm512_fmadd_ps(_mm512_loadu_ps(src + i), a, _mm512_loadu_ps(dst + i))); + _mm512_storeu_ps(dst + i + 16, _mm512_fmadd_ps(_mm512_loadu_ps(src + i + 16), a, + _mm512_loadu_ps(dst + i + 16))); + _mm512_storeu_ps(dst + i + 32, _mm512_fmadd_ps(_mm512_loadu_ps(src + i + 32), a, + _mm512_loadu_ps(dst + i + 32))); + _mm512_storeu_ps(dst + i + 48, _mm512_fmadd_ps(_mm512_loadu_ps(src + i + 48), a, + _mm512_loadu_ps(dst + i + 48))); + } + for (; i + 16 <= n; i += 16) { + _mm512_storeu_ps(dst + i, + _mm512_fmadd_ps(_mm512_loadu_ps(src + i), a, _mm512_loadu_ps(dst + i))); + } + for (; i < n; i++) + dst[i] += alpha * src[i]; +# else + int i = 0; + __m256 a = _mm256_set1_ps(alpha); + for (; i + 32 <= n; i += 32) { + _mm256_storeu_ps(dst + i, + _mm256_fmadd_ps(_mm256_loadu_ps(src + i), a, _mm256_loadu_ps(dst + i))); + _mm256_storeu_ps(dst + i + 8, _mm256_fmadd_ps(_mm256_loadu_ps(src + i + 8), a, + _mm256_loadu_ps(dst + i + 8))); + _mm256_storeu_ps(dst + i + 16, _mm256_fmadd_ps(_mm256_loadu_ps(src + i + 16), a, + _mm256_loadu_ps(dst + i + 16))); + _mm256_storeu_ps(dst + i + 24, _mm256_fmadd_ps(_mm256_loadu_ps(src + i + 24), a, + _mm256_loadu_ps(dst + i + 24))); + } + for (; i + 8 <= n; i += 8) { + _mm256_storeu_ps(dst + i, + _mm256_fmadd_ps(_mm256_loadu_ps(src + i), a, _mm256_loadu_ps(dst + i))); + } + for (; i < n; i++) + dst[i] += alpha * src[i]; +# endif +} + +static inline void vec_scale_add_impl(float *dst, const float *src, float correction, int n) { +# if defined(__AVX512F__) + int i = 0; + __m512 c = _mm512_set1_ps(correction); + for (; i + 64 <= n; i += 64) { + _mm512_storeu_ps(dst + i, + _mm512_fmadd_ps(_mm512_loadu_ps(dst + i), c, _mm512_loadu_ps(src + i))); + _mm512_storeu_ps(dst + i + 16, _mm512_fmadd_ps(_mm512_loadu_ps(dst + i + 16), c, + _mm512_loadu_ps(src + i + 16))); + _mm512_storeu_ps(dst + i + 32, _mm512_fmadd_ps(_mm512_loadu_ps(dst + i + 32), c, + _mm512_loadu_ps(src + i + 32))); + _mm512_storeu_ps(dst + i + 48, _mm512_fmadd_ps(_mm512_loadu_ps(dst + i + 48), c, + _mm512_loadu_ps(src + i + 48))); + } + for (; i + 16 <= n; i += 16) { + _mm512_storeu_ps(dst + i, + _mm512_fmadd_ps(_mm512_loadu_ps(dst + i), c, _mm512_loadu_ps(src + i))); + } + for (; i < n; i++) + dst[i] = dst[i] * correction + src[i]; +# else + int i = 0; + __m256 c = _mm256_set1_ps(correction); + for (; i + 32 <= n; i += 32) { + _mm256_storeu_ps(dst + i, + _mm256_fmadd_ps(_mm256_loadu_ps(dst + i), c, _mm256_loadu_ps(src + i))); + _mm256_storeu_ps(dst + i + 8, _mm256_fmadd_ps(_mm256_loadu_ps(dst + i + 8), c, + _mm256_loadu_ps(src + i + 8))); + _mm256_storeu_ps(dst + i + 16, _mm256_fmadd_ps(_mm256_loadu_ps(dst + i + 16), c, + _mm256_loadu_ps(src + i + 16))); + _mm256_storeu_ps(dst + i + 24, _mm256_fmadd_ps(_mm256_loadu_ps(dst + i + 24), c, + _mm256_loadu_ps(src + i + 24))); + } + for (; i + 8 <= n; i += 8) { + _mm256_storeu_ps(dst + i, + _mm256_fmadd_ps(_mm256_loadu_ps(dst + i), c, _mm256_loadu_ps(src + i))); + } + for (; i < n; i++) + dst[i] = dst[i] * correction + src[i]; +# endif +} + +#else /* no SIMD path: dispatch to the scalar references */ + +# define bf16_matvec_fused_impl bf16_matvec_fused_generic +# define dot_f32_impl dot_f32_generic +# define vec_scale_inplace_impl vec_scale_inplace_generic +# define vec_axpy_inplace_impl vec_axpy_inplace_generic +# define vec_scale_add_impl vec_scale_add_generic + +#endif + +#endif /* KERNELS_IMPL_H */ diff --git a/libffwd/internal.h b/libffwd/internal.h new file mode 100644 index 0000000..6030c36 --- /dev/null +++ b/libffwd/internal.h @@ -0,0 +1,129 @@ +#ifndef FFWD_INTERNAL_H +#define FFWD_INTERNAL_H + +#include "ffwd.h" + +#include <stddef.h> +#include <stdio.h> + +typedef struct ffwd_model ffwd_model_t; +typedef struct ffwd_workspace ffwd_workspace_t; +typedef struct ffwd_late_model ffwd_late_model_t; +typedef struct ffwd_late_workspace ffwd_late_workspace_t; + +/* Help text used by the frontends. */ +/* TODO: tools should not depend on the internal API */ +void ffwd_cli_help(FILE *f); +void ffwd_server_help(FILE *f); + +/* + * Lower-level CPU model API. These functions are used by backend glue, tests, + * benchmarks, and parity harnesses; they are not exported from libffwd. + */ +ffwd_model_t *ffwd_model_load(const char *model_dir); +void ffwd_model_free(ffwd_model_t *model); + +ffwd_workspace_t *ffwd_workspace_new(const ffwd_model_t *model); +void ffwd_workspace_free(ffwd_workspace_t *ws); + +const ffwd_config_t *ffwd_model_config(const ffwd_model_t *model); +size_t ffwd_workspace_nbytes(const ffwd_workspace_t *ws); + +float *ffwd_model_encode(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens); + +int ffwd_model_encode_into(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens, + float *out_embedding); + +int ffwd_model_encode_batch(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const ffwd_input_t *inputs, + int batch, + float *out_embeddings); + +int ffwd_pool_batch(const ffwd_config_t *cfg, + const float *states, + const int *seq_lengths, + int batch, + float *out_embeddings); + +float *ffwd_model_forward(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens); + +int ffwd_model_forward_into(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens, + float *out_states); + +int ffwd_pool_spans(const ffwd_config_t *cfg, + const float *states, + int n_tokens, + const ffwd_span_t *spans, + int n_spans, + float *out_embeddings); + +int ffwd_model_encode_spans(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const int *token_ids, + int n_tokens, + const ffwd_span_t *spans, + int n_spans, + float *out_embeddings); + +int ffwd_model_encode_spans_batch(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const ffwd_context_input_t *inputs, + int batch, + float *out_embeddings); + +/* + * Late-interaction CPU model API. The public engine path exposes reranking via + * ffwd_rerank(); these entry points are for tests/backends only. + */ +ffwd_late_model_t *ffwd_late_model_load(const char *model_dir); +void ffwd_late_model_free(ffwd_late_model_t *model); + +ffwd_late_workspace_t *ffwd_late_workspace_new(const ffwd_late_model_t *model); +void ffwd_late_workspace_free(ffwd_late_workspace_t *ws); + +const ffwd_config_t *ffwd_late_model_config(const ffwd_late_model_t *model); +int ffwd_late_model_token_dim(const ffwd_late_model_t *model); + +int ffwd_late_model_encode_tokens(const ffwd_late_model_t *model, + ffwd_late_workspace_t *ws, + const int *token_ids, + int n_tokens, + int normalize, + float *out_vectors); + +int ffwd_late_model_encode_docs(const ffwd_late_model_t *model, + ffwd_late_workspace_t *ws, + const int *const *doc_ids, + const int *n_tokens, + const int *const *keep, + const int *n_keep, + int n_docs, + int normalize, + float *out_vectors, + int *out_offsets); + +float ffwd_late_maxsim( + const float *query_vectors, int query_tokens, const float *doc_vectors, int doc_tokens, int dim); + +int ffwd_late_maxsim_batch(const float *query_vectors, + int query_tokens, + const float *doc_vectors, + const int *doc_offsets, + int docs, + int dim, + float *scores); + +#endif /* FFWD_INTERNAL_H */ diff --git a/libffwd/kernels.h b/libffwd/kernels.h new file mode 100644 index 0000000..11312db --- /dev/null +++ b/libffwd/kernels.h @@ -0,0 +1,181 @@ +/* + * Transformer inference kernels (CPU, float32). + * + * Low-level math operations. All operate on float32 tensors in row-major order. + */ + +#ifndef KERNELS_H +#define KERNELS_H + +#include <stddef.h> +#include <stdint.h> + +/* Basic operations */ + +void add_inplace(float *a, const float *b, int n); + +/* Matrix operations */ + +/* C = A @ B^T: A[M,K], B[N,K], C[M,N] */ +void matmul_t(float *C, const float *A, const float *B, int M, int K, int N); + +/* y = x @ W^T + b: x[seq,in], W[out,in], b[out], y[seq,out] */ +void linear( + float *y, const float *x, const float *W, const float *b, int seq_len, int in_dim, int out_dim); + +void linear_nobias(float *y, const float *x, const float *W, int seq_len, int in_dim, int out_dim); + +/* bf16 weight variants */ + +void bf16_to_f32_buf(float *dst, const uint16_t *src, size_t n); +void linear_nobias_bf16( + float *y, const float *x, const uint16_t *W_bf16, int seq_len, int in_dim, int out_dim); + +/* Small-sequence BF16 path: compute two projections with one threaded dispatch. */ +void linear_nobias_bf16_pair(float *a, + float *b, + const float *x, + const uint16_t *Wa_bf16, + const uint16_t *Wb_bf16, + int seq_len, + int in_dim, + int a_dim, + int b_dim); + +/* Small-sequence BF16 path: compute Q/K/V matvecs with one threaded dispatch. */ +void linear_nobias_bf16_qkv(float *q, + float *k, + float *v, + const float *x, + const uint16_t *Wq_bf16, + const uint16_t *Wk_bf16, + const uint16_t *Wv_bf16, + int seq_len, + int in_dim, + int q_dim, + int kv_dim); + +/* Dot product using the best available local SIMD implementation. */ +float dot_f32(const float *a, const float *b, int n); + +/* Normalization */ + +/* RMS Normalization: out = x / rms(x) * weight */ +void rms_norm(float *out, const float *x, const float *weight, int seq_len, int hidden, float eps); + +/* Per-head RMS Normalization for Q/K norms in decoder + * x: [seq, n_heads, head_dim], weight: [head_dim] + * Normalizes each head independently */ +void rms_norm_per_head( + float *x, const float *weight, int seq_len, int n_heads, int head_dim, float eps); + +/* Layer normalization for BERT-family models: + * out = gamma * (x - mean) / sqrt(var + eps) + beta. + * Mean and biased/population variance are computed over the hidden axis per row. + * Unlike RMSNorm, this subtracts the mean and adds a bias. */ +void layer_norm(float *out, + const float *x, + const float *gamma, + const float *beta, + int seq_len, + int hidden, + float eps); + +/* Activation functions */ + +void softmax(float *x, int rows, int cols); +/* gate = SiLU(gate) * up */ +void silu_mul_inplace(float *gate, const float *up, int n); + +/* Exact (erf) GeLU in place: x = 0.5 * x * (1 + erf(x / sqrt(2))). + * This is the "gelu" activation HF BERT/BGE/MiniLM use, not the tanh approximation. */ +void gelu_inplace(float *x, int n); + +/* Tanh-approximation GeLU in place (HF gelu_new / gelu_pytorch_tanh): + * x = 0.5 * x * (1 + tanh(sqrt(2/pi) * (x + 0.044715 * x^3))). */ +void gelu_tanh_inplace(float *x, int n); + +/* Attention operations */ + +/* + * Bidirectional GQA attention over a packed/ragged batch. + * + * Q: [total_seq, n_heads * head_dim] + * K,V: [total_seq, n_kv_heads * head_dim] + * offsets: [batch + 1], token spans for each sequence. + * + * Attention is block-diagonal: tokens only attend within their own + * offsets[b]..offsets[b+1] span. + */ +void bidirectional_gqa_attention_packed(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale); + +/* Optional reusable scratch for the BLAS-tiled packed attention path. */ +size_t bidirectional_gqa_attention_packed_scratch_bytes(const int *offsets, int batch); +void bidirectional_gqa_attention_packed_with_scratch(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + float *scratch, + size_t scratch_bytes); + +/* + * Causal GQA attention over a packed/ragged batch. Query position i attends + * only to positions at or before i within its sequence. + */ +void causal_gqa_attention_packed(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale); +void causal_gqa_attention_packed_with_scratch(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + float *scratch, + size_t scratch_bytes); + +/* Position embeddings */ + +/* + * NeoX-style RoPE: compute cos/sin for positions. + * cos_out, sin_out: [seq, head_dim] + * cos[d] and cos[half+d] are the same (duplicated for full head_dim). + */ +void compute_rope_neox( + float *cos_out, float *sin_out, const int *positions, int seq, int head_dim, float theta); + +/* + * Apply NeoX-style RoPE to Q or K. + * x: [seq, n_heads * head_dim] (in-place) + * cos_vals, sin_vals: [seq, head_dim] + */ +void apply_rope_neox( + float *x, const float *cos_vals, const float *sin_vals, int seq, int n_heads, int head_dim); + +#endif /* KERNELS_H */ diff --git a/libffwd/model.c b/libffwd/model.c new file mode 100644 index 0000000..7346e32 --- /dev/null +++ b/libffwd/model.c @@ -0,0 +1,511 @@ +#include "ffwd.h" +#include "model_internal.h" +#include "config.h" +#include "dtype.h" + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> + +/* Model-directory probes */ + +static int model_dir_has_file(const char *model_dir, const char *rel) { + char path[1024]; + int n = snprintf(path, sizeof(path), "%s/%s", model_dir, rel); + if (n < 0 || (size_t)n >= sizeof(path)) + return 0; + FILE *f = fopen(path, "rb"); + if (!f) + return 0; + fclose(f); + return 1; +} + +int model_dir_has_late_projection(const char *model_dir) { + return model_dir_has_file(model_dir, "1_Dense/config.json") && + model_dir_has_file(model_dir, "1_Dense/model.safetensors"); +} + +/* Weight tensor loading and validation (direct mmap pointers into safetensors) */ + +static size_t dtype_size(safetensor_dtype_t dtype) { + switch (dtype) { + case DTYPE_F32: + return sizeof(float); + case DTYPE_BF16: + case DTYPE_F16: + return sizeof(uint16_t); + default: + return 0; + } +} + +static const char *dtype_name(safetensor_dtype_t dtype) { + switch (dtype) { + case DTYPE_F32: + return "F32"; + case DTYPE_F16: + return "F16"; + case DTYPE_BF16: + return "BF16"; + case DTYPE_I32: + return "I32"; + case DTYPE_I64: + return "I64"; + case DTYPE_BOOL: + return "BOOL"; + default: + return "UNKNOWN"; + } +} + +int tensor_has_supported_shape(const safetensors_file_t *sf, + const safetensor_t *t, + const char *name, + const int64_t *shape, + int ndim, + int bf16_ok) { + /* bf16_ok admits either 16-bit float format (BF16 or F16); both are widened + * to F32 by the caller (BF16 lazily per matmul, F16 once at load). */ + int sixteen_ok = bf16_ok && (t->dtype == DTYPE_BF16 || t->dtype == DTYPE_F16); + if (t->dtype != DTYPE_F32 && !sixteen_ok) { + fprintf(stderr, "ffwd: unsupported dtype for %s: got %s, expected %s\n", name, + dtype_name(t->dtype), bf16_ok ? "F32, BF16, or F16" : "F32"); + return 0; + } + if (t->ndim != ndim) { + fprintf(stderr, "ffwd: bad rank for %s: got %d, expected %d\n", name, t->ndim, ndim); + return 0; + } + + size_t numel = 1; + for (int i = 0; i < ndim; i++) { + if (t->shape[i] != shape[i]) { + fprintf(stderr, + "ffwd: bad shape for %s at dim %d: " + "got %lld, expected %lld\n", + name, i, (long long)t->shape[i], (long long)shape[i]); + return 0; + } + if (shape[i] < 0 || (uint64_t)shape[i] > SIZE_MAX / numel) { + fprintf(stderr, "ffwd: shape too large for %s\n", name); + return 0; + } + numel *= (size_t)shape[i]; + } + + size_t elem_size = dtype_size(t->dtype); + if (elem_size == 0) { + fprintf(stderr, "ffwd: unsupported dtype for %s: got %s\n", name, dtype_name(t->dtype)); + return 0; + } + + size_t bytes; + if (mul_size(numel, elem_size, &bytes) != 0) { + fprintf(stderr, "ffwd: tensor data too large for %s\n", name); + return 0; + } + if (t->data_size != bytes) { + fprintf(stderr, "ffwd: bad data size for %s: got %zu, expected %zu\n", name, t->data_size, + bytes); + return 0; + } + + size_t data_start = 8 + sf->header_size; + if (data_start > sf->file_size || t->data_offset > sf->file_size - data_start || + t->data_size > sf->file_size - data_start - t->data_offset) { + fprintf(stderr, "ffwd: tensor data out of bounds for %s\n", name); + return 0; + } + return 1; +} + +/* Widen a safetensors tensor to f32. Caller frees. */ +static float *safetensors_get_f32(const safetensors_file_t *sf, const safetensor_t *t) { + int64_t n = safetensor_numel(t); + if (n <= 0) + return NULL; + float *out = malloc((size_t)n * sizeof(float)); + if (!out) + return NULL; + const void *data = safetensors_data(sf, t); + switch (t->dtype) { + case DTYPE_F32: + memcpy(out, data, (size_t)n * sizeof(float)); + break; + case DTYPE_BF16: { + const uint16_t *s = (const uint16_t *)data; + for (int64_t i = 0; i < n; i++) + out[i] = ffwd_bf16_to_f32(s[i]); + break; + } + case DTYPE_F16: { + const uint16_t *s = (const uint16_t *)data; + for (int64_t i = 0; i < n; i++) + out[i] = ffwd_f16_to_f32(s[i]); + break; + } + default: + free(out); + return NULL; + } + return out; +} + +ffwd_weight_ref_t weight_ref_from_tensor(const safetensors_file_t *sf, const safetensor_t *t) { + ffwd_weight_ref_t ref = {DTYPE_UNKNOWN, NULL, 0}; + /* F32/BF16 are consumed directly by the forward pass, so the ref borrows the + * mmap (zero copy; BF16 is widened lazily per matmul). F16 has neither a + * fused kernel nor a zero-copy F32 view, so widen it once here into an owned + * buffer the model frees on unload. DTYPE_F16 never reaches the forward. */ + if (t->dtype == DTYPE_F16) { + float *buf = safetensors_get_f32(sf, t); + if (!buf) + return ref; + ref.dtype = DTYPE_F32; + ref.data = buf; + ref.owned = 1; + } else { + ref.dtype = t->dtype; + ref.data = safetensors_data(sf, t); + } + return ref; +} + +static ffwd_weight_ref_t +load_weight_direct(multi_safetensors_t *ms, const char *name, const int64_t *shape, int ndim) { + ffwd_weight_ref_t ref = {DTYPE_UNKNOWN, NULL, 0}; + safetensors_file_t *sf = NULL; + const safetensor_t *t = multi_safetensors_find(ms, name, &sf); + if (!t) { + fprintf(stderr, "ffwd: tensor not found: %s\n", name); + return ref; + } + if (!tensor_has_supported_shape(sf, t, name, shape, ndim, 1)) + return ref; + + return weight_ref_from_tensor(sf, t); +} + +static float * +load_norm_f32(multi_safetensors_t *ms, const char *name, const int64_t *shape, int ndim) { + safetensors_file_t *sf = NULL; + const safetensor_t *t = multi_safetensors_find(ms, name, &sf); + if (!t) { + fprintf(stderr, "ffwd: tensor not found: %s\n", name); + return NULL; + } + if (!tensor_has_supported_shape(sf, t, name, shape, ndim, 1)) + return NULL; + return safetensors_get_f32(sf, t); +} + +/* Model / workspace load and free */ + +/* BERT-family weight loader. + * Tensor names differ completely from Qwen + * (encoder.layer.N.*, embeddings.*), so this family has its own entry point. + * + * Reused ffwd_layer_t slots keep their BERT meanings: + * wo is the attention output dense; + * input_norm/post_attn_norm are the two block LayerNorm weights; + * up_proj/down_proj are the two FFN matrices. + * + * See internal.h. + */ +static int +load_bert_weights(ffwd_model_t *model, multi_safetensors_t *ms, int layer_start, int layer_end) { + const ffwd_config_t *cfg = &model->config; + ffwd_weights_t *w = &model->weights; + char name[256]; + + /* sentence-transformers checkpoints store encoder tensors unprefixed; raw + * HF BERT and RoBERTa/XLM-R checkpoints use "bert." or "roberta.". */ + const char *p; + if (multi_safetensors_find(ms, "embeddings.word_embeddings.weight", NULL)) + p = ""; + else if (multi_safetensors_find(ms, "bert.embeddings.word_embeddings.weight", NULL)) + p = "bert."; + else if (multi_safetensors_find(ms, "roberta.embeddings.word_embeddings.weight", NULL)) + p = "roberta."; + else { + fprintf(stderr, "ffwd_model_load: BERT-family word embeddings not found\n"); + return -1; + } + + int hidden = cfg->hidden_size, inter = cfg->intermediate_size; + + if (layer_start == 0) { + const int64_t emb_shape[2] = {cfg->vocab_size, hidden}; + snprintf(name, sizeof(name), "%sembeddings.word_embeddings.weight", p); + w->embed_tokens = load_weight_direct(ms, name, emb_shape, 2); + const int64_t pos_shape[2] = {cfg->max_position_embeddings, hidden}; + snprintf(name, sizeof(name), "%sembeddings.position_embeddings.weight", p); + w->position_embeddings = load_norm_f32(ms, name, pos_shape, 2); + const int64_t type_shape[2] = {cfg->type_vocab_size, hidden}; + snprintf(name, sizeof(name), "%sembeddings.token_type_embeddings.weight", p); + w->token_type_embeddings = load_norm_f32(ms, name, type_shape, 2); + const int64_t ln_shape[1] = {hidden}; + snprintf(name, sizeof(name), "%sembeddings.LayerNorm.weight", p); + w->ffwd_ln_w = load_norm_f32(ms, name, ln_shape, 1); + snprintf(name, sizeof(name), "%sembeddings.LayerNorm.bias", p); + w->ffwd_ln_b = load_norm_f32(ms, name, ln_shape, 1); + if (!w->embed_tokens.data || !w->position_embeddings || !w->token_type_embeddings || + !w->ffwd_ln_w || !w->ffwd_ln_b) + return -1; + } + + w->layers = (ffwd_layer_t *)calloc(cfg->n_layers, sizeof(ffwd_layer_t)); + if (!w->layers) + return -1; + + for (int i = layer_start; i < layer_end; i++) { + ffwd_layer_t *l = &w->layers[i]; + +#define BERT_W(field, suffix, d0, d1) \ + do { \ + const int64_t e[2] = {(d0), (d1)}; \ + snprintf(name, sizeof(name), "%sencoder.layer.%d." suffix, p, i); \ + l->field = load_weight_direct(ms, name, e, 2); \ + if (!l->field.data) \ + return -1; \ + } while (0) +#define BERT_V(field, suffix, d0) \ + do { \ + const int64_t e[1] = {(d0)}; \ + snprintf(name, sizeof(name), "%sencoder.layer.%d." suffix, p, i); \ + l->field = load_norm_f32(ms, name, e, 1); \ + if (!l->field) \ + return -1; \ + } while (0) + + BERT_W(wq, "attention.self.query.weight", hidden, hidden); + BERT_V(q_bias, "attention.self.query.bias", hidden); + BERT_W(wk, "attention.self.key.weight", hidden, hidden); + BERT_V(k_bias, "attention.self.key.bias", hidden); + BERT_W(wv, "attention.self.value.weight", hidden, hidden); + BERT_V(v_bias, "attention.self.value.bias", hidden); + BERT_W(wo, "attention.output.dense.weight", hidden, hidden); + BERT_V(o_bias, "attention.output.dense.bias", hidden); + BERT_V(input_norm, "attention.output.LayerNorm.weight", hidden); + BERT_V(attn_ln_bias, "attention.output.LayerNorm.bias", hidden); + BERT_W(up_proj, "intermediate.dense.weight", inter, hidden); + BERT_V(ffn_inter_bias, "intermediate.dense.bias", inter); + BERT_W(down_proj, "output.dense.weight", hidden, inter); + BERT_V(ffn_out_bias, "output.dense.bias", hidden); + BERT_V(post_attn_norm, "output.LayerNorm.weight", hidden); + BERT_V(ffn_ln_bias, "output.LayerNorm.bias", hidden); + +#undef BERT_W +#undef BERT_V + } + /* BERT has no final norm: w->norm stays NULL, and pooling skips it. */ + return 0; +} + +ffwd_model_t * +model_load_range_ex(const char *model_dir, int layer_start, int layer_end, int allow_late) { + if (!allow_late && model_dir_has_late_projection(model_dir)) { + fprintf(stderr, "ffwd_model_load: late-interaction models require " + "token-level MaxSim support and are not valid pooled " + "embedding models yet\n"); + return NULL; + } + + ffwd_model_t *model = (ffwd_model_t *)calloc(1, sizeof(ffwd_model_t)); + if (!model) + return NULL; + + /* Parse config.json */ + if (ffwd_config_parse(&model->config, model_dir) != 0) { + free(model); + return NULL; + } + + const ffwd_config_t *cfg = &model->config; + if (layer_end == -1) + layer_end = cfg->n_layers; + if (layer_start < 0 || layer_start >= layer_end || layer_end > cfg->n_layers) { + fprintf(stderr, + "ffwd_model_load: invalid layer range [%d, %d) " + "for %d-layer model\n", + layer_start, layer_end, cfg->n_layers); + free(model); + return NULL; + } + model->layer_start = layer_start; + model->layer_end = layer_end; + + /* Open safetensors (handles single file or multi-shard) */ + multi_safetensors_t *ms = multi_safetensors_open(model_dir); + if (!ms) { + fprintf(stderr, "ffwd_model_load: failed to open safetensors in %s\n", model_dir); + free(model); + return NULL; + } + model->safetensors = ms; + + if (cfg->family == FFWD_FAMILY_BERT) { + if (load_bert_weights(model, ms, layer_start, layer_end) != 0) + goto fail; + if (ffwd_verbose >= 1) + fprintf(stderr, "ffwd_model_load: BERT layers [%d, %d) loaded (%d-dim)\n", layer_start, + layer_end, cfg->hidden_size); + return model; + } + + const char *weight_prefix = multi_safetensors_weight_prefix(ms, "embed_tokens.weight"); + if (!weight_prefix) { + fprintf(stderr, "ffwd_model_load: neither embed_tokens.weight nor " + "model.embed_tokens.weight was found\n"); + goto fail; + } + + if (ffwd_verbose >= 1) + fprintf(stderr, "ffwd_model_load: loading weights from %s\n", model_dir); + + char name[256]; + + /* Only the first stage needs token embeddings. */ + ffwd_weights_t *w = &model->weights; + if (layer_start == 0) { + const int64_t embed_shape[2] = {cfg->vocab_size, cfg->hidden_size}; + snprintf(name, sizeof(name), "%sembed_tokens.weight", weight_prefix); + w->embed_tokens = load_weight_direct(ms, name, embed_shape, 2); + if (!w->embed_tokens.data) + goto fail; + } + + /* Allocate layer array */ + w->layers = (ffwd_layer_t *)calloc(cfg->n_layers, sizeof(ffwd_layer_t)); + if (!w->layers) + goto fail; + + /* Per-layer weights */ + for (int i = layer_start; i < layer_end; i++) { + ffwd_layer_t *l = &w->layers[i]; + +#define LOAD_NORM(field, fmt, d0) \ + do { \ + const int64_t expect[1] = {(d0)}; \ + snprintf(name, sizeof(name), fmt, weight_prefix, i); \ + l->field = load_norm_f32(ms, name, expect, 1); \ + if (!l->field) \ + goto fail; \ + } while (0) + +#define LOAD2(field, fmt, d0, d1) \ + do { \ + const int64_t expect[2] = {(d0), (d1)}; \ + snprintf(name, sizeof(name), fmt, weight_prefix, i); \ + l->field = load_weight_direct(ms, name, expect, 2); \ + if (!l->field.data) \ + goto fail; \ + } while (0) + + LOAD2(wq, "%slayers.%d.self_attn.q_proj.weight", cfg->q_dim, cfg->hidden_size); + LOAD2(wk, "%slayers.%d.self_attn.k_proj.weight", cfg->kv_dim, cfg->hidden_size); + LOAD2(wv, "%slayers.%d.self_attn.v_proj.weight", cfg->kv_dim, cfg->hidden_size); + LOAD2(wo, "%slayers.%d.self_attn.o_proj.weight", cfg->hidden_size, cfg->q_dim); + if (cfg->qkv_bias) { + LOAD_NORM(q_bias, "%slayers.%d.self_attn.q_proj.bias", cfg->q_dim); + LOAD_NORM(k_bias, "%slayers.%d.self_attn.k_proj.bias", cfg->kv_dim); + LOAD_NORM(v_bias, "%slayers.%d.self_attn.v_proj.bias", cfg->kv_dim); + } + if (cfg->qk_norm) { + LOAD_NORM(q_norm, "%slayers.%d.self_attn.q_norm.weight", cfg->head_dim); + LOAD_NORM(k_norm, "%slayers.%d.self_attn.k_norm.weight", cfg->head_dim); + } + LOAD_NORM(input_norm, "%slayers.%d.input_layernorm.weight", cfg->hidden_size); + LOAD_NORM(post_attn_norm, "%slayers.%d.post_attention_layernorm.weight", cfg->hidden_size); + LOAD2(gate_proj, "%slayers.%d.mlp.gate_proj.weight", cfg->intermediate_size, + cfg->hidden_size); + LOAD2(up_proj, "%slayers.%d.mlp.up_proj.weight", cfg->intermediate_size, cfg->hidden_size); + LOAD2(down_proj, "%slayers.%d.mlp.down_proj.weight", cfg->hidden_size, + cfg->intermediate_size); + +#undef LOAD2 +#undef LOAD_NORM + + if (ffwd_verbose >= 2) + fprintf(stderr, " layer %d loaded\n", i); + } + + /* Only the final stage applies the output RMSNorm. */ + if (layer_end == cfg->n_layers) { + const int64_t norm_shape[1] = {cfg->hidden_size}; + snprintf(name, sizeof(name), "%snorm.weight", weight_prefix); + w->norm = load_norm_f32(ms, name, norm_shape, 1); + if (!w->norm) + goto fail; + } + + if (ffwd_verbose >= 1) + fprintf(stderr, + "ffwd_model_load: layers [%d, %d) loaded " + "(%d-dim embeddings)\n", + layer_start, layer_end, cfg->hidden_size); + + return model; + +fail: + ffwd_model_free(model); + return NULL; +} + +ffwd_model_t *ffwd_model_load(const char *model_dir) { + return model_load_range_ex(model_dir, 0, -1, 0); +} + +/* Release a weight ref's backing store. Only F16-widened refs own their buffer; + * borrowed F32/BF16 views into the mmap are a no-op (see weight_ref_from_tensor). */ +static void free_weight_ref(ffwd_weight_ref_t *w) { + if (w->owned) + free((void *)w->data); + w->data = NULL; + w->owned = 0; +} + +void ffwd_model_free(ffwd_model_t *model) { + if (!model) + return; + if (model->weights.layers) { + for (int i = 0; i < model->config.n_layers; i++) { + ffwd_layer_t *l = &model->weights.layers[i]; + free_weight_ref(&l->wq); + free_weight_ref(&l->wk); + free_weight_ref(&l->wv); + free_weight_ref(&l->wo); + free_weight_ref(&l->gate_proj); + free_weight_ref(&l->up_proj); + free_weight_ref(&l->down_proj); + free((void *)l->q_bias); + free((void *)l->k_bias); + free((void *)l->v_bias); + free((void *)l->q_norm); + free((void *)l->k_norm); + free((void *)l->input_norm); + free((void *)l->post_attn_norm); + free((void *)l->o_bias); + free((void *)l->attn_ln_bias); + free((void *)l->ffn_inter_bias); + free((void *)l->ffn_out_bias); + free((void *)l->ffn_ln_bias); + } + } + free_weight_ref(&model->weights.embed_tokens); + free((void *)model->weights.norm); + free((void *)model->weights.position_embeddings); + free((void *)model->weights.token_type_embeddings); + free((void *)model->weights.ffwd_ln_w); + free((void *)model->weights.ffwd_ln_b); + if (model->safetensors) + multi_safetensors_close((multi_safetensors_t *)model->safetensors); + free(model->weights.layers); + free(model); +} + +const ffwd_config_t *ffwd_model_config(const ffwd_model_t *model) { + return model ? &model->config : NULL; +} diff --git a/libffwd/model_blas.c b/libffwd/model_blas.c new file mode 100644 index 0000000..aae2a44 --- /dev/null +++ b/libffwd/model_blas.c @@ -0,0 +1,201 @@ +#include "ffwd.h" +#include "model_internal.h" +#include "kernels.h" + +#include <limits.h> +#include <math.h> +#include <stdint.h> +#include <stdlib.h> + +/* CPU workspace ownership and scratch growth. GPU builds use their own + * backend contexts and do not link this translation unit. */ + +ffwd_workspace_t *ffwd_workspace_new(const ffwd_model_t *model) { + if (!model) + return NULL; + ffwd_workspace_t *ws = (ffwd_workspace_t *)calloc(1, sizeof(ffwd_workspace_t)); + if (!ws) + return NULL; + ws->model = model; + return ws; +} + +void ffwd_workspace_free(ffwd_workspace_t *ws) { + if (!ws) + return; + free(ws->x); + free(ws->x_norm); + free(ws->q); + free(ws->k); + free(ws->v); + free(ws->attn_out); + free(ws->proj_out); + free(ws->ffn_gate); + free(ws->ffn_up); + free(ws->attn_scores); + free(ws->offsets); + free(ws->rope_cos); + free(ws->rope_sin); + free(ws->bf16_widen); + free(ws); +} + +int ensure_buffers(ffwd_workspace_t *ws, const ffwd_config_t *c, int seq) { + if (seq <= ws->buf_seq_cap) + return 0; + + int cap; + if (grow_cap(ws->buf_seq_cap, seq, &cap) != 0) + return -1; + +#define R(ptr, n) \ + do { \ + if (realloc_floats_2d(&(ptr), cap, (n)) != 0) \ + return -1; \ + } while (0) + + R(ws->x, c->hidden_size); + R(ws->x_norm, c->hidden_size); + R(ws->q, c->q_dim); + R(ws->k, c->kv_dim); + R(ws->v, c->kv_dim); + R(ws->attn_out, c->q_dim); + R(ws->proj_out, c->hidden_size); + R(ws->ffn_gate, c->intermediate_size); + R(ws->ffn_up, c->intermediate_size); + +#undef R + + ws->buf_seq_cap = cap; + return 0; +} + +void ensure_attention_scores(ffwd_workspace_t *ws, const int *offsets, int batch) { + size_t bytes = bidirectional_gqa_attention_packed_scratch_bytes(offsets, batch); + if (bytes <= ws->attn_scores_bytes) + return; + + void *p = realloc(ws->attn_scores, bytes); + if (!p) + return; + ws->attn_scores = (float *)p; + ws->attn_scores_bytes = bytes; +} + +int ensure_rope_cache(ffwd_workspace_t *ws, const ffwd_config_t *cfg, int n_pos) { + if (n_pos <= ws->rope_cache_cap) + return 0; + + int cap = ws->rope_cache_cap > 0 ? ws->rope_cache_cap : 512; + while (cap < n_pos) { + if (cap > INT_MAX / 2) + return -1; + cap *= 2; + } + + int head_dim = cfg->head_dim; + int half = head_dim / 2; + float theta = cfg->rope_theta; + + size_t count; + if (mul_size((size_t)cap, (size_t)head_dim, &count) != 0) + return -1; + if (realloc_floats(&ws->rope_cos, count) != 0) + return -1; + if (realloc_floats(&ws->rope_sin, count) != 0) + return -1; + + for (int pos = ws->rope_cache_cap; pos < cap; pos++) { + float fp = (float)pos; + float *c = ws->rope_cos + (size_t)pos * head_dim; + float *s = ws->rope_sin + (size_t)pos * head_dim; + for (int d = 0; d < half; d++) { + float inv = 1.0f / powf(theta, (float)(2 * d) / (float)head_dim); + float angle = fp * inv; + float cv = cosf(angle), sv = sinf(angle); + c[d] = cv; + c[half + d] = cv; + s[d] = sv; + s[half + d] = sv; + } + } + + ws->rope_cache_cap = cap; + return 0; +} + +int ensure_offsets(ffwd_workspace_t *ws, int batch) { + if (!ws || batch <= 0 || batch == INT_MAX) + return -1; + + int needed = batch + 1; + if (needed <= ws->offsets_cap) + return 0; + + if (realloc_ints(&ws->offsets, (size_t)needed) != 0) + return -1; + ws->offsets_cap = needed; + return 0; +} + +size_t ffwd_workspace_nbytes(const ffwd_workspace_t *ws) { + if (!ws) + return 0; + + size_t total = sizeof(*ws); + const ffwd_model_t *model = ws->model; + if (!model) + return total; + + const ffwd_config_t *c = &model->config; + size_t row_floats = 0; + const int dims[] = { + c->hidden_size, /* x */ + c->hidden_size, /* x_norm */ + c->q_dim, /* q */ + c->kv_dim, /* k */ + c->kv_dim, /* v */ + c->q_dim, /* attn_out */ + c->hidden_size, /* proj_out */ + c->intermediate_size, /* ffn_gate */ + c->intermediate_size, /* ffn_up */ + }; + + for (size_t i = 0; i < sizeof(dims) / sizeof(dims[0]); i++) { + if (dims[i] < 0 || add_size(row_floats, (size_t)dims[i], &row_floats) != 0) + return SIZE_MAX; + } + + size_t buf_floats = 0; + if (ws->buf_seq_cap > 0 && mul_size((size_t)ws->buf_seq_cap, row_floats, &buf_floats) != 0) + return SIZE_MAX; + + size_t rope_floats = 0; + if (ws->rope_cache_cap > 0) { + size_t rope_rows = 0; + if (mul_size((size_t)ws->rope_cache_cap, (size_t)c->head_dim, &rope_rows) != 0 || + mul_size(rope_rows, 2, &rope_floats) != 0) + return SIZE_MAX; + } + + size_t all_floats = 0, bytes = 0; + if (add_size(buf_floats, rope_floats, &all_floats) != 0 || + mul_size(all_floats, sizeof(float), &bytes) != 0 || add_size(total, bytes, &total) != 0) + return SIZE_MAX; + + size_t offset_bytes = 0; + if (ws->offsets_cap > 0 && mul_size((size_t)ws->offsets_cap, sizeof(int), &offset_bytes) != 0) + return SIZE_MAX; + if (add_size(total, offset_bytes, &total) != 0) + return SIZE_MAX; + if (add_size(total, ws->attn_scores_bytes, &total) != 0) + return SIZE_MAX; + + size_t widen_bytes = 0; + if (ws->bf16_widen_count > 0 && mul_size(ws->bf16_widen_count, sizeof(float), &widen_bytes) != 0) + return SIZE_MAX; + if (add_size(total, widen_bytes, &total) != 0) + return SIZE_MAX; + + return total; +} diff --git a/libffwd/model_internal.h b/libffwd/model_internal.h new file mode 100644 index 0000000..b828e67 --- /dev/null +++ b/libffwd/model_internal.h @@ -0,0 +1,85 @@ +/* Cross-file declarations private to CPU model code. + * + * model_types.h holds the shared struct definitions; this header holds the + * shared functions. + * + * These are not part of the public API (ffwd.h) and do not use FFWD_API + * visibility. They are shared between translation units that used to be one + * file: model loading, the forward pass, and late interaction. + * + * Frontends and GPU backends do not include this header. + */ + +#ifndef FFWD_MODEL_INTERNAL_H +#define FFWD_MODEL_INTERNAL_H + +#include "model_types.h" +#include "alloc.h" + +/* Validate that a safetensor has the expected dtype, rank, and shape, and that + * its bytes are within the mapped file. bf16_ok also allows either 16-bit float + * format, BF16 or F16, in addition to F32. */ +int tensor_has_supported_shape(const safetensors_file_t *sf, + const safetensor_t *t, + const char *name, + const int64_t *shape, + int ndim, + int bf16_ok); + +/* Build a weight ref from a located tensor. + * F32/BF16 borrow the mmap; F16 is widened to an owned F32 buffer because there + * is no fused F16 kernel. + * + * Shared by the model loader and late-interaction projection loader. + * The owner frees ref.data when ref.owned is set. + * + * Returns ref.data == NULL on allocation failure. */ +ffwd_weight_ref_t weight_ref_from_tensor(const safetensors_file_t *sf, const safetensor_t *t); + +/* True if model_dir carries a 1_Dense projection (a late-interaction model). */ +int model_dir_has_late_projection(const char *model_dir); + +/* Load (a layer range of) a model. allow_late lets the late loader accept a + * 1_Dense snapshot that the pooled loader rejects. layer_end == -1 means all. */ +ffwd_model_t * +model_load_range_ex(const char *model_dir, int layer_start, int layer_end, int allow_late); + +/* Workspace scratch grows lazily per forward call. + * Defined in model.c beside the workspace lifecycle. ffwd_workspace_nbytes mirrors this sizing. + * Called from the forward pass in forward.c. */ +int ensure_buffers(ffwd_workspace_t *ws, const ffwd_config_t *c, int seq); +void ensure_attention_scores(ffwd_workspace_t *ws, const int *offsets, int batch); +int ensure_rope_cache(ffwd_workspace_t *ws, const ffwd_config_t *cfg, int n_pos); +int ensure_offsets(ffwd_workspace_t *ws, int batch); + +/* y[seq,out] = x[seq,in] @ w^T, no bias; dispatches F32/BF16 paths. Shared by + * the forward pass and the late projection. */ +void linear_nobias_weight(ffwd_workspace_t *ws, + float *y, + const float *x, + const ffwd_weight_ref_t *w, + int seq_len, + int in_dim, + int out_dim); + +/* Compute packed sequence offsets [batch+1] plus total and max sequence length + * over a batch of inputs. require_ids rejects inputs with a NULL id pointer. */ +int build_offsets(const ffwd_input_t *inputs, + int batch, + int require_ids, + int *offsets, + int *total_out, + int *max_out); + +/* Run the full transformer forward over a packed batch in ws, leaving hidden + * states in ws->x. Used by pooled encoding and by late-interaction encoding. */ +int forward_packed_inplace(const ffwd_model_t *model, + ffwd_workspace_t *ws, + const ffwd_input_t *inputs, + int batch, + const int *offsets, + int total_seq, + int max_seq, + int apply_final_norm); + +#endif /* FFWD_MODEL_INTERNAL_H */ diff --git a/libffwd/model_types.h b/libffwd/model_types.h new file mode 100644 index 0000000..4f01ca2 --- /dev/null +++ b/libffwd/model_types.h @@ -0,0 +1,122 @@ +/* In-memory layout of a loaded model: model/workspace structs, per-layer weight + * references, and accessors for GPU backends. + * + * Shared by CPU model code and the CUDA backend. Not part of the public ABI + * (ffwd.h). + * + * model_internal.h declares the CPU model's private functions. + */ + +#ifndef FFWD_MODEL_TYPES_H +#define FFWD_MODEL_TYPES_H + +#include "internal.h" +#include "safetensors.h" + +typedef struct { + safetensor_dtype_t dtype; + const void *data; + /* 0: data is a borrowed view into the safetensors mmap (F32/BF16, the common + * case). 1: data is a heap buffer owned by the model and freed on unload. + * Set when an F16 tensor is widened to F32 at load (no fused F16 kernel). */ + int owned; +} ffwd_weight_ref_t; + +typedef struct { + ffwd_weight_ref_t wq; /* [q_dim, hidden] */ + ffwd_weight_ref_t wk; /* [kv_dim, hidden] */ + ffwd_weight_ref_t wv; /* [kv_dim, hidden] */ + ffwd_weight_ref_t wo; /* [hidden, q_dim] */ + const float *q_bias; /* [q_dim], optional */ + const float *k_bias; /* [kv_dim], optional */ + const float *v_bias; /* [kv_dim], optional */ + const float *q_norm; /* [head_dim] */ + const float *k_norm; /* [head_dim] */ + const float *input_norm; /* [hidden] */ + const float *post_attn_norm; /* [hidden] */ + ffwd_weight_ref_t gate_proj; /* [intermediate, hidden] */ + ffwd_weight_ref_t up_proj; /* [intermediate, hidden] */ + ffwd_weight_ref_t down_proj; /* [hidden, intermediate] */ + /* BERT family (all NULL for Qwen3). The attention projections reuse + * wq/wk/wv/wo and q/k/v_bias; the two FFN matrices reuse up_proj + * (intermediate.dense) and down_proj (output.dense); the two block + * LayerNorm weights reuse input_norm (post-attention) and post_attn_norm + * (post-FFN). These fields add the biases those reused slots lack. */ + const float *o_bias; /* attention output dense bias [hidden] */ + const float *attn_ln_bias; /* post-attention LayerNorm bias [hidden] */ + const float *ffn_inter_bias; /* intermediate.dense bias [intermediate] */ + const float *ffn_out_bias; /* output.dense bias [hidden] */ + const float *ffn_ln_bias; /* post-FFN LayerNorm bias [hidden] */ +} ffwd_layer_t; + +typedef struct { + ffwd_weight_ref_t embed_tokens; /* [vocab_size, hidden] */ + ffwd_layer_t *layers; /* [n_layers] heap-allocated */ + const float *norm; /* [hidden]; NULL for BERT (no final norm) */ + /* BERT family (NULL for Qwen3): learned absolute position embeddings, + * token-type (segment) embeddings, and the embedding LayerNorm. */ + const float *position_embeddings; /* [max_position_embeddings, hidden] */ + const float *token_type_embeddings; /* [type_vocab_size, hidden] */ + const float *ffwd_ln_w; /* embedding LayerNorm weight [hidden] */ + const float *ffwd_ln_b; /* embedding LayerNorm bias [hidden] */ +} ffwd_weights_t; + +typedef void (*ffwd_attention_fn)(float *out, + const float *Q, + const float *K, + const float *V, + const int *offsets, + int batch, + int n_heads, + int n_kv_heads, + int head_dim, + float scale, + float *scratch, + size_t scratch_bytes); + +struct ffwd_model { + ffwd_config_t config; + ffwd_weights_t weights; + void *safetensors; /* multi_safetensors_t*, keeps mmap alive */ + int layer_start; /* loaded transformer range, inclusive */ + int layer_end; /* loaded transformer range, exclusive */ +}; + +struct ffwd_workspace { + const ffwd_model_t *model; /* immutable model this workspace belongs to */ + int buf_seq_cap; + float *x; /* [seq, hidden] */ + float *x_norm; /* [seq, hidden] */ + float *q; /* [seq, q_dim] */ + float *k; /* [seq, kv_dim] */ + float *v; /* [seq, kv_dim] */ + float *attn_out; /* [seq, q_dim] */ + float *proj_out; /* [seq, hidden] */ + float *ffn_gate; /* [seq, intermediate] */ + float *ffn_up; /* [seq, intermediate] */ + float *attn_scores; /* reusable BLAS attention score tiles */ + size_t attn_scores_bytes; + + int *offsets; /* [batch + 1] packed sequence offsets */ + int offsets_cap; + + /* RoPE cosine/sine cache [n_pos, head_dim], grown lazily. */ + float *rope_cos; + float *rope_sin; + int rope_cache_cap; + + /* F32 view of one BF16 weight matrix for SGEMM dispatch, grown to the + * largest projection. Workspace-owned so concurrent workspaces on one + * model never share conversion scratch. */ + float *bf16_widen; + size_t bf16_widen_count; +}; + +/* Internal accessors for the late-interaction model. GPU backends build a device + * context from an already-loaded CPU late model because the model dir is a late + * snapshot that the pooled loader rejects; these expose the base model and the + * projection weight without making struct ffwd_late_model public. */ +ffwd_model_t *ffwd_late_model_base(const ffwd_late_model_t *model); +const ffwd_weight_ref_t *ffwd_late_model_projection(const ffwd_late_model_t *model); + +#endif /* FFWD_MODEL_TYPES_H */ diff --git a/libffwd/neon.c b/libffwd/neon.c new file mode 100644 index 0000000..50cf649 --- /dev/null +++ b/libffwd/neon.c @@ -0,0 +1,118 @@ +/* + * ARM NEON hot kernels. + */ + +#include "impl.h" + +#ifdef __ARM_NEON + +# include <arm_neon.h> +# include <string.h> + +void bf16_matvec_fused_neon( + float *y, const float *x, const uint16_t *W_bf16, const float *bias, int in_dim, int out_dim) { + int o = 0; + + /* Process 2 output rows at a time, 32 elements/iter, 8 accumulators */ + for (; o + 1 < out_dim; o += 2) { + const uint16_t *w0 = W_bf16 + (size_t)o * in_dim; + const uint16_t *w1 = W_bf16 + (size_t)(o + 1) * in_dim; + float s0 = bias ? bias[o] : 0.0f; + float s1 = bias ? bias[o + 1] : 0.0f; + + float32x4_t a0 = vdupq_n_f32(0.0f), a1 = vdupq_n_f32(0.0f); + float32x4_t a2 = vdupq_n_f32(0.0f), a3 = vdupq_n_f32(0.0f); + float32x4_t b0 = vdupq_n_f32(0.0f), b1 = vdupq_n_f32(0.0f); + float32x4_t b2 = vdupq_n_f32(0.0f), b3 = vdupq_n_f32(0.0f); + int k = 0; + + for (; k + 32 <= in_dim; k += 32) { + float32x4_t x0 = vld1q_f32(x + k); + float32x4_t x1 = vld1q_f32(x + k + 4); + float32x4_t x2 = vld1q_f32(x + k + 8); + float32x4_t x3 = vld1q_f32(x + k + 12); + float32x4_t x4 = vld1q_f32(x + k + 16); + float32x4_t x5 = vld1q_f32(x + k + 20); + float32x4_t x6 = vld1q_f32(x + k + 24); + float32x4_t x7 = vld1q_f32(x + k + 28); + + uint16x8_t r0a = vld1q_u16(w0 + k); + uint16x8_t r0b = vld1q_u16(w0 + k + 8); + uint16x8_t r0c = vld1q_u16(w0 + k + 16); + uint16x8_t r0d = vld1q_u16(w0 + k + 24); + a0 = vfmaq_f32(a0, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r0a), 16)), x0); + a1 = vfmaq_f32(a1, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r0a), 16)), x1); + a2 = vfmaq_f32(a2, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r0b), 16)), x2); + a3 = vfmaq_f32(a3, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r0b), 16)), x3); + a0 = vfmaq_f32(a0, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r0c), 16)), x4); + a1 = vfmaq_f32(a1, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r0c), 16)), x5); + a2 = vfmaq_f32(a2, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r0d), 16)), x6); + a3 = vfmaq_f32(a3, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r0d), 16)), x7); + + uint16x8_t r1a = vld1q_u16(w1 + k); + uint16x8_t r1b = vld1q_u16(w1 + k + 8); + uint16x8_t r1c = vld1q_u16(w1 + k + 16); + uint16x8_t r1d = vld1q_u16(w1 + k + 24); + b0 = vfmaq_f32(b0, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r1a), 16)), x0); + b1 = vfmaq_f32(b1, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r1a), 16)), x1); + b2 = vfmaq_f32(b2, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r1b), 16)), x2); + b3 = vfmaq_f32(b3, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r1b), 16)), x3); + b0 = vfmaq_f32(b0, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r1c), 16)), x4); + b1 = vfmaq_f32(b1, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r1c), 16)), x5); + b2 = vfmaq_f32(b2, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r1d), 16)), x6); + b3 = vfmaq_f32(b3, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r1d), 16)), x7); + } + for (; k + 8 <= in_dim; k += 8) { + float32x4_t x0 = vld1q_f32(x + k); + float32x4_t x1 = vld1q_f32(x + k + 4); + uint16x8_t r0 = vld1q_u16(w0 + k); + uint16x8_t r1 = vld1q_u16(w1 + k); + a0 = vfmaq_f32(a0, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r0), 16)), x0); + a1 = vfmaq_f32(a1, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r0), 16)), x1); + b0 = vfmaq_f32(b0, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(r1), 16)), x0); + b1 = vfmaq_f32(b1, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(r1), 16)), x1); + } + s0 += vaddvq_f32(vaddq_f32(vaddq_f32(a0, a2), vaddq_f32(a1, a3))); + s1 += vaddvq_f32(vaddq_f32(vaddq_f32(b0, b2), vaddq_f32(b1, b3))); + + for (; k < in_dim; k++) { + uint32_t bits0 = ((uint32_t)w0[k]) << 16; + uint32_t bits1 = ((uint32_t)w1[k]) << 16; + float wv0, wv1; + memcpy(&wv0, &bits0, sizeof(float)); + memcpy(&wv1, &bits1, sizeof(float)); + s0 += wv0 * x[k]; + s1 += wv1 * x[k]; + } + y[o] = s0; + y[o + 1] = s1; + } + + /* Handle remaining odd row */ + for (; o < out_dim; o++) { + const uint16_t *w_row = W_bf16 + (size_t)o * in_dim; + float sum = bias ? bias[o] : 0.0f; + int k = 0; + + float32x4_t acc0 = vdupq_n_f32(0.0f); + float32x4_t acc1 = vdupq_n_f32(0.0f); + for (; k + 8 <= in_dim; k += 8) { + uint16x8_t bf = vld1q_u16(w_row + k); + acc0 = vfmaq_f32(acc0, vreinterpretq_f32_u32(vshll_n_u16(vget_low_u16(bf), 16)), + vld1q_f32(x + k)); + acc1 = vfmaq_f32(acc1, vreinterpretq_f32_u32(vshll_n_u16(vget_high_u16(bf), 16)), + vld1q_f32(x + k + 4)); + } + sum += vaddvq_f32(vaddq_f32(acc0, acc1)); + + for (; k < in_dim; k++) { + uint32_t f32_bits = ((uint32_t)w_row[k]) << 16; + float w_val; + memcpy(&w_val, &f32_bits, sizeof(float)); + sum += w_val * x[k]; + } + y[o] = sum; + } +} + +#endif /* __ARM_NEON */ diff --git a/libffwd/norm.c b/libffwd/norm.c new file mode 100644 index 0000000..57c93db --- /dev/null +++ b/libffwd/norm.c @@ -0,0 +1,138 @@ +/* + * Normalization kernels: RMSNorm (Qwen) and LayerNorm (BERT), plus the + * per-head RMSNorm used for QK-norm. + */ + +#include "kernels.h" +#include "threadpool.h" + +#include <math.h> +#include <stdlib.h> +#include <string.h> + +#ifdef USE_BLAS +# ifdef __APPLE__ +# include <Accelerate/Accelerate.h> +# else +# include <cblas.h> +# endif +#endif + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +/* Normalization */ + +/* Rows split across the pool only when the tensor is big enough to pay + * for the dispatch. */ +#define RMS_NORM_PARALLEL_ELEMS 262144 + +/* The sum of squares and the scaled write-back are plain loops: the compiler + * auto-vectorizes both in place, which is faster than reducing through a + * two-input dot kernel that reloads the row and is reduction-latency bound. */ +static void rms_norm_range( + float *out, const float *x, const float *weight, int start, int end, int hidden, float eps) { + for (int s = start; s < end; s++) { + const float *x_row = x + (size_t)s * hidden; + float *out_row = out + (size_t)s * hidden; + + float sum_sq = 0.0f; + for (int i = 0; i < hidden; i++) + sum_sq += x_row[i] * x_row[i]; + float rms_inv = 1.0f / sqrtf(sum_sq / hidden + eps); + for (int i = 0; i < hidden; i++) + out_row[i] = x_row[i] * rms_inv * weight[i]; + } +} + +typedef struct { + float *out; + const float *x; + const float *weight; + int seq_len; + int hidden; + float eps; +} rms_norm_task_t; + +static void rms_norm_worker(int tid, int n_threads, void *arg) { + rms_norm_task_t *t = (rms_norm_task_t *)arg; + int chunk = (t->seq_len + n_threads - 1) / n_threads; + int start = tid * chunk; + int end = start + chunk; + if (end > t->seq_len) + end = t->seq_len; + if (start >= end) + return; + rms_norm_range(t->out, t->x, t->weight, start, end, t->hidden, t->eps); +} + +void rms_norm(float *out, const float *x, const float *weight, int seq_len, int hidden, float eps) { + if (seq_len <= 0 || hidden <= 0) + return; + + long long elems = (long long)seq_len * hidden; + if (tp_num_threads() > 1 && elems >= RMS_NORM_PARALLEL_ELEMS) { + rms_norm_task_t task = { + .out = out, + .x = x, + .weight = weight, + .seq_len = seq_len, + .hidden = hidden, + .eps = eps, + }; + parallel_for(rms_norm_worker, &task); + } else { + rms_norm_range(out, x, weight, 0, seq_len, hidden, eps); + } +} + +void rms_norm_per_head( + float *x, const float *weight, int seq_len, int n_heads, int head_dim, float eps) { + /* x is [seq, n_heads * head_dim] - normalize each [head_dim] segment */ + int hidden = n_heads * head_dim; + for (int s = 0; s < seq_len; s++) { + for (int h = 0; h < n_heads; h++) { + float *vec = x + s * hidden + h * head_dim; + + float sum_sq = 0.0f; + for (int d = 0; d < head_dim; d++) + sum_sq += vec[d] * vec[d]; + float rms_inv = 1.0f / sqrtf(sum_sq / head_dim + eps); + for (int d = 0; d < head_dim; d++) + vec[d] = vec[d] * rms_inv * weight[d]; + } + } +} + +/* Mean-subtracting LayerNorm with bias. Safe in place (out may alias x): each + * row's mean and variance are reduced before the row is rewritten. Scalar for + * now; LayerNorm is not GEMM-bound, so SIMD/threading is a later perf step. */ +void layer_norm(float *out, + const float *x, + const float *gamma, + const float *beta, + int seq_len, + int hidden, + float eps) { + if (seq_len <= 0 || hidden <= 0) + return; + float inv_h = 1.0f / (float)hidden; + for (int s = 0; s < seq_len; s++) { + const float *row = x + (size_t)s * hidden; + float *dst = out + (size_t)s * hidden; + float mean = 0.0f; + for (int d = 0; d < hidden; d++) + mean += row[d]; + mean *= inv_h; + float var = 0.0f; + for (int d = 0; d < hidden; d++) { + float diff = row[d] - mean; + var += diff * diff; + } + var *= inv_h; + float inv_std = 1.0f / sqrtf(var + eps); + for (int d = 0; d < hidden; d++) + dst[d] = gamma[d] * (row[d] - mean) * inv_std + beta[d]; + } +} diff --git a/libffwd/rope.c b/libffwd/rope.c new file mode 100644 index 0000000..29cbb17 --- /dev/null +++ b/libffwd/rope.c @@ -0,0 +1,137 @@ +/* + * Rotary position embeddings: cache-free NeoX-style RoPE compute and apply. + */ + +#include "kernels.h" +#include "impl.h" + +#include <math.h> +#include <stdlib.h> +#include <string.h> + +#if (defined(__AVX512F__) || defined(__AVX2__)) && \ + (defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86)) +# include <immintrin.h> +#endif +#if defined(__ARM_NEON) || defined(__ARM_NEON__) +# include <arm_neon.h> +#endif + +#ifdef USE_BLAS +# ifdef __APPLE__ +# include <Accelerate/Accelerate.h> +# else +# include <cblas.h> +# endif +#endif + +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +/* Position embeddings */ + +void compute_rope_neox( + float *cos_out, float *sin_out, const int *positions, int seq, int head_dim, float theta) { + int half = head_dim / 2; + + for (int s = 0; s < seq; s++) { + float pos = (float)positions[s]; + for (int d = 0; d < half; d++) { + float freq = 1.0f / powf(theta, (float)(2 * d) / (float)head_dim); + float angle = pos * freq; + float c = cosf(angle); + float sn = sinf(angle); + /* Duplicate for full head_dim */ + cos_out[s * head_dim + d] = c; + cos_out[s * head_dim + half + d] = c; + sin_out[s * head_dim + d] = sn; + sin_out[s * head_dim + half + d] = sn; + } + } +} + +void apply_rope_neox( + float *x, const float *cos_vals, const float *sin_vals, int seq, int n_heads, int head_dim) { + /* + * NeoX split-half style: + * x1 = x[..., :half], x2 = x[..., half:] + * rotated = cat(-x2, x1) + * result = x * cos + rotated * sin + */ + int half = head_dim / 2; + int hidden = n_heads * head_dim; + + for (int s = 0; s < seq; s++) { + const float *c = cos_vals + s * head_dim; + const float *sn = sin_vals + s * head_dim; + + for (int h = 0; h < n_heads; h++) { + float *vec = x + s * hidden + h * head_dim; + +#if defined(__AVX512F__) && defined(__FMA__) + int d = 0; + for (; d + 16 <= half; d += 16) { + __m512 x1 = _mm512_loadu_ps(vec + d); + __m512 x2 = _mm512_loadu_ps(vec + half + d); + /* RoPE cache duplicates cos/sin across halves. */ + __m512 cc = _mm512_loadu_ps(c + d); + __m512 ss = _mm512_loadu_ps(sn + d); + __m512 new1 = _mm512_fmsub_ps(x1, cc, _mm512_mul_ps(x2, ss)); + __m512 new2 = _mm512_fmadd_ps(x2, cc, _mm512_mul_ps(x1, ss)); + _mm512_storeu_ps(vec + d, new1); + _mm512_storeu_ps(vec + half + d, new2); + } + for (; d < half; d++) { + float x1 = vec[d]; + float x2 = vec[half + d]; + vec[d] = x1 * c[d] + (-x2) * sn[d]; + vec[half + d] = x2 * c[half + d] + x1 * sn[half + d]; + } +#elif defined(__AVX2__) && defined(__FMA__) + int d = 0; + for (; d + 8 <= half; d += 8) { + __m256 x1 = _mm256_loadu_ps(vec + d); + __m256 x2 = _mm256_loadu_ps(vec + half + d); + __m256 cc = _mm256_loadu_ps(c + d); + __m256 ss = _mm256_loadu_ps(sn + d); + __m256 new1 = _mm256_fmsub_ps(x1, cc, _mm256_mul_ps(x2, ss)); + __m256 new2 = _mm256_fmadd_ps(x2, cc, _mm256_mul_ps(x1, ss)); + _mm256_storeu_ps(vec + d, new1); + _mm256_storeu_ps(vec + half + d, new2); + } + for (; d < half; d++) { + float x1 = vec[d]; + float x2 = vec[half + d]; + vec[d] = x1 * c[d] + (-x2) * sn[d]; + vec[half + d] = x2 * c[half + d] + x1 * sn[half + d]; + } +#elif defined(__ARM_NEON) || defined(__ARM_NEON__) + int d = 0; + for (; d + 4 <= half; d += 4) { + float32x4_t x1 = vld1q_f32(vec + d); + float32x4_t x2 = vld1q_f32(vec + half + d); + float32x4_t cc = vld1q_f32(c + d); + float32x4_t ss = vld1q_f32(sn + d); + float32x4_t new1 = vmlsq_f32(vmulq_f32(x1, cc), x2, ss); + float32x4_t new2 = vfmaq_f32(vmulq_f32(x2, cc), x1, ss); + vst1q_f32(vec + d, new1); + vst1q_f32(vec + half + d, new2); + } + for (; d < half; d++) { + float x1 = vec[d]; + float x2 = vec[half + d]; + vec[d] = x1 * c[d] + (-x2) * sn[d]; + vec[half + d] = x2 * c[half + d] + x1 * sn[half + d]; + } +#else + for (int d = 0; d < half; d++) { + float x1 = vec[d]; /* first half */ + float x2 = vec[half + d]; /* second half */ + vec[d] = x1 * c[d] + (-x2) * sn[d]; + vec[half + d] = x2 * c[half + d] + x1 * sn[half + d]; + } +#endif + } + } +} diff --git a/libffwd/safetensors.c b/libffwd/safetensors.c new file mode 100644 index 0000000..f2c9b26 --- /dev/null +++ b/libffwd/safetensors.c @@ -0,0 +1,518 @@ +#include "safetensors.h" + +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <unistd.h> + +/* Minimal JSON parser for safetensors header */ +/* TODO: consider replacing with yyjson */ + +static void skip_whitespace(const char **p) { + while (**p == ' ' || **p == '\n' || **p == '\r' || **p == '\t') + (*p)++; +} + +static int parse_string(const char **p, char *out, size_t max_len) { + skip_whitespace(p); + if (**p != '"') + return -1; + (*p)++; + size_t i = 0; + while (**p && **p != '"' && i < max_len - 1) { + if (**p == '\\') { + (*p)++; + if (**p == 'n') + out[i++] = '\n'; + else if (**p == 't') + out[i++] = '\t'; + else if (**p == '"') + out[i++] = '"'; + else if (**p == '\\') + out[i++] = '\\'; + else + out[i++] = **p; + } else { + out[i++] = **p; + } + (*p)++; + } + out[i] = '\0'; + if (**p != '"') + return -1; + (*p)++; + return 0; +} + +static int parse_int(const char **p, int64_t *out) { + skip_whitespace(p); + int64_t val = 0; + int neg = 0; + if (**p == '-') { + neg = 1; + (*p)++; + } + if (**p < '0' || **p > '9') + return -1; + while (**p >= '0' && **p <= '9') { + val = val * 10 + (**p - '0'); + (*p)++; + } + *out = neg ? -val : val; + return 0; +} + +static safetensor_dtype_t parse_dtype(const char *s) { + if (strcmp(s, "F32") == 0) + return DTYPE_F32; + if (strcmp(s, "F16") == 0) + return DTYPE_F16; + if (strcmp(s, "BF16") == 0) + return DTYPE_BF16; + if (strcmp(s, "I32") == 0) + return DTYPE_I32; + if (strcmp(s, "I64") == 0) + return DTYPE_I64; + if (strcmp(s, "BOOL") == 0) + return DTYPE_BOOL; + return DTYPE_UNKNOWN; +} + +static int parse_tensor_entry(const char **p, safetensor_t *t) { + skip_whitespace(p); + if (**p != '{') + return -1; + (*p)++; + + t->dtype = DTYPE_UNKNOWN; + t->ndim = 0; + t->data_offset = 0; + t->data_size = 0; + + while (**p && **p != '}') { + skip_whitespace(p); + if (**p == ',') { + (*p)++; + continue; + } + + char key[64]; + if (parse_string(p, key, sizeof(key)) != 0) + return -1; + skip_whitespace(p); + if (**p != ':') + return -1; + (*p)++; + skip_whitespace(p); + + if (strcmp(key, "dtype") == 0) { + char dtype_str[32]; + if (parse_string(p, dtype_str, sizeof(dtype_str)) != 0) + return -1; + t->dtype = parse_dtype(dtype_str); + } else if (strcmp(key, "shape") == 0) { + if (**p != '[') + return -1; + (*p)++; + t->ndim = 0; + while (**p && **p != ']') { + skip_whitespace(p); + if (**p == ',') { + (*p)++; + continue; + } + if (t->ndim >= 8) + return -1; + if (parse_int(p, &t->shape[t->ndim]) != 0) + return -1; + t->ndim++; + } + if (**p != ']') + return -1; + (*p)++; + } else if (strcmp(key, "data_offsets") == 0) { + if (**p != '[') + return -1; + (*p)++; + skip_whitespace(p); + int64_t start_i; + if (parse_int(p, &start_i) != 0) + return -1; + skip_whitespace(p); + if (**p != ',') + return -1; + (*p)++; + skip_whitespace(p); + int64_t end_i; + if (parse_int(p, &end_i) != 0) + return -1; + if (start_i < 0 || end_i < start_i) + return -1; + t->data_offset = (size_t)start_i; + t->data_size = (size_t)(end_i - start_i); + skip_whitespace(p); + if (**p != ']') + return -1; + (*p)++; + } else { + /* Skip unknown value */ + if (**p == '"') { + (*p)++; + while (**p && **p != '"') { + if (**p == '\\') + (*p)++; + if (**p) + (*p)++; + } + if (**p == '"') + (*p)++; + } else if (**p == '[') { + int depth = 1; + (*p)++; + while (**p && depth > 0) { + if (**p == '[') + depth++; + else if (**p == ']') + depth--; + (*p)++; + } + } else if (**p == '{') { + int depth = 1; + (*p)++; + while (**p && depth > 0) { + if (**p == '{') + depth++; + else if (**p == '}') + depth--; + (*p)++; + } + } else { + while (**p && **p != ',' && **p != '}') + (*p)++; + } + } + } + if (**p != '}') + return -1; + (*p)++; + return 0; +} + +static int parse_header(safetensors_file_t *sf) { + const char *p = sf->header_json; + skip_whitespace(&p); + if (*p != '{') + return -1; + p++; + + sf->num_tensors = 0; + + while (*p && *p != '}') { + skip_whitespace(&p); + if (*p == ',') { + p++; + continue; + } + if (*p == '}') + break; + + char name[256]; + if (parse_string(&p, name, sizeof(name)) != 0) + return -1; + skip_whitespace(&p); + if (*p != ':') + return -1; + p++; + + if (strcmp(name, "__metadata__") == 0) { + skip_whitespace(&p); + if (*p == '{') { + int depth = 1; + p++; + while (*p && depth > 0) { + if (*p == '{') + depth++; + else if (*p == '}') + depth--; + p++; + } + } + continue; + } + + if (sf->num_tensors >= SAFETENSORS_MAX_TENSORS) { + fprintf(stderr, "safetensors: too many tensors in %s (max %d)\n", + sf->path ? sf->path : "<unknown>", SAFETENSORS_MAX_TENSORS); + return -1; + } + + safetensor_t *t = &sf->tensors[sf->num_tensors]; + snprintf(t->name, sizeof(t->name), "%s", name); + if (parse_tensor_entry(&p, t) != 0) + return -1; + sf->num_tensors++; + } + if (*p != '}') + return -1; + return 0; +} + +/* Single file operations */ + +safetensors_file_t *safetensors_open(const char *path) { + int fd = open(path, O_RDONLY); + if (fd < 0) + return NULL; + + struct stat st; + if (fstat(fd, &st) < 0) { + close(fd); + return NULL; + } + + size_t file_size = (size_t)st.st_size; + if (file_size < 8) { + close(fd); + return NULL; + } + + void *data = mmap(NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0); + close(fd); + if (data == MAP_FAILED) + return NULL; + + uint64_t header_size = 0; + memcpy(&header_size, data, 8); + if (header_size > file_size - 8) { + munmap(data, file_size); + return NULL; + } + + safetensors_file_t *sf = calloc(1, sizeof(safetensors_file_t)); + if (!sf) { + munmap(data, file_size); + return NULL; + } + + sf->path = strdup(path); + sf->data = data; + sf->file_size = file_size; + sf->header_size = (size_t)header_size; + + sf->header_json = malloc(header_size + 1); + if (!sf->header_json) { + safetensors_close(sf); + return NULL; + } + memcpy(sf->header_json, (char *)data + 8, header_size); + sf->header_json[header_size] = '\0'; + + if (parse_header(sf) != 0) { + safetensors_close(sf); + return NULL; + } + + return sf; +} + +void safetensors_close(safetensors_file_t *sf) { + if (!sf) + return; + if (sf->data) + munmap(sf->data, sf->file_size); + free(sf->path); + free(sf->header_json); + free(sf); +} + +const void *safetensors_data(const safetensors_file_t *sf, const safetensor_t *t) { + return (const char *)sf->data + 8 + sf->header_size + t->data_offset; +} + +int64_t safetensor_numel(const safetensor_t *t) { + int64_t n = 1; + for (int i = 0; i < t->ndim; i++) + n *= t->shape[i]; + return n; +} + +/* Multi-shard operations */ + +static int cmp_shard_names(const void *a, const void *b) { + const char *sa = (const char *)a; + const char *sb = (const char *)b; + return strcmp(sa, sb); +} + +static int parse_shard_name(const char *name, int *idx, int *total) { + int i = 0, n = 0, consumed = 0; + if (sscanf(name, "model-%d-of-%d.safetensors%n", &i, &n, &consumed) != 2) + return -1; + if (name[consumed] != '\0' || i <= 0 || n <= 0 || i > n) + return -1; + *idx = i; + *total = n; + return 0; +} + +multi_safetensors_t *multi_safetensors_open(const char *model_dir) { + multi_safetensors_t *ms = calloc(1, sizeof(multi_safetensors_t)); + if (!ms) + return NULL; + + char path[4096]; + + /* Try single file first */ + snprintf(path, sizeof(path), "%s/model.safetensors", model_dir); + safetensors_file_t *sf = safetensors_open(path); + if (sf) { + ms->shards[0] = sf; + ms->num_shards = 1; + return ms; + } + + /* Scan directory for shard files */ + DIR *dir = opendir(model_dir); + if (!dir) { + free(ms); + return NULL; + } + + struct dirent *entry; + char shard_names[SAFETENSORS_MAX_SHARDS][256]; + int seen[SAFETENSORS_MAX_SHARDS] = {0}; + int n_shards = 0; + int expected_total = 0; + + while ((entry = readdir(dir)) != NULL) { + if (strncmp(entry->d_name, "model-", 6) == 0 && + strstr(entry->d_name, ".safetensors") != NULL) { + int shard_idx = 0, shard_total = 0; + if (parse_shard_name(entry->d_name, &shard_idx, &shard_total) != 0) { + fprintf(stderr, "multi_safetensors_open: bad shard name: %s\n", entry->d_name); + closedir(dir); + free(ms); + return NULL; + } + if (shard_total > SAFETENSORS_MAX_SHARDS) { + fprintf(stderr, "multi_safetensors_open: too many shards (%d > %d)\n", shard_total, + SAFETENSORS_MAX_SHARDS); + closedir(dir); + free(ms); + return NULL; + } + if (expected_total == 0) { + expected_total = shard_total; + } else if (expected_total != shard_total) { + fprintf(stderr, "multi_safetensors_open: inconsistent shard total in %s\n", + entry->d_name); + closedir(dir); + free(ms); + return NULL; + } + if (seen[shard_idx - 1]) { + fprintf(stderr, "multi_safetensors_open: duplicate shard index %d\n", shard_idx); + closedir(dir); + free(ms); + return NULL; + } + seen[shard_idx - 1] = 1; + + snprintf(shard_names[n_shards], sizeof(shard_names[n_shards]), "%s", entry->d_name); + n_shards++; + } + } + closedir(dir); + + if (n_shards == 0) { + fprintf(stderr, "multi_safetensors_open: no safetensors files in %s\n", model_dir); + free(ms); + return NULL; + } + if (expected_total != n_shards) { + fprintf(stderr, "multi_safetensors_open: expected %d shard(s), found %d in %s\n", + expected_total, n_shards, model_dir); + free(ms); + return NULL; + } + for (int i = 0; i < expected_total; i++) { + if (!seen[i]) { + fprintf(stderr, "multi_safetensors_open: missing shard %d of %d in %s\n", i + 1, + expected_total, model_dir); + free(ms); + return NULL; + } + } + + /* Sort shard names to ensure consistent ordering */ + qsort(shard_names, n_shards, sizeof(shard_names[0]), cmp_shard_names); + + /* Open each shard */ + for (int i = 0; i < n_shards; i++) { + snprintf(path, sizeof(path), "%s/%s", model_dir, shard_names[i]); + ms->shards[i] = safetensors_open(path); + if (!ms->shards[i]) { + fprintf(stderr, "multi_safetensors_open: failed to open %s\n", path); + multi_safetensors_close(ms); + return NULL; + } + } + ms->num_shards = n_shards; + return ms; +} + +void multi_safetensors_close(multi_safetensors_t *ms) { + if (!ms) + return; + for (int i = 0; i < ms->num_shards; i++) { + safetensors_close(ms->shards[i]); + } + free(ms); +} + +int multi_safetensors_data_nbytes(const multi_safetensors_t *ms, size_t *out_nbytes) { + if (!ms || !out_nbytes) + return -1; + size_t total = 0; + for (int s = 0; s < ms->num_shards; s++) { + const safetensors_file_t *sf = ms->shards[s]; + for (int i = 0; i < sf->num_tensors; i++) { + size_t n = sf->tensors[i].data_size; + if (n > SIZE_MAX - total) + return -1; + total += n; + } + } + *out_nbytes = total; + return 0; +} + +const safetensor_t * +multi_safetensors_find(const multi_safetensors_t *ms, const char *name, safetensors_file_t **out_sf) { + for (int s = 0; s < ms->num_shards; s++) { + safetensors_file_t *sf = ms->shards[s]; + for (int i = 0; i < sf->num_tensors; i++) { + if (strcmp(sf->tensors[i].name, name) == 0) { + if (out_sf) + *out_sf = sf; + return &sf->tensors[i]; + } + } + } + return NULL; +} + +const char *multi_safetensors_weight_prefix(const multi_safetensors_t *ms, const char *tensor_name) { + if (multi_safetensors_find(ms, tensor_name, NULL)) + return ""; + + char prefixed[sizeof(((safetensor_t *)0)->name)]; + int n = snprintf(prefixed, sizeof(prefixed), "model.%s", tensor_name); + if (n < 0 || (size_t)n >= sizeof(prefixed)) + return NULL; + return multi_safetensors_find(ms, prefixed, NULL) ? "model." : NULL; +} diff --git a/libffwd/safetensors.h b/libffwd/safetensors.h new file mode 100644 index 0000000..f2ea9fd --- /dev/null +++ b/libffwd/safetensors.h @@ -0,0 +1,71 @@ +/* + * Safetensors file format reader with multi-shard support. + */ + +#ifndef SAFETENSORS_H +#define SAFETENSORS_H + +#include <stddef.h> +#include <stdint.h> + +#define SAFETENSORS_MAX_TENSORS 1024 +#define SAFETENSORS_MAX_SHARDS 8 + +typedef enum { + DTYPE_F32 = 0, + DTYPE_F16 = 1, + DTYPE_BF16 = 2, + DTYPE_I32 = 3, + DTYPE_I64 = 4, + DTYPE_BOOL = 5, + DTYPE_UNKNOWN = -1 +} safetensor_dtype_t; + +typedef struct { + char name[256]; + safetensor_dtype_t dtype; + int ndim; + int64_t shape[8]; + size_t data_offset; + size_t data_size; +} safetensor_t; + +typedef struct { + char *path; + void *data; + size_t file_size; + size_t header_size; + char *header_json; + int num_tensors; + safetensor_t tensors[SAFETENSORS_MAX_TENSORS]; +} safetensors_file_t; + +/* Multi-shard wrapper: opens all shard files and provides unified tensor lookup */ +typedef struct { + safetensors_file_t *shards[SAFETENSORS_MAX_SHARDS]; + int num_shards; +} multi_safetensors_t; + +/* Open a single safetensors file (memory-mapped) */ +safetensors_file_t *safetensors_open(const char *path); +void safetensors_close(safetensors_file_t *sf); + +/* Open model from directory (auto-detects single file or multi-shard) */ +multi_safetensors_t *multi_safetensors_open(const char *model_dir); +void multi_safetensors_close(multi_safetensors_t *ms); +int multi_safetensors_data_nbytes(const multi_safetensors_t *ms, size_t *out_nbytes); + +/* Find a tensor by name across all shards */ +const safetensor_t * +multi_safetensors_find(const multi_safetensors_t *ms, const char *name, safetensors_file_t **out_sf); + +/* Resolve an optional Hugging Face "model." namespace for a backbone tensor. + * Returns "", "model.", or NULL when neither form exists. */ +const char *multi_safetensors_weight_prefix(const multi_safetensors_t *ms, const char *tensor_name); + +/* Get raw pointer to tensor data (within mmap'd region) */ +const void *safetensors_data(const safetensors_file_t *sf, const safetensor_t *t); + +int64_t safetensor_numel(const safetensor_t *t); + +#endif /* SAFETENSORS_H */ diff --git a/libffwd/sentencepiece.c b/libffwd/sentencepiece.c new file mode 100644 index 0000000..e5e8393 --- /dev/null +++ b/libffwd/sentencepiece.c @@ -0,0 +1,947 @@ +/* + * SentencePiece Unigram tokenizer. + * + * The implementation reads the small subset of ModelProto needed at runtime: + * pieces, scores, piece types, trainer model_type, and normalizer options. The + * normalizer supports SentencePiece's precompiled charsmap format used by + * nmt_nfkc, then the encoder runs the standard Unigram Viterbi dynamic program + * over UTF-8 byte offsets. + */ + +#include "sentencepiece.h" + +#include <errno.h> +#include <float.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +enum { + SP_MODEL_UNIGRAM = 1, + SP_PIECE_NORMAL = 1, + SP_PIECE_UNKNOWN = 2, + SP_PIECE_CONTROL = 3, + SP_PIECE_USER_DEFINED = 4, + SP_PIECE_UNUSED = 5, + SP_PIECE_BYTE = 6, +}; + +typedef struct { + const char *key; + uint16_t len; + int val; +} sp_entry_t; + +typedef struct { + int id; + int prev; + double score; +} sp_best_t; + +struct tok_spm_workspace { + char *norm; + size_t norm_len; + size_t norm_cap; + sp_best_t *best; + int best_cap; + int *ids; + int ids_cap; +}; + +static uint64_t fnv1a_bytes(const char *s, size_t n) { + uint64_t h = 1469598103934665603ULL; + for (size_t i = 0; i < n; i++) { + h ^= (unsigned char)s[i]; + h *= 1099511628211ULL; + } + return h; +} + +static int next_pow2(int x) { + int p = 1; + while (p < x && p <= (1 << 29)) + p <<= 1; + return p; +} + +static int map_put(sp_entry_t *map, int cap, const char *key, int len, int val) { + if (len <= 0 || len > UINT16_MAX) + return -1; + uint64_t mask = (uint64_t)cap - 1; + int pos = (int)(fnv1a_bytes(key, (size_t)len) & mask); + while (map[pos].key) { + if (map[pos].len == (uint16_t)len && memcmp(map[pos].key, key, (size_t)len) == 0) { + map[pos].val = val; + return 0; + } + pos = (int)((pos + 1) & (int)mask); + } + map[pos].key = key; + map[pos].len = (uint16_t)len; + map[pos].val = val; + return 0; +} + +static int map_get(const sp_entry_t *map, int cap, const char *key, int len) { + if (!map || len <= 0) + return -1; + uint64_t mask = (uint64_t)cap - 1; + int pos = (int)(fnv1a_bytes(key, (size_t)len) & mask); + while (map[pos].key) { + if (map[pos].len == (uint16_t)len && memcmp(map[pos].key, key, (size_t)len) == 0) + return map[pos].val; + pos = (int)((pos + 1) & (int)mask); + } + return -1; +} + +static int is_tokenizable_type(int type) { + return type == SP_PIECE_NORMAL || type == SP_PIECE_USER_DEFINED || type == SP_PIECE_UNUSED || + type == SP_PIECE_BYTE; +} + +static int reserve_chars(char **buf, size_t *cap, size_t need) { + if (need <= *cap) + return 0; + size_t nc = *cap ? *cap : 128; + while (nc < need) { + if (nc > SIZE_MAX / 2) + return -1; + nc *= 2; + } + char *p = (char *)realloc(*buf, nc); + if (!p) + return -1; + *buf = p; + *cap = nc; + return 0; +} + +static int append_bytes(char **buf, size_t *len, size_t *cap, const char *s, size_t n) { + if (n > SIZE_MAX - *len - 1 || reserve_chars(buf, cap, *len + n + 1) != 0) + return -1; + memcpy(*buf + *len, s, n); + *len += n; + (*buf)[*len] = '\0'; + return 0; +} + +static int reserve_best(tok_spm_workspace_t *ws, int need) { + if (need <= ws->best_cap) + return 0; + int nc = ws->best_cap ? ws->best_cap : 128; + while (nc < need) { + if (nc > INT_MAX / 2) + return -1; + nc *= 2; + } + sp_best_t *p = (sp_best_t *)realloc(ws->best, (size_t)nc * sizeof(*p)); + if (!p) + return -1; + ws->best = p; + ws->best_cap = nc; + return 0; +} + +static int reserve_ids(tok_spm_workspace_t *ws, int need) { + if (need <= ws->ids_cap) + return 0; + int nc = ws->ids_cap ? ws->ids_cap : 64; + while (nc < need) { + if (nc > INT_MAX / 2) + return -1; + nc *= 2; + } + int *p = (int *)realloc(ws->ids, (size_t)nc * sizeof(*p)); + if (!p) + return -1; + ws->ids = p; + ws->ids_cap = nc; + return 0; +} + +static int utf8_len(unsigned char c) { + if (c < 0x80) + return 1; + if ((c & 0xE0) == 0xC0) + return 2; + if ((c & 0xF0) == 0xE0) + return 3; + if ((c & 0xF8) == 0xF0) + return 4; + return 1; +} + +static int valid_utf8_one(const char *s, size_t n, size_t off, size_t *len_out) { + if (off >= n) + return 0; + const unsigned char *p = (const unsigned char *)s + off; + int len = utf8_len(p[0]); + if (off + (size_t)len > n) { + *len_out = 1; + return 0; + } + if (len > 1) { + for (int i = 1; i < len; i++) { + if ((p[i] & 0xC0) != 0x80) { + *len_out = 1; + return 0; + } + } + } + *len_out = (size_t)len; + return 1; +} + +static uint32_t read_le32(const unsigned char *p) { + return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); +} + +static int pb_read_varint(const unsigned char **p, const unsigned char *end, uint64_t *out) { + uint64_t v = 0; + int shift = 0; + while (*p < end && shift <= 63) { + unsigned char c = *(*p)++; + v |= (uint64_t)(c & 0x7f) << shift; + if (!(c & 0x80)) { + *out = v; + return 0; + } + shift += 7; + } + return -1; +} + +static int pb_read_key(const unsigned char **p, const unsigned char *end, int *field, int *wire) { + uint64_t key = 0; + if (pb_read_varint(p, end, &key) != 0) + return -1; + *field = (int)(key >> 3); + *wire = (int)(key & 7); + return *field > 0 ? 0 : -1; +} + +static int pb_read_len(const unsigned char **p, + const unsigned char *end, + const unsigned char **data, + size_t *len) { + uint64_t n = 0; + if (pb_read_varint(p, end, &n) != 0 || n > (uint64_t)(end - *p)) + return -1; + *data = *p; + *len = (size_t)n; + *p += n; + return 0; +} + +static int pb_skip(const unsigned char **p, const unsigned char *end, int wire) { + uint64_t v = 0; + const unsigned char *data = NULL; + size_t len = 0; + switch (wire) { + case 0: + return pb_read_varint(p, end, &v); + case 1: + if ((size_t)(end - *p) < 8) + return -1; + *p += 8; + return 0; + case 2: + return pb_read_len(p, end, &data, &len); + case 5: + if ((size_t)(end - *p) < 4) + return -1; + *p += 4; + return 0; + default: + return -1; + } +} + +static char *memdup0(const unsigned char *data, size_t len) { + char *s = (char *)malloc(len + 1); + if (!s) + return NULL; + memcpy(s, data, len); + s[len] = '\0'; + return s; +} + +static int parse_piece(tok_spm_t *tok, int idx, const unsigned char *data, size_t len) { + const unsigned char *p = data, *end = data + len; + char *piece = NULL; + float score = 0.0f; + int type = SP_PIECE_NORMAL; + while (p < end) { + int field = 0, wire = 0; + if (pb_read_key(&p, end, &field, &wire) != 0) + goto fail; + if (field == 1 && wire == 2) { + const unsigned char *s = NULL; + size_t n = 0; + if (pb_read_len(&p, end, &s, &n) != 0 || n > UINT16_MAX) + goto fail; + free(piece); + piece = memdup0(s, n); + if (!piece) + goto fail; + tok->piece_lens[idx] = (uint16_t)n; + } else if (field == 2 && wire == 5) { + if ((size_t)(end - p) < 4) + goto fail; + uint32_t bits = read_le32(p); + memcpy(&score, &bits, sizeof(score)); + p += 4; + } else if (field == 3 && wire == 0) { + uint64_t v = 0; + if (pb_read_varint(&p, end, &v) != 0 || v > UINT8_MAX) + goto fail; + type = (int)v; + } else if (pb_skip(&p, end, wire) != 0) { + goto fail; + } + } + if (!piece) + goto fail; + tok->id_to_piece[idx] = piece; + tok->scores[idx] = score; + tok->types[idx] = (uint8_t)type; + if (is_tokenizable_type(type) && tok->piece_lens[idx] > tok->max_piece_len) + tok->max_piece_len = tok->piece_lens[idx]; + return 0; +fail: + free(piece); + return -1; +} + +static int parse_trainer(tok_spm_t *tok, const unsigned char *data, size_t len) { + const unsigned char *p = data, *end = data + len; + int model_type = SP_MODEL_UNIGRAM; + while (p < end) { + int field = 0, wire = 0; + if (pb_read_key(&p, end, &field, &wire) != 0) + return -1; + if (field == 3 && wire == 0) { + uint64_t v = 0; + if (pb_read_varint(&p, end, &v) != 0 || v > INT_MAX) + return -1; + model_type = (int)v; + } else if (pb_skip(&p, end, wire) != 0) { + return -1; + } + } + if (model_type != SP_MODEL_UNIGRAM) { + fprintf(stderr, "sentencepiece: unsupported model_type %d (only Unigram is implemented)\n", + model_type); + return -1; + } + (void)tok; + return 0; +} + +static int parse_normalizer(tok_spm_t *tok, const unsigned char *data, size_t len) { + const unsigned char *p = data, *end = data + len; + while (p < end) { + int field = 0, wire = 0; + if (pb_read_key(&p, end, &field, &wire) != 0) + return -1; + if ((field == 3 || field == 4 || field == 5) && wire == 0) { + uint64_t v = 0; + if (pb_read_varint(&p, end, &v) != 0) + return -1; + if (field == 3) + tok->add_dummy_prefix = v != 0; + else if (field == 4) + tok->remove_extra_whitespaces = v != 0; + else + tok->escape_whitespaces = v != 0; + } else if (field == 2 && wire == 2) { + const unsigned char *blob = NULL; + size_t n = 0; + if (pb_read_len(&p, end, &blob, &n) != 0) + return -1; + if (n >= 4) { + uint32_t xcda_bytes = read_le32(blob); + if ((size_t)xcda_bytes + 4 <= n && (xcda_bytes % 4) == 0) { + tok->xcda_count = xcda_bytes / 4; + tok->xcda = (uint32_t *)malloc(tok->xcda_count * sizeof(*tok->xcda)); + if (!tok->xcda) + return -1; + for (size_t i = 0; i < tok->xcda_count; i++) + tok->xcda[i] = read_le32(blob + 4 + i * 4); + tok->prefix_replacements_len = n - 4 - xcda_bytes; + tok->prefix_replacements = (char *)malloc(tok->prefix_replacements_len + 1); + if (!tok->prefix_replacements) + return -1; + memcpy(tok->prefix_replacements, blob + 4 + xcda_bytes, + tok->prefix_replacements_len); + tok->prefix_replacements[tok->prefix_replacements_len] = '\0'; + } + } + } else if (pb_skip(&p, end, wire) != 0) { + return -1; + } + } + return 0; +} + +static int count_pieces(const unsigned char *data, size_t len) { + const unsigned char *p = data, *end = data + len; + int n = 0; + while (p < end) { + int field = 0, wire = 0; + if (pb_read_key(&p, end, &field, &wire) != 0) + return -1; + if (field == 1 && wire == 2) { + const unsigned char *msg = NULL; + size_t msg_len = 0; + if (pb_read_len(&p, end, &msg, &msg_len) != 0) + return -1; + n++; + } else if (pb_skip(&p, end, wire) != 0) { + return -1; + } + } + return n; +} + +static int parse_model(tok_spm_t *tok, const unsigned char *data, size_t len) { + int n_pieces = count_pieces(data, len); + if (n_pieces <= 0) + return -1; + tok->piece_count = n_pieces; + tok->id_to_piece = (char **)calloc((size_t)n_pieces, sizeof(*tok->id_to_piece)); + tok->piece_lens = (uint16_t *)calloc((size_t)n_pieces, sizeof(*tok->piece_lens)); + tok->scores = (float *)calloc((size_t)n_pieces, sizeof(*tok->scores)); + tok->types = (uint8_t *)calloc((size_t)n_pieces, sizeof(*tok->types)); + if (!tok->id_to_piece || !tok->piece_lens || !tok->scores || !tok->types) + return -1; + + const unsigned char *p = data, *end = data + len; + int piece_idx = 0; + int saw_trainer = 0; + while (p < end) { + int field = 0, wire = 0; + if (pb_read_key(&p, end, &field, &wire) != 0) + return -1; + if (wire == 2 && field == 1) { + const unsigned char *msg = NULL; + size_t msg_len = 0; + if (pb_read_len(&p, end, &msg, &msg_len) != 0 || piece_idx >= n_pieces || + parse_piece(tok, piece_idx++, msg, msg_len) != 0) + return -1; + } else if (wire == 2 && field == 2) { + const unsigned char *msg = NULL; + size_t msg_len = 0; + if (pb_read_len(&p, end, &msg, &msg_len) != 0 || parse_trainer(tok, msg, msg_len) != 0) + return -1; + saw_trainer = 1; + } else if (wire == 2 && field == 3) { + const unsigned char *msg = NULL; + size_t msg_len = 0; + if (pb_read_len(&p, end, &msg, &msg_len) != 0 || parse_normalizer(tok, msg, msg_len) != 0) + return -1; + } else if (pb_skip(&p, end, wire) != 0) { + return -1; + } + } + if (piece_idx != n_pieces || !saw_trainer) + return -1; + return 0; +} + +static int raw_piece_id(const tok_spm_t *tok, const char *piece) { + if (!tok || !piece) + return -1; + return map_get((const sp_entry_t *)tok->piece_map, tok->piece_map_cap, piece, (int)strlen(piece)); +} + +static int raw_to_model_id(const tok_spm_t *tok, int raw_id) { + if (!tok || raw_id < 0 || raw_id >= tok->piece_count) + return -1; + if (tok->id_map == TOK_SPM_XLM_ROBERTA) { + if (raw_id == 1) + return 0; /* <s> */ + if (raw_id == 2) + return 2; /* </s> */ + if (raw_id == 0) + return 3; /* <unk> */ + return raw_id + 1; + } + return raw_id; +} + +static int model_to_raw_id(const tok_spm_t *tok, int id) { + if (!tok || id < 0) + return -1; + if (tok->id_map == TOK_SPM_XLM_ROBERTA) { + if (id == 0) + return 1; + if (id == 2) + return 2; + if (id == 3) + return 0; + if (id >= 4 && id <= tok->piece_count) + return id - 1; + return -1; + } + return id < tok->piece_count ? id : -1; +} + +static int file_contains_xlm_roberta(const char *model_dir, const char *name) { + char path[1024]; + int n = snprintf(path, sizeof(path), "%s/%s", model_dir, name); + if (n < 0 || (size_t)n >= sizeof(path)) + return 0; + FILE *f = fopen(path, "rb"); + if (!f) + return 0; + if (fseek(f, 0, SEEK_END) != 0) { + fclose(f); + return 0; + } + long sz = ftell(f); + if (sz < 0 || (size_t)sz == SIZE_MAX || fseek(f, 0, SEEK_SET) != 0) { + fclose(f); + return 0; + } + char *buf = (char *)malloc((size_t)sz + 1); + if (!buf) { + fclose(f); + return 0; + } + size_t got = fread(buf, 1, (size_t)sz, f); + fclose(f); + buf[got] = '\0'; + int yes = strstr(buf, "XLMRobertaTokenizer") != NULL || + strstr(buf, "XLMRobertaTokenizerFast") != NULL || + strstr(buf, "XLMRobertaModel") != NULL || strstr(buf, "xlm-roberta") != NULL; + free(buf); + return yes; +} + +static int detect_xlm_roberta(const char *model_dir) { + return file_contains_xlm_roberta(model_dir, "tokenizer_config.json") || + file_contains_xlm_roberta(model_dir, "config.json"); +} + +static int find_model_file(const char *model_dir, char path[1024]) { + static const char *names[] = { + "sentencepiece.bpe.model", + "tokenizer.model", + "spiece.model", + }; + for (int i = 0; i < (int)(sizeof(names) / sizeof(names[0])); i++) { + int n = snprintf(path, 1024, "%s/%s", model_dir, names[i]); + if (n < 0 || n >= 1024) + return -1; + FILE *f = fopen(path, "rb"); + if (f) { + fclose(f); + return 0; + } + if (errno != ENOENT) + return -1; + } + return -1; +} + +static int load_file(const char *path, unsigned char **out, size_t *out_len) { + FILE *f = fopen(path, "rb"); + if (!f) + return -1; + if (fseek(f, 0, SEEK_END) != 0) { + fclose(f); + return -1; + } + long sz = ftell(f); + if (sz < 0 || (size_t)sz == SIZE_MAX || fseek(f, 0, SEEK_SET) != 0) { + fclose(f); + return -1; + } + unsigned char *buf = (unsigned char *)malloc((size_t)sz); + if (!buf) { + fclose(f); + return -1; + } + if (fread(buf, 1, (size_t)sz, f) != (size_t)sz) { + fclose(f); + free(buf); + return -1; + } + fclose(f); + *out = buf; + *out_len = (size_t)sz; + return 0; +} + +static uint32_t xcda_base(const tok_spm_t *tok, size_t idx) { + uint32_t v = tok->xcda[idx]; + return (v >> 10) << ((v & (1u << 9)) >> 6); +} + +static uint32_t xcda_lcheck(const tok_spm_t *tok, size_t idx) { + return tok->xcda[idx] & ((1u << 31) | 0xffu); +} + +static int xcda_leaf(const tok_spm_t *tok, size_t idx) { return (tok->xcda[idx] >> 8) & 1u; } + +static uint32_t xcda_value(const tok_spm_t *tok, size_t idx) { + return tok->xcda[idx] & ((1u << 31) - 1u); +} + +typedef struct { + const char *data; + size_t len; + size_t consumed; +} norm_prefix_t; + +static norm_prefix_t +normalize_prefix(const tok_spm_t *tok, const char *input, size_t input_len, size_t off) { + static const char repl[] = "\xef\xbf\xbd"; + if (off >= input_len) + return (norm_prefix_t){input + off, 0, 0}; + + size_t best_len = 0; + size_t best_repl = 0; + if (tok->xcda && tok->xcda_count > 0) { + uint32_t node = xcda_base(tok, 0); + for (size_t p = off; p < input_len; p++) { + unsigned char c = (unsigned char)input[p]; + if (c == 0) + break; + node ^= c; + if (node >= tok->xcda_count || xcda_lcheck(tok, node) != c) + break; + int leaf = xcda_leaf(tok, node); + node ^= xcda_base(tok, node); + if (leaf) { + best_len = p - off + 1; + best_repl = xcda_value(tok, node); + } + } + } + if (best_len > 0 && best_repl < tok->prefix_replacements_len) { + const char *r = tok->prefix_replacements + best_repl; + size_t max = tok->prefix_replacements_len - best_repl; + size_t n = strnlen(r, max); + return (norm_prefix_t){r, n, best_len}; + } + + size_t clen = 1; + if (valid_utf8_one(input, input_len, off, &clen)) + return (norm_prefix_t){input + off, clen, clen}; + return (norm_prefix_t){repl, 3, 1}; +} + +static int normalize_into(const tok_spm_t *tok, tok_spm_workspace_t *ws, const char *text) { + ws->norm_len = 0; + if (!text) + return -1; + size_t input_len = strlen(text); + const char space[] = "\xe2\x96\x81"; /* U+2581 */ + const char *space_s = tok->escape_whitespaces ? space : " "; + size_t space_n = tok->escape_whitespaces ? 3 : 1; + int processing_non_ws = 0; + int space_prepended = 0; + + for (size_t off = 0; off < input_len;) { + norm_prefix_t r = normalize_prefix(tok, text, input_len, off); + for (size_t i = 0; i < r.len; i++) { + char c = r.data[i]; + if (c != ' ') { + if (!processing_non_ws) { + processing_non_ws = 1; + if ((tok->add_dummy_prefix && !space_prepended) || + tok->remove_extra_whitespaces) { + if (append_bytes(&ws->norm, &ws->norm_len, &ws->norm_cap, space_s, space_n) != + 0) + return -1; + space_prepended = 1; + } + } + if (append_bytes(&ws->norm, &ws->norm_len, &ws->norm_cap, &c, 1) != 0) + return -1; + } else { + processing_non_ws = 0; + if (!tok->remove_extra_whitespaces) { + if (append_bytes(&ws->norm, &ws->norm_len, &ws->norm_cap, space_s, space_n) != 0) + return -1; + } + } + } + off += r.consumed ? r.consumed : 1; + } + if (ws->norm) + ws->norm[ws->norm_len] = '\0'; + return 0; +} + +static double compute_unknown_score(const tok_spm_t *tok) { + float min_score = FLT_MAX; + for (int i = 0; i < tok->piece_count; i++) { + if (tok->types[i] == SP_PIECE_NORMAL && tok->scores[i] < min_score) + min_score = tok->scores[i]; + } + if (min_score == FLT_MAX) + min_score = -10.0f; + return (double)min_score - 10.0; +} + +static int segment(tok_spm_t const *tok, tok_spm_workspace_t *ws) { + size_t n = ws->norm_len; + if (n == 0) + return 0; + if (n > (size_t)INT_MAX - 1 || reserve_best(ws, (int)n + 1) != 0 || + reserve_ids(ws, (int)n + 1) != 0) + return -1; + for (size_t i = 0; i <= n; i++) + ws->best[i] = (sp_best_t){tok->unk_id, -1, -DBL_MAX}; + ws->best[0] = (sp_best_t){tok->unk_id, 0, 0.0}; + double unk_score = tok->unknown_score; + + for (size_t off = 0; off < n;) { + size_t cp_len = 1; + valid_utf8_one(ws->norm, n, off, &cp_len); + sp_best_t cur = ws->best[off]; + if (cur.score <= -DBL_MAX / 2.0) { + off += cp_len; + continue; + } + + int single_cp_found = 0; + for (size_t end = off + cp_len; end <= n && end - off <= (size_t)tok->max_piece_len;) { + int raw = map_get((const sp_entry_t *)tok->piece_map, tok->piece_map_cap, ws->norm + off, + (int)(end - off)); + if (raw >= 0 && is_tokenizable_type(tok->types[raw])) { + if (end - off == cp_len) + single_cp_found = 1; + double sc = + cur.score + (tok->types[raw] == SP_PIECE_USER_DEFINED ? 0.0 : tok->scores[raw]); + if (sc > ws->best[end].score) + ws->best[end] = (sp_best_t){raw_to_model_id(tok, raw), (int)off, sc}; + } + if (end == n) + break; + size_t next_len = 1; + valid_utf8_one(ws->norm, n, end, &next_len); + end += next_len; + } + if (!single_cp_found) { + size_t end = off + cp_len; + double sc = cur.score + unk_score; + if (sc > ws->best[end].score) + ws->best[end] = (sp_best_t){tok->unk_id, (int)off, sc}; + } + off += cp_len; + } + + int out_n = 0; + int prev_unk = 0; + for (size_t off = n; off > 0;) { + sp_best_t b = ws->best[off]; + if (b.prev < 0 || b.prev >= (int)off) + return -1; + int is_unk = b.id == tok->unk_id; + if (!(prev_unk && is_unk)) { + if (out_n >= ws->ids_cap) + return -1; + ws->ids[out_n++] = b.id; + } + prev_unk = is_unk; + off = (size_t)b.prev; + } + for (int i = 0; i < out_n / 2; i++) { + int t = ws->ids[i]; + ws->ids[i] = ws->ids[out_n - 1 - i]; + ws->ids[out_n - 1 - i] = t; + } + return out_n; +} + +tok_spm_t *tok_spm_load(const char *model_dir) { + if (!model_dir) + return NULL; + char path[1024]; + if (find_model_file(model_dir, path) != 0) { + fprintf(stderr, "sentencepiece: model file not found under %s\n", model_dir); + return NULL; + } + unsigned char *data = NULL; + size_t len = 0; + if (load_file(path, &data, &len) != 0) { + fprintf(stderr, "sentencepiece: cannot read %s\n", path); + return NULL; + } + + tok_spm_t *tok = (tok_spm_t *)calloc(1, sizeof(tok_spm_t)); + if (!tok) { + free(data); + return NULL; + } + tok->add_dummy_prefix = 1; + tok->remove_extra_whitespaces = 1; + tok->escape_whitespaces = 1; + tok->id_map = detect_xlm_roberta(model_dir) ? TOK_SPM_XLM_ROBERTA : TOK_SPM_IDENTITY; + if (parse_model(tok, data, len) != 0) { + fprintf(stderr, "sentencepiece: malformed or unsupported model: %s\n", path); + free(data); + tok_spm_free(tok); + return NULL; + } + free(data); + + int cap = next_pow2(tok->piece_count * 2 + 1); + tok->piece_map = calloc((size_t)cap, sizeof(sp_entry_t)); + if (!tok->piece_map) { + tok_spm_free(tok); + return NULL; + } + tok->piece_map_cap = cap; + for (int i = 0; i < tok->piece_count; i++) { + if (!tok->id_to_piece[i] || map_put((sp_entry_t *)tok->piece_map, cap, tok->id_to_piece[i], + tok->piece_lens[i], i) != 0) { + tok_spm_free(tok); + return NULL; + } + } + + tok->unk_id = raw_to_model_id(tok, raw_piece_id(tok, "<unk>")); + tok->bos_id = raw_to_model_id(tok, raw_piece_id(tok, "<s>")); + tok->eos_id = raw_to_model_id(tok, raw_piece_id(tok, "</s>")); + tok->pad_id = + tok->id_map == TOK_SPM_XLM_ROBERTA ? 1 : raw_to_model_id(tok, raw_piece_id(tok, "<pad>")); + tok->mask_id = tok->id_map == TOK_SPM_XLM_ROBERTA + ? tok->piece_count + 1 + : raw_to_model_id(tok, raw_piece_id(tok, "<mask>")); + tok->vocab_size = tok->id_map == TOK_SPM_XLM_ROBERTA ? tok->piece_count + 2 : tok->piece_count; + tok->unknown_score = compute_unknown_score(tok); + if (tok->unk_id < 0 || tok->max_piece_len <= 0) { + fprintf(stderr, "sentencepiece: missing <unk> or normal pieces in %s\n", path); + tok_spm_free(tok); + return NULL; + } + return tok; +} + +const char *tok_spm_decode(const tok_spm_t *tok, int id) { + if (!tok || id < 0) + return NULL; + if (tok->id_map == TOK_SPM_XLM_ROBERTA) { + if (id == 1) + return "<pad>"; + if (id == tok->piece_count + 1) + return "<mask>"; + } + int raw = model_to_raw_id(tok, id); + return raw >= 0 ? tok->id_to_piece[raw] : NULL; +} + +int tok_spm_token_id(const tok_spm_t *tok, const char *token) { + if (!tok || !token) + return -1; + if (tok->id_map == TOK_SPM_XLM_ROBERTA) { + if (!strcmp(token, "<s>")) + return 0; + if (!strcmp(token, "<pad>")) + return 1; + if (!strcmp(token, "</s>")) + return 2; + if (!strcmp(token, "<unk>")) + return 3; + if (!strcmp(token, "<mask>")) + return tok->piece_count + 1; + } + return raw_to_model_id(tok, raw_piece_id(tok, token)); +} + +int tok_spm_encode_into(const tok_spm_t *tok, + tok_spm_workspace_t *ws, + const char *text, + int *out_ids, + int out_cap, + int *out_n_tokens) { + if (out_n_tokens) + *out_n_tokens = 0; + if (!tok || !ws || !text) + return -1; + if (normalize_into(tok, ws, text) != 0) + return -1; + int n = segment(tok, ws); + if (n < 0) + return -1; + if (out_n_tokens) + *out_n_tokens = n; + if (n == 0) + return 0; + if (!out_ids || out_cap < n) + return -2; + memcpy(out_ids, ws->ids, (size_t)n * sizeof(*out_ids)); + return 0; +} + +int *tok_spm_encode_with_workspace(const tok_spm_t *tok, + tok_spm_workspace_t *ws, + const char *text, + int *out_n_tokens) { + if (out_n_tokens) + *out_n_tokens = 0; + if (!tok || !ws || !text) + return NULL; + int n = 0; + int rc = tok_spm_encode_into(tok, ws, text, NULL, 0, &n); + if (rc < 0 && rc != -2) + return NULL; + if (out_n_tokens) + *out_n_tokens = n; + if (n == 0) + return NULL; + int *ids = (int *)malloc((size_t)n * sizeof(*ids)); + if (!ids) + return NULL; + memcpy(ids, ws->ids, (size_t)n * sizeof(*ids)); + return ids; +} + +int *tok_spm_encode(const tok_spm_t *tok, const char *text, int *out_n_tokens) { + tok_spm_workspace_t *ws = tok_spm_workspace_new(); + if (!ws) { + if (out_n_tokens) + *out_n_tokens = 0; + return NULL; + } + int *ids = tok_spm_encode_with_workspace(tok, ws, text, out_n_tokens); + tok_spm_workspace_free(ws); + return ids; +} + +tok_spm_workspace_t *tok_spm_workspace_new(void) { + return (tok_spm_workspace_t *)calloc(1, sizeof(tok_spm_workspace_t)); +} + +void tok_spm_workspace_free(tok_spm_workspace_t *ws) { + if (!ws) + return; + free(ws->norm); + free(ws->best); + free(ws->ids); + free(ws); +} + +void tok_spm_free(tok_spm_t *tok) { + if (!tok) + return; + if (tok->id_to_piece) { + for (int i = 0; i < tok->piece_count; i++) + free(tok->id_to_piece[i]); + } + free(tok->id_to_piece); + free(tok->piece_lens); + free(tok->scores); + free(tok->types); + free(tok->piece_map); + free(tok->xcda); + free(tok->prefix_replacements); + free(tok); +} diff --git a/libffwd/sentencepiece.h b/libffwd/sentencepiece.h new file mode 100644 index 0000000..b79f47c --- /dev/null +++ b/libffwd/sentencepiece.h @@ -0,0 +1,88 @@ +/* + * SentencePiece tokenizer for XLM-R style models. + * + * Loads a SentencePiece ModelProto (sentencepiece.bpe.model, tokenizer.model, + * or spiece.model) and implements the Unigram Viterbi encoder used by XLM-R + * and multilingual E5. It encodes text only; frontends add the model's special + * token layout, e.g. <s> ... </s> for XLM-R. + */ + +#ifndef TOK_SPM_H +#define TOK_SPM_H + +#include <stddef.h> +#include <stdint.h> + +typedef enum { + TOK_SPM_IDENTITY = 0, + TOK_SPM_XLM_ROBERTA = 1, +} tok_spm_id_map_t; + +typedef struct { + char **id_to_piece; /* [piece_count] raw SentencePiece ids */ + uint16_t *piece_lens; /* byte length for id_to_piece entries */ + float *scores; /* SentencePiece score per raw id */ + uint8_t *types; /* SentencePiece piece type per raw id */ + int piece_count; /* raw SentencePiece vocabulary size */ + int vocab_size; /* model embedding vocab size after id mapping */ + int unk_id; /* model-space unknown-token id */ + int bos_id; /* model-space beginning-token id, or -1 */ + int eos_id; /* model-space end-token id, or -1 */ + int pad_id; /* model-space pad-token id, or -1 */ + int mask_id; /* model-space mask-token id, or -1 */ + int max_piece_len; /* max normal/user/unused piece byte length */ + double unknown_score; /* score assigned to an unmatched UTF-8 codepoint */ + int add_dummy_prefix; + int remove_extra_whitespaces; + int escape_whitespaces; + tok_spm_id_map_t id_map; + + void *piece_map; /* piece bytes -> raw SentencePiece id */ + int piece_map_cap; + + uint32_t *xcda; + size_t xcda_count; + char *prefix_replacements; + size_t prefix_replacements_len; +} tok_spm_t; + +typedef struct tok_spm_workspace tok_spm_workspace_t; + +/* Load the first supported SentencePiece model found in model_dir. Returns NULL + * on missing model file, unsupported model type, malformed protobuf, or OOM. */ +tok_spm_t *tok_spm_load(const char *model_dir); + +/* Decode one model-space id to an internal token string, or NULL when invalid. */ +const char *tok_spm_decode(const tok_spm_t *tok, int id); + +/* Resolve a named token (e.g. "<s>", "</s>", "<pad>", "<mask>") to a + * model-space id, or -1 when absent. */ +int tok_spm_token_id(const tok_spm_t *tok, const char *token); + +/* Encode UTF-8 text into malloc'd model-space token ids. Empty text returns + * NULL with *out_n_tokens = 0. NULL with a negative/unspecified count is an + * error. */ +int *tok_spm_encode(const tok_spm_t *tok, const char *text, int *out_n_tokens); + +tok_spm_workspace_t *tok_spm_workspace_new(void); +void tok_spm_workspace_free(tok_spm_workspace_t *ws); + +/* Encode into caller-provided out_ids. Returns 0 on success, -1 on error, -2 if + * out_cap is too small. *out_n_tokens receives the emitted/needed token count. */ +int tok_spm_encode_into(const tok_spm_t *tok, + tok_spm_workspace_t *ws, + const char *text, + int *out_ids, + int out_cap, + int *out_n_tokens); + +/* Same tokenization as tok_spm_encode(), reusing ws internally + * and performing only the final returned int[] allocation. */ +int *tok_spm_encode_with_workspace(const tok_spm_t *tok, + tok_spm_workspace_t *ws, + const char *text, + int *out_n_tokens); + +void tok_spm_free(tok_spm_t *tok); + +#endif /* TOK_SPM_H */ diff --git a/libffwd/threadpool.c b/libffwd/threadpool.c new file mode 100644 index 0000000..6cb4252 --- /dev/null +++ b/libffwd/threadpool.c @@ -0,0 +1,218 @@ +/* + * CPU thread pool. + */ + +#if defined(__linux__) && !defined(_GNU_SOURCE) +# define _GNU_SOURCE +#endif + +#include "threadpool.h" + +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifdef __APPLE__ +# include <sys/sysctl.h> +#else +# ifdef __linux__ +# include <sched.h> +# endif +# include <unistd.h> +#endif + +#if defined(USE_OPENBLAS) +void openblas_set_num_threads(int num_threads); +#endif + +#define TP_MAX_THREADS 16 + +static struct { + pthread_t threads[TP_MAX_THREADS - 1]; + int tids[TP_MAX_THREADS - 1]; + int n_threads; + int shutdown; + + parallel_fn_t fn; + void *arg; + uint64_t generation; + + pthread_mutex_t mutex; + pthread_cond_t cond_work; + pthread_cond_t cond_done; + int n_done; +} tp = { + .n_threads = 1, + .shutdown = 0, + .generation = 0, + .mutex = PTHREAD_MUTEX_INITIALIZER, + .cond_work = PTHREAD_COND_INITIALIZER, + .cond_done = PTHREAD_COND_INITIALIZER, +}; + +static void *worker_loop(void *arg) { + int tid = *(int *)arg; + uint64_t my_gen = 0; + + for (;;) { + pthread_mutex_lock(&tp.mutex); + while (tp.generation == my_gen && !tp.shutdown) + pthread_cond_wait(&tp.cond_work, &tp.mutex); + if (tp.shutdown) { + pthread_mutex_unlock(&tp.mutex); + return NULL; + } + my_gen = tp.generation; + parallel_fn_t fn = tp.fn; + void *a = tp.arg; + int nt = tp.n_threads; + pthread_mutex_unlock(&tp.mutex); + + fn(tid, nt, a); + + pthread_mutex_lock(&tp.mutex); + if (++tp.n_done >= tp.n_threads - 1) + pthread_cond_signal(&tp.cond_done); + pthread_mutex_unlock(&tp.mutex); + } +} + +void tp_set_threads(int n) { + if (n < 1) + n = 1; + if (n > TP_MAX_THREADS) + n = TP_MAX_THREADS; + +#if defined(USE_OPENBLAS) + /* OpenBLAS owns the heavy F32 GEMM parallelism on Linux. Keep the + * auxiliary pool serial to avoid nested thread contention. */ + int blas_threads = n; + openblas_set_num_threads(blas_threads); + n = 1; +#endif + + /* Shutdown existing workers */ + if (tp.n_threads > 1) { + pthread_mutex_lock(&tp.mutex); + tp.shutdown = 1; + pthread_cond_broadcast(&tp.cond_work); + pthread_mutex_unlock(&tp.mutex); + for (int i = 0; i < tp.n_threads - 1; i++) + pthread_join(tp.threads[i], NULL); + tp.shutdown = 0; + tp.generation = 0; + } + + tp.n_threads = n; + if (n <= 1) + return; + + for (int i = 0; i < n - 1; i++) { + tp.tids[i] = i + 1; + pthread_create(&tp.threads[i], NULL, worker_loop, &tp.tids[i]); + } +} + +#ifndef __APPLE__ +/* CPU quota available to this process, in whole cores, or 0 if uncapped. + * Containers such as Docker --cpus, Kubernetes, and RunPod may expose every host + * CPU through sysconf()/cpuset, while CFS quota limits the CPU time the process + * can actually use. + * + * Sizing the BLAS/thread pool to the host CPU count oversubscribes that quota: + * threads use the period budget early, then get throttled for the rest of the + * period, causing large bursty stalls. Example: a 16-thread default against a + * 5.1-core quota ran the 0.6B model about 2x slower than a 5-thread pool. + * + * Floor instead of ceil, so a fully busy pool stays under quota and avoids + * throttling. + */ +static int cgroup_cpu_quota(void) { + /* cgroup v2: "/sys/fs/cgroup/cpu.max" is "<quota> <period>", or + * "max <period>" when unlimited. */ + FILE *f = fopen("/sys/fs/cgroup/cpu.max", "r"); + if (f) { + char quota[32]; + long period = 0; + int got = fscanf(f, "%31s %ld", quota, &period); + fclose(f); + if (got == 2 && period > 0 && strcmp(quota, "max") != 0) { + long q = atol(quota); + if (q > 0) + return q / period < 1 ? 1 : (int)(q / period); + } + } + /* cgroup v1: separate files; quota -1 means unlimited. */ + long q = -1, period = 0; + f = fopen("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r"); + if (f) { + if (fscanf(f, "%ld", &q) != 1) + q = -1; + fclose(f); + } + f = fopen("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "r"); + if (f) { + if (fscanf(f, "%ld", &period) != 1) + period = 0; + fclose(f); + } + if (q > 0 && period > 0) + return q / period < 1 ? 1 : (int)(q / period); + return 0; +} +#endif + +int tp_get_num_cpus(void) { +#ifdef __APPLE__ + int n = 0; + size_t len = sizeof(n); + sysctlbyname("hw.ncpu", &n, &len, NULL, 0); + return n > 0 ? n : 1; +#else + int online = (int)sysconf(_SC_NPROCESSORS_ONLN); + if (online < 1) + online = 1; +# ifdef __linux__ + cpu_set_t set; + if (sched_getaffinity(0, sizeof(set), &set) == 0) { + int affinity = CPU_COUNT(&set); + if (affinity >= 1 && affinity < online) + online = affinity; + } +# endif + /* Honor a cgroup CPU quota when it is tighter than the visible core count, + * so the default thread pool does not oversubscribe a CPU-limited + * container and get throttled. */ + int quota = cgroup_cpu_quota(); + if (quota >= 1 && quota < online) + return quota; + return online; +#endif +} + +int tp_num_threads(void) { return tp.n_threads; } + +/* Dispatch work to all threads; main thread is tid=0 */ +void parallel_for(parallel_fn_t fn, void *arg) { + if (tp.n_threads <= 1) { + fn(0, 1, arg); + return; + } + + pthread_mutex_lock(&tp.mutex); + tp.fn = fn; + tp.arg = arg; + tp.n_done = 0; + tp.generation++; + pthread_cond_broadcast(&tp.cond_work); + pthread_mutex_unlock(&tp.mutex); + + fn(0, tp.n_threads, arg); + + pthread_mutex_lock(&tp.mutex); + while (tp.n_done < tp.n_threads - 1) + pthread_cond_wait(&tp.cond_done, &tp.mutex); + pthread_mutex_unlock(&tp.mutex); +} diff --git a/libffwd/threadpool.h b/libffwd/threadpool.h new file mode 100644 index 0000000..f9739a1 --- /dev/null +++ b/libffwd/threadpool.h @@ -0,0 +1,30 @@ +#ifndef TP_THREADPOOL_H +#define TP_THREADPOOL_H + +/* Persistent worker pool for CPU kernels. + * Uses a fixed thread set that sleeps until parallel_for() gives it a callback. + * Also provides the cgroup-quota-aware CPU-count helper used to size the pool. + * + * Kept separate from the kernels so they depend only on parallel_for and + * tp_num_threads, not pool internals. + * + * kernels.h includes this, so kernel callers need no extra include. */ + +/* Worker callback: (thread index, total threads, user arg). */ +typedef void (*parallel_fn_t)(int tid, int n_threads, void *arg); + +/* Set the persistent pool size (default 1). Creates/rebuilds the pool; call + * before inference. */ +void tp_set_threads(int n); + +/* Available CPU cores, capped to a cgroup CPU quota when one is tighter. */ +int tp_get_num_cpus(void); + +/* Current pool size (1 == serial). Kernels gate parallelism on this. */ +int tp_num_threads(void); + +/* Run fn across the pool; the calling thread participates as tid 0. Blocks + * until every worker has finished. */ +void parallel_for(parallel_fn_t fn, void *arg); + +#endif /* TP_THREADPOOL_H */ diff --git a/libffwd/tokenize.c b/libffwd/tokenize.c new file mode 100644 index 0000000..2af151a --- /dev/null +++ b/libffwd/tokenize.c @@ -0,0 +1,349 @@ +/* + * Model-aware tokenization: text -> token ids the model expects. + * + * Backend-independent, so this uses its own handle, ffwd_tok_t, instead of the + * per-backend ffwd_t. + * + * ffwd_tok_open selects the tokenizer from the files present and resolves the + * model's special-token ids. + * + * ffwd_tokenize applies the model's token layout: BERT/XLM-R [CLS]..[SEP], or + * Qwen appended terminal token. + */ + +#include "ffwd.h" + +#include "bpe.h" +#include "sentencepiece.h" +#include "wordpiece.h" + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +typedef enum { TOK_KIND_BPE, TOK_KIND_WP, TOK_KIND_SPM } tok_kind_t; + +struct ffwd_tok { + tok_kind_t kind; + /* exactly one trio is populated, by kind */ + tok_bpe_t *bpe; + tok_bpe_workspace_t *bpe_ws; + tok_wp_t *wp; + tok_wp_workspace_t *wp_ws; + tok_spm_t *spm; + tok_spm_workspace_t *spm_ws; + + int cls_id; /* [CLS] / <s> — wp/spm wrap with these */ + int sep_id; /* [SEP] / </s> */ + int context_separator_id; + int terminal_id; /* appended on the bpe path when append_terminal */ + int append_terminal; /* Qwen3-Embedding pools the last token; see note */ + + int is_late; + int late_mask_id; + int late_query_prefix_id; + int late_document_prefix_id; + int late_skip_ids[64]; + int n_late_skip_ids; +}; + +static void set_err(char *err, size_t errlen, const char *msg) { + if (err && errlen) + snprintf(err, errlen, "%s", msg); +} + +ffwd_tok_t *ffwd_tok_open(const char *model_dir, int is_late, char *err, size_t errlen) { + if (!model_dir) { + set_err(err, errlen, "model_dir is NULL"); + return NULL; + } + ffwd_tok_t *t = calloc(1, sizeof(*t)); + if (!t) { + set_err(err, errlen, "out of memory"); + return NULL; + } + t->cls_id = -1; + t->sep_id = -1; + t->terminal_id = -1; + t->is_late = is_late; + + char path[1024]; + + /* WordPiece (BERT family): vocab.txt. */ + snprintf(path, sizeof(path), "%s/vocab.txt", model_dir); + if (access(path, R_OK) == 0) { + t->kind = TOK_KIND_WP; + t->wp = tok_wp_load(model_dir); + if (!t->wp) { + set_err(err, errlen, "failed to load WordPiece tokenizer"); + goto fail; + } + t->wp_ws = tok_wp_workspace_new(); + if (!t->wp_ws) { + set_err(err, errlen, "out of memory"); + goto fail; + } + t->cls_id = tok_wp_token_id(t->wp, "[CLS]"); + t->sep_id = tok_wp_token_id(t->wp, "[SEP]"); + if (t->cls_id < 0 || t->sep_id < 0) { + set_err(err, errlen, "WordPiece vocab missing [CLS]/[SEP]"); + goto fail; + } + return t; + } + + /* SentencePiece (XLM-R family): sentencepiece.bpe.model / tokenizer.model / + * spiece.model. */ + snprintf(path, sizeof(path), "%s/sentencepiece.bpe.model", model_dir); + if (access(path, R_OK) != 0) { + snprintf(path, sizeof(path), "%s/tokenizer.model", model_dir); + if (access(path, R_OK) != 0) + snprintf(path, sizeof(path), "%s/spiece.model", model_dir); + } + if (access(path, R_OK) == 0) { + t->kind = TOK_KIND_SPM; + t->spm = tok_spm_load(model_dir); + if (!t->spm) { + set_err(err, errlen, "failed to load SentencePiece tokenizer"); + goto fail; + } + t->spm_ws = tok_spm_workspace_new(); + if (!t->spm_ws) { + set_err(err, errlen, "out of memory"); + goto fail; + } + t->cls_id = tok_spm_token_id(t->spm, "<s>"); + t->sep_id = tok_spm_token_id(t->spm, "</s>"); + if (t->cls_id < 0 || t->sep_id < 0) { + set_err(err, errlen, "SentencePiece vocab missing <s>/</s>"); + goto fail; + } + return t; + } + + /* Byte-level BPE (Qwen family): vocab.json. */ + snprintf(path, sizeof(path), "%s/vocab.json", model_dir); + t->kind = TOK_KIND_BPE; + t->bpe = tok_bpe_load(path); + if (!t->bpe) { + set_err(err, errlen, "failed to load tokenizer"); + goto fail; + } + t->bpe_ws = tok_bpe_workspace_new(); + if (!t->bpe_ws) { + set_err(err, errlen, "out of memory"); + goto fail; + } + /* The contextual chunk separator is the tokenizer's <|endoftext|>; released + * models keep it out of vocab.json, so the family constant is the fallback. + * Qwen3-Embedding pools the last token (the same <|endoftext|>), so the bpe + * path appends it. A config that disables append_terminal_token would need + * to override this; the released models all enable it. */ + int sep = tok_bpe_token_id(t->bpe, "<|endoftext|>"); + t->context_separator_id = sep >= 0 ? sep : FFWD_CONTEXT_SEPARATOR_TOKEN_ID; + t->terminal_id = t->context_separator_id; + /* Off until the caller sets it from config->append_terminal_token; it is + * model-specific (pplx-embed does not append, for example). */ + t->append_terminal = 0; + + if (is_late) { + int id = tok_bpe_token_id(t->bpe, "[MASK]"); + t->late_mask_id = id >= 0 ? id : FFWD_LATE_MASK_TOKEN_ID; + id = tok_bpe_token_id(t->bpe, "[Q]"); + t->late_query_prefix_id = id >= 0 ? id : FFWD_LATE_QUERY_PREFIX_ID; + id = tok_bpe_token_id(t->bpe, "[D]"); + t->late_document_prefix_id = id >= 0 ? id : FFWD_LATE_DOCUMENT_PREFIX_ID; + + const char *punct = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; + for (const char *p = punct; + *p && t->n_late_skip_ids < (int)(sizeof(t->late_skip_ids) / sizeof(t->late_skip_ids[0])); + p++) { + char text[2] = {*p, '\0'}; + int n_ids = 0; + int *ids = tok_bpe_encode(t->bpe, text, &n_ids); + if (ids && n_ids == 1) + t->late_skip_ids[t->n_late_skip_ids++] = ids[0]; + free(ids); + } + } + return t; + +fail: + ffwd_tok_free(t); + return NULL; +} + +void ffwd_tok_free(ffwd_tok_t *t) { + if (!t) + return; + if (t->bpe_ws) + tok_bpe_workspace_free(t->bpe_ws); + if (t->bpe) + tok_bpe_free(t->bpe); + if (t->wp_ws) + tok_wp_workspace_free(t->wp_ws); + if (t->wp) + tok_wp_free(t->wp); + if (t->spm_ws) + tok_spm_workspace_free(t->spm_ws); + if (t->spm) + tok_spm_free(t->spm); + free(t); +} + +int ffwd_tok_context_separator_id(const ffwd_tok_t *t) { return t ? t->context_separator_id : -1; } + +void ffwd_tok_set_append_terminal(ffwd_tok_t *t, int on) { + if (t) + t->append_terminal = on ? 1 : 0; +} + +/* Encode text and apply the model's special-token layout. Returns malloc'd ids + * and sets *n_out; NULL on error. */ +static int *tokenize_core(ffwd_tok_t *t, const char *text, int *n_out) { + *n_out = 0; + + if (t->kind == TOK_KIND_WP || t->kind == TOK_KIND_SPM) { + int n = 0; + int *core = (t->kind == TOK_KIND_WP) + ? tok_wp_encode_with_workspace(t->wp, t->wp_ws, text, &n) + : tok_spm_encode_with_workspace(t->spm, t->spm_ws, text, &n); + /* Wrap [CLS] core [SEP]. A whitespace-only input cleans to zero core + * tokens (core == NULL, n == 0); the bare [CLS][SEP] pair is what + * Hugging Face produces for it too. Grow in place so only the one + * allocation is touched. */ + if (n < 0 || n > INT_MAX - 2) { + free(core); + return NULL; + } + int *ids = realloc(core, (size_t)(n + 2) * sizeof(*ids)); + if (!ids) { + free(core); + return NULL; + } + memmove(ids + 1, ids, (size_t)n * sizeof(*ids)); + ids[0] = t->cls_id; + ids[n + 1] = t->sep_id; + *n_out = n + 2; + return ids; + } + + /* BPE. */ + int n = 0; + int *ids = tok_bpe_encode_with_workspace(t->bpe, t->bpe_ws, text, &n); + if (!ids || n <= 0) { + free(ids); + return NULL; + } + if (t->append_terminal) { + if (n == INT_MAX) { + free(ids); + return NULL; + } + int *grown = realloc(ids, (size_t)(n + 1) * sizeof(*ids)); + if (!grown) { + free(ids); + return NULL; + } + ids = grown; + ids[n++] = t->terminal_id; + } + *n_out = n; + return ids; +} + +int *ffwd_tokenize(ffwd_tok_t *t, const char *text, const char *query_instruct, int *n_out) { + if (n_out) + *n_out = 0; + if (!t || !text) + return NULL; + + int n = 0; + int *ids; + if (!query_instruct) { + ids = tokenize_core(t, text, &n); + } else { + /* Tokenize the instruction and the input in one pass. */ + size_t plen = strlen(query_instruct); + size_t tlen = strlen(text); + char *buf = malloc(plen + tlen + 1); + if (!buf) + return NULL; + memcpy(buf, query_instruct, plen); + memcpy(buf + plen, text, tlen + 1); + ids = tokenize_core(t, buf, &n); + free(buf); + } + if (!ids) + return NULL; + if (n_out) + *n_out = n; + return ids; +} + +static int late_id_is_skipped(const ffwd_tok_t *t, int id) { + for (int i = 0; i < t->n_late_skip_ids; i++) { + if (t->late_skip_ids[i] == id) + return 1; + } + return 0; +} + +int ffwd_tokenize_late(ffwd_tok_t *t, const char *text, int is_query, ffwd_late_tokens_t *out) { + memset(out, 0, sizeof(*out)); + if (!t || !text) + return -1; + + int raw_n = 0; + int *raw = tokenize_core(t, text, &raw_n); + if (!raw) + return -1; + + int target = raw_n + 1; + if (is_query && target < FFWD_LATE_QUERY_TOKENS) + target = FFWD_LATE_QUERY_TOKENS; + + out->ids = malloc((size_t)target * sizeof(*out->ids)); + if (!out->ids) { + free(raw); + return -1; + } + out->ids[0] = raw[0]; + out->ids[1] = is_query ? t->late_query_prefix_id : t->late_document_prefix_id; + if (raw_n > 1) + memcpy(out->ids + 2, raw + 1, (size_t)(raw_n - 1) * sizeof(*out->ids)); + out->n_tokens = raw_n + 1; + free(raw); + + if (is_query) { + while (out->n_tokens < FFWD_LATE_QUERY_TOKENS) + out->ids[out->n_tokens++] = t->late_mask_id; + out->n_keep = out->n_tokens; + } else { + out->keep = malloc((size_t)out->n_tokens * sizeof(*out->keep)); + if (!out->keep) { + ffwd_late_tokens_free(out); + return -1; + } + for (int i = 0; i < out->n_tokens; i++) { + if (!late_id_is_skipped(t, out->ids[i])) + out->keep[out->n_keep++] = i; + } + } + + if (out->n_keep <= 0) { + ffwd_late_tokens_free(out); + return -1; + } + return 0; +} + +void ffwd_late_tokens_free(ffwd_late_tokens_t *out) { + if (!out) + return; + free(out->ids); + free(out->keep); + memset(out, 0, sizeof(*out)); +} diff --git a/libffwd/unicode.h b/libffwd/unicode.h new file mode 100644 index 0000000..bce8967 --- /dev/null +++ b/libffwd/unicode.h @@ -0,0 +1,4992 @@ +/* Generated by tools/gen_wordpiece_unicode.py from Python unicodedata + * (Unicode 13.0.0). Do not edit by hand. + * + * Tables for the BasicTokenizer, matching transformers' BertTokenizer. + * + * WP_PUNCT_RANGES / WP_CONTROL_RANGES: _is_punctuation and _is_control as sorted + * [lo, hi] ranges. Punctuation is the ASCII set (33-47/58-64/91-96/123-126) plus + * every category P* codepoint. Control is categories Cc, Cf, Co minus + * tab/newline/carriage-return (those route to whitespace). Unassigned (Cn) is + * intentionally not control: it never occurs in real text and would bind the + * table to one Unicode version. + * + * WP_FOLD: the do_lower_case fold (Python str.lower then NFD then drop Mn marks), + * sorted by codepoint. n is the output length (0 drops a combining mark; up to + * 3). Hangul syllables (U+AC00..U+D7A3) decompose by formula on the C side and + * are excluded here. */ +#ifndef WORDPIECE_UNICODE_H +#define WORDPIECE_UNICODE_H + +#include <stdint.h> + +typedef struct { + uint32_t lo; + uint32_t hi; +} wp_range_t; + +typedef struct { + uint32_t cp; + uint32_t out[3]; + uint8_t n; +} wp_fold_t; + +static const wp_range_t WP_PUNCT_RANGES[] = { + {0x21, 0x2F}, {0x3A, 0x40}, {0x5B, 0x60}, {0x7B, 0x7E}, + {0xA1, 0xA1}, {0xA7, 0xA7}, {0xAB, 0xAB}, {0xB6, 0xB7}, + {0xBB, 0xBB}, {0xBF, 0xBF}, {0x37E, 0x37E}, {0x387, 0x387}, + {0x55A, 0x55F}, {0x589, 0x58A}, {0x5BE, 0x5BE}, {0x5C0, 0x5C0}, + {0x5C3, 0x5C3}, {0x5C6, 0x5C6}, {0x5F3, 0x5F4}, {0x609, 0x60A}, + {0x60C, 0x60D}, {0x61B, 0x61B}, {0x61E, 0x61F}, {0x66A, 0x66D}, + {0x6D4, 0x6D4}, {0x700, 0x70D}, {0x7F7, 0x7F9}, {0x830, 0x83E}, + {0x85E, 0x85E}, {0x964, 0x965}, {0x970, 0x970}, {0x9FD, 0x9FD}, + {0xA76, 0xA76}, {0xAF0, 0xAF0}, {0xC77, 0xC77}, {0xC84, 0xC84}, + {0xDF4, 0xDF4}, {0xE4F, 0xE4F}, {0xE5A, 0xE5B}, {0xF04, 0xF12}, + {0xF14, 0xF14}, {0xF3A, 0xF3D}, {0xF85, 0xF85}, {0xFD0, 0xFD4}, + {0xFD9, 0xFDA}, {0x104A, 0x104F}, {0x10FB, 0x10FB}, {0x1360, 0x1368}, + {0x1400, 0x1400}, {0x166E, 0x166E}, {0x169B, 0x169C}, {0x16EB, 0x16ED}, + {0x1735, 0x1736}, {0x17D4, 0x17D6}, {0x17D8, 0x17DA}, {0x1800, 0x180A}, + {0x1944, 0x1945}, {0x1A1E, 0x1A1F}, {0x1AA0, 0x1AA6}, {0x1AA8, 0x1AAD}, + {0x1B5A, 0x1B60}, {0x1BFC, 0x1BFF}, {0x1C3B, 0x1C3F}, {0x1C7E, 0x1C7F}, + {0x1CC0, 0x1CC7}, {0x1CD3, 0x1CD3}, {0x2010, 0x2027}, {0x2030, 0x2043}, + {0x2045, 0x2051}, {0x2053, 0x205E}, {0x207D, 0x207E}, {0x208D, 0x208E}, + {0x2308, 0x230B}, {0x2329, 0x232A}, {0x2768, 0x2775}, {0x27C5, 0x27C6}, + {0x27E6, 0x27EF}, {0x2983, 0x2998}, {0x29D8, 0x29DB}, {0x29FC, 0x29FD}, + {0x2CF9, 0x2CFC}, {0x2CFE, 0x2CFF}, {0x2D70, 0x2D70}, {0x2E00, 0x2E2E}, + {0x2E30, 0x2E4F}, {0x2E52, 0x2E52}, {0x3001, 0x3003}, {0x3008, 0x3011}, + {0x3014, 0x301F}, {0x3030, 0x3030}, {0x303D, 0x303D}, {0x30A0, 0x30A0}, + {0x30FB, 0x30FB}, {0xA4FE, 0xA4FF}, {0xA60D, 0xA60F}, {0xA673, 0xA673}, + {0xA67E, 0xA67E}, {0xA6F2, 0xA6F7}, {0xA874, 0xA877}, {0xA8CE, 0xA8CF}, + {0xA8F8, 0xA8FA}, {0xA8FC, 0xA8FC}, {0xA92E, 0xA92F}, {0xA95F, 0xA95F}, + {0xA9C1, 0xA9CD}, {0xA9DE, 0xA9DF}, {0xAA5C, 0xAA5F}, {0xAADE, 0xAADF}, + {0xAAF0, 0xAAF1}, {0xABEB, 0xABEB}, {0xFD3E, 0xFD3F}, {0xFE10, 0xFE19}, + {0xFE30, 0xFE52}, {0xFE54, 0xFE61}, {0xFE63, 0xFE63}, {0xFE68, 0xFE68}, + {0xFE6A, 0xFE6B}, {0xFF01, 0xFF03}, {0xFF05, 0xFF0A}, {0xFF0C, 0xFF0F}, + {0xFF1A, 0xFF1B}, {0xFF1F, 0xFF20}, {0xFF3B, 0xFF3D}, {0xFF3F, 0xFF3F}, + {0xFF5B, 0xFF5B}, {0xFF5D, 0xFF5D}, {0xFF5F, 0xFF65}, {0x10100, 0x10102}, + {0x1039F, 0x1039F}, {0x103D0, 0x103D0}, {0x1056F, 0x1056F}, {0x10857, 0x10857}, + {0x1091F, 0x1091F}, {0x1093F, 0x1093F}, {0x10A50, 0x10A58}, {0x10A7F, 0x10A7F}, + {0x10AF0, 0x10AF6}, {0x10B39, 0x10B3F}, {0x10B99, 0x10B9C}, {0x10EAD, 0x10EAD}, + {0x10F55, 0x10F59}, {0x11047, 0x1104D}, {0x110BB, 0x110BC}, {0x110BE, 0x110C1}, + {0x11140, 0x11143}, {0x11174, 0x11175}, {0x111C5, 0x111C8}, {0x111CD, 0x111CD}, + {0x111DB, 0x111DB}, {0x111DD, 0x111DF}, {0x11238, 0x1123D}, {0x112A9, 0x112A9}, + {0x1144B, 0x1144F}, {0x1145A, 0x1145B}, {0x1145D, 0x1145D}, {0x114C6, 0x114C6}, + {0x115C1, 0x115D7}, {0x11641, 0x11643}, {0x11660, 0x1166C}, {0x1173C, 0x1173E}, + {0x1183B, 0x1183B}, {0x11944, 0x11946}, {0x119E2, 0x119E2}, {0x11A3F, 0x11A46}, + {0x11A9A, 0x11A9C}, {0x11A9E, 0x11AA2}, {0x11C41, 0x11C45}, {0x11C70, 0x11C71}, + {0x11EF7, 0x11EF8}, {0x11FFF, 0x11FFF}, {0x12470, 0x12474}, {0x16A6E, 0x16A6F}, + {0x16AF5, 0x16AF5}, {0x16B37, 0x16B3B}, {0x16B44, 0x16B44}, {0x16E97, 0x16E9A}, + {0x16FE2, 0x16FE2}, {0x1BC9F, 0x1BC9F}, {0x1DA87, 0x1DA8B}, {0x1E95E, 0x1E95F}, +}; +enum { WP_PUNCT_RANGES_N = (int)(sizeof(WP_PUNCT_RANGES) / sizeof(WP_PUNCT_RANGES[0])) }; + +static const wp_range_t WP_CONTROL_RANGES[] = { + {0x0, 0x8}, {0xB, 0xC}, {0xE, 0x1F}, {0x7F, 0x9F}, + {0xAD, 0xAD}, {0x600, 0x605}, {0x61C, 0x61C}, {0x6DD, 0x6DD}, + {0x70F, 0x70F}, {0x8E2, 0x8E2}, {0x180E, 0x180E}, {0x200B, 0x200F}, + {0x202A, 0x202E}, {0x2060, 0x2064}, {0x2066, 0x206F}, {0xE000, 0xF8FF}, + {0xFEFF, 0xFEFF}, {0xFFF9, 0xFFFB}, {0x110BD, 0x110BD}, {0x110CD, 0x110CD}, + {0x13430, 0x13438}, {0x1BCA0, 0x1BCA3}, {0x1D173, 0x1D17A}, {0xE0001, 0xE0001}, + {0xE0020, 0xE007F}, {0xF0000, 0xFFFFD}, {0x100000, 0x10FFFD}, +}; +enum { WP_CONTROL_RANGES_N = (int)(sizeof(WP_CONTROL_RANGES) / sizeof(WP_CONTROL_RANGES[0])) }; + +static const wp_fold_t WP_FOLD[] = { + {0x41, {0x61, 0x0, 0x0}, 1}, + {0x42, {0x62, 0x0, 0x0}, 1}, + {0x43, {0x63, 0x0, 0x0}, 1}, + {0x44, {0x64, 0x0, 0x0}, 1}, + {0x45, {0x65, 0x0, 0x0}, 1}, + {0x46, {0x66, 0x0, 0x0}, 1}, + {0x47, {0x67, 0x0, 0x0}, 1}, + {0x48, {0x68, 0x0, 0x0}, 1}, + {0x49, {0x69, 0x0, 0x0}, 1}, + {0x4A, {0x6A, 0x0, 0x0}, 1}, + {0x4B, {0x6B, 0x0, 0x0}, 1}, + {0x4C, {0x6C, 0x0, 0x0}, 1}, + {0x4D, {0x6D, 0x0, 0x0}, 1}, + {0x4E, {0x6E, 0x0, 0x0}, 1}, + {0x4F, {0x6F, 0x0, 0x0}, 1}, + {0x50, {0x70, 0x0, 0x0}, 1}, + {0x51, {0x71, 0x0, 0x0}, 1}, + {0x52, {0x72, 0x0, 0x0}, 1}, + {0x53, {0x73, 0x0, 0x0}, 1}, + {0x54, {0x74, 0x0, 0x0}, 1}, + {0x55, {0x75, 0x0, 0x0}, 1}, + {0x56, {0x76, 0x0, 0x0}, 1}, + {0x57, {0x77, 0x0, 0x0}, 1}, + {0x58, {0x78, 0x0, 0x0}, 1}, + {0x59, {0x79, 0x0, 0x0}, 1}, + {0x5A, {0x7A, 0x0, 0x0}, 1}, + {0xC0, {0x61, 0x0, 0x0}, 1}, + {0xC1, {0x61, 0x0, 0x0}, 1}, + {0xC2, {0x61, 0x0, 0x0}, 1}, + {0xC3, {0x61, 0x0, 0x0}, 1}, + {0xC4, {0x61, 0x0, 0x0}, 1}, + {0xC5, {0x61, 0x0, 0x0}, 1}, + {0xC6, {0xE6, 0x0, 0x0}, 1}, + {0xC7, {0x63, 0x0, 0x0}, 1}, + {0xC8, {0x65, 0x0, 0x0}, 1}, + {0xC9, {0x65, 0x0, 0x0}, 1}, + {0xCA, {0x65, 0x0, 0x0}, 1}, + {0xCB, {0x65, 0x0, 0x0}, 1}, + {0xCC, {0x69, 0x0, 0x0}, 1}, + {0xCD, {0x69, 0x0, 0x0}, 1}, + {0xCE, {0x69, 0x0, 0x0}, 1}, + {0xCF, {0x69, 0x0, 0x0}, 1}, + {0xD0, {0xF0, 0x0, 0x0}, 1}, + {0xD1, {0x6E, 0x0, 0x0}, 1}, + {0xD2, {0x6F, 0x0, 0x0}, 1}, + {0xD3, {0x6F, 0x0, 0x0}, 1}, + {0xD4, {0x6F, 0x0, 0x0}, 1}, + {0xD5, {0x6F, 0x0, 0x0}, 1}, + {0xD6, {0x6F, 0x0, 0x0}, 1}, + {0xD8, {0xF8, 0x0, 0x0}, 1}, + {0xD9, {0x75, 0x0, 0x0}, 1}, + {0xDA, {0x75, 0x0, 0x0}, 1}, + {0xDB, {0x75, 0x0, 0x0}, 1}, + {0xDC, {0x75, 0x0, 0x0}, 1}, + {0xDD, {0x79, 0x0, 0x0}, 1}, + {0xDE, {0xFE, 0x0, 0x0}, 1}, + {0xE0, {0x61, 0x0, 0x0}, 1}, + {0xE1, {0x61, 0x0, 0x0}, 1}, + {0xE2, {0x61, 0x0, 0x0}, 1}, + {0xE3, {0x61, 0x0, 0x0}, 1}, + {0xE4, {0x61, 0x0, 0x0}, 1}, + {0xE5, {0x61, 0x0, 0x0}, 1}, + {0xE7, {0x63, 0x0, 0x0}, 1}, + {0xE8, {0x65, 0x0, 0x0}, 1}, + {0xE9, {0x65, 0x0, 0x0}, 1}, + {0xEA, {0x65, 0x0, 0x0}, 1}, + {0xEB, {0x65, 0x0, 0x0}, 1}, + {0xEC, {0x69, 0x0, 0x0}, 1}, + {0xED, {0x69, 0x0, 0x0}, 1}, + {0xEE, {0x69, 0x0, 0x0}, 1}, + {0xEF, {0x69, 0x0, 0x0}, 1}, + {0xF1, {0x6E, 0x0, 0x0}, 1}, + {0xF2, {0x6F, 0x0, 0x0}, 1}, + {0xF3, {0x6F, 0x0, 0x0}, 1}, + {0xF4, {0x6F, 0x0, 0x0}, 1}, + {0xF5, {0x6F, 0x0, 0x0}, 1}, + {0xF6, {0x6F, 0x0, 0x0}, 1}, + {0xF9, {0x75, 0x0, 0x0}, 1}, + {0xFA, {0x75, 0x0, 0x0}, 1}, + {0xFB, {0x75, 0x0, 0x0}, 1}, + {0xFC, {0x75, 0x0, 0x0}, 1}, + {0xFD, {0x79, 0x0, 0x0}, 1}, + {0xFF, {0x79, 0x0, 0x0}, 1}, + {0x100, {0x61, 0x0, 0x0}, 1}, + {0x101, {0x61, 0x0, 0x0}, 1}, + {0x102, {0x61, 0x0, 0x0}, 1}, + {0x103, {0x61, 0x0, 0x0}, 1}, + {0x104, {0x61, 0x0, 0x0}, 1}, + {0x105, {0x61, 0x0, 0x0}, 1}, + {0x106, {0x63, 0x0, 0x0}, 1}, + {0x107, {0x63, 0x0, 0x0}, 1}, + {0x108, {0x63, 0x0, 0x0}, 1}, + {0x109, {0x63, 0x0, 0x0}, 1}, + {0x10A, {0x63, 0x0, 0x0}, 1}, + {0x10B, {0x63, 0x0, 0x0}, 1}, + {0x10C, {0x63, 0x0, 0x0}, 1}, + {0x10D, {0x63, 0x0, 0x0}, 1}, + {0x10E, {0x64, 0x0, 0x0}, 1}, + {0x10F, {0x64, 0x0, 0x0}, 1}, + {0x110, {0x111, 0x0, 0x0}, 1}, + {0x112, {0x65, 0x0, 0x0}, 1}, + {0x113, {0x65, 0x0, 0x0}, 1}, + {0x114, {0x65, 0x0, 0x0}, 1}, + {0x115, {0x65, 0x0, 0x0}, 1}, + {0x116, {0x65, 0x0, 0x0}, 1}, + {0x117, {0x65, 0x0, 0x0}, 1}, + {0x118, {0x65, 0x0, 0x0}, 1}, + {0x119, {0x65, 0x0, 0x0}, 1}, + {0x11A, {0x65, 0x0, 0x0}, 1}, + {0x11B, {0x65, 0x0, 0x0}, 1}, + {0x11C, {0x67, 0x0, 0x0}, 1}, + {0x11D, {0x67, 0x0, 0x0}, 1}, + {0x11E, {0x67, 0x0, 0x0}, 1}, + {0x11F, {0x67, 0x0, 0x0}, 1}, + {0x120, {0x67, 0x0, 0x0}, 1}, + {0x121, {0x67, 0x0, 0x0}, 1}, + {0x122, {0x67, 0x0, 0x0}, 1}, + {0x123, {0x67, 0x0, 0x0}, 1}, + {0x124, {0x68, 0x0, 0x0}, 1}, + {0x125, {0x68, 0x0, 0x0}, 1}, + {0x126, {0x127, 0x0, 0x0}, 1}, + {0x128, {0x69, 0x0, 0x0}, 1}, + {0x129, {0x69, 0x0, 0x0}, 1}, + {0x12A, {0x69, 0x0, 0x0}, 1}, + {0x12B, {0x69, 0x0, 0x0}, 1}, + {0x12C, {0x69, 0x0, 0x0}, 1}, + {0x12D, {0x69, 0x0, 0x0}, 1}, + {0x12E, {0x69, 0x0, 0x0}, 1}, + {0x12F, {0x69, 0x0, 0x0}, 1}, + {0x130, {0x69, 0x0, 0x0}, 1}, + {0x132, {0x133, 0x0, 0x0}, 1}, + {0x134, {0x6A, 0x0, 0x0}, 1}, + {0x135, {0x6A, 0x0, 0x0}, 1}, + {0x136, {0x6B, 0x0, 0x0}, 1}, + {0x137, {0x6B, 0x0, 0x0}, 1}, + {0x139, {0x6C, 0x0, 0x0}, 1}, + {0x13A, {0x6C, 0x0, 0x0}, 1}, + {0x13B, {0x6C, 0x0, 0x0}, 1}, + {0x13C, {0x6C, 0x0, 0x0}, 1}, + {0x13D, {0x6C, 0x0, 0x0}, 1}, + {0x13E, {0x6C, 0x0, 0x0}, 1}, + {0x13F, {0x140, 0x0, 0x0}, 1}, + {0x141, {0x142, 0x0, 0x0}, 1}, + {0x143, {0x6E, 0x0, 0x0}, 1}, + {0x144, {0x6E, 0x0, 0x0}, 1}, + {0x145, {0x6E, 0x0, 0x0}, 1}, + {0x146, {0x6E, 0x0, 0x0}, 1}, + {0x147, {0x6E, 0x0, 0x0}, 1}, + {0x148, {0x6E, 0x0, 0x0}, 1}, + {0x14A, {0x14B, 0x0, 0x0}, 1}, + {0x14C, {0x6F, 0x0, 0x0}, 1}, + {0x14D, {0x6F, 0x0, 0x0}, 1}, + {0x14E, {0x6F, 0x0, 0x0}, 1}, + {0x14F, {0x6F, 0x0, 0x0}, 1}, + {0x150, {0x6F, 0x0, 0x0}, 1}, + {0x151, {0x6F, 0x0, 0x0}, 1}, + {0x152, {0x153, 0x0, 0x0}, 1}, + {0x154, {0x72, 0x0, 0x0}, 1}, + {0x155, {0x72, 0x0, 0x0}, 1}, + {0x156, {0x72, 0x0, 0x0}, 1}, + {0x157, {0x72, 0x0, 0x0}, 1}, + {0x158, {0x72, 0x0, 0x0}, 1}, + {0x159, {0x72, 0x0, 0x0}, 1}, + {0x15A, {0x73, 0x0, 0x0}, 1}, + {0x15B, {0x73, 0x0, 0x0}, 1}, + {0x15C, {0x73, 0x0, 0x0}, 1}, + {0x15D, {0x73, 0x0, 0x0}, 1}, + {0x15E, {0x73, 0x0, 0x0}, 1}, + {0x15F, {0x73, 0x0, 0x0}, 1}, + {0x160, {0x73, 0x0, 0x0}, 1}, + {0x161, {0x73, 0x0, 0x0}, 1}, + {0x162, {0x74, 0x0, 0x0}, 1}, + {0x163, {0x74, 0x0, 0x0}, 1}, + {0x164, {0x74, 0x0, 0x0}, 1}, + {0x165, {0x74, 0x0, 0x0}, 1}, + {0x166, {0x167, 0x0, 0x0}, 1}, + {0x168, {0x75, 0x0, 0x0}, 1}, + {0x169, {0x75, 0x0, 0x0}, 1}, + {0x16A, {0x75, 0x0, 0x0}, 1}, + {0x16B, {0x75, 0x0, 0x0}, 1}, + {0x16C, {0x75, 0x0, 0x0}, 1}, + {0x16D, {0x75, 0x0, 0x0}, 1}, + {0x16E, {0x75, 0x0, 0x0}, 1}, + {0x16F, {0x75, 0x0, 0x0}, 1}, + {0x170, {0x75, 0x0, 0x0}, 1}, + {0x171, {0x75, 0x0, 0x0}, 1}, + {0x172, {0x75, 0x0, 0x0}, 1}, + {0x173, {0x75, 0x0, 0x0}, 1}, + {0x174, {0x77, 0x0, 0x0}, 1}, + {0x175, {0x77, 0x0, 0x0}, 1}, + {0x176, {0x79, 0x0, 0x0}, 1}, + {0x177, {0x79, 0x0, 0x0}, 1}, + {0x178, {0x79, 0x0, 0x0}, 1}, + {0x179, {0x7A, 0x0, 0x0}, 1}, + {0x17A, {0x7A, 0x0, 0x0}, 1}, + {0x17B, {0x7A, 0x0, 0x0}, 1}, + {0x17C, {0x7A, 0x0, 0x0}, 1}, + {0x17D, {0x7A, 0x0, 0x0}, 1}, + {0x17E, {0x7A, 0x0, 0x0}, 1}, + {0x181, {0x253, 0x0, 0x0}, 1}, + {0x182, {0x183, 0x0, 0x0}, 1}, + {0x184, {0x185, 0x0, 0x0}, 1}, + {0x186, {0x254, 0x0, 0x0}, 1}, + {0x187, {0x188, 0x0, 0x0}, 1}, + {0x189, {0x256, 0x0, 0x0}, 1}, + {0x18A, {0x257, 0x0, 0x0}, 1}, + {0x18B, {0x18C, 0x0, 0x0}, 1}, + {0x18E, {0x1DD, 0x0, 0x0}, 1}, + {0x18F, {0x259, 0x0, 0x0}, 1}, + {0x190, {0x25B, 0x0, 0x0}, 1}, + {0x191, {0x192, 0x0, 0x0}, 1}, + {0x193, {0x260, 0x0, 0x0}, 1}, + {0x194, {0x263, 0x0, 0x0}, 1}, + {0x196, {0x269, 0x0, 0x0}, 1}, + {0x197, {0x268, 0x0, 0x0}, 1}, + {0x198, {0x199, 0x0, 0x0}, 1}, + {0x19C, {0x26F, 0x0, 0x0}, 1}, + {0x19D, {0x272, 0x0, 0x0}, 1}, + {0x19F, {0x275, 0x0, 0x0}, 1}, + {0x1A0, {0x6F, 0x0, 0x0}, 1}, + {0x1A1, {0x6F, 0x0, 0x0}, 1}, + {0x1A2, {0x1A3, 0x0, 0x0}, 1}, + {0x1A4, {0x1A5, 0x0, 0x0}, 1}, + {0x1A6, {0x280, 0x0, 0x0}, 1}, + {0x1A7, {0x1A8, 0x0, 0x0}, 1}, + {0x1A9, {0x283, 0x0, 0x0}, 1}, + {0x1AC, {0x1AD, 0x0, 0x0}, 1}, + {0x1AE, {0x288, 0x0, 0x0}, 1}, + {0x1AF, {0x75, 0x0, 0x0}, 1}, + {0x1B0, {0x75, 0x0, 0x0}, 1}, + {0x1B1, {0x28A, 0x0, 0x0}, 1}, + {0x1B2, {0x28B, 0x0, 0x0}, 1}, + {0x1B3, {0x1B4, 0x0, 0x0}, 1}, + {0x1B5, {0x1B6, 0x0, 0x0}, 1}, + {0x1B7, {0x292, 0x0, 0x0}, 1}, + {0x1B8, {0x1B9, 0x0, 0x0}, 1}, + {0x1BC, {0x1BD, 0x0, 0x0}, 1}, + {0x1C4, {0x1C6, 0x0, 0x0}, 1}, + {0x1C5, {0x1C6, 0x0, 0x0}, 1}, + {0x1C7, {0x1C9, 0x0, 0x0}, 1}, + {0x1C8, {0x1C9, 0x0, 0x0}, 1}, + {0x1CA, {0x1CC, 0x0, 0x0}, 1}, + {0x1CB, {0x1CC, 0x0, 0x0}, 1}, + {0x1CD, {0x61, 0x0, 0x0}, 1}, + {0x1CE, {0x61, 0x0, 0x0}, 1}, + {0x1CF, {0x69, 0x0, 0x0}, 1}, + {0x1D0, {0x69, 0x0, 0x0}, 1}, + {0x1D1, {0x6F, 0x0, 0x0}, 1}, + {0x1D2, {0x6F, 0x0, 0x0}, 1}, + {0x1D3, {0x75, 0x0, 0x0}, 1}, + {0x1D4, {0x75, 0x0, 0x0}, 1}, + {0x1D5, {0x75, 0x0, 0x0}, 1}, + {0x1D6, {0x75, 0x0, 0x0}, 1}, + {0x1D7, {0x75, 0x0, 0x0}, 1}, + {0x1D8, {0x75, 0x0, 0x0}, 1}, + {0x1D9, {0x75, 0x0, 0x0}, 1}, + {0x1DA, {0x75, 0x0, 0x0}, 1}, + {0x1DB, {0x75, 0x0, 0x0}, 1}, + {0x1DC, {0x75, 0x0, 0x0}, 1}, + {0x1DE, {0x61, 0x0, 0x0}, 1}, + {0x1DF, {0x61, 0x0, 0x0}, 1}, + {0x1E0, {0x61, 0x0, 0x0}, 1}, + {0x1E1, {0x61, 0x0, 0x0}, 1}, + {0x1E2, {0xE6, 0x0, 0x0}, 1}, + {0x1E3, {0xE6, 0x0, 0x0}, 1}, + {0x1E4, {0x1E5, 0x0, 0x0}, 1}, + {0x1E6, {0x67, 0x0, 0x0}, 1}, + {0x1E7, {0x67, 0x0, 0x0}, 1}, + {0x1E8, {0x6B, 0x0, 0x0}, 1}, + {0x1E9, {0x6B, 0x0, 0x0}, 1}, + {0x1EA, {0x6F, 0x0, 0x0}, 1}, + {0x1EB, {0x6F, 0x0, 0x0}, 1}, + {0x1EC, {0x6F, 0x0, 0x0}, 1}, + {0x1ED, {0x6F, 0x0, 0x0}, 1}, + {0x1EE, {0x292, 0x0, 0x0}, 1}, + {0x1EF, {0x292, 0x0, 0x0}, 1}, + {0x1F0, {0x6A, 0x0, 0x0}, 1}, + {0x1F1, {0x1F3, 0x0, 0x0}, 1}, + {0x1F2, {0x1F3, 0x0, 0x0}, 1}, + {0x1F4, {0x67, 0x0, 0x0}, 1}, + {0x1F5, {0x67, 0x0, 0x0}, 1}, + {0x1F6, {0x195, 0x0, 0x0}, 1}, + {0x1F7, {0x1BF, 0x0, 0x0}, 1}, + {0x1F8, {0x6E, 0x0, 0x0}, 1}, + {0x1F9, {0x6E, 0x0, 0x0}, 1}, + {0x1FA, {0x61, 0x0, 0x0}, 1}, + {0x1FB, {0x61, 0x0, 0x0}, 1}, + {0x1FC, {0xE6, 0x0, 0x0}, 1}, + {0x1FD, {0xE6, 0x0, 0x0}, 1}, + {0x1FE, {0xF8, 0x0, 0x0}, 1}, + {0x1FF, {0xF8, 0x0, 0x0}, 1}, + {0x200, {0x61, 0x0, 0x0}, 1}, + {0x201, {0x61, 0x0, 0x0}, 1}, + {0x202, {0x61, 0x0, 0x0}, 1}, + {0x203, {0x61, 0x0, 0x0}, 1}, + {0x204, {0x65, 0x0, 0x0}, 1}, + {0x205, {0x65, 0x0, 0x0}, 1}, + {0x206, {0x65, 0x0, 0x0}, 1}, + {0x207, {0x65, 0x0, 0x0}, 1}, + {0x208, {0x69, 0x0, 0x0}, 1}, + {0x209, {0x69, 0x0, 0x0}, 1}, + {0x20A, {0x69, 0x0, 0x0}, 1}, + {0x20B, {0x69, 0x0, 0x0}, 1}, + {0x20C, {0x6F, 0x0, 0x0}, 1}, + {0x20D, {0x6F, 0x0, 0x0}, 1}, + {0x20E, {0x6F, 0x0, 0x0}, 1}, + {0x20F, {0x6F, 0x0, 0x0}, 1}, + {0x210, {0x72, 0x0, 0x0}, 1}, + {0x211, {0x72, 0x0, 0x0}, 1}, + {0x212, {0x72, 0x0, 0x0}, 1}, + {0x213, {0x72, 0x0, 0x0}, 1}, + {0x214, {0x75, 0x0, 0x0}, 1}, + {0x215, {0x75, 0x0, 0x0}, 1}, + {0x216, {0x75, 0x0, 0x0}, 1}, + {0x217, {0x75, 0x0, 0x0}, 1}, + {0x218, {0x73, 0x0, 0x0}, 1}, + {0x219, {0x73, 0x0, 0x0}, 1}, + {0x21A, {0x74, 0x0, 0x0}, 1}, + {0x21B, {0x74, 0x0, 0x0}, 1}, + {0x21C, {0x21D, 0x0, 0x0}, 1}, + {0x21E, {0x68, 0x0, 0x0}, 1}, + {0x21F, {0x68, 0x0, 0x0}, 1}, + {0x220, {0x19E, 0x0, 0x0}, 1}, + {0x222, {0x223, 0x0, 0x0}, 1}, + {0x224, {0x225, 0x0, 0x0}, 1}, + {0x226, {0x61, 0x0, 0x0}, 1}, + {0x227, {0x61, 0x0, 0x0}, 1}, + {0x228, {0x65, 0x0, 0x0}, 1}, + {0x229, {0x65, 0x0, 0x0}, 1}, + {0x22A, {0x6F, 0x0, 0x0}, 1}, + {0x22B, {0x6F, 0x0, 0x0}, 1}, + {0x22C, {0x6F, 0x0, 0x0}, 1}, + {0x22D, {0x6F, 0x0, 0x0}, 1}, + {0x22E, {0x6F, 0x0, 0x0}, 1}, + {0x22F, {0x6F, 0x0, 0x0}, 1}, + {0x230, {0x6F, 0x0, 0x0}, 1}, + {0x231, {0x6F, 0x0, 0x0}, 1}, + {0x232, {0x79, 0x0, 0x0}, 1}, + {0x233, {0x79, 0x0, 0x0}, 1}, + {0x23A, {0x2C65, 0x0, 0x0}, 1}, + {0x23B, {0x23C, 0x0, 0x0}, 1}, + {0x23D, {0x19A, 0x0, 0x0}, 1}, + {0x23E, {0x2C66, 0x0, 0x0}, 1}, + {0x241, {0x242, 0x0, 0x0}, 1}, + {0x243, {0x180, 0x0, 0x0}, 1}, + {0x244, {0x289, 0x0, 0x0}, 1}, + {0x245, {0x28C, 0x0, 0x0}, 1}, + {0x246, {0x247, 0x0, 0x0}, 1}, + {0x248, {0x249, 0x0, 0x0}, 1}, + {0x24A, {0x24B, 0x0, 0x0}, 1}, + {0x24C, {0x24D, 0x0, 0x0}, 1}, + {0x24E, {0x24F, 0x0, 0x0}, 1}, + {0x300, {0x0, 0x0, 0x0}, 0}, + {0x301, {0x0, 0x0, 0x0}, 0}, + {0x302, {0x0, 0x0, 0x0}, 0}, + {0x303, {0x0, 0x0, 0x0}, 0}, + {0x304, {0x0, 0x0, 0x0}, 0}, + {0x305, {0x0, 0x0, 0x0}, 0}, + {0x306, {0x0, 0x0, 0x0}, 0}, + {0x307, {0x0, 0x0, 0x0}, 0}, + {0x308, {0x0, 0x0, 0x0}, 0}, + {0x309, {0x0, 0x0, 0x0}, 0}, + {0x30A, {0x0, 0x0, 0x0}, 0}, + {0x30B, {0x0, 0x0, 0x0}, 0}, + {0x30C, {0x0, 0x0, 0x0}, 0}, + {0x30D, {0x0, 0x0, 0x0}, 0}, + {0x30E, {0x0, 0x0, 0x0}, 0}, + {0x30F, {0x0, 0x0, 0x0}, 0}, + {0x310, {0x0, 0x0, 0x0}, 0}, + {0x311, {0x0, 0x0, 0x0}, 0}, + {0x312, {0x0, 0x0, 0x0}, 0}, + {0x313, {0x0, 0x0, 0x0}, 0}, + {0x314, {0x0, 0x0, 0x0}, 0}, + {0x315, {0x0, 0x0, 0x0}, 0}, + {0x316, {0x0, 0x0, 0x0}, 0}, + {0x317, {0x0, 0x0, 0x0}, 0}, + {0x318, {0x0, 0x0, 0x0}, 0}, + {0x319, {0x0, 0x0, 0x0}, 0}, + {0x31A, {0x0, 0x0, 0x0}, 0}, + {0x31B, {0x0, 0x0, 0x0}, 0}, + {0x31C, {0x0, 0x0, 0x0}, 0}, + {0x31D, {0x0, 0x0, 0x0}, 0}, + {0x31E, {0x0, 0x0, 0x0}, 0}, + {0x31F, {0x0, 0x0, 0x0}, 0}, + {0x320, {0x0, 0x0, 0x0}, 0}, + {0x321, {0x0, 0x0, 0x0}, 0}, + {0x322, {0x0, 0x0, 0x0}, 0}, + {0x323, {0x0, 0x0, 0x0}, 0}, + {0x324, {0x0, 0x0, 0x0}, 0}, + {0x325, {0x0, 0x0, 0x0}, 0}, + {0x326, {0x0, 0x0, 0x0}, 0}, + {0x327, {0x0, 0x0, 0x0}, 0}, + {0x328, {0x0, 0x0, 0x0}, 0}, + {0x329, {0x0, 0x0, 0x0}, 0}, + {0x32A, {0x0, 0x0, 0x0}, 0}, + {0x32B, {0x0, 0x0, 0x0}, 0}, + {0x32C, {0x0, 0x0, 0x0}, 0}, + {0x32D, {0x0, 0x0, 0x0}, 0}, + {0x32E, {0x0, 0x0, 0x0}, 0}, + {0x32F, {0x0, 0x0, 0x0}, 0}, + {0x330, {0x0, 0x0, 0x0}, 0}, + {0x331, {0x0, 0x0, 0x0}, 0}, + {0x332, {0x0, 0x0, 0x0}, 0}, + {0x333, {0x0, 0x0, 0x0}, 0}, + {0x334, {0x0, 0x0, 0x0}, 0}, + {0x335, {0x0, 0x0, 0x0}, 0}, + {0x336, {0x0, 0x0, 0x0}, 0}, + {0x337, {0x0, 0x0, 0x0}, 0}, + {0x338, {0x0, 0x0, 0x0}, 0}, + {0x339, {0x0, 0x0, 0x0}, 0}, + {0x33A, {0x0, 0x0, 0x0}, 0}, + {0x33B, {0x0, 0x0, 0x0}, 0}, + {0x33C, {0x0, 0x0, 0x0}, 0}, + {0x33D, {0x0, 0x0, 0x0}, 0}, + {0x33E, {0x0, 0x0, 0x0}, 0}, + {0x33F, {0x0, 0x0, 0x0}, 0}, + {0x340, {0x0, 0x0, 0x0}, 0}, + {0x341, {0x0, 0x0, 0x0}, 0}, + {0x342, {0x0, 0x0, 0x0}, 0}, + {0x343, {0x0, 0x0, 0x0}, 0}, + {0x344, {0x0, 0x0, 0x0}, 0}, + {0x345, {0x0, 0x0, 0x0}, 0}, + {0x346, {0x0, 0x0, 0x0}, 0}, + {0x347, {0x0, 0x0, 0x0}, 0}, + {0x348, {0x0, 0x0, 0x0}, 0}, + {0x349, {0x0, 0x0, 0x0}, 0}, + {0x34A, {0x0, 0x0, 0x0}, 0}, + {0x34B, {0x0, 0x0, 0x0}, 0}, + {0x34C, {0x0, 0x0, 0x0}, 0}, + {0x34D, {0x0, 0x0, 0x0}, 0}, + {0x34E, {0x0, 0x0, 0x0}, 0}, + {0x34F, {0x0, 0x0, 0x0}, 0}, + {0x350, {0x0, 0x0, 0x0}, 0}, + {0x351, {0x0, 0x0, 0x0}, 0}, + {0x352, {0x0, 0x0, 0x0}, 0}, + {0x353, {0x0, 0x0, 0x0}, 0}, + {0x354, {0x0, 0x0, 0x0}, 0}, + {0x355, {0x0, 0x0, 0x0}, 0}, + {0x356, {0x0, 0x0, 0x0}, 0}, + {0x357, {0x0, 0x0, 0x0}, 0}, + {0x358, {0x0, 0x0, 0x0}, 0}, + {0x359, {0x0, 0x0, 0x0}, 0}, + {0x35A, {0x0, 0x0, 0x0}, 0}, + {0x35B, {0x0, 0x0, 0x0}, 0}, + {0x35C, {0x0, 0x0, 0x0}, 0}, + {0x35D, {0x0, 0x0, 0x0}, 0}, + {0x35E, {0x0, 0x0, 0x0}, 0}, + {0x35F, {0x0, 0x0, 0x0}, 0}, + {0x360, {0x0, 0x0, 0x0}, 0}, + {0x361, {0x0, 0x0, 0x0}, 0}, + {0x362, {0x0, 0x0, 0x0}, 0}, + {0x363, {0x0, 0x0, 0x0}, 0}, + {0x364, {0x0, 0x0, 0x0}, 0}, + {0x365, {0x0, 0x0, 0x0}, 0}, + {0x366, {0x0, 0x0, 0x0}, 0}, + {0x367, {0x0, 0x0, 0x0}, 0}, + {0x368, {0x0, 0x0, 0x0}, 0}, + {0x369, {0x0, 0x0, 0x0}, 0}, + {0x36A, {0x0, 0x0, 0x0}, 0}, + {0x36B, {0x0, 0x0, 0x0}, 0}, + {0x36C, {0x0, 0x0, 0x0}, 0}, + {0x36D, {0x0, 0x0, 0x0}, 0}, + {0x36E, {0x0, 0x0, 0x0}, 0}, + {0x36F, {0x0, 0x0, 0x0}, 0}, + {0x370, {0x371, 0x0, 0x0}, 1}, + {0x372, {0x373, 0x0, 0x0}, 1}, + {0x374, {0x2B9, 0x0, 0x0}, 1}, + {0x376, {0x377, 0x0, 0x0}, 1}, + {0x37E, {0x3B, 0x0, 0x0}, 1}, + {0x37F, {0x3F3, 0x0, 0x0}, 1}, + {0x385, {0xA8, 0x0, 0x0}, 1}, + {0x386, {0x3B1, 0x0, 0x0}, 1}, + {0x387, {0xB7, 0x0, 0x0}, 1}, + {0x388, {0x3B5, 0x0, 0x0}, 1}, + {0x389, {0x3B7, 0x0, 0x0}, 1}, + {0x38A, {0x3B9, 0x0, 0x0}, 1}, + {0x38C, {0x3BF, 0x0, 0x0}, 1}, + {0x38E, {0x3C5, 0x0, 0x0}, 1}, + {0x38F, {0x3C9, 0x0, 0x0}, 1}, + {0x390, {0x3B9, 0x0, 0x0}, 1}, + {0x391, {0x3B1, 0x0, 0x0}, 1}, + {0x392, {0x3B2, 0x0, 0x0}, 1}, + {0x393, {0x3B3, 0x0, 0x0}, 1}, + {0x394, {0x3B4, 0x0, 0x0}, 1}, + {0x395, {0x3B5, 0x0, 0x0}, 1}, + {0x396, {0x3B6, 0x0, 0x0}, 1}, + {0x397, {0x3B7, 0x0, 0x0}, 1}, + {0x398, {0x3B8, 0x0, 0x0}, 1}, + {0x399, {0x3B9, 0x0, 0x0}, 1}, + {0x39A, {0x3BA, 0x0, 0x0}, 1}, + {0x39B, {0x3BB, 0x0, 0x0}, 1}, + {0x39C, {0x3BC, 0x0, 0x0}, 1}, + {0x39D, {0x3BD, 0x0, 0x0}, 1}, + {0x39E, {0x3BE, 0x0, 0x0}, 1}, + {0x39F, {0x3BF, 0x0, 0x0}, 1}, + {0x3A0, {0x3C0, 0x0, 0x0}, 1}, + {0x3A1, {0x3C1, 0x0, 0x0}, 1}, + {0x3A3, {0x3C3, 0x0, 0x0}, 1}, + {0x3A4, {0x3C4, 0x0, 0x0}, 1}, + {0x3A5, {0x3C5, 0x0, 0x0}, 1}, + {0x3A6, {0x3C6, 0x0, 0x0}, 1}, + {0x3A7, {0x3C7, 0x0, 0x0}, 1}, + {0x3A8, {0x3C8, 0x0, 0x0}, 1}, + {0x3A9, {0x3C9, 0x0, 0x0}, 1}, + {0x3AA, {0x3B9, 0x0, 0x0}, 1}, + {0x3AB, {0x3C5, 0x0, 0x0}, 1}, + {0x3AC, {0x3B1, 0x0, 0x0}, 1}, + {0x3AD, {0x3B5, 0x0, 0x0}, 1}, + {0x3AE, {0x3B7, 0x0, 0x0}, 1}, + {0x3AF, {0x3B9, 0x0, 0x0}, 1}, + {0x3B0, {0x3C5, 0x0, 0x0}, 1}, + {0x3CA, {0x3B9, 0x0, 0x0}, 1}, + {0x3CB, {0x3C5, 0x0, 0x0}, 1}, + {0x3CC, {0x3BF, 0x0, 0x0}, 1}, + {0x3CD, {0x3C5, 0x0, 0x0}, 1}, + {0x3CE, {0x3C9, 0x0, 0x0}, 1}, + {0x3CF, {0x3D7, 0x0, 0x0}, 1}, + {0x3D3, {0x3D2, 0x0, 0x0}, 1}, + {0x3D4, {0x3D2, 0x0, 0x0}, 1}, + {0x3D8, {0x3D9, 0x0, 0x0}, 1}, + {0x3DA, {0x3DB, 0x0, 0x0}, 1}, + {0x3DC, {0x3DD, 0x0, 0x0}, 1}, + {0x3DE, {0x3DF, 0x0, 0x0}, 1}, + {0x3E0, {0x3E1, 0x0, 0x0}, 1}, + {0x3E2, {0x3E3, 0x0, 0x0}, 1}, + {0x3E4, {0x3E5, 0x0, 0x0}, 1}, + {0x3E6, {0x3E7, 0x0, 0x0}, 1}, + {0x3E8, {0x3E9, 0x0, 0x0}, 1}, + {0x3EA, {0x3EB, 0x0, 0x0}, 1}, + {0x3EC, {0x3ED, 0x0, 0x0}, 1}, + {0x3EE, {0x3EF, 0x0, 0x0}, 1}, + {0x3F4, {0x3B8, 0x0, 0x0}, 1}, + {0x3F7, {0x3F8, 0x0, 0x0}, 1}, + {0x3F9, {0x3F2, 0x0, 0x0}, 1}, + {0x3FA, {0x3FB, 0x0, 0x0}, 1}, + {0x3FD, {0x37B, 0x0, 0x0}, 1}, + {0x3FE, {0x37C, 0x0, 0x0}, 1}, + {0x3FF, {0x37D, 0x0, 0x0}, 1}, + {0x400, {0x435, 0x0, 0x0}, 1}, + {0x401, {0x435, 0x0, 0x0}, 1}, + {0x402, {0x452, 0x0, 0x0}, 1}, + {0x403, {0x433, 0x0, 0x0}, 1}, + {0x404, {0x454, 0x0, 0x0}, 1}, + {0x405, {0x455, 0x0, 0x0}, 1}, + {0x406, {0x456, 0x0, 0x0}, 1}, + {0x407, {0x456, 0x0, 0x0}, 1}, + {0x408, {0x458, 0x0, 0x0}, 1}, + {0x409, {0x459, 0x0, 0x0}, 1}, + {0x40A, {0x45A, 0x0, 0x0}, 1}, + {0x40B, {0x45B, 0x0, 0x0}, 1}, + {0x40C, {0x43A, 0x0, 0x0}, 1}, + {0x40D, {0x438, 0x0, 0x0}, 1}, + {0x40E, {0x443, 0x0, 0x0}, 1}, + {0x40F, {0x45F, 0x0, 0x0}, 1}, + {0x410, {0x430, 0x0, 0x0}, 1}, + {0x411, {0x431, 0x0, 0x0}, 1}, + {0x412, {0x432, 0x0, 0x0}, 1}, + {0x413, {0x433, 0x0, 0x0}, 1}, + {0x414, {0x434, 0x0, 0x0}, 1}, + {0x415, {0x435, 0x0, 0x0}, 1}, + {0x416, {0x436, 0x0, 0x0}, 1}, + {0x417, {0x437, 0x0, 0x0}, 1}, + {0x418, {0x438, 0x0, 0x0}, 1}, + {0x419, {0x438, 0x0, 0x0}, 1}, + {0x41A, {0x43A, 0x0, 0x0}, 1}, + {0x41B, {0x43B, 0x0, 0x0}, 1}, + {0x41C, {0x43C, 0x0, 0x0}, 1}, + {0x41D, {0x43D, 0x0, 0x0}, 1}, + {0x41E, {0x43E, 0x0, 0x0}, 1}, + {0x41F, {0x43F, 0x0, 0x0}, 1}, + {0x420, {0x440, 0x0, 0x0}, 1}, + {0x421, {0x441, 0x0, 0x0}, 1}, + {0x422, {0x442, 0x0, 0x0}, 1}, + {0x423, {0x443, 0x0, 0x0}, 1}, + {0x424, {0x444, 0x0, 0x0}, 1}, + {0x425, {0x445, 0x0, 0x0}, 1}, + {0x426, {0x446, 0x0, 0x0}, 1}, + {0x427, {0x447, 0x0, 0x0}, 1}, + {0x428, {0x448, 0x0, 0x0}, 1}, + {0x429, {0x449, 0x0, 0x0}, 1}, + {0x42A, {0x44A, 0x0, 0x0}, 1}, + {0x42B, {0x44B, 0x0, 0x0}, 1}, + {0x42C, {0x44C, 0x0, 0x0}, 1}, + {0x42D, {0x44D, 0x0, 0x0}, 1}, + {0x42E, {0x44E, 0x0, 0x0}, 1}, + {0x42F, {0x44F, 0x0, 0x0}, 1}, + {0x439, {0x438, 0x0, 0x0}, 1}, + {0x450, {0x435, 0x0, 0x0}, 1}, + {0x451, {0x435, 0x0, 0x0}, 1}, + {0x453, {0x433, 0x0, 0x0}, 1}, + {0x457, {0x456, 0x0, 0x0}, 1}, + {0x45C, {0x43A, 0x0, 0x0}, 1}, + {0x45D, {0x438, 0x0, 0x0}, 1}, + {0x45E, {0x443, 0x0, 0x0}, 1}, + {0x460, {0x461, 0x0, 0x0}, 1}, + {0x462, {0x463, 0x0, 0x0}, 1}, + {0x464, {0x465, 0x0, 0x0}, 1}, + {0x466, {0x467, 0x0, 0x0}, 1}, + {0x468, {0x469, 0x0, 0x0}, 1}, + {0x46A, {0x46B, 0x0, 0x0}, 1}, + {0x46C, {0x46D, 0x0, 0x0}, 1}, + {0x46E, {0x46F, 0x0, 0x0}, 1}, + {0x470, {0x471, 0x0, 0x0}, 1}, + {0x472, {0x473, 0x0, 0x0}, 1}, + {0x474, {0x475, 0x0, 0x0}, 1}, + {0x476, {0x475, 0x0, 0x0}, 1}, + {0x477, {0x475, 0x0, 0x0}, 1}, + {0x478, {0x479, 0x0, 0x0}, 1}, + {0x47A, {0x47B, 0x0, 0x0}, 1}, + {0x47C, {0x47D, 0x0, 0x0}, 1}, + {0x47E, {0x47F, 0x0, 0x0}, 1}, + {0x480, {0x481, 0x0, 0x0}, 1}, + {0x483, {0x0, 0x0, 0x0}, 0}, + {0x484, {0x0, 0x0, 0x0}, 0}, + {0x485, {0x0, 0x0, 0x0}, 0}, + {0x486, {0x0, 0x0, 0x0}, 0}, + {0x487, {0x0, 0x0, 0x0}, 0}, + {0x48A, {0x48B, 0x0, 0x0}, 1}, + {0x48C, {0x48D, 0x0, 0x0}, 1}, + {0x48E, {0x48F, 0x0, 0x0}, 1}, + {0x490, {0x491, 0x0, 0x0}, 1}, + {0x492, {0x493, 0x0, 0x0}, 1}, + {0x494, {0x495, 0x0, 0x0}, 1}, + {0x496, {0x497, 0x0, 0x0}, 1}, + {0x498, {0x499, 0x0, 0x0}, 1}, + {0x49A, {0x49B, 0x0, 0x0}, 1}, + {0x49C, {0x49D, 0x0, 0x0}, 1}, + {0x49E, {0x49F, 0x0, 0x0}, 1}, + {0x4A0, {0x4A1, 0x0, 0x0}, 1}, + {0x4A2, {0x4A3, 0x0, 0x0}, 1}, + {0x4A4, {0x4A5, 0x0, 0x0}, 1}, + {0x4A6, {0x4A7, 0x0, 0x0}, 1}, + {0x4A8, {0x4A9, 0x0, 0x0}, 1}, + {0x4AA, {0x4AB, 0x0, 0x0}, 1}, + {0x4AC, {0x4AD, 0x0, 0x0}, 1}, + {0x4AE, {0x4AF, 0x0, 0x0}, 1}, + {0x4B0, {0x4B1, 0x0, 0x0}, 1}, + {0x4B2, {0x4B3, 0x0, 0x0}, 1}, + {0x4B4, {0x4B5, 0x0, 0x0}, 1}, + {0x4B6, {0x4B7, 0x0, 0x0}, 1}, + {0x4B8, {0x4B9, 0x0, 0x0}, 1}, + {0x4BA, {0x4BB, 0x0, 0x0}, 1}, + {0x4BC, {0x4BD, 0x0, 0x0}, 1}, + {0x4BE, {0x4BF, 0x0, 0x0}, 1}, + {0x4C0, {0x4CF, 0x0, 0x0}, 1}, + {0x4C1, {0x436, 0x0, 0x0}, 1}, + {0x4C2, {0x436, 0x0, 0x0}, 1}, + {0x4C3, {0x4C4, 0x0, 0x0}, 1}, + {0x4C5, {0x4C6, 0x0, 0x0}, 1}, + {0x4C7, {0x4C8, 0x0, 0x0}, 1}, + {0x4C9, {0x4CA, 0x0, 0x0}, 1}, + {0x4CB, {0x4CC, 0x0, 0x0}, 1}, + {0x4CD, {0x4CE, 0x0, 0x0}, 1}, + {0x4D0, {0x430, 0x0, 0x0}, 1}, + {0x4D1, {0x430, 0x0, 0x0}, 1}, + {0x4D2, {0x430, 0x0, 0x0}, 1}, + {0x4D3, {0x430, 0x0, 0x0}, 1}, + {0x4D4, {0x4D5, 0x0, 0x0}, 1}, + {0x4D6, {0x435, 0x0, 0x0}, 1}, + {0x4D7, {0x435, 0x0, 0x0}, 1}, + {0x4D8, {0x4D9, 0x0, 0x0}, 1}, + {0x4DA, {0x4D9, 0x0, 0x0}, 1}, + {0x4DB, {0x4D9, 0x0, 0x0}, 1}, + {0x4DC, {0x436, 0x0, 0x0}, 1}, + {0x4DD, {0x436, 0x0, 0x0}, 1}, + {0x4DE, {0x437, 0x0, 0x0}, 1}, + {0x4DF, {0x437, 0x0, 0x0}, 1}, + {0x4E0, {0x4E1, 0x0, 0x0}, 1}, + {0x4E2, {0x438, 0x0, 0x0}, 1}, + {0x4E3, {0x438, 0x0, 0x0}, 1}, + {0x4E4, {0x438, 0x0, 0x0}, 1}, + {0x4E5, {0x438, 0x0, 0x0}, 1}, + {0x4E6, {0x43E, 0x0, 0x0}, 1}, + {0x4E7, {0x43E, 0x0, 0x0}, 1}, + {0x4E8, {0x4E9, 0x0, 0x0}, 1}, + {0x4EA, {0x4E9, 0x0, 0x0}, 1}, + {0x4EB, {0x4E9, 0x0, 0x0}, 1}, + {0x4EC, {0x44D, 0x0, 0x0}, 1}, + {0x4ED, {0x44D, 0x0, 0x0}, 1}, + {0x4EE, {0x443, 0x0, 0x0}, 1}, + {0x4EF, {0x443, 0x0, 0x0}, 1}, + {0x4F0, {0x443, 0x0, 0x0}, 1}, + {0x4F1, {0x443, 0x0, 0x0}, 1}, + {0x4F2, {0x443, 0x0, 0x0}, 1}, + {0x4F3, {0x443, 0x0, 0x0}, 1}, + {0x4F4, {0x447, 0x0, 0x0}, 1}, + {0x4F5, {0x447, 0x0, 0x0}, 1}, + {0x4F6, {0x4F7, 0x0, 0x0}, 1}, + {0x4F8, {0x44B, 0x0, 0x0}, 1}, + {0x4F9, {0x44B, 0x0, 0x0}, 1}, + {0x4FA, {0x4FB, 0x0, 0x0}, 1}, + {0x4FC, {0x4FD, 0x0, 0x0}, 1}, + {0x4FE, {0x4FF, 0x0, 0x0}, 1}, + {0x500, {0x501, 0x0, 0x0}, 1}, + {0x502, {0x503, 0x0, 0x0}, 1}, + {0x504, {0x505, 0x0, 0x0}, 1}, + {0x506, {0x507, 0x0, 0x0}, 1}, + {0x508, {0x509, 0x0, 0x0}, 1}, + {0x50A, {0x50B, 0x0, 0x0}, 1}, + {0x50C, {0x50D, 0x0, 0x0}, 1}, + {0x50E, {0x50F, 0x0, 0x0}, 1}, + {0x510, {0x511, 0x0, 0x0}, 1}, + {0x512, {0x513, 0x0, 0x0}, 1}, + {0x514, {0x515, 0x0, 0x0}, 1}, + {0x516, {0x517, 0x0, 0x0}, 1}, + {0x518, {0x519, 0x0, 0x0}, 1}, + {0x51A, {0x51B, 0x0, 0x0}, 1}, + {0x51C, {0x51D, 0x0, 0x0}, 1}, + {0x51E, {0x51F, 0x0, 0x0}, 1}, + {0x520, {0x521, 0x0, 0x0}, 1}, + {0x522, {0x523, 0x0, 0x0}, 1}, + {0x524, {0x525, 0x0, 0x0}, 1}, + {0x526, {0x527, 0x0, 0x0}, 1}, + {0x528, {0x529, 0x0, 0x0}, 1}, + {0x52A, {0x52B, 0x0, 0x0}, 1}, + {0x52C, {0x52D, 0x0, 0x0}, 1}, + {0x52E, {0x52F, 0x0, 0x0}, 1}, + {0x531, {0x561, 0x0, 0x0}, 1}, + {0x532, {0x562, 0x0, 0x0}, 1}, + {0x533, {0x563, 0x0, 0x0}, 1}, + {0x534, {0x564, 0x0, 0x0}, 1}, + {0x535, {0x565, 0x0, 0x0}, 1}, + {0x536, {0x566, 0x0, 0x0}, 1}, + {0x537, {0x567, 0x0, 0x0}, 1}, + {0x538, {0x568, 0x0, 0x0}, 1}, + {0x539, {0x569, 0x0, 0x0}, 1}, + {0x53A, {0x56A, 0x0, 0x0}, 1}, + {0x53B, {0x56B, 0x0, 0x0}, 1}, + {0x53C, {0x56C, 0x0, 0x0}, 1}, + {0x53D, {0x56D, 0x0, 0x0}, 1}, + {0x53E, {0x56E, 0x0, 0x0}, 1}, + {0x53F, {0x56F, 0x0, 0x0}, 1}, + {0x540, {0x570, 0x0, 0x0}, 1}, + {0x541, {0x571, 0x0, 0x0}, 1}, + {0x542, {0x572, 0x0, 0x0}, 1}, + {0x543, {0x573, 0x0, 0x0}, 1}, + {0x544, {0x574, 0x0, 0x0}, 1}, + {0x545, {0x575, 0x0, 0x0}, 1}, + {0x546, {0x576, 0x0, 0x0}, 1}, + {0x547, {0x577, 0x0, 0x0}, 1}, + {0x548, {0x578, 0x0, 0x0}, 1}, + {0x549, {0x579, 0x0, 0x0}, 1}, + {0x54A, {0x57A, 0x0, 0x0}, 1}, + {0x54B, {0x57B, 0x0, 0x0}, 1}, + {0x54C, {0x57C, 0x0, 0x0}, 1}, + {0x54D, {0x57D, 0x0, 0x0}, 1}, + {0x54E, {0x57E, 0x0, 0x0}, 1}, + {0x54F, {0x57F, 0x0, 0x0}, 1}, + {0x550, {0x580, 0x0, 0x0}, 1}, + {0x551, {0x581, 0x0, 0x0}, 1}, + {0x552, {0x582, 0x0, 0x0}, 1}, + {0x553, {0x583, 0x0, 0x0}, 1}, + {0x554, {0x584, 0x0, 0x0}, 1}, + {0x555, {0x585, 0x0, 0x0}, 1}, + {0x556, {0x586, 0x0, 0x0}, 1}, + {0x591, {0x0, 0x0, 0x0}, 0}, + {0x592, {0x0, 0x0, 0x0}, 0}, + {0x593, {0x0, 0x0, 0x0}, 0}, + {0x594, {0x0, 0x0, 0x0}, 0}, + {0x595, {0x0, 0x0, 0x0}, 0}, + {0x596, {0x0, 0x0, 0x0}, 0}, + {0x597, {0x0, 0x0, 0x0}, 0}, + {0x598, {0x0, 0x0, 0x0}, 0}, + {0x599, {0x0, 0x0, 0x0}, 0}, + {0x59A, {0x0, 0x0, 0x0}, 0}, + {0x59B, {0x0, 0x0, 0x0}, 0}, + {0x59C, {0x0, 0x0, 0x0}, 0}, + {0x59D, {0x0, 0x0, 0x0}, 0}, + {0x59E, {0x0, 0x0, 0x0}, 0}, + {0x59F, {0x0, 0x0, 0x0}, 0}, + {0x5A0, {0x0, 0x0, 0x0}, 0}, + {0x5A1, {0x0, 0x0, 0x0}, 0}, + {0x5A2, {0x0, 0x0, 0x0}, 0}, + {0x5A3, {0x0, 0x0, 0x0}, 0}, + {0x5A4, {0x0, 0x0, 0x0}, 0}, + {0x5A5, {0x0, 0x0, 0x0}, 0}, + {0x5A6, {0x0, 0x0, 0x0}, 0}, + {0x5A7, {0x0, 0x0, 0x0}, 0}, + {0x5A8, {0x0, 0x0, 0x0}, 0}, + {0x5A9, {0x0, 0x0, 0x0}, 0}, + {0x5AA, {0x0, 0x0, 0x0}, 0}, + {0x5AB, {0x0, 0x0, 0x0}, 0}, + {0x5AC, {0x0, 0x0, 0x0}, 0}, + {0x5AD, {0x0, 0x0, 0x0}, 0}, + {0x5AE, {0x0, 0x0, 0x0}, 0}, + {0x5AF, {0x0, 0x0, 0x0}, 0}, + {0x5B0, {0x0, 0x0, 0x0}, 0}, + {0x5B1, {0x0, 0x0, 0x0}, 0}, + {0x5B2, {0x0, 0x0, 0x0}, 0}, + {0x5B3, {0x0, 0x0, 0x0}, 0}, + {0x5B4, {0x0, 0x0, 0x0}, 0}, + {0x5B5, {0x0, 0x0, 0x0}, 0}, + {0x5B6, {0x0, 0x0, 0x0}, 0}, + {0x5B7, {0x0, 0x0, 0x0}, 0}, + {0x5B8, {0x0, 0x0, 0x0}, 0}, + {0x5B9, {0x0, 0x0, 0x0}, 0}, + {0x5BA, {0x0, 0x0, 0x0}, 0}, + {0x5BB, {0x0, 0x0, 0x0}, 0}, + {0x5BC, {0x0, 0x0, 0x0}, 0}, + {0x5BD, {0x0, 0x0, 0x0}, 0}, + {0x5BF, {0x0, 0x0, 0x0}, 0}, + {0x5C1, {0x0, 0x0, 0x0}, 0}, + {0x5C2, {0x0, 0x0, 0x0}, 0}, + {0x5C4, {0x0, 0x0, 0x0}, 0}, + {0x5C5, {0x0, 0x0, 0x0}, 0}, + {0x5C7, {0x0, 0x0, 0x0}, 0}, + {0x610, {0x0, 0x0, 0x0}, 0}, + {0x611, {0x0, 0x0, 0x0}, 0}, + {0x612, {0x0, 0x0, 0x0}, 0}, + {0x613, {0x0, 0x0, 0x0}, 0}, + {0x614, {0x0, 0x0, 0x0}, 0}, + {0x615, {0x0, 0x0, 0x0}, 0}, + {0x616, {0x0, 0x0, 0x0}, 0}, + {0x617, {0x0, 0x0, 0x0}, 0}, + {0x618, {0x0, 0x0, 0x0}, 0}, + {0x619, {0x0, 0x0, 0x0}, 0}, + {0x61A, {0x0, 0x0, 0x0}, 0}, + {0x622, {0x627, 0x0, 0x0}, 1}, + {0x623, {0x627, 0x0, 0x0}, 1}, + {0x624, {0x648, 0x0, 0x0}, 1}, + {0x625, {0x627, 0x0, 0x0}, 1}, + {0x626, {0x64A, 0x0, 0x0}, 1}, + {0x64B, {0x0, 0x0, 0x0}, 0}, + {0x64C, {0x0, 0x0, 0x0}, 0}, + {0x64D, {0x0, 0x0, 0x0}, 0}, + {0x64E, {0x0, 0x0, 0x0}, 0}, + {0x64F, {0x0, 0x0, 0x0}, 0}, + {0x650, {0x0, 0x0, 0x0}, 0}, + {0x651, {0x0, 0x0, 0x0}, 0}, + {0x652, {0x0, 0x0, 0x0}, 0}, + {0x653, {0x0, 0x0, 0x0}, 0}, + {0x654, {0x0, 0x0, 0x0}, 0}, + {0x655, {0x0, 0x0, 0x0}, 0}, + {0x656, {0x0, 0x0, 0x0}, 0}, + {0x657, {0x0, 0x0, 0x0}, 0}, + {0x658, {0x0, 0x0, 0x0}, 0}, + {0x659, {0x0, 0x0, 0x0}, 0}, + {0x65A, {0x0, 0x0, 0x0}, 0}, + {0x65B, {0x0, 0x0, 0x0}, 0}, + {0x65C, {0x0, 0x0, 0x0}, 0}, + {0x65D, {0x0, 0x0, 0x0}, 0}, + {0x65E, {0x0, 0x0, 0x0}, 0}, + {0x65F, {0x0, 0x0, 0x0}, 0}, + {0x670, {0x0, 0x0, 0x0}, 0}, + {0x6C0, {0x6D5, 0x0, 0x0}, 1}, + {0x6C2, {0x6C1, 0x0, 0x0}, 1}, + {0x6D3, {0x6D2, 0x0, 0x0}, 1}, + {0x6D6, {0x0, 0x0, 0x0}, 0}, + {0x6D7, {0x0, 0x0, 0x0}, 0}, + {0x6D8, {0x0, 0x0, 0x0}, 0}, + {0x6D9, {0x0, 0x0, 0x0}, 0}, + {0x6DA, {0x0, 0x0, 0x0}, 0}, + {0x6DB, {0x0, 0x0, 0x0}, 0}, + {0x6DC, {0x0, 0x0, 0x0}, 0}, + {0x6DF, {0x0, 0x0, 0x0}, 0}, + {0x6E0, {0x0, 0x0, 0x0}, 0}, + {0x6E1, {0x0, 0x0, 0x0}, 0}, + {0x6E2, {0x0, 0x0, 0x0}, 0}, + {0x6E3, {0x0, 0x0, 0x0}, 0}, + {0x6E4, {0x0, 0x0, 0x0}, 0}, + {0x6E7, {0x0, 0x0, 0x0}, 0}, + {0x6E8, {0x0, 0x0, 0x0}, 0}, + {0x6EA, {0x0, 0x0, 0x0}, 0}, + {0x6EB, {0x0, 0x0, 0x0}, 0}, + {0x6EC, {0x0, 0x0, 0x0}, 0}, + {0x6ED, {0x0, 0x0, 0x0}, 0}, + {0x711, {0x0, 0x0, 0x0}, 0}, + {0x730, {0x0, 0x0, 0x0}, 0}, + {0x731, {0x0, 0x0, 0x0}, 0}, + {0x732, {0x0, 0x0, 0x0}, 0}, + {0x733, {0x0, 0x0, 0x0}, 0}, + {0x734, {0x0, 0x0, 0x0}, 0}, + {0x735, {0x0, 0x0, 0x0}, 0}, + {0x736, {0x0, 0x0, 0x0}, 0}, + {0x737, {0x0, 0x0, 0x0}, 0}, + {0x738, {0x0, 0x0, 0x0}, 0}, + {0x739, {0x0, 0x0, 0x0}, 0}, + {0x73A, {0x0, 0x0, 0x0}, 0}, + {0x73B, {0x0, 0x0, 0x0}, 0}, + {0x73C, {0x0, 0x0, 0x0}, 0}, + {0x73D, {0x0, 0x0, 0x0}, 0}, + {0x73E, {0x0, 0x0, 0x0}, 0}, + {0x73F, {0x0, 0x0, 0x0}, 0}, + {0x740, {0x0, 0x0, 0x0}, 0}, + {0x741, {0x0, 0x0, 0x0}, 0}, + {0x742, {0x0, 0x0, 0x0}, 0}, + {0x743, {0x0, 0x0, 0x0}, 0}, + {0x744, {0x0, 0x0, 0x0}, 0}, + {0x745, {0x0, 0x0, 0x0}, 0}, + {0x746, {0x0, 0x0, 0x0}, 0}, + {0x747, {0x0, 0x0, 0x0}, 0}, + {0x748, {0x0, 0x0, 0x0}, 0}, + {0x749, {0x0, 0x0, 0x0}, 0}, + {0x74A, {0x0, 0x0, 0x0}, 0}, + {0x7A6, {0x0, 0x0, 0x0}, 0}, + {0x7A7, {0x0, 0x0, 0x0}, 0}, + {0x7A8, {0x0, 0x0, 0x0}, 0}, + {0x7A9, {0x0, 0x0, 0x0}, 0}, + {0x7AA, {0x0, 0x0, 0x0}, 0}, + {0x7AB, {0x0, 0x0, 0x0}, 0}, + {0x7AC, {0x0, 0x0, 0x0}, 0}, + {0x7AD, {0x0, 0x0, 0x0}, 0}, + {0x7AE, {0x0, 0x0, 0x0}, 0}, + {0x7AF, {0x0, 0x0, 0x0}, 0}, + {0x7B0, {0x0, 0x0, 0x0}, 0}, + {0x7EB, {0x0, 0x0, 0x0}, 0}, + {0x7EC, {0x0, 0x0, 0x0}, 0}, + {0x7ED, {0x0, 0x0, 0x0}, 0}, + {0x7EE, {0x0, 0x0, 0x0}, 0}, + {0x7EF, {0x0, 0x0, 0x0}, 0}, + {0x7F0, {0x0, 0x0, 0x0}, 0}, + {0x7F1, {0x0, 0x0, 0x0}, 0}, + {0x7F2, {0x0, 0x0, 0x0}, 0}, + {0x7F3, {0x0, 0x0, 0x0}, 0}, + {0x7FD, {0x0, 0x0, 0x0}, 0}, + {0x816, {0x0, 0x0, 0x0}, 0}, + {0x817, {0x0, 0x0, 0x0}, 0}, + {0x818, {0x0, 0x0, 0x0}, 0}, + {0x819, {0x0, 0x0, 0x0}, 0}, + {0x81B, {0x0, 0x0, 0x0}, 0}, + {0x81C, {0x0, 0x0, 0x0}, 0}, + {0x81D, {0x0, 0x0, 0x0}, 0}, + {0x81E, {0x0, 0x0, 0x0}, 0}, + {0x81F, {0x0, 0x0, 0x0}, 0}, + {0x820, {0x0, 0x0, 0x0}, 0}, + {0x821, {0x0, 0x0, 0x0}, 0}, + {0x822, {0x0, 0x0, 0x0}, 0}, + {0x823, {0x0, 0x0, 0x0}, 0}, + {0x825, {0x0, 0x0, 0x0}, 0}, + {0x826, {0x0, 0x0, 0x0}, 0}, + {0x827, {0x0, 0x0, 0x0}, 0}, + {0x829, {0x0, 0x0, 0x0}, 0}, + {0x82A, {0x0, 0x0, 0x0}, 0}, + {0x82B, {0x0, 0x0, 0x0}, 0}, + {0x82C, {0x0, 0x0, 0x0}, 0}, + {0x82D, {0x0, 0x0, 0x0}, 0}, + {0x859, {0x0, 0x0, 0x0}, 0}, + {0x85A, {0x0, 0x0, 0x0}, 0}, + {0x85B, {0x0, 0x0, 0x0}, 0}, + {0x8D3, {0x0, 0x0, 0x0}, 0}, + {0x8D4, {0x0, 0x0, 0x0}, 0}, + {0x8D5, {0x0, 0x0, 0x0}, 0}, + {0x8D6, {0x0, 0x0, 0x0}, 0}, + {0x8D7, {0x0, 0x0, 0x0}, 0}, + {0x8D8, {0x0, 0x0, 0x0}, 0}, + {0x8D9, {0x0, 0x0, 0x0}, 0}, + {0x8DA, {0x0, 0x0, 0x0}, 0}, + {0x8DB, {0x0, 0x0, 0x0}, 0}, + {0x8DC, {0x0, 0x0, 0x0}, 0}, + {0x8DD, {0x0, 0x0, 0x0}, 0}, + {0x8DE, {0x0, 0x0, 0x0}, 0}, + {0x8DF, {0x0, 0x0, 0x0}, 0}, + {0x8E0, {0x0, 0x0, 0x0}, 0}, + {0x8E1, {0x0, 0x0, 0x0}, 0}, + {0x8E3, {0x0, 0x0, 0x0}, 0}, + {0x8E4, {0x0, 0x0, 0x0}, 0}, + {0x8E5, {0x0, 0x0, 0x0}, 0}, + {0x8E6, {0x0, 0x0, 0x0}, 0}, + {0x8E7, {0x0, 0x0, 0x0}, 0}, + {0x8E8, {0x0, 0x0, 0x0}, 0}, + {0x8E9, {0x0, 0x0, 0x0}, 0}, + {0x8EA, {0x0, 0x0, 0x0}, 0}, + {0x8EB, {0x0, 0x0, 0x0}, 0}, + {0x8EC, {0x0, 0x0, 0x0}, 0}, + {0x8ED, {0x0, 0x0, 0x0}, 0}, + {0x8EE, {0x0, 0x0, 0x0}, 0}, + {0x8EF, {0x0, 0x0, 0x0}, 0}, + {0x8F0, {0x0, 0x0, 0x0}, 0}, + {0x8F1, {0x0, 0x0, 0x0}, 0}, + {0x8F2, {0x0, 0x0, 0x0}, 0}, + {0x8F3, {0x0, 0x0, 0x0}, 0}, + {0x8F4, {0x0, 0x0, 0x0}, 0}, + {0x8F5, {0x0, 0x0, 0x0}, 0}, + {0x8F6, {0x0, 0x0, 0x0}, 0}, + {0x8F7, {0x0, 0x0, 0x0}, 0}, + {0x8F8, {0x0, 0x0, 0x0}, 0}, + {0x8F9, {0x0, 0x0, 0x0}, 0}, + {0x8FA, {0x0, 0x0, 0x0}, 0}, + {0x8FB, {0x0, 0x0, 0x0}, 0}, + {0x8FC, {0x0, 0x0, 0x0}, 0}, + {0x8FD, {0x0, 0x0, 0x0}, 0}, + {0x8FE, {0x0, 0x0, 0x0}, 0}, + {0x8FF, {0x0, 0x0, 0x0}, 0}, + {0x900, {0x0, 0x0, 0x0}, 0}, + {0x901, {0x0, 0x0, 0x0}, 0}, + {0x902, {0x0, 0x0, 0x0}, 0}, + {0x929, {0x928, 0x0, 0x0}, 1}, + {0x931, {0x930, 0x0, 0x0}, 1}, + {0x934, {0x933, 0x0, 0x0}, 1}, + {0x93A, {0x0, 0x0, 0x0}, 0}, + {0x93C, {0x0, 0x0, 0x0}, 0}, + {0x941, {0x0, 0x0, 0x0}, 0}, + {0x942, {0x0, 0x0, 0x0}, 0}, + {0x943, {0x0, 0x0, 0x0}, 0}, + {0x944, {0x0, 0x0, 0x0}, 0}, + {0x945, {0x0, 0x0, 0x0}, 0}, + {0x946, {0x0, 0x0, 0x0}, 0}, + {0x947, {0x0, 0x0, 0x0}, 0}, + {0x948, {0x0, 0x0, 0x0}, 0}, + {0x94D, {0x0, 0x0, 0x0}, 0}, + {0x951, {0x0, 0x0, 0x0}, 0}, + {0x952, {0x0, 0x0, 0x0}, 0}, + {0x953, {0x0, 0x0, 0x0}, 0}, + {0x954, {0x0, 0x0, 0x0}, 0}, + {0x955, {0x0, 0x0, 0x0}, 0}, + {0x956, {0x0, 0x0, 0x0}, 0}, + {0x957, {0x0, 0x0, 0x0}, 0}, + {0x958, {0x915, 0x0, 0x0}, 1}, + {0x959, {0x916, 0x0, 0x0}, 1}, + {0x95A, {0x917, 0x0, 0x0}, 1}, + {0x95B, {0x91C, 0x0, 0x0}, 1}, + {0x95C, {0x921, 0x0, 0x0}, 1}, + {0x95D, {0x922, 0x0, 0x0}, 1}, + {0x95E, {0x92B, 0x0, 0x0}, 1}, + {0x95F, {0x92F, 0x0, 0x0}, 1}, + {0x962, {0x0, 0x0, 0x0}, 0}, + {0x963, {0x0, 0x0, 0x0}, 0}, + {0x981, {0x0, 0x0, 0x0}, 0}, + {0x9BC, {0x0, 0x0, 0x0}, 0}, + {0x9C1, {0x0, 0x0, 0x0}, 0}, + {0x9C2, {0x0, 0x0, 0x0}, 0}, + {0x9C3, {0x0, 0x0, 0x0}, 0}, + {0x9C4, {0x0, 0x0, 0x0}, 0}, + {0x9CB, {0x9C7, 0x9BE, 0x0}, 2}, + {0x9CC, {0x9C7, 0x9D7, 0x0}, 2}, + {0x9CD, {0x0, 0x0, 0x0}, 0}, + {0x9DC, {0x9A1, 0x0, 0x0}, 1}, + {0x9DD, {0x9A2, 0x0, 0x0}, 1}, + {0x9DF, {0x9AF, 0x0, 0x0}, 1}, + {0x9E2, {0x0, 0x0, 0x0}, 0}, + {0x9E3, {0x0, 0x0, 0x0}, 0}, + {0x9FE, {0x0, 0x0, 0x0}, 0}, + {0xA01, {0x0, 0x0, 0x0}, 0}, + {0xA02, {0x0, 0x0, 0x0}, 0}, + {0xA33, {0xA32, 0x0, 0x0}, 1}, + {0xA36, {0xA38, 0x0, 0x0}, 1}, + {0xA3C, {0x0, 0x0, 0x0}, 0}, + {0xA41, {0x0, 0x0, 0x0}, 0}, + {0xA42, {0x0, 0x0, 0x0}, 0}, + {0xA47, {0x0, 0x0, 0x0}, 0}, + {0xA48, {0x0, 0x0, 0x0}, 0}, + {0xA4B, {0x0, 0x0, 0x0}, 0}, + {0xA4C, {0x0, 0x0, 0x0}, 0}, + {0xA4D, {0x0, 0x0, 0x0}, 0}, + {0xA51, {0x0, 0x0, 0x0}, 0}, + {0xA59, {0xA16, 0x0, 0x0}, 1}, + {0xA5A, {0xA17, 0x0, 0x0}, 1}, + {0xA5B, {0xA1C, 0x0, 0x0}, 1}, + {0xA5E, {0xA2B, 0x0, 0x0}, 1}, + {0xA70, {0x0, 0x0, 0x0}, 0}, + {0xA71, {0x0, 0x0, 0x0}, 0}, + {0xA75, {0x0, 0x0, 0x0}, 0}, + {0xA81, {0x0, 0x0, 0x0}, 0}, + {0xA82, {0x0, 0x0, 0x0}, 0}, + {0xABC, {0x0, 0x0, 0x0}, 0}, + {0xAC1, {0x0, 0x0, 0x0}, 0}, + {0xAC2, {0x0, 0x0, 0x0}, 0}, + {0xAC3, {0x0, 0x0, 0x0}, 0}, + {0xAC4, {0x0, 0x0, 0x0}, 0}, + {0xAC5, {0x0, 0x0, 0x0}, 0}, + {0xAC7, {0x0, 0x0, 0x0}, 0}, + {0xAC8, {0x0, 0x0, 0x0}, 0}, + {0xACD, {0x0, 0x0, 0x0}, 0}, + {0xAE2, {0x0, 0x0, 0x0}, 0}, + {0xAE3, {0x0, 0x0, 0x0}, 0}, + {0xAFA, {0x0, 0x0, 0x0}, 0}, + {0xAFB, {0x0, 0x0, 0x0}, 0}, + {0xAFC, {0x0, 0x0, 0x0}, 0}, + {0xAFD, {0x0, 0x0, 0x0}, 0}, + {0xAFE, {0x0, 0x0, 0x0}, 0}, + {0xAFF, {0x0, 0x0, 0x0}, 0}, + {0xB01, {0x0, 0x0, 0x0}, 0}, + {0xB3C, {0x0, 0x0, 0x0}, 0}, + {0xB3F, {0x0, 0x0, 0x0}, 0}, + {0xB41, {0x0, 0x0, 0x0}, 0}, + {0xB42, {0x0, 0x0, 0x0}, 0}, + {0xB43, {0x0, 0x0, 0x0}, 0}, + {0xB44, {0x0, 0x0, 0x0}, 0}, + {0xB48, {0xB47, 0x0, 0x0}, 1}, + {0xB4B, {0xB47, 0xB3E, 0x0}, 2}, + {0xB4C, {0xB47, 0xB57, 0x0}, 2}, + {0xB4D, {0x0, 0x0, 0x0}, 0}, + {0xB55, {0x0, 0x0, 0x0}, 0}, + {0xB56, {0x0, 0x0, 0x0}, 0}, + {0xB5C, {0xB21, 0x0, 0x0}, 1}, + {0xB5D, {0xB22, 0x0, 0x0}, 1}, + {0xB62, {0x0, 0x0, 0x0}, 0}, + {0xB63, {0x0, 0x0, 0x0}, 0}, + {0xB82, {0x0, 0x0, 0x0}, 0}, + {0xB94, {0xB92, 0xBD7, 0x0}, 2}, + {0xBC0, {0x0, 0x0, 0x0}, 0}, + {0xBCA, {0xBC6, 0xBBE, 0x0}, 2}, + {0xBCB, {0xBC7, 0xBBE, 0x0}, 2}, + {0xBCC, {0xBC6, 0xBD7, 0x0}, 2}, + {0xBCD, {0x0, 0x0, 0x0}, 0}, + {0xC00, {0x0, 0x0, 0x0}, 0}, + {0xC04, {0x0, 0x0, 0x0}, 0}, + {0xC3E, {0x0, 0x0, 0x0}, 0}, + {0xC3F, {0x0, 0x0, 0x0}, 0}, + {0xC40, {0x0, 0x0, 0x0}, 0}, + {0xC46, {0x0, 0x0, 0x0}, 0}, + {0xC47, {0x0, 0x0, 0x0}, 0}, + {0xC48, {0x0, 0x0, 0x0}, 0}, + {0xC4A, {0x0, 0x0, 0x0}, 0}, + {0xC4B, {0x0, 0x0, 0x0}, 0}, + {0xC4C, {0x0, 0x0, 0x0}, 0}, + {0xC4D, {0x0, 0x0, 0x0}, 0}, + {0xC55, {0x0, 0x0, 0x0}, 0}, + {0xC56, {0x0, 0x0, 0x0}, 0}, + {0xC62, {0x0, 0x0, 0x0}, 0}, + {0xC63, {0x0, 0x0, 0x0}, 0}, + {0xC81, {0x0, 0x0, 0x0}, 0}, + {0xCBC, {0x0, 0x0, 0x0}, 0}, + {0xCBF, {0x0, 0x0, 0x0}, 0}, + {0xCC0, {0xCD5, 0x0, 0x0}, 1}, + {0xCC6, {0x0, 0x0, 0x0}, 0}, + {0xCC7, {0xCD5, 0x0, 0x0}, 1}, + {0xCC8, {0xCD6, 0x0, 0x0}, 1}, + {0xCCA, {0xCC2, 0x0, 0x0}, 1}, + {0xCCB, {0xCC2, 0xCD5, 0x0}, 2}, + {0xCCC, {0x0, 0x0, 0x0}, 0}, + {0xCCD, {0x0, 0x0, 0x0}, 0}, + {0xCE2, {0x0, 0x0, 0x0}, 0}, + {0xCE3, {0x0, 0x0, 0x0}, 0}, + {0xD00, {0x0, 0x0, 0x0}, 0}, + {0xD01, {0x0, 0x0, 0x0}, 0}, + {0xD3B, {0x0, 0x0, 0x0}, 0}, + {0xD3C, {0x0, 0x0, 0x0}, 0}, + {0xD41, {0x0, 0x0, 0x0}, 0}, + {0xD42, {0x0, 0x0, 0x0}, 0}, + {0xD43, {0x0, 0x0, 0x0}, 0}, + {0xD44, {0x0, 0x0, 0x0}, 0}, + {0xD4A, {0xD46, 0xD3E, 0x0}, 2}, + {0xD4B, {0xD47, 0xD3E, 0x0}, 2}, + {0xD4C, {0xD46, 0xD57, 0x0}, 2}, + {0xD4D, {0x0, 0x0, 0x0}, 0}, + {0xD62, {0x0, 0x0, 0x0}, 0}, + {0xD63, {0x0, 0x0, 0x0}, 0}, + {0xD81, {0x0, 0x0, 0x0}, 0}, + {0xDCA, {0x0, 0x0, 0x0}, 0}, + {0xDD2, {0x0, 0x0, 0x0}, 0}, + {0xDD3, {0x0, 0x0, 0x0}, 0}, + {0xDD4, {0x0, 0x0, 0x0}, 0}, + {0xDD6, {0x0, 0x0, 0x0}, 0}, + {0xDDA, {0xDD9, 0x0, 0x0}, 1}, + {0xDDC, {0xDD9, 0xDCF, 0x0}, 2}, + {0xDDD, {0xDD9, 0xDCF, 0x0}, 2}, + {0xDDE, {0xDD9, 0xDDF, 0x0}, 2}, + {0xE31, {0x0, 0x0, 0x0}, 0}, + {0xE34, {0x0, 0x0, 0x0}, 0}, + {0xE35, {0x0, 0x0, 0x0}, 0}, + {0xE36, {0x0, 0x0, 0x0}, 0}, + {0xE37, {0x0, 0x0, 0x0}, 0}, + {0xE38, {0x0, 0x0, 0x0}, 0}, + {0xE39, {0x0, 0x0, 0x0}, 0}, + {0xE3A, {0x0, 0x0, 0x0}, 0}, + {0xE47, {0x0, 0x0, 0x0}, 0}, + {0xE48, {0x0, 0x0, 0x0}, 0}, + {0xE49, {0x0, 0x0, 0x0}, 0}, + {0xE4A, {0x0, 0x0, 0x0}, 0}, + {0xE4B, {0x0, 0x0, 0x0}, 0}, + {0xE4C, {0x0, 0x0, 0x0}, 0}, + {0xE4D, {0x0, 0x0, 0x0}, 0}, + {0xE4E, {0x0, 0x0, 0x0}, 0}, + {0xEB1, {0x0, 0x0, 0x0}, 0}, + {0xEB4, {0x0, 0x0, 0x0}, 0}, + {0xEB5, {0x0, 0x0, 0x0}, 0}, + {0xEB6, {0x0, 0x0, 0x0}, 0}, + {0xEB7, {0x0, 0x0, 0x0}, 0}, + {0xEB8, {0x0, 0x0, 0x0}, 0}, + {0xEB9, {0x0, 0x0, 0x0}, 0}, + {0xEBA, {0x0, 0x0, 0x0}, 0}, + {0xEBB, {0x0, 0x0, 0x0}, 0}, + {0xEBC, {0x0, 0x0, 0x0}, 0}, + {0xEC8, {0x0, 0x0, 0x0}, 0}, + {0xEC9, {0x0, 0x0, 0x0}, 0}, + {0xECA, {0x0, 0x0, 0x0}, 0}, + {0xECB, {0x0, 0x0, 0x0}, 0}, + {0xECC, {0x0, 0x0, 0x0}, 0}, + {0xECD, {0x0, 0x0, 0x0}, 0}, + {0xF18, {0x0, 0x0, 0x0}, 0}, + {0xF19, {0x0, 0x0, 0x0}, 0}, + {0xF35, {0x0, 0x0, 0x0}, 0}, + {0xF37, {0x0, 0x0, 0x0}, 0}, + {0xF39, {0x0, 0x0, 0x0}, 0}, + {0xF43, {0xF42, 0x0, 0x0}, 1}, + {0xF4D, {0xF4C, 0x0, 0x0}, 1}, + {0xF52, {0xF51, 0x0, 0x0}, 1}, + {0xF57, {0xF56, 0x0, 0x0}, 1}, + {0xF5C, {0xF5B, 0x0, 0x0}, 1}, + {0xF69, {0xF40, 0x0, 0x0}, 1}, + {0xF71, {0x0, 0x0, 0x0}, 0}, + {0xF72, {0x0, 0x0, 0x0}, 0}, + {0xF73, {0x0, 0x0, 0x0}, 0}, + {0xF74, {0x0, 0x0, 0x0}, 0}, + {0xF75, {0x0, 0x0, 0x0}, 0}, + {0xF76, {0x0, 0x0, 0x0}, 0}, + {0xF77, {0x0, 0x0, 0x0}, 0}, + {0xF78, {0x0, 0x0, 0x0}, 0}, + {0xF79, {0x0, 0x0, 0x0}, 0}, + {0xF7A, {0x0, 0x0, 0x0}, 0}, + {0xF7B, {0x0, 0x0, 0x0}, 0}, + {0xF7C, {0x0, 0x0, 0x0}, 0}, + {0xF7D, {0x0, 0x0, 0x0}, 0}, + {0xF7E, {0x0, 0x0, 0x0}, 0}, + {0xF80, {0x0, 0x0, 0x0}, 0}, + {0xF81, {0x0, 0x0, 0x0}, 0}, + {0xF82, {0x0, 0x0, 0x0}, 0}, + {0xF83, {0x0, 0x0, 0x0}, 0}, + {0xF84, {0x0, 0x0, 0x0}, 0}, + {0xF86, {0x0, 0x0, 0x0}, 0}, + {0xF87, {0x0, 0x0, 0x0}, 0}, + {0xF8D, {0x0, 0x0, 0x0}, 0}, + {0xF8E, {0x0, 0x0, 0x0}, 0}, + {0xF8F, {0x0, 0x0, 0x0}, 0}, + {0xF90, {0x0, 0x0, 0x0}, 0}, + {0xF91, {0x0, 0x0, 0x0}, 0}, + {0xF92, {0x0, 0x0, 0x0}, 0}, + {0xF93, {0x0, 0x0, 0x0}, 0}, + {0xF94, {0x0, 0x0, 0x0}, 0}, + {0xF95, {0x0, 0x0, 0x0}, 0}, + {0xF96, {0x0, 0x0, 0x0}, 0}, + {0xF97, {0x0, 0x0, 0x0}, 0}, + {0xF99, {0x0, 0x0, 0x0}, 0}, + {0xF9A, {0x0, 0x0, 0x0}, 0}, + {0xF9B, {0x0, 0x0, 0x0}, 0}, + {0xF9C, {0x0, 0x0, 0x0}, 0}, + {0xF9D, {0x0, 0x0, 0x0}, 0}, + {0xF9E, {0x0, 0x0, 0x0}, 0}, + {0xF9F, {0x0, 0x0, 0x0}, 0}, + {0xFA0, {0x0, 0x0, 0x0}, 0}, + {0xFA1, {0x0, 0x0, 0x0}, 0}, + {0xFA2, {0x0, 0x0, 0x0}, 0}, + {0xFA3, {0x0, 0x0, 0x0}, 0}, + {0xFA4, {0x0, 0x0, 0x0}, 0}, + {0xFA5, {0x0, 0x0, 0x0}, 0}, + {0xFA6, {0x0, 0x0, 0x0}, 0}, + {0xFA7, {0x0, 0x0, 0x0}, 0}, + {0xFA8, {0x0, 0x0, 0x0}, 0}, + {0xFA9, {0x0, 0x0, 0x0}, 0}, + {0xFAA, {0x0, 0x0, 0x0}, 0}, + {0xFAB, {0x0, 0x0, 0x0}, 0}, + {0xFAC, {0x0, 0x0, 0x0}, 0}, + {0xFAD, {0x0, 0x0, 0x0}, 0}, + {0xFAE, {0x0, 0x0, 0x0}, 0}, + {0xFAF, {0x0, 0x0, 0x0}, 0}, + {0xFB0, {0x0, 0x0, 0x0}, 0}, + {0xFB1, {0x0, 0x0, 0x0}, 0}, + {0xFB2, {0x0, 0x0, 0x0}, 0}, + {0xFB3, {0x0, 0x0, 0x0}, 0}, + {0xFB4, {0x0, 0x0, 0x0}, 0}, + {0xFB5, {0x0, 0x0, 0x0}, 0}, + {0xFB6, {0x0, 0x0, 0x0}, 0}, + {0xFB7, {0x0, 0x0, 0x0}, 0}, + {0xFB8, {0x0, 0x0, 0x0}, 0}, + {0xFB9, {0x0, 0x0, 0x0}, 0}, + {0xFBA, {0x0, 0x0, 0x0}, 0}, + {0xFBB, {0x0, 0x0, 0x0}, 0}, + {0xFBC, {0x0, 0x0, 0x0}, 0}, + {0xFC6, {0x0, 0x0, 0x0}, 0}, + {0x1026, {0x1025, 0x0, 0x0}, 1}, + {0x102D, {0x0, 0x0, 0x0}, 0}, + {0x102E, {0x0, 0x0, 0x0}, 0}, + {0x102F, {0x0, 0x0, 0x0}, 0}, + {0x1030, {0x0, 0x0, 0x0}, 0}, + {0x1032, {0x0, 0x0, 0x0}, 0}, + {0x1033, {0x0, 0x0, 0x0}, 0}, + {0x1034, {0x0, 0x0, 0x0}, 0}, + {0x1035, {0x0, 0x0, 0x0}, 0}, + {0x1036, {0x0, 0x0, 0x0}, 0}, + {0x1037, {0x0, 0x0, 0x0}, 0}, + {0x1039, {0x0, 0x0, 0x0}, 0}, + {0x103A, {0x0, 0x0, 0x0}, 0}, + {0x103D, {0x0, 0x0, 0x0}, 0}, + {0x103E, {0x0, 0x0, 0x0}, 0}, + {0x1058, {0x0, 0x0, 0x0}, 0}, + {0x1059, {0x0, 0x0, 0x0}, 0}, + {0x105E, {0x0, 0x0, 0x0}, 0}, + {0x105F, {0x0, 0x0, 0x0}, 0}, + {0x1060, {0x0, 0x0, 0x0}, 0}, + {0x1071, {0x0, 0x0, 0x0}, 0}, + {0x1072, {0x0, 0x0, 0x0}, 0}, + {0x1073, {0x0, 0x0, 0x0}, 0}, + {0x1074, {0x0, 0x0, 0x0}, 0}, + {0x1082, {0x0, 0x0, 0x0}, 0}, + {0x1085, {0x0, 0x0, 0x0}, 0}, + {0x1086, {0x0, 0x0, 0x0}, 0}, + {0x108D, {0x0, 0x0, 0x0}, 0}, + {0x109D, {0x0, 0x0, 0x0}, 0}, + {0x10A0, {0x2D00, 0x0, 0x0}, 1}, + {0x10A1, {0x2D01, 0x0, 0x0}, 1}, + {0x10A2, {0x2D02, 0x0, 0x0}, 1}, + {0x10A3, {0x2D03, 0x0, 0x0}, 1}, + {0x10A4, {0x2D04, 0x0, 0x0}, 1}, + {0x10A5, {0x2D05, 0x0, 0x0}, 1}, + {0x10A6, {0x2D06, 0x0, 0x0}, 1}, + {0x10A7, {0x2D07, 0x0, 0x0}, 1}, + {0x10A8, {0x2D08, 0x0, 0x0}, 1}, + {0x10A9, {0x2D09, 0x0, 0x0}, 1}, + {0x10AA, {0x2D0A, 0x0, 0x0}, 1}, + {0x10AB, {0x2D0B, 0x0, 0x0}, 1}, + {0x10AC, {0x2D0C, 0x0, 0x0}, 1}, + {0x10AD, {0x2D0D, 0x0, 0x0}, 1}, + {0x10AE, {0x2D0E, 0x0, 0x0}, 1}, + {0x10AF, {0x2D0F, 0x0, 0x0}, 1}, + {0x10B0, {0x2D10, 0x0, 0x0}, 1}, + {0x10B1, {0x2D11, 0x0, 0x0}, 1}, + {0x10B2, {0x2D12, 0x0, 0x0}, 1}, + {0x10B3, {0x2D13, 0x0, 0x0}, 1}, + {0x10B4, {0x2D14, 0x0, 0x0}, 1}, + {0x10B5, {0x2D15, 0x0, 0x0}, 1}, + {0x10B6, {0x2D16, 0x0, 0x0}, 1}, + {0x10B7, {0x2D17, 0x0, 0x0}, 1}, + {0x10B8, {0x2D18, 0x0, 0x0}, 1}, + {0x10B9, {0x2D19, 0x0, 0x0}, 1}, + {0x10BA, {0x2D1A, 0x0, 0x0}, 1}, + {0x10BB, {0x2D1B, 0x0, 0x0}, 1}, + {0x10BC, {0x2D1C, 0x0, 0x0}, 1}, + {0x10BD, {0x2D1D, 0x0, 0x0}, 1}, + {0x10BE, {0x2D1E, 0x0, 0x0}, 1}, + {0x10BF, {0x2D1F, 0x0, 0x0}, 1}, + {0x10C0, {0x2D20, 0x0, 0x0}, 1}, + {0x10C1, {0x2D21, 0x0, 0x0}, 1}, + {0x10C2, {0x2D22, 0x0, 0x0}, 1}, + {0x10C3, {0x2D23, 0x0, 0x0}, 1}, + {0x10C4, {0x2D24, 0x0, 0x0}, 1}, + {0x10C5, {0x2D25, 0x0, 0x0}, 1}, + {0x10C7, {0x2D27, 0x0, 0x0}, 1}, + {0x10CD, {0x2D2D, 0x0, 0x0}, 1}, + {0x135D, {0x0, 0x0, 0x0}, 0}, + {0x135E, {0x0, 0x0, 0x0}, 0}, + {0x135F, {0x0, 0x0, 0x0}, 0}, + {0x13A0, {0xAB70, 0x0, 0x0}, 1}, + {0x13A1, {0xAB71, 0x0, 0x0}, 1}, + {0x13A2, {0xAB72, 0x0, 0x0}, 1}, + {0x13A3, {0xAB73, 0x0, 0x0}, 1}, + {0x13A4, {0xAB74, 0x0, 0x0}, 1}, + {0x13A5, {0xAB75, 0x0, 0x0}, 1}, + {0x13A6, {0xAB76, 0x0, 0x0}, 1}, + {0x13A7, {0xAB77, 0x0, 0x0}, 1}, + {0x13A8, {0xAB78, 0x0, 0x0}, 1}, + {0x13A9, {0xAB79, 0x0, 0x0}, 1}, + {0x13AA, {0xAB7A, 0x0, 0x0}, 1}, + {0x13AB, {0xAB7B, 0x0, 0x0}, 1}, + {0x13AC, {0xAB7C, 0x0, 0x0}, 1}, + {0x13AD, {0xAB7D, 0x0, 0x0}, 1}, + {0x13AE, {0xAB7E, 0x0, 0x0}, 1}, + {0x13AF, {0xAB7F, 0x0, 0x0}, 1}, + {0x13B0, {0xAB80, 0x0, 0x0}, 1}, + {0x13B1, {0xAB81, 0x0, 0x0}, 1}, + {0x13B2, {0xAB82, 0x0, 0x0}, 1}, + {0x13B3, {0xAB83, 0x0, 0x0}, 1}, + {0x13B4, {0xAB84, 0x0, 0x0}, 1}, + {0x13B5, {0xAB85, 0x0, 0x0}, 1}, + {0x13B6, {0xAB86, 0x0, 0x0}, 1}, + {0x13B7, {0xAB87, 0x0, 0x0}, 1}, + {0x13B8, {0xAB88, 0x0, 0x0}, 1}, + {0x13B9, {0xAB89, 0x0, 0x0}, 1}, + {0x13BA, {0xAB8A, 0x0, 0x0}, 1}, + {0x13BB, {0xAB8B, 0x0, 0x0}, 1}, + {0x13BC, {0xAB8C, 0x0, 0x0}, 1}, + {0x13BD, {0xAB8D, 0x0, 0x0}, 1}, + {0x13BE, {0xAB8E, 0x0, 0x0}, 1}, + {0x13BF, {0xAB8F, 0x0, 0x0}, 1}, + {0x13C0, {0xAB90, 0x0, 0x0}, 1}, + {0x13C1, {0xAB91, 0x0, 0x0}, 1}, + {0x13C2, {0xAB92, 0x0, 0x0}, 1}, + {0x13C3, {0xAB93, 0x0, 0x0}, 1}, + {0x13C4, {0xAB94, 0x0, 0x0}, 1}, + {0x13C5, {0xAB95, 0x0, 0x0}, 1}, + {0x13C6, {0xAB96, 0x0, 0x0}, 1}, + {0x13C7, {0xAB97, 0x0, 0x0}, 1}, + {0x13C8, {0xAB98, 0x0, 0x0}, 1}, + {0x13C9, {0xAB99, 0x0, 0x0}, 1}, + {0x13CA, {0xAB9A, 0x0, 0x0}, 1}, + {0x13CB, {0xAB9B, 0x0, 0x0}, 1}, + {0x13CC, {0xAB9C, 0x0, 0x0}, 1}, + {0x13CD, {0xAB9D, 0x0, 0x0}, 1}, + {0x13CE, {0xAB9E, 0x0, 0x0}, 1}, + {0x13CF, {0xAB9F, 0x0, 0x0}, 1}, + {0x13D0, {0xABA0, 0x0, 0x0}, 1}, + {0x13D1, {0xABA1, 0x0, 0x0}, 1}, + {0x13D2, {0xABA2, 0x0, 0x0}, 1}, + {0x13D3, {0xABA3, 0x0, 0x0}, 1}, + {0x13D4, {0xABA4, 0x0, 0x0}, 1}, + {0x13D5, {0xABA5, 0x0, 0x0}, 1}, + {0x13D6, {0xABA6, 0x0, 0x0}, 1}, + {0x13D7, {0xABA7, 0x0, 0x0}, 1}, + {0x13D8, {0xABA8, 0x0, 0x0}, 1}, + {0x13D9, {0xABA9, 0x0, 0x0}, 1}, + {0x13DA, {0xABAA, 0x0, 0x0}, 1}, + {0x13DB, {0xABAB, 0x0, 0x0}, 1}, + {0x13DC, {0xABAC, 0x0, 0x0}, 1}, + {0x13DD, {0xABAD, 0x0, 0x0}, 1}, + {0x13DE, {0xABAE, 0x0, 0x0}, 1}, + {0x13DF, {0xABAF, 0x0, 0x0}, 1}, + {0x13E0, {0xABB0, 0x0, 0x0}, 1}, + {0x13E1, {0xABB1, 0x0, 0x0}, 1}, + {0x13E2, {0xABB2, 0x0, 0x0}, 1}, + {0x13E3, {0xABB3, 0x0, 0x0}, 1}, + {0x13E4, {0xABB4, 0x0, 0x0}, 1}, + {0x13E5, {0xABB5, 0x0, 0x0}, 1}, + {0x13E6, {0xABB6, 0x0, 0x0}, 1}, + {0x13E7, {0xABB7, 0x0, 0x0}, 1}, + {0x13E8, {0xABB8, 0x0, 0x0}, 1}, + {0x13E9, {0xABB9, 0x0, 0x0}, 1}, + {0x13EA, {0xABBA, 0x0, 0x0}, 1}, + {0x13EB, {0xABBB, 0x0, 0x0}, 1}, + {0x13EC, {0xABBC, 0x0, 0x0}, 1}, + {0x13ED, {0xABBD, 0x0, 0x0}, 1}, + {0x13EE, {0xABBE, 0x0, 0x0}, 1}, + {0x13EF, {0xABBF, 0x0, 0x0}, 1}, + {0x13F0, {0x13F8, 0x0, 0x0}, 1}, + {0x13F1, {0x13F9, 0x0, 0x0}, 1}, + {0x13F2, {0x13FA, 0x0, 0x0}, 1}, + {0x13F3, {0x13FB, 0x0, 0x0}, 1}, + {0x13F4, {0x13FC, 0x0, 0x0}, 1}, + {0x13F5, {0x13FD, 0x0, 0x0}, 1}, + {0x1712, {0x0, 0x0, 0x0}, 0}, + {0x1713, {0x0, 0x0, 0x0}, 0}, + {0x1714, {0x0, 0x0, 0x0}, 0}, + {0x1732, {0x0, 0x0, 0x0}, 0}, + {0x1733, {0x0, 0x0, 0x0}, 0}, + {0x1734, {0x0, 0x0, 0x0}, 0}, + {0x1752, {0x0, 0x0, 0x0}, 0}, + {0x1753, {0x0, 0x0, 0x0}, 0}, + {0x1772, {0x0, 0x0, 0x0}, 0}, + {0x1773, {0x0, 0x0, 0x0}, 0}, + {0x17B4, {0x0, 0x0, 0x0}, 0}, + {0x17B5, {0x0, 0x0, 0x0}, 0}, + {0x17B7, {0x0, 0x0, 0x0}, 0}, + {0x17B8, {0x0, 0x0, 0x0}, 0}, + {0x17B9, {0x0, 0x0, 0x0}, 0}, + {0x17BA, {0x0, 0x0, 0x0}, 0}, + {0x17BB, {0x0, 0x0, 0x0}, 0}, + {0x17BC, {0x0, 0x0, 0x0}, 0}, + {0x17BD, {0x0, 0x0, 0x0}, 0}, + {0x17C6, {0x0, 0x0, 0x0}, 0}, + {0x17C9, {0x0, 0x0, 0x0}, 0}, + {0x17CA, {0x0, 0x0, 0x0}, 0}, + {0x17CB, {0x0, 0x0, 0x0}, 0}, + {0x17CC, {0x0, 0x0, 0x0}, 0}, + {0x17CD, {0x0, 0x0, 0x0}, 0}, + {0x17CE, {0x0, 0x0, 0x0}, 0}, + {0x17CF, {0x0, 0x0, 0x0}, 0}, + {0x17D0, {0x0, 0x0, 0x0}, 0}, + {0x17D1, {0x0, 0x0, 0x0}, 0}, + {0x17D2, {0x0, 0x0, 0x0}, 0}, + {0x17D3, {0x0, 0x0, 0x0}, 0}, + {0x17DD, {0x0, 0x0, 0x0}, 0}, + {0x180B, {0x0, 0x0, 0x0}, 0}, + {0x180C, {0x0, 0x0, 0x0}, 0}, + {0x180D, {0x0, 0x0, 0x0}, 0}, + {0x1885, {0x0, 0x0, 0x0}, 0}, + {0x1886, {0x0, 0x0, 0x0}, 0}, + {0x18A9, {0x0, 0x0, 0x0}, 0}, + {0x1920, {0x0, 0x0, 0x0}, 0}, + {0x1921, {0x0, 0x0, 0x0}, 0}, + {0x1922, {0x0, 0x0, 0x0}, 0}, + {0x1927, {0x0, 0x0, 0x0}, 0}, + {0x1928, {0x0, 0x0, 0x0}, 0}, + {0x1932, {0x0, 0x0, 0x0}, 0}, + {0x1939, {0x0, 0x0, 0x0}, 0}, + {0x193A, {0x0, 0x0, 0x0}, 0}, + {0x193B, {0x0, 0x0, 0x0}, 0}, + {0x1A17, {0x0, 0x0, 0x0}, 0}, + {0x1A18, {0x0, 0x0, 0x0}, 0}, + {0x1A1B, {0x0, 0x0, 0x0}, 0}, + {0x1A56, {0x0, 0x0, 0x0}, 0}, + {0x1A58, {0x0, 0x0, 0x0}, 0}, + {0x1A59, {0x0, 0x0, 0x0}, 0}, + {0x1A5A, {0x0, 0x0, 0x0}, 0}, + {0x1A5B, {0x0, 0x0, 0x0}, 0}, + {0x1A5C, {0x0, 0x0, 0x0}, 0}, + {0x1A5D, {0x0, 0x0, 0x0}, 0}, + {0x1A5E, {0x0, 0x0, 0x0}, 0}, + {0x1A60, {0x0, 0x0, 0x0}, 0}, + {0x1A62, {0x0, 0x0, 0x0}, 0}, + {0x1A65, {0x0, 0x0, 0x0}, 0}, + {0x1A66, {0x0, 0x0, 0x0}, 0}, + {0x1A67, {0x0, 0x0, 0x0}, 0}, + {0x1A68, {0x0, 0x0, 0x0}, 0}, + {0x1A69, {0x0, 0x0, 0x0}, 0}, + {0x1A6A, {0x0, 0x0, 0x0}, 0}, + {0x1A6B, {0x0, 0x0, 0x0}, 0}, + {0x1A6C, {0x0, 0x0, 0x0}, 0}, + {0x1A73, {0x0, 0x0, 0x0}, 0}, + {0x1A74, {0x0, 0x0, 0x0}, 0}, + {0x1A75, {0x0, 0x0, 0x0}, 0}, + {0x1A76, {0x0, 0x0, 0x0}, 0}, + {0x1A77, {0x0, 0x0, 0x0}, 0}, + {0x1A78, {0x0, 0x0, 0x0}, 0}, + {0x1A79, {0x0, 0x0, 0x0}, 0}, + {0x1A7A, {0x0, 0x0, 0x0}, 0}, + {0x1A7B, {0x0, 0x0, 0x0}, 0}, + {0x1A7C, {0x0, 0x0, 0x0}, 0}, + {0x1A7F, {0x0, 0x0, 0x0}, 0}, + {0x1AB0, {0x0, 0x0, 0x0}, 0}, + {0x1AB1, {0x0, 0x0, 0x0}, 0}, + {0x1AB2, {0x0, 0x0, 0x0}, 0}, + {0x1AB3, {0x0, 0x0, 0x0}, 0}, + {0x1AB4, {0x0, 0x0, 0x0}, 0}, + {0x1AB5, {0x0, 0x0, 0x0}, 0}, + {0x1AB6, {0x0, 0x0, 0x0}, 0}, + {0x1AB7, {0x0, 0x0, 0x0}, 0}, + {0x1AB8, {0x0, 0x0, 0x0}, 0}, + {0x1AB9, {0x0, 0x0, 0x0}, 0}, + {0x1ABA, {0x0, 0x0, 0x0}, 0}, + {0x1ABB, {0x0, 0x0, 0x0}, 0}, + {0x1ABC, {0x0, 0x0, 0x0}, 0}, + {0x1ABD, {0x0, 0x0, 0x0}, 0}, + {0x1ABF, {0x0, 0x0, 0x0}, 0}, + {0x1AC0, {0x0, 0x0, 0x0}, 0}, + {0x1B00, {0x0, 0x0, 0x0}, 0}, + {0x1B01, {0x0, 0x0, 0x0}, 0}, + {0x1B02, {0x0, 0x0, 0x0}, 0}, + {0x1B03, {0x0, 0x0, 0x0}, 0}, + {0x1B06, {0x1B05, 0x1B35, 0x0}, 2}, + {0x1B08, {0x1B07, 0x1B35, 0x0}, 2}, + {0x1B0A, {0x1B09, 0x1B35, 0x0}, 2}, + {0x1B0C, {0x1B0B, 0x1B35, 0x0}, 2}, + {0x1B0E, {0x1B0D, 0x1B35, 0x0}, 2}, + {0x1B12, {0x1B11, 0x1B35, 0x0}, 2}, + {0x1B34, {0x0, 0x0, 0x0}, 0}, + {0x1B36, {0x0, 0x0, 0x0}, 0}, + {0x1B37, {0x0, 0x0, 0x0}, 0}, + {0x1B38, {0x0, 0x0, 0x0}, 0}, + {0x1B39, {0x0, 0x0, 0x0}, 0}, + {0x1B3A, {0x0, 0x0, 0x0}, 0}, + {0x1B3B, {0x1B35, 0x0, 0x0}, 1}, + {0x1B3C, {0x0, 0x0, 0x0}, 0}, + {0x1B3D, {0x1B35, 0x0, 0x0}, 1}, + {0x1B40, {0x1B3E, 0x1B35, 0x0}, 2}, + {0x1B41, {0x1B3F, 0x1B35, 0x0}, 2}, + {0x1B42, {0x0, 0x0, 0x0}, 0}, + {0x1B43, {0x1B35, 0x0, 0x0}, 1}, + {0x1B6B, {0x0, 0x0, 0x0}, 0}, + {0x1B6C, {0x0, 0x0, 0x0}, 0}, + {0x1B6D, {0x0, 0x0, 0x0}, 0}, + {0x1B6E, {0x0, 0x0, 0x0}, 0}, + {0x1B6F, {0x0, 0x0, 0x0}, 0}, + {0x1B70, {0x0, 0x0, 0x0}, 0}, + {0x1B71, {0x0, 0x0, 0x0}, 0}, + {0x1B72, {0x0, 0x0, 0x0}, 0}, + {0x1B73, {0x0, 0x0, 0x0}, 0}, + {0x1B80, {0x0, 0x0, 0x0}, 0}, + {0x1B81, {0x0, 0x0, 0x0}, 0}, + {0x1BA2, {0x0, 0x0, 0x0}, 0}, + {0x1BA3, {0x0, 0x0, 0x0}, 0}, + {0x1BA4, {0x0, 0x0, 0x0}, 0}, + {0x1BA5, {0x0, 0x0, 0x0}, 0}, + {0x1BA8, {0x0, 0x0, 0x0}, 0}, + {0x1BA9, {0x0, 0x0, 0x0}, 0}, + {0x1BAB, {0x0, 0x0, 0x0}, 0}, + {0x1BAC, {0x0, 0x0, 0x0}, 0}, + {0x1BAD, {0x0, 0x0, 0x0}, 0}, + {0x1BE6, {0x0, 0x0, 0x0}, 0}, + {0x1BE8, {0x0, 0x0, 0x0}, 0}, + {0x1BE9, {0x0, 0x0, 0x0}, 0}, + {0x1BED, {0x0, 0x0, 0x0}, 0}, + {0x1BEF, {0x0, 0x0, 0x0}, 0}, + {0x1BF0, {0x0, 0x0, 0x0}, 0}, + {0x1BF1, {0x0, 0x0, 0x0}, 0}, + {0x1C2C, {0x0, 0x0, 0x0}, 0}, + {0x1C2D, {0x0, 0x0, 0x0}, 0}, + {0x1C2E, {0x0, 0x0, 0x0}, 0}, + {0x1C2F, {0x0, 0x0, 0x0}, 0}, + {0x1C30, {0x0, 0x0, 0x0}, 0}, + {0x1C31, {0x0, 0x0, 0x0}, 0}, + {0x1C32, {0x0, 0x0, 0x0}, 0}, + {0x1C33, {0x0, 0x0, 0x0}, 0}, + {0x1C36, {0x0, 0x0, 0x0}, 0}, + {0x1C37, {0x0, 0x0, 0x0}, 0}, + {0x1C90, {0x10D0, 0x0, 0x0}, 1}, + {0x1C91, {0x10D1, 0x0, 0x0}, 1}, + {0x1C92, {0x10D2, 0x0, 0x0}, 1}, + {0x1C93, {0x10D3, 0x0, 0x0}, 1}, + {0x1C94, {0x10D4, 0x0, 0x0}, 1}, + {0x1C95, {0x10D5, 0x0, 0x0}, 1}, + {0x1C96, {0x10D6, 0x0, 0x0}, 1}, + {0x1C97, {0x10D7, 0x0, 0x0}, 1}, + {0x1C98, {0x10D8, 0x0, 0x0}, 1}, + {0x1C99, {0x10D9, 0x0, 0x0}, 1}, + {0x1C9A, {0x10DA, 0x0, 0x0}, 1}, + {0x1C9B, {0x10DB, 0x0, 0x0}, 1}, + {0x1C9C, {0x10DC, 0x0, 0x0}, 1}, + {0x1C9D, {0x10DD, 0x0, 0x0}, 1}, + {0x1C9E, {0x10DE, 0x0, 0x0}, 1}, + {0x1C9F, {0x10DF, 0x0, 0x0}, 1}, + {0x1CA0, {0x10E0, 0x0, 0x0}, 1}, + {0x1CA1, {0x10E1, 0x0, 0x0}, 1}, + {0x1CA2, {0x10E2, 0x0, 0x0}, 1}, + {0x1CA3, {0x10E3, 0x0, 0x0}, 1}, + {0x1CA4, {0x10E4, 0x0, 0x0}, 1}, + {0x1CA5, {0x10E5, 0x0, 0x0}, 1}, + {0x1CA6, {0x10E6, 0x0, 0x0}, 1}, + {0x1CA7, {0x10E7, 0x0, 0x0}, 1}, + {0x1CA8, {0x10E8, 0x0, 0x0}, 1}, + {0x1CA9, {0x10E9, 0x0, 0x0}, 1}, + {0x1CAA, {0x10EA, 0x0, 0x0}, 1}, + {0x1CAB, {0x10EB, 0x0, 0x0}, 1}, + {0x1CAC, {0x10EC, 0x0, 0x0}, 1}, + {0x1CAD, {0x10ED, 0x0, 0x0}, 1}, + {0x1CAE, {0x10EE, 0x0, 0x0}, 1}, + {0x1CAF, {0x10EF, 0x0, 0x0}, 1}, + {0x1CB0, {0x10F0, 0x0, 0x0}, 1}, + {0x1CB1, {0x10F1, 0x0, 0x0}, 1}, + {0x1CB2, {0x10F2, 0x0, 0x0}, 1}, + {0x1CB3, {0x10F3, 0x0, 0x0}, 1}, + {0x1CB4, {0x10F4, 0x0, 0x0}, 1}, + {0x1CB5, {0x10F5, 0x0, 0x0}, 1}, + {0x1CB6, {0x10F6, 0x0, 0x0}, 1}, + {0x1CB7, {0x10F7, 0x0, 0x0}, 1}, + {0x1CB8, {0x10F8, 0x0, 0x0}, 1}, + {0x1CB9, {0x10F9, 0x0, 0x0}, 1}, + {0x1CBA, {0x10FA, 0x0, 0x0}, 1}, + {0x1CBD, {0x10FD, 0x0, 0x0}, 1}, + {0x1CBE, {0x10FE, 0x0, 0x0}, 1}, + {0x1CBF, {0x10FF, 0x0, 0x0}, 1}, + {0x1CD0, {0x0, 0x0, 0x0}, 0}, + {0x1CD1, {0x0, 0x0, 0x0}, 0}, + {0x1CD2, {0x0, 0x0, 0x0}, 0}, + {0x1CD4, {0x0, 0x0, 0x0}, 0}, + {0x1CD5, {0x0, 0x0, 0x0}, 0}, + {0x1CD6, {0x0, 0x0, 0x0}, 0}, + {0x1CD7, {0x0, 0x0, 0x0}, 0}, + {0x1CD8, {0x0, 0x0, 0x0}, 0}, + {0x1CD9, {0x0, 0x0, 0x0}, 0}, + {0x1CDA, {0x0, 0x0, 0x0}, 0}, + {0x1CDB, {0x0, 0x0, 0x0}, 0}, + {0x1CDC, {0x0, 0x0, 0x0}, 0}, + {0x1CDD, {0x0, 0x0, 0x0}, 0}, + {0x1CDE, {0x0, 0x0, 0x0}, 0}, + {0x1CDF, {0x0, 0x0, 0x0}, 0}, + {0x1CE0, {0x0, 0x0, 0x0}, 0}, + {0x1CE2, {0x0, 0x0, 0x0}, 0}, + {0x1CE3, {0x0, 0x0, 0x0}, 0}, + {0x1CE4, {0x0, 0x0, 0x0}, 0}, + {0x1CE5, {0x0, 0x0, 0x0}, 0}, + {0x1CE6, {0x0, 0x0, 0x0}, 0}, + {0x1CE7, {0x0, 0x0, 0x0}, 0}, + {0x1CE8, {0x0, 0x0, 0x0}, 0}, + {0x1CED, {0x0, 0x0, 0x0}, 0}, + {0x1CF4, {0x0, 0x0, 0x0}, 0}, + {0x1CF8, {0x0, 0x0, 0x0}, 0}, + {0x1CF9, {0x0, 0x0, 0x0}, 0}, + {0x1DC0, {0x0, 0x0, 0x0}, 0}, + {0x1DC1, {0x0, 0x0, 0x0}, 0}, + {0x1DC2, {0x0, 0x0, 0x0}, 0}, + {0x1DC3, {0x0, 0x0, 0x0}, 0}, + {0x1DC4, {0x0, 0x0, 0x0}, 0}, + {0x1DC5, {0x0, 0x0, 0x0}, 0}, + {0x1DC6, {0x0, 0x0, 0x0}, 0}, + {0x1DC7, {0x0, 0x0, 0x0}, 0}, + {0x1DC8, {0x0, 0x0, 0x0}, 0}, + {0x1DC9, {0x0, 0x0, 0x0}, 0}, + {0x1DCA, {0x0, 0x0, 0x0}, 0}, + {0x1DCB, {0x0, 0x0, 0x0}, 0}, + {0x1DCC, {0x0, 0x0, 0x0}, 0}, + {0x1DCD, {0x0, 0x0, 0x0}, 0}, + {0x1DCE, {0x0, 0x0, 0x0}, 0}, + {0x1DCF, {0x0, 0x0, 0x0}, 0}, + {0x1DD0, {0x0, 0x0, 0x0}, 0}, + {0x1DD1, {0x0, 0x0, 0x0}, 0}, + {0x1DD2, {0x0, 0x0, 0x0}, 0}, + {0x1DD3, {0x0, 0x0, 0x0}, 0}, + {0x1DD4, {0x0, 0x0, 0x0}, 0}, + {0x1DD5, {0x0, 0x0, 0x0}, 0}, + {0x1DD6, {0x0, 0x0, 0x0}, 0}, + {0x1DD7, {0x0, 0x0, 0x0}, 0}, + {0x1DD8, {0x0, 0x0, 0x0}, 0}, + {0x1DD9, {0x0, 0x0, 0x0}, 0}, + {0x1DDA, {0x0, 0x0, 0x0}, 0}, + {0x1DDB, {0x0, 0x0, 0x0}, 0}, + {0x1DDC, {0x0, 0x0, 0x0}, 0}, + {0x1DDD, {0x0, 0x0, 0x0}, 0}, + {0x1DDE, {0x0, 0x0, 0x0}, 0}, + {0x1DDF, {0x0, 0x0, 0x0}, 0}, + {0x1DE0, {0x0, 0x0, 0x0}, 0}, + {0x1DE1, {0x0, 0x0, 0x0}, 0}, + {0x1DE2, {0x0, 0x0, 0x0}, 0}, + {0x1DE3, {0x0, 0x0, 0x0}, 0}, + {0x1DE4, {0x0, 0x0, 0x0}, 0}, + {0x1DE5, {0x0, 0x0, 0x0}, 0}, + {0x1DE6, {0x0, 0x0, 0x0}, 0}, + {0x1DE7, {0x0, 0x0, 0x0}, 0}, + {0x1DE8, {0x0, 0x0, 0x0}, 0}, + {0x1DE9, {0x0, 0x0, 0x0}, 0}, + {0x1DEA, {0x0, 0x0, 0x0}, 0}, + {0x1DEB, {0x0, 0x0, 0x0}, 0}, + {0x1DEC, {0x0, 0x0, 0x0}, 0}, + {0x1DED, {0x0, 0x0, 0x0}, 0}, + {0x1DEE, {0x0, 0x0, 0x0}, 0}, + {0x1DEF, {0x0, 0x0, 0x0}, 0}, + {0x1DF0, {0x0, 0x0, 0x0}, 0}, + {0x1DF1, {0x0, 0x0, 0x0}, 0}, + {0x1DF2, {0x0, 0x0, 0x0}, 0}, + {0x1DF3, {0x0, 0x0, 0x0}, 0}, + {0x1DF4, {0x0, 0x0, 0x0}, 0}, + {0x1DF5, {0x0, 0x0, 0x0}, 0}, + {0x1DF6, {0x0, 0x0, 0x0}, 0}, + {0x1DF7, {0x0, 0x0, 0x0}, 0}, + {0x1DF8, {0x0, 0x0, 0x0}, 0}, + {0x1DF9, {0x0, 0x0, 0x0}, 0}, + {0x1DFB, {0x0, 0x0, 0x0}, 0}, + {0x1DFC, {0x0, 0x0, 0x0}, 0}, + {0x1DFD, {0x0, 0x0, 0x0}, 0}, + {0x1DFE, {0x0, 0x0, 0x0}, 0}, + {0x1DFF, {0x0, 0x0, 0x0}, 0}, + {0x1E00, {0x61, 0x0, 0x0}, 1}, + {0x1E01, {0x61, 0x0, 0x0}, 1}, + {0x1E02, {0x62, 0x0, 0x0}, 1}, + {0x1E03, {0x62, 0x0, 0x0}, 1}, + {0x1E04, {0x62, 0x0, 0x0}, 1}, + {0x1E05, {0x62, 0x0, 0x0}, 1}, + {0x1E06, {0x62, 0x0, 0x0}, 1}, + {0x1E07, {0x62, 0x0, 0x0}, 1}, + {0x1E08, {0x63, 0x0, 0x0}, 1}, + {0x1E09, {0x63, 0x0, 0x0}, 1}, + {0x1E0A, {0x64, 0x0, 0x0}, 1}, + {0x1E0B, {0x64, 0x0, 0x0}, 1}, + {0x1E0C, {0x64, 0x0, 0x0}, 1}, + {0x1E0D, {0x64, 0x0, 0x0}, 1}, + {0x1E0E, {0x64, 0x0, 0x0}, 1}, + {0x1E0F, {0x64, 0x0, 0x0}, 1}, + {0x1E10, {0x64, 0x0, 0x0}, 1}, + {0x1E11, {0x64, 0x0, 0x0}, 1}, + {0x1E12, {0x64, 0x0, 0x0}, 1}, + {0x1E13, {0x64, 0x0, 0x0}, 1}, + {0x1E14, {0x65, 0x0, 0x0}, 1}, + {0x1E15, {0x65, 0x0, 0x0}, 1}, + {0x1E16, {0x65, 0x0, 0x0}, 1}, + {0x1E17, {0x65, 0x0, 0x0}, 1}, + {0x1E18, {0x65, 0x0, 0x0}, 1}, + {0x1E19, {0x65, 0x0, 0x0}, 1}, + {0x1E1A, {0x65, 0x0, 0x0}, 1}, + {0x1E1B, {0x65, 0x0, 0x0}, 1}, + {0x1E1C, {0x65, 0x0, 0x0}, 1}, + {0x1E1D, {0x65, 0x0, 0x0}, 1}, + {0x1E1E, {0x66, 0x0, 0x0}, 1}, + {0x1E1F, {0x66, 0x0, 0x0}, 1}, + {0x1E20, {0x67, 0x0, 0x0}, 1}, + {0x1E21, {0x67, 0x0, 0x0}, 1}, + {0x1E22, {0x68, 0x0, 0x0}, 1}, + {0x1E23, {0x68, 0x0, 0x0}, 1}, + {0x1E24, {0x68, 0x0, 0x0}, 1}, + {0x1E25, {0x68, 0x0, 0x0}, 1}, + {0x1E26, {0x68, 0x0, 0x0}, 1}, + {0x1E27, {0x68, 0x0, 0x0}, 1}, + {0x1E28, {0x68, 0x0, 0x0}, 1}, + {0x1E29, {0x68, 0x0, 0x0}, 1}, + {0x1E2A, {0x68, 0x0, 0x0}, 1}, + {0x1E2B, {0x68, 0x0, 0x0}, 1}, + {0x1E2C, {0x69, 0x0, 0x0}, 1}, + {0x1E2D, {0x69, 0x0, 0x0}, 1}, + {0x1E2E, {0x69, 0x0, 0x0}, 1}, + {0x1E2F, {0x69, 0x0, 0x0}, 1}, + {0x1E30, {0x6B, 0x0, 0x0}, 1}, + {0x1E31, {0x6B, 0x0, 0x0}, 1}, + {0x1E32, {0x6B, 0x0, 0x0}, 1}, + {0x1E33, {0x6B, 0x0, 0x0}, 1}, + {0x1E34, {0x6B, 0x0, 0x0}, 1}, + {0x1E35, {0x6B, 0x0, 0x0}, 1}, + {0x1E36, {0x6C, 0x0, 0x0}, 1}, + {0x1E37, {0x6C, 0x0, 0x0}, 1}, + {0x1E38, {0x6C, 0x0, 0x0}, 1}, + {0x1E39, {0x6C, 0x0, 0x0}, 1}, + {0x1E3A, {0x6C, 0x0, 0x0}, 1}, + {0x1E3B, {0x6C, 0x0, 0x0}, 1}, + {0x1E3C, {0x6C, 0x0, 0x0}, 1}, + {0x1E3D, {0x6C, 0x0, 0x0}, 1}, + {0x1E3E, {0x6D, 0x0, 0x0}, 1}, + {0x1E3F, {0x6D, 0x0, 0x0}, 1}, + {0x1E40, {0x6D, 0x0, 0x0}, 1}, + {0x1E41, {0x6D, 0x0, 0x0}, 1}, + {0x1E42, {0x6D, 0x0, 0x0}, 1}, + {0x1E43, {0x6D, 0x0, 0x0}, 1}, + {0x1E44, {0x6E, 0x0, 0x0}, 1}, + {0x1E45, {0x6E, 0x0, 0x0}, 1}, + {0x1E46, {0x6E, 0x0, 0x0}, 1}, + {0x1E47, {0x6E, 0x0, 0x0}, 1}, + {0x1E48, {0x6E, 0x0, 0x0}, 1}, + {0x1E49, {0x6E, 0x0, 0x0}, 1}, + {0x1E4A, {0x6E, 0x0, 0x0}, 1}, + {0x1E4B, {0x6E, 0x0, 0x0}, 1}, + {0x1E4C, {0x6F, 0x0, 0x0}, 1}, + {0x1E4D, {0x6F, 0x0, 0x0}, 1}, + {0x1E4E, {0x6F, 0x0, 0x0}, 1}, + {0x1E4F, {0x6F, 0x0, 0x0}, 1}, + {0x1E50, {0x6F, 0x0, 0x0}, 1}, + {0x1E51, {0x6F, 0x0, 0x0}, 1}, + {0x1E52, {0x6F, 0x0, 0x0}, 1}, + {0x1E53, {0x6F, 0x0, 0x0}, 1}, + {0x1E54, {0x70, 0x0, 0x0}, 1}, + {0x1E55, {0x70, 0x0, 0x0}, 1}, + {0x1E56, {0x70, 0x0, 0x0}, 1}, + {0x1E57, {0x70, 0x0, 0x0}, 1}, + {0x1E58, {0x72, 0x0, 0x0}, 1}, + {0x1E59, {0x72, 0x0, 0x0}, 1}, + {0x1E5A, {0x72, 0x0, 0x0}, 1}, + {0x1E5B, {0x72, 0x0, 0x0}, 1}, + {0x1E5C, {0x72, 0x0, 0x0}, 1}, + {0x1E5D, {0x72, 0x0, 0x0}, 1}, + {0x1E5E, {0x72, 0x0, 0x0}, 1}, + {0x1E5F, {0x72, 0x0, 0x0}, 1}, + {0x1E60, {0x73, 0x0, 0x0}, 1}, + {0x1E61, {0x73, 0x0, 0x0}, 1}, + {0x1E62, {0x73, 0x0, 0x0}, 1}, + {0x1E63, {0x73, 0x0, 0x0}, 1}, + {0x1E64, {0x73, 0x0, 0x0}, 1}, + {0x1E65, {0x73, 0x0, 0x0}, 1}, + {0x1E66, {0x73, 0x0, 0x0}, 1}, + {0x1E67, {0x73, 0x0, 0x0}, 1}, + {0x1E68, {0x73, 0x0, 0x0}, 1}, + {0x1E69, {0x73, 0x0, 0x0}, 1}, + {0x1E6A, {0x74, 0x0, 0x0}, 1}, + {0x1E6B, {0x74, 0x0, 0x0}, 1}, + {0x1E6C, {0x74, 0x0, 0x0}, 1}, + {0x1E6D, {0x74, 0x0, 0x0}, 1}, + {0x1E6E, {0x74, 0x0, 0x0}, 1}, + {0x1E6F, {0x74, 0x0, 0x0}, 1}, + {0x1E70, {0x74, 0x0, 0x0}, 1}, + {0x1E71, {0x74, 0x0, 0x0}, 1}, + {0x1E72, {0x75, 0x0, 0x0}, 1}, + {0x1E73, {0x75, 0x0, 0x0}, 1}, + {0x1E74, {0x75, 0x0, 0x0}, 1}, + {0x1E75, {0x75, 0x0, 0x0}, 1}, + {0x1E76, {0x75, 0x0, 0x0}, 1}, + {0x1E77, {0x75, 0x0, 0x0}, 1}, + {0x1E78, {0x75, 0x0, 0x0}, 1}, + {0x1E79, {0x75, 0x0, 0x0}, 1}, + {0x1E7A, {0x75, 0x0, 0x0}, 1}, + {0x1E7B, {0x75, 0x0, 0x0}, 1}, + {0x1E7C, {0x76, 0x0, 0x0}, 1}, + {0x1E7D, {0x76, 0x0, 0x0}, 1}, + {0x1E7E, {0x76, 0x0, 0x0}, 1}, + {0x1E7F, {0x76, 0x0, 0x0}, 1}, + {0x1E80, {0x77, 0x0, 0x0}, 1}, + {0x1E81, {0x77, 0x0, 0x0}, 1}, + {0x1E82, {0x77, 0x0, 0x0}, 1}, + {0x1E83, {0x77, 0x0, 0x0}, 1}, + {0x1E84, {0x77, 0x0, 0x0}, 1}, + {0x1E85, {0x77, 0x0, 0x0}, 1}, + {0x1E86, {0x77, 0x0, 0x0}, 1}, + {0x1E87, {0x77, 0x0, 0x0}, 1}, + {0x1E88, {0x77, 0x0, 0x0}, 1}, + {0x1E89, {0x77, 0x0, 0x0}, 1}, + {0x1E8A, {0x78, 0x0, 0x0}, 1}, + {0x1E8B, {0x78, 0x0, 0x0}, 1}, + {0x1E8C, {0x78, 0x0, 0x0}, 1}, + {0x1E8D, {0x78, 0x0, 0x0}, 1}, + {0x1E8E, {0x79, 0x0, 0x0}, 1}, + {0x1E8F, {0x79, 0x0, 0x0}, 1}, + {0x1E90, {0x7A, 0x0, 0x0}, 1}, + {0x1E91, {0x7A, 0x0, 0x0}, 1}, + {0x1E92, {0x7A, 0x0, 0x0}, 1}, + {0x1E93, {0x7A, 0x0, 0x0}, 1}, + {0x1E94, {0x7A, 0x0, 0x0}, 1}, + {0x1E95, {0x7A, 0x0, 0x0}, 1}, + {0x1E96, {0x68, 0x0, 0x0}, 1}, + {0x1E97, {0x74, 0x0, 0x0}, 1}, + {0x1E98, {0x77, 0x0, 0x0}, 1}, + {0x1E99, {0x79, 0x0, 0x0}, 1}, + {0x1E9B, {0x17F, 0x0, 0x0}, 1}, + {0x1E9E, {0xDF, 0x0, 0x0}, 1}, + {0x1EA0, {0x61, 0x0, 0x0}, 1}, + {0x1EA1, {0x61, 0x0, 0x0}, 1}, + {0x1EA2, {0x61, 0x0, 0x0}, 1}, + {0x1EA3, {0x61, 0x0, 0x0}, 1}, + {0x1EA4, {0x61, 0x0, 0x0}, 1}, + {0x1EA5, {0x61, 0x0, 0x0}, 1}, + {0x1EA6, {0x61, 0x0, 0x0}, 1}, + {0x1EA7, {0x61, 0x0, 0x0}, 1}, + {0x1EA8, {0x61, 0x0, 0x0}, 1}, + {0x1EA9, {0x61, 0x0, 0x0}, 1}, + {0x1EAA, {0x61, 0x0, 0x0}, 1}, + {0x1EAB, {0x61, 0x0, 0x0}, 1}, + {0x1EAC, {0x61, 0x0, 0x0}, 1}, + {0x1EAD, {0x61, 0x0, 0x0}, 1}, + {0x1EAE, {0x61, 0x0, 0x0}, 1}, + {0x1EAF, {0x61, 0x0, 0x0}, 1}, + {0x1EB0, {0x61, 0x0, 0x0}, 1}, + {0x1EB1, {0x61, 0x0, 0x0}, 1}, + {0x1EB2, {0x61, 0x0, 0x0}, 1}, + {0x1EB3, {0x61, 0x0, 0x0}, 1}, + {0x1EB4, {0x61, 0x0, 0x0}, 1}, + {0x1EB5, {0x61, 0x0, 0x0}, 1}, + {0x1EB6, {0x61, 0x0, 0x0}, 1}, + {0x1EB7, {0x61, 0x0, 0x0}, 1}, + {0x1EB8, {0x65, 0x0, 0x0}, 1}, + {0x1EB9, {0x65, 0x0, 0x0}, 1}, + {0x1EBA, {0x65, 0x0, 0x0}, 1}, + {0x1EBB, {0x65, 0x0, 0x0}, 1}, + {0x1EBC, {0x65, 0x0, 0x0}, 1}, + {0x1EBD, {0x65, 0x0, 0x0}, 1}, + {0x1EBE, {0x65, 0x0, 0x0}, 1}, + {0x1EBF, {0x65, 0x0, 0x0}, 1}, + {0x1EC0, {0x65, 0x0, 0x0}, 1}, + {0x1EC1, {0x65, 0x0, 0x0}, 1}, + {0x1EC2, {0x65, 0x0, 0x0}, 1}, + {0x1EC3, {0x65, 0x0, 0x0}, 1}, + {0x1EC4, {0x65, 0x0, 0x0}, 1}, + {0x1EC5, {0x65, 0x0, 0x0}, 1}, + {0x1EC6, {0x65, 0x0, 0x0}, 1}, + {0x1EC7, {0x65, 0x0, 0x0}, 1}, + {0x1EC8, {0x69, 0x0, 0x0}, 1}, + {0x1EC9, {0x69, 0x0, 0x0}, 1}, + {0x1ECA, {0x69, 0x0, 0x0}, 1}, + {0x1ECB, {0x69, 0x0, 0x0}, 1}, + {0x1ECC, {0x6F, 0x0, 0x0}, 1}, + {0x1ECD, {0x6F, 0x0, 0x0}, 1}, + {0x1ECE, {0x6F, 0x0, 0x0}, 1}, + {0x1ECF, {0x6F, 0x0, 0x0}, 1}, + {0x1ED0, {0x6F, 0x0, 0x0}, 1}, + {0x1ED1, {0x6F, 0x0, 0x0}, 1}, + {0x1ED2, {0x6F, 0x0, 0x0}, 1}, + {0x1ED3, {0x6F, 0x0, 0x0}, 1}, + {0x1ED4, {0x6F, 0x0, 0x0}, 1}, + {0x1ED5, {0x6F, 0x0, 0x0}, 1}, + {0x1ED6, {0x6F, 0x0, 0x0}, 1}, + {0x1ED7, {0x6F, 0x0, 0x0}, 1}, + {0x1ED8, {0x6F, 0x0, 0x0}, 1}, + {0x1ED9, {0x6F, 0x0, 0x0}, 1}, + {0x1EDA, {0x6F, 0x0, 0x0}, 1}, + {0x1EDB, {0x6F, 0x0, 0x0}, 1}, + {0x1EDC, {0x6F, 0x0, 0x0}, 1}, + {0x1EDD, {0x6F, 0x0, 0x0}, 1}, + {0x1EDE, {0x6F, 0x0, 0x0}, 1}, + {0x1EDF, {0x6F, 0x0, 0x0}, 1}, + {0x1EE0, {0x6F, 0x0, 0x0}, 1}, + {0x1EE1, {0x6F, 0x0, 0x0}, 1}, + {0x1EE2, {0x6F, 0x0, 0x0}, 1}, + {0x1EE3, {0x6F, 0x0, 0x0}, 1}, + {0x1EE4, {0x75, 0x0, 0x0}, 1}, + {0x1EE5, {0x75, 0x0, 0x0}, 1}, + {0x1EE6, {0x75, 0x0, 0x0}, 1}, + {0x1EE7, {0x75, 0x0, 0x0}, 1}, + {0x1EE8, {0x75, 0x0, 0x0}, 1}, + {0x1EE9, {0x75, 0x0, 0x0}, 1}, + {0x1EEA, {0x75, 0x0, 0x0}, 1}, + {0x1EEB, {0x75, 0x0, 0x0}, 1}, + {0x1EEC, {0x75, 0x0, 0x0}, 1}, + {0x1EED, {0x75, 0x0, 0x0}, 1}, + {0x1EEE, {0x75, 0x0, 0x0}, 1}, + {0x1EEF, {0x75, 0x0, 0x0}, 1}, + {0x1EF0, {0x75, 0x0, 0x0}, 1}, + {0x1EF1, {0x75, 0x0, 0x0}, 1}, + {0x1EF2, {0x79, 0x0, 0x0}, 1}, + {0x1EF3, {0x79, 0x0, 0x0}, 1}, + {0x1EF4, {0x79, 0x0, 0x0}, 1}, + {0x1EF5, {0x79, 0x0, 0x0}, 1}, + {0x1EF6, {0x79, 0x0, 0x0}, 1}, + {0x1EF7, {0x79, 0x0, 0x0}, 1}, + {0x1EF8, {0x79, 0x0, 0x0}, 1}, + {0x1EF9, {0x79, 0x0, 0x0}, 1}, + {0x1EFA, {0x1EFB, 0x0, 0x0}, 1}, + {0x1EFC, {0x1EFD, 0x0, 0x0}, 1}, + {0x1EFE, {0x1EFF, 0x0, 0x0}, 1}, + {0x1F00, {0x3B1, 0x0, 0x0}, 1}, + {0x1F01, {0x3B1, 0x0, 0x0}, 1}, + {0x1F02, {0x3B1, 0x0, 0x0}, 1}, + {0x1F03, {0x3B1, 0x0, 0x0}, 1}, + {0x1F04, {0x3B1, 0x0, 0x0}, 1}, + {0x1F05, {0x3B1, 0x0, 0x0}, 1}, + {0x1F06, {0x3B1, 0x0, 0x0}, 1}, + {0x1F07, {0x3B1, 0x0, 0x0}, 1}, + {0x1F08, {0x3B1, 0x0, 0x0}, 1}, + {0x1F09, {0x3B1, 0x0, 0x0}, 1}, + {0x1F0A, {0x3B1, 0x0, 0x0}, 1}, + {0x1F0B, {0x3B1, 0x0, 0x0}, 1}, + {0x1F0C, {0x3B1, 0x0, 0x0}, 1}, + {0x1F0D, {0x3B1, 0x0, 0x0}, 1}, + {0x1F0E, {0x3B1, 0x0, 0x0}, 1}, + {0x1F0F, {0x3B1, 0x0, 0x0}, 1}, + {0x1F10, {0x3B5, 0x0, 0x0}, 1}, + {0x1F11, {0x3B5, 0x0, 0x0}, 1}, + {0x1F12, {0x3B5, 0x0, 0x0}, 1}, + {0x1F13, {0x3B5, 0x0, 0x0}, 1}, + {0x1F14, {0x3B5, 0x0, 0x0}, 1}, + {0x1F15, {0x3B5, 0x0, 0x0}, 1}, + {0x1F18, {0x3B5, 0x0, 0x0}, 1}, + {0x1F19, {0x3B5, 0x0, 0x0}, 1}, + {0x1F1A, {0x3B5, 0x0, 0x0}, 1}, + {0x1F1B, {0x3B5, 0x0, 0x0}, 1}, + {0x1F1C, {0x3B5, 0x0, 0x0}, 1}, + {0x1F1D, {0x3B5, 0x0, 0x0}, 1}, + {0x1F20, {0x3B7, 0x0, 0x0}, 1}, + {0x1F21, {0x3B7, 0x0, 0x0}, 1}, + {0x1F22, {0x3B7, 0x0, 0x0}, 1}, + {0x1F23, {0x3B7, 0x0, 0x0}, 1}, + {0x1F24, {0x3B7, 0x0, 0x0}, 1}, + {0x1F25, {0x3B7, 0x0, 0x0}, 1}, + {0x1F26, {0x3B7, 0x0, 0x0}, 1}, + {0x1F27, {0x3B7, 0x0, 0x0}, 1}, + {0x1F28, {0x3B7, 0x0, 0x0}, 1}, + {0x1F29, {0x3B7, 0x0, 0x0}, 1}, + {0x1F2A, {0x3B7, 0x0, 0x0}, 1}, + {0x1F2B, {0x3B7, 0x0, 0x0}, 1}, + {0x1F2C, {0x3B7, 0x0, 0x0}, 1}, + {0x1F2D, {0x3B7, 0x0, 0x0}, 1}, + {0x1F2E, {0x3B7, 0x0, 0x0}, 1}, + {0x1F2F, {0x3B7, 0x0, 0x0}, 1}, + {0x1F30, {0x3B9, 0x0, 0x0}, 1}, + {0x1F31, {0x3B9, 0x0, 0x0}, 1}, + {0x1F32, {0x3B9, 0x0, 0x0}, 1}, + {0x1F33, {0x3B9, 0x0, 0x0}, 1}, + {0x1F34, {0x3B9, 0x0, 0x0}, 1}, + {0x1F35, {0x3B9, 0x0, 0x0}, 1}, + {0x1F36, {0x3B9, 0x0, 0x0}, 1}, + {0x1F37, {0x3B9, 0x0, 0x0}, 1}, + {0x1F38, {0x3B9, 0x0, 0x0}, 1}, + {0x1F39, {0x3B9, 0x0, 0x0}, 1}, + {0x1F3A, {0x3B9, 0x0, 0x0}, 1}, + {0x1F3B, {0x3B9, 0x0, 0x0}, 1}, + {0x1F3C, {0x3B9, 0x0, 0x0}, 1}, + {0x1F3D, {0x3B9, 0x0, 0x0}, 1}, + {0x1F3E, {0x3B9, 0x0, 0x0}, 1}, + {0x1F3F, {0x3B9, 0x0, 0x0}, 1}, + {0x1F40, {0x3BF, 0x0, 0x0}, 1}, + {0x1F41, {0x3BF, 0x0, 0x0}, 1}, + {0x1F42, {0x3BF, 0x0, 0x0}, 1}, + {0x1F43, {0x3BF, 0x0, 0x0}, 1}, + {0x1F44, {0x3BF, 0x0, 0x0}, 1}, + {0x1F45, {0x3BF, 0x0, 0x0}, 1}, + {0x1F48, {0x3BF, 0x0, 0x0}, 1}, + {0x1F49, {0x3BF, 0x0, 0x0}, 1}, + {0x1F4A, {0x3BF, 0x0, 0x0}, 1}, + {0x1F4B, {0x3BF, 0x0, 0x0}, 1}, + {0x1F4C, {0x3BF, 0x0, 0x0}, 1}, + {0x1F4D, {0x3BF, 0x0, 0x0}, 1}, + {0x1F50, {0x3C5, 0x0, 0x0}, 1}, + {0x1F51, {0x3C5, 0x0, 0x0}, 1}, + {0x1F52, {0x3C5, 0x0, 0x0}, 1}, + {0x1F53, {0x3C5, 0x0, 0x0}, 1}, + {0x1F54, {0x3C5, 0x0, 0x0}, 1}, + {0x1F55, {0x3C5, 0x0, 0x0}, 1}, + {0x1F56, {0x3C5, 0x0, 0x0}, 1}, + {0x1F57, {0x3C5, 0x0, 0x0}, 1}, + {0x1F59, {0x3C5, 0x0, 0x0}, 1}, + {0x1F5B, {0x3C5, 0x0, 0x0}, 1}, + {0x1F5D, {0x3C5, 0x0, 0x0}, 1}, + {0x1F5F, {0x3C5, 0x0, 0x0}, 1}, + {0x1F60, {0x3C9, 0x0, 0x0}, 1}, + {0x1F61, {0x3C9, 0x0, 0x0}, 1}, + {0x1F62, {0x3C9, 0x0, 0x0}, 1}, + {0x1F63, {0x3C9, 0x0, 0x0}, 1}, + {0x1F64, {0x3C9, 0x0, 0x0}, 1}, + {0x1F65, {0x3C9, 0x0, 0x0}, 1}, + {0x1F66, {0x3C9, 0x0, 0x0}, 1}, + {0x1F67, {0x3C9, 0x0, 0x0}, 1}, + {0x1F68, {0x3C9, 0x0, 0x0}, 1}, + {0x1F69, {0x3C9, 0x0, 0x0}, 1}, + {0x1F6A, {0x3C9, 0x0, 0x0}, 1}, + {0x1F6B, {0x3C9, 0x0, 0x0}, 1}, + {0x1F6C, {0x3C9, 0x0, 0x0}, 1}, + {0x1F6D, {0x3C9, 0x0, 0x0}, 1}, + {0x1F6E, {0x3C9, 0x0, 0x0}, 1}, + {0x1F6F, {0x3C9, 0x0, 0x0}, 1}, + {0x1F70, {0x3B1, 0x0, 0x0}, 1}, + {0x1F71, {0x3B1, 0x0, 0x0}, 1}, + {0x1F72, {0x3B5, 0x0, 0x0}, 1}, + {0x1F73, {0x3B5, 0x0, 0x0}, 1}, + {0x1F74, {0x3B7, 0x0, 0x0}, 1}, + {0x1F75, {0x3B7, 0x0, 0x0}, 1}, + {0x1F76, {0x3B9, 0x0, 0x0}, 1}, + {0x1F77, {0x3B9, 0x0, 0x0}, 1}, + {0x1F78, {0x3BF, 0x0, 0x0}, 1}, + {0x1F79, {0x3BF, 0x0, 0x0}, 1}, + {0x1F7A, {0x3C5, 0x0, 0x0}, 1}, + {0x1F7B, {0x3C5, 0x0, 0x0}, 1}, + {0x1F7C, {0x3C9, 0x0, 0x0}, 1}, + {0x1F7D, {0x3C9, 0x0, 0x0}, 1}, + {0x1F80, {0x3B1, 0x0, 0x0}, 1}, + {0x1F81, {0x3B1, 0x0, 0x0}, 1}, + {0x1F82, {0x3B1, 0x0, 0x0}, 1}, + {0x1F83, {0x3B1, 0x0, 0x0}, 1}, + {0x1F84, {0x3B1, 0x0, 0x0}, 1}, + {0x1F85, {0x3B1, 0x0, 0x0}, 1}, + {0x1F86, {0x3B1, 0x0, 0x0}, 1}, + {0x1F87, {0x3B1, 0x0, 0x0}, 1}, + {0x1F88, {0x3B1, 0x0, 0x0}, 1}, + {0x1F89, {0x3B1, 0x0, 0x0}, 1}, + {0x1F8A, {0x3B1, 0x0, 0x0}, 1}, + {0x1F8B, {0x3B1, 0x0, 0x0}, 1}, + {0x1F8C, {0x3B1, 0x0, 0x0}, 1}, + {0x1F8D, {0x3B1, 0x0, 0x0}, 1}, + {0x1F8E, {0x3B1, 0x0, 0x0}, 1}, + {0x1F8F, {0x3B1, 0x0, 0x0}, 1}, + {0x1F90, {0x3B7, 0x0, 0x0}, 1}, + {0x1F91, {0x3B7, 0x0, 0x0}, 1}, + {0x1F92, {0x3B7, 0x0, 0x0}, 1}, + {0x1F93, {0x3B7, 0x0, 0x0}, 1}, + {0x1F94, {0x3B7, 0x0, 0x0}, 1}, + {0x1F95, {0x3B7, 0x0, 0x0}, 1}, + {0x1F96, {0x3B7, 0x0, 0x0}, 1}, + {0x1F97, {0x3B7, 0x0, 0x0}, 1}, + {0x1F98, {0x3B7, 0x0, 0x0}, 1}, + {0x1F99, {0x3B7, 0x0, 0x0}, 1}, + {0x1F9A, {0x3B7, 0x0, 0x0}, 1}, + {0x1F9B, {0x3B7, 0x0, 0x0}, 1}, + {0x1F9C, {0x3B7, 0x0, 0x0}, 1}, + {0x1F9D, {0x3B7, 0x0, 0x0}, 1}, + {0x1F9E, {0x3B7, 0x0, 0x0}, 1}, + {0x1F9F, {0x3B7, 0x0, 0x0}, 1}, + {0x1FA0, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA1, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA2, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA3, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA4, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA5, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA6, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA7, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA8, {0x3C9, 0x0, 0x0}, 1}, + {0x1FA9, {0x3C9, 0x0, 0x0}, 1}, + {0x1FAA, {0x3C9, 0x0, 0x0}, 1}, + {0x1FAB, {0x3C9, 0x0, 0x0}, 1}, + {0x1FAC, {0x3C9, 0x0, 0x0}, 1}, + {0x1FAD, {0x3C9, 0x0, 0x0}, 1}, + {0x1FAE, {0x3C9, 0x0, 0x0}, 1}, + {0x1FAF, {0x3C9, 0x0, 0x0}, 1}, + {0x1FB0, {0x3B1, 0x0, 0x0}, 1}, + {0x1FB1, {0x3B1, 0x0, 0x0}, 1}, + {0x1FB2, {0x3B1, 0x0, 0x0}, 1}, + {0x1FB3, {0x3B1, 0x0, 0x0}, 1}, + {0x1FB4, {0x3B1, 0x0, 0x0}, 1}, + {0x1FB6, {0x3B1, 0x0, 0x0}, 1}, + {0x1FB7, {0x3B1, 0x0, 0x0}, 1}, + {0x1FB8, {0x3B1, 0x0, 0x0}, 1}, + {0x1FB9, {0x3B1, 0x0, 0x0}, 1}, + {0x1FBA, {0x3B1, 0x0, 0x0}, 1}, + {0x1FBB, {0x3B1, 0x0, 0x0}, 1}, + {0x1FBC, {0x3B1, 0x0, 0x0}, 1}, + {0x1FBE, {0x3B9, 0x0, 0x0}, 1}, + {0x1FC1, {0xA8, 0x0, 0x0}, 1}, + {0x1FC2, {0x3B7, 0x0, 0x0}, 1}, + {0x1FC3, {0x3B7, 0x0, 0x0}, 1}, + {0x1FC4, {0x3B7, 0x0, 0x0}, 1}, + {0x1FC6, {0x3B7, 0x0, 0x0}, 1}, + {0x1FC7, {0x3B7, 0x0, 0x0}, 1}, + {0x1FC8, {0x3B5, 0x0, 0x0}, 1}, + {0x1FC9, {0x3B5, 0x0, 0x0}, 1}, + {0x1FCA, {0x3B7, 0x0, 0x0}, 1}, + {0x1FCB, {0x3B7, 0x0, 0x0}, 1}, + {0x1FCC, {0x3B7, 0x0, 0x0}, 1}, + {0x1FCD, {0x1FBF, 0x0, 0x0}, 1}, + {0x1FCE, {0x1FBF, 0x0, 0x0}, 1}, + {0x1FCF, {0x1FBF, 0x0, 0x0}, 1}, + {0x1FD0, {0x3B9, 0x0, 0x0}, 1}, + {0x1FD1, {0x3B9, 0x0, 0x0}, 1}, + {0x1FD2, {0x3B9, 0x0, 0x0}, 1}, + {0x1FD3, {0x3B9, 0x0, 0x0}, 1}, + {0x1FD6, {0x3B9, 0x0, 0x0}, 1}, + {0x1FD7, {0x3B9, 0x0, 0x0}, 1}, + {0x1FD8, {0x3B9, 0x0, 0x0}, 1}, + {0x1FD9, {0x3B9, 0x0, 0x0}, 1}, + {0x1FDA, {0x3B9, 0x0, 0x0}, 1}, + {0x1FDB, {0x3B9, 0x0, 0x0}, 1}, + {0x1FDD, {0x1FFE, 0x0, 0x0}, 1}, + {0x1FDE, {0x1FFE, 0x0, 0x0}, 1}, + {0x1FDF, {0x1FFE, 0x0, 0x0}, 1}, + {0x1FE0, {0x3C5, 0x0, 0x0}, 1}, + {0x1FE1, {0x3C5, 0x0, 0x0}, 1}, + {0x1FE2, {0x3C5, 0x0, 0x0}, 1}, + {0x1FE3, {0x3C5, 0x0, 0x0}, 1}, + {0x1FE4, {0x3C1, 0x0, 0x0}, 1}, + {0x1FE5, {0x3C1, 0x0, 0x0}, 1}, + {0x1FE6, {0x3C5, 0x0, 0x0}, 1}, + {0x1FE7, {0x3C5, 0x0, 0x0}, 1}, + {0x1FE8, {0x3C5, 0x0, 0x0}, 1}, + {0x1FE9, {0x3C5, 0x0, 0x0}, 1}, + {0x1FEA, {0x3C5, 0x0, 0x0}, 1}, + {0x1FEB, {0x3C5, 0x0, 0x0}, 1}, + {0x1FEC, {0x3C1, 0x0, 0x0}, 1}, + {0x1FED, {0xA8, 0x0, 0x0}, 1}, + {0x1FEE, {0xA8, 0x0, 0x0}, 1}, + {0x1FEF, {0x60, 0x0, 0x0}, 1}, + {0x1FF2, {0x3C9, 0x0, 0x0}, 1}, + {0x1FF3, {0x3C9, 0x0, 0x0}, 1}, + {0x1FF4, {0x3C9, 0x0, 0x0}, 1}, + {0x1FF6, {0x3C9, 0x0, 0x0}, 1}, + {0x1FF7, {0x3C9, 0x0, 0x0}, 1}, + {0x1FF8, {0x3BF, 0x0, 0x0}, 1}, + {0x1FF9, {0x3BF, 0x0, 0x0}, 1}, + {0x1FFA, {0x3C9, 0x0, 0x0}, 1}, + {0x1FFB, {0x3C9, 0x0, 0x0}, 1}, + {0x1FFC, {0x3C9, 0x0, 0x0}, 1}, + {0x1FFD, {0xB4, 0x0, 0x0}, 1}, + {0x2000, {0x2002, 0x0, 0x0}, 1}, + {0x2001, {0x2003, 0x0, 0x0}, 1}, + {0x20D0, {0x0, 0x0, 0x0}, 0}, + {0x20D1, {0x0, 0x0, 0x0}, 0}, + {0x20D2, {0x0, 0x0, 0x0}, 0}, + {0x20D3, {0x0, 0x0, 0x0}, 0}, + {0x20D4, {0x0, 0x0, 0x0}, 0}, + {0x20D5, {0x0, 0x0, 0x0}, 0}, + {0x20D6, {0x0, 0x0, 0x0}, 0}, + {0x20D7, {0x0, 0x0, 0x0}, 0}, + {0x20D8, {0x0, 0x0, 0x0}, 0}, + {0x20D9, {0x0, 0x0, 0x0}, 0}, + {0x20DA, {0x0, 0x0, 0x0}, 0}, + {0x20DB, {0x0, 0x0, 0x0}, 0}, + {0x20DC, {0x0, 0x0, 0x0}, 0}, + {0x20E1, {0x0, 0x0, 0x0}, 0}, + {0x20E5, {0x0, 0x0, 0x0}, 0}, + {0x20E6, {0x0, 0x0, 0x0}, 0}, + {0x20E7, {0x0, 0x0, 0x0}, 0}, + {0x20E8, {0x0, 0x0, 0x0}, 0}, + {0x20E9, {0x0, 0x0, 0x0}, 0}, + {0x20EA, {0x0, 0x0, 0x0}, 0}, + {0x20EB, {0x0, 0x0, 0x0}, 0}, + {0x20EC, {0x0, 0x0, 0x0}, 0}, + {0x20ED, {0x0, 0x0, 0x0}, 0}, + {0x20EE, {0x0, 0x0, 0x0}, 0}, + {0x20EF, {0x0, 0x0, 0x0}, 0}, + {0x20F0, {0x0, 0x0, 0x0}, 0}, + {0x2126, {0x3C9, 0x0, 0x0}, 1}, + {0x212A, {0x6B, 0x0, 0x0}, 1}, + {0x212B, {0x61, 0x0, 0x0}, 1}, + {0x2132, {0x214E, 0x0, 0x0}, 1}, + {0x2160, {0x2170, 0x0, 0x0}, 1}, + {0x2161, {0x2171, 0x0, 0x0}, 1}, + {0x2162, {0x2172, 0x0, 0x0}, 1}, + {0x2163, {0x2173, 0x0, 0x0}, 1}, + {0x2164, {0x2174, 0x0, 0x0}, 1}, + {0x2165, {0x2175, 0x0, 0x0}, 1}, + {0x2166, {0x2176, 0x0, 0x0}, 1}, + {0x2167, {0x2177, 0x0, 0x0}, 1}, + {0x2168, {0x2178, 0x0, 0x0}, 1}, + {0x2169, {0x2179, 0x0, 0x0}, 1}, + {0x216A, {0x217A, 0x0, 0x0}, 1}, + {0x216B, {0x217B, 0x0, 0x0}, 1}, + {0x216C, {0x217C, 0x0, 0x0}, 1}, + {0x216D, {0x217D, 0x0, 0x0}, 1}, + {0x216E, {0x217E, 0x0, 0x0}, 1}, + {0x216F, {0x217F, 0x0, 0x0}, 1}, + {0x2183, {0x2184, 0x0, 0x0}, 1}, + {0x219A, {0x2190, 0x0, 0x0}, 1}, + {0x219B, {0x2192, 0x0, 0x0}, 1}, + {0x21AE, {0x2194, 0x0, 0x0}, 1}, + {0x21CD, {0x21D0, 0x0, 0x0}, 1}, + {0x21CE, {0x21D4, 0x0, 0x0}, 1}, + {0x21CF, {0x21D2, 0x0, 0x0}, 1}, + {0x2204, {0x2203, 0x0, 0x0}, 1}, + {0x2209, {0x2208, 0x0, 0x0}, 1}, + {0x220C, {0x220B, 0x0, 0x0}, 1}, + {0x2224, {0x2223, 0x0, 0x0}, 1}, + {0x2226, {0x2225, 0x0, 0x0}, 1}, + {0x2241, {0x223C, 0x0, 0x0}, 1}, + {0x2244, {0x2243, 0x0, 0x0}, 1}, + {0x2247, {0x2245, 0x0, 0x0}, 1}, + {0x2249, {0x2248, 0x0, 0x0}, 1}, + {0x2260, {0x3D, 0x0, 0x0}, 1}, + {0x2262, {0x2261, 0x0, 0x0}, 1}, + {0x226D, {0x224D, 0x0, 0x0}, 1}, + {0x226E, {0x3C, 0x0, 0x0}, 1}, + {0x226F, {0x3E, 0x0, 0x0}, 1}, + {0x2270, {0x2264, 0x0, 0x0}, 1}, + {0x2271, {0x2265, 0x0, 0x0}, 1}, + {0x2274, {0x2272, 0x0, 0x0}, 1}, + {0x2275, {0x2273, 0x0, 0x0}, 1}, + {0x2278, {0x2276, 0x0, 0x0}, 1}, + {0x2279, {0x2277, 0x0, 0x0}, 1}, + {0x2280, {0x227A, 0x0, 0x0}, 1}, + {0x2281, {0x227B, 0x0, 0x0}, 1}, + {0x2284, {0x2282, 0x0, 0x0}, 1}, + {0x2285, {0x2283, 0x0, 0x0}, 1}, + {0x2288, {0x2286, 0x0, 0x0}, 1}, + {0x2289, {0x2287, 0x0, 0x0}, 1}, + {0x22AC, {0x22A2, 0x0, 0x0}, 1}, + {0x22AD, {0x22A8, 0x0, 0x0}, 1}, + {0x22AE, {0x22A9, 0x0, 0x0}, 1}, + {0x22AF, {0x22AB, 0x0, 0x0}, 1}, + {0x22E0, {0x227C, 0x0, 0x0}, 1}, + {0x22E1, {0x227D, 0x0, 0x0}, 1}, + {0x22E2, {0x2291, 0x0, 0x0}, 1}, + {0x22E3, {0x2292, 0x0, 0x0}, 1}, + {0x22EA, {0x22B2, 0x0, 0x0}, 1}, + {0x22EB, {0x22B3, 0x0, 0x0}, 1}, + {0x22EC, {0x22B4, 0x0, 0x0}, 1}, + {0x22ED, {0x22B5, 0x0, 0x0}, 1}, + {0x2329, {0x3008, 0x0, 0x0}, 1}, + {0x232A, {0x3009, 0x0, 0x0}, 1}, + {0x24B6, {0x24D0, 0x0, 0x0}, 1}, + {0x24B7, {0x24D1, 0x0, 0x0}, 1}, + {0x24B8, {0x24D2, 0x0, 0x0}, 1}, + {0x24B9, {0x24D3, 0x0, 0x0}, 1}, + {0x24BA, {0x24D4, 0x0, 0x0}, 1}, + {0x24BB, {0x24D5, 0x0, 0x0}, 1}, + {0x24BC, {0x24D6, 0x0, 0x0}, 1}, + {0x24BD, {0x24D7, 0x0, 0x0}, 1}, + {0x24BE, {0x24D8, 0x0, 0x0}, 1}, + {0x24BF, {0x24D9, 0x0, 0x0}, 1}, + {0x24C0, {0x24DA, 0x0, 0x0}, 1}, + {0x24C1, {0x24DB, 0x0, 0x0}, 1}, + {0x24C2, {0x24DC, 0x0, 0x0}, 1}, + {0x24C3, {0x24DD, 0x0, 0x0}, 1}, + {0x24C4, {0x24DE, 0x0, 0x0}, 1}, + {0x24C5, {0x24DF, 0x0, 0x0}, 1}, + {0x24C6, {0x24E0, 0x0, 0x0}, 1}, + {0x24C7, {0x24E1, 0x0, 0x0}, 1}, + {0x24C8, {0x24E2, 0x0, 0x0}, 1}, + {0x24C9, {0x24E3, 0x0, 0x0}, 1}, + {0x24CA, {0x24E4, 0x0, 0x0}, 1}, + {0x24CB, {0x24E5, 0x0, 0x0}, 1}, + {0x24CC, {0x24E6, 0x0, 0x0}, 1}, + {0x24CD, {0x24E7, 0x0, 0x0}, 1}, + {0x24CE, {0x24E8, 0x0, 0x0}, 1}, + {0x24CF, {0x24E9, 0x0, 0x0}, 1}, + {0x2ADC, {0x2ADD, 0x0, 0x0}, 1}, + {0x2C00, {0x2C30, 0x0, 0x0}, 1}, + {0x2C01, {0x2C31, 0x0, 0x0}, 1}, + {0x2C02, {0x2C32, 0x0, 0x0}, 1}, + {0x2C03, {0x2C33, 0x0, 0x0}, 1}, + {0x2C04, {0x2C34, 0x0, 0x0}, 1}, + {0x2C05, {0x2C35, 0x0, 0x0}, 1}, + {0x2C06, {0x2C36, 0x0, 0x0}, 1}, + {0x2C07, {0x2C37, 0x0, 0x0}, 1}, + {0x2C08, {0x2C38, 0x0, 0x0}, 1}, + {0x2C09, {0x2C39, 0x0, 0x0}, 1}, + {0x2C0A, {0x2C3A, 0x0, 0x0}, 1}, + {0x2C0B, {0x2C3B, 0x0, 0x0}, 1}, + {0x2C0C, {0x2C3C, 0x0, 0x0}, 1}, + {0x2C0D, {0x2C3D, 0x0, 0x0}, 1}, + {0x2C0E, {0x2C3E, 0x0, 0x0}, 1}, + {0x2C0F, {0x2C3F, 0x0, 0x0}, 1}, + {0x2C10, {0x2C40, 0x0, 0x0}, 1}, + {0x2C11, {0x2C41, 0x0, 0x0}, 1}, + {0x2C12, {0x2C42, 0x0, 0x0}, 1}, + {0x2C13, {0x2C43, 0x0, 0x0}, 1}, + {0x2C14, {0x2C44, 0x0, 0x0}, 1}, + {0x2C15, {0x2C45, 0x0, 0x0}, 1}, + {0x2C16, {0x2C46, 0x0, 0x0}, 1}, + {0x2C17, {0x2C47, 0x0, 0x0}, 1}, + {0x2C18, {0x2C48, 0x0, 0x0}, 1}, + {0x2C19, {0x2C49, 0x0, 0x0}, 1}, + {0x2C1A, {0x2C4A, 0x0, 0x0}, 1}, + {0x2C1B, {0x2C4B, 0x0, 0x0}, 1}, + {0x2C1C, {0x2C4C, 0x0, 0x0}, 1}, + {0x2C1D, {0x2C4D, 0x0, 0x0}, 1}, + {0x2C1E, {0x2C4E, 0x0, 0x0}, 1}, + {0x2C1F, {0x2C4F, 0x0, 0x0}, 1}, + {0x2C20, {0x2C50, 0x0, 0x0}, 1}, + {0x2C21, {0x2C51, 0x0, 0x0}, 1}, + {0x2C22, {0x2C52, 0x0, 0x0}, 1}, + {0x2C23, {0x2C53, 0x0, 0x0}, 1}, + {0x2C24, {0x2C54, 0x0, 0x0}, 1}, + {0x2C25, {0x2C55, 0x0, 0x0}, 1}, + {0x2C26, {0x2C56, 0x0, 0x0}, 1}, + {0x2C27, {0x2C57, 0x0, 0x0}, 1}, + {0x2C28, {0x2C58, 0x0, 0x0}, 1}, + {0x2C29, {0x2C59, 0x0, 0x0}, 1}, + {0x2C2A, {0x2C5A, 0x0, 0x0}, 1}, + {0x2C2B, {0x2C5B, 0x0, 0x0}, 1}, + {0x2C2C, {0x2C5C, 0x0, 0x0}, 1}, + {0x2C2D, {0x2C5D, 0x0, 0x0}, 1}, + {0x2C2E, {0x2C5E, 0x0, 0x0}, 1}, + {0x2C60, {0x2C61, 0x0, 0x0}, 1}, + {0x2C62, {0x26B, 0x0, 0x0}, 1}, + {0x2C63, {0x1D7D, 0x0, 0x0}, 1}, + {0x2C64, {0x27D, 0x0, 0x0}, 1}, + {0x2C67, {0x2C68, 0x0, 0x0}, 1}, + {0x2C69, {0x2C6A, 0x0, 0x0}, 1}, + {0x2C6B, {0x2C6C, 0x0, 0x0}, 1}, + {0x2C6D, {0x251, 0x0, 0x0}, 1}, + {0x2C6E, {0x271, 0x0, 0x0}, 1}, + {0x2C6F, {0x250, 0x0, 0x0}, 1}, + {0x2C70, {0x252, 0x0, 0x0}, 1}, + {0x2C72, {0x2C73, 0x0, 0x0}, 1}, + {0x2C75, {0x2C76, 0x0, 0x0}, 1}, + {0x2C7E, {0x23F, 0x0, 0x0}, 1}, + {0x2C7F, {0x240, 0x0, 0x0}, 1}, + {0x2C80, {0x2C81, 0x0, 0x0}, 1}, + {0x2C82, {0x2C83, 0x0, 0x0}, 1}, + {0x2C84, {0x2C85, 0x0, 0x0}, 1}, + {0x2C86, {0x2C87, 0x0, 0x0}, 1}, + {0x2C88, {0x2C89, 0x0, 0x0}, 1}, + {0x2C8A, {0x2C8B, 0x0, 0x0}, 1}, + {0x2C8C, {0x2C8D, 0x0, 0x0}, 1}, + {0x2C8E, {0x2C8F, 0x0, 0x0}, 1}, + {0x2C90, {0x2C91, 0x0, 0x0}, 1}, + {0x2C92, {0x2C93, 0x0, 0x0}, 1}, + {0x2C94, {0x2C95, 0x0, 0x0}, 1}, + {0x2C96, {0x2C97, 0x0, 0x0}, 1}, + {0x2C98, {0x2C99, 0x0, 0x0}, 1}, + {0x2C9A, {0x2C9B, 0x0, 0x0}, 1}, + {0x2C9C, {0x2C9D, 0x0, 0x0}, 1}, + {0x2C9E, {0x2C9F, 0x0, 0x0}, 1}, + {0x2CA0, {0x2CA1, 0x0, 0x0}, 1}, + {0x2CA2, {0x2CA3, 0x0, 0x0}, 1}, + {0x2CA4, {0x2CA5, 0x0, 0x0}, 1}, + {0x2CA6, {0x2CA7, 0x0, 0x0}, 1}, + {0x2CA8, {0x2CA9, 0x0, 0x0}, 1}, + {0x2CAA, {0x2CAB, 0x0, 0x0}, 1}, + {0x2CAC, {0x2CAD, 0x0, 0x0}, 1}, + {0x2CAE, {0x2CAF, 0x0, 0x0}, 1}, + {0x2CB0, {0x2CB1, 0x0, 0x0}, 1}, + {0x2CB2, {0x2CB3, 0x0, 0x0}, 1}, + {0x2CB4, {0x2CB5, 0x0, 0x0}, 1}, + {0x2CB6, {0x2CB7, 0x0, 0x0}, 1}, + {0x2CB8, {0x2CB9, 0x0, 0x0}, 1}, + {0x2CBA, {0x2CBB, 0x0, 0x0}, 1}, + {0x2CBC, {0x2CBD, 0x0, 0x0}, 1}, + {0x2CBE, {0x2CBF, 0x0, 0x0}, 1}, + {0x2CC0, {0x2CC1, 0x0, 0x0}, 1}, + {0x2CC2, {0x2CC3, 0x0, 0x0}, 1}, + {0x2CC4, {0x2CC5, 0x0, 0x0}, 1}, + {0x2CC6, {0x2CC7, 0x0, 0x0}, 1}, + {0x2CC8, {0x2CC9, 0x0, 0x0}, 1}, + {0x2CCA, {0x2CCB, 0x0, 0x0}, 1}, + {0x2CCC, {0x2CCD, 0x0, 0x0}, 1}, + {0x2CCE, {0x2CCF, 0x0, 0x0}, 1}, + {0x2CD0, {0x2CD1, 0x0, 0x0}, 1}, + {0x2CD2, {0x2CD3, 0x0, 0x0}, 1}, + {0x2CD4, {0x2CD5, 0x0, 0x0}, 1}, + {0x2CD6, {0x2CD7, 0x0, 0x0}, 1}, + {0x2CD8, {0x2CD9, 0x0, 0x0}, 1}, + {0x2CDA, {0x2CDB, 0x0, 0x0}, 1}, + {0x2CDC, {0x2CDD, 0x0, 0x0}, 1}, + {0x2CDE, {0x2CDF, 0x0, 0x0}, 1}, + {0x2CE0, {0x2CE1, 0x0, 0x0}, 1}, + {0x2CE2, {0x2CE3, 0x0, 0x0}, 1}, + {0x2CEB, {0x2CEC, 0x0, 0x0}, 1}, + {0x2CED, {0x2CEE, 0x0, 0x0}, 1}, + {0x2CEF, {0x0, 0x0, 0x0}, 0}, + {0x2CF0, {0x0, 0x0, 0x0}, 0}, + {0x2CF1, {0x0, 0x0, 0x0}, 0}, + {0x2CF2, {0x2CF3, 0x0, 0x0}, 1}, + {0x2D7F, {0x0, 0x0, 0x0}, 0}, + {0x2DE0, {0x0, 0x0, 0x0}, 0}, + {0x2DE1, {0x0, 0x0, 0x0}, 0}, + {0x2DE2, {0x0, 0x0, 0x0}, 0}, + {0x2DE3, {0x0, 0x0, 0x0}, 0}, + {0x2DE4, {0x0, 0x0, 0x0}, 0}, + {0x2DE5, {0x0, 0x0, 0x0}, 0}, + {0x2DE6, {0x0, 0x0, 0x0}, 0}, + {0x2DE7, {0x0, 0x0, 0x0}, 0}, + {0x2DE8, {0x0, 0x0, 0x0}, 0}, + {0x2DE9, {0x0, 0x0, 0x0}, 0}, + {0x2DEA, {0x0, 0x0, 0x0}, 0}, + {0x2DEB, {0x0, 0x0, 0x0}, 0}, + {0x2DEC, {0x0, 0x0, 0x0}, 0}, + {0x2DED, {0x0, 0x0, 0x0}, 0}, + {0x2DEE, {0x0, 0x0, 0x0}, 0}, + {0x2DEF, {0x0, 0x0, 0x0}, 0}, + {0x2DF0, {0x0, 0x0, 0x0}, 0}, + {0x2DF1, {0x0, 0x0, 0x0}, 0}, + {0x2DF2, {0x0, 0x0, 0x0}, 0}, + {0x2DF3, {0x0, 0x0, 0x0}, 0}, + {0x2DF4, {0x0, 0x0, 0x0}, 0}, + {0x2DF5, {0x0, 0x0, 0x0}, 0}, + {0x2DF6, {0x0, 0x0, 0x0}, 0}, + {0x2DF7, {0x0, 0x0, 0x0}, 0}, + {0x2DF8, {0x0, 0x0, 0x0}, 0}, + {0x2DF9, {0x0, 0x0, 0x0}, 0}, + {0x2DFA, {0x0, 0x0, 0x0}, 0}, + {0x2DFB, {0x0, 0x0, 0x0}, 0}, + {0x2DFC, {0x0, 0x0, 0x0}, 0}, + {0x2DFD, {0x0, 0x0, 0x0}, 0}, + {0x2DFE, {0x0, 0x0, 0x0}, 0}, + {0x2DFF, {0x0, 0x0, 0x0}, 0}, + {0x302A, {0x0, 0x0, 0x0}, 0}, + {0x302B, {0x0, 0x0, 0x0}, 0}, + {0x302C, {0x0, 0x0, 0x0}, 0}, + {0x302D, {0x0, 0x0, 0x0}, 0}, + {0x304C, {0x304B, 0x0, 0x0}, 1}, + {0x304E, {0x304D, 0x0, 0x0}, 1}, + {0x3050, {0x304F, 0x0, 0x0}, 1}, + {0x3052, {0x3051, 0x0, 0x0}, 1}, + {0x3054, {0x3053, 0x0, 0x0}, 1}, + {0x3056, {0x3055, 0x0, 0x0}, 1}, + {0x3058, {0x3057, 0x0, 0x0}, 1}, + {0x305A, {0x3059, 0x0, 0x0}, 1}, + {0x305C, {0x305B, 0x0, 0x0}, 1}, + {0x305E, {0x305D, 0x0, 0x0}, 1}, + {0x3060, {0x305F, 0x0, 0x0}, 1}, + {0x3062, {0x3061, 0x0, 0x0}, 1}, + {0x3065, {0x3064, 0x0, 0x0}, 1}, + {0x3067, {0x3066, 0x0, 0x0}, 1}, + {0x3069, {0x3068, 0x0, 0x0}, 1}, + {0x3070, {0x306F, 0x0, 0x0}, 1}, + {0x3071, {0x306F, 0x0, 0x0}, 1}, + {0x3073, {0x3072, 0x0, 0x0}, 1}, + {0x3074, {0x3072, 0x0, 0x0}, 1}, + {0x3076, {0x3075, 0x0, 0x0}, 1}, + {0x3077, {0x3075, 0x0, 0x0}, 1}, + {0x3079, {0x3078, 0x0, 0x0}, 1}, + {0x307A, {0x3078, 0x0, 0x0}, 1}, + {0x307C, {0x307B, 0x0, 0x0}, 1}, + {0x307D, {0x307B, 0x0, 0x0}, 1}, + {0x3094, {0x3046, 0x0, 0x0}, 1}, + {0x3099, {0x0, 0x0, 0x0}, 0}, + {0x309A, {0x0, 0x0, 0x0}, 0}, + {0x309E, {0x309D, 0x0, 0x0}, 1}, + {0x30AC, {0x30AB, 0x0, 0x0}, 1}, + {0x30AE, {0x30AD, 0x0, 0x0}, 1}, + {0x30B0, {0x30AF, 0x0, 0x0}, 1}, + {0x30B2, {0x30B1, 0x0, 0x0}, 1}, + {0x30B4, {0x30B3, 0x0, 0x0}, 1}, + {0x30B6, {0x30B5, 0x0, 0x0}, 1}, + {0x30B8, {0x30B7, 0x0, 0x0}, 1}, + {0x30BA, {0x30B9, 0x0, 0x0}, 1}, + {0x30BC, {0x30BB, 0x0, 0x0}, 1}, + {0x30BE, {0x30BD, 0x0, 0x0}, 1}, + {0x30C0, {0x30BF, 0x0, 0x0}, 1}, + {0x30C2, {0x30C1, 0x0, 0x0}, 1}, + {0x30C5, {0x30C4, 0x0, 0x0}, 1}, + {0x30C7, {0x30C6, 0x0, 0x0}, 1}, + {0x30C9, {0x30C8, 0x0, 0x0}, 1}, + {0x30D0, {0x30CF, 0x0, 0x0}, 1}, + {0x30D1, {0x30CF, 0x0, 0x0}, 1}, + {0x30D3, {0x30D2, 0x0, 0x0}, 1}, + {0x30D4, {0x30D2, 0x0, 0x0}, 1}, + {0x30D6, {0x30D5, 0x0, 0x0}, 1}, + {0x30D7, {0x30D5, 0x0, 0x0}, 1}, + {0x30D9, {0x30D8, 0x0, 0x0}, 1}, + {0x30DA, {0x30D8, 0x0, 0x0}, 1}, + {0x30DC, {0x30DB, 0x0, 0x0}, 1}, + {0x30DD, {0x30DB, 0x0, 0x0}, 1}, + {0x30F4, {0x30A6, 0x0, 0x0}, 1}, + {0x30F7, {0x30EF, 0x0, 0x0}, 1}, + {0x30F8, {0x30F0, 0x0, 0x0}, 1}, + {0x30F9, {0x30F1, 0x0, 0x0}, 1}, + {0x30FA, {0x30F2, 0x0, 0x0}, 1}, + {0x30FE, {0x30FD, 0x0, 0x0}, 1}, + {0xA640, {0xA641, 0x0, 0x0}, 1}, + {0xA642, {0xA643, 0x0, 0x0}, 1}, + {0xA644, {0xA645, 0x0, 0x0}, 1}, + {0xA646, {0xA647, 0x0, 0x0}, 1}, + {0xA648, {0xA649, 0x0, 0x0}, 1}, + {0xA64A, {0xA64B, 0x0, 0x0}, 1}, + {0xA64C, {0xA64D, 0x0, 0x0}, 1}, + {0xA64E, {0xA64F, 0x0, 0x0}, 1}, + {0xA650, {0xA651, 0x0, 0x0}, 1}, + {0xA652, {0xA653, 0x0, 0x0}, 1}, + {0xA654, {0xA655, 0x0, 0x0}, 1}, + {0xA656, {0xA657, 0x0, 0x0}, 1}, + {0xA658, {0xA659, 0x0, 0x0}, 1}, + {0xA65A, {0xA65B, 0x0, 0x0}, 1}, + {0xA65C, {0xA65D, 0x0, 0x0}, 1}, + {0xA65E, {0xA65F, 0x0, 0x0}, 1}, + {0xA660, {0xA661, 0x0, 0x0}, 1}, + {0xA662, {0xA663, 0x0, 0x0}, 1}, + {0xA664, {0xA665, 0x0, 0x0}, 1}, + {0xA666, {0xA667, 0x0, 0x0}, 1}, + {0xA668, {0xA669, 0x0, 0x0}, 1}, + {0xA66A, {0xA66B, 0x0, 0x0}, 1}, + {0xA66C, {0xA66D, 0x0, 0x0}, 1}, + {0xA66F, {0x0, 0x0, 0x0}, 0}, + {0xA674, {0x0, 0x0, 0x0}, 0}, + {0xA675, {0x0, 0x0, 0x0}, 0}, + {0xA676, {0x0, 0x0, 0x0}, 0}, + {0xA677, {0x0, 0x0, 0x0}, 0}, + {0xA678, {0x0, 0x0, 0x0}, 0}, + {0xA679, {0x0, 0x0, 0x0}, 0}, + {0xA67A, {0x0, 0x0, 0x0}, 0}, + {0xA67B, {0x0, 0x0, 0x0}, 0}, + {0xA67C, {0x0, 0x0, 0x0}, 0}, + {0xA67D, {0x0, 0x0, 0x0}, 0}, + {0xA680, {0xA681, 0x0, 0x0}, 1}, + {0xA682, {0xA683, 0x0, 0x0}, 1}, + {0xA684, {0xA685, 0x0, 0x0}, 1}, + {0xA686, {0xA687, 0x0, 0x0}, 1}, + {0xA688, {0xA689, 0x0, 0x0}, 1}, + {0xA68A, {0xA68B, 0x0, 0x0}, 1}, + {0xA68C, {0xA68D, 0x0, 0x0}, 1}, + {0xA68E, {0xA68F, 0x0, 0x0}, 1}, + {0xA690, {0xA691, 0x0, 0x0}, 1}, + {0xA692, {0xA693, 0x0, 0x0}, 1}, + {0xA694, {0xA695, 0x0, 0x0}, 1}, + {0xA696, {0xA697, 0x0, 0x0}, 1}, + {0xA698, {0xA699, 0x0, 0x0}, 1}, + {0xA69A, {0xA69B, 0x0, 0x0}, 1}, + {0xA69E, {0x0, 0x0, 0x0}, 0}, + {0xA69F, {0x0, 0x0, 0x0}, 0}, + {0xA6F0, {0x0, 0x0, 0x0}, 0}, + {0xA6F1, {0x0, 0x0, 0x0}, 0}, + {0xA722, {0xA723, 0x0, 0x0}, 1}, + {0xA724, {0xA725, 0x0, 0x0}, 1}, + {0xA726, {0xA727, 0x0, 0x0}, 1}, + {0xA728, {0xA729, 0x0, 0x0}, 1}, + {0xA72A, {0xA72B, 0x0, 0x0}, 1}, + {0xA72C, {0xA72D, 0x0, 0x0}, 1}, + {0xA72E, {0xA72F, 0x0, 0x0}, 1}, + {0xA732, {0xA733, 0x0, 0x0}, 1}, + {0xA734, {0xA735, 0x0, 0x0}, 1}, + {0xA736, {0xA737, 0x0, 0x0}, 1}, + {0xA738, {0xA739, 0x0, 0x0}, 1}, + {0xA73A, {0xA73B, 0x0, 0x0}, 1}, + {0xA73C, {0xA73D, 0x0, 0x0}, 1}, + {0xA73E, {0xA73F, 0x0, 0x0}, 1}, + {0xA740, {0xA741, 0x0, 0x0}, 1}, + {0xA742, {0xA743, 0x0, 0x0}, 1}, + {0xA744, {0xA745, 0x0, 0x0}, 1}, + {0xA746, {0xA747, 0x0, 0x0}, 1}, + {0xA748, {0xA749, 0x0, 0x0}, 1}, + {0xA74A, {0xA74B, 0x0, 0x0}, 1}, + {0xA74C, {0xA74D, 0x0, 0x0}, 1}, + {0xA74E, {0xA74F, 0x0, 0x0}, 1}, + {0xA750, {0xA751, 0x0, 0x0}, 1}, + {0xA752, {0xA753, 0x0, 0x0}, 1}, + {0xA754, {0xA755, 0x0, 0x0}, 1}, + {0xA756, {0xA757, 0x0, 0x0}, 1}, + {0xA758, {0xA759, 0x0, 0x0}, 1}, + {0xA75A, {0xA75B, 0x0, 0x0}, 1}, + {0xA75C, {0xA75D, 0x0, 0x0}, 1}, + {0xA75E, {0xA75F, 0x0, 0x0}, 1}, + {0xA760, {0xA761, 0x0, 0x0}, 1}, + {0xA762, {0xA763, 0x0, 0x0}, 1}, + {0xA764, {0xA765, 0x0, 0x0}, 1}, + {0xA766, {0xA767, 0x0, 0x0}, 1}, + {0xA768, {0xA769, 0x0, 0x0}, 1}, + {0xA76A, {0xA76B, 0x0, 0x0}, 1}, + {0xA76C, {0xA76D, 0x0, 0x0}, 1}, + {0xA76E, {0xA76F, 0x0, 0x0}, 1}, + {0xA779, {0xA77A, 0x0, 0x0}, 1}, + {0xA77B, {0xA77C, 0x0, 0x0}, 1}, + {0xA77D, {0x1D79, 0x0, 0x0}, 1}, + {0xA77E, {0xA77F, 0x0, 0x0}, 1}, + {0xA780, {0xA781, 0x0, 0x0}, 1}, + {0xA782, {0xA783, 0x0, 0x0}, 1}, + {0xA784, {0xA785, 0x0, 0x0}, 1}, + {0xA786, {0xA787, 0x0, 0x0}, 1}, + {0xA78B, {0xA78C, 0x0, 0x0}, 1}, + {0xA78D, {0x265, 0x0, 0x0}, 1}, + {0xA790, {0xA791, 0x0, 0x0}, 1}, + {0xA792, {0xA793, 0x0, 0x0}, 1}, + {0xA796, {0xA797, 0x0, 0x0}, 1}, + {0xA798, {0xA799, 0x0, 0x0}, 1}, + {0xA79A, {0xA79B, 0x0, 0x0}, 1}, + {0xA79C, {0xA79D, 0x0, 0x0}, 1}, + {0xA79E, {0xA79F, 0x0, 0x0}, 1}, + {0xA7A0, {0xA7A1, 0x0, 0x0}, 1}, + {0xA7A2, {0xA7A3, 0x0, 0x0}, 1}, + {0xA7A4, {0xA7A5, 0x0, 0x0}, 1}, + {0xA7A6, {0xA7A7, 0x0, 0x0}, 1}, + {0xA7A8, {0xA7A9, 0x0, 0x0}, 1}, + {0xA7AA, {0x266, 0x0, 0x0}, 1}, + {0xA7AB, {0x25C, 0x0, 0x0}, 1}, + {0xA7AC, {0x261, 0x0, 0x0}, 1}, + {0xA7AD, {0x26C, 0x0, 0x0}, 1}, + {0xA7AE, {0x26A, 0x0, 0x0}, 1}, + {0xA7B0, {0x29E, 0x0, 0x0}, 1}, + {0xA7B1, {0x287, 0x0, 0x0}, 1}, + {0xA7B2, {0x29D, 0x0, 0x0}, 1}, + {0xA7B3, {0xAB53, 0x0, 0x0}, 1}, + {0xA7B4, {0xA7B5, 0x0, 0x0}, 1}, + {0xA7B6, {0xA7B7, 0x0, 0x0}, 1}, + {0xA7B8, {0xA7B9, 0x0, 0x0}, 1}, + {0xA7BA, {0xA7BB, 0x0, 0x0}, 1}, + {0xA7BC, {0xA7BD, 0x0, 0x0}, 1}, + {0xA7BE, {0xA7BF, 0x0, 0x0}, 1}, + {0xA7C2, {0xA7C3, 0x0, 0x0}, 1}, + {0xA7C4, {0xA794, 0x0, 0x0}, 1}, + {0xA7C5, {0x282, 0x0, 0x0}, 1}, + {0xA7C6, {0x1D8E, 0x0, 0x0}, 1}, + {0xA7C7, {0xA7C8, 0x0, 0x0}, 1}, + {0xA7C9, {0xA7CA, 0x0, 0x0}, 1}, + {0xA7F5, {0xA7F6, 0x0, 0x0}, 1}, + {0xA802, {0x0, 0x0, 0x0}, 0}, + {0xA806, {0x0, 0x0, 0x0}, 0}, + {0xA80B, {0x0, 0x0, 0x0}, 0}, + {0xA825, {0x0, 0x0, 0x0}, 0}, + {0xA826, {0x0, 0x0, 0x0}, 0}, + {0xA82C, {0x0, 0x0, 0x0}, 0}, + {0xA8C4, {0x0, 0x0, 0x0}, 0}, + {0xA8C5, {0x0, 0x0, 0x0}, 0}, + {0xA8E0, {0x0, 0x0, 0x0}, 0}, + {0xA8E1, {0x0, 0x0, 0x0}, 0}, + {0xA8E2, {0x0, 0x0, 0x0}, 0}, + {0xA8E3, {0x0, 0x0, 0x0}, 0}, + {0xA8E4, {0x0, 0x0, 0x0}, 0}, + {0xA8E5, {0x0, 0x0, 0x0}, 0}, + {0xA8E6, {0x0, 0x0, 0x0}, 0}, + {0xA8E7, {0x0, 0x0, 0x0}, 0}, + {0xA8E8, {0x0, 0x0, 0x0}, 0}, + {0xA8E9, {0x0, 0x0, 0x0}, 0}, + {0xA8EA, {0x0, 0x0, 0x0}, 0}, + {0xA8EB, {0x0, 0x0, 0x0}, 0}, + {0xA8EC, {0x0, 0x0, 0x0}, 0}, + {0xA8ED, {0x0, 0x0, 0x0}, 0}, + {0xA8EE, {0x0, 0x0, 0x0}, 0}, + {0xA8EF, {0x0, 0x0, 0x0}, 0}, + {0xA8F0, {0x0, 0x0, 0x0}, 0}, + {0xA8F1, {0x0, 0x0, 0x0}, 0}, + {0xA8FF, {0x0, 0x0, 0x0}, 0}, + {0xA926, {0x0, 0x0, 0x0}, 0}, + {0xA927, {0x0, 0x0, 0x0}, 0}, + {0xA928, {0x0, 0x0, 0x0}, 0}, + {0xA929, {0x0, 0x0, 0x0}, 0}, + {0xA92A, {0x0, 0x0, 0x0}, 0}, + {0xA92B, {0x0, 0x0, 0x0}, 0}, + {0xA92C, {0x0, 0x0, 0x0}, 0}, + {0xA92D, {0x0, 0x0, 0x0}, 0}, + {0xA947, {0x0, 0x0, 0x0}, 0}, + {0xA948, {0x0, 0x0, 0x0}, 0}, + {0xA949, {0x0, 0x0, 0x0}, 0}, + {0xA94A, {0x0, 0x0, 0x0}, 0}, + {0xA94B, {0x0, 0x0, 0x0}, 0}, + {0xA94C, {0x0, 0x0, 0x0}, 0}, + {0xA94D, {0x0, 0x0, 0x0}, 0}, + {0xA94E, {0x0, 0x0, 0x0}, 0}, + {0xA94F, {0x0, 0x0, 0x0}, 0}, + {0xA950, {0x0, 0x0, 0x0}, 0}, + {0xA951, {0x0, 0x0, 0x0}, 0}, + {0xA980, {0x0, 0x0, 0x0}, 0}, + {0xA981, {0x0, 0x0, 0x0}, 0}, + {0xA982, {0x0, 0x0, 0x0}, 0}, + {0xA9B3, {0x0, 0x0, 0x0}, 0}, + {0xA9B6, {0x0, 0x0, 0x0}, 0}, + {0xA9B7, {0x0, 0x0, 0x0}, 0}, + {0xA9B8, {0x0, 0x0, 0x0}, 0}, + {0xA9B9, {0x0, 0x0, 0x0}, 0}, + {0xA9BC, {0x0, 0x0, 0x0}, 0}, + {0xA9BD, {0x0, 0x0, 0x0}, 0}, + {0xA9E5, {0x0, 0x0, 0x0}, 0}, + {0xAA29, {0x0, 0x0, 0x0}, 0}, + {0xAA2A, {0x0, 0x0, 0x0}, 0}, + {0xAA2B, {0x0, 0x0, 0x0}, 0}, + {0xAA2C, {0x0, 0x0, 0x0}, 0}, + {0xAA2D, {0x0, 0x0, 0x0}, 0}, + {0xAA2E, {0x0, 0x0, 0x0}, 0}, + {0xAA31, {0x0, 0x0, 0x0}, 0}, + {0xAA32, {0x0, 0x0, 0x0}, 0}, + {0xAA35, {0x0, 0x0, 0x0}, 0}, + {0xAA36, {0x0, 0x0, 0x0}, 0}, + {0xAA43, {0x0, 0x0, 0x0}, 0}, + {0xAA4C, {0x0, 0x0, 0x0}, 0}, + {0xAA7C, {0x0, 0x0, 0x0}, 0}, + {0xAAB0, {0x0, 0x0, 0x0}, 0}, + {0xAAB2, {0x0, 0x0, 0x0}, 0}, + {0xAAB3, {0x0, 0x0, 0x0}, 0}, + {0xAAB4, {0x0, 0x0, 0x0}, 0}, + {0xAAB7, {0x0, 0x0, 0x0}, 0}, + {0xAAB8, {0x0, 0x0, 0x0}, 0}, + {0xAABE, {0x0, 0x0, 0x0}, 0}, + {0xAABF, {0x0, 0x0, 0x0}, 0}, + {0xAAC1, {0x0, 0x0, 0x0}, 0}, + {0xAAEC, {0x0, 0x0, 0x0}, 0}, + {0xAAED, {0x0, 0x0, 0x0}, 0}, + {0xAAF6, {0x0, 0x0, 0x0}, 0}, + {0xABE5, {0x0, 0x0, 0x0}, 0}, + {0xABE8, {0x0, 0x0, 0x0}, 0}, + {0xABED, {0x0, 0x0, 0x0}, 0}, + {0xF900, {0x8C48, 0x0, 0x0}, 1}, + {0xF901, {0x66F4, 0x0, 0x0}, 1}, + {0xF902, {0x8ECA, 0x0, 0x0}, 1}, + {0xF903, {0x8CC8, 0x0, 0x0}, 1}, + {0xF904, {0x6ED1, 0x0, 0x0}, 1}, + {0xF905, {0x4E32, 0x0, 0x0}, 1}, + {0xF906, {0x53E5, 0x0, 0x0}, 1}, + {0xF907, {0x9F9C, 0x0, 0x0}, 1}, + {0xF908, {0x9F9C, 0x0, 0x0}, 1}, + {0xF909, {0x5951, 0x0, 0x0}, 1}, + {0xF90A, {0x91D1, 0x0, 0x0}, 1}, + {0xF90B, {0x5587, 0x0, 0x0}, 1}, + {0xF90C, {0x5948, 0x0, 0x0}, 1}, + {0xF90D, {0x61F6, 0x0, 0x0}, 1}, + {0xF90E, {0x7669, 0x0, 0x0}, 1}, + {0xF90F, {0x7F85, 0x0, 0x0}, 1}, + {0xF910, {0x863F, 0x0, 0x0}, 1}, + {0xF911, {0x87BA, 0x0, 0x0}, 1}, + {0xF912, {0x88F8, 0x0, 0x0}, 1}, + {0xF913, {0x908F, 0x0, 0x0}, 1}, + {0xF914, {0x6A02, 0x0, 0x0}, 1}, + {0xF915, {0x6D1B, 0x0, 0x0}, 1}, + {0xF916, {0x70D9, 0x0, 0x0}, 1}, + {0xF917, {0x73DE, 0x0, 0x0}, 1}, + {0xF918, {0x843D, 0x0, 0x0}, 1}, + {0xF919, {0x916A, 0x0, 0x0}, 1}, + {0xF91A, {0x99F1, 0x0, 0x0}, 1}, + {0xF91B, {0x4E82, 0x0, 0x0}, 1}, + {0xF91C, {0x5375, 0x0, 0x0}, 1}, + {0xF91D, {0x6B04, 0x0, 0x0}, 1}, + {0xF91E, {0x721B, 0x0, 0x0}, 1}, + {0xF91F, {0x862D, 0x0, 0x0}, 1}, + {0xF920, {0x9E1E, 0x0, 0x0}, 1}, + {0xF921, {0x5D50, 0x0, 0x0}, 1}, + {0xF922, {0x6FEB, 0x0, 0x0}, 1}, + {0xF923, {0x85CD, 0x0, 0x0}, 1}, + {0xF924, {0x8964, 0x0, 0x0}, 1}, + {0xF925, {0x62C9, 0x0, 0x0}, 1}, + {0xF926, {0x81D8, 0x0, 0x0}, 1}, + {0xF927, {0x881F, 0x0, 0x0}, 1}, + {0xF928, {0x5ECA, 0x0, 0x0}, 1}, + {0xF929, {0x6717, 0x0, 0x0}, 1}, + {0xF92A, {0x6D6A, 0x0, 0x0}, 1}, + {0xF92B, {0x72FC, 0x0, 0x0}, 1}, + {0xF92C, {0x90CE, 0x0, 0x0}, 1}, + {0xF92D, {0x4F86, 0x0, 0x0}, 1}, + {0xF92E, {0x51B7, 0x0, 0x0}, 1}, + {0xF92F, {0x52DE, 0x0, 0x0}, 1}, + {0xF930, {0x64C4, 0x0, 0x0}, 1}, + {0xF931, {0x6AD3, 0x0, 0x0}, 1}, + {0xF932, {0x7210, 0x0, 0x0}, 1}, + {0xF933, {0x76E7, 0x0, 0x0}, 1}, + {0xF934, {0x8001, 0x0, 0x0}, 1}, + {0xF935, {0x8606, 0x0, 0x0}, 1}, + {0xF936, {0x865C, 0x0, 0x0}, 1}, + {0xF937, {0x8DEF, 0x0, 0x0}, 1}, + {0xF938, {0x9732, 0x0, 0x0}, 1}, + {0xF939, {0x9B6F, 0x0, 0x0}, 1}, + {0xF93A, {0x9DFA, 0x0, 0x0}, 1}, + {0xF93B, {0x788C, 0x0, 0x0}, 1}, + {0xF93C, {0x797F, 0x0, 0x0}, 1}, + {0xF93D, {0x7DA0, 0x0, 0x0}, 1}, + {0xF93E, {0x83C9, 0x0, 0x0}, 1}, + {0xF93F, {0x9304, 0x0, 0x0}, 1}, + {0xF940, {0x9E7F, 0x0, 0x0}, 1}, + {0xF941, {0x8AD6, 0x0, 0x0}, 1}, + {0xF942, {0x58DF, 0x0, 0x0}, 1}, + {0xF943, {0x5F04, 0x0, 0x0}, 1}, + {0xF944, {0x7C60, 0x0, 0x0}, 1}, + {0xF945, {0x807E, 0x0, 0x0}, 1}, + {0xF946, {0x7262, 0x0, 0x0}, 1}, + {0xF947, {0x78CA, 0x0, 0x0}, 1}, + {0xF948, {0x8CC2, 0x0, 0x0}, 1}, + {0xF949, {0x96F7, 0x0, 0x0}, 1}, + {0xF94A, {0x58D8, 0x0, 0x0}, 1}, + {0xF94B, {0x5C62, 0x0, 0x0}, 1}, + {0xF94C, {0x6A13, 0x0, 0x0}, 1}, + {0xF94D, {0x6DDA, 0x0, 0x0}, 1}, + {0xF94E, {0x6F0F, 0x0, 0x0}, 1}, + {0xF94F, {0x7D2F, 0x0, 0x0}, 1}, + {0xF950, {0x7E37, 0x0, 0x0}, 1}, + {0xF951, {0x964B, 0x0, 0x0}, 1}, + {0xF952, {0x52D2, 0x0, 0x0}, 1}, + {0xF953, {0x808B, 0x0, 0x0}, 1}, + {0xF954, {0x51DC, 0x0, 0x0}, 1}, + {0xF955, {0x51CC, 0x0, 0x0}, 1}, + {0xF956, {0x7A1C, 0x0, 0x0}, 1}, + {0xF957, {0x7DBE, 0x0, 0x0}, 1}, + {0xF958, {0x83F1, 0x0, 0x0}, 1}, + {0xF959, {0x9675, 0x0, 0x0}, 1}, + {0xF95A, {0x8B80, 0x0, 0x0}, 1}, + {0xF95B, {0x62CF, 0x0, 0x0}, 1}, + {0xF95C, {0x6A02, 0x0, 0x0}, 1}, + {0xF95D, {0x8AFE, 0x0, 0x0}, 1}, + {0xF95E, {0x4E39, 0x0, 0x0}, 1}, + {0xF95F, {0x5BE7, 0x0, 0x0}, 1}, + {0xF960, {0x6012, 0x0, 0x0}, 1}, + {0xF961, {0x7387, 0x0, 0x0}, 1}, + {0xF962, {0x7570, 0x0, 0x0}, 1}, + {0xF963, {0x5317, 0x0, 0x0}, 1}, + {0xF964, {0x78FB, 0x0, 0x0}, 1}, + {0xF965, {0x4FBF, 0x0, 0x0}, 1}, + {0xF966, {0x5FA9, 0x0, 0x0}, 1}, + {0xF967, {0x4E0D, 0x0, 0x0}, 1}, + {0xF968, {0x6CCC, 0x0, 0x0}, 1}, + {0xF969, {0x6578, 0x0, 0x0}, 1}, + {0xF96A, {0x7D22, 0x0, 0x0}, 1}, + {0xF96B, {0x53C3, 0x0, 0x0}, 1}, + {0xF96C, {0x585E, 0x0, 0x0}, 1}, + {0xF96D, {0x7701, 0x0, 0x0}, 1}, + {0xF96E, {0x8449, 0x0, 0x0}, 1}, + {0xF96F, {0x8AAA, 0x0, 0x0}, 1}, + {0xF970, {0x6BBA, 0x0, 0x0}, 1}, + {0xF971, {0x8FB0, 0x0, 0x0}, 1}, + {0xF972, {0x6C88, 0x0, 0x0}, 1}, + {0xF973, {0x62FE, 0x0, 0x0}, 1}, + {0xF974, {0x82E5, 0x0, 0x0}, 1}, + {0xF975, {0x63A0, 0x0, 0x0}, 1}, + {0xF976, {0x7565, 0x0, 0x0}, 1}, + {0xF977, {0x4EAE, 0x0, 0x0}, 1}, + {0xF978, {0x5169, 0x0, 0x0}, 1}, + {0xF979, {0x51C9, 0x0, 0x0}, 1}, + {0xF97A, {0x6881, 0x0, 0x0}, 1}, + {0xF97B, {0x7CE7, 0x0, 0x0}, 1}, + {0xF97C, {0x826F, 0x0, 0x0}, 1}, + {0xF97D, {0x8AD2, 0x0, 0x0}, 1}, + {0xF97E, {0x91CF, 0x0, 0x0}, 1}, + {0xF97F, {0x52F5, 0x0, 0x0}, 1}, + {0xF980, {0x5442, 0x0, 0x0}, 1}, + {0xF981, {0x5973, 0x0, 0x0}, 1}, + {0xF982, {0x5EEC, 0x0, 0x0}, 1}, + {0xF983, {0x65C5, 0x0, 0x0}, 1}, + {0xF984, {0x6FFE, 0x0, 0x0}, 1}, + {0xF985, {0x792A, 0x0, 0x0}, 1}, + {0xF986, {0x95AD, 0x0, 0x0}, 1}, + {0xF987, {0x9A6A, 0x0, 0x0}, 1}, + {0xF988, {0x9E97, 0x0, 0x0}, 1}, + {0xF989, {0x9ECE, 0x0, 0x0}, 1}, + {0xF98A, {0x529B, 0x0, 0x0}, 1}, + {0xF98B, {0x66C6, 0x0, 0x0}, 1}, + {0xF98C, {0x6B77, 0x0, 0x0}, 1}, + {0xF98D, {0x8F62, 0x0, 0x0}, 1}, + {0xF98E, {0x5E74, 0x0, 0x0}, 1}, + {0xF98F, {0x6190, 0x0, 0x0}, 1}, + {0xF990, {0x6200, 0x0, 0x0}, 1}, + {0xF991, {0x649A, 0x0, 0x0}, 1}, + {0xF992, {0x6F23, 0x0, 0x0}, 1}, + {0xF993, {0x7149, 0x0, 0x0}, 1}, + {0xF994, {0x7489, 0x0, 0x0}, 1}, + {0xF995, {0x79CA, 0x0, 0x0}, 1}, + {0xF996, {0x7DF4, 0x0, 0x0}, 1}, + {0xF997, {0x806F, 0x0, 0x0}, 1}, + {0xF998, {0x8F26, 0x0, 0x0}, 1}, + {0xF999, {0x84EE, 0x0, 0x0}, 1}, + {0xF99A, {0x9023, 0x0, 0x0}, 1}, + {0xF99B, {0x934A, 0x0, 0x0}, 1}, + {0xF99C, {0x5217, 0x0, 0x0}, 1}, + {0xF99D, {0x52A3, 0x0, 0x0}, 1}, + {0xF99E, {0x54BD, 0x0, 0x0}, 1}, + {0xF99F, {0x70C8, 0x0, 0x0}, 1}, + {0xF9A0, {0x88C2, 0x0, 0x0}, 1}, + {0xF9A1, {0x8AAA, 0x0, 0x0}, 1}, + {0xF9A2, {0x5EC9, 0x0, 0x0}, 1}, + {0xF9A3, {0x5FF5, 0x0, 0x0}, 1}, + {0xF9A4, {0x637B, 0x0, 0x0}, 1}, + {0xF9A5, {0x6BAE, 0x0, 0x0}, 1}, + {0xF9A6, {0x7C3E, 0x0, 0x0}, 1}, + {0xF9A7, {0x7375, 0x0, 0x0}, 1}, + {0xF9A8, {0x4EE4, 0x0, 0x0}, 1}, + {0xF9A9, {0x56F9, 0x0, 0x0}, 1}, + {0xF9AA, {0x5BE7, 0x0, 0x0}, 1}, + {0xF9AB, {0x5DBA, 0x0, 0x0}, 1}, + {0xF9AC, {0x601C, 0x0, 0x0}, 1}, + {0xF9AD, {0x73B2, 0x0, 0x0}, 1}, + {0xF9AE, {0x7469, 0x0, 0x0}, 1}, + {0xF9AF, {0x7F9A, 0x0, 0x0}, 1}, + {0xF9B0, {0x8046, 0x0, 0x0}, 1}, + {0xF9B1, {0x9234, 0x0, 0x0}, 1}, + {0xF9B2, {0x96F6, 0x0, 0x0}, 1}, + {0xF9B3, {0x9748, 0x0, 0x0}, 1}, + {0xF9B4, {0x9818, 0x0, 0x0}, 1}, + {0xF9B5, {0x4F8B, 0x0, 0x0}, 1}, + {0xF9B6, {0x79AE, 0x0, 0x0}, 1}, + {0xF9B7, {0x91B4, 0x0, 0x0}, 1}, + {0xF9B8, {0x96B8, 0x0, 0x0}, 1}, + {0xF9B9, {0x60E1, 0x0, 0x0}, 1}, + {0xF9BA, {0x4E86, 0x0, 0x0}, 1}, + {0xF9BB, {0x50DA, 0x0, 0x0}, 1}, + {0xF9BC, {0x5BEE, 0x0, 0x0}, 1}, + {0xF9BD, {0x5C3F, 0x0, 0x0}, 1}, + {0xF9BE, {0x6599, 0x0, 0x0}, 1}, + {0xF9BF, {0x6A02, 0x0, 0x0}, 1}, + {0xF9C0, {0x71CE, 0x0, 0x0}, 1}, + {0xF9C1, {0x7642, 0x0, 0x0}, 1}, + {0xF9C2, {0x84FC, 0x0, 0x0}, 1}, + {0xF9C3, {0x907C, 0x0, 0x0}, 1}, + {0xF9C4, {0x9F8D, 0x0, 0x0}, 1}, + {0xF9C5, {0x6688, 0x0, 0x0}, 1}, + {0xF9C6, {0x962E, 0x0, 0x0}, 1}, + {0xF9C7, {0x5289, 0x0, 0x0}, 1}, + {0xF9C8, {0x677B, 0x0, 0x0}, 1}, + {0xF9C9, {0x67F3, 0x0, 0x0}, 1}, + {0xF9CA, {0x6D41, 0x0, 0x0}, 1}, + {0xF9CB, {0x6E9C, 0x0, 0x0}, 1}, + {0xF9CC, {0x7409, 0x0, 0x0}, 1}, + {0xF9CD, {0x7559, 0x0, 0x0}, 1}, + {0xF9CE, {0x786B, 0x0, 0x0}, 1}, + {0xF9CF, {0x7D10, 0x0, 0x0}, 1}, + {0xF9D0, {0x985E, 0x0, 0x0}, 1}, + {0xF9D1, {0x516D, 0x0, 0x0}, 1}, + {0xF9D2, {0x622E, 0x0, 0x0}, 1}, + {0xF9D3, {0x9678, 0x0, 0x0}, 1}, + {0xF9D4, {0x502B, 0x0, 0x0}, 1}, + {0xF9D5, {0x5D19, 0x0, 0x0}, 1}, + {0xF9D6, {0x6DEA, 0x0, 0x0}, 1}, + {0xF9D7, {0x8F2A, 0x0, 0x0}, 1}, + {0xF9D8, {0x5F8B, 0x0, 0x0}, 1}, + {0xF9D9, {0x6144, 0x0, 0x0}, 1}, + {0xF9DA, {0x6817, 0x0, 0x0}, 1}, + {0xF9DB, {0x7387, 0x0, 0x0}, 1}, + {0xF9DC, {0x9686, 0x0, 0x0}, 1}, + {0xF9DD, {0x5229, 0x0, 0x0}, 1}, + {0xF9DE, {0x540F, 0x0, 0x0}, 1}, + {0xF9DF, {0x5C65, 0x0, 0x0}, 1}, + {0xF9E0, {0x6613, 0x0, 0x0}, 1}, + {0xF9E1, {0x674E, 0x0, 0x0}, 1}, + {0xF9E2, {0x68A8, 0x0, 0x0}, 1}, + {0xF9E3, {0x6CE5, 0x0, 0x0}, 1}, + {0xF9E4, {0x7406, 0x0, 0x0}, 1}, + {0xF9E5, {0x75E2, 0x0, 0x0}, 1}, + {0xF9E6, {0x7F79, 0x0, 0x0}, 1}, + {0xF9E7, {0x88CF, 0x0, 0x0}, 1}, + {0xF9E8, {0x88E1, 0x0, 0x0}, 1}, + {0xF9E9, {0x91CC, 0x0, 0x0}, 1}, + {0xF9EA, {0x96E2, 0x0, 0x0}, 1}, + {0xF9EB, {0x533F, 0x0, 0x0}, 1}, + {0xF9EC, {0x6EBA, 0x0, 0x0}, 1}, + {0xF9ED, {0x541D, 0x0, 0x0}, 1}, + {0xF9EE, {0x71D0, 0x0, 0x0}, 1}, + {0xF9EF, {0x7498, 0x0, 0x0}, 1}, + {0xF9F0, {0x85FA, 0x0, 0x0}, 1}, + {0xF9F1, {0x96A3, 0x0, 0x0}, 1}, + {0xF9F2, {0x9C57, 0x0, 0x0}, 1}, + {0xF9F3, {0x9E9F, 0x0, 0x0}, 1}, + {0xF9F4, {0x6797, 0x0, 0x0}, 1}, + {0xF9F5, {0x6DCB, 0x0, 0x0}, 1}, + {0xF9F6, {0x81E8, 0x0, 0x0}, 1}, + {0xF9F7, {0x7ACB, 0x0, 0x0}, 1}, + {0xF9F8, {0x7B20, 0x0, 0x0}, 1}, + {0xF9F9, {0x7C92, 0x0, 0x0}, 1}, + {0xF9FA, {0x72C0, 0x0, 0x0}, 1}, + {0xF9FB, {0x7099, 0x0, 0x0}, 1}, + {0xF9FC, {0x8B58, 0x0, 0x0}, 1}, + {0xF9FD, {0x4EC0, 0x0, 0x0}, 1}, + {0xF9FE, {0x8336, 0x0, 0x0}, 1}, + {0xF9FF, {0x523A, 0x0, 0x0}, 1}, + {0xFA00, {0x5207, 0x0, 0x0}, 1}, + {0xFA01, {0x5EA6, 0x0, 0x0}, 1}, + {0xFA02, {0x62D3, 0x0, 0x0}, 1}, + {0xFA03, {0x7CD6, 0x0, 0x0}, 1}, + {0xFA04, {0x5B85, 0x0, 0x0}, 1}, + {0xFA05, {0x6D1E, 0x0, 0x0}, 1}, + {0xFA06, {0x66B4, 0x0, 0x0}, 1}, + {0xFA07, {0x8F3B, 0x0, 0x0}, 1}, + {0xFA08, {0x884C, 0x0, 0x0}, 1}, + {0xFA09, {0x964D, 0x0, 0x0}, 1}, + {0xFA0A, {0x898B, 0x0, 0x0}, 1}, + {0xFA0B, {0x5ED3, 0x0, 0x0}, 1}, + {0xFA0C, {0x5140, 0x0, 0x0}, 1}, + {0xFA0D, {0x55C0, 0x0, 0x0}, 1}, + {0xFA10, {0x585A, 0x0, 0x0}, 1}, + {0xFA12, {0x6674, 0x0, 0x0}, 1}, + {0xFA15, {0x51DE, 0x0, 0x0}, 1}, + {0xFA16, {0x732A, 0x0, 0x0}, 1}, + {0xFA17, {0x76CA, 0x0, 0x0}, 1}, + {0xFA18, {0x793C, 0x0, 0x0}, 1}, + {0xFA19, {0x795E, 0x0, 0x0}, 1}, + {0xFA1A, {0x7965, 0x0, 0x0}, 1}, + {0xFA1B, {0x798F, 0x0, 0x0}, 1}, + {0xFA1C, {0x9756, 0x0, 0x0}, 1}, + {0xFA1D, {0x7CBE, 0x0, 0x0}, 1}, + {0xFA1E, {0x7FBD, 0x0, 0x0}, 1}, + {0xFA20, {0x8612, 0x0, 0x0}, 1}, + {0xFA22, {0x8AF8, 0x0, 0x0}, 1}, + {0xFA25, {0x9038, 0x0, 0x0}, 1}, + {0xFA26, {0x90FD, 0x0, 0x0}, 1}, + {0xFA2A, {0x98EF, 0x0, 0x0}, 1}, + {0xFA2B, {0x98FC, 0x0, 0x0}, 1}, + {0xFA2C, {0x9928, 0x0, 0x0}, 1}, + {0xFA2D, {0x9DB4, 0x0, 0x0}, 1}, + {0xFA2E, {0x90DE, 0x0, 0x0}, 1}, + {0xFA2F, {0x96B7, 0x0, 0x0}, 1}, + {0xFA30, {0x4FAE, 0x0, 0x0}, 1}, + {0xFA31, {0x50E7, 0x0, 0x0}, 1}, + {0xFA32, {0x514D, 0x0, 0x0}, 1}, + {0xFA33, {0x52C9, 0x0, 0x0}, 1}, + {0xFA34, {0x52E4, 0x0, 0x0}, 1}, + {0xFA35, {0x5351, 0x0, 0x0}, 1}, + {0xFA36, {0x559D, 0x0, 0x0}, 1}, + {0xFA37, {0x5606, 0x0, 0x0}, 1}, + {0xFA38, {0x5668, 0x0, 0x0}, 1}, + {0xFA39, {0x5840, 0x0, 0x0}, 1}, + {0xFA3A, {0x58A8, 0x0, 0x0}, 1}, + {0xFA3B, {0x5C64, 0x0, 0x0}, 1}, + {0xFA3C, {0x5C6E, 0x0, 0x0}, 1}, + {0xFA3D, {0x6094, 0x0, 0x0}, 1}, + {0xFA3E, {0x6168, 0x0, 0x0}, 1}, + {0xFA3F, {0x618E, 0x0, 0x0}, 1}, + {0xFA40, {0x61F2, 0x0, 0x0}, 1}, + {0xFA41, {0x654F, 0x0, 0x0}, 1}, + {0xFA42, {0x65E2, 0x0, 0x0}, 1}, + {0xFA43, {0x6691, 0x0, 0x0}, 1}, + {0xFA44, {0x6885, 0x0, 0x0}, 1}, + {0xFA45, {0x6D77, 0x0, 0x0}, 1}, + {0xFA46, {0x6E1A, 0x0, 0x0}, 1}, + {0xFA47, {0x6F22, 0x0, 0x0}, 1}, + {0xFA48, {0x716E, 0x0, 0x0}, 1}, + {0xFA49, {0x722B, 0x0, 0x0}, 1}, + {0xFA4A, {0x7422, 0x0, 0x0}, 1}, + {0xFA4B, {0x7891, 0x0, 0x0}, 1}, + {0xFA4C, {0x793E, 0x0, 0x0}, 1}, + {0xFA4D, {0x7949, 0x0, 0x0}, 1}, + {0xFA4E, {0x7948, 0x0, 0x0}, 1}, + {0xFA4F, {0x7950, 0x0, 0x0}, 1}, + {0xFA50, {0x7956, 0x0, 0x0}, 1}, + {0xFA51, {0x795D, 0x0, 0x0}, 1}, + {0xFA52, {0x798D, 0x0, 0x0}, 1}, + {0xFA53, {0x798E, 0x0, 0x0}, 1}, + {0xFA54, {0x7A40, 0x0, 0x0}, 1}, + {0xFA55, {0x7A81, 0x0, 0x0}, 1}, + {0xFA56, {0x7BC0, 0x0, 0x0}, 1}, + {0xFA57, {0x7DF4, 0x0, 0x0}, 1}, + {0xFA58, {0x7E09, 0x0, 0x0}, 1}, + {0xFA59, {0x7E41, 0x0, 0x0}, 1}, + {0xFA5A, {0x7F72, 0x0, 0x0}, 1}, + {0xFA5B, {0x8005, 0x0, 0x0}, 1}, + {0xFA5C, {0x81ED, 0x0, 0x0}, 1}, + {0xFA5D, {0x8279, 0x0, 0x0}, 1}, + {0xFA5E, {0x8279, 0x0, 0x0}, 1}, + {0xFA5F, {0x8457, 0x0, 0x0}, 1}, + {0xFA60, {0x8910, 0x0, 0x0}, 1}, + {0xFA61, {0x8996, 0x0, 0x0}, 1}, + {0xFA62, {0x8B01, 0x0, 0x0}, 1}, + {0xFA63, {0x8B39, 0x0, 0x0}, 1}, + {0xFA64, {0x8CD3, 0x0, 0x0}, 1}, + {0xFA65, {0x8D08, 0x0, 0x0}, 1}, + {0xFA66, {0x8FB6, 0x0, 0x0}, 1}, + {0xFA67, {0x9038, 0x0, 0x0}, 1}, + {0xFA68, {0x96E3, 0x0, 0x0}, 1}, + {0xFA69, {0x97FF, 0x0, 0x0}, 1}, + {0xFA6A, {0x983B, 0x0, 0x0}, 1}, + {0xFA6B, {0x6075, 0x0, 0x0}, 1}, + {0xFA6C, {0x242EE, 0x0, 0x0}, 1}, + {0xFA6D, {0x8218, 0x0, 0x0}, 1}, + {0xFA70, {0x4E26, 0x0, 0x0}, 1}, + {0xFA71, {0x51B5, 0x0, 0x0}, 1}, + {0xFA72, {0x5168, 0x0, 0x0}, 1}, + {0xFA73, {0x4F80, 0x0, 0x0}, 1}, + {0xFA74, {0x5145, 0x0, 0x0}, 1}, + {0xFA75, {0x5180, 0x0, 0x0}, 1}, + {0xFA76, {0x52C7, 0x0, 0x0}, 1}, + {0xFA77, {0x52FA, 0x0, 0x0}, 1}, + {0xFA78, {0x559D, 0x0, 0x0}, 1}, + {0xFA79, {0x5555, 0x0, 0x0}, 1}, + {0xFA7A, {0x5599, 0x0, 0x0}, 1}, + {0xFA7B, {0x55E2, 0x0, 0x0}, 1}, + {0xFA7C, {0x585A, 0x0, 0x0}, 1}, + {0xFA7D, {0x58B3, 0x0, 0x0}, 1}, + {0xFA7E, {0x5944, 0x0, 0x0}, 1}, + {0xFA7F, {0x5954, 0x0, 0x0}, 1}, + {0xFA80, {0x5A62, 0x0, 0x0}, 1}, + {0xFA81, {0x5B28, 0x0, 0x0}, 1}, + {0xFA82, {0x5ED2, 0x0, 0x0}, 1}, + {0xFA83, {0x5ED9, 0x0, 0x0}, 1}, + {0xFA84, {0x5F69, 0x0, 0x0}, 1}, + {0xFA85, {0x5FAD, 0x0, 0x0}, 1}, + {0xFA86, {0x60D8, 0x0, 0x0}, 1}, + {0xFA87, {0x614E, 0x0, 0x0}, 1}, + {0xFA88, {0x6108, 0x0, 0x0}, 1}, + {0xFA89, {0x618E, 0x0, 0x0}, 1}, + {0xFA8A, {0x6160, 0x0, 0x0}, 1}, + {0xFA8B, {0x61F2, 0x0, 0x0}, 1}, + {0xFA8C, {0x6234, 0x0, 0x0}, 1}, + {0xFA8D, {0x63C4, 0x0, 0x0}, 1}, + {0xFA8E, {0x641C, 0x0, 0x0}, 1}, + {0xFA8F, {0x6452, 0x0, 0x0}, 1}, + {0xFA90, {0x6556, 0x0, 0x0}, 1}, + {0xFA91, {0x6674, 0x0, 0x0}, 1}, + {0xFA92, {0x6717, 0x0, 0x0}, 1}, + {0xFA93, {0x671B, 0x0, 0x0}, 1}, + {0xFA94, {0x6756, 0x0, 0x0}, 1}, + {0xFA95, {0x6B79, 0x0, 0x0}, 1}, + {0xFA96, {0x6BBA, 0x0, 0x0}, 1}, + {0xFA97, {0x6D41, 0x0, 0x0}, 1}, + {0xFA98, {0x6EDB, 0x0, 0x0}, 1}, + {0xFA99, {0x6ECB, 0x0, 0x0}, 1}, + {0xFA9A, {0x6F22, 0x0, 0x0}, 1}, + {0xFA9B, {0x701E, 0x0, 0x0}, 1}, + {0xFA9C, {0x716E, 0x0, 0x0}, 1}, + {0xFA9D, {0x77A7, 0x0, 0x0}, 1}, + {0xFA9E, {0x7235, 0x0, 0x0}, 1}, + {0xFA9F, {0x72AF, 0x0, 0x0}, 1}, + {0xFAA0, {0x732A, 0x0, 0x0}, 1}, + {0xFAA1, {0x7471, 0x0, 0x0}, 1}, + {0xFAA2, {0x7506, 0x0, 0x0}, 1}, + {0xFAA3, {0x753B, 0x0, 0x0}, 1}, + {0xFAA4, {0x761D, 0x0, 0x0}, 1}, + {0xFAA5, {0x761F, 0x0, 0x0}, 1}, + {0xFAA6, {0x76CA, 0x0, 0x0}, 1}, + {0xFAA7, {0x76DB, 0x0, 0x0}, 1}, + {0xFAA8, {0x76F4, 0x0, 0x0}, 1}, + {0xFAA9, {0x774A, 0x0, 0x0}, 1}, + {0xFAAA, {0x7740, 0x0, 0x0}, 1}, + {0xFAAB, {0x78CC, 0x0, 0x0}, 1}, + {0xFAAC, {0x7AB1, 0x0, 0x0}, 1}, + {0xFAAD, {0x7BC0, 0x0, 0x0}, 1}, + {0xFAAE, {0x7C7B, 0x0, 0x0}, 1}, + {0xFAAF, {0x7D5B, 0x0, 0x0}, 1}, + {0xFAB0, {0x7DF4, 0x0, 0x0}, 1}, + {0xFAB1, {0x7F3E, 0x0, 0x0}, 1}, + {0xFAB2, {0x8005, 0x0, 0x0}, 1}, + {0xFAB3, {0x8352, 0x0, 0x0}, 1}, + {0xFAB4, {0x83EF, 0x0, 0x0}, 1}, + {0xFAB5, {0x8779, 0x0, 0x0}, 1}, + {0xFAB6, {0x8941, 0x0, 0x0}, 1}, + {0xFAB7, {0x8986, 0x0, 0x0}, 1}, + {0xFAB8, {0x8996, 0x0, 0x0}, 1}, + {0xFAB9, {0x8ABF, 0x0, 0x0}, 1}, + {0xFABA, {0x8AF8, 0x0, 0x0}, 1}, + {0xFABB, {0x8ACB, 0x0, 0x0}, 1}, + {0xFABC, {0x8B01, 0x0, 0x0}, 1}, + {0xFABD, {0x8AFE, 0x0, 0x0}, 1}, + {0xFABE, {0x8AED, 0x0, 0x0}, 1}, + {0xFABF, {0x8B39, 0x0, 0x0}, 1}, + {0xFAC0, {0x8B8A, 0x0, 0x0}, 1}, + {0xFAC1, {0x8D08, 0x0, 0x0}, 1}, + {0xFAC2, {0x8F38, 0x0, 0x0}, 1}, + {0xFAC3, {0x9072, 0x0, 0x0}, 1}, + {0xFAC4, {0x9199, 0x0, 0x0}, 1}, + {0xFAC5, {0x9276, 0x0, 0x0}, 1}, + {0xFAC6, {0x967C, 0x0, 0x0}, 1}, + {0xFAC7, {0x96E3, 0x0, 0x0}, 1}, + {0xFAC8, {0x9756, 0x0, 0x0}, 1}, + {0xFAC9, {0x97DB, 0x0, 0x0}, 1}, + {0xFACA, {0x97FF, 0x0, 0x0}, 1}, + {0xFACB, {0x980B, 0x0, 0x0}, 1}, + {0xFACC, {0x983B, 0x0, 0x0}, 1}, + {0xFACD, {0x9B12, 0x0, 0x0}, 1}, + {0xFACE, {0x9F9C, 0x0, 0x0}, 1}, + {0xFACF, {0x2284A, 0x0, 0x0}, 1}, + {0xFAD0, {0x22844, 0x0, 0x0}, 1}, + {0xFAD1, {0x233D5, 0x0, 0x0}, 1}, + {0xFAD2, {0x3B9D, 0x0, 0x0}, 1}, + {0xFAD3, {0x4018, 0x0, 0x0}, 1}, + {0xFAD4, {0x4039, 0x0, 0x0}, 1}, + {0xFAD5, {0x25249, 0x0, 0x0}, 1}, + {0xFAD6, {0x25CD0, 0x0, 0x0}, 1}, + {0xFAD7, {0x27ED3, 0x0, 0x0}, 1}, + {0xFAD8, {0x9F43, 0x0, 0x0}, 1}, + {0xFAD9, {0x9F8E, 0x0, 0x0}, 1}, + {0xFB1D, {0x5D9, 0x0, 0x0}, 1}, + {0xFB1E, {0x0, 0x0, 0x0}, 0}, + {0xFB1F, {0x5F2, 0x0, 0x0}, 1}, + {0xFB2A, {0x5E9, 0x0, 0x0}, 1}, + {0xFB2B, {0x5E9, 0x0, 0x0}, 1}, + {0xFB2C, {0x5E9, 0x0, 0x0}, 1}, + {0xFB2D, {0x5E9, 0x0, 0x0}, 1}, + {0xFB2E, {0x5D0, 0x0, 0x0}, 1}, + {0xFB2F, {0x5D0, 0x0, 0x0}, 1}, + {0xFB30, {0x5D0, 0x0, 0x0}, 1}, + {0xFB31, {0x5D1, 0x0, 0x0}, 1}, + {0xFB32, {0x5D2, 0x0, 0x0}, 1}, + {0xFB33, {0x5D3, 0x0, 0x0}, 1}, + {0xFB34, {0x5D4, 0x0, 0x0}, 1}, + {0xFB35, {0x5D5, 0x0, 0x0}, 1}, + {0xFB36, {0x5D6, 0x0, 0x0}, 1}, + {0xFB38, {0x5D8, 0x0, 0x0}, 1}, + {0xFB39, {0x5D9, 0x0, 0x0}, 1}, + {0xFB3A, {0x5DA, 0x0, 0x0}, 1}, + {0xFB3B, {0x5DB, 0x0, 0x0}, 1}, + {0xFB3C, {0x5DC, 0x0, 0x0}, 1}, + {0xFB3E, {0x5DE, 0x0, 0x0}, 1}, + {0xFB40, {0x5E0, 0x0, 0x0}, 1}, + {0xFB41, {0x5E1, 0x0, 0x0}, 1}, + {0xFB43, {0x5E3, 0x0, 0x0}, 1}, + {0xFB44, {0x5E4, 0x0, 0x0}, 1}, + {0xFB46, {0x5E6, 0x0, 0x0}, 1}, + {0xFB47, {0x5E7, 0x0, 0x0}, 1}, + {0xFB48, {0x5E8, 0x0, 0x0}, 1}, + {0xFB49, {0x5E9, 0x0, 0x0}, 1}, + {0xFB4A, {0x5EA, 0x0, 0x0}, 1}, + {0xFB4B, {0x5D5, 0x0, 0x0}, 1}, + {0xFB4C, {0x5D1, 0x0, 0x0}, 1}, + {0xFB4D, {0x5DB, 0x0, 0x0}, 1}, + {0xFB4E, {0x5E4, 0x0, 0x0}, 1}, + {0xFE00, {0x0, 0x0, 0x0}, 0}, + {0xFE01, {0x0, 0x0, 0x0}, 0}, + {0xFE02, {0x0, 0x0, 0x0}, 0}, + {0xFE03, {0x0, 0x0, 0x0}, 0}, + {0xFE04, {0x0, 0x0, 0x0}, 0}, + {0xFE05, {0x0, 0x0, 0x0}, 0}, + {0xFE06, {0x0, 0x0, 0x0}, 0}, + {0xFE07, {0x0, 0x0, 0x0}, 0}, + {0xFE08, {0x0, 0x0, 0x0}, 0}, + {0xFE09, {0x0, 0x0, 0x0}, 0}, + {0xFE0A, {0x0, 0x0, 0x0}, 0}, + {0xFE0B, {0x0, 0x0, 0x0}, 0}, + {0xFE0C, {0x0, 0x0, 0x0}, 0}, + {0xFE0D, {0x0, 0x0, 0x0}, 0}, + {0xFE0E, {0x0, 0x0, 0x0}, 0}, + {0xFE0F, {0x0, 0x0, 0x0}, 0}, + {0xFE20, {0x0, 0x0, 0x0}, 0}, + {0xFE21, {0x0, 0x0, 0x0}, 0}, + {0xFE22, {0x0, 0x0, 0x0}, 0}, + {0xFE23, {0x0, 0x0, 0x0}, 0}, + {0xFE24, {0x0, 0x0, 0x0}, 0}, + {0xFE25, {0x0, 0x0, 0x0}, 0}, + {0xFE26, {0x0, 0x0, 0x0}, 0}, + {0xFE27, {0x0, 0x0, 0x0}, 0}, + {0xFE28, {0x0, 0x0, 0x0}, 0}, + {0xFE29, {0x0, 0x0, 0x0}, 0}, + {0xFE2A, {0x0, 0x0, 0x0}, 0}, + {0xFE2B, {0x0, 0x0, 0x0}, 0}, + {0xFE2C, {0x0, 0x0, 0x0}, 0}, + {0xFE2D, {0x0, 0x0, 0x0}, 0}, + {0xFE2E, {0x0, 0x0, 0x0}, 0}, + {0xFE2F, {0x0, 0x0, 0x0}, 0}, + {0xFF21, {0xFF41, 0x0, 0x0}, 1}, + {0xFF22, {0xFF42, 0x0, 0x0}, 1}, + {0xFF23, {0xFF43, 0x0, 0x0}, 1}, + {0xFF24, {0xFF44, 0x0, 0x0}, 1}, + {0xFF25, {0xFF45, 0x0, 0x0}, 1}, + {0xFF26, {0xFF46, 0x0, 0x0}, 1}, + {0xFF27, {0xFF47, 0x0, 0x0}, 1}, + {0xFF28, {0xFF48, 0x0, 0x0}, 1}, + {0xFF29, {0xFF49, 0x0, 0x0}, 1}, + {0xFF2A, {0xFF4A, 0x0, 0x0}, 1}, + {0xFF2B, {0xFF4B, 0x0, 0x0}, 1}, + {0xFF2C, {0xFF4C, 0x0, 0x0}, 1}, + {0xFF2D, {0xFF4D, 0x0, 0x0}, 1}, + {0xFF2E, {0xFF4E, 0x0, 0x0}, 1}, + {0xFF2F, {0xFF4F, 0x0, 0x0}, 1}, + {0xFF30, {0xFF50, 0x0, 0x0}, 1}, + {0xFF31, {0xFF51, 0x0, 0x0}, 1}, + {0xFF32, {0xFF52, 0x0, 0x0}, 1}, + {0xFF33, {0xFF53, 0x0, 0x0}, 1}, + {0xFF34, {0xFF54, 0x0, 0x0}, 1}, + {0xFF35, {0xFF55, 0x0, 0x0}, 1}, + {0xFF36, {0xFF56, 0x0, 0x0}, 1}, + {0xFF37, {0xFF57, 0x0, 0x0}, 1}, + {0xFF38, {0xFF58, 0x0, 0x0}, 1}, + {0xFF39, {0xFF59, 0x0, 0x0}, 1}, + {0xFF3A, {0xFF5A, 0x0, 0x0}, 1}, + {0x101FD, {0x0, 0x0, 0x0}, 0}, + {0x102E0, {0x0, 0x0, 0x0}, 0}, + {0x10376, {0x0, 0x0, 0x0}, 0}, + {0x10377, {0x0, 0x0, 0x0}, 0}, + {0x10378, {0x0, 0x0, 0x0}, 0}, + {0x10379, {0x0, 0x0, 0x0}, 0}, + {0x1037A, {0x0, 0x0, 0x0}, 0}, + {0x10400, {0x10428, 0x0, 0x0}, 1}, + {0x10401, {0x10429, 0x0, 0x0}, 1}, + {0x10402, {0x1042A, 0x0, 0x0}, 1}, + {0x10403, {0x1042B, 0x0, 0x0}, 1}, + {0x10404, {0x1042C, 0x0, 0x0}, 1}, + {0x10405, {0x1042D, 0x0, 0x0}, 1}, + {0x10406, {0x1042E, 0x0, 0x0}, 1}, + {0x10407, {0x1042F, 0x0, 0x0}, 1}, + {0x10408, {0x10430, 0x0, 0x0}, 1}, + {0x10409, {0x10431, 0x0, 0x0}, 1}, + {0x1040A, {0x10432, 0x0, 0x0}, 1}, + {0x1040B, {0x10433, 0x0, 0x0}, 1}, + {0x1040C, {0x10434, 0x0, 0x0}, 1}, + {0x1040D, {0x10435, 0x0, 0x0}, 1}, + {0x1040E, {0x10436, 0x0, 0x0}, 1}, + {0x1040F, {0x10437, 0x0, 0x0}, 1}, + {0x10410, {0x10438, 0x0, 0x0}, 1}, + {0x10411, {0x10439, 0x0, 0x0}, 1}, + {0x10412, {0x1043A, 0x0, 0x0}, 1}, + {0x10413, {0x1043B, 0x0, 0x0}, 1}, + {0x10414, {0x1043C, 0x0, 0x0}, 1}, + {0x10415, {0x1043D, 0x0, 0x0}, 1}, + {0x10416, {0x1043E, 0x0, 0x0}, 1}, + {0x10417, {0x1043F, 0x0, 0x0}, 1}, + {0x10418, {0x10440, 0x0, 0x0}, 1}, + {0x10419, {0x10441, 0x0, 0x0}, 1}, + {0x1041A, {0x10442, 0x0, 0x0}, 1}, + {0x1041B, {0x10443, 0x0, 0x0}, 1}, + {0x1041C, {0x10444, 0x0, 0x0}, 1}, + {0x1041D, {0x10445, 0x0, 0x0}, 1}, + {0x1041E, {0x10446, 0x0, 0x0}, 1}, + {0x1041F, {0x10447, 0x0, 0x0}, 1}, + {0x10420, {0x10448, 0x0, 0x0}, 1}, + {0x10421, {0x10449, 0x0, 0x0}, 1}, + {0x10422, {0x1044A, 0x0, 0x0}, 1}, + {0x10423, {0x1044B, 0x0, 0x0}, 1}, + {0x10424, {0x1044C, 0x0, 0x0}, 1}, + {0x10425, {0x1044D, 0x0, 0x0}, 1}, + {0x10426, {0x1044E, 0x0, 0x0}, 1}, + {0x10427, {0x1044F, 0x0, 0x0}, 1}, + {0x104B0, {0x104D8, 0x0, 0x0}, 1}, + {0x104B1, {0x104D9, 0x0, 0x0}, 1}, + {0x104B2, {0x104DA, 0x0, 0x0}, 1}, + {0x104B3, {0x104DB, 0x0, 0x0}, 1}, + {0x104B4, {0x104DC, 0x0, 0x0}, 1}, + {0x104B5, {0x104DD, 0x0, 0x0}, 1}, + {0x104B6, {0x104DE, 0x0, 0x0}, 1}, + {0x104B7, {0x104DF, 0x0, 0x0}, 1}, + {0x104B8, {0x104E0, 0x0, 0x0}, 1}, + {0x104B9, {0x104E1, 0x0, 0x0}, 1}, + {0x104BA, {0x104E2, 0x0, 0x0}, 1}, + {0x104BB, {0x104E3, 0x0, 0x0}, 1}, + {0x104BC, {0x104E4, 0x0, 0x0}, 1}, + {0x104BD, {0x104E5, 0x0, 0x0}, 1}, + {0x104BE, {0x104E6, 0x0, 0x0}, 1}, + {0x104BF, {0x104E7, 0x0, 0x0}, 1}, + {0x104C0, {0x104E8, 0x0, 0x0}, 1}, + {0x104C1, {0x104E9, 0x0, 0x0}, 1}, + {0x104C2, {0x104EA, 0x0, 0x0}, 1}, + {0x104C3, {0x104EB, 0x0, 0x0}, 1}, + {0x104C4, {0x104EC, 0x0, 0x0}, 1}, + {0x104C5, {0x104ED, 0x0, 0x0}, 1}, + {0x104C6, {0x104EE, 0x0, 0x0}, 1}, + {0x104C7, {0x104EF, 0x0, 0x0}, 1}, + {0x104C8, {0x104F0, 0x0, 0x0}, 1}, + {0x104C9, {0x104F1, 0x0, 0x0}, 1}, + {0x104CA, {0x104F2, 0x0, 0x0}, 1}, + {0x104CB, {0x104F3, 0x0, 0x0}, 1}, + {0x104CC, {0x104F4, 0x0, 0x0}, 1}, + {0x104CD, {0x104F5, 0x0, 0x0}, 1}, + {0x104CE, {0x104F6, 0x0, 0x0}, 1}, + {0x104CF, {0x104F7, 0x0, 0x0}, 1}, + {0x104D0, {0x104F8, 0x0, 0x0}, 1}, + {0x104D1, {0x104F9, 0x0, 0x0}, 1}, + {0x104D2, {0x104FA, 0x0, 0x0}, 1}, + {0x104D3, {0x104FB, 0x0, 0x0}, 1}, + {0x10A01, {0x0, 0x0, 0x0}, 0}, + {0x10A02, {0x0, 0x0, 0x0}, 0}, + {0x10A03, {0x0, 0x0, 0x0}, 0}, + {0x10A05, {0x0, 0x0, 0x0}, 0}, + {0x10A06, {0x0, 0x0, 0x0}, 0}, + {0x10A0C, {0x0, 0x0, 0x0}, 0}, + {0x10A0D, {0x0, 0x0, 0x0}, 0}, + {0x10A0E, {0x0, 0x0, 0x0}, 0}, + {0x10A0F, {0x0, 0x0, 0x0}, 0}, + {0x10A38, {0x0, 0x0, 0x0}, 0}, + {0x10A39, {0x0, 0x0, 0x0}, 0}, + {0x10A3A, {0x0, 0x0, 0x0}, 0}, + {0x10A3F, {0x0, 0x0, 0x0}, 0}, + {0x10AE5, {0x0, 0x0, 0x0}, 0}, + {0x10AE6, {0x0, 0x0, 0x0}, 0}, + {0x10C80, {0x10CC0, 0x0, 0x0}, 1}, + {0x10C81, {0x10CC1, 0x0, 0x0}, 1}, + {0x10C82, {0x10CC2, 0x0, 0x0}, 1}, + {0x10C83, {0x10CC3, 0x0, 0x0}, 1}, + {0x10C84, {0x10CC4, 0x0, 0x0}, 1}, + {0x10C85, {0x10CC5, 0x0, 0x0}, 1}, + {0x10C86, {0x10CC6, 0x0, 0x0}, 1}, + {0x10C87, {0x10CC7, 0x0, 0x0}, 1}, + {0x10C88, {0x10CC8, 0x0, 0x0}, 1}, + {0x10C89, {0x10CC9, 0x0, 0x0}, 1}, + {0x10C8A, {0x10CCA, 0x0, 0x0}, 1}, + {0x10C8B, {0x10CCB, 0x0, 0x0}, 1}, + {0x10C8C, {0x10CCC, 0x0, 0x0}, 1}, + {0x10C8D, {0x10CCD, 0x0, 0x0}, 1}, + {0x10C8E, {0x10CCE, 0x0, 0x0}, 1}, + {0x10C8F, {0x10CCF, 0x0, 0x0}, 1}, + {0x10C90, {0x10CD0, 0x0, 0x0}, 1}, + {0x10C91, {0x10CD1, 0x0, 0x0}, 1}, + {0x10C92, {0x10CD2, 0x0, 0x0}, 1}, + {0x10C93, {0x10CD3, 0x0, 0x0}, 1}, + {0x10C94, {0x10CD4, 0x0, 0x0}, 1}, + {0x10C95, {0x10CD5, 0x0, 0x0}, 1}, + {0x10C96, {0x10CD6, 0x0, 0x0}, 1}, + {0x10C97, {0x10CD7, 0x0, 0x0}, 1}, + {0x10C98, {0x10CD8, 0x0, 0x0}, 1}, + {0x10C99, {0x10CD9, 0x0, 0x0}, 1}, + {0x10C9A, {0x10CDA, 0x0, 0x0}, 1}, + {0x10C9B, {0x10CDB, 0x0, 0x0}, 1}, + {0x10C9C, {0x10CDC, 0x0, 0x0}, 1}, + {0x10C9D, {0x10CDD, 0x0, 0x0}, 1}, + {0x10C9E, {0x10CDE, 0x0, 0x0}, 1}, + {0x10C9F, {0x10CDF, 0x0, 0x0}, 1}, + {0x10CA0, {0x10CE0, 0x0, 0x0}, 1}, + {0x10CA1, {0x10CE1, 0x0, 0x0}, 1}, + {0x10CA2, {0x10CE2, 0x0, 0x0}, 1}, + {0x10CA3, {0x10CE3, 0x0, 0x0}, 1}, + {0x10CA4, {0x10CE4, 0x0, 0x0}, 1}, + {0x10CA5, {0x10CE5, 0x0, 0x0}, 1}, + {0x10CA6, {0x10CE6, 0x0, 0x0}, 1}, + {0x10CA7, {0x10CE7, 0x0, 0x0}, 1}, + {0x10CA8, {0x10CE8, 0x0, 0x0}, 1}, + {0x10CA9, {0x10CE9, 0x0, 0x0}, 1}, + {0x10CAA, {0x10CEA, 0x0, 0x0}, 1}, + {0x10CAB, {0x10CEB, 0x0, 0x0}, 1}, + {0x10CAC, {0x10CEC, 0x0, 0x0}, 1}, + {0x10CAD, {0x10CED, 0x0, 0x0}, 1}, + {0x10CAE, {0x10CEE, 0x0, 0x0}, 1}, + {0x10CAF, {0x10CEF, 0x0, 0x0}, 1}, + {0x10CB0, {0x10CF0, 0x0, 0x0}, 1}, + {0x10CB1, {0x10CF1, 0x0, 0x0}, 1}, + {0x10CB2, {0x10CF2, 0x0, 0x0}, 1}, + {0x10D24, {0x0, 0x0, 0x0}, 0}, + {0x10D25, {0x0, 0x0, 0x0}, 0}, + {0x10D26, {0x0, 0x0, 0x0}, 0}, + {0x10D27, {0x0, 0x0, 0x0}, 0}, + {0x10EAB, {0x0, 0x0, 0x0}, 0}, + {0x10EAC, {0x0, 0x0, 0x0}, 0}, + {0x10F46, {0x0, 0x0, 0x0}, 0}, + {0x10F47, {0x0, 0x0, 0x0}, 0}, + {0x10F48, {0x0, 0x0, 0x0}, 0}, + {0x10F49, {0x0, 0x0, 0x0}, 0}, + {0x10F4A, {0x0, 0x0, 0x0}, 0}, + {0x10F4B, {0x0, 0x0, 0x0}, 0}, + {0x10F4C, {0x0, 0x0, 0x0}, 0}, + {0x10F4D, {0x0, 0x0, 0x0}, 0}, + {0x10F4E, {0x0, 0x0, 0x0}, 0}, + {0x10F4F, {0x0, 0x0, 0x0}, 0}, + {0x10F50, {0x0, 0x0, 0x0}, 0}, + {0x11001, {0x0, 0x0, 0x0}, 0}, + {0x11038, {0x0, 0x0, 0x0}, 0}, + {0x11039, {0x0, 0x0, 0x0}, 0}, + {0x1103A, {0x0, 0x0, 0x0}, 0}, + {0x1103B, {0x0, 0x0, 0x0}, 0}, + {0x1103C, {0x0, 0x0, 0x0}, 0}, + {0x1103D, {0x0, 0x0, 0x0}, 0}, + {0x1103E, {0x0, 0x0, 0x0}, 0}, + {0x1103F, {0x0, 0x0, 0x0}, 0}, + {0x11040, {0x0, 0x0, 0x0}, 0}, + {0x11041, {0x0, 0x0, 0x0}, 0}, + {0x11042, {0x0, 0x0, 0x0}, 0}, + {0x11043, {0x0, 0x0, 0x0}, 0}, + {0x11044, {0x0, 0x0, 0x0}, 0}, + {0x11045, {0x0, 0x0, 0x0}, 0}, + {0x11046, {0x0, 0x0, 0x0}, 0}, + {0x1107F, {0x0, 0x0, 0x0}, 0}, + {0x11080, {0x0, 0x0, 0x0}, 0}, + {0x11081, {0x0, 0x0, 0x0}, 0}, + {0x1109A, {0x11099, 0x0, 0x0}, 1}, + {0x1109C, {0x1109B, 0x0, 0x0}, 1}, + {0x110AB, {0x110A5, 0x0, 0x0}, 1}, + {0x110B3, {0x0, 0x0, 0x0}, 0}, + {0x110B4, {0x0, 0x0, 0x0}, 0}, + {0x110B5, {0x0, 0x0, 0x0}, 0}, + {0x110B6, {0x0, 0x0, 0x0}, 0}, + {0x110B9, {0x0, 0x0, 0x0}, 0}, + {0x110BA, {0x0, 0x0, 0x0}, 0}, + {0x11100, {0x0, 0x0, 0x0}, 0}, + {0x11101, {0x0, 0x0, 0x0}, 0}, + {0x11102, {0x0, 0x0, 0x0}, 0}, + {0x11127, {0x0, 0x0, 0x0}, 0}, + {0x11128, {0x0, 0x0, 0x0}, 0}, + {0x11129, {0x0, 0x0, 0x0}, 0}, + {0x1112A, {0x0, 0x0, 0x0}, 0}, + {0x1112B, {0x0, 0x0, 0x0}, 0}, + {0x1112D, {0x0, 0x0, 0x0}, 0}, + {0x1112E, {0x0, 0x0, 0x0}, 0}, + {0x1112F, {0x0, 0x0, 0x0}, 0}, + {0x11130, {0x0, 0x0, 0x0}, 0}, + {0x11131, {0x0, 0x0, 0x0}, 0}, + {0x11132, {0x0, 0x0, 0x0}, 0}, + {0x11133, {0x0, 0x0, 0x0}, 0}, + {0x11134, {0x0, 0x0, 0x0}, 0}, + {0x11173, {0x0, 0x0, 0x0}, 0}, + {0x11180, {0x0, 0x0, 0x0}, 0}, + {0x11181, {0x0, 0x0, 0x0}, 0}, + {0x111B6, {0x0, 0x0, 0x0}, 0}, + {0x111B7, {0x0, 0x0, 0x0}, 0}, + {0x111B8, {0x0, 0x0, 0x0}, 0}, + {0x111B9, {0x0, 0x0, 0x0}, 0}, + {0x111BA, {0x0, 0x0, 0x0}, 0}, + {0x111BB, {0x0, 0x0, 0x0}, 0}, + {0x111BC, {0x0, 0x0, 0x0}, 0}, + {0x111BD, {0x0, 0x0, 0x0}, 0}, + {0x111BE, {0x0, 0x0, 0x0}, 0}, + {0x111C9, {0x0, 0x0, 0x0}, 0}, + {0x111CA, {0x0, 0x0, 0x0}, 0}, + {0x111CB, {0x0, 0x0, 0x0}, 0}, + {0x111CC, {0x0, 0x0, 0x0}, 0}, + {0x111CF, {0x0, 0x0, 0x0}, 0}, + {0x1122F, {0x0, 0x0, 0x0}, 0}, + {0x11230, {0x0, 0x0, 0x0}, 0}, + {0x11231, {0x0, 0x0, 0x0}, 0}, + {0x11234, {0x0, 0x0, 0x0}, 0}, + {0x11236, {0x0, 0x0, 0x0}, 0}, + {0x11237, {0x0, 0x0, 0x0}, 0}, + {0x1123E, {0x0, 0x0, 0x0}, 0}, + {0x112DF, {0x0, 0x0, 0x0}, 0}, + {0x112E3, {0x0, 0x0, 0x0}, 0}, + {0x112E4, {0x0, 0x0, 0x0}, 0}, + {0x112E5, {0x0, 0x0, 0x0}, 0}, + {0x112E6, {0x0, 0x0, 0x0}, 0}, + {0x112E7, {0x0, 0x0, 0x0}, 0}, + {0x112E8, {0x0, 0x0, 0x0}, 0}, + {0x112E9, {0x0, 0x0, 0x0}, 0}, + {0x112EA, {0x0, 0x0, 0x0}, 0}, + {0x11300, {0x0, 0x0, 0x0}, 0}, + {0x11301, {0x0, 0x0, 0x0}, 0}, + {0x1133B, {0x0, 0x0, 0x0}, 0}, + {0x1133C, {0x0, 0x0, 0x0}, 0}, + {0x11340, {0x0, 0x0, 0x0}, 0}, + {0x1134B, {0x11347, 0x1133E, 0x0}, 2}, + {0x1134C, {0x11347, 0x11357, 0x0}, 2}, + {0x11366, {0x0, 0x0, 0x0}, 0}, + {0x11367, {0x0, 0x0, 0x0}, 0}, + {0x11368, {0x0, 0x0, 0x0}, 0}, + {0x11369, {0x0, 0x0, 0x0}, 0}, + {0x1136A, {0x0, 0x0, 0x0}, 0}, + {0x1136B, {0x0, 0x0, 0x0}, 0}, + {0x1136C, {0x0, 0x0, 0x0}, 0}, + {0x11370, {0x0, 0x0, 0x0}, 0}, + {0x11371, {0x0, 0x0, 0x0}, 0}, + {0x11372, {0x0, 0x0, 0x0}, 0}, + {0x11373, {0x0, 0x0, 0x0}, 0}, + {0x11374, {0x0, 0x0, 0x0}, 0}, + {0x11438, {0x0, 0x0, 0x0}, 0}, + {0x11439, {0x0, 0x0, 0x0}, 0}, + {0x1143A, {0x0, 0x0, 0x0}, 0}, + {0x1143B, {0x0, 0x0, 0x0}, 0}, + {0x1143C, {0x0, 0x0, 0x0}, 0}, + {0x1143D, {0x0, 0x0, 0x0}, 0}, + {0x1143E, {0x0, 0x0, 0x0}, 0}, + {0x1143F, {0x0, 0x0, 0x0}, 0}, + {0x11442, {0x0, 0x0, 0x0}, 0}, + {0x11443, {0x0, 0x0, 0x0}, 0}, + {0x11444, {0x0, 0x0, 0x0}, 0}, + {0x11446, {0x0, 0x0, 0x0}, 0}, + {0x1145E, {0x0, 0x0, 0x0}, 0}, + {0x114B3, {0x0, 0x0, 0x0}, 0}, + {0x114B4, {0x0, 0x0, 0x0}, 0}, + {0x114B5, {0x0, 0x0, 0x0}, 0}, + {0x114B6, {0x0, 0x0, 0x0}, 0}, + {0x114B7, {0x0, 0x0, 0x0}, 0}, + {0x114B8, {0x0, 0x0, 0x0}, 0}, + {0x114BA, {0x0, 0x0, 0x0}, 0}, + {0x114BB, {0x114B9, 0x0, 0x0}, 1}, + {0x114BC, {0x114B9, 0x114B0, 0x0}, 2}, + {0x114BE, {0x114B9, 0x114BD, 0x0}, 2}, + {0x114BF, {0x0, 0x0, 0x0}, 0}, + {0x114C0, {0x0, 0x0, 0x0}, 0}, + {0x114C2, {0x0, 0x0, 0x0}, 0}, + {0x114C3, {0x0, 0x0, 0x0}, 0}, + {0x115B2, {0x0, 0x0, 0x0}, 0}, + {0x115B3, {0x0, 0x0, 0x0}, 0}, + {0x115B4, {0x0, 0x0, 0x0}, 0}, + {0x115B5, {0x0, 0x0, 0x0}, 0}, + {0x115BA, {0x115B8, 0x115AF, 0x0}, 2}, + {0x115BB, {0x115B9, 0x115AF, 0x0}, 2}, + {0x115BC, {0x0, 0x0, 0x0}, 0}, + {0x115BD, {0x0, 0x0, 0x0}, 0}, + {0x115BF, {0x0, 0x0, 0x0}, 0}, + {0x115C0, {0x0, 0x0, 0x0}, 0}, + {0x115DC, {0x0, 0x0, 0x0}, 0}, + {0x115DD, {0x0, 0x0, 0x0}, 0}, + {0x11633, {0x0, 0x0, 0x0}, 0}, + {0x11634, {0x0, 0x0, 0x0}, 0}, + {0x11635, {0x0, 0x0, 0x0}, 0}, + {0x11636, {0x0, 0x0, 0x0}, 0}, + {0x11637, {0x0, 0x0, 0x0}, 0}, + {0x11638, {0x0, 0x0, 0x0}, 0}, + {0x11639, {0x0, 0x0, 0x0}, 0}, + {0x1163A, {0x0, 0x0, 0x0}, 0}, + {0x1163D, {0x0, 0x0, 0x0}, 0}, + {0x1163F, {0x0, 0x0, 0x0}, 0}, + {0x11640, {0x0, 0x0, 0x0}, 0}, + {0x116AB, {0x0, 0x0, 0x0}, 0}, + {0x116AD, {0x0, 0x0, 0x0}, 0}, + {0x116B0, {0x0, 0x0, 0x0}, 0}, + {0x116B1, {0x0, 0x0, 0x0}, 0}, + {0x116B2, {0x0, 0x0, 0x0}, 0}, + {0x116B3, {0x0, 0x0, 0x0}, 0}, + {0x116B4, {0x0, 0x0, 0x0}, 0}, + {0x116B5, {0x0, 0x0, 0x0}, 0}, + {0x116B7, {0x0, 0x0, 0x0}, 0}, + {0x1171D, {0x0, 0x0, 0x0}, 0}, + {0x1171E, {0x0, 0x0, 0x0}, 0}, + {0x1171F, {0x0, 0x0, 0x0}, 0}, + {0x11722, {0x0, 0x0, 0x0}, 0}, + {0x11723, {0x0, 0x0, 0x0}, 0}, + {0x11724, {0x0, 0x0, 0x0}, 0}, + {0x11725, {0x0, 0x0, 0x0}, 0}, + {0x11727, {0x0, 0x0, 0x0}, 0}, + {0x11728, {0x0, 0x0, 0x0}, 0}, + {0x11729, {0x0, 0x0, 0x0}, 0}, + {0x1172A, {0x0, 0x0, 0x0}, 0}, + {0x1172B, {0x0, 0x0, 0x0}, 0}, + {0x1182F, {0x0, 0x0, 0x0}, 0}, + {0x11830, {0x0, 0x0, 0x0}, 0}, + {0x11831, {0x0, 0x0, 0x0}, 0}, + {0x11832, {0x0, 0x0, 0x0}, 0}, + {0x11833, {0x0, 0x0, 0x0}, 0}, + {0x11834, {0x0, 0x0, 0x0}, 0}, + {0x11835, {0x0, 0x0, 0x0}, 0}, + {0x11836, {0x0, 0x0, 0x0}, 0}, + {0x11837, {0x0, 0x0, 0x0}, 0}, + {0x11839, {0x0, 0x0, 0x0}, 0}, + {0x1183A, {0x0, 0x0, 0x0}, 0}, + {0x118A0, {0x118C0, 0x0, 0x0}, 1}, + {0x118A1, {0x118C1, 0x0, 0x0}, 1}, + {0x118A2, {0x118C2, 0x0, 0x0}, 1}, + {0x118A3, {0x118C3, 0x0, 0x0}, 1}, + {0x118A4, {0x118C4, 0x0, 0x0}, 1}, + {0x118A5, {0x118C5, 0x0, 0x0}, 1}, + {0x118A6, {0x118C6, 0x0, 0x0}, 1}, + {0x118A7, {0x118C7, 0x0, 0x0}, 1}, + {0x118A8, {0x118C8, 0x0, 0x0}, 1}, + {0x118A9, {0x118C9, 0x0, 0x0}, 1}, + {0x118AA, {0x118CA, 0x0, 0x0}, 1}, + {0x118AB, {0x118CB, 0x0, 0x0}, 1}, + {0x118AC, {0x118CC, 0x0, 0x0}, 1}, + {0x118AD, {0x118CD, 0x0, 0x0}, 1}, + {0x118AE, {0x118CE, 0x0, 0x0}, 1}, + {0x118AF, {0x118CF, 0x0, 0x0}, 1}, + {0x118B0, {0x118D0, 0x0, 0x0}, 1}, + {0x118B1, {0x118D1, 0x0, 0x0}, 1}, + {0x118B2, {0x118D2, 0x0, 0x0}, 1}, + {0x118B3, {0x118D3, 0x0, 0x0}, 1}, + {0x118B4, {0x118D4, 0x0, 0x0}, 1}, + {0x118B5, {0x118D5, 0x0, 0x0}, 1}, + {0x118B6, {0x118D6, 0x0, 0x0}, 1}, + {0x118B7, {0x118D7, 0x0, 0x0}, 1}, + {0x118B8, {0x118D8, 0x0, 0x0}, 1}, + {0x118B9, {0x118D9, 0x0, 0x0}, 1}, + {0x118BA, {0x118DA, 0x0, 0x0}, 1}, + {0x118BB, {0x118DB, 0x0, 0x0}, 1}, + {0x118BC, {0x118DC, 0x0, 0x0}, 1}, + {0x118BD, {0x118DD, 0x0, 0x0}, 1}, + {0x118BE, {0x118DE, 0x0, 0x0}, 1}, + {0x118BF, {0x118DF, 0x0, 0x0}, 1}, + {0x11938, {0x11935, 0x11930, 0x0}, 2}, + {0x1193B, {0x0, 0x0, 0x0}, 0}, + {0x1193C, {0x0, 0x0, 0x0}, 0}, + {0x1193E, {0x0, 0x0, 0x0}, 0}, + {0x11943, {0x0, 0x0, 0x0}, 0}, + {0x119D4, {0x0, 0x0, 0x0}, 0}, + {0x119D5, {0x0, 0x0, 0x0}, 0}, + {0x119D6, {0x0, 0x0, 0x0}, 0}, + {0x119D7, {0x0, 0x0, 0x0}, 0}, + {0x119DA, {0x0, 0x0, 0x0}, 0}, + {0x119DB, {0x0, 0x0, 0x0}, 0}, + {0x119E0, {0x0, 0x0, 0x0}, 0}, + {0x11A01, {0x0, 0x0, 0x0}, 0}, + {0x11A02, {0x0, 0x0, 0x0}, 0}, + {0x11A03, {0x0, 0x0, 0x0}, 0}, + {0x11A04, {0x0, 0x0, 0x0}, 0}, + {0x11A05, {0x0, 0x0, 0x0}, 0}, + {0x11A06, {0x0, 0x0, 0x0}, 0}, + {0x11A07, {0x0, 0x0, 0x0}, 0}, + {0x11A08, {0x0, 0x0, 0x0}, 0}, + {0x11A09, {0x0, 0x0, 0x0}, 0}, + {0x11A0A, {0x0, 0x0, 0x0}, 0}, + {0x11A33, {0x0, 0x0, 0x0}, 0}, + {0x11A34, {0x0, 0x0, 0x0}, 0}, + {0x11A35, {0x0, 0x0, 0x0}, 0}, + {0x11A36, {0x0, 0x0, 0x0}, 0}, + {0x11A37, {0x0, 0x0, 0x0}, 0}, + {0x11A38, {0x0, 0x0, 0x0}, 0}, + {0x11A3B, {0x0, 0x0, 0x0}, 0}, + {0x11A3C, {0x0, 0x0, 0x0}, 0}, + {0x11A3D, {0x0, 0x0, 0x0}, 0}, + {0x11A3E, {0x0, 0x0, 0x0}, 0}, + {0x11A47, {0x0, 0x0, 0x0}, 0}, + {0x11A51, {0x0, 0x0, 0x0}, 0}, + {0x11A52, {0x0, 0x0, 0x0}, 0}, + {0x11A53, {0x0, 0x0, 0x0}, 0}, + {0x11A54, {0x0, 0x0, 0x0}, 0}, + {0x11A55, {0x0, 0x0, 0x0}, 0}, + {0x11A56, {0x0, 0x0, 0x0}, 0}, + {0x11A59, {0x0, 0x0, 0x0}, 0}, + {0x11A5A, {0x0, 0x0, 0x0}, 0}, + {0x11A5B, {0x0, 0x0, 0x0}, 0}, + {0x11A8A, {0x0, 0x0, 0x0}, 0}, + {0x11A8B, {0x0, 0x0, 0x0}, 0}, + {0x11A8C, {0x0, 0x0, 0x0}, 0}, + {0x11A8D, {0x0, 0x0, 0x0}, 0}, + {0x11A8E, {0x0, 0x0, 0x0}, 0}, + {0x11A8F, {0x0, 0x0, 0x0}, 0}, + {0x11A90, {0x0, 0x0, 0x0}, 0}, + {0x11A91, {0x0, 0x0, 0x0}, 0}, + {0x11A92, {0x0, 0x0, 0x0}, 0}, + {0x11A93, {0x0, 0x0, 0x0}, 0}, + {0x11A94, {0x0, 0x0, 0x0}, 0}, + {0x11A95, {0x0, 0x0, 0x0}, 0}, + {0x11A96, {0x0, 0x0, 0x0}, 0}, + {0x11A98, {0x0, 0x0, 0x0}, 0}, + {0x11A99, {0x0, 0x0, 0x0}, 0}, + {0x11C30, {0x0, 0x0, 0x0}, 0}, + {0x11C31, {0x0, 0x0, 0x0}, 0}, + {0x11C32, {0x0, 0x0, 0x0}, 0}, + {0x11C33, {0x0, 0x0, 0x0}, 0}, + {0x11C34, {0x0, 0x0, 0x0}, 0}, + {0x11C35, {0x0, 0x0, 0x0}, 0}, + {0x11C36, {0x0, 0x0, 0x0}, 0}, + {0x11C38, {0x0, 0x0, 0x0}, 0}, + {0x11C39, {0x0, 0x0, 0x0}, 0}, + {0x11C3A, {0x0, 0x0, 0x0}, 0}, + {0x11C3B, {0x0, 0x0, 0x0}, 0}, + {0x11C3C, {0x0, 0x0, 0x0}, 0}, + {0x11C3D, {0x0, 0x0, 0x0}, 0}, + {0x11C3F, {0x0, 0x0, 0x0}, 0}, + {0x11C92, {0x0, 0x0, 0x0}, 0}, + {0x11C93, {0x0, 0x0, 0x0}, 0}, + {0x11C94, {0x0, 0x0, 0x0}, 0}, + {0x11C95, {0x0, 0x0, 0x0}, 0}, + {0x11C96, {0x0, 0x0, 0x0}, 0}, + {0x11C97, {0x0, 0x0, 0x0}, 0}, + {0x11C98, {0x0, 0x0, 0x0}, 0}, + {0x11C99, {0x0, 0x0, 0x0}, 0}, + {0x11C9A, {0x0, 0x0, 0x0}, 0}, + {0x11C9B, {0x0, 0x0, 0x0}, 0}, + {0x11C9C, {0x0, 0x0, 0x0}, 0}, + {0x11C9D, {0x0, 0x0, 0x0}, 0}, + {0x11C9E, {0x0, 0x0, 0x0}, 0}, + {0x11C9F, {0x0, 0x0, 0x0}, 0}, + {0x11CA0, {0x0, 0x0, 0x0}, 0}, + {0x11CA1, {0x0, 0x0, 0x0}, 0}, + {0x11CA2, {0x0, 0x0, 0x0}, 0}, + {0x11CA3, {0x0, 0x0, 0x0}, 0}, + {0x11CA4, {0x0, 0x0, 0x0}, 0}, + {0x11CA5, {0x0, 0x0, 0x0}, 0}, + {0x11CA6, {0x0, 0x0, 0x0}, 0}, + {0x11CA7, {0x0, 0x0, 0x0}, 0}, + {0x11CAA, {0x0, 0x0, 0x0}, 0}, + {0x11CAB, {0x0, 0x0, 0x0}, 0}, + {0x11CAC, {0x0, 0x0, 0x0}, 0}, + {0x11CAD, {0x0, 0x0, 0x0}, 0}, + {0x11CAE, {0x0, 0x0, 0x0}, 0}, + {0x11CAF, {0x0, 0x0, 0x0}, 0}, + {0x11CB0, {0x0, 0x0, 0x0}, 0}, + {0x11CB2, {0x0, 0x0, 0x0}, 0}, + {0x11CB3, {0x0, 0x0, 0x0}, 0}, + {0x11CB5, {0x0, 0x0, 0x0}, 0}, + {0x11CB6, {0x0, 0x0, 0x0}, 0}, + {0x11D31, {0x0, 0x0, 0x0}, 0}, + {0x11D32, {0x0, 0x0, 0x0}, 0}, + {0x11D33, {0x0, 0x0, 0x0}, 0}, + {0x11D34, {0x0, 0x0, 0x0}, 0}, + {0x11D35, {0x0, 0x0, 0x0}, 0}, + {0x11D36, {0x0, 0x0, 0x0}, 0}, + {0x11D3A, {0x0, 0x0, 0x0}, 0}, + {0x11D3C, {0x0, 0x0, 0x0}, 0}, + {0x11D3D, {0x0, 0x0, 0x0}, 0}, + {0x11D3F, {0x0, 0x0, 0x0}, 0}, + {0x11D40, {0x0, 0x0, 0x0}, 0}, + {0x11D41, {0x0, 0x0, 0x0}, 0}, + {0x11D42, {0x0, 0x0, 0x0}, 0}, + {0x11D43, {0x0, 0x0, 0x0}, 0}, + {0x11D44, {0x0, 0x0, 0x0}, 0}, + {0x11D45, {0x0, 0x0, 0x0}, 0}, + {0x11D47, {0x0, 0x0, 0x0}, 0}, + {0x11D90, {0x0, 0x0, 0x0}, 0}, + {0x11D91, {0x0, 0x0, 0x0}, 0}, + {0x11D95, {0x0, 0x0, 0x0}, 0}, + {0x11D97, {0x0, 0x0, 0x0}, 0}, + {0x11EF3, {0x0, 0x0, 0x0}, 0}, + {0x11EF4, {0x0, 0x0, 0x0}, 0}, + {0x16AF0, {0x0, 0x0, 0x0}, 0}, + {0x16AF1, {0x0, 0x0, 0x0}, 0}, + {0x16AF2, {0x0, 0x0, 0x0}, 0}, + {0x16AF3, {0x0, 0x0, 0x0}, 0}, + {0x16AF4, {0x0, 0x0, 0x0}, 0}, + {0x16B30, {0x0, 0x0, 0x0}, 0}, + {0x16B31, {0x0, 0x0, 0x0}, 0}, + {0x16B32, {0x0, 0x0, 0x0}, 0}, + {0x16B33, {0x0, 0x0, 0x0}, 0}, + {0x16B34, {0x0, 0x0, 0x0}, 0}, + {0x16B35, {0x0, 0x0, 0x0}, 0}, + {0x16B36, {0x0, 0x0, 0x0}, 0}, + {0x16E40, {0x16E60, 0x0, 0x0}, 1}, + {0x16E41, {0x16E61, 0x0, 0x0}, 1}, + {0x16E42, {0x16E62, 0x0, 0x0}, 1}, + {0x16E43, {0x16E63, 0x0, 0x0}, 1}, + {0x16E44, {0x16E64, 0x0, 0x0}, 1}, + {0x16E45, {0x16E65, 0x0, 0x0}, 1}, + {0x16E46, {0x16E66, 0x0, 0x0}, 1}, + {0x16E47, {0x16E67, 0x0, 0x0}, 1}, + {0x16E48, {0x16E68, 0x0, 0x0}, 1}, + {0x16E49, {0x16E69, 0x0, 0x0}, 1}, + {0x16E4A, {0x16E6A, 0x0, 0x0}, 1}, + {0x16E4B, {0x16E6B, 0x0, 0x0}, 1}, + {0x16E4C, {0x16E6C, 0x0, 0x0}, 1}, + {0x16E4D, {0x16E6D, 0x0, 0x0}, 1}, + {0x16E4E, {0x16E6E, 0x0, 0x0}, 1}, + {0x16E4F, {0x16E6F, 0x0, 0x0}, 1}, + {0x16E50, {0x16E70, 0x0, 0x0}, 1}, + {0x16E51, {0x16E71, 0x0, 0x0}, 1}, + {0x16E52, {0x16E72, 0x0, 0x0}, 1}, + {0x16E53, {0x16E73, 0x0, 0x0}, 1}, + {0x16E54, {0x16E74, 0x0, 0x0}, 1}, + {0x16E55, {0x16E75, 0x0, 0x0}, 1}, + {0x16E56, {0x16E76, 0x0, 0x0}, 1}, + {0x16E57, {0x16E77, 0x0, 0x0}, 1}, + {0x16E58, {0x16E78, 0x0, 0x0}, 1}, + {0x16E59, {0x16E79, 0x0, 0x0}, 1}, + {0x16E5A, {0x16E7A, 0x0, 0x0}, 1}, + {0x16E5B, {0x16E7B, 0x0, 0x0}, 1}, + {0x16E5C, {0x16E7C, 0x0, 0x0}, 1}, + {0x16E5D, {0x16E7D, 0x0, 0x0}, 1}, + {0x16E5E, {0x16E7E, 0x0, 0x0}, 1}, + {0x16E5F, {0x16E7F, 0x0, 0x0}, 1}, + {0x16F4F, {0x0, 0x0, 0x0}, 0}, + {0x16F8F, {0x0, 0x0, 0x0}, 0}, + {0x16F90, {0x0, 0x0, 0x0}, 0}, + {0x16F91, {0x0, 0x0, 0x0}, 0}, + {0x16F92, {0x0, 0x0, 0x0}, 0}, + {0x16FE4, {0x0, 0x0, 0x0}, 0}, + {0x1BC9D, {0x0, 0x0, 0x0}, 0}, + {0x1BC9E, {0x0, 0x0, 0x0}, 0}, + {0x1D15E, {0x1D157, 0x1D165, 0x0}, 2}, + {0x1D15F, {0x1D158, 0x1D165, 0x0}, 2}, + {0x1D160, {0x1D158, 0x1D165, 0x1D16E}, 3}, + {0x1D161, {0x1D158, 0x1D165, 0x1D16F}, 3}, + {0x1D162, {0x1D158, 0x1D165, 0x1D170}, 3}, + {0x1D163, {0x1D158, 0x1D165, 0x1D171}, 3}, + {0x1D164, {0x1D158, 0x1D165, 0x1D172}, 3}, + {0x1D167, {0x0, 0x0, 0x0}, 0}, + {0x1D168, {0x0, 0x0, 0x0}, 0}, + {0x1D169, {0x0, 0x0, 0x0}, 0}, + {0x1D17B, {0x0, 0x0, 0x0}, 0}, + {0x1D17C, {0x0, 0x0, 0x0}, 0}, + {0x1D17D, {0x0, 0x0, 0x0}, 0}, + {0x1D17E, {0x0, 0x0, 0x0}, 0}, + {0x1D17F, {0x0, 0x0, 0x0}, 0}, + {0x1D180, {0x0, 0x0, 0x0}, 0}, + {0x1D181, {0x0, 0x0, 0x0}, 0}, + {0x1D182, {0x0, 0x0, 0x0}, 0}, + {0x1D185, {0x0, 0x0, 0x0}, 0}, + {0x1D186, {0x0, 0x0, 0x0}, 0}, + {0x1D187, {0x0, 0x0, 0x0}, 0}, + {0x1D188, {0x0, 0x0, 0x0}, 0}, + {0x1D189, {0x0, 0x0, 0x0}, 0}, + {0x1D18A, {0x0, 0x0, 0x0}, 0}, + {0x1D18B, {0x0, 0x0, 0x0}, 0}, + {0x1D1AA, {0x0, 0x0, 0x0}, 0}, + {0x1D1AB, {0x0, 0x0, 0x0}, 0}, + {0x1D1AC, {0x0, 0x0, 0x0}, 0}, + {0x1D1AD, {0x0, 0x0, 0x0}, 0}, + {0x1D1BB, {0x1D1B9, 0x1D165, 0x0}, 2}, + {0x1D1BC, {0x1D1BA, 0x1D165, 0x0}, 2}, + {0x1D1BD, {0x1D1B9, 0x1D165, 0x1D16E}, 3}, + {0x1D1BE, {0x1D1BA, 0x1D165, 0x1D16E}, 3}, + {0x1D1BF, {0x1D1B9, 0x1D165, 0x1D16F}, 3}, + {0x1D1C0, {0x1D1BA, 0x1D165, 0x1D16F}, 3}, + {0x1D242, {0x0, 0x0, 0x0}, 0}, + {0x1D243, {0x0, 0x0, 0x0}, 0}, + {0x1D244, {0x0, 0x0, 0x0}, 0}, + {0x1DA00, {0x0, 0x0, 0x0}, 0}, + {0x1DA01, {0x0, 0x0, 0x0}, 0}, + {0x1DA02, {0x0, 0x0, 0x0}, 0}, + {0x1DA03, {0x0, 0x0, 0x0}, 0}, + {0x1DA04, {0x0, 0x0, 0x0}, 0}, + {0x1DA05, {0x0, 0x0, 0x0}, 0}, + {0x1DA06, {0x0, 0x0, 0x0}, 0}, + {0x1DA07, {0x0, 0x0, 0x0}, 0}, + {0x1DA08, {0x0, 0x0, 0x0}, 0}, + {0x1DA09, {0x0, 0x0, 0x0}, 0}, + {0x1DA0A, {0x0, 0x0, 0x0}, 0}, + {0x1DA0B, {0x0, 0x0, 0x0}, 0}, + {0x1DA0C, {0x0, 0x0, 0x0}, 0}, + {0x1DA0D, {0x0, 0x0, 0x0}, 0}, + {0x1DA0E, {0x0, 0x0, 0x0}, 0}, + {0x1DA0F, {0x0, 0x0, 0x0}, 0}, + {0x1DA10, {0x0, 0x0, 0x0}, 0}, + {0x1DA11, {0x0, 0x0, 0x0}, 0}, + {0x1DA12, {0x0, 0x0, 0x0}, 0}, + {0x1DA13, {0x0, 0x0, 0x0}, 0}, + {0x1DA14, {0x0, 0x0, 0x0}, 0}, + {0x1DA15, {0x0, 0x0, 0x0}, 0}, + {0x1DA16, {0x0, 0x0, 0x0}, 0}, + {0x1DA17, {0x0, 0x0, 0x0}, 0}, + {0x1DA18, {0x0, 0x0, 0x0}, 0}, + {0x1DA19, {0x0, 0x0, 0x0}, 0}, + {0x1DA1A, {0x0, 0x0, 0x0}, 0}, + {0x1DA1B, {0x0, 0x0, 0x0}, 0}, + {0x1DA1C, {0x0, 0x0, 0x0}, 0}, + {0x1DA1D, {0x0, 0x0, 0x0}, 0}, + {0x1DA1E, {0x0, 0x0, 0x0}, 0}, + {0x1DA1F, {0x0, 0x0, 0x0}, 0}, + {0x1DA20, {0x0, 0x0, 0x0}, 0}, + {0x1DA21, {0x0, 0x0, 0x0}, 0}, + {0x1DA22, {0x0, 0x0, 0x0}, 0}, + {0x1DA23, {0x0, 0x0, 0x0}, 0}, + {0x1DA24, {0x0, 0x0, 0x0}, 0}, + {0x1DA25, {0x0, 0x0, 0x0}, 0}, + {0x1DA26, {0x0, 0x0, 0x0}, 0}, + {0x1DA27, {0x0, 0x0, 0x0}, 0}, + {0x1DA28, {0x0, 0x0, 0x0}, 0}, + {0x1DA29, {0x0, 0x0, 0x0}, 0}, + {0x1DA2A, {0x0, 0x0, 0x0}, 0}, + {0x1DA2B, {0x0, 0x0, 0x0}, 0}, + {0x1DA2C, {0x0, 0x0, 0x0}, 0}, + {0x1DA2D, {0x0, 0x0, 0x0}, 0}, + {0x1DA2E, {0x0, 0x0, 0x0}, 0}, + {0x1DA2F, {0x0, 0x0, 0x0}, 0}, + {0x1DA30, {0x0, 0x0, 0x0}, 0}, + {0x1DA31, {0x0, 0x0, 0x0}, 0}, + {0x1DA32, {0x0, 0x0, 0x0}, 0}, + {0x1DA33, {0x0, 0x0, 0x0}, 0}, + {0x1DA34, {0x0, 0x0, 0x0}, 0}, + {0x1DA35, {0x0, 0x0, 0x0}, 0}, + {0x1DA36, {0x0, 0x0, 0x0}, 0}, + {0x1DA3B, {0x0, 0x0, 0x0}, 0}, + {0x1DA3C, {0x0, 0x0, 0x0}, 0}, + {0x1DA3D, {0x0, 0x0, 0x0}, 0}, + {0x1DA3E, {0x0, 0x0, 0x0}, 0}, + {0x1DA3F, {0x0, 0x0, 0x0}, 0}, + {0x1DA40, {0x0, 0x0, 0x0}, 0}, + {0x1DA41, {0x0, 0x0, 0x0}, 0}, + {0x1DA42, {0x0, 0x0, 0x0}, 0}, + {0x1DA43, {0x0, 0x0, 0x0}, 0}, + {0x1DA44, {0x0, 0x0, 0x0}, 0}, + {0x1DA45, {0x0, 0x0, 0x0}, 0}, + {0x1DA46, {0x0, 0x0, 0x0}, 0}, + {0x1DA47, {0x0, 0x0, 0x0}, 0}, + {0x1DA48, {0x0, 0x0, 0x0}, 0}, + {0x1DA49, {0x0, 0x0, 0x0}, 0}, + {0x1DA4A, {0x0, 0x0, 0x0}, 0}, + {0x1DA4B, {0x0, 0x0, 0x0}, 0}, + {0x1DA4C, {0x0, 0x0, 0x0}, 0}, + {0x1DA4D, {0x0, 0x0, 0x0}, 0}, + {0x1DA4E, {0x0, 0x0, 0x0}, 0}, + {0x1DA4F, {0x0, 0x0, 0x0}, 0}, + {0x1DA50, {0x0, 0x0, 0x0}, 0}, + {0x1DA51, {0x0, 0x0, 0x0}, 0}, + {0x1DA52, {0x0, 0x0, 0x0}, 0}, + {0x1DA53, {0x0, 0x0, 0x0}, 0}, + {0x1DA54, {0x0, 0x0, 0x0}, 0}, + {0x1DA55, {0x0, 0x0, 0x0}, 0}, + {0x1DA56, {0x0, 0x0, 0x0}, 0}, + {0x1DA57, {0x0, 0x0, 0x0}, 0}, + {0x1DA58, {0x0, 0x0, 0x0}, 0}, + {0x1DA59, {0x0, 0x0, 0x0}, 0}, + {0x1DA5A, {0x0, 0x0, 0x0}, 0}, + {0x1DA5B, {0x0, 0x0, 0x0}, 0}, + {0x1DA5C, {0x0, 0x0, 0x0}, 0}, + {0x1DA5D, {0x0, 0x0, 0x0}, 0}, + {0x1DA5E, {0x0, 0x0, 0x0}, 0}, + {0x1DA5F, {0x0, 0x0, 0x0}, 0}, + {0x1DA60, {0x0, 0x0, 0x0}, 0}, + {0x1DA61, {0x0, 0x0, 0x0}, 0}, + {0x1DA62, {0x0, 0x0, 0x0}, 0}, + {0x1DA63, {0x0, 0x0, 0x0}, 0}, + {0x1DA64, {0x0, 0x0, 0x0}, 0}, + {0x1DA65, {0x0, 0x0, 0x0}, 0}, + {0x1DA66, {0x0, 0x0, 0x0}, 0}, + {0x1DA67, {0x0, 0x0, 0x0}, 0}, + {0x1DA68, {0x0, 0x0, 0x0}, 0}, + {0x1DA69, {0x0, 0x0, 0x0}, 0}, + {0x1DA6A, {0x0, 0x0, 0x0}, 0}, + {0x1DA6B, {0x0, 0x0, 0x0}, 0}, + {0x1DA6C, {0x0, 0x0, 0x0}, 0}, + {0x1DA75, {0x0, 0x0, 0x0}, 0}, + {0x1DA84, {0x0, 0x0, 0x0}, 0}, + {0x1DA9B, {0x0, 0x0, 0x0}, 0}, + {0x1DA9C, {0x0, 0x0, 0x0}, 0}, + {0x1DA9D, {0x0, 0x0, 0x0}, 0}, + {0x1DA9E, {0x0, 0x0, 0x0}, 0}, + {0x1DA9F, {0x0, 0x0, 0x0}, 0}, + {0x1DAA1, {0x0, 0x0, 0x0}, 0}, + {0x1DAA2, {0x0, 0x0, 0x0}, 0}, + {0x1DAA3, {0x0, 0x0, 0x0}, 0}, + {0x1DAA4, {0x0, 0x0, 0x0}, 0}, + {0x1DAA5, {0x0, 0x0, 0x0}, 0}, + {0x1DAA6, {0x0, 0x0, 0x0}, 0}, + {0x1DAA7, {0x0, 0x0, 0x0}, 0}, + {0x1DAA8, {0x0, 0x0, 0x0}, 0}, + {0x1DAA9, {0x0, 0x0, 0x0}, 0}, + {0x1DAAA, {0x0, 0x0, 0x0}, 0}, + {0x1DAAB, {0x0, 0x0, 0x0}, 0}, + {0x1DAAC, {0x0, 0x0, 0x0}, 0}, + {0x1DAAD, {0x0, 0x0, 0x0}, 0}, + {0x1DAAE, {0x0, 0x0, 0x0}, 0}, + {0x1DAAF, {0x0, 0x0, 0x0}, 0}, + {0x1E000, {0x0, 0x0, 0x0}, 0}, + {0x1E001, {0x0, 0x0, 0x0}, 0}, + {0x1E002, {0x0, 0x0, 0x0}, 0}, + {0x1E003, {0x0, 0x0, 0x0}, 0}, + {0x1E004, {0x0, 0x0, 0x0}, 0}, + {0x1E005, {0x0, 0x0, 0x0}, 0}, + {0x1E006, {0x0, 0x0, 0x0}, 0}, + {0x1E008, {0x0, 0x0, 0x0}, 0}, + {0x1E009, {0x0, 0x0, 0x0}, 0}, + {0x1E00A, {0x0, 0x0, 0x0}, 0}, + {0x1E00B, {0x0, 0x0, 0x0}, 0}, + {0x1E00C, {0x0, 0x0, 0x0}, 0}, + {0x1E00D, {0x0, 0x0, 0x0}, 0}, + {0x1E00E, {0x0, 0x0, 0x0}, 0}, + {0x1E00F, {0x0, 0x0, 0x0}, 0}, + {0x1E010, {0x0, 0x0, 0x0}, 0}, + {0x1E011, {0x0, 0x0, 0x0}, 0}, + {0x1E012, {0x0, 0x0, 0x0}, 0}, + {0x1E013, {0x0, 0x0, 0x0}, 0}, + {0x1E014, {0x0, 0x0, 0x0}, 0}, + {0x1E015, {0x0, 0x0, 0x0}, 0}, + {0x1E016, {0x0, 0x0, 0x0}, 0}, + {0x1E017, {0x0, 0x0, 0x0}, 0}, + {0x1E018, {0x0, 0x0, 0x0}, 0}, + {0x1E01B, {0x0, 0x0, 0x0}, 0}, + {0x1E01C, {0x0, 0x0, 0x0}, 0}, + {0x1E01D, {0x0, 0x0, 0x0}, 0}, + {0x1E01E, {0x0, 0x0, 0x0}, 0}, + {0x1E01F, {0x0, 0x0, 0x0}, 0}, + {0x1E020, {0x0, 0x0, 0x0}, 0}, + {0x1E021, {0x0, 0x0, 0x0}, 0}, + {0x1E023, {0x0, 0x0, 0x0}, 0}, + {0x1E024, {0x0, 0x0, 0x0}, 0}, + {0x1E026, {0x0, 0x0, 0x0}, 0}, + {0x1E027, {0x0, 0x0, 0x0}, 0}, + {0x1E028, {0x0, 0x0, 0x0}, 0}, + {0x1E029, {0x0, 0x0, 0x0}, 0}, + {0x1E02A, {0x0, 0x0, 0x0}, 0}, + {0x1E130, {0x0, 0x0, 0x0}, 0}, + {0x1E131, {0x0, 0x0, 0x0}, 0}, + {0x1E132, {0x0, 0x0, 0x0}, 0}, + {0x1E133, {0x0, 0x0, 0x0}, 0}, + {0x1E134, {0x0, 0x0, 0x0}, 0}, + {0x1E135, {0x0, 0x0, 0x0}, 0}, + {0x1E136, {0x0, 0x0, 0x0}, 0}, + {0x1E2EC, {0x0, 0x0, 0x0}, 0}, + {0x1E2ED, {0x0, 0x0, 0x0}, 0}, + {0x1E2EE, {0x0, 0x0, 0x0}, 0}, + {0x1E2EF, {0x0, 0x0, 0x0}, 0}, + {0x1E8D0, {0x0, 0x0, 0x0}, 0}, + {0x1E8D1, {0x0, 0x0, 0x0}, 0}, + {0x1E8D2, {0x0, 0x0, 0x0}, 0}, + {0x1E8D3, {0x0, 0x0, 0x0}, 0}, + {0x1E8D4, {0x0, 0x0, 0x0}, 0}, + {0x1E8D5, {0x0, 0x0, 0x0}, 0}, + {0x1E8D6, {0x0, 0x0, 0x0}, 0}, + {0x1E900, {0x1E922, 0x0, 0x0}, 1}, + {0x1E901, {0x1E923, 0x0, 0x0}, 1}, + {0x1E902, {0x1E924, 0x0, 0x0}, 1}, + {0x1E903, {0x1E925, 0x0, 0x0}, 1}, + {0x1E904, {0x1E926, 0x0, 0x0}, 1}, + {0x1E905, {0x1E927, 0x0, 0x0}, 1}, + {0x1E906, {0x1E928, 0x0, 0x0}, 1}, + {0x1E907, {0x1E929, 0x0, 0x0}, 1}, + {0x1E908, {0x1E92A, 0x0, 0x0}, 1}, + {0x1E909, {0x1E92B, 0x0, 0x0}, 1}, + {0x1E90A, {0x1E92C, 0x0, 0x0}, 1}, + {0x1E90B, {0x1E92D, 0x0, 0x0}, 1}, + {0x1E90C, {0x1E92E, 0x0, 0x0}, 1}, + {0x1E90D, {0x1E92F, 0x0, 0x0}, 1}, + {0x1E90E, {0x1E930, 0x0, 0x0}, 1}, + {0x1E90F, {0x1E931, 0x0, 0x0}, 1}, + {0x1E910, {0x1E932, 0x0, 0x0}, 1}, + {0x1E911, {0x1E933, 0x0, 0x0}, 1}, + {0x1E912, {0x1E934, 0x0, 0x0}, 1}, + {0x1E913, {0x1E935, 0x0, 0x0}, 1}, + {0x1E914, {0x1E936, 0x0, 0x0}, 1}, + {0x1E915, {0x1E937, 0x0, 0x0}, 1}, + {0x1E916, {0x1E938, 0x0, 0x0}, 1}, + {0x1E917, {0x1E939, 0x0, 0x0}, 1}, + {0x1E918, {0x1E93A, 0x0, 0x0}, 1}, + {0x1E919, {0x1E93B, 0x0, 0x0}, 1}, + {0x1E91A, {0x1E93C, 0x0, 0x0}, 1}, + {0x1E91B, {0x1E93D, 0x0, 0x0}, 1}, + {0x1E91C, {0x1E93E, 0x0, 0x0}, 1}, + {0x1E91D, {0x1E93F, 0x0, 0x0}, 1}, + {0x1E91E, {0x1E940, 0x0, 0x0}, 1}, + {0x1E91F, {0x1E941, 0x0, 0x0}, 1}, + {0x1E920, {0x1E942, 0x0, 0x0}, 1}, + {0x1E921, {0x1E943, 0x0, 0x0}, 1}, + {0x1E944, {0x0, 0x0, 0x0}, 0}, + {0x1E945, {0x0, 0x0, 0x0}, 0}, + {0x1E946, {0x0, 0x0, 0x0}, 0}, + {0x1E947, {0x0, 0x0, 0x0}, 0}, + {0x1E948, {0x0, 0x0, 0x0}, 0}, + {0x1E949, {0x0, 0x0, 0x0}, 0}, + {0x1E94A, {0x0, 0x0, 0x0}, 0}, + {0x2F800, {0x4E3D, 0x0, 0x0}, 1}, + {0x2F801, {0x4E38, 0x0, 0x0}, 1}, + {0x2F802, {0x4E41, 0x0, 0x0}, 1}, + {0x2F803, {0x20122, 0x0, 0x0}, 1}, + {0x2F804, {0x4F60, 0x0, 0x0}, 1}, + {0x2F805, {0x4FAE, 0x0, 0x0}, 1}, + {0x2F806, {0x4FBB, 0x0, 0x0}, 1}, + {0x2F807, {0x5002, 0x0, 0x0}, 1}, + {0x2F808, {0x507A, 0x0, 0x0}, 1}, + {0x2F809, {0x5099, 0x0, 0x0}, 1}, + {0x2F80A, {0x50E7, 0x0, 0x0}, 1}, + {0x2F80B, {0x50CF, 0x0, 0x0}, 1}, + {0x2F80C, {0x349E, 0x0, 0x0}, 1}, + {0x2F80D, {0x2063A, 0x0, 0x0}, 1}, + {0x2F80E, {0x514D, 0x0, 0x0}, 1}, + {0x2F80F, {0x5154, 0x0, 0x0}, 1}, + {0x2F810, {0x5164, 0x0, 0x0}, 1}, + {0x2F811, {0x5177, 0x0, 0x0}, 1}, + {0x2F812, {0x2051C, 0x0, 0x0}, 1}, + {0x2F813, {0x34B9, 0x0, 0x0}, 1}, + {0x2F814, {0x5167, 0x0, 0x0}, 1}, + {0x2F815, {0x518D, 0x0, 0x0}, 1}, + {0x2F816, {0x2054B, 0x0, 0x0}, 1}, + {0x2F817, {0x5197, 0x0, 0x0}, 1}, + {0x2F818, {0x51A4, 0x0, 0x0}, 1}, + {0x2F819, {0x4ECC, 0x0, 0x0}, 1}, + {0x2F81A, {0x51AC, 0x0, 0x0}, 1}, + {0x2F81B, {0x51B5, 0x0, 0x0}, 1}, + {0x2F81C, {0x291DF, 0x0, 0x0}, 1}, + {0x2F81D, {0x51F5, 0x0, 0x0}, 1}, + {0x2F81E, {0x5203, 0x0, 0x0}, 1}, + {0x2F81F, {0x34DF, 0x0, 0x0}, 1}, + {0x2F820, {0x523B, 0x0, 0x0}, 1}, + {0x2F821, {0x5246, 0x0, 0x0}, 1}, + {0x2F822, {0x5272, 0x0, 0x0}, 1}, + {0x2F823, {0x5277, 0x0, 0x0}, 1}, + {0x2F824, {0x3515, 0x0, 0x0}, 1}, + {0x2F825, {0x52C7, 0x0, 0x0}, 1}, + {0x2F826, {0x52C9, 0x0, 0x0}, 1}, + {0x2F827, {0x52E4, 0x0, 0x0}, 1}, + {0x2F828, {0x52FA, 0x0, 0x0}, 1}, + {0x2F829, {0x5305, 0x0, 0x0}, 1}, + {0x2F82A, {0x5306, 0x0, 0x0}, 1}, + {0x2F82B, {0x5317, 0x0, 0x0}, 1}, + {0x2F82C, {0x5349, 0x0, 0x0}, 1}, + {0x2F82D, {0x5351, 0x0, 0x0}, 1}, + {0x2F82E, {0x535A, 0x0, 0x0}, 1}, + {0x2F82F, {0x5373, 0x0, 0x0}, 1}, + {0x2F830, {0x537D, 0x0, 0x0}, 1}, + {0x2F831, {0x537F, 0x0, 0x0}, 1}, + {0x2F832, {0x537F, 0x0, 0x0}, 1}, + {0x2F833, {0x537F, 0x0, 0x0}, 1}, + {0x2F834, {0x20A2C, 0x0, 0x0}, 1}, + {0x2F835, {0x7070, 0x0, 0x0}, 1}, + {0x2F836, {0x53CA, 0x0, 0x0}, 1}, + {0x2F837, {0x53DF, 0x0, 0x0}, 1}, + {0x2F838, {0x20B63, 0x0, 0x0}, 1}, + {0x2F839, {0x53EB, 0x0, 0x0}, 1}, + {0x2F83A, {0x53F1, 0x0, 0x0}, 1}, + {0x2F83B, {0x5406, 0x0, 0x0}, 1}, + {0x2F83C, {0x549E, 0x0, 0x0}, 1}, + {0x2F83D, {0x5438, 0x0, 0x0}, 1}, + {0x2F83E, {0x5448, 0x0, 0x0}, 1}, + {0x2F83F, {0x5468, 0x0, 0x0}, 1}, + {0x2F840, {0x54A2, 0x0, 0x0}, 1}, + {0x2F841, {0x54F6, 0x0, 0x0}, 1}, + {0x2F842, {0x5510, 0x0, 0x0}, 1}, + {0x2F843, {0x5553, 0x0, 0x0}, 1}, + {0x2F844, {0x5563, 0x0, 0x0}, 1}, + {0x2F845, {0x5584, 0x0, 0x0}, 1}, + {0x2F846, {0x5584, 0x0, 0x0}, 1}, + {0x2F847, {0x5599, 0x0, 0x0}, 1}, + {0x2F848, {0x55AB, 0x0, 0x0}, 1}, + {0x2F849, {0x55B3, 0x0, 0x0}, 1}, + {0x2F84A, {0x55C2, 0x0, 0x0}, 1}, + {0x2F84B, {0x5716, 0x0, 0x0}, 1}, + {0x2F84C, {0x5606, 0x0, 0x0}, 1}, + {0x2F84D, {0x5717, 0x0, 0x0}, 1}, + {0x2F84E, {0x5651, 0x0, 0x0}, 1}, + {0x2F84F, {0x5674, 0x0, 0x0}, 1}, + {0x2F850, {0x5207, 0x0, 0x0}, 1}, + {0x2F851, {0x58EE, 0x0, 0x0}, 1}, + {0x2F852, {0x57CE, 0x0, 0x0}, 1}, + {0x2F853, {0x57F4, 0x0, 0x0}, 1}, + {0x2F854, {0x580D, 0x0, 0x0}, 1}, + {0x2F855, {0x578B, 0x0, 0x0}, 1}, + {0x2F856, {0x5832, 0x0, 0x0}, 1}, + {0x2F857, {0x5831, 0x0, 0x0}, 1}, + {0x2F858, {0x58AC, 0x0, 0x0}, 1}, + {0x2F859, {0x214E4, 0x0, 0x0}, 1}, + {0x2F85A, {0x58F2, 0x0, 0x0}, 1}, + {0x2F85B, {0x58F7, 0x0, 0x0}, 1}, + {0x2F85C, {0x5906, 0x0, 0x0}, 1}, + {0x2F85D, {0x591A, 0x0, 0x0}, 1}, + {0x2F85E, {0x5922, 0x0, 0x0}, 1}, + {0x2F85F, {0x5962, 0x0, 0x0}, 1}, + {0x2F860, {0x216A8, 0x0, 0x0}, 1}, + {0x2F861, {0x216EA, 0x0, 0x0}, 1}, + {0x2F862, {0x59EC, 0x0, 0x0}, 1}, + {0x2F863, {0x5A1B, 0x0, 0x0}, 1}, + {0x2F864, {0x5A27, 0x0, 0x0}, 1}, + {0x2F865, {0x59D8, 0x0, 0x0}, 1}, + {0x2F866, {0x5A66, 0x0, 0x0}, 1}, + {0x2F867, {0x36EE, 0x0, 0x0}, 1}, + {0x2F868, {0x36FC, 0x0, 0x0}, 1}, + {0x2F869, {0x5B08, 0x0, 0x0}, 1}, + {0x2F86A, {0x5B3E, 0x0, 0x0}, 1}, + {0x2F86B, {0x5B3E, 0x0, 0x0}, 1}, + {0x2F86C, {0x219C8, 0x0, 0x0}, 1}, + {0x2F86D, {0x5BC3, 0x0, 0x0}, 1}, + {0x2F86E, {0x5BD8, 0x0, 0x0}, 1}, + {0x2F86F, {0x5BE7, 0x0, 0x0}, 1}, + {0x2F870, {0x5BF3, 0x0, 0x0}, 1}, + {0x2F871, {0x21B18, 0x0, 0x0}, 1}, + {0x2F872, {0x5BFF, 0x0, 0x0}, 1}, + {0x2F873, {0x5C06, 0x0, 0x0}, 1}, + {0x2F874, {0x5F53, 0x0, 0x0}, 1}, + {0x2F875, {0x5C22, 0x0, 0x0}, 1}, + {0x2F876, {0x3781, 0x0, 0x0}, 1}, + {0x2F877, {0x5C60, 0x0, 0x0}, 1}, + {0x2F878, {0x5C6E, 0x0, 0x0}, 1}, + {0x2F879, {0x5CC0, 0x0, 0x0}, 1}, + {0x2F87A, {0x5C8D, 0x0, 0x0}, 1}, + {0x2F87B, {0x21DE4, 0x0, 0x0}, 1}, + {0x2F87C, {0x5D43, 0x0, 0x0}, 1}, + {0x2F87D, {0x21DE6, 0x0, 0x0}, 1}, + {0x2F87E, {0x5D6E, 0x0, 0x0}, 1}, + {0x2F87F, {0x5D6B, 0x0, 0x0}, 1}, + {0x2F880, {0x5D7C, 0x0, 0x0}, 1}, + {0x2F881, {0x5DE1, 0x0, 0x0}, 1}, + {0x2F882, {0x5DE2, 0x0, 0x0}, 1}, + {0x2F883, {0x382F, 0x0, 0x0}, 1}, + {0x2F884, {0x5DFD, 0x0, 0x0}, 1}, + {0x2F885, {0x5E28, 0x0, 0x0}, 1}, + {0x2F886, {0x5E3D, 0x0, 0x0}, 1}, + {0x2F887, {0x5E69, 0x0, 0x0}, 1}, + {0x2F888, {0x3862, 0x0, 0x0}, 1}, + {0x2F889, {0x22183, 0x0, 0x0}, 1}, + {0x2F88A, {0x387C, 0x0, 0x0}, 1}, + {0x2F88B, {0x5EB0, 0x0, 0x0}, 1}, + {0x2F88C, {0x5EB3, 0x0, 0x0}, 1}, + {0x2F88D, {0x5EB6, 0x0, 0x0}, 1}, + {0x2F88E, {0x5ECA, 0x0, 0x0}, 1}, + {0x2F88F, {0x2A392, 0x0, 0x0}, 1}, + {0x2F890, {0x5EFE, 0x0, 0x0}, 1}, + {0x2F891, {0x22331, 0x0, 0x0}, 1}, + {0x2F892, {0x22331, 0x0, 0x0}, 1}, + {0x2F893, {0x8201, 0x0, 0x0}, 1}, + {0x2F894, {0x5F22, 0x0, 0x0}, 1}, + {0x2F895, {0x5F22, 0x0, 0x0}, 1}, + {0x2F896, {0x38C7, 0x0, 0x0}, 1}, + {0x2F897, {0x232B8, 0x0, 0x0}, 1}, + {0x2F898, {0x261DA, 0x0, 0x0}, 1}, + {0x2F899, {0x5F62, 0x0, 0x0}, 1}, + {0x2F89A, {0x5F6B, 0x0, 0x0}, 1}, + {0x2F89B, {0x38E3, 0x0, 0x0}, 1}, + {0x2F89C, {0x5F9A, 0x0, 0x0}, 1}, + {0x2F89D, {0x5FCD, 0x0, 0x0}, 1}, + {0x2F89E, {0x5FD7, 0x0, 0x0}, 1}, + {0x2F89F, {0x5FF9, 0x0, 0x0}, 1}, + {0x2F8A0, {0x6081, 0x0, 0x0}, 1}, + {0x2F8A1, {0x393A, 0x0, 0x0}, 1}, + {0x2F8A2, {0x391C, 0x0, 0x0}, 1}, + {0x2F8A3, {0x6094, 0x0, 0x0}, 1}, + {0x2F8A4, {0x226D4, 0x0, 0x0}, 1}, + {0x2F8A5, {0x60C7, 0x0, 0x0}, 1}, + {0x2F8A6, {0x6148, 0x0, 0x0}, 1}, + {0x2F8A7, {0x614C, 0x0, 0x0}, 1}, + {0x2F8A8, {0x614E, 0x0, 0x0}, 1}, + {0x2F8A9, {0x614C, 0x0, 0x0}, 1}, + {0x2F8AA, {0x617A, 0x0, 0x0}, 1}, + {0x2F8AB, {0x618E, 0x0, 0x0}, 1}, + {0x2F8AC, {0x61B2, 0x0, 0x0}, 1}, + {0x2F8AD, {0x61A4, 0x0, 0x0}, 1}, + {0x2F8AE, {0x61AF, 0x0, 0x0}, 1}, + {0x2F8AF, {0x61DE, 0x0, 0x0}, 1}, + {0x2F8B0, {0x61F2, 0x0, 0x0}, 1}, + {0x2F8B1, {0x61F6, 0x0, 0x0}, 1}, + {0x2F8B2, {0x6210, 0x0, 0x0}, 1}, + {0x2F8B3, {0x621B, 0x0, 0x0}, 1}, + {0x2F8B4, {0x625D, 0x0, 0x0}, 1}, + {0x2F8B5, {0x62B1, 0x0, 0x0}, 1}, + {0x2F8B6, {0x62D4, 0x0, 0x0}, 1}, + {0x2F8B7, {0x6350, 0x0, 0x0}, 1}, + {0x2F8B8, {0x22B0C, 0x0, 0x0}, 1}, + {0x2F8B9, {0x633D, 0x0, 0x0}, 1}, + {0x2F8BA, {0x62FC, 0x0, 0x0}, 1}, + {0x2F8BB, {0x6368, 0x0, 0x0}, 1}, + {0x2F8BC, {0x6383, 0x0, 0x0}, 1}, + {0x2F8BD, {0x63E4, 0x0, 0x0}, 1}, + {0x2F8BE, {0x22BF1, 0x0, 0x0}, 1}, + {0x2F8BF, {0x6422, 0x0, 0x0}, 1}, + {0x2F8C0, {0x63C5, 0x0, 0x0}, 1}, + {0x2F8C1, {0x63A9, 0x0, 0x0}, 1}, + {0x2F8C2, {0x3A2E, 0x0, 0x0}, 1}, + {0x2F8C3, {0x6469, 0x0, 0x0}, 1}, + {0x2F8C4, {0x647E, 0x0, 0x0}, 1}, + {0x2F8C5, {0x649D, 0x0, 0x0}, 1}, + {0x2F8C6, {0x6477, 0x0, 0x0}, 1}, + {0x2F8C7, {0x3A6C, 0x0, 0x0}, 1}, + {0x2F8C8, {0x654F, 0x0, 0x0}, 1}, + {0x2F8C9, {0x656C, 0x0, 0x0}, 1}, + {0x2F8CA, {0x2300A, 0x0, 0x0}, 1}, + {0x2F8CB, {0x65E3, 0x0, 0x0}, 1}, + {0x2F8CC, {0x66F8, 0x0, 0x0}, 1}, + {0x2F8CD, {0x6649, 0x0, 0x0}, 1}, + {0x2F8CE, {0x3B19, 0x0, 0x0}, 1}, + {0x2F8CF, {0x6691, 0x0, 0x0}, 1}, + {0x2F8D0, {0x3B08, 0x0, 0x0}, 1}, + {0x2F8D1, {0x3AE4, 0x0, 0x0}, 1}, + {0x2F8D2, {0x5192, 0x0, 0x0}, 1}, + {0x2F8D3, {0x5195, 0x0, 0x0}, 1}, + {0x2F8D4, {0x6700, 0x0, 0x0}, 1}, + {0x2F8D5, {0x669C, 0x0, 0x0}, 1}, + {0x2F8D6, {0x80AD, 0x0, 0x0}, 1}, + {0x2F8D7, {0x43D9, 0x0, 0x0}, 1}, + {0x2F8D8, {0x6717, 0x0, 0x0}, 1}, + {0x2F8D9, {0x671B, 0x0, 0x0}, 1}, + {0x2F8DA, {0x6721, 0x0, 0x0}, 1}, + {0x2F8DB, {0x675E, 0x0, 0x0}, 1}, + {0x2F8DC, {0x6753, 0x0, 0x0}, 1}, + {0x2F8DD, {0x233C3, 0x0, 0x0}, 1}, + {0x2F8DE, {0x3B49, 0x0, 0x0}, 1}, + {0x2F8DF, {0x67FA, 0x0, 0x0}, 1}, + {0x2F8E0, {0x6785, 0x0, 0x0}, 1}, + {0x2F8E1, {0x6852, 0x0, 0x0}, 1}, + {0x2F8E2, {0x6885, 0x0, 0x0}, 1}, + {0x2F8E3, {0x2346D, 0x0, 0x0}, 1}, + {0x2F8E4, {0x688E, 0x0, 0x0}, 1}, + {0x2F8E5, {0x681F, 0x0, 0x0}, 1}, + {0x2F8E6, {0x6914, 0x0, 0x0}, 1}, + {0x2F8E7, {0x3B9D, 0x0, 0x0}, 1}, + {0x2F8E8, {0x6942, 0x0, 0x0}, 1}, + {0x2F8E9, {0x69A3, 0x0, 0x0}, 1}, + {0x2F8EA, {0x69EA, 0x0, 0x0}, 1}, + {0x2F8EB, {0x6AA8, 0x0, 0x0}, 1}, + {0x2F8EC, {0x236A3, 0x0, 0x0}, 1}, + {0x2F8ED, {0x6ADB, 0x0, 0x0}, 1}, + {0x2F8EE, {0x3C18, 0x0, 0x0}, 1}, + {0x2F8EF, {0x6B21, 0x0, 0x0}, 1}, + {0x2F8F0, {0x238A7, 0x0, 0x0}, 1}, + {0x2F8F1, {0x6B54, 0x0, 0x0}, 1}, + {0x2F8F2, {0x3C4E, 0x0, 0x0}, 1}, + {0x2F8F3, {0x6B72, 0x0, 0x0}, 1}, + {0x2F8F4, {0x6B9F, 0x0, 0x0}, 1}, + {0x2F8F5, {0x6BBA, 0x0, 0x0}, 1}, + {0x2F8F6, {0x6BBB, 0x0, 0x0}, 1}, + {0x2F8F7, {0x23A8D, 0x0, 0x0}, 1}, + {0x2F8F8, {0x21D0B, 0x0, 0x0}, 1}, + {0x2F8F9, {0x23AFA, 0x0, 0x0}, 1}, + {0x2F8FA, {0x6C4E, 0x0, 0x0}, 1}, + {0x2F8FB, {0x23CBC, 0x0, 0x0}, 1}, + {0x2F8FC, {0x6CBF, 0x0, 0x0}, 1}, + {0x2F8FD, {0x6CCD, 0x0, 0x0}, 1}, + {0x2F8FE, {0x6C67, 0x0, 0x0}, 1}, + {0x2F8FF, {0x6D16, 0x0, 0x0}, 1}, + {0x2F900, {0x6D3E, 0x0, 0x0}, 1}, + {0x2F901, {0x6D77, 0x0, 0x0}, 1}, + {0x2F902, {0x6D41, 0x0, 0x0}, 1}, + {0x2F903, {0x6D69, 0x0, 0x0}, 1}, + {0x2F904, {0x6D78, 0x0, 0x0}, 1}, + {0x2F905, {0x6D85, 0x0, 0x0}, 1}, + {0x2F906, {0x23D1E, 0x0, 0x0}, 1}, + {0x2F907, {0x6D34, 0x0, 0x0}, 1}, + {0x2F908, {0x6E2F, 0x0, 0x0}, 1}, + {0x2F909, {0x6E6E, 0x0, 0x0}, 1}, + {0x2F90A, {0x3D33, 0x0, 0x0}, 1}, + {0x2F90B, {0x6ECB, 0x0, 0x0}, 1}, + {0x2F90C, {0x6EC7, 0x0, 0x0}, 1}, + {0x2F90D, {0x23ED1, 0x0, 0x0}, 1}, + {0x2F90E, {0x6DF9, 0x0, 0x0}, 1}, + {0x2F90F, {0x6F6E, 0x0, 0x0}, 1}, + {0x2F910, {0x23F5E, 0x0, 0x0}, 1}, + {0x2F911, {0x23F8E, 0x0, 0x0}, 1}, + {0x2F912, {0x6FC6, 0x0, 0x0}, 1}, + {0x2F913, {0x7039, 0x0, 0x0}, 1}, + {0x2F914, {0x701E, 0x0, 0x0}, 1}, + {0x2F915, {0x701B, 0x0, 0x0}, 1}, + {0x2F916, {0x3D96, 0x0, 0x0}, 1}, + {0x2F917, {0x704A, 0x0, 0x0}, 1}, + {0x2F918, {0x707D, 0x0, 0x0}, 1}, + {0x2F919, {0x7077, 0x0, 0x0}, 1}, + {0x2F91A, {0x70AD, 0x0, 0x0}, 1}, + {0x2F91B, {0x20525, 0x0, 0x0}, 1}, + {0x2F91C, {0x7145, 0x0, 0x0}, 1}, + {0x2F91D, {0x24263, 0x0, 0x0}, 1}, + {0x2F91E, {0x719C, 0x0, 0x0}, 1}, + {0x2F91F, {0x243AB, 0x0, 0x0}, 1}, + {0x2F920, {0x7228, 0x0, 0x0}, 1}, + {0x2F921, {0x7235, 0x0, 0x0}, 1}, + {0x2F922, {0x7250, 0x0, 0x0}, 1}, + {0x2F923, {0x24608, 0x0, 0x0}, 1}, + {0x2F924, {0x7280, 0x0, 0x0}, 1}, + {0x2F925, {0x7295, 0x0, 0x0}, 1}, + {0x2F926, {0x24735, 0x0, 0x0}, 1}, + {0x2F927, {0x24814, 0x0, 0x0}, 1}, + {0x2F928, {0x737A, 0x0, 0x0}, 1}, + {0x2F929, {0x738B, 0x0, 0x0}, 1}, + {0x2F92A, {0x3EAC, 0x0, 0x0}, 1}, + {0x2F92B, {0x73A5, 0x0, 0x0}, 1}, + {0x2F92C, {0x3EB8, 0x0, 0x0}, 1}, + {0x2F92D, {0x3EB8, 0x0, 0x0}, 1}, + {0x2F92E, {0x7447, 0x0, 0x0}, 1}, + {0x2F92F, {0x745C, 0x0, 0x0}, 1}, + {0x2F930, {0x7471, 0x0, 0x0}, 1}, + {0x2F931, {0x7485, 0x0, 0x0}, 1}, + {0x2F932, {0x74CA, 0x0, 0x0}, 1}, + {0x2F933, {0x3F1B, 0x0, 0x0}, 1}, + {0x2F934, {0x7524, 0x0, 0x0}, 1}, + {0x2F935, {0x24C36, 0x0, 0x0}, 1}, + {0x2F936, {0x753E, 0x0, 0x0}, 1}, + {0x2F937, {0x24C92, 0x0, 0x0}, 1}, + {0x2F938, {0x7570, 0x0, 0x0}, 1}, + {0x2F939, {0x2219F, 0x0, 0x0}, 1}, + {0x2F93A, {0x7610, 0x0, 0x0}, 1}, + {0x2F93B, {0x24FA1, 0x0, 0x0}, 1}, + {0x2F93C, {0x24FB8, 0x0, 0x0}, 1}, + {0x2F93D, {0x25044, 0x0, 0x0}, 1}, + {0x2F93E, {0x3FFC, 0x0, 0x0}, 1}, + {0x2F93F, {0x4008, 0x0, 0x0}, 1}, + {0x2F940, {0x76F4, 0x0, 0x0}, 1}, + {0x2F941, {0x250F3, 0x0, 0x0}, 1}, + {0x2F942, {0x250F2, 0x0, 0x0}, 1}, + {0x2F943, {0x25119, 0x0, 0x0}, 1}, + {0x2F944, {0x25133, 0x0, 0x0}, 1}, + {0x2F945, {0x771E, 0x0, 0x0}, 1}, + {0x2F946, {0x771F, 0x0, 0x0}, 1}, + {0x2F947, {0x771F, 0x0, 0x0}, 1}, + {0x2F948, {0x774A, 0x0, 0x0}, 1}, + {0x2F949, {0x4039, 0x0, 0x0}, 1}, + {0x2F94A, {0x778B, 0x0, 0x0}, 1}, + {0x2F94B, {0x4046, 0x0, 0x0}, 1}, + {0x2F94C, {0x4096, 0x0, 0x0}, 1}, + {0x2F94D, {0x2541D, 0x0, 0x0}, 1}, + {0x2F94E, {0x784E, 0x0, 0x0}, 1}, + {0x2F94F, {0x788C, 0x0, 0x0}, 1}, + {0x2F950, {0x78CC, 0x0, 0x0}, 1}, + {0x2F951, {0x40E3, 0x0, 0x0}, 1}, + {0x2F952, {0x25626, 0x0, 0x0}, 1}, + {0x2F953, {0x7956, 0x0, 0x0}, 1}, + {0x2F954, {0x2569A, 0x0, 0x0}, 1}, + {0x2F955, {0x256C5, 0x0, 0x0}, 1}, + {0x2F956, {0x798F, 0x0, 0x0}, 1}, + {0x2F957, {0x79EB, 0x0, 0x0}, 1}, + {0x2F958, {0x412F, 0x0, 0x0}, 1}, + {0x2F959, {0x7A40, 0x0, 0x0}, 1}, + {0x2F95A, {0x7A4A, 0x0, 0x0}, 1}, + {0x2F95B, {0x7A4F, 0x0, 0x0}, 1}, + {0x2F95C, {0x2597C, 0x0, 0x0}, 1}, + {0x2F95D, {0x25AA7, 0x0, 0x0}, 1}, + {0x2F95E, {0x25AA7, 0x0, 0x0}, 1}, + {0x2F95F, {0x7AEE, 0x0, 0x0}, 1}, + {0x2F960, {0x4202, 0x0, 0x0}, 1}, + {0x2F961, {0x25BAB, 0x0, 0x0}, 1}, + {0x2F962, {0x7BC6, 0x0, 0x0}, 1}, + {0x2F963, {0x7BC9, 0x0, 0x0}, 1}, + {0x2F964, {0x4227, 0x0, 0x0}, 1}, + {0x2F965, {0x25C80, 0x0, 0x0}, 1}, + {0x2F966, {0x7CD2, 0x0, 0x0}, 1}, + {0x2F967, {0x42A0, 0x0, 0x0}, 1}, + {0x2F968, {0x7CE8, 0x0, 0x0}, 1}, + {0x2F969, {0x7CE3, 0x0, 0x0}, 1}, + {0x2F96A, {0x7D00, 0x0, 0x0}, 1}, + {0x2F96B, {0x25F86, 0x0, 0x0}, 1}, + {0x2F96C, {0x7D63, 0x0, 0x0}, 1}, + {0x2F96D, {0x4301, 0x0, 0x0}, 1}, + {0x2F96E, {0x7DC7, 0x0, 0x0}, 1}, + {0x2F96F, {0x7E02, 0x0, 0x0}, 1}, + {0x2F970, {0x7E45, 0x0, 0x0}, 1}, + {0x2F971, {0x4334, 0x0, 0x0}, 1}, + {0x2F972, {0x26228, 0x0, 0x0}, 1}, + {0x2F973, {0x26247, 0x0, 0x0}, 1}, + {0x2F974, {0x4359, 0x0, 0x0}, 1}, + {0x2F975, {0x262D9, 0x0, 0x0}, 1}, + {0x2F976, {0x7F7A, 0x0, 0x0}, 1}, + {0x2F977, {0x2633E, 0x0, 0x0}, 1}, + {0x2F978, {0x7F95, 0x0, 0x0}, 1}, + {0x2F979, {0x7FFA, 0x0, 0x0}, 1}, + {0x2F97A, {0x8005, 0x0, 0x0}, 1}, + {0x2F97B, {0x264DA, 0x0, 0x0}, 1}, + {0x2F97C, {0x26523, 0x0, 0x0}, 1}, + {0x2F97D, {0x8060, 0x0, 0x0}, 1}, + {0x2F97E, {0x265A8, 0x0, 0x0}, 1}, + {0x2F97F, {0x8070, 0x0, 0x0}, 1}, + {0x2F980, {0x2335F, 0x0, 0x0}, 1}, + {0x2F981, {0x43D5, 0x0, 0x0}, 1}, + {0x2F982, {0x80B2, 0x0, 0x0}, 1}, + {0x2F983, {0x8103, 0x0, 0x0}, 1}, + {0x2F984, {0x440B, 0x0, 0x0}, 1}, + {0x2F985, {0x813E, 0x0, 0x0}, 1}, + {0x2F986, {0x5AB5, 0x0, 0x0}, 1}, + {0x2F987, {0x267A7, 0x0, 0x0}, 1}, + {0x2F988, {0x267B5, 0x0, 0x0}, 1}, + {0x2F989, {0x23393, 0x0, 0x0}, 1}, + {0x2F98A, {0x2339C, 0x0, 0x0}, 1}, + {0x2F98B, {0x8201, 0x0, 0x0}, 1}, + {0x2F98C, {0x8204, 0x0, 0x0}, 1}, + {0x2F98D, {0x8F9E, 0x0, 0x0}, 1}, + {0x2F98E, {0x446B, 0x0, 0x0}, 1}, + {0x2F98F, {0x8291, 0x0, 0x0}, 1}, + {0x2F990, {0x828B, 0x0, 0x0}, 1}, + {0x2F991, {0x829D, 0x0, 0x0}, 1}, + {0x2F992, {0x52B3, 0x0, 0x0}, 1}, + {0x2F993, {0x82B1, 0x0, 0x0}, 1}, + {0x2F994, {0x82B3, 0x0, 0x0}, 1}, + {0x2F995, {0x82BD, 0x0, 0x0}, 1}, + {0x2F996, {0x82E6, 0x0, 0x0}, 1}, + {0x2F997, {0x26B3C, 0x0, 0x0}, 1}, + {0x2F998, {0x82E5, 0x0, 0x0}, 1}, + {0x2F999, {0x831D, 0x0, 0x0}, 1}, + {0x2F99A, {0x8363, 0x0, 0x0}, 1}, + {0x2F99B, {0x83AD, 0x0, 0x0}, 1}, + {0x2F99C, {0x8323, 0x0, 0x0}, 1}, + {0x2F99D, {0x83BD, 0x0, 0x0}, 1}, + {0x2F99E, {0x83E7, 0x0, 0x0}, 1}, + {0x2F99F, {0x8457, 0x0, 0x0}, 1}, + {0x2F9A0, {0x8353, 0x0, 0x0}, 1}, + {0x2F9A1, {0x83CA, 0x0, 0x0}, 1}, + {0x2F9A2, {0x83CC, 0x0, 0x0}, 1}, + {0x2F9A3, {0x83DC, 0x0, 0x0}, 1}, + {0x2F9A4, {0x26C36, 0x0, 0x0}, 1}, + {0x2F9A5, {0x26D6B, 0x0, 0x0}, 1}, + {0x2F9A6, {0x26CD5, 0x0, 0x0}, 1}, + {0x2F9A7, {0x452B, 0x0, 0x0}, 1}, + {0x2F9A8, {0x84F1, 0x0, 0x0}, 1}, + {0x2F9A9, {0x84F3, 0x0, 0x0}, 1}, + {0x2F9AA, {0x8516, 0x0, 0x0}, 1}, + {0x2F9AB, {0x273CA, 0x0, 0x0}, 1}, + {0x2F9AC, {0x8564, 0x0, 0x0}, 1}, + {0x2F9AD, {0x26F2C, 0x0, 0x0}, 1}, + {0x2F9AE, {0x455D, 0x0, 0x0}, 1}, + {0x2F9AF, {0x4561, 0x0, 0x0}, 1}, + {0x2F9B0, {0x26FB1, 0x0, 0x0}, 1}, + {0x2F9B1, {0x270D2, 0x0, 0x0}, 1}, + {0x2F9B2, {0x456B, 0x0, 0x0}, 1}, + {0x2F9B3, {0x8650, 0x0, 0x0}, 1}, + {0x2F9B4, {0x865C, 0x0, 0x0}, 1}, + {0x2F9B5, {0x8667, 0x0, 0x0}, 1}, + {0x2F9B6, {0x8669, 0x0, 0x0}, 1}, + {0x2F9B7, {0x86A9, 0x0, 0x0}, 1}, + {0x2F9B8, {0x8688, 0x0, 0x0}, 1}, + {0x2F9B9, {0x870E, 0x0, 0x0}, 1}, + {0x2F9BA, {0x86E2, 0x0, 0x0}, 1}, + {0x2F9BB, {0x8779, 0x0, 0x0}, 1}, + {0x2F9BC, {0x8728, 0x0, 0x0}, 1}, + {0x2F9BD, {0x876B, 0x0, 0x0}, 1}, + {0x2F9BE, {0x8786, 0x0, 0x0}, 1}, + {0x2F9BF, {0x45D7, 0x0, 0x0}, 1}, + {0x2F9C0, {0x87E1, 0x0, 0x0}, 1}, + {0x2F9C1, {0x8801, 0x0, 0x0}, 1}, + {0x2F9C2, {0x45F9, 0x0, 0x0}, 1}, + {0x2F9C3, {0x8860, 0x0, 0x0}, 1}, + {0x2F9C4, {0x8863, 0x0, 0x0}, 1}, + {0x2F9C5, {0x27667, 0x0, 0x0}, 1}, + {0x2F9C6, {0x88D7, 0x0, 0x0}, 1}, + {0x2F9C7, {0x88DE, 0x0, 0x0}, 1}, + {0x2F9C8, {0x4635, 0x0, 0x0}, 1}, + {0x2F9C9, {0x88FA, 0x0, 0x0}, 1}, + {0x2F9CA, {0x34BB, 0x0, 0x0}, 1}, + {0x2F9CB, {0x278AE, 0x0, 0x0}, 1}, + {0x2F9CC, {0x27966, 0x0, 0x0}, 1}, + {0x2F9CD, {0x46BE, 0x0, 0x0}, 1}, + {0x2F9CE, {0x46C7, 0x0, 0x0}, 1}, + {0x2F9CF, {0x8AA0, 0x0, 0x0}, 1}, + {0x2F9D0, {0x8AED, 0x0, 0x0}, 1}, + {0x2F9D1, {0x8B8A, 0x0, 0x0}, 1}, + {0x2F9D2, {0x8C55, 0x0, 0x0}, 1}, + {0x2F9D3, {0x27CA8, 0x0, 0x0}, 1}, + {0x2F9D4, {0x8CAB, 0x0, 0x0}, 1}, + {0x2F9D5, {0x8CC1, 0x0, 0x0}, 1}, + {0x2F9D6, {0x8D1B, 0x0, 0x0}, 1}, + {0x2F9D7, {0x8D77, 0x0, 0x0}, 1}, + {0x2F9D8, {0x27F2F, 0x0, 0x0}, 1}, + {0x2F9D9, {0x20804, 0x0, 0x0}, 1}, + {0x2F9DA, {0x8DCB, 0x0, 0x0}, 1}, + {0x2F9DB, {0x8DBC, 0x0, 0x0}, 1}, + {0x2F9DC, {0x8DF0, 0x0, 0x0}, 1}, + {0x2F9DD, {0x208DE, 0x0, 0x0}, 1}, + {0x2F9DE, {0x8ED4, 0x0, 0x0}, 1}, + {0x2F9DF, {0x8F38, 0x0, 0x0}, 1}, + {0x2F9E0, {0x285D2, 0x0, 0x0}, 1}, + {0x2F9E1, {0x285ED, 0x0, 0x0}, 1}, + {0x2F9E2, {0x9094, 0x0, 0x0}, 1}, + {0x2F9E3, {0x90F1, 0x0, 0x0}, 1}, + {0x2F9E4, {0x9111, 0x0, 0x0}, 1}, + {0x2F9E5, {0x2872E, 0x0, 0x0}, 1}, + {0x2F9E6, {0x911B, 0x0, 0x0}, 1}, + {0x2F9E7, {0x9238, 0x0, 0x0}, 1}, + {0x2F9E8, {0x92D7, 0x0, 0x0}, 1}, + {0x2F9E9, {0x92D8, 0x0, 0x0}, 1}, + {0x2F9EA, {0x927C, 0x0, 0x0}, 1}, + {0x2F9EB, {0x93F9, 0x0, 0x0}, 1}, + {0x2F9EC, {0x9415, 0x0, 0x0}, 1}, + {0x2F9ED, {0x28BFA, 0x0, 0x0}, 1}, + {0x2F9EE, {0x958B, 0x0, 0x0}, 1}, + {0x2F9EF, {0x4995, 0x0, 0x0}, 1}, + {0x2F9F0, {0x95B7, 0x0, 0x0}, 1}, + {0x2F9F1, {0x28D77, 0x0, 0x0}, 1}, + {0x2F9F2, {0x49E6, 0x0, 0x0}, 1}, + {0x2F9F3, {0x96C3, 0x0, 0x0}, 1}, + {0x2F9F4, {0x5DB2, 0x0, 0x0}, 1}, + {0x2F9F5, {0x9723, 0x0, 0x0}, 1}, + {0x2F9F6, {0x29145, 0x0, 0x0}, 1}, + {0x2F9F7, {0x2921A, 0x0, 0x0}, 1}, + {0x2F9F8, {0x4A6E, 0x0, 0x0}, 1}, + {0x2F9F9, {0x4A76, 0x0, 0x0}, 1}, + {0x2F9FA, {0x97E0, 0x0, 0x0}, 1}, + {0x2F9FB, {0x2940A, 0x0, 0x0}, 1}, + {0x2F9FC, {0x4AB2, 0x0, 0x0}, 1}, + {0x2F9FD, {0x29496, 0x0, 0x0}, 1}, + {0x2F9FE, {0x980B, 0x0, 0x0}, 1}, + {0x2F9FF, {0x980B, 0x0, 0x0}, 1}, + {0x2FA00, {0x9829, 0x0, 0x0}, 1}, + {0x2FA01, {0x295B6, 0x0, 0x0}, 1}, + {0x2FA02, {0x98E2, 0x0, 0x0}, 1}, + {0x2FA03, {0x4B33, 0x0, 0x0}, 1}, + {0x2FA04, {0x9929, 0x0, 0x0}, 1}, + {0x2FA05, {0x99A7, 0x0, 0x0}, 1}, + {0x2FA06, {0x99C2, 0x0, 0x0}, 1}, + {0x2FA07, {0x99FE, 0x0, 0x0}, 1}, + {0x2FA08, {0x4BCE, 0x0, 0x0}, 1}, + {0x2FA09, {0x29B30, 0x0, 0x0}, 1}, + {0x2FA0A, {0x9B12, 0x0, 0x0}, 1}, + {0x2FA0B, {0x9C40, 0x0, 0x0}, 1}, + {0x2FA0C, {0x9CFD, 0x0, 0x0}, 1}, + {0x2FA0D, {0x4CCE, 0x0, 0x0}, 1}, + {0x2FA0E, {0x4CED, 0x0, 0x0}, 1}, + {0x2FA0F, {0x9D67, 0x0, 0x0}, 1}, + {0x2FA10, {0x2A0CE, 0x0, 0x0}, 1}, + {0x2FA11, {0x4CF8, 0x0, 0x0}, 1}, + {0x2FA12, {0x2A105, 0x0, 0x0}, 1}, + {0x2FA13, {0x2A20E, 0x0, 0x0}, 1}, + {0x2FA14, {0x2A291, 0x0, 0x0}, 1}, + {0x2FA15, {0x9EBB, 0x0, 0x0}, 1}, + {0x2FA16, {0x4D56, 0x0, 0x0}, 1}, + {0x2FA17, {0x9EF9, 0x0, 0x0}, 1}, + {0x2FA18, {0x9EFE, 0x0, 0x0}, 1}, + {0x2FA19, {0x9F05, 0x0, 0x0}, 1}, + {0x2FA1A, {0x9F0F, 0x0, 0x0}, 1}, + {0x2FA1B, {0x9F16, 0x0, 0x0}, 1}, + {0x2FA1C, {0x9F3B, 0x0, 0x0}, 1}, + {0x2FA1D, {0x2A600, 0x0, 0x0}, 1}, + {0xE0100, {0x0, 0x0, 0x0}, 0}, + {0xE0101, {0x0, 0x0, 0x0}, 0}, + {0xE0102, {0x0, 0x0, 0x0}, 0}, + {0xE0103, {0x0, 0x0, 0x0}, 0}, + {0xE0104, {0x0, 0x0, 0x0}, 0}, + {0xE0105, {0x0, 0x0, 0x0}, 0}, + {0xE0106, {0x0, 0x0, 0x0}, 0}, + {0xE0107, {0x0, 0x0, 0x0}, 0}, + {0xE0108, {0x0, 0x0, 0x0}, 0}, + {0xE0109, {0x0, 0x0, 0x0}, 0}, + {0xE010A, {0x0, 0x0, 0x0}, 0}, + {0xE010B, {0x0, 0x0, 0x0}, 0}, + {0xE010C, {0x0, 0x0, 0x0}, 0}, + {0xE010D, {0x0, 0x0, 0x0}, 0}, + {0xE010E, {0x0, 0x0, 0x0}, 0}, + {0xE010F, {0x0, 0x0, 0x0}, 0}, + {0xE0110, {0x0, 0x0, 0x0}, 0}, + {0xE0111, {0x0, 0x0, 0x0}, 0}, + {0xE0112, {0x0, 0x0, 0x0}, 0}, + {0xE0113, {0x0, 0x0, 0x0}, 0}, + {0xE0114, {0x0, 0x0, 0x0}, 0}, + {0xE0115, {0x0, 0x0, 0x0}, 0}, + {0xE0116, {0x0, 0x0, 0x0}, 0}, + {0xE0117, {0x0, 0x0, 0x0}, 0}, + {0xE0118, {0x0, 0x0, 0x0}, 0}, + {0xE0119, {0x0, 0x0, 0x0}, 0}, + {0xE011A, {0x0, 0x0, 0x0}, 0}, + {0xE011B, {0x0, 0x0, 0x0}, 0}, + {0xE011C, {0x0, 0x0, 0x0}, 0}, + {0xE011D, {0x0, 0x0, 0x0}, 0}, + {0xE011E, {0x0, 0x0, 0x0}, 0}, + {0xE011F, {0x0, 0x0, 0x0}, 0}, + {0xE0120, {0x0, 0x0, 0x0}, 0}, + {0xE0121, {0x0, 0x0, 0x0}, 0}, + {0xE0122, {0x0, 0x0, 0x0}, 0}, + {0xE0123, {0x0, 0x0, 0x0}, 0}, + {0xE0124, {0x0, 0x0, 0x0}, 0}, + {0xE0125, {0x0, 0x0, 0x0}, 0}, + {0xE0126, {0x0, 0x0, 0x0}, 0}, + {0xE0127, {0x0, 0x0, 0x0}, 0}, + {0xE0128, {0x0, 0x0, 0x0}, 0}, + {0xE0129, {0x0, 0x0, 0x0}, 0}, + {0xE012A, {0x0, 0x0, 0x0}, 0}, + {0xE012B, {0x0, 0x0, 0x0}, 0}, + {0xE012C, {0x0, 0x0, 0x0}, 0}, + {0xE012D, {0x0, 0x0, 0x0}, 0}, + {0xE012E, {0x0, 0x0, 0x0}, 0}, + {0xE012F, {0x0, 0x0, 0x0}, 0}, + {0xE0130, {0x0, 0x0, 0x0}, 0}, + {0xE0131, {0x0, 0x0, 0x0}, 0}, + {0xE0132, {0x0, 0x0, 0x0}, 0}, + {0xE0133, {0x0, 0x0, 0x0}, 0}, + {0xE0134, {0x0, 0x0, 0x0}, 0}, + {0xE0135, {0x0, 0x0, 0x0}, 0}, + {0xE0136, {0x0, 0x0, 0x0}, 0}, + {0xE0137, {0x0, 0x0, 0x0}, 0}, + {0xE0138, {0x0, 0x0, 0x0}, 0}, + {0xE0139, {0x0, 0x0, 0x0}, 0}, + {0xE013A, {0x0, 0x0, 0x0}, 0}, + {0xE013B, {0x0, 0x0, 0x0}, 0}, + {0xE013C, {0x0, 0x0, 0x0}, 0}, + {0xE013D, {0x0, 0x0, 0x0}, 0}, + {0xE013E, {0x0, 0x0, 0x0}, 0}, + {0xE013F, {0x0, 0x0, 0x0}, 0}, + {0xE0140, {0x0, 0x0, 0x0}, 0}, + {0xE0141, {0x0, 0x0, 0x0}, 0}, + {0xE0142, {0x0, 0x0, 0x0}, 0}, + {0xE0143, {0x0, 0x0, 0x0}, 0}, + {0xE0144, {0x0, 0x0, 0x0}, 0}, + {0xE0145, {0x0, 0x0, 0x0}, 0}, + {0xE0146, {0x0, 0x0, 0x0}, 0}, + {0xE0147, {0x0, 0x0, 0x0}, 0}, + {0xE0148, {0x0, 0x0, 0x0}, 0}, + {0xE0149, {0x0, 0x0, 0x0}, 0}, + {0xE014A, {0x0, 0x0, 0x0}, 0}, + {0xE014B, {0x0, 0x0, 0x0}, 0}, + {0xE014C, {0x0, 0x0, 0x0}, 0}, + {0xE014D, {0x0, 0x0, 0x0}, 0}, + {0xE014E, {0x0, 0x0, 0x0}, 0}, + {0xE014F, {0x0, 0x0, 0x0}, 0}, + {0xE0150, {0x0, 0x0, 0x0}, 0}, + {0xE0151, {0x0, 0x0, 0x0}, 0}, + {0xE0152, {0x0, 0x0, 0x0}, 0}, + {0xE0153, {0x0, 0x0, 0x0}, 0}, + {0xE0154, {0x0, 0x0, 0x0}, 0}, + {0xE0155, {0x0, 0x0, 0x0}, 0}, + {0xE0156, {0x0, 0x0, 0x0}, 0}, + {0xE0157, {0x0, 0x0, 0x0}, 0}, + {0xE0158, {0x0, 0x0, 0x0}, 0}, + {0xE0159, {0x0, 0x0, 0x0}, 0}, + {0xE015A, {0x0, 0x0, 0x0}, 0}, + {0xE015B, {0x0, 0x0, 0x0}, 0}, + {0xE015C, {0x0, 0x0, 0x0}, 0}, + {0xE015D, {0x0, 0x0, 0x0}, 0}, + {0xE015E, {0x0, 0x0, 0x0}, 0}, + {0xE015F, {0x0, 0x0, 0x0}, 0}, + {0xE0160, {0x0, 0x0, 0x0}, 0}, + {0xE0161, {0x0, 0x0, 0x0}, 0}, + {0xE0162, {0x0, 0x0, 0x0}, 0}, + {0xE0163, {0x0, 0x0, 0x0}, 0}, + {0xE0164, {0x0, 0x0, 0x0}, 0}, + {0xE0165, {0x0, 0x0, 0x0}, 0}, + {0xE0166, {0x0, 0x0, 0x0}, 0}, + {0xE0167, {0x0, 0x0, 0x0}, 0}, + {0xE0168, {0x0, 0x0, 0x0}, 0}, + {0xE0169, {0x0, 0x0, 0x0}, 0}, + {0xE016A, {0x0, 0x0, 0x0}, 0}, + {0xE016B, {0x0, 0x0, 0x0}, 0}, + {0xE016C, {0x0, 0x0, 0x0}, 0}, + {0xE016D, {0x0, 0x0, 0x0}, 0}, + {0xE016E, {0x0, 0x0, 0x0}, 0}, + {0xE016F, {0x0, 0x0, 0x0}, 0}, + {0xE0170, {0x0, 0x0, 0x0}, 0}, + {0xE0171, {0x0, 0x0, 0x0}, 0}, + {0xE0172, {0x0, 0x0, 0x0}, 0}, + {0xE0173, {0x0, 0x0, 0x0}, 0}, + {0xE0174, {0x0, 0x0, 0x0}, 0}, + {0xE0175, {0x0, 0x0, 0x0}, 0}, + {0xE0176, {0x0, 0x0, 0x0}, 0}, + {0xE0177, {0x0, 0x0, 0x0}, 0}, + {0xE0178, {0x0, 0x0, 0x0}, 0}, + {0xE0179, {0x0, 0x0, 0x0}, 0}, + {0xE017A, {0x0, 0x0, 0x0}, 0}, + {0xE017B, {0x0, 0x0, 0x0}, 0}, + {0xE017C, {0x0, 0x0, 0x0}, 0}, + {0xE017D, {0x0, 0x0, 0x0}, 0}, + {0xE017E, {0x0, 0x0, 0x0}, 0}, + {0xE017F, {0x0, 0x0, 0x0}, 0}, + {0xE0180, {0x0, 0x0, 0x0}, 0}, + {0xE0181, {0x0, 0x0, 0x0}, 0}, + {0xE0182, {0x0, 0x0, 0x0}, 0}, + {0xE0183, {0x0, 0x0, 0x0}, 0}, + {0xE0184, {0x0, 0x0, 0x0}, 0}, + {0xE0185, {0x0, 0x0, 0x0}, 0}, + {0xE0186, {0x0, 0x0, 0x0}, 0}, + {0xE0187, {0x0, 0x0, 0x0}, 0}, + {0xE0188, {0x0, 0x0, 0x0}, 0}, + {0xE0189, {0x0, 0x0, 0x0}, 0}, + {0xE018A, {0x0, 0x0, 0x0}, 0}, + {0xE018B, {0x0, 0x0, 0x0}, 0}, + {0xE018C, {0x0, 0x0, 0x0}, 0}, + {0xE018D, {0x0, 0x0, 0x0}, 0}, + {0xE018E, {0x0, 0x0, 0x0}, 0}, + {0xE018F, {0x0, 0x0, 0x0}, 0}, + {0xE0190, {0x0, 0x0, 0x0}, 0}, + {0xE0191, {0x0, 0x0, 0x0}, 0}, + {0xE0192, {0x0, 0x0, 0x0}, 0}, + {0xE0193, {0x0, 0x0, 0x0}, 0}, + {0xE0194, {0x0, 0x0, 0x0}, 0}, + {0xE0195, {0x0, 0x0, 0x0}, 0}, + {0xE0196, {0x0, 0x0, 0x0}, 0}, + {0xE0197, {0x0, 0x0, 0x0}, 0}, + {0xE0198, {0x0, 0x0, 0x0}, 0}, + {0xE0199, {0x0, 0x0, 0x0}, 0}, + {0xE019A, {0x0, 0x0, 0x0}, 0}, + {0xE019B, {0x0, 0x0, 0x0}, 0}, + {0xE019C, {0x0, 0x0, 0x0}, 0}, + {0xE019D, {0x0, 0x0, 0x0}, 0}, + {0xE019E, {0x0, 0x0, 0x0}, 0}, + {0xE019F, {0x0, 0x0, 0x0}, 0}, + {0xE01A0, {0x0, 0x0, 0x0}, 0}, + {0xE01A1, {0x0, 0x0, 0x0}, 0}, + {0xE01A2, {0x0, 0x0, 0x0}, 0}, + {0xE01A3, {0x0, 0x0, 0x0}, 0}, + {0xE01A4, {0x0, 0x0, 0x0}, 0}, + {0xE01A5, {0x0, 0x0, 0x0}, 0}, + {0xE01A6, {0x0, 0x0, 0x0}, 0}, + {0xE01A7, {0x0, 0x0, 0x0}, 0}, + {0xE01A8, {0x0, 0x0, 0x0}, 0}, + {0xE01A9, {0x0, 0x0, 0x0}, 0}, + {0xE01AA, {0x0, 0x0, 0x0}, 0}, + {0xE01AB, {0x0, 0x0, 0x0}, 0}, + {0xE01AC, {0x0, 0x0, 0x0}, 0}, + {0xE01AD, {0x0, 0x0, 0x0}, 0}, + {0xE01AE, {0x0, 0x0, 0x0}, 0}, + {0xE01AF, {0x0, 0x0, 0x0}, 0}, + {0xE01B0, {0x0, 0x0, 0x0}, 0}, + {0xE01B1, {0x0, 0x0, 0x0}, 0}, + {0xE01B2, {0x0, 0x0, 0x0}, 0}, + {0xE01B3, {0x0, 0x0, 0x0}, 0}, + {0xE01B4, {0x0, 0x0, 0x0}, 0}, + {0xE01B5, {0x0, 0x0, 0x0}, 0}, + {0xE01B6, {0x0, 0x0, 0x0}, 0}, + {0xE01B7, {0x0, 0x0, 0x0}, 0}, + {0xE01B8, {0x0, 0x0, 0x0}, 0}, + {0xE01B9, {0x0, 0x0, 0x0}, 0}, + {0xE01BA, {0x0, 0x0, 0x0}, 0}, + {0xE01BB, {0x0, 0x0, 0x0}, 0}, + {0xE01BC, {0x0, 0x0, 0x0}, 0}, + {0xE01BD, {0x0, 0x0, 0x0}, 0}, + {0xE01BE, {0x0, 0x0, 0x0}, 0}, + {0xE01BF, {0x0, 0x0, 0x0}, 0}, + {0xE01C0, {0x0, 0x0, 0x0}, 0}, + {0xE01C1, {0x0, 0x0, 0x0}, 0}, + {0xE01C2, {0x0, 0x0, 0x0}, 0}, + {0xE01C3, {0x0, 0x0, 0x0}, 0}, + {0xE01C4, {0x0, 0x0, 0x0}, 0}, + {0xE01C5, {0x0, 0x0, 0x0}, 0}, + {0xE01C6, {0x0, 0x0, 0x0}, 0}, + {0xE01C7, {0x0, 0x0, 0x0}, 0}, + {0xE01C8, {0x0, 0x0, 0x0}, 0}, + {0xE01C9, {0x0, 0x0, 0x0}, 0}, + {0xE01CA, {0x0, 0x0, 0x0}, 0}, + {0xE01CB, {0x0, 0x0, 0x0}, 0}, + {0xE01CC, {0x0, 0x0, 0x0}, 0}, + {0xE01CD, {0x0, 0x0, 0x0}, 0}, + {0xE01CE, {0x0, 0x0, 0x0}, 0}, + {0xE01CF, {0x0, 0x0, 0x0}, 0}, + {0xE01D0, {0x0, 0x0, 0x0}, 0}, + {0xE01D1, {0x0, 0x0, 0x0}, 0}, + {0xE01D2, {0x0, 0x0, 0x0}, 0}, + {0xE01D3, {0x0, 0x0, 0x0}, 0}, + {0xE01D4, {0x0, 0x0, 0x0}, 0}, + {0xE01D5, {0x0, 0x0, 0x0}, 0}, + {0xE01D6, {0x0, 0x0, 0x0}, 0}, + {0xE01D7, {0x0, 0x0, 0x0}, 0}, + {0xE01D8, {0x0, 0x0, 0x0}, 0}, + {0xE01D9, {0x0, 0x0, 0x0}, 0}, + {0xE01DA, {0x0, 0x0, 0x0}, 0}, + {0xE01DB, {0x0, 0x0, 0x0}, 0}, + {0xE01DC, {0x0, 0x0, 0x0}, 0}, + {0xE01DD, {0x0, 0x0, 0x0}, 0}, + {0xE01DE, {0x0, 0x0, 0x0}, 0}, + {0xE01DF, {0x0, 0x0, 0x0}, 0}, + {0xE01E0, {0x0, 0x0, 0x0}, 0}, + {0xE01E1, {0x0, 0x0, 0x0}, 0}, + {0xE01E2, {0x0, 0x0, 0x0}, 0}, + {0xE01E3, {0x0, 0x0, 0x0}, 0}, + {0xE01E4, {0x0, 0x0, 0x0}, 0}, + {0xE01E5, {0x0, 0x0, 0x0}, 0}, + {0xE01E6, {0x0, 0x0, 0x0}, 0}, + {0xE01E7, {0x0, 0x0, 0x0}, 0}, + {0xE01E8, {0x0, 0x0, 0x0}, 0}, + {0xE01E9, {0x0, 0x0, 0x0}, 0}, + {0xE01EA, {0x0, 0x0, 0x0}, 0}, + {0xE01EB, {0x0, 0x0, 0x0}, 0}, + {0xE01EC, {0x0, 0x0, 0x0}, 0}, + {0xE01ED, {0x0, 0x0, 0x0}, 0}, + {0xE01EE, {0x0, 0x0, 0x0}, 0}, + {0xE01EF, {0x0, 0x0, 0x0}, 0}, +}; +enum { WP_FOLD_N = (int)(sizeof(WP_FOLD) / sizeof(WP_FOLD[0])) }; + +#endif /* WORDPIECE_UNICODE_H */ diff --git a/libffwd/vec.c b/libffwd/vec.c new file mode 100644 index 0000000..ec6b27d --- /dev/null +++ b/libffwd/vec.c @@ -0,0 +1,47 @@ +#include "ffwd.h" + +#include <math.h> + +/* Vector helpers */ + +int ffwd_l2_normalize(float *vec, int dim) { + if (!vec || dim <= 0) + return -1; + + float norm_sq = 0.0f; + for (int i = 0; i < dim; i++) + norm_sq += vec[i] * vec[i]; + + if (!(norm_sq > 0.0f) || !isfinite(norm_sq)) + return -1; + + float inv_norm = 1.0f / sqrtf(norm_sq); + for (int i = 0; i < dim; i++) + vec[i] *= inv_norm; + return 0; +} + +float ffwd_cosine_similarity(const float *a, const float *b, int dim) { + if (!a || !b || dim <= 0) + return 0.0f; + + float dot = 0.0f; + float norm_a_sq = 0.0f; + float norm_b_sq = 0.0f; + for (int i = 0; i < dim; i++) { + float av = a[i]; + float bv = b[i]; + dot += av * bv; + norm_a_sq += av * av; + norm_b_sq += bv * bv; + } + + if (!(norm_a_sq > 0.0f) || !(norm_b_sq > 0.0f) || !isfinite(norm_a_sq) || !isfinite(norm_b_sq) || + !isfinite(dot)) + return 0.0f; + + double denom = sqrt((double)norm_a_sq) * sqrt((double)norm_b_sq); + if (!(denom > 0.0) || !isfinite(denom)) + return 0.0f; + return (float)((double)dot / denom); +} diff --git a/libffwd/wordpiece.c b/libffwd/wordpiece.c new file mode 100644 index 0000000..dc0d995 --- /dev/null +++ b/libffwd/wordpiece.c @@ -0,0 +1,679 @@ +/* + * BERT-family WordPiece tokenizer. + * + * Pipeline matching transformers' BertTokenizer: + * BasicTokenizer: clean control/whitespace -> space CJK ideographs -> + * whitespace split -> lowercase + strip accents, if enabled -> + * split on punctuation. + * WordPiece: greedy longest-match-first against vocab.txt, with continuation + * pieces marked "##". If any piece fails, the whole token becomes [UNK]. + * + * Unicode handling fully matches transformers using generated tables + * (unicode.h): Unicode category for punctuation/control, full Zs set for + * whitespace, BERT ideograph ranges for CJK splitting, and a lowercase + NFD + + * strip-marks table for do_lower_case, with Hangul decomposed by formula. + * + * Cased and uncased multilingual encoders, such as + * bert-base-multilingual-cased / -uncased, match Hugging Face exactly, + * including Greek, Cyrillic, Vietnamese, and Korean. + * + * Folding is per-codepoint, matching transformers, which lowercases one + * character at a time, with no context-sensitive Greek final sigma. + */ + +#include "wordpiece.h" + +#include "unicode.h" + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* string -> int open-addressing map */ + +typedef struct { + const char *key; /* points into tok->id_to_token; not owned */ + int val; +} wp_entry_t; + +static uint64_t fnv1a_hash(const char *s) { + uint64_t h = 1469598103934665603ULL; + for (const unsigned char *p = (const unsigned char *)s; *p; p++) { + h ^= (uint64_t)*p; + h *= 1099511628211ULL; + } + return h; +} + +static int next_pow2(int x) { + int p = 1; + while (p < x) + p <<= 1; + return p; +} + +static void map_put(wp_entry_t *map, int cap, const char *key, int val) { + uint64_t mask = (uint64_t)cap - 1; + int pos = (int)(fnv1a_hash(key) & mask); + while (map[pos].key) { + if (!strcmp(map[pos].key, key)) { + map[pos].val = val; /* keep the first/lowest id, like a dict insert */ + return; + } + pos = (int)((pos + 1) & (int)mask); + } + map[pos].key = key; + map[pos].val = val; +} + +static int map_get(const wp_entry_t *map, int cap, const char *key) { + if (!map) + return -1; + uint64_t mask = (uint64_t)cap - 1; + int pos = (int)(fnv1a_hash(key) & mask); + while (map[pos].key) { + if (!strcmp(map[pos].key, key)) + return map[pos].val; + pos = (int)((pos + 1) & (int)mask); + } + return -1; +} + +/* UTF-8 helpers */ + +/* Decode one codepoint. Returns bytes consumed (>=1). + * On malformed lead byte, emits U+FFFD and consumes one byte so iteration advances. */ +static int utf8_decode(const unsigned char *p, const unsigned char *end, uint32_t *cp) { + unsigned char c = p[0]; + if (c < 0x80) { + *cp = c; + return 1; + } + int n; + uint32_t v; + if ((c & 0xE0) == 0xC0) { + n = 2; + v = c & 0x1F; + } else if ((c & 0xF0) == 0xE0) { + n = 3; + v = c & 0x0F; + } else if ((c & 0xF8) == 0xF0) { + n = 4; + v = c & 0x07; + } else { + *cp = 0xFFFD; + return 1; + } + if (p + n > end) { + *cp = 0xFFFD; + return 1; + } + for (int i = 1; i < n; i++) { + if ((p[i] & 0xC0) != 0x80) { + *cp = 0xFFFD; + return 1; + } + v = (v << 6) | (p[i] & 0x3F); + } + *cp = v; + return n; +} + +static int utf8_encode(uint32_t cp, char out[4]) { + if (cp < 0x80) { + out[0] = (char)cp; + return 1; + } + if (cp < 0x800) { + out[0] = (char)(0xC0 | (cp >> 6)); + out[1] = (char)(0x80 | (cp & 0x3F)); + return 2; + } + if (cp < 0x10000) { + out[0] = (char)(0xE0 | (cp >> 12)); + out[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); + out[2] = (char)(0x80 | (cp & 0x3F)); + return 3; + } + out[0] = (char)(0xF0 | (cp >> 18)); + out[1] = (char)(0x80 | ((cp >> 12) & 0x3F)); + out[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); + out[3] = (char)(0x80 | (cp & 0x3F)); + return 4; +} + +/* Unicode classification */ + +static int wp_is_whitespace(uint32_t cp) { + if (cp == ' ' || cp == '\t' || cp == '\n' || cp == '\r') + return 1; + return cp == 0x00A0 || cp == 0x1680 || (cp >= 0x2000 && cp <= 0x200A) || cp == 0x2028 || + cp == 0x2029 || cp == 0x202F || cp == 0x205F || cp == 0x3000; +} + +/* Binary search a sorted, non-overlapping [lo, hi] range table. */ +static int wp_in_ranges(uint32_t cp, const wp_range_t *r, int n) { + int lo = 0, hi = n - 1; + while (lo <= hi) { + int mid = (lo + hi) >> 1; + if (cp < r[mid].lo) + hi = mid - 1; + else if (cp > r[mid].hi) + lo = mid + 1; + else + return 1; + } + return 0; +} + +/* Match transformers' _is_control: + * tab/newline/carriage-return are whitespace, not control, and fall through to + * wp_is_whitespace; everything else is Unicode category Cc, Cf, or Co. */ +static int wp_is_control(uint32_t cp) { + if (cp == '\t' || cp == '\n' || cp == '\r') + return 0; + return wp_in_ranges(cp, WP_CONTROL_RANGES, WP_CONTROL_RANGES_N); +} + +/* transformers' _is_punctuation: the ASCII non-alphanumeric set plus every + * Unicode category P* codepoint, both folded into the generated table. */ +static int wp_is_punct(uint32_t cp) { return wp_in_ranges(cp, WP_PUNCT_RANGES, WP_PUNCT_RANGES_N); } + +static int wp_is_cjk(uint32_t cp) { + return (cp >= 0x4E00 && cp <= 0x9FFF) || (cp >= 0x3400 && cp <= 0x4DBF) || + (cp >= 0x20000 && cp <= 0x2A6DF) || (cp >= 0x2A700 && cp <= 0x2B73F) || + (cp >= 0x2B740 && cp <= 0x2B81F) || (cp >= 0x2B820 && cp <= 0x2CEAF) || + (cp >= 0xF900 && cp <= 0xFAFF) || (cp >= 0x2F800 && cp <= 0x2FA1F); +} + +/* Hangul syllable decomposition constants for the standard algorithm. + * Hangul syllables are not cased, so their do_lower_case fold is canonical NFD: + * leading (L), vowel (V), and optional trailing (T) jamo. + * None are combining marks, so they stay out of the generated WP_FOLD table. */ +enum { + WP_HANGUL_S = 0xAC00, + WP_HANGUL_L = 0x1100, + WP_HANGUL_V = 0x1161, + WP_HANGUL_T = 0x11A7, + WP_HANGUL_TCOUNT = 28, + WP_HANGUL_NCOUNT = 588, + WP_HANGUL_SCOUNT = 11172 +}; + +/* Apply do_lower_case to one codepoint: + * lowercase using Python-compatible str.lower behavior, then NFD, + * then drop combining marks. + * Writes 0..3 output codepoints to out and the count to *n_out. + * 0 outputs means the codepoint was a dropped mark. + * Hangul decomposes by formula; all else uses WP_FOLD or identity. */ +static void wp_fold(uint32_t cp, uint32_t out[3], int *n_out) { + if (cp >= WP_HANGUL_S && cp < WP_HANGUL_S + WP_HANGUL_SCOUNT) { + int s = (int)(cp - WP_HANGUL_S); + out[0] = (uint32_t)(WP_HANGUL_L + s / WP_HANGUL_NCOUNT); + out[1] = (uint32_t)(WP_HANGUL_V + (s % WP_HANGUL_NCOUNT) / WP_HANGUL_TCOUNT); + int t = s % WP_HANGUL_TCOUNT; + if (t == 0) { + *n_out = 2; + } else { + out[2] = (uint32_t)(WP_HANGUL_T + t); + *n_out = 3; + } + return; + } + int lo = 0, hi = WP_FOLD_N - 1; + while (lo <= hi) { + int mid = (lo + hi) >> 1; + if (cp < WP_FOLD[mid].cp) { + hi = mid - 1; + } else if (cp > WP_FOLD[mid].cp) { + lo = mid + 1; + } else { + int n = WP_FOLD[mid].n; + for (int i = 0; i < n; i++) + out[i] = WP_FOLD[mid].out[i]; + *n_out = n; + return; + } + } + out[0] = cp; + *n_out = 1; +} + +/* workspace */ + +struct tok_wp_workspace { + uint32_t *cps; /* cleaned + CJK-spaced codepoints of the whole input */ + int cps_cap; + uint32_t *norm; /* one word's normalized codepoints */ + int norm_cap; + char *wbuf; /* one sub-word's UTF-8 bytes */ + int wbuf_cap; + int *boff; /* byte offset of each codepoint within wbuf (+1 sentinel) */ + int boff_cap; + char *look; /* "##" + candidate substring, for vocab lookup */ + int look_cap; + int *ids; /* output ids */ + int ids_cap; +}; + +static int grow_u32(uint32_t **buf, int *cap, int need) { + if (*cap >= need) + return 0; + int n = *cap ? *cap : 64; + while (n < need) + n *= 2; + uint32_t *p = (uint32_t *)realloc(*buf, (size_t)n * sizeof(uint32_t)); + if (!p) + return -1; + *buf = p; + *cap = n; + return 0; +} + +static int grow_int(int **buf, int *cap, int need) { + if (*cap >= need) + return 0; + int n = *cap ? *cap : 64; + while (n < need) + n *= 2; + int *p = (int *)realloc(*buf, (size_t)n * sizeof(int)); + if (!p) + return -1; + *buf = p; + *cap = n; + return 0; +} + +static int grow_char(char **buf, int *cap, int need) { + if (*cap >= need) + return 0; + int n = *cap ? *cap : 256; + while (n < need) + n *= 2; + char *p = (char *)realloc(*buf, (size_t)n); + if (!p) + return -1; + *buf = p; + *cap = n; + return 0; +} + +tok_wp_workspace_t *tok_wp_workspace_new(void) { + return (tok_wp_workspace_t *)calloc(1, sizeof(tok_wp_workspace_t)); +} + +void tok_wp_workspace_free(tok_wp_workspace_t *ws) { + if (!ws) + return; + free(ws->cps); + free(ws->norm); + free(ws->wbuf); + free(ws->boff); + free(ws->look); + free(ws->ids); + free(ws); +} + +/* WordPiece core */ + +/* Append the WordPiece ids of one BasicTokenizer sub-word (codepoints + * word[0..n)) to ws->ids[*n_out]. The whole sub-word maps to [UNK] when it is + * too long or any greedy piece misses the vocab. Returns 0 / -1 on OOM. */ +static int +tok_wp_subword(const tok_wp_t *tok, tok_wp_workspace_t *ws, const uint32_t *word, int n, int *n_out) { + if (n == 0) + return 0; + if (n > tok->max_chars_per_word) { + if (grow_int(&ws->ids, &ws->ids_cap, *n_out + 1) != 0) + return -1; + ws->ids[(*n_out)++] = tok->unk_id; + return 0; + } + + /* Materialize the sub-word as UTF-8 with per-codepoint byte offsets. */ + if (grow_int(&ws->boff, &ws->boff_cap, n + 1) != 0 || + grow_char(&ws->wbuf, &ws->wbuf_cap, n * 4 + 1) != 0) + return -1; + int bytes = 0; + for (int i = 0; i < n; i++) { + ws->boff[i] = bytes; + bytes += utf8_encode(word[i], ws->wbuf + bytes); + } + ws->boff[n] = bytes; + + int start_count = *n_out; + int start = 0; + int bad = 0; + while (start < n) { + int end = n; + int matched = -1; + int matched_end = start; + while (start < end) { + int seg = ws->boff[end] - ws->boff[start]; + int lp = 0; + if (grow_char(&ws->look, &ws->look_cap, seg + 3) != 0) + return -1; + if (start > 0) { + ws->look[lp++] = '#'; + ws->look[lp++] = '#'; + } + memcpy(ws->look + lp, ws->wbuf + ws->boff[start], (size_t)seg); + ws->look[lp + seg] = '\0'; + int id = map_get((const wp_entry_t *)tok->vocab_map, tok->vocab_map_cap, ws->look); + if (id >= 0) { + matched = id; + matched_end = end; + break; + } + end--; + } + if (matched < 0) { + bad = 1; + break; + } + if (grow_int(&ws->ids, &ws->ids_cap, *n_out + 1) != 0) + return -1; + ws->ids[(*n_out)++] = matched; + start = matched_end; + } + if (bad) { + *n_out = start_count; /* discard partial pieces; emit one [UNK] */ + if (grow_int(&ws->ids, &ws->ids_cap, *n_out + 1) != 0) + return -1; + ws->ids[(*n_out)++] = tok->unk_id; + } + return 0; +} + +/* Run the full BasicTokenizer + WordPiece pipeline into ws->ids. + * Returns the token count, or -1 on allocation failure. */ +static int tok_wp_run(const tok_wp_t *tok, tok_wp_workspace_t *ws, const char *text) { + /* clean_text + tokenize_chinese_chars, in one pass over the input. */ + const unsigned char *p = (const unsigned char *)text; + const unsigned char *end = p + strlen(text); + int n_cps = 0; + while (p < end) { + uint32_t cp; + p += utf8_decode(p, end, &cp); + if (cp == 0 || cp == 0xFFFD || wp_is_control(cp)) + continue; + if (wp_is_whitespace(cp)) { + if (grow_u32(&ws->cps, &ws->cps_cap, n_cps + 1) != 0) + return -1; + ws->cps[n_cps++] = ' '; + continue; + } + int extra = wp_is_cjk(cp) ? 3 : 1; + if (grow_u32(&ws->cps, &ws->cps_cap, n_cps + extra) != 0) + return -1; + if (extra == 3) { + ws->cps[n_cps++] = ' '; + ws->cps[n_cps++] = cp; + ws->cps[n_cps++] = ' '; + } else { + ws->cps[n_cps++] = cp; + } + } + + int n_out = 0; + int i = 0; + while (i < n_cps) { + if (ws->cps[i] == ' ') { + i++; + continue; + } + int wstart = i; + while (i < n_cps && ws->cps[i] != ' ') + i++; + int wlen = i - wstart; + + /* lowercase + strip accents; the fold can drop a codepoint (combining + * mark) or expand it to up to 3 (Hangul, a few ligatures). */ + int m = 0; + if (grow_u32(&ws->norm, &ws->norm_cap, tok->do_lower_case ? wlen * 3 : wlen) != 0) + return -1; + for (int k = 0; k < wlen; k++) { + uint32_t cp = ws->cps[wstart + k]; + if (tok->do_lower_case) { + uint32_t out[3]; + int no = 0; + wp_fold(cp, out, &no); + for (int j = 0; j < no; j++) + ws->norm[m++] = out[j]; + } else { + ws->norm[m++] = cp; + } + } + + /* split_on_punc, then WordPiece each piece. */ + int run = 0; + for (int k = 0; k < m; k++) { + if (wp_is_punct(ws->norm[k])) { + if (run > 0 && tok_wp_subword(tok, ws, ws->norm + (k - run), run, &n_out) != 0) + return -1; + run = 0; + if (tok_wp_subword(tok, ws, ws->norm + k, 1, &n_out) != 0) + return -1; + } else { + run++; + } + } + if (run > 0 && tok_wp_subword(tok, ws, ws->norm + (m - run), run, &n_out) != 0) + return -1; + } + return n_out; +} + +/* loading */ + +static char *read_file(const char *path, long *out_len) { + FILE *f = fopen(path, "rb"); + if (!f) + return NULL; + if (fseek(f, 0, SEEK_END) != 0) { + fclose(f); + return NULL; + } + long len = ftell(f); + if (len < 0 || fseek(f, 0, SEEK_SET) != 0) { + fclose(f); + return NULL; + } + char *buf = (char *)malloc((size_t)len + 1); + if (!buf) { + fclose(f); + return NULL; + } + size_t rd = fread(buf, 1, (size_t)len, f); + fclose(f); + if (rd != (size_t)len) { + free(buf); + return NULL; + } + buf[len] = '\0'; + if (out_len) + *out_len = len; + return buf; +} + +/* do_lower_case from config.json: default true (BERT uncased). A + * lightweight scan is enough; we only need one boolean and avoid a JSON dep + * here, mirroring how the config layer reads small flags. */ +static int read_do_lower_case(const char *model_dir) { + char path[2048]; + snprintf(path, sizeof(path), "%s/tokenizer_config.json", model_dir); + char *buf = read_file(path, NULL); + if (!buf) + return 1; + int result = 1; + const char *k = strstr(buf, "\"do_lower_case\""); + if (k) { + const char *c = strchr(k, ':'); + if (c) { + c++; + while (*c == ' ' || *c == '\t' || *c == '\n' || *c == '\r') + c++; + if (!strncmp(c, "false", 5)) + result = 0; + } + } + free(buf); + return result; +} + +tok_wp_t *tok_wp_load(const char *model_dir) { + if (!model_dir) + return NULL; + char path[2048]; + snprintf(path, sizeof(path), "%s/vocab.txt", model_dir); + long len = 0; + char *buf = read_file(path, &len); + if (!buf) { + fprintf(stderr, "wordpiece: cannot read %s\n", path); + return NULL; + } + + /* Count lines (one token each); a single trailing newline adds no token. */ + int count = 0; + for (long i = 0; i < len; i++) + if (buf[i] == '\n') + count++; + if (len > 0 && buf[len - 1] != '\n') + count++; + + tok_wp_t *tok = (tok_wp_t *)calloc(1, sizeof(*tok)); + char **id_to_token = (char **)calloc((size_t)(count > 0 ? count : 1), sizeof(char *)); + if (!tok || !id_to_token) { + free(buf); + free(tok); + free(id_to_token); + return NULL; + } + + int n = 0; + char *line = buf; + for (long i = 0; i <= len && n < count; i++) { + if (i == len || buf[i] == '\n') { + char *e = buf + i; + if (e > line && e[-1] == '\r') + e[-1] = '\0'; + else + *e = '\0'; + id_to_token[n] = strdup(line); + if (!id_to_token[n]) { + for (int j = 0; j < n; j++) + free(id_to_token[j]); + free(id_to_token); + free(tok); + free(buf); + return NULL; + } + n++; + line = buf + i + 1; + } + } + free(buf); + + tok->id_to_token = id_to_token; + tok->vocab_size = n; + tok->do_lower_case = read_do_lower_case(model_dir); + tok->max_chars_per_word = 100; + + int cap = next_pow2(n * 2 < 16 ? 16 : n * 2); + wp_entry_t *map = (wp_entry_t *)calloc((size_t)cap, sizeof(wp_entry_t)); + if (!map) { + tok_wp_free(tok); + return NULL; + } + for (int i = 0; i < n; i++) + map_put(map, cap, id_to_token[i], i); + tok->vocab_map = map; + tok->vocab_map_cap = cap; + + tok->unk_id = map_get(map, cap, "[UNK]"); + if (tok->unk_id < 0) { + fprintf(stderr, "wordpiece: vocab.txt has no [UNK] token\n"); + tok_wp_free(tok); + return NULL; + } + return tok; +} + +/* public API*/ + +const char *tok_wp_decode(const tok_wp_t *tok, int id) { + if (!tok || id < 0 || id >= tok->vocab_size) + return NULL; + return tok->id_to_token[id]; +} + +int tok_wp_token_id(const tok_wp_t *tok, const char *token) { + if (!tok || !token) + return -1; + return map_get((const wp_entry_t *)tok->vocab_map, tok->vocab_map_cap, token); +} + +int tok_wp_encode_into(const tok_wp_t *tok, + tok_wp_workspace_t *ws, + const char *text, + int *out_ids, + int out_cap, + int *out_n_tokens) { + if (out_n_tokens) + *out_n_tokens = 0; + if (!tok || !ws || !text) + return -1; + int n = tok_wp_run(tok, ws, text); + if (n < 0) + return -1; + if (out_n_tokens) + *out_n_tokens = n; + if (!out_ids || out_cap < n) + return out_ids ? -2 : -1; + memcpy(out_ids, ws->ids, (size_t)n * sizeof(int)); + return 0; +} + +int *tok_wp_encode_with_workspace(const tok_wp_t *tok, + tok_wp_workspace_t *ws, + const char *text, + int *out_n_tokens) { + if (out_n_tokens) + *out_n_tokens = 0; + if (!tok || !ws || !text) + return NULL; + int n = tok_wp_run(tok, ws, text); + if (n < 0) + return NULL; + int *ids = (int *)malloc((size_t)(n > 0 ? n : 1) * sizeof(int)); + if (!ids) + return NULL; + memcpy(ids, ws->ids, (size_t)n * sizeof(int)); + if (out_n_tokens) + *out_n_tokens = n; + return ids; +} + +int *tok_wp_encode(const tok_wp_t *tok, const char *text, int *out_n) { + if (out_n) + *out_n = 0; + tok_wp_workspace_t *ws = tok_wp_workspace_new(); + if (!ws) + return NULL; + int *ids = tok_wp_encode_with_workspace(tok, ws, text, out_n); + tok_wp_workspace_free(ws); + return ids; +} + +void tok_wp_free(tok_wp_t *tok) { + if (!tok) + return; + if (tok->id_to_token) { + for (int i = 0; i < tok->vocab_size; i++) + free(tok->id_to_token[i]); + free(tok->id_to_token); + } + free(tok->vocab_map); + free(tok); +} diff --git a/libffwd/wordpiece.h b/libffwd/wordpiece.h new file mode 100644 index 0000000..f8a63d6 --- /dev/null +++ b/libffwd/wordpiece.h @@ -0,0 +1,72 @@ +/* + * BERT-family WordPiece tokenizer. + * + * Implements BERT tokenization: BasicTokenizer (cleanup, optional lowercase + * and accent stripping, CJK spacing, whitespace splitting, and punctuation + * splitting), then greedy longest-match WordPiece using vocab.txt. + * + * Encodes text only. The frontend adds [CLS]/[SEP], just as the byte-level BPE + * backend leaves terminal and separator tokens to the frontend. + * + * Public API mirrors bpe.h: load, encode with reusable workspace, decode, + * resolve named special tokens, and free. This lets both tokenizers share one + * tokenizer interface later. + */ + +#ifndef TOK_WP_H +#define TOK_WP_H + +typedef struct { + char **id_to_token; /* [vocab_size] vocab.txt entries, line order = id */ + int vocab_size; + int unk_id; /* [UNK] id, resolved at load */ + int do_lower_case; /* lowercase + strip accents before WordPiece */ + int max_chars_per_word; /* words longer than this map to [UNK] (BERT: 100) */ + + /* Opaque token-string -> id hash map. */ + void *vocab_map; + int vocab_map_cap; +} tok_wp_t; + +typedef struct tok_wp_workspace tok_wp_workspace_t; + +/* Load <model_dir>/vocab.txt and do_lower_case from + * <model_dir>/tokenizer_config.json; defaults to lowercase. + * Returns NULL on error: missing vocab.txt, missing [UNK], or allocation failure. */ +tok_wp_t *tok_wp_load(const char *model_dir); + +/* Decode one id to its vocab token string, or NULL. + * Returns an internal pointer. */ +const char *tok_wp_decode(const tok_wp_t *tok, int id); + +/* Resolve a named token (e.g. "[CLS]") to its id, or -1 when absent. */ +int tok_wp_token_id(const tok_wp_t *tok, const char *token); + +/* Encode UTF-8 text into a malloc'd id array and set *out_n_tokens. + * Returns NULL on error and sets *out_n_tokens = 0. */ +int *tok_wp_encode(const tok_wp_t *tok, const char *text, int *out_n_tokens); + +/* Reusable scratch space for repeated encoding with fewer allocations. */ +tok_wp_workspace_t *tok_wp_workspace_new(void); +void tok_wp_workspace_free(tok_wp_workspace_t *ws); + +/* Encode into caller-provided out_ids[0..out_cap-1]. + * Returns 0 on success, -1 on error, or -2 if out_cap is too small. + * If non-NULL, *out_n_tokens is always set to tokens needed/emitted. */ +int tok_wp_encode_into(const tok_wp_t *tok, + tok_wp_workspace_t *ws, + const char *text, + int *out_ids, + int out_cap, + int *out_n_tokens); + +/* Same tokenization as tok_wp_encode(). + * Reuses ws internally and only allocates the returned int[]. */ +int *tok_wp_encode_with_workspace(const tok_wp_t *tok, + tok_wp_workspace_t *ws, + const char *text, + int *out_n_tokens); + +void tok_wp_free(tok_wp_t *tok); + +#endif /* TOK_WP_H */ |
