diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 606cff2..02f7d5e 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,6 +1,8 @@ -[bumpversion] -current_version = 4.0.5 -commit = True -tag = True - -[bumpversion:file:setup.py] +[bumpversion] +current_version = 5.0.0 +commit = True +tag = True + +[bumpversion:file:pyproject.toml] +search = version = "{current_version}" +replace = version = "{new_version}" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1be0fd6..14b064b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,7 +19,7 @@ jobs: - uses: actions/checkout@v4.1.7 - name: Setting up Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: 3.13 @@ -37,15 +37,31 @@ jobs: run: | pytest - - name: Ensuring documentation builds - run: | - cd docs && make clean && make html - - uses: actions/upload-artifact@v4.6.0 with: name: dist-sdist path: dist/*.tar.gz + docs: + name: Building documentation + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4.1.7 + + - name: Setting up Python + uses: actions/setup-python@v6 + with: + python-version: '3.13' + + - name: Installing release dependencies + run: | + python -m pip install --upgrade pip + pip install -e ".[release]" + + - name: Building documentation + run: | + sphinx-build -W -n --keep-going -b html docs docs/_build/html + build_wheels: needs: [sdist] name: "[${{ strategy.job-index }}/${{ strategy.job-total }}] py${{ matrix.py }} on ${{ matrix.os }}" @@ -54,12 +70,12 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest, windows-latest, macos-14] - py: ["cp39", "cp310", "cp311", "cp312", "cp313", "pp39", "pp310"] + py: ["cp39", "cp310", "cp311", "cp312", "cp313", "cp314", "pp311"] steps: - uses: actions/checkout@v4.1.7 - - uses: actions/setup-python@v5 + - uses: actions/setup-python@v6 name: Setting up Python with: python-version: '3.13' @@ -71,12 +87,12 @@ jobs: platforms: all - name: Build & test wheels - uses: pypa/cibuildwheel@v2.22.0 + uses: pypa/cibuildwheel@v3.2.1 env: CIBW_ARCHS_LINUX: auto aarch64 ppc64le s390x CIBW_ARCHS_MACOS: x86_64 arm64 universal2 CIBW_BUILD: "${{ matrix.py }}-*" - CIBW_TEST_SKIP: "*_arm64 *_universal2:arm64" + CIBW_TEST_SKIP: "*_aarch64 *_ppc64le *_s390x" - uses: actions/upload-artifact@v4.6.0 with: @@ -108,7 +124,7 @@ jobs: - uses: actions/checkout@v3 - name: Setting up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v6 with: python-version: 3.13 diff --git a/.gitignore b/.gitignore index b2ef22d..67247d8 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ _build/ dist/ .idea .vscode/settings.json +uv.lock diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2301d2d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,95 @@ +# Changelog + +## v5.0.0 (unreleased) + +### Added + +- A SAX-style streaming parser, `yyjson.sax()`. It parses inputs of unlimited + size with bounded memory - a fixed sliding window plus the nesting depth - + calling optional methods on a handler object for each token, and can stop + early when a handler returns `False`. Sources can be `str`, `bytes`, a + `pathlib.Path`, or a binary file-like object. +- `Document()` accepts binary file-like objects and parses them directly into + a document. +- The module-level `loads()` is now implemented in C and accepts `str`, + `bytes`, `bytearray`, or `pathlib.Path`. +- `Document.from_obj()` and `Document.from_json()` classmethods - explicit + counterparts to the type-guessing constructor, resolving the `Document(str)` + ambiguity between "parse this JSON text" and "build from this Python + string". +- `Document.patch()` accepts any `Document`-constructible value (a `dict`, + `list`, JSON text, or `Path`), wrapping it in a temporary `Document`. +- `Document.bytes_read`: the number of input bytes consumed by the parse + (useful with `ReaderFlags.STOP_WHEN_DONE`, e.g. for NDJSON). +- `SAXHandler`, an optional base class for `sax()` handlers: subclass it and + override only the events you need, with IDE completion and static type + checking. +- `sax()` accepts any object exporting a contiguous byte buffer + (`bytearray`, `memoryview`, `mmap`, ...) as a zero-copy source. The buffer + is pinned for the duration of the parse; resizing it from a handler + callback raises `BufferError`. +- Type annotations: a `py.typed` marker and `.pyi` stubs for the C extension. +- New `ReaderFlags`: `JSON5` (full JSON5 parsing), `ALLOW_BOM`, and the + individual `ALLOW_EXT_NUMBER`, `ALLOW_EXT_ESCAPE`, `ALLOW_EXT_WHITESPACE`, + `ALLOW_SINGLE_QUOTED_STR`, and `ALLOW_UNQUOTED_KEY` extensions. JSON5 is + supported by the DOM readers only; `sax()` ignores non-standard flags. +- New `WriterFlags`: `FP_TO_FLOAT` and `WriterFlags.fp_to_fixed(precision)` + for controlling floating-point output, `LOWERCASE_HEX` for lowercase + `\uXXXX` escapes, and `ALLOW_INVALID_UNICODE`. +- Python 3.14 and PyPy 3.11 wheels. + +### Changed + +- Major performance work on the read path - object-key caching, direct + document-tape walking, an ASCII fast path, and presized containers. + `loads()` and `Document.as_obj` are now competitive with the fastest + Python JSON libraries. +- Parsing from file-like objects and `Path`s now reads into a single owned + buffer parsed in place: roughly 20% faster with roughly 20% lower peak + memory. +- Parse errors from `Document()` and `loads()` now include the position: + line, column and byte offset for in-memory and stream inputs, byte offset + for file paths. +- Dictionary keys must be strings when serializing; other key types now raise + `TypeError` instead of producing undefined behaviour. +- Tuples are serialized as JSON arrays. +- Converting documents nested deeper than 1024 container levels raises + `RecursionError` instead of risking a C stack overflow. +- `flags` and `default` arguments are keyword-only. (They always were at + runtime; the documentation and type stubs now agree.) +- `Document.freeze()` and `Document.thaw()` return the document itself + (previously `None`), allowing chaining such as + `Document(obj).freeze().dumps()`. +- `Document.patch()` no longer converts its patch argument between the + frozen and thawed representations in place; the argument comes back + exactly as it went in. +- Opening a file that is missing or unreadable via `Document(Path)`, + `loads(Path)`, or `sax(Path)` raises `OSError` (e.g. `FileNotFoundError`) + carrying the filename, instead of a generic `ValueError`. +- A non-blocking stream returning `None` from `read()`/`readinto()` ("no + data available yet") raises `BlockingIOError` instead of being treated as + end-of-input and silently truncating the document. +- Converting thawed (mutable) documents to Python objects uses the same + optimized path as frozen documents; `patch()` results convert roughly 30% + faster and the 1024-level nesting limit now applies uniformly. +- Wheels are no longer built against the limited API (required by the new + ASCII fast path), and EOL PyPy 3.9/3.10 wheels are no longer produced. +- The vendored yyjson was upgraded to 0.12.0. + +### Fixed + +- A UTF-8 corruption bug on platforms where `char` is signed. +- A possible segfault when parsing extremely deep documents. +- A crash when `Document.patch()` was called with a non-`Document` value. +- A memory leak when serializing integers larger than 64 bits. +- A memory leak when `Document.__init__()` was invoked more than once on the + same object. +- Non-ASCII file paths now work on Windows: `Document(Path)` and + `sax(Path)` open paths through the wide-character API instead of the ANSI + codepage. +- A segfault when serializing a `Decimal` subclass whose `__str__` raises. +- An allocation failure during `freeze()`/`thaw()` could destroy the + document's contents while reporting success; it now raises `MemoryError` + with the document intact. Other out-of-memory conditions during + serialization now raise `MemoryError` instead of silently dropping data + or raising `SystemError`. diff --git a/MANIFEST.in b/MANIFEST.in index 0952c7b..3475fc4 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -5,4 +5,10 @@ include yyjson/memory.c include yyjson/memory.h include yyjson/document.c include yyjson/document.h -include yyjson/decimal.h \ No newline at end of file +include yyjson/element_to_primitive.h +include yyjson/decimal.h +include yyjson/pathlib.h +include yyjson/sax.c +include yyjson/sax.h +include yyjson/py.typed +include yyjson/__init__.pyi \ No newline at end of file diff --git a/README.md b/README.md index 667f57d..9a35a04 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ![py_yyjson Logo](misc/logo_small.png) -Fast and flexible Python JSON parsing built on the excellent [yyjson][] +Fast and flexible Python JSON/JSON5 parsing built on the excellent [yyjson][] project. ![GitHub Sponsors](https://img.shields.io/github/sponsors/tktech) @@ -30,6 +30,9 @@ project. objects. - **Traceable**: `yyjson` uses Python's memory allocator by default, so you can trace memory leaks and other memory issues using Python's built-in tools. +- **SAX-style parsing**: An optional SAX-style parser allowing you to read + JSON documents of unlimited size with bounded memory usage, typically 2-5x + faster than `ijson`. ## Documentation diff --git a/docs/api.rst b/docs/api.rst index 669926a..73d0789 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -1,6 +1,8 @@ API --- +.. currentmodule:: yyjson + .. note:: When creating and manipulating a :class:`Document`, it's important to keep diff --git a/docs/changelog.md b/docs/changelog.md new file mode 100644 index 0000000..66efc0f --- /dev/null +++ b/docs/changelog.md @@ -0,0 +1,2 @@ +```{include} ../CHANGELOG.md +``` diff --git a/docs/conf.py b/docs/conf.py index 9ffa736..a0426dd 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -32,7 +32,21 @@ "sphinx.ext.viewcode", "sphinx.ext.todo", "sphinx.ext.doctest", + "sphinx.ext.intersphinx", "sphinx_copybutton", + "myst_parser", +] + +intersphinx_mapping = { + "python": ("https://docs.python.org/3", None), +} + +# `:type foo: callable, optional` in the C docstrings produces py:class +# lookups for words that aren't classes; ignore those so the build can run +# with -n (nitpicky) and -W (warnings are errors) in CI. +nitpick_ignore = [ + ("py:class", "callable"), + ("py:class", "optional"), ] # Add any paths that contain templates here, relative to this directory. diff --git a/docs/development.rst b/docs/development.rst index bfbd427..ed99ef1 100644 --- a/docs/development.rst +++ b/docs/development.rst @@ -1,29 +1,37 @@ Development =========== -To install development requirements, use: +To build the extension and install development requirements, use: pip install -e ".[test]" -You can then build the project using ``python setup.py develop``. Keep in -mind since this is a C extension, you'll need a C compiler installed. On -Windows, you can use Visual Studio Community Edition. On Linux, you can -use GCC or Clang. You'll also need to re-run it each time you make a -change to a ``.c`` or ``.h`` file to recompile. +Keep in mind since this is a C extension, you'll need a C compiler installed. +On Windows, you can use Visual Studio Community Edition. On Linux and macOS, +you can use GCC or Clang. You'll also need to re-run the install each time +you make a change to a ``.c`` or ``.h`` file to recompile. To run the tests, just type ``pytest``. To prepare for a release or to rebuild documentation, you need a few extra dependencies: pip install -e ".[release]" -You can then rebuild the documentation by running ``make html`` within the -``docs/`` directory. +You can then rebuild the documentation with the same strict build CI uses: + sphinx-build -W -n -b html docs docs/_build/html -Why not Poetry? ---------------- -Poetry is a great tool, but it's not a good fit for this project. Poetry -is designed to manage Python projects, and this project is a C extension -with a Python wrapper. Poetry's current support for building C extensions -is a hack which may change at any time. \ No newline at end of file +Profiling +--------- + +Performance is a core feature of this project, not an afterthought - being +several times faster than the builtin ``json`` module is most of the reason +py_yyjson exists. Changes to the hot paths (parsing, DOM-to-Python +conversion, and serialization) should come with before/after numbers, and +"obvious" improvements should be measured rather than assumed: more than one +past optimization turned out to be a wash once benchmarked. + +Benchmarks intentionally live outside of this repository in +`json_benchmark `_, which compares +py_yyjson against other popular JSON libraries with a single harness and a +corpus of real-world documents. If you're proposing a performance change, +run it before and after. diff --git a/docs/index.rst b/docs/index.rst index cd7a1d9..8b5e19e 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -6,8 +6,9 @@ api.rst development.rst + changelog.md -Fast and flexible Python JSON parsing built on the excellent `yyjson`_ library. +Fast and flexible Python JSON/JSON5 parsing built on the excellent `yyjson`_ library. .. image:: https://img.shields.io/github/sponsors/tktech :alt: GitHub Sponsors @@ -40,6 +41,9 @@ Features objects. - **Traceable**: `yyjson` uses Python's memory allocator by default, so you can trace memory leaks and other memory issues using Python's built-in tools. +- **SAX-style parsing**: An optional SAX-style parser allows you to read JSON + documents of unlimited size with bounded memory usage, typically 2-5x + faster than `ijson`. Installation @@ -108,8 +112,8 @@ spent parsing JSON in Python is spent just creating the Python objects! .. code-block:: python - >> from pathlib import Path - >> from yyjson import Document + >>> from pathlib import Path + >>> from yyjson import Document >>> doc = Document(Path("canada.json")) >>> features = doc.get_pointer("/features") @@ -178,7 +182,7 @@ if we wanted to allow comments and trailing commas, we could do: >>> from yyjson import Document, ReaderFlags, WriterFlags >>> doc = Document( ... '{"hello": "world",} // This is a comment', - ... ReaderFlags.ALLOW_COMMENTS | ReaderFlags.ALLOW_TRAILING_COMMAS + ... flags=ReaderFlags.ALLOW_COMMENTS | ReaderFlags.ALLOW_TRAILING_COMMAS ... ) @@ -229,5 +233,54 @@ too large for Python's float type as Decimals: +Streaming (SAX) parsing +^^^^^^^^^^^^^^^^^^^^^^^ + +Some documents are too large to hold in memory, and sometimes you only need +a few values out of a huge file. :func:`yyjson.sax` parses JSON as a stream +of events with bounded memory: peak usage is a fixed sliding window +(``window_size``, 256 KiB by default) plus the nesting depth, no matter how +large the input is. The source can be a ``str``, any bytes-like object +(``bytes``, ``bytearray``, ``memoryview``, ``mmap``, ...), a +``pathlib.Path``, or a binary file-like object. + +The handler is any object; each of the following methods is called if it +exists, and every one of them is optional: + +* ``obj_begin()`` / ``obj_end(count)`` -- an object opened or closed; + ``count`` is the number of members. +* ``arr_begin()`` / ``arr_end(count)`` -- an array opened or closed; + ``count`` is the number of elements. +* ``key(value)`` -- an object member key. +* ``string(value)`` -- a string value. +* ``number(value)`` -- an ``int``, ``float``, or (with the Decimal reader + flags) ``Decimal`` value. +* ``boolean(value)`` / ``null()``. + +For example, collecting every key used anywhere in a document: + +.. code-block:: python + + >>> from yyjson import sax, SAXHandler + >>> class KeyCollector(SAXHandler): + ... def __init__(self): + ... self.keys = set() + ... def key(self, value): + ... self.keys.add(value) + ... + >>> collector = KeyCollector() + >>> sax('{"a": 1, "b": {"c": [1, 2]}}', collector) + >>> sorted(collector.keys) + ['a', 'b', 'c'] + +A handler method can return ``False`` to stop parsing early -- for example +once you've found the value you were looking for -- and ``sax()`` returns +normally. The reader flags are honored, so combining ``sax()`` with +``ReaderFlags.NUMBERS_AS_DECIMAL`` streams perfect-precision numbers. + +The one constraint is that a single string or number token cannot be larger +than the window; such an input raises ``ValueError``. + + .. _yyjson: https://github.com/ibireme/yyjson .. _json_benchmark: https://github.com/tktech/json_benchmark \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 97fbed6..3e1e550 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "yyjson" -version = "4.0.6" +version = "5.0.0" description = "JSON parser & serializer built on yyjson" readme = "README.md" authors = [ @@ -22,11 +22,13 @@ classifiers = [ "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Operating System :: OS Independent", "Topic :: Software Development :: Libraries :: Python Modules", ] +requires-python = ">=3.9" [project.urls] Homepage = "https://github.com/tktech/py_yyjson" @@ -37,6 +39,7 @@ test = ["pytest"] release = [ "sphinx", "sphinx-copybutton", + "myst-parser", "ghp-import", "bumpversion", "black", @@ -45,7 +48,20 @@ release = [ [tool.setuptools] ext-modules = [ - { name = "cyyjson", sources = ["yyjson/binding.c", "yyjson/yyjson.c", "yyjson/memory.c", "yyjson/document.c"], py-limited-api = true} + { name = "cyyjson", sources = ["yyjson/binding.c", "yyjson/yyjson.c", "yyjson/memory.c", "yyjson/document.c", "yyjson/sax.c"] } ] packages = ["yyjson"] +[tool.setuptools.package-data] +yyjson = ["py.typed", "*.pyi"] + +[tool.cibuildwheel] +enable = ["pypy"] +test-requires = "pytest" +test-command = "pytest {project}/tests" + +[tool.uv] +cache-keys = [ + { file = "yyjson/*.c" }, + { file = "yyjson/*.h" } +] diff --git a/tests/test_document.py b/tests/test_document.py index 62afd04..5d45636 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -25,6 +25,38 @@ def test_document_from_str(): assert doc.as_obj == {"hello": "world"} +def test_document_from_json(): + """Document.from_json always parses str/bytes/Path as JSON text.""" + doc = Document.from_json('{"a": 1}') + assert doc.as_obj == {"a": 1} + assert doc.is_thawed is False # parsed documents are immutable + + assert Document.from_json(b'{"a": 1}').as_obj == {"a": 1} + + # Reader flags are honored. + doc = Document.from_json( + '{"a": 1,}', flags=ReaderFlags.ALLOW_TRAILING_COMMAS + ) + assert doc.as_obj == {"a": 1} + + # A non-JSON-text argument is a TypeError, not a silent build-from-object. + with pytest.raises(TypeError): + Document.from_json({"a": 1}) + with pytest.raises(TypeError): + Document.from_json(123) + + # Invalid JSON raises ValueError. + with pytest.raises(ValueError): + Document.from_json("not json") + + +def test_document_from_json_path(tmp_path): + """Document.from_json reads and parses a file given a Path.""" + p = tmp_path / "doc.json" + p.write_text('{"greeting": "café 日本"}', encoding="utf-8") + assert Document.from_json(p).as_obj == {"greeting": "café 日本"} + + def test_document_types(): """Ensure each primitive type can be upcast (which does not have its own dedicated test.)""" @@ -41,6 +73,31 @@ def test_document_types(): assert doc.as_obj == dst +def test_document_unicode_as_obj(): + """Non-ASCII strings must survive the round-trip through .as_obj, not just + dumps(). Regression test for the ASCII fast-path miscounting multi-byte + UTF-8 continuation bytes.""" + cases = [ + "café", # 2-byte sequences + "naïve", + "日本語", # 3-byte sequences + "Ω≈ç√", + "🙇🎉", # 4-byte sequences (astral plane) + "mixed café 日本 🙇 tail", # ASCII interleaved with multi-byte + ] + for value in cases: + doc = Document('{"key": "%s"}' % value) + assert doc.as_obj == {"key": value} + + # Non-ASCII object keys go through the same fast-path. + doc = Document('{"café": "value"}') + assert doc.as_obj == {"café": "value"} + + # The bytes input path decodes through the same conversion. + doc = Document('["日本語"]'.encode("utf-8")) + assert doc.as_obj == ["日本語"] + + def test_document_dumps(): """ Ensure we can properly dump a document to a string. @@ -152,6 +209,30 @@ def test_document_boolean_type(): assert doc.dumps() == "[false]" assert doc.as_obj == [False] +def test_document_list_type(): + doc = Document('[1,2,3,4]') + assert doc.dumps() == '[1,2,3,4]' + assert doc.as_obj == [1, 2, 3, 4] + + doc = Document([1, 2, 3, 4]) + assert doc.dumps() == '[1,2,3,4]' + assert doc.as_obj == [1, 2, 3, 4] + +def test_document_tuple_type(): + doc = Document(()) + assert doc.dumps() == '[]' + + doc = Document((1,)) + assert doc.dumps() == '[1]' + + doc = Document((1, 2, 3, 4)) + assert doc.dumps() == '[1,2,3,4]' + + doc = Document([(1, 2), (3, 4)]) + assert doc.dumps() == '[[1,2],[3,4]]' + + doc = Document({'test': (1, 2)}) + assert doc.dumps() == '{"test":[1,2]}' def test_document_none_type(): """ @@ -166,6 +247,27 @@ def test_document_none_type(): assert doc.as_obj == [None] +def test_document_dict_type(): + """ + Ensure we can load and dump the dict type. + """ + doc = Document('{"a": "b"}') + assert doc.dumps() == '{"a":"b"}' + assert doc.as_obj == {'a': 'b'} + + doc = Document({"a": "b"}) + assert doc.dumps() == '{"a":"b"}' + assert doc.as_obj == {'a': 'b'} + + with pytest.raises(TypeError) as exc: + Document({1: 'b'}) + assert exc.value.args[0] == 'Dictionary keys must be strings' + + with pytest.raises(TypeError) as exc: + Document({'\ud83d\ude47': 'foo'}) + assert exc.value.args[0] == 'Dictionary keys must be strings' + + def test_document_get_pointer(): """ Ensure JSON pointers work. @@ -240,3 +342,151 @@ def test_document_freeze(): doc.freeze() assert doc.is_thawed is False + + +def test_document_size(): + """ + Test the size attribute that returns the size of data read from original JSON input. + """ + # Test with immutable document (created from JSON string) + json_str = '{"hello": "world", "number": 42}' + doc = Document(json_str) + assert doc.bytes_read == len(json_str) + + # Test with different sized JSON inputs + small_json = "{}" + doc_small = Document(small_json) + assert doc_small.bytes_read == len(small_json) + + large_json = '{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}], "count": 2}' + doc_large = Document(large_json) + assert doc_large.bytes_read == len(large_json) + + # Test with mutable document (created from Python object) - should return 0 + doc_mutable = Document({"hello": "world"}) + assert doc_mutable.bytes_read == 0 + + +def test_document_deeply_nested_raises(): + """Deeply nested input must raise a catchable RecursionError rather than + overflowing the C stack, matching the stdlib json module.""" + depth = 100_000 + + # Parsing succeeds (yyjson's reader is iterative); materializing to Python + # objects recurses and must raise instead of segfaulting. + doc = Document("[" * depth + "]" * depth) + with pytest.raises(RecursionError): + doc.as_obj + + # Serializing a deeply nested Python object recurses on the way in. + nested = [] + current = nested + for _ in range(depth): + child = [] + current.append(child) + current = child + with pytest.raises(RecursionError): + Document(nested) + + +class _NonBlockingReadinto: + """Simulates a non-blocking binary stream: chunks, then None forever.""" + + def __init__(self, *chunks): + self._chunks = list(chunks) + + def readinto(self, buf): + if not self._chunks: + return None + chunk = self._chunks.pop(0) + buf[: len(chunk)] = chunk + return len(chunk) + + +class _NonBlockingRead: + def __init__(self, *chunks): + self._chunks = list(chunks) + + def read(self, n): + if not self._chunks: + return None + return self._chunks.pop(0) + + +def test_stream_nonblocking_readinto_raises(): + """None from readinto() means "no data yet", not EOF. Previously this + silently truncated: b'[1]' followed by None parsed as [1].""" + with pytest.raises(BlockingIOError): + Document(_NonBlockingReadinto(b"[1]")) + with pytest.raises(BlockingIOError): + Document(_NonBlockingReadinto()) + + +def test_stream_nonblocking_read_raises(): + with pytest.raises(BlockingIOError): + Document(_NonBlockingRead(b'{"a": 1}')) + with pytest.raises(BlockingIOError): + Document(_NonBlockingRead()) + + +def test_thawed_conversion_parity(): + """The thawed (mutable) conversion path shares one implementation with + the frozen path; all scalar types and containers round-trip.""" + from decimal import Decimal + + obj = { + "nums": [1, -2, 3.5, 2**80, Decimal("1.23")], + "nested": {"a": [True, False, None, "x"]}, + "unicode": "café € 日本語", + "empty": {}, + "repeated_keys": [{"id": i, "name": "n"} for i in range(100)], + } + doc = Document(obj) + assert doc.is_thawed + assert doc.as_obj == obj + + +def test_thawed_deep_nesting_raises(): + """Conversion depth is capped at 1024 for thawed documents too (it always + was for frozen ones).""" + deep = cur = [] + for _ in range(1100): + nxt = [] + cur.append(nxt) + cur = nxt + doc = Document(deep) + with pytest.raises(RecursionError): + doc.as_obj + + +def test_freeze_thaw_chaining(): + doc = Document({"a": 1}) + assert doc.freeze() is doc + assert doc.is_thawed is False + assert doc.thaw() is doc + assert doc.is_thawed is True + # already in the target state: still returns self + assert doc.thaw() is doc + assert Document({"a": 1}).freeze().dumps() == '{"a":1}' + + +def test_parse_errors_include_position(): + """Parse errors report line/column/byte for in-memory and stream inputs, + and at least the byte offset for file paths.""" + import io + from yyjson import loads + + bad = '{\n "a": 1,\n bad\n}' + with pytest.raises(ValueError, match=r"at line 3, column \d+ \(byte \d+\)"): + Document(bad) + with pytest.raises(ValueError, match=r"at line 3, column \d+"): + loads(bad.encode()) + with pytest.raises(ValueError, match=r"at line 3, column \d+"): + Document(io.BytesIO(bad.encode())) + + +def test_parse_error_from_path_has_byte_offset(tmp_path): + p = tmp_path / "bad.json" + p.write_bytes(b'{"a": 1, bad}') + with pytest.raises(ValueError, match=r"at byte \d+"): + Document(p) diff --git a/tests/test_flags.py b/tests/test_flags.py new file mode 100644 index 0000000..0aa52df --- /dev/null +++ b/tests/test_flags.py @@ -0,0 +1,120 @@ +""" +Tests for the ReaderFlags/WriterFlags additions (yyjson 0.11/0.12 flags). +""" +import math +import re + +import pytest + +from yyjson import Document, ReaderFlags, WriterFlags, loads + + +def test_flag_values_match_yyjson_header(): + """The enum values must match the vendored yyjson.h constants.""" + header = open("yyjson/yyjson.h").read() + + def c_value(name): + m = re.search(rf"{name}\s+= 1 << (\d+);", header) + return 1 << int(m.group(1)) + + assert ReaderFlags.ALLOW_BOM == c_value("YYJSON_READ_ALLOW_BOM") + assert ReaderFlags.ALLOW_EXT_NUMBER == c_value("YYJSON_READ_ALLOW_EXT_NUMBER") + assert ReaderFlags.ALLOW_EXT_ESCAPE == c_value("YYJSON_READ_ALLOW_EXT_ESCAPE") + assert ReaderFlags.ALLOW_EXT_WHITESPACE == c_value( + "YYJSON_READ_ALLOW_EXT_WHITESPACE" + ) + assert ReaderFlags.ALLOW_SINGLE_QUOTED_STR == c_value( + "YYJSON_READ_ALLOW_SINGLE_QUOTED_STR" + ) + assert ReaderFlags.ALLOW_UNQUOTED_KEY == c_value("YYJSON_READ_ALLOW_UNQUOTED_KEY") + assert WriterFlags.LOWERCASE_HEX == c_value("YYJSON_WRITE_LOWERCASE_HEX") + assert WriterFlags.ALLOW_INVALID_UNICODE == c_value( + "YYJSON_WRITE_ALLOW_INVALID_UNICODE" + ) + assert WriterFlags.FP_TO_FLOAT == 1 << 27 + assert WriterFlags.fp_to_fixed(15) == 15 << 28 + # JSON5 mirrors the composite in yyjson.h + assert ReaderFlags.JSON5 == ( + ReaderFlags.ALLOW_TRAILING_COMMAS + | ReaderFlags.ALLOW_COMMENTS + | ReaderFlags.ALLOW_INF_AND_NAN + | ReaderFlags.ALLOW_EXT_NUMBER + | ReaderFlags.ALLOW_EXT_ESCAPE + | ReaderFlags.ALLOW_EXT_WHITESPACE + | ReaderFlags.ALLOW_SINGLE_QUOTED_STR + | ReaderFlags.ALLOW_UNQUOTED_KEY + ) + + +def test_allow_bom(): + data = b'\xef\xbb\xbf{"a": 1}' + with pytest.raises(ValueError): + Document(data) + assert Document(data, flags=ReaderFlags.ALLOW_BOM).as_obj == {"a": 1} + + +def test_json5(): + doc = """ + { + // a comment + unquoted: 'single-quoted', + hex: 0x7B, + leadingDot: .5, + nan: NaN, + trailing: [1, 2, 3,], + } + """ + with pytest.raises(ValueError): + Document(doc) + obj = Document(doc, flags=ReaderFlags.JSON5).as_obj + assert obj["unquoted"] == "single-quoted" + assert obj["hex"] == 0x7B + assert obj["leadingDot"] == 0.5 + assert math.isnan(obj["nan"]) + assert obj["trailing"] == [1, 2, 3] + + +def test_individual_json5_flags(): + assert Document( + "{a: 1}", flags=ReaderFlags.ALLOW_UNQUOTED_KEY + ).as_obj == {"a": 1} + assert Document( + "['x']", flags=ReaderFlags.ALLOW_SINGLE_QUOTED_STR + ).as_obj == ["x"] + assert Document("[0xFF]", flags=ReaderFlags.ALLOW_EXT_NUMBER).as_obj == [255] + + +def test_fp_to_float(): + third = 1.0 / 3.0 + assert Document({"v": third}).dumps() == '{"v":0.3333333333333333}' + assert ( + Document({"v": third}).dumps(flags=WriterFlags.FP_TO_FLOAT) + == '{"v":0.33333334}' + ) + + +def test_fp_to_fixed(): + third = 1.0 / 3.0 + assert ( + Document({"v": third}).dumps(flags=WriterFlags.fp_to_fixed(2)) + == '{"v":0.33}' + ) + # trailing zeros are removed + assert Document({"v": 0.5}).dumps(flags=WriterFlags.fp_to_fixed(6)) == '{"v":0.5}' + # composes with other flags + out = Document({"v": third}).dumps( + flags=WriterFlags.fp_to_fixed(3) | WriterFlags.WRITE_NEWLINE_AT_END + ) + assert out == '{"v":0.333}\n' + with pytest.raises(ValueError): + WriterFlags.fp_to_fixed(0) + with pytest.raises(ValueError): + WriterFlags.fp_to_fixed(16) + + +def test_lowercase_hex(): + doc = Document({"s": "é"}) + upper = doc.dumps(flags=WriterFlags.ESCAPE_UNICODE) + lower = doc.dumps(flags=WriterFlags.ESCAPE_UNICODE | WriterFlags.LOWERCASE_HEX) + assert upper == '{"s":"\\u00E9"}' + assert lower == '{"s":"\\u00e9"}' diff --git a/tests/test_numbers.py b/tests/test_numbers.py index 61fda78..43f84b1 100644 --- a/tests/test_numbers.py +++ b/tests/test_numbers.py @@ -10,6 +10,14 @@ def test_big_numbers(): The test set is from: https://blog.trl.sn/blog/what-is-a-json-number/#python-3-8-1 """ + # The 4302-digit value below trips the int<->str conversion DoS guard + # (default 4300 digits) on interpreters whose decimal is pure Python + # (PyPy), since _pydecimal round-trips the coefficient through int(). + # CPython's C _decimal is exempt. Guarded: CPython < 3.9.14/3.10.7 + # lacks the function. + if hasattr(sys, "set_int_max_str_digits"): + sys.set_int_max_str_digits(5000) + test_numbers = [ "10", "1000000000", diff --git a/tests/test_patch.py b/tests/test_patch.py index 545e0eb..625863b 100644 --- a/tests/test_patch.py +++ b/tests/test_patch.py @@ -45,6 +45,51 @@ def test_json_patch(context): assert modified.as_obj == context["modified"] +@pytest.mark.parametrize( + "self_content", + [ + {"a": 1, "b": 2}, # mutable Document (built from a Python object) + '{"a": 1, "b": 2}', # immutable Document (parsed from JSON text) + ], +) +def test_patch_coerces_plain_arguments(self_content): + """ + patch() accepts a plain dict / list / JSON-string patch and coerces it to a + Document, so callers don't have to wrap it by hand. + """ + doc = Document(self_content) + + # JSON Patch as a plain list of operations. + assert doc.patch([{"op": "add", "path": "/c", "value": 3}]).as_obj == { + "a": 1, "b": 2, "c": 3 + } + # Merge-Patch as a plain dict (null deletes "b"). + assert doc.patch({"b": None, "c": 3}, use_merge_patch=True).as_obj == { + "a": 1, "c": 3 + } + # Merge-Patch given as a JSON string is parsed, not treated as a value. + assert doc.patch('{"b": 9}', use_merge_patch=True).as_obj == {"a": 1, "b": 9} + + +@pytest.mark.parametrize( + "self_content", + [ + {"a": 1}, # mutable Document + '{"a": 1}', # immutable Document + ], +) +@pytest.mark.parametrize("bad_patch", ["not valid json", 42]) +def test_patch_invalid_argument_raises(self_content, bad_patch): + """ + An argument that can't become a valid patch Document raises cleanly on both + mutable and immutable documents. The mutable path previously skipped the + type check and segfaulted on the raw pointer cast. + """ + doc = Document(self_content) + with pytest.raises((TypeError, ValueError)): + doc.patch(bad_patch) + + def test_json_patch_samples(): tests = Document(Path(__file__).parent / "tests.json").as_obj @@ -68,3 +113,47 @@ def test_json_patch_samples(): assert modified.as_obj == test["expected"] + + +def test_patch_does_not_mutate_argument(): + """Applying a patch never converts the caller's documents between + representations (previously the patch argument was frozen/thawed in + place to match the target).""" + # thawed patch onto a frozen target + target = Document('{"a": 1}') + patch = Document([{"op": "add", "path": "/b", "value": 2}]) + assert patch.is_thawed is True + assert target.patch(patch).as_obj == {"a": 1, "b": 2} + assert patch.is_thawed is True + assert target.is_thawed is False + + # frozen patch onto a thawed target + patch2 = Document('[{"op": "add", "path": "/b", "value": 2}]') + target2 = Document({"a": 1}) + assert patch2.is_thawed is False + assert target2.patch(patch2).as_obj == {"a": 1, "b": 2} + assert patch2.is_thawed is False + assert target2.is_thawed is True + + # one patch object works against all target representations, repeatedly + for _ in range(3): + for make_target in (lambda: Document('{"a": 1}'), + lambda: Document({"a": 1})): + for p in (patch, patch2): + assert make_target().patch(p).as_obj == {"a": 1, "b": 2} + assert patch.is_thawed is True + assert patch2.is_thawed is False + + +def test_merge_patch_does_not_mutate_argument(): + # thawed patch onto a frozen target + patch = Document({"b": 2}) + target = Document('{"a": 1}') + assert target.patch(patch, use_merge_patch=True).as_obj == {"a": 1, "b": 2} + assert patch.is_thawed is True + + # frozen patch onto a thawed target + patch2 = Document('{"b": 2}') + target2 = Document({"a": 1}) + assert target2.patch(patch2, use_merge_patch=True).as_obj == {"a": 1, "b": 2} + assert patch2.is_thawed is False diff --git a/tests/test_paths.py b/tests/test_paths.py new file mode 100644 index 0000000..d8c27cb --- /dev/null +++ b/tests/test_paths.py @@ -0,0 +1,74 @@ +""" +Path handling must work with non-ASCII names on every platform. On Windows in +particular, narrow fopen() interprets paths in the ANSI codepage, so these +only pass if the extension routes paths through the wide-char API. +""" +import pytest + +from yyjson import Document, sax, loads + +NON_ASCII_NAMES = [ + "café.json", # latin-1 range + "日本語.json", # BMP, outside latin-1 + "emoji-😀.json", # astral plane + "mixed-šžé-中.json", # a bit of everything +] + + +def non_ascii_file(tmp_path, name, payload=b'{"hello": "world"}'): + """Create a file with a non-ASCII name, skipping if the filesystem can't + represent it (e.g. a non-UTF-8 POSIX locale).""" + path = tmp_path / name + try: + path.write_bytes(payload) + except (OSError, UnicodeEncodeError): + pytest.skip(f"filesystem cannot represent {name!r}") + return path + + +@pytest.mark.parametrize("name", NON_ASCII_NAMES) +def test_document_non_ascii_path(tmp_path, name): + path = non_ascii_file(tmp_path, name) + assert Document(path).as_obj == {"hello": "world"} + + +@pytest.mark.parametrize("name", NON_ASCII_NAMES) +def test_loads_non_ascii_path(tmp_path, name): + path = non_ascii_file(tmp_path, name) + assert loads(path) == {"hello": "world"} + + +@pytest.mark.parametrize("name", NON_ASCII_NAMES) +def test_sax_non_ascii_path(tmp_path, name): + class KeyCollector: + def __init__(self): + self.keys = [] + + def key(self, value): + self.keys.append(value) + + path = non_ascii_file(tmp_path, name) + collector = KeyCollector() + sax(path, collector) + assert collector.keys == ["hello"] + + +def test_document_non_ascii_dir(tmp_path): + """Non-ASCII directory components, not just the file name.""" + subdir = tmp_path / "ディレクトリ" + try: + subdir.mkdir() + except (OSError, UnicodeEncodeError): + pytest.skip("filesystem cannot represent non-ASCII directory") + path = non_ascii_file(subdir, "fichier-à.json") + assert Document(path).as_obj == {"hello": "world"} + + +def test_missing_path_raises_oserror(tmp_path): + """Opening a missing file reports the OS error with the filename.""" + path = tmp_path / "does-not-exist-é.json" + with pytest.raises(OSError) as excinfo: + Document(path) + assert excinfo.value.filename == path + with pytest.raises(OSError): + sax(path, object()) diff --git a/tests/test_sax.py b/tests/test_sax.py new file mode 100644 index 0000000..5706bc4 --- /dev/null +++ b/tests/test_sax.py @@ -0,0 +1,335 @@ +""" +Tests for the streaming (SAX) reader, ``yyjson.sax``. + +The central test reconstructs a Python object from the SAX event stream and +asserts it equals the DOM parse (``Document.as_obj``) -- across many window +sizes and, for file-like sources, tiny read chunks that force the C window to +compact and rebase at every byte boundary. +""" + +import io +import json +import decimal + +import pytest + +import yyjson +from yyjson import ReaderFlags + + +class Builder: + """A handler that rebuilds the native Python object from SAX events.""" + + def __init__(self): + self._stack = [] # containers being built + self._keys = [] # pending key per open object + self.result = None + + def _emit(self, value): + if not self._stack: + self.result = value + return + top = self._stack[-1] + if isinstance(top, dict): + top[self._keys[-1]] = value + else: + top.append(value) + + def obj_begin(self): + d = {} + self._emit(d) + self._stack.append(d) + self._keys.append(None) + + def obj_end(self, count): + assert len(self._stack.pop()) == count + self._keys.pop() + + def arr_begin(self): + a = [] + self._emit(a) + self._stack.append(a) + + def arr_end(self, count): + assert len(self._stack.pop()) == count + + def key(self, value): + self._keys[-1] = value + + def string(self, value): + self._emit(value) + + def number(self, value): + self._emit(value) + + def boolean(self, value): + self._emit(value) + + def null(self): + self._emit(None) + + +class ChunkedReader: + """A binary file-like that yields at most ``chunk`` bytes per readinto.""" + + def __init__(self, data, chunk): + self.data = data + self.pos = 0 + self.chunk = chunk + + def readinto(self, buf): + n = min(len(buf), self.chunk, len(self.data) - self.pos) + buf[:n] = self.data[self.pos:self.pos + n] + self.pos += n + return n + + +def sax_to_obj(source, **kw): + b = Builder() + yyjson.sax(source, b, **kw) + return b.result + + +DOCUMENTS = [ + "null", "true", "false", "42", "-17", "3.14", "1e10", '""', '"hello"', + '"a\\"b\\\\c\\n\\u00e9"', "[]", "{}", "[1,2,3]", '{"a":1,"b":2}', + '{"nested":{"a":[1,2,{"b":null}],"c":"x"},"list":[[],[1],[[2]]]}', + '[null,true,false,"s",1,2.5,-3,"\\u20ac"]', + '{"unicode":"café €","empty":"","deep":[[[[[1]]]]]}', +] + + +@pytest.mark.parametrize("doc", DOCUMENTS) +def test_sax_matches_dom_bytes(doc): + expected = yyjson.Document(doc).as_obj + for window in (0, 8192, 65536): + got = sax_to_obj(doc.encode("utf-8"), window_size=window) + assert got == expected, (doc, window) + + +@pytest.mark.parametrize("doc", DOCUMENTS) +def test_sax_matches_dom_str(doc): + assert sax_to_obj(doc) == yyjson.Document(doc).as_obj + + +@pytest.mark.parametrize("doc", DOCUMENTS) +def test_sax_filelike_tiny_chunks(doc): + """Force compaction/rebasing at every byte boundary with 1..3 byte reads.""" + data = doc.encode("utf-8") + expected = yyjson.Document(doc).as_obj + for chunk in (1, 2, 3, 7): + got = sax_to_obj(ChunkedReader(data, chunk), window_size=8192) + assert got == expected, (doc, chunk) + + +def test_sax_large_document_compaction(): + """A document much larger than the window streams correctly.""" + obj = [{"i": i, "name": f"item-{i}", "vals": [i, i * 2, i * 3]} + for i in range(5000)] + data = json.dumps(obj).encode("utf-8") + assert len(data) > 8192 # ensures the window compacts many times + got = sax_to_obj(ChunkedReader(data, 64), window_size=8192) + assert got == obj + + +def test_sax_counts(): + counts = [] + + class H: + def obj_end(self, n): + counts.append(("obj", n)) + + def arr_end(self, n): + counts.append(("arr", n)) + + yyjson.sax(b'{"a":1,"b":2,"c":[1,2,3,4]}', H()) + assert counts == [("arr", 4), ("obj", 3)] + + +def test_sax_decimal_mode(): + got = sax_to_obj(b"[1, 2.5, 123456789012345678901234567890]", + flags=ReaderFlags.NUMBERS_AS_DECIMAL) + assert got == [decimal.Decimal("1"), decimal.Decimal("2.5"), + decimal.Decimal("123456789012345678901234567890")] + assert all(isinstance(x, decimal.Decimal) for x in got) + + +def test_sax_bignum_as_raw(): + got = sax_to_obj(b"[1, 2.5, 123456789012345678901234567890]", + flags=ReaderFlags.BIG_NUMBERS_AS_DECIMAL) + assert got[0] == 1 and isinstance(got[0], int) + assert got[1] == 2.5 and isinstance(got[1], float) + assert got[2] == decimal.Decimal("123456789012345678901234567890") + + +def test_sax_early_stop_returns_false(): + seen = [] + + class H: + def number(self, v): + seen.append(v) + return v < 3 # stop once we hit 3 + + # returns normally (no exception) even though parsing stopped early + yyjson.sax(b"[1,2,3,4,5]", H()) + assert seen == [1, 2, 3] + + +def test_sax_exception_propagates(): + class Boom(Exception): + pass + + class H: + def string(self, v): + raise Boom(v) + + with pytest.raises(Boom): + yyjson.sax(b'["x"]', H()) + + +def test_sax_missing_methods_are_skipped(): + # A handler with only one method still parses the whole document. + strings = [] + + class H: + def string(self, v): + strings.append(v) + + yyjson.sax(b'{"a":"x","b":[1,"y",{"c":"z"}]}', H()) + assert strings == ["x", "y", "z"] + + +@pytest.mark.parametrize("bad,exc", [ + (b"", ValueError), + (b"[1,2", ValueError), + (b'{"a":}', ValueError), + (b"[1] trailing", ValueError), +]) +def test_sax_errors(bad, exc): + with pytest.raises(exc): + yyjson.sax(bad, object()) + + +def test_sax_stop_when_done_allows_trailing(): + # Only the first document is consumed; trailing content is not an error. + got = sax_to_obj(b'{"a":1} {"b":2}', flags=ReaderFlags.STOP_WHEN_DONE) + assert got == {"a": 1} + + +def test_sax_token_larger_than_window(): + big = b'"' + b"x" * 20000 + b'"' + with pytest.raises(ValueError, match="window"): + yyjson.sax(big, object(), window_size=8192) + # fits with a larger window + assert sax_to_obj(big, window_size=65536) == "x" * 20000 + + +def test_sax_max_depth(): + data = b"[" * 100 + b"]" * 100 + with pytest.raises(ValueError, match="depth"): + yyjson.sax(data, object(), max_depth=10) + + +def test_sax_bad_source_type(): + with pytest.raises(TypeError): + yyjson.sax(1234, object()) + + +def test_sax_path(tmp_path): + p = tmp_path / "doc.json" + p.write_bytes(b'{"from":"path","nums":[1,2,3]}') + assert sax_to_obj(p) == {"from": "path", "nums": [1, 2, 3]} + + +@pytest.mark.parametrize("doc", DOCUMENTS) +def test_sax_matches_dom_buffer_sources(doc): + """bytearray and memoryview sources parse zero-copy via the buffer + protocol and match the DOM parse.""" + expected = yyjson.Document(doc).as_obj + data = doc.encode("utf-8") + assert sax_to_obj(bytearray(data)) == expected + assert sax_to_obj(memoryview(data)) == expected + # a slice of a larger buffer works too + padded = memoryview(b" " + data + b" ")[5:5 + len(data)] + assert sax_to_obj(padded) == expected + + +def test_sax_mmap_source(tmp_path): + import mmap + + p = tmp_path / "doc.json" + p.write_bytes(b'{"via": "mmap", "n": [1, 2, 3]}') + with open(p, "rb") as fp: + with mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ) as mm: + assert sax_to_obj(mm) == {"via": "mmap", "n": [1, 2, 3]} + + +def test_sax_bytearray_resize_during_parse_is_safe(): + """The source buffer is pinned while parsing: a handler that resizes the + bytearray gets a BufferError instead of invalidating the parser's view + of the memory (previously a use-after-free).""" + source = bytearray(b'{"a": 1, "b": 2, "c": 3}') + + class Resizer: + def key(self, value): + source.clear() # would realloc/free the buffer + + with pytest.raises(BufferError): + yyjson.sax(source, Resizer()) + + # the buffer survived intact and can be parsed again afterwards + assert bytes(source) == b'{"a": 1, "b": 2, "c": 3}' + assert sax_to_obj(source) == {"a": 1, "b": 2, "c": 3} + + +def test_sax_non_contiguous_buffer_rejected(): + data = memoryview(b"[1, 2, 3, 4, 5, 6]")[::2] # non-contiguous view + with pytest.raises((BufferError, TypeError)): + yyjson.sax(data, object()) + + +def test_sax_handler_base_class(): + """Subclassing SAXHandler and overriding a subset works; events left as + None on the base are skipped exactly like missing methods.""" + + class KeyCollector(yyjson.SAXHandler): + def __init__(self): + self.keys = [] + + def key(self, value): + self.keys.append(value) + + collector = KeyCollector() + yyjson.sax('{"a": 1, "b": {"c": [true, null]}}', collector) + assert collector.keys == ["a", "b", "c"] + assert collector.obj_begin is None # inherited default, skipped by C + + +def test_sax_nonblocking_stream_raises(): + """None from readinto()/read() (non-blocking, no data yet) raises + BlockingIOError instead of silently truncating the stream.""" + + class NonBlockingReadinto: + def __init__(self, *chunks): + self._chunks = list(chunks) + + def readinto(self, buf): + if not self._chunks: + return None + chunk = self._chunks.pop(0) + buf[: len(chunk)] = chunk + return len(chunk) + + class NonBlockingRead: + def __init__(self, *chunks): + self._chunks = list(chunks) + + def read(self, n): + if not self._chunks: + return None + return self._chunks.pop(0) + + with pytest.raises(BlockingIOError): + yyjson.sax(NonBlockingReadinto(b"[1, 2"), Builder()) + with pytest.raises(BlockingIOError): + yyjson.sax(NonBlockingRead(b"[1, 2"), Builder()) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index a9c6482..7d2ce54 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -33,3 +33,16 @@ def default(obj): {"example": ClassThatCantBeSerialized()}, default=default ) assert doc.as_obj["example"] == "I'm a string now!" + + +def test_decimal_subclass_raising_str_does_not_crash(): + """str() of a Decimal subclass can raise; this used to feed NULL into + PyUnicode_AsUTF8AndSize and segfault the interpreter.""" + from decimal import Decimal + + class Evil(Decimal): + def __str__(self): + raise RuntimeError("nope") + + with pytest.raises(RuntimeError, match="nope"): + yyjson.Document({"d": Evil("1.5")}) diff --git a/tests/test_shim.py b/tests/test_shim.py index ef59624..512a31e 100644 --- a/tests/test_shim.py +++ b/tests/test_shim.py @@ -3,8 +3,11 @@ We ignore most options. """ +import json from io import BytesIO, StringIO +import pytest + import yyjson @@ -37,3 +40,41 @@ def test_loads(): Ensure we can load a document from a string. """ assert yyjson.loads('{"a":1,"b":2}') == {"a": 1, "b": 2} + + +@pytest.mark.parametrize( + "value, expected", + [ + ("hello", '"hello"'), # a str is a JSON string value, not JSON text + ("123", '"123"'), # previously silently emitted 123 + ("[1,2]", '"[1,2]"'), # previously silently emitted [1,2] + ("3.14", '"3.14"'), # previously silently emitted 3.14 + ("", '""'), + (123, "123"), + (3.14, "3.14"), + (True, "true"), + (None, "null"), + ({"a": "b"}, '{"a":"b"}'), + (["x", 1, None], '["x",1,null]'), + ], +) +def test_dumps_serializes_values_like_json(value, expected): + """ + dumps() must serialize its argument as a Python value, matching the stdlib + json module -- in particular a str must not be reparsed as JSON text. + """ + assert yyjson.dumps(value) == expected + assert yyjson.dumps(value) == json.dumps(value, separators=(",", ":")) + + +def test_dump_str_value(): + """dump() of a str writes a JSON string, not the reparsed text.""" + with StringIO() as fp: + yyjson.dump("hello", fp) + assert fp.getvalue() == '"hello"' + + +def test_dumps_bytes_not_serializable(): + """bytes is not JSON-serializable, matching stdlib json (not reparsed).""" + with pytest.raises(TypeError): + yyjson.dumps(b"hello") diff --git a/yyjson/__init__.py b/yyjson/__init__.py index aa8bed3..7f97447 100644 --- a/yyjson/__init__.py +++ b/yyjson/__init__.py @@ -1,8 +1,19 @@ -__all__ = ["Document", "ReaderFlags", "WriterFlags"] +__all__ = [ + "Document", + "ReaderFlags", + "WriterFlags", + "SAXHandler", + "sax", + "loads", + "load", + "dumps", + "dump", +] import enum +from typing import Any, Callable, Optional -from cyyjson import Document +from cyyjson import Document, sax, loads class ReaderFlags(enum.IntFlag): @@ -34,6 +45,35 @@ class ReaderFlags(enum.IntFlag): #: Like `NUMBERS_AS_DECIMAL`, but only for numbers that are too large to #: fit in a native type. BIG_NUMBERS_AS_DECIMAL = 0x80 + #: Allow a UTF-8 BOM at the start of the input, as commonly produced by + #: Windows tooling. + ALLOW_BOM = 0x100 + #: Allow extended number formats, such as hex (``0x7B``), a leading dot + #: (``.123``), or a leading plus sign. + ALLOW_EXT_NUMBER = 0x200 + #: Allow extended escape sequences, such as ``\\a``, ``\\0``, ``\\x7B``. + ALLOW_EXT_ESCAPE = 0x400 + #: Allow extended whitespace, such as ``\\v``, ``\\f``, or U+2028. + ALLOW_EXT_WHITESPACE = 0x800 + #: Allow single-quoted strings, such as ``'hello'``. + ALLOW_SINGLE_QUOTED_STR = 0x1000 + #: Allow unquoted object keys, such as ``{a: 1}``. + ALLOW_UNQUOTED_KEY = 0x2000 + #: Parse `JSON5 `_: comments, trailing commas, + #: Inf/NaN, extended numbers, escapes and whitespace, single-quoted + #: strings, and unquoted keys. Combine with ``ALLOW_BOM`` if the input + #: may start with a BOM. Only the DOM readers (``Document``, ``loads``) + #: support JSON5; ``sax()`` ignores non-standard flags. + JSON5 = ( + 0x04 # ALLOW_TRAILING_COMMAS + | 0x08 # ALLOW_COMMENTS + | 0x10 # ALLOW_INF_AND_NAN + | 0x200 # ALLOW_EXT_NUMBER + | 0x400 # ALLOW_EXT_ESCAPE + | 0x800 # ALLOW_EXT_WHITESPACE + | 0x1000 # ALLOW_SINGLE_QUOTED_STR + | 0x2000 # ALLOW_UNQUOTED_KEY + ) class WriterFlags(enum.IntFlag): @@ -55,21 +95,115 @@ class WriterFlags(enum.IntFlag): ALLOW_INF_AND_NAN = 0x08 #: Writes Infinity and NaN as `null` instead of raising an error. INF_AND_NAN_AS_NULL = 0x10 + #: Write strings containing invalid unicode as-is instead of raising an + #: error. The output may not be valid JSON or UTF-8. + ALLOW_INVALID_UNICODE = 0x20 #: Write a newline at the end of the JSON string. WRITE_NEWLINE_AT_END = 0x80 + #: Write ``\\uXXXX`` escapes with lowercase hex digits. + LOWERCASE_HEX = 0x100 + #: Write floating-point numbers as single-precision (``double`` is cast + #: to ``float`` first): shorter output, may lose precision. Ignored when + #: combined with :meth:`fp_to_fixed`. + FP_TO_FLOAT = 0x08000000 + + @staticmethod + def fp_to_fixed(precision): + """ + A flag to write floating-point numbers using fixed-point notation + with the given number of decimals (1-15), like + ``Number.prototype.toFixed(precision)`` but with trailing zeros + removed. Combine it with other flags, e.g. + ``dumps(flags=WriterFlags.PRETTY | WriterFlags.fp_to_fixed(3))``. + + :param precision: Number of decimal places, 1 through 15. + :returns: The flag value as an ``int``. + """ + if not 1 <= precision <= 15: + raise ValueError("precision must be between 1 and 15") + return precision << 28 + + +class SAXHandler: + """ + Optional base class for :func:`sax` handlers. + + :func:`sax` accepts *any* object as a handler and calls each event method + only if it exists, so subclassing this is never required - but doing so + and overriding just the events you care about gives you IDE completion + and static type checking for free: + + .. code-block:: python + + class KeyCollector(SAXHandler): + def __init__(self): + self.keys = [] + + def key(self, value): + self.keys.append(value) + + Unimplemented events are skipped entirely. A handler method may return + ``False`` to stop parsing early. + """ + + #: Called when an object opens (``{``). + obj_begin: Optional[Callable[[], Any]] = None + #: Called when an object closes (``}``), with its member count. + obj_end: Optional[Callable[[int], Any]] = None + #: Called when an array opens (``[``). + arr_begin: Optional[Callable[[], Any]] = None + #: Called when an array closes (``]``), with its element count. + arr_end: Optional[Callable[[int], Any]] = None + #: Called for each object member key. + key: Optional[Callable[[str], Any]] = None + #: Called for each string value. + string: Optional[Callable[[str], Any]] = None + #: Called for each number value (``int``, ``float``, or ``Decimal``). + number: Optional[Callable[[Any], Any]] = None + #: Called for each boolean value. + boolean: Optional[Callable[[bool], Any]] = None + #: Called for each ``null``. + null: Optional[Callable[[], Any]] = None def load(fp): - return Document(fp.read()).as_obj + """ + Parse a JSON document from an open file-like object and return the + equivalent Python object. + The entire stream is read into memory before parsing. To process inputs + too large for that, or to pull just a few values out of a huge document, + see :func:`sax`. -def loads(s): - return Document(s).as_obj + :param fp: An open file-like object with a ``read()`` method. + :returns: The equivalent Python object. + """ + return loads(fp.read()) def dumps(obj, *, default=None): - return Document(obj, default=default).dumps() + """ + Serialize a Python object to a JSON ``str``. + + :param obj: The Python object to serialize. + :param default: A function called to convert objects that are not + JSON serializable. Should return a JSON serializable + version of the object or raise a TypeError. + :type default: callable, optional + :returns: The serialized JSON document as a ``str``. + """ + return Document.from_obj(obj, default=default).dumps() def dump(obj, fp, *, default=None): - fp.write(Document(obj, default=default).dumps()) + """ + Serialize a Python object as JSON to an open file-like object. + + :param obj: The Python object to serialize. + :param fp: An open file-like object with a ``write()`` method. + :param default: A function called to convert objects that are not + JSON serializable. Should return a JSON serializable + version of the object or raise a TypeError. + :type default: callable, optional + """ + fp.write(Document.from_obj(obj, default=default).dumps()) diff --git a/yyjson/__init__.pyi b/yyjson/__init__.pyi index fa17364..191580a 100644 --- a/yyjson/__init__.pyi +++ b/yyjson/__init__.pyi @@ -1,6 +1,25 @@ import enum +from decimal import Decimal from pathlib import Path -from typing import Any, Optional, List, Dict, Union, Callable +from typing import Any, BinaryIO, Optional, List, Dict, Union, Callable + +class SAXHandler: + """ + Optional base class for :func:`sax` handlers. Subclass it and override + any subset of the event attributes with methods; events left as ``None`` + are skipped. Subclassing is not required - :func:`sax` accepts any + object and calls each event method only if present. + """ + + obj_begin: Optional[Callable[[], Any]] + obj_end: Optional[Callable[[int], Any]] + arr_begin: Optional[Callable[[], Any]] + arr_end: Optional[Callable[[int], Any]] + key: Optional[Callable[[str], Any]] + string: Optional[Callable[[str], Any]] + number: Optional[Callable[[Any], Any]] + boolean: Optional[Callable[[bool], Any]] + null: Optional[Callable[[], Any]] class ReaderFlags(enum.IntFlag): STOP_WHEN_DONE = 0x02 @@ -11,92 +30,91 @@ class ReaderFlags(enum.IntFlag): NUMBERS_AS_DECIMAL = 0x20 BIGNUM_AS_RAW = 0x80 BIG_NUMBERS_AS_DECIMAL = 0x80 + ALLOW_BOM = 0x100 + ALLOW_EXT_NUMBER = 0x200 + ALLOW_EXT_ESCAPE = 0x400 + ALLOW_EXT_WHITESPACE = 0x800 + ALLOW_SINGLE_QUOTED_STR = 0x1000 + ALLOW_UNQUOTED_KEY = 0x2000 + JSON5 = 0x3E1C class WriterFlags(enum.IntFlag): PRETTY = 0x01 + PRETTY_TWO_SPACES = 0x40 ESCAPE_UNICODE = 0x02 ESCAPE_SLASHES = 0x04 ALLOW_INF_AND_NAN = 0x08 INF_AND_NAN_AS_NULL = 0x10 + ALLOW_INVALID_UNICODE = 0x20 WRITE_NEWLINE_AT_END = 0x80 + LOWERCASE_HEX = 0x100 + FP_TO_FLOAT = 0x08000000 + @staticmethod + def fp_to_fixed(precision: int) -> int: ... -Content = Union[str, bytes, List, Dict, Path] +# The constructor either parses (str/bytes/Path) or builds from a Python +# object; with a ``default`` callback the build side can accept anything, so +# this lists the natively-handled types rather than being exhaustive. +Content = Union[ + str, bytes, Path, BinaryIO, Dict, List, tuple, int, float, bool, Decimal, None +] class Document: as_obj: Any def __init__( self, content: Content, - flags: Optional[ReaderFlags] = ..., - default: Callable[[Any], Any] = ..., + *, + flags: ReaderFlags = ..., + default: Optional[Callable[[Any], Any]] = ..., ): ... + @classmethod + def from_obj( + cls, + obj: Any, + *, + default: Optional[Callable[[Any], Any]] = None, + ) -> "Document": ... + @classmethod + def from_json( + cls, + content: Union[str, bytes, Path], + *, + flags: ReaderFlags = ..., + ) -> "Document": ... def __len__(self) -> int: ... def get_pointer(self, pointer: str) -> Any: ... def dumps( self, - flags: Optional[WriterFlags] = ..., - at_pointer: Optional[str] = ..., + *, + flags: WriterFlags = ..., + at_pointer: str = ..., ) -> str: ... def patch( self, - patch: "Document", + patch: Union["Document", Content], *, at_pointer: Optional[str] = None, use_merge_patch: bool = False ) -> "Document": ... @property def is_thawed(self) -> bool: ... - def freeze(self) -> None: ... - def thaw(self) -> None: ... + @property + def bytes_read(self) -> int: ... + def freeze(self) -> "Document": ... + def thaw(self) -> "Document": ... -def load( - fp, - *, - cls=None, - object_hook=None, - parse_float=None, - parse_int=None, - parse_constant=None, - object_pairs_hook=None, - **kw -): ... -def loads( - s, - *, - cls=None, - object_hook=None, - parse_float=None, - parse_int=None, - parse_constant=None, - object_pairs_hook=None, - **kw -): ... -def dumps( - obj, - *, - skipkeys=False, - ensure_ascii=True, - check_circular=True, - allow_nan=True, - cls=None, - indent=None, - separators=None, - default=None, - sort_keys=False, - **kw -): ... +def load(fp: Any) -> Any: ... +def loads(s: Union[str, bytes, bytearray, Path]) -> Any: ... +def dumps(obj: Any, *, default: Optional[Callable[[Any], Any]] = None) -> str: ... def dump( - obj, - fp, + obj: Any, fp: Any, *, default: Optional[Callable[[Any], Any]] = None +) -> None: ... +def sax( + source: Union[bytes, bytearray, memoryview, str, Path, BinaryIO], + handler: object, *, - skipkeys=False, - ensure_ascii=True, - check_circular=True, - allow_nan=True, - cls=None, - indent=None, - separators=None, - default=None, - sort_keys=False, - **kw -): ... + flags: ReaderFlags = ..., + window_size: int = ..., + max_depth: int = ..., +) -> None: ... diff --git a/yyjson/binding.c b/yyjson/binding.c index dd17adc..64335ca 100644 --- a/yyjson/binding.c +++ b/yyjson/binding.c @@ -4,14 +4,20 @@ #include "document.h" #include "memory.h" #include "decimal.h" +#include "pathlib.h" +#include "sax.h" #include "yyjson.h" PyObject *YY_DecimalModule = NULL; PyObject *YY_DecimalClass = NULL; +PyObject *YY_PathlibModule = NULL; +PyObject *YY_PathClass = NULL; + static PyModuleDef yymodule = { PyModuleDef_HEAD_INIT, .m_name = "cyyjson", - .m_doc = "Python bindings for the yyjson project.", .m_size = -1}; + .m_doc = "Python bindings for the yyjson project.", .m_size = -1, + .m_methods = yyjson_sax_methods}; PyMODINIT_FUNC PyInit_cyyjson(void) { PyObject* m; @@ -32,6 +38,13 @@ PyMODINIT_FUNC PyInit_cyyjson(void) { return NULL; } + // Module-level functions defined in document.c (loads, ...). The sax() + // function is registered via the module def's m_methods above. + if (PyModule_AddFunctions(m, yyjson_doc_methods) < 0) { + Py_DECREF(m); + return NULL; + } + // We need to pre-import the Decimal module to have it available globally. YY_DecimalModule = PyImport_ImportModule("decimal"); if (YY_DecimalModule == NULL) { @@ -45,5 +58,18 @@ PyMODINIT_FUNC PyInit_cyyjson(void) { } Py_INCREF(YY_DecimalClass); + // Same for pathlib.Path, accepted by loads()/Document() as a file path. + YY_PathlibModule = PyImport_ImportModule("pathlib"); + if (YY_PathlibModule == NULL) { + return NULL; + } + Py_INCREF(YY_PathlibModule); + + YY_PathClass = PyObject_GetAttrString(YY_PathlibModule, "Path"); + if (YY_PathClass == NULL) { + return NULL; + } + Py_INCREF(YY_PathClass); + return m; } diff --git a/yyjson/document.c b/yyjson/document.c index 69c6c5f..7d1765e 100644 --- a/yyjson/document.c +++ b/yyjson/document.c @@ -2,37 +2,32 @@ #include "memory.h" #include "decimal.h" +#include "pathlib.h" -#define ENSURE_MUTABLE(self) \ - if (self->i_doc) { \ - self->m_doc = yyjson_doc_mut_copy(self->i_doc, self->alc); \ - yyjson_doc_free(self->i_doc); \ - self->i_doc = NULL; \ - } - -static PyObject *mut_element_to_primitive(yyjson_mut_val *val); -static PyObject *element_to_primitive(yyjson_val *val); - -static PyObject *pathlib = NULL; -static PyObject *path = NULL; +static PyObject *mut_element_to_primitive(yyjson_mut_val *val, int depth); +static PyObject *element_to_primitive(yyjson_val *val, int depth); /** - * Count the number of UTF-8 characters in the given string. + * Return non-zero if the buffer is pure ASCII. Only the yes/no answer is needed + * (not a character count), so this checks a word at a time. */ -static inline size_t num_utf8_chars(const char *src, size_t len) { - size_t count = 0; - for (size_t i = 0; i < len; i++) { - if (yyjson_likely(src[i] >> 6 != 2)) { - count++; - } +static inline int is_ascii(const char *src, size_t len) { + size_t i = 0; + for (; i + 8 <= len; i += 8) { + uint64_t word; + memcpy(&word, src + i, sizeof(word)); + if (word & 0x8080808080808080ULL) return 0; + } + for (; i < len; i++) { + if ((unsigned char)src[i] & 0x80) return 0; } - return count; + return 1; } /** * Convert the given UTF-8 string into a Python unicode object. */ -static inline PyObject *unicode_from_str(const char *src, size_t len) { +PyObject *unicode_from_str(const char *src, size_t len) { #ifndef PYPY_VERSION // Exploit the internals of CPython's unicode implementation to // implement a fast-path for ASCII data, which is by far the @@ -41,9 +36,7 @@ static inline PyObject *unicode_from_str(const char *src, size_t len) { // // The details of these structures are here: // https://github.com/python/cpython/blob/main/Include/cpython/unicodeobject.h#L53 - size_t num_chars = num_utf8_chars(src, len); - - if (yyjson_likely(num_chars == len)) { + if (yyjson_likely(is_ascii(src, len))) { PyObject *uni = PyUnicode_New(len, 127); if (!uni) return NULL; PyASCIIObject *uni_ascii = (PyASCIIObject *)uni; @@ -56,222 +49,177 @@ static inline PyObject *unicode_from_str(const char *src, size_t len) { } /** - * Recursively convert the given value into an equivalent high-level Python - * object. - **/ -static PyObject *element_to_primitive(yyjson_val *val) { - yyjson_type type = yyjson_get_type(val); - - switch (type) { - case YYJSON_TYPE_NULL: - Py_RETURN_NONE; - case YYJSON_TYPE_BOOL: - if (yyjson_get_subtype(val) == YYJSON_SUBTYPE_TRUE) { - Py_RETURN_TRUE; - } else { - Py_RETURN_FALSE; - } - case YYJSON_TYPE_NUM: { - switch (yyjson_get_subtype(val)) { - case YYJSON_SUBTYPE_UINT: - return PyLong_FromUnsignedLongLong(yyjson_get_uint(val)); - case YYJSON_SUBTYPE_SINT: - return PyLong_FromLongLong(yyjson_get_sint(val)); - case YYJSON_SUBTYPE_REAL: - return PyFloat_FromDouble(yyjson_get_real(val)); - } - } - case YYJSON_TYPE_STR: { - size_t str_len = yyjson_get_len(val); - const char *str = yyjson_get_str(val); - return unicode_from_str(str, str_len); - } - case YYJSON_TYPE_ARR: { - PyObject *arr = PyList_New(yyjson_arr_size(val)); - if (!arr) { - return NULL; - } - - yyjson_val *obj_val; - PyObject *py_val; - - yyjson_arr_iter iter = {0}; - yyjson_arr_iter_init(val, &iter); + * Open a path-like object for binary reading. Cross-platform: on Windows the + * path must go through the wide-char API (fopen() interprets narrow paths in + * the ANSI codepage, breaking non-ASCII names); elsewhere the path is encoded + * with the filesystem encoding (not UTF-8 + str(), which breaks surrogate + * names). Shared with the streaming (SAX) reader. + * + * Returns NULL with an OSError set on failure. + * + * TODO: replace with the public Py_fopen() once Python 3.14 is our floor. + */ +FILE *fopen_path(PyObject *path) { + FILE *fp; +#ifdef MS_WINDOWS + PyObject *str = NULL; + wchar_t *wpath; + if (!PyUnicode_FSDecoder(path, &str)) return NULL; + wpath = PyUnicode_AsWideCharString(str, NULL); + Py_DECREF(str); + if (wpath == NULL) return NULL; + fp = _wfopen(wpath, L"rb"); + if (fp == NULL) PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); + PyMem_Free(wpath); +#else + PyObject *bytes = NULL; + if (!PyUnicode_FSConverter(path, &bytes)) return NULL; + fp = fopen(PyBytes_AS_STRING(bytes), "rb"); + if (fp == NULL) PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); + Py_DECREF(bytes); +#endif + return fp; +} - size_t idx = 0; - while ((obj_val = yyjson_arr_iter_next(&iter))) { - py_val = element_to_primitive(obj_val); - if (!py_val) { - return NULL; - } +/* + * Raise ValueError for a parse failure, locating line/column when the input + * bytes are available (pass NULL otherwise, e.g. file reads that no longer + * hold the data). + */ +static void raise_parse_error(const yyjson_read_err *err, + const char *dat, size_t len) { + size_t line, col, chr; + if (dat != NULL && yyjson_locate_pos(dat, len, err->pos, &line, &col, &chr)) { + PyErr_Format(PyExc_ValueError, "%s at line %zu, column %zu (byte %zu)", + err->msg, line, col, err->pos); + } else { + PyErr_Format(PyExc_ValueError, "%s at byte %zu", err->msg, err->pos); + } +} - PyList_SET_ITEM(arr, idx++, py_val); - } +/* + * Object keys repeat heavily in real JSON (every record in an array of objects + * shares the same key set), and re-decoding and re-hashing an identical + * PyUnicode for each occurrence dominates conversion time. This direct-mapped + * cache returns an existing, hash-cached key on a hit, skipping the decode, the + * allocation and the hash. + * + * CPython only; PyPy falls back to creating a fresh key each time. + */ +#ifndef PYPY_VERSION - return arr; - } - case YYJSON_TYPE_OBJ: { - PyObject *dict = PyDict_New(); - if (!dict) { +#define KEY_CACHE_SIZE 4096u /* power of two */ +#define KEY_CACHE_MAX_LEN 64 /* only short keys (identifiers) are cached */ + +/* A cache slot stores the interned key plus its UTF-8 bytes/length, so a hit is + a length check + memcmp with no Python API call. `utf8` points into `obj`'s + own cached UTF-8 buffer and stays valid until the slot is evicted. */ +typedef struct { + PyObject *obj; + const char *utf8; + uint32_t len; +} key_slot; + +static key_slot key_cache[KEY_CACHE_SIZE]; + +/* Return a hash-cached PyUnicode for the given key bytes. New reference. */ +static inline PyObject *cached_key(const char *str, size_t len) { + PyObject *key; + + /* Empty or unusually long keys don't earn a cache slot, but still get their + hash computed so the dict insert can reuse it. */ + if (len == 0 || len > KEY_CACHE_MAX_LEN) { + key = unicode_from_str(str, len); + if (key != NULL && ((PyASCIIObject *)key)->hash == -1) { + if (PyObject_Hash(key) == -1) { + Py_DECREF(key); return NULL; } - - yyjson_val *obj_key, *obj_val; - PyObject *py_key, *py_val; - const char *str; - size_t str_len; - - yyjson_obj_iter iter = {0}; - yyjson_obj_iter_init(val, &iter); - - while ((obj_key = yyjson_obj_iter_next(&iter))) { - obj_val = yyjson_obj_iter_get_val(obj_key); - - str_len = yyjson_get_len(obj_key); - str = yyjson_get_str(obj_key); - - py_key = unicode_from_str(str, str_len); - py_val = element_to_primitive(obj_val); - - if (!py_key) { - return NULL; - } - - if (!py_val) { - Py_DECREF(py_key); - return NULL; - } - - if (PyDict_SetItem(dict, py_key, py_val) == -1) { - return NULL; - } - - Py_DECREF(py_key); - Py_DECREF(py_val); - } - return dict; } - case YYJSON_TYPE_RAW: { - size_t str_len = yyjson_get_len(val); - const char *str = yyjson_get_raw(val); - PyObject *uni = unicode_from_str(str, str_len); - PyObject *result = PyObject_CallOneArg(YY_DecimalClass, uni); - Py_DECREF(uni); - return result; - } - case YYJSON_TYPE_NONE: - default: - PyErr_SetString(PyExc_TypeError, "Unknown tape type encountered."); - return NULL; + return key; } -} - -/** - * Recursively convert the given value into an equivalent high-level Python - * object. - **/ -static PyObject *mut_element_to_primitive(yyjson_mut_val *val) { - yyjson_type type = yyjson_mut_get_type(val); - - switch (type) { - case YYJSON_TYPE_NULL: - Py_RETURN_NONE; - case YYJSON_TYPE_BOOL: - if (yyjson_mut_get_subtype(val) == YYJSON_SUBTYPE_TRUE) { - Py_RETURN_TRUE; - } else { - Py_RETURN_FALSE; - } - case YYJSON_TYPE_NUM: { - switch (yyjson_mut_get_subtype(val)) { - case YYJSON_SUBTYPE_UINT: - return PyLong_FromUnsignedLongLong(yyjson_mut_get_uint(val)); - case YYJSON_SUBTYPE_SINT: - return PyLong_FromLongLong(yyjson_mut_get_sint(val)); - case YYJSON_SUBTYPE_REAL: - return PyFloat_FromDouble(yyjson_mut_get_real(val)); - } - } - case YYJSON_TYPE_STR: { - size_t str_len = yyjson_mut_get_len(val); - const char *str = yyjson_mut_get_str(val); - return PyUnicode_FromStringAndSize(str, str_len); - } - case YYJSON_TYPE_ARR: { - PyObject *arr = PyList_New(yyjson_mut_arr_size(val)); - if (!arr) { - return NULL; - } + /* FNV-1a over the key bytes selects the slot. */ + uint32_t h = 2166136261u; + for (size_t i = 0; i < len; i++) { + h ^= (unsigned char)str[i]; + h *= 16777619u; + } + uint32_t idx = h & (KEY_CACHE_SIZE - 1); - yyjson_mut_val *obj_val; - PyObject *py_val; + key_slot *slot = &key_cache[idx]; + if (slot->obj != NULL && slot->len == (uint32_t)len && + memcmp(slot->utf8, str, len) == 0) { + Py_INCREF(slot->obj); + return slot->obj; + } - yyjson_mut_arr_iter iter = {0}; - yyjson_mut_arr_iter_init(val, &iter); + key = unicode_from_str(str, len); + if (key == NULL) return NULL; + /* Compute and store the hash now, so the dict insert (and every future hit) + skips siphash. */ + if (PyObject_Hash(key) == -1) { + Py_DECREF(key); + return NULL; + } - size_t idx = 0; - while ((obj_val = yyjson_mut_arr_iter_next(&iter))) { - py_val = mut_element_to_primitive(obj_val); - if (!py_val) { - return NULL; - } + /* Cache the object's UTF-8 view for future comparisons (free for ASCII). */ + { + Py_ssize_t clen; + const char *cbytes = PyUnicode_AsUTF8AndSize(key, &clen); + if (cbytes == NULL) { + Py_DECREF(key); + return NULL; + } + Py_XSETREF(slot->obj, key); /* evict previous occupant */ + slot->utf8 = cbytes; + slot->len = (uint32_t)clen; + } - PyList_SET_ITEM(arr, idx++, py_val); - } + Py_INCREF(key); /* one ref for the cache slot (above), one for the caller */ + return key; +} - return arr; - } - case YYJSON_TYPE_OBJ: { - PyObject *dict = PyDict_New(); - if (!dict) { - return NULL; - } +/* Presize the dict to avoid resizes while filling. `cached_key` has already + computed and stored each key's hash in the object, so a plain PyDict_SetItem + reuses it (no siphash) -- we don't need the (3.13-removed) known-hash API. */ +#define NEW_DICT(n) _PyDict_NewPresized((Py_ssize_t)(n)) +#define DICT_SET_KEYVAL(d, k, v) PyDict_SetItem((d), (k), (v)) - yyjson_mut_val *obj_key, *obj_val; - PyObject *py_key, *py_val; +#else /* PYPY_VERSION */ - yyjson_mut_obj_iter iter = {0}; - yyjson_mut_obj_iter_init(val, &iter); +#define cached_key(str, len) unicode_from_str((str), (len)) +#define NEW_DICT(n) PyDict_New() +#define DICT_SET_KEYVAL(d, k, v) PyDict_SetItem((d), (k), (v)) - while ((obj_key = yyjson_mut_obj_iter_next(&iter))) { - obj_val = yyjson_mut_obj_iter_get_val(obj_key); +#endif /* PYPY_VERSION */ - py_key = mut_element_to_primitive(obj_key); - py_val = mut_element_to_primitive(obj_val); +/* Bound on conversion nesting depth. The recursion is a leaf C function with a + small frame, so this is safe on any reasonable stack while still covering any + realistic document. It also has to cover the freeze() path, where the value + graph comes from an in-memory build rather than the parser, so a limit on the + parser alone would not suffice. */ +#define PY_YYJSON_MAX_DEPTH 1024 - if (!py_key) { - return NULL; - } +/* The converter body lives in element_to_primitive.h and is instantiated for + both of yyjson's mirrored value APIs; see that file for the pattern. */ - if (!py_val) { - Py_DECREF(py_key); - return NULL; - } +#define CONVERT_FN element_to_primitive +#define CONVERT_VAL yyjson_val +#define CONVERT_API(n) yyjson_##n +#include "element_to_primitive.h" +#undef CONVERT_FN +#undef CONVERT_VAL +#undef CONVERT_API - if (PyDict_SetItem(dict, py_key, py_val) == -1) { - return NULL; - } +#define CONVERT_FN mut_element_to_primitive +#define CONVERT_VAL yyjson_mut_val +#define CONVERT_API(n) yyjson_mut_##n +#include "element_to_primitive.h" +#undef CONVERT_FN +#undef CONVERT_VAL +#undef CONVERT_API - Py_DECREF(py_key); - Py_DECREF(py_val); - } - return dict; - } - case YYJSON_TYPE_RAW: { - size_t str_len = yyjson_mut_get_len(val); - const char *str = yyjson_mut_get_raw(val); - PyObject *uni = unicode_from_str(str, str_len); - PyObject *result = PyObject_CallOneArg(YY_DecimalClass, uni); - Py_DECREF(uni); - return result; - } - case YYJSON_TYPE_NONE: - default: - PyErr_SetString(PyExc_TypeError, "Unknown tape type encountered."); - return NULL; - } -} PyTypeObject *type_for_conversion(PyObject *obj) { if (obj->ob_type == &PyUnicode_Type) { @@ -284,6 +232,8 @@ PyTypeObject *type_for_conversion(PyObject *obj) { return &PyDict_Type; } else if (obj->ob_type == &PyList_Type) { return &PyList_Type; + } else if (obj->ob_type == &PyTuple_Type) { + return &PyTuple_Type; } else if (obj->ob_type == &PyBool_Type) { return &PyBool_Type; } else if (obj->ob_type == Py_None->ob_type) { @@ -307,7 +257,12 @@ static inline yyjson_mut_val *mut_primitive_to_element( if (result == NULL) { return NULL; } + if (Py_EnterRecursiveCall(" while converting a Python object to JSON")) { + Py_DECREF(result); + return NULL; + } yyjson_mut_val *val = mut_primitive_to_element(self, doc, result); + Py_LeaveRecursiveCall(); Py_DECREF(result); return val; } @@ -335,43 +290,87 @@ static inline yyjson_mut_val *mut_primitive_to_element( // representation. PyErr_Clear(); // Erase the OverflowError PyObject *str_repr = PyObject_Str(obj); + if (str_repr == NULL) return NULL; Py_ssize_t str_len; const char *str = PyUnicode_AsUTF8AndSize(str_repr, &str_len); - return yyjson_mut_rawncpy(doc, str, str_len); + if (str == NULL) { + Py_DECREF(str_repr); + return NULL; + } + yyjson_mut_val *val = yyjson_mut_rawncpy(doc, str, str_len); + Py_DECREF(str_repr); + return val; } else { return yyjson_mut_uint(doc, unum); } } } else if (ob_type == &PyList_Type) { + if (Py_EnterRecursiveCall(" while converting a Python object to JSON")) { + return NULL; + } yyjson_mut_val *val = yyjson_mut_arr(doc); - yyjson_mut_val *object_value = NULL; + if (yyjson_unlikely(val == NULL)) { + PyErr_NoMemory(); + goto error; + } for (Py_ssize_t i = 0; i < PyList_GET_SIZE(obj); i++) { - object_value = mut_primitive_to_element(self, doc, PyList_GET_ITEM(obj, i)); + yyjson_mut_val *object_value = + mut_primitive_to_element(self, doc, PyList_GET_ITEM(obj, i)); + if (yyjson_unlikely(object_value == NULL)) goto error; - if (yyjson_unlikely(object_value == NULL)) { - return NULL; - } + yyjson_mut_arr_append(val, object_value); + } + Py_LeaveRecursiveCall(); + return val; + } else if (ob_type == &PyTuple_Type) { + if (Py_EnterRecursiveCall(" while converting a Python object to JSON")) { + return NULL; + } + yyjson_mut_val *val = yyjson_mut_arr(doc); + if (yyjson_unlikely(val == NULL)) { + PyErr_NoMemory(); + goto error; + } + for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(obj); i++) { + yyjson_mut_val *object_value = + mut_primitive_to_element(self, doc, PyTuple_GET_ITEM(obj, i)); + if (yyjson_unlikely(object_value == NULL)) goto error; yyjson_mut_arr_append(val, object_value); } + Py_LeaveRecursiveCall(); return val; } else if (ob_type == &PyDict_Type) { + if (Py_EnterRecursiveCall(" while converting a Python object to JSON")) { + return NULL; + } yyjson_mut_val *val = yyjson_mut_obj(doc); - yyjson_mut_val *object_value = NULL; Py_ssize_t i = 0; PyObject *key, *value; + if (yyjson_unlikely(val == NULL)) { + PyErr_NoMemory(); + goto error; + } while (PyDict_Next(obj, &i, &key, &value)) { Py_ssize_t str_len; const char *str = PyUnicode_AsUTF8AndSize(key, &str_len); - object_value = mut_primitive_to_element(self, doc, value); - if (yyjson_unlikely(object_value == NULL)) { - return NULL; + if (yyjson_unlikely(str == NULL)) { + PyErr_SetString(PyExc_TypeError, "Dictionary keys must be strings"); + goto error; } - yyjson_mut_obj_add( - val, yyjson_mut_strncpy(doc, str, str_len), object_value - ); + yyjson_mut_val *object_value = mut_primitive_to_element(self, doc, value); + if (yyjson_unlikely(object_value == NULL)) goto error; + + // a NULL key would make obj_add silently drop the member + yyjson_mut_val *key_val = yyjson_mut_strncpy(doc, str, str_len); + if (yyjson_unlikely(key_val == NULL)) { + PyErr_NoMemory(); + goto error; + } + yyjson_mut_obj_add(val, key_val, object_value); } + Py_LeaveRecursiveCall(); return val; } else if (ob_type == &PyFloat_Type) { double dnum = PyFloat_AsDouble(obj); @@ -385,8 +384,13 @@ static inline yyjson_mut_val *mut_primitive_to_element( return yyjson_mut_null(doc); } else if (yyjson_unlikely(PyObject_IsInstance(obj, YY_DecimalClass))) { PyObject *str_repr = PyObject_Str(obj); + if (yyjson_unlikely(str_repr == NULL)) return NULL; Py_ssize_t str_len; const char *str = PyUnicode_AsUTF8AndSize(str_repr, &str_len); + if (yyjson_unlikely(str == NULL)) { + Py_DECREF(str_repr); + return NULL; + } yyjson_mut_val *val = yyjson_mut_rawncpy(doc, str, str_len); Py_DECREF(str_repr); return val; @@ -397,6 +401,10 @@ static inline yyjson_mut_val *mut_primitive_to_element( ); return NULL; } + +error: + Py_LeaveRecursiveCall(); + return NULL; } static void Document_dealloc(DocumentObject *self) { @@ -421,6 +429,295 @@ static PyObject *Document_new( return (PyObject *)self; } +/** + * Build the given (freshly-allocated) Document from a JSON-serializable Python + * object. Unlike parsing, a ``str``/``bytes`` argument is serialized as a JSON + * value rather than interpreted as JSON text. Returns 0 on success, -1 (with an + * exception set) on failure. + */ +static int document_build_from_object(DocumentObject *self, PyObject *content) { + self->m_doc = yyjson_mut_doc_new(self->alc); + if (!self->m_doc) { + PyErr_SetString( + PyExc_ValueError, "Unable to create empty mutable document." + ); + return -1; + } + + yyjson_mut_val *val = mut_primitive_to_element(self, self->m_doc, content); + if (val == NULL) { + if (!PyErr_Occurred()) PyErr_NoMemory(); + return -1; + } + + yyjson_mut_doc_set_root(self->m_doc, val); + return 0; +} + +/** + * Read a binary file-like object (``read``/``readinto``) fully into a buffer + * and parse it into ``self->i_doc``. The whole stream is drained in chunks, so + * any read()-able object works; note that a DOM inherently holds the entire + * document, so peak memory is O(document) regardless. For bounded memory over a + * huge stream, use the module-level ``sax()`` instead. + * + * Returns 0 on success, -1 (with an exception set) on failure. + */ +static int document_read_stream( + DocumentObject *self, PyObject *fileobj, yyjson_read_flag flag +) { + PyObject *readinto = PyObject_GetAttrString(fileobj, "readinto"); + PyObject *readm = NULL; + char *buf = NULL; + size_t cap = 0, len = 0; + const size_t CHUNK = (size_t)1 << 16; + yyjson_read_err err; + + if (!readinto) { + PyErr_Clear(); + readm = PyObject_GetAttrString(fileobj, "read"); + if (!readm) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, + "expected a binary file-like object with read()"); + return -1; + } + } + + /* If the stream is seekable, learn the remaining size and presize the + buffer, so the whole stream is drained in one readinto() with no growth + reallocs (the seek/tell protocol respects the object's own buffering, + unlike fd-level sizing). A short or stale answer is harmless: the drain + loop below still reads to EOF and grows if needed. Only for readinto + streams: text-mode tell() returns opaque cookies, not byte offsets. */ + if (readinto) { + PyObject *r = PyObject_CallMethod(fileobj, "seekable", NULL); + int seekable = r ? PyObject_IsTrue(r) : 0; + Py_XDECREF(r); + if (seekable > 0) { + PyObject *pos_o = PyObject_CallMethod(fileobj, "tell", NULL); + PyObject *end_o = + pos_o ? PyObject_CallMethod(fileobj, "seek", "ii", 0, 2) : NULL; + if (end_o) { + /* we moved the position; failing to restore it must propagate, or + the drain below would silently read nothing from the tail */ + PyObject *back_o = PyObject_CallMethod(fileobj, "seek", "Oi", pos_o, 0); + if (!back_o) { + Py_DECREF(end_o); + Py_DECREF(pos_o); + Py_XDECREF(readinto); + return -1; + } + Py_DECREF(back_o); + { + Py_ssize_t pos = PyNumber_AsSsize_t(pos_o, NULL); + Py_ssize_t end = PyNumber_AsSsize_t(end_o, NULL); + if (!PyErr_Occurred() && end > pos) { + size_t hint = (size_t)(end - pos) + CHUNK + YYJSON_PADDING_SIZE; + buf = (char *)self->alc->malloc(self->alc->ctx, hint); + if (buf) cap = hint; + } + } + } + Py_XDECREF(end_o); + Py_XDECREF(pos_o); + } + if (PyErr_Occurred()) PyErr_Clear(); + } + + for (;;) { + size_t space; + if (len + CHUNK + YYJSON_PADDING_SIZE > cap) { + size_t ncap = cap ? cap * 2 : CHUNK * 2; + char *nb; + while (len + CHUNK + YYJSON_PADDING_SIZE > ncap) ncap *= 2; + nb = (char *)self->alc->realloc(self->alc->ctx, buf, cap, ncap); + if (!nb) { PyErr_NoMemory(); goto error; } + buf = nb; + cap = ncap; + } + space = cap - len - YYJSON_PADDING_SIZE; + + if (readinto) { + PyObject *mv = PyMemoryView_FromMemory(buf + len, (Py_ssize_t)space, + PyBUF_WRITE); + PyObject *r; + Py_ssize_t got; + if (!mv) goto error; + r = PyObject_CallOneArg(readinto, mv); + Py_DECREF(mv); + if (!r) goto error; + if (r == Py_None) { + /* None means "no data available right now" on a non-blocking + stream, not EOF; treating it as EOF would silently truncate. */ + Py_DECREF(r); + PyErr_SetString(PyExc_BlockingIOError, + "readinto() returned None (no data available on a " + "non-blocking stream); a blocking stream is required"); + goto error; + } + got = PyNumber_AsSsize_t(r, NULL); + Py_DECREF(r); + if (got < 0) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_ValueError, "readinto() returned < 0"); + goto error; + } + if (got == 0) break; + len += (size_t)got; + } else { + PyObject *r = PyObject_CallFunction(readm, "n", (Py_ssize_t)space); + char *data; + Py_ssize_t got; + if (!r) goto error; + if (r == Py_None) { + Py_DECREF(r); + PyErr_SetString(PyExc_BlockingIOError, + "read() returned None (no data available on a " + "non-blocking stream); a blocking stream is required"); + goto error; + } + if (!PyBytes_Check(r)) { + Py_DECREF(r); + PyErr_SetString(PyExc_TypeError, + "read() must return bytes; open in binary mode"); + goto error; + } + if (PyBytes_AsStringAndSize(r, &data, &got) < 0) { Py_DECREF(r); goto error; } + if (got == 0) { Py_DECREF(r); break; } + if ((size_t)got > space) got = (Py_ssize_t)space; + memcpy(buf + len, data, (size_t)got); + Py_DECREF(r); + len += (size_t)got; + } + } + + Py_XDECREF(readinto); + Py_XDECREF(readm); + + if (len == 0) { + if (buf) self->alc->free(self->alc->ctx, buf); + PyErr_SetString(PyExc_ValueError, "no data read from stream"); + return -1; + } + + /* Parse in place and hand `buf` to the document via `str_pool`, so it is + freed by yyjson_doc_free. This skips the full copy a non-insitu read + would make (the same pattern yyjson_read_fp uses). Shrink first: the + buffer lives as long as the document, and growth doubling can leave up + to 2x the input in unused capacity. */ + if (cap > len + YYJSON_PADDING_SIZE) { + char *nb = (char *)self->alc->realloc(self->alc->ctx, buf, cap, + len + YYJSON_PADDING_SIZE); + if (nb) buf = nb; /* shrink failure is harmless; keep the larger buffer */ + } + memset(buf + len, 0, YYJSON_PADDING_SIZE); + self->i_doc = + yyjson_read_opts(buf, len, flag | YYJSON_READ_INSITU, self->alc, &err); + if (!self->i_doc) { + // insitu parsing may have rewritten decoded spans; line/col approximate + raise_parse_error(&err, buf, len); + self->alc->free(self->alc->ctx, buf); + return -1; + } + self->i_doc->str_pool = buf; + return 0; + +error: + Py_XDECREF(readinto); + Py_XDECREF(readm); + if (buf) self->alc->free(self->alc->ctx, buf); + return -1; +} + +/** + * Parse JSON text from `content` into a new immutable document, dispatching on + * its type: ``str``, ``bytes``, ``bytearray``, or a ``pathlib.Path`` (read from + * disk). Shared by the Document constructor and the module-level ``loads``. + * + * On success returns the document. On failure returns NULL and sets `*not_text`: + * 1 `content` was not one of the text types above; no exception is set, and + * the caller decides what to do (stream a file-like, build from a value, + * or raise). + * 0 a read or parse error occurred and a Python exception is already set. + */ +static yyjson_doc *parse_content( + PyObject *content, yyjson_read_flag flag, const yyjson_alc *alc, + int *not_text +) { + yyjson_read_err err; + yyjson_doc *doc; + const char *dat = NULL; + size_t dat_len = 0; + + *not_text = 0; + + if (yyjson_likely(PyBytes_Check(content))) { + // Discarding const is safe as long as we never expose the insitu flag. + dat = PyBytes_AS_STRING(content); + dat_len = (size_t)PyBytes_GET_SIZE(content); + doc = yyjson_read_opts((char *)dat, dat_len, flag, alc, &err); + } else if (yyjson_likely(PyUnicode_Check(content))) { + Py_ssize_t len; + dat = PyUnicode_AsUTF8AndSize(content, &len); + if (dat == NULL) return NULL; + dat_len = (size_t)len; + doc = yyjson_read_opts((char *)dat, dat_len, flag, alc, &err); + } else if (PyByteArray_Check(content)) { + dat = PyByteArray_AS_STRING(content); + dat_len = (size_t)PyByteArray_GET_SIZE(content); + doc = yyjson_read_opts((char *)dat, dat_len, flag, alc, &err); + } else { + int is_path; + FILE *fp; + is_path = PyObject_IsInstance(content, YY_PathClass); + if (is_path < 0) return NULL; + if (!is_path) { + *not_text = 1; + return NULL; + } + fp = fopen_path(content); + if (fp == NULL) return NULL; /* OSError with the filename is set */ + doc = yyjson_read_fp(fp, flag, alc, &err); + fclose(fp); + } + + if (doc == NULL) { + raise_parse_error(&err, dat, dat_len); + } + return doc; +} + +/** + * Parse `content` as JSON text into self->i_doc. `content` may be a ``str``, + * ``bytes``, ``bytearray``, a ``pathlib.Path`` (read from disk), or a binary + * file-like object. Returns: + * 0 parsed successfully, + * -1 an error occurred (a Python exception is set), + * 1 `content` is not a JSON-text type and should be built from instead. + */ +static int document_read_json( + DocumentObject *self, PyObject *content, yyjson_read_flag r_flag +) { + int not_text; + yyjson_doc *doc = parse_content(content, r_flag, self->alc, ¬_text); + + if (doc != NULL) { + self->i_doc = doc; + return 0; + } + if (!not_text) { + return -1; // a read/parse error occurred; a Python exception is set + } + // A binary file-like object is streamed into a DOM; anything else is built + // from as a Python value by the caller. + if (PyObject_HasAttrString(content, "readinto") || + PyObject_HasAttrString(content, "read")) { + return document_read_stream(self, content, r_flag); + } + return 1; +} + PyDoc_STRVAR( Document_init_doc, "A single JSON document.\n" @@ -467,7 +764,6 @@ static int Document_init(DocumentObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = {"content", "flags", "default", NULL}; PyObject *content; PyObject *default_func = NULL; - yyjson_read_err err; yyjson_read_flag r_flag = 0; if (!PyArg_ParseTupleAndKeywords( @@ -481,115 +777,187 @@ static int Document_init(DocumentObject *self, PyObject *args, PyObject *kwds) { return -1; } - self->default_func = default_func == Py_None ? NULL : default_func; - Py_XINCREF(default_func); - - if (yyjson_unlikely(pathlib == NULL)) { - pathlib = PyImport_ImportModule("pathlib"); - if (yyjson_unlikely(pathlib == NULL)) { - return -1; - } - path = PyObject_GetAttrString(pathlib, "Path"); - if (yyjson_unlikely(path == NULL)) { - return -1; - } + if (self->i_doc) { + yyjson_doc_free(self->i_doc); + self->i_doc = NULL; } + if (self->m_doc) { + yyjson_mut_doc_free(self->m_doc); + self->m_doc = NULL; + } + Py_CLEAR(self->default_func); - if (yyjson_likely(PyBytes_Check(content))) { - Py_ssize_t content_len; - const char *content_as_utf8 = NULL; - - PyBytes_AsStringAndSize(content, (char **)&content_as_utf8, &content_len); - - self->i_doc = yyjson_read_opts( - // As long as we don't expose the insitu reader flag, it's safe to - // discard the const here. - (char *)content_as_utf8, content_len, r_flag, self->alc, &err - ); + self->default_func = default_func == Py_None ? NULL : default_func; + Py_XINCREF(self->default_func); - if (!self->i_doc) { - PyErr_SetString(PyExc_ValueError, err.msg); - return -1; - } + // A str/bytes/Path is parsed as JSON; anything else is built from as a + // Python object. + int result = document_read_json(self, content, r_flag); + if (result == 1) { + return document_build_from_object(self, content); + } - return 0; - } else if (yyjson_likely(PyUnicode_Check(content))) { - // We were given a string, so just parse it into a document. - Py_ssize_t content_len; - const char *content_as_utf8 = NULL; + return result; +} - content_as_utf8 = PyUnicode_AsUTF8AndSize(content, &content_len); +PyDoc_STRVAR( + Document_from_obj_doc, + "from_obj(obj, *, default=None)\n" + "\n" + "Build a :class:`Document` from a JSON-serializable Python object.\n" + "\n" + "Unlike the constructor, a ``str`` or ``bytes`` argument is serialized as a\n" + "JSON value rather than parsed as JSON text.\n" + "\n" + ":param obj: The Python object to build the document from.\n" + ":param default: A function called to convert objects that are not\n" + " JSON serializable.\n" + ":type default: callable, optional" +); +static PyObject *Document_from_obj( + PyObject *cls, PyObject *args, PyObject *kwds +) { + static char *kwlist[] = {"obj", "default", NULL}; + PyObject *content = NULL; + PyObject *default_func = NULL; - self->i_doc = yyjson_read_opts( - // As long as we don't expose the insitu reader flag, it's safe to - // discard the const here. - (char *)content_as_utf8, content_len, r_flag, self->alc, &err - ); + if (!PyArg_ParseTupleAndKeywords( + args, kwds, "O|$O", kwlist, &content, &default_func + )) { + return NULL; + } - if (!self->i_doc) { - PyErr_SetString(PyExc_ValueError, err.msg); - return -1; - } + if (default_func && default_func != Py_None && + !PyCallable_Check(default_func)) { + PyErr_SetString(PyExc_TypeError, "default must be callable"); + return NULL; + } - return 0; - } else if (yyjson_unlikely(PyObject_IsInstance(content, path))) { - // We were given a Path object to a location on disk. - PyObject *as_str = PyObject_Str(content); - if (!as_str) { - return -1; - } + PyTypeObject *type = (PyTypeObject *)cls; + DocumentObject *self = (DocumentObject *)type->tp_alloc(type, 0); + if (self == NULL) { + return NULL; + } - Py_ssize_t str_len; - const char *str = PyUnicode_AsUTF8AndSize(as_str, &str_len); - if (!str) { - Py_XDECREF(as_str); - return -1; - } + self->m_doc = NULL; + self->i_doc = NULL; + self->alc = &PyMem_Allocator; + self->default_func = default_func == Py_None ? NULL : default_func; + Py_XINCREF(self->default_func); - self->i_doc = yyjson_read_file(str, r_flag, self->alc, &err); + if (document_build_from_object(self, content) < 0) { + Py_DECREF(self); + return NULL; + } - Py_XDECREF(as_str); - Py_XDECREF(str); + return (PyObject *)self; +} - if (!self->i_doc) { - PyErr_SetString(PyExc_ValueError, err.msg); - return -1; - } +PyDoc_STRVAR( + Document_from_json_doc, + "from_json(content, *, flags=0)\n" + "\n" + "Parse a JSON document from a ``str``, ``bytes``, or a ``pathlib.Path``\n" + "(read from disk).\n" + "\n" + "This is the explicit counterpart to :meth:`from_obj`: the argument is\n" + "always parsed as JSON text, never built from as a Python value.\n" + "\n" + ":param content: The JSON document as ``str``/``bytes``, or a ``Path`` to a\n" + " file to read.\n" + ":param flags: Flags that modify the parsing behaviour.\n" + ":type flags: :class:`ReaderFlags`, optional" +); +static PyObject *Document_from_json( + PyObject *cls, PyObject *args, PyObject *kwds +) { + static char *kwlist[] = {"content", "flags", NULL}; + PyObject *content = NULL; + yyjson_read_flag r_flag = 0; - return 0; - } else { - self->m_doc = yyjson_mut_doc_new(self->alc); + if (!PyArg_ParseTupleAndKeywords( + args, kwds, "O|$I", kwlist, &content, &r_flag + )) { + return NULL; + } - if (!self->m_doc) { - PyErr_SetString( - PyExc_ValueError, "Unable to create empty mutable document." - ); - return -1; - } + PyTypeObject *type = (PyTypeObject *)cls; + DocumentObject *self = (DocumentObject *)type->tp_alloc(type, 0); + if (self == NULL) { + return NULL; + } - yyjson_mut_val *val = mut_primitive_to_element(self, self->m_doc, content); + self->m_doc = NULL; + self->i_doc = NULL; + self->alc = &PyMem_Allocator; + self->default_func = NULL; - if (val == NULL) { - return -1; - } + int result = document_read_json(self, content, r_flag); + if (result == 1) { + PyErr_Format(PyExc_TypeError, + "from_json() expects str, bytes, bytearray, or Path, not '%s'", + Py_TYPE(content)->tp_name + ); + result = -1; + } + if (result < 0) { + Py_DECREF(self); + return NULL; + } - yyjson_mut_doc_set_root(self->m_doc, val); + return (PyObject *)self; +} - return 0; +/** + * Convert a document's root to Python objects. + */ +static PyObject *doc_root_to_obj(DocumentObject *self) { + if (self->i_doc) { + return element_to_primitive(yyjson_doc_get_root(self->i_doc), 0); } + return mut_element_to_primitive(yyjson_mut_doc_get_root(self->m_doc), 0); } /** * Recursively convert the document into Python objects. */ static PyObject *Document_as_obj(DocumentObject *self, void *closure) { - if (self->i_doc) { - return element_to_primitive(yyjson_doc_get_root(self->i_doc)); - } else { - return mut_element_to_primitive(yyjson_mut_doc_get_root(self->m_doc)); + return doc_root_to_obj(self); +} + +PyDoc_STRVAR( + py_loads_doc, + "loads(s)\n" + "\n" + "Parse a JSON document from a ``str``, ``bytes``, ``bytearray``, or a\n" + "``pathlib.Path`` (read from disk) and return the equivalent Python object."); +static PyObject *py_loads(PyObject *module, PyObject *arg) { + int not_text; + yyjson_doc *doc; + PyObject *result; + (void)module; + + doc = parse_content(arg, 0, &PyMem_Allocator, ¬_text); + if (doc == NULL) { + if (not_text) { + PyErr_Format( + PyExc_TypeError, + "loads() argument must be str, bytes, bytearray, or Path, not '%s'", + Py_TYPE(arg)->tp_name); + } + return NULL; } + + result = element_to_primitive(yyjson_doc_get_root(doc), 0); + yyjson_doc_free(doc); + return result; } +PyMethodDef yyjson_doc_methods[] = { + {"loads", (PyCFunction)py_loads, METH_O, py_loads_doc}, + {NULL} /* Sentinel */ +}; + /** * Is the document mutable? */ @@ -597,6 +965,17 @@ static PyObject *Document_is_thawed(DocumentObject *self, void *closure) { return PyBool_FromLong(self->m_doc != NULL); } +/** + * Get the size of data read from the original JSON input. + */ +static PyObject *Document_bytes_read(DocumentObject *self, void *closure) { + if (self->i_doc) { + return PyLong_FromSize_t(yyjson_doc_get_read_size(self->i_doc)); + } else { + return PyLong_FromLong(0); + } +} + PyDoc_STRVAR( Document_dumps_doc, "Dumps the document to a string and returns it.\n" @@ -691,7 +1070,7 @@ static PyObject *Document_dumps( } obj_result = PyUnicode_FromStringAndSize(result, w_len); - self->alc->free(NULL, result); + self->alc->free(self->alc->ctx, result); return obj_result; } @@ -724,7 +1103,7 @@ static PyObject *Document_get_pointer(DocumentObject *self, PyObject *args) { return NULL; } - return element_to_primitive(result); + return element_to_primitive(result, 0); } else { yyjson_mut_val *result = yyjson_mut_doc_ptr_getx(self->m_doc, pointer, pointer_len, NULL, &err); @@ -736,7 +1115,7 @@ static PyObject *Document_get_pointer(DocumentObject *self, PyObject *args) { return NULL; } - return mut_element_to_primitive(result); + return mut_element_to_primitive(result, 0); } } @@ -755,15 +1134,22 @@ PyDoc_STRVAR( " ``Document``, such as :func:`patch()`, it will be automatically " "thawed.\n" " This is an advanced function and can usually be ignored.\n" + "\n" + ":returns: This ``Document``, to allow chaining.\n" ); static PyObject *Document_freeze(DocumentObject *self) { if (self->m_doc) { - self->i_doc = yyjson_mut_doc_imut_copy(self->m_doc, self->alc); + yyjson_doc *copy = yyjson_mut_doc_imut_copy(self->m_doc, self->alc); + if (yyjson_unlikely(copy == NULL)) { + return PyErr_NoMemory(); + } + self->i_doc = copy; yyjson_mut_doc_free(self->m_doc); self->m_doc = NULL; } - Py_RETURN_NONE; + Py_INCREF(self); + return (PyObject *)self; } PyDoc_STRVAR( @@ -777,15 +1163,22 @@ PyDoc_STRVAR( ".. note::\n" "\n" " This is an advanced function and can usually be ignored.\n" + "\n" + ":returns: This ``Document``, to allow chaining.\n" ); static PyObject *Document_thaw(DocumentObject *self) { if (self->i_doc) { - self->m_doc = yyjson_doc_mut_copy(self->i_doc, self->alc); + yyjson_mut_doc *copy = yyjson_doc_mut_copy(self->i_doc, self->alc); + if (yyjson_unlikely(copy == NULL)) { + return PyErr_NoMemory(); + } + self->m_doc = copy; yyjson_doc_free(self->i_doc); self->i_doc = NULL; } - Py_RETURN_NONE; + Py_INCREF(self); + return (PyObject *)self; } PyDoc_STRVAR( @@ -798,10 +1191,6 @@ PyDoc_STRVAR( "``use_merge_patch=True`` to\n" "use JSON Merge-Patch instead.\n" "\n" - ".. note::\n" - "\n" - " This method will automatically thaw a frozen ``Document``.\n" - "\n" ":param patch: The ``Document`` to patch with.\n" ":type patch: ``Document``\n" ":param at_pointer: The (optional) JSON Pointer (RFC 6901) to patch at,\n" @@ -815,21 +1204,6 @@ PyDoc_STRVAR( static PyObject *Document_patch( DocumentObject *self, PyObject *args, PyObject *kwds ) { - // Create a new, essentially empty Document which will serve as the - // container for the patch result. - DocumentObject *obj = (DocumentObject *)PyObject_CallFunction( - (PyObject *)&DocumentType, "(O)", Py_None - ); - Py_INCREF(Py_None); - - if (!obj) { - PyErr_SetString( - PyExc_ValueError, - "Unable to create container Document for results of merge-patch" - ); - return NULL; - } - static char *kwlist[] = {"patch", "at_pointer", "use_merge_patch", NULL}; const char *pointer = NULL; @@ -846,6 +1220,38 @@ static PyObject *Document_patch( return NULL; } + // Accept a Document, or coerce any Document-constructible value (a dict, + // list, JSON str/bytes, or Path) into a temporary one, so callers don't have + // to wrap patches by hand. The mutable and immutable paths below both cast + // `patch` to a DocumentObject and dereference it, so it must be one. + // `patch_owned` is non-NULL only when we created the temporary and must free + // it before returning. + PyObject *patch_owned = NULL; + if (!PyObject_IsInstance(patch, (PyObject *)&DocumentType)) { + patch = PyObject_CallFunction((PyObject *)&DocumentType, "(O)", patch); + if (!patch) { + return NULL; + } + patch_owned = patch; + } + + // Create a new, essentially empty Document which will serve as the + // container for the patch result. + DocumentObject *obj = (DocumentObject *)PyObject_CallFunction( + (PyObject *)&DocumentType, "(O)", Py_None + ); + if (!obj) { + PyErr_SetString( + PyExc_ValueError, + "Unable to create container Document for results of merge-patch" + ); + Py_XDECREF(patch_owned); + return NULL; + } + + DocumentObject *patch_doc = (DocumentObject *)patch; + yyjson_doc *tmp_patch = NULL; + // If a pointer was provided, that's the value we're going to be patching, // otherwise we use the root of the document. if (self->i_doc) { @@ -861,36 +1267,30 @@ static PyObject *Document_patch( PyExc_ValueError, ptr_err.msg ? ptr_err.msg : "Not a valid JSON Pointer" ); - return NULL; + goto error; } } else { original = yyjson_doc_get_root(self->i_doc); if (yyjson_unlikely(!original)) { PyErr_SetString(PyExc_ValueError, "Document has no root."); - return NULL; + goto error; } } - if (!PyObject_IsInstance(patch, (PyObject *)&DocumentType)) { - PyErr_SetString(PyExc_TypeError, "Patch must be a Document."); - return NULL; - } - - DocumentObject *patch_doc = (DocumentObject *)patch; - - // If the patch is a mutable document, we need to freeze it before we can - // use it with with the immutable merge_patch API. - if (patch_doc->m_doc) { - patch_doc->i_doc = - yyjson_mut_doc_imut_copy(patch_doc->m_doc, patch_doc->alc); - yyjson_mut_doc_free(patch_doc->m_doc); - patch_doc->m_doc = NULL; + yyjson_val *patch_val = NULL; + if (patch_doc->i_doc) { + patch_val = yyjson_doc_get_root(patch_doc->i_doc); + } else if (patch_doc->m_doc) { + tmp_patch = yyjson_mut_doc_imut_copy(patch_doc->m_doc, self->alc); + if (!tmp_patch) { + PyErr_NoMemory(); + goto error; + } + patch_val = yyjson_doc_get_root(tmp_patch); } - - yyjson_val *patch_val = yyjson_doc_get_root(patch_doc->i_doc); if (!patch_val) { PyErr_SetString(PyExc_ValueError, "Patch document has no root value."); - return NULL; + goto error; } yyjson_mut_val *patched_val = NULL; @@ -907,17 +1307,16 @@ static PyObject *Document_patch( PyExc_ValueError, patch_err.msg ? patch_err.msg : "Unable to apply patch to document." ); - return NULL; + goto error; } } if (!patched_val) { PyErr_SetString(PyExc_ValueError, "Unable to apply patch to document."); - return NULL; + goto error; } yyjson_mut_doc_set_root(obj->m_doc, patched_val); - return (PyObject *)obj; } else { yyjson_mut_val *original = NULL; @@ -932,23 +1331,32 @@ static PyObject *Document_patch( PyExc_ValueError, ptr_err.msg ? ptr_err.msg : "Not a valid JSON Pointer" ); - return NULL; + goto error; } } else { original = yyjson_mut_doc_get_root(self->m_doc); if (yyjson_unlikely(!original)) { PyErr_SetString(PyExc_ValueError, "Document has no root."); - return NULL; + goto error; } } - DocumentObject *patch_doc = (DocumentObject *)patch; - ENSURE_MUTABLE(patch_doc); - - yyjson_mut_val *patch_val = yyjson_mut_doc_get_root(patch_doc->m_doc); + yyjson_mut_val *patch_val = NULL; + if (patch_doc->m_doc) { + patch_val = yyjson_mut_doc_get_root(patch_doc->m_doc); + } else if (patch_doc->i_doc) { + yyjson_val *iroot = yyjson_doc_get_root(patch_doc->i_doc); + if (iroot) { + patch_val = yyjson_val_mut_copy(obj->m_doc, iroot); + if (!patch_val) { + PyErr_NoMemory(); + goto error; + } + } + } if (!patch_val) { PyErr_SetString(PyExc_ValueError, "Patch document has no root value."); - return NULL; + goto error; } yyjson_mut_val *patched_val; @@ -966,18 +1374,27 @@ static PyObject *Document_patch( PyExc_ValueError, patch_err.msg ? patch_err.msg : "Unable to apply patch to document." ); - return NULL; + goto error; } } if (!patched_val) { PyErr_SetString(PyExc_ValueError, "Unable to apply patch to document."); - return NULL; + goto error; } yyjson_mut_doc_set_root(obj->m_doc, patched_val); - return (PyObject *)obj; } + + Py_XDECREF(patch_owned); + if (tmp_patch) yyjson_doc_free(tmp_patch); + return (PyObject *)obj; + +error: + Py_XDECREF(patch_owned); + if (tmp_patch) yyjson_doc_free(tmp_patch); + Py_DECREF(obj); + return NULL; } static Py_ssize_t Document_length(DocumentObject *self) { @@ -989,6 +1406,10 @@ static Py_ssize_t Document_length(DocumentObject *self) { } static PyMethodDef Document_methods[] = { + {"from_obj", (PyCFunction)(void (*)(void))Document_from_obj, + METH_VARARGS | METH_KEYWORDS | METH_CLASS, Document_from_obj_doc}, + {"from_json", (PyCFunction)(void (*)(void))Document_from_json, + METH_VARARGS | METH_KEYWORDS | METH_CLASS, Document_from_json_doc}, {"patch", (PyCFunction)(void (*)(void))Document_patch, METH_VARARGS | METH_KEYWORDS, Document_patch_doc}, {"dumps", (PyCFunction)(void (*)(void))Document_dumps, @@ -1009,6 +1430,8 @@ static PyGetSetDef Document_members[] = { NULL}, {"is_thawed", (getter)Document_is_thawed, NULL, "Returns whether the Document is thawed/mutable.", NULL}, + {"bytes_read", (getter)Document_bytes_read, NULL, + "Returns the size of data read from the original JSON input.", NULL}, {NULL} /* Sentinel */ }; diff --git a/yyjson/document.h b/yyjson/document.h index c260b26..56e1ed6 100644 --- a/yyjson/document.h +++ b/yyjson/document.h @@ -23,4 +23,20 @@ typedef struct { extern PyTypeObject DocumentType; +/** Module-level functions defined in document.c (e.g. ``loads``). */ +extern PyMethodDef yyjson_doc_methods[]; + +/** + * Convert a UTF-8 string of `len` bytes into a Python ``str``, using a fast + * path for pure-ASCII input. Shared with the streaming (SAX) reader. + */ +PyObject *unicode_from_str(const char *src, size_t len); + +/** + * Open a path-like object for binary reading, handling non-ASCII paths on + * every platform. Returns NULL with an OSError set on failure. Shared with + * the streaming (SAX) reader. + */ +FILE *fopen_path(PyObject *path); + #endif \ No newline at end of file diff --git a/yyjson/element_to_primitive.h b/yyjson/element_to_primitive.h new file mode 100644 index 0000000..4f2a80d --- /dev/null +++ b/yyjson/element_to_primitive.h @@ -0,0 +1,115 @@ +/* + * Template body for the document-to-Python-object converters. yyjson's + * mutable API is a 1:1 mirror of the immutable one (identical foreach macro + * shapes and accessor names, differing only by the `mut_` infix), so the one + * optimized implementation is instantiated for both by #including this file + * twice from document.c. No include guard, on purpose. + * + * The includer must define: + * + * CONVERT_FN the function name to generate + * CONVERT_VAL the value type (yyjson_val / yyjson_mut_val) + * CONVERT_API(n) pastes the API prefix (yyjson_##n / yyjson_mut_##n) + * + * and have cached_key(), unicode_from_str(), NEW_DICT/DICT_SET_KEYVAL, + * PY_YYJSON_MAX_DEPTH, and YY_DecimalClass in scope. + */ + +/** + * Recursively convert the given value into an equivalent high-level Python + * object. `depth` is the current container nesting level. + **/ +static PyObject *CONVERT_FN(CONVERT_VAL *val, int depth) { + yyjson_type type = CONVERT_API(get_type)(val); + + // Containers are the cold path -- the vast majority of values are scalars. + // We walk containers directly (via the foreach macros) rather than through + // the iterator API, which avoids the per-element iterator bookkeeping. + if (yyjson_unlikely(type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ)) { + size_t idx, max; + + if (yyjson_unlikely(depth >= PY_YYJSON_MAX_DEPTH)) { + PyErr_SetString( + PyExc_RecursionError, + "maximum recursion depth exceeded while converting JSON"); + return NULL; + } + depth++; + + if (type == YYJSON_TYPE_ARR) { + CONVERT_VAL *ele; + PyObject *arr = PyList_New(CONVERT_API(arr_size)(val)); + if (!arr) return NULL; + CONVERT_API(arr_foreach)(val, idx, max, ele) { + PyObject *py_val = CONVERT_FN(ele, depth); + if (!py_val) { + Py_DECREF(arr); + return NULL; + } + PyList_SET_ITEM(arr, idx, py_val); + } + return arr; + } else { + CONVERT_VAL *key, *ele; + PyObject *dict = NEW_DICT(CONVERT_API(obj_size)(val)); + if (!dict) return NULL; + CONVERT_API(obj_foreach)(val, idx, max, key, ele) { + PyObject *py_key = + cached_key(CONVERT_API(get_str)(key), CONVERT_API(get_len)(key)); + if (!py_key) { + Py_DECREF(dict); + return NULL; + } + PyObject *py_val = CONVERT_FN(ele, depth); + if (!py_val) { + Py_DECREF(py_key); + Py_DECREF(dict); + return NULL; + } + int rc = DICT_SET_KEYVAL(dict, py_key, py_val); + Py_DECREF(py_key); + Py_DECREF(py_val); + if (rc == -1) { + Py_DECREF(dict); + return NULL; + } + } + return dict; + } + } + + // Scalar hot path. + switch (type) { + case YYJSON_TYPE_STR: + return unicode_from_str(CONVERT_API(get_str)(val), + CONVERT_API(get_len)(val)); + case YYJSON_TYPE_NUM: + switch (CONVERT_API(get_subtype)(val)) { + case YYJSON_SUBTYPE_UINT: + return PyLong_FromUnsignedLongLong(CONVERT_API(get_uint)(val)); + case YYJSON_SUBTYPE_SINT: + return PyLong_FromLongLong(CONVERT_API(get_sint)(val)); + default: // YYJSON_SUBTYPE_REAL + return PyFloat_FromDouble(CONVERT_API(get_real)(val)); + } + case YYJSON_TYPE_BOOL: + if (CONVERT_API(get_subtype)(val) == YYJSON_SUBTYPE_TRUE) { + Py_RETURN_TRUE; + } + Py_RETURN_FALSE; + case YYJSON_TYPE_NULL: + Py_RETURN_NONE; + case YYJSON_TYPE_RAW: { + PyObject *result; + PyObject *uni = unicode_from_str(CONVERT_API(get_raw)(val), + CONVERT_API(get_len)(val)); + if (!uni) return NULL; + result = PyObject_CallOneArg(YY_DecimalClass, uni); + Py_DECREF(uni); + return result; + } + default: // YYJSON_TYPE_NONE + PyErr_SetString(PyExc_TypeError, "Unknown tape type encountered."); + return NULL; + } +} diff --git a/yyjson/pathlib.h b/yyjson/pathlib.h new file mode 100644 index 0000000..7f52722 --- /dev/null +++ b/yyjson/pathlib.h @@ -0,0 +1,9 @@ +#ifndef PY_YYJSON_PATHLIB_H +#define PY_YYJSON_PATHLIB_H + +#include + +extern PyObject *YY_PathlibModule; +extern PyObject *YY_PathClass; + +#endif diff --git a/yyjson/py.typed b/yyjson/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/yyjson/sax.c b/yyjson/sax.c new file mode 100644 index 0000000..1ebcd5a --- /dev/null +++ b/yyjson/sax.c @@ -0,0 +1,430 @@ +#include "sax.h" + +#include "document.h" +#include "memory.h" +#include "decimal.h" +#include "yyjson.h" + +#include + +/*============================================================================== + * Streaming (SAX) reader binding. + * + * Wraps `yyjson_sax_read()` so a Python `handler` object receives a method call + * per JSON token, with peak memory bounded by a sliding window rather than the + * document size. The GIL is held throughout (the source and handler callbacks + * re-enter Python), so no threading juggling is required. + *============================================================================*/ + +/* Bound handler methods (borrowed slots owned by this struct) plus flags used + to propagate a Python exception or a clean early stop out of the C reader. */ +typedef struct { + PyObject *m_obj_begin; + PyObject *m_obj_end; + PyObject *m_arr_begin; + PyObject *m_arr_end; + PyObject *m_key; + PyObject *m_string; + PyObject *m_number; + PyObject *m_boolean; + PyObject *m_null; + bool error; /* a Python exception was raised inside a callback/source */ + bool stopped; /* a handler returned False, requesting a clean early stop */ +} py_sax_ctx; + +/* Fetch an optional, callable handler method. Returns a new reference, or NULL + if the attribute is missing, None, or not callable (event then ignored). */ +static PyObject *get_method(PyObject *handler, const char *name) { + PyObject *m = PyObject_GetAttrString(handler, name); + if (!m) { + PyErr_Clear(); + return NULL; + } + if (m == Py_None || !PyCallable_Check(m)) { + Py_DECREF(m); + return NULL; + } + return m; +} + +static void free_methods(py_sax_ctx *c) { + Py_XDECREF(c->m_obj_begin); + Py_XDECREF(c->m_obj_end); + Py_XDECREF(c->m_arr_begin); + Py_XDECREF(c->m_arr_end); + Py_XDECREF(c->m_key); + Py_XDECREF(c->m_string); + Py_XDECREF(c->m_number); + Py_XDECREF(c->m_boolean); + Py_XDECREF(c->m_null); +} + +/* Consume the result of a handler call: NULL -> exception (abort), Py_False -> + clean stop (abort), anything else -> continue. */ +static bool sax_finish(py_sax_ctx *c, PyObject *result) { + bool cont; + if (!result) { + c->error = true; + return false; + } + cont = (result != Py_False); + Py_DECREF(result); + if (!cont) c->stopped = true; + return cont; +} + +/* Convert a scalar number value to a Python object. RAW (Decimal) must be + checked by type first: YYJSON_SUBTYPE_UINT == 0 collides with RAW's subtype. */ +static PyObject *sax_num_to_py(const yyjson_val *v) { + if (yyjson_get_type(v) == YYJSON_TYPE_RAW) { + PyObject *uni = unicode_from_str(yyjson_get_raw(v), yyjson_get_len(v)); + PyObject *res; + if (!uni) return NULL; + res = PyObject_CallOneArg(YY_DecimalClass, uni); + Py_DECREF(uni); + return res; + } + switch (yyjson_get_subtype(v)) { + case YYJSON_SUBTYPE_UINT: + return PyLong_FromUnsignedLongLong(yyjson_get_uint(v)); + case YYJSON_SUBTYPE_SINT: + return PyLong_FromLongLong(yyjson_get_sint(v)); + default: /* REAL */ + return PyFloat_FromDouble(yyjson_get_real(v)); + } +} + +/*------------------------------------------------------------------------------ + * Handler trampolines (installed only for methods the handler defines). + *----------------------------------------------------------------------------*/ + +static bool tr_obj_begin(void *ctx) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + return sax_finish(c, PyObject_CallNoArgs(c->m_obj_begin)); +} +static bool tr_arr_begin(void *ctx) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + return sax_finish(c, PyObject_CallNoArgs(c->m_arr_begin)); +} +static bool tr_null(void *ctx) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + return sax_finish(c, PyObject_CallNoArgs(c->m_null)); +} +static bool tr_obj_end(void *ctx, size_t n) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + PyObject *a = PyLong_FromSize_t(n), *r; + if (!a) { c->error = true; return false; } + r = PyObject_CallOneArg(c->m_obj_end, a); + Py_DECREF(a); + return sax_finish(c, r); +} +static bool tr_arr_end(void *ctx, size_t n) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + PyObject *a = PyLong_FromSize_t(n), *r; + if (!a) { c->error = true; return false; } + r = PyObject_CallOneArg(c->m_arr_end, a); + Py_DECREF(a); + return sax_finish(c, r); +} +static bool tr_key(void *ctx, const char *s, size_t n) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + PyObject *a = unicode_from_str(s, n), *r; + if (!a) { c->error = true; return false; } + r = PyObject_CallOneArg(c->m_key, a); + Py_DECREF(a); + return sax_finish(c, r); +} +static bool tr_string(void *ctx, const char *s, size_t n) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + PyObject *a = unicode_from_str(s, n), *r; + if (!a) { c->error = true; return false; } + r = PyObject_CallOneArg(c->m_string, a); + Py_DECREF(a); + return sax_finish(c, r); +} +static bool tr_number(void *ctx, const yyjson_val *v) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + PyObject *a = sax_num_to_py(v), *r; + if (!a) { c->error = true; return false; } + r = PyObject_CallOneArg(c->m_number, a); + Py_DECREF(a); + return sax_finish(c, r); +} +static bool tr_boolean(void *ctx, bool v) { + py_sax_ctx *c = (py_sax_ctx *)ctx; + return sax_finish(c, PyObject_CallOneArg(c->m_boolean, + v ? Py_True : Py_False)); +} + +/*------------------------------------------------------------------------------ + * Byte sources. + *----------------------------------------------------------------------------*/ + +/* A source over an already-materialized in-memory buffer (bytes/str). */ +typedef struct { + const char *dat; + size_t len; + size_t pos; +} mem_src; + +static size_t mem_source(void *ctx, void *buf, size_t len) { + mem_src *s = (mem_src *)ctx; + size_t give = s->len - s->pos; + if (give > len) give = len; + memcpy(buf, s->dat + s->pos, give); + s->pos += give; + return give; +} + +/* A source over a Python binary file-like object. Prefers ``readinto`` (fills + the window directly, no intermediate allocation) and falls back to ``read``. */ +typedef struct { + PyObject *readinto; /* borrowed */ + PyObject *read; /* borrowed */ + py_sax_ctx *pc; +} py_src; + +static size_t py_source(void *ctx, void *buf, size_t len) { + py_src *s = (py_src *)ctx; + + if (s->readinto) { + PyObject *mv, *r; + Py_ssize_t n; + mv = PyMemoryView_FromMemory((char *)buf, (Py_ssize_t)len, PyBUF_WRITE); + if (!mv) { s->pc->error = true; return YYJSON_SAX_SOURCE_ERROR; } + r = PyObject_CallOneArg(s->readinto, mv); + Py_DECREF(mv); + if (!r) { s->pc->error = true; return YYJSON_SAX_SOURCE_ERROR; } + if (r == Py_None) { + /* None means "no data available right now" on a non-blocking + stream, not EOF; treating it as EOF would silently truncate. */ + Py_DECREF(r); + PyErr_SetString(PyExc_BlockingIOError, + "readinto() returned None (no data available on " + "a non-blocking stream); a blocking stream is " + "required"); + s->pc->error = true; + return YYJSON_SAX_SOURCE_ERROR; + } + n = PyNumber_AsSsize_t(r, NULL); + Py_DECREF(r); + if (n < 0) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_ValueError, "readinto() returned < 0"); + s->pc->error = true; + return YYJSON_SAX_SOURCE_ERROR; + } + return (size_t)n; + } else { + PyObject *r; + char *data; + Py_ssize_t n; + r = PyObject_CallFunction(s->read, "n", (Py_ssize_t)len); + if (!r) { s->pc->error = true; return YYJSON_SAX_SOURCE_ERROR; } + if (r == Py_None) { + Py_DECREF(r); + PyErr_SetString(PyExc_BlockingIOError, + "read() returned None (no data available on a " + "non-blocking stream); a blocking stream is " + "required"); + s->pc->error = true; + return YYJSON_SAX_SOURCE_ERROR; + } + if (!PyBytes_Check(r)) { + Py_DECREF(r); + PyErr_SetString(PyExc_TypeError, + "read() must return bytes; open the stream in " + "binary mode"); + s->pc->error = true; + return YYJSON_SAX_SOURCE_ERROR; + } + if (PyBytes_AsStringAndSize(r, &data, &n) < 0) { + Py_DECREF(r); + s->pc->error = true; + return YYJSON_SAX_SOURCE_ERROR; + } + if ((size_t)n > len) n = (Py_ssize_t)len; /* defensive */ + memcpy(buf, data, (size_t)n); + Py_DECREF(r); + return (size_t)n; + } +} + +/*------------------------------------------------------------------------------ + * Entry point. + *----------------------------------------------------------------------------*/ + +PyDoc_STRVAR( + py_sax_doc, + "sax(source, handler, *, flags=0, window_size=0, max_depth=0)\n" + "\n" + "Parse JSON from ``source`` using bounded memory, calling methods on\n" + "``handler`` for each token as it is encountered. Peak memory is bounded by\n" + "a sliding window (``window_size``) plus the nesting depth, independent of\n" + "the total input size, so inputs far larger than RAM can be processed.\n" + "\n" + "``source`` may be ``str``, any object exporting a contiguous byte buffer\n" + "(``bytes``, ``bytearray``, ``memoryview``, ``mmap``, ...), a binary\n" + "file-like object (with ``readinto`` or ``read``), or a ``pathlib.Path``\n" + "to open and stream. Buffer sources are read zero-copy and are pinned for\n" + "the duration of the parse: resizing one from a handler callback raises\n" + "``BufferError``. File-like sources must be blocking: a read that returns\n" + "``None`` raises ``BlockingIOError``.\n" + "\n" + "``handler`` is any object; the following methods are called if present\n" + "(each is optional):\n" + "\n" + "* ``obj_begin()`` / ``obj_end(count)``\n" + "* ``arr_begin()`` / ``arr_end(count)``\n" + "* ``key(str)`` -- an object member key\n" + "* ``string(str)`` -- a string value\n" + "* ``number(value)`` -- an ``int``, ``float``, or ``Decimal`` value\n" + "* ``boolean(bool)``\n" + "* ``null()``\n" + "\n" + "A handler method may return ``False`` to stop parsing early (``sax()``\n" + "then returns normally). Raising propagates the exception.\n" + "\n" + "The one limitation is that a single string or number token may not exceed\n" + "``window_size``; such input raises ``ValueError``. Only standard JSON is\n" + "supported; ``NUMBERS_AS_DECIMAL``/``BIGNUM_AS_RAW`` and ``STOP_WHEN_DONE``\n" + "flags are honored, others are ignored.\n" + "\n" + ":param source: The JSON input.\n" + ":param handler: An object receiving a method call per token.\n" + ":param flags: :class:`ReaderFlags` controlling parsing.\n" + ":param window_size: Sliding window size in bytes (0 = default, 256 KiB).\n" + ":param max_depth: Maximum container nesting depth (0 = default)." +); +static PyObject *py_sax(PyObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = {"source", "handler", "flags", + "window_size", "max_depth", NULL}; + PyObject *source = NULL, *handler = NULL; + unsigned int flags = 0; + Py_ssize_t window = 0, max_depth = 0; + py_sax_ctx pc; + yyjson_sax_handler h; + yyjson_sax_opts opts; + yyjson_read_err err; + bool ok = false; + int handled = 0; /* 0 = source type not yet consumed */ + + (void)self; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|$Inn", kwlist, &source, + &handler, &flags, &window, &max_depth)) { + return NULL; + } + + memset(&pc, 0, sizeof(pc)); + pc.m_obj_begin = get_method(handler, "obj_begin"); + pc.m_obj_end = get_method(handler, "obj_end"); + pc.m_arr_begin = get_method(handler, "arr_begin"); + pc.m_arr_end = get_method(handler, "arr_end"); + pc.m_key = get_method(handler, "key"); + pc.m_string = get_method(handler, "string"); + pc.m_number = get_method(handler, "number"); + pc.m_boolean = get_method(handler, "boolean"); + pc.m_null = get_method(handler, "null"); + + memset(&h, 0, sizeof(h)); + if (pc.m_obj_begin) h.obj_begin = tr_obj_begin; + if (pc.m_obj_end) h.obj_end = tr_obj_end; + if (pc.m_arr_begin) h.arr_begin = tr_arr_begin; + if (pc.m_arr_end) h.arr_end = tr_arr_end; + if (pc.m_key) h.key = tr_key; + if (pc.m_string) h.str = tr_string; + if (pc.m_number) h.num = tr_number; + if (pc.m_boolean) h.bool_val = tr_boolean; + if (pc.m_null) h.null_val = tr_null; + + memset(&opts, 0, sizeof(opts)); + if (window > 0) opts.window = (size_t)window; + if (max_depth > 0) opts.max_depth = (size_t)max_depth; + memset(&err, 0, sizeof(err)); + + /* in-memory: str */ + if (PyUnicode_Check(source)) { + mem_src ms; + Py_ssize_t len = 0; + ms.pos = 0; + ms.dat = PyUnicode_AsUTF8AndSize(source, &len); + if (!ms.dat) goto cleanup; + ms.len = (size_t)len; + ok = yyjson_sax_read(mem_source, &ms, &h, &pc, flags, &opts, + &PyMem_Allocator, &err); + handled = 1; + } + + /* in-memory: anything exporting a contiguous byte buffer (bytes, + bytearray, memoryview, mmap, ...). Holding the buffer export for the + whole parse pins the memory: a handler callback that tries to resize + the source (e.g. a bytearray) gets a BufferError from Python instead + of leaving our pointer dangling. */ + if (!handled && PyObject_CheckBuffer(source)) { + Py_buffer view; + mem_src ms; + if (PyObject_GetBuffer(source, &view, PyBUF_SIMPLE) < 0) goto cleanup; + ms.dat = (const char *)view.buf; + ms.len = (size_t)view.len; + ms.pos = 0; + ok = yyjson_sax_read(mem_source, &ms, &h, &pc, flags, &opts, + &PyMem_Allocator, &err); + PyBuffer_Release(&view); + handled = 1; + } + + /* pathlib.Path (or any os.PathLike, but not str/bytes handled above) */ + if (!handled && PyObject_HasAttrString(source, "__fspath__")) { + FILE *fp = fopen_path(source); + if (!fp) goto cleanup; + ok = yyjson_sax_read_fp(fp, &h, &pc, flags, &opts, + &PyMem_Allocator, &err); + fclose(fp); + handled = 1; + } + + /* binary file-like object */ + if (!handled) { + py_src ps; + ps.pc = &pc; + ps.readinto = get_method(source, "readinto"); + ps.read = ps.readinto ? NULL : get_method(source, "read"); + if (!ps.readinto && !ps.read) { + PyErr_Format(PyExc_TypeError, + "sax() source must be str, a bytes-like object, a " + "binary file-like object, or a Path, not '%s'", + Py_TYPE(source)->tp_name); + goto cleanup; + } + ok = yyjson_sax_read(py_source, &ps, &h, &pc, flags, &opts, + &PyMem_Allocator, &err); + Py_XDECREF(ps.readinto); + Py_XDECREF(ps.read); + handled = 1; + } + +cleanup: + free_methods(&pc); + + if (pc.error) { + /* a Python exception is already set by the callback/source */ + return NULL; + } + if (!handled) { + /* an exception was set above before dispatching */ + return NULL; + } + if (!ok && err.code != YYJSON_READ_ERROR_ABORTED) { + PyErr_Format(PyExc_ValueError, "%s (at byte %zu)", + err.msg ? err.msg : "SAX parse error", err.pos); + return NULL; + } + Py_RETURN_NONE; +} + +PyMethodDef yyjson_sax_methods[] = { + {"sax", (PyCFunction)(void (*)(void))py_sax, + METH_VARARGS | METH_KEYWORDS, py_sax_doc}, + {NULL} /* Sentinel */ +}; diff --git a/yyjson/sax.h b/yyjson/sax.h new file mode 100644 index 0000000..ccd89ff --- /dev/null +++ b/yyjson/sax.h @@ -0,0 +1,13 @@ +#ifndef PY_YYJSON_SAX_H +#define PY_YYJSON_SAX_H + +#define PY_SSIZE_T_CLEAN +#include + +/** + * Module-level method table exporting ``sax(...)``. Assigned to the module's + * ``m_methods`` in binding.c. See sax.c for the implementation and docstring. + */ +extern PyMethodDef yyjson_sax_methods[]; + +#endif diff --git a/yyjson/yyjson.c b/yyjson/yyjson.c index 3a99d5c..c3575d9 100644 --- a/yyjson/yyjson.c +++ b/yyjson/yyjson.c @@ -1,16 +1,16 @@ /*============================================================================== Copyright (c) 2020 YaoYuan - + 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 @@ -21,12 +21,11 @@ *============================================================================*/ #include "yyjson.h" -#include /* for `HUGE_VAL/INFINIY/NAN` macros, no libm required */ /*============================================================================== - * Warning Suppress + * MARK: - Warning Suppress (Private) *============================================================================*/ #if defined(__clang__) @@ -35,7 +34,7 @@ # pragma clang diagnostic ignored "-Wunused-label" # pragma clang diagnostic ignored "-Wunused-macros" # pragma clang diagnostic ignored "-Wunused-variable" -#elif defined(__GNUC__) +#elif YYJSON_IS_REAL_GCC && yyjson_gcc_available(4, 2, 0) # pragma GCC diagnostic ignored "-Wunused-function" # pragma GCC diagnostic ignored "-Wunused-parameter" # pragma GCC diagnostic ignored "-Wunused-label" @@ -46,13 +45,14 @@ # pragma warning(disable:4101) /* unreferenced variable */ # pragma warning(disable:4102) /* unreferenced label */ # pragma warning(disable:4127) /* conditional expression is constant */ +# pragma warning(disable:4702) /* unreachable code */ # pragma warning(disable:4706) /* assignment within conditional expression */ #endif /*============================================================================== - * Version + * MARK: - Version (Public) *============================================================================*/ uint32_t yyjson_version(void) { @@ -62,7 +62,7 @@ uint32_t yyjson_version(void) { /*============================================================================== - * Flags + * MARK: - Flags (Private) *============================================================================*/ /* msvc intrinsic */ @@ -109,59 +109,82 @@ uint32_t yyjson_version(void) { #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 +#ifndef YYJSON_HAS_INT128 +# if defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16) && \ + (defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)) && \ + (!defined(__EMSCRIPTEN__) && !defined(__wasm__)) +# define YYJSON_HAS_INT128 1 +# else +# define YYJSON_HAS_INT128 0 +# endif #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 && \ +#ifndef YYJSON_HAS_IEEE_754 +# 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 +# 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 +#endif + +#if YYJSON_DISABLE_FAST_FP_CONV && YYJSON_FREESTANDING +# error DISABLE_FAST_FP_CONV and FREESTANDING cannot be used together +#endif + +/* Inf and NaN */ +#ifndef INFINITY +# ifndef HUGE_VAL +# define INFINITY ((double)(1.0 / 0.0)) +# else +# define INFINITY HUGE_VAL +# endif +#endif +#ifndef NAN +# define NAN ((double)(0.0 / 0.0)) #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 + floating-point arithmetic. The x87 FPU loads all floating-point numbers as + 80-bit double-extended precision internally, then rounds the result to the + 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. + not an accurate detection; it just tries 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__) @@ -185,7 +208,14 @@ uint32_t yyjson_version(void) { # define YYJSON_DOUBLE_MATH_CORRECT 1 #endif -/* endian */ +/* + 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() # include /* POSIX */ #endif @@ -197,23 +227,18 @@ uint32_t yyjson_version(void) { # include /* BSD, Darwin */ #endif -#define YYJSON_BIG_ENDIAN 4321 -#define YYJSON_LITTLE_ENDIAN 1234 - #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__) @@ -222,7 +247,6 @@ uint32_t yyjson_version(void) { (__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__) || \ @@ -236,34 +260,33 @@ uint32_t yyjson_version(void) { 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 takes advantage of - the compiler's automatic optimizations to generate unaligned memory access - instructions when the target architecture supports it. - - However, for some older compilers or architectures where `memcpy()` isn't - optimized well and may generate unnecessary function calls, consider defining - this macro as 1. In such cases, yyjson switches to manual byte-by-byte access, - potentially improving performance. An example of the generated assembly code on - the ARM platform can be found here: https://godbolt.org/z/334jjhxPT - - As this flag has already been enabled for some common architectures in the - following code, users typically don't need to manually specify it. If users are - unsure about it, please review the generated assembly code or perform actual - benchmark to make an informed decision. + + 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__) || \ @@ -287,15 +310,15 @@ uint32_t yyjson_version(void) { /* 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. @@ -315,34 +338,17 @@ uint32_t yyjson_version(void) { #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_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 - +#ifndef YYJSON_READER_DEPTH_LIMIT +#define YYJSON_READER_DEPTH_LIMIT 0 +#endif /*============================================================================== - * Macros + * MARK: - Macros (Private) *============================================================================*/ -/* Macros used for loop unrolling and other purpose. */ +/* Macros used for loop unrolling and other purposes. */ #define repeat2(x) { x x } -#define repeat3(x) { 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 } @@ -352,7 +358,6 @@ uint32_t yyjson_version(void) { #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) } @@ -382,38 +387,61 @@ uint32_t yyjson_version(void) { #define U32(hi) ((u32)(hi##UL)) /* Used to cast away (remove) const qualifier. */ -#define constcast(type) (type)(void *)(size_t)(const void *) +#define constcast yyjson_constcast -/* flag test */ -#define has_read_flag(_flag) unlikely(read_flag_eq(flg, YYJSON_READ_##_flag)) -#define has_write_flag(_flag) unlikely(write_flag_eq(flg, YYJSON_WRITE_##_flag)) +/* + Compiler barriers for single variables. -static_inline bool read_flag_eq(yyjson_read_flag flg, yyjson_read_flag chk) { -#if YYJSON_DISABLE_NON_STANDARD - if (chk == YYJSON_READ_ALLOW_INF_AND_NAN || - chk == YYJSON_READ_ALLOW_COMMENTS || - chk == YYJSON_READ_ALLOW_TRAILING_COMMAS || - chk == YYJSON_READ_ALLOW_INVALID_UNICODE) - return false; /* this should be evaluated at compile-time */ -#endif - return (flg & chk) != 0; -} + 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. -static_inline bool write_flag_eq(yyjson_write_flag flg, yyjson_write_flag chk) { -#if YYJSON_DISABLE_NON_STANDARD - if (chk == YYJSON_WRITE_ALLOW_INF_AND_NAN || - chk == YYJSON_WRITE_ALLOW_INVALID_UNICODE) - return false; /* this should be evaluated at compile-time */ + 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 - return (flg & chk) != 0; -} /*============================================================================== - * Integer Constants + * 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" +#define MSG_DEPTH "depth limit exceeded" + /* U64 constant values */ #undef U64_MAX #define U64_MAX U64(0xFFFFFFFF, 0xFFFFFFFF) @@ -430,23 +458,17 @@ static_inline bool write_flag_eq(yyjson_write_flag flg, yyjson_write_flag chk) { #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) - -/*============================================================================== - * IEEE-754 Double Number Constants - *============================================================================*/ - -/* Inf raw value (positive) */ -#define F64_RAW_INF U64(0x7FF00000, 0x00000000) - -/* NaN raw value (quiet NaN, no payload, no sign) */ +/* NaN bits (quiet NaN, no payload, no sign) */ #if defined(__hppa__) || (defined(__mips__) && !defined(__mips_nan2008)) -#define F64_RAW_NAN U64(0x7FF7FFFF, 0xFFFFFFFF) +#define F64_BITS_NAN U64(0x7FF7FFFF, 0xFFFFFFFF) #else -#define F64_RAW_NAN U64(0x7FF80000, 0x00000000) +#define F64_BITS_NAN U64(0x7FF80000, 0x00000000) #endif -/* max significant digits count in decimal when reading double number */ +/* maximum significant digits count in decimal when reading double number */ #define F64_MAX_DEC_DIG 768 /* maximum decimal power of double number (1.7976931348623157e308) */ @@ -496,13 +518,16 @@ static_inline bool write_flag_eq(yyjson_write_flag flg, yyjson_write_flag chk) { /* 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 + /*============================================================================== - * Types + * MARK: - Types (Private) *============================================================================*/ -/** Type define for primitive types. */ +/** Type aliases for primitive types. */ typedef float f32; typedef double f64; typedef int8_t i8; @@ -534,123 +559,91 @@ typedef union v64_uni { v64 v; u64 u; } v64_uni; /*============================================================================== - * Load/Store Utils + * MARK: - Load/Store Utils (Private) *============================================================================*/ -#if YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS - #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) { - repeat2_incr(byte_move_idx) -} - -static_inline void byte_copy_4(void *dst, const void *src) { - repeat4_incr(byte_move_idx) -} - -static_inline void byte_copy_8(void *dst, const void *src) { - repeat8_incr(byte_move_idx) -} - -static_inline void byte_copy_16(void *dst, const void *src) { - repeat16_incr(byte_move_idx) -} - -static_inline void byte_move_2(void *dst, const void *src) { - repeat2_incr(byte_move_idx) -} - -static_inline void byte_move_4(void *dst, const void *src) { - repeat4_incr(byte_move_idx) -} - -static_inline void byte_move_8(void *dst, const void *src) { - repeat8_incr(byte_move_idx) -} - -static_inline void byte_move_16(void *dst, const void *src) { - repeat16_incr(byte_move_idx) -} - -static_inline bool byte_match_2(void *buf, const char *pat) { - return - ((char *)buf)[0] == ((const char *)pat)[0] && - ((char *)buf)[1] == ((const char *)pat)[1]; -} - -static_inline bool byte_match_4(void *buf, const char *pat) { - 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]; -} - -static_inline u16 byte_load_2(const void *src) { - v16_uni uni; - uni.v.c[0] = ((const char *)src)[0]; - uni.v.c[1] = ((const char *)src)[1]; - return uni.u; -} - -static_inline u32 byte_load_3(const void *src) { - v32_uni uni; - 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; - return uni.u; -} - -static_inline u32 byte_load_4(const void *src) { - v32_uni uni; - 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]; - return uni.u; -} - -#undef byte_move_expr - -#else - -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; @@ -658,2199 +651,585 @@ static_inline void byte_move_16(void *dst, const void *src) { 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; } -#endif - /*============================================================================== - * Number Utils - * These functions are used to detect and convert NaN and Inf numbers. + * MARK: - Character Utils (Private) + * These lookup tables were generated by `misc/make_tables.c`. *============================================================================*/ -/** Convert raw binary to double. */ -static_inline f64 f64_from_raw(u64 u) { - /* use memcpy to avoid violating the strict aliasing rule */ - f64 f; - memcpy(&f, &u, sizeof(u)); - return f; +/* 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); } -/** Convert raw binary to float. */ -static_inline f32 f32_from_raw(u32 u) { - /* use memcpy to avoid violating the strict aliasing rule */ - f32 f; - memcpy(&f, &u, sizeof(u)); - return f; +/** 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); } -/** Convert double to raw binary. */ -static_inline u64 f64_to_raw(f64 f) { - /* use memcpy to avoid violating the strict aliasing rule */ - u64 u; - memcpy(&u, &f, sizeof(u)); - return u; +/** Match a JSON number: [.-+0-9]. */ +static_inline bool char_is_num(u8 c) { + return !!(char_table1[c] & CHAR_TYPE_NUM); } -/** Convert double to raw binary. */ -static_inline u32 f32_to_raw(f32 f) { - /* use memcpy to avoid violating the strict aliasing rule */ - u32 u; - memcpy(&u, &f, sizeof(u)); - return u; +/** 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); } -/** Get raw 'infinity' with sign. */ -static_inline u64 f64_raw_get_inf(bool sign) { -#if YYJSON_HAS_IEEE_754 - return F64_RAW_INF | ((u64)sign << 63); -#elif defined(INFINITY) - return f64_to_raw(sign ? -INFINITY : INFINITY); -#else - return f64_to_raw(sign ? -HUGE_VAL : HUGE_VAL); -#endif +/** 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); } -/** Get raw 'nan' with sign. */ -static_inline u64 f64_raw_get_nan(bool sign) { -#if YYJSON_HAS_IEEE_754 - return F64_RAW_NAN | ((u64)sign << 63); -#elif defined(NAN) - return f64_to_raw(sign ? (f64)-NAN : (f64)NAN); -#else - return f64_to_raw((sign ? -0.0 : 0.0) / 0.0); -#endif +/** 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)); } -/** 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; +/** 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); +} -/*============================================================================== - * Size Utils - * 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); +/** 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); } -/** 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; +/** 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); } -/** 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; - } +/** Match a sign: [+-] */ +static_inline bool char_is_sign(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_SIGN); } -/** 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); - } +/** Match a non-zero digit: [1-9] */ +static_inline bool char_is_nonzero(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_NONZERO); } -/** 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; +/** Match a digit: [0-9] */ +static_inline bool char_is_digit(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_DIGIT); } +/** Match an exponent character: [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)); +} -/*============================================================================== - * Bits Utils - * These functions are used by the floating-point number reader and writer. - *============================================================================*/ +/** 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)); +} -/** 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 sequences - 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 +/** Match a JSON container: `{` or `[`. */ +static_inline bool char_is_ctn(u8 c) { + return (c & 0xDF) == 0x5B; /* '[': 0x5B, '{': 0x7B */ } -/** 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 sequences - 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 +/** 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"); +} -/*============================================================================== - * 128-bit Integer Utils - * These functions are used by the floating-point number reader and writer. - *============================================================================*/ +/** 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"); +} -/** 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 +/** 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; } -/** 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 +/** 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; } /*============================================================================== - * File Utils - * These functions are used to read and write JSON files. + * MARK: - Hex Character Reader (Private) + * This function is used by JSON reader to read escaped characters. *============================================================================*/ -#define YYJSON_FOPEN_EXT -#if !defined(_MSC_VER) && defined(__GLIBC__) && defined(__GLIBC_PREREQ) -# if __GLIBC_PREREQ(2, 7) -# undef YYJSON_FOPEN_EXT -# define YYJSON_FOPEN_EXT "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 -} +/** + This table is used to convert a 4-hex-character sequence to a number. + A valid hex character [0-9A-Fa-f] is mapped to its raw value [0x00, 0x0F]; + an invalid hex character is mapped to [0xF0]. + (generated 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 +}; -static_inline FILE *fopen_readonly(const char *path) { - return fopen_safe(path, "rb" YYJSON_FOPEN_EXT); +/** 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; } -static_inline FILE *fopen_writeonly(const char *path) { - return fopen_safe(path, "wb" YYJSON_FOPEN_EXT); +/** 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; } -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 +/** Match a hexadecimal numeric character: [0-9a-fA-F]. */ +static_inline bool char_is_hex(u8 c) { + return hex_conv_table[c] != 0xF0; } /*============================================================================== - * Default Memory Allocator - * This is a simple libc memory allocator wrapper. + * 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. *============================================================================*/ -static void *default_malloc(void *ctx, usize size) { - return malloc(size); -} +/* 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 -static void *default_realloc(void *ctx, void *ptr, usize old_size, usize size) { - return realloc(ptr, size); -} +/* + 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)) ) -static void default_free(void *ctx, void *ptr) { - free(ptr); -} +/* + 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))) ) -static const yyjson_alc YYJSON_DEFAULT_ALC = { - default_malloc, - default_realloc, - default_free, - NULL -}; +/* + 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) ) /*============================================================================== - * Null Memory Allocator - * - * This allocator is just a placeholder to ensure that the internal - * malloc/realloc/free function pointers are not null. + * MARK: - Power10 Lookup Table (Private) + * These data are used by the floating-point number reader and writer. *============================================================================*/ -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; -} +#if !YYJSON_DISABLE_FAST_FP_CONV -static void null_free(void *ctx, void *ptr) { - return; -} +/** Maximum pow10 exponent that can be represented exactly as a float64. */ +#define F64_POW10_MAX_EXACT_EXP 22 -static const yyjson_alc YYJSON_NULL_ALC = { - null_malloc, - null_realloc, - null_free, - NULL +#if YYJSON_DOUBLE_MATH_CORRECT +/** 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 }; +#endif +/** Maximum pow10 exponent that can be represented exactly as a uint64. */ +#define U64_POW10_MAX_EXACT_EXP 19 +/** Table: [ 10^0, ..., 10^19 ] (generated 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) +}; -/*============================================================================== - * Pool Memory Allocator - * - * 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; -} - - - -/*============================================================================== - * Dynamic Memory Allocator - * - * 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; - } -} +/** Minimum decimal exponent in pow10_sig_table. */ +#define POW10_SIG_TABLE_MIN_EXP -343 -/** 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; -} +/** Maximum decimal exponent in pow10_sig_table. */ +#define POW10_SIG_TABLE_MAX_EXP 324 -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, *next; - 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; - } -} +/** Minimum exact decimal exponent in pow10_sig_table */ +#define POW10_SIG_TABLE_MIN_EXACT_EXP 0 -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 *prev, *next, *new_chunk; - dyn_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; -} +/** Maximum exact decimal exponent in pow10_sig_table */ +#define POW10_SIG_TABLE_MAX_EXACT_EXP 55 -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); -} - - - -/*============================================================================== - * JSON document and value - *============================================================================*/ - -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; - } - - 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_UTILS - -/*============================================================================== - * JSON Pointer API (RFC 6901) - *============================================================================*/ - -/** - Get a token from JSON pointer string. - @param ptr [in,out] - 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; - *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)) { - *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 - - - -/*============================================================================== - * JSON Patch API (RFC 6902) - *============================================================================*/ - -/* 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 - - - -/*============================================================================== - * JSON Merge-Patch API (RFC 7386) - *============================================================================*/ - -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 */ - - - -/*============================================================================== - * Power10 Lookup Table - * These data are used by the floating-point number reader and writer. - *============================================================================*/ - -#if (!YYJSON_DISABLE_READER || !YYJSON_DISABLE_WRITER) && \ - (!YYJSON_DISABLE_FAST_FP_CONV) - -/** 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). +/** Normalized significant 128 bits of pow10, not rounded up (size: 10.4KB). This lookup table is used by both the double number reader and writer. - (generate with misc/make_tables.c) */ + (generated 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 */ @@ -3522,2403 +1901,4792 @@ static const u64 pow10_sig_table[] = { 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 cached pow10 value from `pow10_sig_table`. + @param exp10 The exponent of pow(10, e). This value must be in the 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 the highest 64-bit 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 float 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); +#else + return f64_to_bits(sign ? (f64)-INFINITY : (f64)INFINITY); +#endif +} + +/** Returns whether the double value is infinity (not NaN). */ +static_inline bool f64_is_inf(f64 val) { +#if YYJSON_HAS_IEEE_754 + return (f64_to_bits(val) & F64_EXP_MASK) == F64_BITS_INF; +#else + return val >= (f64)INFINITY || val <= (f64)-INFINITY; +#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); +#else + return f64_to_bits(sign ? (f64)-NAN : (f64)NAN); +#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. + *============================================================================*/ + +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + +#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 +} + +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + + + +/*============================================================================== + * 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: - 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: - Default Memory Allocator (Private) + * This is a simple libc memory allocator wrapper. + *============================================================================*/ + +#if defined(YYJSON_CUSTOM_ALC) + +/* user-provided via macro */ +extern const yyjson_alc YYJSON_CUSTOM_ALC; +#define YYJSON_DEFAULT_ALC YYJSON_CUSTOM_ALC + +#elif YYJSON_FREESTANDING + +/* null allocator */ +static const yyjson_alc YYJSON_DEFAULT_ALC = { + null_malloc, null_realloc, null_free, NULL +}; + +#else /* YYJSON_FREESTANDING */ + +/* default libc allocator */ +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 +}; + +#endif /* YYJSON_FREESTANDING */ + + + +/*============================================================================== + * 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, including 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, including 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, including 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; + dyn_ctx *ctx; + alc = (yyjson_alc *)def.malloc(def.ctx, hdr_len); + if (unlikely(!alc)) return NULL; + ctx = (dyn_ctx *)(void *)(alc + 1); + 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_chunk *chunk, *next; + if (unlikely(!alc)) return; + ctx = (dyn_ctx *)(void *)(alc + 1); + 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(const 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(const 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, + const 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 = constcast(yyjson_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, const 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, + const 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(const 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, + const 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(const 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(const 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(const void *lhs, const void *rhs) { + const yyjson_val_uni *luni = &((const yyjson_val *)lhs)->uni; + const yyjson_val_uni *runi = &((const 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(const void *lhs, const 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(const yyjson_val *lhs, const 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(const yyjson_mut_val *lhs, + const 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(constcast(yyjson_mut_val *)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 for pow(10, e). The input value must be in the + 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 to floating-point calculations 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 + if (unlikely(sig == 0)) return_f64_bin(0); + + /* + Fast path 2: + + To keep it simple, we only accept normal number here, + let the slow path handle subnormal and infinite numbers. + */ + 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; + } -/** - 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; + /* 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 } -#endif +#else /* FP_READER */ -/*============================================================================== - * JSON Character Matcher - *============================================================================*/ +/** + 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) -/** Character type */ -typedef u8 char_type; +#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) -/** Whitespace character: ' ', '\\t', '\\n', '\\r'. */ -static const char_type CHAR_TYPE_SPACE = 1 << 0; +#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) -/** Number character: '-', [0-9]. */ -static const char_type CHAR_TYPE_NUMBER = 1 << 1; +#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) -/** JSON Escaped character: '"', '\', [0x00-0x1F]. */ -static const char_type CHAR_TYPE_ESC_ASCII = 1 << 2; +#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) -/** Non-ASCII character: [0x80-0xFF]. */ -static const char_type CHAR_TYPE_NON_ASCII = 1 << 3; + u64 sig, num; + u8 *hdr = *ptr; + u8 *cur = *ptr; + u8 **end = ptr; + u8 *dot = NULL; + u8 *f64_end = NULL; + bool sign; -/** JSON container character: '{', '['. */ -static const char_type CHAR_TYPE_CONTAINER = 1 << 4; + /* 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); + } -/** Comment character: '/'. */ -static const char_type CHAR_TYPE_COMMENT = 1 << 5; + sign = (*hdr == '-'); + cur += sign; + sig = (u8)(*cur - '0'); -/** Line end character: '\\n', '\\r', '\0'. */ -static const char_type CHAR_TYPE_LINE_END = 1 << 6; + /* 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; + } -/** Hexadecimal numeric character: [0-9a-fA-F]. */ -static const char_type CHAR_TYPE_HEX = 1 << 7; + /* 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 -/** Character type table (generate with misc/make_tables.c) */ -static const char_type char_table[256] = { - 0x44, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x05, 0x45, 0x04, 0x04, 0x45, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, - 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, - 0x82, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00, - 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 -}; + /* 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); + } + } -/** Match a character with specified type. */ -static_inline bool char_is_type(u8 c, char_type type) { - return (char_table[c] & type) != 0; -} +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); + } -/** Match a whitespace: ' ', '\\t', '\\n', '\\r'. */ -static_inline bool char_is_space(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_SPACE); -} +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++; + } -/** Match a whitespace or comment: ' ', '\\t', '\\n', '\\r', '/'. */ -static_inline bool char_is_space_or_comment(u8 c) { - return char_is_type(c, (char_type)(CHAR_TYPE_SPACE | CHAR_TYPE_COMMENT)); -} + /* + libc's strtod() is used to parse the floating-point number. -/** Match a JSON number: '-', [0-9]. */ -static_inline bool char_is_number(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_NUMBER); -} + Note that the decimal point character used by strtod() is locale-dependent, + and the rounding direction may affected by fesetround(). -/** Match a JSON container: '{', '['. */ -static_inline bool char_is_container(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_CONTAINER); -} + For currently known locales, (en, zh, ja, ko, am, he, hi) use '.' as the + decimal point, while other locales use ',' as the decimal point. -/** Match a stop character in ASCII string: '"', '\', [0x00-0x1F,0x80-0xFF]. */ -static_inline bool char_is_ascii_stop(u8 c) { - return char_is_type(c, (char_type)(CHAR_TYPE_ESC_ASCII | - CHAR_TYPE_NON_ASCII)); -} + 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(f64_is_inf(val->uni.f64))) { + return_inf(); + } + val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; + *end = cur; + return true; -/** Match a line end character: '\\n', '\\r', '\0'. */ -static_inline bool char_is_line_end(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_LINE_END); +#undef return_err +#undef return_0 +#undef return_i64 +#undef return_f64 +#undef return_f64_bin +#undef return_inf +#undef return_raw } -/** Match a hexadecimal numeric character: [0-9a-fA-F]. */ -static_inline bool char_is_hex(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_HEX); -} +#endif /* FP_READER */ /*============================================================================== - * Digit Character Matcher + * MARK: - String Reader (Private) *============================================================================*/ -/** Digit type */ -typedef u8 digi_type; - -/** Digit: '0'. */ -static const digi_type DIGI_TYPE_ZERO = 1 << 0; - -/** Digit: [1-9]. */ -static const digi_type DIGI_TYPE_NONZERO = 1 << 1; - -/** Plus sign (positive): '+'. */ -static const digi_type DIGI_TYPE_POS = 1 << 2; - -/** Minus sign (negative): '-'. */ -static const digi_type DIGI_TYPE_NEG = 1 << 3; - -/** Decimal point: '.' */ -static const digi_type DIGI_TYPE_DOT = 1 << 4; - -/** Exponent sign: 'e, 'E'. */ -static const digi_type DIGI_TYPE_EXP = 1 << 5; - -/** Digit type table (generate with misc/make_tables.c) */ -static const digi_type digi_table[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, 0x04, 0x00, 0x08, 0x10, 0x00, - 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, - 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 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, 0x20, 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 character with specified type. */ -static_inline bool digi_is_type(u8 d, digi_type type) { - return (digi_table[d] & type) != 0; -} - -/** Match a sign: '+', '-' */ -static_inline bool digi_is_sign(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_POS | DIGI_TYPE_NEG)); -} +/** 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 -/** Match a none zero digit: [1-9] */ -static_inline bool digi_is_nonzero(u8 d) { - return digi_is_type(d, (digi_type)DIGI_TYPE_NONZERO); + 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 } -/** Match a digit: [0-9] */ -static_inline bool digi_is_digit(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_ZERO | DIGI_TYPE_NONZERO)); -} +/** + 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. -/** Match an exponent sign: 'e', 'E'. */ -static_inline bool digi_is_exp(u8 d) { - return digi_is_type(d, (digi_type)DIGI_TYPE_EXP); -} + 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) -/** Match a floating point indicator: '.', 'e', 'E'. */ -static_inline bool digi_is_fp(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_DOT | DIGI_TYPE_EXP)); -} + u8 *hdr = *ptr + 1; + u8 **end = ptr; + u8 *src = hdr, *dst = NULL, *pos; + u32 uni, tmp; -/** Match a digit or floating point indicator: [0-9], '.', 'e', 'E'. */ -static_inline bool digi_is_digit_or_fp(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_ZERO | DIGI_TYPE_NONZERO | - DIGI_TYPE_DOT | DIGI_TYPE_EXP)); -} + /* 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 -#if !YYJSON_DISABLE_READER + 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; -/*============================================================================== - * Hex Character Reader - * This function is used by JSON reader to read escaped characters. - *============================================================================*/ +#define expr_stop(i) \ + skip_ascii_stop##i: \ + src += i; \ + goto skip_ascii_end; -/** - 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 -}; + repeat16_incr(expr_jump) + src += 16; + goto skip_ascii; + repeat16_incr(expr_stop) -/** - Scans an escaped character sequence as a UTF-16 code unit (branchless). - e.g. "\\u005C" should pass "005C" as `cur`. - - This requires the string has 4-byte zero padding. - */ -static_inline bool read_hex_u16(const u8 *cur, u16 *val) { - u16 c0, c1, c2, c3, t0, t1; - c0 = hex_conv_table[cur[0]]; - c1 = hex_conv_table[cur[1]]; - c2 = hex_conv_table[cur[2]]; - c3 = hex_conv_table[cur[3]]; - t0 = (u16)((c0 << 8) | c2); - t1 = (u16)((c1 << 8) | c3); - *val = (u16)((t0 << 4) | t1); - return ((t0 | t1) & (u16)0xF0F0) == 0; -} +#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) -/*============================================================================== - * JSON Reader Utils - * These functions are used by JSON reader to read literals and comments. - *============================================================================*/ +#undef expr_jump +#undef expr_stop + } -/** Read 'true' literal, '*cur' should be 't'. */ -static_inline bool read_true(u8 **ptr, yyjson_val *val) { - u8 *cur = *ptr; - u8 **end = ptr; - if (likely(byte_match_4(cur, "true"))) { - val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; - *end = cur + 4; +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; } - return false; -} -/** Read 'false' literal, '*cur' should be 'f'. */ -static_inline bool read_false(u8 **ptr, yyjson_val *val) { - u8 *cur = *ptr; - u8 **end = ptr; - if (likely(byte_match_4(cur + 1, "alse"))) { - val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; - *end = cur + 5; +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++; } - return false; -} -/** Read 'null' literal, '*cur' should be 'n'. */ -static_inline bool read_null(u8 **ptr, yyjson_val *val) { - u8 *cur = *ptr; - u8 **end = ptr; - if (likely(byte_match_4(cur, "null"))) { - val->tag = YYJSON_TYPE_NULL; - *end = cur + 4; - return true; +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 } - return false; -} -/** Read 'Inf' or 'Infinity' literal (ignoring case). */ -static_inline bool read_inf(bool sign, u8 **ptr, u8 **pre, - yyjson_read_flag flg, yyjson_val *val) { - u8 *hdr = *ptr - sign; - u8 *cur = *ptr; - u8 **end = ptr; - if ((cur[0] == 'I' || cur[0] == 'i') && - (cur[1] == 'N' || cur[1] == 'n') && - (cur[2] == 'F' || cur[2] == 'f')) { - if ((cur[3] == 'I' || cur[3] == 'i') && - (cur[4] == 'N' || cur[4] == 'n') && - (cur[5] == 'I' || cur[5] == 'i') && - (cur[6] == 'T' || cur[6] == 't') && - (cur[7] == 'Y' || cur[7] == 'y')) { - cur += 8; - } else { - cur += 3; + 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); } - *end = cur; - if (has_read_flag(NUMBER_AS_RAW)) { - /* add null-terminator for previous raw string */ - if (*pre) **pre = '\0'; - *pre = cur; - 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_raw_get_inf(sign); + 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); } - return true; + 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; } - return false; + goto copy_escape; + +#undef return_err } -/** Read 'NaN' literal (ignoring case). */ -static_inline bool read_nan(bool sign, u8 **ptr, u8 **pre, - yyjson_read_flag flg, yyjson_val *val) { - u8 *hdr = *ptr - sign; - u8 *cur = *ptr; - u8 **end = ptr; - if ((cur[0] == 'N' || cur[0] == 'n') && - (cur[1] == 'A' || cur[1] == 'a') && - (cur[2] == 'N' || cur[2] == 'n')) { - cur += 3; - *end = cur; - if (has_read_flag(NUMBER_AS_RAW)) { - /* add null-terminator for previous raw string */ - if (*pre) **pre = '\0'; - *pre = cur; - 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_raw_get_nan(sign); - } - return true; - } - return false; +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); } -/** Read 'Inf', 'Infinity' or 'NaN' literal (ignoring case). */ -static_inline bool read_inf_or_nan(bool sign, u8 **ptr, u8 **pre, - yyjson_read_flag flg, yyjson_val *val) { - if (read_inf(sign, ptr, pre, flg, val)) return true; - if (read_nan(sign, ptr, pre, flg, val)) return true; - return false; +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); } -/** Read a JSON number as raw string. */ -static_noinline bool read_number_raw(u8 **ptr, - u8 **pre, - yyjson_read_flag flg, - yyjson_val *val, - const char **msg) { - -#define return_err(_pos, _msg) do { \ +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 = _pos; \ + *end = _end; \ return false; \ } while (false) - -#define return_raw() do { \ - val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \ + +#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 = cur; *end = cur; return true; \ + *pre = _str_end; *end = _cur_end; \ + return true; \ } while (false) - + u8 *hdr = *ptr; - u8 *cur = *ptr; u8 **end = ptr; - + u8 *src = hdr, *dst = NULL; + u32 uni, tmp; + /* add null-terminator for previous raw string */ - if (*pre) **pre = '\0'; - - /* skip sign */ - cur += (*cur == '-'); - - /* read first digit, check leading zero */ - if (unlikely(!digi_is_digit(*cur))) { - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_inf_or_nan(*hdr == '-', &cur, pre, flg, val)) return_raw(); - } - return_err(cur, "no digit after minus sign"); - } - - /* read integral part */ - if (*cur == '0') { - cur++; - if (unlikely(digi_is_digit(*cur))) { - return_err(cur - 1, "number with leading zero is not allowed"); - } - if (!digi_is_fp(*cur)) return_raw(); - } else { - while (digi_is_digit(*cur)) cur++; - if (!digi_is_fp(*cur)) return_raw(); - } - - /* read fraction part */ - if (*cur == '.') { - cur++; - if (!digi_is_digit(*cur++)) { - return_err(cur, "no digit after decimal point"); - } - while (digi_is_digit(*cur)) cur++; - } - - /* read exponent part */ - if (digi_is_exp(*cur)) { - cur += 1 + digi_is_sign(cur[1]); - if (!digi_is_digit(*cur++)) { - return_err(cur, "no digit after exponent sign"); - } - while (digi_is_digit(*cur)) cur++; - } - - return_raw(); - -#undef return_err -#undef return_raw -} + **pre = '\0'; -/** - Skips spaces and comments as many as possible. - - It will return false in these cases: - 1. No character is skipped. The 'end' pointer is set as input cursor. - 2. A multiline comment is not closed. The 'end' pointer is set as the head - of this comment block. - */ -static_noinline bool skip_spaces_and_comments(u8 **ptr) { - u8 *hdr = *ptr; - u8 *cur = *ptr; - u8 **end = ptr; - while (true) { - if (byte_match_2(cur, "/*")) { - hdr = cur; - cur += 2; - while (true) { - if (byte_match_2(cur, "*/")) { - cur += 2; - break; - } - if (*cur == 0) { - *end = hdr; - return false; - } - cur++; - } - continue; - } - if (byte_match_2(cur, "//")) { - cur += 2; - while (!char_is_line_end(*cur)) cur++; - continue; - } - if (char_is_space(*cur)) { - cur += 1; - while (char_is_space(*cur)) cur++; - continue; - } - break; - } - *end = cur; - return hdr != cur; -} +skip_ascii: +#define expr_jump(i) \ + if (likely(char_is_id_ascii(src[i]))) {} \ + else goto skip_ascii_stop##i; -/** - Check truncated string. - Returns true if `cur` match `str` but is truncated. - */ -static_inline bool is_truncated_str(u8 *cur, u8 *end, - const char *str, - bool case_sensitive) { - usize len = strlen(str); - if (cur + len <= end || end <= cur) return false; - if (case_sensitive) { - return memcmp(cur, str, (usize)(end - cur)) == 0; - } - for (; cur < end; cur++, str++) { - if ((*cur != (u8)*str) && (*cur != (u8)*str - 'a' + 'A')) { - return false; - } - } - return true; -} +#define expr_stop(i) \ + skip_ascii_stop##i: \ + src += i; \ + goto skip_ascii_end; -/** - 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 *end, - yyjson_read_code code, - yyjson_read_flag flg) { - if (cur >= end) return true; - if (code == YYJSON_READ_ERROR_LITERAL) { - if (is_truncated_str(cur, end, "true", true) || - is_truncated_str(cur, end, "false", true) || - is_truncated_str(cur, end, "null", true)) { - return true; - } + 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); } - if (code == YYJSON_READ_ERROR_UNEXPECTED_CHARACTER || - code == YYJSON_READ_ERROR_INVALID_NUMBER || - code == YYJSON_READ_ERROR_LITERAL) { - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (*cur == '-') cur++; - if (is_truncated_str(cur, end, "infinity", false) || - is_truncated_str(cur, end, "nan", false)) { - return true; + +skip_utf8: + while (*src >= 0x80) { + if (has_allow(EXT_WHITESPACE)) { + if (char_is_space_ext(*src) && ext_space_len(src)) { + return_suc(src, src); } } - } - if (code == YYJSON_READ_ERROR_UNEXPECTED_CONTENT) { - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (hdr + 3 <= cur && - is_truncated_str(cur - 3, end, "infinity", false)) { - return true; /* e.g. infin would be read as inf + in */ - } + 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 (code == YYJSON_READ_ERROR_INVALID_STRING) { - usize len = (usize)(end - cur); - - /* unicode escape sequence */ - if (*cur == '\\') { - if (len == 1) return true; - if (len <= 5) { - if (*++cur != 'u') return false; - for (++cur; cur < end; cur++) { - if (!char_is_hex(*cur)) return false; - } - return true; + 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); } - return false; } - - /* 2 to 4 bytes UTF-8, see `read_string()` for details. */ - if (*cur & 0x80) { - u8 c0 = cur[0], c1 = cur[1], c2 = cur[2]; - if (len == 1) { - /* 2 bytes UTF-8, truncated */ - if ((c0 & 0xE0) == 0xC0 && (c0 & 0x1E) != 0x00) return true; - /* 3 bytes UTF-8, truncated */ - if ((c0 & 0xF0) == 0xE0) return true; - /* 4 bytes UTF-8, truncated */ - if ((c0 & 0xF8) == 0xF0 && (c0 & 0x07) <= 0x04) return true; - } - if (len == 2) { - /* 3 bytes UTF-8, truncated */ - if ((c0 & 0xF0) == 0xE0 && - (c1 & 0xC0) == 0x80) { - u8 pat = (u8)(((c0 & 0x0F) << 1) | ((c1 & 0x20) >> 5)); - return 0x01 <= pat && pat != 0x1B; - } - /* 4 bytes UTF-8, truncated */ - if ((c0 & 0xF8) == 0xF0 && - (c1 & 0xC0) == 0x80) { - u8 pat = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4)); - return 0x01 <= pat && pat <= 0x10; - } - } - if (len == 3) { - /* 4 bytes UTF-8, truncated */ - if ((c0 & 0xF8) == 0xF0 && - (c1 & 0xC0) == 0x80 && - (c2 & 0xC0) == 0x80) { - u8 pat = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4)); - return 0x01 <= pat && pat <= 0x10; - } - } + 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; } } - return false; -} + if (char_is_id_ascii(*src)) goto copy_ascii; + goto copy_escape; +#undef return_err +#undef return_suc +} -#if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_READER */ /*============================================================================== - * BigInt For Floating Point Number Reader + * MARK: - JSON Reader Implementation (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. + * 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. *============================================================================*/ -/** Maximum exponent of exact pow10 */ -#define U64_POW10_MAX_EXP 19 +/** 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) -/** Table: [ 10^0, ..., 10^19 ] (generate with misc/make_tables.c) */ -static const u64 u64_pow10_table[U64_POW10_MAX_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) -}; + 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 */ -/** Maximum numbers of chunks used by a bigint (58 is enough here). */ -#define BIGINT_MAX_CHUNKS 64 + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ -/** Unsigned arbitrarily large integer */ -typedef struct bigint { - u32 used; /* used chunks count, should not be 0 */ - u64 bits[BIGINT_MAX_CHUNKS]; /* chunks */ -} bigint; + hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); + hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; + alc_num = hdr_len + 1; /* single value */ -/** - 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; -} + 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; -/** - 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; + if (char_is_num(*cur)) { + if (likely(read_num(&cur, pre, flg, val, &msg))) goto doc_end; + goto fail_number; } - for (; idx < max; idx++) { - u128_mul_add(big->bits[idx], val, carry, &hi, &lo); - big->bits[idx] = lo; - carry = hi; + if (*cur == '"') { + if (likely(read_str(&cur, eof, flg, val, &msg))) goto doc_end; + goto fail_string; } - 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; + if (*cur == 't') { + if (likely(read_true(&cur, val))) goto doc_end; + goto fail_literal_true; } -} - -/** - 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_EXP; exp -= U64_POW10_MAX_EXP) { - bigint_mul_u64(big, u64_pow10_table[U64_POW10_MAX_EXP]); + if (*cur == 'f') { + if (likely(read_false(&cur, val))) goto doc_end; + goto fail_literal_false; } - if (exp) { - bigint_mul_u64(big, u64_pow10_table[exp]); + 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; } -} - -/** - 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; + if (has_allow(INF_AND_NAN)) { + if (read_inf_or_nan(&cur, pre, flg, val)) goto doc_end; } - 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; -} + 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; -/** 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++; +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); +fail_depth: return_err(cur, DEPTH, MSG_DEPTH); -/*============================================================================== - * Diy Floating Point - *============================================================================*/ +#undef return_err +} -/** "Do It Yourself Floating Point" struct. */ -typedef struct diy_fp { - u64 sig; /* significand */ - i32 exp; /* exponent, base 2 */ - i32 pad; /* padding, useless */ -} diy_fp; +/** 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) -/** 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; -} +#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) -/** 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; -} + 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 */ -/** 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_RAW_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; - } -} + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ +#if YYJSON_READER_DEPTH_LIMIT + u32 container_depth = 0; /* current array/object depth */ +#endif + 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); -/*============================================================================== - * JSON Number Reader (IEEE-754) - *============================================================================*/ + 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; -/** Maximum exact pow10 exponent for double value. */ -#define F64_POW10_EXP_MAX_EXACT 22 + 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; + } -/** Cached pow10 table. */ -static const f64 f64_pow10_table[] = { - 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, - 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 -}; +arr_begin: +#if YYJSON_READER_DEPTH_LIMIT + container_depth++; + if (unlikely(container_depth >= YYJSON_READER_DEPTH_LIMIT)) { + goto fail_depth; + } +#endif + /* save current container */ + ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | + (ctn->tag & YYJSON_TAG_MASK); -/** - 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_number(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) + /* create a new array value, save parent container offset */ + val_incr(); + val->tag = YYJSON_TYPE_ARR; + val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); -#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_read_flag(BIGNUM_AS_RAW)) return_raw(); \ - if (has_read_flag(ALLOW_INF_AND_NAN)) return_f64_bin(F64_RAW_INF); \ - else return_err(hdr, "number is infinity when parsed as double"); \ -} while (false) - -#define return_raw() do { \ - if (*pre) **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_read_flag(NUMBER_AS_RAW)) { - return read_number_raw(ptr, pre, flg, val, msg); + /* push the new array value as current container */ + ctn = val; + ctn_len = 0; + +arr_val_begin: + if (*cur == '{') { + cur++; + goto obj_begin; } - - sign = (*hdr == '-'); - cur += sign; - - /* begin with a leading zero or non-digit */ - if (unlikely(!digi_is_nonzero(*cur))) { /* 0 or non-digit char */ - if (unlikely(*cur != '0')) { /* non-digit char */ - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_inf_or_nan(sign, &cur, pre, flg, val)) { - *end = cur; - return true; - } - } - return_err(cur, "no digit after minus sign"); - } - /* begin with 0 */ - if (likely(!digi_is_digit_or_fp(*++cur))) return_0(); - if (likely(*cur == '.')) { - dot_pos = cur++; - if (unlikely(!digi_is_digit(*cur))) { - return_err(cur, "no digit after decimal point"); - } - while (unlikely(*cur == '0')) cur++; - if (likely(digi_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(digi_is_digit(*cur))) { - return_err(cur - 1, "number with leading zero is not allowed"); - } - if (unlikely(digi_is_exp(*cur))) { /* 0 with any exponent is still 0 */ - cur += (usize)1 + digi_is_sign(cur[1]); - if (unlikely(!digi_is_digit(*cur))) { - return_err(cur, "no digit after exponent sign"); - } - while (digi_is_digit(*++cur)); + 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; } - return_f64_bin(0); + goto fail_literal_null; } - - /* 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 (!digi_is_digit_or_fp(*cur)) { - /* this number is an integer consisting of 19 digits */ - if (sign && (sig > ((u64)1 << 63))) { /* overflow */ - if (has_read_flag(BIGNUM_AS_RAW)) return_raw(); - return_f64(unsafe_yyjson_u64_to_f64(sig)); - } - return_i64(sig); + if (*cur == ']') { + cur++; + if (likely(ctn_len == 0)) goto arr_end; + if (has_allow(TRAILING_COMMAS)) goto arr_end; + do { cur--; } while (*cur != ','); + goto fail_trailing_comma; } - goto digi_intg_more; /* read more digits in integral part */ - - - /* process first non-digit character */ -#define expr_sepr(i) \ - digi_sepr_##i: \ - if (likely(!digi_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 (!digi_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 (digi_is_digit(*cur)) { - if (!digi_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_read_flag(BIGNUM_AS_RAW)) return_raw(); - return_f64(unsafe_yyjson_u64_to_f64(sig)); - } - return_i64(sig); - } - } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto arr_val_begin; } - - if (digi_is_exp(*cur)) { - dot_pos = cur; - goto digi_exp_more; + 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 (*cur == '.') { - dot_pos = cur++; - if (!digi_is_digit(*cur)) { - return_err(cur, "no digit after decimal point"); - } + 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; } - - - /* 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 (digi_is_digit(*++cur)); - if (!dot_pos) { - if (!digi_is_fp(*cur) && has_read_flag(BIGNUM_AS_RAW)) { - return_raw(); /* it's a large integer */ - } - dot_pos = cur; - if (*cur == '.') { - if (!digi_is_digit(*++cur)) { - return_err(cur, "no digit after decimal point"); - } - while (digi_is_digit(*cur)) cur++; - } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto arr_val_begin; + if (cur == eof) goto fail_comment; } - exp_sig = (i64)(dot_pos - sig_cut); - exp_sig += (dot_pos < sig_cut); - - /* ignore trailing zeros */ - tmp = cur - 1; - while (*tmp == '0' || *tmp == '.') tmp--; - if (tmp < sig_cut) { - sig_cut = NULL; - } else { - sig_end = cur; + goto fail_character_val; + +arr_val_end: + if (*cur == ',') { + cur++; + goto arr_val_begin; } - - if (digi_is_exp(*cur)) goto digi_exp_more; - goto digi_exp_finish; - - - /* fraction part end */ -digi_frac_end: - if (unlikely(dot_pos + 1 == cur)) { - return_err(cur, "no digit after decimal point"); + if (*cur == ']') { + cur++; + goto arr_end; } - sig_end = cur; - exp_sig = -(i64)((u64)(cur - dot_pos) - 1); - if (likely(!digi_is_exp(*cur))) { - if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) { - return_f64_bin(0); /* underflow */ - } - exp = (i32)exp_sig; - goto digi_finish; + 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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth--; +#endif + /* 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 digi_exp_more; + goto arr_val_end; } - - - /* read exponent part */ -digi_exp_more: - exp_sign = (*++cur == '-'); - cur += digi_is_sign(*cur); - if (unlikely(!digi_is_digit(*cur))) { - return_err(cur, "no digit after exponent sign"); + +obj_begin: +#if YYJSON_READER_DEPTH_LIMIT + container_depth++; + if (unlikely(container_depth >= YYJSON_READER_DEPTH_LIMIT)) { + goto fail_depth; } - while (*cur == '0') cur++; - - /* read exponent literal */ - tmp = cur; - while (digi_is_digit(*cur)) { - exp_lit = (i64)((u8)(*cur++ - '0') + (u64)exp_lit * 10); +#endif + /* 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 (unlikely(cur - tmp >= U64_SAFE_DIG)) { - if (exp_sign) { - return_f64_bin(0); /* underflow */ - } else { - return_inf(); /* overflow */ + if (likely(*cur == '}')) { + cur++; + if (likely(ctn_len == 0)) goto obj_end; + if (has_allow(TRAILING_COMMAS)) goto obj_end; + do { cur--; } while (*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; } - 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 (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_val_begin; } - if (unlikely(exp_sig > F64_MAX_DEC_EXP)) { - return_inf(); /* overflow */ + 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; } - 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_EXP_MAX_EXACT && - exp <= +F64_POW10_EXP_MAX_EXACT) { - f64 dbl = (f64)sig; - if (exp < 0) { - dbl /= f64_pow10_table[-exp]; - } else { - dbl *= f64_pow10_table[+exp]; - } - return_f64(dbl); + 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; } -#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); - } + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_val_begin; + if (cur == eof) goto fail_comment; } - - /* - 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_RAW_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); + 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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth--; +#endif + /* 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(raw == F64_RAW_INF)) return_inf(); - return_f64_bin(raw); + 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); +fail_depth: return_err(cur, DEPTH, MSG_DEPTH); + +#undef val_incr #undef return_err -#undef return_inf -#undef return_0 -#undef return_i64 -#undef return_f64 -#undef return_f64_bin -#undef return_raw } +/** 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 */ +#if YYJSON_READER_DEPTH_LIMIT + u32 container_depth = 0; /* current array/object depth */ +#endif + 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); -#else /* FP_READER */ + 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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth++; + if (unlikely(container_depth >= YYJSON_READER_DEPTH_LIMIT)) { + goto fail_depth; + } +#endif + + /* 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; + do { cur--; } while (*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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth--; +#endif + /* 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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth++; + if (unlikely(container_depth >= YYJSON_READER_DEPTH_LIMIT)) { + goto fail_depth; + } +#endif -/** - 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_number(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) + /* 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++; -#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_read_flag(BIGNUM_AS_RAW)) return_raw(); \ - if (has_read_flag(ALLOW_INF_AND_NAN)) return_f64_bin(F64_RAW_INF); \ - else return_err(hdr, "number is infinity when parsed as double"); \ -} while (false) - -#define return_raw() do { \ - if (*pre) **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) - - 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_read_flag(NUMBER_AS_RAW)) { - return read_number_raw(ptr, pre, flg, val, msg); +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; } - - sign = (*hdr == '-'); - cur += sign; - sig = (u8)(*cur - '0'); - - /* read first digit, check leading zero */ - if (unlikely(!digi_is_digit(*cur))) { - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_inf_or_nan(sign, &cur, pre, flg, val)) { - *end = cur; - return true; - } - } - return_err(cur, "no digit after minus sign"); + if (likely(*cur == '}')) { + cur++; + if (likely(ctn_len == 0)) goto obj_end; + if (has_allow(TRAILING_COMMAS)) goto obj_end; + do { cur--; } while (*cur != ','); + goto fail_trailing_comma; } - if (*cur == '0') { + 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++; - if (unlikely(digi_is_digit(*cur))) { - return_err(cur - 1, "number with leading zero is not allowed"); - } - if (!digi_is_fp(*cur)) return_0(); - goto read_double; + goto obj_val_begin; } - - /* 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 (digi_is_digit(cur[0]) && !digi_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_read_flag(BIGNUM_AS_RAW)) return_raw(); - return_f64(unsafe_yyjson_u64_to_f64(sig)); - } - return_i64(sig); - } + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto obj_key_end; } - -intg_end: - /* continuous digits ended */ - if (!digi_is_digit_or_fp(*cur)) { - /* this number is an integer consisting of 1 to 19 digits */ - if (sign && (sig > ((u64)1 << 63))) { - if (has_read_flag(BIGNUM_AS_RAW)) return_raw(); - return_f64(unsafe_yyjson_u64_to_f64(sig)); - } - return_i64(sig); + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (skip_trivia(&cur, eof, flg)) goto obj_key_end; + if (cur == eof) goto fail_comment; } - -read_double: - /* this number should be read as double */ - while (digi_is_digit(*cur)) cur++; - if (!digi_is_fp(*cur) && has_read_flag(BIGNUM_AS_RAW)) { - return_raw(); /* it's a large integer */ + 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 (*cur == '.') { - /* skip fraction part */ - dot = cur; + 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++; - if (!digi_is_digit(*cur)) { - return_err(cur, "no digit after decimal point"); - } + goto obj_begin; + } + if (*cur == '[') { cur++; - while (digi_is_digit(*cur)) cur++; + goto arr_begin; } - if (digi_is_exp(*cur)) { - /* skip exponent part */ - cur += 1 + digi_is_sign(cur[1]); - if (!digi_is_digit(*cur)) { - return_err(cur, "no digit after exponent sign"); + 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++; - while (digi_is_digit(*cur)) cur++; + goto obj_key_begin; } - - /* - 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 (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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth--; +#endif + + /* 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; } - if (unlikely(val->uni.f64 >= HUGE_VAL || val->uni.f64 <= -HUGE_VAL)) { - return_inf(); + +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; } - val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; - *end = cur; - return true; - + + **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); +fail_depth: return_err(cur, DEPTH, MSG_DEPTH); + +#undef val_incr #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 */ - /*============================================================================== - * JSON String Reader + * MARK: - JSON Reader (Public) *============================================================================*/ -/** - Read a JSON string. - @param ptr The head pointer of string before '"' prefix (inout). - @param lst JSON last position. - @param inv Allow invalid unicode. - @param val The string value to be written. - @param msg The error message pointer. - @return Whether success. - */ -static_inline bool read_string(u8 **ptr, - u8 *lst, - bool inv, - yyjson_val *val, - const char **msg) { - /* - Each unicode code point is encoded as 1 to 4 bytes in UTF-8 encoding, - we use 4-byte mask and pattern value to validate UTF-8 byte sequence, - this requires the input data to have 4-byte zero padding. - --------------------------------------------------- - 1 byte - unicode range [U+0000, U+007F] - unicode min [.......0] - unicode max [.1111111] - bit pattern [0.......] - --------------------------------------------------- - 2 byte - unicode range [U+0080, U+07FF] - unicode min [......10 ..000000] - unicode max [...11111 ..111111] - bit require [...xxxx. ........] (1E 00) - bit mask [xxx..... xx......] (E0 C0) - bit pattern [110..... 10......] (C0 80) - --------------------------------------------------- - 3 byte - unicode range [U+0800, U+FFFF] - unicode min [........ ..100000 ..000000] - unicode max [....1111 ..111111 ..111111] - bit require [....xxxx ..x..... ........] (0F 20 00) - bit mask [xxxx.... xx...... xx......] (F0 C0 C0) - bit pattern [1110.... 10...... 10......] (E0 80 80) - --------------------------------------------------- - 3 byte invalid (reserved for surrogate halves) - unicode range [U+D800, U+DFFF] - unicode min [....1101 ..100000 ..000000] - unicode max [....1101 ..111111 ..111111] - bit mask [....xxxx ..x..... ........] (0F 20 00) - bit pattern [....1101 ..1..... ........] (0D 20 00) - --------------------------------------------------- - 4 byte - unicode range [U+10000, U+10FFFF] - unicode min [........ ...10000 ..000000 ..000000] - unicode max [.....100 ..001111 ..111111 ..111111] - bit require [.....xxx ..xx.... ........ ........] (07 30 00 00) - bit mask [xxxxx... xx...... xx...... xx......] (F8 C0 C0 C0) - bit pattern [11110... 10...... 10...... 10......] (F0 80 80 80) - --------------------------------------------------- - */ -#if YYJSON_ENDIAN == YYJSON_BIG_ENDIAN - const u32 b1_mask = 0x80000000UL; - const u32 b1_patt = 0x00000000UL; - const u32 b2_mask = 0xE0C00000UL; - const u32 b2_patt = 0xC0800000UL; - const u32 b2_requ = 0x1E000000UL; - const u32 b3_mask = 0xF0C0C000UL; - const u32 b3_patt = 0xE0808000UL; - const u32 b3_requ = 0x0F200000UL; - const u32 b3_erro = 0x0D200000UL; - const u32 b4_mask = 0xF8C0C0C0UL; - const u32 b4_patt = 0xF0808080UL; - const u32 b4_requ = 0x07300000UL; - const u32 b4_err0 = 0x04000000UL; - const u32 b4_err1 = 0x03300000UL; -#elif YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN - const u32 b1_mask = 0x00000080UL; - const u32 b1_patt = 0x00000000UL; - const u32 b2_mask = 0x0000C0E0UL; - const u32 b2_patt = 0x000080C0UL; - const u32 b2_requ = 0x0000001EUL; - const u32 b3_mask = 0x00C0C0F0UL; - const u32 b3_patt = 0x008080E0UL; - const u32 b3_requ = 0x0000200FUL; - const u32 b3_erro = 0x0000200DUL; - const u32 b4_mask = 0xC0C0C0F8UL; - const u32 b4_patt = 0x808080F0UL; - const u32 b4_requ = 0x00003007UL; - const u32 b4_err0 = 0x00000004UL; - const u32 b4_err1 = 0x00003003UL; -#else - /* this should be evaluated at compile-time */ - v32_uni b1_mask_uni = {{ 0x80, 0x00, 0x00, 0x00 }}; - v32_uni b1_patt_uni = {{ 0x00, 0x00, 0x00, 0x00 }}; - v32_uni b2_mask_uni = {{ 0xE0, 0xC0, 0x00, 0x00 }}; - v32_uni b2_patt_uni = {{ 0xC0, 0x80, 0x00, 0x00 }}; - v32_uni b2_requ_uni = {{ 0x1E, 0x00, 0x00, 0x00 }}; - v32_uni b3_mask_uni = {{ 0xF0, 0xC0, 0xC0, 0x00 }}; - v32_uni b3_patt_uni = {{ 0xE0, 0x80, 0x80, 0x00 }}; - v32_uni b3_requ_uni = {{ 0x0F, 0x20, 0x00, 0x00 }}; - v32_uni b3_erro_uni = {{ 0x0D, 0x20, 0x00, 0x00 }}; - v32_uni b4_mask_uni = {{ 0xF8, 0xC0, 0xC0, 0xC0 }}; - v32_uni b4_patt_uni = {{ 0xF0, 0x80, 0x80, 0x80 }}; - v32_uni b4_requ_uni = {{ 0x07, 0x30, 0x00, 0x00 }}; - v32_uni b4_err0_uni = {{ 0x04, 0x00, 0x00, 0x00 }}; - v32_uni b4_err1_uni = {{ 0x03, 0x30, 0x00, 0x00 }}; - u32 b1_mask = b1_mask_uni.u; - u32 b1_patt = b1_patt_uni.u; - u32 b2_mask = b2_mask_uni.u; - u32 b2_patt = b2_patt_uni.u; - u32 b2_requ = b2_requ_uni.u; - u32 b3_mask = b3_mask_uni.u; - u32 b3_patt = b3_patt_uni.u; - u32 b3_requ = b3_requ_uni.u; - u32 b3_erro = b3_erro_uni.u; - u32 b4_mask = b4_mask_uni.u; - u32 b4_patt = b4_patt_uni.u; - u32 b4_requ = b4_requ_uni.u; - u32 b4_err0 = b4_err0_uni.u; - u32 b4_err1 = b4_err1_uni.u; -#endif - -#define is_valid_seq_1(uni) ( \ - ((uni & b1_mask) == b1_patt) \ -) - -#define is_valid_seq_2(uni) ( \ - ((uni & b2_mask) == b2_patt) && \ - ((uni & b2_requ)) \ -) - -#define is_valid_seq_3(uni) ( \ - ((uni & b3_mask) == b3_patt) && \ - ((tmp = (uni & b3_requ))) && \ - ((tmp != b3_erro)) \ -) - -#define is_valid_seq_4(uni) ( \ - ((uni & b4_mask) == b4_patt) && \ - ((tmp = (uni & b4_requ))) && \ - ((tmp & b4_err0) == 0 || (tmp & b4_err1) == 0) \ -) - -#define return_err(_end, _msg) do { \ - *msg = _msg; \ - *end = _end; \ - return false; \ +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) - - u8 *cur = *ptr; - u8 **end = ptr; - u8 *src = ++cur, *dst, *pos; - u16 hi, lo; - u32 uni, tmp; - -skip_ascii: - /* Most strings have no escaped characters, so we can jump them quickly. */ - -skip_ascii_begin: - /* - 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_stop(*src)))) src++; - else break; - }) - */ -#define expr_jump(i) \ - if (likely(!char_is_ascii_stop(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_begin; - repeat16_incr(expr_stop) - -#undef expr_jump -#undef expr_stop - -skip_ascii_end: - - /* - GCC may store src[i] in a register at each line of expr_jump(i) above. - These instructions are useless and will degrade performance. - This inline asm is a hint for gcc: "the memory has been modified, - do not cache it". - - MSVC, Clang, ICC can generate expected instructions without this hint. - */ -#if YYJSON_IS_REAL_GCC - __asm__ volatile("":"=m"(*src)); -#endif - if (likely(*src == '"')) { - val->tag = ((u64)(src - cur) << YYJSON_TAG_BIT) | - (u64)(YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC); - val->uni.str = (const char *)cur; - *src = '\0'; - *end = src + 1; - 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_valid_seq_3(uni)) { - src += 3; - uni = byte_load_4(src); + + 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); } - if (is_valid_seq_1(uni)) goto skip_ascii; - while (is_valid_seq_2(uni)) { - src += 2; - uni = byte_load_4(src); + hdr = (u8 *)alc.malloc(alc.ctx, len + YYJSON_PADDING_SIZE); + if (unlikely(!hdr)) { + return_err(0, MEMORY_ALLOCATION, MSG_MALLOC); } - while (is_valid_seq_4(uni)) { - src += 4; - uni = byte_load_4(src); + 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); + } + } } -#endif - if (unlikely(pos == src)) { - if (!inv) return_err(src, "invalid UTF-8 encoding in string"); - ++src; + if (unlikely(cur >= eof)) { + return_err(0, EMPTY_CONTENT, "input data is empty"); } - 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': - if (unlikely(!read_hex_u16(++src, &hi))) { - return_err(src - 2, "invalid escaped sequence in string"); - } - src += 4; - 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, "no low surrogate in string"); - } - if (unlikely(!read_hex_u16(src + 2, &lo))) { - return_err(src, "invalid escaped sequence in string"); - } - if (unlikely((lo & 0xFC00) != 0xDC00)) { - return_err(src, "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; - } - break; - default: return_err(src, "invalid escaped character in string"); + + /* 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 if (likely(*src == '"')) { - val->tag = ((u64)(dst - cur) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = (const char *)cur; - *dst = '\0'; - *end = src + 1; - return true; } else { - if (!inv) return_err(src, "unexpected control character in string"); - if (src >= lst) return_err(src, "unclosed string"); - *dst++ = *src++; + doc = read_root_single(hdr, cur, eof, alc, flg, err); } - -copy_ascii: - /* - Copy continuous ASCII, loop unrolling, same as the following code: - - while (true) repeat16({ - if (unlikely(char_is_ascii_stop(*src))) break; - *dst++ = *src++; - }) - */ -#if YYJSON_IS_REAL_GCC -# define expr_jump(i) \ - if (likely(!(char_is_ascii_stop(src[i])))) {} \ - else { __asm__ volatile("":"=m"(src[i])); goto copy_ascii_stop_##i; } -#else -# define expr_jump(i) \ - if (likely(!(char_is_ascii_stop(src[i])))) {} \ - else { goto copy_ascii_stop_##i; } -#endif - repeat16_incr(expr_jump) -#undef expr_jump - - byte_move_16(dst, src); - src += 16; - dst += 16; - goto copy_ascii; - - /* - The memory will be moved forward by at least 1 byte. So the `byte_move` - can be one byte more than needed to reduce the number of instructions. - */ -copy_ascii_stop_0: - goto copy_utf8; -copy_ascii_stop_1: - byte_move_2(dst, src); - src += 1; - dst += 1; - goto copy_utf8; -copy_ascii_stop_2: - byte_move_2(dst, src); - src += 2; - dst += 2; - goto copy_utf8; -copy_ascii_stop_3: - byte_move_4(dst, src); - src += 3; - dst += 3; - goto copy_utf8; -copy_ascii_stop_4: - byte_move_4(dst, src); - src += 4; - dst += 4; - goto copy_utf8; -copy_ascii_stop_5: - byte_move_4(dst, src); - byte_move_2(dst + 4, src + 4); - src += 5; - dst += 5; - goto copy_utf8; -copy_ascii_stop_6: - byte_move_4(dst, src); - byte_move_2(dst + 4, src + 4); - src += 6; - dst += 6; - goto copy_utf8; -copy_ascii_stop_7: - byte_move_8(dst, src); - src += 7; - dst += 7; - goto copy_utf8; -copy_ascii_stop_8: - byte_move_8(dst, src); - src += 8; - dst += 8; - goto copy_utf8; -copy_ascii_stop_9: - byte_move_8(dst, src); - byte_move_2(dst + 8, src + 8); - src += 9; - dst += 9; - goto copy_utf8; -copy_ascii_stop_10: - byte_move_8(dst, src); - byte_move_2(dst + 8, src + 8); - src += 10; - dst += 10; - goto copy_utf8; -copy_ascii_stop_11: - byte_move_8(dst, src); - byte_move_4(dst + 8, src + 8); - src += 11; - dst += 11; - goto copy_utf8; -copy_ascii_stop_12: - byte_move_8(dst, src); - byte_move_4(dst + 8, src + 8); - src += 12; - dst += 12; - goto copy_utf8; -copy_ascii_stop_13: - byte_move_8(dst, src); - byte_move_4(dst + 8, src + 8); - byte_move_2(dst + 12, src + 12); - src += 13; - dst += 13; - goto copy_utf8; -copy_ascii_stop_14: - byte_move_8(dst, src); - byte_move_4(dst + 8, src + 8); - byte_move_2(dst + 12, src + 12); - src += 14; - dst += 14; - goto copy_utf8; -copy_ascii_stop_15: - byte_move_16(dst, src); - src += 15; - dst += 15; - goto copy_utf8; - -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 & b3_mask) == b3_patt) { - byte_copy_4(dst, &uni); - dst += 3; - src += 3; - uni = byte_load_4(src); - } else break; - }) - if ((uni & b1_mask) == b1_patt) goto copy_ascii; - while (true) repeat4({ - if ((uni & b2_mask) == b2_patt) { - byte_copy_2(dst, &uni); - dst += 2; - src += 2; - uni = byte_load_4(src); - } else break; - }) - while (true) repeat4({ - if ((uni & b4_mask) == b4_patt) { - byte_copy_4(dst, &uni); - dst += 4; - src += 4; - uni = byte_load_4(src); - } else break; - }) -#else - while (is_valid_seq_3(uni)) { - byte_copy_4(dst, &uni); - dst += 3; - src += 3; - uni = byte_load_4(src); + + /* 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 +} + +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + +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; + usize dat_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); + if (file_size == -1) file_size = 0; } - if (is_valid_seq_1(uni)) goto copy_ascii; - while (is_valid_seq_2(uni)) { - byte_copy_2(dst, &uni); - dst += 2; - src += 2; - uni = byte_load_4(src); + /* reset to original position, may fail */ + if (fseek(file, file_pos, SEEK_SET) != 0) file_size = 0; + /* get file size from current position to end */ + if (file_size > 0) file_size -= file_pos; + } + + /* read file */ + if (file_size > 0) { + /* read the entire file in one call */ + dat_size = (usize)file_size; + buf_size = dat_size + YYJSON_PADDING_SIZE; + buf = alc.malloc(alc.ctx, buf_size); + if (buf == NULL) { + return_err(MEMORY_ALLOCATION, MSG_MALLOC); } - while (is_valid_seq_4(uni)) { - byte_copy_4(dst, &uni); - dst += 4; - src += 4; - uni = byte_load_4(src); + if (fread_safe(buf, dat_size, file) != dat_size) { + return_err(FILE_READ, MSG_FREAD); } -#endif - if (unlikely(pos == src)) { - if (!inv) return_err(src, "invalid UTF-8 encoding in string"); - goto copy_ascii_stop_1; + } 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); + dat_size += read_size; + if (read_size != chunk_now) break; + + chunk_now *= 2; + if (chunk_now > chunk_max) chunk_now = chunk_max; } - goto copy_ascii; } - goto copy_escape; - -#undef return_err -#undef is_valid_seq_1 -#undef is_valid_seq_2 -#undef is_valid_seq_3 -#undef is_valid_seq_4 -} + /* read JSON */ + memset((u8 *)buf + dat_size, 0, YYJSON_PADDING_SIZE); + flg |= YYJSON_READ_INSITU; + doc = yyjson_read_opts((char *)buf, dat_size, flg, &alc, err); + if (doc) { + doc->str_pool = (char *)buf; + return doc; + } else { + alc.free(alc.ctx, buf); + return NULL; + } +#undef return_err +} -/*============================================================================== - * JSON Reader Implementation - * - * 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. - *============================================================================*/ +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ -/** Read single value JSON document. */ -static_noinline yyjson_doc *read_root_single(u8 *hdr, - u8 *cur, - u8 *end, - yyjson_alc alc, - yyjson_read_flag flg, - yyjson_read_err *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 { \ - if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code, flg)) { \ - err->pos = (usize)(end - hdr); \ - err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ - err->msg = "unexpected end of data"; \ - } else { \ - err->pos = (usize)(_pos - hdr); \ - err->code = YYJSON_READ_ERROR_##_code; \ - err->msg = _msg; \ - } \ - if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ + err->pos = _pos > hdr ? (usize)(_pos - hdr) : 0; \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_##_code; \ 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 */ - - bool raw; /* read number as raw */ - bool inv; /* allow invalid unicode */ - u8 *raw_end; /* raw end for null-terminator */ - u8 **pre; /* 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; - raw = has_read_flag(NUMBER_AS_RAW) || has_read_flag(BIGNUM_AS_RAW); - inv = has_read_flag(ALLOW_INVALID_UNICODE) != 0; - raw_end = NULL; - pre = raw ? &raw_end : NULL; - - if (char_is_number(*cur)) { - if (likely(read_number(&cur, pre, flg, val, &msg))) goto doc_end; - goto fail_number; + + 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 (*cur == '"') { - if (likely(read_string(&cur, end, inv, val, &msg))) goto doc_end; - goto fail_string; + if (unlikely(!val)) { + return_err(cur, INVALID_PARAMETER, "output value is NULL"); } - if (*cur == 't') { - if (likely(read_true(&cur, val))) goto doc_end; - goto fail_literal_true; + +#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); } - if (*cur == 'f') { - if (likely(read_false(&cur, val))) goto doc_end; - goto fail_literal_false; + 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 (*cur == 'n') { - if (likely(read_null(&cur, val))) goto doc_end; - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_nan(false, &cur, pre, flg, val)) goto doc_end; - } - goto fail_literal_null; + 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); } - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_inf_or_nan(false, &cur, pre, flg, val)) goto doc_end; + 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 */ + u8 *raw_ptr; /* pending position for a deferred raw null-terminator */ + u8 raw_end[1]; /* dummy target for the first deferred null-terminator */ +}; + +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); } - goto fail_character; - -doc_end: - /* check invalid contents after json document */ - if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { - if (has_read_flag(ALLOW_COMMENTS)) { - if (!skip_spaces_and_comments(&cur)) { - if (byte_match_2(cur, "/*")) goto fail_comment; - } - } else { - while (char_is_space(*cur)) cur++; + memset(state->hdr + buf_len, 0, YYJSON_PADDING_SIZE); + state->cur = state->hdr; + state->raw_ptr = state->raw_end; + 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 (unlikely(cur < end)) goto fail_garbage; + if (state->hdr && !(state->flg & YYJSON_READ_INSITU)) { + alc.free(alc.ctx, state->hdr); + } + alc.free(alc.ctx, state); } - - if (pre && *pre) **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_read_flag(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, - "memory allocation failed"); -fail_literal_true: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'true'"); -fail_literal_false: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'false'"); -fail_literal_null: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'null'"); -fail_character: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a valid root value"); -fail_comment: - return_err(cur, INVALID_COMMENT, - "unclosed multiline comment"); -fail_garbage: - return_err(cur, UNEXPECTED_CONTENT, - "unexpected content after document"); - -#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 *end, - yyjson_alc alc, - yyjson_read_flag flg, - yyjson_read_err *err) { - +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)) { \ - err->pos = (usize)(end - hdr); \ - err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ - err->msg = "unexpected end of data"; \ + goto unexpected_end; \ } else { \ err->pos = (usize)(_pos - hdr); \ err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ - if (val_hdr) alc.free(alc.ctx, (void *)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)); \ + 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 = 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; \ + state->raw_ptr = raw_ptr; \ + 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 */ @@ -5932,55 +6700,166 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, yyjson_val *ctn_parent; /* parent of current container */ yyjson_doc *doc; /* the JSON document, equals to val_hdr */ const char *msg; /* error message */ - - bool raw; /* read number as raw */ - bool inv; /* allow invalid unicode */ - u8 *raw_end; /* raw end for null-terminator */ - u8 **pre; /* previous raw end pointer */ - - dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur); - hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); - hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; + + yyjson_read_err tmp_err; + u8 *raw_ptr; /* deferred raw null-terminator position, committed at save */ + u8 **pre = &raw_ptr; /* previous raw end pointer */ + u8 **con = NULL; /* for incremental string parsing */ + u8 saved_end = '\0'; /* saved end char */ + +#if YYJSON_READER_DEPTH_LIMIT + u32 container_depth = 0; /* current array/object depth */ +#endif + + /* 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; + raw_ptr = state->raw_ptr; 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; - raw = has_read_flag(NUMBER_AS_RAW) || has_read_flag(BIGNUM_AS_RAW); - inv = has_read_flag(ALLOW_INVALID_UNICODE) != 0; - raw_end = NULL; - pre = raw ? &raw_end : NULL; - - if (*cur++ == '{') { + + /* 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; - } else { + } + 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))) { + /* a root number may continue with more digits in a later chunk */ + if (unlikely(len < state->buf_len)) check_maybe_truncated_number(); + 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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth++; + if (unlikely(container_depth >= YYJSON_READER_DEPTH_LIMIT)) { + goto fail_depth; + } +#endif + /* 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; @@ -5989,16 +6868,17 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, cur++; goto arr_begin; } - if (char_is_number(*cur)) { + if (char_is_num(*cur)) { val_incr(); ctn_len++; - if (likely(read_number(&cur, pre, flg, val, &msg))) goto arr_val_end; + 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_string(&cur, end, inv, val, &msg))) goto arr_val_end; + if (likely(read_str_con(&cur, end, flg, val, &msg, con))) + goto arr_val_end; goto fail_string; } if (*cur == 't') { @@ -6017,4191 +6897,5043 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, val_incr(); ctn_len++; if (likely(read_null(&cur, val))) goto arr_val_end; - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_nan(false, &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_read_flag(ALLOW_TRAILING_COMMAS)) goto arr_end; - while (*cur != ',') cur--; - goto fail_trailing_comma; + if (*cur == ']') { + cur++; + if (likely(ctn_len == 0)) goto arr_end; + do { cur--; } while (*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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth--; +#endif + /* 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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth++; + if (unlikely(container_depth >= YYJSON_READER_DEPTH_LIMIT)) { + goto fail_depth; + } +#endif + + /* 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; + do { cur--; } while (*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: +#if YYJSON_READER_DEPTH_LIMIT + container_depth--; +#endif + + /* 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 || len < state->buf_len) && + !has_flg(STOP_WHEN_DONE)) { + save_incr_state(doc_end); + while (char_is_space(*cur)) cur++; + if (unlikely(cur < end)) goto fail_garbage; + /* the document is complete for the bytes seen so far, but more input + is still pending; it may hold trailing content that has to be + rejected, so request the remaining data before finalizing */ + if (unlikely(len < state->buf_len)) goto unexpected_end; + } + + **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 more 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); +fail_depth: return_err(cur, DEPTH, MSG_DEPTH); + +#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 */ + + + +/*============================================================================== + * MARK: - Streaming (SAX) JSON Reader (Public) + *============================================================================*/ + +#if !YYJSON_DISABLE_SAX_READER + +/* Default window size (bytes) if the caller passes 0. */ +#define SAX_DEFAULT_WINDOW ((usize)256 * 1024) +/* Floor on the window size: it must comfortably hold the longest bounded token + (a number, capped at INCR_NUM_MAX_LEN) plus padding and slack. */ +#define SAX_MIN_WINDOW ((usize)8192) +/* Default maximum container nesting depth. */ +#define SAX_DEFAULT_DEPTH ((usize)1024) +/* Bytes of lookahead guaranteed before dispatching on a value's first byte; + covers the longest fixed-size read (a literal). */ +#define SAX_DISPATCH_LOOKAHEAD ((usize)8) + +/* Result of an internal window fill. */ +#define SAX_FILL_ERR (-1) /* the source callback reported an I/O error */ +#define SAX_FILL_EOF (0) /* the source is exhausted */ +#define SAX_FILL_OK (1) /* more bytes were read */ +#define SAX_FILL_FULL (2) /* the window is full and cannot be grown */ + +/* Parser states for the streaming reader's pushdown automaton. */ +enum { + SAX_ST_VALUE, /* expect a value (root, array element, member value) */ + SAX_ST_ARR_FIRST, /* just after `[` */ + SAX_ST_ARR_COMMA, /* after an array element: expect `,` or `]` */ + SAX_ST_OBJ_FIRST, /* just after `{` */ + SAX_ST_OBJ_KEY, /* expect a key (after `,`) */ + SAX_ST_OBJ_COLON, /* after a key: expect `:` */ + SAX_ST_OBJ_COMMA, /* after a member value: expect `,` or `}` */ + SAX_ST_DONE /* the root value is complete */ +}; + +/* One entry of the container stack. */ +typedef struct { + usize count; /* number of elements/members seen so far */ + bool is_obj; /* true for an object, false for an array */ +} sax_frame; + +/* Internal state for a single streaming parse. */ +typedef struct { + yyjson_alc alc; /* allocator */ + yyjson_read_flag flg; /* sanitized read flags */ + yyjson_sax_source src; /* byte source callback */ + void *src_ctx; /* context for the source */ + const yyjson_sax_handler *h;/* event handler */ + void *hctx; /* context for the handler */ + + u8 *buf; /* window base (owned) */ + usize cap; /* total window capacity in bytes */ + usize usable; /* max live bytes (cap - padding) */ + u8 *cur; /* scan cursor */ + u8 *end; /* one past the last valid byte */ + u8 *anchor; /* earliest byte still needed (reclaim front) */ + usize consumed; /* bytes dropped from the window front */ + bool eof; /* source exhausted */ + bool src_err; /* source reported an error */ + + u8 *raw_ptr; /* pending position for a deferred raw '\0' */ + u8 raw_end[1]; /* dummy target for the deferred '\0' */ + + sax_frame *stack; /* container stack (owned) */ + usize depth; /* current nesting depth */ + usize stack_cap; /* allocated stack frames */ + usize max_depth; /* configured depth limit */ +} sax_ctx; + +/** True if `c` cannot be part of a (standard JSON) number, i.e. terminates it. */ +static_inline bool sax_num_end(u8 c) { + return !(char_is_num(c) || c == 'e' || c == 'E'); +} + +/** + Compact the window (dropping everything before `anchor`) and pull more bytes + from the source into the tail. Rebases `cur`, `end`, `anchor` on compaction. + See SAX_FILL_* for the return values. + */ +static int sax_fill(sax_ctx *c) { + usize shift, used, space; + size_t got; + + if (c->src_err) return SAX_FILL_ERR; + if (c->eof) return SAX_FILL_EOF; + + shift = (usize)(c->anchor - c->buf); + if (shift) { + usize live = (usize)(c->end - c->anchor); + memmove(c->buf, c->anchor, live); + c->cur -= shift; + c->end -= shift; + c->anchor = c->buf; + c->consumed += shift; + } + + used = (usize)(c->end - c->buf); + space = c->usable - used; + if (space == 0) return SAX_FILL_FULL; + + got = c->src(c->src_ctx, c->end, space); + if (got == YYJSON_SAX_SOURCE_ERROR) { + c->src_err = true; + return SAX_FILL_ERR; } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto arr_val_begin; + c->end += got; + /* maintain the null-terminator + zero padding the token readers rely on */ + memset(c->end, 0, YYJSON_PADDING_SIZE); + if (got == 0) { + c->eof = true; + return SAX_FILL_EOF; } - if (has_read_flag(ALLOW_INF_AND_NAN) && - (*cur == 'i' || *cur == 'I' || *cur == 'N')) { - val_incr(); - ctn_len++; - if (read_inf_or_nan(false, &cur, pre, flg, val)) goto arr_val_end; - goto fail_character_val; + return SAX_FILL_OK; +} + +/** Skip whitespace, refilling as needed. Returns false only on source error. */ +static bool sax_skip_ws(sax_ctx *c) { + for (;;) { + while (c->cur < c->end && char_is_space(*c->cur)) c->cur++; + if (c->cur < c->end) return true; + c->anchor = c->cur; /* leading whitespace is fully consumed */ + { + int r = sax_fill(c); + if (r == SAX_FILL_ERR) return false; + if (r == SAX_FILL_EOF || r == SAX_FILL_FULL) return true; + } } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto arr_val_begin; - if (byte_match_2(cur, "/*")) goto fail_comment; +} + +/** Ensure at least `need` bytes are available at `cur`, or EOF is reached. + `need` must be <= usable. Returns false only on source error. */ +static bool sax_ensure(sax_ctx *c, usize need) { + while ((usize)(c->end - c->cur) < need && !c->eof) { + c->anchor = c->cur; + { + int r = sax_fill(c); + if (r == SAX_FILL_ERR) return false; + if (r == SAX_FILL_EOF || r == SAX_FILL_FULL) break; + } } - goto fail_character_val; - -arr_val_end: - if (*cur == ',') { - cur++; - goto arr_val_begin; + return true; +} + +/** Absolute stream offset of the cursor, for error reporting. */ +static_inline usize sax_pos(sax_ctx *c) { + return c->consumed + (usize)(c->cur - c->buf); +} + +/** Grow the container stack to hold at least `need` frames. */ +static bool sax_stack_reserve(sax_ctx *c, usize need) { + sax_frame *tmp; + usize new_cap = c->stack_cap ? c->stack_cap : 16; + if (need <= c->stack_cap) return true; + while (new_cap < need) new_cap *= 2; + tmp = (sax_frame *)c->alc.realloc(c->alc.ctx, c->stack, + c->stack_cap * sizeof(sax_frame), + new_cap * sizeof(sax_frame)); + if (!tmp) return false; + c->stack = tmp; + c->stack_cap = new_cap; + return true; +} + +/** Map an internal SAX error code to a constant message. */ +static const char *sax_err_msg(yyjson_read_code code) { + if (code == YYJSON_READ_ERROR_SOURCE) return "source read error"; + if (code == YYJSON_READ_ERROR_TOKEN_TOO_LARGE) + return "a single token was larger than the streaming window"; + if (code == YYJSON_READ_ERROR_MEMORY_ALLOCATION) + return "memory allocation failed"; + return "read error"; +} + +/** + Ensure the whole number token starting at `cur` is resident: a terminating + byte is present after the digits, or EOF is reached. Returns 0 on success or a + `yyjson_read_code` on failure. + */ +static yyjson_read_code sax_num_ready(sax_ctx *c) { + usize off = 0; /* scan progress, kept across refills (cur may be rebased) */ + c->anchor = c->cur; + for (;;) { + u8 *p = c->cur + off; + while (p < c->end) { + if (sax_num_end(*p)) return YYJSON_READ_SUCCESS; /* terminator */ + p++; + } + off = (usize)(p - c->cur); + if (c->eof) return YYJSON_READ_SUCCESS; /* the '\0' terminates it */ + { + int r = sax_fill(c); + if (r == SAX_FILL_ERR) return YYJSON_READ_ERROR_SOURCE; + if (r == SAX_FILL_EOF) return YYJSON_READ_SUCCESS; + if (r == SAX_FILL_FULL) return YYJSON_READ_ERROR_TOKEN_TOO_LARGE; + } } - if (*cur == ']') { - cur++; - goto arr_end; +} + +/** + Ensure the whole string token starting at `cur` (the opening quote) is + resident: the closing quote is present, or EOF is reached (in which case the + string reader will report the unterminated string). Returns 0 on success or a + `yyjson_read_code` on failure. + */ +static yyjson_read_code sax_str_ready(sax_ctx *c) { + usize off = 1; /* scan progress past the opening quote, kept across + refills (cur may be rebased) */ + bool esc = false; + c->anchor = c->cur; + for (;;) { + u8 *p = c->cur + off; + while (p < c->end) { + u8 ch = *p; + if (esc) esc = false; + else if (ch == '\\') esc = true; + else if (ch == '"') return YYJSON_READ_SUCCESS; /* closing quote */ + p++; + } + off = (usize)(p - c->cur); + if (c->eof) return YYJSON_READ_SUCCESS; + { + int r = sax_fill(c); + if (r == SAX_FILL_ERR) return YYJSON_READ_ERROR_SOURCE; + if (r == SAX_FILL_EOF) return YYJSON_READ_SUCCESS; + if (r == SAX_FILL_FULL) return YYJSON_READ_ERROR_TOKEN_TOO_LARGE; + } } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto arr_val_end; +} + +/** + Probe for a complete string then parse it with the shared string reader. + Returns 0 on success (result in `val`), `YYJSON_READ_ERROR_INVALID_STRING` + (with `*msg` set) on a malformed string, or another `yyjson_read_code`. + */ +static yyjson_read_code sax_scan_string(sax_ctx *c, yyjson_val *val, + const char **msg) { + yyjson_read_code s = sax_str_ready(c); + if (s) return s; + if (!read_str(&c->cur, c->end, c->flg, val, msg)) + return YYJSON_READ_ERROR_INVALID_STRING; + return YYJSON_READ_SUCCESS; +} + +/** Determine the next state after a value completes, from the container stack. */ +static int sax_resolve(sax_ctx *c) { + if (c->depth == 0) return SAX_ST_DONE; + return c->stack[c->depth - 1].is_obj ? SAX_ST_OBJ_COMMA : SAX_ST_ARR_COMMA; +} + +bool yyjson_sax_read(yyjson_sax_source source, void *source_ctx, + const yyjson_sax_handler *handler, void *handler_ctx, + yyjson_read_flag flg, + const yyjson_sax_opts *opts, + const yyjson_alc *alc_ptr, + yyjson_read_err *err) { + +#define return_err(_code, _msg) do { \ + err->code = YYJSON_READ_ERROR_##_code; \ + err->msg = _msg; \ + err->pos = sax_pos(&c); \ + goto cleanup; \ +} while (false) + + /* fail with an internal `yyjson_read_code` produced by a probe helper */ +#define return_err_code(_c) do { \ + err->code = (_c); \ + err->msg = sax_err_msg(_c); \ + err->pos = sax_pos(&c); \ + goto cleanup; \ +} while (false) + + /* invoke a handler callback, aborting on a false return */ +#define EMIT0(cb) do { \ + if (c.h->cb && !c.h->cb(c.hctx)) return_err(ABORTED, "aborted by handler"); \ +} while (false) +#define EMIT1(cb, a) do { \ + if (c.h->cb && !c.h->cb(c.hctx, a)) return_err(ABORTED, "aborted by handler"); \ +} while (false) +#define EMIT2(cb, a, b) do { \ + if (c.h->cb && !c.h->cb(c.hctx, a, b)) return_err(ABORTED, "aborted by handler"); \ +} while (false) + + sax_ctx c; + yyjson_read_err tmp_err; + int state = SAX_ST_VALUE; + bool ok = false; + usize window, max_depth; + yyjson_val scratch; + const char *msg; + + if (!err) err = &tmp_err; + memset(err, 0, sizeof(*err)); + memset(&c, 0, sizeof(c)); + + if (unlikely(!source || !handler)) { + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; + err->msg = "source and handler must not be NULL"; + err->pos = 0; + return false; } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto arr_val_end; - if (byte_match_2(cur, "/*")) goto fail_comment; + + c.alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + /* only standard JSON plus raw-number and stop-when-done are honored */ + c.flg = flg & (YYJSON_READ_NUMBER_AS_RAW | + YYJSON_READ_BIGNUM_AS_RAW | + YYJSON_READ_STOP_WHEN_DONE); + c.src = source; + c.src_ctx = source_ctx; + c.h = handler; + c.hctx = handler_ctx; + + window = (opts && opts->window) ? opts->window : SAX_DEFAULT_WINDOW; + if (window < SAX_MIN_WINDOW) window = SAX_MIN_WINDOW; + max_depth = (opts && opts->max_depth) ? opts->max_depth : SAX_DEFAULT_DEPTH; + c.max_depth = max_depth; + c.cap = window; + c.usable = window - YYJSON_PADDING_SIZE; + + c.buf = (u8 *)c.alc.malloc(c.alc.ctx, c.cap); + if (unlikely(!c.buf)) { + err->code = YYJSON_READ_ERROR_MEMORY_ALLOCATION; + err->msg = "failed to allocate the streaming window"; + err->pos = 0; + return false; } - 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; + c.cur = c.end = c.anchor = c.buf; + memset(c.buf, 0, YYJSON_PADDING_SIZE); + c.raw_ptr = c.raw_end; + + /* prime the window and reject empty input */ + if (!sax_skip_ws(&c)) return_err(SOURCE, "source read error"); + if (c.cur >= c.end) return_err(EMPTY_CONTENT, "empty content"); + + for (;;) { + u8 ch; + + if (!sax_skip_ws(&c)) return_err(SOURCE, "source read error"); + + switch (state) { + case SAX_ST_DONE: + goto finished; + + case SAX_ST_VALUE: + if (!sax_ensure(&c, SAX_DISPATCH_LOOKAHEAD)) + return_err(SOURCE, "source read error"); + if (c.cur >= c.end) return_err(UNEXPECTED_END, "unexpected end"); + ch = *c.cur; + if (ch == '{') { + c.cur++; + if (unlikely(c.depth >= c.max_depth)) + return_err(DEPTH, "exceeded max depth"); + if (!sax_stack_reserve(&c, c.depth + 1)) + return_err_code(YYJSON_READ_ERROR_MEMORY_ALLOCATION); + EMIT0(obj_begin); + c.stack[c.depth].is_obj = true; + c.stack[c.depth].count = 0; + c.depth++; + state = SAX_ST_OBJ_FIRST; + } else if (ch == '[') { + c.cur++; + if (unlikely(c.depth >= c.max_depth)) + return_err(DEPTH, "exceeded max depth"); + if (!sax_stack_reserve(&c, c.depth + 1)) + return_err_code(YYJSON_READ_ERROR_MEMORY_ALLOCATION); + EMIT0(arr_begin); + c.stack[c.depth].is_obj = false; + c.stack[c.depth].count = 0; + c.depth++; + state = SAX_ST_ARR_FIRST; + } else if (ch == '"') { + yyjson_read_code s = sax_scan_string(&c, &scratch, &msg); + if (s == YYJSON_READ_ERROR_INVALID_STRING) + return_err(INVALID_STRING, msg); + if (s) return_err_code(s); + EMIT2(str, unsafe_yyjson_get_str(&scratch), + unsafe_yyjson_get_len(&scratch)); + state = sax_resolve(&c); + } else if (char_is_num(ch)) { + yyjson_read_code s = sax_num_ready(&c); + if (s) return_err_code(s); + /* re-aim the deferred raw '\0' at the dummy: a position from + a previous number may dangle after window compaction */ + c.raw_ptr = c.raw_end; + if (!read_num(&c.cur, &c.raw_ptr, c.flg, &scratch, &msg)) + return_err(INVALID_NUMBER, msg); + EMIT1(num, &scratch); + state = sax_resolve(&c); + } else if (ch == 't') { + if (!read_true(&c.cur, &scratch)) + return_err(LITERAL, "invalid literal, expected `true`"); + EMIT1(bool_val, true); + state = sax_resolve(&c); + } else if (ch == 'f') { + if (!read_false(&c.cur, &scratch)) + return_err(LITERAL, "invalid literal, expected `false`"); + EMIT1(bool_val, false); + state = sax_resolve(&c); + } else if (ch == 'n') { + if (!read_null(&c.cur, &scratch)) + return_err(LITERAL, "invalid literal, expected `null`"); + EMIT0(null_val); + state = sax_resolve(&c); + } else { + return_err(UNEXPECTED_CHARACTER, "unexpected character"); + } + break; + + case SAX_ST_ARR_FIRST: + if (c.cur >= c.end) return_err(UNEXPECTED_END, "unexpected end"); + if (*c.cur == ']') { + c.cur++; + c.depth--; + EMIT1(arr_end, (size_t)0); + state = sax_resolve(&c); + } else { + c.stack[c.depth - 1].count = 1; + state = SAX_ST_VALUE; + } + break; + + case SAX_ST_ARR_COMMA: + if (c.cur >= c.end) return_err(UNEXPECTED_END, "unexpected end"); + if (*c.cur == ']') { + usize n = c.stack[c.depth - 1].count; + c.cur++; + c.depth--; + EMIT1(arr_end, (size_t)n); + state = sax_resolve(&c); + } else if (*c.cur == ',') { + c.cur++; + c.stack[c.depth - 1].count++; + state = SAX_ST_VALUE; + } else { + return_err(UNEXPECTED_CHARACTER, "expected `,` or `]`"); + } + break; + + case SAX_ST_OBJ_FIRST: + if (c.cur >= c.end) return_err(UNEXPECTED_END, "unexpected end"); + if (*c.cur == '}') { + c.cur++; + c.depth--; + EMIT1(obj_end, (size_t)0); + state = sax_resolve(&c); + } else if (*c.cur == '"') { + yyjson_read_code s = sax_scan_string(&c, &scratch, &msg); + if (s == YYJSON_READ_ERROR_INVALID_STRING) + return_err(INVALID_STRING, msg); + if (s) return_err_code(s); + c.stack[c.depth - 1].count = 1; + EMIT2(key, unsafe_yyjson_get_str(&scratch), + unsafe_yyjson_get_len(&scratch)); + state = SAX_ST_OBJ_COLON; + } else { + return_err(UNEXPECTED_CHARACTER, "expected a string key"); + } + break; + + case SAX_ST_OBJ_KEY: + if (c.cur >= c.end) return_err(UNEXPECTED_END, "unexpected end"); + if (*c.cur != '"') + return_err(UNEXPECTED_CHARACTER, "expected a string key"); + { + yyjson_read_code s = sax_scan_string(&c, &scratch, &msg); + if (s == YYJSON_READ_ERROR_INVALID_STRING) + return_err(INVALID_STRING, msg); + if (s) return_err_code(s); + EMIT2(key, unsafe_yyjson_get_str(&scratch), + unsafe_yyjson_get_len(&scratch)); + state = SAX_ST_OBJ_COLON; + } + break; + + case SAX_ST_OBJ_COLON: + if (c.cur >= c.end) return_err(UNEXPECTED_END, "unexpected end"); + if (*c.cur != ':') + return_err(UNEXPECTED_CHARACTER, "expected `:`"); + c.cur++; + state = SAX_ST_VALUE; + break; + + case SAX_ST_OBJ_COMMA: + if (c.cur >= c.end) return_err(UNEXPECTED_END, "unexpected end"); + if (*c.cur == '}') { + usize n = c.stack[c.depth - 1].count; + c.cur++; + c.depth--; + EMIT1(obj_end, (size_t)n); + state = sax_resolve(&c); + } else if (*c.cur == ',') { + c.cur++; + c.stack[c.depth - 1].count++; + state = SAX_ST_OBJ_KEY; + } else { + return_err(UNEXPECTED_CHARACTER, "expected `,` or `}`"); + } + break; + } } - -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_string(&cur, end, inv, val, &msg))) goto obj_key_end; - goto fail_string; + +finished: + /* reject trailing non-whitespace content unless STOP_WHEN_DONE */ + if (!has_flg(STOP_WHEN_DONE)) { + if (!sax_skip_ws(&c)) return_err(SOURCE, "source read error"); + if (c.cur < c.end) return_err(UNEXPECTED_CONTENT, "unexpected content"); } - if (likely(*cur == '}')) { - cur++; - if (likely(ctn_len == 0)) goto obj_end; - if (has_read_flag(ALLOW_TRAILING_COMMAS)) goto obj_end; - while (*cur != ',') cur--; - goto fail_trailing_comma; + ok = true; + +cleanup: + if (c.buf) c.alc.free(c.alc.ctx, c.buf); + if (c.stack) c.alc.free(c.alc.ctx, c.stack); + return ok; + +#undef return_err +#undef return_err_code +#undef EMIT0 +#undef EMIT1 +#undef EMIT2 +} + +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE +static size_t sax_fp_source(void *ctx, void *buf, size_t len) { + FILE *fp = (FILE *)ctx; + size_t got = fread(buf, 1, len, fp); + if (got < len && ferror(fp)) return YYJSON_SAX_SOURCE_ERROR; + return got; +} + +bool yyjson_sax_read_fp(FILE *fp, + const yyjson_sax_handler *handler, void *handler_ctx, + yyjson_read_flag flg, + const yyjson_sax_opts *opts, + const yyjson_alc *alc, + yyjson_read_err *err) { + if (unlikely(!fp)) { + if (err) { + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; + err->msg = "input fp is NULL"; + err->pos = 0; + } + return false; } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_key_begin; + return yyjson_sax_read(sax_fp_source, fp, handler, handler_ctx, + flg, opts, alc, err); +} +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + +#endif /* YYJSON_DISABLE_SAX_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; } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_key_begin; - if (byte_match_2(cur, "/*")) goto fail_comment; +} + +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; } - goto fail_character_obj_key; - -obj_key_end: - if (*cur == ':') { - cur++; - goto obj_val_begin; +} + +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; } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_key_end; +} + + + +/*============================================================================== + * MARK: - Number Writer (Private) + *============================================================================*/ + +#if !YYJSON_DISABLE_FAST_FP_CONV /* FP_WRITER */ + +/** Trailing zero count table for number 0 to 99. + (generated 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; } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_key_end; - if (byte_match_2(cur, "/*")) goto fail_comment; +} + +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; } - goto fail_character_obj_sep; - -obj_val_begin: - if (*cur == '"') { - val++; - ctn_len++; - if (likely(read_string(&cur, end, inv, val, &msg))) goto obj_val_end; - goto fail_string; +} + +/** + 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; } - if (char_is_number(*cur)) { - val++; - ctn_len++; - if (likely(read_number(&cur, pre, flg, val, &msg))) goto obj_val_end; - goto fail_number; +} + +/** + 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; + } } - if (*cur == '{') { - cur++; - goto obj_begin; +} + +/** 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; } - if (*cur == '[') { - cur++; - goto arr_begin; +} + +/** 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; } - if (*cur == 't') { - val++; - ctn_len++; - if (likely(read_true(&cur, val))) goto obj_val_end; - goto fail_literal_true; + + /* 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; } - if (*cur == 'f') { - val++; - ctn_len++; - if (likely(read_false(&cur, val))) goto obj_val_end; - goto fail_literal_false; + + /* + 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; + } } - if (*cur == 'n') { - val++; - ctn_len++; - if (likely(read_null(&cur, val))) goto obj_val_end; - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_nan(false, &cur, pre, flg, val)) goto obj_val_end; + 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; } - goto fail_literal_null; } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_val_begin; + 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 the decimal point to indicate that 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); } - if (has_read_flag(ALLOW_INF_AND_NAN) && - (*cur == 'i' || *cur == 'I' || *cur == 'N')) { - val++; - ctn_len++; - if (read_inf_or_nan(false, &cur, pre, flg, val)) goto obj_val_end; - goto fail_character_val; + + /* 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 (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_val_begin; - if (byte_match_2(cur, "/*")) goto fail_comment; + + 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); + + /* separate 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); } - goto fail_character_val; - -obj_val_end: - if (likely(*cur == ',')) { - cur++; - goto obj_key_begin; +} + +/** + 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 the decimal point to indicate that 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); } - if (likely(*cur == '}')) { - cur++; - goto obj_end; + + /* 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 (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_val_end; + + 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); + + /* separate 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); } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_val_end; - if (byte_match_2(cur, "/*")) goto fail_comment; +} + +/** + 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 the decimal point to indicate that 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); } - 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; + + /* 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; } - -doc_end: - /* check invalid contents after json document */ - if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { - if (has_read_flag(ALLOW_COMMENTS)) { - skip_spaces_and_comments(&cur); - if (byte_match_2(cur, "/*")) goto fail_comment; + + 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); + + /* separate 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 { - while (char_is_space(*cur)) cur++; + /* 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); } - if (unlikely(cur < end)) goto fail_garbage; + } else { + /* subnormal number */ + byte_copy_4(buf, "0.0"); + return buf + 3; } - - if (pre && *pre) **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_read_flag(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, - "memory allocation failed"); -fail_trailing_comma: - return_err(cur, JSON_STRUCTURE, - "trailing comma is not allowed"); -fail_literal_true: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'true'"); -fail_literal_false: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'false'"); -fail_literal_null: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'null'"); -fail_character_val: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a valid JSON value"); -fail_character_arr_end: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a comma or a closing bracket"); -fail_character_obj_key: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a string for object key"); -fail_character_obj_sep: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a colon after object key"); -fail_character_obj_end: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a comma or a closing brace"); -fail_comment: - return_err(cur, INVALID_COMMENT, - "unclosed multiline comment"); -fail_garbage: - return_err(cur, UNEXPECTED_CONTENT, - "unexpected content after document"); - -#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 *end, - yyjson_alc alc, - yyjson_read_flag flg, - yyjson_read_err *err) { - -#define return_err(_pos, _code, _msg) do { \ - if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code, flg)) { \ - err->pos = (usize)(end - hdr); \ - err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ - err->msg = "unexpected end of data"; \ - } else { \ - err->pos = (usize)(_pos - hdr); \ - err->code = YYJSON_READ_ERROR_##_code; \ - err->msg = _msg; \ - } \ - if (val_hdr) alc.free(alc.ctx, (void *)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 */ - - bool raw; /* read number as raw */ - bool inv; /* allow invalid unicode */ - u8 *raw_end; /* raw end for null-terminator */ - u8 **pre; /* previous raw end pointer */ - - dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - 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; - raw = has_read_flag(NUMBER_AS_RAW) || has_read_flag(BIGNUM_AS_RAW); - inv = has_read_flag(ALLOW_INVALID_UNICODE) != 0; - raw_end = NULL; - pre = raw ? &raw_end : NULL; - - 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 /* 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 - while (true) repeat16({ - if (likely(byte_match_2(cur, " "))) cur += 2; - else break; - }) +#define snprintf_num(buf, len, fmt, dig, val) \ + sprintf((char *)buf, fmt, dig, val) #endif - - if (*cur == '{') { - cur++; - goto obj_begin; - } - if (*cur == '[') { - cur++; - goto arr_begin; - } - if (char_is_number(*cur)) { - val_incr(); - ctn_len++; - if (likely(read_number(&cur, pre, flg, val, &msg))) goto arr_val_end; - goto fail_number; - } - if (*cur == '"') { - val_incr(); - ctn_len++; - if (likely(read_string(&cur, end, inv, 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_read_flag(ALLOW_INF_AND_NAN)) { - if (read_nan(false, &cur, pre, flg, val)) goto arr_val_end; + +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; + } } - goto fail_literal_null; - } - if (*cur == ']') { - cur++; - if (likely(ctn_len == 0)) goto arr_end; - if (has_read_flag(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_read_flag(ALLOW_INF_AND_NAN) && - (*cur == 'i' || *cur == 'I' || *cur == 'N')) { - val_incr(); - ctn_len++; - if (read_inf_or_nan(false, &cur, pre, flg, val)) goto arr_val_end; - goto fail_character_val; - } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto arr_val_begin; - if (byte_match_2(cur, "/*")) 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_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto arr_val_end; - if (byte_match_2(cur, "/*")) 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; + return NULL; } else { - goto arr_val_end; + /* 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; } - -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; - }) +} + +/** 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 - while (true) repeat16({ - if (likely(byte_match_2(cur, " "))) cur += 2; - else break; - }) + int dig = F64_DEC_DIG; #endif - if (likely(*cur == '"')) { - val_incr(); - ctn_len++; - if (likely(read_string(&cur, end, inv, val, &msg))) goto obj_key_end; - goto fail_string; - } - if (likely(*cur == '}')) { - cur++; - if (likely(ctn_len == 0)) goto obj_end; - if (has_read_flag(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_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_key_begin; - if (byte_match_2(cur, "/*")) 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_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_key_end; - if (byte_match_2(cur, "/*")) goto fail_comment; - } - goto fail_character_obj_sep; - -obj_val_begin: - if (*cur == '"') { - val++; - ctn_len++; - if (likely(read_string(&cur, end, inv, val, &msg))) goto obj_val_end; - goto fail_string; - } - if (char_is_number(*cur)) { - val++; - ctn_len++; - if (likely(read_number(&cur, pre, flg, val, &msg))) goto obj_val_end; - goto fail_number; - } - if (*cur == '{') { - cur++; - goto obj_begin; - } - if (*cur == '[') { - cur++; - goto arr_begin; + 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); } - if (*cur == 't') { - val++; - ctn_len++; - if (likely(read_true(&cur, val))) goto obj_val_end; - goto fail_literal_true; +} + +#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); + } + } } - if (*cur == 'f') { - val++; - ctn_len++; - if (likely(read_false(&cur, val))) goto obj_val_end; - goto fail_literal_false; +} + +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; } - if (*cur == 'n') { - val++; - ctn_len++; - if (likely(read_null(&cur, val))) goto obj_val_end; - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_nan(false, &cur, pre, flg, val)) goto obj_val_end; +} + + + +/*============================================================================== + * 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 '/'. + (generated 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 '/'. + (generated 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 '/'. + (generated 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 '/'. + (generated 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"]. + (generated 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' +}; + +/** Lowercase variant of esc_hex_char_table. */ +yyjson_align(2) +static const u8 esc_hex_char_table_lower[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. (generated with misc/make_tables.c) */ +yyjson_align(2) +static const u8 esc_single_char_table[512] = { + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + '\\', 'b', '\\', 't', '\\', 'n', ' ', ' ', + '\\', 'f', '\\', 'r', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', '\\', '"', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', '\\', '/', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + '\\', '\\', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' +}; + +/** Returns the hex digit table to use for \uXXXX escapes. */ +static_inline const u8 *get_hex_table_with_flag(yyjson_write_flag flg) { + return has_flg(LOWERCASE_HEX) + ? esc_hex_char_table_lower + : esc_hex_char_table; +} + +/** 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; } - goto fail_literal_null; - } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_val_begin; - } - if (has_read_flag(ALLOW_INF_AND_NAN) && - (*cur == 'i' || *cur == 'I' || *cur == 'N')) { - val++; - ctn_len++; - if (read_inf_or_nan(false, &cur, pre, flg, val)) goto obj_val_end; - goto fail_character_val; - } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_val_begin; - if (byte_match_2(cur, "/*")) 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_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_val_end; - if (byte_match_2(cur, "/*")) 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 < end) && !has_read_flag(STOP_WHEN_DONE)) { - if (has_read_flag(ALLOW_COMMENTS)) { - skip_spaces_and_comments(&cur); - if (byte_match_2(cur, "/*")) goto fail_comment; + if (has_flg(ESCAPE_SLASHES)) { + return enc_table_cpy_slash; } else { - while (char_is_space(*cur)) cur++; + return enc_table_cpy; } - if (unlikely(cur < end)) goto fail_garbage; } - - if (pre && *pre) **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 - val_hdr)) - hdr_len + 1; - doc->str_pool = has_read_flag(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, - "memory allocation failed"); -fail_trailing_comma: - return_err(cur, JSON_STRUCTURE, - "trailing comma is not allowed"); -fail_literal_true: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'true'"); -fail_literal_false: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'false'"); -fail_literal_null: - return_err(cur, LITERAL, - "invalid literal, expected a valid literal such as 'null'"); -fail_character_val: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a valid JSON value"); -fail_character_arr_end: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a comma or a closing bracket"); -fail_character_obj_key: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a string for object key"); -fail_character_obj_sep: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a colon after object key"); -fail_character_obj_end: - return_err(cur, UNEXPECTED_CHARACTER, - "unexpected character, expected a comma or a closing brace"); -fail_comment: - return_err(cur, INVALID_COMMENT, - "unclosed multiline comment"); -fail_garbage: - return_err(cur, UNEXPECTED_CONTENT, - "unexpected content after document"); - -#undef val_incr -#undef return_err } +/** 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, + const u8 *hex_table) { + /* The replacement character U+FFFD, used to indicate invalid character. + Looked up via hex_table so that LOWERCASE_HEX produces "fffd" while + the default produces "FFFD". */ + const v32 pre = {{ '\\', 'u', '0', '0' }}; + const u8 *src = str; + const u8 *end = str + str_len; + *cur++ = '"'; -/*============================================================================== - * JSON Reader Entrance - *============================================================================*/ +copy_ascii: + /* + Copy continuous ASCII, loop unrolling, same as the following code: -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_read_flag(INSITU) && hdr) alc.free(alc.ctx, (void *)hdr); \ - return NULL; \ -} while (false) - - yyjson_read_err dummy_err; - yyjson_alc alc; - yyjson_doc *doc; - u8 *hdr = NULL, *end, *cur; - - /* validate input parameters */ - if (!err) err = &dummy_err; - if (likely(!alc_ptr)) { - alc = YYJSON_DEFAULT_ALC; - } else { - alc = *alc_ptr; - } - 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_read_flag(INSITU)) { - hdr = (u8 *)dat; - end = (u8 *)dat + len; - cur = (u8 *)dat; - } else { - if (unlikely(len >= USIZE_MAX - YYJSON_PADDING_SIZE)) { - return_err(0, MEMORY_ALLOCATION, "memory allocation failed"); - } - hdr = (u8 *)alc.malloc(alc.ctx, len + YYJSON_PADDING_SIZE); - if (unlikely(!hdr)) { - return_err(0, MEMORY_ALLOCATION, "memory allocation failed"); - } - end = hdr + len; - cur = hdr; - memcpy(hdr, dat, len); - memset(end, 0, YYJSON_PADDING_SIZE); - } - - /* skip empty contents before json document */ - if (unlikely(char_is_space_or_comment(*cur))) { - if (has_read_flag(ALLOW_COMMENTS)) { - if (!skip_spaces_and_comments(&cur)) { - return_err(cur - hdr, INVALID_COMMENT, - "unclosed multiline comment"); - } - } else { - if (likely(char_is_space(*cur))) { - while (char_is_space(*++cur)); - } - } - if (unlikely(cur >= end)) { - return_err(0, EMPTY_CONTENT, "input data is empty"); - } + 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; } - - /* read json document */ - if (likely(char_is_container(*cur))) { - if (char_is_space(cur[1]) && char_is_space(cur[2])) { - doc = read_root_pretty(hdr, cur, end, alc, flg, err); - } else { - doc = read_root_minify(hdr, cur, end, alc, flg, err); - } - } else { - doc = read_root_single(hdr, cur, end, alc, flg, err); + + while (end - src >= 4) { + repeat4_incr(expr_jump) + byte_copy_4(cur, src); + cur += 4; src += 4; } - - /* 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 ((hdr[0] == 0xEF && hdr[1] == 0xBB && hdr[2] == 0xBF)) { - err->msg = "byte order mark (BOM) is not supported"; - } else if (len >= 4 && - ((hdr[0] == 0x00 && hdr[1] == 0x00 && - hdr[2] == 0xFE && hdr[3] == 0xFF) || - (hdr[0] == 0xFF && hdr[1] == 0xFE && - hdr[2] == 0x00 && hdr[3] == 0x00))) { - err->msg = "UTF-32 encoding is not supported"; - } else if (len >= 2 && - ((hdr[0] == 0xFE && hdr[1] == 0xFF) || - (hdr[0] == 0xFF && hdr[1] == 0xFE))) { - err->msg = "UTF-16 encoding is not supported"; - } - } - if (!has_read_flag(INSITU)) alc.free(alc.ctx, (void *)hdr); + + while (end > src) { + expr_jump(0) + *cur++ = *src++; } - 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 dummy_err; - yyjson_doc *doc; - FILE *file; - - if (!err) err = &dummy_err; - if (unlikely(!path)) return_err(INVALID_PARAMETER, "input path is NULL"); - - file = fopen_readonly(path); - if (unlikely(!file)) return_err(FILE_OPEN, "file opening failed"); - - doc = yyjson_read_fp(file, flg, alc_ptr, err); - fclose(file); - return doc; - -#undef return_err -} + *cur++ = '"'; + return cur; -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 dummy_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 = &dummy_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; + 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; } - - /* 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, "fail to alloc memory"); + switch (enc_table[*src]) { + case CHAR_ENC_CPY_1: { + *cur++ = *src++; + goto copy_ascii; } - if (fread_safe(buf, (usize)file_size, file) != (usize)file_size) { - return_err(FILE_READ, "file reading failed"); + 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; } - } 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, "fail to alloc memory"); + 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]; } - buf_size += chunk_now; - if (!buf) { - buf = alc.malloc(alc.ctx, buf_size); - if (!buf) return_err(MEMORY_ALLOCATION, "fail to alloc memory"); +#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 { - tmp = alc.realloc(alc.ctx, buf, buf_size - chunk_now, buf_size); - if (!tmp) return_err(MEMORY_ALLOCATION, "fail to alloc memory"); - buf = tmp; + uni = byte_load_3(src); + if (unlikely(!is_utf8_seq3(uni))) goto err_cpy; + byte_copy_4(cur, &uni); } - 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; +#endif + cur += 3; + src += 3; + goto copy_utf8; } - } - - /* 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; - bool raw; /* read number as raw */ - u8 *raw_end; /* raw end for null-terminator */ - u8 **pre; /* previous raw end pointer */ - const char *msg; - yyjson_read_err dummy_err; - -#if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV - u8 buf[128]; - usize dat_len; + 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, &hex_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 - - if (!err) err = &dummy_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_HAS_IEEE_754 || 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, "memory allocation failed"); + u = (u16)(((u16)(src[0] & 0x1F) << 6) | + ((u16)(src[1] & 0x3F) << 0)); + byte_copy_2(cur + 0, &pre); + byte_copy_2(cur + 2, &hex_table[(u >> 8) * 2]); + byte_copy_2(cur + 4, &hex_table[(u & 0xFF) * 2]); + cur += 6; + src += 2; + goto copy_utf8; } - memcpy(hdr, dat, dat_len + 1); - } - hdr[dat_len] = 0; + 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 - - raw = (flg & (YYJSON_READ_NUMBER_AS_RAW | YYJSON_READ_BIGNUM_AS_RAW)) != 0; - raw_end = NULL; - pre = raw ? &raw_end : NULL; - -#if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV - if (!read_number(&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_number(&cur, pre, flg, val, &msg)) { - return_err(cur, INVALID_NUMBER, msg); - } - return (const char *)cur; + 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, &hex_table[(u >> 8) * 2]); + byte_copy_2(cur + 4, &hex_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 - -#undef return_err -} + 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, &hex_table[(hi >> 8) * 2]); + byte_copy_2(cur + 4, &hex_table[(hi & 0xFF) * 2]); + byte_copy_2(cur + 6, &pre); + byte_copy_2(cur + 8, &hex_table[(lo >> 8) * 2]); + byte_copy_2(cur + 10, &hex_table[(lo & 0xFF) * 2]); + cur += 12; + src += 4; + goto copy_utf8; + } + case CHAR_ENC_ERR_1: { + goto err_one; + } + default: break; /* unreachable */ + } -#endif /* YYJSON_DISABLE_READER */ +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); + /* U+FFFD = 0xFFFD, written as two pairs from hex_table so that + LOWERCASE_HEX produces "fffd". Replaces a single byte_copy_4 + from a hardcoded uppercase "FFFD" v32; same total output, one + extra load on the (rare) invalid-UTF-8-with-ALLOW path. */ + byte_copy_2(cur + 2, &hex_table[0xFF * 2]); + byte_copy_2(cur + 4, &hex_table[0xFD * 2]); + cur += 6; + src += 1; + goto copy_utf8; +} -#if !YYJSON_DISABLE_WRITER /*============================================================================== - * Integer Writer - * - * 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 + * MARK: - JSON Writer Utilities (Private) *============================================================================*/ -/** 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; +/** 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; } -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; +/** 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; } -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; +/** 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; } -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; +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + +/** Write data to file pointer. */ +static bool write_dat_to_fp(FILE *fp, u8 *dat, usize len, + yyjson_write_err *err) { + if (fwrite(dat, 1, len, fp) != len) { + err->msg = "file writing failed"; + err->code = YYJSON_WRITE_ERROR_FILE_WRITE; + return false; } + return true; } -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; +/** 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, 1, len, file) != len) { + return_err(FILE_WRITE, MSG_FWRITE); + } + if (fclose(file) != 0) { + file = NULL; + return_err(FILE_WRITE, MSG_FCLOSE); } + return true; + +#undef return_err } +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + /*============================================================================== - * Number Writer + * MARK: - JSON Writer Implementation (Private) *============================================================================*/ -#if YYJSON_HAS_IEEE_754 && !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 -}; +typedef struct yyjson_write_ctx { + usize tag; +} yyjson_write_ctx; -static_inline u8 *write_u32_len_1_to_9(u32 val, u8 *buf) { - if (val >= 100000000) { - u32 hi = val / 10000000; - val = val - hi * 10000000; - *buf++ = (u8)(hi + '0'); - } - return write_u32_len_1_to_8((u32)val, buf); +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 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 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); } -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 single JSON value. */ +static_inline u8 *write_root_single(yyjson_val *val, + yyjson_write_flag flg, + yyjson_alc alc, + char *buf, 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) -/** - 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; - } -} +#define incr_len(_len) do { \ + if (buf) hdr = *dat_len >= _len ? (u8 *)buf : (u8 *)NULL; \ + else hdr = (u8 *)alc.malloc(alc.ctx, _len); \ + if (!hdr) goto fail_alloc; \ + cur = hdr; \ +} while (false) -/** - 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; - } - } -} +#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); + const u8 *hex_table = get_hex_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, hex_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; -/** 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; -} + case YYJSON_TYPE_NULL: + incr_len(8); + cur = write_null(cur); + break; -/** 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; + 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; } -} -/** Magic number for fast `divide by power of 10`. */ -typedef struct { - u64 p10, mul; - u32 shr1, shr2; -} div_pow10_magic; + if (newline) *cur++ = '\n'; + *cur = '\0'; + *dat_len = (usize)(cur - hdr); + memset(err, 0, sizeof(yyjson_write_err)); + return hdr; -/** 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 }, -}; +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); -/** 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; +#undef return_err +#undef check_str_len +#undef incr_len } -/** 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); -} +/** Write JSON document minify. + The root of this document should be a non-empty container. */ +static_inline u8 *write_root_minify(const yyjson_val *root, + const yyjson_write_flag flg, + const yyjson_alc alc, + char *buf, 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) -/** 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); -} +#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); \ + yyjson_assume((u8 *)ctx <= (u8 *)end); \ + ctx_len = (usize)((u8 *)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) -/** 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; +#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); + const u8 *hex_table = get_hex_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; + + if (buf) { + hdr = (u8 *)buf; + alc_len = *dat_len; + alc_len = size_align_down(alc_len, sizeof(yyjson_write_ctx)); + if (alc_len <= sizeof(yyjson_write_ctx)) goto fail_alloc; + } else { + 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; } - - /* 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; + 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, hex_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(2 * sizeof(*ctx)); + 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; } } - 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; -} + 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; -/** 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; +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; } - - /* - 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; - } + +doc_end: + if (newline) { + incr_len(2); + *(cur - 1) = '\n'; + cur++; } - 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; -} + *--cur = '\0'; + *dat_len = (usize)(cur - hdr); + memset(err, 0, sizeof(yyjson_write_err)); + return hdr; -/** 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; +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 inf/nan if allowed. */ -static_inline u8 *write_inf_or_nan(u8 *buf, yyjson_write_flag flg, - u64 sig_raw, bool sign) { - if (has_write_flag(INF_AND_NAN_AS_NULL)) { - byte_copy_4(buf, "null"); - return buf + 4; - } - if (has_write_flag(ALLOW_INF_AND_NAN)) { - if (sig_raw == 0) { - buf[0] = '-'; - buf += sign; - byte_copy_8(buf, "Infinity"); - return buf + 8; +/** Write JSON document pretty. + The root of this document should be a non-empty container. */ +static_inline u8 *write_root_pretty(const yyjson_val *root, + const yyjson_write_flag flg, + const yyjson_alc alc, + char *buf, 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); \ + yyjson_assume((u8 *)ctx <= (u8 *)end); \ + ctx_len = (usize)((u8 *)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); + const u8 *hex_table = get_hex_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; + + if (buf) { + hdr = (u8 *)buf; + alc_len = *dat_len; + alc_len = size_align_down(alc_len, sizeof(yyjson_write_ctx)); + if (alc_len <= sizeof(yyjson_write_ctx)) goto fail_alloc; + } else { + 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); + if ((sizeof(usize) < 8) && !no_indent && + level > (USIZE_MAX - 16 - str_len * 6) / 4) goto fail_alloc; + 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 { - byte_copy_4(buf, "NaN"); - return buf + 3; + cur = write_str(cur, esc, inv, str_ptr, str_len, + enc_table, hex_table); + if (unlikely(!cur)) goto fail_str; } + *cur++ = is_key ? ':' : ','; + *cur++ = is_key ? ' ' : '\n'; + goto val_end; } - 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_raw(f64_to_f32(f64_from_raw(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 (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 (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); - + 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); + incr_len(2 * sizeof(*ctx) + (no_indent ? 0 : level * 4)); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + 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 { - /* 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); + /* 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 = 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 { - /* 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_9(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); + goto ctn_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); +doc_end: + if (newline) { + incr_len(2); + *cur++ = '\n'; } - - /* 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; + *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 +} + +static char *write_root(const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + char *buf, 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 (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); - } - + + if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) { + return (char *)write_root_single(root, flg, alc, buf, dat_len, err); + } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) { + return (char *)write_root_pretty(root, flg, alc, buf, dat_len, err); } 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); + return (char *)write_root_minify(root, flg, alc, buf, dat_len, err); } } -/** - 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); + +/*============================================================================== + * 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) { + return write_root(val, flg, alc_ptr, NULL, 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 write_root(root, flg, alc_ptr, NULL, dat_len, err); +} + +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + +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; } - - /* 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; + + dat = (u8 *)write_root(root, flg, &alc, NULL, &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; } - - 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); - } + + dat = (u8 *)write_root(root, flg, &alc, NULL, &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); +} + +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + +size_t yyjson_val_write_buf(char *buf, size_t buf_len, + const yyjson_val *val, + yyjson_write_flag flg, + yyjson_write_err *err) { + if (unlikely(!buf || !buf_len)) { + if (err) err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; + if (err) err->msg = "input buf or buf_len is invalid"; + return 0; } else { - /* subnormal number */ - byte_copy_4(buf, "0.0"); - return buf + 3; + write_root(val, flg, &YYJSON_NULL_ALC, buf, &buf_len, err); + return buf_len; } } -#else /* FP_WRITER */ +size_t yyjson_write_buf(char *buf, size_t buf_len, + const yyjson_doc *doc, + yyjson_write_flag flg, + yyjson_write_err *err) { + yyjson_val *root = doc ? doc->root : NULL; + return yyjson_val_write_buf(buf, buf_len, root, flg, err); +} -#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(!digi_is_digit(*cur))) { - /* nan, inf, or bad output */ - if (has_write_flag(INF_AND_NAN_AS_NULL)) { - byte_copy_4(buf, "null"); - return buf + 4; - } else if (has_write_flag(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; - } + +/*============================================================================== + * 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); } - return NULL; + chunk = chunk->next; + } + return sum; +} + +/** Write single JSON value. */ +static_inline u8 *mut_write_root_single(yyjson_mut_val *val, + yyjson_write_flag flg, + yyjson_alc alc, + char *buf, usize *dat_len, + yyjson_write_err *err) { + return write_root_single((yyjson_val *)val, flg, alc, buf, dat_len, err); +} + +/** Write JSON document minify. + The root of this document should be a non-empty container. */ +static_inline u8 *mut_write_root_minify(const yyjson_mut_val *root, + usize estimated_val_num, + yyjson_write_flag flg, + yyjson_alc alc, + char *buf, 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); \ + yyjson_assume((u8 *)ctx <= (u8 *)end); \ + ctx_len = (usize)((u8 *)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); + const u8 *hex_table = get_hex_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; + + if (buf) { + hdr = (u8 *)buf; + alc_len = *dat_len; + alc_len = size_align_down(alc_len, sizeof(yyjson_mut_write_ctx)); + if (alc_len <= sizeof(yyjson_mut_write_ctx)) goto fail_alloc; } 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; - } + 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, hex_table); + if (unlikely(!cur)) goto fail_str; } - if (fixed) { - /* remove trailing zeros */ - while (*(end - 1) == '0') end--; - end += *(end - 1) == '.'; + *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(2 * sizeof(*ctx)); + 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 { - 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); - } - } + /* 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; } - 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_raw(raw); - int len = snprintf_num(buf, FP_BUF_LEN, "%.*g", dig, val); - return write_fp_reformat(buf, len, flg, false); -} + 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; -/** 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_raw(raw)); - int len = snprintf_num(buf, FP_BUF_LEN, "%.*g", dig, val); - return write_fp_reformat(buf, len, flg, false); -} +val_end: + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + val = val->next; + goto val_begin; -/** 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_raw(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); +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 { - return write_f64_raw(buf, raw, flg); + goto ctn_end; } -} -#endif /* FP_WRITER */ - -/** Write a JSON number (requires 40 bytes buffer). */ -static_inline u8 *write_number(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); - } - } +doc_end: + if (newline) { + incr_len(2); + *(cur - 1) = '\n'; + cur++; } -} - - - -/*============================================================================== - * String Writer - *============================================================================*/ - -/** 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' -}; + *--cur = '\0'; + *dat_len = (usize)(cur - hdr); + err->code = YYJSON_WRITE_SUCCESS; + err->msg = NULL; + return hdr; -/** 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', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', '\\', '"', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', '\\', '/', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - '\\', '\\', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' -}; +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); -/** Returns the encode table with options. */ -static_inline const char_enc_type *get_enc_table_with_flag( - yyjson_write_flag flg) { - if (has_write_flag(ESCAPE_UNICODE)) { - if (has_write_flag(ESCAPE_SLASHES)) { - return enc_table_esc_slash; +#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 *mut_write_root_pretty(const yyjson_mut_val *root, + usize estimated_val_num, + yyjson_write_flag flg, + yyjson_alc alc, + char *buf, 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); \ + yyjson_assume((u8 *)ctx <= (u8 *)end); \ + ctx_len = (usize)((u8 *)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); + const u8 *hex_table = get_hex_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; + + if (buf) { + hdr = (u8 *)buf; + alc_len = *dat_len; + alc_len = size_align_down(alc_len, sizeof(yyjson_mut_write_ctx)); + if (alc_len <= sizeof(yyjson_mut_write_ctx)) goto fail_alloc; + } else { + 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); + if ((sizeof(usize) < 8) && !no_indent && + level > (USIZE_MAX - 16 - str_len * 6) / 4) goto fail_alloc; + 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 { - return enc_table_esc; + cur = write_str(cur, esc, inv, str_ptr, str_len, + enc_table, hex_table); + if (unlikely(!cur)) goto fail_str; } - } else { - if (has_write_flag(ESCAPE_SLASHES)) { - return enc_table_cpy_slash; + *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); + incr_len(2 * sizeof(*ctx) + (no_indent ? 0 : level * 4)); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + 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 { - return enc_table_cpy; + /* 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 = 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; -/** 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; -} +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); -/** - 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_string_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; +#undef return_err +#undef incr_len +#undef check_str_len } -/** - 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_string(u8 *cur, bool esc, bool inv, - const u8 *str, usize str_len, - const char_enc_type *enc_table) { - - /* UTF-8 character mask and pattern, see `read_string()` for details. */ -#if YYJSON_ENDIAN == YYJSON_BIG_ENDIAN - const u16 b2_mask = 0xE0C0UL; - const u16 b2_patt = 0xC080UL; - const u16 b2_requ = 0x1E00UL; - const u32 b3_mask = 0xF0C0C000UL; - const u32 b3_patt = 0xE0808000UL; - const u32 b3_requ = 0x0F200000UL; - const u32 b3_erro = 0x0D200000UL; - const u32 b4_mask = 0xF8C0C0C0UL; - const u32 b4_patt = 0xF0808080UL; - const u32 b4_requ = 0x07300000UL; - const u32 b4_err0 = 0x04000000UL; - const u32 b4_err1 = 0x03300000UL; -#elif YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN - const u16 b2_mask = 0xC0E0UL; - const u16 b2_patt = 0x80C0UL; - const u16 b2_requ = 0x001EUL; - const u32 b3_mask = 0x00C0C0F0UL; - const u32 b3_patt = 0x008080E0UL; - const u32 b3_requ = 0x0000200FUL; - const u32 b3_erro = 0x0000200DUL; - const u32 b4_mask = 0xC0C0C0F8UL; - const u32 b4_patt = 0x808080F0UL; - const u32 b4_requ = 0x00003007UL; - const u32 b4_err0 = 0x00000004UL; - const u32 b4_err1 = 0x00003003UL; -#else - /* this should be evaluated at compile-time */ - v16_uni b2_mask_uni = {{ 0xE0, 0xC0 }}; - v16_uni b2_patt_uni = {{ 0xC0, 0x80 }}; - v16_uni b2_requ_uni = {{ 0x1E, 0x00 }}; - v32_uni b3_mask_uni = {{ 0xF0, 0xC0, 0xC0, 0x00 }}; - v32_uni b3_patt_uni = {{ 0xE0, 0x80, 0x80, 0x00 }}; - v32_uni b3_requ_uni = {{ 0x0F, 0x20, 0x00, 0x00 }}; - v32_uni b3_erro_uni = {{ 0x0D, 0x20, 0x00, 0x00 }}; - v32_uni b4_mask_uni = {{ 0xF8, 0xC0, 0xC0, 0xC0 }}; - v32_uni b4_patt_uni = {{ 0xF0, 0x80, 0x80, 0x80 }}; - v32_uni b4_requ_uni = {{ 0x07, 0x30, 0x00, 0x00 }}; - v32_uni b4_err0_uni = {{ 0x04, 0x00, 0x00, 0x00 }}; - v32_uni b4_err1_uni = {{ 0x03, 0x30, 0x00, 0x00 }}; - u16 b2_mask = b2_mask_uni.u; - u16 b2_patt = b2_patt_uni.u; - u16 b2_requ = b2_requ_uni.u; - u32 b3_mask = b3_mask_uni.u; - u32 b3_patt = b3_patt_uni.u; - u32 b3_requ = b3_requ_uni.u; - u32 b3_erro = b3_erro_uni.u; - u32 b4_mask = b4_mask_uni.u; - u32 b4_patt = b4_patt_uni.u; - u32 b4_requ = b4_requ_uni.u; - u32 b4_err0 = b4_err0_uni.u; - u32 b4_err1 = b4_err1_uni.u; -#endif - -#define is_valid_seq_2(uni) ( \ - ((uni & b2_mask) == b2_patt) && \ - ((uni & b2_requ)) \ -) - -#define is_valid_seq_3(uni) ( \ - ((uni & b3_mask) == b3_patt) && \ - ((tmp = (uni & b3_requ))) && \ - ((tmp != b3_erro)) \ -) - -#define is_valid_seq_4(uni) ( \ - ((uni & b4_mask) == b4_patt) && \ - ((tmp = (uni & b4_requ))) && \ - ((tmp & b4_err0) == 0 || (tmp & b4_err1) == 0) \ -) - - /* 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; +static char *mut_write_root(const yyjson_mut_val *val, + usize estimated_val_num, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + char *buf, 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; } - switch (enc_table[*src]) { - case CHAR_ENC_CPY_1: { - *cur++ = *src++; - goto copy_ascii; - } - case CHAR_ENC_CPY_2: { - u16 v; -#if YYJSON_DISABLE_UTF8_VALIDATION - byte_copy_2(cur, src); -#else - v = byte_load_2(src); - if (unlikely(!is_valid_seq_2(v))) goto err_cpy; - byte_copy_2(cur, src); -#endif - cur += 2; - src += 2; - goto copy_utf8; - } - case CHAR_ENC_CPY_3: { - u32 v, tmp; -#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 - if (likely(src + 4 <= end)) { - v = byte_load_4(src); - if (unlikely(!is_valid_seq_3(v))) goto err_cpy; - byte_copy_4(cur, src); - } else { - v = byte_load_3(src); - if (unlikely(!is_valid_seq_3(v))) goto err_cpy; - byte_copy_4(cur, &v); - } -#endif - cur += 3; - src += 3; - goto copy_utf8; - } - case CHAR_ENC_CPY_4: { - u32 v, tmp; -#if YYJSON_DISABLE_UTF8_VALIDATION - byte_copy_4(cur, src); -#else - v = byte_load_4(src); - if (unlikely(!is_valid_seq_4(v))) 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, v; -#if !YYJSON_DISABLE_UTF8_VALIDATION - v = byte_load_2(src); - if (unlikely(!is_valid_seq_2(v))) 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_valid_seq_3(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_valid_seq_4(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; + + if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) { + return (char *)mut_write_root_single(root, flg, alc, buf, dat_len, err); + } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) { + return (char *)mut_write_root_pretty(root, estimated_val_num, + flg, alc, buf, dat_len, err); + } else { + return (char *)mut_write_root_minify(root, estimated_val_num, + flg, alc, buf, dat_len, err); } - -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; - -#undef is_valid_seq_2 -#undef is_valid_seq_3 -#undef is_valid_seq_4 } /*============================================================================== - * Writer Utilities + * MARK: - Mutable JSON Writer (Public) *============================================================================*/ -/** 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; +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 mut_write_root(val, 0, flg, alc_ptr, NULL, dat_len, err); } -/** 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); +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 { - byte_copy_8(cur, &v0); + root = NULL; + estimated_val_num = 0; } - return cur + 5 - val; + return mut_write_root(root, estimated_val_num, + flg, alc_ptr, NULL, dat_len, err); } -/** 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; +size_t yyjson_mut_val_write_buf(char *buf, size_t buf_len, + const yyjson_mut_val *val, + yyjson_write_flag flg, + yyjson_write_err *err) { + if (unlikely(!buf || !buf_len)) { + if (err) err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; + if (err) err->msg = "input buf or buf_len is invalid"; + return 0; + } else { + mut_write_root(val, 0, flg, &YYJSON_NULL_ALC, buf, &buf_len, err); + return buf_len; } - return cur; } -/** Write data to file pointer. */ -static bool write_dat_to_fp(FILE *fp, u8 *dat, usize len, +size_t yyjson_mut_write_buf(char *buf, size_t buf_len, + const yyjson_mut_doc *doc, + yyjson_write_flag flg, yyjson_write_err *err) { - if (fwrite(dat, len, 1, fp) != 1) { - err->msg = "file writing failed"; - err->code = YYJSON_WRITE_ERROR_FILE_WRITE; + yyjson_mut_val *root = doc ? doc->root : NULL; + return yyjson_mut_val_write_buf(buf, buf_len, root, flg, err); +} + +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + +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; } - return true; + + 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; } -/** 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, "file opening failed"); - } - if (fwrite(dat, len, 1, file) != 1) { - return_err(FILE_WRITE, "file writing failed"); - } - if (fclose(file) != 0) { - file = NULL; - return_err(FILE_WRITE, "file closing failed"); +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; } - return true; - -#undef return_err + + 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); +} -/*============================================================================== - * JSON Writer Implementation - *============================================================================*/ +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ -typedef struct yyjson_write_ctx { - usize tag; -} yyjson_write_ctx; +#undef has_flg +#undef has_allow +#endif /* YYJSON_DISABLE_WRITER */ -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_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - bool newline = has_write_flag(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_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(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_number(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, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#undef return_err -#undef check_str_len -#undef incr_len -} +#if !YYJSON_DISABLE_UTILS -/** 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_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - bool newline = has_write_flag(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_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; +/*============================================================================== + * 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++; + } } - *cur++ = is_key ? ':' : ','; - goto val_end; + *ptr = cur; + *len = (usize)(cur - hdr) - esc_num; + *esc = esc_num; + return hdr; } - if (val_type == YYJSON_TYPE_NUM) { - incr_len(FP_BUF_LEN); - cur = write_number(cur, val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - goto val_end; +} + +/** + 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 ((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 (*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 equal 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; } - if (val_type == YYJSON_TYPE_BOOL) { - incr_len(16); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - cur++; - goto val_end; +} + +/** + 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(const 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; } - if (val_type == YYJSON_TYPE_NULL) { - incr_len(16); - cur = write_null(cur); - cur++; - goto val_end; +} + +/** + 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(const 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; } - 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; + 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(const 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; } - 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; + 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(const 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; + } } - -doc_end: - if (newline) { - incr_len(2); - *(cur - 1) = '\n'; - cur++; + 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); } - *--cur = '\0'; - *dat_len = (usize)(cur - hdr); - memset(err, 0, sizeof(yyjson_write_err)); - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#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; \ +/* 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 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_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4; - bool newline = has_write_flag(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_string_noesc(cur, str_ptr, str_len); + +#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(const 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 { - cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; + val = NULL; } - *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_number(cur, val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - *cur++ = '\n'; - goto val_end; + if (!val) return_err_resolve(NULL, token - hdr); + if (ptr == end) return constcast(yyjson_val *)val; } - 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; +} + +yyjson_mut_val *unsafe_yyjson_mut_ptr_getx( + const 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 = constcast(yyjson_mut_val *)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 { - /* 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; + 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 constcast(yyjson_mut_val *)val; } - 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; +} + +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 */ } - 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; + + /* 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_syntax(false, ptr - 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); + } } - 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; + + /* 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 { - goto ctn_end; - } - -doc_end: - if (newline) { - incr_len(2); - *cur++ = '\n'; + /* 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; + } } - *cur = '\0'; - *dat_len = (usize)(cur - hdr); - memset(err, 0, sizeof(yyjson_write_err)); - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#undef return_err -#undef incr_len -#undef check_str_len -} -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 dummy_err; - usize dummy_dat_len; - yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; - yyjson_val *root = constcast(yyjson_val *)val; - - err = err ? err : &dummy_err; - dat_len = dat_len ? dat_len : &dummy_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); + /* 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; } -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); -} +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) { -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 dummy_err; - u8 *dat; - usize dat_len = 0; - yyjson_val *root = constcast(yyjson_val *)val; - bool suc; - - alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; - err = err ? err : &dummy_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_ptr, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_file(path, dat, dat_len, err); - alc_ptr->free(alc_ptr->ctx, dat); - return suc; -} + 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; -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 dummy_err; - u8 *dat; - usize dat_len = 0; - yyjson_val *root = constcast(yyjson_val *)val; - bool suc; - - alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; - err = err ? err : &dummy_err; - if (unlikely(!fp)) { - err->msg = "input fp is invalid"; - err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; - return false; + 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); } - - dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_fp(fp, dat, dat_len, err); - alc_ptr->free(alc_ptr->ctx, dat); - return suc; + ctx->old = cur_val; + return cur_val; } -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); -} +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) { -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); + 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 + /*============================================================================== - * Mutable JSON Writer Implementation + * MARK: - JSON Patch API (RFC 6902) (Public) *============================================================================*/ -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; -} +/* 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; -/** 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); +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; + } } -/** 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) { - +/* macros for yyjson_patch */ #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; \ + 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 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_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - bool newline = has_write_flag(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_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - *cur++ = is_key ? ':' : ','; - goto val_end; + +#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, + const yyjson_val *orig, + const 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 (val_type == YYJSON_TYPE_NUM) { - incr_len(FP_BUF_LEN); - cur = write_number(cur, (yyjson_val *)val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - goto val_end; + if (unlikely(!yyjson_is_arr(patch))) { + return_err(INVALID_PARAMETER, "input patch is not array"); } - 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; + 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`"); } } - 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 = "success"; - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#undef return_err -#undef incr_len -#undef check_str_len + return root; } -/** 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_write_flag(ESCAPE_UNICODE) != 0; - bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; - usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4; - bool newline = has_write_flag(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_string_noesc(cur, str_ptr, str_len); - } else { - cur = write_string(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; +yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, + const yyjson_mut_val *orig, + const 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 (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_number(cur, (yyjson_val *)val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - *cur++ = '\n'; - goto val_end; + if (unlikely(!yyjson_mut_is_arr(patch))) { + return_err(INVALID_PARAMETER, "input patch is not array"); } - 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; + root = yyjson_mut_val_mut_copy(doc, orig); + if (unlikely(!root)) return_err_copy(); + + /* iterate through the patch array */ + yyjson_mut_arr_iter_init(constcast(yyjson_mut_val *)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`"); } } - 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; + 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, + const yyjson_val *orig, + const 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); } - -doc_end: - if (newline) { - incr_len(2); - *cur++ = '\n'; + + builder = yyjson_mut_obj(doc); + if (unlikely(!builder)) return NULL; + + memset(&local_orig, 0, sizeof(local_orig)); + if (!yyjson_is_obj(orig)) { + local_orig.tag = builder->tag; + local_orig.uni = builder->uni; + orig = &local_orig; } - *cur = '\0'; - *dat_len = (usize)(cur - hdr); - err->code = YYJSON_WRITE_SUCCESS; - err->msg = "success"; - return hdr; - -fail_alloc: - return_err(MEMORY_ALLOCATION, "memory allocation failed"); -fail_type: - return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); -fail_num: - return_err(NAN_OR_INF, "nan or inf number is not allowed"); -fail_str: - return_err(INVALID_STRING, "invalid utf-8 encoding in string"); - -#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 dummy_err; - usize dummy_dat_len; - yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; - yyjson_mut_val *root = constcast(yyjson_mut_val *)val; - - err = err ? err : &dummy_err; - dat_len = dat_len ? dat_len : &dummy_dat_len; - - if (unlikely(!root)) { - *dat_len = 0; - err->msg = "input JSON is NULL"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - return NULL; + /* 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; + } + } } - - 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); + + /* 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; } -} -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); + return builder; } -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; +yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, + const yyjson_mut_val *orig, + const 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); } - 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 dummy_err; - u8 *dat; - usize dat_len = 0; - yyjson_mut_val *root = constcast(yyjson_mut_val *)val; - bool suc; - - alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; - err = err ? err : &dummy_err; - if (unlikely(!path || !*path)) { - err->msg = "input path is invalid"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - return false; + builder = yyjson_mut_obj(doc); + if (unlikely(!builder)) return NULL; + + memset(&local_orig, 0, sizeof(local_orig)); + if (!yyjson_mut_is_obj(orig)) { + local_orig.tag = builder->tag; + local_orig.uni = builder->uni; + orig = &local_orig; } - - dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_file(path, dat, dat_len, err); - alc_ptr->free(alc_ptr->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 dummy_err; - u8 *dat; - usize dat_len = 0; - yyjson_mut_val *root = constcast(yyjson_mut_val *)val; - bool suc; - - alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; - err = err ? err : &dummy_err; - if (unlikely(!fp)) { - err->msg = "input fp is invalid"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - return false; + /* 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; + } + } } - - dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_fp(fp, dat, dat_len, err); - alc_ptr->free(alc_ptr->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); -} + /* 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; + } -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); + return builder; } -#endif /* YYJSON_DISABLE_WRITER */ +#endif /* YYJSON_DISABLE_UTILS */ diff --git a/yyjson/yyjson.h b/yyjson/yyjson.h index 37c9bfd..83c8ca8 100644 --- a/yyjson/yyjson.h +++ b/yyjson/yyjson.h @@ -1,16 +1,16 @@ /*============================================================================== Copyright (c) 2020 YaoYuan - + 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 @@ -20,7 +20,7 @@ SOFTWARE. *============================================================================*/ -/** +/** @file yyjson.h @date 2019-03-09 @author YaoYuan @@ -32,154 +32,126 @@ /*============================================================================== - * Header Files + * MARK: - Compile-time Options *============================================================================*/ -#include -#include -#include -#include -#include -#include - +/* 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 +#define YYJSON_DISABLE_READER 0 +#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 +#define YYJSON_DISABLE_WRITER 0 +#endif -/*============================================================================== - * Compile-time Options - *============================================================================*/ +/* Define as 1 to disable JSON incremental reader at compile-time. + This disables functions with "incr" in their name. */ +#ifndef YYJSON_DISABLE_INCR_READER +#define YYJSON_DISABLE_INCR_READER 0 +#endif -/* - Define as 1 to disable JSON reader if JSON parsing is not required. - - This will disable these functions at compile-time: - - yyjson_read() - - yyjson_read_opts() - - yyjson_read_file() - - yyjson_read_number() - - yyjson_mut_read_number() - - This will reduce the binary size by about 60%. - */ -#ifndef YYJSON_DISABLE_READER +/* Define as 1 to disable the bounded-memory streaming (SAX) reader at + compile-time. This disables functions with "sax" in their name. */ +#ifndef YYJSON_DISABLE_SAX_READER +#define YYJSON_DISABLE_SAX_READER 0 #endif -/* - Define as 1 to disable JSON writer if JSON serialization is not required. - - This will disable these functions at compile-time: - - yyjson_write() - - yyjson_write_file() - - yyjson_write_opts() - - yyjson_val_write() - - yyjson_val_write_file() - - yyjson_val_write_opts() - - yyjson_mut_write() - - yyjson_mut_write_file() - - yyjson_mut_write_opts() - - yyjson_mut_val_write() - - yyjson_mut_val_write_file() - - yyjson_mut_val_write_opts() - - This will reduce the binary size by about 30%. - */ -#ifndef YYJSON_DISABLE_WRITER +/* Define as 1 to disable file/fp read and write APIs. */ +#ifndef YYJSON_DISABLE_FILE +#define YYJSON_DISABLE_FILE 0 #endif -/* - Define as 1 to disable JSON Pointer, JSON Patch and JSON Merge Patch supports. - - This will disable these functions at compile-time: - - yyjson_ptr_xxx() - - yyjson_mut_ptr_xxx() - - yyjson_doc_ptr_xxx() - - yyjson_mut_doc_ptr_xxx() - - yyjson_patch() - - yyjson_mut_patch() - - yyjson_merge_patch() - - yyjson_mut_merge_patch() - */ +/* Define as 1 to disable JSON Pointer, JSON Patch and JSON Merge Patch. + This disables functions with "ptr" or "patch" in their name. */ #ifndef YYJSON_DISABLE_UTILS +#define YYJSON_DISABLE_UTILS 0 #endif -/* - Define as 1 to disable the fast floating-point number conversion in yyjson, - and use libc's `strtod/snprintf` instead. - - This will reduce the binary size by about 30%, but significantly slow down the - floating-point read/write speed. - */ +/* 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 +#define YYJSON_DISABLE_FAST_FP_CONV 0 #endif -/* - Define as 1 to disable non-standard JSON support at compile-time: - - Reading and writing inf/nan literal, such as `NaN`, `-Infinity`. - - Single line and multiple line comments. - - Single trailing comma at the end of an object or array. - - Invalid unicode in string value. - - This will also invalidate these run-time options: - - YYJSON_READ_ALLOW_INF_AND_NAN - - YYJSON_READ_ALLOW_COMMENTS - - YYJSON_READ_ALLOW_TRAILING_COMMAS - - YYJSON_READ_ALLOW_INVALID_UNICODE - - YYJSON_WRITE_ALLOW_INF_AND_NAN - - YYJSON_WRITE_ALLOW_INVALID_UNICODE - - This will reduce the binary size by about 10%, and speed up the reading and - writing speed by about 2% to 6%. - */ +/* 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 +#define YYJSON_DISABLE_NON_STANDARD 0 #endif -/* - Define as 1 to disable UTF-8 validation at compile time. - - If all input strings are guaranteed to be valid UTF-8 encoding (for example, - some language's String object has already validated the encoding), using this - flag can avoid redundant UTF-8 validation in yyjson. - - This flag can speed up the reading and writing speed of non-ASCII encoded - strings by about 3% to 7%. - - Note: If this flag is used while passing in 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 be concatenated to the next value. - - When accessing `yyjson_mut_val` for serialization, the string ending may be - accessed out of bounds, causing a segmentation fault. - */ +/* 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 +#define YYJSON_DISABLE_UTF8_VALIDATION 0 #endif -/* - Define as 1 to indicate that the target architecture does not support unaligned - memory access. Please refer to the comments in the C file for details. - */ +/* Define as 1 to improve performance on architectures that do not support + unaligned memory access. + + Normally, this does not need to be set manually. */ #ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS +/* auto detected in yyjson.c */ +#endif + +/* Define to an integer to set a depth limit for containers (arrays/objects). */ +#ifndef YYJSON_READER_DEPTH_LIMIT +#define YYJSON_READER_DEPTH_LIMIT 0 +#endif + +/* Define as 1 to build without libc (stdlib, string, math, stdio). + Inline fallbacks for memcpy/memmove/memset/memcmp/strlen are provided. + Optional `YYJSON_FREESTANDING_HEADER` for custom replacements. + + `malloc`/`free` are unavailable; pass `yyjson_alc` per call or define + `YYJSON_CUSTOM_ALC`. Also disables file/fp APIs. Cannot be used with + `YYJSON_DISABLE_FAST_FP_CONV`. */ +#ifndef YYJSON_FREESTANDING +#define YYJSON_FREESTANDING 0 #endif -/* Define as 1 to export symbols when building this library as Windows DLL. */ +/* 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 Windows DLL. */ +/* Define as 1 to import symbols when using this library as a Windows DLL. */ #ifndef YYJSON_IMPORTS #endif -/* Define as 1 to include for compiler which doesn't support C99. */ +/* Define as 1 to include for compilers without C99 support. */ #ifndef YYJSON_HAS_STDINT_H #endif -/* Define as 1 to include for compiler which doesn't support C99. */ +/* Define as 1 to include for compilers without C99 support. */ #ifndef YYJSON_HAS_STDBOOL_H #endif /*============================================================================== - * Compiler Macros + * MARK: - Compiler Macros *============================================================================*/ /** compiler version (MSVC) */ @@ -207,8 +179,10 @@ #endif /** real gcc check */ -#if !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__ICC) && \ - defined(__GNUC__) +#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 @@ -325,6 +299,16 @@ # endif #endif +/** assume for compiler */ +#undef yyjson_assume +#if yyjson_has_builtin(__builtin_unreachable) || yyjson_gcc_available(4, 5, 0) +# define yyjson_assume(expr) ((expr) ? (void)0 : __builtin_unreachable()) +#elif YYJSON_MSC_VER >= 1300 +# define yyjson_assume(expr) __assume(expr) +#else +# define yyjson_assume(expr) ((void)0) +#endif + /** compile-time constant check for compiler */ #ifndef yyjson_constant_p # if yyjson_has_builtin(__builtin_constant_p) || (YYJSON_GCC_VER >= 3) @@ -372,6 +356,82 @@ # define yyjson_api_inline static yyjson_inline #endif +/** Used to cast away (remove) const qualifier. */ +#ifndef yyjson_constcast +# define yyjson_constcast(type) (type)(void *)(size_t)(const void *) +#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: - Header Files + *============================================================================*/ + +#include /* for size_t, NULL */ +#include /* for CHAR_BIT, *_MAX */ +#include /* for floating-point limit macros */ + +/** freestanding */ +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE +#include /* for FILE, fopen, fread, fwrite, sprintf */ +#endif +#if !YYJSON_FREESTANDING +#include /* for malloc, realloc, free, strtod */ +#include /* for memcpy, memmove, memset, memcmp, strlen */ +#include /* for HUGE_VAL, INFINITY, NAN (no libm required) */ +#elif defined(YYJSON_FREESTANDING_HEADER) +# include YYJSON_FREESTANDING_HEADER /* custom replacement for string.h */ +#else +# ifndef memcpy +# define memcpy(d,s,n) yyjson_memcpy(d,s,n) +# endif +# ifndef memmove +# define memmove(d,s,n) yyjson_memmove(d,s,n) +# endif +# ifndef memset +# define memset(d,v,n) yyjson_memset(d,v,n) +# endif +# ifndef memcmp +# define memcmp(a,b,n) yyjson_memcmp(a,b,n) +# endif +# ifndef strlen +# define strlen(s) yyjson_strlen(s) +# endif +yyjson_api_inline void *yyjson_memcpy(void *d, const void *s, size_t n) { + char *p = (char *)d; const char *q = (const char *)s; + while (n--) *p++ = *q++; return d; +} +yyjson_api_inline void *yyjson_memmove(void *d, const void *s, size_t n) { + char *p = (char *)d; const char *q = (const char *)s; + if (p == q || !n) return d; + if (p < q) { while (n--) *p++ = *q++; } + else { p += n; q += n; while (n--) *--p = *--q; } + return d; +} +yyjson_api_inline void *yyjson_memset(void *d, int v, size_t n) { + char *p = (char *)d, x = (char)v; + while (n--) *p++ = x; return d; +} +yyjson_api_inline int yyjson_memcmp(const void *a, const void *b, size_t n) { + const unsigned char *p = (const unsigned char *)a; + const unsigned char *q = (const unsigned char *)b; + while (n--) { if (*p != *q) return (int)(*p - *q); p++; q++; } return 0; +} +yyjson_api_inline size_t yyjson_strlen(const char *s) { + const char *p = s; while (*p) p++; return (size_t)(p - s); +} +#endif + /** stdint (C89 compatible) */ #if (defined(YYJSON_HAS_STDINT_H) && YYJSON_HAS_STDINT_H) || \ YYJSON_MSC_VER >= 1600 || YYJSON_STDC_VER >= 199901L || \ @@ -450,8 +510,8 @@ /** stdbool (C89 compatible) */ #if (defined(YYJSON_HAS_STDBOOL_H) && YYJSON_HAS_STDBOOL_H) || \ - (yyjson_has_include() && !defined(__STRICT_ANSI__)) || \ - YYJSON_MSC_VER >= 1800 || YYJSON_STDC_VER >= 199901L + YYJSON_MSC_VER >= 1800 || YYJSON_STDC_VER >= 199901L || \ + (yyjson_has_include() && !defined(__STRICT_ANSI__)) # include #elif !defined(__bool_true_false_are_defined) # define __bool_true_false_are_defined 1 @@ -478,22 +538,10 @@ # 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 - /*============================================================================== - * Compile Hint Begin + * MARK: - Compile Hint Begin *============================================================================*/ /* extern "C" begin */ @@ -506,12 +554,14 @@ extern "C" { # 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 +#elif YYJSON_IS_REAL_GCC +# if yyjson_gcc_available(4, 6, 0) +# pragma GCC diagnostic push +# endif +# if yyjson_gcc_available(4, 2, 0) +# pragma GCC diagnostic ignored "-Wunused-function" +# pragma GCC diagnostic ignored "-Wunused-parameter" # 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' */ @@ -520,23 +570,23 @@ extern "C" { /*============================================================================== - * Version + * MARK: - Version *============================================================================*/ /** The major version of yyjson. */ #define YYJSON_VERSION_MAJOR 0 /** The minor version of yyjson. */ -#define YYJSON_VERSION_MINOR 10 +#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 0x000A00 +#define YYJSON_VERSION_HEX 0x000C00 /** The version string of yyjson. */ -#define YYJSON_VERSION_STRING "0.10.0" +#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); @@ -544,7 +594,7 @@ yyjson_api uint32_t yyjson_version(void); /*============================================================================== - * JSON Types + * MARK: - JSON Types *============================================================================*/ /** Type of a JSON value (3 bit). */ @@ -580,7 +630,7 @@ typedef uint8_t yyjson_subtype; #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). */ +/** String that does 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. */ @@ -606,12 +656,12 @@ typedef uint8_t yyjson_subtype; /*============================================================================== - * Allocator + * MARK: - Allocator *============================================================================*/ /** A memory allocator. - + Typically you don't need to use it, unless you want to customize your own memory allocator. */ @@ -628,55 +678,55 @@ typedef struct yyjson_alc { /** A pool allocator uses fixed length pre-allocated memory. - - This allocator may be used to avoid malloc/realloc calls. The pre-allocated + + 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 + 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. - + document 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 `alc` is NULL, returns 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. + If `buf` is NULL, returns 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. + If `size` is less than 8 words (32/64 bytes on 32/64-bit OS), + returns false. @return true if the `alc` has been successfully initialized. - - @par Example + + @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}" + + 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); @@ -690,13 +740,13 @@ yyjson_api void yyjson_alc_dyn_free(yyjson_alc *alc); /*============================================================================== - * Text Locating + * 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. @@ -714,7 +764,7 @@ yyjson_api bool yyjson_locate_pos(const char *str, size_t len, size_t pos, /*============================================================================== - * JSON Structure + * MARK: - JSON Structure *============================================================================*/ /** @@ -727,7 +777,7 @@ 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. + document and cannot be freed alone. */ typedef struct yyjson_val yyjson_val; @@ -741,14 +791,14 @@ 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. + document and cannot be freed alone. */ typedef struct yyjson_mut_val yyjson_mut_val; /*============================================================================== - * JSON Reader API + * MARK: - JSON Reader API *============================================================================*/ /** Run-time options for JSON reader. */ @@ -762,51 +812,100 @@ typedef uint32_t yyjson_read_flag; - 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; +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 caller should hold the input data before freeing 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; +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; +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; +static const yyjson_read_flag YYJSON_READ_ALLOW_TRAILING_COMMAS = 1 << 2; -/** Allow C-style single line and multiple line comments (non-standard). */ -static const yyjson_read_flag YYJSON_READ_ALLOW_COMMENTS = 1 << 3; +/** Allow C-style single-line and multi-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; +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; +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; +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; +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 */ @@ -819,7 +918,7 @@ 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 failure occurs. */ +/** Memory allocation failed. */ static const yyjson_read_code YYJSON_READ_ERROR_MEMORY_ALLOCATION = 2; /** Input JSON string is empty. */ @@ -828,7 +927,7 @@ 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 ending, such as `[123`. */ +/** 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]`. */ @@ -837,7 +936,7 @@ 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, such as unclosed multi-line comment. */ +/** 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`. */ @@ -855,6 +954,21 @@ 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; + +/** Read depth limit exceeded. */ +static const yyjson_read_code YYJSON_READ_ERROR_DEPTH = 15; + +/** The streaming (SAX) source callback reported an I/O error. */ +static const yyjson_read_code YYJSON_READ_ERROR_SOURCE = 16; + +/** A single token was larger than the streaming (SAX) reader's window. */ +static const yyjson_read_code YYJSON_READ_ERROR_TOKEN_TOO_LARGE = 17; + +/** Parsing was aborted because a SAX handler callback returned false. */ +static const yyjson_read_code YYJSON_READ_ERROR_ABORTED = 18; + /** Error information for JSON reader. */ typedef struct yyjson_read_err { /** Error code, see `yyjson_read_code` for all possible values. */ @@ -871,18 +985,18 @@ typedef struct yyjson_read_err { /** 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. + If `dat` is NULL, returns 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 + can pass a `const char *` string and cast 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. + If `len` is 0, returns 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. @@ -898,15 +1012,18 @@ yyjson_api yyjson_doc *yyjson_read_opts(char *dat, const yyjson_alc *alc, yyjson_read_err *err); +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + /** 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. - If this path is NULL or invalid, the function will fail and return NULL. + This should be a null-terminated string using the system's native encoding. + If `path` is NULL or invalid, returns 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. @@ -915,7 +1032,7 @@ yyjson_api yyjson_doc *yyjson_read_opts(char *dat, 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, @@ -925,10 +1042,10 @@ yyjson_api yyjson_doc *yyjson_read_file(const char *path, /** 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. + If `fp` is NULL or invalid, returns 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. @@ -937,7 +1054,7 @@ yyjson_api yyjson_doc *yyjson_read_file(const char *path, 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, @@ -945,15 +1062,17 @@ yyjson_api yyjson_doc *yyjson_read_fp(FILE *fp, const yyjson_alc *alc, yyjson_read_err *err); +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + /** 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. + If `dat` is NULL, returns NULL. @param len The length of JSON data in bytes. - If this parameter is 0, the function will fail and return NULL. + If `len` is 0, returns 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. @@ -967,39 +1086,244 @@ yyjson_api_inline yyjson_doc *yyjson_read(const char *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 `buf` is NULL, returns NULL. + @param buf_len The length of the JSON data in `buf`. + If using `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 `len` is 0, returns 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 */ + + + +/*============================================================================== + * JSON Streaming (SAX) Reader API + *============================================================================*/ + +#if !defined(YYJSON_DISABLE_SAX_READER) || !YYJSON_DISABLE_SAX_READER + +/** + Sentinel returned by a `yyjson_sax_source` callback to signal a read error + (as opposed to `0`, which means a clean end-of-input). + */ +#define YYJSON_SAX_SOURCE_ERROR ((size_t)~(size_t)0) + +/** + A pull-based byte source for the streaming reader. + + The reader calls this to fill its window. Copy up to `len` bytes into `buf` + and return the number of bytes written. Return `0` to signal a clean + end-of-input, or `YYJSON_SAX_SOURCE_ERROR` to signal an I/O error (which is + reported to the caller as `YYJSON_READ_ERROR_SOURCE`). + + @param ctx The opaque context passed to `yyjson_sax_read()`. + @param buf The destination buffer to copy bytes into. + @param len The maximum number of bytes to copy. + @return The number of bytes copied, `0` for end-of-input, or + `YYJSON_SAX_SOURCE_ERROR` on error. + */ +typedef size_t (*yyjson_sax_source)(void *ctx, void *buf, size_t len); + +/** + Streaming (SAX) event handler. + + Each callback is invoked as the corresponding JSON token is parsed and returns + `true` to continue parsing or `false` to abort (reported to the caller as + `YYJSON_READ_ERROR_ABORTED`). Any callback may be `NULL`, in which case that + event is ignored with no overhead. + + The `str`/`len` passed to `str` and `key` point directly into the reader's + internal window and are only valid for the duration of the callback. If you + need to retain the data, copy it. The pointer is not null-terminated; always + use `len`. + + The `num` callback receives a temporary `yyjson_val` valid only for the + duration of the call. Use the `yyjson_get_*` accessors on it. With + `YYJSON_READ_NUMBER_AS_RAW` / `YYJSON_READ_BIGNUM_AS_RAW` it may be a RAW + value; use `yyjson_get_raw()` + `yyjson_get_len()` (it is not + null-terminated). + */ +typedef struct yyjson_sax_handler { + /** Called at the start of an object (`{`). */ + bool (*obj_begin)(void *ctx); + /** Called at the end of an object (`}`). `count` is the member count. */ + bool (*obj_end)(void *ctx, size_t count); + /** Called at the start of an array (`[`). */ + bool (*arr_begin)(void *ctx); + /** Called at the end of an array (`]`). `count` is the element count. */ + bool (*arr_end)(void *ctx, size_t count); + /** Called for an object member key (a string). */ + bool (*key)(void *ctx, const char *str, size_t len); + /** Called for a string value. */ + bool (*str)(void *ctx, const char *str, size_t len); + /** Called for a number value (see note above about RAW numbers). */ + bool (*num)(void *ctx, const yyjson_val *num); + /** Called for a boolean value. */ + bool (*bool_val)(void *ctx, bool value); + /** Called for a `null` value. */ + bool (*null_val)(void *ctx); +} yyjson_sax_handler; + +/** Options for the streaming (SAX) reader. */ +typedef struct yyjson_sax_opts { + /** Size in bytes of the sliding window buffer. This is the hard cap on peak + memory (plus O(depth) for the container stack); it must also be large + enough to hold the largest single string/number token in the input. + Pass `0` for a default (currently 256 KiB). Values below a small floor + are raised to it. */ + size_t window; + /** Maximum container nesting depth before `YYJSON_READ_ERROR_DEPTH`. + Pass `0` for a default (currently 1024). */ + size_t max_depth; +} yyjson_sax_opts; + +/** + Parse JSON from a pull-based byte source using bounded memory, invoking the + handler for each token (a SAX-style parser). + + Peak memory is bounded by the window size plus O(nesting depth), independent + of the total input size, so this can process inputs far larger than RAM. The + one constraint is that no single string or number token may exceed the window + size; such an input fails with `YYJSON_READ_ERROR_TOKEN_TOO_LARGE`. + + Only standard JSON is supported. `YYJSON_READ_NUMBER_AS_RAW`, + `YYJSON_READ_BIGNUM_AS_RAW` and `YYJSON_READ_STOP_WHEN_DONE` are honored; other + non-standard flags are ignored. + + @param source The byte source callback. Must not be NULL. + @param source_ctx An opaque context passed to `source`. + @param handler The event handler. Must not be NULL (individual callbacks may + be NULL). + @param handler_ctx An opaque context passed to each handler callback. + @param flg The JSON read options. + @param opts Window/depth options, or NULL for defaults. + @param alc The allocator used for the window and stack, or NULL for the + default allocator. + @param err A pointer to receive error information, or NULL. + @return true on success, false on error (see `err`). + */ +yyjson_api bool yyjson_sax_read(yyjson_sax_source source, void *source_ctx, + const yyjson_sax_handler *handler, + void *handler_ctx, + yyjson_read_flag flg, + const yyjson_sax_opts *opts, + const yyjson_alc *alc, + yyjson_read_err *err); + +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + +/** + Like `yyjson_sax_read()`, but reads from a `FILE *` stream. + + @param fp An open `FILE *` positioned at the JSON data. Not closed by this + function. + @param handler The event handler (must not be NULL). + @param handler_ctx An opaque context passed to each handler callback. + @param flg The JSON read options. + @param opts Window/depth options, or NULL for defaults. + @param alc The allocator, or NULL for the default allocator. + @param err A pointer to receive error information, or NULL. + @return true on success, false on error (see `err`). + */ +yyjson_api bool yyjson_sax_read_fp(FILE *fp, + const yyjson_sax_handler *handler, + void *handler_ctx, + yyjson_read_flag flg, + const yyjson_sax_opts *opts, + const yyjson_alc *alc, + yyjson_read_err *err); + +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + +#endif /* YYJSON_DISABLE_SAX_READER */ + /** - Returns the size of maximum memory usage to read a JSON data. - + Returns the maximum memory usage to read a JSON document. + 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. - - @par Example + + @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 + + // no more malloc() 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() @@ -1011,9 +1335,9 @@ yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len, 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. + 3. yyjson uses 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); @@ -1030,9 +1354,9 @@ yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len, 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. + If `dat` is NULL, returns NULL. @param val The output value where result is stored. - If this parameter is NULL, the function will fail and return NULL. + If `val` is NULL, returns 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. @@ -1051,27 +1375,7 @@ yyjson_api const char *yyjson_read_number(const char *dat, const yyjson_alc *alc, yyjson_read_err *err); -/** - 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. - */ +/** 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, @@ -1085,7 +1389,7 @@ yyjson_api_inline const char *yyjson_mut_read_number(const char *dat, /*============================================================================== - * JSON Writer API + * MARK: - JSON Writer API *============================================================================*/ /** Run-time options for JSON writer. */ @@ -1129,6 +1433,10 @@ static const yyjson_write_flag YYJSON_WRITE_PRETTY_TWO_SPACES = 1 << 6; This can be helpful for text editors or NDJSON. */ static const yyjson_write_flag YYJSON_WRITE_NEWLINE_AT_END = 1 << 7; +/** Use lowercase hex digits in `\uXXXX` escape sequences instead of the default + uppercase. Only effective when `YYJSON_WRITE_ESCAPE_UNICODE` is also set. */ +static const yyjson_write_flag YYJSON_WRITE_LOWERCASE_HEX = 1 << 8; + /** The highest 8 bits of `yyjson_write_flag` and real number value's `tag` @@ -1162,7 +1470,7 @@ 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. */ +/** Memory allocation failed. */ static const yyjson_write_code YYJSON_WRITE_ERROR_MEMORY_ALLOCATION = 2; /** Invalid value type in JSON document. */ @@ -1193,17 +1501,17 @@ typedef struct yyjson_write_err { #if !defined(YYJSON_DISABLE_WRITER) || !YYJSON_DISABLE_WRITER /*============================================================================== - * JSON Document Writer API + * 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. + If `doc` is NULL or has no root, returns 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. @@ -1222,18 +1530,21 @@ yyjson_api char *yyjson_write_opts(const yyjson_doc *doc, size_t *len, yyjson_write_err *err); +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + /** 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. - 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. + This should be a null-terminated string using the system's native encoding. + If `path` is NULL or invalid, returns false. + If the file is not empty, its content is discarded. @param doc The JSON document. - If this doc is NULL or has no root, the function will fail and return false. + If `doc` is NULL or has no root, returns 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. @@ -1241,7 +1552,7 @@ yyjson_api char *yyjson_write_opts(const yyjson_doc *doc, @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, @@ -1252,12 +1563,12 @@ yyjson_api bool yyjson_write_file(const char *path, /** 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. + If `fp` is NULL or invalid, returns false. @param doc The JSON document. - If this doc is NULL or has no root, the function will fail and return false. + If `doc` is NULL or has no root, returns 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. @@ -1265,7 +1576,7 @@ yyjson_api bool yyjson_write_file(const char *path, @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, @@ -1274,13 +1585,39 @@ yyjson_api bool yyjson_write_fp(FILE *fp, const yyjson_alc *alc, yyjson_write_err *err); +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + +/** + Write a document into a buffer. + + This function does not allocate memory, but the buffer must be larger than the + final JSON size to allow temporary space. See `API.md` for details. + + @param buf The output buffer. + If `buf` is NULL, returns 0. + @param buf_len The buffer length. + If `buf_len` is too small, returns 0. + @param doc The JSON document. + If `doc` is NULL or has no root, returns 0. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return The number of bytes written (excluding the null terminator), + or 0 on failure. + */ +yyjson_api size_t yyjson_write_buf(char *buf, size_t buf_len, + const yyjson_doc *doc, + yyjson_write_flag flg, + 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. + If `doc` is NULL or has no root, returns 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 @@ -1299,13 +1636,13 @@ yyjson_api_inline char *yyjson_write(const yyjson_doc *doc, /** 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. + If `doc` is NULL or has no root, returns 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. @@ -1324,19 +1661,22 @@ yyjson_api char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, size_t *len, yyjson_write_err *err); +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + /** 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. - 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. + This should be a null-terminated string using the system's native encoding. + If `path` is NULL or invalid, returns false. + If the file is not empty, its content is discarded. @param doc The mutable JSON document. - If this doc is NULL or has no root, the function will fail and return false. + If `doc` is NULL or has no root, returns 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. @@ -1344,7 +1684,7 @@ yyjson_api char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, @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, @@ -1355,12 +1695,12 @@ yyjson_api bool yyjson_mut_write_file(const char *path, /** 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. + If `fp` is NULL or invalid, returns false. @param doc The mutable JSON document. - If this doc is NULL or has no root, the function will fail and return false. + If `doc` is NULL or has no root, returns 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. @@ -1368,7 +1708,7 @@ yyjson_api bool yyjson_mut_write_file(const char *path, @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, @@ -1377,14 +1717,40 @@ yyjson_api bool yyjson_mut_write_fp(FILE *fp, const yyjson_alc *alc, yyjson_write_err *err); +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + +/** + Write a document into a buffer. + + This function does not allocate memory, but the buffer must be larger than the + final JSON size to allow temporary space. See `API.md` for details. + + @param buf The output buffer. + If `buf` is NULL, returns 0. + @param buf_len The buffer length. + If `buf_len` is too small, returns 0. + @param doc The JSON document. + If `doc` is NULL or has no root, returns 0. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return The number of bytes written (excluding the null terminator), + or 0 on failure. + */ +yyjson_api size_t yyjson_mut_write_buf(char *buf, size_t buf_len, + const yyjson_mut_doc *doc, + yyjson_write_flag flg, + 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. + If `doc` is NULL or has no root, returns 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 @@ -1402,17 +1768,17 @@ yyjson_api_inline char *yyjson_mut_write(const yyjson_mut_doc *doc, /*============================================================================== - * JSON Value Writer API + * 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. + If `val` is NULL, returns 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. @@ -1431,18 +1797,21 @@ yyjson_api char *yyjson_val_write_opts(const yyjson_val *val, size_t *len, yyjson_write_err *err); +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + /** 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. - 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. + This should be a null-terminated string using the system's native encoding. + If `path` is NULL or invalid, returns false. + If the file is not empty, its content is discarded. @param val The JSON root value. - If this parameter is NULL, the function will fail and return NULL. + If `val` is NULL, returns 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. @@ -1450,7 +1819,7 @@ yyjson_api char *yyjson_val_write_opts(const yyjson_val *val, @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, @@ -1461,12 +1830,12 @@ yyjson_api bool yyjson_val_write_file(const char *path, /** 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. + If `fp` is NULL or invalid, returns false. @param val The JSON root value. - If this parameter is NULL, the function will fail and return NULL. + If `val` is NULL, returns 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. @@ -1474,7 +1843,7 @@ yyjson_api bool yyjson_val_write_file(const char *path, @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, @@ -1483,13 +1852,39 @@ yyjson_api bool yyjson_val_write_fp(FILE *fp, const yyjson_alc *alc, yyjson_write_err *err); +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + +/** + Write a value into a buffer. + + This function does not allocate memory, but the buffer must be larger than the + final JSON size to allow temporary space. See `API.md` for details. + + @param buf The output buffer. + If `buf` is NULL, returns 0. + @param buf_len The buffer length. + If `buf_len` is too small, returns 0. + @param val The JSON root value. + If `val` is NULL, returns 0. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return The number of bytes written (excluding the null terminator), + or 0 on failure. + */ +yyjson_api size_t yyjson_val_write_buf(char *buf, size_t buf_len, + const yyjson_val *val, + yyjson_write_flag flg, + 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. + If `val` is NULL, returns 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 @@ -1506,13 +1901,13 @@ yyjson_api_inline char *yyjson_val_write(const yyjson_val *val, /** 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. + If `val` is NULL, returns 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. @@ -1521,7 +1916,7 @@ yyjson_api_inline char *yyjson_val_write(const yyjson_val *val, 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. + @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(). */ @@ -1531,19 +1926,22 @@ yyjson_api char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, size_t *len, yyjson_write_err *err); +#if !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE + /** 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. - 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. + This should be a null-terminated string using the system's native encoding. + If `path` is NULL or invalid, returns false. + If the file is not empty, its content is discarded. @param val The mutable JSON root value. - If this parameter is NULL, the function will fail and return NULL. + If `val` is NULL, returns 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. @@ -1551,7 +1949,7 @@ yyjson_api char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, @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, @@ -1561,13 +1959,13 @@ yyjson_api bool yyjson_mut_val_write_file(const char *path, yyjson_write_err *err); /** - Write a value to JSON file with options. - + 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. + If `fp` is NULL or invalid, returns false. @param val The mutable JSON root value. - If this parameter is NULL, the function will fail and return NULL. + If `val` is NULL, returns 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. @@ -1575,7 +1973,7 @@ yyjson_api bool yyjson_mut_val_write_file(const char *path, @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, @@ -1584,14 +1982,40 @@ yyjson_api bool yyjson_mut_val_write_fp(FILE *fp, const yyjson_alc *alc, yyjson_write_err *err); +#endif /* !YYJSON_FREESTANDING && !YYJSON_DISABLE_FILE */ + +/** + Write a value into a buffer. + + This function does not allocate memory, but the buffer must be larger than the + final JSON size to allow temporary space. See `API.md` for details. + + @param buf The output buffer. + If `buf` is NULL, returns 0. + @param buf_len The buffer length. + If `buf_len` is too small, returns 0. + @param val The JSON root value. + If `val` is NULL, returns 0. + @param flg The JSON write options. + Multiple options can be combined with `|` operator. 0 means no options. + @param err A pointer to receive error information. + Pass NULL if you don't need error information. + @return The number of bytes written (excluding the null terminator), + or 0 on failure. + */ +yyjson_api size_t yyjson_mut_val_write_buf(char *buf, size_t buf_len, + const yyjson_mut_val *val, + yyjson_write_flag flg, + 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. + If `val` is NULL, returns 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 @@ -1606,27 +2030,55 @@ yyjson_api_inline char *yyjson_mut_val_write(const yyjson_mut_val *val, 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 `val` is invalid, returns NULL. + @param buf A buffer to store the resulting null-terminated string. + If `buf` is NULL, returns 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 */ /*============================================================================== - * JSON Document API + * 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); +yyjson_api_inline yyjson_val *yyjson_doc_get_root(const 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); +yyjson_api_inline size_t yyjson_doc_get_read_size(const 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); +yyjson_api_inline size_t yyjson_doc_get_val_count(const 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 @@ -1636,206 +2088,208 @@ yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc); /*============================================================================== - * JSON Value Type API + * 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); +yyjson_api_inline bool yyjson_is_raw(const 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); +yyjson_api_inline bool yyjson_is_null(const 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); +yyjson_api_inline bool yyjson_is_true(const 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); +yyjson_api_inline bool yyjson_is_false(const 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); +yyjson_api_inline bool yyjson_is_bool(const 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); +yyjson_api_inline bool yyjson_is_uint(const 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); +yyjson_api_inline bool yyjson_is_sint(const 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); +yyjson_api_inline bool yyjson_is_int(const 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); +yyjson_api_inline bool yyjson_is_real(const 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); +yyjson_api_inline bool yyjson_is_num(const 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); +yyjson_api_inline bool yyjson_is_str(const 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); +yyjson_api_inline bool yyjson_is_arr(const 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); +yyjson_api_inline bool yyjson_is_obj(const 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); +yyjson_api_inline bool yyjson_is_ctn(const yyjson_val *val); /*============================================================================== - * JSON Value Content API + * 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); +yyjson_api_inline yyjson_type yyjson_get_type(const 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); +yyjson_api_inline yyjson_subtype yyjson_get_subtype(const 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); +yyjson_api_inline uint8_t yyjson_get_tag(const 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); +yyjson_api_inline const char *yyjson_get_type_desc(const 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); +yyjson_api_inline const char *yyjson_get_raw(const yyjson_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_get_bool(yyjson_val *val); + Returns false if `val` is NULL or type is not bool. */ +yyjson_api_inline bool yyjson_get_bool(const yyjson_val *val); -/** Returns the content and cast to uint64_t. +/** Returns the content 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); +yyjson_api_inline uint64_t yyjson_get_uint(const yyjson_val *val); -/** Returns the content and cast to int64_t. +/** Returns the content 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); +yyjson_api_inline int64_t yyjson_get_sint(const yyjson_val *val); -/** Returns the content and cast to int. +/** Returns the content cast to int (may overflow). Returns 0 if `val` is NULL or type is not integer(sint/uint). */ -yyjson_api_inline int yyjson_get_int(yyjson_val *val); +yyjson_api_inline int yyjson_get_int(const 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); +yyjson_api_inline double yyjson_get_real(const yyjson_val *val); -/** Returns the content and typecast to `double` if the value is number. +/** Returns the content cast to `double` if the value is a 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); +yyjson_api_inline double yyjson_get_num(const 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); +yyjson_api_inline const char *yyjson_get_str(const yyjson_val *val); -/** Returns the content length (string length, array size, object size. +/** 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); +yyjson_api_inline size_t yyjson_get_len(const 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 equal to a string. + Returns false if `val` is NULL or type is not string. */ +yyjson_api_inline bool yyjson_equals_str(const yyjson_val *val, + const char *str); -/** Returns whether the JSON value is equals to a string. +/** Returns whether the JSON value is equal 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 false if `val` is NULL or type is not string. */ +yyjson_api_inline bool yyjson_equals_strn(const yyjson_val *val, + const char *str, size_t len); /** Returns whether two JSON values are equal (deep compare). - Returns false if input is NULL. + Returns false if `lhs` or `rhs` 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); +yyjson_api_inline bool yyjson_equals(const yyjson_val *lhs, + const yyjson_val *rhs); /** Set the value to raw. - Returns false if input is NULL or `val` is object or array. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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); +yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int64_t num); /** Set the value to float. - Returns false if input is NULL or `val` is object or array. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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. + Returns false if `val` is NULL or 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); @@ -1843,7 +2297,7 @@ yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, /** 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. + Returns false if `val` is NULL or 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); @@ -1851,39 +2305,39 @@ yyjson_api_inline bool yyjson_set_str_noesc(yyjson_val *val, bool noesc); /*============================================================================== - * JSON Array API + * 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); +yyjson_api_inline size_t yyjson_arr_size(const 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); +yyjson_api_inline yyjson_val *yyjson_arr_get(const 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); +yyjson_api_inline yyjson_val *yyjson_arr_get_first(const 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); +yyjson_api_inline yyjson_val *yyjson_arr_get_last(const yyjson_val *arr); /*============================================================================== - * JSON Array Iterator API + * MARK: - JSON Array Iterator API *============================================================================*/ /** A JSON array iterator. - - @par Example + + @b Example @code yyjson_val *val; yyjson_arr_iter iter = yyjson_arr_iter_with(arr); @@ -1900,46 +2354,46 @@ typedef struct 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. + If `arr` is NULL or not an array, `iter` is cleared. @param iter The iterator to be initialized. - If this parameter is NULL, the function will fail and return false. + If `iter` is NULL, returns 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_api_inline bool yyjson_arr_iter_init(const yyjson_val *arr, yyjson_arr_iter *iter); /** - Create an iterator with an array , same as `yyjson_arr_iter_init()`. - + 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. + If `arr` is NULL or not an array, returns an empty iterator. @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); +yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(const yyjson_val *arr); /** Returns whether the iteration has more elements. - If `iter` is NULL, this function will return false. + If `iter` is NULL, returns 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. + If `iter` is NULL, returns 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. - - @par Example + + @b Example @code size_t idx, max; yyjson_val *val; @@ -1959,43 +2413,44 @@ yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter); /*============================================================================== - * JSON Object API + * 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); +yyjson_api_inline size_t yyjson_obj_size(const 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); +yyjson_api_inline yyjson_val *yyjson_obj_get(const 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); +yyjson_api_inline yyjson_val *yyjson_obj_getn(const yyjson_val *obj, + const char *key, size_t key_len); /*============================================================================== - * JSON Object Iterator API + * MARK: - JSON Object Iterator API *============================================================================*/ /** A JSON object iterator. - - @par Example + + @b Example @code yyjson_val *key, *val; yyjson_obj_iter iter = yyjson_obj_iter_with(obj); @@ -2004,7 +2459,7 @@ yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, const char *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 @@ -2025,61 +2480,61 @@ typedef struct 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. + If `obj` is NULL or not an object, `iter` is cleared. @param iter The iterator to be initialized. - If this parameter is NULL, the function will fail and return false. + If `iter` is NULL, returns 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_api_inline bool yyjson_obj_iter_init(const 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. + If `obj` is NULL or not an object, returns an empty iterator. @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); +yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(const yyjson_val *obj); /** Returns whether the iteration has more elements. - If `iter` is NULL, this function will return false. + If `iter` is NULL, returns 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. + If `iter` is NULL, returns 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. + If `iter` is NULL, returns 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. - + NULL if the key is not found or arguments are 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, @@ -2093,13 +2548,13 @@ yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, 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. + @param key_len 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. - + NULL if the key is not found or arguments are 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, @@ -2109,8 +2564,8 @@ yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, /** Macro for iterating over an object. It works like iterator, but with a more intuitive API. - - @par Example + + @b Example @code size_t idx, max; yyjson_val *key, *val; @@ -2132,7 +2587,7 @@ yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, /*============================================================================== - * Mutable JSON Document API + * MARK: - Mutable JSON Document API *============================================================================*/ /** Returns the root value of this JSON document. @@ -2148,11 +2603,11 @@ yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, 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. @@ -2164,11 +2619,11 @@ yyjson_api bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, 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. @@ -2189,31 +2644,31 @@ yyjson_api yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc); 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, +yyjson_api yyjson_mut_doc *yyjson_doc_mut_copy(const 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, +yyjson_api yyjson_mut_doc *yyjson_mut_doc_mut_copy(const 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. + The memory is managed by the mutable document. @note `imut_val` -> `mut_val`. */ yyjson_api yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *doc, - yyjson_val *val); + const 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. + The memory is managed by the 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); + const 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. @@ -2221,7 +2676,7 @@ yyjson_api yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, @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, +yyjson_api yyjson_doc *yyjson_mut_doc_imut_copy(const yyjson_mut_doc *doc, const yyjson_alc *alc); /** Copies and returns a new immutable document from input, @@ -2230,217 +2685,219 @@ yyjson_api yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *doc, @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, +yyjson_api yyjson_doc *yyjson_mut_val_imut_copy(const yyjson_mut_val *val, const yyjson_alc *alc); /*============================================================================== - * Mutable JSON Value Type API + * 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); +yyjson_api_inline bool yyjson_mut_is_raw(const 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); +yyjson_api_inline bool yyjson_mut_is_null(const 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); +yyjson_api_inline bool yyjson_mut_is_true(const 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); +yyjson_api_inline bool yyjson_mut_is_false(const 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); +yyjson_api_inline bool yyjson_mut_is_bool(const 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); +yyjson_api_inline bool yyjson_mut_is_uint(const 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); +yyjson_api_inline bool yyjson_mut_is_sint(const 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); +yyjson_api_inline bool yyjson_mut_is_int(const 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); +yyjson_api_inline bool yyjson_mut_is_real(const 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); +yyjson_api_inline bool yyjson_mut_is_num(const 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); +yyjson_api_inline bool yyjson_mut_is_str(const 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); +yyjson_api_inline bool yyjson_mut_is_arr(const 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); +yyjson_api_inline bool yyjson_mut_is_obj(const 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); +yyjson_api_inline bool yyjson_mut_is_ctn(const yyjson_mut_val *val); /*============================================================================== - * Mutable JSON Value Content API + * 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); +yyjson_api_inline yyjson_type yyjson_mut_get_type(const 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); +yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype( + const 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); +yyjson_api_inline uint8_t yyjson_mut_get_tag(const 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); +yyjson_api_inline const char *yyjson_mut_get_type_desc( + const 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); +yyjson_api_inline const char *yyjson_mut_get_raw(const 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); +yyjson_api_inline bool yyjson_mut_get_bool(const 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); +yyjson_api_inline uint64_t yyjson_mut_get_uint(const 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); +yyjson_api_inline int64_t yyjson_mut_get_sint(const 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); +yyjson_api_inline int yyjson_mut_get_int(const 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); +yyjson_api_inline double yyjson_mut_get_real(const yyjson_mut_val *val); -/** Returns the content and typecast to `double` if the value is number. +/** Returns the content cast to `double` if the value is a 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); +yyjson_api_inline double yyjson_mut_get_num(const 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); +yyjson_api_inline const char *yyjson_mut_get_str(const yyjson_mut_val *val); -/** Returns the content length (string length, array size, object size. +/** 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); +yyjson_api_inline size_t yyjson_mut_get_len(const yyjson_mut_val *val); -/** Returns whether the JSON value is equals to a string. +/** Returns whether the JSON value is equal 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, + Returns false if `val` is NULL or type is not string. */ +yyjson_api_inline bool yyjson_mut_equals_str(const yyjson_mut_val *val, const char *str); -/** Returns whether the JSON value is equals to a string. +/** Returns whether the JSON value is equal 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, + Returns false if `val` is NULL or type is not string. */ +yyjson_api_inline bool yyjson_mut_equals_strn(const 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. + Returns false if `lhs` or `rhs` 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); +yyjson_api_inline bool yyjson_mut_equals(const yyjson_mut_val *lhs, + const yyjson_mut_val *rhs); /** Set the value to raw. - Returns false if input is NULL. + Returns false if `val` 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. + Returns false if `val` 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. + Returns false if `val` 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. + Returns false if `val` 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. + Returns false if `val` 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. + Returns false if `val` 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); +yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int64_t num); /** Set the value to float. - Returns false if input is NULL. + Returns false if `val` 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. + Returns false if `val` 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. + Returns false if `val` 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. + Returns false if `val` is NULL or is not real type. @see YYJSON_WRITE_FP_TO_FIXED flag. - @warning This will modify the `immutable` value, use with caution. */ + @warning This will modify the `mutable` 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. + Returns false if `val` is NULL or 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, + @warning This will modify the `mutable` 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. + Returns false if `val` 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. + Returns false if `val` 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); @@ -2448,31 +2905,31 @@ yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, /** 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. + Returns false if `val` is NULL or is not string. @see YYJSON_SUBTYPE_NOESC subtype. - @warning This will modify the `immutable` value, use with caution. */ + @warning This will modify the `mutable` 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. + Returns false if `val` 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. +/** Set the value to object. + Returns false if `val` 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); /*============================================================================== - * Mutable JSON Value Creation API + * 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, @@ -2480,7 +2937,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, /** 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, @@ -2568,40 +3025,42 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, /*============================================================================== - * Mutable JSON Array API + * 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); +yyjson_api_inline size_t yyjson_mut_arr_size(const 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(const 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); +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first( + const 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); +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last( + const yyjson_mut_val *arr); /*============================================================================== - * Mutable JSON Array Iterator API + * 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. - - @par Example + + @b Example @code yyjson_mut_val *val; yyjson_mut_arr_iter iter = yyjson_mut_arr_iter_with(arr); @@ -2623,25 +3082,25 @@ typedef struct 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. + If `arr` is NULL or not an array, `iter` is cleared. @param iter The iterator to be initialized. - If this parameter is NULL, the function will fail and return false. + If `iter` is NULL, returns 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()`. - + 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. + If `arr` is NULL or not an array, returns an empty iterator. @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( @@ -2649,21 +3108,21 @@ yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( /** Returns whether the iteration has more elements. - If `iter` is NULL, this function will return false. + If `iter` is NULL, returns 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. + If `iter` is NULL, returns 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. + If `iter` is NULL, returns NULL. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( yyjson_mut_arr_iter *iter); @@ -2671,10 +3130,10 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( /** 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. - - @par Example + + @b Example @code size_t idx, max; yyjson_mut_val *val; @@ -2694,26 +3153,26 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( /*============================================================================== - * Mutable JSON Array Creation API + * 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. + @return The new array. NULL if `doc` is NULL or allocation fails. */ 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The value count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const bool vals[3] = { true, false, true }; yyjson_mut_val *arr = yyjson_mut_arr_with_bool(doc, vals, 3); @@ -2724,14 +3183,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const int64_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); @@ -2742,14 +3201,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const uint64_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint(doc, vals, 3); @@ -2760,14 +3219,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @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); @@ -2778,14 +3237,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const int8_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint8(doc, vals, 3); @@ -2796,14 +3255,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const int16_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint16(doc, vals, 3); @@ -2814,14 +3273,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const int32_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint32(doc, vals, 3); @@ -2832,14 +3291,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const int64_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); @@ -2850,14 +3309,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const uint8_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint8(doc, vals, 3); @@ -2868,14 +3327,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const uint16_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint16(doc, vals, 3); @@ -2886,14 +3345,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const uint32_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint32(doc, vals, 3); @@ -2904,14 +3363,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const uint64_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint64(doc, vals, 3); @@ -2922,14 +3381,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @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); @@ -2940,14 +3399,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( /** 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. + If `doc` is NULL, returns 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. - - @par Example + @param count The number count. If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @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); @@ -2959,20 +3418,20 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( /** 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. + If `doc` is NULL, returns NULL. @param vals A C array of UTF-8 null-terminator strings. - If this array contains NULL, the function will fail and return NULL. + If `vals` contains NULL, returns 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. - + If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + @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. - - @par Example + + @b Example @code const char *vals[3] = { "a", "b", "c" }; yyjson_mut_val *arr = yyjson_mut_arr_with_str(doc, vals, 3); @@ -2984,21 +3443,21 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( /** 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. + If `doc` is NULL, returns 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. + If `vals` contains NULL, returns 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. - + If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + @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. - - @par Example + + @b Example @code const char *vals[3] = { "a", "bb", "c" }; const size_t lens[3] = { 1, 2, 1 }; @@ -3011,16 +3470,16 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( /** 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. + If `doc` is NULL, returns NULL. @param vals A C array of UTF-8 null-terminator strings. - If this array contains NULL, the function will fail and return NULL. + If `vals` contains NULL, returns 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. - - @par Example + If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const char *vals[3] = { "a", "b", "c" }; yyjson_mut_val *arr = yyjson_mut_arr_with_strcpy(doc, vals, 3); @@ -3032,17 +3491,17 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( /** 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. + If `doc` is NULL, returns 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. + If `vals` contains NULL, returns 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. - - @par Example + If this value is 0, an empty array is returned. + @return The new array. NULL if arguments are invalid or allocation fails. + + @b Example @code const char *vals[3] = { "a", "bb", "c" }; const size_t lens[3] = { 1, 2, 1 }; @@ -3055,7 +3514,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( /*============================================================================== - * Mutable JSON Array Modification API + * MARK: - Mutable JSON Array Modification API *============================================================================*/ /** @@ -3065,7 +3524,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( @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. + @return Whether the operation was successful. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, @@ -3076,7 +3535,7 @@ yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, yyjson_mut_val *val); @@ -3141,7 +3600,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( 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. + @return Whether the operation was successful. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, @@ -3151,7 +3610,7 @@ yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, 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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr); @@ -3168,7 +3627,7 @@ yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, /*============================================================================== - * Mutable JSON Array Modification Convenience API + * MARK: - Mutable JSON Array Modification Convenience API *============================================================================*/ /** @@ -3176,7 +3635,7 @@ yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, yyjson_mut_val *val); @@ -3186,7 +3645,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, yyjson_mut_val *arr); @@ -3196,7 +3655,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, yyjson_mut_val *arr); @@ -3206,7 +3665,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, yyjson_mut_val *arr); @@ -3217,7 +3676,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3229,7 +3688,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3241,7 +3700,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3253,7 +3712,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3265,7 +3724,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_float(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3277,7 +3736,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_float(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_double(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3289,7 +3748,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_double(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3301,7 +3760,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. */ @@ -3316,7 +3775,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, 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. + @return Whether the operation was successful. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. */ @@ -3331,7 +3790,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, @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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3344,7 +3803,7 @@ yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, 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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, yyjson_mut_val *arr, @@ -3374,48 +3833,48 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, /*============================================================================== - * Mutable JSON Object API + * 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); +yyjson_api_inline size_t yyjson_mut_obj_size(const 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(const 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(const yyjson_mut_val *obj, const char *key, size_t key_len); /*============================================================================== - * Mutable JSON Object Iterator API + * 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. - - @par Example + + @b Example @code yyjson_mut_val *key, *val; yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); @@ -3427,7 +3886,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, } } @endcode - + If the ordering of the keys is known at compile-time, you can use this method to speed up value lookups: @code @@ -3449,25 +3908,25 @@ typedef struct 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. + If `obj` is NULL or not an object, `iter` is cleared. @param iter The iterator to be initialized. - If this parameter is NULL, the function will fail and return false. + If `iter` is NULL, returns 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()`. - + Create an iterator with an object, same as `yyjson_mut_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. + If `obj` is NULL or not an object, returns an empty iterator. @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( @@ -3475,46 +3934,46 @@ yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( /** Returns whether the iteration has more elements. - If `iter` is NULL, this function will return false. + If `iter` is NULL, returns 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. + If `iter` is NULL, returns 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. + If `iter` is NULL, returns 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. + If `iter` is NULL, returns 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. - + NULL if the key is not found or arguments are 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( @@ -3522,19 +3981,19 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( /** 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. + @param key_len 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. - + NULL if the key is not found or arguments are 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( @@ -3543,14 +4002,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( /** 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. - - @par Example + + @b Example @code size_t idx, max; - yyjson_val *key, *val; - yyjson_obj_foreach(obj, idx, max, key, val) { + yyjson_mut_val *key, *val; + yyjson_mut_obj_foreach(obj, idx, max, key, val) { your_func(key, val); } @endcode @@ -3568,7 +4027,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( /*============================================================================== - * Mutable JSON Object Creation API + * MARK: - Mutable JSON Object Creation API *============================================================================*/ /** Creates and returns a mutable object, returns NULL on error. */ @@ -3576,13 +4035,13 @@ 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 + error. The keys and values are not copied. They should be null-terminated + UTF-8 strings. + + @warning The input strings are not copied; you should keep them unmodified for the lifetime of this JSON document. - - @par Example + + @b Example @code const char *keys[2] = { "id", "name" }; const char *vals[2] = { "01", "Harry" }; @@ -3596,13 +4055,13 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, /** 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 + returns NULL on error. The keys and values are not copied. They should be + null-terminated UTF-8 strings. + + @warning The input strings are not copied; you should keep them unmodified for the lifetime of this JSON document. - - @par Example + + @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); @@ -3615,30 +4074,30 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, /*============================================================================== - * Mutable JSON Object Modification API + * 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. + This function allows duplicate keys 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. + @return Whether the operation was 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. + This function may remove all key-value pairs for the given key before adding. @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. + is the same as `yyjson_mut_obj_remove()`. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, yyjson_mut_val *key, @@ -3646,13 +4105,13 @@ yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, /** Inserts a key-value pair to the object at the given position. - This function allows duplicated key in one object. + This function allows duplicate keys 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. + @return Whether the operation was successful. */ yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, yyjson_mut_val *key, @@ -3660,7 +4119,7 @@ yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, size_t idx); /** - Removes all key-value pair from the object with given key. + Removes all key-value pairs from the object with the 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. @@ -3670,7 +4129,7 @@ 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. + Removes all key-value pairs from the object with the 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. @@ -3680,7 +4139,7 @@ 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. + Removes all key-value pairs from the object with the 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. @@ -3693,17 +4152,17 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( /** 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. + @return Whether the operation was 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. + If the key does 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. + @return Whether the operation was successful. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, @@ -3716,7 +4175,7 @@ yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, `{"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. + @return Whether the operation was successful. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, @@ -3725,13 +4184,13 @@ yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, /*============================================================================== - * Mutable JSON Object Modification Convenience API + * 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. - + This function allows duplicate keys 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, @@ -3740,8 +4199,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3750,8 +4209,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3760,8 +4219,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3770,8 +4229,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3780,8 +4239,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3790,8 +4249,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3800,8 +4259,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3810,8 +4269,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_float(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3820,8 +4279,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_double(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3830,8 +4289,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3842,8 +4301,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, 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. - + This function allows duplicate keys 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, @@ -3854,8 +4313,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3867,8 +4326,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, 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. - + This function allows duplicate keys 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, @@ -3879,8 +4338,8 @@ yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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. @@ -3892,8 +4351,8 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_arr(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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. @@ -3904,8 +4363,8 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_obj(yyjson_mut_doc *doc, /** 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. - + This function allows duplicate keys 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, @@ -3917,7 +4376,7 @@ yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, 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); @@ -3927,7 +4386,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str( 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); @@ -3936,7 +4395,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( 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. */ @@ -3949,7 +4408,7 @@ yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, 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. */ @@ -3965,7 +4424,7 @@ yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, #if !defined(YYJSON_DISABLE_UTILS) || !YYJSON_DISABLE_UTILS /*============================================================================== - * JSON Pointer API (RFC 6901) + * MARK: - JSON Pointer API (RFC 6901) * https://tools.ietf.org/html/rfc6901 *============================================================================*/ @@ -3978,7 +4437,7 @@ 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. */ +/** JSON pointer syntax error, such as invalid escape or missing prefix. */ static const yyjson_ptr_code YYJSON_PTR_ERR_SYNTAX = 2; /** JSON pointer resolve failed, such as index out of range, key not found. */ @@ -4005,12 +4464,12 @@ typedef struct 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]} @@ -4054,7 +4513,7 @@ typedef struct yyjson_ptr_ctx { @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, +yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(const yyjson_doc *doc, const char *ptr); /** @@ -4065,7 +4524,7 @@ yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, @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, +yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(const yyjson_doc *doc, const char *ptr, size_t len); /** @@ -4077,7 +4536,7 @@ yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, @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, +yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(const yyjson_doc *doc, const char *ptr, size_t len, yyjson_ptr_err *err); @@ -4088,7 +4547,7 @@ yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, @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, +yyjson_api_inline yyjson_val *yyjson_ptr_get(const yyjson_val *val, const char *ptr); /** @@ -4099,7 +4558,7 @@ yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, @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, +yyjson_api_inline yyjson_val *yyjson_ptr_getn(const yyjson_val *val, const char *ptr, size_t len); /** @@ -4111,7 +4570,7 @@ yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, @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, +yyjson_api_inline yyjson_val *yyjson_ptr_getx(const yyjson_val *val, const char *ptr, size_t len, yyjson_ptr_err *err); @@ -4122,8 +4581,8 @@ yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, @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); +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get( + const yyjson_mut_doc *doc, const char *ptr); /** Get value by a JSON Pointer. @@ -4133,9 +4592,8 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, @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); +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn( + const yyjson_mut_doc *doc, const char *ptr, size_t len); /** Get value by a JSON Pointer. @@ -4147,11 +4605,9 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, @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); +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx( + const yyjson_mut_doc *doc, const char *ptr, size_t len, + yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Get value by a JSON Pointer. @@ -4160,7 +4616,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, @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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(const yyjson_mut_val *val, const char *ptr); /** @@ -4171,7 +4627,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, @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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(const yyjson_mut_val *val, const char *ptr, size_t len); @@ -4185,7 +4641,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, @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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(const yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, @@ -4222,7 +4678,7 @@ yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, @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 create_parent Whether to create parent nodes if they do 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. @@ -4270,7 +4726,7 @@ yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, @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 create_parent Whether to create parent nodes if they do 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. @@ -4316,7 +4772,7 @@ yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, @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 create_parent Whether to create parent nodes if they do 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. @@ -4367,7 +4823,7 @@ yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, @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 create_parent Whether to create parent nodes if they do 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. @@ -4534,7 +4990,7 @@ yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, @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`. + @note On 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); @@ -4543,14 +4999,14 @@ yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, 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`. + @note On success, the old value will be returned via `ctx->old`. */ yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx); /*============================================================================== - * JSON Patch API (RFC 6902) + * MARK: - JSON Patch API (RFC 6902) * https://tools.ietf.org/html/rfc6902 *============================================================================*/ @@ -4563,7 +5019,7 @@ 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. */ +/** Memory allocation failed. */ static const yyjson_patch_code YYJSON_PATCH_ERROR_MEMORY_ALLOCATION = 2; /** JSON patch operation is not object type. */ @@ -4575,7 +5031,7 @@ 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. */ +/** JSON patch `test` operation failed (values not equal). */ static const yyjson_patch_code YYJSON_PATCH_ERROR_EQUAL = 6; /** JSON patch operation failed on JSON pointer. */ @@ -4600,8 +5056,8 @@ typedef struct yyjson_patch_err { 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, + const yyjson_val *orig, + const yyjson_val *patch, yyjson_patch_err *err); /** @@ -4611,14 +5067,14 @@ yyjson_api yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, 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, + const yyjson_mut_val *orig, + const yyjson_mut_val *patch, yyjson_patch_err *err); /*============================================================================== - * JSON Merge-Patch API (RFC 7386) + * MARK: - JSON Merge-Patch API (RFC 7386) * https://tools.ietf.org/html/rfc7386 *============================================================================*/ @@ -4631,8 +5087,8 @@ yyjson_api yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, object level is too deep. */ yyjson_api yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, - yyjson_val *orig, - yyjson_val *patch); + const yyjson_val *orig, + const yyjson_val *patch); /** Creates and returns a merge-patched JSON value (RFC 7386). @@ -4643,15 +5099,15 @@ yyjson_api yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, 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); + const yyjson_mut_val *orig, + const yyjson_mut_val *patch); #endif /* YYJSON_DISABLE_UTILS */ /*============================================================================== - * JSON Structure (Implementation) + * MARK: - JSON Structure (Implementation) *============================================================================*/ /** Payload of a JSON value (8 bytes). */ @@ -4679,7 +5135,7 @@ struct yyjson_doc { 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). */ + /** The total number of values read when parsing JSON (nonzero). */ size_t val_read; /** The string pool used by JSON values (nullable). */ char *str_pool; @@ -4688,23 +5144,23 @@ struct yyjson_doc { /*============================================================================== - * Unsafe JSON Value API (Implementation) + * 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). + @param len The returned value from strlen(str). */ yyjson_api_inline bool unsafe_yyjson_is_str_noesc(const char *str, size_t len) { #if YYJSON_HAS_CONSTANT_P && \ @@ -4712,12 +5168,12 @@ yyjson_api_inline bool unsafe_yyjson_is_str_noesc(const char *str, size_t len) { 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. @@ -4756,154 +5212,156 @@ yyjson_api_inline double unsafe_yyjson_u64_to_f64(uint64_t num) { #endif } -yyjson_api_inline yyjson_type unsafe_yyjson_get_type(void *val) { - uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; +yyjson_api_inline yyjson_type unsafe_yyjson_get_type(const void *val) { + uint8_t tag = (uint8_t)((const 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; +yyjson_api_inline yyjson_subtype unsafe_yyjson_get_subtype(const void *val) { + uint8_t tag = (uint8_t)((const 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; +yyjson_api_inline uint8_t unsafe_yyjson_get_tag(const void *val) { + uint8_t tag = (uint8_t)((const yyjson_val *)val)->tag; return (uint8_t)(tag & YYJSON_TAG_MASK); } -yyjson_api_inline bool unsafe_yyjson_is_raw(void *val) { +yyjson_api_inline bool unsafe_yyjson_is_raw(const void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_RAW; } -yyjson_api_inline bool unsafe_yyjson_is_null(void *val) { +yyjson_api_inline bool unsafe_yyjson_is_null(const void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NULL; } -yyjson_api_inline bool unsafe_yyjson_is_bool(void *val) { +yyjson_api_inline bool unsafe_yyjson_is_bool(const void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_BOOL; } -yyjson_api_inline bool unsafe_yyjson_is_num(void *val) { +yyjson_api_inline bool unsafe_yyjson_is_num(const void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NUM; } -yyjson_api_inline bool unsafe_yyjson_is_str(void *val) { +yyjson_api_inline bool unsafe_yyjson_is_str(const void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_STR; } -yyjson_api_inline bool unsafe_yyjson_is_arr(void *val) { +yyjson_api_inline bool unsafe_yyjson_is_arr(const void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_ARR; } -yyjson_api_inline bool unsafe_yyjson_is_obj(void *val) { +yyjson_api_inline bool unsafe_yyjson_is_obj(const void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_OBJ; } -yyjson_api_inline bool unsafe_yyjson_is_ctn(void *val) { +yyjson_api_inline bool unsafe_yyjson_is_ctn(const 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) { +yyjson_api_inline bool unsafe_yyjson_is_uint(const 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) { +yyjson_api_inline bool unsafe_yyjson_is_sint(const 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) { +yyjson_api_inline bool unsafe_yyjson_is_int(const 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) { +yyjson_api_inline bool unsafe_yyjson_is_real(const 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) { +yyjson_api_inline bool unsafe_yyjson_is_true(const 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) { +yyjson_api_inline bool unsafe_yyjson_is_false(const 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) { +yyjson_api_inline bool unsafe_yyjson_arr_is_flat(const 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 const char *unsafe_yyjson_get_raw(const void *val) { + return ((const yyjson_val *)val)->uni.str; } -yyjson_api_inline bool unsafe_yyjson_get_bool(void *val) { +yyjson_api_inline bool unsafe_yyjson_get_bool(const 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 uint64_t unsafe_yyjson_get_uint(const void *val) { + return ((const 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 int64_t unsafe_yyjson_get_sint(const void *val) { + return ((const 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 int unsafe_yyjson_get_int(const void *val) { + return (int)((const 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_real(const void *val) { + return ((const yyjson_val *)val)->uni.f64; } -yyjson_api_inline double unsafe_yyjson_get_num(void *val) { +yyjson_api_inline double unsafe_yyjson_get_num(const void *val) { uint8_t tag = unsafe_yyjson_get_tag(val); if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL)) { - return ((yyjson_val *)val)->uni.f64; + return ((const yyjson_val *)val)->uni.f64; } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT)) { - return (double)((yyjson_val *)val)->uni.i64; + return (double)((const 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 unsafe_yyjson_u64_to_f64(((const 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 const char *unsafe_yyjson_get_str(const void *val) { + return ((const 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 size_t unsafe_yyjson_get_len(const void *val) { + return (size_t)(((const 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_first(const yyjson_val *ctn) { + return yyjson_constcast(yyjson_val *)ctn + 1; } -yyjson_api_inline yyjson_val *unsafe_yyjson_get_next(yyjson_val *val) { +yyjson_api_inline yyjson_val *unsafe_yyjson_get_next(const 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); + uint8_t *ptr = yyjson_constcast(uint8_t *)val; + return (yyjson_val *)(void *)(ptr + ofs); } -yyjson_api_inline bool unsafe_yyjson_equals_strn(void *val, const char *str, - size_t len) { +yyjson_api_inline bool unsafe_yyjson_equals_strn(const void *val, + const char *str, size_t len) { return unsafe_yyjson_get_len(val) == len && - memcmp(((yyjson_val *)val)->uni.str, str, len) == 0; + memcmp(((const yyjson_val *)val)->uni.str, str, len) == 0; } -yyjson_api_inline bool unsafe_yyjson_equals_str(void *val, const char *str) { +yyjson_api_inline bool unsafe_yyjson_equals_str(const void *val, + const char *str) { return unsafe_yyjson_equals_strn(val, str, strlen(str)); } @@ -5017,18 +5475,18 @@ yyjson_api_inline void unsafe_yyjson_set_obj(void *val, size_t size) { /*============================================================================== - * JSON Document API (Implementation) + * MARK: - JSON Document API (Implementation) *============================================================================*/ -yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc) { +yyjson_api_inline yyjson_val *yyjson_doc_get_root(const yyjson_doc *doc) { return doc ? doc->root : NULL; } -yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc) { +yyjson_api_inline size_t yyjson_doc_get_read_size(const yyjson_doc *doc) { return doc ? doc->dat_read : 0; } -yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc) { +yyjson_api_inline size_t yyjson_doc_get_val_count(const yyjson_doc *doc) { return doc ? doc->val_read : 0; } @@ -5044,84 +5502,84 @@ yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc) { /*============================================================================== - * JSON Value Type API (Implementation) + * MARK: - JSON Value Type API (Implementation) *============================================================================*/ -yyjson_api_inline bool yyjson_is_raw(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_raw(const yyjson_val *val) { return val ? unsafe_yyjson_is_raw(val) : false; } -yyjson_api_inline bool yyjson_is_null(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_null(const yyjson_val *val) { return val ? unsafe_yyjson_is_null(val) : false; } -yyjson_api_inline bool yyjson_is_true(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_true(const yyjson_val *val) { return val ? unsafe_yyjson_is_true(val) : false; } -yyjson_api_inline bool yyjson_is_false(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_false(const yyjson_val *val) { return val ? unsafe_yyjson_is_false(val) : false; } -yyjson_api_inline bool yyjson_is_bool(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_bool(const yyjson_val *val) { return val ? unsafe_yyjson_is_bool(val) : false; } -yyjson_api_inline bool yyjson_is_uint(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_uint(const yyjson_val *val) { return val ? unsafe_yyjson_is_uint(val) : false; } -yyjson_api_inline bool yyjson_is_sint(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_sint(const yyjson_val *val) { return val ? unsafe_yyjson_is_sint(val) : false; } -yyjson_api_inline bool yyjson_is_int(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_int(const yyjson_val *val) { return val ? unsafe_yyjson_is_int(val) : false; } -yyjson_api_inline bool yyjson_is_real(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_real(const yyjson_val *val) { return val ? unsafe_yyjson_is_real(val) : false; } -yyjson_api_inline bool yyjson_is_num(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_num(const yyjson_val *val) { return val ? unsafe_yyjson_is_num(val) : false; } -yyjson_api_inline bool yyjson_is_str(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_str(const yyjson_val *val) { return val ? unsafe_yyjson_is_str(val) : false; } -yyjson_api_inline bool yyjson_is_arr(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_arr(const yyjson_val *val) { return val ? unsafe_yyjson_is_arr(val) : false; } -yyjson_api_inline bool yyjson_is_obj(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_obj(const yyjson_val *val) { return val ? unsafe_yyjson_is_obj(val) : false; } -yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val) { +yyjson_api_inline bool yyjson_is_ctn(const yyjson_val *val) { return val ? unsafe_yyjson_is_ctn(val) : false; } /*============================================================================== - * JSON Value Content API (Implementation) + * MARK: - JSON Value Content API (Implementation) *============================================================================*/ -yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val) { +yyjson_api_inline yyjson_type yyjson_get_type(const yyjson_val *val) { return val ? unsafe_yyjson_get_type(val) : YYJSON_TYPE_NONE; } -yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val) { +yyjson_api_inline yyjson_subtype yyjson_get_subtype(const yyjson_val *val) { return val ? unsafe_yyjson_get_subtype(val) : YYJSON_SUBTYPE_NONE; } -yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val) { +yyjson_api_inline uint8_t yyjson_get_tag(const yyjson_val *val) { return val ? unsafe_yyjson_get_tag(val) : 0; } -yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val) { +yyjson_api_inline const char *yyjson_get_type_desc(const 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"; @@ -5138,43 +5596,44 @@ yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val) { } } -yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val) { +yyjson_api_inline const char *yyjson_get_raw(const yyjson_val *val) { return yyjson_is_raw(val) ? unsafe_yyjson_get_raw(val) : NULL; } -yyjson_api_inline bool yyjson_get_bool(yyjson_val *val) { +yyjson_api_inline bool yyjson_get_bool(const 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) { +yyjson_api_inline uint64_t yyjson_get_uint(const 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) { +yyjson_api_inline int64_t yyjson_get_sint(const yyjson_val *val) { return yyjson_is_int(val) ? unsafe_yyjson_get_sint(val) : 0; } -yyjson_api_inline int yyjson_get_int(yyjson_val *val) { +yyjson_api_inline int yyjson_get_int(const yyjson_val *val) { return yyjson_is_int(val) ? unsafe_yyjson_get_int(val) : 0; } -yyjson_api_inline double yyjson_get_real(yyjson_val *val) { +yyjson_api_inline double yyjson_get_real(const 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) { +yyjson_api_inline double yyjson_get_num(const yyjson_val *val) { return val ? unsafe_yyjson_get_num(val) : 0.0; } -yyjson_api_inline const char *yyjson_get_str(yyjson_val *val) { +yyjson_api_inline const char *yyjson_get_str(const 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) { +yyjson_api_inline size_t yyjson_get_len(const yyjson_val *val) { return val ? unsafe_yyjson_get_len(val) : 0; } -yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str) { +yyjson_api_inline bool yyjson_equals_str(const yyjson_val *val, + const char *str) { if (yyjson_likely(val && str)) { return unsafe_yyjson_is_str(val) && unsafe_yyjson_equals_str(val, str); @@ -5182,8 +5641,8 @@ yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str) { return false; } -yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, - size_t len) { +yyjson_api_inline bool yyjson_equals_strn(const 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); @@ -5191,9 +5650,11 @@ yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, return false; } -yyjson_api bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); +yyjson_api bool unsafe_yyjson_equals(const yyjson_val *lhs, + const yyjson_val *rhs); -yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) { +yyjson_api_inline bool yyjson_equals(const yyjson_val *lhs, + const yyjson_val *rhs) { if (yyjson_unlikely(!lhs || !rhs)) return false; return unsafe_yyjson_equals(lhs, rhs); } @@ -5201,6 +5662,7 @@ yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *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; + if (yyjson_unlikely(!raw)) return false; unsafe_yyjson_set_raw(val, raw, len); return true; } @@ -5229,9 +5691,9 @@ yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num) { return true; } -yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num) { +yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int64_t num) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; - unsafe_yyjson_set_sint(val, (int64_t)num); + unsafe_yyjson_set_sint(val, num); return true; } @@ -5289,14 +5751,15 @@ yyjson_api_inline bool yyjson_set_str_noesc(yyjson_val *val, bool noesc) { /*============================================================================== - * JSON Array API (Implementation) + * MARK: - JSON Array API (Implementation) *============================================================================*/ -yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr) { +yyjson_api_inline size_t yyjson_arr_size(const 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) { +yyjson_api_inline yyjson_val *yyjson_arr_get(const 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); @@ -5311,7 +5774,7 @@ yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx) { return NULL; } -yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr) { +yyjson_api_inline yyjson_val *yyjson_arr_get_first(const 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); @@ -5320,7 +5783,7 @@ yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr) { return NULL; } -yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr) { +yyjson_api_inline yyjson_val *yyjson_arr_get_last(const yyjson_val *arr) { if (yyjson_likely(yyjson_is_arr(arr))) { size_t len = unsafe_yyjson_get_len(arr); if (yyjson_likely(len > 0)) { @@ -5339,10 +5802,10 @@ yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr) { /*============================================================================== - * JSON Array Iterator API (Implementation) + * MARK: - JSON Array Iterator API (Implementation) *============================================================================*/ -yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, +yyjson_api_inline bool yyjson_arr_iter_init(const yyjson_val *arr, yyjson_arr_iter *iter) { if (yyjson_likely(yyjson_is_arr(arr) && iter)) { iter->idx = 0; @@ -5354,7 +5817,7 @@ yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, return false; } -yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr) { +yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(const yyjson_val *arr) { yyjson_arr_iter iter; yyjson_arr_iter_init(arr, &iter); return iter; @@ -5378,19 +5841,19 @@ yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter) { /*============================================================================== - * JSON Object API (Implementation) + * MARK: - JSON Object API (Implementation) *============================================================================*/ -yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj) { +yyjson_api_inline size_t yyjson_obj_size(const 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, +yyjson_api_inline yyjson_val *yyjson_obj_get(const 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, +yyjson_api_inline yyjson_val *yyjson_obj_getn(const yyjson_val *obj, const char *_key, size_t key_len) { if (yyjson_likely(yyjson_is_obj(obj) && _key)) { @@ -5407,23 +5870,23 @@ yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, /*============================================================================== - * JSON Object Iterator API (Implementation) + * MARK: - JSON Object Iterator API (Implementation) *============================================================================*/ -yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, +yyjson_api_inline bool yyjson_obj_iter_init(const 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; + iter->obj = yyjson_constcast(yyjson_val *)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_api_inline yyjson_obj_iter yyjson_obj_iter_with(const yyjson_val *obj) { yyjson_obj_iter iter; yyjson_obj_iter_init(obj, &iter); return iter; @@ -5484,12 +5947,12 @@ yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, /*============================================================================== - * Mutable JSON Structure (Implementation) + * MARK: - Mutable JSON Structure (Implementation) *============================================================================*/ /** Mutable JSON value, 24 bytes. - The 'tag' and 'uni' field is same as immutable value. + The 'tag' and 'uni' fields are the same as immutable value. The 'next' field links all elements inside the container to be a cycle. */ struct yyjson_mut_val { @@ -5520,7 +5983,7 @@ typedef struct yyjson_str_pool { /** A memory chunk in value memory pool. - `sizeof(yyjson_val_chunk)` should not larger than `sizeof(yyjson_mut_val)`. + `sizeof(yyjson_val_chunk)` should not be larger than `sizeof(yyjson_mut_val)`. */ typedef struct yyjson_val_chunk { struct yyjson_val_chunk *next; /* next chunk linked list */ @@ -5563,6 +6026,10 @@ yyjson_api_inline char *unsafe_yyjson_mut_str_alc(yyjson_mut_doc *doc, char *mem; const yyjson_alc *alc = &doc->alc; yyjson_str_pool *pool = &doc->str_pool; + /* `len + 1` is used below to reserve space for a null terminator; + reject the value that would wrap it to 0 and produce an under-sized + allocation with an out-of-bounds memcpy at the call sites. */ + if (yyjson_unlikely(len == (size_t)-1)) return NULL; if (yyjson_unlikely((size_t)(pool->end - pool->cur) <= len)) { if (yyjson_unlikely(!unsafe_yyjson_str_pool_grow(pool, alc, len + 1))) { return NULL; @@ -5600,7 +6067,7 @@ yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_val(yyjson_mut_doc *doc, /*============================================================================== - * Mutable JSON Document API (Implementation) + * MARK: - Mutable JSON Document API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc) { @@ -5615,138 +6082,140 @@ yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, /*============================================================================== - * Mutable JSON Value Type API (Implementation) + * MARK: - Mutable JSON Value Type API (Implementation) *============================================================================*/ -yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_raw(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_raw(val) : false; } -yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_null(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_null(val) : false; } -yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_true(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_true(val) : false; } -yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_false(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_false(val) : false; } -yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_bool(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_bool(val) : false; } -yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_uint(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_uint(val) : false; } -yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_sint(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_sint(val) : false; } -yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_int(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_int(val) : false; } -yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_real(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_real(val) : false; } -yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_num(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_num(val) : false; } -yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_str(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_str(val) : false; } -yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_arr(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_arr(val) : false; } -yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_obj(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_obj(val) : false; } -yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val) { +yyjson_api_inline bool yyjson_mut_is_ctn(const yyjson_mut_val *val) { return val ? unsafe_yyjson_is_ctn(val) : false; } /*============================================================================== - * Mutable JSON Value Content API (Implementation) + * 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_type yyjson_mut_get_type(const yyjson_mut_val *val) { + return yyjson_get_type((const 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 yyjson_subtype yyjson_mut_get_subtype( + const yyjson_mut_val *val) { + return yyjson_get_subtype((const 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 uint8_t yyjson_mut_get_tag(const yyjson_mut_val *val) { + return yyjson_get_tag((const 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_type_desc( + const yyjson_mut_val *val) { + return yyjson_get_type_desc((const 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 const char *yyjson_mut_get_raw(const yyjson_mut_val *val) { + return yyjson_get_raw((const 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 bool yyjson_mut_get_bool(const yyjson_mut_val *val) { + return yyjson_get_bool((const 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 uint64_t yyjson_mut_get_uint(const yyjson_mut_val *val) { + return yyjson_get_uint((const 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 int64_t yyjson_mut_get_sint(const yyjson_mut_val *val) { + return yyjson_get_sint((const 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 int yyjson_mut_get_int(const yyjson_mut_val *val) { + return yyjson_get_int((const 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_real(const yyjson_mut_val *val) { + return yyjson_get_real((const 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 double yyjson_mut_get_num(const yyjson_mut_val *val) { + return yyjson_get_num((const 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 const char *yyjson_mut_get_str(const yyjson_mut_val *val) { + return yyjson_get_str((const 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 size_t yyjson_mut_get_len(const yyjson_mut_val *val) { + return yyjson_get_len((const yyjson_val *)val); } -yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, +yyjson_api_inline bool yyjson_mut_equals_str(const yyjson_mut_val *val, const char *str) { - return yyjson_equals_str((yyjson_val *)val, str); + return yyjson_equals_str((const yyjson_val *)val, str); } -yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, +yyjson_api_inline bool yyjson_mut_equals_strn(const yyjson_mut_val *val, const char *str, size_t len) { - return yyjson_equals_strn((yyjson_val *)val, str, len); + return yyjson_equals_strn((const yyjson_val *)val, str, len); } -yyjson_api bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, - yyjson_mut_val *rhs); +yyjson_api bool unsafe_yyjson_mut_equals(const yyjson_mut_val *lhs, + const yyjson_mut_val *rhs); -yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, - yyjson_mut_val *rhs) { +yyjson_api_inline bool yyjson_mut_equals(const yyjson_mut_val *lhs, + const yyjson_mut_val *rhs) { if (yyjson_unlikely(!lhs || !rhs)) return false; return unsafe_yyjson_mut_equals(lhs, rhs); } @@ -5782,9 +6251,9 @@ yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num) { return true; } -yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num) { +yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int64_t num) { if (yyjson_unlikely(!val)) return false; - unsafe_yyjson_set_sint(val, (int64_t)num); + unsafe_yyjson_set_sint(val, num); return true; } @@ -5813,7 +6282,7 @@ yyjson_api_inline bool yyjson_mut_set_fp_to_fixed(yyjson_mut_val *val, return true; } -yyjson_api_inline bool yyjson_mut_set_fp_to_float(yyjson_mut_val *val, +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); @@ -5856,7 +6325,7 @@ yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val) { /*============================================================================== - * Mutable JSON Value Creation API (Implementation) + * MARK: - Mutable JSON Value Creation API (Implementation) *============================================================================*/ #define yyjson_mut_val_one(func) \ @@ -5997,14 +6466,14 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, /*============================================================================== - * Mutable JSON Array API (Implementation) + * MARK: - Mutable JSON Array API (Implementation) *============================================================================*/ -yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr) { +yyjson_api_inline size_t yyjson_mut_arr_size(const 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(const 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; @@ -6015,7 +6484,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first( - yyjson_mut_val *arr) { + const yyjson_mut_val *arr) { if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { return ((yyjson_mut_val *)arr->uni.ptr)->next; } @@ -6023,7 +6492,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first( } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last( - yyjson_mut_val *arr) { + const yyjson_mut_val *arr) { if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { return ((yyjson_mut_val *)arr->uni.ptr); } @@ -6033,7 +6502,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last( /*============================================================================== - * Mutable JSON Array Iterator API (Implementation) + * MARK: - Mutable JSON Array Iterator API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, @@ -6084,7 +6553,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( iter->max--; unsafe_yyjson_set_len(iter->arr, iter->max); prev->next = next; - iter->cur = next; + iter->cur = prev; return cur; } return NULL; @@ -6093,7 +6562,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( /*============================================================================== - * Mutable JSON Array Creation API (Implementation) + * MARK: - Mutable JSON Array Creation API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc) { @@ -6273,7 +6742,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( /*============================================================================== - * Mutable JSON Array Modification API (Implementation) + * MARK: - Mutable JSON Array Modification API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, @@ -6444,7 +6913,7 @@ yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *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 > len || _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; @@ -6483,7 +6952,7 @@ yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, /*============================================================================== - * Mutable JSON Array Modification Convenience API (Implementation) + * MARK: - Mutable JSON Array Modification Convenience API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, @@ -6649,19 +7118,19 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, /*============================================================================== - * Mutable JSON Object API (Implementation) + * MARK: - Mutable JSON Object API (Implementation) *============================================================================*/ -yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj) { +yyjson_api_inline size_t yyjson_mut_obj_size(const 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(const 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(const yyjson_mut_val *obj, const char *_key, size_t key_len) { size_t len = yyjson_mut_obj_size(obj); @@ -6678,7 +7147,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, /*============================================================================== - * Mutable JSON Object Iterator API (Implementation) + * MARK: - Mutable JSON Object Iterator API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, @@ -6756,7 +7225,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( cur = cur->next->next; if (unsafe_yyjson_equals_strn(cur, key, key_len)) { iter->idx += idx; - if (iter->idx > max) iter->idx -= max + 1; + if (iter->idx > max) iter->idx -= max; iter->pre = pre; iter->cur = cur; return cur->next; @@ -6769,7 +7238,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( /*============================================================================== - * Mutable JSON Object Creation API (Implementation) + * MARK: - Mutable JSON Object Creation API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc) { @@ -6787,7 +7256,9 @@ 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)))) { + if (yyjson_likely(doc && ((count > 0 && count < + (~(size_t)0) / sizeof(yyjson_mut_val) / 2 && + 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; @@ -6796,8 +7267,10 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, 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]); + uint64_t key_len, val_len; + if (yyjson_unlikely(!keys[i] || !vals[i])) return NULL; + key_len = (uint64_t)strlen(keys[i]); + 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]; @@ -6817,7 +7290,9 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, 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)))) { + if (yyjson_likely(doc && ((count > 0 && count < + (~(size_t)0) / sizeof(yyjson_mut_val) / 2 && + 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; @@ -6828,8 +7303,10 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, 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); + uint64_t key_len, val_len; + if (yyjson_unlikely(!key_str || !val_str)) return NULL; + key_len = (uint64_t)strlen(key_str); + 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; @@ -6849,7 +7326,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, /*============================================================================== - * Mutable JSON Object Modification API (Implementation) + * MARK: - Mutable JSON Object Modification API (Implementation) *============================================================================*/ yyjson_api_inline void unsafe_yyjson_mut_obj_add(yyjson_mut_val *obj, @@ -7042,7 +7519,7 @@ yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, /*============================================================================== - * Mutable JSON Object Modification Convenience API (Implementation) + * MARK: - Mutable JSON Object Modification Convenience API (Implementation) *============================================================================*/ #define yyjson_mut_obj_add_func(func) \ @@ -7269,7 +7746,7 @@ yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, #if !defined(YYJSON_DISABLE_UTILS) || !YYJSON_DISABLE_UTILS /*============================================================================== - * JSON Pointer API (Implementation) + * MARK: - JSON Pointer API (Implementation) *============================================================================*/ #define yyjson_ptr_set_err(_code, _msg) do { \ @@ -7281,12 +7758,12 @@ yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, } while(false) /* require: val != NULL, *ptr == '/', len > 0 */ -yyjson_api yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, +yyjson_api yyjson_val *unsafe_yyjson_ptr_getx(const 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, +yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_getx(const yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, @@ -7313,18 +7790,18 @@ yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_removex(yyjson_mut_val *val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); -yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, +yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(const 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, +yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(const 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, +yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(const yyjson_doc *doc, const char *ptr, size_t len, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); @@ -7346,18 +7823,18 @@ yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, return unsafe_yyjson_ptr_getx(doc->root, ptr, len, err); } -yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, +yyjson_api_inline yyjson_val *yyjson_ptr_get(const 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, +yyjson_api_inline yyjson_val *yyjson_ptr_getn(const 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, +yyjson_api_inline yyjson_val *yyjson_ptr_getx(const yyjson_val *val, const char *ptr, size_t len, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); @@ -7366,7 +7843,7 @@ yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, return NULL; } if (yyjson_unlikely(len == 0)) { - return val; + return yyjson_constcast(yyjson_val *)val; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); @@ -7375,26 +7852,23 @@ yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, 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) { +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get( + const 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) { +yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn( + const 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_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx( + const 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; @@ -7413,32 +7887,32 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(const 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(const 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, +yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(const 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; + return yyjson_constcast(yyjson_mut_val *)val; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); @@ -7469,7 +7943,7 @@ yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, 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; @@ -7532,7 +8006,7 @@ yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, 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; @@ -7570,7 +8044,7 @@ yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, 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; @@ -7636,7 +8110,7 @@ yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, 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; @@ -7670,10 +8144,10 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( 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; @@ -7714,10 +8188,10 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( 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; @@ -7747,10 +8221,10 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( 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; @@ -7791,7 +8265,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, 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; @@ -7813,7 +8287,7 @@ yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, 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; @@ -7928,7 +8402,7 @@ yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx) { /*============================================================================== - * JSON Value at Pointer API (Implementation) + * MARK: - JSON Value at Pointer API (Implementation) *============================================================================*/ /** @@ -7936,7 +8410,7 @@ yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx) { 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) { + const 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); @@ -7951,7 +8425,7 @@ yyjson_api_inline bool yyjson_ptr_get_bool( 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) { + const 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; @@ -7969,7 +8443,7 @@ yyjson_api_inline bool yyjson_ptr_get_uint( 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) { + const 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; @@ -7987,7 +8461,7 @@ yyjson_api_inline bool yyjson_ptr_get_sint( 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) { + const 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); @@ -8003,7 +8477,7 @@ yyjson_api_inline bool yyjson_ptr_get_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) { + const 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); @@ -8018,7 +8492,7 @@ yyjson_api_inline bool yyjson_ptr_get_num( 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) { + const 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); @@ -8031,7 +8505,7 @@ yyjson_api_inline bool yyjson_ptr_get_str( /*============================================================================== - * Deprecated + * MARK: - Deprecated *============================================================================*/ /** @deprecated renamed to `yyjson_doc_ptr_get` */ @@ -8115,14 +8589,14 @@ yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_get_pointer( /*============================================================================== - * Compiler Hint End + * 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 +#elif YYJSON_IS_REAL_GCC +# if yyjson_gcc_available(4, 6, 0) +# pragma GCC diagnostic pop # endif #elif defined(_MSC_VER) # pragma warning(pop)