diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15df27186f..fc4394326b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -518,8 +518,9 @@ jobs: usesh: true prepare: | uname -a + pkg install pkg:/package/pkg pkg update --accept - pkg install gcc13 cmake git pkg-config glib2 dbus sqlite-3 imagemagick + pkg install gcc14 cmake git pkg-config glib2 dbus sqlite-3 imagemagick run: | cmake -DSET_TWEAK=Off -DBUILD_TESTS=On . @@ -650,7 +651,6 @@ jobs: dragonfly-amd64: name: DragonFly-amd64 runs-on: ubuntu-latest - if: false permissions: security-events: write contents: read diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a6ac10728..35b4bf305c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,61 @@ +# 2.51.0 + +Changes: +* Fastfetch now requires [yyjson 0.12](https://github.com/ibireme/yyjson/releases/tag/0.12.0) to build when using `-DENABLE_SYSTEM_YYJSON=ON`. +* The Disk module no longer shows hyperlink mountpoints by default, which cause issues on some real consoles (Disk) + * Instead, the custom key for the Disk module now supports `{mountpoint-link}` and `{name-link}` to show hyperlinks for mountpoints and names. For example, `{ "type": "disk", "key": "Disk ({mountpoint-link})" }` can be used to restore the old behavior. + +Features: +* Adds `succeeded` module condition to JSONC config. When set to `false`, the module will only run if the last module failed (#1908) + * Useful for displaying fallback placeholders when a module fails. For example: +```jsonc +{ + "host", + // If fastfetch fails to detect host info, display "DIY PC" instead + { + "type": "custom", + "condition": { + "succeeded": false + }, + "key": "Host", + "format": "DIY PC" + } +} +``` +* By upgrading to yyjson 0.12, fastfetch now adds [JSON5](https://json5.org/) format support for configuration files (#1907) + * [JSON5](https://json5.org/) is a superset of JSONC that allows unquoted keys, single quotes, multi-line strings, etc., and is fully compatible with JSONC and strict JSON. + * To use JSON5, simply name your config file with a `.json5` extension. The `.jsonc` extension is still supported and used as the default extension for better IDE syntax highlighting support. +* Fastfetch has been ported to [`GNU/Hurd`](https://www.gnu.org/software/hurd/) (#1895) + * Thanks to the efforts of @yelninei! +* Built-in logos now honor `logo.width` (#1905) + * When its value is larger than the actual logo width, the logo will be padded with spaces to the right +* Adds Trinity DE version detection (#1917, DE, Linux) +* Adds formatted free and available disk size fields (#1929, Disk) + * `{size-free}`: free size of the disk + * `{size-available}`: available size of the disk + * See [askubuntu.com](https://askubuntu.com/questions/249387/df-h-used-space-avail-free-space-is-less-than-the-total-size-of-home) for the difference between free and available size +* Adds [x86_64 micro-architecture level](https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels) detection (#1928, CPU) + * Useful when installing software that requires or is optimized for specific CPU features. E.g., [CachyOS](https://wiki.cachyos.org/features/optimized_repos/) + * Exposed via `{march}` in custom format +* Adds [Aarch64 micro-architecture level](https://en.wikipedia.org/wiki/AArch64#Profiles) detection (CPU) + * Supported on Linux (including Android), macOS and Windows + * This is not fully accurate because there are many optional features across different levels, and not all levels are detectable. + * Exposed via `{march}` in custom format. +* Adds shepherd detection support (InitSystem, Linux) + +Bugfixes: +* Refines GPU detection logic to correctly handle virtual devices (#1920, GPU, Windows) +* Fixes possible default route detection failure when the route table is very large (#1919, LocalIP, Linux) +* Fastfetch now correctly parses `hwdata/pci.ids` files alongside `pciids/pci.ids` on FreeBSD when detecting GPU names (#1924, GPU, FreeBSD) +* Fixes twin WM detection (#1917, WM, Linux) +* Various fixes for Android support + * Corrects WM name for Android (WM, Android) + * Fixes battery temperature detection when running in ADB (Battery, Android) + * Adds CPU and GPU temperature detection support (CPU, Android) + +Logos: +* Adds AerynOS + # 2.50.2 Bugfixes: diff --git a/CMakeLists.txt b/CMakeLists.txt index c0737a6e47..a17d159d62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.12.0) # target_link_libraries with OBJECT libs & project homepage url project(fastfetch - VERSION 2.50.2 + VERSION 2.51.0 LANGUAGES C DESCRIPTION "Fast neofetch-like system information tool" HOMEPAGE_URL "https://github.com/fastfetch-cli/fastfetch" @@ -32,6 +32,8 @@ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS") set(SunOS TRUE CACHE BOOL "..." FORCE) elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Haiku") set(Haiku TRUE CACHE BOOL "..." FORCE) +elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "GNU") + set(GNU TRUE CACHE BOOL "..." FORCE) elseif(NOT APPLE AND NOT WIN32) message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}") endif() @@ -56,30 +58,30 @@ include(CheckIncludeFile) include(CMakeDependentOption) -cmake_dependent_option(ENABLE_VULKAN "Enable vulkan" ON "LINUX OR APPLE OR FreeBSD OR OpenBSD OR NetBSD OR WIN32 OR ANDROID OR SunOS OR Haiku" OFF) -cmake_dependent_option(ENABLE_WAYLAND "Enable wayland-client" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD" OFF) -cmake_dependent_option(ENABLE_XCB_RANDR "Enable xcb-randr" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS" OFF) -cmake_dependent_option(ENABLE_XRANDR "Enable xrandr" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS" OFF) -cmake_dependent_option(ENABLE_DRM "Enable libdrm" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS" OFF) -cmake_dependent_option(ENABLE_DRM_AMDGPU "Enable libdrm_amdgpu" ON "LINUX OR FreeBSD" OFF) -cmake_dependent_option(ENABLE_GIO "Enable gio-2.0" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS" OFF) -cmake_dependent_option(ENABLE_DCONF "Enable dconf" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS" OFF) -cmake_dependent_option(ENABLE_DBUS "Enable dbus-1" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS OR Haiku" OFF) -cmake_dependent_option(ENABLE_XFCONF "Enable libxfconf-0" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS" OFF) -cmake_dependent_option(ENABLE_SQLITE3 "Enable sqlite3" ON "LINUX OR FreeBSD OR APPLE OR OpenBSD OR NetBSD OR SunOS" OFF) -cmake_dependent_option(ENABLE_RPM "Enable rpm" ON "LINUX" OFF) -cmake_dependent_option(ENABLE_IMAGEMAGICK7 "Enable imagemagick 7" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR APPLE OR WIN32 OR SunOS OR Haiku" OFF) -cmake_dependent_option(ENABLE_IMAGEMAGICK6 "Enable imagemagick 6" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR APPLE OR SunOS" OFF) +cmake_dependent_option(ENABLE_VULKAN "Enable vulkan" ON "LINUX OR APPLE OR FreeBSD OR OpenBSD OR NetBSD OR WIN32 OR ANDROID OR SunOS OR Haiku OR GNU" OFF) +cmake_dependent_option(ENABLE_WAYLAND "Enable wayland-client" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR GNU" OFF) +cmake_dependent_option(ENABLE_XCB_RANDR "Enable xcb-randr" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS OR GNU" OFF) +cmake_dependent_option(ENABLE_XRANDR "Enable xrandr" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS OR GNU" OFF) +cmake_dependent_option(ENABLE_DRM "Enable libdrm" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS OR GNU" OFF) +cmake_dependent_option(ENABLE_DRM_AMDGPU "Enable libdrm_amdgpu" ON "LINUX OR FreeBSD OR GNU" OFF) +cmake_dependent_option(ENABLE_GIO "Enable gio-2.0" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS OR GNU" OFF) +cmake_dependent_option(ENABLE_DCONF "Enable dconf" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS OR GNU" OFF) +cmake_dependent_option(ENABLE_DBUS "Enable dbus-1" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS OR Haiku OR GNU" OFF) +cmake_dependent_option(ENABLE_XFCONF "Enable libxfconf-0" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR SunOS OR GNU" OFF) +cmake_dependent_option(ENABLE_SQLITE3 "Enable sqlite3" ON "LINUX OR FreeBSD OR APPLE OR OpenBSD OR NetBSD OR SunOS OR GNU" OFF) +cmake_dependent_option(ENABLE_RPM "Enable rpm" ON "LINUX OR GNU" OFF) +cmake_dependent_option(ENABLE_IMAGEMAGICK7 "Enable imagemagick 7" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR APPLE OR WIN32 OR SunOS OR Haiku OR GNU" OFF) +cmake_dependent_option(ENABLE_IMAGEMAGICK6 "Enable imagemagick 6" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR APPLE OR SunOS OR GNU" OFF) cmake_dependent_option(ENABLE_CHAFA "Enable chafa" ON "ENABLE_IMAGEMAGICK6 OR ENABLE_IMAGEMAGICK7" OFF) cmake_dependent_option(ENABLE_ZLIB "Enable zlib" ON "ENABLE_IMAGEMAGICK6 OR ENABLE_IMAGEMAGICK7" OFF) -cmake_dependent_option(ENABLE_EGL "Enable egl" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR ANDROID OR WIN32 OR SunOS OR Haiku" OFF) -cmake_dependent_option(ENABLE_GLX "Enable glx" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR ANDROID OR SunOS" OFF) -cmake_dependent_option(ENABLE_OPENCL "Enable opencl" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR WIN32 OR ANDROID OR SunOS OR Haiku" OFF) +cmake_dependent_option(ENABLE_EGL "Enable egl" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR ANDROID OR WIN32 OR SunOS OR Haiku OR GNU" OFF) +cmake_dependent_option(ENABLE_GLX "Enable glx" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR ANDROID OR SunOS OR GNU" OFF) +cmake_dependent_option(ENABLE_OPENCL "Enable opencl" ON "LINUX OR FreeBSD OR OpenBSD OR NetBSD OR WIN32 OR ANDROID OR SunOS OR Haiku OR GNU" OFF) cmake_dependent_option(ENABLE_FREETYPE "Enable freetype" ON "ANDROID" OFF) -cmake_dependent_option(ENABLE_PULSE "Enable pulse" ON "LINUX" OFF) +cmake_dependent_option(ENABLE_PULSE "Enable pulse" ON "LINUX OR GNU" OFF) cmake_dependent_option(ENABLE_DDCUTIL "Enable ddcutil" ON "LINUX" OFF) cmake_dependent_option(ENABLE_DIRECTX_HEADERS "Enable DirectX headers for WSL" ON "LINUX" OFF) -cmake_dependent_option(ENABLE_ELF "Enable libelf" ON "LINUX OR ANDROID OR DragonFly OR Haiku" OFF) +cmake_dependent_option(ENABLE_ELF "Enable libelf" ON "LINUX OR ANDROID OR DragonFly OR Haiku OR GNU" OFF) cmake_dependent_option(ENABLE_THREADS "Enable multithreading" ON "Threads_FOUND" OFF) cmake_dependent_option(ENABLE_LIBZFS "Enable libzfs" ON "LINUX OR FreeBSD OR SunOS" OFF) cmake_dependent_option(ENABLE_PCIACCESS "Enable libpciaccess" ON "NetBSD OR OpenBSD" OFF) @@ -370,6 +372,7 @@ set(LIBFASTFETCH_SRC src/common/settings.c src/common/size.c src/common/temps.c + src/common/time.c src/detection/bluetoothradio/bluetoothradio.c src/detection/bootmgr/bootmgr.c src/detection/chassis/chassis.c @@ -596,7 +599,7 @@ elseif(ANDROID) src/detection/diskio/diskio_linux.c src/detection/displayserver/displayserver_android.c src/detection/font/font_nosupport.c - src/detection/gpu/gpu_nosupport.c + src/detection/gpu/gpu_android.c src/detection/host/host_android.c src/detection/icons/icons_nosupport.c src/detection/initsystem/initsystem_linux.c @@ -1191,6 +1194,84 @@ elseif(Haiku) src/util/binary_linux.c src/util/haiku/version.cpp ) +elseif(GNU) + list(APPEND LIBFASTFETCH_SRC + src/common/dbus.c + src/common/io/io_unix.c + src/common/netif/netif_gnu.c + src/common/networking/networking_linux.c + src/common/processing_linux.c + src/detection/battery/battery_nosupport.c + src/detection/bios/bios_nosupport.c + src/detection/board/board_nosupport.c + src/detection/bootmgr/bootmgr_nosupport.c + src/detection/brightness/brightness_nosupport.c + src/detection/btrfs/btrfs_nosupport.c + src/detection/chassis/chassis_nosupport.c + src/detection/cpu/cpu_linux.c + src/detection/cpucache/cpucache_nosupport.c + src/detection/cpuusage/cpuusage_linux.c + src/detection/cursor/cursor_linux.c + src/detection/bluetooth/bluetooth_linux.c + src/detection/bluetoothradio/bluetoothradio_linux.c + src/detection/disk/disk_linux.c + src/detection/dns/dns_linux.c + src/detection/physicaldisk/physicaldisk_nosupport.c + src/detection/physicalmemory/physicalmemory_nosupport.c + src/detection/diskio/diskio_nosupport.c + src/detection/displayserver/linux/displayserver_linux.c + src/detection/displayserver/linux/drm.c + src/detection/displayserver/linux/wayland/wayland.c + src/detection/displayserver/linux/wayland/global-output.c + src/detection/displayserver/linux/wayland/zwlr-output.c + src/detection/displayserver/linux/wayland/wlr-output-management-unstable-v1-protocol.c + src/detection/displayserver/linux/wmde.c + src/detection/displayserver/linux/xcb.c + src/detection/displayserver/linux/xlib.c + src/detection/font/font_linux.c + src/detection/gpu/gpu_nosupport.c + src/detection/gpu/gpu_pci.c + src/detection/gtk_qt/gtk.c + src/detection/host/host_nosupport.c + src/detection/icons/icons_linux.c + src/detection/initsystem/initsystem_linux.c + src/detection/keyboard/keyboard_nosupport.c + src/detection/libc/libc_linux.c + src/detection/lm/lm_linux.c + src/detection/loadavg/loadavg_linux.c + src/detection/locale/locale_linux.c + src/detection/localip/localip_linux.c + src/detection/gamepad/gamepad_nosupport.c + src/detection/media/media_linux.c + src/detection/memory/memory_linux.c + src/detection/mouse/mouse_nosupport.c + src/detection/netio/netio_nosupport.c + src/detection/opengl/opengl_linux.c + src/detection/os/os_linux.c + src/detection/packages/packages_linux.c + src/detection/packages/packages_nix.c + src/detection/poweradapter/poweradapter_nosupport.c + src/detection/processes/processes_linux.c + src/detection/gtk_qt/qt.c + src/detection/sound/sound_linux.c + src/detection/swap/swap_linux.c + src/detection/terminalfont/terminalfont_linux.c + src/detection/terminalshell/terminalshell_linux.c + src/detection/terminalsize/terminalsize_linux.c + src/detection/theme/theme_linux.c + src/detection/tpm/tpm_nosupport.c + src/detection/uptime/uptime_linux.c + src/detection/users/users_linux.c + src/detection/wallpaper/wallpaper_linux.c + src/detection/wifi/wifi_nosupport.c + src/detection/wm/wm_linux.c + src/detection/de/de_linux.c + src/detection/wmtheme/wmtheme_linux.c + src/detection/camera/camera_nosupport.c + src/detection/zpool/zpool_nosupport.c + src/util/platform/FFPlatform_unix.c + src/util/binary_linux.c + ) endif() if(ENABLE_DIRECTX_HEADERS) @@ -1314,6 +1395,10 @@ elseif(NetBSD) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,/usr/X11R7/lib -Wl,-rpath,/usr/pkg/lib") # ditto elseif(Haiku) target_compile_definitions(libfastfetch PUBLIC _GNU_SOURCE) +elseif(GNU) + target_compile_definitions(libfastfetch PUBLIC _GNU_SOURCE) + # On Hurd PATH_MAX is not defined. Set an arbitrary limit as workaround. + target_compile_definitions(libfastfetch PUBLIC PATH_MAX=4096) endif() if(FreeBSD OR OpenBSD OR NetBSD) @@ -1638,6 +1723,10 @@ elseif(SunOS) PRIVATE "nvpair" PRIVATE "devinfo" ) +elseif(GNU) + target_link_libraries(libfastfetch + PRIVATE "m" + ) elseif(ANDROID) CHECK_LIBRARY_EXISTS(-l:libandroid-wordexp.a wordexp "" HAVE_LIBANDROID_WORDEXP_STATIC) if(HAVE_LIBANDROID_WORDEXP_STATIC) diff --git a/doc/json_schema.json b/doc/json_schema.json index 6e09686670..660fe2dd8b 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -237,6 +237,19 @@ "description": "Null to disable this condition" } ] + }, + "succeeded": { + "description": "Whether the module succeeded in the last run", + "oneOf": [ + { + "type": "boolean", + "description": "True to only show the module if it succeeded, false to only show it if it failed" + }, + { + "type": "null", + "description": "Null to disable this condition" + } + ] } } }, @@ -305,7 +318,7 @@ "type": "string" }, "cpuFormat": { - "description": "Output format of the module `CPU`. See Wiki for formatting syntax\n 1. {name}: Name\n 2. {vendor}: Vendor\n 3. {cores-physical}: Physical core count\n 4. {cores-logical}: Logical core count\n 5. {cores-online}: Online core count\n 6. {freq-base}: Base frequency (formatted)\n 7. {freq-max}: Max frequency (formatted)\n 8. {temperature}: Temperature (formatted)\n 9. {core-types}: Logical core count grouped by frequency\n 10. {packages}: Processor package count", + "description": "Output format of the module `CPU`. See Wiki for formatting syntax\n 1. {name}: Name\n 2. {vendor}: Vendor\n 3. {cores-physical}: Physical core count\n 4. {cores-logical}: Logical core count\n 5. {cores-online}: Online core count\n 6. {freq-base}: Base frequency (formatted)\n 7. {freq-max}: Max frequency (formatted)\n 8. {temperature}: Temperature (formatted)\n 9. {core-types}: Logical core count grouped by frequency\n 10. {packages}: Processor package count\n 11. {march}: X86-64 CPU microarchitecture", "type": "string" }, "cpucacheFormat": { @@ -333,7 +346,7 @@ "type": "string" }, "diskFormat": { - "description": "Output format of the module `Disk`. See Wiki for formatting syntax\n 1. {size-used}: Size used\n 2. {size-total}: Size total\n 3. {size-percentage}: Size percentage num\n 4. {files-used}: Files used\n 5. {files-total}: Files total\n 6. {files-percentage}: Files percentage num\n 7. {is-external}: True if external volume\n 8. {is-hidden}: True if hidden volume\n 9. {filesystem}: Filesystem\n 10. {name}: Label / name\n 11. {is-readonly}: True if read-only\n 12. {create-time}: Create time in local timezone\n 13. {size-percentage-bar}: Size percentage bar\n 14. {files-percentage-bar}: Files percentage bar\n 15. {days}: Days after creation\n 16. {hours}: Hours after creation\n 17. {minutes}: Minutes after creation\n 18. {seconds}: Seconds after creation\n 19. {milliseconds}: Milliseconds after creation\n 20. {mountpoint}: Mount point / drive letter\n 21. {mount-from}: Mount from (device path)\n 22. {years}: Years integer after creation\n 23. {days-of-year}: Days of year after creation\n 24. {years-fraction}: Years fraction after creation", + "description": "Output format of the module `Disk`. See Wiki for formatting syntax\n 1. {size-used}: Size used\n 2. {size-total}: Size total\n 3. {size-percentage}: Size percentage num\n 4. {files-used}: Files used\n 5. {files-total}: Files total\n 6. {files-percentage}: Files percentage num\n 7. {is-external}: True if external volume\n 8. {is-hidden}: True if hidden volume\n 9. {filesystem}: Filesystem\n 10. {name}: Label / name\n 11. {is-readonly}: True if read-only\n 12. {create-time}: Create time in local timezone\n 13. {size-percentage-bar}: Size percentage bar\n 14. {files-percentage-bar}: Files percentage bar\n 15. {days}: Days after creation\n 16. {hours}: Hours after creation\n 17. {minutes}: Minutes after creation\n 18. {seconds}: Seconds after creation\n 19. {milliseconds}: Milliseconds after creation\n 20. {mountpoint}: Mount point / drive letter\n 21. {mount-from}: Mount from (device path)\n 22. {years}: Years integer after creation\n 23. {days-of-year}: Days of year after creation\n 24. {years-fraction}: Years fraction after creation\n 25. {size-free}: Size free\n 26. {size-available}: Size available", "type": "string" }, "diskioFormat": { diff --git a/src/3rdparty/yyjson/repo.json b/src/3rdparty/yyjson/repo.json index bb2256ef01..bfa523fb55 100644 --- a/src/3rdparty/yyjson/repo.json +++ b/src/3rdparty/yyjson/repo.json @@ -1,6 +1,6 @@ { "home": "https://github.com/ibireme/yyjson", "license": "MIT ( embed in source )", - "version": "0.11.1", + "version": "0.12.0", "author": "ibireme" } diff --git a/src/3rdparty/yyjson/yyjson.c b/src/3rdparty/yyjson/yyjson.c index 20c3050aee..c16d925811 100644 --- a/src/3rdparty/yyjson/yyjson.c +++ b/src/3rdparty/yyjson/yyjson.c @@ -26,7 +26,7 @@ /*============================================================================== - * Warning Suppress + * MARK: - Warning Suppress (Private) *============================================================================*/ #if defined(__clang__) @@ -52,7 +52,7 @@ /*============================================================================== - * Version + * MARK: - Version (Public) *============================================================================*/ uint32_t yyjson_version(void) { @@ -62,7 +62,7 @@ uint32_t yyjson_version(void) { /*============================================================================== - * Flags + * MARK: - Flags (Private) *============================================================================*/ /* msvc intrinsic */ @@ -129,6 +129,8 @@ uint32_t yyjson_version(void) { # 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 /* @@ -185,7 +187,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 +206,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 +226,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,13 +239,11 @@ 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 @@ -341,12 +342,11 @@ uint32_t yyjson_version(void) { /*============================================================================== - * Macros + * MARK: - Macros (Private) *============================================================================*/ /* Macros used for loop unrolling and other purpose. */ #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 } @@ -387,13 +387,40 @@ uint32_t yyjson_version(void) { /* Used to cast away (remove) const qualifier. */ #define constcast(type) (type)(void *)(size_t)(const void *) +/* + Compiler barriers for single variables. + + These macros inform GCC that a read or write access to the given memory + location will occur, preventing certain compiler optimizations or reordering + around the access to 'val'. They do not emit any actual instructions. + + This is useful when GCC's default optimization strategies are suboptimal and + precise control over memory access patterns is required. + These barriers are not needed when using Clang or MSVC. + */ +#if YYJSON_IS_REAL_GCC +# define gcc_load_barrier(val) __asm__ volatile(""::"m"(val)) +# define gcc_store_barrier(val) __asm__ volatile("":"=m"(val)) +# define gcc_full_barrier(val) __asm__ volatile("":"=m"(val):"m"(val)) +#else +# define gcc_load_barrier(val) +# define gcc_store_barrier(val) +# define gcc_full_barrier(val) +#endif + + + +/*============================================================================== + * MARK: - Constants (Private) + *============================================================================*/ + /* Common error messages. */ #define MSG_FOPEN "failed to open file" #define MSG_FREAD "failed to read file" #define MSG_FWRITE "failed to write file" #define MSG_FCLOSE "failed to close file" #define MSG_MALLOC "failed to allocate memory" -#define MSG_CHAT_T "invalid literal, expected 'true'" +#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" @@ -405,46 +432,13 @@ uint32_t yyjson_version(void) { #define MSG_NOT_END "unexpected end of data" #define MSG_COMMENT "unclosed multiline comment" #define MSG_COMMA "trailing comma is not allowed" -#define MSG_INF_NAN "nan or inf number 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_UTF8 "invalid utf-8 encoding in string" #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" -/* - Check flags using a function to avoid `always false` warnings. - When non-standard features are disabled, unnecessary checks - will be evaluated and optimized out at compile-time. - */ -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 || - chk == YYJSON_READ_ALLOW_BOM) - return false; -#endif - return (flg & chk) != 0; -} -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; -#endif - return (flg & chk) != 0; -} -#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)) - - - -/*============================================================================== - * Number Constants - *============================================================================*/ - /* U64 constant values */ #undef U64_MAX #define U64_MAX U64(0xFFFFFFFF, 0xFFFFFFFF) @@ -461,14 +455,14 @@ 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 raw value (positive) */ -#define F64_RAW_INF U64(0x7FF00000, 0x00000000) +/* Inf bits (positive) */ +#define F64_BITS_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 /* maximum significant digits count in decimal when reading double number */ @@ -527,7 +521,7 @@ static_inline bool write_flag_eq(yyjson_write_flag flg, yyjson_write_flag chk) { /*============================================================================== - * Types + * MARK: - Types (Private) *============================================================================*/ /** Type define for primitive types. */ @@ -562,13 +556,14 @@ typedef union v64_uni { v64 v; u64 u; } v64_uni; /*============================================================================== - * Load/Store Utils + * MARK: - Load/Store Utils (Private) *============================================================================*/ #define byte_move_idx(x) ((char *)dst)[x] = ((const char *)src)[x]; #define byte_move_src(x) ((char *)tmp)[x] = ((const char *)src)[x]; #define byte_move_dst(x) ((char *)dst)[x] = ((const char *)tmp)[x]; +/** Same as `memcpy(dst, src, 2)`, no overlap. */ static_inline void byte_copy_2(void *dst, const void *src) { #if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS memcpy(dst, src, 2); @@ -577,6 +572,7 @@ static_inline void byte_copy_2(void *dst, const void *src) { #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); @@ -585,6 +581,7 @@ static_inline void byte_copy_4(void *dst, const void *src) { #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); @@ -593,6 +590,7 @@ static_inline void byte_copy_8(void *dst, const void *src) { #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); @@ -601,6 +599,7 @@ static_inline void byte_copy_16(void *dst, const void *src) { #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; @@ -613,6 +612,7 @@ static_inline void byte_move_2(void *dst, const void *src) { #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; @@ -625,6 +625,7 @@ static_inline void byte_move_4(void *dst, const void *src) { #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; @@ -637,6 +638,7 @@ static_inline void byte_move_8(void *dst, const void *src) { #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; @@ -653,6 +655,17 @@ static_inline void byte_move_16(void *dst, const void *src) { #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; @@ -665,6 +678,7 @@ static_inline bool byte_match_2(void *buf, const char *pat) { #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; @@ -679,6 +693,7 @@ static_inline bool byte_match_4(void *buf, const char *pat) { #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 @@ -690,6 +705,7 @@ static_inline u16 byte_load_2(const void *src) { 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 @@ -705,6 +721,7 @@ static_inline u32 byte_load_3(const void *src) { 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 @@ -721,2147 +738,489 @@ static_inline u32 byte_load_4(const void *src) { /*============================================================================== - * Number Utils - * These functions are used to detect and convert NaN and Inf numbers. - * The `memcpy` is used to avoid violating the strict aliasing rule. + * 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) { - 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) { - 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) { - 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) { - 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. - *============================================================================*/ +/** 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 overflow after increment. */ -static_inline bool size_add_is_overflow(usize size, usize add) { - return size > (size + add); +/** 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); } -/** 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 a sign: [+-] */ +static_inline bool char_is_sign(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_SIGN); } -/** 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 none-zero digit: [1-9] */ +static_inline bool char_is_nonzero(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_NONZERO); } -/** 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 digit: [0-9] */ +static_inline bool char_is_digit(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_DIGIT); } -/** 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 an exponent sign: [eE]. */ +static_inline bool char_is_exp(u8 d) { + return !!(char_table3[d] & CHAR_TYPE_EXP); } +/** Match a floating point indicator: [.eE]. */ +static_inline bool char_is_fp(u8 d) { + return !!(char_table3[d] & (CHAR_TYPE_DOT | CHAR_TYPE_EXP)); +} +/** Match a digit or floating point indicator: [0-9.eE]. */ +static_inline bool char_is_digit_or_fp(u8 d) { + return !!(char_table3[d] & (CHAR_TYPE_DIGIT | CHAR_TYPE_DOT | + CHAR_TYPE_EXP)); +} -/*============================================================================== - * Bits Utils - * These functions are used by the floating-point number reader and writer. - *============================================================================*/ +/** Match a JSON container: `{` or `[`. */ +static_inline bool char_is_ctn(u8 c) { + return (c & 0xDF) == 0x5B; /* '[': 0x5B, '{': 0x7B */ +} -/** 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 +/** Convert ASCII letter to lowercase; valid only for [A-Za-z]. */ +static_inline u8 char_to_lower(u8 c) { + return c | 0x20; } -/** 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 +/** Match UTF-8 byte order mask. */ +static_inline bool is_utf8_bom(const u8 *cur) { + return byte_load_3(cur) == byte_load_3("\xEF\xBB\xBF"); } +/** Match UTF-16 byte order mask. */ +static_inline bool is_utf16_bom(const u8 *cur) { + return byte_load_2(cur) == byte_load_2("\xFE\xFF") || + byte_load_2(cur) == byte_load_2("\xFF\xFE"); +} +/** Match UTF-32 byte order mask, need length check to avoid zero padding. */ +static_inline bool is_utf32_bom(const u8 *cur) { + return byte_load_4(cur) == byte_load_4("\x00\x00\xFE\xFF") || + byte_load_4(cur) == byte_load_4("\xFF\xFE\x00\x00"); +} -/*============================================================================== - * 128-bit Integer Utils - * These functions are used by the floating-point number reader and writer. - *============================================================================*/ +/** 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 (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 +/** 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 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 +}; -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); -} - -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 -}; - - - -/*============================================================================== - * Null Memory Allocator - * - * This allocator is just a placeholder to ensure that the internal - * malloc/realloc/free function pointers are not null. - *============================================================================*/ +/* 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 *null_malloc(void *ctx, usize size) { - return NULL; -} +/* + 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 *null_realloc(void *ctx, void *ptr, usize old_size, usize size) { - return NULL; -} +/* + 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 void null_free(void *ctx, void *ptr) { - return; -} +/* + 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))) ) -static const yyjson_alc YYJSON_NULL_ALC = { - null_malloc, null_realloc, null_free, NULL -}; +/* + 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) ) /*============================================================================== - * Pool Memory Allocator - * - * This allocator is initialized with a fixed-size buffer. - * The buffer is split into multiple memory chunks for memory allocation. + * MARK: - Power10 Lookup Table (Private) + * These data are used by the floating-point number reader and writer. *============================================================================*/ -/** 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 !YYJSON_DISABLE_FAST_FP_CONV - if (unlikely(size >= ctx->size)) return NULL; - pool_size_align(&size); +/** Maximum pow10 exponent that can be represented exactly as a float64. */ +#define F64_POW10_MAX_EXACT_EXP 22 - 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; -} +/** 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 +}; -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; +/** Maximum pow10 exponent that can be represented exactly as a uint64. */ +#define U64_POW10_MAX_EXACT_EXP 19 - while (next && next < cur) { - prev = next; - next = next->next; - } - if (prev) prev->next = cur; - else ctx->free_list = cur; - cur->next = next; +/** Table: [ 10^0, ..., 10^19 ] (generate with misc/make_tables.c) */ +static const u64 u64_pow10_table[U64_POW10_MAX_EXACT_EXP + 1] = { + U64(0x00000000, 0x00000001), U64(0x00000000, 0x0000000A), + U64(0x00000000, 0x00000064), U64(0x00000000, 0x000003E8), + U64(0x00000000, 0x00002710), U64(0x00000000, 0x000186A0), + U64(0x00000000, 0x000F4240), U64(0x00000000, 0x00989680), + U64(0x00000000, 0x05F5E100), U64(0x00000000, 0x3B9ACA00), + U64(0x00000002, 0x540BE400), U64(0x00000017, 0x4876E800), + U64(0x000000E8, 0xD4A51000), U64(0x00000918, 0x4E72A000), + U64(0x00005AF3, 0x107A4000), U64(0x00038D7E, 0xA4C68000), + U64(0x002386F2, 0x6FC10000), U64(0x01634578, 0x5D8A0000), + U64(0x0DE0B6B3, 0xA7640000), U64(0x8AC72304, 0x89E80000) +}; - 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; - } -} +/** Minimum decimal exponent in pow10_sig_table. */ +#define POW10_SIG_TABLE_MIN_EXP -343 -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; +/** Maximum decimal exponent in pow10_sig_table. */ +#define POW10_SIG_TABLE_MAX_EXP 324 - /* 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; +/** Minimum exact decimal exponent in pow10_sig_table */ +#define POW10_SIG_TABLE_MIN_EXACT_EXP 0 - /* 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; - } -} - -/** add a chunk to list header (the chunk must not be in the list) */ -static_inline void dyn_chunk_list_add(dyn_chunk *list, dyn_chunk *chunk) { - chunk->next = list->next; - list->next = chunk; -} - -static void *dyn_malloc(void *ctx_ptr, usize size) { - /* assert(size != 0) */ - const yyjson_alc def = YYJSON_DEFAULT_ALC; - dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; - dyn_chunk *chunk, *prev; - if (unlikely(!dyn_size_align(&size))) return NULL; - - /* freelist is empty, create new chunk */ - if (!ctx->free_list.next) { - chunk = (dyn_chunk *)def.malloc(def.ctx, size); - if (unlikely(!chunk)) return NULL; - chunk->size = size; - chunk->next = NULL; - dyn_chunk_list_add(&ctx->used_list, chunk); - return (void *)(chunk + 1); - } - - /* find a large enough chunk, or resize the largest chunk */ - prev = &ctx->free_list; - while (true) { - chunk = prev->next; - if (chunk->size >= size) { /* enough size, reuse this chunk */ - prev->next = chunk->next; - dyn_chunk_list_add(&ctx->used_list, chunk); - return (void *)(chunk + 1); - } - if (!chunk->next) { /* resize the largest chunk */ - chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); - if (unlikely(!chunk)) return NULL; - prev->next = NULL; - chunk->size = size; - dyn_chunk_list_add(&ctx->used_list, chunk); - return (void *)(chunk + 1); - } - prev = chunk; - } -} - -static void *dyn_realloc(void *ctx_ptr, void *ptr, - usize old_size, usize size) { - /* assert(ptr != NULL && size != 0 && old_size < size) */ - const yyjson_alc def = YYJSON_DEFAULT_ALC; - dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; - dyn_chunk *new_chunk, *chunk = (dyn_chunk *)ptr - 1; - if (unlikely(!dyn_size_align(&size))) return NULL; - if (chunk->size >= size) return ptr; - - dyn_chunk_list_remove(&ctx->used_list, chunk); - new_chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); - if (likely(new_chunk)) { - new_chunk->size = size; - chunk = new_chunk; - } - dyn_chunk_list_add(&ctx->used_list, chunk); - return new_chunk ? (void *)(new_chunk + 1) : NULL; -} - -static void dyn_free(void *ctx_ptr, void *ptr) { - /* assert(ptr != NULL) */ - dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; - dyn_chunk *chunk = (dyn_chunk *)ptr - 1, *prev; - - dyn_chunk_list_remove(&ctx->used_list, chunk); - for (prev = &ctx->free_list; prev; prev = prev->next) { - if (!prev->next || prev->next->size >= chunk->size) { - chunk->next = prev->next; - prev->next = chunk; - break; - } - } -} - -yyjson_alc *yyjson_alc_dyn_new(void) { - const yyjson_alc def = YYJSON_DEFAULT_ALC; - usize hdr_len = sizeof(yyjson_alc) + sizeof(dyn_ctx); - yyjson_alc *alc = (yyjson_alc *)def.malloc(def.ctx, hdr_len); - dyn_ctx *ctx = (dyn_ctx *)(void *)(alc + 1); - if (unlikely(!alc)) return NULL; - alc->malloc = dyn_malloc; - alc->realloc = dyn_realloc; - alc->free = dyn_free; - alc->ctx = alc + 1; - memset(ctx, 0, sizeof(*ctx)); - return alc; -} - -void yyjson_alc_dyn_free(yyjson_alc *alc) { - const yyjson_alc def = YYJSON_DEFAULT_ALC; - dyn_ctx *ctx = (dyn_ctx *)(void *)(alc + 1); - dyn_chunk *chunk, *next; - if (unlikely(!alc)) return; - for (chunk = ctx->free_list.next; chunk; chunk = next) { - next = chunk->next; - def.free(def.ctx, chunk); - } - for (chunk = ctx->used_list.next; chunk; chunk = next) { - next = chunk->next; - def.free(def.ctx, chunk); - } - def.free(def.ctx, alc); -} - - - -/*============================================================================== - * 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; - } -} - -static_inline bool is_utf8_bom(const u8 *hdr) { - return hdr[0] == 0xEF && hdr[1] == 0xBB && hdr[2] == 0xBF; -} - -static_inline bool is_utf16_bom(const u8 *hdr) { - return ((hdr[0] == 0xFE && hdr[1] == 0xFF) || - (hdr[0] == 0xFF && hdr[1] == 0xFE)); -} - -static_inline bool is_utf32_bom(const u8 *hdr) { - /* need check length to avoid zero padding */ - return ((hdr[0] == 0x00 && hdr[1] == 0x00 && - hdr[2] == 0xFE && hdr[3] == 0xFF) || - (hdr[0] == 0xFF && hdr[1] == 0xFE && - hdr[2] == 0x00 && hdr[3] == 0x00)); -} - -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_UTILS - -/*============================================================================== - * JSON Pointer API (RFC 6901) - *============================================================================*/ - -/** - Get a token from JSON pointer string. - @param ptr [in] string that points to current token prefix `/` - [out] string that points to next token prefix `/`, or string end - @param end [in] end of the entire JSON Pointer string - @param len [out] unescaped token length - @param esc [out] number of escaped characters in this token - @return head of the token, or NULL if syntax error - */ -static_inline const char *ptr_next_token(const char **ptr, const char *end, - usize *len, usize *esc) { - const char *hdr = *ptr + 1; - const char *cur = hdr; - /* skip unescaped characters */ - while (cur < end && *cur != '/' && *cur != '~') cur++; - if (likely(cur == end || *cur != '~')) { - /* no escaped characters, return */ - *ptr = cur; - *len = (usize)(cur - hdr); - *esc = 0; - return hdr; - } else { - /* handle escaped characters */ - usize esc_num = 0; - while (cur < end && *cur != '/') { - if (*cur++ == '~') { - if (cur == end || (*cur != '0' && *cur != '1')) { - *ptr = cur - 1; - return NULL; - } - esc_num++; - } - } - *ptr = cur; - *len = (usize)(cur - hdr) - esc_num; - *esc = esc_num; - return hdr; - } -} - -/** - Convert token string to index. - @param cur [in] token head - @param len [in] token length - @param idx [out] the index number, or USIZE_MAX if token is '-' - @return true if token is a valid array index - */ -static_inline bool ptr_token_to_idx(const char *cur, usize len, usize *idx) { - const char *end = cur + len; - usize num = 0, add; - if (unlikely(len == 0 || len > USIZE_SAFE_DIG)) return false; - if (*cur == '0') { - if (unlikely(len > 1)) return false; - *idx = 0; - return true; - } - if (*cur == '-') { - if (unlikely(len > 1)) return false; - *idx = USIZE_MAX; - return true; - } - for (; cur < end && (add = (usize)((u8)*cur - (u8)'0')) <= 9; cur++) { - num = num * 10 + add; - } - if (unlikely(num == 0 || cur < end)) return false; - *idx = num; - return true; -} - -/** - Compare JSON key with token. - @param key a string key (yyjson_val or yyjson_mut_val) - @param token a JSON pointer token - @param len unescaped token length - @param esc number of escaped characters in this token - @return true if `str` is equals to `token` - */ -static_inline bool ptr_token_eq(void *key, - const char *token, usize len, usize esc) { - yyjson_val *val = (yyjson_val *)key; - if (unsafe_yyjson_get_len(val) != len) return false; - if (likely(!esc)) { - return memcmp(val->uni.str, token, len) == 0; - } else { - const char *str = val->uni.str; - for (; len-- > 0; token++, str++) { - if (*token == '~') { - if (*str != (*++token == '0' ? '~' : '/')) return false; - } else { - if (*str != *token) return false; - } - } - return true; - } -} - -/** - Get a value from array by token. - @param arr an array, should not be NULL or non-array type - @param token a JSON pointer token - @param len unescaped token length - @param esc number of escaped characters in this token - @return value at index, or NULL if token is not index or index is out of range - */ -static_inline yyjson_val *ptr_arr_get(yyjson_val *arr, const char *token, - usize len, usize esc) { - yyjson_val *val = unsafe_yyjson_get_first(arr); - usize num = unsafe_yyjson_get_len(arr), idx = 0; - if (unlikely(num == 0)) return NULL; - if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; - if (unlikely(idx >= num)) return NULL; - if (unsafe_yyjson_arr_is_flat(arr)) { - return val + idx; - } else { - while (idx-- > 0) val = unsafe_yyjson_get_next(val); - return val; - } -} - -/** - Get a value from object by token. - @param obj [in] an object, should not be NULL or non-object type - @param token [in] a JSON pointer token - @param len [in] unescaped token length - @param esc [in] number of escaped characters in this token - @return value associated with the token, or NULL if no value - */ -static_inline yyjson_val *ptr_obj_get(yyjson_val *obj, const char *token, - usize len, usize esc) { - yyjson_val *key = unsafe_yyjson_get_first(obj); - usize num = unsafe_yyjson_get_len(obj); - if (unlikely(num == 0)) return NULL; - for (; num > 0; num--, key = unsafe_yyjson_get_next(key + 1)) { - if (ptr_token_eq(key, token, len, esc)) return key + 1; - } - return NULL; -} - -/** - Get a value from array by token. - @param arr [in] an array, should not be NULL or non-array type - @param token [in] a JSON pointer token - @param len [in] unescaped token length - @param esc [in] number of escaped characters in this token - @param pre [out] previous (sibling) value of the returned value - @param last [out] whether index is last - @return value at index, or NULL if token is not index or index is out of range - */ -static_inline yyjson_mut_val *ptr_mut_arr_get(yyjson_mut_val *arr, - const char *token, - usize len, usize esc, - yyjson_mut_val **pre, - bool *last) { - yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; /* last (tail) */ - usize num = unsafe_yyjson_get_len(arr), idx; - if (last) *last = false; - if (pre) *pre = NULL; - if (unlikely(num == 0)) { - if (last && len == 1 && (*token == '0' || *token == '-')) *last = true; - return NULL; - } - if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; - if (last) *last = (idx == num || idx == USIZE_MAX); - if (unlikely(idx >= num)) return NULL; - while (idx-- > 0) val = val->next; - if (pre) *pre = val; - return val->next; -} - -/** - Get a value from object by token. - @param obj [in] an object, should not be NULL or non-object type - @param token [in] a JSON pointer token - @param len [in] unescaped token length - @param esc [in] number of escaped characters in this token - @param pre [out] previous (sibling) key of the returned value's key - @return value associated with the token, or NULL if no value - */ -static_inline yyjson_mut_val *ptr_mut_obj_get(yyjson_mut_val *obj, - const char *token, - usize len, usize esc, - yyjson_mut_val **pre) { - yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr, *key; - usize num = unsafe_yyjson_get_len(obj); - if (pre) *pre = NULL; - if (unlikely(num == 0)) return NULL; - for (; num > 0; num--, pre_key = key) { - key = pre_key->next->next; - if (ptr_token_eq(key, token, len, esc)) { - if (pre) *pre = pre_key; - return key->next; - } - } - return NULL; -} - -/** - Create a string value with JSON pointer token. - @param token [in] a JSON pointer token - @param len [in] unescaped token length - @param esc [in] number of escaped characters in this token - @param doc [in] used for memory allocation when creating value - @return new string value, or NULL if memory allocation failed - */ -static_inline yyjson_mut_val *ptr_new_key(const char *token, - usize len, usize esc, - yyjson_mut_doc *doc) { - const char *src = token; - if (likely(!esc)) { - return yyjson_mut_strncpy(doc, src, len); - } else { - const char *end = src + len + esc; - char *dst = unsafe_yyjson_mut_str_alc(doc, len + esc); - char *str = dst; - if (unlikely(!dst)) return NULL; - for (; src < end; src++, dst++) { - if (*src != '~') *dst = *src; - else *dst = (*++src == '0' ? '~' : '/'); - } - *dst = '\0'; - return yyjson_mut_strn(doc, str, len); - } -} - -/* macros for yyjson_ptr */ -#define return_err(_ret, _code, _pos, _msg) do { \ - if (err) { \ - err->code = YYJSON_PTR_ERR_##_code; \ - err->msg = _msg; \ - err->pos = (usize)(_pos); \ - } \ - return _ret; \ -} while (false) - -#define return_err_resolve(_ret, _pos) \ - return_err(_ret, RESOLVE, _pos, "JSON pointer cannot be resolved") -#define return_err_syntax(_ret, _pos) \ - return_err(_ret, SYNTAX, _pos, "invalid escaped character") -#define return_err_alloc(_ret) \ - return_err(_ret, MEMORY_ALLOCATION, 0, "failed to create value") - -yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, - const char *ptr, size_t ptr_len, - yyjson_ptr_err *err) { - - const char *hdr = ptr, *end = ptr + ptr_len, *token; - usize len, esc; - yyjson_type type; - - while (true) { - token = ptr_next_token(&ptr, end, &len, &esc); - if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr); - type = unsafe_yyjson_get_type(val); - if (type == YYJSON_TYPE_OBJ) { - val = ptr_obj_get(val, token, len, esc); - } else if (type == YYJSON_TYPE_ARR) { - val = ptr_arr_get(val, token, len, esc); - } else { - val = NULL; - } - if (!val) return_err_resolve(NULL, token - hdr); - if (ptr == end) return val; - } -} - -yyjson_mut_val *unsafe_yyjson_mut_ptr_getx( - yyjson_mut_val *val, const char *ptr, size_t ptr_len, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { - - const char *hdr = ptr, *end = ptr + ptr_len, *token; - usize len, esc; - yyjson_mut_val *ctn, *pre = NULL; - yyjson_type type; - bool idx_is_last = false; - - while (true) { - token = ptr_next_token(&ptr, end, &len, &esc); - if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr); - ctn = val; - type = unsafe_yyjson_get_type(val); - if (type == YYJSON_TYPE_OBJ) { - val = ptr_mut_obj_get(val, token, len, esc, &pre); - } else if (type == YYJSON_TYPE_ARR) { - val = ptr_mut_arr_get(val, token, len, esc, &pre, &idx_is_last); - } else { - val = NULL; - } - if (ctx && (ptr == end)) { - if (type == YYJSON_TYPE_OBJ || - (type == YYJSON_TYPE_ARR && (val || idx_is_last))) { - ctx->ctn = ctn; - ctx->pre = pre; - } - } - if (!val) return_err_resolve(NULL, token - hdr); - if (ptr == end) return val; - } -} - -bool unsafe_yyjson_mut_ptr_putx( - yyjson_mut_val *val, const char *ptr, size_t ptr_len, - yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, - bool insert_new, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { - - const char *hdr = ptr, *end = ptr + ptr_len, *token; - usize token_len, esc, ctn_len; - yyjson_mut_val *ctn, *key, *pre = NULL; - yyjson_mut_val *sep_ctn = NULL, *sep_key = NULL, *sep_val = NULL; - yyjson_type ctn_type; - bool idx_is_last = false; - - /* skip exist parent nodes */ - while (true) { - token = ptr_next_token(&ptr, end, &token_len, &esc); - if (unlikely(!token)) return_err_syntax(false, ptr - hdr); - ctn = val; - ctn_type = unsafe_yyjson_get_type(ctn); - if (ctn_type == YYJSON_TYPE_OBJ) { - val = ptr_mut_obj_get(ctn, token, token_len, esc, &pre); - } else if (ctn_type == YYJSON_TYPE_ARR) { - val = ptr_mut_arr_get(ctn, token, token_len, esc, &pre, - &idx_is_last); - } else return_err_resolve(false, token - hdr); - if (!val) break; - if (ptr == end) break; /* is last token */ - } - - /* create parent nodes if not exist */ - if (unlikely(ptr != end)) { /* not last token */ - if (!create_parent) return_err_resolve(false, token - hdr); - - /* add value at last index if container is array */ - if (ctn_type == YYJSON_TYPE_ARR) { - if (!idx_is_last || !insert_new) { - return_err_resolve(false, token - hdr); - } - val = yyjson_mut_obj(doc); - if (!val) return_err_alloc(false); - - /* delay attaching until all operations are completed */ - sep_ctn = ctn; - sep_key = NULL; - sep_val = val; - - /* move to next token */ - ctn = val; - val = NULL; - ctn_type = YYJSON_TYPE_OBJ; - token = ptr_next_token(&ptr, end, &token_len, &esc); - if (unlikely(!token)) return_err_resolve(false, token - hdr); - } - - /* container is object, create parent nodes */ - while (ptr != end) { /* not last token */ - key = ptr_new_key(token, token_len, esc, doc); - if (!key) return_err_alloc(false); - val = yyjson_mut_obj(doc); - if (!val) return_err_alloc(false); - - /* delay attaching until all operations are completed */ - if (!sep_ctn) { - sep_ctn = ctn; - sep_key = key; - sep_val = val; - } else { - yyjson_mut_obj_add(ctn, key, val); - } - - /* move to next token */ - ctn = val; - val = NULL; - token = ptr_next_token(&ptr, end, &token_len, &esc); - if (unlikely(!token)) return_err_syntax(false, ptr - hdr); - } - } - - /* JSON pointer is resolved, insert or replace target value */ - ctn_len = unsafe_yyjson_get_len(ctn); - if (ctn_type == YYJSON_TYPE_OBJ) { - if (ctx) ctx->ctn = ctn; - if (!val || insert_new) { - /* insert new key-value pair */ - key = ptr_new_key(token, token_len, esc, doc); - if (unlikely(!key)) return_err_alloc(false); - if (ctx) ctx->pre = ctn_len ? (yyjson_mut_val *)ctn->uni.ptr : key; - unsafe_yyjson_mut_obj_add(ctn, key, new_val, ctn_len); - } else { - /* replace exist value */ - key = pre->next->next; - if (ctx) ctx->pre = pre; - if (ctx) ctx->old = val; - yyjson_mut_obj_put(ctn, key, new_val); - } - } else { - /* array */ - if (ctx && (val || idx_is_last)) ctx->ctn = ctn; - if (insert_new) { - /* append new value */ - if (val) { - pre->next = new_val; - new_val->next = val; - if (ctx) ctx->pre = pre; - unsafe_yyjson_set_len(ctn, ctn_len + 1); - } else if (idx_is_last) { - if (ctx) ctx->pre = ctn_len ? - (yyjson_mut_val *)ctn->uni.ptr : new_val; - yyjson_mut_arr_append(ctn, new_val); - } else { - return_err_resolve(false, token - hdr); - } - } else { - /* replace exist value */ - if (!val) return_err_resolve(false, token - hdr); - if (ctn_len > 1) { - new_val->next = val->next; - pre->next = new_val; - if (ctn->uni.ptr == val) ctn->uni.ptr = new_val; - } else { - new_val->next = new_val; - ctn->uni.ptr = new_val; - pre = new_val; - } - if (ctx) ctx->pre = pre; - if (ctx) ctx->old = val; - } - } - - /* all operations are completed, attach the new components to the target */ - if (unlikely(sep_ctn)) { - if (sep_key) yyjson_mut_obj_add(sep_ctn, sep_key, sep_val); - else yyjson_mut_arr_append(sep_ctn, sep_val); - } - return true; -} - -yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex( - yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { - - yyjson_mut_val *cur_val; - yyjson_ptr_ctx cur_ctx; - memset(&cur_ctx, 0, sizeof(cur_ctx)); - if (!ctx) ctx = &cur_ctx; - cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); - if (!cur_val) return NULL; - - if (yyjson_mut_is_obj(ctx->ctn)) { - yyjson_mut_val *key = ctx->pre->next->next; - yyjson_mut_obj_put(ctx->ctn, key, new_val); - } else { - yyjson_ptr_ctx_replace(ctx, new_val); - } - ctx->old = cur_val; - return cur_val; -} - -yyjson_mut_val *unsafe_yyjson_mut_ptr_removex( - yyjson_mut_val *val, const char *ptr, size_t len, - yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { - - yyjson_mut_val *cur_val; - yyjson_ptr_ctx cur_ctx; - memset(&cur_ctx, 0, sizeof(cur_ctx)); - if (!ctx) ctx = &cur_ctx; - cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); - if (cur_val) { - if (yyjson_mut_is_obj(ctx->ctn)) { - yyjson_mut_val *key = ctx->pre->next->next; - yyjson_mut_obj_put(ctx->ctn, key, NULL); - } else { - yyjson_ptr_ctx_remove(ctx); - } - ctx->pre = NULL; - ctx->old = cur_val; - } - return cur_val; -} - -/* macros for yyjson_ptr */ -#undef return_err -#undef return_err_resolve -#undef return_err_syntax -#undef return_err_alloc - - - -/*============================================================================== - * 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 +/** Maximum exact decimal exponent in pow10_sig_table */ +#define POW10_SIG_TABLE_MAX_EXACT_EXP 55 /** Normalized significant 128 bits of pow10, no rounded up (size: 10.4KB). This lookup table is used by both the double number reader and writer. @@ -3537,299 +1896,1211 @@ 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 in range + `POW10_SIG_TABLE_MIN_EXP` to `POW10_SIG_TABLE_MAX_EXP`. + @param hi The highest 64 bits of pow(10, e). + @param lo The lower 64 bits after `hi`. + */ +static_inline void pow10_table_get_sig(i32 exp10, u64 *hi, u64 *lo) { + i32 idx = exp10 - (POW10_SIG_TABLE_MIN_EXP); + *hi = pow10_sig_table[idx * 2]; + *lo = pow10_sig_table[idx * 2 + 1]; +} + +/** + Get the exponent (base 2) for highest 64 bits significand in `pow10_sig_table`. + */ +static_inline void pow10_table_get_exp(i32 exp10, i32 *exp2) { + /* e2 = floor(log2(pow(10, e))) - 64 + 1 */ + /* = floor(e * log2(10) - 63) */ + *exp2 = (exp10 * 217706 - 4128768) >> 16; +} + +#endif + + + +/*============================================================================== + * MARK: - Number and Bit Utils (Private) + *============================================================================*/ + +/** Convert bits to double. */ +static_inline f64 f64_from_bits(u64 u) { + f64 f; + memcpy(&f, &u, sizeof(u)); + return f; +} + +/** Convert double to bits. */ +static_inline u64 f64_to_bits(f64 f) { + u64 u; + memcpy(&u, &f, sizeof(u)); + return u; +} + +/** Convert double to bits. */ +static_inline u32 f32_to_bits(f32 f) { + u32 u; + memcpy(&u, &f, sizeof(u)); + return u; +} + +/** Get 'infinity' bits with sign. */ +static_inline u64 f64_bits_inf(bool sign) { +#if YYJSON_HAS_IEEE_754 + return F64_BITS_INF | ((u64)sign << 63); +#elif defined(INFINITY) + return f64_to_bits(sign ? -INFINITY : INFINITY); +#else + return f64_to_bits(sign ? -HUGE_VAL : HUGE_VAL); +#endif +} + +/** Get 'nan' bits with sign. */ +static_inline u64 f64_bits_nan(bool sign) { +#if YYJSON_HAS_IEEE_754 + return F64_BITS_NAN | ((u64)sign << 63); +#elif defined(NAN) + return f64_to_bits(sign ? (f64)-NAN : (f64)NAN); +#else + return f64_to_bits((sign ? -0.0 : 0.0) / 0.0); +#endif +} + +/** Casting double to float, allow overflow. */ +#if yyjson_has_attribute(no_sanitize) +__attribute__((no_sanitize("undefined"))) +#elif yyjson_gcc_available(4, 9, 0) +__attribute__((__no_sanitize_undefined__)) +#endif +static_inline f32 f64_to_f32(f64 val) { + return (f32)val; +} + +/** Returns the number of leading 0-bits in value (input should not be 0). */ +static_inline u32 u64_lz_bits(u64 v) { +#if GCC_HAS_CLZLL + return (u32)__builtin_clzll(v); +#elif MSC_HAS_BIT_SCAN_64 + unsigned long r; + _BitScanReverse64(&r, v); + return (u32)63 - (u32)r; +#elif MSC_HAS_BIT_SCAN + unsigned long hi, lo; + bool hi_set = _BitScanReverse(&hi, (u32)(v >> 32)) != 0; + _BitScanReverse(&lo, (u32)v); + hi |= 32; + return (u32)63 - (u32)(hi_set ? hi : lo); +#else + /* branchless, use De Bruijn sequence */ + /* see: https://www.chessprogramming.org/BitScan */ + const u8 table[64] = { + 63, 16, 62, 7, 15, 36, 61, 3, 6, 14, 22, 26, 35, 47, 60, 2, + 9, 5, 28, 11, 13, 21, 42, 19, 25, 31, 34, 40, 46, 52, 59, 1, + 17, 8, 37, 4, 23, 27, 48, 10, 29, 12, 43, 20, 32, 41, 53, 18, + 38, 24, 49, 30, 44, 33, 54, 39, 50, 45, 55, 51, 56, 57, 58, 0 + }; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v |= v >> 32; + return table[(v * U64(0x03F79D71, 0xB4CB0A89)) >> 58]; +#endif +} + +/** Returns the number of trailing 0-bits in value (input should not be 0). */ +static_inline u32 u64_tz_bits(u64 v) { +#if GCC_HAS_CTZLL + return (u32)__builtin_ctzll(v); +#elif MSC_HAS_BIT_SCAN_64 + unsigned long r; + _BitScanForward64(&r, v); + return (u32)r; +#elif MSC_HAS_BIT_SCAN + unsigned long lo, hi; + bool lo_set = _BitScanForward(&lo, (u32)(v)) != 0; + _BitScanForward(&hi, (u32)(v >> 32)); + hi += 32; + return lo_set ? lo : hi; +#else + /* branchless, use De Bruijn sequence */ + /* see: https://www.chessprogramming.org/BitScan */ + const u8 table[64] = { + 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, + 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, + 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, + 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12 + }; + return table[((v & (~v + 1)) * U64(0x022FDD63, 0xCC95386D)) >> 58]; +#endif +} + +/** Multiplies two 64-bit unsigned integers (a * b), + returns the 128-bit result as 'hi' and 'lo'. */ +static_inline void u128_mul(u64 a, u64 b, u64 *hi, u64 *lo) { +#if YYJSON_HAS_INT128 + u128 m = (u128)a * b; + *hi = (u64)(m >> 64); + *lo = (u64)(m); +#elif MSC_HAS_UMUL128 + *lo = _umul128(a, b, hi); +#else + u32 a0 = (u32)(a), a1 = (u32)(a >> 32); + u32 b0 = (u32)(b), b1 = (u32)(b >> 32); + u64 p00 = (u64)a0 * b0, p01 = (u64)a0 * b1; + u64 p10 = (u64)a1 * b0, p11 = (u64)a1 * b1; + u64 m0 = p01 + (p00 >> 32); + u32 m00 = (u32)(m0), m01 = (u32)(m0 >> 32); + u64 m1 = p10 + m00; + u32 m10 = (u32)(m1), m11 = (u32)(m1 >> 32); + *hi = p11 + m01 + m11; + *lo = ((u64)m10 << 32) | (u32)p00; +#endif +} + +/** Multiplies two 64-bit unsigned integers and add a value (a * b + c), + returns the 128-bit result as 'hi' and 'lo'. */ +static_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) { +#if YYJSON_HAS_INT128 + u128 m = (u128)a * b + c; + *hi = (u64)(m >> 64); + *lo = (u64)(m); +#else + u64 h, l, t; + u128_mul(a, b, &h, &l); + t = l + c; + h += (u64)(((t < l) | (t < c))); + *hi = h; + *lo = t; +#endif +} + + + +/*============================================================================== + * MARK: - File Utils (Private) + * These functions are used to read and write JSON files. + *============================================================================*/ + +#define YYJSON_FOPEN_E +#if !defined(_MSC_VER) && defined(__GLIBC__) && defined(__GLIBC_PREREQ) +# if __GLIBC_PREREQ(2, 7) +# undef YYJSON_FOPEN_E +# define YYJSON_FOPEN_E "e" /* glibc extension to enable O_CLOEXEC */ +# endif +#endif + +static_inline FILE *fopen_safe(const char *path, const char *mode) { +#if YYJSON_MSC_VER >= 1400 + FILE *file = NULL; + if (fopen_s(&file, path, mode) != 0) return NULL; + return file; +#else + return fopen(path, mode); +#endif +} + +static_inline FILE *fopen_readonly(const char *path) { + return fopen_safe(path, "rb" YYJSON_FOPEN_E); +} + +static_inline FILE *fopen_writeonly(const char *path) { + return fopen_safe(path, "wb" YYJSON_FOPEN_E); +} + +static_inline usize fread_safe(void *buf, usize size, FILE *file) { +#if YYJSON_MSC_VER >= 1400 + return fread_s(buf, size, 1, size, file); +#else + return fread(buf, 1, size, file); +#endif +} + + + +/*============================================================================== + * MARK: - Size Utils (Private) + * These functions are used for memory allocation. + *============================================================================*/ + +/** Returns whether the size is overflow after increment. */ +static_inline bool size_add_is_overflow(usize size, usize add) { + return size > (size + add); +} + +/** Returns whether the size is power of 2 (size should not be 0). */ +static_inline bool size_is_pow2(usize size) { + return (size & (size - 1)) == 0; +} + +/** Align size upwards (may overflow). */ +static_inline usize size_align_up(usize size, usize align) { + if (size_is_pow2(align)) { + return (size + (align - 1)) & ~(align - 1); + } else { + return size + align - (size + align - 1) % align - 1; + } +} + +/** Align size downwards. */ +static_inline usize size_align_down(usize size, usize align) { + if (size_is_pow2(align)) { + return size & ~(align - 1); + } else { + return size - (size % align); + } +} + +/** Align address upwards (may overflow). */ +static_inline void *mem_align_up(void *mem, usize align) { + usize size; + memcpy(&size, &mem, sizeof(usize)); + size = size_align_up(size, align); + memcpy(&mem, &size, sizeof(usize)); + return mem; +} + + + +/*============================================================================== + * MARK: - Default Memory Allocator (Private) + * This is a simple libc memory allocator wrapper. + *============================================================================*/ + +static void *default_malloc(void *ctx, usize size) { + return malloc(size); +} + +static void *default_realloc(void *ctx, void *ptr, usize old_size, usize size) { + return realloc(ptr, size); +} + +static void default_free(void *ctx, void *ptr) { + free(ptr); +} + +static const yyjson_alc YYJSON_DEFAULT_ALC = { + default_malloc, default_realloc, default_free, NULL +}; + + + +/*============================================================================== + * MARK: - Null Memory Allocator (Private) + * This allocator is just a placeholder to ensure that the internal + * malloc/realloc/free function pointers are not null. + *============================================================================*/ + +static void *null_malloc(void *ctx, usize size) { + return NULL; +} + +static void *null_realloc(void *ctx, void *ptr, usize old_size, usize size) { + return NULL; +} + +static void null_free(void *ctx, void *ptr) { + return; +} + +static const yyjson_alc YYJSON_NULL_ALC = { + null_malloc, null_realloc, null_free, NULL +}; + + + +/*============================================================================== + * MARK: - Pool Memory Allocator (Public) + * This allocator is initialized with a fixed-size buffer. + * The buffer is split into multiple memory chunks for memory allocation. + *============================================================================*/ + +/** memory chunk header */ +typedef struct pool_chunk { + usize size; /* chunk memory size, include chunk header */ + struct pool_chunk *next; /* linked list, nullable */ + /* char mem[]; flexible array member */ +} pool_chunk; + +/** allocator ctx header */ +typedef struct pool_ctx { + usize size; /* total memory size, include ctx header */ + pool_chunk *free_list; /* linked list, nullable */ + /* pool_chunk chunks[]; flexible array member */ +} pool_ctx; + +/** align up the input size to chunk size */ +static_inline void pool_size_align(usize *size) { + *size = size_align_up(*size, sizeof(pool_chunk)) + sizeof(pool_chunk); +} + +static void *pool_malloc(void *ctx_ptr, usize size) { + /* assert(size != 0) */ + pool_ctx *ctx = (pool_ctx *)ctx_ptr; + pool_chunk *next, *prev = NULL, *cur = ctx->free_list; + + if (unlikely(size >= ctx->size)) return NULL; + pool_size_align(&size); + + while (cur) { + if (cur->size < size) { + /* not enough space, try next chunk */ + prev = cur; + cur = cur->next; + continue; + } + if (cur->size >= size + sizeof(pool_chunk) * 2) { + /* too much space, split this chunk */ + next = (pool_chunk *)(void *)((u8 *)cur + size); + next->size = cur->size - size; + next->next = cur->next; + cur->size = size; + } else { + /* just enough space, use whole chunk */ + next = cur->next; + } + if (prev) prev->next = next; + else ctx->free_list = next; + return (void *)(cur + 1); + } + return NULL; +} + +static void pool_free(void *ctx_ptr, void *ptr) { + /* assert(ptr != NULL) */ + pool_ctx *ctx = (pool_ctx *)ctx_ptr; + pool_chunk *cur = ((pool_chunk *)ptr) - 1; + pool_chunk *prev = NULL, *next = ctx->free_list; + + while (next && next < cur) { + prev = next; + next = next->next; + } + if (prev) prev->next = cur; + else ctx->free_list = cur; + cur->next = next; + + if (next && ((u8 *)cur + cur->size) == (u8 *)next) { + /* merge cur to higher chunk */ + cur->size += next->size; + cur->next = next->next; + } + if (prev && ((u8 *)prev + prev->size) == (u8 *)cur) { + /* merge cur to lower chunk */ + prev->size += cur->size; + prev->next = cur->next; + } +} + +static void *pool_realloc(void *ctx_ptr, void *ptr, + usize old_size, usize size) { + /* assert(ptr != NULL && size != 0 && old_size < size) */ + pool_ctx *ctx = (pool_ctx *)ctx_ptr; + pool_chunk *cur = ((pool_chunk *)ptr) - 1, *prev, *next, *tmp; + + /* check size */ + if (unlikely(size >= ctx->size)) return NULL; + pool_size_align(&old_size); + pool_size_align(&size); + if (unlikely(old_size == size)) return ptr; + + /* find next and prev chunk */ + prev = NULL; + next = ctx->free_list; + while (next && next < cur) { + prev = next; + next = next->next; + } + + if ((u8 *)cur + cur->size == (u8 *)next && cur->size + next->size >= size) { + /* merge to higher chunk if they are contiguous */ + usize free_size = cur->size + next->size - size; + if (free_size > sizeof(pool_chunk) * 2) { + tmp = (pool_chunk *)(void *)((u8 *)cur + size); + if (prev) prev->next = tmp; + else ctx->free_list = tmp; + tmp->next = next->next; + tmp->size = free_size; + cur->size = size; + } else { + if (prev) prev->next = next->next; + else ctx->free_list = next->next; + cur->size += next->size; + } + return ptr; + } else { + /* fallback to malloc and memcpy */ + void *new_ptr = pool_malloc(ctx_ptr, size - sizeof(pool_chunk)); + if (new_ptr) { + memcpy(new_ptr, ptr, cur->size - sizeof(pool_chunk)); + pool_free(ctx_ptr, ptr); + } + return new_ptr; + } +} + +bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, usize size) { + pool_chunk *chunk; + pool_ctx *ctx; + + if (unlikely(!alc)) return false; + *alc = YYJSON_NULL_ALC; + if (size < sizeof(pool_ctx) * 4) return false; + ctx = (pool_ctx *)mem_align_up(buf, sizeof(pool_ctx)); + if (unlikely(!ctx)) return false; + size -= (usize)((u8 *)ctx - (u8 *)buf); + size = size_align_down(size, sizeof(pool_ctx)); + + chunk = (pool_chunk *)(ctx + 1); + chunk->size = size - sizeof(pool_ctx); + chunk->next = NULL; + ctx->size = size; + ctx->free_list = chunk; + + alc->malloc = pool_malloc; + alc->realloc = pool_realloc; + alc->free = pool_free; + alc->ctx = (void *)ctx; + return true; +} + + + +/*============================================================================== + * MARK: - Dynamic Memory Allocator (Public) + * This allocator allocates memory on demand and does not immediately release + * unused memory. Instead, it places the unused memory into a freelist for + * potential reuse in the future. It is only when the entire allocator is + * destroyed that all previously allocated memory is released at once. + *============================================================================*/ + +/** memory chunk header */ +typedef struct dyn_chunk { + usize size; /* chunk size, include header */ + struct dyn_chunk *next; + /* char mem[]; flexible array member */ +} dyn_chunk; + +/** allocator ctx header */ +typedef struct { + dyn_chunk free_list; /* dummy header, sorted from small to large */ + dyn_chunk used_list; /* dummy header */ +} dyn_ctx; + +/** align up the input size to chunk size */ +static_inline bool dyn_size_align(usize *size) { + usize alc_size = *size + sizeof(dyn_chunk); + alc_size = size_align_up(alc_size, YYJSON_ALC_DYN_MIN_SIZE); + if (unlikely(alc_size < *size)) return false; /* overflow */ + *size = alc_size; + return true; +} + +/** remove a chunk from list (the chunk must already be in the list) */ +static_inline void dyn_chunk_list_remove(dyn_chunk *list, dyn_chunk *chunk) { + dyn_chunk *prev = list, *cur; + for (cur = prev->next; cur; cur = cur->next) { + if (cur == chunk) { + prev->next = cur->next; + cur->next = NULL; + return; + } + prev = cur; + } +} + +/** add a chunk to list header (the chunk must not be in the list) */ +static_inline void dyn_chunk_list_add(dyn_chunk *list, dyn_chunk *chunk) { + chunk->next = list->next; + list->next = chunk; +} + +static void *dyn_malloc(void *ctx_ptr, usize size) { + /* assert(size != 0) */ + const yyjson_alc def = YYJSON_DEFAULT_ALC; + dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; + dyn_chunk *chunk, *prev; + if (unlikely(!dyn_size_align(&size))) return NULL; + + /* freelist is empty, create new chunk */ + if (!ctx->free_list.next) { + chunk = (dyn_chunk *)def.malloc(def.ctx, size); + if (unlikely(!chunk)) return NULL; + chunk->size = size; + chunk->next = NULL; + dyn_chunk_list_add(&ctx->used_list, chunk); + return (void *)(chunk + 1); + } + + /* find a large enough chunk, or resize the largest chunk */ + prev = &ctx->free_list; + while (true) { + chunk = prev->next; + if (chunk->size >= size) { /* enough size, reuse this chunk */ + prev->next = chunk->next; + dyn_chunk_list_add(&ctx->used_list, chunk); + return (void *)(chunk + 1); + } + if (!chunk->next) { /* resize the largest chunk */ + chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); + if (unlikely(!chunk)) return NULL; + prev->next = NULL; + chunk->size = size; + dyn_chunk_list_add(&ctx->used_list, chunk); + return (void *)(chunk + 1); + } + prev = chunk; + } +} + +static void *dyn_realloc(void *ctx_ptr, void *ptr, + usize old_size, usize size) { + /* assert(ptr != NULL && size != 0 && old_size < size) */ + const yyjson_alc def = YYJSON_DEFAULT_ALC; + dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; + dyn_chunk *new_chunk, *chunk = (dyn_chunk *)ptr - 1; + if (unlikely(!dyn_size_align(&size))) return NULL; + if (chunk->size >= size) return ptr; + + dyn_chunk_list_remove(&ctx->used_list, chunk); + new_chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); + if (likely(new_chunk)) { + new_chunk->size = size; + chunk = new_chunk; + } + dyn_chunk_list_add(&ctx->used_list, chunk); + return new_chunk ? (void *)(new_chunk + 1) : NULL; } -/** - 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; +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; + } + } } -#endif +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 Character Matcher + * 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. *============================================================================*/ -/** Character type */ -typedef u8 char_type; +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; -/** Whitespace character: ' ', '\\t', '\\n', '\\r'. */ -static const char_type CHAR_TYPE_SPACE = 1 << 0; + /* 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); -/** Number character: '-', [0-9]. */ -static const char_type CHAR_TYPE_NUMBER = 1 << 1; + /* 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; +} -/** JSON Escaped character: '"', '\', [0x00-0x1F]. */ -static const char_type CHAR_TYPE_ESC_ASCII = 1 << 2; +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; +} -/** Non-ASCII character: [0x80-0xFF]. */ -static const char_type CHAR_TYPE_NON_ASCII = 1 << 3; +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; +} -/** JSON container character: '{', '['. */ -static const char_type CHAR_TYPE_CONTAINER = 1 << 4; +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); + } +} -/** Comment character: '/'. */ -static const char_type CHAR_TYPE_COMMENT = 1 << 5; +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)); -/** Line end character: '\\n', '\\r', '\0'. */ -static const char_type CHAR_TYPE_LINE_END = 1 << 6; + 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; +} -/** Hexadecimal numeric character: [0-9a-fA-F]. */ -static const char_type CHAR_TYPE_HEX = 1 << 7; +yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, const yyjson_alc *alc) { + yyjson_mut_doc *m_doc; + yyjson_mut_val *m_val; -/** 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 -}; + 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; -/** Match a character with specified type. */ -static_inline bool char_is_type(u8 c, char_type type) { - return (char_table[c] & type) != 0; + 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; } -/** Match a whitespace: ' ', '\\t', '\\n', '\\r'. */ -static_inline bool char_is_space(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_SPACE); -} +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; -/** 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)); + 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; } -/** Match a JSON number: '-', [0-9]. */ -static_inline bool char_is_num(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_NUMBER); +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; } -/** Match a JSON container: '{', '['. */ -static_inline bool char_is_container(u8 c) { - return char_is_type(c, (char_type)CHAR_TYPE_CONTAINER); +/* 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; + } } -/** 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)); +/* 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; + } } -/** 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); +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); } -/** 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); -} +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); -/*============================================================================== - * Digit Character Matcher - *============================================================================*/ + /* 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; -/** Digit type */ -typedef u8 digi_type; + /* 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; + } + } -/** Digit: '0'. */ -static const digi_type DIGI_TYPE_ZERO = 1 << 0; + /* copy vals and strs */ + doc->val_read = yyjson_imut_copy(&val_hdr, &str_hdr, mval); + doc->dat_read = str_sum + 1; + return doc; +} -/** Digit: [1-9]. */ -static const digi_type DIGI_TYPE_NONZERO = 1 << 1; +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; +} -/** Plus sign (positive): '+'. */ -static const digi_type DIGI_TYPE_POS = 1 << 2; +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); +} -/** Minus sign (negative): '-'. */ -static const digi_type DIGI_TYPE_NEG = 1 << 3; +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; -/** Decimal point: '.' */ -static const digi_type DIGI_TYPE_DOT = 1 << 4; + 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; + } -/** Exponent sign: 'e, 'E'. */ -static const digi_type DIGI_TYPE_EXP = 1 << 5; + 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; + } -/** 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 -}; + case YYJSON_TYPE_NUM: + return unsafe_yyjson_num_equals(lhs, rhs); -/** Match a character with specified type. */ -static_inline bool digi_is_type(u8 d, digi_type type) { - return (digi_table[d] & type) != 0; -} + case YYJSON_TYPE_RAW: + case YYJSON_TYPE_STR: + return unsafe_yyjson_str_equals(lhs, rhs); -/** Match a sign: '+', '-' */ -static_inline bool digi_is_sign(u8 d) { - return digi_is_type(d, (digi_type)(DIGI_TYPE_POS | DIGI_TYPE_NEG)); -} + case YYJSON_TYPE_NULL: + case YYJSON_TYPE_BOOL: + return lhs->tag == rhs->tag; -/** 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); + default: + return false; + } } -/** 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)); -} +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; -/** Match an exponent sign: 'e', 'E'. */ -static_inline bool digi_is_exp(u8 d) { - return digi_is_type(d, (digi_type)DIGI_TYPE_EXP); -} + 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; + } -/** 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)); -} + 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; -/** 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)); + 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 !YYJSON_DISABLE_READER + 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; +} -/*============================================================================== - * Hex Character Reader - * This function is used by JSON reader to read escaped characters. - *============================================================================*/ -/** - This table is used to convert 4 hex character sequence to a number. - A valid hex character [0-9A-Fa-f] will mapped to it's raw number [0x00, 0x0F], - an invalid hex character will mapped to [0xF0]. - (generate with misc/make_tables.c) - */ -static const u8 hex_conv_table[256] = { - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, - 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0 -}; -/** - Scans an escaped character sequence as a UTF-16 code unit (branchless). - e.g. "\\u005C" should pass "005C" as `cur`. +#if !YYJSON_DISABLE_READER /* reader begin */ - 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; +/* 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; } /*============================================================================== - * JSON Reader Utils + * MARK: - JSON Reader Utils (Private) * These functions are used by JSON reader to read literals and comments. *============================================================================*/ -/** Read 'true' literal, '*cur' should be 't'. */ +/** 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"))) { @@ -3840,7 +3111,7 @@ static_inline bool read_true(u8 **ptr, yyjson_val *val) { return false; } -/** Read 'false' literal, '*cur' should be 'f'. */ +/** 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"))) { @@ -3851,7 +3122,7 @@ static_inline bool read_false(u8 **ptr, yyjson_val *val) { return false; } -/** Read 'null' literal, '*cur' should be 'n'. */ +/** 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"))) { @@ -3862,141 +3133,157 @@ static_inline bool read_null(u8 **ptr, yyjson_val *val) { return false; } -/** Read 'Inf' or 'Infinity' literal (ignoring case). */ -static_inline bool read_inf(bool sign, u8 **ptr, u8 **pre, +/** 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 - sign; + u8 *hdr = *ptr; 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') { - if ((cur[4] == 'N' || cur[4] == 'n') && - (cur[5] == 'I' || cur[5] == 'i') && - (cur[6] == 'T' || cur[6] == 't') && - (cur[7] == 'Y' || cur[7] == 'y')) { + 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 { - /* Don't accept INF as a complete value if it's followed by I. - This is to better support incremental parsing. */ return false; } } else { cur += 3; } *end = cur; - if (has_read_flag(NUMBER_AS_RAW)) { - /* add null-terminator for previous raw string */ - if (*pre) **pre = '\0'; - *pre = 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_raw_get_inf(sign); + val->uni.u64 = f64_bits_inf(sign); } return true; } return false; } -/** Read 'NaN' literal (ignoring case). */ -static_inline bool read_nan(bool sign, u8 **ptr, u8 **pre, +/** Read `NaN` literal (ignoring case). */ +static_inline bool read_nan(u8 **ptr, u8 **pre, yyjson_read_flag flg, yyjson_val *val) { - u8 *hdr = *ptr - sign; + u8 *hdr = *ptr; 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')) { + 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_read_flag(NUMBER_AS_RAW)) { - /* add null-terminator for previous raw string */ - if (*pre) **pre = '\0'; - *pre = 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_raw_get_nan(sign); + 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(bool sign, u8 **ptr, u8 **pre, +/** 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(sign, ptr, pre, flg, val)) return true; - if (read_nan(sign, ptr, pre, flg, val)) return true; + 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; \ + *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 = cur; *end = cur; return true; \ + **pre = '\0'; *pre = cur; *end = cur; return true; \ } while (false) u8 *hdr = *ptr; u8 *cur = *ptr; u8 **end = ptr; - /* 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(); + 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 minus sign"); + return_err(cur, "no digit after sign"); } /* read integral part */ if (*cur == '0') { cur++; - if (unlikely(digi_is_digit(*cur))) { + if (unlikely(char_is_digit(*cur))) { return_err(cur - 1, "number with leading zero is not allowed"); } - if (!digi_is_fp(*cur)) return_raw(); + 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 (digi_is_digit(*cur)) cur++; - if (!digi_is_fp(*cur)) return_raw(); + while (char_is_digit(*cur)) cur++; + if (!char_is_fp(*cur)) return_raw(); } +read_double: /* read fraction part */ if (*cur == '.') { cur++; - if (!digi_is_digit(*cur++)) { - return_err(cur, "no digit after decimal point"); + 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 (digi_is_digit(*cur)) cur++; + while (char_is_digit(*cur)) cur++; } /* read exponent part */ - if (digi_is_exp(*cur)) { - cur += 1 + digi_is_sign(cur[1]); - if (!digi_is_digit(*cur++)) { + 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 (digi_is_digit(*cur)) cur++; + while (char_is_digit(*cur)) cur++; } return_raw(); @@ -4005,67 +3292,169 @@ static_noinline bool read_num_raw(u8 **ptr, u8 **pre, yyjson_read_flag flg, #undef return_raw } -/** - 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) { +/** 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; - while (true) { - if (byte_match_2(cur, "/*")) { - hdr = cur; - cur += 2; - while (true) { - if (byte_match_2(cur, "*/")) { - cur += 2; - break; + 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++; } - if (*cur == 0) { - *end = hdr; - return false; + } 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++; + cur += 2; } - 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; + 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; } - break; } - *end = cur; - return hdr != cur; + return false; } /** Check truncated string. Returns true if `cur` match `str` but is truncated. + The `str` should be lowercase ASCII letters. */ -static_inline bool is_truncated_str(u8 *cur, u8 *end, - const char *str, - bool case_sensitive) { +static bool is_truncated_str(u8 *cur, u8 *eof, const char *str, + bool case_sensitive) { usize len = strlen(str); - if (cur + len <= end || end <= cur) return false; + if (cur + len <= eof || eof <= cur) return false; if (case_sensitive) { - return memcmp(cur, str, (usize)(end - cur)) == 0; + return memcmp(cur, str, (usize)(eof - cur)) == 0; } - for (; cur < end; cur++, str++) { - if ((*cur != (u8)*str) && (*cur != (u8)*str - 'a' + 'A')) { - return false; - } + for (; cur < eof; cur++, str++) { + if (char_to_lower(*cur) != *(const u8 *)str) return false; } return true; } @@ -4074,45 +3463,45 @@ static_inline bool is_truncated_str(u8 *cur, u8 *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, +static_noinline bool is_truncated_end(u8 *hdr, u8 *cur, u8 *eof, yyjson_read_code code, yyjson_read_flag flg) { - if (cur >= end) return true; + if (cur >= eof) 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)) { + 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_read_flag(ALLOW_INF_AND_NAN)) { + if (has_allow(INF_AND_NAN)) { if (*cur == '-') cur++; - if (is_truncated_str(cur, end, "infinity", false) || - is_truncated_str(cur, end, "nan", false)) { + 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_read_flag(ALLOW_INF_AND_NAN)) { + if (has_allow(INF_AND_NAN)) { if (hdr + 3 <= cur && - is_truncated_str(cur - 3, end, "infinity", false)) { + 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)(end - cur); + 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 < end; cur++) { + for (++cur; cur < eof; cur++) { if (!char_is_hex(*cur)) return false; } return true; @@ -4120,120 +3509,69 @@ static_noinline bool is_truncated_end(u8 *hdr, u8 *cur, u8 *end, /* incomplete surrogate pair? */ u16 hi; if (*++cur != 'u') return false; - if (!read_hex_u16(++cur, &hi)) return false; + if (!hex_load_4(++cur, &hi)) return false; if ((hi & 0xF800) != 0xD800) return false; cur += 4; - if (cur >= end) return true; + if (cur >= eof) return true; /* valid low surrogate is DC00...DFFF */ if (*cur != '\\') return false; - if (++cur >= end) return true; + if (++cur >= eof) return true; if (*cur != 'u') return false; - if (++cur >= end) return true; + if (++cur >= eof) return true; if (*cur != 'd' && *cur != 'D') return false; - if (++cur >= end) return true; + if (++cur >= eof) return true; if ((*cur < 'c' || *cur > 'f') && (*cur < 'C' || *cur > 'F')) return false; - if (++cur >= end) return true; + if (++cur >= eof) return true; if (!char_is_hex(*cur)) return false; return true; } return false; } - /* 2 to 4 bytes UTF-8, see `read_str()` 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; - } - } + /* 2 to 4 bytes UTF-8 */ + if (is_truncated_utf8(cur, eof)) { + return true; } } - if (has_read_flag(ALLOW_COMMENTS)) { + 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 == end) { + *cur == '/' && cur + 1 == eof) { /* truncated beginning of comment */ return true; } } if (code == YYJSON_READ_ERROR_UNEXPECTED_CHARACTER && - has_read_flag(ALLOW_BOM)) { + has_allow(BOM)) { /* truncated UTF-8 BOM */ - usize len = (usize)(end - cur); + usize len = (usize)(eof - cur); if (cur == hdr && len < 3 && !memcmp(hdr, "\xEF\xBB\xBF", len)) { return true; - } - } - return false; -} - - - -#if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_READER */ - -/*============================================================================== - * BigInt For Floating Point Number Reader - * - * 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. - *============================================================================*/ - -/** Maximum exponent of exact pow10 */ -#define U64_POW10_MAX_EXP 19 - -/** 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) -}; + } + } + return false; +} -/** Maximum numbers of chunks used by a bigint (58 is enough here). */ -#define BIGINT_MAX_CHUNKS 64 + + +#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[BIGINT_MAX_CHUNKS]; /* chunks */ + u64 bits[64]; /* chunks (58 is enough here) */ } bigint; /** @@ -4310,8 +3648,8 @@ static_inline void bigint_mul_pow2(bigint *big, u32 exp) { @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]); + 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]); @@ -4400,7 +3738,7 @@ static_noinline void bigint_set_buf(bigint *big, u64 sig, i32 *exp, /*============================================================================== - * Diy Floating Point + * MARK: - Diy Floating Point (Private) *============================================================================*/ /** "Do It Yourself Floating Point" struct. */ @@ -4446,7 +3784,7 @@ static_inline u64 diy_fp_to_ieee_raw(diy_fp fp) { if (unlikely(exp >= F64_MAX_BIN_EXP)) { /* overflow */ - return F64_RAW_INF; + return F64_BITS_INF; } else if (likely(exp >= F64_MIN_BIN_EXP - 1)) { /* normal */ exp += F64_EXP_BIAS; @@ -4463,20 +3801,9 @@ static_inline u64 diy_fp_to_ieee_raw(diy_fp fp) { /*============================================================================== - * JSON Number Reader (IEEE-754) + * MARK: - Number Reader (Private) *============================================================================*/ -/** Maximum exact pow10 exponent for double value. */ -#define F64_POW10_EXP_MAX_EXACT 22 - -#if YYJSON_DOUBLE_MATH_CORRECT -/** 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 -}; -#endif - /** Read a JSON number. @@ -4488,7 +3815,6 @@ static const f64 f64_pow10_table[] = { */ 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; \ @@ -4520,13 +3846,13 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, } 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); \ + 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 { \ - if (*pre) **pre = '\0'; /* add null-terminator for previous raw string */ \ + **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; \ @@ -4551,7 +3877,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, bool sign; /* read number as raw string if has `YYJSON_READ_NUMBER_AS_RAW` flag */ - if (has_read_flag(NUMBER_AS_RAW)) { + if (has_flg(NUMBER_AS_RAW)) { return read_num_raw(ptr, pre, flg, val, msg); } @@ -4559,40 +3885,59 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, cur += sign; /* begin with a leading zero or non-digit */ - if (unlikely(!digi_is_nonzero(*cur))) { /* 0 or non-digit char */ + while (unlikely(!char_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; + 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 minus sign"); + return_err(cur, "no digit after sign"); } /* begin with 0 */ - if (likely(!digi_is_digit_or_fp(*++cur))) return_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(!digi_is_digit(*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(digi_is_digit(*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(digi_is_digit(*cur))) { + if (unlikely(char_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))) { + 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 (digi_is_digit(*++cur)); + while (char_is_digit(*++cur)); } return_f64_bin(0); } @@ -4617,10 +3962,10 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, cur += 19; /* skip continuous 19 digits */ - if (!digi_is_digit_or_fp(*cur)) { + 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_read_flag(BIGNUM_AS_RAW)) return_raw(); + if (has_flg(BIGNUM_AS_RAW)) return_raw(); return_f64(unsafe_yyjson_u64_to_f64(sig)); } return_i64(sig); @@ -4631,7 +3976,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, /* process first non-digit character */ #define expr_sepr(i) \ digi_sepr_##i: \ - if (likely(!digi_is_fp(cur[i]))) { cur += i; return_i64(sig); } \ + 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; @@ -4649,7 +3994,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, #undef expr_frac cur += 20; /* skip 19 digits and 1 decimal point */ - if (!digi_is_digit(*cur)) goto digi_frac_end; /* fraction part end */ + if (!char_is_digit(*cur)) goto digi_frac_end; /* fraction part end */ goto digi_frac_more; /* read more digits in fraction part */ @@ -4664,8 +4009,8 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, /* read more digits in integral part */ digi_intg_more: - if (digi_is_digit(*cur)) { - if (!digi_is_digit_or_fp(cur[1])) { + 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)) || @@ -4674,7 +4019,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, cur++; /* convert to double if overflow */ if (sign) { - if (has_read_flag(BIGNUM_AS_RAW)) return_raw(); + if (has_flg(BIGNUM_AS_RAW)) return_raw(); return_f64(unsafe_yyjson_u64_to_f64(sig)); } return_i64(sig); @@ -4682,14 +4027,17 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, } } - if (digi_is_exp(*cur)) { + if (char_is_exp(*cur)) { dot_pos = cur; goto digi_exp_more; } if (*cur == '.') { dot_pos = cur++; - if (!digi_is_digit(*cur)) { + if (unlikely(!char_is_digit(*cur))) { + if (has_allow(EXT_NUMBER)) { + goto digi_frac_end; + } return_err(cur, "no digit after decimal point"); } } @@ -4699,17 +4047,19 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, 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)); + while (char_is_digit(*++cur)); if (!dot_pos) { - if (!digi_is_fp(*cur) && has_read_flag(BIGNUM_AS_RAW)) { + if (!char_is_fp(*cur) && has_flg(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"); + if (unlikely(!char_is_digit(*++cur))) { + if (!has_allow(EXT_NUMBER)) { + return_err(cur, "no digit after decimal point"); + } } - while (digi_is_digit(*cur)) cur++; + while (char_is_digit(*cur)) cur++; } } exp_sig = (i64)(dot_pos - sig_cut); @@ -4717,25 +4067,27 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, /* ignore trailing zeros */ tmp = cur - 1; - while (*tmp == '0' || *tmp == '.') tmp--; + while ((*tmp == '0' || *tmp == '.') && tmp > hdr) tmp--; if (tmp < sig_cut) { sig_cut = NULL; } else { sig_end = cur; } - if (digi_is_exp(*cur)) goto digi_exp_more; + 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)) { - return_err(cur, "no digit after decimal point"); + 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(!digi_is_exp(*cur))) { + if (likely(!char_is_exp(*cur))) { if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) { return_f64_bin(0); /* underflow */ } @@ -4749,15 +4101,15 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, /* read exponent part */ digi_exp_more: exp_sign = (*++cur == '-'); - cur += digi_is_sign(*cur); - if (unlikely(!digi_is_digit(*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 (digi_is_digit(*cur)) { + while (char_is_digit(*cur)) { exp_lit = (i64)((u8)(*cur++ - '0') + (u64)exp_lit * 10); } if (unlikely(cur - tmp >= U64_SAFE_DIG)) { @@ -4798,8 +4150,8 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, */ #if YYJSON_DOUBLE_MATH_CORRECT if (sig < ((u64)1 << 53) && - exp >= -F64_POW10_EXP_MAX_EXACT && - exp <= +F64_POW10_EXP_MAX_EXACT) { + exp >= -F64_POW10_MAX_EXACT_EXP && + exp <= +F64_POW10_MAX_EXACT_EXP) { f64 dbl = (f64)sig; if (exp < 0) { dbl /= f64_pow10_table[-exp]; @@ -5031,7 +4383,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, /* get IEEE double raw value */ raw = diy_fp_to_ieee_raw(fp); - if (unlikely(raw == F64_RAW_INF)) return_inf(); + 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 */ @@ -5073,7 +4425,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, raw += (raw & 1); } - if (unlikely(raw == F64_RAW_INF)) return_inf(); + if (unlikely(raw == F64_BITS_INF)) return_inf(); return_f64_bin(raw); } @@ -5097,7 +4449,6 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, */ 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; \ @@ -5129,16 +4480,15 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, } 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); \ + 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 { \ - 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; \ + **pre = '\0'; *pre = cur; *end = cur; return true; \ } while (false) u64 sig, num; @@ -5150,7 +4500,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, bool sign; /* read number as raw string if has `YYJSON_READ_NUMBER_AS_RAW` flag */ - if (has_read_flag(NUMBER_AS_RAW)) { + if (has_flg(NUMBER_AS_RAW)) { return read_num_raw(ptr, pre, flg, val, msg); } @@ -5159,21 +4509,34 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, 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; + 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 minus sign"); + return_err(cur, "no digit after sign"); } if (*cur == '0') { cur++; - if (unlikely(digi_is_digit(*cur))) { + if (unlikely(char_is_digit(*cur))) { return_err(cur - 1, "number with leading zero is not allowed"); } - if (!digi_is_fp(*cur)) return_0(); + 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; } @@ -5186,7 +4549,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, /* here are 19 continuous digits, skip them */ cur += 19; - if (digi_is_digit(cur[0]) && !digi_is_digit_or_fp(cur[1])) { + 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)) || @@ -5194,7 +4557,7 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, sig = num + sig * 10; cur++; if (sign) { - if (has_read_flag(BIGNUM_AS_RAW)) return_raw(); + if (has_flg(BIGNUM_AS_RAW)) return_raw(); return_f64(unsafe_yyjson_u64_to_f64(sig)); } return_i64(sig); @@ -5203,10 +4566,10 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, intg_end: /* continuous digits ended */ - if (!digi_is_digit_or_fp(*cur)) { + 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_read_flag(BIGNUM_AS_RAW)) return_raw(); + if (has_flg(BIGNUM_AS_RAW)) return_raw(); return_f64(unsafe_yyjson_u64_to_f64(sig)); } return_i64(sig); @@ -5214,28 +4577,33 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, 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)) { + while (char_is_digit(*cur)) cur++; + if (!char_is_fp(*cur) && has_flg(BIGNUM_AS_RAW)) { return_raw(); /* it's a large integer */ } - if (*cur == '.') { + while (*cur == '.') { /* skip fraction part */ dot = cur; cur++; - if (!digi_is_digit(*cur)) { - return_err(cur, "no digit after decimal point"); + if (!char_is_digit(*cur)) { + if (has_allow(EXT_NUMBER)) { + break; + } else { + return_err(cur, "no digit after decimal point"); + } } cur++; - while (digi_is_digit(*cur)) cur++; + while (char_is_digit(*cur)) cur++; + break; } - if (digi_is_exp(*cur)) { + if (char_is_exp(*cur)) { /* skip exponent part */ - cur += 1 + digi_is_sign(cur[1]); - if (!digi_is_digit(*cur)) { + cur += 1 + char_is_sign(cur[1]); + if (!char_is_digit(*cur)) { return_err(cur, "no digit after exponent sign"); } cur++; - while (digi_is_digit(*cur)) cur++; + while (char_is_digit(*cur)) cur++; } /* @@ -5285,147 +4653,85 @@ static_inline bool read_num(u8 **ptr, u8 **pre, yyjson_read_flag flg, /*============================================================================== - * JSON String Reader + * MARK: - String Reader (Private) *============================================================================*/ +/** Read unicode escape sequence. */ +static_inline bool read_uni_esc(u8 **src_ptr, u8 **dst_ptr, const char **msg) { +#define return_err(_end, _msg) *msg = _msg; *src_ptr = _end; return false + + u8 *src = *src_ptr; + u8 *dst = *dst_ptr; + u16 hi, lo; + u32 uni; + + src += 2; /* skip `\u` */ + if (unlikely(!hex_load_4(src, &hi))) { + return_err(src - 2, "invalid escaped sequence in string"); + } + src += 4; /* skip hex */ + if (likely((hi & 0xF800) != 0xD800)) { + /* a BMP character */ + if (hi >= 0x800) { + *dst++ = (u8)(0xE0 | (hi >> 12)); + *dst++ = (u8)(0x80 | ((hi >> 6) & 0x3F)); + *dst++ = (u8)(0x80 | (hi & 0x3F)); + } else if (hi >= 0x80) { + *dst++ = (u8)(0xC0 | (hi >> 6)); + *dst++ = (u8)(0x80 | (hi & 0x3F)); + } else { + *dst++ = (u8)hi; + } + } else { + /* a non-BMP character, represented as a surrogate pair */ + if (unlikely((hi & 0xFC00) != 0xD800)) { + return_err(src - 6, "invalid high surrogate in string"); + } + if (unlikely(!byte_match_2(src, "\\u"))) { + return_err(src - 6, "no low surrogate in string"); + } + if (unlikely(!hex_load_4(src + 2, &lo))) { + return_err(src - 6, "invalid escape in string"); + } + if (unlikely((lo & 0xFC00) != 0xDC00)) { + return_err(src - 6, "invalid low surrogate in string"); + } + uni = ((((u32)hi - 0xD800) << 10) | + ((u32)lo - 0xDC00)) + 0x10000; + *dst++ = (u8)(0xF0 | (uni >> 18)); + *dst++ = (u8)(0x80 | ((uni >> 12) & 0x3F)); + *dst++ = (u8)(0x80 | ((uni >> 6) & 0x3F)); + *dst++ = (u8)(0x80 | (uni & 0x3F)); + src += 6; + } + *src_ptr = src; + *dst_ptr = dst; + return true; +#undef return_err +} + /** Read a JSON string. - @param ptr The head pointer of string before '"' prefix (inout). - @param lst JSON last position. - @param inv Allow invalid unicode. + @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(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, - const char **msg, u8 **con) { +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]) { /* - 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) \ -) + GCC may sometimes load variables into registers too early, causing + unnecessary instructions and performance degradation. This inline assembly + serves as a hint to GCC: 'This variable will be modified, so avoid loading + it too early.' Other compilers like MSVC, Clang, and ICC can generate the + expected instructions without needing this hint. + Check out this example: https://godbolt.org/z/YG6a5W5Ec + */ #define return_err(_end, _msg) do { \ *msg = _msg; \ *end = _end; \ @@ -5433,36 +4739,36 @@ static_inline bool read_str(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, return false; \ } while (false) - u8 *cur = *ptr; + u8 *hdr = *ptr + 1; u8 **end = ptr; - u8 *src = ++cur, *dst = NULL, *pos; + u8 *src = hdr, *dst = NULL, *pos; u16 hi, lo; u32 uni, tmp; - if (unlikely(con && con[0])) { - /* Resume incremental parsing. */ + /* 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. */ - -skip_ascii_begin: /* + Most strings have no escaped characters, so we can jump them quickly. + We want to make loop unrolling, as shown in the following code. Some compiler may not generate instructions as expected, so we rewrite it with explicit goto statements. We hope the compiler can generate instructions like this: https://godbolt.org/z/8vjsYq - while (true) repeat16({ - if (likely(!(char_is_ascii_stop(*src)))) src++; - else break; - }) + while (true) repeat16({ + if (likely((char_is_ascii_skip(*src)))) src++; + else break; + }) */ + if (quo == '"') { #define expr_jump(i) \ - if (likely(!char_is_ascii_stop(src[i]))) {} \ + if (likely(char_is_ascii_skip(src[i]))) {} \ else goto skip_ascii_stop##i; #define expr_stop(i) \ @@ -5472,29 +4778,36 @@ static_inline bool read_str(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, repeat16_incr(expr_jump) src += 16; - goto skip_ascii_begin; + goto skip_ascii; repeat16_incr(expr_stop) #undef expr_jump #undef expr_stop + } else { +#define expr_jump(i) \ + if (likely(char_is_ascii_skip_sq(src[i]))) {} \ + else goto skip_ascii_stop_sq##i; -skip_ascii_end: +#define expr_stop(i) \ + skip_ascii_stop_sq##i: \ + src += i; \ + goto 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". + repeat16_incr(expr_jump) + src += 16; + goto skip_ascii; + repeat16_incr(expr_stop) - 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; +#undef expr_jump +#undef expr_stop + } + +skip_ascii_end: + gcc_store_barrier(*src); + if (likely(*src == quo)) { + val->tag = ((u64)(src - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR | + (quo == '"' ? YYJSON_SUBTYPE_NOESC : 0); + val->uni.str = (const char *)hdr; *src = '\0'; *end = src + 1; if (con) con[0] = con[1] = NULL; @@ -5527,23 +4840,23 @@ static_inline bool read_str(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, }) #else uni = byte_load_4(src); - while (is_valid_seq_3(uni)) { + while (is_utf8_seq3(uni)) { src += 3; uni = byte_load_4(src); } - if (is_valid_seq_1(uni)) goto skip_ascii; - while (is_valid_seq_2(uni)) { + if (is_utf8_seq1(uni)) goto skip_ascii; + while (is_utf8_seq2(uni)) { src += 2; uni = byte_load_4(src); } - while (is_valid_seq_4(uni)) { + while (is_utf8_seq4(uni)) { src += 4; uni = byte_load_4(src); } #endif if (unlikely(pos == src)) { - if (!inv) return_err(src, "invalid UTF-8 encoding in string"); - ++src; + if (has_allow(INVALID_UNICODE)) ++src; + else return_err(src, "invalid UTF-8 encoding in string"); } goto skip_ascii; } @@ -5562,57 +4875,70 @@ static_inline bool read_str(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, 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; + 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 { - /* 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(!read_hex_u16(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; + return_err(src - 1, "invalid escaped sequence in string"); } - break; - default: return_err(src - 1, "invalid escaped sequence in string"); + } } - } else if (likely(*src == '"')) { - val->tag = ((u64)(dst - cur) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; - val->uni.str = (const char *)cur; + } 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 (!inv) return_err(src, "unexpected control character in string"); - if (src >= lst) return_err(src, "unclosed string"); + if (!has_allow(INVALID_UNICODE)) { + return_err(src, "unexpected control character in string"); + } + if (src >= eof) return_err(src, "unclosed string"); *dst++ = *src++; } @@ -5620,119 +4946,40 @@ static_inline bool read_str(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, /* Copy continuous ASCII, loop unrolling, same as the following code: - while (true) repeat16({ - if (unlikely(char_is_ascii_stop(*src))) break; - *dst++ = *src++; - }) + while (true) repeat16({ + if (char_is_ascii_skip(*src)) *dst++ = *src++; + else break; + }) */ -#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 + if (quo == '"') { +#define expr_jump(i) \ + if (likely((char_is_ascii_skip(src[i])))) {} \ + else { gcc_store_barrier(src[i]); goto copy_ascii_stop_##i; } + repeat16_incr(expr_jump) +#undef expr_jump + } else { +#define expr_jump(i) \ + if (likely((char_is_ascii_skip_sq(src[i])))) {} \ + else { gcc_store_barrier(src[i]); goto copy_ascii_stop_##i; } repeat16_incr(expr_jump) #undef expr_jump + } byte_move_16(dst, src); - src += 16; - dst += 16; + dst += 16; src += 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. + The memory is copied forward since `dst < src`. + So it's safe to move one extra byte to reduce instruction count. */ -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; +#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 */ @@ -5740,53 +4987,49 @@ static_inline bool read_str(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, uni = byte_load_4(src); #if YYJSON_DISABLE_UTF8_VALIDATION while (true) repeat4({ - if ((uni & b3_mask) == b3_patt) { + if ((uni & utf8_seq(b3_mask)) == utf8_seq(b3_patt)) { byte_copy_4(dst, &uni); - dst += 3; - src += 3; + dst += 3; src += 3; uni = byte_load_4(src); } else break; }) - if ((uni & b1_mask) == b1_patt) goto copy_ascii; + if ((uni & utf8_seq(b1_mask)) == utf8_seq(b1_patt)) goto copy_ascii; while (true) repeat4({ - if ((uni & b2_mask) == b2_patt) { + if ((uni & utf8_seq(b2_mask)) == utf8_seq(b2_patt)) { byte_copy_2(dst, &uni); - dst += 2; - src += 2; + dst += 2; src += 2; uni = byte_load_4(src); } else break; }) while (true) repeat4({ - if ((uni & b4_mask) == b4_patt) { + if ((uni & utf8_seq(b4_mask)) == utf8_seq(b4_patt)) { byte_copy_4(dst, &uni); - dst += 4; - src += 4; + dst += 4; src += 4; uni = byte_load_4(src); } else break; }) #else - while (is_valid_seq_3(uni)) { + while (is_utf8_seq3(uni)) { byte_copy_4(dst, &uni); - dst += 3; - src += 3; + dst += 3; src += 3; uni = byte_load_4(src); } - if (is_valid_seq_1(uni)) goto copy_ascii; - while (is_valid_seq_2(uni)) { + if (is_utf8_seq1(uni)) goto copy_ascii; + while (is_utf8_seq2(uni)) { byte_copy_2(dst, &uni); - dst += 2; - src += 2; + dst += 2; src += 2; uni = byte_load_4(src); } - while (is_valid_seq_4(uni)) { + while (is_utf8_seq4(uni)) { byte_copy_4(dst, &uni); - dst += 4; - src += 4; + dst += 4; src += 4; uni = byte_load_4(src); } #endif if (unlikely(pos == src)) { - if (!inv) return_err(src, MSG_ERR_UTF8); + if (!has_allow(INVALID_UNICODE)) { + return_err(src, MSG_ERR_UTF8); + } goto copy_ascii_stop_1; } goto copy_ascii; @@ -5794,16 +5037,169 @@ static_inline bool read_str(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, 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 +} + +static_inline bool read_str(u8 **ptr, u8 *eof, yyjson_read_flag flg, + yyjson_val *val, const char **msg) { + return read_str_opt('\"', ptr, eof, flg, val, msg, NULL); +} + +static_inline bool read_str_con(u8 **ptr, u8 *eof, yyjson_read_flag flg, + yyjson_val *val, const char **msg, u8 **con) { + return read_str_opt('\"', ptr, eof, flg, val, msg, con); +} + +static_noinline bool read_str_sq(u8 **ptr, u8 *eof, yyjson_read_flag flg, + yyjson_val *val, const char **msg) { + return read_str_opt('\'', ptr, eof, flg, val, msg, NULL); +} + +/** Read unquoted key (identifier name). */ +static_noinline bool read_str_id(u8 **ptr, u8 *eof, yyjson_read_flag flg, + u8 **pre, yyjson_val *val, const char **msg) { +#define return_err(_end, _msg) do { \ + *msg = _msg; \ + *end = _end; \ + return false; \ +} while (false) + +#define return_suc(_str_end, _cur_end) do { \ + val->tag = ((u64)(_str_end - hdr) << YYJSON_TAG_BIT) | \ + (u64)(YYJSON_TYPE_STR); \ + val->uni.str = (const char *)hdr; \ + *pre = _str_end; *end = _cur_end; \ + return true; \ +} while (false) + + u8 *hdr = *ptr; + u8 **end = ptr; + u8 *src = hdr, *dst = NULL; + u16 hi, lo; + u32 uni, tmp; + + /* add null-terminator for previous raw string */ + **pre = '\0'; + +skip_ascii: +#define expr_jump(i) \ + if (likely(char_is_id_ascii(src[i]))) {} \ + else goto skip_ascii_stop##i; + +#define expr_stop(i) \ + skip_ascii_stop##i: \ + src += i; \ + goto skip_ascii_end; + + repeat16_incr(expr_jump) + src += 16; + goto skip_ascii; + repeat16_incr(expr_stop) + +#undef expr_jump +#undef expr_stop + +skip_ascii_end: + gcc_store_barrier(*src); + if (likely(!char_is_id_next(*src))) { + return_suc(src, src); + } + +skip_utf8: + while (*src >= 0x80) { + if (has_allow(EXT_WHITESPACE)) { + if (char_is_space_ext(*src) && ext_space_len(src)) { + return_suc(src, src); + } + } + uni = byte_load_4(src); + if (is_utf8_seq2(uni)) { + src += 2; + } else if (is_utf8_seq3(uni)) { + src += 3; + } else if (is_utf8_seq4(uni)) { + src += 4; + } else { +#if !YYJSON_DISABLE_UTF8_VALIDATION + if (!has_allow(INVALID_UNICODE)) return_err(src, MSG_ERR_UTF8); +#endif + src += 1; + } + } + if (char_is_id_ascii(*src)) goto skip_ascii; + + /* The escape character appears, we need to copy it. */ + dst = src; +copy_escape: + if (byte_match_2(src, "\\u")) { + if (!read_uni_esc(&src, &dst, msg)) return_err(src, *msg); + } else { + if (!char_is_id_next(*src)) return_suc(dst, src); + return_err(src, "unexpected character in key"); + } + +copy_ascii: + /* + Copy continuous ASCII, loop unrolling, same as the following code: + + while (true) repeat16({ + if (char_is_ascii_skip(*src)) *dst++ = *src++; + else break; + }) + */ +#define expr_jump(i) \ + if (likely((char_is_id_ascii(src[i])))) {} \ + else { gcc_store_barrier(src[i]); goto copy_ascii_stop_##i; } + repeat16_incr(expr_jump) +#undef expr_jump + + byte_move_16(dst, src); + dst += 16; src += 16; + goto copy_ascii; + +#define expr_jump(i) \ + copy_ascii_stop_##i: \ + byte_move_forward(dst, src, i); \ + dst += i; src += i; \ + goto copy_utf8; + repeat16_incr(expr_jump) +#undef expr_jump + +copy_utf8: + while (*src >= 0x80) { /* non-ASCII character */ + if (has_allow(EXT_WHITESPACE)) { + if (char_is_space_ext(*src) && ext_space_len(src)) { + return_suc(dst, src); + } + } + uni = byte_load_4(src); + if (is_utf8_seq2(uni)) { + byte_copy_2(dst, &uni); + dst += 2; src += 2; + } else if (is_utf8_seq3(uni)) { + byte_copy_4(dst, &uni); + dst += 3; src += 3; + } else if (is_utf8_seq4(uni)) { + byte_copy_4(dst, &uni); + dst += 4; src += 4; + } else { +#if !YYJSON_DISABLE_UTF8_VALIDATION + if (!has_allow(INVALID_UNICODE)) return_err(src, MSG_ERR_UTF8); +#endif + *dst = *src; + dst += 1; src += 1; + } + } + if (char_is_id_ascii(*src)) goto copy_ascii; + goto copy_escape; + +#undef return_err +#undef return_suc } /*============================================================================== - * JSON Reader Implementation + * MARK: - JSON Reader Implementation (Private) * * We use goto statements to build the finite state machine (FSM). * The FSM's state was held by program counter (PC) and the 'goto' make the @@ -5811,14 +5207,13 @@ static_inline bool read_str(u8 **ptr, u8 *lst, bool inv, yyjson_val *val, *============================================================================*/ /** Read single value JSON document. */ -static_noinline yyjson_doc *read_root_single(u8 *hdr, u8 *cur, u8 *end, +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, end, YYJSON_READ_ERROR_##_code, flg)) { \ - err->pos = (usize)(end - hdr); \ + 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 { \ @@ -5826,7 +5221,7 @@ static_noinline yyjson_doc *read_root_single(u8 *hdr, u8 *cur, u8 *end, err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ - if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ + if (val_hdr) alc.free(alc.ctx, val_hdr); \ return NULL; \ } while (false) @@ -5837,10 +5232,9 @@ static_noinline yyjson_doc *read_root_single(u8 *hdr, u8 *cur, u8 *end, 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 */ + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; @@ -5849,17 +5243,13 @@ static_noinline yyjson_doc *read_root_single(u8 *hdr, u8 *cur, u8 *end, 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_num(*cur)) { if (likely(read_num(&cur, pre, flg, val, &msg))) goto doc_end; goto fail_number; } if (*cur == '"') { - if (likely(read_str(&cur, end, inv, val, &msg, NULL))) goto doc_end; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto doc_end; goto fail_string; } if (*cur == 't') { @@ -5872,42 +5262,45 @@ static_noinline yyjson_doc *read_root_single(u8 *hdr, u8 *cur, u8 *end, } 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; + if (has_allow(INF_AND_NAN)) { + if (read_nan(&cur, pre, flg, val)) goto doc_end; } goto fail_literal_null; } - if (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_inf_or_nan(false, &cur, pre, flg, val)) goto doc_end; + if (has_allow(INF_AND_NAN)) { + if (read_inf_or_nan(&cur, pre, flg, val)) goto doc_end; + } + if (has_allow(SINGLE_QUOTED_STR) && *cur == '\'') { + if (likely(read_str_sq(&cur, eof, flg, val, &msg))) goto doc_end; + goto fail_string; } goto fail_character; doc_end: /* check invalid contents after json document */ - if (unlikely(cur < 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; + 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; } - } else { - while (char_is_space(*cur)) cur++; } - if (unlikely(cur < end)) goto fail_garbage; + if (unlikely(cur < eof)) goto fail_garbage; } - if (pre && *pre) **pre = '\0'; + **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; + 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_CHAT_T); +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); @@ -5918,14 +5311,13 @@ fail_garbage: return_err(cur, UNEXPECTED_CONTENT, MSG_GARBAGE); } /** Read JSON document (accept all style, but optimized for minify). */ -static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, +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, end, YYJSON_READ_ERROR_##_code, flg)) { \ - err->pos = (usize)(end - hdr); \ + 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 { \ @@ -5933,7 +5325,7 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ - if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ + if (val_hdr) alc.free(alc.ctx, val_hdr); \ return NULL; \ } while (false) @@ -5970,12 +5362,11 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, 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 */ + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ - dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur); + 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); @@ -5988,10 +5379,6 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, 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; @@ -6035,7 +5422,7 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, if (*cur == '"') { val_incr(); ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, NULL))) goto arr_val_end; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto arr_val_end; goto fail_string; } if (*cur == 't') { @@ -6054,15 +5441,15 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, 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; + 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_read_flag(ALLOW_TRAILING_COMMAS)) goto arr_end; + if (has_allow(TRAILING_COMMAS)) goto arr_end; while (*cur != ',') cur--; goto fail_trailing_comma; } @@ -6070,16 +5457,22 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, while (char_is_space(*++cur)); goto arr_val_begin; } - if (has_read_flag(ALLOW_INF_AND_NAN) && + if (has_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; + if (read_inf_or_nan(&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; + 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; @@ -6096,9 +5489,9 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, 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; + 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; @@ -6135,23 +5528,35 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, if (likely(*cur == '"')) { val_incr(); ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, NULL))) goto obj_key_end; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto obj_key_end; goto fail_string; } if (likely(*cur == '}')) { cur++; if (likely(ctn_len == 0)) goto obj_end; - if (has_read_flag(ALLOW_TRAILING_COMMAS)) goto obj_end; + if (has_allow(TRAILING_COMMAS)) goto obj_end; while (*cur != ',') cur--; goto fail_trailing_comma; } - if (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto obj_key_begin; + if (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_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_key_begin; - if (byte_match_2(cur, "/*")) goto fail_comment; + 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; @@ -6164,9 +5569,9 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, 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; + 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; @@ -6174,7 +5579,7 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, if (*cur == '"') { val++; ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, NULL))) goto obj_val_end; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto obj_val_end; goto fail_string; } if (char_is_num(*cur)) { @@ -6207,8 +5612,8 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, 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; + if (has_allow(INF_AND_NAN)) { + if (read_nan(&cur, pre, flg, val)) goto obj_val_end; } goto fail_literal_null; } @@ -6216,16 +5621,22 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, while (char_is_space(*++cur)); goto obj_val_begin; } - if (has_read_flag(ALLOW_INF_AND_NAN) && + if (has_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; + if (read_inf_or_nan(&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; + 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; @@ -6242,9 +5653,9 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, 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; + 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; @@ -6265,30 +5676,30 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *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; - } else { - while (char_is_space(*cur)) cur++; + 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 < end)) goto fail_garbage; + if (unlikely(cur < eof)) goto fail_garbage; } - if (pre && *pre) **pre = '\0'; + **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; + 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_CHAT_T); +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); @@ -6304,14 +5715,13 @@ fail_garbage: return_err(cur, UNEXPECTED_CONTENT, MSG_GARBAGE); } /** Read JSON document (accept all style, but optimized for pretty). */ -static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, +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, end, YYJSON_READ_ERROR_##_code, flg)) { \ - err->pos = (usize)(end - hdr); \ + 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 { \ @@ -6319,7 +5729,7 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ - if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ + if (val_hdr) alc.free(alc.ctx, val_hdr); \ return NULL; \ } while (false) @@ -6356,12 +5766,11 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, 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 */ + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ - dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur); + 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); @@ -6374,10 +5783,6 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, 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; @@ -6436,7 +5841,7 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, if (*cur == '"') { val_incr(); ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, NULL))) goto arr_val_end; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto arr_val_end; goto fail_string; } if (*cur == 't') { @@ -6455,15 +5860,15 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, 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; + 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_read_flag(ALLOW_TRAILING_COMMAS)) goto arr_end; + if (has_allow(TRAILING_COMMAS)) goto arr_end; while (*cur != ',') cur--; goto fail_trailing_comma; } @@ -6471,16 +5876,22 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, while (char_is_space(*++cur)); goto arr_val_begin; } - if (has_read_flag(ALLOW_INF_AND_NAN) && + if (has_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; + if (read_inf_or_nan(&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; + 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; @@ -6501,9 +5912,9 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, 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; + 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; @@ -6553,13 +5964,13 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, if (likely(*cur == '"')) { val_incr(); ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, NULL))) goto obj_key_end; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto obj_key_end; goto fail_string; } if (likely(*cur == '}')) { cur++; if (likely(ctn_len == 0)) goto obj_end; - if (has_read_flag(ALLOW_TRAILING_COMMAS)) goto obj_end; + if (has_allow(TRAILING_COMMAS)) goto obj_end; while (*cur != ',') cur--; goto fail_trailing_comma; } @@ -6567,9 +5978,21 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, 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; + 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; @@ -6586,9 +6009,9 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, 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; + 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; @@ -6596,7 +6019,7 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, if (*cur == '"') { val++; ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, NULL))) goto obj_val_end; + if (likely(read_str(&cur, eof, flg, val, &msg))) goto obj_val_end; goto fail_string; } if (char_is_num(*cur)) { @@ -6629,8 +6052,8 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, 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; + if (has_allow(INF_AND_NAN)) { + if (read_nan(&cur, pre, flg, val)) goto obj_val_end; } goto fail_literal_null; } @@ -6638,16 +6061,22 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, while (char_is_space(*++cur)); goto obj_val_begin; } - if (has_read_flag(ALLOW_INF_AND_NAN) && + if (has_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; + if (read_inf_or_nan(&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; + 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; @@ -6668,9 +6097,9 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, 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; + 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; @@ -6692,30 +6121,30 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *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; - } else { - while (char_is_space(*cur)) cur++; + 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 < end)) goto fail_garbage; + if (unlikely(cur < eof)) goto fail_garbage; } - if (pre && *pre) **pre = '\0'; + **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; + 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_CHAT_T); +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); @@ -6733,36 +6162,35 @@ fail_garbage: return_err(cur, UNEXPECTED_CONTENT, MSG_GARBAGE); /*============================================================================== - * JSON Reader Entrance + * MARK: - JSON Reader (Public) *============================================================================*/ yyjson_doc *yyjson_read_opts(char *dat, usize len, yyjson_read_flag flg, const yyjson_alc *alc_ptr, yyjson_read_err *err) { - #define return_err(_pos, _code, _msg) do { \ err->pos = (usize)(_pos); \ err->msg = _msg; \ err->code = YYJSON_READ_ERROR_##_code; \ - if (!has_read_flag(INSITU) && hdr) alc.free(alc.ctx, (void *)hdr); \ + if (!has_flg(INSITU) && hdr) alc.free(alc.ctx, (void *)hdr); \ return NULL; \ } while (false) - yyjson_read_err dummy_err; + yyjson_read_err tmp_err; yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; yyjson_doc *doc; - u8 *hdr = NULL, *end, *cur; + u8 *hdr = NULL, *eof, *cur; /* validate input parameters */ - if (!err) err = &dummy_err; + 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_read_flag(INSITU)) { + if (has_flg(INSITU)) { hdr = (u8 *)dat; - end = (u8 *)dat + len; + eof = (u8 *)dat + len; cur = (u8 *)dat; } else { if (unlikely(len >= USIZE_MAX - YYJSON_PADDING_SIZE)) { @@ -6772,41 +6200,40 @@ yyjson_doc *yyjson_read_opts(char *dat, usize len, if (unlikely(!hdr)) { return_err(0, MEMORY_ALLOCATION, MSG_MALLOC); } - end = hdr + len; + eof = hdr + len; cur = hdr; memcpy(hdr, dat, len); - memset(end, 0, YYJSON_PADDING_SIZE); } + memset(eof, 0, YYJSON_PADDING_SIZE); - if (has_read_flag(ALLOW_BOM)) { + if (has_allow(BOM)) { if (len >= 3 && is_utf8_bom(cur)) cur += 3; } /* 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, MSG_COMMENT); - } - } else { - if (likely(char_is_space(*cur))) { - while (char_is_space(*++cur)); + if (unlikely(!char_is_ctn(*cur))) { + while (char_is_space(*cur)) cur++; + if (unlikely(!char_is_ctn(*cur))) { + if (has_allow(TRIVIA) && char_is_trivia(*cur)) { + if (!skip_trivia(&cur, eof, flg) && cur == eof) { + return_err(cur - hdr, INVALID_COMMENT, MSG_COMMENT); + } } } - if (unlikely(cur >= end)) { + if (unlikely(cur >= eof)) { return_err(0, EMPTY_CONTENT, "input data is empty"); } } /* read json document */ - if (likely(char_is_container(*cur))) { + if (likely(char_is_ctn(*cur))) { if (char_is_space(cur[1]) && char_is_space(cur[2])) { - doc = read_root_pretty(hdr, cur, end, alc, flg, err); + doc = read_root_pretty(hdr, cur, eof, alc, flg, err); } else { - doc = read_root_minify(hdr, cur, end, alc, flg, err); + doc = read_root_minify(hdr, cur, eof, alc, flg, err); } } else { - doc = read_root_single(hdr, cur, end, alc, flg, err); + doc = read_root_single(hdr, cur, eof, alc, flg, err); } /* check result */ @@ -6819,35 +6246,228 @@ yyjson_doc *yyjson_read_opts(char *dat, usize len, 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_read_flag(INSITU)) alc.free(alc.ctx, (void *)hdr); + if (!has_flg(INSITU)) alc.free(alc.ctx, hdr); } return doc; #undef return_err } +yyjson_doc *yyjson_read_file(const char *path, + yyjson_read_flag flg, + const yyjson_alc *alc_ptr, + yyjson_read_err *err) { +#define return_err(_code, _msg) do { \ + err->pos = 0; \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_##_code; \ + return NULL; \ +} while (false) + + yyjson_read_err tmp_err; + yyjson_doc *doc; + FILE *file; + + if (!err) err = &tmp_err; + if (unlikely(!path)) return_err(INVALID_PARAMETER, "input path is NULL"); + + file = fopen_readonly(path); + if (unlikely(!file)) return_err(FILE_OPEN, MSG_FREAD); + + doc = yyjson_read_fp(file, flg, alc_ptr, err); + fclose(file); + return doc; + +#undef return_err +} + +yyjson_doc *yyjson_read_fp(FILE *file, + yyjson_read_flag flg, + const yyjson_alc *alc_ptr, + yyjson_read_err *err) { +#define return_err(_code, _msg) do { \ + err->pos = 0; \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_##_code; \ + if (buf) alc.free(alc.ctx, buf); \ + return NULL; \ +} while (false) + + yyjson_read_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + yyjson_doc *doc; + + long file_size = 0, file_pos; + void *buf = NULL; + usize buf_size = 0; + + /* validate input parameters */ + if (!err) err = &tmp_err; + if (unlikely(!file)) return_err(INVALID_PARAMETER, "input file is NULL"); + + /* get current position */ + file_pos = ftell(file); + if (file_pos != -1) { + /* get total file size, may fail */ + if (fseek(file, 0, SEEK_END) == 0) file_size = ftell(file); + /* reset to original position, may fail */ + if (fseek(file, file_pos, SEEK_SET) != 0) file_size = 0; + /* get file size from current postion to end */ + if (file_size > 0) file_size -= file_pos; + } + + /* read file */ + if (file_size > 0) { + /* read the entire file in one call */ + buf_size = (usize)file_size + YYJSON_PADDING_SIZE; + buf = alc.malloc(alc.ctx, buf_size); + if (buf == NULL) { + return_err(MEMORY_ALLOCATION, MSG_MALLOC); + } + if (fread_safe(buf, (usize)file_size, file) != (usize)file_size) { + return_err(FILE_READ, MSG_FREAD); + } + } else { + /* failed to get file size, read it as a stream */ + usize chunk_min = (usize)64; + usize chunk_max = (usize)512 * 1024 * 1024; + usize chunk_now = chunk_min; + usize read_size; + void *tmp; + + buf_size = YYJSON_PADDING_SIZE; + while (true) { + if (buf_size + chunk_now < buf_size) { /* overflow */ + return_err(MEMORY_ALLOCATION, MSG_MALLOC); + } + buf_size += chunk_now; + if (!buf) { + buf = alc.malloc(alc.ctx, buf_size); + if (!buf) return_err(MEMORY_ALLOCATION, MSG_MALLOC); + } else { + tmp = alc.realloc(alc.ctx, buf, buf_size - chunk_now, buf_size); + if (!tmp) return_err(MEMORY_ALLOCATION, MSG_MALLOC); + buf = tmp; + } + tmp = ((u8 *)buf) + buf_size - YYJSON_PADDING_SIZE - chunk_now; + read_size = fread_safe(tmp, chunk_now, file); + file_size += (long)read_size; + if (read_size != chunk_now) break; + + chunk_now *= 2; + if (chunk_now > chunk_max) chunk_now = chunk_max; + } + } + + /* read JSON */ + memset((u8 *)buf + file_size, 0, YYJSON_PADDING_SIZE); + flg |= YYJSON_READ_INSITU; + doc = yyjson_read_opts((char *)buf, (usize)file_size, flg, &alc, err); + if (doc) { + doc->str_pool = (char *)buf; + return doc; + } else { + alc.free(alc.ctx, buf); + return NULL; + } + +#undef return_err +} + +const char *yyjson_read_number(const char *dat, + yyjson_val *val, + yyjson_read_flag flg, + const yyjson_alc *alc, + yyjson_read_err *err) { +#define return_err(_pos, _code, _msg) do { \ + err->pos = _pos > hdr ? (usize)(_pos - hdr) : 0; \ + err->msg = _msg; \ + err->code = YYJSON_READ_ERROR_##_code; \ + return NULL; \ +} while (false) + + u8 *hdr = constcast(u8 *)dat, *cur = hdr; + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ + const char *msg; + yyjson_read_err tmp_err; + +#if YYJSON_DISABLE_FAST_FP_CONV + u8 buf[128]; + usize dat_len; +#endif + + if (!err) err = &tmp_err; + if (unlikely(!dat)) { + return_err(cur, INVALID_PARAMETER, "input data is NULL"); + } + if (unlikely(!val)) { + return_err(cur, INVALID_PARAMETER, "output value is NULL"); + } + +#if YYJSON_DISABLE_FAST_FP_CONV + if (!alc) alc = &YYJSON_DEFAULT_ALC; + dat_len = strlen(dat); + if (dat_len < sizeof(buf)) { + memcpy(buf, dat, dat_len + 1); + hdr = buf; + cur = hdr; + } else { + hdr = (u8 *)alc->malloc(alc->ctx, dat_len + 1); + cur = hdr; + if (unlikely(!hdr)) { + return_err(cur, MEMORY_ALLOCATION, MSG_MALLOC); + } + memcpy(hdr, dat, dat_len + 1); + } + hdr[dat_len] = 0; +#endif + +#if YYJSON_DISABLE_FAST_FP_CONV + if (!read_num(&cur, pre, flg, val, &msg)) { + if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); + return_err(cur, INVALID_NUMBER, msg); + } + if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); + if (yyjson_is_raw(val)) val->uni.str = dat; + return dat + (cur - hdr); +#else + if (!read_num(&cur, pre, flg, val, &msg)) { + return_err(cur, INVALID_NUMBER, msg); + } + return (const char *)cur; +#endif + +#undef return_err +} + +/*============================================================================== + * MARK: - Incremental JSON Reader (Public) + *============================================================================*/ + #if !YYJSON_DISABLE_INCR_READER /* labels within yyjson_incr_read() to resume incremental parsing */ -#define YYJSON_READ_LABEL_doc_begin 0 -#define YYJSON_READ_LABEL_arr_val_begin 1 -#define YYJSON_READ_LABEL_arr_val_end 2 -#define YYJSON_READ_LABEL_obj_key_begin 3 -#define YYJSON_READ_LABEL_obj_key_end 4 -#define YYJSON_READ_LABEL_obj_val_begin 5 -#define YYJSON_READ_LABEL_obj_val_end 6 -#define YYJSON_READ_LABEL_doc_end 7 +#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 */ - const yyjson_alc *alc; /* allocator */ + yyjson_alc alc; /* allocator */ yyjson_read_flag flg; /* read flags */ - u8 *hdr; /* JSON data */ + u8 *hdr; /* JSON data header */ u8 *cur; /* current position in JSON data */ - usize len; + 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 */ @@ -6860,51 +6480,57 @@ struct yyjson_incr_state { yyjson_incr_state *yyjson_incr_new(char *buf, size_t buf_len, yyjson_read_flag flg, - const yyjson_alc *alc) { + const yyjson_alc *alc_ptr) { yyjson_incr_state *state = NULL; - if (unlikely(!buf)) goto error; - if (likely(!alc)) alc = &YYJSON_DEFAULT_ALC; - state = (yyjson_incr_state *)alc->malloc(alc->ctx, - sizeof(yyjson_incr_state)); - if (!state) goto error; + 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->len = buf_len; + state->buf_len = buf_len; /* add 4-byte zero padding for input data if necessary */ - if (has_read_flag(INSITU)) { + if (has_flg(INSITU)) { state->hdr = (u8 *)buf; - state->cur = (u8 *)buf; } else { - if (unlikely(buf_len >= USIZE_MAX - YYJSON_PADDING_SIZE)) goto error; - state->hdr = (u8 *)alc->malloc(alc->ctx, buf_len + YYJSON_PADDING_SIZE); - if (unlikely(!state->hdr)) goto error; - state->cur = state->hdr; + state->hdr = (u8 *)alc.malloc(alc.ctx, buf_len + YYJSON_PADDING_SIZE); + if (unlikely(!state->hdr)) { + alc.free(alc.ctx, state); + return NULL; + } memcpy(state->hdr, buf, buf_len); - memset(state->hdr + buf_len, 0, YYJSON_PADDING_SIZE); } + memset(state->hdr + buf_len, 0, YYJSON_PADDING_SIZE); + state->cur = state->hdr; + state->label = LABEL_doc_begin; return state; - -error: - if (state) yyjson_incr_free(state); - return NULL; } void yyjson_incr_free(yyjson_incr_state *state) { - const yyjson_alc *alc = state->alc; - if (state->val_hdr != NULL) { - alc->free(alc->ctx, (void *)state->val_hdr); - } - if (state->hdr != NULL && !(state->flg & YYJSON_READ_INSITU)) { - alc->free(alc->ctx, (void *)state->hdr); + if (state) { + yyjson_alc alc = state->alc; + memset(&state->alc, 0, sizeof(alc)); + if (state->val_hdr) { + alc.free(alc.ctx, (void *)state->val_hdr); + } + if (state->hdr && !(state->flg & YYJSON_READ_INSITU)) { + alc.free(alc.ctx, state->hdr); + } + alc.free(alc.ctx, state); } - alc->free(alc->ctx, (void *)state); } yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, yyjson_read_err *err) { - #define return_err_inv_param(_msg) do { \ err->pos = 0; \ err->msg = _msg; \ @@ -6942,9 +6568,9 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, } \ } while (false) + /* save position where it's possible to resume incremental parsing */ #define save_incr_state(_label) do { \ - /* save position where it's possible to resume incremental parsing */ \ - state->label = YYJSON_READ_LABEL_##_label; \ + state->label = LABEL_##_label; \ state->cur = cur; \ state->val = val; \ state->ctn_len = ctn_len; \ @@ -6979,32 +6605,31 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, 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 */ + yyjson_read_err tmp_err; + u8 raw_end[1]; /* raw end for null-terminator */ + u8 *raw_ptr = raw_end; + u8 **pre = &raw_ptr; /* previous raw end pointer */ u8 **con = NULL; /* for incremental string parsing */ u8 saved_end = '\0'; /* saved end char */ /* validate input parameters */ - if (unlikely(!err)) { - return NULL; - } + 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->len)) { + 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; + alc = state->alc; ctn_len = state->ctn_len; hdr_len = state->hdr_len; alc_len = state->alc_len; @@ -7013,12 +6638,7 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, val_end = state->val_end; ctn = state->ctn; con = state->str_con; - alc_max = USIZE_MAX / sizeof(yyjson_val); - 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; /* insert null terminator to make us stop at the specified end, even if the data contains more valid JSON */ @@ -7027,46 +6647,30 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, /* resume parsing from the last save point */ switch (state->label) { - case YYJSON_READ_LABEL_doc_begin: goto doc_begin; - case YYJSON_READ_LABEL_arr_val_begin: goto arr_val_begin; - case YYJSON_READ_LABEL_arr_val_end: goto arr_val_end; - case YYJSON_READ_LABEL_obj_key_begin: goto obj_key_begin; - case YYJSON_READ_LABEL_obj_key_end: goto obj_key_end; - case YYJSON_READ_LABEL_obj_val_begin: goto obj_val_begin; - case YYJSON_READ_LABEL_obj_val_end: goto obj_val_end; - case YYJSON_READ_LABEL_doc_end: goto doc_end; + 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: - if (cur == hdr && has_read_flag(ALLOW_BOM)) { - if (len >= 3 && is_utf8_bom(cur)) cur += 3; - } - /* 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)) { - /* unclosed multiline comment */ - goto unexpected_end; - } - } else { - if (likely(char_is_space(*cur))) { - while (char_is_space(*++cur)); - } - } - if (unlikely(cur >= end)) { - /* input data is empty */ - goto unexpected_end; - } + 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_container(*cur))) { - dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : state->len; + 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); @@ -7103,7 +6707,7 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, goto fail_number; } if (*cur == '"') { - if (likely(read_str(&cur, end, inv, val, &msg, con))) goto doc_end; + if (likely(read_str_con(&cur, end, flg, val, &msg, con))) goto doc_end; goto fail_string; } if (*cur == 't') { @@ -7116,14 +6720,8 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, } 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 (has_read_flag(ALLOW_INF_AND_NAN)) { - if (read_inf_or_nan(false, &cur, pre, flg, val)) goto doc_end; - } msg = "unexpected character, expected a valid root value"; if (cur == hdr) { @@ -7168,7 +6766,8 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, if (*cur == '"') { val_incr(); ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, con))) 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') { @@ -7187,32 +6786,17 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, 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 (char_is_space(*cur)) { - while (char_is_space(*++cur)); - goto arr_val_continue; - } - 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_maybe_end; - goto fail_character_val; + while (*cur != ',') cur--; + goto fail_trailing_comma; } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto arr_val_continue; - if (byte_match_2(cur, "/*")) goto fail_comment; + if (char_is_space(*cur)) { + while (char_is_space(*++cur)); + goto arr_val_continue; } goto fail_character_val; @@ -7235,10 +6819,6 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, 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: @@ -7276,13 +6856,13 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, if (likely(*cur == '"')) { val_incr(); ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, con))) goto obj_key_end; + 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; - if (has_read_flag(ALLOW_TRAILING_COMMAS)) goto obj_end; while (*cur != ',') cur--; goto fail_trailing_comma; } @@ -7290,10 +6870,6 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, while (char_is_space(*++cur)); goto obj_key_continue; } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_key_continue; - if (byte_match_2(cur, "/*")) goto fail_comment; - } goto fail_character_obj_key; obj_key_end: @@ -7306,10 +6882,6 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, 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: @@ -7318,7 +6890,8 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, if (*cur == '"') { val++; ctn_len++; - if (likely(read_str(&cur, end, inv, val, &msg, con))) goto obj_val_end; + if (likely(read_str_con(&cur, end, flg, val, &msg, con))) + goto obj_val_end; goto fail_string; } if (char_is_num(*cur)) { @@ -7351,26 +6924,12 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, 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; - } goto fail_literal_null; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_val_continue; } - 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_maybe_end; - goto fail_character_val; - } - if (has_read_flag(ALLOW_COMMENTS)) { - if (skip_spaces_and_comments(&cur)) goto obj_val_continue; - if (byte_match_2(cur, "/*")) goto fail_comment; - } goto fail_character_val; obj_val_maybe_end: @@ -7392,3421 +6951,4115 @@ yyjson_doc *yyjson_incr_read(yyjson_incr_state *state, size_t len, 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 ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { + goto obj_val_end; + } else { + goto arr_val_end; + } + +doc_end: + /* check invalid contents after json document */ + if (unlikely(cur < end) && !has_flg(STOP_WHEN_DONE)) { + save_incr_state(doc_end); + while (char_is_space(*cur)) cur++; + if (unlikely(cur < end)) goto fail_garbage; + } + + **pre = '\0'; + doc = (yyjson_doc *)val_hdr; + doc->root = val_hdr + hdr_len; + doc->alc = alc; + doc->dat_read = (usize)(cur - hdr); + doc->val_read = (usize)((val - doc->root) + 1); + doc->str_pool = has_flg(INSITU) ? NULL : (char *)hdr; + state->hdr = NULL; + state->val_hdr = NULL; + memset(err, 0, sizeof(yyjson_read_err)); + return doc; + +unexpected_end: + err->pos = len; + /* if no nore data, stop the incr read */ + if (unlikely(len >= state->buf_len)) { + err->code = YYJSON_READ_ERROR_UNEXPECTED_END; + err->msg = MSG_NOT_END; + return NULL; + } + /* save parser state in extended error struct, in addition to what was + * stored in the last save_incr_state */ + err->code = YYJSON_READ_ERROR_MORE; + err->msg = "need more data"; + state->val_end = val_end; + state->ctn = ctn; + state->alc_len = alc_len; + /* restore the end where we've inserted a null terminator */ + *end = saved_end; + return NULL; + +fail_string: return_err(cur, INVALID_STRING, msg); +fail_number: return_err(cur, INVALID_NUMBER, msg); +fail_alloc: return_err(cur, MEMORY_ALLOCATION, MSG_MALLOC); +fail_trailing_comma: return_err(cur, JSON_STRUCTURE, MSG_COMMA); +fail_literal_true: return_err(cur, LITERAL, MSG_CHAR_T); +fail_literal_false: return_err(cur, LITERAL, MSG_CHAR_F); +fail_literal_null: return_err(cur, LITERAL, MSG_CHAR_N); +fail_character_val: return_err(cur, UNEXPECTED_CHARACTER, MSG_CHAR); +fail_character_arr_end: return_err(cur, UNEXPECTED_CHARACTER, MSG_ARR_END); +fail_character_obj_key: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_KEY); +fail_character_obj_sep: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_SEP); +fail_character_obj_end: return_err(cur, UNEXPECTED_CHARACTER, MSG_OBJ_END); +fail_garbage: return_err(cur, UNEXPECTED_CONTENT, MSG_GARBAGE); + +#undef val_incr +#undef return_err +#undef return_err_inv_param +#undef save_incr_state +#undef check_maybe_truncated_number +} + +#endif /* YYJSON_DISABLE_INCR_READER */ + +#undef has_flg +#undef has_allow +#endif /* YYJSON_DISABLE_READER */ + + + +#if !YYJSON_DISABLE_WRITER /* writer begin */ + +/* Check write flag, avoids `always false` warning when disabled. */ +#define has_flg(_flg) unlikely(has_wflag(flg, YYJSON_WRITE_##_flg, 0)) +#define has_allow(_flg) unlikely(has_wflag(flg, YYJSON_WRITE_ALLOW_##_flg, 1)) +static_inline bool has_wflag(yyjson_write_flag flg, yyjson_write_flag chk, + bool non_standard) { +#if YYJSON_DISABLE_NON_STANDARD + if (non_standard) return false; +#endif + return (flg & chk) != 0; +} + +/*============================================================================== + * MARK: - Integer Writer (Private) + * + * The maximum value of uint32_t is 4294967295 (10 digits), + * these digits are named as 'aabbccddee' here. + * + * Although most compilers may convert the "division by constant value" into + * "multiply and shift", manual conversion can still help some compilers + * generate fewer and better instructions. + * + * Reference: + * Division by Invariant Integers using Multiplication, 1994. + * https://gmplib.org/~tege/divcnst-pldi94.pdf + * Improved division by invariant integers, 2011. + * https://gmplib.org/~tege/division-paper.pdf + *============================================================================*/ + +/** Digit table from 00 to 99. */ +yyjson_align(2) +static const char digit_table[200] = { + '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', + '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', + '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', + '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', + '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', + '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', + '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', + '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', + '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', + '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', + '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', + '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', + '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', + '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', + '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', + '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', + '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', + '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', + '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', + '9', '5', '9', '6', '9', '7', '9', '8', '9', '9' +}; + +static_inline u8 *write_u32_len_8(u32 val, u8 *buf) { + u32 aa, bb, cc, dd, aabb, ccdd; /* 8 digits: aabbccdd */ + aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ + ccdd = val - aabb * 10000; /* (val % 10000) */ + aa = (aabb * 5243) >> 19; /* (aabb / 100) */ + cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ + bb = aabb - aa * 100; /* (aabb % 100) */ + dd = ccdd - cc * 100; /* (ccdd % 100) */ + byte_copy_2(buf + 0, digit_table + aa * 2); + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + byte_copy_2(buf + 6, digit_table + dd * 2); + return buf + 8; +} + +static_inline u8 *write_u32_len_4(u32 val, u8 *buf) { + u32 aa, bb; /* 4 digits: aabb */ + aa = (val * 5243) >> 19; /* (val / 100) */ + bb = val - aa * 100; /* (val % 100) */ + byte_copy_2(buf + 0, digit_table + aa * 2); + byte_copy_2(buf + 2, digit_table + bb * 2); + return buf + 4; +} + +static_inline u8 *write_u32_len_1_to_8(u32 val, u8 *buf) { + u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; + + if (val < 100) { /* 1-2 digits: aa */ + lz = val < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + val * 2 + lz); + buf -= lz; + return buf + 2; + + } else if (val < 10000) { /* 3-4 digits: aabb */ + aa = (val * 5243) >> 19; /* (val / 100) */ + bb = val - aa * 100; /* (val % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + return buf + 4; + + } else if (val < 1000000) { /* 5-6 digits: aabbcc */ + aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */ + bbcc = val - aa * 10000; /* (val % 10000) */ + bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */ + cc = bbcc - bb * 100; /* (bbcc % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + return buf + 6; + + } else { /* 7-8 digits: aabbccdd */ + aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ + ccdd = val - aabb * 10000; /* (val % 10000) */ + aa = (aabb * 5243) >> 19; /* (aabb / 100) */ + cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ + bb = aabb - aa * 100; /* (aabb % 100) */ + dd = ccdd - cc * 100; /* (ccdd % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + byte_copy_2(buf + 6, digit_table + dd * 2); + return buf + 8; + } +} + +static_inline u8 *write_u32_len_5_to_8(u32 val, u8 *buf) { + u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; + + if (val < 1000000) { /* 5-6 digits: aabbcc */ + aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */ + bbcc = val - aa * 10000; /* (val % 10000) */ + bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */ + cc = bbcc - bb * 100; /* (bbcc % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + return buf + 6; + + } else { /* 7-8 digits: aabbccdd */ + aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ + ccdd = val - aabb * 10000; /* (val % 10000) */ + aa = (aabb * 5243) >> 19; /* (aabb / 100) */ + cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ + bb = aabb - aa * 100; /* (aabb % 100) */ + dd = ccdd - cc * 100; /* (ccdd % 100) */ + lz = aa < 10; /* leading zero: 0 or 1 */ + byte_copy_2(buf + 0, digit_table + aa * 2 + lz); + buf -= lz; + byte_copy_2(buf + 2, digit_table + bb * 2); + byte_copy_2(buf + 4, digit_table + cc * 2); + byte_copy_2(buf + 6, digit_table + dd * 2); + return buf + 8; + } +} + +static_inline u8 *write_u64(u64 val, u8 *buf) { + u64 tmp, hgh; + u32 mid, low; + + if (val < 100000000) { /* 1-8 digits */ + buf = write_u32_len_1_to_8((u32)val, buf); + return buf; + + } else if (val < (u64)100000000 * 100000000) { /* 9-16 digits */ + hgh = val / 100000000; /* (val / 100000000) */ + low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ + buf = write_u32_len_1_to_8((u32)hgh, buf); + buf = write_u32_len_8(low, buf); + return buf; + + } else { /* 17-20 digits */ + tmp = val / 100000000; /* (val / 100000000) */ + low = (u32)(val - tmp * 100000000); /* (val % 100000000) */ + hgh = (u32)(tmp / 10000); /* (tmp / 10000) */ + mid = (u32)(tmp - hgh * 10000); /* (tmp % 10000) */ + buf = write_u32_len_5_to_8((u32)hgh, buf); + buf = write_u32_len_4(mid, buf); + buf = write_u32_len_8(low, buf); + return buf; } - 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; + + +/*============================================================================== + * MARK: - Number Writer (Private) + *============================================================================*/ + +#if !YYJSON_DISABLE_FAST_FP_CONV /* FP_WRITER */ + +/** Trailing zero count table for number 0 to 99. + (generate with misc/make_tables.c) */ +static const u8 dec_trailing_zero_table[] = { + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +static_inline u8 *write_u64_len_1_to_16(u64 val, u8 *buf) { + u64 hgh; + u32 low; + if (val < 100000000) { /* 1-8 digits */ + buf = write_u32_len_1_to_8((u32)val, buf); + return buf; + } else { /* 9-16 digits */ + hgh = val / 100000000; /* (val / 100000000) */ + low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ + buf = write_u32_len_1_to_8((u32)hgh, buf); + buf = write_u32_len_8(low, buf); + return buf; } +} -doc_end: - /* check invalid contents after json document */ - if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { - save_incr_state(doc_end); - if (has_read_flag(ALLOW_COMMENTS)) { - skip_spaces_and_comments(&cur); - if (byte_match_2(cur, "/*")) goto fail_comment; - if (*cur == '/' && cur + 1 == end) { - /* truncated beginning of comment */ - goto unexpected_end; - } - } else { - while (char_is_space(*cur)) cur++; - } - if (unlikely(cur < end)) goto fail_garbage; +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; } +} - 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; - state->hdr = NULL; - state->val_hdr = NULL; - memset(err, 0, sizeof(yyjson_read_err)); - return doc; +/** + 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; -unexpected_end: - err->pos = len; - if (unlikely(len >= state->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; + 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) */ -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_CHAT_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); + /* 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); -#undef val_incr -#undef return_err -#undef return_err_inv_param -#undef save_incr_state -#undef check_maybe_truncated_number + 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; + } } -#endif /* YYJSON_DISABLE_INCR_READER */ +/** + 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; + } + } +} -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) +/** 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; +} - yyjson_read_err dummy_err; - yyjson_doc *doc; - FILE *file; +/** 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 (!err) err = &dummy_err; - if (unlikely(!path)) return_err(INVALID_PARAMETER, "input path is NULL"); +/** Magic number for fast `divide by power of 10`. */ +typedef struct { + u64 p10, mul; + u32 shr1, shr2; +} div_pow10_magic; - file = fopen_readonly(path); - if (unlikely(!file)) return_err(FILE_OPEN, MSG_FREAD); +/** 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 }, +}; - doc = yyjson_read_fp(file, flg, alc_ptr, err); - fclose(file); - return doc; +/** 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 +/** 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); } -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) +/** 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); +} - yyjson_read_err dummy_err; - yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; - yyjson_doc *doc; +/** 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) { - long file_size = 0, file_pos; - void *buf = NULL; - usize buf_size = 0; + 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; - /* validate input parameters */ - if (!err) err = &dummy_err; - if (unlikely(!file)) return_err(INVALID_PARAMETER, "input file is NULL"); + /* 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; - /* 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; - } + /* 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); - /* read file */ - if (file_size > 0) { - /* read the entire file in one call */ - buf_size = (usize)file_size + YYJSON_PADDING_SIZE; - buf = alc.malloc(alc.ctx, buf_size); - if (buf == NULL) { - return_err(MEMORY_ALLOCATION, MSG_MALLOC); - } - if (fread_safe(buf, (usize)file_size, file) != (usize)file_size) { - return_err(FILE_READ, MSG_FREAD); - } - } else { - /* failed to get file size, read it as a stream */ - usize chunk_min = (usize)64; - usize chunk_max = (usize)512 * 1024 * 1024; - usize chunk_now = chunk_min; - usize read_size; - void *tmp; + /* 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; - buf_size = YYJSON_PADDING_SIZE; - while (true) { - if (buf_size + chunk_now < buf_size) { /* overflow */ - return_err(MEMORY_ALLOCATION, MSG_MALLOC); - } - buf_size += chunk_now; - if (!buf) { - buf = alc.malloc(alc.ctx, buf_size); - if (!buf) return_err(MEMORY_ALLOCATION, MSG_MALLOC); - } else { - tmp = alc.realloc(alc.ctx, buf, buf_size - chunk_now, buf_size); - if (!tmp) return_err(MEMORY_ALLOCATION, MSG_MALLOC); - buf = tmp; - } - tmp = ((u8 *)buf) + buf_size - YYJSON_PADDING_SIZE - chunk_now; - read_size = fread_safe(tmp, chunk_now, file); - file_size += (long)read_size; - if (read_size != chunk_now) break; + /* right shift 4 to fit in u32 */ + c = (mod << (32 - 4)) | (s_lo >> 4); + half_ulp = (u32)(p10_hi >> (32 + 4 - h)); - chunk_now *= 2; - if (chunk_now > chunk_max) chunk_now = chunk_max; - } - } + /* 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; - /* 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; + 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; } -#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; + /* 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; -#if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV - u8 buf[128]; - usize dat_len; -#endif + /* 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; - 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"); - } + 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; -#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, MSG_MALLOC); + 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; } - memcpy(hdr, dat, dat_len + 1); } - hdr[dat_len] = 0; -#endif + 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; +} - raw = (flg & (YYJSON_READ_NUMBER_AS_RAW | YYJSON_READ_BIGNUM_AS_RAW)) != 0; - raw_end = NULL; - pre = raw ? &raw_end : NULL; +/** 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) { -#if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV - if (!read_num(&cur, pre, flg, val, &msg)) { - if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); - return_err(cur, INVALID_NUMBER, msg); - } - if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); - if (yyjson_is_raw(val)) val->uni.str = dat; - return dat + (cur - hdr); -#else - if (!read_num(&cur, pre, flg, val, &msg)) { - return_err(cur, INVALID_NUMBER, msg); - } - return (const char *)cur; -#endif + 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; -#undef return_err -} + /* + Fast path: + For regular spacing significand 'c', there are 4 candidates: -#endif /* YYJSON_DISABLE_READER */ + 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); -#if !YYJSON_DISABLE_WRITER + /* 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; -/*============================================================================== - * 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 - *============================================================================*/ + /* right shift 4 to fit in u64 */ + c = (mod << (64 - 4)) | (s_lo >> 4); + half_ulp = p10_hi >> (4 - h); -/** 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' -}; + /* 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; -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; -} + 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; + } -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; + /* + Schubfach algorithm: + Raffaello Giulietti, The Schubfach way to render doubles, 2022. + https://drive.google.com/file/d/1gp5xv4CAa78SVgCeWfGqqI4FfYYYuNFb (Paper) + https://github.com/openjdk/jdk/pull/3402 (Java implementation) + https://github.com/abolz/Drachennest (C++ implementation) + */ + irregular = (sig_raw == 0 && exp_raw > 1); + is_even = !(sig_bin & 1); + cbl = 4 * sig_bin - 2 + irregular; + cb = 4 * sig_bin; + cbr = 4 * sig_bin + 2; + + /* k = floor(exp_bin * log10(2) + (irregular ? log10(3.0 / 4.0) : 0)); */ + /* h = exp_bin + floor(log2(10) * -k) + 1; (h = 1/2/3/4) */ + k = (i32)(exp_bin * 315653 - (irregular ? 131237 : 0)) >> 20; + h = exp_bin + ((-k * 217707) >> 16) + 1; + pow10_table_get_sig(-k, &p10_hi, &p10_lo); + p10_lo += 1; + + vbl = u128_round_to_odd(p10_hi, p10_lo, cbl << h); + vb = u128_round_to_odd(p10_hi, p10_lo, cb << h); + vbr = u128_round_to_odd(p10_hi, p10_lo, cbr << h); + lower = vbl + !is_even; + upper = vbr - !is_even; + + s = vb / 4; + if (s >= 10) { + sp = s / 10; + u0_inside = (lower <= 40 * sp); + w0_inside = (upper >= 40 * sp + 40); + if (u0_inside != w0_inside) { + *sig_dec = sp * 10 + (w0_inside ? 10 : 0); + *exp_dec = k; + return; + } + } + u1_inside = (lower <= 4 * s); + w1_inside = (upper >= 4 * s + 4); + mid = 4 * s + 2; + round_up = (vb > mid) || (vb == mid && (s & 1) != 0); + *sig_dec = s + ((u1_inside != w1_inside) ? w1_inside : round_up); + *exp_dec = k; } -static_inline u8 *write_u32_len_1_to_8(u32 val, u8 *buf) { - u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; +/** 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; - 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; + irregular = (sig_raw == 0 && exp_raw > 1); - } 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; + /* 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); - } 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; + /* 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); - } 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; + /* round up */ + u = s_lo >= (irregular ? U64(0x55555555, 0x55555555) : ((u64)1 << 63)); + + *sig_dec = s_hi + u; + *exp_dec = k; + *round_up = u; + return; +} + +/** Write inf/nan if allowed. */ +static_inline u8 *write_inf_or_nan(u8 *buf, yyjson_write_flag flg, + u64 sig_raw, bool sign) { + if (has_flg(INF_AND_NAN_AS_NULL)) { + byte_copy_4(buf, "null"); + return buf + 4; + } + if (has_allow(INF_AND_NAN)) { + if (sig_raw == 0) { + buf[0] = '-'; + buf += sign; + byte_copy_8(buf, "Infinity"); + return buf + 8; + } else { + byte_copy_4(buf, "NaN"); + return buf + 3; + } } + return NULL; } -static_inline u8 *write_u32_len_5_to_8(u32 val, u8 *buf) { - u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; +/** + 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; - 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; + /* cast double to float */ + raw = f32_to_bits(f64_to_f32(f64_from_bits(raw_f64))); - } 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; + /* 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); } -} -static_inline u8 *write_u64(u64 val, u8 *buf) { - u64 tmp, hgh; - u32 mid, low; + /* add sign for all finite number */ + buf[0] = '-'; + buf += sign; - if (val < 100000000) { /* 1-8 digits */ - buf = write_u32_len_1_to_8((u32)val, buf); - return buf; + /* return zero */ + if ((raw << 1) == 0) { + byte_copy_4(buf, "0.0"); + return buf + 3; + } - } 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; + 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; - } 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; - } -} + /* 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); -/*============================================================================== - * Number Writer - *============================================================================*/ + /* the decimal point offset relative to the first digit */ + dot_ofs = sig_len + exp_dec; -#if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_WRITER */ + 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; -/** 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 -}; + /* fill zeros */ + memset(buf, '0', 32); -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); -} + /* not prefixed with zero, e.g. 1.234, 1234.0 */ + no_pre_zero = (dot_ofs > 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; - } -} + /* 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); -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; - } -} + /* 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 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; + /* write the dot */ + dot_set_pos = yyjson_max(dot_ofs, 1); + buf[dot_set_pos] = '.'; - 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) */ + /* return the ending */ + dot_end = buf + dot_ofs + 2; + return yyjson_max(dot_end, num_end); - /* 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); + } 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); + } - 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; + /* subnormal number */ + sig_bin = sig_raw; + exp_bin = 1 - F32_EXP_BIAS - F32_SIG_BITS; + + /* binary to decimal */ + f32_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); + + /* write significand part */ + end = write_u32_len_1_to_8(sig_dec, buf + 1); + buf[0] = buf[1]; + buf[1] = '.'; + exp_dec += (i32)(end - buf) - 2; + + /* trim trailing zeros */ + end -= *(end - 1) == '0'; /* branchless for last zero */ + end -= *(end - 1) == '0'; /* branchless for second last zero */ + while (*(end - 1) == '0') end--; /* for unlikely more zeros */ + end -= *(end - 1) == '.'; /* remove dot, e.g. 2.e-321 -> 2e-321 */ + + /* write exponent part */ + return write_f32_exp(exp_dec, end); } } /** - Write 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". + 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_inline u8 *write_u64_len_16_to_17_trim(u64 val, u8 *buf) { - u32 tz, tz1, tz2; +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; - 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); + /* 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); - 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; - } + /* return inf or nan */ + if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) { + return write_inf_or_nan(buf, flg, sig_raw, sign); } -} -/** 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; -} + /* add sign for all finite number */ + buf[0] = '-'; + buf += sign; -/** 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 zero */ + if ((raw << 1) == 0) { + byte_copy_4(buf, "0.0"); return buf + 3; } -} -/** Magic number for fast `divide by power of 10`. */ -typedef struct { - u64 p10, mul; - u32 shr1, shr2; -} div_pow10_magic; + 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; -/** 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 }, -}; + /* 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; + } -/** 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; -} + /* binary to decimal */ + f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); -/** 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); -} + /* the sig length is 16 or 17 */ + sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); -/** 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); -} + /* the decimal point offset relative to the first digit */ + dot_ofs = sig_len + exp_dec; -/** 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) { + 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; - 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; + /* fill zeros */ + memset(buf, '0', 32); - /* 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; + /* not prefixed with zero, e.g. 1.234, 1234.0 */ + no_pre_zero = (dot_ofs > 0); + + /* write the number as digits */ + pre_ofs = no_pre_zero ? 0 : (2 - dot_ofs); + num_hdr = buf + pre_ofs; + num_end = write_u64_len_16_to_17_trim(sig_dec, num_hdr); + + /* seperate these digits to leave a space for dot */ + num_sep_pos = no_pre_zero ? dot_ofs : 0; + num_sep = num_hdr + num_sep_pos; + byte_move_16(num_sep + no_pre_zero, num_sep); + num_end += no_pre_zero; + + /* write the dot */ + dot_set_pos = yyjson_max(dot_ofs, 1); + buf[dot_set_pos] = '.'; + + /* return the ending */ + dot_end = buf + dot_ofs + 2; + return yyjson_max(dot_end, num_end); + + } else { + /* write with scientific notation, e.g. 1.234e56 */ + end = write_u64_len_16_to_17_trim(sig_dec, buf + 1); + end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */ + exp_dec += sig_len - 1; + buf[0] = buf[1]; + buf[1] = '.'; + return write_f64_exp(exp_dec, end); + } + + } else { + /* subnormal number */ + sig_bin = sig_raw; + exp_bin = 1 - F64_EXP_BIAS - F64_SIG_BITS; + + /* binary to decimal */ + f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); + + /* write significand part */ + end = write_u64_len_1_to_17(sig_dec, buf + 1); + buf[0] = buf[1]; + buf[1] = '.'; + exp_dec += (i32)(end - buf) - 2; + + /* trim trailing zeros */ + end -= *(end - 1) == '0'; /* branchless for last zero */ + end -= *(end - 1) == '0'; /* branchless for second last zero */ + while (*(end - 1) == '0') end--; /* for unlikely more zeros */ + end -= *(end - 1) == '.'; /* remove dot, e.g. 2.e-321 -> 2e-321 */ - /* 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); + /* write exponent part */ + return write_f64_exp(exp_dec, end); + } +} - /* 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; +/** + Write a double number using fixed-point notation (requires 40 bytes buffer). - /* right shift 4 to fit in u32 */ - c = (mod << (32 - 4)) | (s_lo >> 4); - half_ulp = (u32)(p10_hi >> (32 + 4 - h)); + 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; - /* 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; + /* 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); - 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; + /* return inf or nan */ + if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) { + return write_inf_or_nan(buf, flg, sig_raw, sign); } - /* 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; + /* add sign for all finite number */ + buf[0] = '-'; + buf += sign; - /* 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; + /* return zero */ + if ((raw << 1) == 0) { + byte_copy_4(buf, "0.0"); + return buf + 3; + } - 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; + 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; - 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; + /* 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; } - } - 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) { + /* 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; - 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; + /* binary to decimal */ + f64_bin_to_dec_fast(sig_raw, exp_raw, sig_bin, exp_bin, + &sig_dec, &exp_dec, &round_up); - /* - Fast path: - For regular spacing significand 'c', there are 4 candidates: + /* the sig length is 16 or 17 */ + sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); - u0 u1 c w1 w0 - ----|----|----|----|----|-*--|----|----|----|----|----|----|----|---- - 9 0 1 2 3 4 5 6 7 8 9 0 1 - |___________________|___________________| - 1ulp + /* 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; - 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; + /* remove round up */ + sig_dec -= round_up; + sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); - /* 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); + /* cut off some digits */ + div_pow10(sig_dec, (u32)sig_len_cut, &div, &mod, &p10); - /* 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; + /* add round up */ + sig_dec = div + (mod >= p10 / 2); - /* right shift 4 to fit in u64 */ - c = (mod << (64 - 4)) | (s_lo >> 4); - half_ulp = p10_hi >> (4 - h); + /* 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; + } + } - /* 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; + /* fill zeros */ + memset(buf, '0', 32); - 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; - } + /* the decimal point offset relative to the first digit */ + dot_ofs = sig_len + exp_dec; - /* - 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; + /* not prefixed with zero, e.g. 1.234, 1234.0 */ + no_pre_zero = (dot_ofs > 0); - /* 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; + /* 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); - 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; + /* the sig length is 16 or 17 */ + sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); - 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; + /* write with scientific notation, e.g. 1.234e56 */ + end = write_u64_len_16_to_17_trim(sig_dec, buf + 1); + end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */ + exp_dec += sig_len - 1; + buf[0] = buf[1]; + buf[1] = '.'; + return write_f64_exp(exp_dec, end); } + } else { + /* subnormal number */ + byte_copy_4(buf, "0.0"); + return buf + 3; } - 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); +#else /* FP_WRITER */ - /* round up */ - u = s_lo >= (irregular ? U64(0x55555555, 0x55555555) : ((u64)1 << 63)); +#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 - *sig_dec = s_hi + u; - *exp_dec = k; - *round_up = u; - return; -} +static_noinline u8 *write_fp_reformat(u8 *buf, int len, + yyjson_write_flag flg, bool fixed) { + u8 *cur = buf; + if (unlikely(len < 1)) return NULL; + cur += (*cur == '-'); + if (unlikely(!char_is_digit(*cur))) { + /* nan, inf, or bad output */ + if (has_flg(INF_AND_NAN_AS_NULL)) { + byte_copy_4(buf, "null"); + return buf + 4; + } else if (has_allow(INF_AND_NAN)) { + if (*cur == 'i') { + byte_copy_8(cur, "Infinity"); + return cur + 8; + } else if (*cur == 'n') { + byte_copy_4(buf, "NaN"); + return buf + 3; + } + } + return NULL; + } else { + /* finite number */ + u8 *end = buf + len, *dot = NULL, *exp = NULL; -/** 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; + /* + 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 { - byte_copy_4(buf, "NaN"); - return buf + 3; + 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; } - 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))); +/** Write a double number (requires 40 bytes buffer). */ +static_noinline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) { +#if defined(DBL_DECIMAL_DIG) && DBL_DECIMAL_DIG < F64_DEC_DIG + int dig = DBL_DECIMAL_DIG; +#else + int dig = F64_DEC_DIG; +#endif + f64 val = f64_from_bits(raw); + int len = snprintf_num(buf, FP_BUF_LEN, "%.*g", dig, val); + return write_fp_reformat(buf, len, flg, false); +} - /* 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; +/** 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); +} - /* return inf or nan */ - if (unlikely(exp_raw == ((u32)1 << F32_EXP_BITS) - 1)) { - return write_inf_or_nan(buf, flg, sig_raw, sign); +/** 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); } +} - /* add sign for all finite number */ - buf[0] = '-'; - buf += sign; +#endif /* FP_WRITER */ - /* return zero */ - if ((raw << 1) == 0) { - byte_copy_4(buf, "0.0"); - return buf + 3; +/** 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 (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; +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; + } +} - /* 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; +/*============================================================================== + * MARK: - String Writer (Private) + *============================================================================*/ - 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; +/** 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'. */ - /* fill zeros */ - memset(buf, '0', 32); +/** 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 +}; - /* not prefixed with zero, e.g. 1.234, 1234.0 */ - no_pre_zero = (dot_ofs > 0); +/** 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 +}; - /* 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); +/** 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 +}; - /* 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; +/** 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 +}; - /* write the dot */ - dot_set_pos = yyjson_max(dot_ofs, 1); - buf[dot_set_pos] = '.'; +/** 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' +}; - /* return the ending */ - dot_end = buf + dot_ofs + 2; - return yyjson_max(dot_end, num_end); +/** Escaped single character table. (generate with misc/make_tables.c) */ +yyjson_align(2) +static const u8 esc_single_char_table[512] = { + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + '\\', 'b', '\\', 't', '\\', 'n', ' ', ' ', + '\\', 'f', '\\', 'r', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', '\\', '"', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', '\\', '/', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + '\\', '\\', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' +}; +/** Returns the encode table with options. */ +static_inline const char_enc_type *get_enc_table_with_flag( + yyjson_write_flag flg) { + if (has_flg(ESCAPE_UNICODE)) { + if (has_flg(ESCAPE_SLASHES)) { + return enc_table_esc_slash; } else { - /* 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); + return enc_table_esc; } - } 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; + if (has_flg(ESCAPE_SLASHES)) { + return enc_table_cpy_slash; + } else { + return enc_table_cpy; + } + } +} - /* 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 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 exponent part */ - return write_f32_exp(exp_dec, end); +/** + 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 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. + 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_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; +static_inline u8 *write_str(u8 *cur, bool esc, bool inv, + const u8 *str, usize str_len, + const char_enc_type *enc_table) { + /* The replacement character U+FFFD, used to indicate invalid character. */ + const v32 rep = {{ 'F', 'F', 'F', 'D' }}; + const v32 pre = {{ '\\', 'u', '0', '0' }}; - /* 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); + const u8 *src = str; + const u8 *end = str + str_len; + *cur++ = '"'; - /* return inf or nan */ - if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) { - return write_inf_or_nan(buf, flg, sig_raw, sign); +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; } - /* add sign for all finite number */ - buf[0] = '-'; - buf += sign; + while (end - src >= 4) { + repeat4_incr(expr_jump) + byte_copy_4(cur, src); + cur += 4; src += 4; + } - /* return zero */ - if ((raw << 1) == 0) { - byte_copy_4(buf, "0.0"); - return buf + 3; + while (end > src) { + expr_jump(0) + *cur++ = *src++; } - 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; + *cur++ = '"'; + return cur; - /* 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; + repeat16_incr(expr_stop) + +#undef expr_jump +#undef expr_stop + +copy_utf8: + if (unlikely(src + 4 > end)) { + if (end == src) goto copy_end; + if (end - src < enc_table[*src] / 2) goto err_one; + } + switch (enc_table[*src]) { + case CHAR_ENC_CPY_1: { + *cur++ = *src++; + goto copy_ascii; + } + case CHAR_ENC_CPY_2: { +#if YYJSON_DISABLE_UTF8_VALIDATION + byte_copy_2(cur, src); +#else + u32 uni = 0; + byte_copy_2(&uni, src); + if (unlikely(!is_utf8_seq2(uni))) goto err_cpy; + byte_copy_2(cur, &uni); +#endif + cur += 2; + src += 2; + goto copy_utf8; + } + case CHAR_ENC_CPY_3: { +#if YYJSON_DISABLE_UTF8_VALIDATION + if (likely(src + 4 <= end)) { + byte_copy_4(cur, src); + } else { + byte_copy_2(cur, src); + cur[2] = src[2]; + } +#else + u32 uni, tmp; + if (likely(src + 4 <= end)) { + uni = byte_load_4(src); + if (unlikely(!is_utf8_seq3(uni))) goto err_cpy; + byte_copy_4(cur, src); + } else { + uni = byte_load_3(src); + if (unlikely(!is_utf8_seq3(uni))) goto err_cpy; + byte_copy_4(cur, &uni); + } +#endif + cur += 3; + src += 3; + goto copy_utf8; + } + case CHAR_ENC_CPY_4: { +#if YYJSON_DISABLE_UTF8_VALIDATION + byte_copy_4(cur, src); +#else + u32 uni, tmp; + uni = byte_load_4(src); + if (unlikely(!is_utf8_seq4(uni))) goto err_cpy; + byte_copy_4(cur, src); +#endif + cur += 4; + src += 4; + goto copy_utf8; + } + case CHAR_ENC_ESC_A: { + byte_copy_2(cur, &esc_single_char_table[*src * 2]); + cur += 2; + src += 1; + goto copy_utf8; + } + case CHAR_ENC_ESC_1: { + byte_copy_4(cur + 0, &pre); + byte_copy_2(cur + 4, &esc_hex_char_table[*src * 2]); + cur += 6; + src += 1; + goto copy_utf8; + } + case CHAR_ENC_ESC_2: { + u16 u; +#if !YYJSON_DISABLE_UTF8_VALIDATION + u32 v4 = 0; + u16 v2 = byte_load_2(src); + byte_copy_2(&v4, &v2); + if (unlikely(!is_utf8_seq2(v4))) goto err_esc; +#endif + u = (u16)(((u16)(src[0] & 0x1F) << 6) | + ((u16)(src[1] & 0x3F) << 0)); + byte_copy_2(cur + 0, &pre); + byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]); + byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]); + cur += 6; + src += 2; + goto copy_utf8; + } + case CHAR_ENC_ESC_3: { + u16 u; + u32 v, tmp; +#if !YYJSON_DISABLE_UTF8_VALIDATION + v = byte_load_3(src); + if (unlikely(!is_utf8_seq3(v))) goto err_esc; +#endif + u = (u16)(((u16)(src[0] & 0x0F) << 12) | + ((u16)(src[1] & 0x3F) << 6) | + ((u16)(src[2] & 0x3F) << 0)); + byte_copy_2(cur + 0, &pre); + byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]); + byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]); + cur += 6; + src += 3; + goto copy_utf8; } + case CHAR_ENC_ESC_4: { + u32 hi, lo, u, v, tmp; +#if !YYJSON_DISABLE_UTF8_VALIDATION + v = byte_load_4(src); + if (unlikely(!is_utf8_seq4(v))) goto err_esc; +#endif + u = ((u32)(src[0] & 0x07) << 18) | + ((u32)(src[1] & 0x3F) << 12) | + ((u32)(src[2] & 0x3F) << 6) | + ((u32)(src[3] & 0x3F) << 0); + u -= 0x10000; + hi = (u >> 10) + 0xD800; + lo = (u & 0x3FF) + 0xDC00; + byte_copy_2(cur + 0, &pre); + byte_copy_2(cur + 2, &esc_hex_char_table[(hi >> 8) * 2]); + byte_copy_2(cur + 4, &esc_hex_char_table[(hi & 0xFF) * 2]); + byte_copy_2(cur + 6, &pre); + byte_copy_2(cur + 8, &esc_hex_char_table[(lo >> 8) * 2]); + byte_copy_2(cur + 10, &esc_hex_char_table[(lo & 0xFF) * 2]); + cur += 12; + src += 4; + goto copy_utf8; + } + case CHAR_ENC_ERR_1: { + goto err_one; + } + default: break; /* unreachable */ + } - /* 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; +copy_end: + *cur++ = '"'; + return cur; - /* fill zeros */ - memset(buf, '0', 32); +err_one: + if (esc) goto err_esc; + else goto err_cpy; - /* not prefixed with zero, e.g. 1.234, 1234.0 */ - no_pre_zero = (dot_ofs > 0); +err_cpy: + if (!inv) return NULL; + *cur++ = *src++; + goto copy_utf8; - /* 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); +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; +} - /* 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); +/*============================================================================== + * MARK: - JSON Writer Utilities (Private) + *============================================================================*/ - } 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); - } +/** Write null (requires 8 bytes buffer). */ +static_inline u8 *write_null(u8 *cur) { + v64 v = {{ 'n', 'u', 'l', 'l', ',', '\n', 0, 0 }}; + byte_copy_8(cur, &v); + return cur + 4; +} +/** Write bool (requires 8 bytes buffer). */ +static_inline u8 *write_bool(u8 *cur, bool val) { + v64 v0 = {{ 'f', 'a', 'l', 's', 'e', ',', '\n', 0 }}; + v64 v1 = {{ 't', 'r', 'u', 'e', ',', '\n', 0, 0 }}; + if (val) { + byte_copy_8(cur, &v1); } else { - /* 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); + byte_copy_8(cur, &v0); } + return cur + 5 - val; } -/** - Write a double number using fixed-point notation (requires 40 bytes buffer). +/** 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; +} - 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; +/** Write data to file pointer. */ +static bool write_dat_to_fp(FILE *fp, u8 *dat, usize len, + yyjson_write_err *err) { + if (fwrite(dat, len, 1, fp) != 1) { + err->msg = "file writing failed"; + err->code = YYJSON_WRITE_ERROR_FILE_WRITE; + return false; + } + return true; +} - /* 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); +/** 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) - /* return inf or nan */ - if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) { - return write_inf_or_nan(buf, flg, sig_raw, sign); + FILE *file = fopen_writeonly(path); + if (file == NULL) { + return_err(FILE_OPEN, MSG_FOPEN); + } + if (fwrite(dat, len, 1, file) != 1) { + return_err(FILE_WRITE, MSG_FWRITE); } + if (fclose(file) != 0) { + file = NULL; + return_err(FILE_WRITE, MSG_FCLOSE); + } + return true; - /* add sign for all finite number */ - buf[0] = '-'; - buf += sign; +#undef return_err +} - /* return zero */ - if ((raw << 1) == 0) { - byte_copy_4(buf, "0.0"); - return buf + 3; - } - if (likely(exp_raw != 0)) { - /* normal number */ - sig_bin = sig_raw | ((u64)1 << F64_SIG_BITS); - exp_bin = (i32)exp_raw - F64_EXP_BIAS - F64_SIG_BITS; - /* fast path for small integer number without fraction */ - if ((-F64_SIG_BITS <= exp_bin && exp_bin <= 0) && - (u64_tz_bits(sig_bin) >= (u32)-exp_bin)) { - sig_dec = sig_bin >> -exp_bin; /* range: [1, 0x1FFFFFFFFFFFFF] */ - buf = write_u64_len_1_to_16(sig_dec, buf); - byte_copy_2(buf, ".0"); - return buf + 2; - } +/*============================================================================== + * MARK: - JSON Writer Implementation (Private) + *============================================================================*/ - /* 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; +typedef struct yyjson_write_ctx { + usize tag; +} yyjson_write_ctx; - /* binary to decimal */ - f64_bin_to_dec_fast(sig_raw, exp_raw, sig_bin, exp_bin, - &sig_dec, &exp_dec, &round_up); +static_inline void yyjson_write_ctx_set(yyjson_write_ctx *ctx, + usize size, bool is_obj) { + ctx->tag = (size << 1) | (usize)is_obj; +} - /* the sig length is 16 or 17 */ - sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); +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); +} - /* 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; +/** 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) - /* remove round up */ - sig_dec -= round_up; - sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); +#define incr_len(_len) do { \ + hdr = (u8 *)alc.malloc(alc.ctx, _len); \ + if (!hdr) goto fail_alloc; \ + cur = hdr; \ +} while (false) - /* cut off some digits */ - div_pow10(sig_dec, (u32)sig_len_cut, &div, &mod, &p10); +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) - /* add round up */ - sig_dec = div + (mod >= p10 / 2); + u8 *hdr = NULL, *cur; + usize str_len; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + bool newline = has_flg(NEWLINE_AT_END) != 0; + const usize end_len = 2; /* '\n' and '\0' */ - /* 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; - } - } + 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; - /* fill zeros */ - memset(buf, '0', 32); + case YYJSON_TYPE_STR: + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 2 + end_len); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; + } + break; - /* the decimal point offset relative to the first digit */ - dot_ofs = sig_len + exp_dec; + case YYJSON_TYPE_NUM: + incr_len(FP_BUF_LEN + end_len); + cur = write_num(cur, val, flg); + if (unlikely(!cur)) goto fail_num; + break; - /* not prefixed with zero, e.g. 1.234, 1234.0 */ - no_pre_zero = (dot_ofs > 0); + case YYJSON_TYPE_BOOL: + incr_len(8); + cur = write_bool(cur, unsafe_yyjson_get_bool(val)); + break; - /* 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); + case YYJSON_TYPE_NULL: + incr_len(8); + cur = write_null(cur); + break; - /* 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); + case YYJSON_TYPE_ARR: + incr_len(2 + end_len); + byte_copy_2(cur, "[]"); + cur += 2; + break; - /* write the dot */ - dot_set_pos = yyjson_max(dot_ofs, 1); - buf[dot_set_pos] = '.'; + case YYJSON_TYPE_OBJ: + incr_len(2 + end_len); + byte_copy_2(cur, "{}"); + cur += 2; + break; - /* 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; + default: + goto fail_type; + } - } else { - /* binary to decimal */ - f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, - &sig_dec, &exp_dec); + if (newline) *cur++ = '\n'; + *cur = '\0'; + *dat_len = (usize)(cur - hdr); + memset(err, 0, sizeof(yyjson_write_err)); + return hdr; - /* the sig length is 16 or 17 */ - sig_len = 16 + (sig_dec >= (u64)100000000 * 100000000); +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 with scientific notation, e.g. 1.234e56 */ - end = write_u64_len_16_to_17_trim(sig_dec, buf + 1); - end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */ - exp_dec += sig_len - 1; - buf[0] = buf[1]; - buf[1] = '.'; - return write_f64_exp(exp_dec, end); - } - } else { - /* subnormal number */ - byte_copy_4(buf, "0.0"); - return buf + 3; - } +#undef return_err +#undef check_str_len +#undef incr_len } -#else /* FP_WRITER */ +/** 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) -#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 +#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) -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; - } - } - return NULL; - } else { - /* finite number */ - u8 *end = buf + len, *dot = NULL, *exp = NULL; +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) - /* - 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; - } + yyjson_val *val; + yyjson_type val_type; + usize ctn_len, ctn_len_tmp; + bool ctn_obj, ctn_obj_tmp, is_key; + u8 *hdr, *cur, *end, *tmp; + yyjson_write_ctx *ctx, *ctx_tmp; + usize alc_len, alc_inc, ctx_len, ext_len, str_len; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + bool newline = has_flg(NEWLINE_AT_END) != 0; + + alc_len = root->uni.ofs / sizeof(yyjson_val); + alc_len = alc_len * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; + alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); + if (!hdr) goto fail_alloc; + cur = hdr; + end = hdr + alc_len; + ctx = (yyjson_write_ctx *)(void *)end; + +doc_begin: + val = constcast(yyjson_val *)root; + val_type = unsafe_yyjson_get_type(val); + ctn_obj = (val_type == YYJSON_TYPE_OBJ); + ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + val++; + +val_begin: + val_type = unsafe_yyjson_get_type(val); + if (val_type == YYJSON_TYPE_STR) { + is_key = ((u8)ctn_obj & (u8)~ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 16); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; } - 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, val, flg); + if (unlikely(!cur)) goto fail_num; + *cur++ = ','; + goto val_end; + } + if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == + (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { + ctn_len_tmp = unsafe_yyjson_get_len(val); + ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); + incr_len(16); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); + *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); + *cur++ = ','; + goto val_end; } else { - 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_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; } - 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); -} - -/** 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); -} - -/** 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); - } else { - return write_f64_raw(buf, raw, flg); + 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; -#endif /* FP_WRITER */ +val_end: + val++; + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + goto val_begin; -/** 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); +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 { - 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); - } - } + goto ctn_end; } -} - +doc_end: + if (newline) { + incr_len(2); + *(cur - 1) = '\n'; + cur++; + } + *--cur = '\0'; + *dat_len = (usize)(cur - hdr); + memset(err, 0, sizeof(yyjson_write_err)); + return hdr; -/*============================================================================== - * String Writer - *============================================================================*/ +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); -/** 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'. */ +#undef return_err +#undef incr_len +#undef check_str_len +} -/** 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 -}; +/** 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) -/** 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 -}; +#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) -/** 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 -}; +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) -/** 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 -}; + yyjson_val *val; + yyjson_type val_type; + usize ctn_len, ctn_len_tmp; + bool ctn_obj, ctn_obj_tmp, is_key, no_indent; + u8 *hdr, *cur, *end, *tmp; + yyjson_write_ctx *ctx, *ctx_tmp; + usize alc_len, alc_inc, ctx_len, ext_len, str_len, level; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + usize spaces = has_flg(PRETTY_TWO_SPACES) ? 2 : 4; + bool newline = has_flg(NEWLINE_AT_END) != 0; -/** 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' -}; + 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; -/** 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', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', '\\', '"', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', '\\', '/', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - '\\', '\\', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' -}; +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; -/** 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; +val_begin: + val_type = unsafe_yyjson_get_type(val); + if (val_type == YYJSON_TYPE_STR) { + is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; + } + *cur++ = is_key ? ':' : ','; + *cur++ = is_key ? ' ' : '\n'; + goto val_end; + } + if (val_type == YYJSON_TYPE_NUM) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(FP_BUF_LEN + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_num(cur, val, flg); + if (unlikely(!cur)) goto fail_num; + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } + if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == + (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + ctn_len_tmp = unsafe_yyjson_get_len(val); + ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); + *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); + *cur++ = ','; + *cur++ = '\n'; + goto val_end; } else { - return enc_table_esc; + /* push context, setup new container */ + incr_len(32 + (no_indent ? 0 : level * 4)); + yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj); + ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; + ctn_obj = ctn_obj_tmp; + cur = write_indent(cur, no_indent ? 0 : level, spaces); + level++; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + *cur++ = '\n'; + val++; + goto val_begin; } + } + if (val_type == YYJSON_TYPE_BOOL) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_bool(cur, unsafe_yyjson_get_bool(val)); + cur += 2; + goto val_end; + } + if (val_type == YYJSON_TYPE_NULL) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_null(cur); + cur += 2; + goto val_end; + } + if (val_type == YYJSON_TYPE_RAW) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len + 3 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_raw(cur, str_ptr, str_len); + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } + goto fail_type; + +val_end: + val++; + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + goto val_begin; + +ctn_end: + cur -= 2; + *cur++ = '\n'; + incr_len(level * 4); + cur = write_indent(cur, --level, spaces); + *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); + if (unlikely((u8 *)ctx >= end)) goto doc_end; + yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj); + ctn_len--; + *cur++ = ','; + *cur++ = '\n'; + if (likely(ctn_len > 0)) { + goto val_begin; } else { - if (has_write_flag(ESCAPE_SLASHES)) { - return enc_table_cpy_slash; - } else { - return enc_table_cpy; - } + goto ctn_end; } -} -/** 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; +doc_end: + if (newline) { + incr_len(2); + *cur++ = '\n'; + } + *cur = '\0'; + *dat_len = (usize)(cur - hdr); + memset(err, 0, sizeof(yyjson_write_err)); + return hdr; + +fail_alloc: return_err(MEMORY_ALLOCATION, MSG_MALLOC); +fail_type: return_err(INVALID_VALUE_TYPE, MSG_ERR_TYPE); +fail_num: return_err(NAN_OR_INF, MSG_NAN_INF); +fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + +#undef return_err +#undef incr_len +#undef check_str_len } -/** - 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; + + +/*============================================================================== + * MARK: - JSON Writer (Public) + *============================================================================*/ + +char *yyjson_val_write_opts(const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + usize tmp_dat_len; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + yyjson_val *root = constcast(yyjson_val *)val; + + if (!err) err = &tmp_err; + if (!dat_len) dat_len = &tmp_dat_len; + + if (unlikely(!root)) { + *dat_len = 0; + err->msg = "input JSON is NULL"; + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; + return NULL; } - while (str_len >= 4) { - byte_copy_4(cur, str); - cur += 4; - str += 4; - str_len -= 4; + + 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); } - while (str_len) { - *cur++ = *str++; - str_len -= 1; +} + +char *yyjson_write_opts(const yyjson_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + yyjson_val *root = doc ? doc->root : NULL; + return yyjson_val_write_opts(root, flg, alc_ptr, dat_len, err); +} + +bool yyjson_val_write_file(const char *path, + const yyjson_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + u8 *dat; + usize dat_len = 0; + yyjson_val *root = constcast(yyjson_val *)val; + bool suc; + + if (!err) err = &tmp_err; + if (unlikely(!path || !*path)) { + err->msg = "input path is invalid"; + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; + return false; } - *cur++ = '"'; - return cur; + + dat = (u8 *)yyjson_val_write_opts(root, flg, &alc, &dat_len, err); + if (unlikely(!dat)) return false; + suc = write_dat_to_file(path, dat, dat_len, err); + alc.free(alc.ctx, dat); + return suc; } -/** - 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) { +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; - /* UTF-8 character mask and pattern, see `read_str()` 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 + if (!err) err = &tmp_err; + if (unlikely(!fp)) { + err->msg = "input fp is invalid"; + err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; + return false; + } + + dat = (u8 *)yyjson_val_write_opts(root, flg, &alc, &dat_len, err); + if (unlikely(!dat)) return false; + suc = write_dat_to_fp(fp, dat, dat_len, err); + alc.free(alc.ctx, dat); + return suc; +} -#define is_valid_seq_2(uni) ( \ - ((uni & b2_mask) == b2_patt) && \ - ((uni & b2_requ)) \ -) +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); +} -#define is_valid_seq_3(uni) ( \ - ((uni & b3_mask) == b3_patt) && \ - ((tmp = (uni & b3_requ))) && \ - ((tmp != b3_erro)) \ -) +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); +} -#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++ = '"'; +/*============================================================================== + * MARK: - Mutable JSON Writer Implementation (Private) + *============================================================================*/ -copy_ascii: - /* - Copy continuous ASCII, loop unrolling, same as the following code: +typedef struct yyjson_mut_write_ctx { + usize tag; + yyjson_mut_val *ctn; +} yyjson_mut_write_ctx; - 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; +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; +} -#define expr_stop(i) \ - stop_char_##i: \ - memcpy(cur, src, i); \ - cur += i; src += i; goto copy_utf8; +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; +} - while (end - src >= 16) { - repeat16_incr(expr_jump) - byte_copy_16(cur, src); - cur += 16; src += 16; +/** 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; +} - while (end - src >= 4) { - repeat4_incr(expr_jump) - byte_copy_4(cur, src); - cur += 4; src += 4; - } +/** 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); +} - while (end > src) { - expr_jump(0) - *cur++ = *src++; - } +/** Write JSON document minify. + The root of this document should be a non-empty container. */ +static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, + usize estimated_val_num, + yyjson_write_flag flg, + yyjson_alc alc, + usize *dat_len, + yyjson_write_err *err) { +#define return_err(_code, _msg) do { \ + *dat_len = 0; \ + err->code = YYJSON_WRITE_ERROR_##_code; \ + err->msg = _msg; \ + if (hdr) alc.free(alc.ctx, hdr); \ + return NULL; \ +} while (false) - *cur++ = '"'; - return cur; +#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) - repeat16_incr(expr_stop) +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) -#undef expr_jump -#undef expr_stop + yyjson_mut_val *val, *ctn; + yyjson_type val_type; + usize ctn_len, ctn_len_tmp; + bool ctn_obj, ctn_obj_tmp, is_key; + u8 *hdr, *cur, *end, *tmp; + yyjson_mut_write_ctx *ctx, *ctx_tmp; + usize alc_len, alc_inc, ctx_len, ext_len, str_len; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + bool newline = has_flg(NEWLINE_AT_END) != 0; -copy_utf8: - if (unlikely(src + 4 > end)) { - if (end == src) goto copy_end; - if (end - src < enc_table[*src] / 2) goto err_one; - } - switch (enc_table[*src]) { - case CHAR_ENC_CPY_1: { - *cur++ = *src++; - goto copy_ascii; - } - case CHAR_ENC_CPY_2: { - 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; + alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; + alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); + if (!hdr) goto fail_alloc; + cur = hdr; + end = hdr + alc_len; + ctx = (yyjson_mut_write_ctx *)(void *)end; + +doc_begin: + val = constcast(yyjson_mut_val *)root; + val_type = unsafe_yyjson_get_type(val); + ctn_obj = (val_type == YYJSON_TYPE_OBJ); + ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + ctn = val; + val = (yyjson_mut_val *)val->uni.ptr; /* tail */ + val = ctn_obj ? val->next->next : val->next; + +val_begin: + val_type = unsafe_yyjson_get_type(val); + if (val_type == YYJSON_TYPE_STR) { + is_key = ((u8)ctn_obj & (u8)~ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 16); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; } - case CHAR_ENC_ERR_1: { - goto err_one; + *cur++ = is_key ? ':' : ','; + goto val_end; + } + if (val_type == YYJSON_TYPE_NUM) { + incr_len(FP_BUF_LEN); + cur = write_num(cur, (yyjson_val *)val, flg); + if (unlikely(!cur)) goto fail_num; + *cur++ = ','; + goto val_end; + } + if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == + (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { + ctn_len_tmp = unsafe_yyjson_get_len(val); + ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); + incr_len(16); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); + *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); + *cur++ = ','; + goto val_end; + } else { + /* push context, setup new container */ + yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); + ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; + ctn_obj = ctn_obj_tmp; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + ctn = val; + val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ + val = ctn_obj ? val->next->next : val->next; + goto val_begin; } - default: break; } + 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; -copy_end: - *cur++ = '"'; - return cur; +val_end: + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + val = val->next; + goto val_begin; -err_one: - if (esc) goto err_esc; - else goto err_cpy; +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; + } -err_cpy: - if (!inv) return NULL; - *cur++ = *src++; - goto copy_utf8; +doc_end: + if (newline) { + incr_len(2); + *(cur - 1) = '\n'; + cur++; + } + *--cur = '\0'; + *dat_len = (usize)(cur - hdr); + err->code = YYJSON_WRITE_SUCCESS; + err->msg = NULL; + return hdr; -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; +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 is_valid_seq_2 -#undef is_valid_seq_3 -#undef is_valid_seq_4 +#undef return_err +#undef incr_len +#undef check_str_len } +/** Write JSON document pretty. + The root of this document should be a non-empty container. */ +static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, + usize estimated_val_num, + yyjson_write_flag flg, + yyjson_alc alc, + usize *dat_len, + yyjson_write_err *err) { +#define return_err(_code, _msg) do { \ + *dat_len = 0; \ + err->code = YYJSON_WRITE_ERROR_##_code; \ + err->msg = _msg; \ + if (hdr) alc.free(alc.ctx, hdr); \ + return NULL; \ +} while (false) + +#define incr_len(_len) do { \ + ext_len = (usize)(_len); \ + if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ + usize ctx_pos = (usize)((u8 *)ctx - hdr); \ + usize cur_pos = (usize)(cur - hdr); \ + ctx_len = (usize)(end - (u8 *)ctx); \ + alc_inc = yyjson_max(alc_len / 2, ext_len); \ + alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ + if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ + goto fail_alloc; \ + alc_len += alc_inc; \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + if (unlikely(!tmp)) goto fail_alloc; \ + ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ + memmove((void *)ctx_tmp, (void *)(tmp + ctx_pos), ctx_len); \ + ctx = ctx_tmp; \ + cur = tmp + cur_pos; \ + end = tmp + alc_len; \ + hdr = tmp; \ + } \ +} while (false) + +#define check_str_len(_len) do { \ + if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ + goto fail_alloc; \ +} while (false) + yyjson_mut_val *val, *ctn; + yyjson_type val_type; + usize ctn_len, ctn_len_tmp; + bool ctn_obj, ctn_obj_tmp, is_key, no_indent; + u8 *hdr, *cur, *end, *tmp; + yyjson_mut_write_ctx *ctx, *ctx_tmp; + usize alc_len, alc_inc, ctx_len, ext_len, str_len, level; + const u8 *str_ptr; + const char_enc_type *enc_table = get_enc_table_with_flag(flg); + bool cpy = (enc_table == enc_table_cpy); + bool esc = has_flg(ESCAPE_UNICODE) != 0; + bool inv = has_allow(INVALID_UNICODE) != 0; + usize spaces = has_flg(PRETTY_TWO_SPACES) ? 2 : 4; + bool newline = has_flg(NEWLINE_AT_END) != 0; -/*============================================================================== - * Writer Utilities - *============================================================================*/ + alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; + alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); + if (!hdr) goto fail_alloc; + cur = hdr; + end = hdr + alc_len; + ctx = (yyjson_mut_write_ctx *)(void *)end; + +doc_begin: + val = constcast(yyjson_mut_val *)root; + val_type = unsafe_yyjson_get_type(val); + ctn_obj = (val_type == YYJSON_TYPE_OBJ); + ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + *cur++ = '\n'; + ctn = val; + val = (yyjson_mut_val *)val->uni.ptr; /* tail */ + val = ctn_obj ? val->next->next : val->next; + level = 1; + +val_begin: + val_type = unsafe_yyjson_get_type(val); + if (val_type == YYJSON_TYPE_STR) { + is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { + cur = write_str_noesc(cur, str_ptr, str_len); + } else { + cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); + if (unlikely(!cur)) goto fail_str; + } + *cur++ = is_key ? ':' : ','; + *cur++ = is_key ? ' ' : '\n'; + goto val_end; + } + if (val_type == YYJSON_TYPE_NUM) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(FP_BUF_LEN + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_num(cur, (yyjson_val *)val, flg); + if (unlikely(!cur)) goto fail_num; + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } + if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == + (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + ctn_len_tmp = unsafe_yyjson_get_len(val); + ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); + if (unlikely(ctn_len_tmp == 0)) { + /* write empty container */ + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); + *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } else { + /* push context, setup new container */ + incr_len(32 + (no_indent ? 0 : level * 4)); + yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); + ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; + ctn_obj = ctn_obj_tmp; + cur = write_indent(cur, no_indent ? 0 : level, spaces); + level++; + *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); + *cur++ = '\n'; + ctn = val; + val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ + val = ctn_obj ? val->next->next : val->next; + goto val_begin; + } + } + if (val_type == YYJSON_TYPE_BOOL) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_bool(cur, unsafe_yyjson_get_bool(val)); + cur += 2; + goto val_end; + } + if (val_type == YYJSON_TYPE_NULL) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + incr_len(16 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_null(cur); + cur += 2; + goto val_end; + } + if (val_type == YYJSON_TYPE_RAW) { + no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); + str_len = unsafe_yyjson_get_len(val); + str_ptr = (const u8 *)unsafe_yyjson_get_str(val); + check_str_len(str_len); + incr_len(str_len + 3 + (no_indent ? 0 : level * 4)); + cur = write_indent(cur, no_indent ? 0 : level, spaces); + cur = write_raw(cur, str_ptr, str_len); + *cur++ = ','; + *cur++ = '\n'; + goto val_end; + } + goto fail_type; -/** 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; -} +val_end: + ctn_len--; + if (unlikely(ctn_len == 0)) goto ctn_end; + val = val->next; + goto val_begin; -/** 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); +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 { - byte_copy_8(cur, &v0); + goto ctn_end; } - return cur + 5 - val; -} -/** Write indent (requires level x 4 bytes buffer). - Param spaces should not larger than 4. */ -static_inline u8 *write_indent(u8 *cur, usize level, usize spaces) { - while (level-- > 0) { - byte_copy_4(cur, " "); - cur += spaces; +doc_end: + if (newline) { + incr_len(2); + *cur++ = '\n'; } - return cur; -} + *cur = '\0'; + *dat_len = (usize)(cur - hdr); + err->code = YYJSON_WRITE_SUCCESS; + err->msg = NULL; + return hdr; -/** Write data to file pointer. */ -static bool write_dat_to_fp(FILE *fp, u8 *dat, usize len, - yyjson_write_err *err) { - if (fwrite(dat, len, 1, fp) != 1) { - err->msg = "file writing failed"; - err->code = YYJSON_WRITE_ERROR_FILE_WRITE; - return false; - } - return true; +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 data to file. */ -static bool write_dat_to_file(const char *path, u8 *dat, usize len, - yyjson_write_err *err) { +static char *yyjson_mut_write_opts_impl(const yyjson_mut_val *val, + usize estimated_val_num, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + usize tmp_dat_len; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + yyjson_mut_val *root = constcast(yyjson_mut_val *)val; -#define return_err(_code, _msg) do { \ - err->msg = _msg; \ - err->code = YYJSON_WRITE_ERROR_##_code; \ - if (file) fclose(file); \ - return false; \ -} while (false) + if (!err) err = &tmp_err; + if (!dat_len) dat_len = &tmp_dat_len; - FILE *file = fopen_writeonly(path); - if (file == NULL) { - return_err(FILE_OPEN, MSG_FOPEN); - } - if (fwrite(dat, len, 1, file) != 1) { - return_err(FILE_WRITE, MSG_FWRITE); - } - if (fclose(file) != 0) { - file = NULL; - return_err(FILE_WRITE, MSG_FCLOSE); + if (unlikely(!root)) { + *dat_len = 0; + err->msg = "input JSON is NULL"; + err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; + return NULL; } - return true; -#undef return_err + 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); + } } /*============================================================================== - * JSON Writer Implementation + * MARK: - Mutable JSON Writer (Public) *============================================================================*/ -typedef struct yyjson_write_ctx { - usize tag; -} yyjson_write_ctx; - -static_inline void yyjson_write_ctx_set(yyjson_write_ctx *ctx, - usize size, bool is_obj) { - ctx->tag = (size << 1) | (usize)is_obj; +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); } -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); +char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + usize *dat_len, + yyjson_write_err *err) { + yyjson_mut_val *root; + usize estimated_val_num; + if (likely(doc)) { + root = doc->root; + estimated_val_num = yyjson_mut_doc_estimated_val_num(doc); + } else { + root = NULL; + estimated_val_num = 0; + } + return yyjson_mut_write_opts_impl(root, estimated_val_num, + flg, alc_ptr, dat_len, err); } -/** 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_str_noesc(cur, str_ptr, str_len); - } else { - cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - break; - - case YYJSON_TYPE_NUM: - incr_len(FP_BUF_LEN + end_len); - cur = write_num(cur, val, flg); - if (unlikely(!cur)) goto fail_num; - break; - - case YYJSON_TYPE_BOOL: - incr_len(8); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - break; - - case YYJSON_TYPE_NULL: - incr_len(8); - cur = write_null(cur); - break; - - case YYJSON_TYPE_ARR: - incr_len(2 + end_len); - byte_copy_2(cur, "[]"); - cur += 2; - break; - - case YYJSON_TYPE_OBJ: - incr_len(2 + end_len); - byte_copy_2(cur, "{}"); - cur += 2; - break; +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; - default: - goto fail_type; + if (!err) err = &tmp_err; + if (unlikely(!path || !*path)) { + err->msg = "input path is invalid"; + err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; + return false; } - if (newline) *cur++ = '\n'; - *cur = '\0'; - *dat_len = (usize)(cur - hdr); - memset(err, 0, sizeof(yyjson_write_err)); - return hdr; + dat = (u8 *)yyjson_mut_val_write_opts(root, flg, &alc, &dat_len, err); + if (unlikely(!dat)) return false; + suc = write_dat_to_file(path, dat, dat_len, err); + alc.free(alc.ctx, dat); + return suc; +} + +bool yyjson_mut_val_write_fp(FILE *fp, + const yyjson_mut_val *val, + yyjson_write_flag flg, + const yyjson_alc *alc_ptr, + yyjson_write_err *err) { + yyjson_write_err tmp_err; + yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; + u8 *dat; + usize dat_len = 0; + yyjson_mut_val *root = constcast(yyjson_mut_val *)val; + bool suc; -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_INF_NAN); -fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + if (!err) err = &tmp_err; + if (unlikely(!fp)) { + err->msg = "input fp is invalid"; + err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; + return false; + } -#undef return_err -#undef check_str_len -#undef incr_len + 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; } -/** 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) { +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); +} -#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) +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); +} -#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) +#undef has_flg +#undef has_allow +#endif /* YYJSON_DISABLE_WRITER */ -#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; +#if !YYJSON_DISABLE_UTILS -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++; +/*============================================================================== + * MARK: - JSON Pointer API (RFC 6901) (Public) + *============================================================================*/ -val_begin: - val_type = unsafe_yyjson_get_type(val); - if (val_type == YYJSON_TYPE_STR) { - is_key = ((u8)ctn_obj & (u8)~ctn_len); - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len * 6 + 16); - if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { - cur = write_str_noesc(cur, str_ptr, str_len); - } else { - cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; +/** + 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; - } - 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; + *ptr = cur; + *len = (usize)(cur - hdr) - esc_num; + *esc = esc_num; + return hdr; } - 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; - } +} + +/** + 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_BOOL) { - incr_len(16); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - cur++; - goto val_end; + if (*cur == '-') { + if (unlikely(len > 1)) return false; + *idx = USIZE_MAX; + return true; } - if (val_type == YYJSON_TYPE_NULL) { - incr_len(16); - cur = write_null(cur); - cur++; - goto val_end; + for (; cur < end && (add = (usize)((u8)*cur - (u8)'0')) <= 9; cur++) { + num = num * 10 + add; } - 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; + 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; } - 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; +/** + 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 { - goto ctn_end; + while (idx-- > 0) val = unsafe_yyjson_get_next(val); + return val; } +} -doc_end: - if (newline) { - incr_len(2); - *(cur - 1) = '\n'; - cur++; +/** + 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; } - *--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_INF_NAN); -fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + return NULL; +} -#undef return_err -#undef incr_len -#undef check_str_len +/** + Get a value from array by token. + @param arr [in] an array, should not be NULL or non-array type + @param token [in] a JSON pointer token + @param len [in] unescaped token length + @param esc [in] number of escaped characters in this token + @param pre [out] previous (sibling) value of the returned value + @param last [out] whether index is last + @return value at index, or NULL if token is not index or index is out of range + */ +static_inline yyjson_mut_val *ptr_mut_arr_get(yyjson_mut_val *arr, + const char *token, + usize len, usize esc, + yyjson_mut_val **pre, + bool *last) { + yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; /* last (tail) */ + usize num = unsafe_yyjson_get_len(arr), idx; + if (last) *last = false; + if (pre) *pre = NULL; + if (unlikely(num == 0)) { + if (last && len == 1 && (*token == '0' || *token == '-')) *last = true; + return NULL; + } + if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; + if (last) *last = (idx == num || idx == USIZE_MAX); + if (unlikely(idx >= num)) return NULL; + while (idx-- > 0) val = val->next; + if (pre) *pre = val; + return val->next; } -/** 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) { +/** + Get a value from object by token. + @param obj [in] an object, should not be NULL or non-object type + @param token [in] a JSON pointer token + @param len [in] unescaped token length + @param esc [in] number of escaped characters in this token + @param pre [out] previous (sibling) key of the returned value's key + @return value associated with the token, or NULL if no value + */ +static_inline yyjson_mut_val *ptr_mut_obj_get(yyjson_mut_val *obj, + const char *token, + usize len, usize esc, + yyjson_mut_val **pre) { + yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr, *key; + usize num = unsafe_yyjson_get_len(obj); + if (pre) *pre = NULL; + if (unlikely(num == 0)) return NULL; + for (; num > 0; num--, pre_key = key) { + key = pre_key->next->next; + if (ptr_token_eq(key, token, len, esc)) { + if (pre) *pre = pre_key; + return key->next; + } + } + return NULL; +} -#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) +/** + 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); + } +} -#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_str_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(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_str(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_num(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 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( + 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 { - /* 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 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; - } - 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; +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) { -ctn_end: - cur -= 2; - *cur++ = '\n'; - incr_len(level * 4); - cur = write_indent(cur, --level, spaces); - *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); - if (unlikely((u8 *)ctx >= end)) goto doc_end; - yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj); - ctn_len--; - *cur++ = ','; - *cur++ = '\n'; - if (likely(ctn_len > 0)) { - goto val_begin; - } else { - goto ctn_end; - } + 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; -doc_end: - if (newline) { - incr_len(2); - *cur++ = '\n'; + /* 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 */ } - *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_INF_NAN); -fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + /* create parent nodes if not exist */ + if (unlikely(ptr != end)) { /* not last token */ + if (!create_parent) return_err_resolve(false, token - hdr); -#undef return_err -#undef incr_len -#undef check_str_len -} + /* 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); -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; + /* 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); + } - err = err ? err : &dummy_err; - dat_len = dat_len ? dat_len : &dummy_dat_len; + /* 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); - if (unlikely(!root)) { - *dat_len = 0; - err->msg = "input JSON is NULL"; - err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; - return NULL; + /* 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; + } } - 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; - 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; + 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; - err = err ? err : &dummy_err; - if (unlikely(!path || !*path)) { - err->msg = "input path 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, &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; + ctx->old = cur_val; + return cur_val; } -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; - 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; +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) { - err = err ? err : &dummy_err; - if (unlikely(!fp)) { - err->msg = "input fp is invalid"; - err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; - return false; + 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; } - - dat = (u8 *)yyjson_val_write_opts(root, flg, &alc, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_fp(fp, dat, dat_len, err); - alc.free(alc.ctx, dat); - return suc; -} - -bool yyjson_write_file(const char *path, - const yyjson_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_val *root = doc ? doc->root : NULL; - return yyjson_val_write_file(path, root, flg, alc_ptr, err); + return cur_val; } -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); -} +/* 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; -} +/* 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; -/** 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; +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; } - return sum; -} - -/** Write single JSON value. */ -static_inline u8 *yyjson_mut_write_single(yyjson_mut_val *val, - yyjson_write_flag flg, - yyjson_alc alc, - usize *dat_len, - yyjson_write_err *err) { - return yyjson_write_single((yyjson_val *)val, flg, alc, dat_len, err); } -/** Write JSON document minify. - The root of this document should be a non-empty container. */ -static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, - usize estimated_val_num, - yyjson_write_flag flg, - yyjson_alc alc, - usize *dat_len, - yyjson_write_err *err) { - +/* 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) +#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) - 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; +#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) - 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; +yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, + yyjson_val *orig, + yyjson_val *patch, + yyjson_patch_err *err) { -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; + 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)); -val_begin: - val_type = unsafe_yyjson_get_type(val); - if (val_type == YYJSON_TYPE_STR) { - is_key = ((u8)ctn_obj & (u8)~ctn_len); - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len * 6 + 16); - if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { - cur = write_str_noesc(cur, str_ptr, str_len); - } else { - cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - *cur++ = is_key ? ':' : ','; - goto val_end; - } - if (val_type == YYJSON_TYPE_NUM) { - incr_len(FP_BUF_LEN); - cur = write_num(cur, (yyjson_val *)val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - goto val_end; - } - if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == - (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { - ctn_len_tmp = unsafe_yyjson_get_len(val); - ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); - incr_len(16); - if (unlikely(ctn_len_tmp == 0)) { - /* write empty container */ - *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); - *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); - *cur++ = ','; - goto val_end; - } else { - /* push context, setup new container */ - yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); - ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; - ctn_obj = ctn_obj_tmp; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - ctn = val; - val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ - val = ctn_obj ? val->next->next : val->next; - goto val_begin; - } - } - if (val_type == YYJSON_TYPE_BOOL) { - incr_len(16); - cur = write_bool(cur, unsafe_yyjson_get_bool(val)); - cur++; - goto val_end; - } - if (val_type == YYJSON_TYPE_NULL) { - incr_len(16); - cur = write_null(cur); - cur++; - goto val_end; + if (unlikely(!doc || !orig || !patch)) { + return_err(INVALID_PARAMETER, "input parameter is NULL"); } - 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; + if (unlikely(!yyjson_is_arr(patch))) { + return_err(INVALID_PARAMETER, "input patch is not array"); } - goto fail_type; + root = yyjson_val_mut_copy(doc, orig); + if (unlikely(!root)) return_err_copy(); -val_end: - ctn_len--; - if (unlikely(ctn_len == 0)) goto ctn_end; - val = val->next; - goto val_begin; + /* 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"); + } -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; - } + /* 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); -doc_end: - if (newline) { - incr_len(2); - *(cur - 1) = '\n'; - cur++; - } - *--cur = '\0'; - *dat_len = (usize)(cur - hdr); - err->code = YYJSON_WRITE_SUCCESS; - err->msg = NULL; - return hdr; + /* 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); -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_INF_NAN); -fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); + /* 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; + } -#undef return_err -#undef incr_len -#undef check_str_len + /* 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; } -/** 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) +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)); -#define check_str_len(_len) do { \ - if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ - goto fail_alloc; \ -} while (false) + 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(); - 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; + /* 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"); + } - 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; + /* 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); -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; + /* 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); -val_begin: - val_type = unsafe_yyjson_get_type(val); - if (val_type == YYJSON_TYPE_STR) { - is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - str_len = unsafe_yyjson_get_len(val); - str_ptr = (const u8 *)unsafe_yyjson_get_str(val); - check_str_len(str_len); - incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { - cur = write_str_noesc(cur, str_ptr, str_len); - } else { - cur = write_str(cur, esc, inv, str_ptr, str_len, enc_table); - if (unlikely(!cur)) goto fail_str; - } - *cur++ = is_key ? ':' : ','; - *cur++ = is_key ? ' ' : '\n'; - goto val_end; - } - if (val_type == YYJSON_TYPE_NUM) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - incr_len(FP_BUF_LEN + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - cur = write_num(cur, (yyjson_val *)val, flg); - if (unlikely(!cur)) goto fail_num; - *cur++ = ','; - *cur++ = '\n'; - goto val_end; - } - if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == - (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { - no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); - ctn_len_tmp = unsafe_yyjson_get_len(val); - ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); - if (unlikely(ctn_len_tmp == 0)) { - /* write empty container */ - incr_len(16 + (no_indent ? 0 : level * 4)); - cur = write_indent(cur, no_indent ? 0 : level, spaces); - *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); - *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); - *cur++ = ','; - *cur++ = '\n'; - goto val_end; - } else { - /* push context, setup new container */ - incr_len(32 + (no_indent ? 0 : level * 4)); - yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); - ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; - ctn_obj = ctn_obj_tmp; - cur = write_indent(cur, no_indent ? 0 : level, spaces); - level++; - *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); - *cur++ = '\n'; - ctn = val; - val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ - val = ctn_obj ? val->next->next : val->next; - goto val_begin; + /* 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; + return root; +} -val_end: - ctn_len--; - if (unlikely(ctn_len == 0)) goto ctn_end; - val = val->next; - goto val_begin; +/* 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 -ctn_end: - cur -= 2; - *cur++ = '\n'; - incr_len(level * 4); - cur = write_indent(cur, --level, spaces); - *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); - if (unlikely((u8 *)ctx >= end)) goto doc_end; - val = ctn->next; - yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj); - ctn_len--; - *cur++ = ','; - *cur++ = '\n'; - if (likely(ctn_len > 0)) { - goto val_begin; - } else { - goto ctn_end; - } -doc_end: - if (newline) { - incr_len(2); - *cur++ = '\n'; - } - *cur = '\0'; - *dat_len = (usize)(cur - hdr); - err->code = YYJSON_WRITE_SUCCESS; - err->msg = NULL; - return hdr; -fail_alloc: return_err(MEMORY_ALLOCATION, MSG_MALLOC); -fail_type: return_err(INVALID_VALUE_TYPE, MSG_ERR_TYPE); -fail_num: return_err(NAN_OR_INF, MSG_INF_NAN); -fail_str: return_err(INVALID_STRING, MSG_ERR_UTF8); +/*============================================================================== + * MARK: - JSON Merge-Patch API (RFC 7386) (Public) + *============================================================================*/ -#undef return_err -#undef incr_len -#undef check_str_len -} +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; -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; + if (unlikely(!yyjson_is_obj(patch))) { + return yyjson_val_mut_copy(doc, patch); + } - err = err ? err : &dummy_err; - dat_len = dat_len ? dat_len : &dummy_dat_len; + builder = yyjson_mut_obj(doc); + if (unlikely(!builder)) return NULL; - if (unlikely(!root)) { - *dat_len = 0; - err->msg = "input JSON is NULL"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - 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 (!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); + /* 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; + } + } } -} - -char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - usize *dat_len, - yyjson_write_err *err) { - return yyjson_mut_write_opts_impl(val, 0, flg, alc_ptr, dat_len, err); -} -char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - usize *dat_len, - yyjson_write_err *err) { - yyjson_mut_val *root; - usize estimated_val_num; - if (likely(doc)) { - root = doc->root; - estimated_val_num = yyjson_mut_doc_estimated_val_num(doc); - } else { - root = NULL; - estimated_val_num = 0; + /* 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 yyjson_mut_write_opts_impl(root, estimated_val_num, - flg, alc_ptr, dat_len, err); + + return builder; } -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; - 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; +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; - err = err ? err : &dummy_err; - if (unlikely(!path || !*path)) { - err->msg = "input path is invalid"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - return false; + if (unlikely(!yyjson_mut_is_obj(patch))) { + return yyjson_mut_val_mut_copy(doc, patch); } - dat = (u8 *)yyjson_mut_val_write_opts(root, flg, &alc, &dat_len, err); - if (unlikely(!dat)) return false; - suc = write_dat_to_file(path, dat, dat_len, err); - alc.free(alc.ctx, dat); - return suc; -} - -bool yyjson_mut_val_write_fp(FILE *fp, - const yyjson_mut_val *val, - yyjson_write_flag flg, - const yyjson_alc *alc_ptr, - yyjson_write_err *err) { - yyjson_write_err dummy_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; + builder = yyjson_mut_obj(doc); + if (unlikely(!builder)) return NULL; - err = err ? err : &dummy_err; - if (unlikely(!fp)) { - err->msg = "input fp is invalid"; - err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; - return false; + memset(&local_orig, 0, sizeof(local_orig)); + if (!yyjson_mut_is_obj(orig)) { + orig = &local_orig; + orig->tag = builder->tag; + orig->uni = builder->uni; } - 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; -} + /* 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; + } + } + } -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/src/3rdparty/yyjson/yyjson.h b/src/3rdparty/yyjson/yyjson.h index 824026059a..5eb6d46801 100644 --- a/src/3rdparty/yyjson/yyjson.h +++ b/src/3rdparty/yyjson/yyjson.h @@ -32,7 +32,7 @@ /*============================================================================== - * Header Files + * MARK: - Header Files *============================================================================*/ #include @@ -45,7 +45,7 @@ /*============================================================================== - * Compile-time Options + * MARK: - Compile-time Options *============================================================================*/ /* @@ -89,14 +89,8 @@ #endif /* - Define as 1 to disable non-standard JSON features support at compile-time: - - YYJSON_READ_ALLOW_INF_AND_NAN - - YYJSON_READ_ALLOW_COMMENTS - - YYJSON_READ_ALLOW_TRAILING_COMMAS - - YYJSON_READ_ALLOW_INVALID_UNICODE - - YYJSON_READ_ALLOW_BOM - - YYJSON_WRITE_ALLOW_INF_AND_NAN - - YYJSON_WRITE_ALLOW_INVALID_UNICODE + 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. */ @@ -151,7 +145,7 @@ /*============================================================================== - * Compiler Macros + * MARK: - Compiler Macros *============================================================================*/ /** compiler version (MSVC) */ @@ -179,8 +173,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 @@ -465,7 +461,7 @@ /*============================================================================== - * Compile Hint Begin + * MARK: - Compile Hint Begin *============================================================================*/ /* extern "C" begin */ @@ -492,23 +488,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 11 +#define YYJSON_VERSION_MINOR 12 /** The patch version of yyjson. */ -#define YYJSON_VERSION_PATCH 1 +#define YYJSON_VERSION_PATCH 0 /** The version of yyjson in hex: `(major << 16) | (minor << 8) | (patch)`. */ -#define YYJSON_VERSION_HEX 0x000B01 +#define YYJSON_VERSION_HEX 0x000C00 /** The version string of yyjson. */ -#define YYJSON_VERSION_STRING "0.11.1" +#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); @@ -516,7 +512,7 @@ yyjson_api uint32_t yyjson_version(void); /*============================================================================== - * JSON Types + * MARK: - JSON Types *============================================================================*/ /** Type of a JSON value (3 bit). */ @@ -578,7 +574,7 @@ typedef uint8_t yyjson_subtype; /*============================================================================== - * Allocator + * MARK: - Allocator *============================================================================*/ /** @@ -662,7 +658,7 @@ yyjson_api void yyjson_alc_dyn_free(yyjson_alc *alc); /*============================================================================== - * Text Locating + * MARK: - Text Locating *============================================================================*/ /** @@ -686,7 +682,7 @@ yyjson_api bool yyjson_locate_pos(const char *str, size_t len, size_t pos, /*============================================================================== - * JSON Structure + * MARK: - JSON Structure *============================================================================*/ /** @@ -720,7 +716,7 @@ typedef struct yyjson_mut_val yyjson_mut_val; /*============================================================================== - * JSON Reader API + * MARK: - JSON Reader API *============================================================================*/ /** Run-time options for JSON reader. */ @@ -734,7 +730,7 @@ 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 @@ -742,27 +738,27 @@ static const yyjson_read_flag YYJSON_READ_NOFLAG = 0; The caller should hold the input data before free the document. The input data must be padded by at least `YYJSON_PADDING_SIZE` bytes. For example: `[1,2]` should be `[1,2]\0\0\0\0`, input length should be 5. */ -static const yyjson_read_flag YYJSON_READ_INSITU = 1 << 0; +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 mult-line comments (non-standard). */ +static const yyjson_read_flag YYJSON_READ_ALLOW_COMMENTS = 1 << 3; /** Allow inf/nan number and literal, case-insensitive, such as 1e999, NaN, inf, -Infinity (non-standard). */ -static const yyjson_read_flag YYJSON_READ_ALLOW_INF_AND_NAN = 1 << 4; +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 @@ -772,16 +768,62 @@ static const yyjson_read_flag YYJSON_READ_NUMBER_AS_RAW = 1 << 5; @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; +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 */ @@ -794,7 +836,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. */ @@ -803,7 +845,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]`. */ @@ -812,7 +854,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`. */ @@ -830,7 +872,7 @@ 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; -/** Unexpected ending during incremental parsing. Parsing state is saved. */ +/** Incomplete input during incremental parsing; parsing state is preserved. */ static const yyjson_read_code YYJSON_READ_ERROR_MORE = 14; /** Error information for JSON reader. */ @@ -884,6 +926,7 @@ yyjson_api yyjson_doc *yyjson_read_opts(char *dat, 2. The `alc` is thread-safe or NULL. @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. If this path is NULL or invalid, the function will fail and return NULL. @param flg The JSON read options. Multiple options can be combined with `|` operator. 0 means no options. @@ -960,6 +1003,9 @@ typedef struct yyjson_incr_state yyjson_incr_state; 2. Call `yyjson_incr_read()` repeatedly. 3. Call `yyjson_incr_free()` to free the state. + Note: The incremental JSON reader only supports standard JSON. + Flags for non-standard features (e.g. comments, trailing commas) are ignored. + @param buf The JSON data, null-terminator is not required. If this parameter is NULL, the function will fail and return NULL. @param buf_len The length of the JSON data in `buf`. @@ -1088,27 +1134,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, @@ -1122,7 +1148,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. */ @@ -1230,7 +1256,7 @@ typedef struct yyjson_write_err { #if !defined(YYJSON_DISABLE_WRITER) || !YYJSON_DISABLE_WRITER /*============================================================================== - * JSON Document Writer API + * MARK: - JSON Document Writer API *============================================================================*/ /** @@ -1267,6 +1293,7 @@ yyjson_api char *yyjson_write_opts(const yyjson_doc *doc, 2. The `alc` is thread-safe or NULL. @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded. @param doc The JSON document. @@ -1370,6 +1397,7 @@ yyjson_api char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, 3. The `alc` is thread-safe or NULL. @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded. @param doc The mutable JSON document. @@ -1439,7 +1467,7 @@ yyjson_api_inline char *yyjson_mut_write(const yyjson_mut_doc *doc, /*============================================================================== - * JSON Value Writer API + * MARK: - JSON Value Writer API *============================================================================*/ /** @@ -1476,6 +1504,7 @@ yyjson_api char *yyjson_val_write_opts(const yyjson_val *val, 2. The `alc` is thread-safe or NULL. @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded. @param val The JSON root value. @@ -1577,6 +1606,7 @@ yyjson_api char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, 3. The `alc` is thread-safe or NULL. @param path The JSON file's path. + This should be a null-terminated string using the system's native encoding. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded. @param val The mutable JSON root value. @@ -1643,12 +1673,40 @@ 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 this parameter is invalid, the function will fail and return NULL. + @param buf A buffer to store the resulting null-terminated string. + If this parameter is NULL, the function will fail and return NULL. + For integer values, the buffer must be at least 21 bytes. + For floating-point values, the buffer must be at least 40 bytes. + @return On success, returns a pointer to the character after the last + written character. On failure, returns NULL. + @note + - This function is thread-safe and does not allocate memory + (when `YYJSON_DISABLE_FAST_FP_CONV` is not defined). + - This function will fail and return NULL only in the following cases: + 1) `val` or `buf` is NULL; + 2) `val` is not a number type; + 3) `val` is `inf` or `nan`, and non-standard JSON is explicitly disabled + via the `YYJSON_DISABLE_NON_STANDARD` flag. + */ +yyjson_api char *yyjson_write_number(const yyjson_val *val, char *buf); + +/** Same as `yyjson_write_number()`. */ +yyjson_api_inline char *yyjson_mut_write_number(const yyjson_mut_val *val, + char *buf) { + return yyjson_write_number((const yyjson_val *)val, buf); +} + #endif /* YYJSON_DISABLE_WRITER */ /*============================================================================== - * JSON Document API + * MARK: - JSON Document API *============================================================================*/ /** Returns the root value of this JSON document. @@ -1673,7 +1731,7 @@ 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. @@ -1735,7 +1793,7 @@ yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val); /*============================================================================== - * JSON Value Content API + * MARK: - JSON Value Content API *============================================================================*/ /** Returns the JSON value's type. @@ -1760,7 +1818,7 @@ yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val); yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val); /** Returns the content if the value is bool. - Returns NULL if `val` is NULL or type is not bool. */ + Returns false if `val` is NULL or type is not bool. */ yyjson_api_inline bool yyjson_get_bool(yyjson_val *val); /** Returns the content and cast to uint64_t. @@ -1888,7 +1946,7 @@ 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. @@ -1914,7 +1972,7 @@ yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr); /*============================================================================== - * JSON Array Iterator API + * MARK: - JSON Array Iterator API *============================================================================*/ /** @@ -1996,7 +2054,7 @@ 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. @@ -2026,7 +2084,7 @@ yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, const char *key, /*============================================================================== - * JSON Object Iterator API + * MARK: - JSON Object Iterator API *============================================================================*/ /** @@ -2169,7 +2227,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. @@ -2273,7 +2331,7 @@ yyjson_api yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *val, /*============================================================================== - * Mutable JSON Value Type API + * MARK: - Mutable JSON Value Type API *============================================================================*/ /** Returns whether the JSON value is raw. @@ -2335,7 +2393,7 @@ yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val); /*============================================================================== - * Mutable JSON Value Content API + * MARK: - Mutable JSON Value Content API *============================================================================*/ /** Returns the JSON value's type. @@ -2504,7 +2562,7 @@ 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. @@ -2605,7 +2663,7 @@ 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. @@ -2629,7 +2687,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last(yyjson_mut_val *arr); /*============================================================================== - * Mutable JSON Array Iterator API + * MARK: - Mutable JSON Array Iterator API *============================================================================*/ /** @@ -2731,7 +2789,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( /*============================================================================== - * Mutable JSON Array Creation API + * MARK: - Mutable JSON Array Creation API *============================================================================*/ /** @@ -3092,7 +3150,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( /*============================================================================== - * Mutable JSON Array Modification API + * MARK: - Mutable JSON Array Modification API *============================================================================*/ /** @@ -3205,7 +3263,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 *============================================================================*/ /** @@ -3411,7 +3469,7 @@ 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. @@ -3443,7 +3501,7 @@ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, /*============================================================================== - * Mutable JSON Object Iterator API + * MARK: - Mutable JSON Object Iterator API *============================================================================*/ /** @@ -3605,7 +3663,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. */ @@ -3652,7 +3710,7 @@ 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 *============================================================================*/ /** @@ -3762,7 +3820,7 @@ 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. @@ -4002,7 +4060,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 *============================================================================*/ @@ -4587,7 +4645,7 @@ 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 *============================================================================*/ @@ -4655,7 +4713,7 @@ yyjson_api yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, /*============================================================================== - * JSON Merge-Patch API (RFC 7386) + * MARK: - JSON Merge-Patch API (RFC 7386) * https://tools.ietf.org/html/rfc7386 *============================================================================*/ @@ -4688,7 +4746,7 @@ yyjson_api yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, /*============================================================================== - * JSON Structure (Implementation) + * MARK: - JSON Structure (Implementation) *============================================================================*/ /** Payload of a JSON value (8 bytes). */ @@ -4725,7 +4783,7 @@ struct yyjson_doc { /*============================================================================== - * Unsafe JSON Value API (Implementation) + * MARK: - Unsafe JSON Value API (Implementation) *============================================================================*/ /* @@ -5054,7 +5112,7 @@ 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) { @@ -5081,7 +5139,7 @@ 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) { @@ -5143,7 +5201,7 @@ yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val) { /*============================================================================== - * JSON Value Content API (Implementation) + * MARK: - JSON Value Content API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val) { @@ -5326,7 +5384,7 @@ 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) { @@ -5376,7 +5434,7 @@ 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, @@ -5415,7 +5473,7 @@ 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) { @@ -5444,7 +5502,7 @@ 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, @@ -5521,7 +5579,7 @@ yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, /*============================================================================== - * Mutable JSON Structure (Implementation) + * MARK: - Mutable JSON Structure (Implementation) *============================================================================*/ /** @@ -5637,7 +5695,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) { @@ -5652,7 +5710,7 @@ 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) { @@ -5714,7 +5772,7 @@ yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val) { /*============================================================================== - * 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) { @@ -5893,7 +5951,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) \ @@ -6034,7 +6092,7 @@ 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) { @@ -6070,7 +6128,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, @@ -6130,7 +6188,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) { @@ -6310,7 +6368,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, @@ -6520,7 +6578,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, @@ -6686,7 +6744,7 @@ 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) { @@ -6715,7 +6773,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, @@ -6806,7 +6864,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) { @@ -6886,7 +6944,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, @@ -7079,7 +7137,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) \ @@ -7306,7 +7364,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 { \ @@ -7965,7 +8023,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) *============================================================================*/ /** @@ -8068,7 +8126,7 @@ yyjson_api_inline bool yyjson_ptr_get_str( /*============================================================================== - * Deprecated + * MARK: - Deprecated *============================================================================*/ /** @deprecated renamed to `yyjson_doc_ptr_get` */ @@ -8152,7 +8210,7 @@ yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_get_pointer( /*============================================================================== - * Compiler Hint End + * MARK: - Compiler Hint End *============================================================================*/ #if defined(__clang__) diff --git a/src/common/duration.c b/src/common/duration.c index 16e5570e9b..c9dd6fbd85 100644 --- a/src/common/duration.c +++ b/src/common/duration.c @@ -4,11 +4,13 @@ void ffDurationAppendNum(uint64_t totalSeconds, FFstrbuf* result) { const FFOptionsDisplay* options = &instance.config.display; - const char* space = instance.config.display.durationSpaceBeforeUnit != FF_SPACE_BEFORE_UNIT_NEVER ? " " : ""; + bool spaceBeforeUnit = options->durationSpaceBeforeUnit != FF_SPACE_BEFORE_UNIT_NEVER; - if(totalSeconds < 60) + if (totalSeconds < 60) { - ffStrbufAppendF(result, options->durationAbbreviation ? "%u%ssec" : "%u%ssecond", (unsigned) totalSeconds, space); + ffStrbufAppendUInt(result, totalSeconds); + if (spaceBeforeUnit) ffStrbufAppendC(result, ' '); + ffStrbufAppendS(result, options->durationAbbreviation ? "sec" : "second"); if (totalSeconds != 1) ffStrbufAppendC(result, 's'); return; @@ -25,63 +27,56 @@ void ffDurationAppendNum(uint64_t totalSeconds, FFstrbuf* result) totalSeconds /= 24; uint32_t days = (uint32_t) totalSeconds; - if(days > 0) + if (days > 0) { - if(options->durationAbbreviation) + ffStrbufAppendUInt(result, days); + if (spaceBeforeUnit) ffStrbufAppendC(result, ' '); + if (options->durationAbbreviation) { - ffStrbufAppendF(result, "%u%sd", days, space); + ffStrbufAppendC(result, 'd'); - if(hours > 0 || minutes > 0) + if (hours > 0 || minutes > 0) ffStrbufAppendC(result, ' '); } else { - ffStrbufAppendF(result, "%u%sday", days, space); + ffStrbufAppendS(result, days == 1 ? "day" : "days"); - if(days > 1) - ffStrbufAppendC(result, 's'); - - if(days >= 100) + if (days >= 100) ffStrbufAppendS(result, "(!)"); - if(hours > 0 || minutes > 0) + if (hours > 0 || minutes > 0) ffStrbufAppendS(result, ", "); } } - if(hours > 0) + if (hours > 0) { - if(options->durationAbbreviation) + ffStrbufAppendUInt(result, hours); + if (spaceBeforeUnit) ffStrbufAppendC(result, ' '); + if (options->durationAbbreviation) { - ffStrbufAppendF(result, "%u%sh", hours, space); + ffStrbufAppendC(result, 'h'); if (minutes > 0) ffStrbufAppendC(result, ' '); } else { - ffStrbufAppendF(result, "%u%shour", hours, space); - - if(hours > 1) - ffStrbufAppendC(result, 's'); + ffStrbufAppendS(result, hours == 1 ? "hour" : "hours"); - if(minutes > 0) + if (minutes > 0) ffStrbufAppendS(result, ", "); } } - if(minutes > 0) + if (minutes > 0) { - if(options->durationAbbreviation) - { - ffStrbufAppendF(result, "%u%sm", minutes, space); - } + ffStrbufAppendUInt(result, minutes); + if (spaceBeforeUnit) ffStrbufAppendC(result, ' '); + if (options->durationAbbreviation) + ffStrbufAppendC(result, 'm'); else - { - ffStrbufAppendF(result, "%u%smin", minutes, space); - - if(minutes > 1) - ffStrbufAppendC(result, 's'); - } + ffStrbufAppendS(result, minutes == 1 ? "min" : "mins"); } } diff --git a/src/common/format.c b/src/common/format.c index d2ec98178f..f6cbcab8b1 100644 --- a/src/common/format.c +++ b/src/common/format.c @@ -11,37 +11,31 @@ void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg) switch(formatarg->type) { case FF_FORMAT_ARG_TYPE_INT: - ffStrbufAppendF(buffer, "%" PRIi32, *(int32_t*)formatarg->value); + ffStrbufAppendSInt(buffer, *(int32_t*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_UINT: - ffStrbufAppendF(buffer, "%" PRIu32, *(uint32_t*)formatarg->value); + ffStrbufAppendUInt(buffer, *(uint32_t*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_UINT64: - ffStrbufAppendF(buffer, "%" PRIu64, *(uint64_t*)formatarg->value); + ffStrbufAppendUInt(buffer, *(uint64_t*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_UINT16: - ffStrbufAppendF(buffer, "%" PRIu16, *(uint16_t*)formatarg->value); + ffStrbufAppendUInt(buffer, *(uint16_t*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_UINT8: - ffStrbufAppendF(buffer, "%" PRIu8, *(uint8_t*)formatarg->value); + ffStrbufAppendUInt(buffer, *(uint8_t*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_STRING: ffStrbufAppendS(buffer, (const char*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_STRBUF: - ffStrbufAppend(buffer, (FFstrbuf*)formatarg->value); + ffStrbufAppend(buffer, (const FFstrbuf*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_FLOAT: - if (instance.config.display.fractionNdigits >= 0) - ffStrbufAppendF(buffer, "%.*f", instance.config.display.fractionNdigits, *(float*)formatarg->value); - else - ffStrbufAppendF(buffer, "%g", *(float*)formatarg->value); + ffStrbufAppendDouble(buffer, *(float*)formatarg->value, instance.config.display.fractionNdigits); break; case FF_FORMAT_ARG_TYPE_DOUBLE: - if (instance.config.display.fractionNdigits >= 0) - ffStrbufAppendF(buffer, "%.*f", instance.config.display.fractionNdigits, *(double*)formatarg->value); - else - ffStrbufAppendF(buffer, "%g", *(double*)formatarg->value); + ffStrbufAppendDouble(buffer, *(double*)formatarg->value, instance.config.display.fractionNdigits); break; case FF_FORMAT_ARG_TYPE_BOOL: ffStrbufAppendS(buffer, *(bool*)formatarg->value ? "true" : "false"); diff --git a/src/common/frequency.c b/src/common/frequency.c index 248bb06387..ccb3b707f5 100644 --- a/src/common/frequency.c +++ b/src/common/frequency.c @@ -6,12 +6,20 @@ bool ffFreqAppendNum(uint32_t mhz, FFstrbuf* result) return false; const FFOptionsDisplay* options = &instance.config.display; - const char* space = options->freqSpaceBeforeUnit == FF_SPACE_BEFORE_UNIT_NEVER ? "" : " "; + bool spaceBeforeUnit = options->freqSpaceBeforeUnit != FF_SPACE_BEFORE_UNIT_NEVER; int8_t ndigits = options->freqNdigits; if (ndigits >= 0) - ffStrbufAppendF(result, "%.*f%sGHz", ndigits, mhz / 1000., space); + { + ffStrbufAppendDouble(result, mhz / 1000., ndigits); + if (spaceBeforeUnit) ffStrbufAppendC(result, ' '); + ffStrbufAppendS(result, "GHz"); + } else - ffStrbufAppendF(result, "%u%sMHz", (unsigned) mhz, space); + { + ffStrbufAppendUInt(result, mhz); + if (spaceBeforeUnit) ffStrbufAppendC(result, ' '); + ffStrbufAppendS(result, "MHz"); + } return true; } diff --git a/src/common/jsonconfig.c b/src/common/jsonconfig.c index 838b307acf..7cd73d9a4a 100644 --- a/src/common/jsonconfig.c +++ b/src/common/jsonconfig.c @@ -101,16 +101,6 @@ const char* ffJsonConfigParseEnum(yyjson_val* val, int* result, FFKeyValuePair p return "Invalid enum value type; must be a string or integer"; } -static inline void genJsonResult(FFModuleBaseInfo* baseInfo, void* options, yyjson_mut_doc* doc) -{ - yyjson_mut_val* module = yyjson_mut_arr_add_obj(doc, doc->root); - yyjson_mut_obj_add_str(doc, module, "type", baseInfo->name); - if (baseInfo->generateJsonResult) - baseInfo->generateJsonResult(options, doc, module); - else - yyjson_mut_obj_add_str(doc, module, "error", "Unsupported for JSON format"); -} - static bool parseModuleJsonObject(const char* type, yyjson_val* jsonVal, yyjson_mut_doc* jsonDoc) { if(!ffCharIsEnglishAlphabet(type[0])) return false; @@ -123,14 +113,39 @@ static bool parseModuleJsonObject(const char* type, yyjson_val* jsonVal, yyjson_ uint8_t optionBuf[FF_OPTION_MAX_SIZE]; baseInfo->initOptions(optionBuf); if (jsonVal) baseInfo->parseJsonObject(optionBuf, jsonVal); - if (__builtin_expect(jsonDoc != NULL, false)) - genJsonResult(baseInfo, optionBuf, jsonDoc); + bool succeeded; + if (jsonDoc) + { + yyjson_mut_val* module = yyjson_mut_arr_add_obj(jsonDoc, jsonDoc->root); + yyjson_mut_obj_add_str(jsonDoc, module, "type", baseInfo->name); + if (baseInfo->generateJsonResult) + succeeded = baseInfo->generateJsonResult(optionBuf, jsonDoc, module); + else + { + yyjson_mut_obj_add_str(jsonDoc, module, "error", "Unsupported for JSON format"); + succeeded = false; + } + } else - baseInfo->printModule(optionBuf); + succeeded = baseInfo->printModule(optionBuf); baseInfo->destroyOptions(optionBuf); - return true; + return succeeded; } } + + if (jsonDoc) + { + yyjson_mut_val* module = yyjson_mut_arr_add_obj(jsonDoc, jsonDoc->root); + yyjson_mut_obj_add_strcpy(jsonDoc, module, "type", type); + yyjson_mut_obj_add_str(jsonDoc, module, "error", "Unknown module type"); + } + else + { + FFModuleArgs moduleArgs; + ffOptionInitModuleArg(&moduleArgs, ""); + ffPrintError(type, 0, &moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown module type"); + ffOptionDestroyModuleArg(&moduleArgs); + } return false; } @@ -217,6 +232,7 @@ static const char* printJsonConfig(bool prepare, yyjson_mut_doc* jsonDoc) if (!modules) return NULL; if (!yyjson_is_arr(modules)) return "Property 'modules' must be an array of strings or objects"; + bool succeeded = true; int32_t thres = instance.config.display.stat; yyjson_val* item; size_t idx, max; @@ -253,6 +269,15 @@ static const char* printJsonConfig(bool prepare, yyjson_mut_doc* jsonDoc) arch = yyjson_obj_get(conditions, "!arch"); if (arch && matchesJsonArray(ffVersionResult.architecture, arch)) continue; + + yyjson_val* previousSucceeded = yyjson_obj_get(conditions, "succeeded"); + if (previousSucceeded && !unsafe_yyjson_is_null(previousSucceeded)) + { + if (!unsafe_yyjson_is_bool(previousSucceeded)) + return "Property 'succeeded' in 'condition' must be a boolean"; + if (succeeded != unsafe_yyjson_get_bool(previousSucceeded)) + continue; + } } type = yyjson_get_str(yyjson_obj_get(module, "type")); @@ -265,8 +290,8 @@ static const char* printJsonConfig(bool prepare, yyjson_mut_doc* jsonDoc) if(prepare) prepareModuleJsonObject(type, module); - else if(!parseModuleJsonObject(type, module, jsonDoc)) - return "Unknown module type"; + else + succeeded = parseModuleJsonObject(type, module, jsonDoc); if(!prepare && thres >= 0) { diff --git a/src/common/library.c b/src/common/library.c index 27bac3b820..8a47a1039a 100644 --- a/src/common/library.c +++ b/src/common/library.c @@ -52,7 +52,7 @@ static void* libraryLoad(const char* path, int maxVersion) for(int i = maxVersion; i >= 0; --i) { uint32_t originalLength = pathbuf.length; - ffStrbufAppendF(&pathbuf, "%i", i); + ffStrbufAppendSInt(&pathbuf, i); result = dlopen(pathbuf.chars, FF_DLOPEN_FLAGS); if(result != NULL) diff --git a/src/common/netif/netif_gnu.c b/src/common/netif/netif_gnu.c new file mode 100644 index 0000000000..b829af9cd7 --- /dev/null +++ b/src/common/netif/netif_gnu.c @@ -0,0 +1,35 @@ +#include "netif.h" +#include "common/io/io.h" + +#include +#include + +#define FF_STR_INDIR(x) #x +#define FF_STR(x) FF_STR_INDIR(x) + +bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) +{ + FILE* FF_AUTO_CLOSE_FILE netRoute = fopen("/proc/route", "r"); + + if (!netRoute) return false; + + // skip first line + FF_UNUSED(fscanf(netRoute, "%*[^\n]\n")); + unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu, ... + while (fscanf(netRoute, "%" FF_STR(IF_NAMESIZE) "s%llx%*[^\n]", result->ifName, &destination) == 2) + { + if (destination != 0) continue; + result->ifIndex = if_nametoindex(result->ifName); + // TODO: Get the preferred source address + return true; + } + result->ifName[0] = '0'; + return false; +} + +bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) +{ + // TODO: AF_INET6 + FF_UNUSED(result); + return false; +} diff --git a/src/common/netif/netif_linux.c b/src/common/netif/netif_linux.c index 1c31000034..c54cc32420 100644 --- a/src/common/netif/netif_linux.c +++ b/src/common/netif/netif_linux.c @@ -88,111 +88,125 @@ bool ffNetifGetDefaultRouteImplV4(FFNetifDefaultRouteResult* result) socklen_t src_addr_len = sizeof(src_addr); uint8_t buffer[1024 * 16]; // 16 KB buffer should be sufficient - - ssize_t received = recvfrom(sock_fd, buffer, sizeof(buffer), 0, - (struct sockaddr*)&src_addr, &src_addr_len); - - if (received < 0) { - FF_DEBUG("Failed to receive netlink response: %s", strerror(errno)); - return false; - } - - if (received >= (ssize_t)sizeof(buffer)) { - FF_DEBUG("Failed to receive complete message (possible truncation)"); - return false; - } - FF_DEBUG("Received netlink response: %zd bytes", received); - - struct { - uint32_t metric; - uint32_t ifindex; - uint32_t prefsrc; - } entry; uint32_t minMetric = UINT32_MAX; int routeCount = 0; - for (const struct nlmsghdr* nlh = (struct nlmsghdr*)buffer; - NLMSG_OK(nlh, received); - nlh = NLMSG_NEXT(nlh, received)) { - if (nlh->nlmsg_seq != 1 || nlh->nlmsg_pid != pid) - continue; - if (nlh->nlmsg_type == NLMSG_DONE) - { - FF_DEBUG("Received NLMSG_DONE, processed %d routes", routeCount); - break; + while (true) + { + ssize_t received = recvfrom(sock_fd, buffer, sizeof(buffer), 0, + (struct sockaddr*)&src_addr, &src_addr_len); + + if (received < 0) { + FF_DEBUG("Failed to receive netlink response: %s", strerror(errno)); + return false; } - if (nlh->nlmsg_type == NLMSG_ERROR) { - struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(nlh); - FF_DEBUG("Netlink reports error: %s", strerror(-err->error)); - continue; + if (received >= (ssize_t)sizeof(buffer)) { + FF_DEBUG("Received truncated message: received %zd, bufsize %zu", received, sizeof(buffer)); + return false; + } + FF_DEBUG("Received netlink response: %zd bytes", received); + if (received == 0) { + FF_DEBUG("Received zero-length netlink response, ending processing"); + break; } - if (nlh->nlmsg_type != RTM_NEWROUTE) - continue; + struct { + uint32_t metric; + uint32_t ifindex; + uint32_t prefsrc; + } entry; - routeCount++; - struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(nlh); - if (rtm->rtm_family != AF_INET) - continue; + for (const struct nlmsghdr* nlh = (struct nlmsghdr*)buffer; + NLMSG_OK(nlh, received); + nlh = NLMSG_NEXT(nlh, received)) + { + if (nlh->nlmsg_seq != 1 || nlh->nlmsg_pid != pid) + continue; + if (nlh->nlmsg_type == NLMSG_DONE) + { + FF_DEBUG("Received NLMSG_DONE, processed %d routes", routeCount); + goto exit; + } - if (rtm->rtm_dst_len != 0) - continue; + if (nlh->nlmsg_type == NLMSG_ERROR) { + FF_DEBUG("Netlink reports error: %s", strerror(-((struct nlmsgerr*)NLMSG_DATA(nlh))->error)); + continue; + } - FF_DEBUG("Processing IPv4 default route candidate #%d", routeCount); - entry = (__typeof__(entry)) { }; // Default to zero metric (no RTA_PRIORITY found) + if (nlh->nlmsg_type != RTM_NEWROUTE) { + FF_DEBUG("Skipping non-route message: type=%d", nlh->nlmsg_type); + continue; + } - // Parse route attributes - size_t rtm_len = RTM_PAYLOAD(nlh); - for (struct rtattr* rta = RTM_RTA(rtm); - RTA_OK(rta, rtm_len); - rta = RTA_NEXT(rta, rtm_len)) - { - if (RTA_PAYLOAD(rta) < sizeof(uint32_t)) - continue; // Skip invalid attributes - - uint32_t rta_data = *(uint32_t*) RTA_DATA(rta); - switch (rta->rta_type) { - case RTA_DST: - FF_DEBUG("Unexpected RTA_DST: %s (len=%u)", inet_ntoa((struct in_addr) { .s_addr = rta_data }), rtm->rtm_dst_len); - goto next; - case RTA_OIF: - entry.ifindex = rta_data; - FF_DEBUG("Found interface index: %u", entry.ifindex); - break; - case RTA_GATEWAY: - FF_DEBUG("Found gateway: %s", inet_ntoa(*(struct in_addr*)&rta_data)); - if (rta_data == 0) goto next; - break; - case RTA_PRIORITY: - FF_DEBUG("Found metric: %u", rta_data); - if (rta_data >= minMetric) goto next; - entry.metric = rta_data; - break; - case RTA_PREFSRC: - entry.prefsrc = rta_data; - FF_DEBUG("Found preferred source: %s", inet_ntoa(*(struct in_addr*)&rta_data)); - break; + routeCount++; + struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(nlh); + if (rtm->rtm_family != AF_INET) { + FF_DEBUG("Skipping non-IPv4 route #%d (family=%d)", routeCount, rtm->rtm_family); + continue; } - } - if (entry.ifindex == 0 || entry.metric >= minMetric) - { - next: - FF_DEBUG("Skipping route: ifindex=%u, metric=%u", entry.ifindex, entry.metric); - continue; - } - minMetric = entry.metric; - result->ifIndex = entry.ifindex; - FF_DEBUG("Updated best route: ifindex=%u, metric=%u, prefsrc=%x", entry.ifindex, entry.metric, entry.prefsrc); - result->preferredSourceAddrV4 = entry.prefsrc; - if (minMetric == 0) - { - FF_DEBUG("Found zero metric route, stopping further processing"); - break; // Stop processing if we found a zero metric route + if (rtm->rtm_dst_len != 0) { + FF_DEBUG("Skipping non-default route #%d (dst_len=%d)", routeCount, rtm->rtm_dst_len); + continue; + } + + FF_DEBUG("Processing IPv4 default route candidate #%d", routeCount); + entry = (__typeof__(entry)) { }; // Default to zero metric (no RTA_PRIORITY found) + + // Parse route attributes + size_t rtm_len = RTM_PAYLOAD(nlh); + for (struct rtattr* rta = RTM_RTA(rtm); + RTA_OK(rta, rtm_len); + rta = RTA_NEXT(rta, rtm_len)) + { + if (RTA_PAYLOAD(rta) < sizeof(uint32_t)) + continue; // Skip invalid attributes + + uint32_t rta_data = *(uint32_t*) RTA_DATA(rta); + switch (rta->rta_type) { + case RTA_DST: + FF_DEBUG("Unexpected RTA_DST: %s (len=%u)", inet_ntoa((struct in_addr) { .s_addr = rta_data }), rtm->rtm_dst_len); + goto next; + case RTA_OIF: + entry.ifindex = rta_data; + FF_DEBUG("Found interface index: %u", entry.ifindex); + break; + case RTA_GATEWAY: + FF_DEBUG("Found gateway: %s", inet_ntoa(*(struct in_addr*)&rta_data)); + if (rta_data == 0) goto next; + break; + case RTA_PRIORITY: + FF_DEBUG("Found metric: %u", rta_data); + if (rta_data >= minMetric) goto next; + entry.metric = rta_data; + break; + case RTA_PREFSRC: + entry.prefsrc = rta_data; + FF_DEBUG("Found preferred source: %s", inet_ntoa(*(struct in_addr*)&rta_data)); + break; + } + } + + if (entry.ifindex == 0 || entry.metric >= minMetric) + { + next: + FF_DEBUG("Skipping route: ifindex=%u, metric=%u", entry.ifindex, entry.metric); + continue; + } + minMetric = entry.metric; + result->ifIndex = entry.ifindex; + FF_DEBUG("Updated best route: ifindex=%u, metric=%u, prefsrc=%x", entry.ifindex, entry.metric, entry.prefsrc); + result->preferredSourceAddrV4 = entry.prefsrc; + if (minMetric == 0) + { + FF_DEBUG("Found zero metric route, stopping further processing"); + break; // Stop processing if we found a zero metric route + } } } +exit: if (minMetric < UINT32_MAX) { if_indextoname(result->ifIndex, result->ifName); @@ -284,114 +298,129 @@ bool ffNetifGetDefaultRouteImplV6(FFNetifDefaultRouteResult* result) socklen_t src_addr_len = sizeof(src_addr); uint8_t buffer[1024 * 16]; // 16 KB buffer should be sufficient - - ssize_t received = recvfrom(sock_fd, buffer, sizeof(buffer), 0, - (struct sockaddr*)&src_addr, &src_addr_len); - - if (received < 0) { - FF_DEBUG("Failed to receive netlink response: %s", strerror(errno)); - return false; - } - - if (received >= (ssize_t)sizeof(buffer)) { - FF_DEBUG("Failed to receive complete message (possible truncation)"); - return false; - } - - struct { - uint32_t metric; - uint32_t ifindex; - } entry; uint32_t minMetric = UINT32_MAX; int routeCount = 0; - for (const struct nlmsghdr* nlh = (struct nlmsghdr*)buffer; - NLMSG_OK(nlh, received); - nlh = NLMSG_NEXT(nlh, received)) { - if (nlh->nlmsg_seq != 1 || nlh->nlmsg_pid != pid) - continue; - if (nlh->nlmsg_type == NLMSG_DONE) - { - FF_DEBUG("Received NLMSG_DONE, processed %d routes", routeCount); - break; + while (true) + { + ssize_t received = recvfrom(sock_fd, buffer, sizeof(buffer), 0, + (struct sockaddr*)&src_addr, &src_addr_len); + + if (received < 0) { + FF_DEBUG("Failed to receive netlink response: %s", strerror(errno)); + return false; } - if (nlh->nlmsg_type == NLMSG_ERROR) { - struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(nlh); - FF_DEBUG("Netlink reports error: %s", strerror(-err->error)); - continue; + if (received >= (ssize_t)sizeof(buffer)) { + FF_DEBUG("Received truncated message: received %zd, bufsize %zu", received, sizeof(buffer)); + return false; } + FF_DEBUG("Received netlink response: %zd bytes", received); + if (received == 0) { + FF_DEBUG("Received zero-length netlink response, ending processing"); + break; + } + + struct { + uint32_t metric; + uint32_t ifindex; + } entry; - if (nlh->nlmsg_type != RTM_NEWROUTE) - continue; + for (const struct nlmsghdr* nlh = (struct nlmsghdr*)buffer; + NLMSG_OK(nlh, received); + nlh = NLMSG_NEXT(nlh, received)) + { + if (nlh->nlmsg_seq != 1 || nlh->nlmsg_pid != pid) + continue; + if (nlh->nlmsg_type == NLMSG_DONE) + { + FF_DEBUG("Received NLMSG_DONE, processed %d routes", routeCount); + goto exit; + } - routeCount++; - struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(nlh); - if (rtm->rtm_family != AF_INET6) - continue; + if (nlh->nlmsg_type == NLMSG_ERROR) { + FF_DEBUG("Netlink reports error: %s", strerror(-((struct nlmsgerr*)NLMSG_DATA(nlh))->error)); + continue; + } - if (rtm->rtm_dst_len != 0) - continue; + if (nlh->nlmsg_type != RTM_NEWROUTE) { + FF_DEBUG("Skipping non-route message: type=%d", nlh->nlmsg_type); + continue; + } - FF_DEBUG("Processing IPv6 default route candidate #%d", routeCount); - entry = (__typeof__(entry)) { }; // Default to zero metric (no RTA_PRIORITY found) + routeCount++; + struct rtmsg* rtm = (struct rtmsg*)NLMSG_DATA(nlh); + if (rtm->rtm_family != AF_INET6) { + FF_DEBUG("Skipping non-IPv6 route #%d (family=%d)", routeCount, rtm->rtm_family); + continue; + } - // Parse route attributes - size_t rtm_len = RTM_PAYLOAD(nlh); - for (struct rtattr* rta = RTM_RTA(rtm); - RTA_OK(rta, rtm_len); - rta = RTA_NEXT(rta, rtm_len)) - { - switch (rta->rta_type) { - case RTA_DST: - if (RTA_PAYLOAD(rta) >= sizeof(struct in6_addr)) { - FF_MAYBE_UNUSED char str[INET6_ADDRSTRLEN]; - FF_DEBUG("Unexpected RTA_DST: %s", inet_ntop(AF_INET6, RTA_DATA(rta), str, sizeof(str))); - goto next; - } - break; - case RTA_OIF: - if (RTA_PAYLOAD(rta) >= sizeof(uint32_t)) { - entry.ifindex = *(uint32_t*) RTA_DATA(rta); - FF_DEBUG("Found interface index: %u", entry.ifindex); - } - break; - case RTA_GATEWAY: - if (RTA_PAYLOAD(rta) >= sizeof(struct in6_addr)) { - struct in6_addr* gw = (struct in6_addr*) RTA_DATA(rta); - if (IN6_IS_ADDR_UNSPECIFIED(gw)) goto next; - FF_MAYBE_UNUSED char str[INET6_ADDRSTRLEN]; - FF_DEBUG("Found gateway: %s", inet_ntop(AF_INET6, gw, str, sizeof(str))); - } - break; - case RTA_PRIORITY: - if (RTA_PAYLOAD(rta) >= sizeof(uint32_t)) { - uint32_t metric = *(uint32_t*) RTA_DATA(rta); - FF_DEBUG("Found metric: %u", metric); - if (metric >= minMetric) goto next; - entry.metric = metric; - } - break; + if (rtm->rtm_dst_len != 0) { + FF_DEBUG("Skipping non-default route #%d (dst_len=%d)", routeCount, rtm->rtm_dst_len); + continue; } - } - if (entry.ifindex == 0 || entry.metric >= minMetric) - { - next: - FF_DEBUG("Skipping route: ifindex=%u, metric=%u", entry.ifindex, entry.metric); - continue; - } - minMetric = entry.metric; - result->ifIndex = entry.ifindex; - FF_DEBUG("Updated best route: ifindex=%u, metric=%u", entry.ifindex, entry.metric); + FF_DEBUG("Processing IPv6 default route candidate #%d", routeCount); + entry = (__typeof__(entry)) { }; // Default to zero metric (no RTA_PRIORITY found) + + // Parse route attributes + size_t rtm_len = RTM_PAYLOAD(nlh); + for (struct rtattr* rta = RTM_RTA(rtm); + RTA_OK(rta, rtm_len); + rta = RTA_NEXT(rta, rtm_len)) + { + switch (rta->rta_type) { + case RTA_DST: + if (RTA_PAYLOAD(rta) >= sizeof(struct in6_addr)) { + FF_MAYBE_UNUSED char str[INET6_ADDRSTRLEN]; + FF_DEBUG("Unexpected RTA_DST: %s", inet_ntop(AF_INET6, RTA_DATA(rta), str, sizeof(str))); + goto next; + } + break; + case RTA_OIF: + if (RTA_PAYLOAD(rta) >= sizeof(uint32_t)) { + entry.ifindex = *(uint32_t*) RTA_DATA(rta); + FF_DEBUG("Found interface index: %u", entry.ifindex); + } + break; + case RTA_GATEWAY: + if (RTA_PAYLOAD(rta) >= sizeof(struct in6_addr)) { + struct in6_addr* gw = (struct in6_addr*) RTA_DATA(rta); + if (IN6_IS_ADDR_UNSPECIFIED(gw)) goto next; + FF_MAYBE_UNUSED char str[INET6_ADDRSTRLEN]; + FF_DEBUG("Found gateway: %s", inet_ntop(AF_INET6, gw, str, sizeof(str))); + } + break; + case RTA_PRIORITY: + if (RTA_PAYLOAD(rta) >= sizeof(uint32_t)) { + uint32_t metric = *(uint32_t*) RTA_DATA(rta); + FF_DEBUG("Found metric: %u", metric); + if (metric >= minMetric) goto next; + entry.metric = metric; + } + break; + } + } - if (minMetric == 0) - { - FF_DEBUG("Found zero metric route, stopping further processing"); - break; // Stop processing if we found a zero metric route + if (entry.ifindex == 0 || entry.metric >= minMetric) + { + next: + FF_DEBUG("Skipping route: ifindex=%u, metric=%u", entry.ifindex, entry.metric); + continue; + } + minMetric = entry.metric; + result->ifIndex = entry.ifindex; + FF_DEBUG("Updated best route: ifindex=%u, metric=%u", entry.ifindex, entry.metric); + + if (minMetric == 0) + { + FF_DEBUG("Found zero metric route, stopping further processing"); + break; // Stop processing if we found a zero metric route + } } } +exit: if (minMetric < UINT32_MAX) { if_indextoname(result->ifIndex, result->ifName); diff --git a/src/common/networking/networking_linux.c b/src/common/networking/networking_linux.c index 074d4f528d..caf19accc4 100644 --- a/src/common/networking/networking_linux.c +++ b/src/common/networking/networking_linux.c @@ -22,7 +22,7 @@ static const char* tryNonThreadingFastPath(FFNetworkingState* state) if (!state->tfo) { - #ifdef __linux__ + #if __linux__ || __GNU__ // Linux doesn't support sendto() on unconnected sockets FF_DEBUG("TCP Fast Open disabled, skipping"); return "TCP Fast Open disabled"; @@ -34,7 +34,7 @@ static const char* tryNonThreadingFastPath(FFNetworkingState* state) #ifndef __APPLE__ // On macOS, TCP_FASTOPEN doesn't seem to be needed // Set TCP Fast Open - #ifdef __linux__ + #if __linux__ || __GNU__ int flag = 5; // the queue length of pending packets #else int flag = 1; // enable TCP Fast Open @@ -50,7 +50,7 @@ static const char* tryNonThreadingFastPath(FFNetworkingState* state) FF_DEBUG("Failed to set TCP_FASTOPEN option: %s", strerror(errno)); return "setsockopt(TCP_FASTOPEN) failed"; } else { - #ifdef __linux__ + #if __linux__ || __GNU__ FF_DEBUG("Successfully set TCP_FASTOPEN option, queue length: %d", flag); #elif defined(__APPLE__) FF_DEBUG("Successfully set TCP_FASTOPEN_FORCE_ENABLE option"); diff --git a/src/common/option.h b/src/common/option.h index a47b9b5041..4b8ea3eabf 100644 --- a/src/common/option.h +++ b/src/common/option.h @@ -29,11 +29,12 @@ typedef struct FFModuleBaseInfo // This is UB, because `void*` is not compatible with `FF*Options*`. // However we can't do it better unless we move to C++, so that `option` becomes a `this` pointer // https://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type + void (*initOptions)(void* options); void (*destroyOptions)(void* options); void (*parseJsonObject)(void* options, struct yyjson_val *module); - void (*printModule)(void* options); - void (*generateJsonResult)(void* options, struct yyjson_mut_doc* doc, struct yyjson_mut_val* module); + bool (*printModule)(void* options); // true on success + bool (*generateJsonResult)(void* options, struct yyjson_mut_doc* doc, struct yyjson_mut_val* module); // true on success void (*generateJsonConfig)(void* options, struct yyjson_mut_doc* doc, struct yyjson_mut_val* obj); FFModuleFormatArgList formatArgs; } FFModuleBaseInfo; diff --git a/src/common/parsing.c b/src/common/parsing.c index 00de9a8d50..668a28773b 100644 --- a/src/common/parsing.c +++ b/src/common/parsing.c @@ -50,13 +50,21 @@ int8_t ffVersionCompare(const FFVersion* version1, const FFVersion* version2) void ffVersionToPretty(const FFVersion* version, FFstrbuf* pretty) { if(version->major > 0 || version->minor > 0 || version->patch > 0) - ffStrbufAppendF(pretty, "%u", version->major); + { + ffStrbufAppendUInt(pretty, version->major); + } if(version->minor > 0 || version->patch > 0) - ffStrbufAppendF(pretty, ".%u", version->minor); + { + ffStrbufAppendC(pretty, '.'); + ffStrbufAppendUInt(pretty, version->minor); + } if(version->patch > 0) - ffStrbufAppendF(pretty, ".%u", version->patch); + { + ffStrbufAppendC(pretty, '.'); + ffStrbufAppendUInt(pretty, version->patch); + } } void ffParseGTK(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3, const FFstrbuf* gtk4) diff --git a/src/common/processing_linux.c b/src/common/processing_linux.c index 1fdfbae2a6..fc9def0dd0 100644 --- a/src/common/processing_linux.c +++ b/src/common/processing_linux.c @@ -234,15 +234,42 @@ void ffProcessGetInfoLinux(pid_t pid, FFstrbuf* processName, FFstrbuf* exe, cons assert(processName->length > 0); ffStrbufClear(exe); - #ifdef __linux__ + #if defined(__linux__) || defined(__GNU__) char filePath[64]; snprintf(filePath, sizeof(filePath), "/proc/%d/cmdline", (int)pid); if(ffReadFileBuffer(filePath, exe)) { - ffStrbufRecalculateLength(exe); //Trim the arguments - ffStrbufTrimRightSpace(exe); + const char* p = exe->chars; + uint32_t len = (uint32_t) strlen(p); + + if (len + 1 < exe->length) + { + const char* name = memrchr(p, '/', len); + if (name) name++; else name = p; + + // For interpreters, try to find the real script path in the arguments + if (ffStrStartsWith(name, "python") + #ifndef __ANDROID__ + || ffStrEquals(name, "guile") // for shepherd + #endif + ) + { + // `cmdline` always ends with a trailing '\0', and ffReadFileBuffer appends another \0 + // So `exe->chars` is always double '\0' terminated + for (p = p + len + 1; *p && *p == '-'; p += strlen(p) + 1) // Skip arguments + assert(p - exe->chars < exe->allocated); + if (*p) + { + len = (uint32_t) strlen(p); + memmove(exe->chars, p, len + 1); + } + } + } + + assert(len < exe->allocated); + exe->length = len; ffStrbufTrimLeft(exe, '-'); //Login shells start with a dash } @@ -436,33 +463,53 @@ const char* ffProcessGetBasicInfoLinux(pid_t pid, FFstrbuf* name, pid_t* ppid, i if (pid <= 0) return "Invalid pid"; - #ifdef __linux__ + #if defined(__linux__) || defined(__GNU__) char procFilePath[64]; - if (ppid) + #if __linux__ + if (ppid || tty) + #endif { snprintf(procFilePath, sizeof(procFilePath), "/proc/%d/stat", (int)pid); char buf[PROC_FILE_BUFFSIZ]; ssize_t nRead = ffReadFileData(procFilePath, sizeof(buf) - 1, buf); if(nRead <= 8) return "ffReadFileData(/proc/pid/stat, PROC_FILE_BUFFSIZ-1, buf) failed"; - buf[nRead] = '\0'; + buf[nRead] = '\0'; // pid (comm) state ppid pgrp session tty - *ppid = 0; - static_assert(sizeof(*ppid) == sizeof(int), ""); + const char* pState = NULL; - ffStrbufEnsureFixedLengthFree(name, 255); - int tty_; - if( - sscanf(buf, "%*s (%255[^)]) %*c %d %*d %*d %d", name->chars, ppid, &tty_) < 2 || //stat (comm) state ppid pgrp session tty - name->chars[0] == '\0' - ) - return "sscanf(stat) failed"; + { + // comm in `/proc/pid/stat` is not encoded, and may contain ' ', ')' or even `\n` + const char* start = memchr(buf, '(', (size_t) nRead); + if (!start) + return "memchr(stat, '(') failed"; + start++; + const char* end = memrchr(start, ')', (size_t) nRead - (size_t) (start - buf)); + if (!end) + return "memrchr(stat, ')') failed"; + ffStrbufSetNS(name, (uint32_t) (end - start), start); + ffStrbufTrimRightSpace(name); + if (name->chars[0] == '\0') + return "process name is empty"; + pState = end + 2; // skip ") " + } - ffStrbufRecalculateLength(name); - if (tty) - *tty = tty_ & 0xFF; + #if !__linux__ + if (ppid || tty) + #endif + { + int ppid_, tty_; + if(sscanf(pState + 2, "%d %*d %*d %d", &ppid_, &tty_) < 2) + return "sscanf(stat) failed"; + + if (ppid) + *ppid = (pid_t) ppid_; + if (tty) + *tty = tty_ & 0xFF; + } } + #if __linux__ else { snprintf(procFilePath, sizeof(procFilePath), "/proc/%d/comm", (int)pid); @@ -471,6 +518,7 @@ const char* ffProcessGetBasicInfoLinux(pid_t pid, FFstrbuf* name, pid_t* ppid, i return "ffReadFileBuffer(/proc/pid/comm, name) failed"; ffStrbufTrimRightSpace(name); } + #endif #elif defined(__APPLE__) diff --git a/src/common/size.c b/src/common/size.c index f32b021812..eafe46a53c 100644 --- a/src/common/size.c +++ b/src/common/size.c @@ -14,11 +14,13 @@ static void appendNum(FFstrbuf* result, uint64_t bytes, uint32_t base, const cha counter++; } - const char* space = options->sizeSpaceBeforeUnit == FF_SPACE_BEFORE_UNIT_NEVER ? "" : " "; - if(counter == 0) - ffStrbufAppendF(result, "%" PRIu64 "%s%s", bytes, space, prefixes[0]); + if (counter == 0) + ffStrbufAppendUInt(result, bytes); else - ffStrbufAppendF(result, "%.*f%s%s", options->sizeNdigits, size, space, prefixes[counter]); + ffStrbufAppendDouble(result, size, (int8_t) options->sizeNdigits); + if (options->sizeSpaceBeforeUnit != FF_SPACE_BEFORE_UNIT_NEVER) + ffStrbufAppendC(result, ' '); + ffStrbufAppendS(result, prefixes[counter]); } void ffSizeAppendNum(uint64_t bytes, FFstrbuf* result) diff --git a/src/common/time.c b/src/common/time.c new file mode 100644 index 0000000000..84eab78be2 --- /dev/null +++ b/src/common/time.c @@ -0,0 +1,3 @@ +#include + +char ffTimeInternalBuffer[64]; // Reduce memory usage and prevent redundant allocations diff --git a/src/common/time.h b/src/common/time.h index 9457dd90d5..7390f18192 100644 --- a/src/common/time.h +++ b/src/common/time.h @@ -11,6 +11,8 @@ #include #endif +#include "util/arrayUtils.h" + static inline double ffTimeGetTick(void) //In msec { #ifdef _WIN32 @@ -64,11 +66,12 @@ static inline const char* ffTimeToFullStr(uint64_t msec) time_t tsec = (time_t) (msec / 1000); const struct tm* tm = localtime(&tsec); - static char buf[32]; - strftime(buf, __builtin_strlen("0000-00-00T00:00:00") + 1, "%FT%T", tm); - snprintf(buf + __builtin_strlen("0000-00-00T00:00:00"), __builtin_strlen(".000") + 1, ".%03u", (unsigned) (msec % 1000)); - strftime(buf + __builtin_strlen("0000-00-00T00:00:00.000"), __builtin_strlen("+0000") + 1, "%z", tm); - return buf; + extern char ffTimeInternalBuffer[64]; + uint32_t len = 0; + len += (uint32_t) strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer) - len, "%FT%T", tm); + len += (uint32_t) snprintf(ffTimeInternalBuffer + len, ARRAY_SIZE(ffTimeInternalBuffer) - len, ".%03u", (unsigned) (msec % 1000)); + len += (uint32_t) strftime(ffTimeInternalBuffer + len, ARRAY_SIZE(ffTimeInternalBuffer) - len, "%z", tm); + return ffTimeInternalBuffer; } // Not thread-safe @@ -77,9 +80,9 @@ static inline const char* ffTimeToShortStr(uint64_t msec) if (msec == 0) return ""; time_t tsec = (time_t) (msec / 1000); - static char buf[32]; - strftime(buf, sizeof(buf), "%F %T", localtime(&tsec)); - return buf; + extern char ffTimeInternalBuffer[64]; + strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer), "%F %T", localtime(&tsec)); + return ffTimeInternalBuffer; } // Not thread-safe @@ -88,10 +91,10 @@ static inline const char* ffTimeToTimeStr(uint64_t msec) if (msec == 0) return ""; time_t tsec = (time_t) (msec / 1000); - static char buf[32]; - strftime(buf, sizeof(buf), "%T", localtime(&tsec)); - sprintf(buf + __builtin_strlen("00:00:00"), ".%03u", (unsigned) (msec % 1000)); - return buf; + extern char ffTimeInternalBuffer[64]; + uint32_t len = (uint32_t) strftime(ffTimeInternalBuffer, ARRAY_SIZE(ffTimeInternalBuffer), "%T", localtime(&tsec)); + sprintf(ffTimeInternalBuffer + len, ".%03u", (unsigned) (msec % 1000)); + return ffTimeInternalBuffer; } #ifdef _WIN32 diff --git a/src/detection/battery/battery_android.c b/src/detection/battery/battery_android.c index 26b8e571d7..c7f0090b3e 100644 --- a/src/detection/battery/battery_android.c +++ b/src/detection/battery/battery_android.c @@ -139,7 +139,11 @@ static const char* parseDumpsys(FFBatteryOptions* options, FFlist* results) if(options->temp) { if (ffParsePropLines(start, "temperature: ", &temp)) + { battery->temperature = ffStrbufToDouble(&temp, FF_BATTERY_TEMP_UNSET); + if (battery->temperature != FF_BATTERY_TEMP_UNSET) + battery->temperature /= 10.0; // Android returns temperature in tenths of a degree + } ffStrbufClear(&temp); } diff --git a/src/detection/cpu/cpu.c b/src/detection/cpu/cpu.c index 40ae66ac9c..5f59df7aa7 100644 --- a/src/detection/cpu/cpu.c +++ b/src/detection/cpu/cpu.c @@ -60,3 +60,490 @@ const char* ffCPUQualcommCodeToName(uint32_t code) default: return NULL; } } + +#if defined(__x86_64__) || defined(__i386__) + +#include + +void ffCPUDetectByCpuid(FFCPUResult* cpu) +{ + uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0; + if (__get_cpuid(0x16, &eax, &ebx, &ecx, &edx)) + { + // WARNING: CPUID may report frequencies of efficient cores + // cpuid returns 0 MHz when hypervisor is enabled + if (eax) cpu->frequencyBase = eax; + if (ebx) cpu->frequencyMax = ebx; + } + + if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) + { + // Feature tests (leaf1.ecx, leaf7.ebx) + bool sse2 = (ecx & bit_SSE2) != 0; + bool sse4_2 = (ecx & bit_SSE4_2) != 0; + bool pclmul = (ecx & bit_PCLMUL) != 0; + bool popcnt = (ecx & bit_POPCNT) != 0; + bool fma = (ecx & bit_FMA) != 0; + bool osxsave = (ecx & bit_OSXSAVE) != 0; + + unsigned int eax7 = 0, ebx7 = 0, ecx7 = 0, edx7 = 0; + __get_cpuid_count(7, 0, &eax7, &ebx7, &ecx7, &edx7); + + bool avx2 = (ebx7 & bit_AVX2) != 0; + bool bmi2 = (ebx7 & bit_BMI2) != 0; + bool avx512f = (ebx7 & bit_AVX512F) != 0; + bool avx512bw = (ebx7 & bit_AVX512BW) != 0; + bool avx512dq = (ebx7 & bit_AVX512DQ) != 0; + + // OS support for AVX/AVX512: check XGETBV (requires OSXSAVE) + bool avx_os = false; + bool avx512_os = false; + if (osxsave) + { + __asm__ __volatile__( + "xgetbv" + : "=a"(eax), "=d"(edx) + : "c"(0) + : + ); + uint64_t xcr0 = ((uint64_t)edx << 32) | eax; + + // AVX requires XCR0[1:2] == 11b (XMM and YMM state) + avx_os = (xcr0 & 0x6ULL) == 0x6ULL; + // AVX512 requires XCR0[7,5,6] etc. common mask 0xE6 (bits 1,2,5,6,7) + avx512_os = (xcr0 & 0xE6ULL) == 0xE6ULL; + } + + cpu->march = "unknown"; + if (avx512f && avx512bw && avx512dq && avx512_os) cpu->march = "x86_64-v4"; + else if (avx2 && fma && bmi2 && avx_os) cpu->march = "x86_64-v3"; + else if (sse4_2 && popcnt && pclmul) cpu->march = "x86_64-v2"; + else if (sse2) cpu->march = "x86_64-v1"; + } +} + +#elif defined(__aarch64__) + +// This is not accurate because a lot of flags are optional from old versions +// https://developer.arm.com/documentation/109697/2025_06/Feature-descriptions?lang=en +// https://en.wikipedia.org/wiki/AArch64#ARM-A_(application_architecture) +// Worth noting: Apple M1 is marked as ARMv8.5-A on Wikipedia, but it lacks BTI (mandatory in v8.5) + +#ifdef __linux__ +#include "common/io/io.h" +#include +#include + +#ifndef HWCAP2_SME +#define HWCAP2_SME (1UL << 23) +#endif +#ifndef HWCAP2_SME2 +#define HWCAP2_SME2 (1UL << 37) +#endif +#ifndef HWCAP2_CSSC +#define HWCAP2_CSSC (1UL << 34) +#endif +#ifndef HWCAP2_SME2P1 +#define HWCAP2_SME2P1 (1UL << 38) +#endif +#ifndef HWCAP2_MOPS +#define HWCAP2_MOPS (1UL << 43) +#endif +#ifndef HWCAP2_F8E4M3 +#define HWCAP2_F8E4M3 (1UL << 55) +#endif +#ifndef HWCAP2_F8E5M2 +#define HWCAP2_F8E5M2 (1UL << 56) +#endif +#ifndef HWCAP_CMPBR +#define HWCAP_CMPBR (1UL << 33) +#endif +#ifndef HWCAP_FPRCVT +#define HWCAP_FPRCVT (1UL << 34) +#endif + +void ffCPUDetectByCpuid(FFCPUResult* cpu) +{ + char buf[PROC_FILE_BUFFSIZ]; + ssize_t nRead = ffReadFileData("/proc/self/auxv", ARRAY_SIZE(buf), buf); + + if (nRead < (ssize_t) sizeof(Elf64_auxv_t)) return; + + uint64_t hwcap = 0, hwcap2 = 0; + + for (Elf64_auxv_t* auxv = (Elf64_auxv_t*)buf; (char*)auxv < buf + nRead; ++auxv) + { + if (auxv->a_type == AT_HWCAP) + { + hwcap = auxv->a_un.a_val; + } + else if (auxv->a_type == AT_HWCAP2) + { + hwcap2 = auxv->a_un.a_val; + } + } + + if (!hwcap) return; + + cpu->march = "unknown"; + + // ARMv8-A + bool has_fp = (hwcap & HWCAP_FP) != 0; + bool has_asimd = (hwcap & HWCAP_ASIMD) != 0; + + // ARMv8.1-A + bool has_atomics = (hwcap & HWCAP_ATOMICS) != 0; // optional from v8.0 + bool has_crc32 = (hwcap & HWCAP_CRC32) != 0; // optional from v8.0 + bool has_asimdrdm = (hwcap & HWCAP_ASIMDRDM) != 0; // optional from v8.0 + + // ARMv8.2-A + bool has_fphp = (hwcap & HWCAP_FPHP) != 0; // optional + bool has_dcpop = (hwcap & HWCAP_DCPOP) != 0; // DC CVAP, optional from v8.1 + + // ARMv8.3-A + bool has_paca = (hwcap & HWCAP_PACA) != 0; // optional from v8.2 + bool has_lrcpc = (hwcap & HWCAP_LRCPC) != 0; // optional from v8.2 + bool has_fcma = (hwcap & HWCAP_FCMA) != 0; // optional from v8.2 + bool has_jscvt = (hwcap & HWCAP_JSCVT) != 0; // optional from v8.2 + + // ARMv8.4-A + bool has_dit = (hwcap & HWCAP_DIT) != 0; // optional from v8.3 + bool has_flagm = (hwcap & HWCAP_FLAGM) != 0; // optional from v8.1 + bool has_ilrcpc = (hwcap & HWCAP_ILRCPC) != 0; // optional from v8.2 + + // ARMv8.5-A + bool has_bti = (hwcap2 & HWCAP2_BTI) != 0; // optional from v8.4 + bool has_sb = (hwcap & HWCAP_SB) != 0; // optional from v8.0 + bool has_dcpodp = (hwcap2 & HWCAP2_DCPODP) != 0; // optional from v8.1 + bool has_flagm2 = (hwcap2 & HWCAP2_FLAGM2) != 0; // optional from v8.4 + bool has_frint = (hwcap2 & HWCAP2_FRINT) != 0; // optional from v8.4 + + // ARMv9.0-A + bool has_sve2 = (hwcap2 & HWCAP2_SVE2) != 0; + + // ARMv9.1-A + // ARMv8.6-A + bool has_bf16 = (hwcap2 & HWCAP2_BF16) != 0; // optional from v8.2 + bool has_i8mm = (hwcap2 & HWCAP2_I8MM) != 0; // optional from v8.1 + + // ARMv8.7-A + bool has_afp = (hwcap2 & HWCAP2_AFP) != 0; // optional from v8.6 + + // ARMv9.2-A + bool has_sme = (hwcap2 & HWCAP2_SME) != 0; + + // ARMv9.3-A + bool has_sme2 = (hwcap2 & HWCAP2_SME2) != 0; // optional from v9.2 + + // ARMv8.8-A + bool has_mops = (hwcap2 & HWCAP2_MOPS) != 0; // optional from v8.7 + + // ARMv8.9-A + bool has_cssc = (hwcap2 & HWCAP2_CSSC) != 0; // optional from v8.7 + + // ARMv9.4-A + bool has_sme2p1 = (hwcap2 & HWCAP2_SME2P1) != 0; // optional from v9.2 + + // ARMv9.5-A + bool has_f8e4m3 = (hwcap2 & HWCAP2_F8E4M3) != 0; // optional from v9.2 + bool has_f8e5m2 = (hwcap2 & HWCAP2_F8E5M2) != 0; // optional from v9.2 + + // ARMv9.6-A + bool has_cmpbr = (hwcap & HWCAP_CMPBR) != 0; // optional from v9.5 + bool has_fprcvt = (hwcap & HWCAP_FPRCVT) != 0; // optional from v9.5 + + if (has_sve2 || has_sme) { + // ARMv9 + if (has_cmpbr && has_fprcvt) { + cpu->march = "ARMv9.6-A"; + } else if (has_f8e5m2 && has_f8e4m3) { + cpu->march = "ARMv9.5-A"; + } else if (has_sme2p1) { + cpu->march = "ARMv9.4-A"; + } else if (has_sme2) { + cpu->march = "ARMv9.3-A"; + } else if (has_sme) { + cpu->march = "ARMv9.2-A"; + } else if (has_i8mm && has_bf16) { + cpu->march = "ARMv9.1-A"; + } else { + cpu->march = "ARMv9.0-A"; + } + } else { + // ARMv8 + if (has_cssc) { + cpu->march = "ARMv8.9-A"; + } else if (has_mops) { + cpu->march = "ARMv8.8-A"; + } else if (has_afp) { + cpu->march = "ARMv8.7-A"; + } else if (has_i8mm && has_bf16) { + cpu->march = "ARMv8.6-A"; + } else if (has_bti && has_sb && has_dcpodp && has_flagm2 && has_frint) { + cpu->march = "ARMv8.5-A"; + } else if (has_dit && has_flagm && has_ilrcpc) { + cpu->march = "ARMv8.4-A"; + } else if (has_paca && has_lrcpc && has_fcma && has_jscvt) { + cpu->march = "ARMv8.3-A"; + } else if (has_fphp && has_dcpop) { + cpu->march = "ARMv8.2-A"; + } else if (has_atomics && has_crc32 && has_asimdrdm) { + cpu->march = "ARMv8.1-A"; + } else if (has_asimd && has_fp) { + cpu->march = "ARMv8-A"; + } + } +} +#elif __APPLE__ +#include +#include + +#ifndef CAP_BIT_AdvSIMD +#define CAP_BIT_AdvSIMD 49 +#endif +#ifndef CAP_BIT_AdvSIMD_HPFPCvt +#define CAP_BIT_AdvSIMD_HPFPCvt 50 +#endif +#ifndef CAP_BIT_FEAT_CRC32 +#define CAP_BIT_FEAT_CRC32 51 +#endif +#ifndef CAP_BIT_FEAT_HBC +#define CAP_BIT_FEAT_HBC 64 +#endif +#ifndef CAP_BIT_FEAT_CSSC +#define CAP_BIT_FEAT_CSSC 67 +#endif + +void ffCPUDetectByCpuid(FFCPUResult* cpu) +{ + uint64_t caps[2] = {0}; // 80-bit capability mask, split into two 64-bit values + size_t size = sizeof(caps); + + if (sysctlbyname("hw.optional.arm.caps", caps, &size, NULL, 0) != 0) return; + + // Helper macro to test bit in 80-bit capability mask + #define FF_HAS_CAP(bit) \ + (((bit) < 64) ? ((caps[0] >> (bit)) & 1ULL) : ((caps[1] >> ((bit) - 64U)) & 1ULL)) + + cpu->march = "unknown"; + + // ARMv8-A + bool has_fp = FF_HAS_CAP(CAP_BIT_AdvSIMD_HPFPCvt); // Full FP16 support (implies FP/ASIMD) + bool has_asimd = FF_HAS_CAP(CAP_BIT_AdvSIMD); // Advanced SIMD (NEON) + + // ARMv8.1-A + bool has_lse = FF_HAS_CAP(CAP_BIT_FEAT_LSE); // Large System Extensions, optional in v8.0 + bool has_crc32 = FF_HAS_CAP(CAP_BIT_FEAT_CRC32); // CRC32 instructions, optional in v8.0 + bool has_rdm = FF_HAS_CAP(CAP_BIT_FEAT_RDM); // AdvSIMD rounding double multiply accumulate, optional in v8.0 + + // ARMv8.2-A + bool has_fp16 = FF_HAS_CAP(CAP_BIT_FEAT_FP16); // Half-precision FP support, optional + bool has_dpb = FF_HAS_CAP(CAP_BIT_FEAT_DPB); // DC CVAP, optional from v8.1 + + // ARMv8.3-A + bool has_pauth = FF_HAS_CAP(CAP_BIT_FEAT_PAuth); // Pointer Authentication (PAC), optional from v8.2 + bool has_lrcpc = FF_HAS_CAP(CAP_BIT_FEAT_LRCPC); // LDAPR/LR with RCPC semantics, optional from v8.2 + bool has_fcma = FF_HAS_CAP(CAP_BIT_FEAT_FCMA); // Complex number multiply-add, optional from v8.2 + bool has_jscvt = FF_HAS_CAP(CAP_BIT_FEAT_JSCVT); // JavaScript-style conversion (FJCVTZS), optional from v8.2 + + // ARMv8.4-A + bool has_lse2 = FF_HAS_CAP(CAP_BIT_FEAT_LSE2); // Large System Extensions version 2, optional from v8.2 + bool has_dit = FF_HAS_CAP(CAP_BIT_FEAT_DIT); // Data Independent Timing, optional from v8.3 + bool has_flagm = FF_HAS_CAP(CAP_BIT_FEAT_FlagM); // Flag manipulation (FMOV/FCVT), optional from v8.1 + bool has_lrcpc2 = FF_HAS_CAP(CAP_BIT_FEAT_LRCPC2); // Enhanced RCPC (LDAPUR/LDAPST), optional from v8.2 + + // ARMv8.5-A + bool has_bti = FF_HAS_CAP(CAP_BIT_FEAT_BTI); // Branch Target Identification, optional from v8.4 + bool has_sb = FF_HAS_CAP(CAP_BIT_FEAT_SB); // Speculative Barrier, optional from v8.0 + bool has_dpb2 = FF_HAS_CAP(CAP_BIT_FEAT_DPB2); // DC CVADP (DPB2), optional from v8.1 + bool has_flagm2 = FF_HAS_CAP(CAP_BIT_FEAT_FlagM2); // Enhanced FlagM, optional from v8.4 + bool has_frintts = FF_HAS_CAP(CAP_BIT_FEAT_FRINTTS); // Floating-point to integer instructions, optional from v8.4 + + // ARMv9.0-A + bool has_sve2 = false; // Not exposed and not supported by Apple M4 + + // ARMv9.1-A + // ARMv8.6-A + bool has_bf16 = FF_HAS_CAP(CAP_BIT_FEAT_BF16); // Brain float16, optional from v8.2 + bool has_i8mm = FF_HAS_CAP(CAP_BIT_FEAT_I8MM); // Int8 Matrix Multiply, optional from v8.1 + + // ARMv8.7-A + bool has_afp = FF_HAS_CAP(CAP_BIT_FEAT_AFP); // Alternate FP16 (FEXPA), optional from v8.6 + + // ARMv9.2-A + bool has_sme = FF_HAS_CAP(CAP_BIT_FEAT_SME); // Scalable Matrix Extension, optional from v9.2 + + // ARMv9.3-A + bool has_sme2 = FF_HAS_CAP(CAP_BIT_FEAT_SME2); // SME2, optional from v9.2 + + // ARMv8.8-A + bool has_hbc = FF_HAS_CAP(CAP_BIT_FEAT_HBC); // Hinted conditional branches, optional from v8.7 + + // ARMv8.9-A + bool has_cssc = FF_HAS_CAP(CAP_BIT_FEAT_CSSC); // Common Short String Compare, optional from v8.7 + + // ARMv9.4-A+ are not exposed yet + + if (has_sve2 || has_sme) { + // ARMv9 family + if (has_sme2) { + cpu->march = "ARMv9.3-A"; + } else if (has_sme) { + cpu->march = "ARMv9.2-A"; + } else if (has_i8mm && has_bf16) { + cpu->march = "ARMv9.1-A"; + } else { + cpu->march = "ARMv9.0-A"; + } + } else { + // ARMv8 family + if (has_cssc) { + cpu->march = "ARMv8.9-A"; + } else if (has_hbc) { + cpu->march = "ARMv8.8-A"; + } else if (has_afp) { + cpu->march = "ARMv8.7-A"; + } else if (has_i8mm && has_bf16) { + cpu->march = "ARMv8.6-A"; + } else if (has_bti && has_sb && has_dpb2 && has_flagm2 && has_frintts) { + cpu->march = "ARMv8.5-A"; + } else if (has_lse2 && has_dit && has_flagm && has_lrcpc2) { + cpu->march = "ARMv8.4-A"; + } else if (has_pauth && has_lrcpc && has_fcma && has_jscvt) { + cpu->march = "ARMv8.3-A"; + } else if (has_fp16 && has_dpb) { + cpu->march = "ARMv8.2-A"; + } else if (has_lse && has_crc32 && has_rdm) { + cpu->march = "ARMv8.1-A"; + } else if (has_asimd && has_fp) { + cpu->march = "ARMv8-A"; + } + } + + #undef HAS_CAP +} +#elif _WIN32 +#include + +// Missing from winnt.h of MinGW-w64 +#define PF_ARM_LSE2_AVAILABLE 62 +#define PF_RESERVED_FEATURE 63 +#define PF_ARM_SHA3_INSTRUCTIONS_AVAILABLE 64 +#define PF_ARM_SHA512_INSTRUCTIONS_AVAILABLE 65 +#define PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE 66 +#define PF_ARM_V82_FP16_INSTRUCTIONS_AVAILABLE 67 +#define PF_ARM_V86_BF16_INSTRUCTIONS_AVAILABLE 68 +#define PF_ARM_V86_EBF16_INSTRUCTIONS_AVAILABLE 69 +#define PF_ARM_SME_INSTRUCTIONS_AVAILABLE 70 +#define PF_ARM_SME2_INSTRUCTIONS_AVAILABLE 71 +#define PF_ARM_SME2_1_INSTRUCTIONS_AVAILABLE 72 +#define PF_ARM_SME2_2_INSTRUCTIONS_AVAILABLE 73 +#define PF_ARM_SME_AES_INSTRUCTIONS_AVAILABLE 74 +#define PF_ARM_SME_SBITPERM_INSTRUCTIONS_AVAILABLE 75 +#define PF_ARM_SME_SF8MM4_INSTRUCTIONS_AVAILABLE 76 +#define PF_ARM_SME_SF8MM8_INSTRUCTIONS_AVAILABLE 77 +#define PF_ARM_SME_SF8DP2_INSTRUCTIONS_AVAILABLE 78 +#define PF_ARM_SME_SF8DP4_INSTRUCTIONS_AVAILABLE 79 +#define PF_ARM_SME_SF8FMA_INSTRUCTIONS_AVAILABLE 80 +#define PF_ARM_SME_F8F32_INSTRUCTIONS_AVAILABLE 81 +#define PF_ARM_SME_F8F16_INSTRUCTIONS_AVAILABLE 82 +#define PF_ARM_SME_F16F16_INSTRUCTIONS_AVAILABLE 83 +#define PF_ARM_SME_B16B16_INSTRUCTIONS_AVAILABLE 84 +#define PF_ARM_SME_F64F64_INSTRUCTIONS_AVAILABLE 85 +#define PF_ARM_SME_I16I64_INSTRUCTIONS_AVAILABLE 86 +#define PF_ARM_SME_LUTv2_INSTRUCTIONS_AVAILABLE 87 +#define PF_ARM_SME_FA64_INSTRUCTIONS_AVAILABLE 88 + +void ffCPUDetectByCpuid(FFCPUResult* cpu) +{ + // ARMv8-A + bool has_vfp = IsProcessorFeaturePresent(PF_ARM_VFP_32_REGISTERS_AVAILABLE); // Implies basic FP support + bool has_neon = IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE); // NEON (ASIMD) + + // ARMv8.1-A + bool has_atomics = IsProcessorFeaturePresent(PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE); // LSE atomics + bool has_crc32 = IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE); // CRC32 + + // ARMv8.2-A + bool has_fp16 = IsProcessorFeaturePresent(PF_ARM_V82_FP16_INSTRUCTIONS_AVAILABLE); // Half-precision FP + + // ARMv8.3-A + bool has_lrcpc = IsProcessorFeaturePresent(PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE); // LDAPR/LR with RCPC semantics + bool has_jscvt = IsProcessorFeaturePresent(PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE); // FJCVTZS + + // ARMv8.4-A + // My CPU (Apple M1 Pro in VM) does support LSE2, but Windows doesn't detect it for some reason + // bool has_lse2 = IsProcessorFeaturePresent(PF_ARM_LSE2_AVAILABLE); // Large System Extensions version 2, optional from v8.2 + bool has_dp = IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE); // DotProd, optional from v8.1 (*) + + // ARMv9.0-A + bool has_sve2 = IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE); // SVE2 + + // ARMv9.1-A + // ARMv8.6-A + bool has_bf16 = IsProcessorFeaturePresent(PF_ARM_V86_BF16_INSTRUCTIONS_AVAILABLE); // BF16, optional from v8.2 + bool has_i8mm = IsProcessorFeaturePresent(PF_ARM_V82_I8MM_INSTRUCTIONS_AVAILABLE); // Int8 matrix multiply, optional from v8.2 + + // ARMv8.7-A + bool has_ebf16 = IsProcessorFeaturePresent(PF_ARM_V86_EBF16_INSTRUCTIONS_AVAILABLE); // Extended BFloat16 behaviors, optional from v8.2 + + // ARMv9.2-A + bool has_sme = IsProcessorFeaturePresent(PF_ARM_SME_INSTRUCTIONS_AVAILABLE); // SME + + // ARMv9.3-A + bool has_sme2 = IsProcessorFeaturePresent(PF_ARM_SME2_INSTRUCTIONS_AVAILABLE); // SME2 + + // ARMv9.4-A + bool has_sme2p1 = IsProcessorFeaturePresent(PF_ARM_SME2_1_INSTRUCTIONS_AVAILABLE); // SME2.1 + + + if (has_sve2 || has_sme) + { + // ARMv9 family + if (has_sme2p1) { + cpu->march = "ARMv9.4-A"; + } else if (has_sme2) { + cpu->march = "ARMv9.3-A"; + } else if (has_sme) { + cpu->march = "ARMv9.2-A"; + } else if (has_i8mm && has_bf16) { + cpu->march = "ARMv9.1-A"; + } else { + cpu->march = "ARMv9.0-A"; + } + } + else + { + // ARMv8 family + if (has_ebf16) { + cpu->march = "ARMv8.7-A"; + } else if (has_i8mm && has_bf16) { + cpu->march = "ARMv8.6-A"; + } else if (has_dp) { + cpu->march = "ARMv8.4-A"; + } else if (has_lrcpc && has_jscvt) { + cpu->march = "ARMv8.3-A"; + } else if (has_fp16) { + cpu->march = "ARMv8.2-A"; + } else if (has_atomics && has_crc32) { + cpu->march = "ARMv8.1-A"; + } else if (has_neon && has_vfp) { + cpu->march = "ARMv8-A"; + } + } +} +#else +void ffCPUDetectByCpuid(FF_MAYBE_UNUSED FFCPUResult* cpu) +{ + // Unsupported system +} +#endif + +#else + +void ffCPUDetectByCpuid(FF_MAYBE_UNUSED FFCPUResult* cpu) +{ + // Unsupported architecture +} + +#endif diff --git a/src/detection/cpu/cpu.h b/src/detection/cpu/cpu.h index 4f07b97799..bac4aa3e52 100644 --- a/src/detection/cpu/cpu.h +++ b/src/detection/cpu/cpu.h @@ -15,6 +15,7 @@ typedef struct FFCPUResult { FFstrbuf name; FFstrbuf vendor; + const char* march; // Microarchitecture uint16_t packages; uint16_t coresPhysical; @@ -32,29 +33,4 @@ typedef struct FFCPUResult const char* ffDetectCPU(const FFCPUOptions* options, FFCPUResult* cpu); const char* ffCPUAppleCodeToName(uint32_t code); const char* ffCPUQualcommCodeToName(uint32_t code); - -#if defined(__x86_64__) || defined(__i386__) - -#include - -// WARNING: CPUID may report frequencies of efficient cores -inline static const char* ffCPUDetectSpeedByCpuid(FFCPUResult* cpu) -{ - uint32_t base = 0, max = 0, bus = 0, unused = 0; - if (!__get_cpuid(0x16, &base, &max, &bus, &unused)) - return "Unsupported instruction"; - - // cpuid returns 0 MHz when hyper-v is enabled - if (base) cpu->frequencyBase = base; - if (max) cpu->frequencyMax = max; - return NULL; -} - -#else - -inline static const char* ffCPUDetectSpeedByCpuid(FF_MAYBE_UNUSED FFCPUResult* cpu) -{ - return "Unsupported platform"; -} - -#endif +void ffCPUDetectByCpuid(FFCPUResult* cpu); diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index 1226131e53..7a09d4c7b3 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -126,6 +126,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) if(cpu->coresOnline == 1) cpu->coresOnline = (uint16_t) ffSysctlGetInt("hw.activecpu", 1); + ffCPUDetectByCpuid(cpu); detectFrequency(cpu); if (options->showPeCoreCount) detectCoreCount(cpu); diff --git a/src/detection/cpu/cpu_bsd.c b/src/detection/cpu/cpu_bsd.c index 7eed83beaa..9d8329aa09 100644 --- a/src/detection/cpu/cpu_bsd.c +++ b/src/detection/cpu/cpu_bsd.c @@ -70,7 +70,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), ¤tCPU); #endif - ffCPUDetectSpeedByCpuid(cpu); + ffCPUDetectByCpuid(cpu); uint32_t clockrate = (uint32_t) ffSysctlGetInt("hw.clockrate", 0); if (clockrate > cpu->frequencyBase) cpu->frequencyBase = clockrate; diff --git a/src/detection/cpu/cpu_haiku.c b/src/detection/cpu/cpu_haiku.c index 17dc1385cf..2c604d4bed 100644 --- a/src/detection/cpu/cpu_haiku.c +++ b/src/detection/cpu/cpu_haiku.c @@ -55,7 +55,7 @@ const char* ffDetectCPUImpl(FF_MAYBE_UNUSED const FFCPUOptions* options, FFCPURe ffStrbufSetF(&cpu->name, "(Unknown %" B_PRIx32 ")", cpuModel); ffStrbufSetS(&cpu->vendor, get_cpu_vendor_string(cpuVendor)); - ffCPUDetectSpeedByCpuid(cpu); + ffCPUDetectByCpuid(cpu); if (cpu->frequencyBase < frequency) cpu->frequencyBase = frequency; cpu->packages = packages; cpu->coresPhysical = cores; diff --git a/src/detection/cpu/cpu_linux.c b/src/detection/cpu/cpu_linux.c index 411c1f0d56..0c4cda4097 100644 --- a/src/detection/cpu/cpu_linux.c +++ b/src/detection/cpu/cpu_linux.c @@ -13,87 +13,129 @@ #define FF_CPUINFO_PATH "/proc/cpuinfo" -static double parseHwmonDir(FFstrbuf* dir, FFstrbuf* buffer) +static double parseTZDir(int dfd, FFstrbuf* buffer) { - //https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface - uint32_t dirLength = dir->length; - ffStrbufAppendS(dir, "temp1_input"); + if (!ffReadFileBufferRelative(dfd, "type", buffer)) + return FF_CPU_TEMP_UNSET; - if(!ffReadFileBuffer(dir->chars, buffer)) - { - // Some badly implemented system put temp file in /hwmonN/device - ffStrbufSubstrBefore(dir, dirLength); - ffStrbufAppendS(dir, "device/"); - dirLength = dir->length; - ffStrbufAppendS(dir, "temp1_input"); - - if(!ffReadFileBuffer(dir->chars, buffer)) - return FF_CPU_TEMP_UNSET; - } + if (!ffStrbufStartsWithS(buffer, "cpu") && + !ffStrbufStartsWithS(buffer, "soc") && + #if __x86_64__ || __i386__ + !ffStrbufEqualS(buffer, "x86_pkg_temp") && + #endif + true + ) return FF_CPU_TEMP_UNSET; - ffStrbufSubstrBefore(dir, dirLength); + if (!ffReadFileBufferRelative(dfd, "temp", buffer)) + return FF_CPU_TEMP_UNSET; double value = ffStrbufToDouble(buffer, FF_CPU_TEMP_UNSET);// millidegree Celsius - - if(value == FF_CPU_TEMP_UNSET) + if (value == FF_CPU_TEMP_UNSET) return FF_CPU_TEMP_UNSET; - ffStrbufAppendS(dir, "name"); - if (!ffReadFileBuffer(dir->chars, buffer)) + return value / 1000.; +} + +static double parseHwmonDir(int dfd, FFstrbuf* buffer) +{ + if (!ffReadFileBufferRelative(dfd, "name", buffer)) return FF_CPU_TEMP_UNSET; ffStrbufTrimRightSpace(buffer); - if( - ffStrbufContainS(buffer, "cpu") || - ffStrbufEqualS(buffer, "k10temp") || // AMD - ffStrbufEqualS(buffer, "fam15h_power") || // AMD - ffStrbufEqualS(buffer, "coretemp") // Intel - ) return value / 1000.; + if ( + !ffStrbufContainS(buffer, "cpu") && + #if __x86_64__ || __i386__ + !ffStrbufEqualS(buffer, "k10temp") && // AMD + !ffStrbufEqualS(buffer, "fam15h_power") && // AMD + !ffStrbufEqualS(buffer, "coretemp") && // Intel + #endif + true + ) return FF_CPU_TEMP_UNSET; - return FF_CPU_TEMP_UNSET; -} + //https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface + if (!ffReadFileBufferRelative(dfd, "temp1_input", buffer)) + return FF_CPU_TEMP_UNSET; -static double detectTZTemp(FFstrbuf* buffer) -{ - if (ffReadFileBuffer("/sys/class/thermal/thermal_zone0/temp", buffer)) - { - double value = ffStrbufToDouble(buffer, FF_CPU_TEMP_UNSET);// millidegree Celsius - return value != FF_CPU_TEMP_UNSET ? value / 1000. : FF_CPU_TEMP_UNSET; - } - return FF_CPU_TEMP_UNSET; + double value = ffStrbufToDouble(buffer, FF_CPU_TEMP_UNSET);// millidegree Celsius + if (value == FF_CPU_TEMP_UNSET) + return FF_CPU_TEMP_UNSET; + + return value / 1000.; } static double detectCPUTemp(void) { - FF_STRBUF_AUTO_DESTROY baseDir = ffStrbufCreateA(64); - ffStrbufAppendS(&baseDir, "/sys/class/hwmon/"); - FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); + { + FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/hwmon/"); + if(dirp) + { + int dfd = dirfd(dirp); + struct dirent* entry; + while((entry = readdir(dirp)) != NULL) + { + if(entry->d_name[0] == '.') + continue; - uint32_t baseDirLength = baseDir.length; - - FF_AUTO_CLOSE_DIR DIR* dirp = opendir(baseDir.chars); - if(dirp == NULL) - return FF_CPU_TEMP_UNSET; + FF_AUTO_CLOSE_FD int subfd = openat(dfd, entry->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if(subfd < 0) + continue; - struct dirent* entry; - while((entry = readdir(dirp)) != NULL) + double result = parseHwmonDir(subfd, &buffer); + if (result != FF_CPU_TEMP_UNSET) + return result; + } + } + } { - if(entry->d_name[0] == '.') - continue; + FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/thermal/"); + if(dirp) + { + int dfd = dirfd(dirp); + struct dirent* entry; + while((entry = readdir(dirp)) != NULL) + { + if(entry->d_name[0] == '.') + continue; + if(!ffStrStartsWith(entry->d_name, "thermal_zone")) + continue; - ffStrbufAppendS(&baseDir, entry->d_name); - ffStrbufAppendC(&baseDir, '/'); + FF_AUTO_CLOSE_FD int subfd = openat(dfd, entry->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if(subfd < 0) + continue; - double result = parseHwmonDir(&baseDir, &buffer); - if (result != FF_CPU_TEMP_UNSET) - return result; + double result = parseTZDir(subfd, &buffer); + if (result != FF_CPU_TEMP_UNSET) + return result; + } + } + } + { + FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/devices/platform/"); + if(dirp) + { + int dfd = dirfd(dirp); + struct dirent* entry; + while((entry = readdir(dirp)) != NULL) + { + if(entry->d_name[0] == '.') + continue; + if(!ffStrStartsWith(entry->d_name, "cputemp.")) + continue; - ffStrbufSubstrBefore(&baseDir, baseDirLength); + FF_AUTO_CLOSE_FD int subfd = openat(dfd, entry->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if(subfd < 0) + continue; + + double result = parseHwmonDir(subfd, &buffer); + if (result != FF_CPU_TEMP_UNSET) + return result; + } + } } - return detectTZTemp(&buffer); + return FF_CPU_TEMP_UNSET; } #ifdef __ANDROID__ @@ -456,7 +498,7 @@ FF_MAYBE_UNUSED static const char* detectCPUX86(const FFCPUOptions* options, FFC cpu->coresPhysical *= cpu->packages; // Ref https://github.com/fastfetch-cli/fastfetch/issues/1194#issuecomment-2295058252 - ffCPUDetectSpeedByCpuid(cpu); + ffCPUDetectByCpuid(cpu); if (!detectFrequency(cpu, options) || cpu->frequencyBase == 0) cpu->frequencyBase = (uint32_t) ffStrbufToUInt(&cpuMHz, 0); @@ -734,6 +776,8 @@ FF_MAYBE_UNUSED static const char* detectCPUOthers(const FFCPUOptions* options, if (cpu->coresPhysical == 0) detectPhysicalCores(cpu); + ffCPUDetectByCpuid(cpu); + return NULL; } #endif diff --git a/src/detection/cpu/cpu_nbsd.c b/src/detection/cpu/cpu_nbsd.c index 16ad005054..4ab134d383 100644 --- a/src/detection/cpu/cpu_nbsd.c +++ b/src/detection/cpu/cpu_nbsd.c @@ -64,7 +64,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) cpu->coresLogical = cpu->coresPhysical; cpu->coresOnline = (uint16_t) ffSysctlGetInt("hw.ncpuonline", cpu->coresLogical); - ffCPUDetectSpeedByCpuid(cpu); + ffCPUDetectByCpuid(cpu); uint32_t freq = (uint32_t) ffSysctlGetInt("machdep.cpu.frequency.target", 0); if (freq == 0) freq = (uint32_t) (ffSysctlGetInt64("hw.cpu0.clock_frequency", 0) / 1000000); diff --git a/src/detection/cpu/cpu_obsd.c b/src/detection/cpu/cpu_obsd.c index 51d12a297a..ea2d279349 100644 --- a/src/detection/cpu/cpu_obsd.c +++ b/src/detection/cpu/cpu_obsd.c @@ -56,7 +56,7 @@ const char *ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) cpu->coresLogical = cpu->coresPhysical; cpu->coresOnline = (uint16_t) ffSysctlGetInt(CTL_HW, HW_NCPUONLINE, cpu->coresLogical); - ffCPUDetectSpeedByCpuid(cpu); + ffCPUDetectByCpuid(cpu); uint32_t cpuspeed = (uint32_t) ffSysctlGetInt(CTL_HW, HW_CPUSPEED, 0); if (cpuspeed > cpu->frequencyBase) cpu->frequencyBase = cpuspeed; diff --git a/src/detection/cpu/cpu_sunos.c b/src/detection/cpu/cpu_sunos.c index 6f9f668bbd..3185919b98 100644 --- a/src/detection/cpu/cpu_sunos.c +++ b/src/detection/cpu/cpu_sunos.c @@ -122,7 +122,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) kstat_named_t* kn = kstat_data_lookup(ks, "vendor_id"); if (kn) ffStrbufSetNS(&cpu->vendor, KSTAT_NAMED_STR_BUFLEN(kn) - 1, KSTAT_NAMED_STR_PTR(kn)); } - ffCPUDetectSpeedByCpuid(cpu); + ffCPUDetectByCpuid(cpu); { kstat_named_t* kn = kstat_data_lookup(ks, "clock_MHz"); if (kn && kn->value.ui32 > cpu->frequencyBase) diff --git a/src/detection/cpu/cpu_windows.c b/src/detection/cpu/cpu_windows.c index f27ec806a7..79ed7a660a 100644 --- a/src/detection/cpu/cpu_windows.c +++ b/src/detection/cpu/cpu_windows.c @@ -287,7 +287,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) if (error) return error; - ffCPUDetectSpeedByCpuid(cpu); + ffCPUDetectByCpuid(cpu); if (options->showPeCoreCount) detectCoreTypes(cpu); if (cpu->frequencyMax == 0) diff --git a/src/detection/cpuusage/cpuusage.c b/src/detection/cpuusage/cpuusage.c index b9b944d323..40e569528b 100644 --- a/src/detection/cpuusage/cpuusage.c +++ b/src/detection/cpuusage/cpuusage.c @@ -54,6 +54,7 @@ const char* ffGetCpuUsageResult(FFCPUUsageOptions* options, FFlist* result) ffTimeSleep(options->waitTime); goto retry; } + return "CPU time did not increase. Try increasing wait time."; } } diff --git a/src/detection/de/de_linux.c b/src/detection/de/de_linux.c index 2f3570e193..5913c66c93 100644 --- a/src/detection/de/de_linux.c +++ b/src/detection/de/de_linux.c @@ -6,8 +6,9 @@ #include "common/parsing.h" #include "common/properties.h" #include "common/processing.h" +#include "util/binary.h" +#include "util/path.h" #include "detection/displayserver/displayserver.h" -#include "util/stringUtils.h" #include #ifdef __FreeBSD__ @@ -195,6 +196,45 @@ static void getUnity(FFstrbuf* result, FF_MAYBE_UNUSED FFDEOptions* options) ffStrbufSubstrBeforeFirstC(result, '"'); } +static bool extractTdeVersion(const char* line, uint32_t len, void *userdata) +{ + int count = 0; + sscanf(line, "R%*d.%*d.%*d%n", &count); + if (count == 0) return true; + + ffStrbufSetNS((FFstrbuf*) userdata, len, line); + return false; +} + +static const char* getTrinity(FFstrbuf* result, FFDEOptions* options) +{ + FF_STRBUF_AUTO_DESTROY path = ffStrbufCreate(); + const char* error = ffFindExecutableInPath("tde-config", &path); + if (error) return "Failed to find tde-config path"; + + ffStrbufSubstrBeforeLastC(&path, '/'); + ffStrbufAppendS(&path, "/../lib/libtdecore.so"); + + if (ffBinaryExtractStrings(path.chars, extractTdeVersion, result, strlen("R0.0.0")) == NULL) + return NULL; + + if (options->slowVersionDetection) + { + ffStrbufClear(&path); + ffProcessAppendStdOut(&path, (char* const[]){ + "tde-config", + "--version", + NULL + }); + + ffParsePropLines(path.chars , "TDE: ", result); + return NULL; + } + + return "All methods failed"; +} + + const char* ffDetectDEVersion(const FFstrbuf* deName, FFstrbuf* result, FFDEOptions* options) { if (!instance.config.general.detectVersion) return "Disabled by config"; @@ -215,6 +255,8 @@ const char* ffDetectDEVersion(const FFstrbuf* deName, FFstrbuf* result, FFDEOpti getBudgie(result, options); else if (ffStrbufEqualS(deName, FF_DE_PRETTY_UNITY)) getUnity(result, options); + else if (ffStrbufEqualS(deName, "trinity")) + getTrinity(result, options); else return "Unsupported DE"; return NULL; diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index fe13c8599d..377faf9fc0 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -9,7 +9,6 @@ #include #include #include -#include #ifdef __USE_LARGEFILE64 #define stat stat64 diff --git a/src/detection/diskio/diskio_linux.c b/src/detection/diskio/diskio_linux.c index 4d8d622419..f07b063c5e 100644 --- a/src/detection/diskio/diskio_linux.c +++ b/src/detection/diskio/diskio_linux.c @@ -23,8 +23,8 @@ static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* res ffStrbufAppendC(&name, ' '); } - if (ffAppendFileBufferRelative(devfd, "model", &name)) - ffStrbufTrimRightSpace(&name); + ffAppendFileBufferRelative(devfd, "model", &name); + ffStrbufTrimRightSpace(&name); if (name.length == 0) ffStrbufSetS(&name, devName); diff --git a/src/detection/displayserver/displayserver_android.c b/src/detection/displayserver/displayserver_android.c index 0c7a3c1c97..abe7235fbc 100644 --- a/src/detection/displayserver/displayserver_android.c +++ b/src/detection/displayserver/displayserver_android.c @@ -147,8 +147,8 @@ static bool detectWithGetprop(FFDisplayServerResult* ds) void ffConnectDisplayServerImpl(FFDisplayServerResult* ds) { - ffStrbufSetStatic(&ds->wmProcessName, "WindowManager"); - ffStrbufSetStatic(&ds->wmPrettyName, "Window Manager"); + ffStrbufSetStatic(&ds->wmProcessName, "surfaceflinger"); + ffStrbufSetStatic(&ds->wmPrettyName, "SurfaceFlinger"); if (!detectWithGetprop(ds)) detectWithDumpsys(ds); diff --git a/src/detection/displayserver/linux/wayland/kde-output-device-v2-client-protocol.h b/src/detection/displayserver/linux/wayland/kde-output-device-v2-client-protocol.h index a7e98be7a1..9e824fce9c 100644 --- a/src/detection/displayserver/linux/wayland/kde-output-device-v2-client-protocol.h +++ b/src/detection/displayserver/linux/wayland/kde-output-device-v2-client-protocol.h @@ -1,4 +1,4 @@ -/* Generated by wayland-scanner 1.23.1 */ +/* Generated by wayland-scanner 1.24.0 */ #ifndef KDE_OUTPUT_DEVICE_V2_CLIENT_PROTOCOL_H #define KDE_OUTPUT_DEVICE_V2_CLIENT_PROTOCOL_H @@ -234,6 +234,11 @@ enum kde_output_device_v2_capability { * @since 16 */ KDE_OUTPUT_DEVICE_V2_CAPABILITY_EDR = 0x800, + /** + * if this outputdevice supports the sharpness setting + * @since 17 + */ + KDE_OUTPUT_DEVICE_V2_CAPABILITY_SHARPNESS = 0x1000, }; /** * @ingroup iface_kde_output_device_v2 @@ -271,6 +276,10 @@ enum kde_output_device_v2_capability { * @ingroup iface_kde_output_device_v2 */ #define KDE_OUTPUT_DEVICE_V2_CAPABILITY_EDR_SINCE_VERSION 16 +/** + * @ingroup iface_kde_output_device_v2 + */ +#define KDE_OUTPUT_DEVICE_V2_CAPABILITY_SHARPNESS_SINCE_VERSION 17 #endif /* KDE_OUTPUT_DEVICE_V2_CAPABILITY_ENUM */ #ifndef KDE_OUTPUT_DEVICE_V2_VRR_POLICY_ENUM @@ -774,6 +783,17 @@ struct kde_output_device_v2_listener { void (*edr_policy)(void *data, struct kde_output_device_v2 *kde_output_device_v2, uint32_t policy); + /** + * sharpness strength + * + * This is the sharpness modifier of the output. 0 is sharpness + * disabled and 10000 is the maximum sharpness + * @param sharpness sharpness in 0-10000 + * @since 17 + */ + void (*sharpness)(void *data, + struct kde_output_device_v2 *kde_output_device_v2, + uint32_t sharpness); }; /** @@ -919,6 +939,10 @@ kde_output_device_v2_add_listener(struct kde_output_device_v2 *kde_output_device * @ingroup iface_kde_output_device_v2 */ #define KDE_OUTPUT_DEVICE_V2_EDR_POLICY_SINCE_VERSION 16 +/** + * @ingroup iface_kde_output_device_v2 + */ +#define KDE_OUTPUT_DEVICE_V2_SHARPNESS_SINCE_VERSION 17 /** @ingroup iface_kde_output_device_v2 */ diff --git a/src/detection/displayserver/linux/wayland/kde-output-device-v2-protocol.c b/src/detection/displayserver/linux/wayland/kde-output-device-v2-protocol.c index 2d7e153581..889d372549 100644 --- a/src/detection/displayserver/linux/wayland/kde-output-device-v2-protocol.c +++ b/src/detection/displayserver/linux/wayland/kde-output-device-v2-protocol.c @@ -1,6 +1,6 @@ #ifdef FF_HAVE_WAYLAND -/* Generated by wayland-scanner 1.23.1 */ +/* Generated by wayland-scanner 1.24.0 */ /* * SPDX-FileCopyrightText: 2008-2011 Kristian Høgsberg @@ -66,12 +66,13 @@ static const struct wl_message kde_output_device_v2_events[] = { { "max_bits_per_color_range", "15uu", kde_output_device_v2_types + 0 }, { "automatic_max_bits_per_color_limit", "15u", kde_output_device_v2_types + 0 }, { "edr_policy", "16u", kde_output_device_v2_types + 0 }, + { "sharpness", "17u", kde_output_device_v2_types + 0 }, }; WL_EXPORT const struct wl_interface kde_output_device_v2_interface = { - "kde_output_device_v2", 16, + "kde_output_device_v2", 17, 0, NULL, - 33, kde_output_device_v2_events, + 34, kde_output_device_v2_events, }; static const struct wl_message kde_output_device_mode_v2_events[] = { diff --git a/src/detection/displayserver/linux/wayland/kde-output.c b/src/detection/displayserver/linux/wayland/kde-output.c index 3a50913876..59be773dfb 100644 --- a/src/detection/displayserver/linux/wayland/kde-output.c +++ b/src/detection/displayserver/linux/wayland/kde-output.c @@ -177,6 +177,7 @@ static struct kde_output_device_v2_listener outputListener = { .max_bits_per_color_range = (void*) stubListener, .automatic_max_bits_per_color_limit = (void*) stubListener, .edr_policy = (void*) stubListener, + .sharpness = (void*) stubListener, }; const char* ffWaylandHandleKdeOutput(WaylandData* wldata, struct wl_registry* registry, uint32_t name, uint32_t version) diff --git a/src/detection/displayserver/linux/wayland/wayland.c b/src/detection/displayserver/linux/wayland/wayland.c index f64d186324..843bab93a3 100644 --- a/src/detection/displayserver/linux/wayland/wayland.c +++ b/src/detection/displayserver/linux/wayland/wayland.c @@ -26,9 +26,9 @@ static bool waylandDetectWM(int fd, FFDisplayServerResult* result) { -#if __linux__ || (__FreeBSD__ && !__DragonFly__) +#if __linux__ || __GNU__ || (__FreeBSD__ && !__DragonFly__) -#if __linux +#if __linux__ || __GNU__ struct ucred ucred = {}; socklen_t len = sizeof(ucred); if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1 || ucred.pid <= 0) diff --git a/src/detection/displayserver/linux/wmde.c b/src/detection/displayserver/linux/wmde.c index f735bd40c0..8310c4bc9a 100644 --- a/src/detection/displayserver/linux/wmde.c +++ b/src/detection/displayserver/linux/wmde.c @@ -351,7 +351,7 @@ static const char* getFromProcesses(FFDisplayServerResult* result) break; } } -#elif __linux__ +#elif __linux__ || __GNU__ FF_AUTO_CLOSE_DIR DIR* procdir = opendir("/proc"); if(procdir == NULL) return "opendir(\"/proc\") failed"; diff --git a/src/detection/displayserver/linux/xcb.c b/src/detection/displayserver/linux/xcb.c index c27672bec8..796ef8632e 100644 --- a/src/detection/displayserver/linux/xcb.c +++ b/src/detection/displayserver/linux/xcb.c @@ -83,11 +83,11 @@ static void xcbDetectWMfromEWMH(XcbPropertyData* data, xcb_connection_t* connect if(wmWindow == NULL) return; - FF_AUTO_FREE char* wmName = (char*) xcbGetProperty(data, connection, *wmWindow, "_NET_WM_NAME"); - if(wmName == NULL) - wmName = (char*) xcbGetProperty(data, connection, *wmWindow, "WM_NAME"); + FF_AUTO_FREE char* wmName = (char*) xcbGetProperty(data, connection, *wmWindow, "WM_NAME"); + if(!ffStrSet(wmName)) + wmName = (char*) xcbGetProperty(data, connection, *wmWindow, "_NET_WM_NAME"); - if(wmName == NULL || *wmName == '\0') + if(!ffStrSet(wmName)) return; ffStrbufSetS(&result->wmProcessName, wmName); diff --git a/src/detection/displayserver/linux/xlib.c b/src/detection/displayserver/linux/xlib.c index 324290b32b..c0381231cf 100644 --- a/src/detection/displayserver/linux/xlib.c +++ b/src/detection/displayserver/linux/xlib.c @@ -53,9 +53,9 @@ static void x11DetectWMFromEWMH(X11PropertyData* data, Display* display, FFDispl if(wmWindow == NULL) return; - char* wmName = (char*) x11GetProperty(data, display, *wmWindow, "_NET_WM_NAME"); - if(wmName == NULL) - wmName = (char*) x11GetProperty(data, display, *wmWindow, "WM_NAME"); + char* wmName = (char*) x11GetProperty(data, display, *wmWindow, "WM_NAME"); + if(!ffStrSet(wmName)) + wmName = (char*) x11GetProperty(data, display, *wmWindow, "_NET_WM_NAME"); if(ffStrSet(wmName)) ffStrbufSetS(&result->wmProcessName, wmName); diff --git a/src/detection/gpu/gpu.c b/src/detection/gpu/gpu.c index 5bd667ff22..c66d780e6a 100644 --- a/src/detection/gpu/gpu.c +++ b/src/detection/gpu/gpu.c @@ -116,6 +116,13 @@ const char* ffDetectGPU(const FFGPUOptions* options, FFlist* result) { ffListDestroy(result); ffListInitMove(result, &vulkan->gpus); + + #ifdef __ANDROID__ + double ffGPUDetectTempFromTZ(void); + if (options->temp && result->length == 1) + FF_LIST_GET(FFGPUResult, *result, 0)->temperature = ffGPUDetectTempFromTZ(); + #endif + return NULL; } } diff --git a/src/detection/gpu/gpu_android.c b/src/detection/gpu/gpu_android.c new file mode 100644 index 0000000000..a0406790f7 --- /dev/null +++ b/src/detection/gpu/gpu_android.c @@ -0,0 +1,53 @@ +#include "gpu.h" +#include "common/io/io.h" +#include "util/stringUtils.h" + +#include + +static double parseTZDir(int dfd, FFstrbuf* buffer) +{ + if (!ffReadFileBufferRelative(dfd, "type", buffer) || !ffStrbufStartsWithS(buffer, "gpu")) + return FF_GPU_TEMP_UNSET; + + if (!ffReadFileBufferRelative(dfd, "temp", buffer)) + return FF_GPU_TEMP_UNSET; + + double value = ffStrbufToDouble(buffer, FF_GPU_TEMP_UNSET);// millidegree Celsius + if (value == FF_GPU_TEMP_UNSET) + return FF_GPU_TEMP_UNSET; + + return value / 1000.; +} + +double ffGPUDetectTempFromTZ(void) +{ + FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/thermal/"); + if(dirp) + { + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); + int dfd = dirfd(dirp); + struct dirent* entry; + while((entry = readdir(dirp)) != NULL) + { + if(entry->d_name[0] == '.') + continue; + if(!ffStrStartsWith(entry->d_name, "thermal_zone")) + continue; + + FF_AUTO_CLOSE_FD int subfd = openat(dfd, entry->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); + if(subfd < 0) + continue; + + double result = parseTZDir(subfd, &buffer); + if (result != FF_GPU_TEMP_UNSET) + return result; + } + } + return FF_GPU_TEMP_UNSET; +} + +const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) +{ + FF_UNUSED(options, gpus); + return "No permission. Fallbacks to Vulkan, OpenCL or OpenGL instead"; +} diff --git a/src/detection/gpu/gpu_pci.c b/src/detection/gpu/gpu_pci.c index 47992f4b2c..cce1deb45d 100644 --- a/src/detection/gpu/gpu_pci.c +++ b/src/detection/gpu/gpu_pci.c @@ -45,10 +45,10 @@ static const FFstrbuf* loadPciIds() if (pciids.length == 0) ffReadFileBuffer(FASTFETCH_TARGET_DIR_USR "/local/share/hwdata/pci.ids", &pciids); } - #elif __FreeBSD__ || __NetBSD__ - ffReadFileBuffer(_PATH_LOCALBASE "/share/pciids/pci.ids", &pciids); - #elif __OpenBSD__ + #elif __OpenBSD__ || __FreeBSD__ || __NetBSD__ ffReadFileBuffer(_PATH_LOCALBASE "/share/hwdata/pci.ids", &pciids); + if (pciids.length == 0) + ffReadFileBuffer(_PATH_LOCALBASE "/share/pciids/pci.ids", &pciids); #elif __sun ffReadFileBuffer(FASTFETCH_TARGET_DIR_ROOT "/usr/share/hwdata/pci.ids", &pciids); #elif __HAIKU__ diff --git a/src/detection/gpu/gpu_windows.c b/src/detection/gpu/gpu_windows.c index d32b60004a..944e1030b8 100644 --- a/src/detection/gpu/gpu_windows.c +++ b/src/detection/gpu/gpu_windows.c @@ -40,17 +40,11 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* return "CM_Get_Device_ID_ListW failed"; } - int deviceCount = 0; + FF_MAYBE_UNUSED int deviceCount = 0; for (wchar_t* devId = devIdList; *devId; devId += wcslen(devId) + 1) { FF_DEBUG("Processing device ID: %ls", devId); - if (wcsncmp(devId, L"SWD\\", 4) == 0) - { - FF_DEBUG("Skipping SWD device interface to avoid duplicates"); - continue; - } - DEVINST devInst = 0; { @@ -121,6 +115,25 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* wchar_t buffer[256]; ULONG bufferLen = 0; + + FF_DEBUG("Get device description as device name"); + bufferLen = sizeof(buffer); + if (CM_Get_DevNode_Registry_PropertyW(devInst, CM_DRP_DEVICEDESC, NULL, buffer, &bufferLen, 0) == CR_SUCCESS) + { + ffStrbufSetWS(&gpu->name, buffer); + FF_DEBUG("Found device description: %s", gpu->name.chars); + } + else + { + FF_DEBUG("Failed to get device description"); + } + + if (wcsncmp(devId, L"SWD\\", 4) == 0 || wcsncmp(devId, L"ROOT\\DISPLAY\\", 13) == 0) + { + FF_DEBUG("Skipping virtual devices to avoid duplicates"); + continue; + } + if (CM_Open_DevNode_Key(devInst, KEY_QUERY_VALUE, 0, RegDisposition_OpenExisting, &hVideoIdKey, CM_REGISTRY_HARDWARE) == CR_SUCCESS) { FF_DEBUG("Opened device node registry key"); @@ -145,8 +158,12 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* } } - if (ffRegReadStrbuf(hDirectxKey, L"Description", &gpu->name, NULL)) - FF_DEBUG("Found GPU description: %s", gpu->name.chars); + if (gpu->name.length == 0) + { + FF_DEBUG("Trying to get GPU name from DirectX registry"); + if (ffRegReadStrbuf(hDirectxKey, L"Description", &gpu->name, NULL)) + FF_DEBUG("Found GPU description: %s", gpu->name.chars); + } if (ffRegReadUint64(hDirectxKey, L"DedicatedVideoMemory", &gpu->dedicated.total, NULL)) FF_DEBUG("Found dedicated video memory: %llu bytes", gpu->dedicated.total); @@ -309,21 +326,6 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist* FF_DEBUG("No driver-specific detection function found for vendor: %s", gpu->vendor.chars); } - if (!gpu->name.length) - { - FF_DEBUG("Trying to get device description as fallback"); - bufferLen = sizeof(buffer); - if (CM_Get_DevNode_Registry_PropertyW(devInst, CM_DRP_DEVICEDESC, NULL, buffer, &bufferLen, 0) == CR_SUCCESS) - { - ffStrbufSetWS(&gpu->name, buffer); - FF_DEBUG("Found device description: %s", gpu->name.chars); - } - else - { - FF_DEBUG("Failed to get device description"); - } - } - if (gpu->type == FF_GPU_TYPE_UNKNOWN && adapterLuid > 0) { FF_DEBUG("Trying to determine GPU type using D3DKMT APIs"); diff --git a/src/detection/host/host_mac.c b/src/detection/host/host_mac.c index ecea1681d7..b84bec8023 100644 --- a/src/detection/host/host_mac.c +++ b/src/detection/host/host_mac.c @@ -140,7 +140,7 @@ const char* ffHostGetMacProductNameWithHwModel(const FFstrbuf* hwModel) if(ffStrEquals(version, "14,5") || ffStrEquals(version, "14,9")) return "MacBook Pro (14-inch, 2023)"; if(ffStrEquals(version, "14,3")) return "Mac mini (M2, 2023, Two Thunderbolt 4 ports)"; - if(ffStrEquals(version, "14,12")) return "Mac mini (M2, 2023, Four Thunderbolt 4 ports)"; + if(ffStrEquals(version, "14,12")) return "Mac mini (M2 Pro, 2023, Four Thunderbolt 4 ports)"; if(ffStrEquals(version, "14,7")) return "MacBook Pro (13-inch, M2, 2022)"; if(ffStrEquals(version, "14,2")) return "MacBook Air (M2, 2022)"; if(ffStrEquals(version, "13,1")) return "Mac Studio (M1 Max, 2022, Two USB-C front ports)"; diff --git a/src/detection/initsystem/initsystem_linux.c b/src/detection/initsystem/initsystem_linux.c index 6776ef7584..b730062a0a 100644 --- a/src/detection/initsystem/initsystem_linux.c +++ b/src/detection/initsystem/initsystem_linux.c @@ -48,7 +48,7 @@ const char* ffDetectInitSystem(FFInitSystemResult* result) if (instance.config.general.detectVersion) { - #if __linux__ && !__ANDROID__ + #if (defined(__linux__) && !defined(__ANDROID__)) || defined(__GNU__) if (ffStrbufEqualS(&result->name, "systemd")) { ffBinaryExtractStrings(result->exe.chars, extractSystemdVersion, &result->version, (uint32_t) strlen("systemd 0.0 running in x")); @@ -84,6 +84,23 @@ const char* ffDetectInitSystem(FFInitSystemResult* result) ffStrbufSubstrAfterLastC(&result->version, ' '); } } + else if (ffStrbufEqualS(&result->name, "shepherd")) + { + if (ffProcessAppendStdOut(&result->version, (char* const[]) { + ffStrbufEndsWithS(&result->exe, "/shepherd") ? result->exe.chars : "shepherd", + "--version", + NULL, + }) == NULL && result->version.length) + { + // shepherd (GNU Shepherd) 1.0.6 + // The first line in the output might not contain the version + if (!ffStrbufStartsWithS(&result->version, "shepherd")) + ffStrbufSubstrAfterFirstC(&result->version, '\n'); + + ffStrbufSubstrBeforeFirstC(&result->version, '\n'); + ffStrbufSubstrAfterLastC(&result->version, ' '); + } + } #elif __APPLE__ if (ffStrbufEqualS(&result->name, "launchd")) { diff --git a/src/detection/loadavg/loadavg_linux.c b/src/detection/loadavg/loadavg_linux.c index 9e8b091c57..c75afb9622 100644 --- a/src/detection/loadavg/loadavg_linux.c +++ b/src/detection/loadavg/loadavg_linux.c @@ -19,7 +19,7 @@ const char* ffDetectLoadavg(double result[3]) } #endif - + #ifndef __GNU__ // getloadavg requires higher ANDROID_API version struct sysinfo si; if (sysinfo(&si) < 0) @@ -27,5 +27,6 @@ const char* ffDetectLoadavg(double result[3]) for (int i = 0; i < 3; i++) result[i] = (double) si.loads[i] / (1 << SI_LOAD_SHIFT); + #endif return NULL; } diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index ad96452298..36604bd206 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -29,7 +29,7 @@ #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__NetBSD__) || defined(__HAIKU__) #include #include -#else +#elif !defined(__GNU__) #include #endif #if defined(__sun) || defined(__HAIKU__) @@ -69,12 +69,14 @@ static const FFLocalIpNIFlag niFlagOptions[] = { #ifdef IFF_NOTRAILERS FF_LOCALIP_NIFLAG(NOTRAILERS), #endif -#ifdef __linux__ +#if defined( __linux__) || defined (__GNU__) FF_LOCALIP_NIFLAG(MASTER), FF_LOCALIP_NIFLAG(SLAVE), FF_LOCALIP_NIFLAG(PORTSEL), FF_LOCALIP_NIFLAG(AUTOMEDIA), FF_LOCALIP_NIFLAG(DYNAMIC), +#endif +#ifdef __linux__ FF_LOCALIP_NIFLAG(LOWER_UP), FF_LOCALIP_NIFLAG(DORMANT), FF_LOCALIP_NIFLAG(ECHO), @@ -396,7 +398,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) adapter->mac = ifa; FF_DEBUG("Updated MAC entry for interface %s", ifa->ifa_name); break; - #elif !__sun + #elif !__sun && !__GNU__ case AF_PACKET: adapter->mac = ifa; FF_DEBUG("Updated MAC entry for interface %s", ifa->ifa_name); @@ -543,7 +545,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) } } mac: - #ifndef __sun + #if !defined( __sun) && !defined(__GNU__) if (options->showType & FF_LOCALIP_TYPE_MAC_BIT) { if (adapter->mac->ifa_addr) @@ -1006,14 +1008,16 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) #endif } - #ifdef __sun + #if __sun || __GNU__ if ((options->showType & FF_LOCALIP_TYPE_MAC_BIT) && ioctl(sockfd, SIOCGIFHWADDR, &ifr) == 0) { const uint8_t* ptr = (uint8_t*) ifr.ifr_addr.sa_data; // NOT ifr_enaddr ffStrbufSetF(&iface->mac, "%02x:%02x:%02x:%02x:%02x:%02x", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]); - FF_DEBUG("Added MAC address %s for interface %s (Solaris)", iface->mac.chars, iface->name.chars); + FF_DEBUG("Added MAC address %s for interface %s (Solaris/GNU)", iface->mac.chars, iface->name.chars); } + #endif + #if __sun if (options->showType & FF_LOCALIP_TYPE_SPEED_BIT) { __attribute__((__cleanup__(kstatFreeWrap))) kstat_ctl_t* kc = kstat_open(); diff --git a/src/detection/localip/localip_windows.c b/src/detection/localip/localip_windows.c index 0fe1038530..2e12280c30 100644 --- a/src/detection/localip/localip_windows.c +++ b/src/detection/localip/localip_windows.c @@ -74,8 +74,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) } } - int adapterCount = 0; - int processedCount = 0; + FF_MAYBE_UNUSED int adapterCount = 0, processedCount = 0; // Iterate through all of the adapters for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next) @@ -137,7 +136,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) uint32_t typesToAdd = options->showType & (FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_IPV6_BIT | FF_LOCALIP_TYPE_ALL_IPS_BIT); FF_DEBUG("Types to add for adapter %u: 0x%X", (unsigned)adapter->IfIndex, typesToAdd); - int ipv4Count = 0, ipv6Count = 0; + FF_MAYBE_UNUSED int ipv4Count = 0, ipv6Count = 0; for (IP_ADAPTER_UNICAST_ADDRESS* ifa = adapter->FirstUnicastAddress; ifa; ifa = ifa->Next) { diff --git a/src/detection/os/os_linux.c b/src/detection/os/os_linux.c index 9b5ef3690f..376bf8d343 100644 --- a/src/detection/os/os_linux.c +++ b/src/detection/os/os_linux.c @@ -370,7 +370,7 @@ void ffDetectOSImpl(FFOSResult* os) { detectOS(os); - #ifdef __linux__ + #if __linux__ || __GNU__ if(ffStrbufEqualS(&os->id, "ubuntu")) getUbuntuFlavour(os); else if(ffStrbufEqualS(&os->id, "debian")) diff --git a/src/detection/packages/packages.h b/src/detection/packages/packages.h index 21e5ba5446..7464a30c17 100644 --- a/src/detection/packages/packages.h +++ b/src/detection/packages/packages.h @@ -55,7 +55,7 @@ const char* ffDetectPackages(FFPackagesResult* result, FFPackagesOptions* option bool ffPackagesReadCache(FFstrbuf* cacheDir, FFstrbuf* cacheContent, const char* filePath, const char* packageId, uint32_t* result); bool ffPackagesWriteCache(FFstrbuf* cacheDir, FFstrbuf* cacheContent, uint32_t num_elements); -#if defined(__linux__) || defined(__APPLE__) +#if defined(__linux__) || defined(__APPLE__) || defined(__GNU__) uint32_t ffPackagesGetNix(FFstrbuf* baseDir, const char* dirname); #endif #ifndef _WIN32 diff --git a/src/detection/packages/packages_linux.c b/src/detection/packages/packages_linux.c index fbd607413c..3d83f3a524 100644 --- a/src/detection/packages/packages_linux.c +++ b/src/detection/packages/packages_linux.c @@ -258,6 +258,8 @@ static uint32_t getAMSystem(FFstrbuf* baseDir) static uint32_t getAMUser(void) { + if (instance.state.platform.configDirs.length == 0) return 0; + // check if $XDG_CONFIG_HOME/appman/appman-config exists FFstrbuf* baseDir = FF_LIST_GET(FFstrbuf, instance.state.platform.configDirs, 0); uint32_t baseLen = baseDir->length; @@ -517,7 +519,6 @@ void ffDetectPackagesImpl(FFPackagesResult* result, FFPackagesOptions* options) #endif ffStrbufSet(&baseDir, &instance.state.platform.homeDir); - if (!(options->disabled & FF_PACKAGES_FLAG_NIX_BIT)) { // Count packages from $HOME/.nix-profile diff --git a/src/detection/processes/processes_linux.c b/src/detection/processes/processes_linux.c index 58fea64162..1c46411785 100644 --- a/src/detection/processes/processes_linux.c +++ b/src/detection/processes/processes_linux.c @@ -16,7 +16,7 @@ const char* ffDetectProcesses(uint32_t* result) { if ( #ifdef _DIRENT_HAVE_D_TYPE - entry->d_type == DT_DIR && + (entry->d_type == DT_DIR || entry->d_type == DT_UNKNOWN) && #endif ffCharIsDigit(entry->d_name[0])) ++num; diff --git a/src/detection/swap/swap_haiku.c b/src/detection/swap/swap_haiku.c index b483668b36..990e741fd7 100644 --- a/src/detection/swap/swap_haiku.c +++ b/src/detection/swap/swap_haiku.c @@ -16,7 +16,7 @@ const char* ffDetectSwap(FFlist* result) if (kvms) { const char* swapAuto = get_driver_parameter(kvms, "swap_auto", NULL, NULL); - if (swapAuto) + if (swapAuto) ffStrbufSetStatic(&swap->name, swapAuto[0] == 'y' ? "Auto" : "Manual"); unload_driver_settings(kvms); } diff --git a/src/detection/terminalshell/terminalshell.c b/src/detection/terminalshell/terminalshell.c index d8cc0cd7ea..48476b63a7 100644 --- a/src/detection/terminalshell/terminalshell.c +++ b/src/detection/terminalshell/terminalshell.c @@ -85,8 +85,9 @@ static bool getShellVersionFish(FFstrbuf* exe, FFstrbuf* version) if(!getExeVersionRaw(exe, version)) return false; - //fish, version 3.6.0 - ffStrbufSubstrAfterLastC(version, ' '); + //fish, version 4.0.2-1 (Built by MSYS2 project) + ffStrbufSubstrAfterFirstS(version, "version "); + ffStrbufSubstrBeforeFirstC(version, ' '); return true; } @@ -579,10 +580,10 @@ static bool getTerminalVersionZed(FFstrbuf* exe, FFstrbuf* version) #ifndef _WIN32 static bool getTerminalVersionKitty(FFstrbuf* exe, FFstrbuf* version) { - #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__GNU__) char buffer[1024] = {}; if ( - #ifdef __linux__ + #if __linux__ || __GNU__ ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib64/kitty/kitty/constants.py", ARRAY_SIZE(buffer) - 1, buffer) || ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib/kitty/kitty/constants.py", ARRAY_SIZE(buffer) - 1, buffer) #else @@ -767,7 +768,7 @@ bool fftsGetTerminalVersion(FFstrbuf* processName, FF_MAYBE_UNUSED FFstrbuf* exe #endif - #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun) || defined(__NetBSD__) || defined(__HAIKU__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun) || defined(__NetBSD__) || defined(__HAIKU__) || defined(__GNU__) if(ffStrbufStartsWithIgnCaseS(processName, "gnome-terminal")) return getTerminalVersionGnome(exe, version); diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index adda26c038..0f50b04521 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -31,13 +31,14 @@ static pid_t getShellInfo(FFShellResult* result, pid_t pid) userShellName = instance.state.platform.userShell.chars + index + 1; } - while (ffProcessGetBasicInfoLinux(pid, &result->processName, &ppid, &tty) == NULL) + while (pid > 1 && ffProcessGetBasicInfoLinux(pid, &result->processName, &ppid, &tty) == NULL) { if (!ffStrbufEqualS(&result->processName, userShellName)) { //Common programs that are between terminal and own process, but are not the shell if( // tty < 0 || //A shell should connect to a tty + pid == 1 || // init/systemd ffStrbufEqualS(&result->processName, "sh") || //This prevents us from detecting things like pipes and redirects, i hope nobody uses plain `sh` as shell ffStrbufEqualS(&result->processName, "sudo") || ffStrbufEqualS(&result->processName, "su") || @@ -50,7 +51,6 @@ static pid_t getShellInfo(FFShellResult* result, pid_t pid) ffStrbufEqualS(&result->processName, "perf") || ffStrbufEqualS(&result->processName, "guake-wrapped") || ffStrbufEqualS(&result->processName, "time") || - ffStrbufContainS(&result->processName, "hyfetch") || //when hyfetch uses fastfetch as backend ffStrbufEqualS(&result->processName, "clifm") || //https://github.com/leo-arch/clifm/issues/289 ffStrbufEqualS(&result->processName, "valgrind") || ffStrbufEqualS(&result->processName, "fastfetch") || //#994 @@ -74,17 +74,18 @@ static pid_t getShellInfo(FFShellResult* result, pid_t pid) ffProcessGetInfoLinux(pid, &result->processName, &result->exe, &result->exeName, &result->exePath); break; } - return ppid; + return pid > 1 ? ppid : 0; } static pid_t getTerminalInfo(FFTerminalResult* result, pid_t pid) { pid_t ppid = 0; - while (ffProcessGetBasicInfoLinux(pid, &result->processName, &ppid, NULL) == NULL) + while (pid > 1 && ffProcessGetBasicInfoLinux(pid, &result->processName, &ppid, NULL) == NULL) { //Known shells if ( + pid == 1 || // init/systemd ffStrbufEqualS(&result->processName, "sudo") || ffStrbufEqualS(&result->processName, "su") || ffStrbufEqualS(&result->processName, "sh") || @@ -109,8 +110,6 @@ static pid_t getTerminalInfo(FFTerminalResult* result, pid_t pid) ffStrbufEqualS(&result->processName, "chezmoi") || // #762 ffStrbufEqualS(&result->processName, "proot") || ffStrbufEqualS(&result->processName, "script") || - ffStrbufEqualS(&result->processName, "init") || - ffStrbufEqualS(&result->processName, "systemd") || #ifdef __linux__ ffStrbufStartsWithS(&result->processName, "flatpak-") || // #707 #endif @@ -145,7 +144,7 @@ static pid_t getTerminalInfo(FFTerminalResult* result, pid_t pid) ffProcessGetInfoLinux(pid, &result->processName, &result->exe, &result->exeName, &result->exePath); break; } - return ppid; + return pid > 1 ? ppid : 0; } static bool getTerminalInfoByPidEnv(FFTerminalResult* result, const char* pidEnv) @@ -241,7 +240,7 @@ static void getTerminalFromEnv(FFTerminalResult* result) } #endif - #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__GNU__) //Konsole else if( getenv("KONSOLE_VERSION") != NULL @@ -334,7 +333,7 @@ static void setTerminalInfoDetails(FFTerminalResult* result) else if(ffStrbufEqualS(&result->processName, "com.termux")) ffStrbufInitStatic(&result->prettyName, "Termux"); - #elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) + #elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__GNU__) else if(ffStrbufStartsWithS(&result->processName, "gnome-terminal")) ffStrbufInitStatic(&result->prettyName, "GNOME Terminal"); diff --git a/src/detection/terminalshell/terminalshell_windows.c b/src/detection/terminalshell/terminalshell_windows.c index 68043ab28f..8e4e388a90 100644 --- a/src/detection/terminalshell/terminalshell_windows.c +++ b/src/detection/terminalshell/terminalshell_windows.c @@ -33,7 +33,6 @@ static uint32_t getShellInfo(FFShellResult* result, uint32_t pid) ffStrbufIgnCaseEqualS(&result->prettyName, "python") || // python on windows generates shim executables ffStrbufIgnCaseEqualS(&result->prettyName, "fastfetch") || // scoop warps the real binaries with a "shim" exe ffStrbufIgnCaseEqualS(&result->prettyName, "flashfetch") || - ffStrbufIgnCaseEqualS(&result->prettyName, "hyfetch") || // uses fastfetch as backend ffStrbufContainIgnCaseS(&result->prettyName, "debug") || ffStrbufContainIgnCaseS(&result->prettyName, "time") || ffStrbufStartsWithIgnCaseS(&result->prettyName, "ConEmu") // https://github.com/fastfetch-cli/fastfetch/issues/488#issuecomment-1619982014 diff --git a/src/detection/uptime/uptime_linux.c b/src/detection/uptime/uptime_linux.c index 44705a5fcf..1007374ce6 100644 --- a/src/detection/uptime/uptime_linux.c +++ b/src/detection/uptime/uptime_linux.c @@ -26,13 +26,15 @@ const char* ffDetectUptime(FFUptimeResult* result) } #endif - + #ifndef __GNU__ struct timespec uptime; if (clock_gettime(CLOCK_BOOTTIME, &uptime) != 0) return "clock_gettime(CLOCK_BOOTTIME) failed"; result->uptime = (uint64_t) uptime.tv_sec * 1000 + (uint64_t) uptime.tv_nsec / 1000000; result->bootTime = ffTimeGetNow() - result->uptime; - return NULL; + #else + return "read(/proc/uptime) failed"; + #endif } diff --git a/src/detection/uptime/uptime_windows.c b/src/detection/uptime/uptime_windows.c index 775b428e94..6978a1163d 100644 --- a/src/detection/uptime/uptime_windows.c +++ b/src/detection/uptime/uptime_windows.c @@ -1,13 +1,11 @@ #include "uptime.h" #include "common/time.h" -#include +#include const char* ffDetectUptime(FFUptimeResult* result) { - // According to MSDN, this function only fails if it's called with NULL - QueryUnbiasedInterruptTime(&result->uptime); - result->uptime /= 10000; // Convert from 100-nanosecond intervals to milliseconds + result->uptime = GetTickCount64(); result->bootTime = ffTimeGetNow() - result->uptime; return NULL; } diff --git a/src/detection/users/users_linux.c b/src/detection/users/users_linux.c index 99e27627c2..acf860d774 100644 --- a/src/detection/users/users_linux.c +++ b/src/detection/users/users_linux.c @@ -10,7 +10,7 @@ #define setutxent setutent #define getutxent getutent #endif -#ifdef __linux__ +#if __linux__ || __GNU__ #include #include #endif @@ -40,7 +40,7 @@ const char* ffDetectUsers(FFUsersOptions* options, FFlist* users) ffStrbufInitS(&user->hostName, n->ut_host); ffStrbufInitS(&user->sessionName, n->ut_line); ffStrbufInit(&user->clientIp); - #ifdef __linux__ + #if __linux__ || __GNU__ bool isIpv6 = false; for (int i = 1; i < 4; ++i) { if (n->ut_addr_v6[i] != 0) { diff --git a/src/detection/version/version.c b/src/detection/version/version.c index f47ffcaa16..7d278107e7 100644 --- a/src/detection/version/version.c +++ b/src/detection/version/version.c @@ -54,6 +54,8 @@ #define FF_SYSNAME "NetBSD" #elif defined(__HAIKU__) #define FF_SYSNAME "Haiku" +#elif defined(__GNU__) + #define FF_SYSNAME "GNU" #else #define FF_SYSNAME "Unknown" #endif diff --git a/src/fastfetch.c b/src/fastfetch.c index 56818ca87c..cd8210cd33 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -65,15 +65,17 @@ static void printCommandFormatHelp(const char* command) { if (baseInfo->formatArgs.count > 0) { - printf("--%s-format:\n", type.chars); + FF_STRBUF_AUTO_DESTROY variable = ffStrbufCreate(); + printf("-- In config file: { \"type\": \"%s\", \"format\": \"{}\" }\n", type.chars); printf("Sets the format string for %s output.\n", baseInfo->name); - puts("To see how a format string is constructed, take a look at \"fastfetch --help format\"."); - puts("The following values are passed:"); + puts("To see how a format string is constructed, take a look at https://github.com/fastfetch-cli/fastfetch/wiki/Format-String-Guide."); + puts("The following variables are passed:"); for (unsigned i = 0; i < baseInfo->formatArgs.count; i++) { const FFModuleFormatArg* arg = &baseInfo->formatArgs.args[i]; - printf("%16s {%u}: %s\n", arg->name, i + 1, arg->desc); + ffStrbufSetF(&variable, "{%s}", arg->name); + printf("%20s: %s\n", variable.chars, arg->desc); } } else @@ -376,13 +378,12 @@ static void listModules(bool pretty) } } -static bool parseJsoncFile(const char* path, bool strictJson) +static bool parseJsoncFile(const char* path, yyjson_read_flag flg) { assert(!instance.state.configDoc); { yyjson_read_err error; - yyjson_read_flag flg = strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS; instance.state.configDoc = path ? yyjson_read_file(path, flg, NULL, &error) : yyjson_read_fp(stdin, flg, NULL, &error); @@ -434,6 +435,12 @@ static void generateConfigFile(bool force, const char* filePath, bool fullConfig { if (!filePath) { + if (instance.state.platform.configDirs.length == 0) + { + fprintf(stderr, "Error: No config directory found to generate config file in. Use --gen-config to specify a path\n"); + exit(477); + } + ffStrbufSet(&instance.state.genConfigPath, FF_LIST_GET(FFstrbuf, instance.state.platform.configDirs, 0)); ffStrbufAppendS(&instance.state.genConfigPath, "fastfetch/config.jsonc"); } @@ -480,11 +487,19 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateS(value); bool strictJson = ffStrbufEndsWithIgnCaseS(&absolutePath, ".json"); - bool needExtension = !strictJson && !ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc"); + bool jsonc = !strictJson && ffStrbufEndsWithIgnCaseS(&absolutePath, ".jsonc"); + bool json5 = !strictJson && !jsonc && ffStrbufEndsWithIgnCaseS(&absolutePath, ".json5"); + bool needExtension = !strictJson && !jsonc && !json5; if (needExtension) ffStrbufAppendS(&absolutePath, ".jsonc"); - if (parseJsoncFile(absolutePath.chars, strictJson)) return; + yyjson_read_flag flag = strictJson + ? 0 + : jsonc + ? YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS + : YYJSON_READ_JSON5; + + if (parseJsoncFile(absolutePath.chars, flag)) return; //Try to load as a relative path @@ -496,7 +511,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val if (needExtension) ffStrbufAppendS(&absolutePath, ".jsonc"); - if (parseJsoncFile(absolutePath.chars, strictJson)) return; + if (parseJsoncFile(absolutePath.chars, flag)) return; } //Try to load as a relative path with the directory of fastfetch binary @@ -511,7 +526,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val ffStrbufAppendS(&absolutePath, value); if (needExtension) ffStrbufAppendS(&absolutePath, ".jsonc"); - if (parseJsoncFile(absolutePath.chars, strictJson)) return; + if (parseJsoncFile(absolutePath.chars, flag)) return; // Try {exePath}/presets/ ffStrbufSubstrBefore(&absolutePath, lastSlash); @@ -519,7 +534,7 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val ffStrbufAppendS(&absolutePath, value); if (needExtension) ffStrbufAppendS(&absolutePath, ".jsonc"); - if (parseJsoncFile(absolutePath.chars, strictJson)) return; + if (parseJsoncFile(absolutePath.chars, flag)) return; } //File not found @@ -685,7 +700,12 @@ static void parseConfigFiles(void) uint32_t dirLength = dir->length; ffStrbufAppendS(dir, "fastfetch/config.jsonc"); - bool success = parseJsoncFile(dir->chars, false); + bool success = parseJsoncFile(dir->chars, YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS); + ffStrbufSubstrBefore(dir, dirLength); + if (success) return; + + ffStrbufAppendS(dir, "fastfetch/config.json5"); + success = parseJsoncFile(dir->chars, YYJSON_READ_JSON5); ffStrbufSubstrBefore(dir, dirLength); if (success) return; } @@ -776,7 +796,11 @@ static void writeConfigFile(FFdata* data) exit(1); } if (ffWriteFileData(filename->chars, len, str)) - printf("The generated config file has been written in `%s`\n", filename->chars); + { + printf("✓ Configuration file generated: `%s`\n" + "* Tip: Use a JSON schema-aware editor for better editing experience\n" + "* Documentation: https://github.com/fastfetch-cli/fastfetch/wiki/Configuration\n", filename->chars); + } else { printf("Error: failed to write file in `%s`\n", filename->chars); diff --git a/src/fastfetch.h b/src/fastfetch.h index ea4d14621c..f38bb864d3 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -15,6 +15,7 @@ #define __attribute__(x) #endif +#include "util/arrayUtils.h" #include "util/FFstrbuf.h" #include "util/FFlist.h" #include "util/platform/FFPlatform.h" @@ -24,17 +25,6 @@ #include "options/display.h" #include "options/general.h" -#ifdef __has_builtin - #if __has_builtin(__is_array) - #define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) - #elif __has_builtin(__builtin_types_compatible_p) - #define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) - #endif -#endif -#ifndef ARRAY_SIZE - #define ARRAY_SIZE(x) ((uint32_t) (sizeof(x) / sizeof(*(x)))) -#endif - typedef struct FFconfig { diff --git a/src/logo/ascii/aerynos.txt b/src/logo/ascii/aerynos.txt new file mode 100644 index 0000000000..3078dac39a --- /dev/null +++ b/src/logo/ascii/aerynos.txt @@ -0,0 +1,16 @@ + ;llll. + 0MMMMMM: + NMMMMMMMMd + .@ .cccccccccoWMMMMMMMMMM0 + @@ .MMMMMMMMMMMMMMMMMMMMMMMN + OMMMMMMMMMMMMMMMW. +.@ .MMMMMMMMMMMMMMMMMMM. +.@ dMMMMMMMMMMMMMMMMMMl OMMMMMMMMk + .OWMMMMM; dMMMMMMMMMk + .MMMMMMMMMk + @@ ooooooooooooooooooo .MMMMMMMMMN + .@ oooooMMMMMMMMMMMMP NMMMMMMMMW. + KMMMMMMMMMM. 0MMMMMMMMM: + NMMMMMMMMM. dMMMMMMMMMd + .WMMMMMMMW. XMMMMMMMMO + .MMMMMMMk. xMMMMMMMMX \ No newline at end of file diff --git a/src/logo/builtin.c b/src/logo/builtin.c index c7608fbfbe..4de3cf0324 100644 --- a/src/logo/builtin.c +++ b/src/logo/builtin.c @@ -39,6 +39,15 @@ static const FFlogo A[] = { FF_COLOR_FG_256 "36", }, }, + // Aeon + { + .names = {"AerynOS"}, + .lines = FASTFETCH_DATATEXT_LOGO_AERYNOS, + .colors = { + FF_COLOR_FG_DEFAULT, + FF_COLOR_FG_MAGENTA, + }, + }, // Afterglow { .names = {"Afterglow"}, diff --git a/src/logo/logo.c b/src/logo/logo.c index c2c4acbbf1..cc6814b168 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -114,7 +114,7 @@ static bool ffLogoPrintCharsRaw(const char* data, size_t length, bool printError static uint32_t logoAppendChars(const char* data, bool doColorReplacement, FFstrbuf* result) { FFOptionsLogo* options = &instance.config.logo; - uint32_t currentlineLength = 0; + uint32_t currentlineLength = options->width; uint32_t logoHeight = 0; if (result) @@ -260,7 +260,7 @@ static uint32_t logoAppendChars(const char* data, bool doColorReplacement, FFstr if(currentlineLength > instance.state.logoWidth) instance.state.logoWidth = currentlineLength; - return logoHeight; + return options->height > logoHeight ? options->height : logoHeight; } void ffLogoPrintChars(const char* data, bool doColorReplacement) diff --git a/src/modules/battery/battery.c b/src/modules/battery/battery.c index 99bf868731..dc87a32693 100644 --- a/src/modules/battery/battery.c +++ b/src/modules/battery/battery.c @@ -125,7 +125,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin } } -void ffPrintBattery(FFBatteryOptions* options) +bool ffPrintBattery(FFBatteryOptions* options) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBatteryResult)); @@ -134,12 +134,12 @@ void ffPrintBattery(FFBatteryOptions* options) if (error) { ffPrintError(FF_BATTERY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(results.length == 0) { ffPrintError(FF_BATTERY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "No batteries found"); - return; + return false; } for(uint32_t i = 0; i < results.length; i++) @@ -157,6 +157,7 @@ void ffPrintBattery(FFBatteryOptions* options) ffStrbufDestroy(&result->serial); ffStrbufDestroy(&result->manufactureDate); } + return true; } void ffParseBatteryJsonObject(FFBatteryOptions* options, yyjson_val* module) @@ -198,7 +199,7 @@ void ffGenerateBatteryJsonConfig(FFBatteryOptions* options, yyjson_mut_doc* doc, ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBatteryResult)); @@ -206,7 +207,7 @@ void ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc, if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -241,6 +242,8 @@ void ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc, ffStrbufDestroy(&battery->status); ffStrbufDestroy(&battery->serial); } + + return true; } void ffInitBatteryOptions(FFBatteryOptions* options) diff --git a/src/modules/battery/battery.h b/src/modules/battery/battery.h index 820a7bc7cf..16ad027d80 100644 --- a/src/modules/battery/battery.h +++ b/src/modules/battery/battery.h @@ -4,7 +4,7 @@ #define FF_BATTERY_MODULE_NAME "Battery" -void ffPrintBattery(FFBatteryOptions* options); +bool ffPrintBattery(FFBatteryOptions* options); void ffInitBatteryOptions(FFBatteryOptions* options); void ffDestroyBatteryOptions(FFBatteryOptions* options); diff --git a/src/modules/bios/bios.c b/src/modules/bios/bios.c index d2f59629e0..4eee627302 100644 --- a/src/modules/bios/bios.c +++ b/src/modules/bios/bios.c @@ -4,8 +4,9 @@ #include "modules/bios/bios.h" #include "util/stringUtils.h" -void ffPrintBios(FFBiosOptions* options) +bool ffPrintBios(FFBiosOptions* options) { + bool success = false; FFBiosResult bios; ffStrbufInit(&bios.date); ffStrbufInit(&bios.release); @@ -66,6 +67,7 @@ void ffPrintBios(FFBiosOptions* options) FF_FORMAT_ARG(bios.type, "type"), })); } + success = true; exit: ffStrbufDestroy(&bios.date); @@ -73,6 +75,8 @@ void ffPrintBios(FFBiosOptions* options) ffStrbufDestroy(&bios.vendor); ffStrbufDestroy(&bios.version); ffStrbufDestroy(&bios.type); + + return success; } void ffParseBiosJsonObject(FFBiosOptions* options, yyjson_val* module) @@ -93,8 +97,9 @@ void ffGenerateBiosJsonConfig(FFBiosOptions* options, yyjson_mut_doc* doc, yyjso ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFBiosResult bios; ffStrbufInit(&bios.date); ffStrbufInit(&bios.release); @@ -116,6 +121,7 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &bios.vendor); yyjson_mut_obj_add_strbuf(doc, obj, "version", &bios.version); yyjson_mut_obj_add_strbuf(doc, obj, "type", &bios.type); + success = true; exit: ffStrbufDestroy(&bios.date); @@ -123,6 +129,7 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut ffStrbufDestroy(&bios.vendor); ffStrbufDestroy(&bios.version); ffStrbufDestroy(&bios.type); + return success; } void ffInitBiosOptions(FFBiosOptions* options) diff --git a/src/modules/bios/bios.h b/src/modules/bios/bios.h index 22b35edda4..d3b077e4d1 100644 --- a/src/modules/bios/bios.h +++ b/src/modules/bios/bios.h @@ -4,7 +4,7 @@ #define FF_BIOS_MODULE_NAME "BIOS" -void ffPrintBios(FFBiosOptions* options); +bool ffPrintBios(FFBiosOptions* options); void ffInitBiosOptions(FFBiosOptions* options); void ffDestroyBiosOptions(FFBiosOptions* options); diff --git a/src/modules/bluetooth/bluetooth.c b/src/modules/bluetooth/bluetooth.c index b5e05f21bc..ad6bcd0640 100644 --- a/src/modules/bluetooth/bluetooth.c +++ b/src/modules/bluetooth/bluetooth.c @@ -56,7 +56,7 @@ static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* de } } -void ffPrintBluetooth(FFBluetoothOptions* options) +bool ffPrintBluetooth(FFBluetoothOptions* options) { FF_LIST_AUTO_DESTROY devices = ffListCreate(sizeof (FFBluetoothResult)); const char* error = ffDetectBluetooth(options, &devices); @@ -64,29 +64,28 @@ void ffPrintBluetooth(FFBluetoothOptions* options) if(error) { ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); + return false; } - else - { - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothResult*)); - FF_LIST_FOR_EACH(FFBluetoothResult, device, devices) - { - if(!device->connected && !options->showDisconnected) - continue; + FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFBluetoothResult*)); - *(FFBluetoothResult**)ffListAdd(&filtered) = device; - } + FF_LIST_FOR_EACH(FFBluetoothResult, device, devices) + { + if(!device->connected && !options->showDisconnected) + continue; - if(filtered.length == 0) - { - ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No bluetooth devices found"); - } + *(FFBluetoothResult**)ffListAdd(&filtered) = device; + } - for(uint32_t i = 0; i < filtered.length; i++) - { - uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1); - printDevice(options, *FF_LIST_GET(FFBluetoothResult*, filtered, i), index); - } + if(filtered.length == 0) + { + ffPrintError(FF_BLUETOOTH_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No bluetooth devices found"); + } + + for(uint32_t i = 0; i < filtered.length; i++) + { + uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1); + printDevice(options, *FF_LIST_GET(FFBluetoothResult*, filtered, i), index); } FF_LIST_FOR_EACH(FFBluetoothResult, device, devices) @@ -95,6 +94,7 @@ void ffPrintBluetooth(FFBluetoothOptions* options) ffStrbufDestroy(&device->type); ffStrbufDestroy(&device->address); } + return true; } void ffParseBluetoothJsonObject(FFBluetoothOptions* options, yyjson_val* module) @@ -128,7 +128,7 @@ void ffGenerateBluetoothJsonConfig(FFBluetoothOptions* options, yyjson_mut_doc* ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBluetoothResult)); @@ -136,7 +136,7 @@ void ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -157,6 +157,7 @@ void ffGenerateBluetoothJsonResult(FFBluetoothOptions* options, yyjson_mut_doc* ffStrbufDestroy(&device->type); ffStrbufDestroy(&device->address); } + return true; } void ffInitBluetoothOptions(FFBluetoothOptions* options) diff --git a/src/modules/bluetooth/bluetooth.h b/src/modules/bluetooth/bluetooth.h index 889be9690d..d9aa059e37 100644 --- a/src/modules/bluetooth/bluetooth.h +++ b/src/modules/bluetooth/bluetooth.h @@ -4,7 +4,7 @@ #define FF_BLUETOOTH_MODULE_NAME "Bluetooth" -void ffPrintBluetooth(FFBluetoothOptions* options); +bool ffPrintBluetooth(FFBluetoothOptions* options); void ffInitBluetoothOptions(FFBluetoothOptions* options); void ffDestroyBluetoothOptions(FFBluetoothOptions* options); diff --git a/src/modules/bluetoothradio/bluetoothradio.c b/src/modules/bluetoothradio/bluetoothradio.c index 810b5f438e..b127f93491 100644 --- a/src/modules/bluetoothradio/bluetoothradio.c +++ b/src/modules/bluetoothradio/bluetoothradio.c @@ -67,7 +67,7 @@ static void printDevice(FFBluetoothRadioOptions* options, const FFBluetoothRadio } } -void ffPrintBluetoothRadio(FFBluetoothRadioOptions* options) +bool ffPrintBluetoothRadio(FFBluetoothRadioOptions* options) { FF_LIST_AUTO_DESTROY radios = ffListCreate(sizeof (FFBluetoothRadioResult)); const char* error = ffDetectBluetoothRadio(&radios); @@ -75,26 +75,25 @@ void ffPrintBluetoothRadio(FFBluetoothRadioOptions* options) if(error) { ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); + return false; } - else + + uint8_t index = 0; + FF_LIST_FOR_EACH(FFBluetoothRadioResult, radio, radios) + { + if (!radio->enabled) + continue; + + index++; + printDevice(options, radio, index); + } + + if (index == 0) { - uint8_t index = 0; - FF_LIST_FOR_EACH(FFBluetoothRadioResult, radio, radios) - { - if (!radio->enabled) - continue; - - index++; - printDevice(options, radio, index); - } - - if (index == 0) - { - if (radios.length > 0) - ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Bluetooth radios found but none enabled"); - else - ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected"); - } + if (radios.length > 0) + ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Bluetooth radios found but none enabled"); + else + ffPrintError(FF_BLUETOOTHRADIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected"); } FF_LIST_FOR_EACH(FFBluetoothRadioResult, radio, radios) @@ -103,6 +102,7 @@ void ffPrintBluetoothRadio(FFBluetoothRadioOptions* options) ffStrbufDestroy(&radio->address); ffStrbufDestroy(&radio->vendor); } + return true; } void ffParseBluetoothRadioJsonObject(FFBluetoothRadioOptions* options, yyjson_val* module) @@ -123,7 +123,7 @@ void ffGenerateBluetoothRadioJsonConfig(FFBluetoothRadioOptions* options, yyjson ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateBluetoothRadioJsonResult(FF_MAYBE_UNUSED FFBluetoothRadioOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateBluetoothRadioJsonResult(FF_MAYBE_UNUSED FFBluetoothRadioOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBluetoothRadioResult)); @@ -131,7 +131,7 @@ void ffGenerateBluetoothRadioJsonResult(FF_MAYBE_UNUSED FFBluetoothRadioOptions* if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -161,6 +161,8 @@ void ffGenerateBluetoothRadioJsonResult(FF_MAYBE_UNUSED FFBluetoothRadioOptions* ffStrbufDestroy(&radio->address); ffStrbufDestroy(&radio->vendor); } + + return true; } void ffInitBluetoothRadioOptions(FFBluetoothRadioOptions* options) diff --git a/src/modules/bluetoothradio/bluetoothradio.h b/src/modules/bluetoothradio/bluetoothradio.h index 9d63525fe9..4707f1ba5e 100644 --- a/src/modules/bluetoothradio/bluetoothradio.h +++ b/src/modules/bluetoothradio/bluetoothradio.h @@ -4,7 +4,7 @@ #define FF_BLUETOOTHRADIO_MODULE_NAME "BluetoothRadio" -void ffPrintBluetoothRadio(FFBluetoothRadioOptions* options); +bool ffPrintBluetoothRadio(FFBluetoothRadioOptions* options); void ffInitBluetoothRadioOptions(FFBluetoothRadioOptions* options); void ffDestroyBluetoothRadioOptions(FFBluetoothRadioOptions* options); diff --git a/src/modules/board/board.c b/src/modules/board/board.c index a9cf573f67..9f572e69fa 100644 --- a/src/modules/board/board.c +++ b/src/modules/board/board.c @@ -4,8 +4,9 @@ #include "modules/board/board.h" #include "util/stringUtils.h" -void ffPrintBoard(FFBoardOptions* options) +bool ffPrintBoard(FFBoardOptions* options) { + bool success = false; FFBoardResult result; ffStrbufInit(&result.name); ffStrbufInit(&result.vendor); @@ -42,12 +43,14 @@ void ffPrintBoard(FFBoardOptions* options) FF_FORMAT_ARG(result.serial, "serial"), })); } + success = true; exit: ffStrbufDestroy(&result.name); ffStrbufDestroy(&result.vendor); ffStrbufDestroy(&result.version); ffStrbufDestroy(&result.serial); + return success; } void ffParseBoardJsonObject(FFBoardOptions* options, yyjson_val* module) @@ -68,8 +71,9 @@ void ffGenerateBoardJsonConfig(FFBoardOptions* options, yyjson_mut_doc* doc, yyj ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateBoardJsonResult(FF_MAYBE_UNUSED FFBoardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateBoardJsonResult(FF_MAYBE_UNUSED FFBoardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFBoardResult board; ffStrbufInit(&board.name); ffStrbufInit(&board.vendor); @@ -95,12 +99,14 @@ void ffGenerateBoardJsonResult(FF_MAYBE_UNUSED FFBoardOptions* options, yyjson_m yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &board.vendor); yyjson_mut_obj_add_strbuf(doc, obj, "version", &board.version); yyjson_mut_obj_add_strbuf(doc, obj, "serial", &board.serial); + success = true; exit: ffStrbufDestroy(&board.name); ffStrbufDestroy(&board.vendor); ffStrbufDestroy(&board.version); ffStrbufDestroy(&board.serial); + return success; } void ffInitBoardOptions(FFBoardOptions* options) diff --git a/src/modules/board/board.h b/src/modules/board/board.h index 3fdea021de..eae3d73c11 100644 --- a/src/modules/board/board.h +++ b/src/modules/board/board.h @@ -4,7 +4,7 @@ #define FF_BOARD_MODULE_NAME "Board" -void ffPrintBoard(FFBoardOptions* options); +bool ffPrintBoard(FFBoardOptions* options); void ffInitBoardOptions(FFBoardOptions* options); void ffDestroyBoardOptions(FFBoardOptions* options); diff --git a/src/modules/bootmgr/bootmgr.c b/src/modules/bootmgr/bootmgr.c index 350499530e..d953a15032 100644 --- a/src/modules/bootmgr/bootmgr.c +++ b/src/modules/bootmgr/bootmgr.c @@ -4,8 +4,9 @@ #include "modules/bootmgr/bootmgr.h" #include "util/stringUtils.h" -void ffPrintBootmgr(FFBootmgrOptions* options) +bool ffPrintBootmgr(FFBootmgrOptions* options) { + bool success = false; FFBootmgrResult bootmgr = { .name = ffStrbufCreate(), .firmware = ffStrbufCreate(), @@ -16,38 +17,44 @@ void ffPrintBootmgr(FFBootmgrOptions* options) if(error) { ffPrintError(FF_BOOTMGR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; - } - - FF_STRBUF_AUTO_DESTROY firmwareName = ffStrbufCreateCopy(&bootmgr.firmware); - #ifndef __APPLE__ - ffStrbufSubstrAfterLastC(&firmwareName, '\\'); - #else - ffStrbufSubstrAfterLastC(&firmwareName, '/'); - #endif - - if(options->moduleArgs.outputFormat.length == 0) - { - ffPrintLogoAndKey(FF_BOOTMGR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - ffStrbufWriteTo(&bootmgr.name, stdout); - if (firmwareName.length > 0) - printf(" - %s\n", firmwareName.chars); - else - putchar('\n'); + goto exit; } else { - FF_PRINT_FORMAT_CHECKED(FF_BOOTMGR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { - FF_FORMAT_ARG(bootmgr.name, "name"), - FF_FORMAT_ARG(bootmgr.firmware, "firmware-path"), - FF_FORMAT_ARG(firmwareName, "firmware-name"), - FF_FORMAT_ARG(bootmgr.secureBoot, "secure-boot"), - FF_FORMAT_ARG(bootmgr.order, "order"), - })); + FF_STRBUF_AUTO_DESTROY firmwareName = ffStrbufCreateCopy(&bootmgr.firmware); + #ifndef __APPLE__ + ffStrbufSubstrAfterLastC(&firmwareName, '\\'); + #else + ffStrbufSubstrAfterLastC(&firmwareName, '/'); + #endif + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(FF_BOOTMGR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + ffStrbufWriteTo(&bootmgr.name, stdout); + if (firmwareName.length > 0) + printf(" - %s\n", firmwareName.chars); + else + putchar('\n'); + } + else + { + FF_PRINT_FORMAT_CHECKED(FF_BOOTMGR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { + FF_FORMAT_ARG(bootmgr.name, "name"), + FF_FORMAT_ARG(bootmgr.firmware, "firmware-path"), + FF_FORMAT_ARG(firmwareName, "firmware-name"), + FF_FORMAT_ARG(bootmgr.secureBoot, "secure-boot"), + FF_FORMAT_ARG(bootmgr.order, "order"), + })); + } } + success = true; +exit: ffStrbufDestroy(&bootmgr.name); ffStrbufDestroy(&bootmgr.firmware); + + return success; } void ffParseBootmgrJsonObject(FFBootmgrOptions* options, yyjson_val* module) @@ -68,8 +75,9 @@ void ffGenerateBootmgrJsonConfig(FFBootmgrOptions* options, yyjson_mut_doc* doc, ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateBootmgrJsonResult(FF_MAYBE_UNUSED FFBootmgrOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateBootmgrJsonResult(FF_MAYBE_UNUSED FFBootmgrOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFBootmgrResult bootmgr = { .name = ffStrbufCreate(), .firmware = ffStrbufCreate(), @@ -88,10 +96,12 @@ void ffGenerateBootmgrJsonResult(FF_MAYBE_UNUSED FFBootmgrOptions* options, yyjs yyjson_mut_obj_add_strbuf(doc, obj, "firmware", &bootmgr.firmware); yyjson_mut_obj_add_uint(doc, obj, "order", bootmgr.order); yyjson_mut_obj_add_bool(doc, obj, "secureBoot", bootmgr.secureBoot); + success = true; exit: ffStrbufDestroy(&bootmgr.name); ffStrbufDestroy(&bootmgr.firmware); + return success; } void ffInitBootmgrOptions(FFBootmgrOptions* options) diff --git a/src/modules/bootmgr/bootmgr.h b/src/modules/bootmgr/bootmgr.h index 9150f5218d..4f6a2bac25 100644 --- a/src/modules/bootmgr/bootmgr.h +++ b/src/modules/bootmgr/bootmgr.h @@ -4,7 +4,7 @@ #define FF_BOOTMGR_MODULE_NAME "Bootmgr" -void ffPrintBootmgr(FFBootmgrOptions* options); +bool ffPrintBootmgr(FFBootmgrOptions* options); void ffInitBootmgrOptions(FFBootmgrOptions* options); void ffDestroyBootmgrOptions(FFBootmgrOptions* options); diff --git a/src/modules/break/break.c b/src/modules/break/break.c index 34fb4c2add..e3b958782d 100644 --- a/src/modules/break/break.c +++ b/src/modules/break/break.c @@ -2,10 +2,11 @@ #include "logo/logo.h" #include "modules/break/break.h" -void ffPrintBreak(FF_MAYBE_UNUSED FFBreakOptions* options) +bool ffPrintBreak(FF_MAYBE_UNUSED FFBreakOptions* options) { ffLogoPrintLine(); putchar('\n'); + return true; } void ffParseBreakJsonObject(FF_MAYBE_UNUSED FFBreakOptions* options, FF_MAYBE_UNUSED yyjson_val* module) diff --git a/src/modules/break/break.h b/src/modules/break/break.h index 772d19744b..f611b73790 100644 --- a/src/modules/break/break.h +++ b/src/modules/break/break.h @@ -4,7 +4,7 @@ #define FF_BREAK_MODULE_NAME "Break" -void ffPrintBreak(FFBreakOptions* options); +bool ffPrintBreak(FFBreakOptions* options); void ffInitBreakOptions(FFBreakOptions* options); void ffDestroyBreakOptions(FFBreakOptions* options); diff --git a/src/modules/brightness/brightness.c b/src/modules/brightness/brightness.c index c3855b07d1..cc758e2855 100644 --- a/src/modules/brightness/brightness.c +++ b/src/modules/brightness/brightness.c @@ -5,7 +5,7 @@ #include "modules/brightness/brightness.h" #include "util/stringUtils.h" -void ffPrintBrightness(FFBrightnessOptions* options) +bool ffPrintBrightness(FFBrightnessOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult)); @@ -14,13 +14,13 @@ void ffPrintBrightness(FFBrightnessOptions* options) if(error) { ffPrintError(FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(result.length == 0) { ffPrintError(FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No result is detected."); - return; + return false; } FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type; @@ -40,7 +40,7 @@ void ffPrintBrightness(FFBrightnessOptions* options) ffPrintLogoAndKey(FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); ffStrbufPutTo(&str, stdout); - return; + return true; } FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate(); @@ -110,6 +110,8 @@ void ffPrintBrightness(FFBrightnessOptions* options) ffStrbufDestroy(&item->name); ++index; } + + return true; } void ffParseBrightnessJsonObject(FFBrightnessOptions* options, yyjson_val* module) @@ -151,7 +153,7 @@ void ffGenerateBrightnessJsonConfig(FFBrightnessOptions* options, yyjson_mut_doc yyjson_mut_obj_add_bool(doc, module, "compact", options->compact); } -void ffGenerateBrightnessJsonResult(FF_MAYBE_UNUSED FFBrightnessOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateBrightnessJsonResult(FF_MAYBE_UNUSED FFBrightnessOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFBrightnessResult)); @@ -160,7 +162,7 @@ void ffGenerateBrightnessJsonResult(FF_MAYBE_UNUSED FFBrightnessOptions* options if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_arr(doc); @@ -180,6 +182,8 @@ void ffGenerateBrightnessJsonResult(FF_MAYBE_UNUSED FFBrightnessOptions* options { ffStrbufDestroy(&item->name); } + + return true; } void ffInitBrightnessOptions(FFBrightnessOptions* options) diff --git a/src/modules/brightness/brightness.h b/src/modules/brightness/brightness.h index 3b443425f4..085366141f 100644 --- a/src/modules/brightness/brightness.h +++ b/src/modules/brightness/brightness.h @@ -4,7 +4,7 @@ #define FF_BRIGHTNESS_MODULE_NAME "Brightness" -void ffPrintBrightness(FFBrightnessOptions* options); +bool ffPrintBrightness(FFBrightnessOptions* options); void ffInitBrightnessOptions(FFBrightnessOptions* options); void ffDestroyBrightnessOptions(FFBrightnessOptions* options); diff --git a/src/modules/btrfs/btrfs.c b/src/modules/btrfs/btrfs.c index e337d4c72b..e021d8035d 100644 --- a/src/modules/btrfs/btrfs.c +++ b/src/modules/btrfs/btrfs.c @@ -97,7 +97,7 @@ static void printBtrfs(FFBtrfsOptions* options, FFBtrfsResult* result, uint8_t i } } -void ffPrintBtrfs(FFBtrfsOptions* options) +bool ffPrintBtrfs(FFBtrfsOptions* options) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBtrfsResult)); @@ -106,12 +106,12 @@ void ffPrintBtrfs(FFBtrfsOptions* options) if (error) { ffPrintError(FF_BTRFS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(results.length == 0) { ffPrintError(FF_BTRFS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "No btrfs drive found"); - return; + return false; } for(uint32_t i = 0; i < results.length; i++) @@ -128,6 +128,8 @@ void ffPrintBtrfs(FFBtrfsOptions* options) ffStrbufDestroy(&result->devices); ffStrbufDestroy(&result->features); } + + return true; } void ffParseBtrfsJsonObject(FFBtrfsOptions* options, yyjson_val* module) @@ -153,7 +155,7 @@ void ffGenerateBtrfsJsonConfig(FFBtrfsOptions* options, yyjson_mut_doc* doc, yyj ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateBtrfsJsonResult(FF_MAYBE_UNUSED FFBtrfsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateBtrfsJsonResult(FF_MAYBE_UNUSED FFBtrfsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFBtrfsResult)); @@ -161,7 +163,7 @@ void ffGenerateBtrfsJsonResult(FF_MAYBE_UNUSED FFBtrfsOptions* options, yyjson_m if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -195,6 +197,8 @@ void ffGenerateBtrfsJsonResult(FF_MAYBE_UNUSED FFBtrfsOptions* options, yyjson_m ffStrbufDestroy(&btrfs->devices); ffStrbufDestroy(&btrfs->features); } + + return true; } void ffInitBtrfsOptions(FFBtrfsOptions* options) diff --git a/src/modules/btrfs/btrfs.h b/src/modules/btrfs/btrfs.h index 3cd77df322..fc0c6abf4b 100644 --- a/src/modules/btrfs/btrfs.h +++ b/src/modules/btrfs/btrfs.h @@ -4,7 +4,7 @@ #define FF_BTRFS_MODULE_NAME "Btrfs" -void ffPrintBtrfs(FFBtrfsOptions* options); +bool ffPrintBtrfs(FFBtrfsOptions* options); void ffInitBtrfsOptions(FFBtrfsOptions* options); void ffDestroyBtrfsOptions(FFBtrfsOptions* options); diff --git a/src/modules/camera/camera.c b/src/modules/camera/camera.c index d12d7a6622..19c28e95aa 100644 --- a/src/modules/camera/camera.c +++ b/src/modules/camera/camera.c @@ -36,7 +36,7 @@ static void printDevice(FFCameraOptions* options, const FFCameraResult* device, } } -void ffPrintCamera(FFCameraOptions* options) +bool ffPrintCamera(FFCameraOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFCameraResult)); const char* error = ffDetectCamera(&result); @@ -44,13 +44,13 @@ void ffPrintCamera(FFCameraOptions* options) if (error) { ffPrintError(FF_CAMERA_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if (result.length == 0) { ffPrintError(FF_CAMERA_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No camera found"); - return; + return false; } for(uint32_t i = 0; i < result.length; i++) @@ -65,6 +65,8 @@ void ffPrintCamera(FFCameraOptions* options) ffStrbufDestroy(&dev->id); ffStrbufDestroy(&dev->colorspace); } + + return true; } void ffParseCameraJsonObject(FFCameraOptions* options, yyjson_val* module) @@ -85,7 +87,7 @@ void ffGenerateCameraJsonConfig(FFCameraOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateCameraJsonResult(FF_MAYBE_UNUSED FFCameraOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateCameraJsonResult(FF_MAYBE_UNUSED FFCameraOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFCameraResult)); const char* error = ffDetectCamera(&result); @@ -93,7 +95,7 @@ void ffGenerateCameraJsonResult(FF_MAYBE_UNUSED FFCameraOptions* options, yyjson if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -115,6 +117,8 @@ void ffGenerateCameraJsonResult(FF_MAYBE_UNUSED FFCameraOptions* options, yyjson ffStrbufDestroy(&dev->id); ffStrbufDestroy(&dev->colorspace); } + + return true; } void ffInitCameraOptions(FFCameraOptions* options) diff --git a/src/modules/camera/camera.h b/src/modules/camera/camera.h index 1de49bdcaf..e8b3560400 100644 --- a/src/modules/camera/camera.h +++ b/src/modules/camera/camera.h @@ -4,7 +4,7 @@ #define FF_CAMERA_MODULE_NAME "Camera" -void ffPrintCamera(FFCameraOptions* options); +bool ffPrintCamera(FFCameraOptions* options); void ffInitCameraOptions(FFCameraOptions* options); void ffDestroyCameraOptions(FFCameraOptions* options); diff --git a/src/modules/chassis/chassis.c b/src/modules/chassis/chassis.c index e1344ab2bd..068e51cc07 100644 --- a/src/modules/chassis/chassis.c +++ b/src/modules/chassis/chassis.c @@ -4,8 +4,10 @@ #include "modules/chassis/chassis.h" #include "util/stringUtils.h" -void ffPrintChassis(FFChassisOptions* options) +bool ffPrintChassis(FFChassisOptions* options) { + bool success = false; + FFChassisResult result; ffStrbufInit(&result.type); ffStrbufInit(&result.vendor); @@ -43,12 +45,14 @@ void ffPrintChassis(FFChassisOptions* options) FF_FORMAT_ARG(result.serial, "serial"), })); } + success = true; exit: ffStrbufDestroy(&result.type); ffStrbufDestroy(&result.vendor); ffStrbufDestroy(&result.version); ffStrbufDestroy(&result.serial); + return success; } void ffParseChassisJsonObject(FFChassisOptions* options, yyjson_val* module) @@ -69,8 +73,9 @@ void ffGenerateChassisJsonConfig(FFChassisOptions* options, yyjson_mut_doc* doc, ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateChassisJsonResult(FF_MAYBE_UNUSED FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateChassisJsonResult(FF_MAYBE_UNUSED FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFChassisResult result; ffStrbufInit(&result.type); ffStrbufInit(&result.vendor); @@ -96,12 +101,14 @@ void ffGenerateChassisJsonResult(FF_MAYBE_UNUSED FFChassisOptions* options, yyjs yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &result.vendor); yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version); yyjson_mut_obj_add_strbuf(doc, obj, "serial", &result.serial); + success = true; exit: ffStrbufDestroy(&result.type); ffStrbufDestroy(&result.vendor); ffStrbufDestroy(&result.version); ffStrbufDestroy(&result.serial); + return success; } void ffInitChassisOptions(FFChassisOptions* options) diff --git a/src/modules/chassis/chassis.h b/src/modules/chassis/chassis.h index 47a80d6fff..d3ba4892cb 100644 --- a/src/modules/chassis/chassis.h +++ b/src/modules/chassis/chassis.h @@ -4,7 +4,7 @@ #define FF_CHASSIS_MODULE_NAME "Chassis" -void ffPrintChassis(FFChassisOptions* options); +bool ffPrintChassis(FFChassisOptions* options); void ffInitChassisOptions(FFChassisOptions* options); void ffDestroyChassisOptions(FFChassisOptions* options); diff --git a/src/modules/colors/colors.c b/src/modules/colors/colors.c index 27dae0f40f..e3e9e04a74 100644 --- a/src/modules/colors/colors.c +++ b/src/modules/colors/colors.c @@ -15,7 +15,7 @@ static inline uint8_t max(uint8_t a, uint8_t b) return a > b ? a : b; } -void ffPrintColors(FFColorsOptions* options) +bool ffPrintColors(FFColorsOptions* options) { bool flag = false; @@ -122,7 +122,10 @@ void ffPrintColors(FFColorsOptions* options) if (!flag) { ffPrintError(FF_COLORS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "Nothing to print"); + return false; } + + return true; } void ffParseColorsJsonObject(FFColorsOptions* options, yyjson_val* module) diff --git a/src/modules/colors/colors.h b/src/modules/colors/colors.h index 3581dabd9d..c88ccd7a50 100644 --- a/src/modules/colors/colors.h +++ b/src/modules/colors/colors.h @@ -4,7 +4,7 @@ #define FF_COLORS_MODULE_NAME "Colors" -void ffPrintColors(FFColorsOptions* options); +bool ffPrintColors(FFColorsOptions* options); void ffInitColorsOptions(FFColorsOptions* options); void ffDestroyColorsOptions(FFColorsOptions* options); diff --git a/src/modules/command/command.c b/src/modules/command/command.c index 5a92d3ceaf..a46d771151 100644 --- a/src/modules/command/command.c +++ b/src/modules/command/command.c @@ -4,7 +4,7 @@ #include "modules/command/command.h" #include "util/stringUtils.h" -void ffPrintCommand(FFCommandOptions* options) +bool ffPrintCommand(FFCommandOptions* options) { FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); const char* error = ffProcessAppendStdOut(&result, options->param.length ? (char* const[]){ @@ -21,13 +21,13 @@ void ffPrintCommand(FFCommandOptions* options) if(error) { ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(!result.length) { - ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No result printed"); - return; + ffPrintError(FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No result generated"); + return false; } if (options->moduleArgs.outputFormat.length == 0) @@ -41,6 +41,8 @@ void ffPrintCommand(FFCommandOptions* options) FF_FORMAT_ARG(result, "result") })); } + + return true; } void ffParseCommandJsonObject(FFCommandOptions* options, yyjson_val* module) @@ -85,7 +87,7 @@ void ffGenerateCommandJsonConfig(FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_obj_add_strbuf(doc, module, "text", &options->text); } -void ffGenerateCommandJsonResult(FF_MAYBE_UNUSED FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateCommandJsonResult(FF_MAYBE_UNUSED FFCommandOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); const char* error = ffProcessAppendStdOut(&result, options->param.length ? (char* const[]){ @@ -102,16 +104,18 @@ void ffGenerateCommandJsonResult(FF_MAYBE_UNUSED FFCommandOptions* options, yyjs if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } if(!result.length) { - yyjson_mut_obj_add_str(doc, module, "error", "No result printed"); - return; + yyjson_mut_obj_add_str(doc, module, "error", "No result generated"); + return false; } yyjson_mut_obj_add_strbuf(doc, module, "result", &result); + + return true; } void ffInitCommandOptions(FFCommandOptions* options) diff --git a/src/modules/command/command.h b/src/modules/command/command.h index dd28a8056f..204553747e 100644 --- a/src/modules/command/command.h +++ b/src/modules/command/command.h @@ -4,7 +4,7 @@ #define FF_COMMAND_MODULE_NAME "Command" -void ffPrintCommand(FFCommandOptions* options); +bool ffPrintCommand(FFCommandOptions* options); void ffInitCommandOptions(FFCommandOptions* options); void ffDestroyCommandOptions(FFCommandOptions* options); diff --git a/src/modules/cpu/cpu.c b/src/modules/cpu/cpu.c index 857cdd2ab9..9a9911e337 100644 --- a/src/modules/cpu/cpu.c +++ b/src/modules/cpu/cpu.c @@ -12,8 +12,9 @@ static int sortCores(const FFCPUCore* a, const FFCPUCore* b) return (int)b->freq - (int)a->freq; } -void ffPrintCPU(FFCPUOptions* options) +bool ffPrintCPU(FFCPUOptions* options) { + bool success = false; FFCPUResult cpu = { .temperature = FF_CPU_TEMP_UNSET, .frequencyMax = 0, @@ -109,12 +110,16 @@ void ffPrintCPU(FFCPUOptions* options) FF_FORMAT_ARG(tempStr, "temperature"), FF_FORMAT_ARG(coreTypes, "core-types"), FF_FORMAT_ARG(cpu.packages, "packages"), + FF_FORMAT_ARG(cpu.march, "march"), })); } + success = true; } ffStrbufDestroy(&cpu.name); ffStrbufDestroy(&cpu.vendor); + + return success; } void ffParseCPUJsonObject(FFCPUOptions* options, yyjson_val* module) @@ -154,8 +159,9 @@ void ffGenerateCPUJsonConfig(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_ yyjson_mut_obj_add_bool(doc, module, "showPeCoreCount", options->showPeCoreCount); } -void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFCPUResult cpu = { .temperature = FF_CPU_TEMP_UNSET, .frequencyMax = 0, @@ -205,10 +211,19 @@ void ffGenerateCPUJsonResult(FFCPUOptions* options, yyjson_mut_doc* doc, yyjson_ yyjson_mut_obj_add_real(doc, obj, "temperature", cpu.temperature); else yyjson_mut_obj_add_null(doc, obj, "temperature"); + + if (cpu.march) + yyjson_mut_obj_add_str(doc, obj, "march", cpu.march); + else + yyjson_mut_obj_add_null(doc, obj, "march"); + + success = true; } ffStrbufDestroy(&cpu.name); ffStrbufDestroy(&cpu.vendor); + + return success; } void ffInitCPUOptions(FFCPUOptions* options) @@ -244,5 +259,6 @@ FFModuleBaseInfo ffCPUModuleInfo = { {"Temperature (formatted)", "temperature"}, {"Logical core count grouped by frequency", "core-types"}, {"Processor package count", "packages"}, + {"X86-64 CPU microarchitecture", "march"}, })) }; diff --git a/src/modules/cpu/cpu.h b/src/modules/cpu/cpu.h index c6be135006..33fff6bf5b 100644 --- a/src/modules/cpu/cpu.h +++ b/src/modules/cpu/cpu.h @@ -4,7 +4,7 @@ #define FF_CPU_MODULE_NAME "CPU" -void ffPrintCPU(FFCPUOptions* options); +bool ffPrintCPU(FFCPUOptions* options); void ffInitCPUOptions(FFCPUOptions* options); void ffDestroyCPUOptions(FFCPUOptions* options); diff --git a/src/modules/cpucache/cpucache.c b/src/modules/cpucache/cpucache.c index aeb9422b4e..e3664909a3 100644 --- a/src/modules/cpucache/cpucache.c +++ b/src/modules/cpucache/cpucache.c @@ -101,8 +101,9 @@ static void printCPUCacheCompact(const FFCPUCacheResult* result, FFCPUCacheOptio } } -void ffPrintCPUCache(FFCPUCacheOptions* options) +bool ffPrintCPUCache(FFCPUCacheOptions* options) { + bool success = false; FFCPUCacheResult result = { .caches = { ffListCreate(sizeof(FFCPUCache)), @@ -124,12 +125,15 @@ void ffPrintCPUCache(FFCPUCacheOptions* options) printCPUCacheNormal(&result, options); else printCPUCacheCompact(&result, options); + success = true; exit: ffListDestroy(&result.caches[0]); ffListDestroy(&result.caches[1]); ffListDestroy(&result.caches[2]); ffListDestroy(&result.caches[3]); + + return success; } void ffParseCPUCacheJsonObject(FFCPUCacheOptions* options, yyjson_val* module) @@ -158,8 +162,9 @@ void ffGenerateCPUCacheJsonConfig(FFCPUCacheOptions* options, yyjson_mut_doc* do yyjson_mut_obj_add_bool(doc, module, "compact", options->compact); } -void ffGenerateCPUCacheJsonResult(FF_MAYBE_UNUSED FFCPUCacheOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateCPUCacheJsonResult(FF_MAYBE_UNUSED FFCPUCacheOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFCPUCacheResult result = { .caches = { ffListCreate(sizeof(FFCPUCache)), @@ -199,12 +204,14 @@ void ffGenerateCPUCacheJsonResult(FF_MAYBE_UNUSED FFCPUCacheOptions* options, yy yyjson_mut_obj_add_str(doc, item, "type", typeStr); } } + success = true; exit: ffListDestroy(&result.caches[0]); ffListDestroy(&result.caches[1]); ffListDestroy(&result.caches[2]); ffListDestroy(&result.caches[3]); + return success; } void ffInitCPUCacheOptions(FFCPUCacheOptions* options) diff --git a/src/modules/cpucache/cpucache.h b/src/modules/cpucache/cpucache.h index 22987a8571..5061b44417 100644 --- a/src/modules/cpucache/cpucache.h +++ b/src/modules/cpucache/cpucache.h @@ -4,7 +4,7 @@ #define FF_CPUCACHE_MODULE_NAME "CPUCache" -void ffPrintCPUCache(FFCPUCacheOptions* options); +bool ffPrintCPUCache(FFCPUCacheOptions* options); void ffInitCPUCacheOptions(FFCPUCacheOptions* options); void ffDestroyCPUCacheOptions(FFCPUCacheOptions* options); diff --git a/src/modules/cpuusage/cpuusage.c b/src/modules/cpuusage/cpuusage.c index a86f78e209..8c2ffab57f 100644 --- a/src/modules/cpuusage/cpuusage.c +++ b/src/modules/cpuusage/cpuusage.c @@ -7,7 +7,7 @@ #define FF_CPUUSAGE_DISPLAY_NAME "CPU Usage" -void ffPrintCPUUsage(FFCPUUsageOptions* options) +bool ffPrintCPUUsage(FFCPUUsageOptions* options) { FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double)); const char* error = ffGetCpuUsageResult(options, &percentages); @@ -15,7 +15,7 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options) if(error) { ffPrintError(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } double maxValue = -999, minValue = 999, sumValue = 0; @@ -114,6 +114,8 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options) FF_FORMAT_ARG(minBar, "min-bar"), })); } + + return true; } void ffParseCPUUsageJsonObject(FFCPUUsageOptions* options, yyjson_val* module) @@ -155,7 +157,7 @@ void ffGenerateCPUUsageJsonConfig(FFCPUUsageOptions* options, yyjson_mut_doc* do yyjson_mut_obj_add_uint(doc, module, "waitTime", options->waitTime); } -void ffGenerateCPUUsageJsonResult(FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateCPUUsageJsonResult(FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double)); const char* error = ffGetCpuUsageResult(options, &percentages); @@ -163,13 +165,15 @@ void ffGenerateCPUUsageJsonResult(FFCPUUsageOptions* options, yyjson_mut_doc* do if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* result = yyjson_mut_obj_add_arr(doc, module, "result"); FF_LIST_FOR_EACH(double, percent, percentages) { yyjson_mut_arr_add_real(doc, result, *percent); } + + return true; } void ffInitCPUUsageOptions(FFCPUUsageOptions* options) diff --git a/src/modules/cpuusage/cpuusage.h b/src/modules/cpuusage/cpuusage.h index 221e747784..efa8b485cc 100644 --- a/src/modules/cpuusage/cpuusage.h +++ b/src/modules/cpuusage/cpuusage.h @@ -6,7 +6,7 @@ void ffPrepareCPUUsage(); -void ffPrintCPUUsage(FFCPUUsageOptions* options); +bool ffPrintCPUUsage(FFCPUUsageOptions* options); void ffInitCPUUsageOptions(FFCPUUsageOptions* options); void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options); diff --git a/src/modules/cursor/cursor.c b/src/modules/cursor/cursor.c index d8ce2c26e7..93e14dcbb3 100644 --- a/src/modules/cursor/cursor.c +++ b/src/modules/cursor/cursor.c @@ -4,8 +4,9 @@ #include "modules/cursor/cursor.h" #include "util/stringUtils.h" -void ffPrintCursor(FFCursorOptions* options) +bool ffPrintCursor(FFCursorOptions* options) { + bool success = false; FFCursorResult result; ffStrbufInit(&result.error); ffStrbufInit(&result.theme); @@ -41,11 +42,15 @@ void ffPrintCursor(FFCursorOptions* options) FF_FORMAT_ARG(result.size, "size"), })); } + + success = true; } ffStrbufDestroy(&result.error); ffStrbufDestroy(&result.theme); ffStrbufDestroy(&result.size); + + return success; } void ffParseCursorJsonObject(FFCursorOptions* options, yyjson_val* module) @@ -66,8 +71,9 @@ void ffGenerateCursorJsonConfig(FFCursorOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateCursorJsonResult(FF_MAYBE_UNUSED FFCursorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateCursorJsonResult(FF_MAYBE_UNUSED FFCursorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFCursorResult result; ffStrbufInit(&result.error); ffStrbufInit(&result.theme); @@ -84,11 +90,14 @@ void ffGenerateCursorJsonResult(FF_MAYBE_UNUSED FFCursorOptions* options, yyjson yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); yyjson_mut_obj_add_strbuf(doc, obj, "theme", &result.theme); yyjson_mut_obj_add_strbuf(doc, obj, "size", &result.size); + success = true; } ffStrbufDestroy(&result.error); ffStrbufDestroy(&result.theme); ffStrbufDestroy(&result.size); + + return success; } void ffInitCursorOptions(FFCursorOptions* options) diff --git a/src/modules/cursor/cursor.h b/src/modules/cursor/cursor.h index c74031cfab..e7695692cd 100644 --- a/src/modules/cursor/cursor.h +++ b/src/modules/cursor/cursor.h @@ -4,7 +4,7 @@ #define FF_CURSOR_MODULE_NAME "Cursor" -void ffPrintCursor(FFCursorOptions* options); +bool ffPrintCursor(FFCursorOptions* options); void ffInitCursorOptions(FFCursorOptions* options); void ffDestroyCursorOptions(FFCursorOptions* options); diff --git a/src/modules/custom/custom.c b/src/modules/custom/custom.c index 4c2b8137d3..bee9ed21fd 100644 --- a/src/modules/custom/custom.c +++ b/src/modules/custom/custom.c @@ -4,9 +4,10 @@ #include "util/textModifier.h" #include "util/stringUtils.h" -void ffPrintCustom(FFCustomOptions* options) +bool ffPrintCustom(FFCustomOptions* options) { ffPrintFormat(FF_CUSTOM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, 0, ((FFformatarg[]) {})); + return true; } void ffGenerateCustomJsonConfig(FFCustomOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) diff --git a/src/modules/custom/custom.h b/src/modules/custom/custom.h index f4dfefab0c..628a621a32 100644 --- a/src/modules/custom/custom.h +++ b/src/modules/custom/custom.h @@ -4,7 +4,7 @@ #define FF_CUSTOM_MODULE_NAME "Custom" -void ffPrintCustom(FFCustomOptions* options); +bool ffPrintCustom(FFCustomOptions* options); void ffInitCustomOptions(FFCustomOptions* options); void ffDestroyCustomOptions(FFCustomOptions* options); diff --git a/src/modules/datetime/datetime.c b/src/modules/datetime/datetime.c index 213b3bfdcc..233f11cf75 100644 --- a/src/modules/datetime/datetime.c +++ b/src/modules/datetime/datetime.c @@ -38,7 +38,7 @@ typedef struct FFDateTimeResult char timezoneName[FASTFETCH_STRBUF_DEFAULT_ALLOC]; } FFDateTimeResult; -void ffPrintDateTimeFormat(struct tm* tm, const FFModuleArgs* moduleArgs) +static void printDateTimeFormat(struct tm* tm, const FFModuleArgs* moduleArgs) { FFDateTimeResult result; @@ -93,7 +93,7 @@ void ffPrintDateTimeFormat(struct tm* tm, const FFModuleArgs* moduleArgs) })); } -void ffPrintDateTime(FFDateTimeOptions* options) +bool ffPrintDateTime(FFDateTimeOptions* options) { uint64_t msNow = ffTimeGetNow(); time_t sNow = (time_t) (msNow / 1000); @@ -101,20 +101,21 @@ void ffPrintDateTime(FFDateTimeOptions* options) if(options->moduleArgs.outputFormat.length > 0) { - ffPrintDateTimeFormat(tm, &options->moduleArgs); - return; + printDateTimeFormat(tm, &options->moduleArgs); + return true; } char buffer[32]; if (strftime(buffer, ARRAY_SIZE(buffer), "%F %T", tm) == 0) //yyyy-MM-dd HH:mm:ss { ffPrintError(FF_DATETIME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "strftime() failed"); - return; + return false; } ffPrintLogoAndKey(FF_DATETIME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); puts(buffer); + return true; } void ffParseDateTimeJsonObject(FFDateTimeOptions* options, yyjson_val* module) @@ -135,9 +136,10 @@ void ffGenerateDateTimeJsonConfig(FFDateTimeOptions* options, yyjson_mut_doc* do ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateDateTimeJsonResult(FF_MAYBE_UNUSED FFDateTimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateDateTimeJsonResult(FF_MAYBE_UNUSED FFDateTimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { yyjson_mut_obj_add_strcpy(doc, module, "result", ffTimeToFullStr(ffTimeGetNow())); + return true; } void ffInitDateTimeOptions(FFDateTimeOptions* options) diff --git a/src/modules/datetime/datetime.h b/src/modules/datetime/datetime.h index bdf8f732d6..29c6f54e41 100644 --- a/src/modules/datetime/datetime.h +++ b/src/modules/datetime/datetime.h @@ -4,7 +4,7 @@ #define FF_DATETIME_MODULE_NAME "DateTime" -void ffPrintDateTime(FFDateTimeOptions* options); +bool ffPrintDateTime(FFDateTimeOptions* options); void ffInitDateTimeOptions(FFDateTimeOptions* options); void ffDestroyDateTimeOptions(FFDateTimeOptions* options); diff --git a/src/modules/de/de.c b/src/modules/de/de.c index ac901566db..60870e3c95 100644 --- a/src/modules/de/de.c +++ b/src/modules/de/de.c @@ -5,14 +5,14 @@ #include "modules/de/de.h" #include "util/stringUtils.h" -void ffPrintDE(FFDEOptions* options) +bool ffPrintDE(FFDEOptions* options) { const FFDisplayServerResult* result = ffConnectDisplayServer(); if(result->dePrettyName.length == 0) { ffPrintError(FF_DE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No DE found"); - return; + return false; } FF_STRBUF_AUTO_DESTROY version = ffStrbufCreate(); @@ -40,6 +40,8 @@ void ffPrintDE(FFDEOptions* options) FF_FORMAT_ARG(version, "version") })); } + + return true; } void ffParseDEJsonObject(FFDEOptions* options, yyjson_val* module) @@ -68,14 +70,14 @@ void ffGenerateDEJsonConfig(FFDEOptions* options, yyjson_mut_doc* doc, yyjson_mu yyjson_mut_obj_add_bool(doc, module, "slowVersionDetection", options->slowVersionDetection); } -void ffGenerateDEJsonResult(FF_MAYBE_UNUSED FFDEOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateDEJsonResult(FF_MAYBE_UNUSED FFDEOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFDisplayServerResult* result = ffConnectDisplayServer(); if(result->dePrettyName.length == 0) { yyjson_mut_obj_add_str(doc, module, "error", "No DE found"); - return; + return false; } FF_STRBUF_AUTO_DESTROY version = ffStrbufCreate(); @@ -85,6 +87,7 @@ void ffGenerateDEJsonResult(FF_MAYBE_UNUSED FFDEOptions* options, yyjson_mut_doc yyjson_mut_obj_add_strbuf(doc, obj, "processName", &result->deProcessName); yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &result->dePrettyName); yyjson_mut_obj_add_strbuf(doc, obj, "version", &version); + return true; } void ffInitDEOptions(FFDEOptions* options) diff --git a/src/modules/de/de.h b/src/modules/de/de.h index ca945380bd..8aaf642d07 100644 --- a/src/modules/de/de.h +++ b/src/modules/de/de.h @@ -4,7 +4,7 @@ #define FF_DE_MODULE_NAME "DE" -void ffPrintDE(FFDEOptions* options); +bool ffPrintDE(FFDEOptions* options); void ffInitDEOptions(FFDEOptions* options); void ffDestroyDEOptions(FFDEOptions* options); diff --git a/src/modules/disk/disk.c b/src/modules/disk/disk.c index ea74e4ef3b..43283de4a5 100644 --- a/src/modules/disk/disk.c +++ b/src/modules/disk/disk.c @@ -15,31 +15,42 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index if(options->moduleArgs.key.length == 0) { - if(instance.config.display.pipe) - ffStrbufAppendF(&key, "%s (%s)", FF_DISK_MODULE_NAME, disk->mountpoint.chars); - else + ffStrbufSetF(&key, "%s (%s)", FF_DISK_MODULE_NAME, disk->mountpoint.chars); + } + else + { + FF_STRBUF_AUTO_DESTROY mountpointLink = ffStrbufCreate(); + FF_STRBUF_AUTO_DESTROY nameLink = ffStrbufCreate(); + #ifdef __linux__ + if (getenv("WSL_DISTRO_NAME") != NULL && getenv("WT_SESSION") != NULL) { - #ifdef __linux__ - if (getenv("WSL_DISTRO_NAME") != NULL && getenv("WT_SESSION") != NULL) + if (ffStrbufEqualS(&disk->filesystem, "9p") && ffStrbufStartsWithS(&disk->mountpoint, "/mnt/")) { - if (ffStrbufEqualS(&disk->filesystem, "9p") && ffStrbufStartsWithS(&disk->mountpoint, "/mnt/")) - ffStrbufAppendF(&key, "%s (\e]8;;file:///%c:/\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, disk->mountpoint.chars[5], disk->mountpoint.chars); - else - ffStrbufAppendF(&key, "%s (\e]8;;file:////wsl.localhost/%s%s\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, getenv("WSL_DISTRO_NAME"), disk->mountpoint.chars, disk->mountpoint.chars); + ffStrbufSetF(&mountpointLink, "\e]8;;file:///%c:/\e\\%s\e]8;;\e\\", disk->mountpoint.chars[5], disk->mountpoint.chars); + ffStrbufSetF(&nameLink, "\e]8;;file:///%c:/\e\\%s\e]8;;\e\\", disk->mountpoint.chars[5], disk->name.chars); } else - #endif - ffStrbufAppendF(&key, "%s (\e]8;;file://%s\e\\%s\e]8;;\e\\)", FF_DISK_MODULE_NAME, disk->mountpoint.chars, disk->mountpoint.chars); + { + ffStrbufSetF(&mountpointLink, "\e]8;;file:////wsl.localhost/%s%s\e\\%s\e]8;;\e\\", getenv("WSL_DISTRO_NAME"), disk->mountpoint.chars, disk->mountpoint.chars); + ffStrbufSetF(&nameLink, "\e]8;;file:////wsl.localhost/%s%s\e\\%s\e]8;;\e\\", getenv("WSL_DISTRO_NAME"), disk->mountpoint.chars, disk->name.chars); + } } - } - else - { + else + #endif + { + ffStrbufSetF(&mountpointLink, "\e]8;;file://%s\e\\%s\e]8;;\e\\", disk->mountpoint.chars, disk->mountpoint.chars); + ffStrbufSetF(&nameLink, "\e]8;;file://%s\e\\%s\e]8;;\e\\", disk->mountpoint.chars, disk->name.chars); + } + FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, ((FFformatarg[]) { FF_FORMAT_ARG(disk->mountpoint, "mountpoint"), FF_FORMAT_ARG(disk->name, "name"), FF_FORMAT_ARG(disk->mountFrom, "mount-from"), FF_FORMAT_ARG(options->moduleArgs.keyIcon, "icon"), FF_FORMAT_ARG(index, "index"), + FF_FORMAT_ARG(disk->filesystem, "filesystem"), + FF_FORMAT_ARG(mountpointLink, "mountpoint-link"), + FF_FORMAT_ARG(nameLink, "name-link"), })); } @@ -125,6 +136,12 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index bool isHidden = !!(disk->type & FF_DISK_VOLUME_TYPE_HIDDEN_BIT); bool isReadOnly = !!(disk->type & FF_DISK_VOLUME_TYPE_READONLY_BIT); + FF_STRBUF_AUTO_DESTROY freePretty = ffStrbufCreate(); + ffSizeAppendNum(disk->bytesFree, &freePretty); + + FF_STRBUF_AUTO_DESTROY availPretty = ffStrbufCreate(); + ffSizeAppendNum(disk->bytesAvailable, &availPretty); + uint64_t now = ffTimeGetNow(); uint64_t duration = now - disk->createTime; uint32_t milliseconds = (uint32_t) (duration % 1000); @@ -163,11 +180,13 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index FF_FORMAT_ARG(age.years, "years"), FF_FORMAT_ARG(age.daysOfYear, "days-of-year"), FF_FORMAT_ARG(age.yearsFraction, "years-fraction"), + FF_FORMAT_ARG(freePretty, "size-free"), + FF_FORMAT_ARG(availPretty, "size-available"), })); } } -void ffPrintDisk(FFDiskOptions* options) +bool ffPrintDisk(FFDiskOptions* options) { FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk)); const char* error = ffDetectDisks(options, &disks); @@ -175,23 +194,22 @@ void ffPrintDisk(FFDiskOptions* options) if(error) { ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); + return false; } - else + + uint32_t index = 0; + FF_LIST_FOR_EACH(FFDisk, disk, disks) { - uint32_t index = 0; - FF_LIST_FOR_EACH(FFDisk, disk, disks) - { - if(__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes)) - continue; + if(__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes)) + continue; - if (options->hideFolders.length && ffDiskMatchMountpoint(&options->hideFolders, disk->mountpoint.chars)) - continue; + if (options->hideFolders.length && ffDiskMatchMountpoint(&options->hideFolders, disk->mountpoint.chars)) + continue; - if (options->hideFS.length && ffStrbufMatchSeparated(&disk->filesystem, &options->hideFS, ':')) - continue; + if (options->hideFS.length && ffStrbufMatchSeparated(&disk->filesystem, &options->hideFS, ':')) + continue; - printDisk(options, disk, ++index); - } + printDisk(options, disk, ++index); } FF_LIST_FOR_EACH(FFDisk, disk, disks) @@ -201,6 +219,8 @@ void ffPrintDisk(FFDiskOptions* options) ffStrbufDestroy(&disk->filesystem); ffStrbufDestroy(&disk->name); } + + return true; } void ffParseDiskJsonObject(FFDiskOptions* options, yyjson_val* module) @@ -327,7 +347,7 @@ void ffGenerateDiskJsonConfig(FFDiskOptions* options, yyjson_mut_doc* doc, yyjso ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk)); const char* error = ffDetectDisks(options, &disks); @@ -335,7 +355,7 @@ void ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjso if(error) { yyjson_mut_obj_add_str(doc, module, "result", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -394,6 +414,8 @@ void ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjso ffStrbufDestroy(&item->filesystem); ffStrbufDestroy(&item->name); } + + return true; } void ffInitDiskOptions(FFDiskOptions* options) @@ -454,5 +476,7 @@ FFModuleBaseInfo ffDiskModuleInfo = { {"Years integer after creation", "years"}, {"Days of year after creation", "days-of-year"}, {"Years fraction after creation", "years-fraction"}, + {"Size free", "size-free"}, + {"Size available", "size-available"}, })) }; diff --git a/src/modules/disk/disk.h b/src/modules/disk/disk.h index c96155fe13..5f7820ac7c 100644 --- a/src/modules/disk/disk.h +++ b/src/modules/disk/disk.h @@ -4,7 +4,7 @@ #define FF_DISK_MODULE_NAME "Disk" -void ffPrintDisk(FFDiskOptions* options); +bool ffPrintDisk(FFDiskOptions* options); void ffInitDiskOptions(FFDiskOptions* options); void ffDestroyDiskOptions(FFDiskOptions* options); diff --git a/src/modules/diskio/diskio.c b/src/modules/diskio/diskio.c index 5fa60982e6..e353d95f5a 100644 --- a/src/modules/diskio/diskio.c +++ b/src/modules/diskio/diskio.c @@ -30,7 +30,7 @@ static void formatKey(const FFDiskIOOptions* options, FFDiskIOResult* dev, uint3 } } -void ffPrintDiskIO(FFDiskIOOptions* options) +bool ffPrintDiskIO(FFDiskIOOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFDiskIOResult)); const char* error = ffDetectDiskIO(&result, options); @@ -38,7 +38,7 @@ void ffPrintDiskIO(FFDiskIOOptions* options) if(error) { ffPrintError(FF_DISKIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, "%s", error); - return; + return false; } ffListSort(&result, (const void*) sortDevices); @@ -93,6 +93,8 @@ void ffPrintDiskIO(FFDiskIOOptions* options) ffStrbufDestroy(&dev->name); ffStrbufDestroy(&dev->devPath); } + + return true; } void ffParseDiskIOJsonObject(FFDiskIOOptions* options, yyjson_val* module) @@ -137,7 +139,7 @@ void ffGenerateDiskIOJsonConfig(FFDiskIOOptions* options, yyjson_mut_doc* doc, y yyjson_mut_obj_add_uint(doc, module, "waitTime", options->waitTime); } -void ffGenerateDiskIOJsonResult(FFDiskIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateDiskIOJsonResult(FFDiskIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFDiskIOResult)); const char* error = ffDetectDiskIO(&result, options); @@ -145,7 +147,7 @@ void ffGenerateDiskIOJsonResult(FFDiskIOOptions* options, yyjson_mut_doc* doc, y if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -165,6 +167,8 @@ void ffGenerateDiskIOJsonResult(FFDiskIOOptions* options, yyjson_mut_doc* doc, y ffStrbufDestroy(&dev->name); ffStrbufDestroy(&dev->devPath); } + + return true; } void ffInitDiskIOOptions(FFDiskIOOptions* options) diff --git a/src/modules/diskio/diskio.h b/src/modules/diskio/diskio.h index ba3977bae2..7fba81c9de 100644 --- a/src/modules/diskio/diskio.h +++ b/src/modules/diskio/diskio.h @@ -6,7 +6,7 @@ void ffPrepareDiskIO(FFDiskIOOptions* options); -void ffPrintDiskIO(FFDiskIOOptions* options); +bool ffPrintDiskIO(FFDiskIOOptions* options); void ffInitDiskIOOptions(FFDiskIOOptions* options); void ffDestroyDiskIOOptions(FFDiskIOOptions* options); diff --git a/src/modules/display/display.c b/src/modules/display/display.c index 33b48e9117..c8f81f84e3 100644 --- a/src/modules/display/display.c +++ b/src/modules/display/display.c @@ -17,14 +17,14 @@ static int sortByNameDesc(FFDisplayResult* a, FFDisplayResult* b) return -ffStrbufComp(&a->name, &b->name); } -void ffPrintDisplay(FFDisplayOptions* options) +bool ffPrintDisplay(FFDisplayOptions* options) { const FFDisplayServerResult* dsResult = ffConnectDisplayServer(); if(dsResult->displays.length == 0) { ffPrintError(FF_DISPLAY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Couldn't detect display"); - return; + return false; } if (options->order != FF_DISPLAY_ORDER_NONE) @@ -68,7 +68,7 @@ void ffPrintDisplay(FFDisplayOptions* options) ffStrbufTrimRight(&buffer, ' '); ffStrbufTrimRight(&buffer, ','); ffStrbufPutTo(&buffer, stdout); - return; + return true; } FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate(); @@ -215,6 +215,8 @@ void ffPrintDisplay(FFDisplayOptions* options) })); } } + + return true; } void ffParseDisplayJsonObject(FFDisplayOptions* options, yyjson_val* module) @@ -319,14 +321,14 @@ void ffGenerateDisplayJsonConfig(FFDisplayOptions* options, yyjson_mut_doc* doc, } } -void ffGenerateDisplayJsonResult(FF_MAYBE_UNUSED FFDisplayOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateDisplayJsonResult(FF_MAYBE_UNUSED FFDisplayOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFDisplayServerResult* dsResult = ffConnectDisplayServer(); if(dsResult->displays.length == 0) { yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect display"); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -406,6 +408,8 @@ void ffGenerateDisplayJsonResult(FF_MAYBE_UNUSED FFDisplayOptions* options, yyjs yyjson_mut_obj_add_str(doc, obj, "platformApi", item->platformApi); } + + return true; } void ffInitDisplayOptions(FFDisplayOptions* options) diff --git a/src/modules/display/display.h b/src/modules/display/display.h index 8b9f419418..a0a4b30959 100644 --- a/src/modules/display/display.h +++ b/src/modules/display/display.h @@ -4,7 +4,7 @@ #define FF_DISPLAY_MODULE_NAME "Display" -void ffPrintDisplay(FFDisplayOptions* options); +bool ffPrintDisplay(FFDisplayOptions* options); void ffInitDisplayOptions(FFDisplayOptions* options); void ffDestroyDisplayOptions(FFDisplayOptions* options); diff --git a/src/modules/dns/dns.c b/src/modules/dns/dns.c index aa36ceacbe..a92784df6b 100644 --- a/src/modules/dns/dns.c +++ b/src/modules/dns/dns.c @@ -4,7 +4,7 @@ #include "modules/dns/dns.h" #include "util/stringUtils.h" -void ffPrintDNS(FFDNSOptions* options) +bool ffPrintDNS(FFDNSOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFstrbuf)); @@ -13,13 +13,13 @@ void ffPrintDNS(FFDNSOptions* options) if (error) { ffPrintError(FF_DNS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if (result.length == 0) { ffPrintError(FF_DNS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "NO DNS servers detected"); - return; + return false; } FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate(); @@ -55,6 +55,8 @@ void ffPrintDNS(FFDNSOptions* options) { ffStrbufDestroy(item); } + + return true; } void ffParseDNSJsonObject(FFDNSOptions* options, yyjson_val* module) @@ -104,7 +106,7 @@ void ffGenerateDNSJsonConfig(FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_ } } -void ffGenerateDNSJsonResult(FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateDNSJsonResult(FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFstrbuf)); @@ -113,7 +115,7 @@ void ffGenerateDNSJsonResult(FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_ if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - goto exit; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -123,11 +125,12 @@ void ffGenerateDNSJsonResult(FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_ yyjson_mut_arr_add_strbuf(doc, arr, item); } -exit: FF_LIST_FOR_EACH(FFstrbuf, item, result) { ffStrbufDestroy(item); } + + return true; } void ffInitDNSOptions(FFDNSOptions* options) diff --git a/src/modules/dns/dns.h b/src/modules/dns/dns.h index e337637751..aec4e85fc6 100644 --- a/src/modules/dns/dns.h +++ b/src/modules/dns/dns.h @@ -4,7 +4,7 @@ #define FF_DNS_MODULE_NAME "DNS" -void ffPrintDNS(FFDNSOptions* options); +bool ffPrintDNS(FFDNSOptions* options); void ffInitDNSOptions(FFDNSOptions* options); void ffDestroyDNSOptions(FFDNSOptions* options); diff --git a/src/modules/editor/editor.c b/src/modules/editor/editor.c index e001d22e88..6e15514022 100644 --- a/src/modules/editor/editor.c +++ b/src/modules/editor/editor.c @@ -5,7 +5,7 @@ #include "modules/editor/editor.h" #include "util/stringUtils.h" -void ffPrintEditor(FFEditorOptions* options) +bool ffPrintEditor(FFEditorOptions* options) { FFEditorResult result = { .type = "Unknown", @@ -19,7 +19,7 @@ void ffPrintEditor(FFEditorOptions* options) if (error) { ffPrintError(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if (options->moduleArgs.outputFormat.length == 0) @@ -52,6 +52,8 @@ void ffPrintEditor(FFEditorOptions* options) ffStrbufDestroy(&result.path); ffStrbufDestroy(&result.exe); ffStrbufDestroy(&result.version); + + return true; } void ffParseEditorJsonObject(FFEditorOptions* options, yyjson_val* module) @@ -72,7 +74,7 @@ void ffGenerateEditorJsonConfig(FFEditorOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateEditorJsonResult(FF_MAYBE_UNUSED FFEditorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateEditorJsonResult(FF_MAYBE_UNUSED FFEditorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFEditorResult result = { .name = ffStrbufCreate(), @@ -85,7 +87,7 @@ void ffGenerateEditorJsonResult(FF_MAYBE_UNUSED FFEditorOptions* options, yyjson if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -99,6 +101,8 @@ void ffGenerateEditorJsonResult(FF_MAYBE_UNUSED FFEditorOptions* options, yyjson ffStrbufDestroy(&result.path); ffStrbufDestroy(&result.exe); ffStrbufDestroy(&result.version); + + return true; } void ffInitEditorOptions(FFEditorOptions* options) diff --git a/src/modules/editor/editor.h b/src/modules/editor/editor.h index 0e5be29e1f..293d0a6e19 100644 --- a/src/modules/editor/editor.h +++ b/src/modules/editor/editor.h @@ -4,7 +4,7 @@ #define FF_EDITOR_MODULE_NAME "Editor" -void ffPrintEditor(FFEditorOptions* options); +bool ffPrintEditor(FFEditorOptions* options); void ffInitEditorOptions(FFEditorOptions* options); void ffDestroyEditorOptions(FFEditorOptions* options); diff --git a/src/modules/font/font.c b/src/modules/font/font.c index eedf32c4ea..b85a9e94de 100644 --- a/src/modules/font/font.c +++ b/src/modules/font/font.c @@ -4,8 +4,9 @@ #include "modules/font/font.h" #include "util/stringUtils.h" -void ffPrintFont(FFFontOptions* options) +bool ffPrintFont(FFFontOptions* options) { + bool success = false; FFFontResult font; for(uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) ffStrbufInit(&font.fonts[i]); @@ -34,11 +35,15 @@ void ffPrintFont(FFFontOptions* options) FF_FORMAT_ARG(font.display, "combined"), })); } + + success = true; } ffStrbufDestroy(&font.display); for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) ffStrbufDestroy(&font.fonts[i]); + + return success; } void ffParseFontJsonObject(FFFontOptions* options, yyjson_val* module) @@ -59,8 +64,9 @@ void ffGenerateFontJsonConfig(FFFontOptions* options, yyjson_mut_doc* doc, yyjso ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateFontJsonResult(FF_MAYBE_UNUSED FFFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateFontJsonResult(FF_MAYBE_UNUSED FFFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFFontResult font; for(uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) ffStrbufInit(&font.fonts[i]); @@ -78,11 +84,14 @@ void ffGenerateFontJsonResult(FF_MAYBE_UNUSED FFFontOptions* options, yyjson_mut yyjson_mut_val* fontsArr = yyjson_mut_obj_add_arr(doc, obj, "fonts"); for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) yyjson_mut_arr_add_strbuf(doc, fontsArr, &font.fonts[i]); + success = true; } ffStrbufDestroy(&font.display); for (uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i) ffStrbufDestroy(&font.fonts[i]); + + return success; } void ffInitFontOptions(FFFontOptions* options) diff --git a/src/modules/font/font.h b/src/modules/font/font.h index 9d3630bd22..b793b9fdd3 100644 --- a/src/modules/font/font.h +++ b/src/modules/font/font.h @@ -4,7 +4,7 @@ #define FF_FONT_MODULE_NAME "Font" -void ffPrintFont(FFFontOptions* options); +bool ffPrintFont(FFFontOptions* options); void ffInitFontOptions(FFFontOptions* options); void ffDestroyFontOptions(FFFontOptions* options); diff --git a/src/modules/gamepad/gamepad.c b/src/modules/gamepad/gamepad.c index 5b1b6f7d5d..087e7dc359 100644 --- a/src/modules/gamepad/gamepad.c +++ b/src/modules/gamepad/gamepad.c @@ -50,7 +50,7 @@ static void printDevice(FFGamepadOptions* options, const FFGamepadDevice* device } } -void ffPrintGamepad(FFGamepadOptions* options) +bool ffPrintGamepad(FFGamepadOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFGamepadDevice)); @@ -59,13 +59,13 @@ void ffPrintGamepad(FFGamepadOptions* options) if(error) { ffPrintError(FF_GAMEPAD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(!result.length) { ffPrintError(FF_GAMEPAD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected"); - return; + return false; } uint8_t index = 0; @@ -75,6 +75,8 @@ void ffPrintGamepad(FFGamepadOptions* options) ffStrbufDestroy(&device->serial); ffStrbufDestroy(&device->name); } + + return true; } void ffParseGamepadJsonObject(FFGamepadOptions* options, yyjson_val* module) @@ -100,7 +102,7 @@ void ffGenerateGamepadJsonConfig(FFGamepadOptions* options, yyjson_mut_doc* doc, ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateGamepadJsonResult(FF_MAYBE_UNUSED FFGamepadOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateGamepadJsonResult(FF_MAYBE_UNUSED FFGamepadOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFGamepadDevice)); @@ -109,7 +111,7 @@ void ffGenerateGamepadJsonResult(FF_MAYBE_UNUSED FFGamepadOptions* options, yyjs if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -125,6 +127,8 @@ void ffGenerateGamepadJsonResult(FF_MAYBE_UNUSED FFGamepadOptions* options, yyjs ffStrbufDestroy(&device->serial); ffStrbufDestroy(&device->name); } + + return true; } void ffInitGamepadOptions(FFGamepadOptions* options) diff --git a/src/modules/gamepad/gamepad.h b/src/modules/gamepad/gamepad.h index 7d252d8c07..395c90b2e3 100644 --- a/src/modules/gamepad/gamepad.h +++ b/src/modules/gamepad/gamepad.h @@ -4,7 +4,7 @@ #define FF_GAMEPAD_MODULE_NAME "Gamepad" -void ffPrintGamepad(FFGamepadOptions* options); +bool ffPrintGamepad(FFGamepadOptions* options); void ffInitGamepadOptions(FFGamepadOptions* options); void ffDestroyGamepadOptions(FFGamepadOptions* options); diff --git a/src/modules/gpu/gpu.c b/src/modules/gpu/gpu.c index d128ac6fbc..64fb143807 100644 --- a/src/modules/gpu/gpu.c +++ b/src/modules/gpu/gpu.c @@ -159,14 +159,14 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu } } -void ffPrintGPU(FFGPUOptions* options) +bool ffPrintGPU(FFGPUOptions* options) { FF_LIST_AUTO_DESTROY gpus = ffListCreate(sizeof (FFGPUResult)); const char* error = ffDetectGPU(options, &gpus); if (error) { ffPrintError(FF_GPU_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } FF_LIST_AUTO_DESTROY selectedGPUs; @@ -200,6 +200,8 @@ void ffPrintGPU(FFGPUOptions* options) ffStrbufDestroy(&gpu->platformApi); ffStrbufDestroy(&gpu->memoryType); } + + return true; } void ffParseGPUJsonObject(FFGPUOptions* options, yyjson_val* module) @@ -313,14 +315,14 @@ void ffGenerateGPUJsonConfig(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_ ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY gpus = ffListCreate(sizeof (FFGPUResult)); const char* error = ffDetectGPU(options, &gpus); if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -408,6 +410,8 @@ void ffGenerateGPUJsonResult(FFGPUOptions* options, yyjson_mut_doc* doc, yyjson_ ffStrbufDestroy(&gpu->platformApi); ffStrbufDestroy(&gpu->memoryType); } + + return true; } void ffInitGPUOptions(FFGPUOptions* options) diff --git a/src/modules/gpu/gpu.h b/src/modules/gpu/gpu.h index afc952d6ce..e2a6f14248 100644 --- a/src/modules/gpu/gpu.h +++ b/src/modules/gpu/gpu.h @@ -4,7 +4,7 @@ #define FF_GPU_MODULE_NAME "GPU" -void ffPrintGPU(FFGPUOptions* options); +bool ffPrintGPU(FFGPUOptions* options); void ffInitGPUOptions(FFGPUOptions* options); void ffDestroyGPUOptions(FFGPUOptions* options); diff --git a/src/modules/host/host.c b/src/modules/host/host.c index 9a265c058a..b86e37d542 100644 --- a/src/modules/host/host.c +++ b/src/modules/host/host.c @@ -4,8 +4,9 @@ #include "modules/host/host.h" #include "util/stringUtils.h" -void ffPrintHost(FFHostOptions* options) +bool ffPrintHost(FFHostOptions* options) { + bool success = false; FFHostResult host; ffStrbufInit(&host.family); ffStrbufInit(&host.name); @@ -56,6 +57,7 @@ void ffPrintHost(FFHostOptions* options) FF_FORMAT_ARG(host.uuid, "uuid"), })); } + success = true; exit: ffStrbufDestroy(&host.family); @@ -65,6 +67,8 @@ void ffPrintHost(FFHostOptions* options) ffStrbufDestroy(&host.serial); ffStrbufDestroy(&host.uuid); ffStrbufDestroy(&host.vendor); + + return success; } void ffParseHostJsonObject(FFHostOptions* options, yyjson_val* module) @@ -85,8 +89,9 @@ void ffGenerateHostJsonConfig(FFHostOptions* options, yyjson_mut_doc* doc, yyjso ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateHostJsonResult(FF_MAYBE_UNUSED FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateHostJsonResult(FF_MAYBE_UNUSED FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFHostResult host; ffStrbufInit(&host.family); ffStrbufInit(&host.name); @@ -117,6 +122,7 @@ void ffGenerateHostJsonResult(FF_MAYBE_UNUSED FFHostOptions* options, yyjson_mut yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &host.vendor); yyjson_mut_obj_add_strbuf(doc, obj, "serial", &host.serial); yyjson_mut_obj_add_strbuf(doc, obj, "uuid", &host.uuid); + success = true; exit: ffStrbufDestroy(&host.family); @@ -126,6 +132,8 @@ void ffGenerateHostJsonResult(FF_MAYBE_UNUSED FFHostOptions* options, yyjson_mut ffStrbufDestroy(&host.serial); ffStrbufDestroy(&host.uuid); ffStrbufDestroy(&host.vendor); + + return success; } void ffInitHostOptions(FFHostOptions* options) diff --git a/src/modules/host/host.h b/src/modules/host/host.h index 79f22e5272..1f98fd406a 100644 --- a/src/modules/host/host.h +++ b/src/modules/host/host.h @@ -4,7 +4,7 @@ #define FF_HOST_MODULE_NAME "Host" -void ffPrintHost(FFHostOptions* options); +bool ffPrintHost(FFHostOptions* options); void ffInitHostOptions(FFHostOptions* options); void ffDestroyHostOptions(FFHostOptions* options); diff --git a/src/modules/icons/icons.c b/src/modules/icons/icons.c index c1347790e3..b50b85363e 100644 --- a/src/modules/icons/icons.c +++ b/src/modules/icons/icons.c @@ -4,8 +4,9 @@ #include "modules/icons/icons.h" #include "util/stringUtils.h" -void ffPrintIcons(FFIconsOptions* options) +bool ffPrintIcons(FFIconsOptions* options) { + bool success = false; FFIconsResult result = { .icons1 = ffStrbufCreate(), .icons2 = ffStrbufCreate(), @@ -15,7 +16,7 @@ void ffPrintIcons(FFIconsOptions* options) if(error) { ffPrintError(FF_ICONS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + goto exit; } if(options->moduleArgs.outputFormat.length == 0) @@ -38,9 +39,13 @@ void ffPrintIcons(FFIconsOptions* options) FF_FORMAT_ARG(result.icons2, "icons2"), })); } + success = true; +exit: ffStrbufDestroy(&result.icons1); ffStrbufDestroy(&result.icons2); + + return success; } void ffParseIconsJsonObject(FFIconsOptions* options, yyjson_val* module) @@ -61,8 +66,9 @@ void ffGenerateIconsJsonConfig(FFIconsOptions* options, yyjson_mut_doc* doc, yyj ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateIconsJsonResult(FF_MAYBE_UNUSED FFIconsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateIconsJsonResult(FF_MAYBE_UNUSED FFIconsOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFIconsResult result = { .icons1 = ffStrbufCreate(), .icons2 = ffStrbufCreate() @@ -72,15 +78,19 @@ void ffGenerateIconsJsonResult(FF_MAYBE_UNUSED FFIconsOptions* options, yyjson_m if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + goto exit; } yyjson_mut_val* icons = yyjson_mut_obj_add_obj(doc, module, "result"); yyjson_mut_obj_add_strbuf(doc, icons, "icons1", &result.icons1); yyjson_mut_obj_add_strbuf(doc, icons, "icons2", &result.icons2); + success = true; +exit: ffStrbufDestroy(&result.icons1); ffStrbufDestroy(&result.icons2); + + return success; } void ffInitIconsOptions(FFIconsOptions* options) diff --git a/src/modules/icons/icons.h b/src/modules/icons/icons.h index 6f2bb877cc..a583a22571 100644 --- a/src/modules/icons/icons.h +++ b/src/modules/icons/icons.h @@ -4,7 +4,7 @@ #define FF_ICONS_MODULE_NAME "Icons" -void ffPrintIcons(FFIconsOptions* options); +bool ffPrintIcons(FFIconsOptions* options); void ffInitIconsOptions(FFIconsOptions* options); void ffDestroyIconsOptions(FFIconsOptions* options); diff --git a/src/modules/initsystem/initsystem.c b/src/modules/initsystem/initsystem.c index 28ad9ba80b..d32f7a6dd2 100644 --- a/src/modules/initsystem/initsystem.c +++ b/src/modules/initsystem/initsystem.c @@ -6,8 +6,9 @@ #define FF_INITSYSTEM_DISPLAY_NAME "Init System" -void ffPrintInitSystem(FFInitSystemOptions* options) +bool ffPrintInitSystem(FFInitSystemOptions* options) { + bool success = false; FFInitSystemResult result = { .name = ffStrbufCreate(), .exe = ffStrbufCreate(), @@ -41,11 +42,14 @@ void ffPrintInitSystem(FFInitSystemOptions* options) FF_FORMAT_ARG(result.pid, "pid"), })); } + success = true; exit: ffStrbufDestroy(&result.name); ffStrbufDestroy(&result.exe); ffStrbufDestroy(&result.version); + + return success; } void ffParseInitSystemJsonObject(FFInitSystemOptions* options, yyjson_val* module) @@ -66,8 +70,9 @@ void ffGenerateInitSystemJsonConfig(FFInitSystemOptions* options, yyjson_mut_doc ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateInitSystemJsonResult(FF_MAYBE_UNUSED FFInitSystemOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateInitSystemJsonResult(FF_MAYBE_UNUSED FFInitSystemOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFInitSystemResult result = { .name = ffStrbufCreate(), .exe = ffStrbufCreate(), @@ -88,11 +93,13 @@ void ffGenerateInitSystemJsonResult(FF_MAYBE_UNUSED FFInitSystemOptions* options yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe); yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version); yyjson_mut_obj_add_uint(doc, obj, "pid", result.pid); + success = true; exit: ffStrbufDestroy(&result.name); ffStrbufDestroy(&result.exe); ffStrbufDestroy(&result.version); + return success; } void ffInitInitSystemOptions(FFInitSystemOptions* options) diff --git a/src/modules/initsystem/initsystem.h b/src/modules/initsystem/initsystem.h index 37b601481b..381b955431 100644 --- a/src/modules/initsystem/initsystem.h +++ b/src/modules/initsystem/initsystem.h @@ -4,7 +4,7 @@ #define FF_INITSYSTEM_MODULE_NAME "InitSystem" -void ffPrintInitSystem(FFInitSystemOptions* options); +bool ffPrintInitSystem(FFInitSystemOptions* options); void ffInitInitSystemOptions(FFInitSystemOptions* options); void ffDestroyInitSystemOptions(FFInitSystemOptions* options); diff --git a/src/modules/kernel/kernel.c b/src/modules/kernel/kernel.c index 042ef3735a..62254b22c6 100644 --- a/src/modules/kernel/kernel.c +++ b/src/modules/kernel/kernel.c @@ -4,7 +4,7 @@ #include "modules/kernel/kernel.h" #include "util/stringUtils.h" -void ffPrintKernel(FFKernelOptions* options) +bool ffPrintKernel(FFKernelOptions* options) { const FFPlatformSysinfo* info = &instance.state.platform.sysinfo; if(options->moduleArgs.outputFormat.length == 0) @@ -30,6 +30,8 @@ void ffPrintKernel(FFKernelOptions* options) FF_FORMAT_ARG(str, "page-size"), })); } + + return true; } void ffParseKernelJsonObject(FFKernelOptions* options, yyjson_val* module) @@ -50,7 +52,7 @@ void ffGenerateKernelJsonConfig(FFKernelOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateKernelJsonResult(FF_MAYBE_UNUSED FFKernelOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateKernelJsonResult(FF_MAYBE_UNUSED FFKernelOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFPlatformSysinfo* info = &instance.state.platform.sysinfo; @@ -61,6 +63,8 @@ void ffGenerateKernelJsonResult(FF_MAYBE_UNUSED FFKernelOptions* options, yyjson yyjson_mut_obj_add_strbuf(doc, obj, "version", &info->version); yyjson_mut_obj_add_strbuf(doc, obj, "displayVersion", &info->displayVersion); yyjson_mut_obj_add_uint(doc, obj, "pageSize", info->pageSize); + + return true; } void ffInitKernelOptions(FFKernelOptions* options) diff --git a/src/modules/kernel/kernel.h b/src/modules/kernel/kernel.h index 743ac21487..010ec54c4d 100644 --- a/src/modules/kernel/kernel.h +++ b/src/modules/kernel/kernel.h @@ -4,7 +4,7 @@ #define FF_KERNEL_MODULE_NAME "Kernel" -void ffPrintKernel(FFKernelOptions* options); +bool ffPrintKernel(FFKernelOptions* options); void ffInitKernelOptions(FFKernelOptions* options); void ffDestroyKernelOptions(FFKernelOptions* options); diff --git a/src/modules/keyboard/keyboard.c b/src/modules/keyboard/keyboard.c index ed67bf5696..c02c8653bf 100644 --- a/src/modules/keyboard/keyboard.c +++ b/src/modules/keyboard/keyboard.c @@ -21,7 +21,7 @@ static void printDevice(FFKeyboardOptions* options, const FFKeyboardDevice* devi } } -void ffPrintKeyboard(FFKeyboardOptions* options) +bool ffPrintKeyboard(FFKeyboardOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFKeyboardDevice)); @@ -30,13 +30,13 @@ void ffPrintKeyboard(FFKeyboardOptions* options) if(error) { ffPrintError(FF_KEYBOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(!result.length) { ffPrintError(FF_KEYBOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected"); - return; + return false; } uint8_t index = 0; @@ -46,6 +46,8 @@ void ffPrintKeyboard(FFKeyboardOptions* options) ffStrbufDestroy(&device->serial); ffStrbufDestroy(&device->name); } + + return true; } void ffParseKeyboardJsonObject(FFKeyboardOptions* options, yyjson_val* module) @@ -66,7 +68,7 @@ void ffGenerateKeyboardJsonConfig(FFKeyboardOptions* options, yyjson_mut_doc* do ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateKeyboardJsonResult(FF_MAYBE_UNUSED FFKeyboardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateKeyboardJsonResult(FF_MAYBE_UNUSED FFKeyboardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFKeyboardDevice)); @@ -75,7 +77,7 @@ void ffGenerateKeyboardJsonResult(FF_MAYBE_UNUSED FFKeyboardOptions* options, yy if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -91,6 +93,8 @@ void ffGenerateKeyboardJsonResult(FF_MAYBE_UNUSED FFKeyboardOptions* options, yy ffStrbufDestroy(&device->serial); ffStrbufDestroy(&device->name); } + + return true; } void ffInitKeyboardOptions(FFKeyboardOptions* options) diff --git a/src/modules/keyboard/keyboard.h b/src/modules/keyboard/keyboard.h index fee46ccb1e..bd6e69c56b 100644 --- a/src/modules/keyboard/keyboard.h +++ b/src/modules/keyboard/keyboard.h @@ -4,7 +4,7 @@ #define FF_KEYBOARD_MODULE_NAME "Keyboard" -void ffPrintKeyboard(FFKeyboardOptions* options); +bool ffPrintKeyboard(FFKeyboardOptions* options); void ffInitKeyboardOptions(FFKeyboardOptions* options); void ffDestroyKeyboardOptions(FFKeyboardOptions* options); diff --git a/src/modules/lm/lm.c b/src/modules/lm/lm.c index df67aa076f..0b17f37445 100644 --- a/src/modules/lm/lm.c +++ b/src/modules/lm/lm.c @@ -4,8 +4,9 @@ #include "modules/lm/lm.h" #include "util/stringUtils.h" -void ffPrintLM(FFLMOptions* options) +bool ffPrintLM(FFLMOptions* options) { + bool success = false; FFLMResult result; ffStrbufInit(&result.service); ffStrbufInit(&result.type); @@ -15,13 +16,13 @@ void ffPrintLM(FFLMOptions* options) if(error) { ffPrintError(FF_LM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + goto exit; } if(result.service.length == 0) { ffPrintError(FF_LM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No LM service found"); - return; + goto exit; } if(options->moduleArgs.outputFormat.length == 0) @@ -42,9 +43,14 @@ void ffPrintLM(FFLMOptions* options) FF_FORMAT_ARG(result.version, "version"), })); } + success = true; + +exit: ffStrbufDestroy(&result.service); ffStrbufDestroy(&result.type); ffStrbufDestroy(&result.version); + + return success; } void ffParseLMJsonObject(FFLMOptions* options, yyjson_val* module) @@ -65,8 +71,9 @@ void ffGenerateLMJsonConfig(FFLMOptions* options, yyjson_mut_doc* doc, yyjson_mu ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateLMJsonResult(FF_MAYBE_UNUSED FFLMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateLMJsonResult(FF_MAYBE_UNUSED FFLMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFLMResult result; ffStrbufInit(&result.service); ffStrbufInit(&result.type); @@ -89,11 +96,14 @@ void ffGenerateLMJsonResult(FF_MAYBE_UNUSED FFLMOptions* options, yyjson_mut_doc yyjson_mut_obj_add_strbuf(doc, obj, "service", &result.service); yyjson_mut_obj_add_strbuf(doc, obj, "type", &result.type); yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version); + success = true; exit: ffStrbufDestroy(&result.service); ffStrbufDestroy(&result.type); ffStrbufDestroy(&result.version); + + return success; } void ffInitLMOptions(FFLMOptions* options) diff --git a/src/modules/lm/lm.h b/src/modules/lm/lm.h index 950ca6ad1d..1ec33b6494 100644 --- a/src/modules/lm/lm.h +++ b/src/modules/lm/lm.h @@ -4,7 +4,7 @@ #define FF_LM_MODULE_NAME "LM" -void ffPrintLM(FFLMOptions* options); +bool ffPrintLM(FFLMOptions* options); void ffInitLMOptions(FFLMOptions* options); void ffDestroyLMOptions(FFLMOptions* options); diff --git a/src/modules/loadavg/loadavg.c b/src/modules/loadavg/loadavg.c index 8d1d76ead9..793786e042 100644 --- a/src/modules/loadavg/loadavg.c +++ b/src/modules/loadavg/loadavg.c @@ -6,7 +6,7 @@ #include "modules/loadavg/loadavg.h" #include "util/stringUtils.h" -void ffPrintLoadavg(FFLoadavgOptions* options) +bool ffPrintLoadavg(FFLoadavgOptions* options) { double result[3] = { 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0 }; @@ -14,7 +14,7 @@ void ffPrintLoadavg(FFLoadavgOptions* options) if(error) { ffPrintError(FF_LOADAVG_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -88,6 +88,8 @@ void ffPrintLoadavg(FFLoadavgOptions* options) FF_FORMAT_ARG(result[2], "loadavg3"), })); } + + return true; } void ffParseLoadavgJsonObject(FFLoadavgOptions* options, yyjson_val* module) @@ -129,7 +131,7 @@ void ffGenerateLoadavgJsonConfig(FFLoadavgOptions* options, yyjson_mut_doc* doc, ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateLoadavgJsonResult(FF_MAYBE_UNUSED FFLoadavgOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateLoadavgJsonResult(FF_MAYBE_UNUSED FFLoadavgOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { double result[3] = { 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0 }; @@ -137,12 +139,14 @@ void ffGenerateLoadavgJsonResult(FF_MAYBE_UNUSED FFLoadavgOptions* options, yyjs if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); for (size_t i = 0; i < 3; i++) yyjson_mut_arr_add_real(doc, arr, result[i]); + + return true; } void ffInitLoadavgOptions(FFLoadavgOptions* options) diff --git a/src/modules/loadavg/loadavg.h b/src/modules/loadavg/loadavg.h index 8ee495bf83..9af2786f90 100644 --- a/src/modules/loadavg/loadavg.h +++ b/src/modules/loadavg/loadavg.h @@ -4,7 +4,7 @@ #define FF_LOADAVG_MODULE_NAME "Loadavg" -void ffPrintLoadavg(FFLoadavgOptions* options); +bool ffPrintLoadavg(FFLoadavgOptions* options); void ffInitLoadavgOptions(FFLoadavgOptions* options); void ffDestroyLoadavgOptions(FFLoadavgOptions* options); diff --git a/src/modules/locale/locale.c b/src/modules/locale/locale.c index 1dc81d3424..a5bc15a43a 100644 --- a/src/modules/locale/locale.c +++ b/src/modules/locale/locale.c @@ -4,7 +4,7 @@ #include "modules/locale/locale.h" #include "util/stringUtils.h" -void ffPrintLocale(FFLocaleOptions* options) +bool ffPrintLocale(FFLocaleOptions* options) { FF_STRBUF_AUTO_DESTROY locale = ffStrbufCreate(); @@ -12,7 +12,7 @@ void ffPrintLocale(FFLocaleOptions* options) if(locale.length == 0) { ffPrintError(FF_LOCALE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No locale found"); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -26,6 +26,8 @@ void ffPrintLocale(FFLocaleOptions* options) FF_FORMAT_ARG(locale, "result") })); } + + return true; } void ffParseLocaleJsonObject(FFLocaleOptions* options, yyjson_val* module) @@ -46,7 +48,7 @@ void ffGenerateLocaleJsonConfig(FFLocaleOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateLocaleJsonResult(FF_MAYBE_UNUSED FFLocaleOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateLocaleJsonResult(FF_MAYBE_UNUSED FFLocaleOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_STRBUF_AUTO_DESTROY locale = ffStrbufCreate(); @@ -54,10 +56,12 @@ void ffGenerateLocaleJsonResult(FF_MAYBE_UNUSED FFLocaleOptions* options, yyjson if(locale.length == 0) { yyjson_mut_obj_add_str(doc, module, "error", "No locale found"); - return; + return false; } yyjson_mut_obj_add_strbuf(doc, module, "result", &locale); + + return true; } void ffInitLocaleOptions(FFLocaleOptions* options) diff --git a/src/modules/locale/locale.h b/src/modules/locale/locale.h index a56a612ae1..fd86e42c36 100644 --- a/src/modules/locale/locale.h +++ b/src/modules/locale/locale.h @@ -4,7 +4,7 @@ #define FF_LOCALE_MODULE_NAME "Locale" -void ffPrintLocale(FFLocaleOptions* options); +bool ffPrintLocale(FFLocaleOptions* options); void ffInitLocaleOptions(FFLocaleOptions* options); void ffDestroyLocaleOptions(FFLocaleOptions* options); diff --git a/src/modules/localip/localip.c b/src/modules/localip/localip.c index 6892305ecf..95c94d5065 100644 --- a/src/modules/localip/localip.c +++ b/src/modules/localip/localip.c @@ -101,7 +101,7 @@ static void printIp(FFLocalIpResult* ip, bool markDefaultRoute, FFstrbuf* buffer ffStrbufAppendS(buffer, " *"); } -void ffPrintLocalIp(FFLocalIpOptions* options) +bool ffPrintLocalIp(FFLocalIpOptions* options) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFLocalIpResult)); @@ -110,13 +110,13 @@ void ffPrintLocalIp(FFLocalIpOptions* options) if(error) { ffPrintError(FF_LOCALIP_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(results.length == 0) { ffPrintError(FF_LOCALIP_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Failed to detect any IPs"); - return; + return false; } ffListSort(&results, (const void*) sortIps); @@ -178,6 +178,8 @@ void ffPrintLocalIp(FFLocalIpOptions* options) ffStrbufDestroy(&ip->mac); ffStrbufDestroy(&ip->flags); } + + return true; } void ffParseLocalIpJsonObject(FFLocalIpOptions* options, yyjson_val* module) @@ -327,7 +329,7 @@ void ffGenerateLocalIpJsonConfig(FFLocalIpOptions* options, yyjson_mut_doc* doc, yyjson_mut_obj_add_strbuf(doc, module, "namePrefix", &options->namePrefix); } -void ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFLocalIpResult)); @@ -336,7 +338,7 @@ void ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjs if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -374,6 +376,8 @@ void ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjs ffStrbufDestroy(&ip->mac); ffStrbufDestroy(&ip->flags); } + + return true; } void ffInitLocalIpOptions(FFLocalIpOptions* options) diff --git a/src/modules/localip/localip.h b/src/modules/localip/localip.h index 6164699752..f9e9c4e437 100644 --- a/src/modules/localip/localip.h +++ b/src/modules/localip/localip.h @@ -4,7 +4,7 @@ #define FF_LOCALIP_MODULE_NAME "LocalIp" -void ffPrintLocalIp(FFLocalIpOptions* options); +bool ffPrintLocalIp(FFLocalIpOptions* options); void ffInitLocalIpOptions(FFLocalIpOptions* options); void ffDestroyLocalIpOptions(FFLocalIpOptions* options); diff --git a/src/modules/media/media.c b/src/modules/media/media.c index 1a175008ff..f82b4b8567 100644 --- a/src/modules/media/media.c +++ b/src/modules/media/media.c @@ -41,14 +41,14 @@ static bool artistInSongTitle(const FFstrbuf* song, const FFstrbuf* artist) return false; } -void ffPrintMedia(FFMediaOptions* options) +bool ffPrintMedia(FFMediaOptions* options) { const FFMediaResult* media = ffDetectMedia(); if(media->error.length > 0) { ffPrintError(FF_MEDIA_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", media->error.chars); - return; + return false; } FF_STRBUF_AUTO_DESTROY songPretty = ffStrbufCreateCopy(&media->song); @@ -104,6 +104,8 @@ void ffPrintMedia(FFMediaOptions* options) FF_FORMAT_ARG(media->url, "url"), })); } + + return true; } void ffParseMediaJsonObject(FFMediaOptions* options, yyjson_val* module) @@ -124,14 +126,14 @@ void ffGenerateMediaJsonConfig(FFMediaOptions* options, yyjson_mut_doc* doc, yyj ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateMediaJsonResult(FF_MAYBE_UNUSED FFMediaOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateMediaJsonResult(FF_MAYBE_UNUSED FFMediaOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFMediaResult* media = ffDetectMedia(); if(media->error.length > 0) { yyjson_mut_obj_add_strbuf(doc, module, "error", &media->error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -146,6 +148,8 @@ void ffGenerateMediaJsonResult(FF_MAYBE_UNUSED FFMediaOptions* options, yyjson_m yyjson_mut_obj_add_strbuf(doc, player, "name", &media->player); yyjson_mut_obj_add_strbuf(doc, player, "id", &media->playerId); yyjson_mut_obj_add_strbuf(doc, player, "url", &media->url); + + return true; } void ffInitMediaOptions(FFMediaOptions* options) diff --git a/src/modules/media/media.h b/src/modules/media/media.h index af18735b9b..8320227d8a 100644 --- a/src/modules/media/media.h +++ b/src/modules/media/media.h @@ -4,7 +4,7 @@ #define FF_MEDIA_MODULE_NAME "Media" -void ffPrintMedia(FFMediaOptions* options); +bool ffPrintMedia(FFMediaOptions* options); void ffInitMediaOptions(FFMediaOptions* options); void ffDestroyMediaOptions(FFMediaOptions* options); diff --git a/src/modules/memory/memory.c b/src/modules/memory/memory.c index 659c440134..182d55567c 100644 --- a/src/modules/memory/memory.c +++ b/src/modules/memory/memory.c @@ -6,7 +6,7 @@ #include "modules/memory/memory.h" #include "util/stringUtils.h" -void ffPrintMemory(FFMemoryOptions* options) +bool ffPrintMemory(FFMemoryOptions* options) { FFMemoryResult storage = {}; const char* error = ffDetectMemory(&storage); @@ -14,7 +14,7 @@ void ffPrintMemory(FFMemoryOptions* options) if(error) { ffPrintError(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate(); @@ -69,6 +69,8 @@ void ffPrintMemory(FFMemoryOptions* options) FF_FORMAT_ARG(percentageBar, "percentage-bar"), })); } + + return true; } void ffParseMemoryJsonObject(FFMemoryOptions* options, yyjson_val* module) @@ -94,7 +96,7 @@ void ffGenerateMemoryJsonConfig(FFMemoryOptions* options, yyjson_mut_doc* doc, y ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateMemoryJsonResult(FF_MAYBE_UNUSED FFMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateMemoryJsonResult(FF_MAYBE_UNUSED FFMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFMemoryResult storage = {}; const char* error = ffDetectMemory(&storage); @@ -102,12 +104,14 @@ void ffGenerateMemoryJsonResult(FF_MAYBE_UNUSED FFMemoryOptions* options, yyjson if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); yyjson_mut_obj_add_uint(doc, obj, "total", storage.bytesTotal); yyjson_mut_obj_add_uint(doc, obj, "used", storage.bytesUsed); + + return true; } void ffInitMemoryOptions(FFMemoryOptions* options) diff --git a/src/modules/memory/memory.h b/src/modules/memory/memory.h index 5c43b4f759..7ed026fc3d 100644 --- a/src/modules/memory/memory.h +++ b/src/modules/memory/memory.h @@ -4,7 +4,7 @@ #define FF_MEMORY_MODULE_NAME "Memory" -void ffPrintMemory(FFMemoryOptions* options); +bool ffPrintMemory(FFMemoryOptions* options); void ffInitMemoryOptions(FFMemoryOptions* options); void ffDestroyMemoryOptions(FFMemoryOptions* options); diff --git a/src/modules/monitor/monitor.c b/src/modules/monitor/monitor.c index 65af67a12b..46a4ad78e3 100644 --- a/src/modules/monitor/monitor.c +++ b/src/modules/monitor/monitor.c @@ -6,14 +6,14 @@ #include -void ffPrintMonitor(FFMonitorOptions* options) +bool ffPrintMonitor(FFMonitorOptions* options) { const FFDisplayServerResult* result = ffConnectDisplayServer(); if(!result->displays.length) { ffPrintError(FF_MONITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No display detected"); - return; + return false; } FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate(); @@ -84,6 +84,8 @@ void ffPrintMonitor(FFMonitorOptions* options) ffStrbufDestroy(&display->name); ++index; } + + return true; } void ffParseMonitorJsonObject(FFMonitorOptions* options, yyjson_val* module) @@ -104,9 +106,10 @@ void ffGenerateMonitorJsonConfig(FFMonitorOptions* options, yyjson_mut_doc* doc, ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateMonitorJsonResult(FF_MAYBE_UNUSED FFMonitorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateMonitorJsonResult(FF_MAYBE_UNUSED FFMonitorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { yyjson_mut_obj_add_str(doc, module, "error", "Monitor module is an alias of Display module"); + return false; } void ffInitMonitorOptions(FFMonitorOptions* options) diff --git a/src/modules/monitor/monitor.h b/src/modules/monitor/monitor.h index 17320b22ea..82f9491fe6 100644 --- a/src/modules/monitor/monitor.h +++ b/src/modules/monitor/monitor.h @@ -4,7 +4,7 @@ #define FF_MONITOR_MODULE_NAME "Monitor" -void ffPrintMonitor(FFMonitorOptions* options); +bool ffPrintMonitor(FFMonitorOptions* options); void ffInitMonitorOptions(FFMonitorOptions* options); void ffDestroyMonitorOptions(FFMonitorOptions* options); diff --git a/src/modules/mouse/mouse.c b/src/modules/mouse/mouse.c index 00ce7d4bac..f7afcccb09 100644 --- a/src/modules/mouse/mouse.c +++ b/src/modules/mouse/mouse.c @@ -21,7 +21,7 @@ static void printDevice(FFMouseOptions* options, const FFMouseDevice* device, ui } } -void ffPrintMouse(FFMouseOptions* options) +bool ffPrintMouse(FFMouseOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFMouseDevice)); @@ -30,13 +30,13 @@ void ffPrintMouse(FFMouseOptions* options) if(error) { ffPrintError(FF_MOUSE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(!result.length) { ffPrintError(FF_MOUSE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected"); - return; + return false; } uint8_t index = 0; @@ -46,6 +46,8 @@ void ffPrintMouse(FFMouseOptions* options) ffStrbufDestroy(&device->serial); ffStrbufDestroy(&device->name); } + + return true; } void ffParseMouseJsonObject(FFMouseOptions* options, yyjson_val* module) @@ -66,7 +68,7 @@ void ffGenerateMouseJsonConfig(FFMouseOptions* options, yyjson_mut_doc* doc, yyj ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateMouseJsonResult(FF_MAYBE_UNUSED FFMouseOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateMouseJsonResult(FF_MAYBE_UNUSED FFMouseOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFMouseDevice)); @@ -75,7 +77,7 @@ void ffGenerateMouseJsonResult(FF_MAYBE_UNUSED FFMouseOptions* options, yyjson_m if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -91,6 +93,8 @@ void ffGenerateMouseJsonResult(FF_MAYBE_UNUSED FFMouseOptions* options, yyjson_m ffStrbufDestroy(&device->serial); ffStrbufDestroy(&device->name); } + + return true; } void ffInitMouseOptions(FFMouseOptions* options) diff --git a/src/modules/mouse/mouse.h b/src/modules/mouse/mouse.h index 796c998e9e..afbc20ff9e 100644 --- a/src/modules/mouse/mouse.h +++ b/src/modules/mouse/mouse.h @@ -4,7 +4,7 @@ #define FF_MOUSE_MODULE_NAME "Mouse" -void ffPrintMouse(FFMouseOptions* options); +bool ffPrintMouse(FFMouseOptions* options); void ffInitMouseOptions(FFMouseOptions* options); void ffDestroyMouseOptions(FFMouseOptions* options); diff --git a/src/modules/netio/netio.c b/src/modules/netio/netio.c index 8e932d4057..fd5c90349e 100644 --- a/src/modules/netio/netio.c +++ b/src/modules/netio/netio.c @@ -32,7 +32,7 @@ static void formatKey(const FFNetIOOptions* options, FFNetIOResult* inf, uint32_ } } -void ffPrintNetIO(FFNetIOOptions* options) +bool ffPrintNetIO(FFNetIOOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFNetIOResult)); const char* error = ffDetectNetIO(&result, options); @@ -40,7 +40,7 @@ void ffPrintNetIO(FFNetIOOptions* options) if(error) { ffPrintError(FF_NETIO_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } ffListSort(&result, (const void*) sortInfs); @@ -101,6 +101,8 @@ void ffPrintNetIO(FFNetIOOptions* options) { ffStrbufDestroy(&inf->name); } + + return true; } void ffParseNetIOJsonObject(FFNetIOOptions* options, yyjson_val* module) @@ -153,7 +155,7 @@ void ffGenerateNetIOJsonConfig(FFNetIOOptions* options, yyjson_mut_doc* doc, yyj yyjson_mut_obj_add_uint(doc, module, "waitTime", options->waitTime); } -void ffGenerateNetIOJsonResult(FFNetIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateNetIOJsonResult(FFNetIOOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFNetIOResult)); const char* error = ffDetectNetIO(&result, options); @@ -161,7 +163,7 @@ void ffGenerateNetIOJsonResult(FFNetIOOptions* options, yyjson_mut_doc* doc, yyj if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -184,6 +186,8 @@ void ffGenerateNetIOJsonResult(FFNetIOOptions* options, yyjson_mut_doc* doc, yyj { ffStrbufDestroy(&inf->name); } + + return true; } void ffInitNetIOOptions(FFNetIOOptions* options) diff --git a/src/modules/netio/netio.h b/src/modules/netio/netio.h index b5468347ee..653b7e97ce 100644 --- a/src/modules/netio/netio.h +++ b/src/modules/netio/netio.h @@ -6,7 +6,7 @@ void ffPrepareNetIO(FFNetIOOptions* options); -void ffPrintNetIO(FFNetIOOptions* options); +bool ffPrintNetIO(FFNetIOOptions* options); void ffInitNetIOOptions(FFNetIOOptions* options); void ffDestroyNetIOOptions(FFNetIOOptions* options); diff --git a/src/modules/opencl/opencl.c b/src/modules/opencl/opencl.c index 64f0ba63e6..fd35e5bf5c 100644 --- a/src/modules/opencl/opencl.c +++ b/src/modules/opencl/opencl.c @@ -5,14 +5,14 @@ #include "modules/opencl/opencl.h" #include "util/stringUtils.h" -void ffPrintOpenCL(FFOpenCLOptions* options) +bool ffPrintOpenCL(FFOpenCLOptions* options) { FFOpenCLResult* result = ffDetectOpenCL(); if(result->error != NULL) { ffPrintError(FF_OPENCL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", result->error); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -28,6 +28,8 @@ void ffPrintOpenCL(FFOpenCLOptions* options) FF_FORMAT_ARG(result->vendor, "vendor"), })); } + + return true; } void ffParseOpenCLJsonObject(FFOpenCLOptions* options, yyjson_val* module) @@ -48,14 +50,14 @@ void ffGenerateOpenCLJsonConfig(FFOpenCLOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateOpenCLJsonResult(FF_MAYBE_UNUSED FFOpenCLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateOpenCLJsonResult(FF_MAYBE_UNUSED FFOpenCLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFOpenCLResult* result = ffDetectOpenCL(); if(result->error != NULL) { yyjson_mut_obj_add_str(doc, module, "error", result->error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -109,6 +111,8 @@ void ffGenerateOpenCLJsonResult(FF_MAYBE_UNUSED FFOpenCLOptions* options, yyjson yyjson_mut_obj_add_uint(doc, gpuObj, "deviceId", gpu->deviceId); } + + return true; } void ffInitOpenCLOptions(FFOpenCLOptions* options) diff --git a/src/modules/opencl/opencl.h b/src/modules/opencl/opencl.h index bf47c6fbe9..aafab369f1 100644 --- a/src/modules/opencl/opencl.h +++ b/src/modules/opencl/opencl.h @@ -4,7 +4,7 @@ #define FF_OPENCL_MODULE_NAME "OpenCL" -void ffPrintOpenCL(FFOpenCLOptions* options); +bool ffPrintOpenCL(FFOpenCLOptions* options); void ffInitOpenCLOptions(FFOpenCLOptions* options); void ffDestroyOpenCLOptions(FFOpenCLOptions* options); diff --git a/src/modules/opengl/opengl.c b/src/modules/opengl/opengl.c index 9dc99887ef..5dcc555407 100644 --- a/src/modules/opengl/opengl.c +++ b/src/modules/opengl/opengl.c @@ -4,8 +4,9 @@ #include "modules/opengl/opengl.h" #include "util/stringUtils.h" -void ffPrintOpenGL(FFOpenGLOptions* options) +bool ffPrintOpenGL(FFOpenGLOptions* options) { + bool success = false; FFOpenGLResult result; ffStrbufInit(&result.version); ffStrbufInit(&result.renderer); @@ -17,23 +18,25 @@ void ffPrintOpenGL(FFOpenGLOptions* options) if(error) { ffPrintError(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; - } - - if(options->moduleArgs.outputFormat.length == 0) - { - ffPrintLogoAndKey(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - puts(result.version.chars); } else { - FF_PRINT_FORMAT_CHECKED(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { - FF_FORMAT_ARG(result.version, "version"), - FF_FORMAT_ARG(result.renderer, "renderer"), - FF_FORMAT_ARG(result.vendor, "vendor"), - FF_FORMAT_ARG(result.slv, "slv"), - FF_FORMAT_ARG(result.library, "library"), - })); + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + puts(result.version.chars); + } + else + { + FF_PRINT_FORMAT_CHECKED(FF_OPENGL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { + FF_FORMAT_ARG(result.version, "version"), + FF_FORMAT_ARG(result.renderer, "renderer"), + FF_FORMAT_ARG(result.vendor, "vendor"), + FF_FORMAT_ARG(result.slv, "slv"), + FF_FORMAT_ARG(result.library, "library"), + })); + } + success = true; } ffStrbufDestroy(&result.version); @@ -41,6 +44,8 @@ void ffPrintOpenGL(FFOpenGLOptions* options) ffStrbufDestroy(&result.vendor); ffStrbufDestroy(&result.slv); ffStrbufDestroy(&result.library); + + return success; } void ffParseOpenGLJsonObject(FFOpenGLOptions* options, yyjson_val* module) @@ -90,8 +95,9 @@ void ffGenerateOpenGLJsonConfig(FFOpenGLOptions* options, yyjson_mut_doc* doc, y } } -void ffGenerateOpenGLJsonResult(FF_MAYBE_UNUSED FFOpenGLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateOpenGLJsonResult(FF_MAYBE_UNUSED FFOpenGLOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFOpenGLResult result; ffStrbufInit(&result.version); ffStrbufInit(&result.renderer); @@ -112,6 +118,7 @@ void ffGenerateOpenGLJsonResult(FF_MAYBE_UNUSED FFOpenGLOptions* options, yyjson yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &result.vendor); yyjson_mut_obj_add_strbuf(doc, obj, "slv", &result.slv); yyjson_mut_obj_add_strbuf(doc, obj, "library", &result.library); + success = true; } ffStrbufDestroy(&result.version); @@ -119,6 +126,8 @@ void ffGenerateOpenGLJsonResult(FF_MAYBE_UNUSED FFOpenGLOptions* options, yyjson ffStrbufDestroy(&result.vendor); ffStrbufDestroy(&result.slv); ffStrbufDestroy(&result.library); + + return success; } void ffInitOpenGLOptions(FFOpenGLOptions* options) diff --git a/src/modules/opengl/opengl.h b/src/modules/opengl/opengl.h index f60938d1e6..8caffa5a5b 100644 --- a/src/modules/opengl/opengl.h +++ b/src/modules/opengl/opengl.h @@ -4,7 +4,7 @@ #define FF_OPENGL_MODULE_NAME "OpenGL" -void ffPrintOpenGL(FFOpenGLOptions* options); +bool ffPrintOpenGL(FFOpenGLOptions* options); void ffInitOpenGLOptions(FFOpenGLOptions* options); void ffDestroyOpenGLOptions(FFOpenGLOptions* options); diff --git a/src/modules/os/os.c b/src/modules/os/os.c index c3b3efd661..4745225ec5 100644 --- a/src/modules/os/os.c +++ b/src/modules/os/os.c @@ -53,14 +53,14 @@ static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result) } } -void ffPrintOS(FFOSOptions* options) +bool ffPrintOS(FFOSOptions* options) { const FFOSResult* os = ffDetectOS(); if(os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0) { ffPrintError(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Could not detect OS"); - return; + return false; } FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate(); @@ -113,6 +113,8 @@ void ffPrintOS(FFOSOptions* options) FF_FORMAT_ARG(instance.state.platform.sysinfo.release, "kernel-release"), })); } + + return true; } void ffParseOSJsonObject(FFOSOptions* options, yyjson_val* module) @@ -133,14 +135,14 @@ void ffGenerateOSJsonConfig(FFOSOptions* options, yyjson_mut_doc* doc, yyjson_mu ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateOSJsonResult(FF_MAYBE_UNUSED FFOSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateOSJsonResult(FF_MAYBE_UNUSED FFOSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFOSResult* os = ffDetectOS(); if(os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0) { yyjson_mut_obj_add_str(doc, module, "error", "Could not detect OS"); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -154,6 +156,8 @@ void ffGenerateOSJsonResult(FF_MAYBE_UNUSED FFOSOptions* options, yyjson_mut_doc yyjson_mut_obj_add_strbuf(doc, obj, "variantID", &os->variantID); yyjson_mut_obj_add_strbuf(doc, obj, "version", &os->version); yyjson_mut_obj_add_strbuf(doc, obj, "versionID", &os->versionID); + + return true; } void ffInitOSOptions(FFOSOptions* options) @@ -176,7 +180,7 @@ void ffInitOSOptions(FFOSOptions* options) #elif __Haiku__ "" #else - "?" + "ó°¢»" #endif ); } diff --git a/src/modules/os/os.h b/src/modules/os/os.h index bf6ebf9d60..a53d28516e 100644 --- a/src/modules/os/os.h +++ b/src/modules/os/os.h @@ -4,7 +4,7 @@ #define FF_OS_MODULE_NAME "OS" -void ffPrintOS(FFOSOptions* options); +bool ffPrintOS(FFOSOptions* options); void ffInitOSOptions(FFOSOptions* options); void ffDestroyOSOptions(FFOSOptions* options); diff --git a/src/modules/packages/packages.c b/src/modules/packages/packages.c index d17911de01..1d1094ae70 100644 --- a/src/modules/packages/packages.c +++ b/src/modules/packages/packages.c @@ -4,7 +4,7 @@ #include "modules/packages/packages.h" #include "util/stringUtils.h" -void ffPrintPackages(FFPackagesOptions* options) +bool ffPrintPackages(FFPackagesOptions* options) { FFPackagesResult counts = {}; ffStrbufInit(&counts.pacmanBranch); @@ -14,7 +14,7 @@ void ffPrintPackages(FFPackagesOptions* options) if(error) { ffPrintError(FF_PACKAGES_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } uint32_t nixAll = counts.nixDefault + counts.nixSystem + counts.nixUser; @@ -203,6 +203,8 @@ void ffPrintPackages(FFPackagesOptions* options) } ffStrbufDestroy(&counts.pacmanBranch); + + return true; } void ffParsePackagesJsonObject(FFPackagesOptions* options, yyjson_val* module) @@ -370,7 +372,7 @@ void ffGeneratePackagesJsonConfig(FFPackagesOptions* options, yyjson_mut_doc* do yyjson_mut_obj_add_bool(doc, module, "combined", options->combined); } -void ffGeneratePackagesJsonResult(FF_MAYBE_UNUSED FFPackagesOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGeneratePackagesJsonResult(FF_MAYBE_UNUSED FFPackagesOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFPackagesResult counts = {}; ffStrbufInit(&counts.pacmanBranch); @@ -380,7 +382,7 @@ void ffGeneratePackagesJsonResult(FF_MAYBE_UNUSED FFPackagesOptions* options, yy if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -427,6 +429,8 @@ void ffGeneratePackagesJsonResult(FF_MAYBE_UNUSED FFPackagesOptions* options, yy FF_APPEND_PACKAGE_COUNT(winget) FF_APPEND_PACKAGE_COUNT(xbps) yyjson_mut_obj_add_strbuf(doc, obj, "pacmanBranch", &counts.pacmanBranch); + + return true; } void ffInitPackagesOptions(FFPackagesOptions* options) diff --git a/src/modules/packages/packages.h b/src/modules/packages/packages.h index 8b904d48db..4e58768702 100644 --- a/src/modules/packages/packages.h +++ b/src/modules/packages/packages.h @@ -4,7 +4,7 @@ #define FF_PACKAGES_MODULE_NAME "Packages" -void ffPrintPackages(FFPackagesOptions* options); +bool ffPrintPackages(FFPackagesOptions* options); void ffInitPackagesOptions(FFPackagesOptions* options); void ffDestroyPackagesOptions(FFPackagesOptions* options); diff --git a/src/modules/physicaldisk/physicaldisk.c b/src/modules/physicaldisk/physicaldisk.c index 72ffe85f08..e6dca99d16 100644 --- a/src/modules/physicaldisk/physicaldisk.c +++ b/src/modules/physicaldisk/physicaldisk.c @@ -31,7 +31,7 @@ static void formatKey(const FFPhysicalDiskOptions* options, FFPhysicalDiskResult } } -void ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) +bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalDiskResult)); const char* error = ffDetectPhysicalDisk(&result, options); @@ -39,7 +39,7 @@ void ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) if(error) { ffPrintError(FF_PHYSICALDISK_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } ffListSort(&result, (const void*) sortDevices); @@ -131,6 +131,8 @@ void ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) ffStrbufDestroy(&dev->serial); ffStrbufDestroy(&dev->revision); } + + return true; } void ffParsePhysicalDiskJsonObject(FFPhysicalDiskOptions* options, yyjson_val* module) @@ -164,7 +166,7 @@ void ffGeneratePhysicalDiskJsonConfig(FFPhysicalDiskOptions* options, yyjson_mut ffTempsGenerateJsonConfig(doc, module, options->temp, options->tempConfig); } -void ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalDiskResult)); const char* error = ffDetectPhysicalDisk(&result, options); @@ -172,7 +174,7 @@ void ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -223,6 +225,8 @@ void ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut ffStrbufDestroy(&dev->serial); ffStrbufDestroy(&dev->revision); } + + return true; } void ffInitPhysicalDiskOptions(FFPhysicalDiskOptions* options) diff --git a/src/modules/physicaldisk/physicaldisk.h b/src/modules/physicaldisk/physicaldisk.h index e73b3f358d..aaad8883e6 100644 --- a/src/modules/physicaldisk/physicaldisk.h +++ b/src/modules/physicaldisk/physicaldisk.h @@ -4,7 +4,7 @@ #define FF_PHYSICALDISK_MODULE_NAME "PhysicalDisk" -void ffPrintPhysicalDisk(FFPhysicalDiskOptions* options); +bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options); void ffInitPhysicalDiskOptions(FFPhysicalDiskOptions* options); void ffDestroyPhysicalDiskOptions(FFPhysicalDiskOptions* options); diff --git a/src/modules/physicalmemory/physicalmemory.c b/src/modules/physicalmemory/physicalmemory.c index 09534595d6..81e593f3f5 100644 --- a/src/modules/physicalmemory/physicalmemory.c +++ b/src/modules/physicalmemory/physicalmemory.c @@ -8,7 +8,7 @@ #define FF_PHYSICALMEMORY_DISPLAY_NAME "Physical Memory" -void ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options) +bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalMemoryResult)); const char* error = ffDetectPhysicalMemory(&result); @@ -16,13 +16,13 @@ void ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options) if(error) { ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if (result.length == 0) { ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No physical memory detected"); - return; + return false; } FF_STRBUF_AUTO_DESTROY prettySize = ffStrbufCreate(); @@ -78,6 +78,8 @@ void ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options) ffStrbufDestroy(&device->serial); ffStrbufDestroy(&device->partNumber); } + + return true; } void ffParsePhysicalMemoryJsonObject(FFPhysicalMemoryOptions* options, yyjson_val* module) @@ -98,7 +100,7 @@ void ffGeneratePhysicalMemoryJsonConfig(FFPhysicalMemoryOptions* options, yyjson ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFPhysicalMemoryResult)); const char* error = ffDetectPhysicalMemory(&result); @@ -106,7 +108,7 @@ void ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions* if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -134,6 +136,8 @@ void ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions* ffStrbufDestroy(&device->serial); ffStrbufDestroy(&device->partNumber); } + + return true; } void ffInitPhysicalMemoryOptions(FFPhysicalMemoryOptions* options) diff --git a/src/modules/physicalmemory/physicalmemory.h b/src/modules/physicalmemory/physicalmemory.h index 7df2b6c94b..feb3d1d293 100644 --- a/src/modules/physicalmemory/physicalmemory.h +++ b/src/modules/physicalmemory/physicalmemory.h @@ -4,7 +4,7 @@ #define FF_PHYSICALMEMORY_MODULE_NAME "PhysicalMemory" -void ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options); +bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options); void ffInitPhysicalMemoryOptions(FFPhysicalMemoryOptions* options); void ffDestroyPhysicalMemoryOptions(FFPhysicalMemoryOptions* options); diff --git a/src/modules/player/player.c b/src/modules/player/player.c index 347a2977ea..69002cb488 100644 --- a/src/modules/player/player.c +++ b/src/modules/player/player.c @@ -8,20 +8,20 @@ #define FF_PLAYER_DISPLAY_NAME "Media Player" -void ffPrintPlayer(FFPlayerOptions* options) +bool ffPrintPlayer(FFPlayerOptions* options) { const FFMediaResult* media = ffDetectMedia(); if(media->error.length > 0) { ffPrintError(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", media->error.chars); - return; + return false; } if (media->player.length == 0) { ffPrintError(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No media player detected"); - return; + return false; } FF_STRBUF_AUTO_DESTROY playerPretty = ffStrbufCreate(); @@ -79,6 +79,8 @@ void ffPrintPlayer(FFPlayerOptions* options) FF_FORMAT_ARG(media->url, "url"), })); } + + return true; } void ffParsePlayerJsonObject(FFPlayerOptions* options, yyjson_val* module) @@ -99,9 +101,10 @@ void ffGeneratePlayerJsonConfig(FFPlayerOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGeneratePlayerJsonResult(FF_MAYBE_UNUSED FFMediaOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGeneratePlayerJsonResult(FF_MAYBE_UNUSED FFMediaOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { yyjson_mut_obj_add_str(doc, module, "error", "Player module is an alias of Media module"); + return false; } void ffInitPlayerOptions(FFPlayerOptions* options) diff --git a/src/modules/player/player.h b/src/modules/player/player.h index b99a6da6b5..8b18d29828 100644 --- a/src/modules/player/player.h +++ b/src/modules/player/player.h @@ -5,7 +5,7 @@ #define FF_PLAYER_MODULE_NAME "Player" -void ffPrintPlayer(FFPlayerOptions* options); +bool ffPrintPlayer(FFPlayerOptions* options); void ffInitPlayerOptions(FFPlayerOptions* options); void ffDestroyPlayerOptions(FFPlayerOptions* options); diff --git a/src/modules/poweradapter/poweradapter.c b/src/modules/poweradapter/poweradapter.c index 00c96ab0e4..1361bc60d1 100644 --- a/src/modules/poweradapter/poweradapter.c +++ b/src/modules/poweradapter/poweradapter.c @@ -6,7 +6,7 @@ #define FF_POWERADAPTER_DISPLAY_NAME "Power Adapter" -void ffPrintPowerAdapter(FFPowerAdapterOptions* options) +bool ffPrintPowerAdapter(FFPowerAdapterOptions* options) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFPowerAdapterResult)); @@ -15,45 +15,48 @@ void ffPrintPowerAdapter(FFPowerAdapterOptions* options) if (error) { ffPrintError(FF_POWERADAPTER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); + return false; } - else if(results.length == 0) + + if(results.length == 0) { ffPrintError(FF_POWERADAPTER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No power adapters found"); + return false; } - else + + for(uint8_t i = 0; i < (uint8_t) results.length; i++) { - for(uint8_t i = 0; i < (uint8_t) results.length; i++) - { - FFPowerAdapterResult* result = FF_LIST_GET(FFPowerAdapterResult, results, i); + FFPowerAdapterResult* result = FF_LIST_GET(FFPowerAdapterResult, results, i); - if(options->moduleArgs.outputFormat.length == 0) - { - ffPrintLogoAndKey(FF_POWERADAPTER_DISPLAY_NAME, i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(FF_POWERADAPTER_DISPLAY_NAME, i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - if(result->name.length > 0) - puts(result->name.chars); - else - printf("%dW\n", result->watts); - } + if(result->name.length > 0) + puts(result->name.chars); else - { - FF_PRINT_FORMAT_CHECKED(FF_POWERADAPTER_DISPLAY_NAME, i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ - FF_FORMAT_ARG(result->watts, "watts"), - FF_FORMAT_ARG(result->name, "name"), - FF_FORMAT_ARG(result->manufacturer, "manufacturer"), - FF_FORMAT_ARG(result->modelName, "model-name"), - FF_FORMAT_ARG(result->description, "description"), - FF_FORMAT_ARG(result->serial, "serial"), - })); - } - - ffStrbufDestroy(&result->manufacturer); - ffStrbufDestroy(&result->description); - ffStrbufDestroy(&result->modelName); - ffStrbufDestroy(&result->name); - ffStrbufDestroy(&result->serial); + printf("%dW\n", result->watts); } + else + { + FF_PRINT_FORMAT_CHECKED(FF_POWERADAPTER_DISPLAY_NAME, i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ + FF_FORMAT_ARG(result->watts, "watts"), + FF_FORMAT_ARG(result->name, "name"), + FF_FORMAT_ARG(result->manufacturer, "manufacturer"), + FF_FORMAT_ARG(result->modelName, "model-name"), + FF_FORMAT_ARG(result->description, "description"), + FF_FORMAT_ARG(result->serial, "serial"), + })); + } + + ffStrbufDestroy(&result->manufacturer); + ffStrbufDestroy(&result->description); + ffStrbufDestroy(&result->modelName); + ffStrbufDestroy(&result->name); + ffStrbufDestroy(&result->serial); } + + return true; } void ffGeneratePowerAdapterJsonConfig(FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) @@ -74,7 +77,7 @@ void ffParsePowerAdapterJsonObject(FFPowerAdapterOptions* options, yyjson_val* m } } -void ffGeneratePowerAdapterJsonResult(FF_MAYBE_UNUSED FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGeneratePowerAdapterJsonResult(FF_MAYBE_UNUSED FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFPowerAdapterResult)); @@ -83,7 +86,7 @@ void ffGeneratePowerAdapterJsonResult(FF_MAYBE_UNUSED FFPowerAdapterOptions* opt if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -106,6 +109,8 @@ void ffGeneratePowerAdapterJsonResult(FF_MAYBE_UNUSED FFPowerAdapterOptions* opt ffStrbufDestroy(&item->name); ffStrbufDestroy(&item->serial); } + + return true; } void ffInitPowerAdapterOptions(FFPowerAdapterOptions* options) diff --git a/src/modules/poweradapter/poweradapter.h b/src/modules/poweradapter/poweradapter.h index 165060a5e4..3e61025506 100644 --- a/src/modules/poweradapter/poweradapter.h +++ b/src/modules/poweradapter/poweradapter.h @@ -4,7 +4,7 @@ #define FF_POWERADAPTER_MODULE_NAME "PowerAdapter" -void ffPrintPowerAdapter(FFPowerAdapterOptions* options); +bool ffPrintPowerAdapter(FFPowerAdapterOptions* options); void ffInitPowerAdapterOptions(FFPowerAdapterOptions* options); void ffDestroyPowerAdapterOptions(FFPowerAdapterOptions* options); diff --git a/src/modules/processes/processes.c b/src/modules/processes/processes.c index e7740a448a..fcec4c0416 100644 --- a/src/modules/processes/processes.c +++ b/src/modules/processes/processes.c @@ -4,7 +4,7 @@ #include "modules/processes/processes.h" #include "util/stringUtils.h" -void ffPrintProcesses(FFProcessesOptions* options) +bool ffPrintProcesses(FFProcessesOptions* options) { uint32_t numProcesses = 0; const char* error = ffDetectProcesses(&numProcesses); @@ -12,7 +12,7 @@ void ffPrintProcesses(FFProcessesOptions* options) if(error) { ffPrintError(FF_PROCESSES_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -27,6 +27,8 @@ void ffPrintProcesses(FFProcessesOptions* options) FF_FORMAT_ARG(numProcesses, "result") })); } + + return true; } void ffParseProcessesJsonObject(FFProcessesOptions* options, yyjson_val* module) @@ -47,7 +49,7 @@ void ffGenerateProcessesJsonConfig(FFProcessesOptions* options, yyjson_mut_doc* ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateProcessesJsonResult(FF_MAYBE_UNUSED FFProcessesOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateProcessesJsonResult(FF_MAYBE_UNUSED FFProcessesOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { uint32_t result; const char* error = ffDetectProcesses(&result); @@ -55,10 +57,12 @@ void ffGenerateProcessesJsonResult(FF_MAYBE_UNUSED FFProcessesOptions* options, if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_obj_add_uint(doc, module, "result", result); + + return true; } void ffInitProcessesOptions(FFProcessesOptions* options) diff --git a/src/modules/processes/processes.h b/src/modules/processes/processes.h index d9f0ae3d82..0220c17dd0 100644 --- a/src/modules/processes/processes.h +++ b/src/modules/processes/processes.h @@ -4,7 +4,7 @@ #define FF_PROCESSES_MODULE_NAME "Processes" -void ffPrintProcesses(FFProcessesOptions* options); +bool ffPrintProcesses(FFProcessesOptions* options); void ffInitProcessesOptions(FFProcessesOptions* options); void ffDestroyProcessesOptions(FFProcessesOptions* options); diff --git a/src/modules/publicip/publicip.c b/src/modules/publicip/publicip.c index e52696dd65..493d3d5314 100644 --- a/src/modules/publicip/publicip.c +++ b/src/modules/publicip/publicip.c @@ -6,7 +6,7 @@ #define FF_PUBLICIP_DISPLAY_NAME "Public IP" -void ffPrintPublicIp(FFPublicIPOptions* options) +bool ffPrintPublicIp(FFPublicIPOptions* options) { FFPublicIpResult result; ffStrbufInit(&result.ip); @@ -16,7 +16,7 @@ void ffPrintPublicIp(FFPublicIPOptions* options) if (error) { ffPrintError(FF_PUBLICIP_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if (options->moduleArgs.outputFormat.length == 0) @@ -37,6 +37,8 @@ void ffPrintPublicIp(FFPublicIPOptions* options) ffStrbufDestroy(&result.ip); ffStrbufDestroy(&result.location); + + return true; } void ffParsePublicIpJsonObject(FFPublicIPOptions* options, yyjson_val* module) @@ -81,7 +83,7 @@ void ffGeneratePublicIpJsonConfig(FFPublicIPOptions* options, yyjson_mut_doc* do yyjson_mut_obj_add_bool(doc, module, "ipv6", options->ipv6); } -void ffGeneratePublicIpJsonResult(FFPublicIPOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGeneratePublicIpJsonResult(FFPublicIPOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFPublicIpResult result; ffStrbufInit(&result.ip); @@ -91,7 +93,7 @@ void ffGeneratePublicIpJsonResult(FFPublicIPOptions* options, yyjson_mut_doc* do if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -100,6 +102,8 @@ void ffGeneratePublicIpJsonResult(FFPublicIPOptions* options, yyjson_mut_doc* do ffStrbufDestroy(&result.ip); ffStrbufDestroy(&result.location); + + return true; } void ffInitPublicIpOptions(FFPublicIPOptions* options) diff --git a/src/modules/publicip/publicip.h b/src/modules/publicip/publicip.h index 5fd70ab4f3..92768cbb6e 100644 --- a/src/modules/publicip/publicip.h +++ b/src/modules/publicip/publicip.h @@ -6,7 +6,7 @@ void ffPreparePublicIp(FFPublicIPOptions* options); -void ffPrintPublicIp(FFPublicIPOptions* options); +bool ffPrintPublicIp(FFPublicIPOptions* options); void ffInitPublicIpOptions(FFPublicIPOptions* options); void ffDestroyPublicIpOptions(FFPublicIPOptions* options); diff --git a/src/modules/separator/separator.c b/src/modules/separator/separator.c index 4e31590c75..79ca19f961 100644 --- a/src/modules/separator/separator.c +++ b/src/modules/separator/separator.c @@ -33,7 +33,7 @@ static inline uint32_t getWcsWidth(const FFstrbuf* mbstr, wchar_t* wstr, mbstate return result > 0 ? (uint32_t) result : mbstr->length; } -void ffPrintSeparator(FFSeparatorOptions* options) +bool ffPrintSeparator(FFSeparatorOptions* options) { ffLogoPrintLine(); @@ -111,6 +111,8 @@ void ffPrintSeparator(FFSeparatorOptions* options) if(options->outputColor.length && !instance.config.display.pipe) fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout); putchar('\n'); + + return true; } void ffParseSeparatorJsonObject(FFSeparatorOptions* options, yyjson_val* module) diff --git a/src/modules/separator/separator.h b/src/modules/separator/separator.h index d23fc6c943..eebf39af36 100644 --- a/src/modules/separator/separator.h +++ b/src/modules/separator/separator.h @@ -4,7 +4,7 @@ #define FF_SEPARATOR_MODULE_NAME "Separator" -void ffPrintSeparator(FFSeparatorOptions* options); +bool ffPrintSeparator(FFSeparatorOptions* options); void ffInitSeparatorOptions(FFSeparatorOptions* options); void ffDestroySeparatorOptions(FFSeparatorOptions* options); diff --git a/src/modules/shell/shell.c b/src/modules/shell/shell.c index 42bb9149d8..73feb01194 100644 --- a/src/modules/shell/shell.c +++ b/src/modules/shell/shell.c @@ -4,14 +4,14 @@ #include "modules/shell/shell.h" #include "util/stringUtils.h" -void ffPrintShell(FFShellOptions* options) +bool ffPrintShell(FFShellOptions* options) { const FFShellResult* result = ffDetectShell(); if(result->processName.length == 0) { ffPrintError(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Couldn't detect shell"); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -40,6 +40,8 @@ void ffPrintShell(FFShellOptions* options) FF_FORMAT_ARG(result->tty, "tty"), })); } + + return true; } void ffParseShellJsonObject(FFShellOptions* options, yyjson_val* module) @@ -60,14 +62,14 @@ void ffGenerateShellJsonConfig(FFShellOptions* options, yyjson_mut_doc* doc, yyj ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateShellJsonResult(FF_MAYBE_UNUSED FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateShellJsonResult(FF_MAYBE_UNUSED FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFShellResult* result = ffDetectShell(); if(result->processName.length == 0) { yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect shell"); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -83,6 +85,8 @@ void ffGenerateShellJsonResult(FF_MAYBE_UNUSED FFShellOptions* options, yyjson_m yyjson_mut_obj_add_int(doc, obj, "tty", result->tty); else yyjson_mut_obj_add_null(doc, obj, "tty"); + + return true; } void ffInitShellOptions(FFShellOptions* options) diff --git a/src/modules/shell/shell.h b/src/modules/shell/shell.h index db39aa9643..50a094c80a 100644 --- a/src/modules/shell/shell.h +++ b/src/modules/shell/shell.h @@ -4,7 +4,7 @@ #define FF_SHELL_MODULE_NAME "Shell" -void ffPrintShell(FFShellOptions* options); +bool ffPrintShell(FFShellOptions* options); void ffInitShellOptions(FFShellOptions* options); void ffDestroyShellOptions(FFShellOptions* options); diff --git a/src/modules/sound/sound.c b/src/modules/sound/sound.c index 1b4d0285cd..e87af35d69 100644 --- a/src/modules/sound/sound.c +++ b/src/modules/sound/sound.c @@ -66,8 +66,9 @@ static void printDevice(FFSoundOptions* options, const FFSoundDevice* device, ui } } -void ffPrintSound(FFSoundOptions* options) +bool ffPrintSound(FFSoundOptions* options) { + bool success = false; FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSoundDevice)); const char* error = ffDetectSound(&result); @@ -75,41 +76,47 @@ void ffPrintSound(FFSoundOptions* options) if(error) { ffPrintError(FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + goto exit; } - FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFSoundDevice*)); - - FF_LIST_FOR_EACH(FFSoundDevice, device, result) { - switch (options->soundType) + FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFSoundDevice*)); + + FF_LIST_FOR_EACH(FFSoundDevice, device, result) { - case FF_SOUND_TYPE_MAIN: if (!device->main) continue; break; - case FF_SOUND_TYPE_ACTIVE: if (!device->active) continue; break; - case FF_SOUND_TYPE_ALL: break; - } + switch (options->soundType) + { + case FF_SOUND_TYPE_MAIN: if (!device->main) continue; break; + case FF_SOUND_TYPE_ACTIVE: if (!device->active) continue; break; + case FF_SOUND_TYPE_ALL: break; + } - *(FFSoundDevice**)ffListAdd(&filtered) = device; - } + *(FFSoundDevice**)ffListAdd(&filtered) = device; + } - if(filtered.length == 0) - { - ffPrintError(FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No active sound devices found"); - return; - } + if(filtered.length == 0) + { + ffPrintError(FF_SOUND_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No active sound devices found"); + goto exit; + } - uint8_t index = 1; - FF_LIST_FOR_EACH(FFSoundDevice*, device, filtered) - { - printDevice(options, *device, filtered.length == 1 ? 0 : index++); + uint8_t index = 1; + FF_LIST_FOR_EACH(FFSoundDevice*, device, filtered) + { + printDevice(options, *device, filtered.length == 1 ? 0 : index++); + } } + success = true; +exit: FF_LIST_FOR_EACH(FFSoundDevice, device, result) { ffStrbufDestroy(&device->identifier); ffStrbufDestroy(&device->name); ffStrbufDestroy(&device->platformApi); } + + return success; } void ffParseSoundJsonObject(FFSoundOptions* options, yyjson_val* module) @@ -164,7 +171,7 @@ void ffGenerateSoundJsonConfig(FFSoundOptions* options, yyjson_mut_doc* doc, yyj ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateSoundJsonResult(FF_MAYBE_UNUSED FFSoundOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateSoundJsonResult(FF_MAYBE_UNUSED FFSoundOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSoundDevice)); const char* error = ffDetectSound(&result); @@ -172,7 +179,7 @@ void ffGenerateSoundJsonResult(FF_MAYBE_UNUSED FFSoundOptions* options, yyjson_m if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -198,6 +205,8 @@ void ffGenerateSoundJsonResult(FF_MAYBE_UNUSED FFSoundOptions* options, yyjson_m ffStrbufDestroy(&device->name); ffStrbufDestroy(&device->platformApi); } + + return true; } void ffInitSoundOptions(FFSoundOptions* options) diff --git a/src/modules/sound/sound.h b/src/modules/sound/sound.h index 80766b5ae4..a23f8d5e1b 100644 --- a/src/modules/sound/sound.h +++ b/src/modules/sound/sound.h @@ -4,7 +4,7 @@ #define FF_SOUND_MODULE_NAME "Sound" -void ffPrintSound(FFSoundOptions* options); +bool ffPrintSound(FFSoundOptions* options); void ffInitSoundOptions(FFSoundOptions* options); void ffDestroySoundOptions(FFSoundOptions* options); diff --git a/src/modules/swap/swap.c b/src/modules/swap/swap.c index 45e01c77bb..d336cf972a 100644 --- a/src/modules/swap/swap.c +++ b/src/modules/swap/swap.c @@ -91,7 +91,7 @@ void printSwap(FFSwapOptions* options, uint8_t index, FFSwapResult* storage) } } -void ffPrintSwap(FFSwapOptions* options) +bool ffPrintSwap(FFSwapOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSwapResult)); const char* error = ffDetectSwap(&result); @@ -99,7 +99,7 @@ void ffPrintSwap(FFSwapOptions* options) if(error) { ffPrintError(FF_SWAP_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if (options->separate) @@ -129,6 +129,8 @@ void ffPrintSwap(FFSwapOptions* options) { ffStrbufDestroy(&storage->name); } + + return true; } void ffParseSwapJsonObject(FFSwapOptions* options, yyjson_val* module) @@ -161,7 +163,7 @@ void ffGenerateSwapJsonConfig(FFSwapOptions* options, yyjson_mut_doc* doc, yyjso yyjson_mut_obj_add_bool(doc, module, "separate", options->separate); } -void ffGenerateSwapJsonResult(FF_MAYBE_UNUSED FFSwapOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateSwapJsonResult(FF_MAYBE_UNUSED FFSwapOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFSwapResult)); const char* error = ffDetectSwap(&result); @@ -169,7 +171,7 @@ void ffGenerateSwapJsonResult(FF_MAYBE_UNUSED FFSwapOptions* options, yyjson_mut if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -185,6 +187,8 @@ void ffGenerateSwapJsonResult(FF_MAYBE_UNUSED FFSwapOptions* options, yyjson_mut { ffStrbufDestroy(&storage->name); } + + return true; } void ffInitSwapOptions(FFSwapOptions* options) diff --git a/src/modules/swap/swap.h b/src/modules/swap/swap.h index 7f31ecd01a..e6b9a8b8ee 100644 --- a/src/modules/swap/swap.h +++ b/src/modules/swap/swap.h @@ -4,7 +4,7 @@ #define FF_SWAP_MODULE_NAME "Swap" -void ffPrintSwap(FFSwapOptions* options); +bool ffPrintSwap(FFSwapOptions* options); void ffInitSwapOptions(FFSwapOptions* options); void ffDestroySwapOptions(FFSwapOptions* options); diff --git a/src/modules/terminal/terminal.c b/src/modules/terminal/terminal.c index f5085421dd..fdaf93dcc8 100644 --- a/src/modules/terminal/terminal.c +++ b/src/modules/terminal/terminal.c @@ -4,14 +4,14 @@ #include "modules/terminal/terminal.h" #include "util/stringUtils.h" -void ffPrintTerminal(FFTerminalOptions* options) +bool ffPrintTerminal(FFTerminalOptions* options) { const FFTerminalResult* result = ffDetectTerminal(); if(result->processName.length == 0) { ffPrintError(FF_TERMINAL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Couldn't detect terminal"); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -36,6 +36,8 @@ void ffPrintTerminal(FFTerminalOptions* options) FF_FORMAT_ARG(result->tty, "tty"), })); } + + return true; } void ffParseTerminalJsonObject(FFTerminalOptions* options, yyjson_val* module) @@ -56,14 +58,14 @@ void ffGenerateTerminalJsonConfig(FFTerminalOptions* options, yyjson_mut_doc* do ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateTerminalJsonResult(FF_MAYBE_UNUSED FFTerminalOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateTerminalJsonResult(FF_MAYBE_UNUSED FFTerminalOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFTerminalResult* result = ffDetectTerminal(); if(result->processName.length == 0) { yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect terminal"); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -76,6 +78,8 @@ void ffGenerateTerminalJsonResult(FF_MAYBE_UNUSED FFTerminalOptions* options, yy yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &result->prettyName); yyjson_mut_obj_add_strbuf(doc, obj, "version", &result->version); yyjson_mut_obj_add_strbuf(doc, obj, "tty", &result->tty); + + return true; } void ffInitTerminalOptions(FFTerminalOptions* options) diff --git a/src/modules/terminal/terminal.h b/src/modules/terminal/terminal.h index df30bfadf7..bb7c9a10d8 100644 --- a/src/modules/terminal/terminal.h +++ b/src/modules/terminal/terminal.h @@ -4,7 +4,7 @@ #define FF_TERMINAL_MODULE_NAME "Terminal" -void ffPrintTerminal(FFTerminalOptions* options); +bool ffPrintTerminal(FFTerminalOptions* options); void ffInitTerminalOptions(FFTerminalOptions* options); void ffDestroyTerminalOptions(FFTerminalOptions* options); diff --git a/src/modules/terminalfont/terminalfont.c b/src/modules/terminalfont/terminalfont.c index 7ba2fc6730..b99ed6edad 100644 --- a/src/modules/terminalfont/terminalfont.c +++ b/src/modules/terminalfont/terminalfont.c @@ -6,8 +6,9 @@ #define FF_TERMINALFONT_DISPLAY_NAME "Terminal Font" -void ffPrintTerminalFont(FFTerminalFontOptions* options) +bool ffPrintTerminalFont(FFTerminalFontOptions* options) { + bool success = false; FFTerminalFontResult terminalFont; ffFontInit(&terminalFont.font); ffFontInit(&terminalFont.fallback); @@ -39,11 +40,14 @@ void ffPrintTerminalFont(FFTerminalFontOptions* options) FF_FORMAT_ARG(terminalFont.font.styles, "styles"), })); } + success = true; } ffStrbufDestroy(&terminalFont.error); ffFontDestroy(&terminalFont.font); ffFontDestroy(&terminalFont.fallback); + + return success; } void ffParseTerminalFontJsonObject(FFTerminalFontOptions* options, yyjson_val* module) @@ -64,8 +68,9 @@ void ffGenerateTerminalFontJsonConfig(FFTerminalFontOptions* options, yyjson_mut ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateTerminalFontJsonResult(FF_MAYBE_UNUSED FFTerminalFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateTerminalFontJsonResult(FF_MAYBE_UNUSED FFTerminalFontOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + bool success = false; FFTerminalFontResult result; ffFontInit(&result.font); ffFontInit(&result.fallback); @@ -96,11 +101,13 @@ void ffGenerateTerminalFontJsonResult(FF_MAYBE_UNUSED FFTerminalFontOptions* opt yyjson_mut_arr_add_strbuf(doc, fallbackStyles, style); } yyjson_mut_obj_add_strbuf(doc, fallback, "pretty", &result.fallback.pretty); + success = true; } ffStrbufDestroy(&result.error); ffFontDestroy(&result.font); ffFontDestroy(&result.fallback); + return success; } void ffInitTerminalFontOptions(FFTerminalFontOptions* options) diff --git a/src/modules/terminalfont/terminalfont.h b/src/modules/terminalfont/terminalfont.h index c1716912ab..04987a61ea 100644 --- a/src/modules/terminalfont/terminalfont.h +++ b/src/modules/terminalfont/terminalfont.h @@ -4,7 +4,7 @@ #define FF_TERMINALFONT_MODULE_NAME "TerminalFont" -void ffPrintTerminalFont(FFTerminalFontOptions* options); +bool ffPrintTerminalFont(FFTerminalFontOptions* options); void ffInitTerminalFontOptions(FFTerminalFontOptions* options); void ffDestroyTerminalFontOptions(FFTerminalFontOptions* options); diff --git a/src/modules/terminalsize/terminalsize.c b/src/modules/terminalsize/terminalsize.c index bc220107e2..b604c597ce 100644 --- a/src/modules/terminalsize/terminalsize.c +++ b/src/modules/terminalsize/terminalsize.c @@ -6,36 +6,36 @@ #define FF_TERMINALSIZE_DISPLAY_NAME "Terminal Size" -void ffPrintTerminalSize(FFTerminalSizeOptions* options) +bool ffPrintTerminalSize(FFTerminalSizeOptions* options) { FFTerminalSizeResult result = {}; if(!ffDetectTerminalSize(&result)) { ffPrintError(FF_TERMINALSIZE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Failed to detect terminal size"); + return false; + } + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(FF_TERMINALSIZE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + printf("%u columns x %u rows", result.columns, result.rows); + + if (result.width != 0 && result.height != 0) + printf(" (%upx x %upx)", result.width, result.height); + + putchar('\n'); } else { - if(options->moduleArgs.outputFormat.length == 0) - { - ffPrintLogoAndKey(FF_TERMINALSIZE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - printf("%u columns x %u rows", result.columns, result.rows); - - if (result.width != 0 && result.height != 0) - printf(" (%upx x %upx)", result.width, result.height); - - putchar('\n'); - } - else - { - FF_PRINT_FORMAT_CHECKED(FF_TERMINALSIZE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ - FF_FORMAT_ARG(result.rows, "rows"), - FF_FORMAT_ARG(result.columns, "columns"), - FF_FORMAT_ARG(result.width, "width"), - FF_FORMAT_ARG(result.height, "height"), - })); - } + FF_PRINT_FORMAT_CHECKED(FF_TERMINALSIZE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ + FF_FORMAT_ARG(result.rows, "rows"), + FF_FORMAT_ARG(result.columns, "columns"), + FF_FORMAT_ARG(result.width, "width"), + FF_FORMAT_ARG(result.height, "height"), + })); } + return true; } void ffParseTerminalSizeJsonObject(FFTerminalSizeOptions* options, yyjson_val* module) @@ -56,14 +56,14 @@ void ffGenerateTerminalSizeJsonConfig(FFTerminalSizeOptions* options, yyjson_mut ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateTerminalSizeJsonResult(FF_MAYBE_UNUSED FFTerminalSizeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateTerminalSizeJsonResult(FF_MAYBE_UNUSED FFTerminalSizeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFTerminalSizeResult result; if(!ffDetectTerminalSize(&result)) { yyjson_mut_obj_add_str(doc, module, "error", "Failed to detect terminal size"); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -71,6 +71,8 @@ void ffGenerateTerminalSizeJsonResult(FF_MAYBE_UNUSED FFTerminalSizeOptions* opt yyjson_mut_obj_add_uint(doc, obj, "rows", result.rows); yyjson_mut_obj_add_uint(doc, obj, "width", result.width); yyjson_mut_obj_add_uint(doc, obj, "height", result.height); + + return true; } void ffInitTerminalSizeOptions(FFTerminalSizeOptions* options) diff --git a/src/modules/terminalsize/terminalsize.h b/src/modules/terminalsize/terminalsize.h index 859d09b0c1..3cd05d5b23 100644 --- a/src/modules/terminalsize/terminalsize.h +++ b/src/modules/terminalsize/terminalsize.h @@ -4,7 +4,7 @@ #define FF_TERMINALSIZE_MODULE_NAME "TerminalSize" -void ffPrintTerminalSize(FFTerminalSizeOptions* options); +bool ffPrintTerminalSize(FFTerminalSizeOptions* options); void ffInitTerminalSizeOptions(FFTerminalSizeOptions* options); void ffDestroyTerminalSizeOptions(FFTerminalSizeOptions* options); diff --git a/src/modules/terminaltheme/terminaltheme.c b/src/modules/terminaltheme/terminaltheme.c index e6286dd6a3..f77d5e60ce 100644 --- a/src/modules/terminaltheme/terminaltheme.c +++ b/src/modules/terminaltheme/terminaltheme.c @@ -8,39 +8,40 @@ #define FF_TERMINALTHEME_DISPLAY_NAME "Terminal Theme" -void ffPrintTerminalTheme(FFTerminalThemeOptions* options) +bool ffPrintTerminalTheme(FFTerminalThemeOptions* options) { FFTerminalThemeResult result = {}; if(!ffDetectTerminalTheme(&result, false)) { ffPrintError(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Failed to detect terminal theme"); + return false; + } + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + printf("#%02" PRIX16 "%02" PRIX16 "%02" PRIX16 " (FG) - #%02" PRIX16 "%02" PRIX16 "%02" PRIX16 " (BG) [%s]\n", + result.fg.r, result.fg.g, result.fg.b, + result.bg.r, result.bg.g, result.bg.b, + result.bg.dark ? "Dark" : "Light"); } else { - if(options->moduleArgs.outputFormat.length == 0) - { - ffPrintLogoAndKey(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - printf("#%02" PRIX16 "%02" PRIX16 "%02" PRIX16 " (FG) - #%02" PRIX16 "%02" PRIX16 "%02" PRIX16 " (BG) [%s]\n", - result.fg.r, result.fg.g, result.fg.b, - result.bg.r, result.bg.g, result.bg.b, - result.bg.dark ? "Dark" : "Light"); - } - else - { - char fg[32], bg[32]; - const char* fgType = result.fg.dark ? "Dark" : "Light"; - const char* bgType = result.bg.dark ? "Dark" : "Light"; - snprintf(fg, ARRAY_SIZE(fg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.fg.r, result.fg.g, result.fg.b); - snprintf(bg, ARRAY_SIZE(bg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.bg.r, result.bg.g, result.bg.b); - FF_PRINT_FORMAT_CHECKED(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ - FF_FORMAT_ARG(fg, "fg-color"), - FF_FORMAT_ARG(fgType, "fg-type"), - FF_FORMAT_ARG(bg, "bg-color"), - FF_FORMAT_ARG(bgType, "bg-type"), - })); - } + char fg[32], bg[32]; + const char* fgType = result.fg.dark ? "Dark" : "Light"; + const char* bgType = result.bg.dark ? "Dark" : "Light"; + snprintf(fg, ARRAY_SIZE(fg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.fg.r, result.fg.g, result.fg.b); + snprintf(bg, ARRAY_SIZE(bg), "#%02" PRIX16 "%02" PRIX16 "%02" PRIX16, result.bg.r, result.bg.g, result.bg.b); + FF_PRINT_FORMAT_CHECKED(FF_TERMINALTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ + FF_FORMAT_ARG(fg, "fg-color"), + FF_FORMAT_ARG(fgType, "fg-type"), + FF_FORMAT_ARG(bg, "bg-color"), + FF_FORMAT_ARG(bgType, "bg-type"), + })); } + + return true; } void ffParseTerminalThemeJsonObject(FFTerminalThemeOptions* options, yyjson_val* module) @@ -61,14 +62,14 @@ void ffGenerateTerminalThemeJsonConfig(FFTerminalThemeOptions* options, yyjson_m ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateTerminalThemeJsonResult(FF_MAYBE_UNUSED FFTerminalThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateTerminalThemeJsonResult(FF_MAYBE_UNUSED FFTerminalThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFTerminalThemeResult result = {}; if(!ffDetectTerminalTheme(&result, false)) { yyjson_mut_obj_add_str(doc, module, "error", "Failed to detect terminal theme"); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -84,6 +85,8 @@ void ffGenerateTerminalThemeJsonResult(FF_MAYBE_UNUSED FFTerminalThemeOptions* o yyjson_mut_obj_add_uint(doc, bg, "g", result.bg.g); yyjson_mut_obj_add_uint(doc, bg, "b", result.bg.b); yyjson_mut_obj_add_bool(doc, bg, "dark", result.bg.dark); + + return true; } void ffInitTerminalThemeOptions(FFTerminalThemeOptions* options) diff --git a/src/modules/terminaltheme/terminaltheme.h b/src/modules/terminaltheme/terminaltheme.h index dbe6ac192f..db120f74ce 100644 --- a/src/modules/terminaltheme/terminaltheme.h +++ b/src/modules/terminaltheme/terminaltheme.h @@ -4,7 +4,7 @@ #define FF_TERMINALTHEME_MODULE_NAME "TerminalTheme" -void ffPrintTerminalTheme(FFTerminalThemeOptions* options); +bool ffPrintTerminalTheme(FFTerminalThemeOptions* options); void ffInitTerminalThemeOptions(FFTerminalThemeOptions* options); void ffDestroyTerminalThemeOptions(FFTerminalThemeOptions* options); diff --git a/src/modules/theme/theme.c b/src/modules/theme/theme.c index d0945fb0d6..89cd332d2e 100644 --- a/src/modules/theme/theme.c +++ b/src/modules/theme/theme.c @@ -4,7 +4,7 @@ #include "modules/theme/theme.h" #include "util/stringUtils.h" -void ffPrintTheme(FFThemeOptions* options) +bool ffPrintTheme(FFThemeOptions* options) { FFThemeResult result = { .theme1 = ffStrbufCreate(), @@ -15,7 +15,7 @@ void ffPrintTheme(FFThemeOptions* options) if(error) { ffPrintError(FF_THEME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -41,6 +41,7 @@ void ffPrintTheme(FFThemeOptions* options) ffStrbufDestroy(&result.theme1); ffStrbufDestroy(&result.theme2); + return true; } void ffParseThemeJsonObject(FFThemeOptions* options, yyjson_val* module) @@ -61,7 +62,7 @@ void ffGenerateThemeJsonConfig(FFThemeOptions* options, yyjson_mut_doc* doc, yyj ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateThemeJsonResult(FF_MAYBE_UNUSED FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateThemeJsonResult(FF_MAYBE_UNUSED FFThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFThemeResult result = { .theme1 = ffStrbufCreate(), @@ -72,7 +73,7 @@ void ffGenerateThemeJsonResult(FF_MAYBE_UNUSED FFThemeOptions* options, yyjson_m if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* theme = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -81,6 +82,8 @@ void ffGenerateThemeJsonResult(FF_MAYBE_UNUSED FFThemeOptions* options, yyjson_m ffStrbufDestroy(&result.theme1); ffStrbufDestroy(&result.theme2); + + return true; } void ffInitThemeOptions(FFThemeOptions* options) diff --git a/src/modules/theme/theme.h b/src/modules/theme/theme.h index 6d677a2d05..56ba49c812 100644 --- a/src/modules/theme/theme.h +++ b/src/modules/theme/theme.h @@ -4,7 +4,7 @@ #define FF_THEME_MODULE_NAME "Theme" -void ffPrintTheme(FFThemeOptions* options); +bool ffPrintTheme(FFThemeOptions* options); void ffInitThemeOptions(FFThemeOptions* options); void ffDestroyThemeOptions(FFThemeOptions* options); diff --git a/src/modules/title/title.c b/src/modules/title/title.c index d05a86b62f..dd85192080 100644 --- a/src/modules/title/title.c +++ b/src/modules/title/title.c @@ -21,7 +21,7 @@ static void appendText(FFstrbuf* output, const FFstrbuf* text, const FFstrbuf* c ffStrbufAppendS(output, FASTFETCH_TEXT_MODIFIER_RESET); } -void ffPrintTitle(FFTitleOptions* options) +bool ffPrintTitle(FFTitleOptions* options) { FF_STRBUF_AUTO_DESTROY userNameColored = ffStrbufCreate(); appendText(&userNameColored, &instance.state.platform.userName, &options->colorUser); @@ -66,6 +66,8 @@ void ffPrintTitle(FFTitleOptions* options) FF_FORMAT_ARG(instance.state.platform.fullUserName, "full-user-name"), })); } + + return true; } void ffParseTitleJsonObject(FFTitleOptions* options, yyjson_val* module) @@ -116,7 +118,7 @@ void ffGenerateTitleJsonConfig(FFTitleOptions* options, yyjson_mut_doc* doc, yyj yyjson_mut_obj_add_strbuf(doc, color, "host", &options->colorHost); } -void ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); yyjson_mut_obj_add_strbuf(doc, obj, "userName", &instance.state.platform.userName); @@ -125,6 +127,8 @@ void ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_m yyjson_mut_obj_add_strbuf(doc, obj, "homeDir", &instance.state.platform.homeDir); yyjson_mut_obj_add_strbuf(doc, obj, "exePath", &instance.state.platform.exePath); yyjson_mut_obj_add_strbuf(doc, obj, "userShell", &instance.state.platform.userShell); + + return true; } void ffInitTitleOptions(FFTitleOptions* options) diff --git a/src/modules/title/title.h b/src/modules/title/title.h index 4a41bcfe66..83c9496543 100644 --- a/src/modules/title/title.h +++ b/src/modules/title/title.h @@ -4,7 +4,7 @@ #define FF_TITLE_MODULE_NAME "Title" -void ffPrintTitle(FFTitleOptions* options); +bool ffPrintTitle(FFTitleOptions* options); void ffInitTitleOptions(FFTitleOptions* options); void ffDestroyTitleOptions(FFTitleOptions* options); diff --git a/src/modules/tpm/tpm.c b/src/modules/tpm/tpm.c index ad540bf121..b3f4e53cf7 100644 --- a/src/modules/tpm/tpm.c +++ b/src/modules/tpm/tpm.c @@ -4,7 +4,7 @@ #include "modules/tpm/tpm.h" #include "util/stringUtils.h" -void ffPrintTPM(FFTPMOptions* options) +bool ffPrintTPM(FFTPMOptions* options) { FFTPMResult result = { .version = ffStrbufCreate(), @@ -15,7 +15,7 @@ void ffPrintTPM(FFTPMOptions* options) if(error) { ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -36,6 +36,8 @@ void ffPrintTPM(FFTPMOptions* options) ffStrbufDestroy(&result.version); ffStrbufDestroy(&result.description); + + return true; } void ffParseTPMJsonObject(FFTPMOptions* options, yyjson_val* module) @@ -56,7 +58,7 @@ void ffGenerateTPMJsonConfig(FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_ ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateTPMJsonResult(FF_MAYBE_UNUSED FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateTPMJsonResult(FF_MAYBE_UNUSED FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFTPMResult result = { .version = ffStrbufCreate(), @@ -67,7 +69,7 @@ void ffGenerateTPMJsonResult(FF_MAYBE_UNUSED FFTPMOptions* options, yyjson_mut_d if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -76,6 +78,8 @@ void ffGenerateTPMJsonResult(FF_MAYBE_UNUSED FFTPMOptions* options, yyjson_mut_d ffStrbufDestroy(&result.version); ffStrbufDestroy(&result.description); + + return true; } void ffInitTPMOptions(FFTPMOptions* options) diff --git a/src/modules/tpm/tpm.h b/src/modules/tpm/tpm.h index c5ef666df9..215f8c98aa 100644 --- a/src/modules/tpm/tpm.h +++ b/src/modules/tpm/tpm.h @@ -4,7 +4,7 @@ #define FF_TPM_MODULE_NAME "TPM" -void ffPrintTPM(FFTPMOptions* options); +bool ffPrintTPM(FFTPMOptions* options); void ffInitTPMOptions(FFTPMOptions* options); void ffDestroyTPMOptions(FFTPMOptions* options); diff --git a/src/modules/uptime/uptime.c b/src/modules/uptime/uptime.c index ca64ec572a..e1cbd9a8c6 100644 --- a/src/modules/uptime/uptime.c +++ b/src/modules/uptime/uptime.c @@ -6,7 +6,7 @@ #include "modules/uptime/uptime.h" #include "util/stringUtils.h" -void ffPrintUptime(FFUptimeOptions* options) +bool ffPrintUptime(FFUptimeOptions* options) { FFUptimeResult result = {}; @@ -15,7 +15,7 @@ void ffPrintUptime(FFUptimeOptions* options) if(error) { ffPrintError(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } uint64_t uptime = result.uptime; @@ -54,6 +54,8 @@ void ffPrintUptime(FFUptimeOptions* options) FF_FORMAT_ARG(buffer, "formatted") })); } + + return true; } void ffParseUptimeJsonObject(FFUptimeOptions* options, yyjson_val* module) @@ -74,7 +76,7 @@ void ffGenerateUptimeJsonConfig(FFUptimeOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateUptimeJsonResult(FF_MAYBE_UNUSED FFUptimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateUptimeJsonResult(FF_MAYBE_UNUSED FFUptimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFUptimeResult result; const char* error = ffDetectUptime(&result); @@ -82,12 +84,14 @@ void ffGenerateUptimeJsonResult(FF_MAYBE_UNUSED FFUptimeOptions* options, yyjson if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); yyjson_mut_obj_add_uint(doc, obj, "uptime", result.uptime); yyjson_mut_obj_add_strcpy(doc, obj, "bootTime", ffTimeToFullStr(result.bootTime)); + + return true; } void ffInitUptimeOptions(FFUptimeOptions* options) diff --git a/src/modules/uptime/uptime.h b/src/modules/uptime/uptime.h index a12fcd549f..dec5f95e5c 100644 --- a/src/modules/uptime/uptime.h +++ b/src/modules/uptime/uptime.h @@ -4,7 +4,7 @@ #define FF_UPTIME_MODULE_NAME "Uptime" -void ffPrintUptime(FFUptimeOptions* options); +bool ffPrintUptime(FFUptimeOptions* options); void ffInitUptimeOptions(FFUptimeOptions* options); void ffDestroyUptimeOptions(FFUptimeOptions* options); diff --git a/src/modules/users/users.c b/src/modules/users/users.c index fce2dc7efa..7518161796 100644 --- a/src/modules/users/users.c +++ b/src/modules/users/users.c @@ -7,7 +7,7 @@ #pragma GCC diagnostic ignored "-Wformat" // warning: unknown conversion type character 'F' in format -void ffPrintUsers(FFUsersOptions* options) +bool ffPrintUsers(FFUsersOptions* options) { FF_LIST_AUTO_DESTROY users = ffListCreate(sizeof(FFUserResult)); @@ -16,13 +16,13 @@ void ffPrintUsers(FFUsersOptions* options) if(error) { ffPrintError(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(users.length == 0) { ffPrintError(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "Unable to detect any users"); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -105,6 +105,8 @@ void ffPrintUsers(FFUsersOptions* options) ffStrbufDestroy(&user->sessionName); ffStrbufDestroy(&user->name); } + + return true; } void ffParseUsersJsonObject(FFUsersOptions* options, yyjson_val* module) @@ -144,7 +146,7 @@ void ffGenerateUsersJsonConfig(FFUsersOptions* options, yyjson_mut_doc* doc, yyj yyjson_mut_obj_add_bool(doc, module, "myselfOnly", options->myselfOnly); } -void ffGenerateUsersJsonResult(FFUsersOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateUsersJsonResult(FFUsersOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFUserResult)); @@ -153,7 +155,7 @@ void ffGenerateUsersJsonResult(FFUsersOptions* options, yyjson_mut_doc* doc, yyj if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -178,6 +180,8 @@ void ffGenerateUsersJsonResult(FFUsersOptions* options, yyjson_mut_doc* doc, yyj ffStrbufDestroy(&user->sessionName); ffStrbufDestroy(&user->name); } + + return true; } void ffInitUsersOptions(FFUsersOptions* options) diff --git a/src/modules/users/users.h b/src/modules/users/users.h index bc249bda1e..44c8b01d7e 100644 --- a/src/modules/users/users.h +++ b/src/modules/users/users.h @@ -4,7 +4,7 @@ #define FF_USERS_MODULE_NAME "Users" -void ffPrintUsers(FFUsersOptions* options); +bool ffPrintUsers(FFUsersOptions* options); void ffInitUsersOptions(FFUsersOptions* options); void ffDestroyUsersOptions(FFUsersOptions* options); diff --git a/src/modules/version/version.c b/src/modules/version/version.c index 15f519c123..4350e71707 100644 --- a/src/modules/version/version.c +++ b/src/modules/version/version.c @@ -5,7 +5,7 @@ #include "modules/version/version.h" #include "util/stringUtils.h" -void ffPrintVersion(FFVersionOptions* options) +bool ffPrintVersion(FFVersionOptions* options) { FFVersionResult* result = &ffVersionResult; @@ -42,6 +42,8 @@ void ffPrintVersion(FFVersionOptions* options) FF_FORMAT_ARG(buf, "libc"), })); } + + return true; } void ffParseVersionJsonObject(FFVersionOptions* options, yyjson_val* module) @@ -62,7 +64,7 @@ void ffGenerateVersionJsonConfig(FFVersionOptions* options, yyjson_mut_doc* doc, ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateVersionJsonResult(FF_MAYBE_UNUSED FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateVersionJsonResult(FF_MAYBE_UNUSED FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FFVersionResult* result = &ffVersionResult; @@ -92,6 +94,8 @@ void ffGenerateVersionJsonResult(FF_MAYBE_UNUSED FFVersionOptions* options, yyjs } yyjson_mut_obj_add_strbuf(doc, obj, "libc", &buf); } + + return true; } void ffInitVersionOptions(FFVersionOptions* options) diff --git a/src/modules/version/version.h b/src/modules/version/version.h index 1135e53943..add5e1198f 100644 --- a/src/modules/version/version.h +++ b/src/modules/version/version.h @@ -4,7 +4,7 @@ #define FF_VERSION_MODULE_NAME "Version" -void ffPrintVersion(FFVersionOptions* options); +bool ffPrintVersion(FFVersionOptions* options); void ffInitVersionOptions(FFVersionOptions* options); void ffDestroyVersionOptions(FFVersionOptions* options); diff --git a/src/modules/vulkan/vulkan.c b/src/modules/vulkan/vulkan.c index 429df582c0..d774007250 100644 --- a/src/modules/vulkan/vulkan.c +++ b/src/modules/vulkan/vulkan.c @@ -5,14 +5,14 @@ #include "modules/vulkan/vulkan.h" #include "util/stringUtils.h" -void ffPrintVulkan(FFVulkanOptions* options) +bool ffPrintVulkan(FFVulkanOptions* options) { const FFVulkanResult* vulkan = ffDetectVulkan(); if(vulkan->error) { ffPrintError(FF_VULKAN_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", vulkan->error); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -23,21 +23,22 @@ void ffPrintVulkan(FFVulkanOptions* options) { ffStrbufWriteTo(&vulkan->instanceVersion, stdout); puts(" [Software only]"); - return; } - - if(vulkan->apiVersion.length > 0) + else { - ffStrbufWriteTo(&vulkan->apiVersion, stdout); + if(vulkan->apiVersion.length > 0) + { + ffStrbufWriteTo(&vulkan->apiVersion, stdout); - if(vulkan->driver.length > 0) - fputs(" - ", stdout); - } + if(vulkan->driver.length > 0) + fputs(" - ", stdout); + } - if(vulkan->driver.length > 0) - ffStrbufWriteTo(&vulkan->driver, stdout); + if(vulkan->driver.length > 0) + ffStrbufWriteTo(&vulkan->driver, stdout); - putchar('\n'); + putchar('\n'); + } } else { @@ -48,6 +49,8 @@ void ffPrintVulkan(FFVulkanOptions* options) FF_FORMAT_ARG(vulkan->instanceVersion, "instance-version"), })); } + + return true; } void ffParseVulkanJsonObject(FFVulkanOptions* options, yyjson_val* module) @@ -68,14 +71,14 @@ void ffGenerateVulkanJsonConfig(FFVulkanOptions* options, yyjson_mut_doc* doc, y ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateVulkanJsonResult(FF_MAYBE_UNUSED FFVulkanOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateVulkanJsonResult(FF_MAYBE_UNUSED FFVulkanOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFVulkanResult* result = ffDetectVulkan(); if(result->error) { yyjson_mut_obj_add_str(doc, module, "error", result->error); - return; + return false; } yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); @@ -123,6 +126,8 @@ void ffGenerateVulkanJsonResult(FF_MAYBE_UNUSED FFVulkanOptions* options, yyjson yyjson_mut_obj_add_uint(doc, gpuObj, "deviceId", vulkanGpu->deviceId); } + + return true; } void ffInitVulkanOptions(FFVulkanOptions* options) diff --git a/src/modules/vulkan/vulkan.h b/src/modules/vulkan/vulkan.h index 7a5d0f29d4..c838028c5d 100644 --- a/src/modules/vulkan/vulkan.h +++ b/src/modules/vulkan/vulkan.h @@ -4,7 +4,7 @@ #define FF_VULKAN_MODULE_NAME "Vulkan" -void ffPrintVulkan(FFVulkanOptions* options); +bool ffPrintVulkan(FFVulkanOptions* options); void ffInitVulkanOptions(FFVulkanOptions* options); void ffDestroyVulkanOptions(FFVulkanOptions* options); diff --git a/src/modules/wallpaper/wallpaper.c b/src/modules/wallpaper/wallpaper.c index 6c6466b385..61098f2784 100644 --- a/src/modules/wallpaper/wallpaper.c +++ b/src/modules/wallpaper/wallpaper.c @@ -4,7 +4,7 @@ #include "modules/wallpaper/wallpaper.h" #include "util/stringUtils.h" -void ffPrintWallpaper(FFWallpaperOptions* options) +bool ffPrintWallpaper(FFWallpaperOptions* options) { FF_STRBUF_AUTO_DESTROY fullpath = ffStrbufCreate(); const char* error = ffDetectWallpaper(&fullpath); @@ -23,7 +23,7 @@ void ffPrintWallpaper(FFWallpaperOptions* options) if(error) { ffPrintError(FF_WALLPAPER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(options->moduleArgs.outputFormat.length == 0) @@ -38,6 +38,8 @@ void ffPrintWallpaper(FFWallpaperOptions* options) FF_FORMAT_ARG(fullpath, "full-path"), })); } + + return true; } void ffParseWallpaperJsonObject(FFWallpaperOptions* options, yyjson_val* module) @@ -58,16 +60,18 @@ void ffGenerateWallpaperJsonConfig(FFWallpaperOptions* options, yyjson_mut_doc* ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateWallpaperJsonResult(FF_MAYBE_UNUSED FFWallpaperOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateWallpaperJsonResult(FF_MAYBE_UNUSED FFWallpaperOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_STRBUF_AUTO_DESTROY fullpath = ffStrbufCreate(); const char* error = ffDetectWallpaper(&fullpath); if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_obj_add_strbuf(doc, module, "result", &fullpath); + + return true; } void ffInitWallpaperOptions(FFWallpaperOptions* options) diff --git a/src/modules/wallpaper/wallpaper.h b/src/modules/wallpaper/wallpaper.h index 3ca28bfcdb..c3e72911be 100644 --- a/src/modules/wallpaper/wallpaper.h +++ b/src/modules/wallpaper/wallpaper.h @@ -4,7 +4,7 @@ #define FF_WALLPAPER_MODULE_NAME "Wallpaper" -void ffPrintWallpaper(FFWallpaperOptions* options); +bool ffPrintWallpaper(FFWallpaperOptions* options); void ffInitWallpaperOptions(FFWallpaperOptions* options); void ffDestroyWallpaperOptions(FFWallpaperOptions* options); diff --git a/src/modules/weather/weather.c b/src/modules/weather/weather.c index fec43ff5da..3fc3d3a483 100644 --- a/src/modules/weather/weather.c +++ b/src/modules/weather/weather.c @@ -4,7 +4,7 @@ #include "modules/weather/weather.h" #include "util/stringUtils.h" -void ffPrintWeather(FFWeatherOptions* options) +bool ffPrintWeather(FFWeatherOptions* options) { FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); const char* error = ffDetectWeather(options, &result); @@ -12,10 +12,9 @@ void ffPrintWeather(FFWeatherOptions* options) if(error) { ffPrintError(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } - if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_WEATHER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); @@ -27,6 +26,8 @@ void ffPrintWeather(FFWeatherOptions* options) FF_FORMAT_ARG(result, "result"), })); } + + return true; } void ffParseWeatherJsonObject(FFWeatherOptions* options, yyjson_val* module) @@ -71,7 +72,7 @@ void ffGenerateWeatherJsonConfig(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_obj_add_uint(doc, module, "timeout", options->timeout); } -void ffGenerateWeatherJsonResult(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateWeatherJsonResult(FFWeatherOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate(); const char* error = ffDetectWeather(options, &result); @@ -79,10 +80,12 @@ void ffGenerateWeatherJsonResult(FFWeatherOptions* options, yyjson_mut_doc* doc, if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_obj_add_strbuf(doc, module, "result", &result); + + return true; } void ffInitWeatherOptions(FFWeatherOptions* options) diff --git a/src/modules/weather/weather.h b/src/modules/weather/weather.h index 5b3c9739bf..f28a4c2332 100644 --- a/src/modules/weather/weather.h +++ b/src/modules/weather/weather.h @@ -6,7 +6,7 @@ void ffPrepareWeather(FFWeatherOptions* options); -void ffPrintWeather(FFWeatherOptions* options); +bool ffPrintWeather(FFWeatherOptions* options); void ffInitWeatherOptions(FFWeatherOptions* options); void ffDestroyWeatherOptions(FFWeatherOptions* options); diff --git a/src/modules/wifi/wifi.c b/src/modules/wifi/wifi.c index 69df42cf58..136211418f 100644 --- a/src/modules/wifi/wifi.c +++ b/src/modules/wifi/wifi.c @@ -4,7 +4,7 @@ #include "modules/wifi/wifi.h" #include "util/stringUtils.h" -void ffPrintWifi(FFWifiOptions* options) +bool ffPrintWifi(FFWifiOptions* options) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFWifiResult)); @@ -12,12 +12,12 @@ void ffPrintWifi(FFWifiOptions* options) if(error) { ffPrintError(FF_WIFI_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(!result.length) { ffPrintError(FF_WIFI_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No Wifi interfaces found"); - return; + return false; } FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type; @@ -125,7 +125,10 @@ void ffPrintWifi(FFWifiOptions* options) FF_FORMAT_ARG(bandStr, "band"), })); } + } + FF_LIST_FOR_EACH(FFWifiResult, item, result) + { ffStrbufDestroy(&item->inf.description); ffStrbufDestroy(&item->inf.status); ffStrbufDestroy(&item->conn.status); @@ -134,6 +137,8 @@ void ffPrintWifi(FFWifiOptions* options) ffStrbufDestroy(&item->conn.protocol); ffStrbufDestroy(&item->conn.security); } + + return true; } void ffParseWifiJsonObject(FFWifiOptions* options, yyjson_val* module) @@ -159,14 +164,14 @@ void ffGenerateWifiJsonConfig(FFWifiOptions* options, yyjson_mut_doc* doc, yyjso ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateWifiJsonResult(FF_MAYBE_UNUSED FFWifiOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateWifiJsonResult(FF_MAYBE_UNUSED FFWifiOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFWifiResult)); const char* error = ffDetectWifi(&result); if(error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -210,6 +215,8 @@ void ffGenerateWifiJsonResult(FF_MAYBE_UNUSED FFWifiOptions* options, yyjson_mut ffStrbufDestroy(&item->conn.protocol); ffStrbufDestroy(&item->conn.security); } + + return true; } void ffInitWifiOptions(FFWifiOptions* options) diff --git a/src/modules/wifi/wifi.h b/src/modules/wifi/wifi.h index 0fcef5f1c8..506e1e22af 100644 --- a/src/modules/wifi/wifi.h +++ b/src/modules/wifi/wifi.h @@ -4,7 +4,7 @@ #define FF_WIFI_MODULE_NAME "Wifi" -void ffPrintWifi(FFWifiOptions* options); +bool ffPrintWifi(FFWifiOptions* options); void ffInitWifiOptions(FFWifiOptions* options); void ffDestroyWifiOptions(FFWifiOptions* options); diff --git a/src/modules/wm/wm.c b/src/modules/wm/wm.c index 6753ce2da8..23c724fb65 100644 --- a/src/modules/wm/wm.c +++ b/src/modules/wm/wm.c @@ -5,14 +5,14 @@ #include "modules/wm/wm.h" #include "util/stringUtils.h" -void ffPrintWM(FFWMOptions* options) +bool ffPrintWM(FFWMOptions* options) { const FFDisplayServerResult* result = ffConnectDisplayServer(); if(result->wmPrettyName.length == 0) { ffPrintError(FF_WM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No WM found"); - return; + return false; } FF_STRBUF_AUTO_DESTROY pluginName = ffStrbufCreate(); @@ -61,6 +61,8 @@ void ffPrintWM(FFWMOptions* options) FF_FORMAT_ARG(version, "version"), })); } + + return true; } void ffParseWMJsonObject(FFWMOptions* options, yyjson_val* module) @@ -89,14 +91,14 @@ void ffGenerateWMJsonConfig(FFWMOptions* options, yyjson_mut_doc* doc, yyjson_mu yyjson_mut_obj_add_bool(doc, module, "detectPlugin", options->detectPlugin); } -void ffGenerateWMJsonResult(FF_MAYBE_UNUSED FFWMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateWMJsonResult(FF_MAYBE_UNUSED FFWMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { const FFDisplayServerResult* result = ffConnectDisplayServer(); if(result->wmPrettyName.length == 0) { yyjson_mut_obj_add_str(doc, module, "error", "No WM found"); - return; + return false; } FF_STRBUF_AUTO_DESTROY pluginName = ffStrbufCreate(); @@ -113,6 +115,8 @@ void ffGenerateWMJsonResult(FF_MAYBE_UNUSED FFWMOptions* options, yyjson_mut_doc yyjson_mut_obj_add_strbuf(doc, obj, "protocolName", &result->wmProtocolName); yyjson_mut_obj_add_strbuf(doc, obj, "pluginName", &pluginName); yyjson_mut_obj_add_strbuf(doc, obj, "version", &version); + + return true; } void ffInitWMOptions(FFWMOptions* options) diff --git a/src/modules/wm/wm.h b/src/modules/wm/wm.h index 5b6556806b..4819d73946 100644 --- a/src/modules/wm/wm.h +++ b/src/modules/wm/wm.h @@ -4,7 +4,7 @@ #define FF_WM_MODULE_NAME "WM" -void ffPrintWM(FFWMOptions* options); +bool ffPrintWM(FFWMOptions* options); void ffInitWMOptions(FFWMOptions* options); void ffDestroyWMOptions(FFWMOptions* options); diff --git a/src/modules/wmtheme/wmtheme.c b/src/modules/wmtheme/wmtheme.c index d40ab821e9..eee270df2f 100644 --- a/src/modules/wmtheme/wmtheme.c +++ b/src/modules/wmtheme/wmtheme.c @@ -6,27 +6,28 @@ #define FF_WMTHEME_DISPLAY_NAME "WM Theme" -void ffPrintWMTheme(FFWMThemeOptions* options) +bool ffPrintWMTheme(FFWMThemeOptions* options) { FF_STRBUF_AUTO_DESTROY themeOrError = ffStrbufCreate(); - if(ffDetectWmTheme(&themeOrError)) + if(!ffDetectWmTheme(&themeOrError)) { - if(options->moduleArgs.outputFormat.length == 0) - { - ffPrintLogoAndKey(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - puts(themeOrError.chars); - } - else - { - FF_PRINT_FORMAT_CHECKED(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ - FF_FORMAT_ARG(themeOrError, "result"), - })); - } + ffPrintError(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", themeOrError.chars); + return false; + } + + if(options->moduleArgs.outputFormat.length == 0) + { + ffPrintLogoAndKey(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + puts(themeOrError.chars); } else { - ffPrintError(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%*s", themeOrError.length, themeOrError.chars); + FF_PRINT_FORMAT_CHECKED(FF_WMTHEME_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]){ + FF_FORMAT_ARG(themeOrError, "result"), + })); } + + return true; } void ffParseWMThemeJsonObject(FFWMThemeOptions* options, yyjson_val* module) @@ -47,16 +48,17 @@ void ffGenerateWMThemeJsonConfig(FFWMThemeOptions* options, yyjson_mut_doc* doc, ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); } -void ffGenerateWMThemeJsonResult(FF_MAYBE_UNUSED FFWMThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateWMThemeJsonResult(FF_MAYBE_UNUSED FFWMThemeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_STRBUF_AUTO_DESTROY themeOrError = ffStrbufCreate(); if(!ffDetectWmTheme(&themeOrError)) { yyjson_mut_obj_add_strbuf(doc, module, "error", &themeOrError); - return; + return false; } yyjson_mut_obj_add_strbuf(doc, module, "result", &themeOrError); + return true; } void ffInitWMThemeOptions(FFWMThemeOptions* options) diff --git a/src/modules/wmtheme/wmtheme.h b/src/modules/wmtheme/wmtheme.h index e3bad8ee06..5142ca42cc 100644 --- a/src/modules/wmtheme/wmtheme.h +++ b/src/modules/wmtheme/wmtheme.h @@ -4,7 +4,7 @@ #define FF_WMTHEME_MODULE_NAME "WMTheme" -void ffPrintWMTheme(FFWMThemeOptions* options); +bool ffPrintWMTheme(FFWMThemeOptions* options); void ffInitWMThemeOptions(FFWMThemeOptions* options); void ffDestroyWMThemeOptions(FFWMThemeOptions* options); diff --git a/src/modules/zpool/zpool.c b/src/modules/zpool/zpool.c index 9efddacdb8..4193a25b96 100644 --- a/src/modules/zpool/zpool.c +++ b/src/modules/zpool/zpool.c @@ -76,7 +76,7 @@ static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t i } } -void ffPrintZpool(FFZpoolOptions* options) +bool ffPrintZpool(FFZpoolOptions* options) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFZpoolResult)); @@ -85,12 +85,12 @@ void ffPrintZpool(FFZpoolOptions* options) if (error) { ffPrintError(FF_ZPOOL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); - return; + return false; } if(results.length == 0) { ffPrintError(FF_ZPOOL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "No zpool found"); - return; + return false; } for(uint32_t i = 0; i < results.length; i++) @@ -105,6 +105,7 @@ void ffPrintZpool(FFZpoolOptions* options) ffStrbufDestroy(&result->name); ffStrbufDestroy(&result->state); } + return true; } void ffParseZpoolJsonObject(FFZpoolOptions* options, yyjson_val* module) @@ -130,7 +131,7 @@ void ffGenerateZpoolJsonConfig(FFZpoolOptions* options, yyjson_mut_doc* doc, yyj ffPercentGenerateJsonConfig(doc, module, options->percent); } -void ffGenerateZpoolJsonResult(FF_MAYBE_UNUSED FFZpoolOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +bool ffGenerateZpoolJsonResult(FF_MAYBE_UNUSED FFZpoolOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { FF_LIST_AUTO_DESTROY results = ffListCreate(sizeof(FFZpoolResult)); @@ -138,7 +139,7 @@ void ffGenerateZpoolJsonResult(FF_MAYBE_UNUSED FFZpoolOptions* options, yyjson_m if (error) { yyjson_mut_obj_add_str(doc, module, "error", error); - return; + return false; } yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result"); @@ -162,6 +163,7 @@ void ffGenerateZpoolJsonResult(FF_MAYBE_UNUSED FFZpoolOptions* options, yyjson_m ffStrbufDestroy(&zpool->name); ffStrbufDestroy(&zpool->state); } + return true; } void ffInitZpoolOptions(FFZpoolOptions* options) diff --git a/src/modules/zpool/zpool.h b/src/modules/zpool/zpool.h index 7f282f2538..35706bb356 100644 --- a/src/modules/zpool/zpool.h +++ b/src/modules/zpool/zpool.h @@ -4,7 +4,7 @@ #define FF_ZPOOL_MODULE_NAME "Zpool" -void ffPrintZpool(FFZpoolOptions* options); +bool ffPrintZpool(FFZpoolOptions* options); void ffInitZpoolOptions(FFZpoolOptions* options); void ffDestroyZpoolOptions(FFZpoolOptions* options); diff --git a/src/options/display.c b/src/options/display.c index 95cff25e87..5bb2a888e1 100644 --- a/src/options/display.c +++ b/src/options/display.c @@ -492,9 +492,14 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key options->pipe = ffOptionParseBoolean(value); else if(ffStrEqualsIgnCase(key, "--show-errors")) options->showErrors = ffOptionParseBoolean(value); - #ifndef NDEBUG else if(ffStrEqualsIgnCase(key, "--debug")) + #ifndef NDEBUG options->debugMode = ffOptionParseBoolean(value); + #else + { + fprintf(stderr, "--debug is only available in debug builds\n"); + exit(477); + } #endif else if(ffStrEqualsIgnCase(key, "--disable-linewrap")) options->disableLinewrap = ffOptionParseBoolean(value); diff --git a/src/options/general.c b/src/options/general.c index 72f79798bb..128d917998 100644 --- a/src/options/general.c +++ b/src/options/general.c @@ -30,7 +30,7 @@ const char* ffOptionsParseGeneralJsonConfig(FFOptionsGeneral* options, yyjson_va else if (unsafe_yyjson_equals_str(key, "detectVersion")) options->detectVersion = yyjson_get_bool(val); - #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) || defined(__GNU__) else if (unsafe_yyjson_equals_str(key, "playerName")) ffStrbufSetJsonVal(&options->playerName, val); else if (unsafe_yyjson_equals_str(key, "dsForceDrm")) @@ -73,7 +73,7 @@ bool ffOptionsParseGeneralCommandLine(FFOptionsGeneral* options, const char* key else if(ffStrEqualsIgnCase(key, "--detect-version")) options->detectVersion = ffOptionParseBoolean(value); - #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) || defined(__GNU__) else if(ffStrEqualsIgnCase(key, "--player-name")) ffOptionParseString(key, value, &options->playerName); else if(ffStrEqualsIgnCase(key, "--ds-force-drm")) @@ -102,7 +102,7 @@ void ffOptionsInitGeneral(FFOptionsGeneral* options) options->multithreading = true; options->detectVersion = true; - #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) || defined(__GNU__) ffStrbufInit(&options->playerName); options->dsForceDrm = FF_DS_FORCE_DRM_TYPE_FALSE; #elif defined(_WIN32) @@ -112,7 +112,7 @@ void ffOptionsInitGeneral(FFOptionsGeneral* options) void ffOptionsDestroyGeneral(FF_MAYBE_UNUSED FFOptionsGeneral* options) { - #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) || defined(__GNU__) ffStrbufDestroy(&options->playerName); #endif } @@ -127,7 +127,7 @@ void ffOptionsGenerateGeneralJsonConfig(FFOptionsGeneral* options, yyjson_mut_do yyjson_mut_obj_add_bool(doc, obj, "detectVersion", options->detectVersion); - #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) || defined(__GNU__) yyjson_mut_obj_add_strbuf(doc, obj, "playerName", &options->playerName); diff --git a/src/options/general.h b/src/options/general.h index 9e703a946b..0edcfeb2d5 100644 --- a/src/options/general.h +++ b/src/options/general.h @@ -16,7 +16,7 @@ typedef struct FFOptionsGeneral bool detectVersion; // Module options that cannot be put in module option structure - #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) + #if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) || defined(__GNU__) FFstrbuf playerName; FFDsForceDrmType dsForceDrm; #elif defined(_WIN32) diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 6e0db579b4..f0f85e61ed 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -2,6 +2,7 @@ #include #include +#include char* CHAR_NULL_PTR = ""; @@ -542,6 +543,76 @@ int64_t ffStrbufToSInt(const FFstrbuf* strbuf, int64_t defaultValue) return str_end == strbuf->chars ? defaultValue : (int64_t)result; } +void ffStrbufAppendSInt(FFstrbuf* strbuf, int64_t value) +{ + ffStrbufEnsureFree(strbuf, 21); // Required by yyjson_write_number + char* start = strbuf->chars + strbuf->length; + + yyjson_val val = {}; + unsafe_yyjson_set_sint(&val, value); + char* end = yyjson_write_number(&val, start); + + assert(end != NULL); + + strbuf->length += (uint32_t)(end - start); +} + +void ffStrbufAppendUInt(FFstrbuf* strbuf, uint64_t value) +{ + ffStrbufEnsureFree(strbuf, 21); // Required by yyjson_write_number + char* start = strbuf->chars + strbuf->length; + + yyjson_val val = {}; + unsafe_yyjson_set_uint(&val, value); + char* end = yyjson_write_number(&val, start); + + assert(end != NULL); + + strbuf->length += (uint32_t)(end - start); +} + +void ffStrbufAppendDouble(FFstrbuf* strbuf, double value, int8_t precision) +{ + assert(precision <= 15); // yyjson_write_number supports up to 15 digits after the decimal point + + ffStrbufEnsureFree(strbuf, 40); // Required by yyjson_write_number + char* start = strbuf->chars + strbuf->length; + + if (precision == 0) + value = round(value); + yyjson_val val = {}; + unsafe_yyjson_set_double(&val, value); + if (precision > 0) + unsafe_yyjson_set_fp_to_fixed(&val, precision); + + // Write at most digits after the decimal point; doesn't append trailing zeros + char* end = yyjson_write_number(&val, start); + + assert(end > start); + + strbuf->length += (uint32_t)(end - start); + + if (__builtin_expect(value > 1e21 || value < -1e21, false)) + { + // If the value is too large, yyjson_write_number will write it in scientific notation + return; + } + + if (precision > 1) + { + for (char* p = end - 1; *p != '.' && p > start; --p) + --precision; + if (precision > 0) + ffStrbufAppendNC(strbuf, (uint32_t) precision, '0'); + } + else if (precision == 0 || (precision < 0 && end[-1] == '0')) + { + // yyjson always appends ".0", so we need to remove it + strbuf->length -= 2; + strbuf->chars[strbuf->length] = '\0'; + } +} + void ffStrbufUpperCase(FFstrbuf* strbuf) { for (uint32_t i = 0; i < strbuf->length; ++i) diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index 39737fed00..704f2a332e 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -103,6 +103,12 @@ bool ffStrbufMatchSeparatedNS(const FFstrbuf* strbuf, uint32_t compLength, const int ffStrbufAppendUtf32CodePoint(FFstrbuf* strbuf, uint32_t codepoint); +void ffStrbufAppendSInt(FFstrbuf* strbuf, int64_t value); +void ffStrbufAppendUInt(FFstrbuf* strbuf, uint64_t value); +// Appends a double value to the string buffer with the specified precision (0~15). +// if `precision < 0`, let yyjson decide the precision +void ffStrbufAppendDouble(FFstrbuf* strbuf, double value, int8_t precision); + FF_C_NODISCARD static inline FFstrbuf ffStrbufCreateA(uint32_t allocate) { FFstrbuf strbuf; diff --git a/src/util/apple/Info.plist.in b/src/util/apple/Info.plist.in index 9e24ccb7c3..52200538b6 100644 --- a/src/util/apple/Info.plist.in +++ b/src/util/apple/Info.plist.in @@ -6,8 +6,8 @@ fastfetch CFBundleName @PROJECT_NAME@ - CFBundleShortVersionString - @PROJECT_VERSION@ + CFBundleShortVersionString + @PROJECT_VERSION@ CFBundleDevelopmentRegion English NSBluetoothAlwaysUsageDescription diff --git a/src/util/arrayUtils.h b/src/util/arrayUtils.h new file mode 100644 index 0000000000..ad251ba1ed --- /dev/null +++ b/src/util/arrayUtils.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#ifdef __has_builtin + #if __has_builtin(__is_array) + #define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) + #elif __has_builtin(__builtin_types_compatible_p) + #define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) + #endif +#endif +#ifndef ARRAY_SIZE + #define ARRAY_SIZE(x) ((uint32_t) (sizeof(x) / sizeof(*(x)))) +#endif diff --git a/src/util/platform/FFPlatform_unix.c b/src/util/platform/FFPlatform_unix.c index eb9ee450e8..682c2e6f73 100644 --- a/src/util/platform/FFPlatform_unix.c +++ b/src/util/platform/FFPlatform_unix.c @@ -22,7 +22,7 @@ static void getExePath(FFPlatform* platform) { char exePath[PATH_MAX + 1]; - #ifdef __linux__ + #if defined(__linux__) || defined (__GNU__) ssize_t exePathLen = readlink("/proc/self/exe", exePath, sizeof(exePath) - 1); if (exePathLen >= 0) exePath[exePathLen] = '\0'; diff --git a/src/util/smbiosHelper.c b/src/util/smbiosHelper.c index 87569515f0..593527a72f 100644 --- a/src/util/smbiosHelper.c +++ b/src/util/smbiosHelper.c @@ -52,7 +52,7 @@ const FFSmbiosHeader* ffSmbiosNextEntry(const FFSmbiosHeader* header) return (const FFSmbiosHeader*) (p + 1); } -#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(__HAIKU__) || defined(__OpenBSD__) +#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__sun) || defined(__HAIKU__) || defined(__OpenBSD__) || defined(__GNU__) #include #include #include @@ -143,7 +143,7 @@ const FFSmbiosHeaderTable* ffGetSmbiosHeaderTable() { FF_DEBUG("Initializing SMBIOS buffer"); ffStrbufInit(&buffer); - #if !__HAIKU__ && !__OpenBSD__ && !__DragonFly__ + #if !__HAIKU__ && !__OpenBSD__ && !__DragonFly__ && !__GNU__ #ifdef __linux__ FF_DEBUG("Using Linux implementation - trying /sys/firmware/dmi/tables/DMI"); if (!ffAppendFileBuffer("/sys/firmware/dmi/tables/DMI", &buffer)) diff --git a/tests/strbuf.c b/tests/strbuf.c index 456b9c3300..39c845a3bc 100644 --- a/tests/strbuf.c +++ b/tests/strbuf.c @@ -720,6 +720,144 @@ int main(void) ffStrbufDestroy(&strbuf); } + { + ffStrbufAppendSInt(&strbuf, 1234567890); + VERIFY(ffStrbufEqualS(&strbuf, "1234567890")); + + ffStrbufClear(&strbuf); + ffStrbufAppendSInt(&strbuf, -1234567890); + VERIFY(ffStrbufEqualS(&strbuf, "-1234567890")); + + ffStrbufClear(&strbuf); + ffStrbufAppendSInt(&strbuf, 0); + VERIFY(ffStrbufEqualS(&strbuf, "0")); + + ffStrbufClear(&strbuf); + ffStrbufAppendUInt(&strbuf, 1234567890); + VERIFY(ffStrbufEqualS(&strbuf, "1234567890")); + + ffStrbufClear(&strbuf); + ffStrbufAppendUInt(&strbuf, 0); + VERIFY(ffStrbufEqualS(&strbuf, "0")); + + ffStrbufDestroy(&strbuf); + } + + { + ffStrbufAppendDouble(&strbuf, 120.0, 0); + VERIFY(ffStrbufEqualS(&strbuf, "120")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 120.0, 1); + VERIFY(ffStrbufEqualS(&strbuf, "120.0")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 120.0, 5); + VERIFY(ffStrbufEqualS(&strbuf, "120.00000")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 120.123456789, 5); + VERIFY(ffStrbufEqualS(&strbuf, "120.12346")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 120.888888, 0); + VERIFY(ffStrbufEqualS(&strbuf, "121")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 120.999999, 2); + VERIFY(ffStrbufEqualS(&strbuf, "121.00")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 120.123456789, 0); + VERIFY(ffStrbufEqualS(&strbuf, "120")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 120.123456789, -1); + VERIFY(ffStrbufEqualS(&strbuf, "120.123456789")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 120.123, 5); + VERIFY(ffStrbufEqualS(&strbuf, "120.12300")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.0, 0); + VERIFY(ffStrbufEqualS(&strbuf, "-120")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.0, 1); + VERIFY(ffStrbufEqualS(&strbuf, "-120.0")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.0, 5); + VERIFY(ffStrbufEqualS(&strbuf, "-120.00000")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.123456789, 5); + VERIFY(ffStrbufEqualS(&strbuf, "-120.12346")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.123456789, 0); + VERIFY(ffStrbufEqualS(&strbuf, "-120")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.123456789, -1); + VERIFY(ffStrbufEqualS(&strbuf, "-120.123456789")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.123, 5); + VERIFY(ffStrbufEqualS(&strbuf, "-120.12300")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.888888, 0); + VERIFY(ffStrbufEqualS(&strbuf, "-121")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -120.999999, 2); + VERIFY(ffStrbufEqualS(&strbuf, "-121.00")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 1.2345e50, 1); + VERIFY(ffStrbufEqualS(&strbuf, "1.2345e50")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -1.2345e50, 1); + VERIFY(ffStrbufEqualS(&strbuf, "-1.2345e50")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 1.2345e50, 0); + VERIFY(ffStrbufEqualS(&strbuf, "1.2345e50")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -1.2345e50, 0); + VERIFY(ffStrbufEqualS(&strbuf, "-1.2345e50")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 1.2345e50, -1); + VERIFY(ffStrbufEqualS(&strbuf, "1.2345e50")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -1.2345e50, -1); + VERIFY(ffStrbufEqualS(&strbuf, "-1.2345e50")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, 1.2345e20, 1); + VERIFY(ffStrbufEqualS(&strbuf, "123450000000000000000.0")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -1.2345e20, 1); + VERIFY(ffStrbufEqualS(&strbuf, "-123450000000000000000.0")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, +0.0, 0); + VERIFY(ffStrbufEqualS(&strbuf, "0")); + + ffStrbufClear(&strbuf); + ffStrbufAppendDouble(&strbuf, -0.0, 0); + VERIFY(ffStrbufEqualS(&strbuf, "-0")); + + ffStrbufDestroy(&strbuf); + } + //Success puts("\e[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET); }