diff --git a/.clang-format b/.clang-format index 0fd07240..228a7913 100644 --- a/.clang-format +++ b/.clang-format @@ -31,23 +31,23 @@ BinPackParameters: true BitFieldColonSpacing: Both BraceWrapping: AfterCaseLabel: false - AfterClass: true - AfterControlStatement: Always - AfterEnum: true - AfterExternBlock: true - AfterFunction: true - AfterNamespace: true - AfterObjCDeclaration: true - AfterStruct: true - AfterUnion: true + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false BeforeCatch: true BeforeElse: true BeforeLambdaBody: false BeforeWhile: false IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false # BreakAfterAttributes: Never # since clang-format 16 BreakAfterJavaFieldAnnotations: false # BreakArrays: true # since clang-format 16 @@ -100,7 +100,7 @@ IndentCaseBlocks: false IndentCaseLabels: false IndentExternBlock: AfterExternBlock IndentGotoLabels: true -IndentPPDirectives: BeforeHash +IndentPPDirectives: None # IndentRequiresClause: true # since clang-format 15 IndentWidth: 4 IndentWrappedFunctionNames: false diff --git a/.github/workflows/amalgamation_check.yml b/.github/workflows/amalgamation_check.yml index f75f6315..207f1eed 100644 --- a/.github/workflows/amalgamation_check.yml +++ b/.github/workflows/amalgamation_check.yml @@ -9,11 +9,13 @@ on: - .github/workflows/amalgamation_check.yml - include/fkYAML/** - single_include/fkYAML/** + - scripts/*_amalgamation.* pull_request: paths: - .github/workflows/amalgamation_check.yml - include/fkYAML/** - single_include/fkYAML/** + - scripts/*_amalgamation.* workflow_dispatch: concurrency: diff --git a/.github/workflows/clang_format_check.yml b/.github/workflows/clang_format_check.yml index 2bd25b78..00e8c5c3 100644 --- a/.github/workflows/clang_format_check.yml +++ b/.github/workflows/clang_format_check.yml @@ -10,12 +10,14 @@ on: - include/** - test/unit_test/** - .clang-format + - scripts/run_clang_format.* pull_request: paths: - .github/workflows/clang_format_check.yml - include/** - test/unit_test/** - .clang-format + - scripts/run_clang_format.* workflow_dispatch: concurrency: diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index fc22059f..252cad55 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -35,8 +35,9 @@ jobs: runs-on: windows-2019 strategy: matrix: + arch: [ Win32, x64 ] build_type: [ Debug, Release ] - single_header: ["ON", "OFF"] + single_header: [ "ON", "OFF" ] steps: - uses: actions/checkout@v4 @@ -44,7 +45,7 @@ jobs: submodules: recursive - name: Configure CMake - run: cmake -B ${{github.workspace}}/build -G "Visual Studio 16 2019" -A x64 -DFK_YAML_BUILD_TEST=ON -DFK_YAML_USE_SINGLE_HEADER=${{matrix.single_header}} + run: cmake -B ${{github.workspace}}/build -G "Visual Studio 16 2019" -A ${{matrix.arch}} -DFK_YAML_BUILD_TEST=ON -DFK_YAML_USE_SINGLE_HEADER=${{matrix.single_header}} - name: Build run: cmake --build ${{github.workspace}}/build --config ${{matrix.build_type}} -j ${{env.JOBS}} @@ -58,8 +59,9 @@ jobs: runs-on: windows-latest strategy: matrix: + arch: [ Win32, x64 ] build_type: [ Debug, Release ] - single_header: ["ON", "OFF"] + single_header: [ "ON", "OFF" ] steps: - uses: actions/checkout@v4 @@ -67,7 +69,7 @@ jobs: submodules: recursive - name: Configure CMake - run: cmake -B ${{github.workspace}}/build -G "Visual Studio 17 2022" -A x64 -DFK_YAML_BUILD_TEST=ON -DFK_YAML_USE_SINGLE_HEADER=${{matrix.single_header}} + run: cmake -B ${{github.workspace}}/build -G "Visual Studio 17 2022" -A ${{matrix.arch}} -DFK_YAML_BUILD_TEST=ON -DFK_YAML_USE_SINGLE_HEADER=${{matrix.single_header}} - name: Build run: cmake --build ${{github.workspace}}/build --config ${{matrix.build_type}} -j ${{env.JOBS}} diff --git a/.gitignore b/.gitignore index 00a6ca75..6160d917 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ /.vs/ # documentation -/docs/mkdocs/docs/examples/ /docs/mkdocs/site/ /docs/mkdocs/venv/ diff --git a/.reuse/templates/fkYAML.commented.jinja2 b/.reuse/templates/fkYAML.commented.jinja2 index 6da69169..f923c838 100644 --- a/.reuse/templates/fkYAML.commented.jinja2 +++ b/.reuse/templates/fkYAML.commented.jinja2 @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// {% for copyright_line in copyright_lines %} diff --git a/.reuse/templates/fkYAML_support.jinja2 b/.reuse/templates/fkYAML_support.jinja2 index e0574bd0..95b3ef49 100644 --- a/.reuse/templates/fkYAML_support.jinja2 +++ b/.reuse/templates/fkYAML_support.jinja2 @@ -1,6 +1,6 @@ _______ __ __ __ _____ __ __ __ | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -| __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +| __| _ < \_ _/| ___ | _ | |___ version 0.3.5 |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML {% for copyright_line in copyright_lines %} diff --git a/CHANGELOG.md b/CHANGELOG.md index 586fe72b..2835b7a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Changelog +## [v0.3.5](https://github.com/fktn-k/fkYAML/releases/tag/v0.3.5) (2024-04-27) + +[Full Changelog](https://github.com/fktn-k/fkYAML/compare/v0.3.4...v0.3.5) + +- Fix parse error on the first node which isn't a block mapping [\#322](https://github.com/fktn-k/fkYAML/pull/322) ([fktn-k](https://github.com/fktn-k)) + +- Add Bash & Batch scripts to run clang-format & amalgamation [\#325](https://github.com/fktn-k/fkYAML/pull/325) ([fktn-k](https://github.com/fktn-k)) +- Force LF newline codes in the amalgamated file [\#324](https://github.com/fktn-k/fkYAML/pull/324) ([fktn-k](https://github.com/fktn-k)) +- Add workflows items to test Win32 builds [\#323](https://github.com/fktn-k/fkYAML/pull/323) ([fktn-k](https://github.com/fktn-k)) +- Set the unit test app as the Visual Studio startup project [\#321](https://github.com/fktn-k/fkYAML/pull/321) ([fktn-k](https://github.com/fktn-k)) +- Reformat source files [\#320](https://github.com/fktn-k/fkYAML/pull/320) ([fktn-k](https://github.com/fktn-k)) +- Serialize container keys [\#319](https://github.com/fktn-k/fkYAML/pull/319) ([fktn-k](https://github.com/fktn-k)) +- Improved UTF encoding processing [\#318](https://github.com/fktn-k/fkYAML/pull/318) ([fktn-k](https://github.com/fktn-k)) +- Query the count of UTF-8 character bytes [\#317](https://github.com/fktn-k/fkYAML/pull/317) ([fktn-k](https://github.com/fktn-k)) +- Use Clang-Format python distribution [\#316](https://github.com/fktn-k/fkYAML/pull/316) ([fktn-k](https://github.com/fktn-k)) + ## [v0.3.4](https://github.com/fktn-k/fkYAML/releases/tag/v0.3.4) (2024-04-20) [Full Changelog](https://github.com/fktn-k/fkYAML/compare/v0.3.3...v0.3.4) diff --git a/CMakeLists.txt b/CMakeLists.txt index b22d3947..e73858b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ cmake_minimum_required(VERSION 3.8) project( fkYAML - VERSION 0.3.4 + VERSION 0.3.5 LANGUAGES CXX) ############################################################# @@ -140,6 +140,13 @@ if(FK_YAML_BUILD_TEST OR FK_YAML_BUILD_ALL_TEST) include(CTest) enable_testing() add_subdirectory(test) + + # Set the unit test app project as the Visual Studio startup project + # if the target compiler is some version of Microsoft Visual C++ and + # if this project is the main project. + if(MSVC AND CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT fkYAMLUnitTest) + endif() endif() ####################### diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8fbb448f..c40a21a6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,18 +8,15 @@ To make it as easy as possible for you to contribute and for me to keep an overv Usually, all issues are tracked publicly on [GitHub](https://github.com/fktn-k/fkYAML/issues). If you want to make a private report (e.g., for a vulnerability or to attach an example that is not meant to be published), please send an email to . -## Prerequisites +## Describe your request/issue Please create a [discussion](https://github.com/fktn-k/fkYAML/discussions) or [issue](https://github.com/fktn-k/fkYAML/issues/new/choose), assuming one does not already exist, and describe your concern. Note you need a [GitHub account](https://github.com/signup/free) for this. -## Describe your request/issue - -Clearly describe the request/issue: - +Please keep in mind the followings: - If you propose a change or addition, try to give an **example** how the improved code could look like or how to use it. -- If it is a bug, please describe how to **reproduce** it. If possible, attach a complete example which demonstrates the error. Please also state what you **expected** to happen instead of the error. -- If you found a compilation error, please tell us which **compiler** (version and operating system) you used and paste the (relevant part of) the error messages to the ticket. +- If you find a bug, please describe how to **reproduce** it. If possible, attach a complete example which demonstrates the error. Please also state what you **expected** to happen instead of the error. +- If you find a compilation error, please tell us which **compiler** (version and operating system) you used and paste the (relevant part of) the error messages to the ticket. For questions, feature or support requests, please [open a discussion](https://github.com/fktn-k/fkYAML/discussions/new). @@ -30,14 +27,33 @@ To make changes to the fkYAML project, you need to edit the following files: ### 1. [`include/fkYAML/**.hpp`](https://github.com/fktn-k/fkYAML/tree/develop/include/fkYAML) -These files are the sources of the fkYAML library. After making changes in those files, please run `make amalgamate` to regenerate [`single_include/fkYAML/node.hpp`](https://github.com/fktn-k/fkYAML/tree/develop/single_include/fkYAML/node.hpp) at least before making a PR. If the `make` command is unavailable on your local environment, the following commands will do the same: +These files are the sources of the fkYAML library. After making changes in those files, please run the script ([`run_amalgamation.bat`](https://github.com/fktn-k/fkYAML/scripts/run_amalgamation.bat) for Windows, [`run_amalgamation.sh`](https://github.com/fktn-k/fkYAML/scripts/run_amalgamation.sh) otherwise) to regenerate [`single_include/fkYAML/node.hpp`](https://github.com/fktn-k/fkYAML/tree/develop/single_include/fkYAML/node.hpp) at least before making a PR, with the following commands: + +**Windows** +```batch +cd path\to\fkYAML +scripts\run_amalgamation.bat +``` +**Otherwise** ```bash -$ cd path/to/fkYAML -$ python3 ./tool/amalgamation/amalgamate.py -c ./tool/amalgamation/fkYAML.json -s . --verbose=yes +cd path/to/fkYAML +scripts/run_amalgamation.sh ``` -Note that the amalgamation tool assumes that your current directory is at the root of the fkYAML project. +If you just want to check if amalgamation is needed, run the script ([`run_amalgamation.bat`](https://github.com/fktn-k/fkYAML/scripts/run_amalgamation.bat) for Windows, [`run_amalgamation.sh`](https://github.com/fktn-k/fkYAML/scripts/run_amalgamation.sh) otherwise) with the following commands: + +**Windows** +```batch +cd path\to\fkYAML +scripts\check_amalgamation.bat +``` + +**Otherwise** +```bash +cd path/to/fkYAML +scripts/check_amalgamation.sh +``` ### 2. [`test/unit_test/*.cpp`](https://github.com/fktn-k/fkYAML/tree/develop/test/unit_test) @@ -46,10 +62,10 @@ These files contain the [Catch2](https://github.com/catchorg/Catch2) unit tests The unit tests can be compiled and executed with the following commands: ```bash -$ cd path/to/fkYAML -$ cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug -DFK_YAML_BUILD_TEST=ON [-DFK_YAML_USE_SINGLE_HEADER=ON|OFF] -$ cmake --build build --config Debug -$ ctest -C Debug --test-dir build --output-on-failure +cd path/to/fkYAML +cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug -DFK_YAML_BUILD_TEST=ON [-DFK_YAML_USE_SINGLE_HEADER=(ON|OFF)] +cmake --build build --config Debug +ctest -C Debug --test-dir build --output-on-failure ``` Furthermore, you can test the single-header version of fkYAML by passing `-DFK_YAML_USE_SINGLE_HEADER=ON` when you configure CMake. (disabled by default) @@ -64,8 +80,8 @@ In such cases, please run the following commands and open a generated `index.htm Make sure `lcov` and `genhtml` commands are available before execution. ```bash -$ cd /path/to/fkYAML -$ make html-coverage +cd /path/to/fkYAML +make html-coverage ``` ##### For Windows users (help needed) @@ -81,28 +97,55 @@ These files are the sources of the documentation from which [MkDocs](https://www Also, you can build & check the updated documentation on your local environment by executing the following commands: ```bash -$ cd path/to/fkYAML -$ make -C docs/mkdocs serve +cd path/to/fkYAML +make -C docs/mkdocs serve ``` The commands above will automatically install all the dependencies using [Python venv](https://docs.python.org/3.10/library/venv.html) with the [`requirements.txt`](https://github.com/fktn-k/fkYAML/blob/develop/docs/mkdocs/requirements.txt) file. The generated documentation will be available by accessing http://127.0.0.1:8000/. -## Before Opening a PR +### 4. Format source files -[GitHub Actions](https://github.com/fktn-k/fkYAML/actions) will test the updated project with the following Clang tools with the specific versions listed down below once you open a PR for your changes. So, it would be more efficient to check if your changes follow the predefined rules on your local environment in advance. +[GitHub Actions](https://github.com/fktn-k/fkYAML/actions) will test the updated project with the [Clang-Format](https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormat.html) tool (14.0.0) when you push commits which include changes in the source files in either `include` or `test` directories. +So, it would be more efficient to check if your changes follow the rules defined in the [`.clang-format`](https://github.com/fktn-k/fkYAML/tree/develop/.clang-format) file on your local environment in advance. +Since the Clang-Format tool does not seem to gurantee backward compatibility and its behaviors vary from version to version, the script files ([`run_clang_format.bat`](https://github.com/fktn-k/fkYAML/scripts/run_clang_format.bat) for Windows, [`run_clang_format.sh`](https://github.com/fktn-k/fkYAML/scripts/run_clang_format.sh) otherwise) are available to avoid unnecessary confusion for that kind of reason. +The scripts uses [the Clang-Format Python distribution](https://pypi.org/project/clang-format/14.0.0/) and installs it using [the Python venv module](https://docs.python.org/3/library/venv.html) if it's not installed yet. +So, all you need to run the script is only Python 3.3 or better. +You can run it with the following commands: -| Clang Tool Name | Version | -| ------------------------------------------------------------------------------------------------- | ------- | -| [Clang-Format](https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormat.html) | 14.0.0 | -| [Clang-Tidy](https://releases.llvm.org/14.0.0/tools/clang/tools/extra/docs/clang-tidy/index.html) | 14.0.0 | +**Windows** +```batch +cd path\to\fkYAML +scripts\run_clang_format.bat +``` -Those Clang tools, however, do not seem to gurantee backward compatibility and their behaviours vary from version to version. So, it is highly recommended to use the specific versions listed above to avoid unnecessary confusion. +**Otherwise** +```bash +cd path/to/fkYAML +scripts/run_clang_format.sh +``` ## Note -When you open a pull request, fkYAML will automatically be built/tested with (1) various combinations of compilers and operating systems and (2) [Valgrind](https://valgrind.org/) and [Clang Sanitizers](https://clang.llvm.org/docs/index.html) to detect runtime issues (e.g., memory leaks), on [GitHub Actions](https://github.com/fktn-k/fkYAML/actions) once you open a pull request. +When you open a pull request, fkYAML will automatically be built/tested with (1) various combinations of compilers and operating systems and (2) analyzers such as [Valgrind](https://valgrind.org/) and [Clang Sanitizers](https://clang.llvm.org/docs/index.html) to detect runtime issues (e.g., memory leaks), on [GitHub Actions](https://github.com/fktn-k/fkYAML/actions) once you open a pull request. These can result in failing builds and/or unit tests which run successfully on your local environment. As a policy of this project, however, all the workflow checks must be passed before merging. +You can run tests with those tools with the following commands: + +**Valgrind** (assuming Valgrind is already installed.) +```bash +cd path/to/fkYAML +cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug -DFK_YAML_BUOLD_TEST=ON -DFK_YAML_RUN_VALGRIND=ON +cmake --build build --config Debug +ctest -C Debug -T memcheck --test-dir build --output-on-failure +``` + +**Clang Sanitizers** (assuming some version of Clang is already installed.) +```bash +cd path/to/fkYAML +CXX=clang++ cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug -DFK_YAML_BUILD_TEST=ON -DFK_YAML_RUN_CLANG_SANITIZERS=ON +cmake --build build --config Debug +ctest -C Debug --test-dir build --output-on-failure +``` ## Please don't diff --git a/Makefile b/Makefile index bc0f2b10..5cdda56e 100644 --- a/Makefile +++ b/Makefile @@ -6,15 +6,15 @@ # list of source files in the include directory. SRCS = $(shell find include -type f -name '*.hpp' | sort) -# The single-header version -SINGLE_SRC = 'single_include/fkYAML/node.hpp' # list of sources in the test directory. -TEST_SRCS = $(shell find test -type f \( -name '*.hpp' -o -name '*.cpp' \) | sort) +TEST_SRCS = $(shell find test -type f -name '*.hpp' -o -name '*.cpp' | sort) +# list of sources in the examples directory. +EXAMPLE_SRCS = $(shell find docs/examples -type f -name '*.cpp' | sort) # target version definition TARGET_MAJOR_VERSION := 0 TARGET_MINOR_VERSION := 3 -TARGET_PATCH_VERSION := 4 +TARGET_PATCH_VERSION := 5 TARGET_VERSION_FULL := $(TARGET_MAJOR_VERSION).$(TARGET_MINOR_VERSION).$(TARGET_PATCH_VERSION) VERSION_MACRO_FILE := include/fkYAML/detail/macros/version_macros.hpp @@ -42,9 +42,8 @@ all: # Static Code Analyzers # ############################# -# pre-requisites: clang-format clang-format: - for FILE in $(SRCS) $(TEST_SRCS); do echo $$FILE; clang-format -i $$FILE; done + ./scripts/run_clang_format.sh # pre-requisites: clang-tidy clang-tidy: @@ -64,27 +63,23 @@ iwyu: clang-sanitizers: CXX=clang++ cmake -B build_clang_sanitizers -S . -DCMAKE_BUILD_TYPE=Debug -DFK_YAML_BUILD_TEST=ON -DFK_YAML_RUN_CLANG_SANITIZERS=ON cmake --build build_clang_sanitizers --config Debug -j $(JOBS) - ctest -C Debug --output-on-failure --test-dir build_clang_sanitizers -j $(JOBS) + ctest -C Debug --test-dir build_clang_sanitizers --output-on-failure -j $(JOBS) # pre-requisites: valgrind valgrind: cmake -B build_valgrind -S . -DCMAKE_BUILD_TYPE=Debug -DFK_YAML_BUILD_TEST=ON -DFK_YAML_RUN_VALGRIND=ON cmake --build build_valgrind --config Debug -j $(JOBS) - ctest -C Debug -T memcheck --test-dir build_valgrind -j $(JOBS) + ctest -C Debug -T memcheck --test-dir build_valgrind --output-on-failure -j $(JOBS) ########################### # Source Amalgamation # ########################### amalgamate: - python3 ./tool/amalgamation/amalgamate.py -c ./tool/amalgamation/fkYAML.json -s . --verbose=yes + ./scripts/run_amalgamation.sh check-amalgamate: - @cp $(SINGLE_SRC) $(SINGLE_SRC)~ - @$(MAKE) amalgamate - @diff $(SINGLE_SRC) $(SINGLE_SRC)~ || (echo Amalgamation required. Please follow the guideline in the CONTRIBUTING.md file. ; mv $(SINGLE_SRC)~ $(SINGLE_SRC) ; false) - @mv $(SINGLE_SRC)~ $(SINGLE_SRC) - @echo Amalgamation check passed successfully. + ./scripts/check_amalgamation.sh ########################################## # Natvis Debugger Visualization File # @@ -108,7 +103,7 @@ build-docs: examples @$(MAKE) -C ./docs/mkdocs build serve-docs: examples - @$(MAKE) -C serve + @$(MAKE) -C ./docs/mkdocs serve ############### # Version # @@ -134,7 +129,7 @@ reuse: update-reuse-templates pipx run reuse annotate $(SRCS) --template fkYAML \ --copyright "Kensuke Fukutani " --copyright-style spdx \ --license MIT --year "2023-2024" --style c - pipx run reuse annotate $(TEST_SRCS) --template fkYAML_support \ + pipx run reuse annotate $(TEST_SRCS) $(EXAMPLE_SRCS) --template fkYAML_support \ --copyright "Kensuke Fukutani " --copyright-style spdx \ --license MIT --year "2023-2024" --style c pipx run reuse lint diff --git a/docs/examples/ex_basic_node_add_anchor_name.cpp b/docs/examples/ex_basic_node_add_anchor_name.cpp index 2169518a..9bed815a 100644 --- a/docs/examples/ex_basic_node_add_anchor_name.cpp +++ b/docs/examples/ex_basic_node_add_anchor_name.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node n = 123; diff --git a/docs/examples/ex_basic_node_add_tag_name.cpp b/docs/examples/ex_basic_node_add_tag_name.cpp index 7474c591..9824bf93 100644 --- a/docs/examples/ex_basic_node_add_tag_name.cpp +++ b/docs/examples/ex_basic_node_add_tag_name.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node n = 123; diff --git a/docs/examples/ex_basic_node_alias_of.cpp b/docs/examples/ex_basic_node_alias_of.cpp index 5519d1c6..196f685a 100644 --- a/docs/examples/ex_basic_node_alias_of.cpp +++ b/docs/examples/ex_basic_node_alias_of.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node anchor_node = "test"; @@ -13,7 +20,7 @@ int main() fkyaml::node alias_node = fkyaml::node::alias_of(anchor_node); // print the value in the alias node. - std::cout<< alias_node.get_value_ref() << std::endl; + std::cout << alias_node.get_value_ref() << std::endl; return 0; } diff --git a/docs/examples/ex_basic_node_at_basic_node.cpp b/docs/examples/ex_basic_node_at_basic_node.cpp index 8a48bcae..2b5b6d34 100644 --- a/docs/examples/ex_basic_node_at_basic_node.cpp +++ b/docs/examples/ex_basic_node_at_basic_node.cpp @@ -1,29 +1,34 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML sequence node. fkyaml::node n1 = {123, 234, 345, 456}; // print YAML nodes at the following indexes. - fkyaml::node index_zero = 0; - fkyaml::node index_one = 1; - fkyaml::node index_two = 2; + fkyaml::node index_zero = 0; + fkyaml::node index_one = 1; + fkyaml::node index_two = 2; fkyaml::node index_three = 3; - std::cout << n1[index_zero] << std::endl; - std::cout << n1[index_one] << std::endl; - std::cout << n1[index_two] << std::endl; + std::cout << n1[index_zero] << std::endl; + std::cout << n1[index_one] << std::endl; + std::cout << n1[index_two] << std::endl; std::cout << n1[index_three] << std::endl; // try to print a YAML node with an index which exceeds the size. - try - { + try { fkyaml::node index_four = 4; std::cout << n1.at(index_four) << std::endl; } - catch (const fkyaml::out_of_range& e) - { + catch (const fkyaml::out_of_range& e) { std::cout << e.what() << std::endl; } @@ -37,13 +42,11 @@ int main() std::cout << n2[bar_key] << std::endl; // try to print a YAML node with a key which does not exist. - try - { + try { fkyaml::node true_key = true; std::cout << n2.at(true_key) << std::endl; } - catch (const fkyaml::out_of_range& e) - { + catch (const fkyaml::out_of_range& e) { std::cout << e.what() << std::endl; } diff --git a/docs/examples/ex_basic_node_at_compatible_type.cpp b/docs/examples/ex_basic_node_at_compatible_type.cpp index 971fb790..bef10397 100644 --- a/docs/examples/ex_basic_node_at_compatible_type.cpp +++ b/docs/examples/ex_basic_node_at_compatible_type.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML sequence node. fkyaml::node n1 = {123, 234, 345, 456}; @@ -13,12 +20,10 @@ int main() std::cout << n1.at(3) << std::endl; // try to print a YAML node with an index which exceeds the size. - try - { + try { std::cout << n1.at(4) << std::endl; } - catch (const fkyaml::out_of_range& e) - { + catch (const fkyaml::out_of_range& e) { std::cout << e.what() << std::endl; } @@ -30,12 +35,10 @@ int main() std::cout << n2.at("bar") << std::endl; // try to print a YAML node with a key which does not exist. - try - { + try { std::cout << n2.at(true) << std::endl; } - catch (const fkyaml::out_of_range& e) - { + catch (const fkyaml::out_of_range& e) { std::cout << e.what() << std::endl; } diff --git a/docs/examples/ex_basic_node_begin.cpp b/docs/examples/ex_basic_node_begin.cpp index 23aa0b74..9554a0c6 100644 --- a/docs/examples/ex_basic_node_begin.cpp +++ b/docs/examples/ex_basic_node_begin.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a sequence node. fkyaml::node n = {"foo", "bar"}; // get an iterator to the first element. diff --git a/docs/examples/ex_basic_node_boolean_type.cpp b/docs/examples/ex_basic_node_boolean_type.cpp index 427118a5..dbe9e5f3 100644 --- a/docs/examples/ex_basic_node_boolean_type.cpp +++ b/docs/examples/ex_basic_node_boolean_type.cpp @@ -1,10 +1,17 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include -int main() -{ +int main() { std::cout << std::boolalpha << std::is_same::value << std::endl; return 0; } diff --git a/docs/examples/ex_basic_node_const_iterator.cpp b/docs/examples/ex_basic_node_const_iterator.cpp index 8c4bc88f..e422911c 100644 --- a/docs/examples/ex_basic_node_const_iterator.cpp +++ b/docs/examples/ex_basic_node_const_iterator.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. const fkyaml::node sequence_node = {1, 2, 3}; // get an iterator to the first sequence element. diff --git a/docs/examples/ex_basic_node_constructor_1.cpp b/docs/examples/ex_basic_node_constructor_1.cpp index 634a44ed..4b0ed3eb 100644 --- a/docs/examples/ex_basic_node_constructor_1.cpp +++ b/docs/examples/ex_basic_node_constructor_1.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { fkyaml::node n; std::cout << n << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_constructor_2.cpp b/docs/examples/ex_basic_node_constructor_2.cpp index cf51d42c..ac17bac4 100644 --- a/docs/examples/ex_basic_node_constructor_2.cpp +++ b/docs/examples/ex_basic_node_constructor_2.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { fkyaml::node n(fkyaml::node::node_t::INTEGER); std::cout << n << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_constructor_3.cpp b/docs/examples/ex_basic_node_constructor_3.cpp index d64c0b84..2a26c6fa 100644 --- a/docs/examples/ex_basic_node_constructor_3.cpp +++ b/docs/examples/ex_basic_node_constructor_3.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { fkyaml::node n(fkyaml::node::node_t::BOOLEAN); fkyaml::node n2(n); std::cout << n << std::endl; diff --git a/docs/examples/ex_basic_node_constructor_4.cpp b/docs/examples/ex_basic_node_constructor_4.cpp index d64c0b84..2a26c6fa 100644 --- a/docs/examples/ex_basic_node_constructor_4.cpp +++ b/docs/examples/ex_basic_node_constructor_4.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { fkyaml::node n(fkyaml::node::node_t::BOOLEAN); fkyaml::node n2(n); std::cout << n << std::endl; diff --git a/docs/examples/ex_basic_node_constructor_5.cpp b/docs/examples/ex_basic_node_constructor_5.cpp index 866fbaf4..1af45a6b 100644 --- a/docs/examples/ex_basic_node_constructor_5.cpp +++ b/docs/examples/ex_basic_node_constructor_5.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { double pi = 3.141592; fkyaml::node n = pi; std::cout << n << std::endl; diff --git a/docs/examples/ex_basic_node_constructor_6.cpp b/docs/examples/ex_basic_node_constructor_6.cpp index 97bb6417..14de78a8 100644 --- a/docs/examples/ex_basic_node_constructor_6.cpp +++ b/docs/examples/ex_basic_node_constructor_6.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { fkyaml::node n({true, false}); std::cout << n << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_constructor_7.cpp b/docs/examples/ex_basic_node_constructor_7.cpp index a4c246f0..28727304 100644 --- a/docs/examples/ex_basic_node_constructor_7.cpp +++ b/docs/examples/ex_basic_node_constructor_7.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { fkyaml::node n = {true, false}; std::cout << n << std::endl; diff --git a/docs/examples/ex_basic_node_contains.cpp b/docs/examples/ex_basic_node_contains.cpp index 23716595..dae0eaef 100644 --- a/docs/examples/ex_basic_node_contains.cpp +++ b/docs/examples/ex_basic_node_contains.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create a YAML mapping node. fkyaml::node n = {{"foo", true}, {"bar", 123}}; diff --git a/docs/examples/ex_basic_node_copy_assignment_operator.cpp b/docs/examples/ex_basic_node_copy_assignment_operator.cpp index 97820ef6..5707e7a0 100644 --- a/docs/examples/ex_basic_node_copy_assignment_operator.cpp +++ b/docs/examples/ex_basic_node_copy_assignment_operator.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { fkyaml::node n = true; fkyaml::node n2 = 123; n = n2; diff --git a/docs/examples/ex_basic_node_deserialize_char_array.cpp b/docs/examples/ex_basic_node_deserialize_char_array.cpp index 5011d190..480ee408 100644 --- a/docs/examples/ex_basic_node_deserialize_char_array.cpp +++ b/docs/examples/ex_basic_node_deserialize_char_array.cpp @@ -1,10 +1,17 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include -int main() -{ +int main() { // deserialize a YAML string. char input[] = R"( foo: true diff --git a/docs/examples/ex_basic_node_deserialize_file_pointer.cpp b/docs/examples/ex_basic_node_deserialize_file_pointer.cpp index 23af9164..93c7f9ab 100644 --- a/docs/examples/ex_basic_node_deserialize_file_pointer.cpp +++ b/docs/examples/ex_basic_node_deserialize_file_pointer.cpp @@ -1,15 +1,21 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include #include -int main() -{ +int main() { // deserialize a YAML string. FILE* p_file = std::fopen("input.yaml", "r"); - if (!p_file) - { + if (!p_file) { // You must not pass a null FILE pointer. return -1; } diff --git a/docs/examples/ex_basic_node_deserialize_iterators.cpp b/docs/examples/ex_basic_node_deserialize_iterators.cpp index 94486376..3b767a7c 100644 --- a/docs/examples/ex_basic_node_deserialize_iterators.cpp +++ b/docs/examples/ex_basic_node_deserialize_iterators.cpp @@ -1,3 +1,11 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include @@ -5,12 +13,9 @@ #include #include -int main() -{ +int main() { // deserialize a YAML string. - std::array input = { - 'f', 'o', 'o', ':', ' ', 't', 'r', 'u', 'e', 'd', 'u', 'm', 'm', 'y' - }; + std::array input = {'f', 'o', 'o', ':', ' ', 't', 'r', 'u', 'e', 'd', 'u', 'm', 'm', 'y'}; fkyaml::node n = fkyaml::node::deserialize(input.begin(), input.begin() + 9); // check the deserialization result. diff --git a/docs/examples/ex_basic_node_deserialize_string.cpp b/docs/examples/ex_basic_node_deserialize_string.cpp index babff77b..6c0c47b7 100644 --- a/docs/examples/ex_basic_node_deserialize_string.cpp +++ b/docs/examples/ex_basic_node_deserialize_string.cpp @@ -1,11 +1,18 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include #include -int main() -{ +int main() { // deserialize a YAML string. std::string s = R"( foo: true diff --git a/docs/examples/ex_basic_node_empty.cpp b/docs/examples/ex_basic_node_empty.cpp index eb474506..a393d278 100644 --- a/docs/examples/ex_basic_node_empty.cpp +++ b/docs/examples/ex_basic_node_empty.cpp @@ -1,12 +1,18 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. - std::vector nodes = - { + std::vector nodes = { fkyaml::node::sequence(), {1, 2, 3}, {{"foo", true}, {"bar", false}}, @@ -14,18 +20,14 @@ int main() true, 256, 3.14, - "Hello, world!" - }; + "Hello, world!"}; - for (const auto& n : nodes) - { - try - { + for (const auto& n : nodes) { + try { // call empty() std::cout << std::boolalpha << n.empty() << std::endl; } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << "The node does not have a container nor string value." << std::endl; } } diff --git a/docs/examples/ex_basic_node_end.cpp b/docs/examples/ex_basic_node_end.cpp index 8a9ae8c4..8124ec3c 100644 --- a/docs/examples/ex_basic_node_end.cpp +++ b/docs/examples/ex_basic_node_end.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a sequence node. fkyaml::node n = {"foo", "bar"}; // get an iterator to the past-the-last element. diff --git a/docs/examples/ex_basic_node_extraction_operator.cpp b/docs/examples/ex_basic_node_extraction_operator.cpp index 448a9834..1223222c 100644 --- a/docs/examples/ex_basic_node_extraction_operator.cpp +++ b/docs/examples/ex_basic_node_extraction_operator.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { std::ifstream ifs("input.yaml"); fkyaml::node n; ifs >> n; diff --git a/docs/examples/ex_basic_node_float_number_type.cpp b/docs/examples/ex_basic_node_float_number_type.cpp index 31bb79b7..41e727e3 100644 --- a/docs/examples/ex_basic_node_float_number_type.cpp +++ b/docs/examples/ex_basic_node_float_number_type.cpp @@ -1,12 +1,17 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include -int main() -{ - std::cout << std::boolalpha - << std::is_same::value - << std::endl; +int main() { + std::cout << std::boolalpha << std::is_same::value << std::endl; return 0; } diff --git a/docs/examples/ex_basic_node_get_anchor_name.cpp b/docs/examples/ex_basic_node_get_anchor_name.cpp index 578669cb..a9f718c2 100644 --- a/docs/examples/ex_basic_node_get_anchor_name.cpp +++ b/docs/examples/ex_basic_node_get_anchor_name.cpp @@ -1,18 +1,23 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node n = 123; // try to get an anchor name before any anchor name has been set. - try - { + try { std::cout << n.get_anchor_name() << std::endl; } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } diff --git a/docs/examples/ex_basic_node_get_tag_name.cpp b/docs/examples/ex_basic_node_get_tag_name.cpp index c99aa545..18e82f75 100644 --- a/docs/examples/ex_basic_node_get_tag_name.cpp +++ b/docs/examples/ex_basic_node_get_tag_name.cpp @@ -1,18 +1,23 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node n = 123; // try to get a tag name before any tag name has been set. - try - { + try { std::cout << n.get_tag_name() << std::endl; } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } diff --git a/docs/examples/ex_basic_node_get_value.cpp b/docs/examples/ex_basic_node_get_value.cpp index ed219ed0..194e01a5 100644 --- a/docs/examples/ex_basic_node_get_value.cpp +++ b/docs/examples/ex_basic_node_get_value.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node n = 123; fkyaml::node n2 = "foo"; @@ -16,12 +23,10 @@ int main() std::cout << str_val << std::endl; // specifying incompatible type throws an exception - try - { + try { auto float_val = n2.get_value(); } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } diff --git a/docs/examples/ex_basic_node_get_value_ref.cpp b/docs/examples/ex_basic_node_get_value_ref.cpp index b59c64c2..8d31065e 100644 --- a/docs/examples/ex_basic_node_get_value_ref.cpp +++ b/docs/examples/ex_basic_node_get_value_ref.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node n = 123; @@ -15,12 +22,10 @@ int main() std::cout << cref << std::endl; // specifying incompatible reference type throws an exception - try - { + try { auto iref = n.get_value_ref(); } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } diff --git a/docs/examples/ex_basic_node_get_yaml_version.cpp b/docs/examples/ex_basic_node_get_yaml_version.cpp index fde04718..6efdf12a 100644 --- a/docs/examples/ex_basic_node_get_yaml_version.cpp +++ b/docs/examples/ex_basic_node_get_yaml_version.cpp @@ -1,14 +1,19 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // A `fkyaml::node` object has `yaml_version_t::VER_1_2` by default. fkyaml::node n; - std::cout << std::boolalpha - << (n.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_2) - << std::endl; + std::cout << std::boolalpha << (n.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_2) << std::endl; return 0; } diff --git a/docs/examples/ex_basic_node_has_anchor_name.cpp b/docs/examples/ex_basic_node_has_anchor_name.cpp index 67f62761..df173213 100644 --- a/docs/examples/ex_basic_node_has_anchor_name.cpp +++ b/docs/examples/ex_basic_node_has_anchor_name.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node n = {{"foo", true}}; diff --git a/docs/examples/ex_basic_node_has_tag_name.cpp b/docs/examples/ex_basic_node_has_tag_name.cpp index 5c6469b5..25e54098 100644 --- a/docs/examples/ex_basic_node_has_tag_name.cpp +++ b/docs/examples/ex_basic_node_has_tag_name.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. fkyaml::node n = {{"foo", true}}; diff --git a/docs/examples/ex_basic_node_insertion_operator.cpp b/docs/examples/ex_basic_node_insertion_operator.cpp index 53bbe894..2e48aaec 100644 --- a/docs/examples/ex_basic_node_insertion_operator.cpp +++ b/docs/examples/ex_basic_node_insertion_operator.cpp @@ -1,17 +1,17 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a basic_node object. - fkyaml::node n = { - {"foo", true}, - {"bar", {1, 2, 3}}, - {"baz", { - {"qux", 3.14}, - {"corge", nullptr} - }} - }; + fkyaml::node n = {{"foo", true}, {"bar", {1, 2, 3}}, {"baz", {{"qux", 3.14}, {"corge", nullptr}}}}; // serialize the basic_node object with insertion operator. // this is equivalent with: diff --git a/docs/examples/ex_basic_node_integer_type.cpp b/docs/examples/ex_basic_node_integer_type.cpp index b8af829f..10e2a763 100644 --- a/docs/examples/ex_basic_node_integer_type.cpp +++ b/docs/examples/ex_basic_node_integer_type.cpp @@ -1,13 +1,18 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include #include -int main() -{ - std::cout << std::boolalpha - << std::is_same::value - << std::endl; +int main() { + std::cout << std::boolalpha << std::is_same::value << std::endl; return 0; } diff --git a/docs/examples/ex_basic_node_is_alias.cpp b/docs/examples/ex_basic_node_is_alias.cpp index b15a689f..0b76079b 100644 --- a/docs/examples/ex_basic_node_is_alias.cpp +++ b/docs/examples/ex_basic_node_is_alias.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes (both anchor/non-anchor) fkyaml::node anchor = true; anchor.add_anchor_name("anchor"); diff --git a/docs/examples/ex_basic_node_is_anchor.cpp b/docs/examples/ex_basic_node_is_anchor.cpp index 4314c2f2..421b0a22 100644 --- a/docs/examples/ex_basic_node_is_anchor.cpp +++ b/docs/examples/ex_basic_node_is_anchor.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes (both anchor/non-anchor) fkyaml::node anchor = true; anchor.add_anchor_name("anchor"); diff --git a/docs/examples/ex_basic_node_is_boolean.cpp b/docs/examples/ex_basic_node_is_boolean.cpp index 20ce835a..010cdf4b 100644 --- a/docs/examples/ex_basic_node_is_boolean.cpp +++ b/docs/examples/ex_basic_node_is_boolean.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::node n = true; std::cout << std::boolalpha << n.is_boolean() << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_is_float_number.cpp b/docs/examples/ex_basic_node_is_float_number.cpp index 6b817719..86ac5acb 100644 --- a/docs/examples/ex_basic_node_is_float_number.cpp +++ b/docs/examples/ex_basic_node_is_float_number.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::node n = 3.14; std::cout << std::boolalpha << n.is_float_number() << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_is_integer.cpp b/docs/examples/ex_basic_node_is_integer.cpp index 4e1cdbfe..1380ad7a 100644 --- a/docs/examples/ex_basic_node_is_integer.cpp +++ b/docs/examples/ex_basic_node_is_integer.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::node n = 123; std::cout << std::boolalpha << n.is_integer() << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_is_mapping.cpp b/docs/examples/ex_basic_node_is_mapping.cpp index ec6ea424..2838b9c3 100644 --- a/docs/examples/ex_basic_node_is_mapping.cpp +++ b/docs/examples/ex_basic_node_is_mapping.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::node n = {{"foo", true}}; std::cout << std::boolalpha << n.is_mapping() << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_is_null.cpp b/docs/examples/ex_basic_node_is_null.cpp index 81666375..7fe459e8 100644 --- a/docs/examples/ex_basic_node_is_null.cpp +++ b/docs/examples/ex_basic_node_is_null.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::node n; std::cout << std::boolalpha << n.is_null() << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_is_scalar.cpp b/docs/examples/ex_basic_node_is_scalar.cpp index e192e1bb..1d99f041 100644 --- a/docs/examples/ex_basic_node_is_scalar.cpp +++ b/docs/examples/ex_basic_node_is_scalar.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node null_node; fkyaml::node boolean_node = true; diff --git a/docs/examples/ex_basic_node_is_sequence.cpp b/docs/examples/ex_basic_node_is_sequence.cpp index fc07454f..f997cf65 100644 --- a/docs/examples/ex_basic_node_is_sequence.cpp +++ b/docs/examples/ex_basic_node_is_sequence.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::node n = {1, 2, 3}; std::cout << std::boolalpha << n.is_sequence() << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_is_string.cpp b/docs/examples/ex_basic_node_is_string.cpp index 19d01396..ad963c84 100644 --- a/docs/examples/ex_basic_node_is_string.cpp +++ b/docs/examples/ex_basic_node_is_string.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::node n = "foo"; std::cout << std::boolalpha << n.is_string() << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_iterator.cpp b/docs/examples/ex_basic_node_iterator.cpp index 21223609..03ebe32f 100644 --- a/docs/examples/ex_basic_node_iterator.cpp +++ b/docs/examples/ex_basic_node_iterator.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node sequence_node = {1, 2, 3}; // get an iterator to the first sequence element. diff --git a/docs/examples/ex_basic_node_mapping.cpp b/docs/examples/ex_basic_node_mapping.cpp index 903d802e..a283b22d 100644 --- a/docs/examples/ex_basic_node_mapping.cpp +++ b/docs/examples/ex_basic_node_mapping.cpp @@ -1,12 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ - fkyaml::node::mapping_type m = { - {"foo", false}, - {"bar", 3.14} - }; +int main() { + fkyaml::node::mapping_type m = {{"foo", false}, {"bar", 3.14}}; fkyaml::node n = fkyaml::node::mapping(m); std::cout << n << std::endl; return 0; diff --git a/docs/examples/ex_basic_node_mapping_type.cpp b/docs/examples/ex_basic_node_mapping_type.cpp index 743f709c..2a2e3942 100644 --- a/docs/examples/ex_basic_node_mapping_type.cpp +++ b/docs/examples/ex_basic_node_mapping_type.cpp @@ -1,13 +1,19 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include #include -int main() -{ - std::cout << std::boolalpha - << std::is_same, fkyaml::node::mapping_type>::value - << std::endl; +int main() { + std::cout << std::boolalpha << std::is_same, fkyaml::node::mapping_type>::value + << std::endl; return 0; } diff --git a/docs/examples/ex_basic_node_node.cpp b/docs/examples/ex_basic_node_node.cpp index c900e043..37fecd5a 100644 --- a/docs/examples/ex_basic_node_node.cpp +++ b/docs/examples/ex_basic_node_node.cpp @@ -1,18 +1,17 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML node. - fkyaml::node n = - { - {"foo", 3.14}, - {"bar", true}, - {"baz", nullptr}, - {"qux", { - {"corge", {1, 2, 3}} - }} - }; + fkyaml::node n = {{"foo", 3.14}, {"bar", true}, {"baz", nullptr}, {"qux", {{"corge", {1, 2, 3}}}}}; // add a new value. n["qux"]["key"] = {"another", "value"}; diff --git a/docs/examples/ex_basic_node_node_t.cpp b/docs/examples/ex_basic_node_node_t.cpp index 2ac32173..df8a0928 100644 --- a/docs/examples/ex_basic_node_node_t.cpp +++ b/docs/examples/ex_basic_node_node_t.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node sequence_node = {1, 2, 3}; fkyaml::node mapping_node = {{"foo", true}, {"bar", false}}; diff --git a/docs/examples/ex_basic_node_operator_eq.cpp b/docs/examples/ex_basic_node_operator_eq.cpp index b5ca1e14..cfd69165 100644 --- a/docs/examples/ex_basic_node_operator_eq.cpp +++ b/docs/examples/ex_basic_node_operator_eq.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node seq_1 = {1, 2, 3}; fkyaml::node seq_2 = {1, 2, 4}; diff --git a/docs/examples/ex_basic_node_operator_ge.cpp b/docs/examples/ex_basic_node_operator_ge.cpp index f5cef30e..2d19a352 100644 --- a/docs/examples/ex_basic_node_operator_ge.cpp +++ b/docs/examples/ex_basic_node_operator_ge.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node seq_1 = {1, 2, 3}; fkyaml::node seq_2 = {1, 2, 4}; diff --git a/docs/examples/ex_basic_node_operator_gt.cpp b/docs/examples/ex_basic_node_operator_gt.cpp index cd1bf995..5cdc07b0 100644 --- a/docs/examples/ex_basic_node_operator_gt.cpp +++ b/docs/examples/ex_basic_node_operator_gt.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node seq_1 = {1, 2, 3}; fkyaml::node seq_2 = {1, 2, 4}; diff --git a/docs/examples/ex_basic_node_operator_le.cpp b/docs/examples/ex_basic_node_operator_le.cpp index 07a1e52f..29d8d5c7 100644 --- a/docs/examples/ex_basic_node_operator_le.cpp +++ b/docs/examples/ex_basic_node_operator_le.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node seq_1 = {1, 2, 3}; fkyaml::node seq_2 = {1, 2, 4}; diff --git a/docs/examples/ex_basic_node_operator_lt.cpp b/docs/examples/ex_basic_node_operator_lt.cpp index 9413b826..f71452b4 100644 --- a/docs/examples/ex_basic_node_operator_lt.cpp +++ b/docs/examples/ex_basic_node_operator_lt.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node seq_1 = {1, 2, 3}; fkyaml::node seq_2 = {1, 2, 4}; diff --git a/docs/examples/ex_basic_node_operator_ne.cpp b/docs/examples/ex_basic_node_operator_ne.cpp index 682b5def..39815eac 100644 --- a/docs/examples/ex_basic_node_operator_ne.cpp +++ b/docs/examples/ex_basic_node_operator_ne.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node seq_1 = {1, 2, 3}; fkyaml::node seq_2 = {1, 2, 4}; diff --git a/docs/examples/ex_basic_node_sequence.cpp b/docs/examples/ex_basic_node_sequence.cpp index b80ac6ed..a04f95d0 100644 --- a/docs/examples/ex_basic_node_sequence.cpp +++ b/docs/examples/ex_basic_node_sequence.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { fkyaml::node::sequence_type s = {fkyaml::node(true), fkyaml::node(false)}; fkyaml::node n = fkyaml::node::sequence(s); std::cout << n << std::endl; diff --git a/docs/examples/ex_basic_node_sequence_type.cpp b/docs/examples/ex_basic_node_sequence_type.cpp index 761fac78..3c544954 100644 --- a/docs/examples/ex_basic_node_sequence_type.cpp +++ b/docs/examples/ex_basic_node_sequence_type.cpp @@ -1,13 +1,19 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include #include -int main() -{ - std::cout << std::boolalpha - << std::is_same, fkyaml::node::sequence_type>::value - << std::endl; +int main() { + std::cout << std::boolalpha << std::is_same, fkyaml::node::sequence_type>::value + << std::endl; return 0; } diff --git a/docs/examples/ex_basic_node_serialize.cpp b/docs/examples/ex_basic_node_serialize.cpp index a37e527d..8edee6eb 100644 --- a/docs/examples/ex_basic_node_serialize.cpp +++ b/docs/examples/ex_basic_node_serialize.cpp @@ -1,17 +1,27 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a basic_node object. fkyaml::node n = { {"foo", true}, {"bar", {1, 2, 3}}, - {"baz", { - {"qux", 3.14}, - {"corge", nullptr} - }} - }; + {"baz", {{"qux", 3.14}, {"corge", nullptr}}}, + {{{true, 123}}, false}, + {{1.23, 4.56, 7.89}, 123456789}}; + // set tags to some nodes. + n["foo"].add_tag_name("!!bool"); + n["bar"][1].add_tag_name("!"); + // set an anchor name to a node. + n["baz"].add_anchor_name("anchor"); // serialize the basic_node object. std::cout << fkyaml::node::serialize(n) << std::endl; diff --git a/docs/examples/ex_basic_node_serialize.output b/docs/examples/ex_basic_node_serialize.output index b65b861e..2e41da1b 100644 --- a/docs/examples/ex_basic_node_serialize.output +++ b/docs/examples/ex_basic_node_serialize.output @@ -1,9 +1,15 @@ +? - 1.23 + - 4.56 + - 7.89 +: 123456789 +? true: 123 +: false bar: - 1 - - 2 + - ! 2 - 3 -baz: +baz: &anchor corge: null qux: 3.14 -foo: true +foo: !!bool true diff --git a/docs/examples/ex_basic_node_set_yaml_version.cpp b/docs/examples/ex_basic_node_set_yaml_version.cpp index 335b6f49..eb192144 100644 --- a/docs/examples/ex_basic_node_set_yaml_version.cpp +++ b/docs/examples/ex_basic_node_set_yaml_version.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::node n; n.set_yaml_version(fkyaml::node::yaml_version_t::VER_1_1); fkyaml::node n2; diff --git a/docs/examples/ex_basic_node_size.cpp b/docs/examples/ex_basic_node_size.cpp index 4e47b404..63f6b5ec 100644 --- a/docs/examples/ex_basic_node_size.cpp +++ b/docs/examples/ex_basic_node_size.cpp @@ -1,30 +1,26 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. - std::vector nodes = - { - {1, 2, 3}, - {{"foo", true}, {"bar", false}, {"baz", true}}, - fkyaml::node(), - true, - 256, - 3.14, - "foo" - }; + std::vector nodes = { + {1, 2, 3}, {{"foo", true}, {"bar", false}, {"baz", true}}, fkyaml::node(), true, 256, 3.14, "foo"}; - for (const auto& n : nodes) - { - try - { + for (const auto& n : nodes) { + try { // call size() std::cout << n.size() << std::endl; } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << "The node does not have a container nor string value." << std::endl; } } diff --git a/docs/examples/ex_basic_node_string_type.cpp b/docs/examples/ex_basic_node_string_type.cpp index 39f010cd..942ff49d 100644 --- a/docs/examples/ex_basic_node_string_type.cpp +++ b/docs/examples/ex_basic_node_string_type.cpp @@ -1,13 +1,18 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include #include -int main() -{ - std::cout << std::boolalpha - << std::is_same::value - << std::endl; +int main() { + std::cout << std::boolalpha << std::is_same::value << std::endl; return 0; } diff --git a/docs/examples/ex_basic_node_subscript_operator_basic_node.cpp b/docs/examples/ex_basic_node_subscript_operator_basic_node.cpp index d4450570..a2b054ff 100644 --- a/docs/examples/ex_basic_node_subscript_operator_basic_node.cpp +++ b/docs/examples/ex_basic_node_subscript_operator_basic_node.cpp @@ -1,19 +1,26 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML sequence node. fkyaml::node n1 = {123, 234, 345, 456}; // print YAML nodes at the following indexes. - fkyaml::node index_zero = 0; - fkyaml::node index_one = 1; - fkyaml::node index_two = 2; + fkyaml::node index_zero = 0; + fkyaml::node index_one = 1; + fkyaml::node index_two = 2; fkyaml::node index_three = 3; - std::cout << n1[index_zero] << std::endl; - std::cout << n1[index_one] << std::endl; - std::cout << n1[index_two] << std::endl; + std::cout << n1[index_zero] << std::endl; + std::cout << n1[index_one] << std::endl; + std::cout << n1[index_two] << std::endl; std::cout << n1[index_three] << std::endl; // this will cause an undefined behavior! diff --git a/docs/examples/ex_basic_node_subscript_operator_compatible_type.cpp b/docs/examples/ex_basic_node_subscript_operator_compatible_type.cpp index f2dd7c6a..a8deff69 100644 --- a/docs/examples/ex_basic_node_subscript_operator_compatible_type.cpp +++ b/docs/examples/ex_basic_node_subscript_operator_compatible_type.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create a YAML sequence node. fkyaml::node n1 = {123, 234, 345, 456}; diff --git a/docs/examples/ex_basic_node_swap_member.cpp b/docs/examples/ex_basic_node_swap_member.cpp index 10228e0f..873dee73 100644 --- a/docs/examples/ex_basic_node_swap_member.cpp +++ b/docs/examples/ex_basic_node_swap_member.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node n1 = 123; fkyaml::node n2 = "foo"; diff --git a/docs/examples/ex_basic_node_swap_std.cpp b/docs/examples/ex_basic_node_swap_std.cpp index b043d782..36df1464 100644 --- a/docs/examples/ex_basic_node_swap_std.cpp +++ b/docs/examples/ex_basic_node_swap_std.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node n1 = 123; fkyaml::node n2 = "foo"; diff --git a/docs/examples/ex_basic_node_type.cpp b/docs/examples/ex_basic_node_type.cpp index 2ac32173..df8a0928 100644 --- a/docs/examples/ex_basic_node_type.cpp +++ b/docs/examples/ex_basic_node_type.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // create YAML nodes. fkyaml::node sequence_node = {1, 2, 3}; fkyaml::node mapping_node = {{"foo", true}, {"bar", false}}; diff --git a/docs/examples/ex_basic_node_value_converter_type.cpp b/docs/examples/ex_basic_node_value_converter_type.cpp index 4c0104a2..9c679fd1 100644 --- a/docs/examples/ex_basic_node_value_converter_type.cpp +++ b/docs/examples/ex_basic_node_value_converter_type.cpp @@ -1,15 +1,22 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include -int main() -{ +int main() { std::cout << std::boolalpha - << std::is_same< - fkyaml::node_value_converter, - fkyaml::node::value_converter_type>::value - << std::endl; + << std::is_same< + fkyaml::node_value_converter, + fkyaml::node::value_converter_type>::value + << std::endl; fkyaml::node n = 3.14; double d = 0.0; diff --git a/docs/examples/ex_basic_node_yaml_version_t.cpp b/docs/examples/ex_basic_node_yaml_version_t.cpp index c2bba69f..003c045b 100644 --- a/docs/examples/ex_basic_node_yaml_version_t.cpp +++ b/docs/examples/ex_basic_node_yaml_version_t.cpp @@ -1,13 +1,19 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ - char input[] = - "%YAML 1.2\n" - "---\n" - "foo: bar\n"; +int main() { + char input[] = "%YAML 1.2\n" + "---\n" + "foo: bar\n"; // deserialize a YAML formatted string. fkyaml::node n = fkyaml::node::deserialize(input); diff --git a/docs/examples/ex_exception_constructor_msg.cpp b/docs/examples/ex_exception_constructor_msg.cpp index 4a6496ca..fabbeb81 100644 --- a/docs/examples/ex_exception_constructor_msg.cpp +++ b/docs/examples/ex_exception_constructor_msg.cpp @@ -1,14 +1,19 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ - try - { +int main() { + try { throw fkyaml::exception("An error message."); } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } return 0; diff --git a/docs/examples/ex_exception_constructor_noarg.cpp b/docs/examples/ex_exception_constructor_noarg.cpp index 791639f8..db4beb19 100644 --- a/docs/examples/ex_exception_constructor_noarg.cpp +++ b/docs/examples/ex_exception_constructor_noarg.cpp @@ -1,14 +1,19 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ - try - { +int main() { + try { throw fkyaml::exception(); } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } return 0; diff --git a/docs/examples/ex_exception_what.cpp b/docs/examples/ex_exception_what.cpp index e3337633..8a84d9a1 100644 --- a/docs/examples/ex_exception_what.cpp +++ b/docs/examples/ex_exception_what.cpp @@ -1,14 +1,19 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ - try - { +int main() { + try { throw fkyaml::exception("An error message."); } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } diff --git a/docs/examples/ex_macros_versions.cpp b/docs/examples/ex_macros_versions.cpp index 40defed7..8bf046a0 100644 --- a/docs/examples/ex_macros_versions.cpp +++ b/docs/examples/ex_macros_versions.cpp @@ -1,11 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ - std::cout << "fkYAML version " - << FK_YAML_MAJOR_VERSION << "." - << FK_YAML_MINOR_VERSION << "." - << FK_YAML_PATCH_VERSION << std::endl; +int main() { + std::cout << "fkYAML version " << FK_YAML_MAJOR_VERSION << "." << FK_YAML_MINOR_VERSION << "." + << FK_YAML_PATCH_VERSION << std::endl; return 0; } diff --git a/docs/examples/ex_macros_versions.output b/docs/examples/ex_macros_versions.output index f27bddd8..a087352f 100644 --- a/docs/examples/ex_macros_versions.output +++ b/docs/examples/ex_macros_versions.output @@ -1 +1 @@ -fkYAML version 0.3.4 +fkYAML version 0.3.5 diff --git a/docs/examples/ex_node_value_converter_from_node.cpp b/docs/examples/ex_node_value_converter_from_node.cpp index 2d7e9d64..93258662 100644 --- a/docs/examples/ex_node_value_converter_from_node.cpp +++ b/docs/examples/ex_node_value_converter_from_node.cpp @@ -1,37 +1,40 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -namespace ns -{ +namespace ns { -struct book -{ +struct book { std::string title; std::string author; int year; }; -void from_node(const fkyaml::node& n, book& b) -{ - b.title = n["title"].get_value_ref(); +void from_node(const fkyaml::node& n, book& b) { + b.title = n["title"].get_value_ref(); b.author = n["author"].get_value_ref(); - b.year = n["year"].get_value(); + b.year = n["year"].get_value(); } } // namespace ns -int main() -{ +int main() { fkyaml::node n = { - { "title", "Noman's Journey" }, - { "author", "John Doe" }, - { "year", 2023 }, + {"title", "Noman's Journey"}, + {"author", "John Doe"}, + {"year", 2023}, }; auto b = n.get_value(); - std::cout << "\"" << b.title << "\" was written by " << b.author - << " in " << b.year << "." << std::endl; + std::cout << "\"" << b.title << "\" was written by " << b.author << " in " << b.year << "." << std::endl; return 0; } diff --git a/docs/examples/ex_node_value_converter_to_node.cpp b/docs/examples/ex_node_value_converter_to_node.cpp index d5d8ca0c..f1d83c3d 100644 --- a/docs/examples/ex_node_value_converter_to_node.cpp +++ b/docs/examples/ex_node_value_converter_to_node.cpp @@ -1,30 +1,30 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -namespace ns -{ +namespace ns { -struct book -{ +struct book { std::string title; std::string author; int year; }; -void to_node(fkyaml::node& n, const book& b) -{ - n = { - { "title", b.title }, - { "author", b.author }, - { "year", b.year } - }; +void to_node(fkyaml::node& n, const book& b) { + n = {{"title", b.title}, {"author", b.author}, {"year", b.year}}; } } // namespace ns -int main() -{ - ns::book b = { "Noman's Journey", "John Doe", 2023 }; +int main() { + ns::book b = {"Noman's Journey", "John Doe", 2023}; fkyaml::node n = b; diff --git a/docs/examples/ex_operator_literal_yaml.cpp b/docs/examples/ex_operator_literal_yaml.cpp index c6ddbe18..d9b6010e 100644 --- a/docs/examples/ex_operator_literal_yaml.cpp +++ b/docs/examples/ex_operator_literal_yaml.cpp @@ -1,8 +1,15 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ +int main() { // bring the user-defined literals into this scope. using namespace fkyaml::literals::yaml_literals; diff --git a/docs/examples/ex_ordered_map_at.cpp b/docs/examples/ex_ordered_map_at.cpp index a808ad77..9ac83ee2 100644 --- a/docs/examples/ex_ordered_map_at.cpp +++ b/docs/examples/ex_ordered_map_at.cpp @@ -1,23 +1,25 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ - fkyaml::ordered_map om = { - { "foo", 123 }, - { "bar", "baz" } - }; +int main() { + fkyaml::ordered_map om = {{"foo", 123}, {"bar", "baz"}}; std::cout << om.at("foo") << std::endl; std::cout << om.at("bar") << std::endl; // accesses with a unknown key will throw an exception. - try - { + try { fkyaml::node& n = om.at("baz"); } - catch (const fkyaml::exception& e) - { + catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } diff --git a/docs/examples/ex_ordered_map_constructor_initializer_list.cpp b/docs/examples/ex_ordered_map_constructor_initializer_list.cpp index 255232f4..56501e90 100644 --- a/docs/examples/ex_ordered_map_constructor_initializer_list.cpp +++ b/docs/examples/ex_ordered_map_constructor_initializer_list.cpp @@ -1,15 +1,18 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include -int main() -{ - fkyaml::ordered_map om = { - { "foo", 123 }, - { "bar", "baz" } - }; +int main() { + fkyaml::ordered_map om = {{"foo", 123}, {"bar", "baz"}}; - for (auto& pair : om) - { + for (auto& pair : om) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; diff --git a/docs/examples/ex_ordered_map_constructor_noarg.cpp b/docs/examples/ex_ordered_map_constructor_noarg.cpp index bd93373d..ddad0873 100644 --- a/docs/examples/ex_ordered_map_constructor_noarg.cpp +++ b/docs/examples/ex_ordered_map_constructor_noarg.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { fkyaml::ordered_map om; std::cout << std::boolalpha << om.empty() << std::endl; return 0; diff --git a/docs/examples/ex_ordered_map_emplace.cpp b/docs/examples/ex_ordered_map_emplace.cpp index 70a52a44..345c02df 100644 --- a/docs/examples/ex_ordered_map_emplace.cpp +++ b/docs/examples/ex_ordered_map_emplace.cpp @@ -1,26 +1,28 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ - fkyaml::ordered_map om = { - { "foo", 123 }, - { "bar", "baz" } - }; +int main() { + fkyaml::ordered_map om = {{"foo", 123}, {"bar", "baz"}}; // insert a value with a new key. auto ret = om.emplace("qux", 3.14); - if (ret.second) - { + if (ret.second) { std::cout << "emplacement took place." << std::endl; } std::cout << ret.first->second << std::endl; // insert a value with an existing key. auto ret2 = om.emplace("foo", true); - if (!ret2.second) - { + if (!ret2.second) { std::cout << "emplacement did not take place." << std::endl; } std::cout << ret2.first->second << std::endl; diff --git a/docs/examples/ex_ordered_map_find.cpp b/docs/examples/ex_ordered_map_find.cpp index 914fc6db..91ac317e 100644 --- a/docs/examples/ex_ordered_map_find.cpp +++ b/docs/examples/ex_ordered_map_find.cpp @@ -1,25 +1,27 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ - fkyaml::ordered_map om = { - { "foo", 123 }, - { "bar", "baz" } - }; +int main() { + fkyaml::ordered_map om = {{"foo", 123}, {"bar", "baz"}}; // search for a value with an existing key. auto itr = om.find("foo"); - if (itr != om.end()) - { + if (itr != om.end()) { std::cout << itr->second << std::endl; } // search for a value with a key which does not exist. auto itr2 = om.find("qux"); - if (itr2 == om.end()) - { + if (itr2 == om.end()) { std::cout << "key does not exist." << std::endl; } diff --git a/docs/examples/ex_ordered_map_subscript_operator.cpp b/docs/examples/ex_ordered_map_subscript_operator.cpp index fe6e7ffa..8603b4f9 100644 --- a/docs/examples/ex_ordered_map_subscript_operator.cpp +++ b/docs/examples/ex_ordered_map_subscript_operator.cpp @@ -1,13 +1,17 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ - fkyaml::ordered_map om = { - { "foo", 123 }, - { "bar", "baz" } - }; +int main() { + fkyaml::ordered_map om = {{"foo", 123}, {"bar", "baz"}}; std::cout << om["foo"] << std::endl; std::cout << om["bar"] << std::endl; diff --git a/docs/examples/tutorial_1.cpp b/docs/examples/tutorial_1.cpp index 94e96231..e98c0742 100644 --- a/docs/examples/tutorial_1.cpp +++ b/docs/examples/tutorial_1.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // open a YAML file. Other streams or strings are also usable as an input. std::ifstream ifs("example.yaml"); diff --git a/docs/examples/tutorial_2.cpp b/docs/examples/tutorial_2.cpp index 2461ac87..525f37b1 100644 --- a/docs/examples/tutorial_2.cpp +++ b/docs/examples/tutorial_2.cpp @@ -1,9 +1,16 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include -int main() -{ +int main() { // open a YAML file. Other streams or strings are also usable as an input. std::ifstream ifs("example.yaml"); @@ -11,8 +18,7 @@ int main() fkyaml::node root = fkyaml::node::deserialize(ifs); // print only values associated with "title" key. - for (auto& novel_node : root["novels"]) - { + for (auto& novel_node : root["novels"]) { // get reference to the "title" value with `get_value_ref` function. std::cout << novel_node["title"].get_value_ref() << std::endl; } diff --git a/docs/examples/tutorial_3.cpp b/docs/examples/tutorial_3.cpp index 6ab7ed5f..c40f0f20 100644 --- a/docs/examples/tutorial_3.cpp +++ b/docs/examples/tutorial_3.cpp @@ -1,10 +1,17 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include -int main() -{ +int main() { // open a YAML file. Other streams or strings are also usable as an input. std::ifstream ifs("example.yaml"); @@ -12,17 +19,13 @@ int main() fkyaml::node root = fkyaml::node::deserialize(ifs); // create an empty YAML sequence node. - fkyaml::node response = {{ "recommends", fkyaml::node::sequence() }}; + fkyaml::node response = {{"recommends", fkyaml::node::sequence()}}; auto& recommends = response["recommends"].get_value_ref(); // generate recommendations by extracting "title" & "author" values. - for (auto& novel_node : root["novels"]) - { + for (auto& novel_node : root["novels"]) { // create a recommendation node with an initializer list. - fkyaml::node recommend = { - { "title", novel_node["title"] }, - { "author", novel_node["author"] } - }; + fkyaml::node recommend = {{"title", novel_node["title"]}, {"author", novel_node["author"]}}; recommends.emplace_back(std::move(recommend)); } diff --git a/docs/examples/tutorial_4.cpp b/docs/examples/tutorial_4.cpp index 7b29cc05..1d3923d2 100644 --- a/docs/examples/tutorial_4.cpp +++ b/docs/examples/tutorial_4.cpp @@ -1,45 +1,44 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + #include #include #include #include // creating a namespace is not mandatory. -namespace ns -{ +namespace ns { -struct novel -{ +struct novel { std::string title; std::string author; int year; }; -struct recommend -{ +struct recommend { std::string title; std::string author; }; // overloads must be defined in the same namespace as user-defined types. -void from_node(const fkyaml::node& node, novel& novel) -{ +void from_node(const fkyaml::node& node, novel& novel) { novel.title = node["title"].get_value_ref(); novel.author = node["author"].get_value_ref(); novel.year = node["year"].get_value(); } -void to_node(fkyaml::node& node, const recommend& recommend) -{ - node = fkyaml::node { - { "title", recommend.title }, - { "author", recommend.author } - }; +void to_node(fkyaml::node& node, const recommend& recommend) { + node = fkyaml::node {{"title", recommend.title}, {"author", recommend.author}}; } } // namespace ns -int main() -{ +int main() { // open a YAML file. Other streams or strings are also usable as an input. std::ifstream ifs("example.yaml"); @@ -47,17 +46,16 @@ int main() fkyaml::node root = fkyaml::node::deserialize(ifs); // create an empty YAML sequence node. - fkyaml::node response = {{ "recommends", fkyaml::node::sequence() }}; + fkyaml::node response = {{"recommends", fkyaml::node::sequence()}}; auto& recommends = response["recommends"].get_value_ref(); // get novels directly from the node. auto novels = root["novels"].get_value>(); // generate recommendations by extracting "title" & "author" values. - for (auto& novel : novels) - { + for (auto& novel : novels) { // create a recommendation node directly with a recommend object. - ns::recommend recommend = { std::move(novel.title), std::move(novel.author) }; + ns::recommend recommend = {std::move(novel.title), std::move(novel.author)}; recommends.emplace_back(recommend); } diff --git a/docs/mkdocs/Makefile b/docs/mkdocs/Makefile index eee70251..22a9776f 100644 --- a/docs/mkdocs/Makefile +++ b/docs/mkdocs/Makefile @@ -1,5 +1,5 @@ # build the site -build: install-venv prepare_files +build: install-venv venv/bin/mkdocs build # serve the site locally @@ -14,11 +14,3 @@ install-venv: requirements.txt uninstall-venv: rm -rf venv - -clean: - rm -rf docs/examples - -# prepare example snippets and outputs -prepare_files: clean - mkdir docs/examples - cp -r ../examples/*.cpp ../examples/*.output ../examples/*.yaml docs/examples diff --git a/docs/mkdocs/docs/api/basic_node/add_anchor_name.md b/docs/mkdocs/docs/api/basic_node/add_anchor_name.md index 3f45ec85..6154a3a7 100644 --- a/docs/mkdocs/docs/api/basic_node/add_anchor_name.md +++ b/docs/mkdocs/docs/api/basic_node/add_anchor_name.md @@ -18,7 +18,7 @@ If the basic_node has already had any anchor name, the new anchor name overwrite ???+ Example ```cpp - --8<-- "examples/ex_basic_node_add_anchor_name.cpp" + --8<-- "examples/ex_basic_node_add_anchor_name.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/add_tag_name.md b/docs/mkdocs/docs/api/basic_node/add_tag_name.md index 87ad182a..55965862 100644 --- a/docs/mkdocs/docs/api/basic_node/add_tag_name.md +++ b/docs/mkdocs/docs/api/basic_node/add_tag_name.md @@ -18,7 +18,7 @@ If the basic_node has already had any tag name, the new tag name overwrites the ???+ Example ```cpp - --8<-- "examples/ex_basic_node_add_tag_name.cpp" + --8<-- "examples/ex_basic_node_add_tag_name.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/alias_of.md b/docs/mkdocs/docs/api/basic_node/alias_of.md index 2ec58d76..2c091381 100644 --- a/docs/mkdocs/docs/api/basic_node/alias_of.md +++ b/docs/mkdocs/docs/api/basic_node/alias_of.md @@ -26,7 +26,7 @@ If the given anchor node does not have any non-empty anchor name, an [`fkyaml::e ???+ Example ```cpp - --8<-- "examples/ex_basic_node_alias_of.cpp" + --8<-- "examples/ex_basic_node_alias_of.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/at.md b/docs/mkdocs/docs/api/basic_node/at.md index 5e05aadf..041fa9fc 100644 --- a/docs/mkdocs/docs/api/basic_node/at.md +++ b/docs/mkdocs/docs/api/basic_node/at.md @@ -76,7 +76,7 @@ Reference, or constant reference, to the YAML node object associated with the gi ???+ Example ```cpp - --8<-- "examples/ex_basic_node_at_compatible_type.cpp" + --8<-- "examples/ex_basic_node_at_compatible_type.cpp:9" ``` output: @@ -117,7 +117,7 @@ Reference, or constant reference, to the YAML node object associated with the gi ???+ Example ```cpp - --8<-- "examples/ex_basic_node_at_basic_node.cpp" + --8<-- "examples/ex_basic_node_at_basic_node.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/begin.md b/docs/mkdocs/docs/api/basic_node/begin.md index 2ae0c05a..dbeaf971 100644 --- a/docs/mkdocs/docs/api/basic_node/begin.md +++ b/docs/mkdocs/docs/api/basic_node/begin.md @@ -19,7 +19,7 @@ An iterator to the first element of a container node value (either sequence or m ???+ Example ```cpp - --8<-- "examples/ex_basic_node_begin.cpp" + --8<-- "examples/ex_basic_node_begin.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/boolean_type.md b/docs/mkdocs/docs/api/basic_node/boolean_type.md index 368e8c01..d22eead8 100644 --- a/docs/mkdocs/docs/api/basic_node/boolean_type.md +++ b/docs/mkdocs/docs/api/basic_node/boolean_type.md @@ -19,7 +19,7 @@ With the decided type, boolean objects are stored directly inside a [`basic_node ???+ Example ```cpp - --8<-- "examples/ex_basic_node_boolean_type.cpp" + --8<-- "examples/ex_basic_node_boolean_type.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/const_iterator.md b/docs/mkdocs/docs/api/basic_node/const_iterator.md index 4566b004..33c5ab86 100644 --- a/docs/mkdocs/docs/api/basic_node/const_iterator.md +++ b/docs/mkdocs/docs/api/basic_node/const_iterator.md @@ -12,7 +12,7 @@ This iterator type is commonly used for sequence and mapping container values. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_const_iterator.cpp" + --8<-- "examples/ex_basic_node_const_iterator.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/constructor.md b/docs/mkdocs/docs/api/basic_node/constructor.md index 22a73a4b..10a094c3 100644 --- a/docs/mkdocs/docs/api/basic_node/constructor.md +++ b/docs/mkdocs/docs/api/basic_node/constructor.md @@ -43,7 +43,7 @@ The resulting basic_node has the [`node_t::NULL_OBJECT`](node_t.md) type. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_constructor_1.cpp" + --8<-- "examples/ex_basic_node_constructor_1.cpp:9" ``` output: @@ -67,7 +67,7 @@ The resulting basic_node has a default value for the given type. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_constructor_2.cpp" + --8<-- "examples/ex_basic_node_constructor_2.cpp:9" ``` output: @@ -91,7 +91,7 @@ The resulting basic_node has the same type and value as `rhs`. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_constructor_3.cpp" + --8<-- "examples/ex_basic_node_constructor_3.cpp:9" ``` output: @@ -116,7 +116,7 @@ The value of the argument `rhs` after calling this move constructor, will be the ???+ Example ```cpp - --8<-- "examples/ex_basic_node_constructor_4.cpp" + --8<-- "examples/ex_basic_node_constructor_4.cpp:9" ``` output: @@ -156,7 +156,7 @@ The resulting basic_node has the value of `val` and the type which is associated ???+ Example ```cpp - --8<-- "examples/ex_basic_node_constructor_5.cpp" + --8<-- "examples/ex_basic_node_constructor_5.cpp:9" ``` output: @@ -194,7 +194,7 @@ The resulting basic_node has the value of the referenced basic_node by `node_ref ???+ Example ```cpp - --8<-- "examples/ex_basic_node_constructor_6.cpp" + --8<-- "examples/ex_basic_node_constructor_6.cpp:9" ``` output: @@ -220,7 +220,7 @@ If `init` contains a sequence of basic_node objects in which the number of basic ???+ Example ```cpp - --8<-- "examples/ex_basic_node_constructor_7.cpp" + --8<-- "examples/ex_basic_node_constructor_7.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/contains.md b/docs/mkdocs/docs/api/basic_node/contains.md index d6074b7b..662bd0dc 100644 --- a/docs/mkdocs/docs/api/basic_node/contains.md +++ b/docs/mkdocs/docs/api/basic_node/contains.md @@ -37,7 +37,7 @@ The `KeyType` can be a compatible type with [`fkyaml::basic_node`](index.md) or ???+ Example ```cpp - --8<-- "examples/ex_basic_node_contains.cpp" + --8<-- "examples/ex_basic_node_contains.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/deserialize.md b/docs/mkdocs/docs/api/basic_node/deserialize.md index cd419823..6a48325c 100644 --- a/docs/mkdocs/docs/api/basic_node/deserialize.md +++ b/docs/mkdocs/docs/api/basic_node/deserialize.md @@ -82,7 +82,7 @@ The resulting `basic_node` object deserialized from the pair of iterators. ???+ Example "Example (a character array)" ```cpp - --8<-- "examples/ex_basic_node_deserialize_char_array.cpp" + --8<-- "examples/ex_basic_node_deserialize_char_array.cpp:9" ``` output: @@ -93,7 +93,7 @@ The resulting `basic_node` object deserialized from the pair of iterators. ???+ Example "Example (a std::string object)" ```cpp - --8<-- "examples/ex_basic_node_deserialize_string.cpp" + --8<-- "examples/ex_basic_node_deserialize_string.cpp:9" ``` output: @@ -108,7 +108,7 @@ The resulting `basic_node` object deserialized from the pair of iterators. ``` ```cpp - --8<-- "examples/ex_basic_node_deserialize_file_pointer.cpp" + --8<-- "examples/ex_basic_node_deserialize_file_pointer.cpp:9" ``` output: @@ -119,7 +119,7 @@ The resulting `basic_node` object deserialized from the pair of iterators. ???+ Example "Example (a pair of iterators)" ```cpp - --8<-- "examples/ex_basic_node_deserialize_iterators.cpp" + --8<-- "examples/ex_basic_node_deserialize_iterators.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/empty.md b/docs/mkdocs/docs/api/basic_node/empty.md index 52f120c9..669eb1cf 100644 --- a/docs/mkdocs/docs/api/basic_node/empty.md +++ b/docs/mkdocs/docs/api/basic_node/empty.md @@ -16,7 +16,7 @@ Throws a [`fkyaml::exception`](../exception/index.md) if a basic_node does not h ???+ Example ```cpp - --8<-- "examples/ex_basic_node_empty.cpp" + --8<-- "examples/ex_basic_node_empty.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/end.md b/docs/mkdocs/docs/api/basic_node/end.md index 92375a22..00bedf7e 100644 --- a/docs/mkdocs/docs/api/basic_node/end.md +++ b/docs/mkdocs/docs/api/basic_node/end.md @@ -19,7 +19,7 @@ An iterator to the past-the-last element of a container node value (either seque ???+ Example ```cpp - --8<-- "examples/ex_basic_node_end.cpp" + --8<-- "examples/ex_basic_node_end.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/extraction_operator.md b/docs/mkdocs/docs/api/basic_node/extraction_operator.md index 7ef54163..a4282c4c 100644 --- a/docs/mkdocs/docs/api/basic_node/extraction_operator.md +++ b/docs/mkdocs/docs/api/basic_node/extraction_operator.md @@ -8,7 +8,7 @@ inline std::istream& operator>>(std::istream& is, basic_node& n); Insertion operator for basic_node template class. Deserializes an input stream into a [`basic_node`](index.md). -This API is a wrapper of [`basic_node::deserialize()`](deserialize.md) function for input streams to simplify the implementation in the user's code. +This API is a wrapper of [`basic_node::deserialize()`](deserialize.md) function for input streams to simplify the implementation in the client code. Note that the contents of the input stream must be encoded in either the UTF-8, UTF-16BE/LE or UTF-32LE/BE format and begin with either a byte order mark or an ASCII character. ## **Parameters** @@ -30,7 +30,7 @@ Reference to the input stream object `is`. ``` ```cpp - --8<-- "examples/ex_basic_node_extraction_operator.cpp" + --8<-- "examples/ex_basic_node_extraction_operator.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/float_number_type.md b/docs/mkdocs/docs/api/basic_node/float_number_type.md index 43416dc3..f1654e83 100644 --- a/docs/mkdocs/docs/api/basic_node/float_number_type.md +++ b/docs/mkdocs/docs/api/basic_node/float_number_type.md @@ -19,7 +19,7 @@ With the decided type, floating point number objects are stored directly inside ???+ Example ```cpp - --8<-- "examples/ex_basic_node_float_number_type.cpp" + --8<-- "examples/ex_basic_node_float_number_type.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/get_anchor_name.md b/docs/mkdocs/docs/api/basic_node/get_anchor_name.md index f8f5e8d8..58cbc669 100644 --- a/docs/mkdocs/docs/api/basic_node/get_anchor_name.md +++ b/docs/mkdocs/docs/api/basic_node/get_anchor_name.md @@ -18,7 +18,7 @@ If no anchor name has been set, an [`fkyaml::exception`](../exception/index.md) ???+ Example ```cpp - --8<-- "examples/ex_basic_node_get_anchor_name.cpp" + --8<-- "examples/ex_basic_node_get_anchor_name.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/get_tag_name.md b/docs/mkdocs/docs/api/basic_node/get_tag_name.md index 97acc959..101f9704 100644 --- a/docs/mkdocs/docs/api/basic_node/get_tag_name.md +++ b/docs/mkdocs/docs/api/basic_node/get_tag_name.md @@ -18,7 +18,7 @@ If no tag name has been set, an [`fkyaml::exception`](../exception/index.md) wil ???+ Example ```cpp - --8<-- "examples/ex_basic_node_get_tag_name.cpp" + --8<-- "examples/ex_basic_node_get_tag_name.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/get_value.md b/docs/mkdocs/docs/api/basic_node/get_value.md index 8a1f592d..d968eed4 100644 --- a/docs/mkdocs/docs/api/basic_node/get_value.md +++ b/docs/mkdocs/docs/api/basic_node/get_value.md @@ -35,7 +35,7 @@ A compatible native data value converted from the basic_node object. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_get_value.cpp" + --8<-- "examples/ex_basic_node_get_value.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/get_value_ref.md b/docs/mkdocs/docs/api/basic_node/get_value_ref.md index f89f0fe3..67701fb4 100644 --- a/docs/mkdocs/docs/api/basic_node/get_value_ref.md +++ b/docs/mkdocs/docs/api/basic_node/get_value_ref.md @@ -38,7 +38,7 @@ A [`fkyaml::exception`](../exception/index.md) would be thrown otherwise. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_get_value_ref.cpp" + --8<-- "examples/ex_basic_node_get_value_ref.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/get_yaml_version.md b/docs/mkdocs/docs/api/basic_node/get_yaml_version.md index 63ac2709..1dea35a4 100644 --- a/docs/mkdocs/docs/api/basic_node/get_yaml_version.md +++ b/docs/mkdocs/docs/api/basic_node/get_yaml_version.md @@ -20,7 +20,7 @@ The version of the YAML format applied to the basic_node object. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_get_yaml_version.cpp" + --8<-- "examples/ex_basic_node_get_yaml_version.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/has_anchor_name.md b/docs/mkdocs/docs/api/basic_node/has_anchor_name.md index 815392cf..d8b4c78c 100644 --- a/docs/mkdocs/docs/api/basic_node/has_anchor_name.md +++ b/docs/mkdocs/docs/api/basic_node/has_anchor_name.md @@ -15,7 +15,7 @@ Check if the YAML node has an anchor name. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_has_anchor_name.cpp" + --8<-- "examples/ex_basic_node_has_anchor_name.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/has_tag_name.md b/docs/mkdocs/docs/api/basic_node/has_tag_name.md index 02327154..a353f36a 100644 --- a/docs/mkdocs/docs/api/basic_node/has_tag_name.md +++ b/docs/mkdocs/docs/api/basic_node/has_tag_name.md @@ -15,7 +15,7 @@ Check if the YAML node has a tag name. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_has_tag_name.cpp" + --8<-- "examples/ex_basic_node_has_tag_name.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/insertion_operator.md b/docs/mkdocs/docs/api/basic_node/insertion_operator.md index b6c62d1a..33c17103 100644 --- a/docs/mkdocs/docs/api/basic_node/insertion_operator.md +++ b/docs/mkdocs/docs/api/basic_node/insertion_operator.md @@ -8,7 +8,7 @@ inline std::ostream& operator<<(std::ostream& os, const basic_node& n); Extraction operator for basic_node template class. Serializes YAML node values into an output stream. -This API is a wrapper of [`basic_node::serialize()`](serialize.md) function to simplify the implementation in the user's code. +This API is a wrapper of [`basic_node::serialize()`](serialize.md) function to simplify the implementation in the client code. For more detailed descriptions, please visit the reference page for the [`basic_node::serialize()`](serialize.md) function. ## **Template Parameters** @@ -49,7 +49,7 @@ Reference to the output stream object `os`. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_insertion_operator.cpp" + --8<-- "examples/ex_basic_node_insertion_operator.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/integer_type.md b/docs/mkdocs/docs/api/basic_node/integer_type.md index 3ee2da73..4e70c810 100644 --- a/docs/mkdocs/docs/api/basic_node/integer_type.md +++ b/docs/mkdocs/docs/api/basic_node/integer_type.md @@ -15,7 +15,7 @@ With the decided type, integer objects are stored directly inside a [`basic_node ???+ Example ```cpp - --8<-- "examples/ex_basic_node_integer_type.cpp" + --8<-- "examples/ex_basic_node_integer_type.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_alias.md b/docs/mkdocs/docs/api/basic_node/is_alias.md index 845b9190..0c9b215c 100644 --- a/docs/mkdocs/docs/api/basic_node/is_alias.md +++ b/docs/mkdocs/docs/api/basic_node/is_alias.md @@ -15,7 +15,7 @@ Tests whether the node is an alias node. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_alias.cpp" + --8<-- "examples/ex_basic_node_is_alias.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_anchor.md b/docs/mkdocs/docs/api/basic_node/is_anchor.md index bbc9d7df..2921ecd0 100644 --- a/docs/mkdocs/docs/api/basic_node/is_anchor.md +++ b/docs/mkdocs/docs/api/basic_node/is_anchor.md @@ -15,7 +15,7 @@ Tests whether the node is an anchor node. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_anchor.cpp" + --8<-- "examples/ex_basic_node_is_anchor.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_boolean.md b/docs/mkdocs/docs/api/basic_node/is_boolean.md index 28e5bd2e..492e74a2 100644 --- a/docs/mkdocs/docs/api/basic_node/is_boolean.md +++ b/docs/mkdocs/docs/api/basic_node/is_boolean.md @@ -15,7 +15,7 @@ Tests whether the node value type is [`node_t::BOOLEAN`](node_t.md). ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_boolean.cpp" + --8<-- "examples/ex_basic_node_is_boolean.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_float_number.md b/docs/mkdocs/docs/api/basic_node/is_float_number.md index 40e42759..cccb12d3 100644 --- a/docs/mkdocs/docs/api/basic_node/is_float_number.md +++ b/docs/mkdocs/docs/api/basic_node/is_float_number.md @@ -15,7 +15,7 @@ Tests whether the node value type is [`node_t::FLOAT_NUMBER`](node_t.md). ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_float_number.cpp" + --8<-- "examples/ex_basic_node_is_float_number.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_integer.md b/docs/mkdocs/docs/api/basic_node/is_integer.md index f28881a0..c18a8bca 100644 --- a/docs/mkdocs/docs/api/basic_node/is_integer.md +++ b/docs/mkdocs/docs/api/basic_node/is_integer.md @@ -15,7 +15,7 @@ Tests whether the node value type is [`node_t::INTEGER`](node_t.md). ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_integer.cpp" + --8<-- "examples/ex_basic_node_is_integer.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_mapping.md b/docs/mkdocs/docs/api/basic_node/is_mapping.md index 90036848..ddbd0b54 100644 --- a/docs/mkdocs/docs/api/basic_node/is_mapping.md +++ b/docs/mkdocs/docs/api/basic_node/is_mapping.md @@ -15,7 +15,7 @@ Tests whether the node value type is [`node_t::MAPPING`](node_t.md). ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_mapping.cpp" + --8<-- "examples/ex_basic_node_is_mapping.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_null.md b/docs/mkdocs/docs/api/basic_node/is_null.md index dbfd3f89..215c1335 100644 --- a/docs/mkdocs/docs/api/basic_node/is_null.md +++ b/docs/mkdocs/docs/api/basic_node/is_null.md @@ -15,7 +15,7 @@ Tests whether the node value type is [`node_t::NULL_OBJECT`](node_t.md). ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_null.cpp" + --8<-- "examples/ex_basic_node_is_null.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_scalar.md b/docs/mkdocs/docs/api/basic_node/is_scalar.md index bc7cd90e..e78f2027 100644 --- a/docs/mkdocs/docs/api/basic_node/is_scalar.md +++ b/docs/mkdocs/docs/api/basic_node/is_scalar.md @@ -20,7 +20,7 @@ Tests whether the node value type is one of the followings: ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_scalar.cpp" + --8<-- "examples/ex_basic_node_is_scalar.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_sequence.md b/docs/mkdocs/docs/api/basic_node/is_sequence.md index 5d2239e0..108c900d 100644 --- a/docs/mkdocs/docs/api/basic_node/is_sequence.md +++ b/docs/mkdocs/docs/api/basic_node/is_sequence.md @@ -15,7 +15,7 @@ Tests whether the node value type is [`node_t::SEQUENCE`](node_t.md). ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_sequence.cpp" + --8<-- "examples/ex_basic_node_is_sequence.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/is_string.md b/docs/mkdocs/docs/api/basic_node/is_string.md index 8901c0a1..f92c9fd4 100644 --- a/docs/mkdocs/docs/api/basic_node/is_string.md +++ b/docs/mkdocs/docs/api/basic_node/is_string.md @@ -15,7 +15,7 @@ Tests whether the node value type is [`node_t::STRING`](node_t.md). ???+ Example ```cpp - --8<-- "examples/ex_basic_node_is_string.cpp" + --8<-- "examples/ex_basic_node_is_string.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/iterator.md b/docs/mkdocs/docs/api/basic_node/iterator.md index 05fbd3b9..e7e90d09 100644 --- a/docs/mkdocs/docs/api/basic_node/iterator.md +++ b/docs/mkdocs/docs/api/basic_node/iterator.md @@ -12,7 +12,7 @@ This iterator type is commonly used for sequence and mapping container values. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_iterator.cpp" + --8<-- "examples/ex_basic_node_iterator.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/mapping.md b/docs/mkdocs/docs/api/basic_node/mapping.md index 1b608b62..43040cb7 100644 --- a/docs/mkdocs/docs/api/basic_node/mapping.md +++ b/docs/mkdocs/docs/api/basic_node/mapping.md @@ -15,7 +15,7 @@ The resulting basic_node has the [`node_t::MAPPING`](node_t.md) type. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_mapping.cpp" + --8<-- "examples/ex_basic_node_mapping.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/mapping_type.md b/docs/mkdocs/docs/api/basic_node/mapping_type.md index 2257a4df..b8141b67 100644 --- a/docs/mkdocs/docs/api/basic_node/mapping_type.md +++ b/docs/mkdocs/docs/api/basic_node/mapping_type.md @@ -36,7 +36,7 @@ Note that mapping objects are stored as pointers in a [`basic_node`](index.md) s ???+ Example ```cpp - --8<-- "examples/ex_basic_node_mapping_type.cpp" + --8<-- "examples/ex_basic_node_mapping_type.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/node.md b/docs/mkdocs/docs/api/basic_node/node.md index 4e464e9b..b27ccb73 100644 --- a/docs/mkdocs/docs/api/basic_node/node.md +++ b/docs/mkdocs/docs/api/basic_node/node.md @@ -11,7 +11,7 @@ This type is the default specialization of the [basic_node](index.md) class whic ???+ Example ```cpp - --8<-- "examples/ex_basic_node_node.cpp" + --8<-- "examples/ex_basic_node_node.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/node_t.md b/docs/mkdocs/docs/api/basic_node/node_t.md index e10c8bc0..2ec58f85 100644 --- a/docs/mkdocs/docs/api/basic_node/node_t.md +++ b/docs/mkdocs/docs/api/basic_node/node_t.md @@ -29,7 +29,7 @@ This enumeration collects the different YAML value types. They are internally us ???+ Example ```cpp - --8<-- "examples/ex_basic_node_node_t.cpp" + --8<-- "examples/ex_basic_node_node_t.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/operator=.md b/docs/mkdocs/docs/api/basic_node/operator=.md index f6847073..9d9dde97 100644 --- a/docs/mkdocs/docs/api/basic_node/operator=.md +++ b/docs/mkdocs/docs/api/basic_node/operator=.md @@ -32,7 +32,7 @@ Reference to this basic_node object. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_copy_assignment_operator.cpp" + --8<-- "examples/ex_basic_node_copy_assignment_operator.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/operator[].md b/docs/mkdocs/docs/api/basic_node/operator[].md index ee911d69..548f5d16 100644 --- a/docs/mkdocs/docs/api/basic_node/operator[].md +++ b/docs/mkdocs/docs/api/basic_node/operator[].md @@ -79,7 +79,7 @@ Reference, or constant reference, to the YAML node object associated with the gi ???+ Example ```cpp - --8<-- "examples/ex_basic_node_subscript_operator_compatible_type.cpp" + --8<-- "examples/ex_basic_node_subscript_operator_compatible_type.cpp:9" ``` output: @@ -120,7 +120,7 @@ Reference, or constant reference, to the YAML node object associated with the gi ???+ Example ```cpp - --8<-- "examples/ex_basic_node_subscript_operator_basic_node.cpp" + --8<-- "examples/ex_basic_node_subscript_operator_basic_node.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/operator_eq.md b/docs/mkdocs/docs/api/basic_node/operator_eq.md index 0d8df51d..fb51fdb6 100644 --- a/docs/mkdocs/docs/api/basic_node/operator_eq.md +++ b/docs/mkdocs/docs/api/basic_node/operator_eq.md @@ -24,7 +24,7 @@ Compares two `basic_node` objects for equality according to the following rules: ???+ Example ```cpp - --8<-- "examples/ex_basic_node_operator_eq.cpp" + --8<-- "examples/ex_basic_node_operator_eq.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/operator_ge.md b/docs/mkdocs/docs/api/basic_node/operator_ge.md index 7ee4bfa1..d1a873ab 100644 --- a/docs/mkdocs/docs/api/basic_node/operator_ge.md +++ b/docs/mkdocs/docs/api/basic_node/operator_ge.md @@ -22,7 +22,7 @@ The operator returns the result of `!(*this < rhs)`. (see [`operator<`](operator ???+ Example ```cpp - --8<-- "examples/ex_basic_node_operator_ge.cpp" + --8<-- "examples/ex_basic_node_operator_ge.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/operator_gt.md b/docs/mkdocs/docs/api/basic_node/operator_gt.md index 9967447d..d3630ae2 100644 --- a/docs/mkdocs/docs/api/basic_node/operator_gt.md +++ b/docs/mkdocs/docs/api/basic_node/operator_gt.md @@ -22,7 +22,7 @@ The operator returns the result of `!(*this <= rhs)`. (see [`operator<=`](operat ???+ Example ```cpp - --8<-- "examples/ex_basic_node_operator_gt.cpp" + --8<-- "examples/ex_basic_node_operator_gt.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/operator_le.md b/docs/mkdocs/docs/api/basic_node/operator_le.md index ff96167a..566701ae 100644 --- a/docs/mkdocs/docs/api/basic_node/operator_le.md +++ b/docs/mkdocs/docs/api/basic_node/operator_le.md @@ -22,7 +22,7 @@ The operator returns the result of `!(rhs < *this)`. (see [`operator<`](operator ???+ Example ```cpp - --8<-- "examples/ex_basic_node_operator_le.cpp" + --8<-- "examples/ex_basic_node_operator_le.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/operator_lt.md b/docs/mkdocs/docs/api/basic_node/operator_lt.md index c7517f60..3678d9d1 100644 --- a/docs/mkdocs/docs/api/basic_node/operator_lt.md +++ b/docs/mkdocs/docs/api/basic_node/operator_lt.md @@ -33,7 +33,7 @@ Check if `this` object is less than `rhs` according to the following rules: ???+ Example ```cpp - --8<-- "examples/ex_basic_node_operator_lt.cpp" + --8<-- "examples/ex_basic_node_operator_lt.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/operator_ne.md b/docs/mkdocs/docs/api/basic_node/operator_ne.md index c8c04447..5291094b 100644 --- a/docs/mkdocs/docs/api/basic_node/operator_ne.md +++ b/docs/mkdocs/docs/api/basic_node/operator_ne.md @@ -22,7 +22,7 @@ This operator returns the result of `!(*this == rhs)`. (see [`operator==`](opera ???+ Example ```cpp - --8<-- "examples/ex_basic_node_operator_ne.cpp" + --8<-- "examples/ex_basic_node_operator_ne.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/sequence.md b/docs/mkdocs/docs/api/basic_node/sequence.md index e451b373..b305fdde 100644 --- a/docs/mkdocs/docs/api/basic_node/sequence.md +++ b/docs/mkdocs/docs/api/basic_node/sequence.md @@ -15,7 +15,7 @@ The resulting basic_node has the [`node_t::SEQUENCE`](node_t.md) type. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_sequence.cpp" + --8<-- "examples/ex_basic_node_sequence.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/sequence_type.md b/docs/mkdocs/docs/api/basic_node/sequence_type.md index a6c4d4aa..f8cfb6e0 100644 --- a/docs/mkdocs/docs/api/basic_node/sequence_type.md +++ b/docs/mkdocs/docs/api/basic_node/sequence_type.md @@ -15,7 +15,7 @@ Note that sequence objects are stored as pointers to the decided type in a [`bas ???+ Example ```cpp - --8<-- "examples/ex_basic_node_sequence_type.cpp" + --8<-- "examples/ex_basic_node_sequence_type.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/serialize.md b/docs/mkdocs/docs/api/basic_node/serialize.md index bfd8a47b..ca7d5098 100644 --- a/docs/mkdocs/docs/api/basic_node/serialize.md +++ b/docs/mkdocs/docs/api/basic_node/serialize.md @@ -14,22 +14,31 @@ This function serializes the given `node` parameter in the following format. # A scalar {[anchor] [tag] value | alias} +# A sequence key presented as an explicit key node. +? - + - +: + # A sequence value. # Extra 2 spaces are inserted before the sequence indicators denoted as "- ". # If an anchor and/or a tag are set to the sequence, they are put on the same line as the key. -# Sequence keys are not yet supported. : [anchor] [tag] - - [anchor] [tag] # A child sequence node. - + - [anchor] [tag] # A mapping node. + # A mapping whose value is a scalar. : +# A mapping key presented as an explicit key node. +? +: + # A mapping value whose value is a container node, either a mapping or a sequence. # Extra 2 spaces are inserted before the value to indicate indentation. # If an anchor and/or a tag are set to the mapping, they are put on the same line as the key. -# Mapping keys are not yet supported. : [anchor] [tag] : : [anchor] [tag] # A child mapping node. @@ -48,7 +57,7 @@ The resulting string object from the serialization of the `node` object. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_serialize.cpp" + --8<-- "examples/ex_basic_node_serialize.cpp:9" ``` output: @@ -59,4 +68,8 @@ The resulting string object from the serialization of the `node` object. ### **See Also** * [basic_node](index.md) +* [add_anchor_name](add_anchor_name.md) +* [add_tag_name](add_tag_name.md) * [deserialize](deserialize.md) +* [operator<<](insertion_operator.md) +* [operator"" _yaml](../operator_literal_yaml.md) diff --git a/docs/mkdocs/docs/api/basic_node/set_yaml_version.md b/docs/mkdocs/docs/api/basic_node/set_yaml_version.md index e02e2f9a..55446c0b 100644 --- a/docs/mkdocs/docs/api/basic_node/set_yaml_version.md +++ b/docs/mkdocs/docs/api/basic_node/set_yaml_version.md @@ -16,7 +16,7 @@ Sets the version of the YAML format to the `basic_node` object. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_set_yaml_version.cpp" + --8<-- "examples/ex_basic_node_set_yaml_version.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/size.md b/docs/mkdocs/docs/api/basic_node/size.md index 0a90c21d..e48dca10 100644 --- a/docs/mkdocs/docs/api/basic_node/size.md +++ b/docs/mkdocs/docs/api/basic_node/size.md @@ -16,7 +16,7 @@ The size of a node value. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_size.cpp" + --8<-- "examples/ex_basic_node_size.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/string_type.md b/docs/mkdocs/docs/api/basic_node/string_type.md index fd3ed34b..92861edd 100644 --- a/docs/mkdocs/docs/api/basic_node/string_type.md +++ b/docs/mkdocs/docs/api/basic_node/string_type.md @@ -15,7 +15,7 @@ Note that string objects are stored as pointers to the decided type in a [`basic ???+ Example ```cpp - --8<-- "examples/ex_basic_node_string_type.cpp" + --8<-- "examples/ex_basic_node_string_type.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/swap.md b/docs/mkdocs/docs/api/basic_node/swap.md index 9121fb66..03017cad 100644 --- a/docs/mkdocs/docs/api/basic_node/swap.md +++ b/docs/mkdocs/docs/api/basic_node/swap.md @@ -30,7 +30,7 @@ void swap(basic_node& rhs) noexcept; // (1) ???+ Example ```cpp - --8<-- "examples/ex_basic_node_swap_member.cpp" + --8<-- "examples/ex_basic_node_swap_member.cpp:9" ``` output: @@ -61,7 +61,7 @@ inline void swap( ???+ Example ```cpp - --8<-- "examples/ex_basic_node_swap_std.cpp" + --8<-- "examples/ex_basic_node_swap_std.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/type.md b/docs/mkdocs/docs/api/basic_node/type.md index b4a3ba69..727f087f 100644 --- a/docs/mkdocs/docs/api/basic_node/type.md +++ b/docs/mkdocs/docs/api/basic_node/type.md @@ -25,7 +25,7 @@ The type of the YAML node value. ???+ Example ```cpp - --8<-- "examples/ex_basic_node_type.cpp" + --8<-- "examples/ex_basic_node_type.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/value_converter_type.md b/docs/mkdocs/docs/api/basic_node/value_converter_type.md index 1130fb82..a1b103b4 100644 --- a/docs/mkdocs/docs/api/basic_node/value_converter_type.md +++ b/docs/mkdocs/docs/api/basic_node/value_converter_type.md @@ -24,7 +24,7 @@ If you want to convert some type from/to `basic_node`, however, it is recommende ???+ Example ```cpp - --8<-- "examples/ex_basic_node_value_converter_type.cpp" + --8<-- "examples/ex_basic_node_value_converter_type.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/basic_node/yaml_version_t.md b/docs/mkdocs/docs/api/basic_node/yaml_version_t.md index 8e9d4cbd..5f9a6e58 100644 --- a/docs/mkdocs/docs/api/basic_node/yaml_version_t.md +++ b/docs/mkdocs/docs/api/basic_node/yaml_version_t.md @@ -15,7 +15,7 @@ This enumeration collects the used versions of YAML specification. It is used as ???+ Example ```cpp - --8<-- "examples/ex_basic_node_yaml_version_t.cpp" + --8<-- "examples/ex_basic_node_yaml_version_t.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/exception/constructor.md b/docs/mkdocs/docs/api/exception/constructor.md index c81a2ba9..0b0ba0ae 100644 --- a/docs/mkdocs/docs/api/exception/constructor.md +++ b/docs/mkdocs/docs/api/exception/constructor.md @@ -22,7 +22,7 @@ Constructs an exception object without an error message. ???+ Example ```cpp - --8<-- "examples/ex_exception_constructor_noarg.cpp" + --8<-- "examples/ex_exception_constructor_noarg.cpp:9" ``` output: @@ -47,7 +47,7 @@ The given error message can be retrieved by calling [`exception::what()`](what.m ???+ Example ```cpp - --8<-- "examples/ex_exception_constructor_msg.cpp" + --8<-- "examples/ex_exception_constructor_msg.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/exception/what.md b/docs/mkdocs/docs/api/exception/what.md index 793d4379..4526d47a 100644 --- a/docs/mkdocs/docs/api/exception/what.md +++ b/docs/mkdocs/docs/api/exception/what.md @@ -11,7 +11,7 @@ Returns an error message for an exception. If nothing, a non-null, empty string ???+ Example ```cpp - --8<-- "examples/ex_exception_what.cpp" + --8<-- "examples/ex_exception_what.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/macros.md b/docs/mkdocs/docs/api/macros.md index 55ed66f1..90baf253 100644 --- a/docs/mkdocs/docs/api/macros.md +++ b/docs/mkdocs/docs/api/macros.md @@ -17,7 +17,7 @@ These macros are available for client applications as the metadata of this libra ??? Example annotate "Example: print the library version" ```cpp - --8<-- "examples/ex_macros_versions.cpp" + --8<-- "examples/ex_macros_versions.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/node_value_converter/from_node.md b/docs/mkdocs/docs/api/node_value_converter/from_node.md index ef845cf5..41d98cb9 100644 --- a/docs/mkdocs/docs/api/node_value_converter/from_node.md +++ b/docs/mkdocs/docs/api/node_value_converter/from_node.md @@ -39,7 +39,7 @@ Note that the `TargetType` must be default-constructible. ???+ Example ```cpp - --8<-- "examples/ex_node_value_converter_from_node.cpp" + --8<-- "examples/ex_node_value_converter_from_node.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/node_value_converter/to_node.md b/docs/mkdocs/docs/api/node_value_converter/to_node.md index e27088a3..420f808e 100644 --- a/docs/mkdocs/docs/api/node_value_converter/to_node.md +++ b/docs/mkdocs/docs/api/node_value_converter/to_node.md @@ -37,7 +37,7 @@ This function is usually called by the constructors of the [`basic_node`](../bas ???+ Example ```cpp - --8<-- "examples/ex_node_value_converter_to_node.cpp" + --8<-- "examples/ex_node_value_converter_to_node.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/operator_literal_yaml.md b/docs/mkdocs/docs/api/operator_literal_yaml.md index 41992b96..bc8da3c0 100644 --- a/docs/mkdocs/docs/api/operator_literal_yaml.md +++ b/docs/mkdocs/docs/api/operator_literal_yaml.md @@ -36,7 +36,7 @@ The resulting basic_node object deserialized from the input string `s`. ???+ Example ```cpp - --8<-- "examples/ex_operator_literal_yaml.cpp" + --8<-- "examples/ex_operator_literal_yaml.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/ordered_map/at.md b/docs/mkdocs/docs/api/ordered_map/at.md index f76e50a9..ba052bb1 100644 --- a/docs/mkdocs/docs/api/ordered_map/at.md +++ b/docs/mkdocs/docs/api/ordered_map/at.md @@ -34,7 +34,7 @@ Reference, or constant reference, to a `mapped_type` object associated with the ???+ Example ```cpp - --8<-- "examples/ex_ordered_map_at.cpp" + --8<-- "examples/ex_ordered_map_at.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/ordered_map/constructor.md b/docs/mkdocs/docs/api/ordered_map/constructor.md index 30ea99fe..33498f8a 100644 --- a/docs/mkdocs/docs/api/ordered_map/constructor.md +++ b/docs/mkdocs/docs/api/ordered_map/constructor.md @@ -23,7 +23,7 @@ The content of a newly constructed ordered_map is an empty list of key-value pai ???+ Example ```cpp - --8<-- "examples/ex_ordered_map_constructor_noarg.cpp" + --8<-- "examples/ex_ordered_map_constructor_noarg.cpp:9" ``` output: @@ -48,7 +48,7 @@ The resulting ordered_map object has the same list of key-value pairs as the giv ???+ Example ```cpp - --8<-- "examples/ex_ordered_map_constructor_initializer_list.cpp" + --8<-- "examples/ex_ordered_map_constructor_initializer_list.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/ordered_map/emplace.md b/docs/mkdocs/docs/api/ordered_map/emplace.md index e2a74c67..ebd1c3f4 100644 --- a/docs/mkdocs/docs/api/ordered_map/emplace.md +++ b/docs/mkdocs/docs/api/ordered_map/emplace.md @@ -29,7 +29,7 @@ A pair consisting of an iterator to the inserted element, or the already-existin ???+ Example ```cpp - --8<-- "examples/ex_ordered_map_constructor_initializer_list.cpp" + --8<-- "examples/ex_ordered_map_constructor_initializer_list.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/ordered_map/find.md b/docs/mkdocs/docs/api/ordered_map/find.md index 4d8878bc..ad492b9f 100644 --- a/docs/mkdocs/docs/api/ordered_map/find.md +++ b/docs/mkdocs/docs/api/ordered_map/find.md @@ -33,7 +33,7 @@ An iterator to the target value if found, the result of end() otherwise. ???+ Example ```cpp - --8<-- "examples/ex_ordered_map_emplace.cpp" + --8<-- "examples/ex_ordered_map_emplace.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/api/ordered_map/operator[].md b/docs/mkdocs/docs/api/ordered_map/operator[].md index 484befca..7e16c694 100644 --- a/docs/mkdocs/docs/api/ordered_map/operator[].md +++ b/docs/mkdocs/docs/api/ordered_map/operator[].md @@ -35,7 +35,7 @@ Possibly a default value of the `mapped_type` if the ordered_map does not contai ???+ Example ```cpp - --8<-- "examples/ex_ordered_map_subscript_operator.cpp" + --8<-- "examples/ex_ordered_map_subscript_operator.cpp:9" ``` output: diff --git a/docs/mkdocs/docs/home/releases.md b/docs/mkdocs/docs/home/releases.md index c39a71ca..8d431cc3 100644 --- a/docs/mkdocs/docs/home/releases.md +++ b/docs/mkdocs/docs/home/releases.md @@ -1,5 +1,57 @@ # Releases +## **fkYAML version 0.3.5** + +!!! abstract "Release Packages" + + * [fkYAML.tgz](https://github.com/fktn-k/fkYAML/releases/download/v0.3.5/fkYAML.tgz) + * [fkYAML.zip](https://github.com/fktn-k/fkYAML/releases/download/v0.3.5/fkYAML.zip) + * [fkYAML_single_header.zip](https://github.com/fktn-k/fkYAML/releases/download/v0.3.5/fkYAML_single_header.zip) + * [fkYAML_single_header.tgz](https://github.com/fktn-k/fkYAML/releases/download/v0.3.5/fkYAML_single_header.tgz) + * [node.hpp](https://github.com/fktn-k/fkYAML/releases/download/v0.3.5/node.hpp) (single header) + +### Summary + +This release adds the new support for serialization of YAML container key nodes either with or without tags and/or anchors. +In addition, Win32 builds are now tested during the GitHub Actions workflows. +Moreover, the performance and efficiency of Unicode handlings have been improved by refactoring the deserialization feature. + +For contributors, fkYAML now depends on the Clang-Format Python distribution as the source code formatter and provides Bash/Batch scripts for running the Clang-Format and amalgamation. +For more information, see the latest CONTRIBUTING.md file. + +### What's Changed + +#### :sparkles: New Features + +- Serialize container keys by [@fktn-k](https://github.com/fktn-k) in [\#319](https://github.com/fktn-k/fkYAML/pull/319) + +#### :zap: Improvements + +- Query the count of UTF-8 character bytes by [@fktn-k](https://github.com/fktn-k) in [\#317](https://github.com/fktn-k/fkYAML/pull/317) +- Improved UTF encoding processing by [@fktn-k](https://github.com/fktn-k) in [\#318](https://github.com/fktn-k/fkYAML/pull/318) +- Reformat source files by [@fktn-k](https://github.com/fktn-k) in [\#320](https://github.com/fktn-k/fkYAML/pull/320) + +#### :bug: Bug Fixes + +- Fix parse error on the first node which isn't a block mapping by [@fktn-k](https://github.com/fktn-k) in [\#322](https://github.com/fktn-k/fkYAML/pull/322) +- Force LF newline codes in the amalgamated file by [@fktn-k](https://github.com/fktn-k) in [\#324](https://github.com/fktn-k/fkYAML/pull/324) + +#### :robot: CI + +- Add workflows items to test Win32 builds by [@fktn-k](https://github.com/fktn-k) in [\#323](https://github.com/fktn-k/fkYAML/pull/323) + +#### :people_holding_hands: Community + +- Use Clang-Format python distribution by [@fktn-k](https://github.com/fktn-k) in [\#316](https://github.com/fktn-k/fkYAML/pull/316) +- Set the unit test app as the Visual Studio startup project by [@fktn-k](https://github.com/fktn-k) in [\#321](https://github.com/fktn-k/fkYAML/pull/321) +- Add Bash & Batch scripts to run clang-format & amalgamation by [@fktn-k](https://github.com/fktn-k) in [\#325](https://github.com/fktn-k/fkYAML/pull/325) + + + +**Full Changelog**: https://github.com/fktn-k/fkYAML/compare/v0.3.3...v0.3.5 + +--- + ## **fkYAML version 0.3.4** !!! abstract "Release Packages" @@ -42,6 +94,8 @@ For CI processes, workflows using the macOS 14 GitHub Actions runner image have **Full Changelog**: https://github.com/fktn-k/fkYAML/compare/v0.3.3...v0.3.4 +--- + ## **fkYAML version 0.3.3** !!! abstract "Release Packages" diff --git a/docs/mkdocs/docs/tutorials/cmake_integration.md b/docs/mkdocs/docs/tutorials/cmake_integration.md index 41e6c86f..79a9fda4 100644 --- a/docs/mkdocs/docs/tutorials/cmake_integration.md +++ b/docs/mkdocs/docs/tutorials/cmake_integration.md @@ -57,7 +57,7 @@ Since CMake v3.11, [`FetchContent`](https://cmake.org/cmake/help/latest/module/F FetchContent_Declare( fkYAML GIT_REPOSITORY https://github.com/fktn-k/fkYAML.git - GIT_TAG v0.3.4 + GIT_TAG v0.3.5 ) FetchContent_MakeAvailable(fkYAML) diff --git a/docs/mkdocs/docs/tutorials/index.md b/docs/mkdocs/docs/tutorials/index.md index 6deb74a9..012e1602 100644 --- a/docs/mkdocs/docs/tutorials/index.md +++ b/docs/mkdocs/docs/tutorials/index.md @@ -76,7 +76,7 @@ Also, make sure the example.yaml file is encoded in either the UTF-8, UTF-16BE/L === "tutorial.cpp" ```cpp - --8<-- "examples/tutorial_1.cpp" + --8<-- "examples/tutorial_1.cpp:9" ``` === "CMakeLists.txt" @@ -113,8 +113,8 @@ You can also access each YAML node with the fkYAML APIs. Say you just want to care about values associated with the `title` key and ignore the others. You can do it by modifying the tutorial.cpp file as follows: -```cpp title="tutorial.cpp" hl_lines="13-18" ---8<-- "examples/tutorial_2.cpp" +```cpp title="tutorial.cpp" hl_lines="12-16" +--8<-- "examples/tutorial_2.cpp:9" ``` Rebuild and run the application, and you'll see the output like: @@ -130,8 +130,8 @@ The fkYAML library also provides a feature to realize such a need. You can achieve that by changing the highlighted part of the code snippet: -```cpp title="tutorial.cpp" hl_lines="14-16 18 21-26 29-30" ---8<-- "examples/tutorial_3.cpp" +```cpp title="tutorial.cpp" hl_lines="13-15 17 19-21 24-25" +--8<-- "examples/tutorial_3.cpp:9" ``` Rebuild and run the application, and you'll see the output like: @@ -147,8 +147,8 @@ Note that you don't need to implement specializations for STL types (such as std The updated code snippet down below shows how the specializations for user-defined types can reduce boilerplate code. -```cpp title="tutorial.cpp" hl_lines="6-39 53-54 56-57 59-61" ---8<-- "examples/tutorial_4.cpp" +```cpp title="tutorial.cpp" hl_lines="6-31 44-45 48-51" +--8<-- "examples/tutorial_4.cpp:9" ``` Rebuild and run the application, and you'll see the same output as before: diff --git a/docs/mkdocs/mkdocs.yml b/docs/mkdocs/mkdocs.yml index 4e720c05..ac2064d5 100644 --- a/docs/mkdocs/mkdocs.yml +++ b/docs/mkdocs/mkdocs.yml @@ -64,7 +64,7 @@ markdown_extensions: alternate_style: true - pymdownx.tilde - pymdownx.snippets: - base_path: docs + base_path: .. check_paths: true - toc: permalink: true diff --git a/fkYAML.natvis b/fkYAML.natvis index f5a9d0d7..ba25d9dc 100644 --- a/fkYAML.natvis +++ b/fkYAML.natvis @@ -4,26 +4,26 @@ - - - {*(m_node_value.p_sequence)} - {*(m_node_value.p_mapping)} - nullptr - {m_node_value.boolean} - {m_node_value.integer} - {m_node_value.float_val} - {*(m_node_value.p_string)} + + + {*(m_node_value.p_sequence)} + {*(m_node_value.p_mapping)} + nullptr + {m_node_value.boolean} + {m_node_value.integer} + {m_node_value.float_val} + {*(m_node_value.p_string)} - + *(m_node_value.p_sequence),view(simple) - + *(m_node_value.p_mapping),view(simple) - + {second} second diff --git a/include/fkYAML/detail/assert.hpp b/include/fkYAML/detail/assert.hpp index 1d311a80..87b344fb 100644 --- a/include/fkYAML/detail/assert.hpp +++ b/include/fkYAML/detail/assert.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -13,12 +13,12 @@ // if FK_YAML_ASSERT is not user-defined. apply the default assert impl. #ifndef FK_YAML_ASSERT - #ifndef NDEBUG - #include - #define FK_YAML_ASSERT(x) assert(x) // NOLINT(cppcoreguidelines-macro-usage) - #else - #define FK_YAML_ASSERT(x) // NOLINT(cppcoreguidelines-macro-usage) - #endif +#ifndef NDEBUG +#include +#define FK_YAML_ASSERT(x) assert(x) // NOLINT(cppcoreguidelines-macro-usage) +#else +#define FK_YAML_ASSERT(x) // NOLINT(cppcoreguidelines-macro-usage) +#endif #endif #endif /* FK_YAML_DETAIL_ASSERT_HPP_ */ diff --git a/include/fkYAML/detail/conversions/from_node.hpp b/include/fkYAML/detail/conversions/from_node.hpp index 97d66daa..4bdaea49 100644 --- a/include/fkYAML/detail/conversions/from_node.hpp +++ b/include/fkYAML/detail/conversions/from_node.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -23,15 +23,7 @@ #include #include -/** - * @namespace fkyaml - * @brief namespace for fkYAML library. - */ -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /////////////////// // from_node // @@ -42,10 +34,8 @@ namespace detail /// @param n A basic_node object. /// @param s A sequence node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::sequence_type& s) -{ - if (!n.is_sequence()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::sequence_type& s) { + if (!n.is_sequence()) { throw type_error("The target node value type is not sequence type.", n.type()); } s = n.template get_value_ref(); @@ -64,17 +54,14 @@ template < has_from_node, negation, typename BasicNodeType::sequence_type>>>::value, int> = 0> -inline void from_node(const BasicNodeType& n, std::vector& s) -{ - if (!n.is_sequence()) - { +inline void from_node(const BasicNodeType& n, std::vector& s) { + if (!n.is_sequence()) { throw type_error("The target node value is not sequence type.", n.type()); } s.reserve(n.size()); - for (const auto& elem : n) - { + for (const auto& elem : n) { s.emplace_back(elem.template get_value()); } } @@ -84,15 +71,12 @@ inline void from_node(const BasicNodeType& n, std::vector& /// @param n A basic_node object. /// @param m A mapping node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::mapping_type& m) -{ - if (!n.is_mapping()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::mapping_type& m) { + if (!n.is_mapping()) { throw type_error("The target node value type is not mapping type.", n.type()); } - for (auto pair : n.template get_value_ref()) - { + for (auto pair : n.template get_value_ref()) { m.emplace(pair.first, pair.second); } } @@ -106,15 +90,12 @@ template < negation>, has_from_node, has_from_node>::value, int> = 0> -inline void from_node(const BasicNodeType& n, std::map& m) -{ - if (!n.is_mapping()) - { +inline void from_node(const BasicNodeType& n, std::map& m) { + if (!n.is_mapping()) { throw type_error("The target node value type is not mapping type.", n.type()); } - for (auto pair : n.template get_value_ref()) - { + for (auto pair : n.template get_value_ref()) { m.emplace( pair.first.template get_value(), pair.second.template get_value()); } @@ -125,11 +106,9 @@ inline void from_node(const BasicNodeType& n, std::map::value, int> = 0> -inline void from_node(const BasicNodeType& n, std::nullptr_t& null) -{ +inline void from_node(const BasicNodeType& n, std::nullptr_t& null) { // to ensure the target node value type is null. - if (!n.is_null()) - { + if (!n.is_null()) { throw type_error("The target node value type is not null type.", n.type()); } null = nullptr; @@ -140,10 +119,8 @@ inline void from_node(const BasicNodeType& n, std::nullptr_t& null) /// @param n A basic_node object. /// @param b A boolean node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::boolean_type& b) -{ - if (!n.is_boolean()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::boolean_type& b) { + if (!n.is_boolean()) { throw type_error("The target node value type is not boolean type.", n.type()); } b = n.template get_value_ref(); @@ -154,10 +131,8 @@ inline void from_node(const BasicNodeType& n, typename BasicNodeType::boolean_ty /// @param n A basic_node object. /// @param i An integer node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::integer_type& i) -{ - if (!n.is_integer()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::integer_type& i) { + if (!n.is_integer()) { throw type_error("The target node value type is not integer type.", n.type()); } i = n.template get_value_ref(); @@ -175,22 +150,18 @@ template < is_non_bool_integral, negation>>::value, int> = 0> -inline void from_node(const BasicNodeType& n, IntegerType& i) -{ - if (!n.is_integer()) - { +inline void from_node(const BasicNodeType& n, IntegerType& i) { + if (!n.is_integer()) { throw type_error("The target node value type is not integer type.", n.type()); } // under/overflow check. using node_int_type = typename BasicNodeType::integer_type; node_int_type tmp_int = n.template get_value_ref(); - if (tmp_int < static_cast(std::numeric_limits::min())) - { + if (tmp_int < static_cast(std::numeric_limits::min())) { throw exception("Integer value underflow detected."); } - if (static_cast(std::numeric_limits::max()) < tmp_int) - { + if (static_cast(std::numeric_limits::max()) < tmp_int) { throw exception("Integer value overflow detected."); } @@ -202,10 +173,8 @@ inline void from_node(const BasicNodeType& n, IntegerType& i) /// @param n A basic_node object. /// @param f A float number node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::float_number_type& f) -{ - if (!n.is_float_number()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::float_number_type& f) { + if (!n.is_float_number()) { throw type_error("The target node value type is not float number type.", n.type()); } f = n.template get_value_ref(); @@ -223,20 +192,16 @@ template < std::is_floating_point, negation>>::value, int> = 0> -inline void from_node(const BasicNodeType& n, FloatType& f) -{ - if (!n.is_float_number()) - { +inline void from_node(const BasicNodeType& n, FloatType& f) { + if (!n.is_float_number()) { throw type_error("The target node value type is not float number type.", n.type()); } auto tmp_float = n.template get_value_ref(); - if (tmp_float < std::numeric_limits::min()) - { + if (tmp_float < std::numeric_limits::min()) { throw exception("Floating point value underflow detected."); } - if (std::numeric_limits::max() < tmp_float) - { + if (std::numeric_limits::max() < tmp_float) { throw exception("Floating point value overflow detected."); } @@ -248,10 +213,8 @@ inline void from_node(const BasicNodeType& n, FloatType& f) /// @param n A basic_node object. /// @param s A string node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::string_type& s) -{ - if (!n.is_string()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::string_type& s) { + if (!n.is_string()) { throw type_error("The target node value type is not string type.", n.type()); } s = n.template get_value_ref(); @@ -272,10 +235,8 @@ template < std::is_constructible, std::is_assignable>>::value, int> = 0> -inline void from_node(const BasicNodeType& n, CompatibleStringType& s) -{ - if (!n.is_string()) - { +inline void from_node(const BasicNodeType& n, CompatibleStringType& s) { + if (!n.is_string()) { throw type_error("The target node value type is not string type.", n.type()); } s = n.template get_value_ref(); @@ -283,8 +244,7 @@ inline void from_node(const BasicNodeType& n, CompatibleStringType& s) /// @brief A function object to call from_node functions. /// @note User-defined specialization is available by providing implementation **OUTSIDE** fkyaml namespace. -struct from_node_fn -{ +struct from_node_fn { /// @brief Call from_node function suitable for the given T type. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam T A target value type assigned from the basic_node object. @@ -293,13 +253,14 @@ struct from_node_fn /// @return decltype(from_node(n, std::forward(val))) void by default. User can set it to some other type. template auto operator()(const BasicNodeType& n, T&& val) const noexcept(noexcept(from_node(n, std::forward(val)))) - -> decltype(from_node(n, std::forward(val))) - { + -> decltype(from_node(n, std::forward(val))) { return from_node(n, std::forward(val)); } }; -} // namespace detail +FK_YAML_DETAIL_NAMESPACE_END + +FK_YAML_NAMESPACE_BEGIN #ifndef FK_YAML_HAS_CXX_17 // anonymous namespace to hold `from_node` functor. diff --git a/include/fkYAML/detail/conversions/from_string.hpp b/include/fkYAML/detail/conversions/from_string.hpp index 642b461c..93465b00 100644 --- a/include/fkYAML/detail/conversions/from_string.hpp +++ b/include/fkYAML/detail/conversions/from_string.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -22,11 +22,7 @@ #include #include -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Convert a string YAML token to a ValueType object. /// @tparam ValueType A target value type. @@ -37,10 +33,8 @@ inline ValueType from_string(const std::basic_string& s, type_tag -inline std::nullptr_t from_string(const std::string& s, type_tag /*unused*/) -{ - if (s == "null" || s == "Null" || s == "NULL" || s == "~") - { +inline std::nullptr_t from_string(const std::string& s, type_tag /*unused*/) { + if (s == "null" || s == "Null" || s == "NULL" || s == "~") { return nullptr; } @@ -50,15 +44,12 @@ inline std::nullptr_t from_string(const std::string& s, type_tag /// @brief Specialization of from_string() for boolean values with std::string. /// @tparam N/A template <> -inline bool from_string(const std::string& s, type_tag /*unused*/) -{ - if (s == "true" || s == "True" || s == "TRUE") - { +inline bool from_string(const std::string& s, type_tag /*unused*/) { + if (s == "true" || s == "True" || s == "TRUE") { return true; } - if (s == "false" || s == "False" || s == "FALSE") - { + if (s == "false" || s == "False" || s == "FALSE") { return false; } @@ -68,17 +59,14 @@ inline bool from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for int values with std::string. /// @tparam N/A template <> -inline int from_string(const std::string& s, type_tag /*unused*/) -{ +inline int from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; long ret = 0; - try - { + try { ret = std::stoi(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into an int value."); } @@ -88,17 +76,14 @@ inline int from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for long values with std::string. /// @tparam N/A template <> -inline long from_string(const std::string& s, type_tag /*unused*/) -{ +inline long from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; long ret = 0; - try - { + try { ret = std::stol(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into a long value."); } @@ -108,17 +93,14 @@ inline long from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for long long values with std::string. /// @tparam N/A template <> -inline long long from_string(const std::string& s, type_tag /*unused*/) -{ +inline long long from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; long long ret = 0; - try - { + try { ret = std::stoll(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into a long long value."); } @@ -133,11 +115,9 @@ inline enable_if_t< is_non_bool_integral, std::is_signed, negation>, negation>, negation>>::value, SignedIntType> -from_string(const std::string& s, type_tag /*unused*/) -{ +from_string(const std::string& s, type_tag /*unused*/) { const auto tmp_ret = from_string(s, type_tag {}); - if (static_cast(std::numeric_limits::max()) < tmp_ret) - { + if (static_cast(std::numeric_limits::max()) < tmp_ret) { throw exception("Failed to convert a long long value into a SignedIntegerType value."); } @@ -147,17 +127,14 @@ from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for unsigned long values with std::string. /// @tparam N/A template <> -inline unsigned long from_string(const std::string& s, type_tag /*unused*/) -{ +inline unsigned long from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; unsigned long ret = 0; - try - { + try { ret = std::stoul(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into an unsigned long value."); } @@ -167,17 +144,14 @@ inline unsigned long from_string(const std::string& s, type_tag / /// @brief Specialization of from_string() for unsigned long long values with std::string. /// @tparam N/A template <> -inline unsigned long long from_string(const std::string& s, type_tag /*unused*/) -{ +inline unsigned long long from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; unsigned long long ret = 0; - try - { + try { ret = std::stoull(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into an unsigned long long value."); } @@ -193,11 +167,9 @@ inline enable_if_t< negation>, negation>>::value, UnsignedIntType> -from_string(const std::string& s, type_tag /*unused*/) -{ +from_string(const std::string& s, type_tag /*unused*/) { const auto tmp_ret = from_string(s, type_tag {}); - if (static_cast(std::numeric_limits::max()) < tmp_ret) - { + if (static_cast(std::numeric_limits::max()) < tmp_ret) { throw exception("Failed to convert an unsigned long long into an unsigned integer value."); } @@ -207,33 +179,27 @@ from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for float values with std::string. /// @tparam N/A template <> -inline float from_string(const std::string& s, type_tag /*unused*/) -{ - if (s == ".inf" || s == ".Inf" || s == ".INF") - { +inline float from_string(const std::string& s, type_tag /*unused*/) { + if (s == ".inf" || s == ".Inf" || s == ".INF") { return std::numeric_limits::infinity(); } - if (s == "-.inf" || s == "-.Inf" || s == "-.INF") - { + if (s == "-.inf" || s == "-.Inf" || s == "-.INF") { static_assert(std::numeric_limits::is_iec559, "IEEE 754 required."); return -1 * std::numeric_limits::infinity(); } - if (s == ".nan" || s == ".NaN" || s == ".NAN") - { + if (s == ".nan" || s == ".NaN" || s == ".NAN") { return std::nanf(""); } std::size_t idx = 0; float ret = 0.0f; - try - { + try { ret = std::stof(s, &idx); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into a float value."); } @@ -243,41 +209,33 @@ inline float from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for double values with std::string. /// @tparam N/A template <> -inline double from_string(const std::string& s, type_tag /*unused*/) -{ - if (s == ".inf" || s == ".Inf" || s == ".INF") - { +inline double from_string(const std::string& s, type_tag /*unused*/) { + if (s == ".inf" || s == ".Inf" || s == ".INF") { return std::numeric_limits::infinity(); } - if (s == "-.inf" || s == "-.Inf" || s == "-.INF") - { + if (s == "-.inf" || s == "-.Inf" || s == "-.INF") { static_assert(std::numeric_limits::is_iec559, "IEEE 754 required."); return -1 * std::numeric_limits::infinity(); } - if (s == ".nan" || s == ".NaN" || s == ".NAN") - { + if (s == ".nan" || s == ".NaN" || s == ".NAN") { return std::nan(""); } std::size_t idx = 0; double ret = 0.0; - try - { + try { ret = std::stod(s, &idx); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into a double value."); } return ret; } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_CONVERSIONS_FROM_STRING_HPP_ */ diff --git a/include/fkYAML/detail/conversions/to_node.hpp b/include/fkYAML/detail/conversions/to_node.hpp index 57b74cec..c299d4a0 100644 --- a/include/fkYAML/detail/conversions/to_node.hpp +++ b/include/fkYAML/detail/conversions/to_node.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -19,12 +19,7 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /////////////////////////////////// // external_node_constructor // @@ -41,15 +36,13 @@ struct external_node_constructor; /// @brief The specialization of external_node_constructor for sequence nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with const lvalue sequence. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param s A lvalue sequence value. template ::value, int> = 0> - static void construct(BasicNodeType& n, const typename BasicNodeType::sequence_type& s) noexcept - { + static void construct(BasicNodeType& n, const typename BasicNodeType::sequence_type& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::SEQUENCE; n.m_node_value.p_sequence = BasicNodeType::template create_object(s); @@ -60,8 +53,7 @@ struct external_node_constructor /// @param n A basic_node object. /// @param s A rvalue sequence value. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::sequence_type&& s) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::sequence_type&& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::SEQUENCE; n.m_node_value.p_sequence = @@ -71,15 +63,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for mapping nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with const lvalue mapping. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param m A lvalue mapping value. template ::value, int> = 0> - static void construct(BasicNodeType& n, const typename BasicNodeType::mapping_type& m) noexcept - { + static void construct(BasicNodeType& n, const typename BasicNodeType::mapping_type& m) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::MAPPING; n.m_node_value.p_mapping = BasicNodeType::template create_object(m); @@ -90,8 +80,7 @@ struct external_node_constructor /// @param n A basic_node object. /// @param m A rvalue mapping value. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::mapping_type&& m) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::mapping_type&& m) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::MAPPING; n.m_node_value.p_mapping = @@ -101,15 +90,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for null nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with nullptr. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param (unused) nullptr template ::value, int> = 0> - static void construct(BasicNodeType& n, std::nullptr_t /*unused*/) noexcept - { + static void construct(BasicNodeType& n, std::nullptr_t /*unused*/) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::NULL_OBJECT; n.m_node_value.p_mapping = nullptr; @@ -118,15 +105,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for boolean scalar nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with boolean. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param b A boolean value. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::boolean_type b) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::boolean_type b) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::BOOLEAN; n.m_node_value.boolean = b; @@ -135,15 +120,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for integer scalar nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with integers. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param i An integer value. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::integer_type i) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::integer_type i) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::INTEGER; n.m_node_value.integer = i; @@ -152,15 +135,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for float number scalar nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with floating point numbers. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param f A floating point number. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::float_number_type f) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::float_number_type f) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::FLOAT_NUMBER; n.m_node_value.float_val = f; @@ -169,15 +150,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for string scalar nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with const lvalue strings. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param s A constant lvalue string. template ::value, int> = 0> - static void construct(BasicNodeType& n, const typename BasicNodeType::string_type& s) noexcept - { + static void construct(BasicNodeType& n, const typename BasicNodeType::string_type& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::STRING; n.m_node_value.p_string = BasicNodeType::template create_object(s); @@ -188,8 +167,7 @@ struct external_node_constructor /// @param n A basic_node object. /// @param s A rvalue string. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::string_type&& s) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::string_type&& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::STRING; n.m_node_value.p_string = @@ -208,8 +186,7 @@ struct external_node_constructor is_basic_node, negation>>::value, int> = 0> - static void construct(BasicNodeType& n, const CompatibleStringType& s) noexcept - { + static void construct(BasicNodeType& n, const CompatibleStringType& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::STRING; n.m_node_value.p_string = BasicNodeType::template create_object(s); @@ -232,8 +209,7 @@ template < is_basic_node, std::is_same>>::value, int> = 0> -inline void to_node(BasicNodeType& n, T&& s) noexcept -{ +inline void to_node(BasicNodeType& n, T&& s) noexcept { external_node_constructor::construct(n, std::forward(s)); } @@ -248,8 +224,7 @@ template < conjunction< is_basic_node, std::is_same>>::value, int> = 0> -inline void to_node(BasicNodeType& n, T&& m) noexcept -{ +inline void to_node(BasicNodeType& n, T&& m) noexcept { external_node_constructor::construct(n, std::forward(m)); } @@ -259,8 +234,7 @@ inline void to_node(BasicNodeType& n, T&& m) noexcept template < typename BasicNodeType, typename NullType, enable_if_t, std::is_same>::value, int> = 0> -inline void to_node(BasicNodeType& n, NullType /*unused*/) -{ +inline void to_node(BasicNodeType& n, NullType /*unused*/) { external_node_constructor::construct(n, nullptr); } @@ -274,8 +248,7 @@ template < enable_if_t< conjunction, std::is_same>::value, int> = 0> -inline void to_node(BasicNodeType& n, T b) noexcept -{ +inline void to_node(BasicNodeType& n, T b) noexcept { external_node_constructor::construct(n, b); } @@ -287,8 +260,7 @@ inline void to_node(BasicNodeType& n, T b) noexcept template < typename BasicNodeType, typename T, enable_if_t, is_non_bool_integral>::value, int> = 0> -inline void to_node(BasicNodeType& n, T i) noexcept -{ +inline void to_node(BasicNodeType& n, T i) noexcept { external_node_constructor::construct(n, i); } @@ -300,8 +272,7 @@ inline void to_node(BasicNodeType& n, T i) noexcept template < typename BasicNodeType, typename T, enable_if_t, std::is_floating_point>::value, int> = 0> -inline void to_node(BasicNodeType& n, T f) noexcept -{ +inline void to_node(BasicNodeType& n, T f) noexcept { external_node_constructor::construct(n, f); } @@ -317,8 +288,7 @@ template < is_basic_node, negation>, std::is_constructible>::value, int> = 0> -inline void to_node(BasicNodeType& n, const T& s) -{ +inline void to_node(BasicNodeType& n, const T& s) { external_node_constructor::construct(n, s); } @@ -327,15 +297,13 @@ inline void to_node(BasicNodeType& n, const T& s) /// @param n A basic_node object. /// @param s An rvalue string node value. template ::value, int> = 0> -inline void to_node(BasicNodeType& n, typename BasicNodeType::string_type&& s) noexcept -{ +inline void to_node(BasicNodeType& n, typename BasicNodeType::string_type&& s) noexcept { external_node_constructor::construct(n, std::move(s)); } /// @brief A function object to call to_node functions. /// @note User-defined specialization is available by providing implementation **OUTSIDE** fkyaml namespace. -struct to_node_fn -{ +struct to_node_fn { /// @brief Call to_node function suitable for the given T type. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam T A target value type assigned to the basic_node object. @@ -344,13 +312,14 @@ struct to_node_fn /// @return decltype(to_node(n, std::forward(val))) void by default. User can set it to some other type. template auto operator()(BasicNodeType& n, T&& val) const noexcept(noexcept(to_node(n, std::forward(val)))) - -> decltype(to_node(n, std::forward(val))) - { + -> decltype(to_node(n, std::forward(val))) { return to_node(n, std::forward(val)); } }; -} // namespace detail +FK_YAML_DETAIL_NAMESPACE_END + +FK_YAML_NAMESPACE_BEGIN #ifndef FK_YAML_HAS_CXX_17 // anonymous namespace to hold `to_node` functor. diff --git a/include/fkYAML/detail/conversions/to_string.hpp b/include/fkYAML/detail/conversions/to_string.hpp index bda5a186..7e70e3bc 100644 --- a/include/fkYAML/detail/conversions/to_string.hpp +++ b/include/fkYAML/detail/conversions/to_string.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -21,12 +21,7 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Converts a ValueType object to a string YAML token. /// @tparam ValueType A source value type. @@ -40,8 +35,7 @@ inline void to_string(ValueType v, std::basic_string& s) noexcept; /// @param s A resulting string YAML token. /// @param (unused) nullptr template <> -inline void to_string(std::nullptr_t /*unused*/, std::string& s) noexcept -{ +inline void to_string(std::nullptr_t /*unused*/, std::string& s) noexcept { s = "null"; } @@ -49,8 +43,7 @@ inline void to_string(std::nullptr_t /*unused*/, std::string& s) noexcept /// @param s A resulting string YAML token. /// @param b A boolean source value. template <> -inline void to_string(bool b, std::string& s) noexcept -{ +inline void to_string(bool b, std::string& s) noexcept { s = b ? "true" : "false"; } @@ -59,8 +52,7 @@ inline void to_string(bool b, std::string& s) noexcept /// @param s A resulting string YAML token. /// @param i An integer source value. template -inline enable_if_t::value> to_string(IntegerType i, std::string& s) noexcept -{ +inline enable_if_t::value> to_string(IntegerType i, std::string& s) noexcept { s = std::to_string(i); } @@ -69,22 +61,17 @@ inline enable_if_t::value> to_string(IntegerTy /// @param s A resulting string YAML token. /// @param f A floating point number source value. template -inline enable_if_t::value> to_string(FloatType f, std::string& s) noexcept -{ - if (std::isnan(f)) - { +inline enable_if_t::value> to_string(FloatType f, std::string& s) noexcept { + if (std::isnan(f)) { s = ".nan"; return; } - if (std::isinf(f)) - { - if (f == std::numeric_limits::infinity()) - { + if (std::isinf(f)) { + if (f == std::numeric_limits::infinity()) { s = ".inf"; } - else - { + else { s = "-.inf"; } return; @@ -95,8 +82,6 @@ inline enable_if_t::value> to_string(FloatType s = oss.str(); } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* TO__string_HPP_ */ diff --git a/include/fkYAML/detail/directive_set.hpp b/include/fkYAML/detail/directive_set.hpp index 8e0528ec..ba695633 100644 --- a/include/fkYAML/detail/directive_set.hpp +++ b/include/fkYAML/detail/directive_set.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -17,18 +17,10 @@ #include #include -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementaions of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief The set of directives for a YAML document. -struct directive_set -{ +struct directive_set { /// The YAML version used for the YAML document. yaml_version_t version {yaml_version_t::VER_1_2}; /// Whether or not the YAML version has been specified. @@ -41,8 +33,6 @@ struct directive_set std::map named_handle_map {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_DIRECTIVE_SET_HPP_ */ diff --git a/include/fkYAML/detail/encodings/encode_detector.hpp b/include/fkYAML/detail/encodings/encode_detector.hpp index 647d9996..093c7069 100644 --- a/include/fkYAML/detail/encodings/encode_detector.hpp +++ b/include/fkYAML/detail/encodings/encode_detector.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -18,73 +18,58 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Detect an encoding type for UTF-8 expected inputs. /// @note This function doesn't support the case where the first character is null. /// @param[in] bytes 4 bytes of an input character sequence. /// @param[out] has_bom Whether or not the input contains a BOM. /// @return A detected encoding type. -inline utf_encode_t detect_encoding_type(const std::array& bytes, bool& has_bom) noexcept -{ +inline utf_encode_t detect_encoding_type(const std::array& bytes, bool& has_bom) noexcept { has_bom = false; // Check if a BOM exists. - if (bytes[0] == uint8_t(0xEFu) && bytes[1] == uint8_t(0xBBu) && bytes[2] == uint8_t(0xBFu)) - { + if (bytes[0] == uint8_t(0xEFu) && bytes[1] == uint8_t(0xBBu) && bytes[2] == uint8_t(0xBFu)) { has_bom = true; return utf_encode_t::UTF_8; } - if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == uint8_t(0xFEu) && bytes[3] == uint8_t(0xFFu)) - { + if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == uint8_t(0xFEu) && bytes[3] == uint8_t(0xFFu)) { has_bom = true; return utf_encode_t::UTF_32BE; } - if (bytes[0] == uint8_t(0xFFu) && bytes[1] == uint8_t(0xFEu) && bytes[2] == 0 && bytes[3] == 0) - { + if (bytes[0] == uint8_t(0xFFu) && bytes[1] == uint8_t(0xFEu) && bytes[2] == 0 && bytes[3] == 0) { has_bom = true; return utf_encode_t::UTF_32LE; } - if (bytes[0] == uint8_t(0xFEu) && bytes[1] == uint8_t(0xFFu)) - { + if (bytes[0] == uint8_t(0xFEu) && bytes[1] == uint8_t(0xFFu)) { has_bom = true; return utf_encode_t::UTF_16BE; } - if (bytes[0] == uint8_t(0xFFu) && bytes[1] == uint8_t(0xFEu)) - { + if (bytes[0] == uint8_t(0xFFu) && bytes[1] == uint8_t(0xFEu)) { has_bom = true; return utf_encode_t::UTF_16LE; } // Test the first character assuming it's an ASCII character. - if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0 && 0 < bytes[3] && bytes[3] < uint8_t(0x80u)) - { + if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0 && 0 < bytes[3] && bytes[3] < uint8_t(0x80u)) { return utf_encode_t::UTF_32BE; } - if (0 < bytes[0] && bytes[0] < uint8_t(0x80u) && bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0) - { + if (0 < bytes[0] && bytes[0] < uint8_t(0x80u) && bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0) { return utf_encode_t::UTF_32LE; } - if (bytes[0] == 0 && 0 < bytes[1] && bytes[1] < uint8_t(0x80u)) - { + if (bytes[0] == 0 && 0 < bytes[1] && bytes[1] < uint8_t(0x80u)) { return utf_encode_t::UTF_16BE; } - if (0 < bytes[0] && bytes[0] < uint8_t(0x80u) && bytes[1] == 0) - { + if (0 < bytes[0] && bytes[0] < uint8_t(0x80u) && bytes[1] == 0) { return utf_encode_t::UTF_16LE; } @@ -98,25 +83,20 @@ inline utf_encode_t detect_encoding_type(const std::array& bytes, bo /// @param end The end of input iterators. /// @return A detected encoding type. template ())))> -inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& end) -{ +inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& end) { std::array bytes = {{0xFFu, 0xFFu, 0xFFu, 0xFFu}}; - switch (ElemSize) - { - case sizeof(char): { // this case covers char8_t as well when compiled with C++20 features. - for (std::size_t i = 0; i < 4 && begin + i != end; i++) - { + switch (ElemSize) { + case sizeof(char): { // this case covers char8_t as well when compiled with C++20 or better. + for (std::size_t i = 0; i < 4 && begin + i != end; i++) { bytes[i] = uint8_t(begin[i]); } bool has_bom = false; utf_encode_t encode_type = detect_encoding_type(bytes, has_bom); - if (has_bom) - { + if (has_bom) { // skip reading the BOM. - switch (encode_type) - { + switch (encode_type) { case utf_encode_t::UTF_8: std::advance(begin, 3); break; @@ -134,12 +114,10 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& return encode_type; } case sizeof(char16_t): { - if (begin == end) - { + if (begin == end) { return utf_encode_t::UTF_16BE; } - for (int i = 0; i < 2 && begin + i != end; i++) - { + for (int i = 0; i < 2 && begin + i != end; i++) { bytes[i * 2] = uint8_t((begin[i] & 0xFF00u) >> 8); bytes[i * 2 + 1] = uint8_t(begin[i] & 0xFFu); } @@ -147,13 +125,11 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& bool has_bom = false; utf_encode_t encode_type = detect_encoding_type(bytes, has_bom); - if (encode_type != utf_encode_t::UTF_16BE && encode_type != utf_encode_t::UTF_16LE) - { + if (encode_type != utf_encode_t::UTF_16BE && encode_type != utf_encode_t::UTF_16LE) { throw exception("char16_t characters must be encoded in the UTF-16 format."); } - if (has_bom) - { + if (has_bom) { // skip reading the BOM. std::advance(begin, 1); } @@ -161,8 +137,7 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& return encode_type; } case sizeof(char32_t): { - if (begin == end) - { + if (begin == end) { return utf_encode_t::UTF_32BE; } @@ -174,13 +149,11 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& bool has_bom = false; utf_encode_t encode_type = detect_encoding_type(bytes, has_bom); - if (encode_type != utf_encode_t::UTF_32BE && encode_type != utf_encode_t::UTF_32LE) - { + if (encode_type != utf_encode_t::UTF_32BE && encode_type != utf_encode_t::UTF_32LE) { throw exception("char32_t characters must be encoded in the UTF-32 format."); } - if (has_bom) - { + if (has_bom) { // skip reading the BOM. std::advance(begin, 1); } @@ -192,15 +165,12 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& } } -inline utf_encode_t detect_encoding_and_skip_bom(std::FILE* file) noexcept -{ +inline utf_encode_t detect_encoding_and_skip_bom(std::FILE* file) noexcept { std::array bytes = {{0xFFu, 0xFFu, 0xFFu, 0xFFu}}; - for (std::size_t i = 0; i < 4; i++) - { + for (std::size_t i = 0; i < 4; i++) { char byte = 0; std::size_t size = std::fread(&byte, sizeof(char), 1, file); - if (size != sizeof(char)) - { + if (size != sizeof(char)) { break; } bytes[i] = uint8_t(byte & 0xFF); @@ -211,10 +181,8 @@ inline utf_encode_t detect_encoding_and_skip_bom(std::FILE* file) noexcept // move back to the beginning if a BOM doesn't exist. long offset = 0; - if (has_bom) - { - switch (encode_type) - { + if (has_bom) { + switch (encode_type) { case utf_encode_t::UTF_8: offset = 3; break; @@ -233,16 +201,13 @@ inline utf_encode_t detect_encoding_and_skip_bom(std::FILE* file) noexcept return encode_type; } -inline utf_encode_t detect_encoding_and_skip_bom(std::istream& is) noexcept -{ +inline utf_encode_t detect_encoding_and_skip_bom(std::istream& is) noexcept { std::array bytes = {{0xFFu, 0xFFu, 0xFFu, 0xFFu}}; - for (std::size_t i = 0; i < 4; i++) - { + for (std::size_t i = 0; i < 4; i++) { char ch = 0; is.read(&ch, 1); std::streamsize size = is.gcount(); - if (size != 1) - { + if (size != 1) { // without this, seekg() fails in the switch-case statement below. is.clear(); break; @@ -255,10 +220,8 @@ inline utf_encode_t detect_encoding_and_skip_bom(std::istream& is) noexcept // move back to the beginning if a BOM doesn't exist. std::streamoff offset = 0; - if (has_bom) - { - switch (encode_type) - { + if (has_bom) { + switch (encode_type) { case utf_encode_t::UTF_8: offset = 3; break; @@ -277,8 +240,6 @@ inline utf_encode_t detect_encoding_and_skip_bom(std::istream& is) noexcept return encode_type; } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_ENCODINGS_ENCODE_DETECTOR_HPP_ */ diff --git a/include/fkYAML/detail/encodings/uri_encoding.hpp b/include/fkYAML/detail/encodings/uri_encoding.hpp index c53d013c..e3ce4440 100644 --- a/include/fkYAML/detail/encodings/uri_encoding.hpp +++ b/include/fkYAML/detail/encodings/uri_encoding.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -16,39 +16,26 @@ #include -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementaions of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A class which handles URI encodings. -class uri_encoding -{ +class uri_encoding { public: /// @brief Validates the encoding of the given character sequence. /// @param begin An iterator to the first element of the character sequence. /// @param end An iterator to the past-the-end element of the character sequence. /// @return true if all the characters are valid, false otherwise. - static bool validate(std::string::const_iterator begin, std::string::const_iterator end) noexcept - { - if (begin == end) - { + static bool validate(std::string::const_iterator begin, std::string::const_iterator end) noexcept { + if (begin == end) { return true; } std::string::const_iterator current = begin; - for (; current != end; ++current) - { - if (*current == '%') - { + for (; current != end; ++current) { + if (*current == '%') { bool are_valid_octets = validate_octets(++current, end); - if (!are_valid_octets) - { + if (!are_valid_octets) { return false; } @@ -56,8 +43,7 @@ class uri_encoding } bool is_allowed_character = validate_character(*current); - if (!is_allowed_character) - { + if (!is_allowed_character) { return false; } } @@ -70,25 +56,20 @@ class uri_encoding /// @param begin An iterator to the first octet. /// @param end An iterator to the past-the-end element of the whole character sequence. /// @return true if the octets are valid, false otherwise. - static bool validate_octets(std::string::const_iterator& begin, std::string::const_iterator& end) - { - for (int i = 0; i < 2; i++, ++begin) - { - if (begin == end) - { + static bool validate_octets(std::string::const_iterator& begin, std::string::const_iterator& end) { + for (int i = 0; i < 2; i++, ++begin) { + if (begin == end) { return false; } // Normalize a character for a-f/A-F comparison int octet = std::tolower(*begin); - if ('0' <= octet && octet <= '9') - { + if ('0' <= octet && octet <= '9') { continue; } - if ('a' <= octet && octet <= 'f') - { + if ('a' <= octet && octet <= 'f') { continue; } @@ -101,15 +82,13 @@ class uri_encoding /// @brief Verify if the given character is allowed as a URI character. /// @param c The target character. /// @return true if the given character is allowed as a URI character, false otherwise. - static bool validate_character(char c) - { + static bool validate_character(char c) { // Check if the current character is one of reserved/unreserved characters which are allowed for // use. See the following links for details: // * reserved characters: https://datatracker.ietf.org/doc/html/rfc3986#section-2.2 // * unreserved characters: https://datatracker.ietf.org/doc/html/rfc3986#section-2.3 - switch (c) - { + switch (c) { // reserved characters (gen-delims) case ':': case '/': @@ -143,8 +122,6 @@ class uri_encoding } }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_ENCODINGS_URI_ENCODING_HPP_ */ diff --git a/include/fkYAML/detail/encodings/utf8_encoding.hpp b/include/fkYAML/detail/encodings/utf8_encoding.hpp deleted file mode 100644 index f8c15aa2..00000000 --- a/include/fkYAML/detail/encodings/utf8_encoding.hpp +++ /dev/null @@ -1,328 +0,0 @@ -/// _______ __ __ __ _____ __ __ __ -/// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 -/// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML -/// -/// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani -/// SPDX-License-Identifier: MIT -/// -/// @file - -#ifndef FK_YAML_DETAIL_ENCODINGS_UTF_ENCODING_HPP_ -#define FK_YAML_DETAIL_ENCODINGS_UTF_ENCODING_HPP_ - -#include -#include - -#include -#include - -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ - -template -class utf_encoding; - -///////////////////////// -// UTF-8 Encoding /// -///////////////////////// - -/// @brief A class which handles UTF-8 encodings. -class utf8_encoding -{ - using int_type = std::char_traits::int_type; - -public: - /// @brief Validates the encoding of a given byte array whose length is 1. - /// @param[in] byte_array The byte array to be validated. - /// @return true if a given byte array is valid, false otherwise. - static bool validate(std::array byte_array) noexcept - { - // U+0000..U+007F - return (0x00 <= byte_array[0] && byte_array[0] <= 0x7F); - } - - /// @brief Validates the encoding of a given byte array whose length is 2. - /// @param[in] byte_array The byte array to be validated. - /// @return true if a given byte array is valid, false otherwise. - static bool validate(std::array byte_array) noexcept - { - // U+0080..U+07FF - // 1st Byte: 0xC2..0xDF - // 2nd Byte: 0x80..0xBF - if (0xC2 <= byte_array[0] && byte_array[0] <= 0xDF) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0xBF) - { - return true; - } - } - - // The rest of byte combinations are invalid. - return false; - } - - /// @brief Validates the encoding of a given byte array whose length is 3. - /// @param[in] byte_array The byte array to be validated. - /// @return true if a given byte array is valid, false otherwise. - static bool validate(std::array byte_array) noexcept - { - // U+1000..U+CFFF: - // 1st Byte: 0xE0..0xEC - // 2nd Byte: 0x80..0xBF - // 3rd Byte: 0x80..0xBF - if (0xE0 <= byte_array[0] && byte_array[0] <= 0xEC) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0xBF) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - return true; - } - } - return false; - } - - // U+D000..U+D7FF: - // 1st Byte: 0xED - // 2nd Byte: 0x80..0x9F - // 3rd Byte: 0x80..0xBF - if (byte_array[0] == 0xED) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0x9F) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - return true; - } - } - return false; - } - - // U+E000..U+FFFF: - // 1st Byte: 0xEE..0xEF - // 2nd Byte: 0x80..0xBF - // 3rd Byte: 0x80..0xBF - if (byte_array[0] == 0xEE || byte_array[0] == 0xEF) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0xBF) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - return true; - } - } - return false; - } - - // The rest of byte combinations are invalid. - return false; - } - - /// @brief Validates the encoding of a given byte array whose length is 4. - /// @param[in] byte_array The byte array to be validated. - /// @return true if a given byte array is valid, false otherwise. - static bool validate(std::array byte_array) noexcept - { - // U+10000..U+3FFFF: - // 1st Byte: 0xF0 - // 2nd Byte: 0x90..0xBF - // 3rd Byte: 0x80..0xBF - // 4th Byte: 0x80..0xBF - if (byte_array[0] == 0xF0) - { - if (0x90 <= byte_array[1] && byte_array[1] <= 0xBF) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - if (0x80 <= byte_array[3] && byte_array[3] <= 0xBF) - { - return true; - } - } - } - return false; - } - - // U+40000..U+FFFFF: - // 1st Byte: 0xF1..0xF3 - // 2nd Byte: 0x80..0xBF - // 3rd Byte: 0x80..0xBF - // 4th Byte: 0x80..0xBF - if (0xF1 <= byte_array[0] && byte_array[0] <= 0xF3) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0xBF) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - if (0x80 <= byte_array[3] && byte_array[3] <= 0xBF) - { - return true; - } - } - } - return false; - } - - // U+100000..U+10FFFF: - // 1st Byte: 0xF4 - // 2nd Byte: 0x80..0x8F - // 3rd Byte: 0x80..0xBF - // 4th Byte: 0x80..0xBF - if (byte_array[0] == 0xF4) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0x8F) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - if (0x80 <= byte_array[3] && byte_array[3] <= 0xBF) - { - return true; - } - } - } - return false; - } - - // The rest of byte combinations are invalid. - return false; - } - - /// @brief Converts UTF-16 encoded characters to UTF-8 encoded bytes. - /// @param[in] utf16 UTF-16 encoded character(s). - /// @param[out] utf8_bytes UTF-8 encoded bytes. - /// @param[out] consumed_size The number of UTF-16 encoded characters used for the conversion. - /// @param[out] encoded_size The size of UTF-encoded bytes. - static void from_utf16( - std::array utf16, std::array& utf8_bytes, std::size_t& consumed_size, - std::size_t& encoded_size) - { - utf8_bytes.fill(0); - consumed_size = 0; - encoded_size = 0; - bool is_valid = false; - - if (utf16[0] < char16_t(0x80u)) - { - utf8_bytes[0] = static_cast(utf16[0] & 0x7Fu); - consumed_size = 1; - encoded_size = 1; - is_valid = true; - } - else if (utf16[0] <= char16_t(0x7FFu)) - { - uint16_t utf8_encoded = 0xC080u; - utf8_encoded |= static_cast((utf16[0] & 0x07C0u) << 2); - utf8_encoded |= static_cast(utf16[0] & 0x003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF00u) >> 8); - utf8_bytes[1] = static_cast(utf8_encoded & 0x00FFu); - consumed_size = 1; - encoded_size = 2; - is_valid = true; - } - else if (utf16[0] < char16_t(0xD800u) || char16_t(0xE000u) <= utf16[0]) - { - uint32_t utf8_encoded = 0xE08080u; - utf8_encoded |= static_cast((utf16[0] & 0xF000u) << 4); - utf8_encoded |= static_cast((utf16[0] & 0x0FC0u) << 2); - utf8_encoded |= static_cast(utf16[0] & 0x003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF0000u) >> 16); - utf8_bytes[1] = static_cast((utf8_encoded & 0x00FF00u) >> 8); - utf8_bytes[2] = static_cast(utf8_encoded & 0x0000FFu); - consumed_size = 1; - encoded_size = 3; - is_valid = true; - } - else if (utf16[0] <= char16_t(0xDBFFu) && char16_t(0xDC00u) <= utf16[1] && utf16[1] <= char16_t(0xDFFFu)) - { - // for surrogate pairs - uint32_t code_point = 0x10000u + ((utf16[0] & 0x03FFu) << 10) + (utf16[1] & 0x03FFu); - uint32_t utf8_encoded = 0xF0808080u; - utf8_encoded |= static_cast((code_point & 0x1C0000u) << 6); - utf8_encoded |= static_cast((code_point & 0x03F000u) << 4); - utf8_encoded |= static_cast((code_point & 0x000FC0u) << 2); - utf8_encoded |= static_cast(code_point & 0x00003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF000000u) >> 24); - utf8_bytes[1] = static_cast((utf8_encoded & 0x00FF0000u) >> 16); - utf8_bytes[2] = static_cast((utf8_encoded & 0x0000FF00u) >> 8); - utf8_bytes[3] = static_cast(utf8_encoded & 0x000000FFu); - consumed_size = 2; - encoded_size = 4; - is_valid = true; - } - - if (!is_valid) - { - throw invalid_encoding("Invalid UTF-16 encoding detected.", utf16); - } - } - - /// @brief Converts a UTF-32 encoded character to UTF-8 encoded bytes. - /// @param[in] utf32 A UTF-32 encoded character. - /// @param[out] utf8_bytes UTF-8 encoded bytes. - /// @param[in] encoded_size The size of UTF-encoded bytes. - static void from_utf32(const char32_t utf32, std::array& utf8_bytes, std::size_t& encoded_size) - { - utf8_bytes.fill(0); - encoded_size = 0; - bool is_valid = false; - - if (utf32 < char32_t(0x80u)) - { - utf8_bytes[0] = static_cast(utf32 & 0x007F); - encoded_size = 1; - is_valid = true; - } - else if (utf32 <= char32_t(0x7FFu)) - { - uint16_t utf8_encoded = 0xC080u; - utf8_encoded |= static_cast((utf32 & 0x07C0u) << 2); - utf8_encoded |= static_cast(utf32 & 0x003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF00u) >> 8); - utf8_bytes[1] = static_cast(utf8_encoded & 0x00FFu); - encoded_size = 2; - is_valid = true; - } - else if (utf32 <= char32_t(0xFFFFu)) - { - uint32_t utf8_encoded = 0xE08080u; - utf8_encoded |= static_cast((utf32 & 0xF000u) << 4); - utf8_encoded |= static_cast((utf32 & 0x0FC0u) << 2); - utf8_encoded |= static_cast(utf32 & 0x003F); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF0000u) >> 16); - utf8_bytes[1] = static_cast((utf8_encoded & 0x00FF00u) >> 8); - utf8_bytes[2] = static_cast(utf8_encoded & 0x0000FFu); - encoded_size = 3; - is_valid = true; - } - else if (utf32 <= char32_t(0x10FFFFu)) - { - uint32_t utf8_encoded = 0xF0808080u; - utf8_encoded |= static_cast((utf32 & 0x1C0000u) << 6); - utf8_encoded |= static_cast((utf32 & 0x03F000u) << 4); - utf8_encoded |= static_cast((utf32 & 0x000FC0u) << 2); - utf8_encoded |= static_cast(utf32 & 0x00003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF000000u) >> 24); - utf8_bytes[1] = static_cast((utf8_encoded & 0x00FF0000u) >> 16); - utf8_bytes[2] = static_cast((utf8_encoded & 0x0000FF00u) >> 8); - utf8_bytes[3] = static_cast(utf8_encoded & 0x000000FFu); - encoded_size = 4; - is_valid = true; - } - - if (!is_valid) - { - throw invalid_encoding("Invalid UTF-32 encoding detected.", utf32); - } - } -}; - -} // namespace detail - -FK_YAML_NAMESPACE_END - -#endif /* FK_YAML_DETAIL_ENCODINGS_UTF_ENCODING_HPP_ */ diff --git a/include/fkYAML/detail/encodings/utf_encode_t.hpp b/include/fkYAML/detail/encodings/utf_encode_t.hpp index 8abe7508..558c2573 100644 --- a/include/fkYAML/detail/encodings/utf_encode_t.hpp +++ b/include/fkYAML/detail/encodings/utf_encode_t.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -13,17 +13,11 @@ #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of Unicode encoding types /// @note Since fkYAML doesn't treat UTF-16/UTF-32 encoded characters per byte, endians do not matter. -enum class utf_encode_t -{ +enum class utf_encode_t { UTF_8, //!< UTF-8 UTF_16BE, //!< UTF-16 Big Endian UTF_16LE, //!< UTF-16 Little Endian @@ -31,8 +25,6 @@ enum class utf_encode_t UTF_32LE, //!< UTF-32 Little Endian }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_ENCODINGS_UTF_ENCODE_T_HPP_ */ diff --git a/include/fkYAML/detail/encodings/utf_encodings.hpp b/include/fkYAML/detail/encodings/utf_encodings.hpp new file mode 100644 index 00000000..a374b9d8 --- /dev/null +++ b/include/fkYAML/detail/encodings/utf_encodings.hpp @@ -0,0 +1,290 @@ +/// _______ __ __ __ _____ __ __ __ +/// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +/// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +/// +/// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +/// SPDX-License-Identifier: MIT +/// +/// @file + +#ifndef FK_YAML_DETAIL_ENCODINGS_UTF_ENCODINGS_HPP_ +#define FK_YAML_DETAIL_ENCODINGS_UTF_ENCODINGS_HPP_ + +#include +#include + +#include +#include + +FK_YAML_DETAIL_NAMESPACE_BEGIN + +///////////////////////// +// UTF-8 Encoding /// +///////////////////////// + +/// @brief A class which handles UTF-8 encodings. +namespace utf8 { + +/// @brief Query the number of UTF-8 character bytes with the first byte. +/// @param first_byte The first byte of a UTF-8 character. +/// @return The number of UTF-8 character bytes. +inline uint32_t get_num_bytes(uint8_t first_byte) { + // The first byte starts with 0b0XXX'XXXX -> 1-byte character + if (first_byte < 0x80) { + return 1; + } + // The first byte starts with 0b110X'XXXX -> 2-byte character + else if ((first_byte & 0xE0) == 0xC0) { + return 2; + } + // The first byte starts with 0b1110'XXXX -> 3-byte character + else if ((first_byte & 0xF0) == 0xE0) { + return 3; + } + // The first byte starts with 0b1111'0XXX -> 4-byte character + else if ((first_byte & 0xF8) == 0xF0) { + return 4; + } + + // The first byte starts with 0b10XX'XXXX or 0b1111'1XXX -> invalid + throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", {first_byte}); +} + +/// @brief Validates the encoding of a given byte array whose length is 1. +/// @param[in] byte_array The byte array to be validated. +/// @return true if a given byte array is valid, false otherwise. +inline bool validate(const std::initializer_list& byte_array) noexcept { + switch (byte_array.size()) { + case 1: + // U+0000..U+007F + return uint8_t(*(byte_array.begin())) <= uint8_t(0x7Fu); + case 2: { + auto itr = byte_array.begin(); + uint8_t first = *itr++; + uint8_t second = *itr; + + // U+0080..U+07FF + // 1st Byte: 0xC2..0xDF + // 2nd Byte: 0x80..0xBF + if (uint8_t(0xC2u) <= first && first <= uint8_t(0xDFu)) { + if (0x80 <= second && second <= 0xBF) { + return true; + } + } + + // The rest of byte combinations are invalid. + return false; + } + case 3: { + auto itr = byte_array.begin(); + uint8_t first = *itr++; + uint8_t second = *itr++; + uint8_t third = *itr; + + // U+1000..U+CFFF: + // 1st Byte: 0xE0..0xEC + // 2nd Byte: 0x80..0xBF + // 3rd Byte: 0x80..0xBF + if (0xE0 <= first && first <= 0xEC) { + if (0x80 <= second && second <= 0xBF) { + if (0x80 <= third && third <= 0xBF) { + return true; + } + } + return false; + } + + // U+D000..U+D7FF: + // 1st Byte: 0xED + // 2nd Byte: 0x80..0x9F + // 3rd Byte: 0x80..0xBF + if (first == 0xED) { + if (0x80 <= second && second <= 0x9F) { + if (0x80 <= third && third <= 0xBF) { + return true; + } + } + return false; + } + + // U+E000..U+FFFF: + // 1st Byte: 0xEE..0xEF + // 2nd Byte: 0x80..0xBF + // 3rd Byte: 0x80..0xBF + if (first == 0xEE || first == 0xEF) { + if (0x80 <= second && second <= 0xBF) { + if (0x80 <= third && third <= 0xBF) { + return true; + } + } + return false; + } + + // The rest of byte combinations are invalid. + return false; + } + case 4: { + auto itr = byte_array.begin(); + uint8_t first = *itr++; + uint8_t second = *itr++; + uint8_t third = *itr++; + uint8_t fourth = *itr; + + // U+10000..U+3FFFF: + // 1st Byte: 0xF0 + // 2nd Byte: 0x90..0xBF + // 3rd Byte: 0x80..0xBF + // 4th Byte: 0x80..0xBF + if (first == 0xF0) { + if (0x90 <= second && second <= 0xBF) { + if (0x80 <= third && third <= 0xBF) { + if (0x80 <= fourth && fourth <= 0xBF) { + return true; + } + } + } + return false; + } + + // U+40000..U+FFFFF: + // 1st Byte: 0xF1..0xF3 + // 2nd Byte: 0x80..0xBF + // 3rd Byte: 0x80..0xBF + // 4th Byte: 0x80..0xBF + if (0xF1 <= first && first <= 0xF3) { + if (0x80 <= second && second <= 0xBF) { + if (0x80 <= third && third <= 0xBF) { + if (0x80 <= fourth && fourth <= 0xBF) { + return true; + } + } + } + return false; + } + + // U+100000..U+10FFFF: + // 1st Byte: 0xF4 + // 2nd Byte: 0x80..0x8F + // 3rd Byte: 0x80..0xBF + // 4th Byte: 0x80..0xBF + if (first == 0xF4) { + if (0x80 <= second && second <= 0x8F) { + if (0x80 <= third && third <= 0xBF) { + if (0x80 <= fourth && fourth <= 0xBF) { + return true; + } + } + } + return false; + } + + // The rest of byte combinations are invalid. + return false; + } + default: // LCOV_EXCL_LINE + return false; // LCOV_EXCL_LINE + } +} + +/// @brief Converts UTF-16 encoded characters to UTF-8 encoded bytes. +/// @param[in] utf16 UTF-16 encoded character(s). +/// @param[out] utf8 UTF-8 encoded bytes. +/// @param[out] consumed_size The number of UTF-16 encoded characters used for the conversion. +/// @param[out] encoded_size The size of UTF-encoded bytes. +inline void from_utf16( + std::array utf16, std::array& utf8, std::size_t& consumed_size, + std::size_t& encoded_size) { + if (utf16[0] < char16_t(0x80u)) { + utf8[0] = static_cast(utf16[0] & 0x7Fu); + consumed_size = 1; + encoded_size = 1; + return; + } + else if (utf16[0] <= char16_t(0x7FFu)) { + uint16_t utf8_chunk = static_cast(0xC080u) | static_cast((utf16[0] & 0x07C0u) << 2) | + static_cast(utf16[0] & 0x003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF00u) >> 8); + utf8[1] = static_cast(utf8_chunk & 0x00FFu); + consumed_size = 1; + encoded_size = 2; + return; + } + else if (utf16[0] < char16_t(0xD800u) || char16_t(0xE000u) <= utf16[0]) { + uint32_t utf8_chunk = static_cast(0xE08080u) | static_cast((utf16[0] & 0xF000u) << 4) | + static_cast((utf16[0] & 0x0FC0u) << 2) | + static_cast(utf16[0] & 0x003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF0000u) >> 16); + utf8[1] = static_cast((utf8_chunk & 0x00FF00u) >> 8); + utf8[2] = static_cast(utf8_chunk & 0x0000FFu); + consumed_size = 1; + encoded_size = 3; + return; + } + else if (utf16[0] <= char16_t(0xDBFFu) && char16_t(0xDC00u) <= utf16[1] && utf16[1] <= char16_t(0xDFFFu)) { + // for surrogate pairs + uint32_t code_point = 0x10000u + ((utf16[0] & 0x03FFu) << 10) + (utf16[1] & 0x03FFu); + uint32_t utf8_chunk = + static_cast(0xF0808080u) | static_cast((code_point & 0x1C0000u) << 6) | + static_cast((code_point & 0x03F000u) << 4) | + static_cast((code_point & 0x000FC0u) << 2) | static_cast(code_point & 0x00003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF000000u) >> 24); + utf8[1] = static_cast((utf8_chunk & 0x00FF0000u) >> 16); + utf8[2] = static_cast((utf8_chunk & 0x0000FF00u) >> 8); + utf8[3] = static_cast(utf8_chunk & 0x000000FFu); + consumed_size = 2; + encoded_size = 4; + return; + } + + throw invalid_encoding("Invalid UTF-16 encoding detected.", utf16); +} + +/// @brief Converts a UTF-32 encoded character to UTF-8 encoded bytes. +/// @param[in] utf32 A UTF-32 encoded character. +/// @param[out] utf8 UTF-8 encoded bytes. +/// @param[in] encoded_size The size of UTF-encoded bytes. +inline void from_utf32(const char32_t utf32, std::array& utf8, std::size_t& encoded_size) { + if (utf32 < char32_t(0x80u)) { + utf8[0] = static_cast(utf32 & 0x007F); + encoded_size = 1; + return; + } + else if (utf32 <= char32_t(0x7FFu)) { + uint16_t utf8_chunk = static_cast(0xC080u) | static_cast((utf32 & 0x07C0u) << 2) | + static_cast(utf32 & 0x003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF00u) >> 8); + utf8[1] = static_cast(utf8_chunk & 0x00FFu); + encoded_size = 2; + return; + } + else if (utf32 <= char32_t(0xFFFFu)) { + uint32_t utf8_chunk = static_cast(0xE08080u) | static_cast((utf32 & 0xF000u) << 4) | + static_cast((utf32 & 0x0FC0u) << 2) | static_cast(utf32 & 0x003F); + utf8[0] = static_cast((utf8_chunk & 0xFF0000u) >> 16); + utf8[1] = static_cast((utf8_chunk & 0x00FF00u) >> 8); + utf8[2] = static_cast(utf8_chunk & 0x0000FFu); + encoded_size = 3; + return; + } + else if (utf32 <= char32_t(0x10FFFFu)) { + uint32_t utf8_chunk = static_cast(0xF0808080u) | static_cast((utf32 & 0x1C0000u) << 6) | + static_cast((utf32 & 0x03F000u) << 4) | + static_cast((utf32 & 0x000FC0u) << 2) | + static_cast(utf32 & 0x00003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF000000u) >> 24); + utf8[1] = static_cast((utf8_chunk & 0x00FF0000u) >> 16); + utf8[2] = static_cast((utf8_chunk & 0x0000FF00u) >> 8); + utf8[3] = static_cast(utf8_chunk & 0x000000FFu); + encoded_size = 4; + return; + } + + throw invalid_encoding("Invalid UTF-32 encoding detected.", utf32); +} + +} // namespace utf8 + +FK_YAML_DETAIL_NAMESPACE_END + +#endif /* FK_YAML_DETAIL_ENCODINGS_UTF_ENCODINGS_HPP_ */ diff --git a/include/fkYAML/detail/input/deserializer.hpp b/include/fkYAML/detail/input/deserializer.hpp index 62a956b7..63d9f246 100644 --- a/include/fkYAML/detail/input/deserializer.hpp +++ b/include/fkYAML/detail/input/deserializer.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -27,18 +27,12 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A class which provides the feature of deserializing YAML documents. /// @tparam BasicNodeType A type of the container for deserialized YAML values. template -class basic_deserializer -{ +class basic_deserializer { static_assert(is_basic_node::value, "basic_deserializer only accepts basic_node<...>"); /** A type for the target basic_node. */ @@ -58,15 +52,13 @@ class basic_deserializer /** A type for string node values. */ using string_type = typename node_type::string_type; - struct indentation - { + struct indentation { indentation() = default; indentation(std::size_t _line, std::size_t _indent, bool _is_explicit_key) : line(_line), indent(_indent), - is_explicit_key(_is_explicit_key) - { + is_explicit_key(_is_explicit_key) { } std::size_t line {0}; @@ -83,8 +75,7 @@ class basic_deserializer /// @param source A YAML-formatted source string. /// @return node_type A root YAML node deserialized from the source string. template ::value, int> = 0> - node_type deserialize(InputAdapterType&& input_adapter) - { + node_type deserialize(InputAdapterType&& input_adapter) { lexer_type lexer(std::forward(input_adapter)); lexical_token_t type {lexical_token_t::END_OF_BUFFER}; @@ -94,6 +85,16 @@ class basic_deserializer // parse directives first. deserialize_directives(lexer, root, type); + switch (type) { + case lexical_token_t::SEQUENCE_BLOCK_PREFIX: + case lexical_token_t::SEQUENCE_FLOW_BEGIN: + root = node_type::sequence(); + apply_directive_set(root); + break; + default: + break; + } + // parse YAML nodes recursively deserialize_node(lexer, type); @@ -113,26 +114,20 @@ class basic_deserializer /// @param lexer The lexical analyzer to be used. /// @param root The root YAML node. /// @param type The variable to store the last lexical token type. - void deserialize_directives(lexer_type& lexer, node_type& root, lexical_token_t& last_type) - { - for (;;) - { + void deserialize_directives(lexer_type& lexer, node_type& root, lexical_token_t& last_type) { + for (;;) { lexical_token_t type = lexer.get_next_token(); - switch (type) - { + switch (type) { case lexical_token_t::YAML_VER_DIRECTIVE: - if (!mp_directive_set) - { + if (!mp_directive_set) { mp_directive_set = std::shared_ptr(new directive_set()); } - if (!root.mp_directive_set) - { + if (!root.mp_directive_set) { root.mp_directive_set = mp_directive_set; } - if (mp_directive_set->is_version_specified) - { + if (mp_directive_set->is_version_specified) { throw parse_error( "YAML version cannot be specified more than once.", lexer.get_lines_processed(), @@ -143,22 +138,18 @@ class basic_deserializer mp_directive_set->is_version_specified = true; break; case lexical_token_t::TAG_DIRECTIVE: { - if (!mp_directive_set) - { + if (!mp_directive_set) { mp_directive_set = std::shared_ptr(new directive_set()); } - if (!root.mp_directive_set) - { + if (!root.mp_directive_set) { root.mp_directive_set = mp_directive_set; } const std::string& tag_handle = lexer.get_tag_handle(); - switch (tag_handle.size()) - { + switch (tag_handle.size()) { case 1: { bool is_already_specified = !mp_directive_set->primary_handle_prefix.empty(); - if (is_already_specified) - { + if (is_already_specified) { throw parse_error( "Primary handle cannot be specified more than once.", lexer.get_lines_processed(), @@ -169,8 +160,7 @@ class basic_deserializer } case 2: { bool is_already_specified = !mp_directive_set->secondary_handle_prefix.empty(); - if (is_already_specified) - { + if (is_already_specified) { throw parse_error( "Secondary handle cannot be specified more than once.", lexer.get_lines_processed(), @@ -182,8 +172,7 @@ class basic_deserializer default: { bool is_already_specified = !(mp_directive_set->named_handle_map.emplace(tag_handle, lexer.get_tag_prefix()).second); - if (is_already_specified) - { + if (is_already_specified) { throw parse_error( "The same named handle cannot be specified more than once.", lexer.get_lines_processed(), @@ -211,31 +200,26 @@ class basic_deserializer /// @brief Deserializes the YAML nodes recursively. /// @param lexer The lexical analyzer to be used. /// @param first_type The first lexical token type. - void deserialize_node(lexer_type& lexer, lexical_token_t first_type) - { + void deserialize_node(lexer_type& lexer, lexical_token_t first_type) { lexical_token_t type = first_type; std::size_t line = lexer.get_lines_processed(); std::size_t indent = lexer.get_last_token_begin_pos(); - do - { - switch (type) - { + do { + switch (type) { case lexical_token_t::END_OF_BUFFER: // This handles an empty input. break; case lexical_token_t::EXPLICIT_KEY_PREFIX: { bool needs_to_move_back = !m_indent_stack.empty() && indent < m_indent_stack.back().indent; - if (needs_to_move_back) - { + if (needs_to_move_back) { auto target_itr = std::find_if( // LCOV_EXCL_LINE m_indent_stack.rbegin(), m_indent_stack.rend(), [indent](const indentation& i) { return indent > i.indent; }); auto pop_num = std::distance(m_indent_stack.rbegin(), target_itr); - for (auto i = 0; i < pop_num; i++) - { + for (auto i = 0; i < pop_num; i++) { // move back to the previous container node. mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); @@ -243,8 +227,7 @@ class basic_deserializer } } - if (mp_current_node->is_null()) - { + if (mp_current_node->is_null()) { *mp_current_node = node_type::mapping(); } @@ -252,8 +235,7 @@ class basic_deserializer m_indent_stack.emplace_back(line, indent, true); type = lexer.get_next_token(); - if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) - { + if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) { m_indent_stack.emplace_back(lexer.get_lines_processed(), lexer.get_last_token_begin_pos(), false); mp_current_node = new node_type(node_t::SEQUENCE); apply_directive_set(*mp_current_node); @@ -269,8 +251,7 @@ class basic_deserializer } case lexical_token_t::KEY_SEPARATOR: { bool is_stack_empty = m_node_stack.empty(); - if (is_stack_empty) - { + if (is_stack_empty) { throw parse_error("A key separator found without key.", line, indent); } @@ -283,8 +264,7 @@ class basic_deserializer indent = lexer.get_last_token_begin_pos(); bool found_props = deserialize_node_properties(lexer, type, line, indent); - if (found_props && line == lexer.get_lines_processed()) - { + if (found_props && line == lexer.get_lines_processed()) { // defer applying node properties for the subsequent node on the same line. continue; } @@ -294,19 +274,15 @@ class basic_deserializer bool is_implicit_same_line = (line == old_line) && (m_indent_stack.empty() || old_indent > m_indent_stack.back().indent); - if (is_implicit_same_line) - { + if (is_implicit_same_line) { // a key separator for an implicit key with its value on the same line. continue; } - if (line > old_line) - { - if (m_needs_tag_impl) - { + if (line > old_line) { + if (m_needs_tag_impl) { tag_t tag_type = tag_resolver::resolve_tag(m_tag_name, mp_directive_set); - if (tag_type == tag_t::MAPPING) - { + if (tag_type == tag_t::MAPPING) { // set YAML node properties here to distinguish them from those for the first key node // as shown in the following snippet: // @@ -323,8 +299,7 @@ class basic_deserializer } } - switch (type) - { + switch (type) { case lexical_token_t::SEQUENCE_BLOCK_PREFIX: // a key separator preceeding block sequence entries *mp_current_node = node_type::sequence(); @@ -356,20 +331,17 @@ class basic_deserializer // handle explicit mapping key separators. - while (!m_indent_stack.back().is_explicit_key) - { + while (!m_indent_stack.back().is_explicit_key) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); m_indent_stack.pop_back(); } - if (m_node_stack.back()->is_sequence()) - { + if (m_node_stack.back()->is_sequence()) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); } - if (m_node_stack.back() == mp_current_node) - { + if (m_node_stack.back() == mp_current_node) { // This path is for nested explicit mapping keys like: // ```yaml // ? ? foo @@ -387,8 +359,7 @@ class basic_deserializer m_node_stack.push_back(m_node_stack.back()); m_indent_stack.back().is_explicit_key = false; - if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) - { + if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) { *mp_current_node = node_type::sequence(); apply_directive_set(*mp_current_node); apply_node_properties(*mp_current_node); @@ -398,6 +369,7 @@ class basic_deserializer continue; } case lexical_token_t::VALUE_SEPARATOR: + FK_YAML_ASSERT(m_flow_context_depth > 0); break; // just ignore directives case lexical_token_t::YAML_VER_DIRECTIVE: @@ -416,18 +388,15 @@ class basic_deserializer continue; case lexical_token_t::SEQUENCE_BLOCK_PREFIX: - if (mp_current_node->is_sequence()) - { + if (mp_current_node->is_sequence()) { bool is_empty = mp_current_node->empty(); - if (is_empty) - { + if (is_empty) { m_indent_stack.emplace_back(line, indent, false); break; } // move back to the previous sequence if necessary. - while (!mp_current_node->is_sequence() || indent != m_indent_stack.back().indent) - { + while (!mp_current_node->is_sequence() || indent != m_indent_stack.back().indent) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); m_indent_stack.pop_back(); @@ -435,15 +404,8 @@ class basic_deserializer break; } - // if the current node is a mapping. - if (m_node_stack.empty()) - { - throw parse_error("Invalid sequence block prefix(- ) found.", line, indent); - } - // move back to the previous sequence if necessary. - while (!mp_current_node->is_sequence() || indent != m_indent_stack.back().indent) - { + while (!mp_current_node->is_sequence() || indent != m_indent_stack.back().indent) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); m_indent_stack.pop_back(); @@ -461,23 +423,32 @@ class basic_deserializer apply_directive_set(*mp_current_node); apply_node_properties(*mp_current_node); break; - case lexical_token_t::SEQUENCE_FLOW_END: + case lexical_token_t::SEQUENCE_FLOW_END: { FK_YAML_ASSERT(m_flow_context_depth > 0); --m_flow_context_depth; - mp_current_node = m_node_stack.back(); - m_node_stack.pop_back(); + bool is_stack_empty = m_node_stack.empty(); + if (!is_stack_empty) { + mp_current_node = m_node_stack.back(); + m_node_stack.pop_back(); + } break; + } case lexical_token_t::MAPPING_FLOW_BEGIN: ++m_flow_context_depth; *mp_current_node = node_type::mapping(); apply_directive_set(*mp_current_node); apply_node_properties(*mp_current_node); break; - case lexical_token_t::MAPPING_FLOW_END: + case lexical_token_t::MAPPING_FLOW_END: { FK_YAML_ASSERT(m_flow_context_depth > 0); --m_flow_context_depth; - mp_current_node = m_node_stack.back(); + bool is_stack_empty = m_node_stack.empty(); + if (!is_stack_empty) { + mp_current_node = m_node_stack.back(); + m_node_stack.pop_back(); + } break; + } case lexical_token_t::ALIAS_PREFIX: case lexical_token_t::NULL_VALUE: case lexical_token_t::BOOLEAN_VALUE: @@ -485,8 +456,7 @@ class basic_deserializer case lexical_token_t::FLOAT_NUMBER_VALUE: case lexical_token_t::STRING_VALUE: { bool do_continue = deserialize_scalar(lexer, indent, line, type); - if (do_continue) - { + if (do_continue) { continue; } break; @@ -498,9 +468,8 @@ class basic_deserializer break; } - lexical_token_t prev_type = type; type = lexer.get_next_token(); - indent = (prev_type == lexical_token_t::ANCHOR_PREFIX) ? indent : lexer.get_last_token_begin_pos(); + indent = lexer.get_last_token_begin_pos(); line = lexer.get_lines_processed(); } while (type != lexical_token_t::END_OF_BUFFER); } @@ -512,25 +481,20 @@ class basic_deserializer /// @param indent The variable to store the indent of either the first property or the last non-property token. /// @return true if any property is found, false otherwise. bool deserialize_node_properties( - lexer_type& lexer, lexical_token_t& last_type, std::size_t& line, std::size_t& indent) - { + lexer_type& lexer, lexical_token_t& last_type, std::size_t& line, std::size_t& indent) { std::set prop_types {lexical_token_t::ANCHOR_PREFIX, lexical_token_t::TAG_PREFIX}; lexical_token_t type = last_type; bool ends_loop {false}; - do - { - if (line < lexer.get_lines_processed()) - { + do { + if (line < lexer.get_lines_processed()) { break; } - switch (type) - { + switch (type) { case lexical_token_t::ANCHOR_PREFIX: { bool already_specified = prop_types.find(type) == prop_types.end(); - if (already_specified) - { + if (already_specified) { throw parse_error( "anchor name cannot be specified more than once to the same node.", lexer.get_lines_processed(), @@ -541,8 +505,7 @@ class basic_deserializer m_anchor_name = lexer.get_string(); m_needs_anchor_impl = true; - if (prop_types.size() == 1) - { + if (prop_types.size() == 1) { line = lexer.get_lines_processed(); indent = lexer.get_last_token_begin_pos(); } @@ -551,8 +514,7 @@ class basic_deserializer } case lexical_token_t::TAG_PREFIX: { bool already_specified = prop_types.find(type) == prop_types.end(); - if (already_specified) - { + if (already_specified) { throw parse_error( "tag name cannot be specified more than once to the same node.", lexer.get_lines_processed(), @@ -563,8 +525,7 @@ class basic_deserializer m_tag_name = lexer.get_string(); m_needs_tag_impl = true; - if (prop_types.size() == 1) - { + if (prop_types.size() == 1) { line = lexer.get_lines_processed(); indent = lexer.get_last_token_begin_pos(); } @@ -576,15 +537,13 @@ class basic_deserializer break; } - if (!ends_loop) - { + if (!ends_loop) { type = lexer.get_next_token(); } } while (!ends_loop); last_type = type; - if (prop_types.size() == 2) - { + if (prop_types.size() == 2) { line = lexer.get_lines_processed(); indent = lexer.get_last_token_begin_pos(); } @@ -594,23 +553,19 @@ class basic_deserializer /// @brief Add new key string to the current YAML node. /// @param key a key string to be added to the current YAML node. - void add_new_key(node_type&& key, const std::size_t indent, const std::size_t line) - { - if (!m_indent_stack.empty() && indent < m_indent_stack.back().indent) - { + void add_new_key(node_type&& key, const std::size_t indent, const std::size_t line) { + if (!m_indent_stack.empty() && indent < m_indent_stack.back().indent) { auto target_itr = std::find_if(m_indent_stack.rbegin(), m_indent_stack.rend(), [indent](const indentation& i) { return indent == i.indent; }); bool is_indent_valid = (target_itr != m_indent_stack.rend()); - if (!is_indent_valid) - { + if (!is_indent_valid) { throw parse_error("Detected invalid indentaion.", line, indent); } auto pop_num = std::distance(m_indent_stack.rbegin(), target_itr); - for (auto i = 0; i < pop_num; i++) - { + for (auto i = 0; i < pop_num; i++) { // move back to the previous container node. mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); @@ -618,8 +573,7 @@ class basic_deserializer } } - if (mp_current_node->is_sequence()) - { + if (mp_current_node->is_sequence()) { mp_current_node->template get_value_ref().emplace_back(node_type::mapping()); m_node_stack.push_back(mp_current_node); mp_current_node = &(mp_current_node->operator[](mp_current_node->size() - 1)); @@ -627,16 +581,15 @@ class basic_deserializer mapping_type& map = mp_current_node->template get_value_ref(); bool is_empty = map.empty(); - if (is_empty) - { - m_indent_stack.emplace_back(line, indent, false); + if (is_empty) { + if (m_flow_context_depth == 0) { + m_indent_stack.emplace_back(line, indent, false); + } } - else - { + else { // check key duplication in the current mapping if not empty. auto itr = map.find(key); - if (itr != map.end()) - { + if (itr != map.end()) { throw parse_error("Detected duplication in mapping keys.", line, indent); } } @@ -648,18 +601,15 @@ class basic_deserializer /// @brief Assign node value to the current node. /// @param node_value A rvalue node_type object to be assigned to the current node. - void assign_node_value(node_type&& node_value) noexcept - { - if (mp_current_node->is_sequence()) - { + void assign_node_value(node_type&& node_value) noexcept { + if (mp_current_node->is_sequence()) { mp_current_node->template get_value_ref().emplace_back(std::move(node_value)); return; } // a scalar node *mp_current_node = std::move(node_value); - if (!m_indent_stack.back().is_explicit_key) - { + if (m_flow_context_depth > 0 || !m_indent_stack.back().is_explicit_key) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); } @@ -671,17 +621,14 @@ class basic_deserializer /// @param indent The last indent size. /// @param line The last line. /// @return The created YAML scalar node. - node_type create_scalar_node(lexer_type& lexer, lexical_token_t type, std::size_t indent, std::size_t line) - { + node_type create_scalar_node(lexer_type& lexer, lexical_token_t type, std::size_t indent, std::size_t line) { FK_YAML_ASSERT( type == lexical_token_t::NULL_VALUE || type == lexical_token_t::BOOLEAN_VALUE || type == lexical_token_t::INTEGER_VALUE || type == lexical_token_t::FLOAT_NUMBER_VALUE || type == lexical_token_t::STRING_VALUE || type == lexical_token_t::ALIAS_PREFIX); - if (m_needs_tag_impl) - { - if (type == lexical_token_t::ALIAS_PREFIX) - { + if (m_needs_tag_impl) { + if (type == lexical_token_t::ALIAS_PREFIX) { throw parse_error("Tag cannot be specified to alias nodes", line, indent); } @@ -689,8 +636,7 @@ class basic_deserializer FK_YAML_ASSERT(tag_type != tag_t::SEQUENCE && tag_type != tag_t::MAPPING); - switch (tag_type) - { + switch (tag_type) { case tag_t::NULL_VALUE: type = lexical_token_t::NULL_VALUE; break; @@ -718,8 +664,7 @@ class basic_deserializer } node_type node {}; - switch (type) - { + switch (type) { case lexical_token_t::NULL_VALUE: node = node_type(lexer.get_null()); break; @@ -738,8 +683,7 @@ class basic_deserializer case lexical_token_t::ALIAS_PREFIX: { const string_type& alias_name = lexer.get_string(); auto itr = m_anchor_table.find(alias_name); - if (itr == m_anchor_table.end()) - { + if (itr == m_anchor_table.end()) { throw parse_error("The given anchor name must appear prior to the alias node.", line, indent); } node = node_type::alias_of(m_anchor_table[alias_name]); @@ -760,27 +704,21 @@ class basic_deserializer /// @param indent The current indentation width. Can be updated in this function. /// @param line The number of processed lines. Can be updated in this function. /// @return true if next token has already been got, false otherwise. - bool deserialize_scalar(lexer_type& lexer, std::size_t& indent, std::size_t& line, lexical_token_t& type) - { + bool deserialize_scalar(lexer_type& lexer, std::size_t& indent, std::size_t& line, lexical_token_t& type) { node_type node = create_scalar_node(lexer, type, indent, line); - if (mp_current_node->is_mapping()) - { + if (mp_current_node->is_mapping()) { add_new_key(std::move(node), indent, line); return false; } type = lexer.get_next_token(); - if (type == lexical_token_t::KEY_SEPARATOR) - { - if (mp_current_node->is_scalar()) - { - if (line != lexer.get_lines_processed()) - { + if (type == lexical_token_t::KEY_SEPARATOR) { + if (mp_current_node->is_scalar()) { + if (line != lexer.get_lines_processed()) { // This path is for explicit mapping key separator(:) assign_node_value(std::move(node)); - if (!m_indent_stack.back().is_explicit_key) - { + if (!m_indent_stack.back().is_explicit_key) { m_indent_stack.pop_back(); } indent = lexer.get_last_token_begin_pos(); @@ -789,8 +727,7 @@ class basic_deserializer } indentation& last_indent = m_indent_stack.back(); - if (last_indent.line == line && !last_indent.is_explicit_key) - { + if (last_indent.line == line && !last_indent.is_explicit_key) { throw parse_error("multiple mapping keys are specified on the same line.", line, indent); } @@ -799,8 +736,7 @@ class basic_deserializer } add_new_key(std::move(node), indent, line); } - else - { + else { assign_node_value(std::move(node)); } indent = lexer.get_last_token_begin_pos(); @@ -810,28 +746,23 @@ class basic_deserializer /// @brief Set YAML directive properties to the given node. /// @param node A node_type object to be set YAML directive properties. - void apply_directive_set(node_type& node) noexcept - { - if (mp_directive_set) - { + void apply_directive_set(node_type& node) noexcept { + if (mp_directive_set) { node.mp_directive_set = mp_directive_set; } } /// @brief Set YAML node properties (anchor and/or tag names) to the given node. /// @param node A node type object to be set YAML node properties. - void apply_node_properties(node_type& node) - { - if (m_needs_anchor_impl) - { + void apply_node_properties(node_type& node) { + if (m_needs_anchor_impl) { node.add_anchor_name(m_anchor_name); m_anchor_table[m_anchor_name] = node; m_needs_anchor_impl = false; m_anchor_name.clear(); } - if (m_needs_tag_impl) - { + if (m_needs_tag_impl) { node.add_tag_name(m_tag_name); m_needs_tag_impl = false; m_tag_name.clear(); @@ -840,8 +771,7 @@ class basic_deserializer /// @brief Update the target YAML version with an input string. /// @param version_str A YAML version string. - yaml_version_t convert_yaml_version(const string_type& version_str) noexcept - { + yaml_version_t convert_yaml_version(const string_type& version_str) noexcept { return (version_str == "1.1") ? yaml_version_t::VER_1_1 : yaml_version_t::VER_1_2; } @@ -868,8 +798,6 @@ class basic_deserializer string_type m_tag_name {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_DESERIALIZER_HPP_ */ diff --git a/include/fkYAML/detail/input/input_adapter.hpp b/include/fkYAML/detail/input/input_adapter.hpp index 995080d6..843ff1e3 100644 --- a/include/fkYAML/detail/input/input_adapter.hpp +++ b/include/fkYAML/detail/input/input_adapter.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -22,16 +22,11 @@ #include #include #include -#include +#include #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /////////////////////// // input_adapter // @@ -44,8 +39,8 @@ class iterator_input_adapter; /// @tparam IterType An iterator type. template class iterator_input_adapter< - IterType, enable_if_t::value_type>, char>::value>> -{ + IterType, + enable_if_t::value_type>, char>::value>> { public: /// @brief Construct a new iterator_input_adapter object. iterator_input_adapter() = default; @@ -57,8 +52,7 @@ class iterator_input_adapter< iterator_input_adapter(IterType begin, IterType end, utf_encode_t encode_type) noexcept : m_current(begin), m_end(end), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { } // allow only move construct/assignment like other input adapters. @@ -70,12 +64,10 @@ class iterator_input_adapter< /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { + void fill_buffer(std::string& buffer) { buffer.clear(); - switch (m_encode_type) - { + switch (m_encode_type) { case utf_encode_t::UTF_8: fill_buffer_utf8(buffer); break; @@ -93,52 +85,43 @@ class iterator_input_adapter< private: /// @brief The concrete implementation of fill_buffer() for UTF-8 encoded inputs. /// @param buffer A buffer to be filled with the input. - void fill_buffer_utf8(std::string& buffer) - { + void fill_buffer_utf8(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_8); IterType current = m_current; - while (current != m_end) - { - char first = *current++; - - // The first byte starts with 0b0XXX'XXXX -> 1-byte character - if ((first & 0xC0) == 0x80) - { - // The first byte must not start with 0b10XX'XXXX - std::array bytes {{first}}; - throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); - } - // The first byte starts with 0b110X'XXXX -> 2-byte character - else if ((first & 0xE0) == 0xC0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + while (current != m_end) { + uint8_t first = uint8_t(*current++); + uint32_t num_bytes = utf8::get_num_bytes(first); + + switch (num_bytes) { + case 2: { + std::initializer_list bytes {first, uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0b1110'XXXX -> 3-byte character - else if ((first & 0xF0) == 0xE0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 3: { + std::initializer_list bytes {first, uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0x1111'0XXX -> 4-byte character - else if ((first & 0xF8) == 0xF0) - { - std::array bytes { - {uint8_t(first), uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 4: { + std::initializer_list bytes { + first, uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; + } + case 1: + default: + break; } } @@ -147,13 +130,11 @@ class iterator_input_adapter< /// @brief The concrete implementation of get_character() for UTF-16 encoded inputs. /// @param buffer A buffer to be filled with the input. - void fill_buffer_utf16(std::string& buffer) - { + void fill_buffer_utf16(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_16BE || m_encode_type == utf_encode_t::UTF_16LE); int shift_bits[2] {0, 0}; - if (m_encode_type == utf_encode_t::UTF_16BE) - { + if (m_encode_type == utf_encode_t::UTF_16BE) { shift_bits[0] = 8; } else // m_encode_type == utf_encode_t::UTF_16LE @@ -163,40 +144,35 @@ class iterator_input_adapter< std::array encoded_buffer {{0, 0}}; std::size_t encoded_buf_size {0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (m_current != m_end || encoded_buf_size != 0) - { - while (m_current != m_end && encoded_buf_size < 2) - { - encoded_buffer[encoded_buf_size] = char16_t(uint8_t(*m_current++) << shift_bits[0]); - encoded_buffer[encoded_buf_size++] |= char16_t(uint8_t(*m_current++) << shift_bits[1]); + while (m_current != m_end || encoded_buf_size != 0) { + while (m_current != m_end && encoded_buf_size < 2) { + encoded_buffer[encoded_buf_size] = static_cast(uint8_t(*m_current++) << shift_bits[0]); + encoded_buffer[encoded_buf_size++] |= static_cast(uint8_t(*m_current++) << shift_bits[1]); } std::size_t consumed_size = 0; - utf8_encoding::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); + utf8::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); - if (consumed_size == 1) - { + if (consumed_size == 1) { encoded_buffer[0] = encoded_buffer[1]; encoded_buffer[1] = 0; } encoded_buf_size -= consumed_size; - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } /// @brief The concrete implementation of get_character() for UTF-32 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf32(std::string& buffer) - { + void fill_buffer_utf32(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_32BE || m_encode_type == utf_encode_t::UTF_32LE); int shift_bits[4] {0, 0, 0, 0}; - if (m_encode_type == utf_encode_t::UTF_32BE) - { + if (m_encode_type == utf_encode_t::UTF_32BE) { shift_bits[0] = 24; shift_bits[1] = 16; shift_bits[2] = 8; @@ -208,19 +184,18 @@ class iterator_input_adapter< shift_bits[3] = 24; } - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (m_current != m_end) - { - char32_t utf32 = char32_t(*m_current++ << shift_bits[0]); - utf32 |= char32_t(*m_current++ << shift_bits[1]); - utf32 |= char32_t(*m_current++ << shift_bits[2]); - utf32 |= char32_t(*m_current++ << shift_bits[3]); + while (m_current != m_end) { + char32_t utf32 = static_cast(*m_current++ << shift_bits[0]); + utf32 |= static_cast(*m_current++ << shift_bits[1]); + utf32 |= static_cast(*m_current++ << shift_bits[2]); + utf32 |= static_cast(*m_current++ << shift_bits[3]); - utf8_encoding::from_utf32(utf32, utf8_buffer, utf8_buf_size); + utf8::from_utf32(utf32, utf8_buffer, utf8_buf_size); - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } @@ -240,8 +215,7 @@ class iterator_input_adapter< template class iterator_input_adapter< IterType, - enable_if_t::value_type>, char8_t>::value>> -{ + enable_if_t::value_type>, char8_t>::value>> { public: /// @brief Construct a new iterator_input_adapter object. iterator_input_adapter() = default; @@ -253,8 +227,7 @@ class iterator_input_adapter< iterator_input_adapter(IterType begin, IterType end, utf_encode_t encode_type) noexcept : m_current(begin), m_end(end), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { // char8_t characters must be encoded in the UTF-8 format. // See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0482r6.html. FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_8); @@ -269,55 +242,45 @@ class iterator_input_adapter< /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { + void fill_buffer(std::string& buffer) { IterType current = m_current; - while (current != m_end) - { - char first = *current++; - - // The first byte starts with 0b0XXX'XXXX -> 1-byte character - if ((first & 0xC0) == 0x80) - { - // The first byte must not start with 0b10XX'XXXX - std::array bytes {{first}}; - throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); - } - // The first byte starts with 0b110X'XXXX -> 2-byte character - else if ((first & 0xE0) == 0xC0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + while (current != m_end) { + uint8_t first = static_cast(*current++); + uint32_t num_bytes = utf8::get_num_bytes(first); + + switch (num_bytes) { + case 2: { + std::initializer_list bytes {first, uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0b1110'XXXX -> 3-byte character - else if ((first & 0xF0) == 0xE0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 3: { + std::initializer_list bytes {first, uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0x1111'0XXX -> 4-byte character - else if ((first & 0xF8) == 0xF0) - { - std::array bytes { - {uint8_t(first), uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 4: { + std::initializer_list bytes { + first, uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; + } + case 1: + default: + break; } } - while (m_current != m_end) - { + while (m_current != m_end) { buffer.push_back(char(*m_current++)); } } @@ -338,8 +301,7 @@ class iterator_input_adapter< template class iterator_input_adapter< IterType, - enable_if_t::value_type>, char16_t>::value>> -{ + enable_if_t::value_type>, char16_t>::value>> { public: /// @brief Construct a new iterator_input_adapter object. iterator_input_adapter() = default; @@ -351,8 +313,7 @@ class iterator_input_adapter< iterator_input_adapter(IterType begin, IterType end, utf_encode_t encode_type) noexcept : m_current(begin), m_end(end), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_16BE || m_encode_type == utf_encode_t::UTF_16LE); } @@ -365,35 +326,32 @@ class iterator_input_adapter< /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { + void fill_buffer(std::string& buffer) { int shift_bits = (m_encode_type == utf_encode_t::UTF_16BE) ? 0 : 8; std::array encoded_buffer {{0, 0}}; std::size_t encoded_buf_size {0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (m_current != m_end || encoded_buf_size != 0) - { - while (m_current != m_end && encoded_buf_size < 2) - { + while (m_current != m_end || encoded_buf_size != 0) { + while (m_current != m_end && encoded_buf_size < 2) { char16_t tmp = *m_current++; - encoded_buffer[encoded_buf_size] = char16_t((tmp & 0x00FFu) << shift_bits); - encoded_buffer[encoded_buf_size++] |= char16_t((tmp & 0xFF00u) >> shift_bits); + encoded_buffer[encoded_buf_size++] = char16_t( + static_cast((tmp & 0x00FFu) << shift_bits) | + static_cast((tmp & 0xFF00u) >> shift_bits)); } std::size_t consumed_size = 0; - utf8_encoding::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); + utf8::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); - if (consumed_size == 1) - { + if (consumed_size == 1) { encoded_buffer[0] = encoded_buffer[1]; encoded_buffer[1] = 0; } encoded_buf_size -= consumed_size; - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } @@ -411,8 +369,7 @@ class iterator_input_adapter< template class iterator_input_adapter< IterType, - enable_if_t::value_type>, char32_t>::value>> -{ + enable_if_t::value_type>, char32_t>::value>> { public: /// @brief Construct a new iterator_input_adapter object. iterator_input_adapter() = default; @@ -424,8 +381,7 @@ class iterator_input_adapter< iterator_input_adapter(IterType begin, IterType end, utf_encode_t encode_type) noexcept : m_current(begin), m_end(end), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_32BE || m_encode_type == utf_encode_t::UTF_32LE); } @@ -438,31 +394,29 @@ class iterator_input_adapter< /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { + void fill_buffer(std::string& buffer) { int shift_bits[4] {0, 0, 0, 0}; - if (m_encode_type == utf_encode_t::UTF_32LE) - { + if (m_encode_type == utf_encode_t::UTF_32LE) { shift_bits[0] = 24; shift_bits[1] = 8; shift_bits[2] = 8; shift_bits[3] = 24; } - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (m_current != m_end) - { + while (m_current != m_end) { char32_t tmp = *m_current++; - char32_t utf32 = char32_t((tmp & 0xFF000000u) >> shift_bits[0]); - utf32 |= char32_t((tmp & 0x00FF0000u) >> shift_bits[1]); - utf32 |= char32_t((tmp & 0x0000FF00u) << shift_bits[2]); - utf32 |= char32_t((tmp & 0x000000FFu) << shift_bits[3]); + char32_t utf32 = char32_t( + static_cast((tmp & 0xFF000000u) >> shift_bits[0]) | + static_cast((tmp & 0x00FF0000u) >> shift_bits[1]) | + static_cast((tmp & 0x0000FF00u) << shift_bits[2]) | + static_cast((tmp & 0x000000FFu) << shift_bits[3])); - utf8_encoding::from_utf32(utf32, utf8_buffer, utf8_buf_size); + utf8::from_utf32(utf32, utf8_buffer, utf8_buf_size); - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } @@ -476,8 +430,7 @@ class iterator_input_adapter< }; /// @brief An input adapter for C-style file handles. -class file_input_adapter -{ +class file_input_adapter { public: /// @brief Construct a new file_input_adapter object. file_input_adapter() = default; @@ -490,8 +443,7 @@ class file_input_adapter /// @param encode_type The encoding type for this input adapter. explicit file_input_adapter(std::FILE* file, utf_encode_t encode_type) noexcept : m_file(file), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { } // allow only move construct/assignment @@ -503,10 +455,8 @@ class file_input_adapter /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { - switch (m_encode_type) - { + void fill_buffer(std::string& buffer) { + switch (m_encode_type) { case utf_encode_t::UTF_8: fill_buffer_utf8(buffer); break; @@ -524,73 +474,61 @@ class file_input_adapter private: /// @brief The concrete implementation of get_character() for UTF-8 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf8(std::string& buffer) - { + void fill_buffer_utf8(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_8); char tmp_buf[256] {}; std::size_t read_size = 0; - while ((read_size = std::fread(&tmp_buf[0], sizeof(char), sizeof(tmp_buf) / sizeof(tmp_buf[0]), m_file)) > 0) - { + while ((read_size = std::fread(&tmp_buf[0], sizeof(char), sizeof(tmp_buf) / sizeof(tmp_buf[0]), m_file)) > 0) { buffer.append(tmp_buf, read_size); } auto current = buffer.begin(); auto end = buffer.end(); - while (current != end) - { - char first = *current++; - - // The first byte starts with 0b0XXX'XXXX -> 1-byte character - if ((first & 0xC0) == 0x80) - { - // The first byte must not start with 0b10XX'XXXX - std::array bytes {{first}}; - throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); - } - // The first byte starts with 0b110X'XXXX -> 2-byte character - else if ((first & 0xE0) == 0xC0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + while (current != end) { + uint8_t first = static_cast(*current++); + uint32_t num_bytes = utf8::get_num_bytes(first); + + switch (num_bytes) { + case 2: { + std::initializer_list bytes {first, uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0b1110'XXXX -> 3-byte character - else if ((first & 0xF0) == 0xE0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 3: { + std::initializer_list bytes {first, uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0x1111'0XXX -> 4-byte character - else if ((first & 0xF8) == 0xF0) - { - std::array bytes { - {uint8_t(first), uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 4: { + std::initializer_list bytes { + first, uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; + } + case 1: + default: + break; } } } /// @brief The concrete implementation of get_character() for UTF-16 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf16(std::string& buffer) - { + void fill_buffer_utf16(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_16BE || m_encode_type == utf_encode_t::UTF_16LE); int shift_bits[2] {0, 0}; - if (m_encode_type == utf_encode_t::UTF_16BE) - { + if (m_encode_type == utf_encode_t::UTF_16BE) { shift_bits[0] = 8; } else // m_encode_type == utf_encode_t::UTF_16LE @@ -601,40 +539,36 @@ class file_input_adapter char chars[2] = {0, 0}; std::array encoded_buffer {{0, 0}}; std::size_t encoded_buf_size {0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (std::feof(m_file) == 0) - { - while (encoded_buf_size < 2 && std::fread(&chars[0], sizeof(char), 2, m_file) == 2) - { - encoded_buffer[encoded_buf_size] = char16_t(uint8_t(chars[0]) << shift_bits[0]); - encoded_buffer[encoded_buf_size++] |= char16_t(uint8_t(chars[1]) << shift_bits[1]); + while (std::feof(m_file) == 0) { + while (encoded_buf_size < 2 && std::fread(&chars[0], sizeof(char), 2, m_file) == 2) { + encoded_buffer[encoded_buf_size++] = char16_t( + static_cast(uint8_t(chars[0]) << shift_bits[0]) | + static_cast(uint8_t(chars[1]) << shift_bits[1])); } std::size_t consumed_size = 0; - utf8_encoding::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); + utf8::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); - if (consumed_size == 1) - { + if (consumed_size == 1) { encoded_buffer[0] = encoded_buffer[1]; encoded_buffer[1] = 0; } encoded_buf_size -= consumed_size; - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } /// @brief The concrete implementation of get_character() for UTF-32 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf32(std::string& buffer) - { + void fill_buffer_utf32(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_32BE || m_encode_type == utf_encode_t::UTF_32LE); int shift_bits[4] {0, 0, 0, 0}; - if (m_encode_type == utf_encode_t::UTF_32BE) - { + if (m_encode_type == utf_encode_t::UTF_32BE) { shift_bits[0] = 24; shift_bits[1] = 16; shift_bits[2] = 8; @@ -647,25 +581,24 @@ class file_input_adapter } char chars[4] = {0, 0, 0, 0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (std::feof(m_file) == 0) - { + while (std::feof(m_file) == 0) { std::size_t size = std::fread(&chars[0], sizeof(char), 4, m_file); - if (size != 4) - { + if (size != 4) { return; } - char32_t utf32 = char32_t(uint8_t(chars[0]) << shift_bits[0]); - utf32 |= char32_t(uint8_t(chars[1]) << shift_bits[1]); - utf32 |= char32_t(uint8_t(chars[2]) << shift_bits[2]); - utf32 |= char32_t(uint8_t(chars[3]) << shift_bits[3]); + char32_t utf32 = char32_t( + static_cast(uint8_t(chars[0]) << shift_bits[0]) | + static_cast(uint8_t(chars[1]) << shift_bits[1]) | + static_cast(uint8_t(chars[2]) << shift_bits[2]) | + static_cast(uint8_t(chars[3]) << shift_bits[3])); - utf8_encoding::from_utf32(utf32, utf8_buffer, utf8_buf_size); + utf8::from_utf32(utf32, utf8_buffer, utf8_buf_size); - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } @@ -677,8 +610,7 @@ class file_input_adapter }; /// @brief An input adapter for streams -class stream_input_adapter -{ +class stream_input_adapter { public: /// @brief Construct a new stream_input_adapter object. stream_input_adapter() = default; @@ -687,8 +619,7 @@ class stream_input_adapter /// @param is A reference to the target input stream. explicit stream_input_adapter(std::istream& is, utf_encode_t encode_type) noexcept : m_istream(&is), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { } // allow only move construct/assignment @@ -700,10 +631,8 @@ class stream_input_adapter /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { - switch (m_encode_type) - { + void fill_buffer(std::string& buffer) { + switch (m_encode_type) { case utf_encode_t::UTF_8: fill_buffer_utf8(buffer); break; @@ -721,74 +650,62 @@ class stream_input_adapter private: /// @brief The concrete implementation of get_character() for UTF-8 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf8(std::string& buffer) - { + void fill_buffer_utf8(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_8); char tmp_buf[256] {}; - do - { + do { m_istream->read(&tmp_buf[0], 256); - std::size_t read_size = m_istream->gcount(); + std::size_t read_size = static_cast(m_istream->gcount()); buffer.append(tmp_buf, read_size); } while (!m_istream->eof()); auto current = buffer.begin(); auto end = buffer.end(); - while (current != end) - { - char first = *current++; - - // The first byte starts with 0b0XXX'XXXX -> 1-byte character - if ((first & 0xC0) == 0x80) - { - // The first byte must not start with 0b10XX'XXXX - std::array bytes {{first}}; - throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); - } - // The first byte starts with 0b110X'XXXX -> 2-byte character - else if ((first & 0xE0) == 0xC0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + while (current != end) { + uint8_t first = static_cast(*current++); + uint32_t num_bytes = utf8::get_num_bytes(first); + + switch (num_bytes) { + case 2: { + std::initializer_list bytes {first, uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0b1110'XXXX -> 3-byte character - else if ((first & 0xF0) == 0xE0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 3: { + std::initializer_list bytes {first, uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0x1111'0XXX -> 4-byte character - else if ((first & 0xF8) == 0xF0) - { - std::array bytes { - {uint8_t(first), uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 4: { + std::initializer_list bytes { + first, uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; + } + case 1: + default: + break; } } } /// @brief The concrete implementation of get_character() for UTF-16 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf16(std::string& buffer) - { + void fill_buffer_utf16(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_16BE || m_encode_type == utf_encode_t::UTF_16LE); int shift_bits[2] {0, 0}; - if (m_encode_type == utf_encode_t::UTF_16BE) - { + if (m_encode_type == utf_encode_t::UTF_16BE) { shift_bits[0] = 8; } else // m_encode_type == utf_encode_t::UTF_16LE @@ -799,47 +716,42 @@ class stream_input_adapter char chars[2] = {0, 0}; std::array encoded_buffer {{0, 0}}; std::size_t encoded_buf_size {0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - do - { - while (encoded_buf_size < 2) - { + do { + while (encoded_buf_size < 2) { m_istream->read(&chars[0], 2); std::streamsize size = m_istream->gcount(); - if (size != 2) - { + if (size != 2) { break; } - encoded_buffer[encoded_buf_size] = char16_t(uint8_t(chars[0]) << shift_bits[0]); - encoded_buffer[encoded_buf_size++] |= char16_t(uint8_t(chars[1]) << shift_bits[1]); + encoded_buffer[encoded_buf_size++] = char16_t( + static_cast(uint8_t(chars[0]) << shift_bits[0]) | + static_cast(uint8_t(chars[1]) << shift_bits[1])); }; std::size_t consumed_size = 0; - utf8_encoding::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); + utf8::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); - if (consumed_size == 1) - { + if (consumed_size == 1) { encoded_buffer[0] = encoded_buffer[1]; encoded_buffer[1] = 0; } encoded_buf_size -= consumed_size; - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } while (!m_istream->eof()); } /// @brief The concrete implementation of get_character() for UTF-32 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf32(std::string& buffer) - { + void fill_buffer_utf32(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_32BE || m_encode_type == utf_encode_t::UTF_32LE); int shift_bits[4] {0, 0, 0, 0}; - if (m_encode_type == utf_encode_t::UTF_32BE) - { + if (m_encode_type == utf_encode_t::UTF_32BE) { shift_bits[0] = 24; shift_bits[1] = 16; shift_bits[2] = 8; @@ -852,26 +764,25 @@ class stream_input_adapter } char chars[4] = {0, 0, 0, 0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - do - { + do { m_istream->read(&chars[0], 4); std::streamsize size = m_istream->gcount(); - if (size != 4) - { + if (size != 4) { return; } - char32_t utf32 = char32_t(uint8_t(chars[0]) << shift_bits[0]); - utf32 |= char32_t(uint8_t(chars[1]) << shift_bits[1]); - utf32 |= char32_t(uint8_t(chars[2]) << shift_bits[2]); - utf32 |= char32_t(uint8_t(chars[3]) << shift_bits[3]); + char32_t utf32 = char32_t( + static_cast(uint8_t(chars[0]) << shift_bits[0]) | + static_cast(uint8_t(chars[1]) << shift_bits[1]) | + static_cast(uint8_t(chars[2]) << shift_bits[2]) | + static_cast(uint8_t(chars[3]) << shift_bits[3])); - utf8_encoding::from_utf32(utf32, utf8_buffer, utf8_buf_size); + utf8::from_utf32(utf32, utf8_buffer, utf8_buf_size); - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } while (!m_istream->eof()); } @@ -892,8 +803,7 @@ class stream_input_adapter /// @param end The end of iterators. /// @return iterator_input_adapter An iterator_input_adapter object for the target iterator type. template ())))> -inline iterator_input_adapter input_adapter(ItrType begin, ItrType end) -{ +inline iterator_input_adapter input_adapter(ItrType begin, ItrType end) { utf_encode_t encode_type = detect_encoding_and_skip_bom(begin, end); return iterator_input_adapter(begin, end, encode_type); } @@ -903,14 +813,12 @@ inline iterator_input_adapter input_adapter(ItrType begin, ItrType end) /// @tparam N A size of an array. /// @return decltype(input_adapter(array, array + N)) An iterator_input_adapter object for the target array. template -inline auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + (N - 1))) -{ +inline auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + (N - 1))) { return input_adapter(array, array + (N - 1)); } /// @brief A namespace to implement container_input_adapter_factory for internal use. -namespace input_adapter_factory -{ +namespace input_adapter_factory { using std::begin; using std::end; @@ -919,16 +827,13 @@ using std::end; /// @tparam ContainerType A container type. /// @tparam typename N/A template -struct container_input_adapter_factory -{ -}; +struct container_input_adapter_factory {}; /// @brief A partial specialization of container_input_adapter_factory if begin()/end() are available for ContainerType. /// @tparam ContainerType A container type. template struct container_input_adapter_factory< - ContainerType, void_t()), end(std::declval()))>> -{ + ContainerType, void_t()), end(std::declval()))>> { /// A type for resulting input adapter object. using adapter_type = decltype(input_adapter(begin(std::declval()), end(std::declval()))); @@ -936,8 +841,7 @@ struct container_input_adapter_factory< /// @brief A factory method of input adapter objects for the target container objects. /// @param container A container-like input object. /// @return adapter_type An iterator_input_adapter object. - static adapter_type create(const ContainerType& container) - { + static adapter_type create(const ContainerType& container) { return input_adapter(begin(container), end(container)); } }; @@ -950,18 +854,15 @@ struct container_input_adapter_factory< /// @return input_adapter_factory::container_input_adapter_factory::adapter_type template inline typename input_adapter_factory::container_input_adapter_factory::adapter_type input_adapter( - ContainerType&& container) -{ + ContainerType&& container) { return input_adapter_factory::container_input_adapter_factory::create(container); } /// @brief A factory method for file_input_adapter objects with C-style file handles. /// @param file A file handle. /// @return file_input_adapter A file_input_adapter object. -inline file_input_adapter input_adapter(std::FILE* file) -{ - if (!file) - { +inline file_input_adapter input_adapter(std::FILE* file) { + if (!file) { throw fkyaml::exception("Invalid FILE object pointer."); } utf_encode_t encode_type = detect_encoding_and_skip_bom(file); @@ -971,14 +872,11 @@ inline file_input_adapter input_adapter(std::FILE* file) /// @brief A factory method for stream_input_adapter objects with std::istream objects. /// @param stream An input stream. /// @return stream_input_adapter A stream_input_adapter object. -inline stream_input_adapter input_adapter(std::istream& stream) noexcept -{ +inline stream_input_adapter input_adapter(std::istream& stream) noexcept { utf_encode_t encode_type = detect_encoding_and_skip_bom(stream); return stream_input_adapter(stream, encode_type); } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_INPUT_ADAPTER_HPP_ */ diff --git a/include/fkYAML/detail/input/lexical_analyzer.hpp b/include/fkYAML/detail/input/lexical_analyzer.hpp index 0b66ffdd..6348dc9b 100644 --- a/include/fkYAML/detail/input/lexical_analyzer.hpp +++ b/include/fkYAML/detail/input/lexical_analyzer.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include #include #include @@ -32,35 +32,21 @@ #include #include -/** - * @namespace fkyaml - * @brief namespace for fkYAML library. - */ -FK_YAML_NAMESPACE_BEGIN - -/** - * @namespace detail - * @brief namespace for internal implementations of fkYAML library. - */ -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A class which lexically analizes YAML formatted inputs. /// @tparam BasicNodeType A type of the container for YAML values. template ::value, int> = 0> -class lexical_analyzer -{ +class lexical_analyzer { private: using char_traits_type = typename std::char_traits; - enum class block_style_indicator_t - { + enum class block_style_indicator_t { LITERAL, //!< keeps newlines inside the block as they are indicated by a pipe `|`. FOLDED, //!< replaces newlines inside the block with spaces indicated by a right angle bracket `>`. }; - enum class chomping_indicator_t - { + enum class chomping_indicator_t { STRIP, //!< excludes final line breaks and traiing empty lines indicated by `-`. KEEP, //!< preserves final line breaks but excludes trailing empty lines. no indicator means this type. CLIP, //!< preserves final line breaks and trailing empty lines indicated by `+`. @@ -76,8 +62,7 @@ class lexical_analyzer /// @tparam InputAdapterType The type of the input adapter. /// @param input_adapter An input adapter object. template ::value, int> = 0> - explicit lexical_analyzer(InputAdapterType&& input_adapter) - { + explicit lexical_analyzer(InputAdapterType&& input_adapter) { std::forward(input_adapter).fill_buffer(m_input_buffer); m_cur_itr = m_token_begin_itr = m_input_buffer.cbegin(); m_end_itr = m_input_buffer.cend(); @@ -86,8 +71,7 @@ class lexical_analyzer /// @brief Get the next lexical token type by scanning the left of the input buffer. /// @return lexical_token_t The next lexical token type. - lexical_token_t get_next_token() - { + lexical_token_t get_next_token() { skip_white_spaces_and_newline_codes(); m_token_begin_itr = m_cur_itr; @@ -95,35 +79,29 @@ class lexical_analyzer m_last_token_begin_pos = m_pos_tracker.get_cur_pos_in_line(); m_last_token_begin_line = m_pos_tracker.get_lines_read(); - if (m_cur_itr == m_end_itr) - { + if (m_cur_itr == m_end_itr) { return m_last_token_type = lexical_token_t::END_OF_BUFFER; } - switch (char current = *m_cur_itr) - { + switch (char current = *m_cur_itr) { case '?': - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { m_value_buffer = "?"; return m_last_token_type = lexical_token_t::STRING_VALUE; } - switch (*m_cur_itr) - { + switch (*m_cur_itr) { case ' ': return m_last_token_type = lexical_token_t::EXPLICIT_KEY_PREFIX; default: return m_last_token_type = scan_scalar(); } case ':': { // key separater - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { return m_last_token_type = lexical_token_t::KEY_SEPARATOR; } - switch (*m_cur_itr) - { + switch (*m_cur_itr) { case ' ': case '\t': case '\r': @@ -134,8 +112,7 @@ class lexical_analyzer case ']': case '{': case '}': - if (m_flow_context_depth > 0) - { + if (m_flow_context_depth > 0) { // the above characters are not "safe" to be followed in a flow context. // See https://yaml.org/spec/1.2.2/#733-plain-style for more details. break; @@ -153,8 +130,7 @@ class lexical_analyzer case '&': { // anchor prefix extract_anchor_name(); bool is_empty = m_value_buffer.empty(); - if (is_empty) - { + if (is_empty) { emit_error("anchor name must not be empty."); } return m_last_token_type = lexical_token_t::ANCHOR_PREFIX; @@ -162,8 +138,7 @@ class lexical_analyzer case '*': { // alias prefix extract_anchor_name(); bool is_empty = m_value_buffer.empty(); - if (is_empty) - { + if (is_empty) { emit_error("anchor name must not be empty."); } @@ -179,19 +154,16 @@ class lexical_analyzer return m_last_token_type = scan_directive(); case '-': { char next = *(m_cur_itr + 1); - if (next == ' ') - { + if (next == ' ') { // Move a cursor to the beginning of the next token. m_cur_itr += 2; return m_last_token_type = lexical_token_t::SEQUENCE_BLOCK_PREFIX; } bool is_available = (std::distance(m_cur_itr, m_end_itr) > 2); - if (is_available) - { + if (is_available) { m_cur_itr += 3; - if (std::equal(m_token_begin_itr, m_cur_itr, "---")) - { + if (std::equal(m_token_begin_itr, m_cur_itr, "---")) { return m_last_token_type = lexical_token_t::END_OF_DIRECTIVES; } } @@ -203,8 +175,7 @@ class lexical_analyzer ++m_cur_itr; return m_last_token_type = lexical_token_t::SEQUENCE_FLOW_BEGIN; case ']': // sequence flow end - if (m_flow_context_depth == 0) - { + if (m_flow_context_depth == 0) { emit_error("An invalid flow sequence ending."); } m_flow_context_depth--; @@ -215,8 +186,7 @@ class lexical_analyzer ++m_cur_itr; return m_last_token_type = lexical_token_t::MAPPING_FLOW_BEGIN; case '}': // mapping flow end - if (m_flow_context_depth == 0) - { + if (m_flow_context_depth == 0) { emit_error("An invalid flow mapping ending."); } m_flow_context_depth--; @@ -233,10 +203,8 @@ class lexical_analyzer return m_last_token_type = scan_scalar(); case '.': { bool is_available = (std::distance(m_cur_itr, m_end_itr) > 2); - if (is_available) - { - if (std::equal(m_cur_itr, m_cur_itr + 3, "...")) - { + if (is_available) { + if (std::equal(m_cur_itr, m_cur_itr + 3, "...")) { m_cur_itr += 3; return m_last_token_type = lexical_token_t::END_OF_DOCUMENT; } @@ -265,39 +233,33 @@ class lexical_analyzer /// @brief Get the beginning position of a last token. /// @return std::size_t The beginning position of a last token. - std::size_t get_last_token_begin_pos() const noexcept - { + std::size_t get_last_token_begin_pos() const noexcept { return m_last_token_begin_pos; } /// @brief Get the number of lines already processed. /// @return std::size_t The number of lines already processed. - std::size_t get_lines_processed() const noexcept - { + std::size_t get_lines_processed() const noexcept { return m_last_token_begin_line; } /// @brief Convert from string to null and get the converted value. /// @return std::nullptr_t A null value converted from one of the followings: "null", "Null", "NULL", "~". - std::nullptr_t get_null() const - { + std::nullptr_t get_null() const { return from_string(m_value_buffer, type_tag {}); } /// @brief Convert from string to boolean and get the converted value. /// @return true A string token is one of the followings: "true", "True", "TRUE". /// @return false A string token is one of the followings: "false", "False", "FALSE". - boolean_type get_boolean() const - { + boolean_type get_boolean() const { return from_string(m_value_buffer, type_tag {}); } /// @brief Convert from string to integer and get the converted value. /// @return integer_type An integer value converted from the source string. - integer_type get_integer() const - { - if (m_value_buffer.size() > 2 && m_value_buffer.rfind("0o", 0) != std::string::npos) - { + integer_type get_integer() const { + if (m_value_buffer.size() > 2 && m_value_buffer.rfind("0o", 0) != std::string::npos) { // Replace the prefix "0o" with "0" since STL functions can detect octal chars. // Note that the YAML specifies octal values start with the prefix "0o", not "0". // See https://yaml.org/spec/1.2.2/#1032-tag-resolution for more details. @@ -308,15 +270,13 @@ class lexical_analyzer /// @brief Convert from string to float number and get the converted value. /// @return float_number_type A float number value converted from the source string. - float_number_type get_float_number() const - { + float_number_type get_float_number() const { return from_string(m_value_buffer, type_tag {}); } /// @brief Get a scanned string value. /// @return const string_type& Constant reference to a scanned string. - const string_type& get_string() const noexcept - { + const string_type& get_string() const noexcept { // TODO: Provide support for different string types between nodes & inputs. static_assert(std::is_same::value, "Unsupported, different string types."); return m_value_buffer; @@ -324,8 +284,7 @@ class lexical_analyzer /// @brief Get the YAML version specification. /// @return const string_type& A YAML version specification. - const string_type& get_yaml_version() const - { + const string_type& get_yaml_version() const { FK_YAML_ASSERT(!m_value_buffer.empty() && m_value_buffer.size() == 3); FK_YAML_ASSERT(m_value_buffer == "1.1" || m_value_buffer == "1.2"); @@ -334,16 +293,14 @@ class lexical_analyzer /// @brief Get the YAML tag handle defined in the TAG directive. /// @return const std::string& A tag handle. - const std::string& get_tag_handle() const - { + const std::string& get_tag_handle() const { FK_YAML_ASSERT(!m_tag_handle.empty()); return m_tag_handle; } /// @brief Get the YAML tag prefix defined in the TAG directive. /// @return const std::string A tag prefix. - const std::string& get_tag_prefix() const - { + const std::string& get_tag_prefix() const { FK_YAML_ASSERT(!m_tag_prefix.empty()); return m_tag_prefix; } @@ -352,22 +309,18 @@ class lexical_analyzer /// @brief A utility function to convert a hexadecimal character to an integer. /// @param source A hexadecimal character ('0'~'9', 'A'~'F', 'a'~'f') /// @return char A integer converted from @a source. - char convert_hex_char_to_byte(char source) const - { - if ('0' <= source && source <= '9') - { + char convert_hex_char_to_byte(char source) const { + if ('0' <= source && source <= '9') { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) return static_cast(source - '0'); } - if ('A' <= source && source <= 'F') - { + if ('A' <= source && source <= 'F') { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) return static_cast(source - 'A' + 10); } - if ('a' <= source && source <= 'f') - { + if ('a' <= source && source <= 'f') { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) return static_cast(source - 'a' + 10); } @@ -376,8 +329,7 @@ class lexical_analyzer } /// @brief Skip until a newline code or a null character is found. - void scan_comment() - { + void scan_comment() { FK_YAML_ASSERT(*m_cur_itr == '#'); skip_until_line_end(); } @@ -385,18 +337,15 @@ class lexical_analyzer /// @brief Scan directives starting with the prefix '%' /// @note Currently, only %YAML directive is supported. If not, returns invalid or throws an exception. /// @return lexical_token_t The lexical token type for directives. - lexical_token_t scan_directive() - { + lexical_token_t scan_directive() { FK_YAML_ASSERT(*m_cur_itr == '%'); m_value_buffer.clear(); m_token_begin_itr = ++m_cur_itr; bool ends_loop = false; - while (!ends_loop && m_cur_itr != m_end_itr) - { - switch (*m_cur_itr) - { + while (!ends_loop && m_cur_itr != m_end_itr) { + switch (*m_cur_itr) { case ' ': case '\t': ends_loop = true; @@ -413,20 +362,16 @@ class lexical_analyzer m_value_buffer.assign(m_token_begin_itr, m_cur_itr); - if (m_value_buffer == "TAG") - { - if (!ends_loop) - { + if (m_value_buffer == "TAG") { + if (!ends_loop) { emit_error("There must be at least one white space between \"%TAG\" and tag info."); } skip_white_spaces(); return scan_tag_directive(); } - if (m_value_buffer == "YAML") - { - if (!ends_loop) - { + if (m_value_buffer == "YAML") { + if (!ends_loop) { emit_error("There must be at least one white space between \"%YAML\" and tag info."); } skip_white_spaces(); @@ -439,8 +384,7 @@ class lexical_analyzer /// @brief Scan a YAML tag directive. /// @return lexical_token_t The lexical token type for YAML tag directives. - lexical_token_t scan_tag_directive() - { + lexical_token_t scan_tag_directive() { m_tag_handle.clear(); m_tag_prefix.clear(); m_token_begin_itr = m_cur_itr; @@ -449,50 +393,41 @@ class lexical_analyzer // extract a tag handle // - if (*m_cur_itr != '!') - { + if (*m_cur_itr != '!') { emit_error("Tag handle must start with \'!\'."); } - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { emit_error("invalid TAG directive is found."); } - switch (*m_cur_itr) - { + switch (*m_cur_itr) { case ' ': case '\t': // primary handle (!) break; case '!': - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { emit_error("invalid TAG directive is found."); } - if (*m_cur_itr != ' ' && *m_cur_itr != '\t') - { + if (*m_cur_itr != ' ' && *m_cur_itr != '\t') { emit_error("invalid tag handle is found."); } break; default: { bool ends_loop = false; - do - { - switch (*m_cur_itr) - { + do { + switch (*m_cur_itr) { case ' ': case '\t': emit_error("invalid tag handle is found."); case '!': { - if (m_cur_itr + 1 == m_end_itr) - { + if (m_cur_itr + 1 == m_end_itr) { ends_loop = true; break; } char next = *(m_cur_itr + 1); - if (next != ' ' && next != '\t') - { + if (next != ' ' && next != '\t') { emit_error("invalid tag handle is found."); } ends_loop = true; @@ -501,16 +436,14 @@ class lexical_analyzer case '-': break; default: - if (!isalnum(*m_cur_itr)) - { + if (!isalnum(*m_cur_itr)) { // See https://yaml.org/spec/1.2.2/#rule-c-named-tag-handle for more details. emit_error("named handle can contain only numbers(0-9), alphabets(A-Z,a-z) and hyphens(-)."); } break; } - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { emit_error("invalid TAG directive is found."); } } while (!ends_loop); @@ -527,8 +460,7 @@ class lexical_analyzer // m_token_begin_itr = m_cur_itr; - switch (*m_cur_itr) - { + switch (*m_cur_itr) { // a tag prefix must not start with flow indicators to avoid ambiguity. // See https://yaml.org/spec/1.2.2/#rule-ns-global-tag-prefix for more details. case ',': @@ -541,10 +473,8 @@ class lexical_analyzer // extract the rest of a tag prefix. bool ends_loop = false; - do - { - switch (*m_cur_itr) - { + do { + switch (*m_cur_itr) { case ' ': case '\t': case '\r': @@ -557,8 +487,7 @@ class lexical_analyzer m_tag_prefix.assign(m_token_begin_itr, m_cur_itr); bool is_valid = uri_encoding::validate(m_tag_prefix.begin(), m_tag_prefix.end()); - if (!is_valid) - { + if (!is_valid) { emit_error("invalid URI character is found in a tag prefix."); } @@ -568,16 +497,13 @@ class lexical_analyzer /// @brief Scan a YAML version directive. /// @note Only 1.1 and 1.2 are supported. If not, throws an exception. /// @return lexical_token_t The lexical token type for YAML version directives. - lexical_token_t scan_yaml_version_directive() - { + lexical_token_t scan_yaml_version_directive() { m_value_buffer.clear(); m_token_begin_itr = m_cur_itr; bool ends_loop = false; - while (!ends_loop && m_cur_itr != m_end_itr) - { - switch (*m_cur_itr) - { + while (!ends_loop && m_cur_itr != m_end_itr) { + switch (*m_cur_itr) { case ' ': case '\t': case '\r': @@ -592,8 +518,7 @@ class lexical_analyzer m_value_buffer.assign(m_token_begin_itr, m_cur_itr); - if (m_value_buffer != "1.1" && m_value_buffer != "1.2") - { + if (m_value_buffer != "1.1" && m_value_buffer != "1.2") { emit_error("Only 1.1 and 1.2 can be specified as the YAML version."); } @@ -601,18 +526,15 @@ class lexical_analyzer } /// @brief Extracts an anchor name from the input and assigns the result to `m_value_buffer`. - void extract_anchor_name() - { + void extract_anchor_name() { FK_YAML_ASSERT(*m_cur_itr == '&' || *m_cur_itr == '*'); m_value_buffer.clear(); m_token_begin_itr = ++m_cur_itr; bool ends_loop = false; - for (; m_cur_itr != m_end_itr; ++m_cur_itr) - { - switch (*m_cur_itr) - { + for (; m_cur_itr != m_end_itr; ++m_cur_itr) { + switch (*m_cur_itr) { // anchor name must not contain white spaces, newline codes and flow indicators. // See https://yaml.org/spec/1.2.2/#692-node-anchors for more details. case ' ': @@ -628,14 +550,12 @@ class lexical_analyzer break; case ':': { auto next_itr = m_cur_itr + 1; - if (next_itr == m_end_itr) - { + if (next_itr == m_end_itr) { ++m_cur_itr; ends_loop = true; break; } - switch (*next_itr) - { + switch (*next_itr) { case ' ': case '\t': case '\r': @@ -650,8 +570,7 @@ class lexical_analyzer break; } - if (ends_loop) - { + if (ends_loop) { break; } } @@ -660,14 +579,12 @@ class lexical_analyzer } /// @brief Extracts a tag name from the input and assigns the result to `m_value_buffer`. - void extract_tag_name() - { + void extract_tag_name() { m_value_buffer.clear(); FK_YAML_ASSERT(*m_cur_itr == '!'); - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { // Just "!" is a non-specific tag. m_value_buffer = "!"; return; @@ -676,8 +593,7 @@ class lexical_analyzer bool is_verbatim = false; bool allows_another_tag_prefix = false; - switch (*m_cur_itr) - { + switch (*m_cur_itr) { case ' ': case '\r': case '\n': @@ -700,15 +616,12 @@ class lexical_analyzer bool is_named_handle = false; bool ends_loop = false; - do - { - if (++m_cur_itr == m_end_itr) - { + do { + if (++m_cur_itr == m_end_itr) { break; } - switch (*m_cur_itr) - { + switch (*m_cur_itr) { // Tag names must not contain spaces or newline codes. case ' ': case '\t': @@ -717,8 +630,7 @@ class lexical_analyzer ends_loop = true; break; case '!': - if (!allows_another_tag_prefix) - { + if (!allows_another_tag_prefix) { emit_error("invalid tag prefix (!) is found."); } @@ -733,35 +645,29 @@ class lexical_analyzer m_value_buffer.assign(m_token_begin_itr, m_cur_itr); - if (is_verbatim) - { + if (is_verbatim) { char last = m_value_buffer.back(); - if (last != '>') - { + if (last != '>') { emit_error("verbatim tag (!) must be ended with \'>\'."); } auto tag_begin = m_value_buffer.begin() + 2; auto tag_end = m_value_buffer.end() - 1; - if (tag_begin == tag_end) - { + if (tag_begin == tag_end) { emit_error("verbatim tag(!) must not be empty."); } bool is_valid_uri = uri_encoding::validate(tag_begin, tag_end); - if (!is_valid_uri) - { + if (!is_valid_uri) { emit_error("invalid URI character is found in a verbatim tag."); } return; } - if (is_named_handle) - { + if (is_named_handle) { char last = m_value_buffer.back(); - if (last == '!') - { + if (last == '!') { // Tag shorthand must be followed by a non-empty suffix. // See the "Tag Shorthands" section in https://yaml.org/spec/1.2.2/#691-node-tags. emit_error("named handle has no suffix."); @@ -770,8 +676,7 @@ class lexical_analyzer std::size_t last_tag_prefix_pos = m_value_buffer.find_last_of('!'); bool is_valid_uri = uri_encoding::validate(m_value_buffer.begin() + last_tag_prefix_pos + 1, m_value_buffer.end()); - if (!is_valid_uri) - { + if (!is_valid_uri) { emit_error("Invalid URI character is found in a named tag handle."); } } @@ -779,18 +684,15 @@ class lexical_analyzer /// @brief Scan a string token, either plain, single-quoted or double-quoted. /// @return lexical_token_t The lexical token type for strings. - lexical_token_t scan_scalar() - { + lexical_token_t scan_scalar() { m_value_buffer.clear(); bool needs_last_single_quote = false; bool needs_last_double_quote = false; - if (m_cur_itr == m_token_begin_itr) - { + if (m_cur_itr == m_token_begin_itr) { needs_last_single_quote = (*m_cur_itr == '\''); needs_last_double_quote = (*m_cur_itr == '\"'); - if (needs_last_double_quote || needs_last_single_quote) - { + if (needs_last_double_quote || needs_last_single_quote) { m_token_begin_itr = ++m_cur_itr; } } @@ -798,8 +700,7 @@ class lexical_analyzer lexical_token_t type = extract_string_token(needs_last_single_quote, needs_last_double_quote); FK_YAML_ASSERT(type == lexical_token_t::STRING_VALUE); - if (needs_last_single_quote || needs_last_double_quote) - { + if (needs_last_single_quote || needs_last_double_quote) { // just returned the extracted string value if quoted. return type; } @@ -810,12 +711,10 @@ class lexical_analyzer /// @brief Check if the given character is allowed in a single-quoted scalar token. /// @param c The character to be checked. /// @return true if the given character is allowed, false otherwise. - bool is_allowed_single(char c) - { + bool is_allowed_single(char c) { bool ret = false; - switch (c) - { + switch (c) { case '\r': case '\n': // TODO: Support multi-line string scalars. @@ -824,15 +723,13 @@ class lexical_analyzer case '\'': // If single quotation marks are repeated twice in a single-quoted string token, // they are considered as an escaped single quotation mark. - if (m_cur_itr + 1 == m_end_itr) - { + if (m_cur_itr + 1 == m_end_itr) { m_value_buffer.append(m_token_begin_itr, m_cur_itr++); m_token_begin_itr = m_cur_itr; break; } - if (*(m_cur_itr + 1) != '\'') - { + if (*(m_cur_itr + 1) != '\'') { m_value_buffer.append(m_token_begin_itr, m_cur_itr++); break; } @@ -852,12 +749,10 @@ class lexical_analyzer /// @brief Check if the given character is allowed in a double-quoted scalar token. /// @param c The character to be checked. /// @return true if the given character is allowed, false otherwise. - bool is_allowed_double(char c) - { + bool is_allowed_double(char c) { bool ret = false; - switch (c) - { + switch (c) { case '\r': case '\n': // TODO: Support multi-line string scalars. @@ -873,8 +768,7 @@ class lexical_analyzer // Handle escaped characters. // See https://yaml.org/spec/1.2.2/#57-escaped-characters for more details. c = *++m_cur_itr; - switch (c) - { + switch (c) { case 'a': m_value_buffer.push_back('\a'); break; @@ -912,20 +806,20 @@ class lexical_analyzer m_value_buffer.push_back('\\'); break; case 'N': // next line - utf8_encoding::from_utf32(0x85u, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(0x85u, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); break; case '_': // non-breaking space - utf8_encoding::from_utf32(0xA0u, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(0xA0u, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); break; case 'L': // line separator - utf8_encoding::from_utf32(0x2028u, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(0x2028u, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); break; case 'P': // paragraph separator - utf8_encoding::from_utf32(0x2029u, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(0x2029u, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); break; case 'x': handle_escaped_unicode(1); @@ -951,12 +845,10 @@ class lexical_analyzer /// @brief Check if the given character is allowed in a plain scalar token. /// @param c The character to be checked. /// @return true if the given character is allowed, false otherwise. - bool is_allowed_plain(char c) - { + bool is_allowed_plain(char c) { bool ret = false; - switch (c) - { + switch (c) { case '\r': case '\n': m_value_buffer.append(m_token_begin_itr, m_cur_itr); @@ -970,10 +862,8 @@ class lexical_analyzer // These characters are permitted when not inside a flow collection, and not inside an implicit key. // TODO: Support detection of implicit key context for this check. - if (m_flow_context_depth > 0) - { - switch (next) - { + if (m_flow_context_depth > 0) { + switch (next) { case '{': case '}': case '[': @@ -984,30 +874,25 @@ class lexical_analyzer break; } - if (is_appended) - { + if (is_appended) { break; } } // " :" is permitted in a plain style string token, but not when followed by a space. - if (next == ':') - { + if (next == ':') { char peeked = *(m_cur_itr + 2); - if (peeked == ' ') - { + if (peeked == ' ') { m_value_buffer.append(m_token_begin_itr, m_cur_itr++); is_appended = true; } - if (is_appended) - { + if (is_appended) { break; } } - switch (next) - { + switch (next) { case ' ': case '\r': case '\n': @@ -1028,8 +913,7 @@ class lexical_analyzer // A colon as a key separator must be followed by // * a white space or // * a newline code. - switch (next) - { + switch (next) { case ' ': case '\t': case '\r': @@ -1049,8 +933,7 @@ class lexical_analyzer case ']': case ',': // just regard the flow indicators as a normal character if plain but not inside a flow context. - if (m_flow_context_depth == 0) - { + if (m_flow_context_depth == 0) { ret = true; break; } @@ -1067,8 +950,7 @@ class lexical_analyzer /// @brief Extracts a string token, either plain, single-quoted or double-quoted, from the input buffer. /// @return lexical_token_t The lexical token type for strings. - lexical_token_t extract_string_token(bool needs_last_single_quote, bool needs_last_double_quote) - { + lexical_token_t extract_string_token(bool needs_last_single_quote, bool needs_last_double_quote) { // change behaviors depending on the type of a comming string scalar token. // * single quoted // * double quoted @@ -1077,13 +959,11 @@ class lexical_analyzer std::string check_filters {"\r\n\\"}; bool (lexical_analyzer::*pfn_is_allowed)(char) = nullptr; - if (needs_last_single_quote) - { + if (needs_last_single_quote) { check_filters.append("\'"); pfn_is_allowed = &lexical_analyzer::is_allowed_single; } - else if (needs_last_double_quote) - { + else if (needs_last_double_quote) { check_filters.append("\""); pfn_is_allowed = &lexical_analyzer::is_allowed_double; } @@ -1095,68 +975,45 @@ class lexical_analyzer // scan the contents of a string scalar token. - for (; m_cur_itr != m_end_itr; m_cur_itr = (m_cur_itr == m_end_itr) ? m_cur_itr : ++m_cur_itr) - { + for (; m_cur_itr != m_end_itr; m_cur_itr = (m_cur_itr == m_end_itr) ? m_cur_itr : ++m_cur_itr) { char current = *m_cur_itr; + uint32_t num_bytes = utf8::get_num_bytes(static_cast(current)); + if (num_bytes == 1) { + auto ret = check_filters.find(current); + if (ret != std::string::npos) { + bool is_allowed = (this->*pfn_is_allowed)(current); + if (!is_allowed) { + return lexical_token_t::STRING_VALUE; + } - auto ret = check_filters.find(current); - if (ret != std::string::npos) - { - bool is_allowed = (this->*pfn_is_allowed)(current); - if (!is_allowed) - { - return lexical_token_t::STRING_VALUE; + continue; } - continue; - } - - uint8_t byte = static_cast(current); - - // Handle unescaped control characters. - if (byte <= 0x1F) - { - m_value_buffer.append(m_token_begin_itr, m_cur_itr); - handle_unescaped_control_char(current); - m_token_begin_itr = m_cur_itr + 1; - continue; - } - - // The other characters are already checked while creating an input handler. + uint8_t byte = static_cast(current); - // Handle ASCII characters except control characters. - if (byte <= 0x7E) - { - continue; - } - - // Handle 2-byte characters encoded in UTF-8. (U+0080..U+07FF) - if (byte <= 0xDF) - { - ++m_cur_itr; - continue; - } + // Handle unescaped control characters. + if (byte <= 0x1F) { + m_value_buffer.append(m_token_begin_itr, m_cur_itr); + handle_unescaped_control_char(current); + m_token_begin_itr = m_cur_itr + 1; + continue; + } - // Handle 3-byte characters encoded in UTF-8. (U+1000..U+D7FF,U+E000..U+FFFF) - if (byte <= 0xEF) - { - m_cur_itr += 2; continue; } - // Handle 4-byte characters encoded in UTF-8. (U+10000..U+FFFFF,U+100000..U+10FFFF) - m_cur_itr += 3; + // Multi-byte characters are already validated while creating an input handler. + // So just advance the iterator. + m_cur_itr += num_bytes - 1; } // Handle the end of input buffer. - if (needs_last_double_quote) - { + if (needs_last_double_quote) { emit_error("Invalid end of input buffer in a double-quoted string token."); } - if (needs_last_single_quote) - { + if (needs_last_single_quote) { emit_error("Invalid end of input buffer in a single-quoted string token."); } @@ -1170,26 +1027,21 @@ class lexical_analyzer /// @param indent The indent size specified for the given token. /// @return The lexical token type for strings. lexical_token_t scan_block_style_string_token( - block_style_indicator_t style, chomping_indicator_t chomp, std::size_t indent) - { + block_style_indicator_t style, chomping_indicator_t chomp, std::size_t indent) { m_value_buffer.clear(); // Handle leading all-space lines. - for (char current = 0; m_cur_itr != m_end_itr; ++m_cur_itr) - { + for (char current = 0; m_cur_itr != m_end_itr; ++m_cur_itr) { current = *m_cur_itr; - if (current == ' ') - { + if (current == ' ') { continue; } - if (current == '\r') - { + if (current == '\r') { current = *++m_cur_itr; } - if (current == '\n') - { + if (current == '\n') { m_value_buffer.push_back('\n'); continue; } @@ -1197,10 +1049,8 @@ class lexical_analyzer break; } - if (m_cur_itr == m_end_itr) - { - if (chomp != chomping_indicator_t::KEEP) - { + if (m_cur_itr == m_end_itr) { + if (chomp != chomping_indicator_t::KEEP) { m_value_buffer.clear(); } return lexical_token_t::STRING_VALUE; @@ -1210,22 +1060,18 @@ class lexical_analyzer std::size_t cur_indent = m_pos_tracker.get_cur_pos_in_line(); // TODO: preserve and compare the last indentation with `cur_indent` - if (indent == 0) - { + if (indent == 0) { indent = cur_indent; } - else if (cur_indent < indent) - { + else if (cur_indent < indent) { emit_error("A block style scalar is less indented than the indicated level."); } int chars_in_line = 0; bool is_extra_indented = false; - if (cur_indent > indent) - { + if (cur_indent > indent) { std::size_t diff = cur_indent - indent; - if (style == block_style_indicator_t::FOLDED) - { + if (style == block_style_indicator_t::FOLDED) { m_value_buffer.push_back('\n'); is_extra_indented = true; } @@ -1233,34 +1079,28 @@ class lexical_analyzer chars_in_line += static_cast(diff); } - for (char current = 0; m_cur_itr != m_end_itr; ++m_cur_itr) - { + for (char current = 0; m_cur_itr != m_end_itr; ++m_cur_itr) { current = *m_cur_itr; - if (current == '\r') - { + if (current == '\r') { // Ignore CR assuming the next character is LF. continue; } - if (current == '\n') - { - if (style == block_style_indicator_t::LITERAL) - { + if (current == '\n') { + if (style == block_style_indicator_t::LITERAL) { m_value_buffer.push_back(current); } else // block_style_indicator_t::FOLDED { - if (chars_in_line == 0) - { + if (chars_in_line == 0) { // Just append a newline if the current line is empty. m_value_buffer.push_back('\n'); is_extra_indented = false; continue; } - if (is_extra_indented) - { + if (is_extra_indented) { // A line being more indented is not folded. m_value_buffer.push_back('\n'); chars_in_line = 0; @@ -1271,26 +1111,21 @@ class lexical_analyzer // Append a newline if the next line is empty. bool is_end_of_token = false; bool is_next_empty = false; - for (std::size_t i = 0; i < indent; i++) - { - if (++m_cur_itr == m_end_itr) - { + for (std::size_t i = 0; i < indent; i++) { + if (++m_cur_itr == m_end_itr) { is_end_of_token = true; break; } current = *m_cur_itr; - if (current == ' ') - { + if (current == ' ') { continue; } - if (current == '\r') - { + if (current == '\r') { current = *++m_cur_itr; } - if (current == '\n') - { + if (current == '\n') { is_next_empty = true; break; } @@ -1299,20 +1134,17 @@ class lexical_analyzer break; } - if (is_end_of_token) - { + if (is_end_of_token) { m_value_buffer.push_back('\n'); break; } - if (is_next_empty) - { + if (is_next_empty) { m_value_buffer.push_back('\n'); continue; } - switch (char next = *(m_cur_itr + 1)) - { + switch (char next = *(m_cur_itr + 1)) { case '\r': { ++m_cur_itr; next = *++m_cur_itr; @@ -1343,10 +1175,8 @@ class lexical_analyzer // Handle indentation m_pos_tracker.update_position(m_cur_itr); cur_indent = m_pos_tracker.get_cur_pos_in_line(); - if (cur_indent < indent) - { - if (current != ' ') - { + if (cur_indent < indent) { + if (current != ' ') { // Interpret less indented non-space characters as the start of the next token. break; } @@ -1354,8 +1184,7 @@ class lexical_analyzer continue; } - if (style == block_style_indicator_t::FOLDED && chars_in_line == 0 && current == ' ') - { + if (style == block_style_indicator_t::FOLDED && chars_in_line == 0 && current == ' ') { // A line being more indented is not folded. m_value_buffer.push_back('\n'); is_extra_indented = true; @@ -1365,15 +1194,12 @@ class lexical_analyzer } // Manipulate the trailing line endings chomping indicator type. - switch (chomp) - { + switch (chomp) { case chomping_indicator_t::STRIP: - while (!m_value_buffer.empty()) - { + while (!m_value_buffer.empty()) { // Empty strings are handled above, so no check for the case. char last = m_value_buffer.back(); - if (last != '\n') - { + if (last != '\n') { break; } m_value_buffer.pop_back(); @@ -1381,17 +1207,14 @@ class lexical_analyzer break; case chomping_indicator_t::CLIP: { char last = m_value_buffer.back(); - if (last != '\n') - { + if (last != '\n') { // No need to chomp the trailing newlines. break; } - while (m_value_buffer.size() > 1) - { + while (m_value_buffer.size() > 1) { // Strings with only newlines are handled above, so no check for the case. char second_last = *(m_value_buffer.end() - 2); - if (second_last != '\n') - { + if (second_last != '\n') { break; } m_value_buffer.pop_back(); @@ -1407,12 +1230,10 @@ class lexical_analyzer /// @brief Handle unescaped control characters. /// @param c A target character. - void handle_unescaped_control_char(char c) - { + void handle_unescaped_control_char(char c) { FK_YAML_ASSERT(0x00 <= c && c <= 0x1F); - switch (c) - { + switch (c) { // 0x00(NULL) has already been handled above. case 0x01: emit_error("Control character U+0001 (SOH) must be escaped to \\u0001."); @@ -1480,30 +1301,26 @@ class lexical_analyzer /// @brief Unescape the given escaped unicode character. /// @param bytes_to_read The number of bytes to be read from the input buffer. - void handle_escaped_unicode(int bytes_to_read) - { + void handle_escaped_unicode(int bytes_to_read) { int read_size = bytes_to_read * 2; char32_t code_point = 0; - for (int i = read_size - 1; i >= 0; i--) - { + for (int i = read_size - 1; i >= 0; i--) { char four_bits = convert_hex_char_to_byte(*++m_cur_itr); // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) code_point |= static_cast(four_bits << (4 * i)); } // Treats the code point as a UTF-32 encoded character. - utf8_encoding::from_utf32(code_point, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(code_point, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); } /// @brief Gets the metadata of a following block style string scalar. /// @param chomp_type A variable to store the retrieved chomping style type. /// @param indent A variable to store the retrieved indent size. - void get_block_style_metadata(chomping_indicator_t& chomp_type, std::size_t& indent) - { + void get_block_style_metadata(chomping_indicator_t& chomp_type, std::size_t& indent) { chomp_type = chomping_indicator_t::CLIP; - switch (*++m_cur_itr) - { + switch (*++m_cur_itr) { case '-': chomp_type = chomping_indicator_t::STRIP; ++m_cur_itr; @@ -1516,14 +1333,12 @@ class lexical_analyzer break; } - if (*m_cur_itr == '0') - { + if (*m_cur_itr == '0') { emit_error("An indentation level for a block style scalar cannot be \'0\'"); } indent = 0; - if (std::isdigit(*m_cur_itr)) - { + if (std::isdigit(*m_cur_itr)) { indent = convert_hex_char_to_byte(*m_cur_itr++); } @@ -1532,19 +1347,15 @@ class lexical_analyzer } /// @brief Skip white spaces (half-width spaces and tabs) from the current position. - void skip_white_spaces() - { + void skip_white_spaces() { m_cur_itr = std::find_if_not(m_cur_itr, m_end_itr, [](char c) { return (c == ' ' || c == '\t'); }); } /// @brief Skip white spaces and newline codes (CR/LF) from the current position. - void skip_white_spaces_and_newline_codes() - { - if (m_cur_itr != m_end_itr) - { + void skip_white_spaces_and_newline_codes() { + if (m_cur_itr != m_end_itr) { m_cur_itr = std::find_if_not(m_cur_itr, m_end_itr, [](char c) { - switch (c) - { + switch (c) { case ' ': case '\t': case '\r': @@ -1558,19 +1369,14 @@ class lexical_analyzer } /// @brief Skip the rest in the current line. - void skip_until_line_end() - { - while (m_cur_itr != m_end_itr) - { - switch (*m_cur_itr) - { + void skip_until_line_end() { + while (m_cur_itr != m_end_itr) { + switch (*m_cur_itr) { case '\r': - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { return; } - if (*m_cur_itr == '\n') - { + if (*m_cur_itr == '\n') { ++m_cur_itr; } return; @@ -1586,8 +1392,7 @@ class lexical_analyzer /// @brief Emits an error with the given message. /// @param msg A message for the resulting error. - [[noreturn]] void emit_error(const char* msg) const - { + [[noreturn]] void emit_error(const char* msg) const { m_pos_tracker.update_position(m_cur_itr); throw fkyaml::parse_error(msg, m_pos_tracker.get_lines_read(), m_pos_tracker.get_cur_pos_in_line()); } @@ -1610,7 +1415,7 @@ class lexical_analyzer /// The last tag prefix std::string m_tag_prefix {}; /// A temporal buffer to store a UTF-8 encoded char sequence. - std::array m_encode_buffer {}; + std::array m_encode_buffer {}; /// The actual size of a UTF-8 encoded char sequence. std::size_t m_encoded_size {0}; /// The beginning position of the last lexical token. (zero origin) @@ -1629,8 +1434,6 @@ class lexical_analyzer float_number_type m_float_val {0.0}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_LEXICAL_ANALIZER_HPP_ */ diff --git a/include/fkYAML/detail/input/position_tracker.hpp b/include/fkYAML/detail/input/position_tracker.hpp index 94600e38..2a3c642a 100644 --- a/include/fkYAML/detail/input/position_tracker.hpp +++ b/include/fkYAML/detail/input/position_tracker.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -20,20 +20,13 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A position tracker of the target buffer. -class position_tracker -{ +class position_tracker { private: /// @brief A set of information on the current position in an input buffer. - struct position - { + struct position { /// The current position from the beginning of an input buffer. std::size_t cur_pos {0}; /// The current position in the current line. @@ -43,8 +36,7 @@ class position_tracker }; public: - void set_target_buffer(const std::string& buffer) - { + void set_target_buffer(const std::string& buffer) { m_begin = m_last = buffer.begin(); m_end = buffer.end(); m_position = position {}; @@ -53,23 +45,19 @@ class position_tracker /// @brief Update the set of the current position informations. /// @note This function doesn't support cases where cur_pos has moved backward from the last call. /// @param cur_pos The iterator to the current element of the buffer. - void update_position(std::string::const_iterator cur_pos) - { + void update_position(std::string::const_iterator cur_pos) { m_position.cur_pos = static_cast(std::distance(m_begin, cur_pos)); m_position.lines_read += std::count(m_last, cur_pos, '\n'); m_last = cur_pos; - if (m_position.lines_read == 0) - { + if (m_position.lines_read == 0) { m_position.cur_pos_in_line = m_position.cur_pos; return; } std::size_t count = 0; - while (--cur_pos != m_begin) - { - if (*cur_pos == '\n') - { + while (--cur_pos != m_begin) { + if (*cur_pos == '\n') { break; } count++; @@ -77,22 +65,19 @@ class position_tracker m_position.cur_pos_in_line = count; } - std::size_t get_cur_pos() const noexcept - { + std::size_t get_cur_pos() const noexcept { return m_position.cur_pos; } /// @brief Get the current position in the current line. /// @return std::size_t The current position in the current line. - std::size_t get_cur_pos_in_line() const noexcept - { + std::size_t get_cur_pos_in_line() const noexcept { return m_position.cur_pos_in_line; } /// @brief Get the number of lines which have already been read. /// @return std::size_t The number of lines which have already been read. - std::size_t get_lines_read() const noexcept - { + std::size_t get_lines_read() const noexcept { return m_position.lines_read; } @@ -107,8 +92,6 @@ class position_tracker position m_position {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_POSITION_TRACKER_HPP_ */ diff --git a/include/fkYAML/detail/input/scalar_scanner.hpp b/include/fkYAML/detail/input/scalar_scanner.hpp index 4969aed4..aa48db9e 100644 --- a/include/fkYAML/detail/input/scalar_scanner.hpp +++ b/include/fkYAML/detail/input/scalar_scanner.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -18,70 +18,51 @@ #include #include -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN +FK_YAML_DETAIL_NAMESPACE_BEGIN -/// @namespace detail -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ - -namespace -{ +namespace { /// @brief Check if the given character is a digit. /// @note This function is needed to avoid assertion failures in `std::isdigit()` especially when compiled with MSVC. /// @param c A character to be checked. /// @return true if the given character is a digit, false otherwise. -inline bool is_digit(char c) -{ +inline bool is_digit(char c) { return ('0' <= c && c <= '9'); } -inline bool is_xdigit(char c) -{ +inline bool is_xdigit(char c) { return (('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f')); } } // namespace -class scalar_scanner -{ +class scalar_scanner { public: - static lexical_token_t scan(const std::string& token) - { - switch (token.size()) - { + static lexical_token_t scan(const std::string& token) { + switch (token.size()) { case 0: return lexical_token_t::STRING_VALUE; case 1: - if (token[0] == '~') - { + if (token[0] == '~') { return lexical_token_t::NULL_VALUE; } break; case 4: - if (token == "null" || token == "Null" || token == "NULL") - { + if (token == "null" || token == "Null" || token == "NULL") { return lexical_token_t::NULL_VALUE; } - if (token == "true" || token == "True" || token == "TRUE") - { + if (token == "true" || token == "True" || token == "TRUE") { return lexical_token_t::BOOLEAN_VALUE; } if (token == ".inf" || token == ".Inf" || token == ".INF" || token == ".nan" || token == ".NaN" || - token == ".NAN") - { + token == ".NAN") { return lexical_token_t::FLOAT_NUMBER_VALUE; } break; case 5: - if (token == "false" || token == "False" || token == "FALSE") - { + if (token == "false" || token == "False" || token == "FALSE") { return lexical_token_t::BOOLEAN_VALUE; } - if (token == "-.inf" || token == "-.Inf" || token == "-.INF") - { + if (token == "-.inf" || token == "-.Inf" || token == "-.INF") { return lexical_token_t::FLOAT_NUMBER_VALUE; } break; @@ -91,14 +72,12 @@ class scalar_scanner } private: - static lexical_token_t scan_possible_number_token(const std::string& token) - { + static lexical_token_t scan_possible_number_token(const std::string& token) { std::string::const_iterator itr = token.begin(); std::size_t size = token.size(); FK_YAML_ASSERT(size > 0); - switch (*itr) - { + switch (*itr) { case '-': return (size > 1) ? scan_negative_number(++itr, --size) : lexical_token_t::STRING_VALUE; case '+': @@ -120,34 +99,28 @@ class scalar_scanner } } - static lexical_token_t scan_negative_number(std::string::const_iterator itr, std::size_t size) - { + static lexical_token_t scan_negative_number(std::string::const_iterator itr, std::size_t size) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { return (size > 1) ? scan_decimal_number(++itr, --size, false) : lexical_token_t::INTEGER_VALUE; } return lexical_token_t::STRING_VALUE; } - static lexical_token_t scan_after_zero_at_first(std::string::const_iterator itr, std::size_t size) - { + static lexical_token_t scan_after_zero_at_first(std::string::const_iterator itr, std::size_t size) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { // a token consisting of the beginning '0' and some following numbers, e.g., `0123`, is not an integer // according to https://yaml.org/spec/1.2.2/#10213-integer. return lexical_token_t::STRING_VALUE; } - switch (*itr) - { + switch (*itr) { case '.': { - if (size == 1) - { + if (size == 1) { return lexical_token_t::STRING_VALUE; } lexical_token_t ret = scan_after_decimal_point(++itr, --size, true); @@ -164,20 +137,16 @@ class scalar_scanner } static lexical_token_t scan_decimal_number( - std::string::const_iterator itr, std::size_t size, bool has_decimal_point) - { + std::string::const_iterator itr, std::size_t size, bool has_decimal_point) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { return (size > 1) ? scan_decimal_number(++itr, --size, has_decimal_point) : lexical_token_t::INTEGER_VALUE; } - switch (*itr) - { + switch (*itr) { case '.': { - if (has_decimal_point) - { + if (has_decimal_point) { // the token has more than one period, e.g., a semantic version `1.2.3`. return lexical_token_t::STRING_VALUE; } @@ -194,12 +163,10 @@ class scalar_scanner } static lexical_token_t scan_after_decimal_point( - std::string::const_iterator itr, std::size_t size, bool has_decimal_point) - { + std::string::const_iterator itr, std::size_t size, bool has_decimal_point) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { return (size > 1) ? scan_decimal_number(++itr, --size, has_decimal_point) : lexical_token_t::FLOAT_NUMBER_VALUE; } @@ -208,18 +175,15 @@ class scalar_scanner } static lexical_token_t scan_after_exponent( - std::string::const_iterator itr, std::size_t size, bool has_decimal_point) - { + std::string::const_iterator itr, std::size_t size, bool has_decimal_point) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { return (size > 1) ? scan_decimal_number(++itr, --size, has_decimal_point) : lexical_token_t::FLOAT_NUMBER_VALUE; } - switch (*itr) - { + switch (*itr) { case '+': case '-': return (size > 1) ? scan_decimal_number(++itr, --size, has_decimal_point) : lexical_token_t::STRING_VALUE; @@ -228,12 +192,10 @@ class scalar_scanner } } - static lexical_token_t scan_octal_number(std::string::const_iterator itr, std::size_t size) - { + static lexical_token_t scan_octal_number(std::string::const_iterator itr, std::size_t size) { FK_YAML_ASSERT(size > 0); - switch (*itr) - { + switch (*itr) { case '0': case '1': case '2': @@ -248,20 +210,16 @@ class scalar_scanner } } - static lexical_token_t scan_hexadecimal_number(std::string::const_iterator itr, std::size_t size) - { + static lexical_token_t scan_hexadecimal_number(std::string::const_iterator itr, std::size_t size) { FK_YAML_ASSERT(size > 0); - if (is_xdigit(*itr)) - { + if (is_xdigit(*itr)) { return (size > 1) ? scan_hexadecimal_number(++itr, --size) : lexical_token_t::INTEGER_VALUE; } return lexical_token_t::STRING_VALUE; } }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_SCALAR_SCANNER_HPP_ */ diff --git a/include/fkYAML/detail/input/tag_resolver.hpp b/include/fkYAML/detail/input/tag_resolver.hpp index 27147658..8a1d00b7 100644 --- a/include/fkYAML/detail/input/tag_resolver.hpp +++ b/include/fkYAML/detail/input/tag_resolver.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -21,14 +21,7 @@ #include #include -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementaions of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN namespace /*default prefixes*/ { @@ -38,32 +31,26 @@ const std::string default_secondary_handle_prefix = "tag:yaml.org,2002:"; } // namespace -class tag_resolver -{ +class tag_resolver { public: /// @brief Resolve the input tag name into an expanded tag name prepended with a registered prefix. /// @param tag The input tag name. /// @return The type of a node deduced from the given tag name. - static tag_t resolve_tag(const std::string& tag, const std::shared_ptr& directives) - { + static tag_t resolve_tag(const std::string& tag, const std::shared_ptr& directives) { std::string normalized = normalize_tag_name(tag, directives); return convert_to_tag_type(normalized); } private: - static std::string normalize_tag_name(const std::string& tag, const std::shared_ptr& directives) - { - if (tag.empty()) - { + static std::string normalize_tag_name(const std::string& tag, const std::shared_ptr& directives) { + if (tag.empty()) { throw invalid_tag("tag must not be empty.", ""); } - if (tag[0] != '!') - { + if (tag[0] != '!') { throw invalid_tag("tag must start with \'!\'", tag.c_str()); } - if (tag.size() == 1) - { + if (tag.size() == 1) { // Non-specific tag ("!") will be interpreted as one of the following: // * tag:yaml.org,2002:seq // * tag:yaml.org,2002:map @@ -74,27 +61,22 @@ class tag_resolver } std::string normalized {"!<"}; - switch (tag[1]) - { + switch (tag[1]) { case '!': { // handle a secondary tag handle (!!suffix -> !<[secondary][suffix]>) bool is_null_or_empty = !directives || directives->secondary_handle_prefix.empty(); - if (is_null_or_empty) - { + if (is_null_or_empty) { normalized += default_secondary_handle_prefix + tag.substr(2); } - else - { + else { normalized += directives->secondary_handle_prefix + tag.substr(2); } break; } case '<': - if (tag[2] == '!') - { + if (tag[2] == '!') { bool is_null_or_empty = !directives || directives->primary_handle_prefix.empty(); - if (is_null_or_empty) - { + if (is_null_or_empty) { return normalized + default_primary_handle_prefix + tag.substr(3); } return normalized + directives->primary_handle_prefix + tag.substr(3); @@ -107,22 +89,19 @@ class tag_resolver auto tag_end_pos = tag.find_first_of('!', 1); // handle a named handle (!tag!suffix -> !<[tag][suffix]>) - if (tag_end_pos != std::string::npos) - { + if (tag_end_pos != std::string::npos) { // there must be a non-empty suffix. (already checked by the lexer.) FK_YAML_ASSERT(tag_end_pos < tag.size() - 1); bool is_null_or_empty = !directives || directives->named_handle_map.empty(); - if (is_null_or_empty) - { + if (is_null_or_empty) { throw invalid_tag("named handle has not been registered.", tag.c_str()); } // find the extracted named handle in the map. auto named_handle_itr = directives->named_handle_map.find(tag.substr(0, tag_end_pos + 1)); auto end_itr = directives->named_handle_map.end(); - if (named_handle_itr == end_itr) - { + if (named_handle_itr == end_itr) { throw invalid_tag("named handle has not been registered.", tag.c_str()); } @@ -137,12 +116,10 @@ class tag_resolver // handle a primary tag handle (!suffix -> !<[primary][suffix]>) bool is_null_or_empty = !directives || directives->primary_handle_prefix.empty(); - if (is_null_or_empty) - { + if (is_null_or_empty) { normalized += default_primary_handle_prefix + tag.substr(1); } - else - { + else { normalized += directives->primary_handle_prefix + tag.substr(1); } @@ -154,48 +131,37 @@ class tag_resolver return normalized; } - static tag_t convert_to_tag_type(const std::string& normalized) - { - if (normalized == "!") - { + static tag_t convert_to_tag_type(const std::string& normalized) { + if (normalized == "!") { return tag_t::NON_SPECIFIC; } - if (normalized.size() < 24 /* size of !") - { + if (normalized == "!") { return tag_t::SEQUENCE; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::MAPPING; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::NULL_VALUE; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::BOOLEAN; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::INTEGER; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::FLOATING_NUMBER; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::STRING; } @@ -203,8 +169,6 @@ class tag_resolver } }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_TAG_RESOLVER_HPP_ */ diff --git a/include/fkYAML/detail/input/tag_t.hpp b/include/fkYAML/detail/input/tag_t.hpp index fc093ef8..ef43f298 100644 --- a/include/fkYAML/detail/input/tag_t.hpp +++ b/include/fkYAML/detail/input/tag_t.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -13,18 +13,10 @@ #include -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementaions of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of YAML directive sets. -enum class tag_t -{ +enum class tag_t { NON_SPECIFIC, //!< Represents a non-specific tag. CUSTOM_TAG, //!< Represents a cumstom tag SEQUENCE, //!< Represents a sequence tag. @@ -36,8 +28,6 @@ enum class tag_t STRING, //!< Represents a string tag. }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_TAG_T_HPP_ */ diff --git a/include/fkYAML/detail/iterator.hpp b/include/fkYAML/detail/iterator.hpp index 10a57854..7764a62c 100644 --- a/include/fkYAML/detail/iterator.hpp +++ b/include/fkYAML/detail/iterator.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -18,28 +18,18 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A tag which tells Iterator will contain sequence value iterator. -struct sequence_iterator_tag -{ -}; +struct sequence_iterator_tag {}; /// @brief A tag which tells Iterator will contain mapping value iterator. -struct mapping_iterator_tag -{ -}; +struct mapping_iterator_tag {}; /// @brief The template definitions of type informations used in @ref Iterator class /// @tparam ValueType The type of iterated elements. template -struct iterator_traits -{ +struct iterator_traits { /// A type of iterated elements. using value_type = ValueType; /// A type to represent difference between iterators. @@ -55,8 +45,7 @@ struct iterator_traits /// @brief A specialization of @ref iterator_traits for constant value types. /// @tparam ValueType The type of iterated elements. template -struct iterator_traits -{ +struct iterator_traits { /// A type of iterated elements. using value_type = ValueType; /// A type to represent difference between iterators. @@ -70,8 +59,7 @@ struct iterator_traits }; /// @brief Definitions of iterator types for iterators internally held. -enum class iterator_t -{ +enum class iterator_t { SEQUENCE, //!< sequence iterator type. MAPPING, //!< mapping iterator type. }; @@ -79,8 +67,7 @@ enum class iterator_t /// @brief A class which holds iterators either of sequence or mapping type /// @tparam ValueType The type of iterated elements. template -class iterator -{ +class iterator { public: /// A type for iterator traits of instantiated @Iterator template class. using ItrTraitsType = iterator_traits; @@ -105,8 +92,7 @@ class iterator static_assert(is_basic_node::value, "Iterator only accepts basic_node<...>"); /// @brief The actual storage for iterators internally held in @ref Iterator. - struct iterator_holder - { + struct iterator_holder { /// A sequence iterator object. typename NonConstValueType::sequence_type::iterator sequence_iterator {}; /// A mapping iterator object. @@ -117,26 +103,22 @@ class iterator /// @brief Construct a new iterator object with sequence iterator object. /// @param[in] itr An sequence iterator object. iterator(sequence_iterator_tag /* unused */, const typename ValueType::sequence_type::iterator& itr) noexcept - : m_inner_iterator_type(iterator_t::SEQUENCE) - { + : m_inner_iterator_type(iterator_t::SEQUENCE) { m_iterator_holder.sequence_iterator = itr; } /// @brief Construct a new iterator object with mapping iterator object. /// @param[in] itr An mapping iterator object. iterator(mapping_iterator_tag /* unused */, const typename ValueType::mapping_type::iterator& itr) noexcept - : m_inner_iterator_type(iterator_t::MAPPING) - { + : m_inner_iterator_type(iterator_t::MAPPING) { m_iterator_holder.mapping_iterator = itr; } /// @brief Copy constructor of the iterator class. /// @param other An iterator object to be copied with. iterator(const iterator& other) noexcept - : m_inner_iterator_type(other.m_inner_iterator_type) - { - switch (m_inner_iterator_type) - { + : m_inner_iterator_type(other.m_inner_iterator_type) { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: m_iterator_holder.sequence_iterator = other.m_iterator_holder.sequence_iterator; break; @@ -149,10 +131,8 @@ class iterator /// @brief Move constructor of the iterator class. /// @param other An iterator object to be moved from. iterator(iterator&& other) noexcept - : m_inner_iterator_type(other.m_inner_iterator_type) - { - switch (m_inner_iterator_type) - { + : m_inner_iterator_type(other.m_inner_iterator_type) { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: m_iterator_holder.sequence_iterator = std::move(other.m_iterator_holder.sequence_iterator); break; @@ -169,16 +149,13 @@ class iterator /// @brief A copy assignment operator of the iterator class. /// @param rhs An iterator object to be copied with. /// @return iterator& Reference to this iterator object. - iterator& operator=(const iterator& rhs) noexcept - { - if (&rhs == this) - { + iterator& operator=(const iterator& rhs) noexcept { + if (&rhs == this) { return *this; } m_inner_iterator_type = rhs.m_inner_iterator_type; - switch (m_inner_iterator_type) - { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: m_iterator_holder.sequence_iterator = rhs.m_iterator_holder.sequence_iterator; break; @@ -193,16 +170,13 @@ class iterator /// @brief A move assignment operator of the iterator class. /// @param rhs An iterator object to be moved from. /// @return iterator& Reference to this iterator object. - iterator& operator=(iterator&& rhs) noexcept - { - if (&rhs == this) - { + iterator& operator=(iterator&& rhs) noexcept { + if (&rhs == this) { return *this; } m_inner_iterator_type = rhs.m_inner_iterator_type; - switch (m_inner_iterator_type) - { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: m_iterator_holder.sequence_iterator = std::move(rhs.m_iterator_holder.sequence_iterator); break; @@ -216,10 +190,8 @@ class iterator /// @brief An arrow operator of the iterator class. /// @return pointer A pointer to the BasicNodeType object internally referenced by the actual iterator object. - pointer operator->() noexcept - { - if (m_inner_iterator_type == iterator_t::SEQUENCE) - { + pointer operator->() noexcept { + if (m_inner_iterator_type == iterator_t::SEQUENCE) { return &(*(m_iterator_holder.sequence_iterator)); } @@ -229,10 +201,8 @@ class iterator /// @brief A dereference operator of the iterator class. /// @return reference Reference to the Node object internally referenced by the actual iterator object. - reference operator*() noexcept - { - if (m_inner_iterator_type == iterator_t::SEQUENCE) - { + reference operator*() noexcept { + if (m_inner_iterator_type == iterator_t::SEQUENCE) { return *(m_iterator_holder.sequence_iterator); } @@ -243,10 +213,8 @@ class iterator /// @brief A compound assignment operator by sum of the Iterator class. /// @param i The difference from this Iterator object with which it moves forward. /// @return Iterator& Reference to this Iterator object. - iterator& operator+=(difference_type i) noexcept - { - switch (m_inner_iterator_type) - { + iterator& operator+=(difference_type i) noexcept { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: std::advance(m_iterator_holder.sequence_iterator, i); break; @@ -260,8 +228,7 @@ class iterator /// @brief A plus operator of the iterator class. /// @param i The difference from this iterator object. /// @return iterator An iterator object which has been added @a i. - iterator operator+(difference_type i) const noexcept - { + iterator operator+(difference_type i) const noexcept { auto result = *this; result += i; return result; @@ -269,10 +236,8 @@ class iterator /// @brief An pre-increment operator of the iterator class. /// @return iterator& Reference to this iterator object. - iterator& operator++() noexcept - { - switch (m_inner_iterator_type) - { + iterator& operator++() noexcept { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: std::advance(m_iterator_holder.sequence_iterator, 1); break; @@ -285,8 +250,7 @@ class iterator /// @brief A post-increment opretor of the iterator class. /// @return iterator An iterator object which has been incremented. - iterator operator++(int) & noexcept - { + iterator operator++(int) & noexcept { auto result = *this; ++(*this); return result; @@ -295,16 +259,14 @@ class iterator /// @brief A compound assignment operator by difference of the iterator class. /// @param i The difference from this iterator object with which it moves backward. /// @return iterator& Reference to this iterator object. - iterator& operator-=(difference_type i) noexcept - { + iterator& operator-=(difference_type i) noexcept { return operator+=(-i); } /// @brief A minus operator of the iterator class. /// @param i The difference from this iterator object. /// @return iterator An iterator object from which has been subtracted @ i. - iterator operator-(difference_type i) noexcept - { + iterator operator-(difference_type i) noexcept { auto result = *this; result -= i; return result; @@ -312,10 +274,8 @@ class iterator /// @brief A pre-decrement operator of the iterator class. /// @return iterator& Reference to this iterator object. - iterator& operator--() noexcept - { - switch (m_inner_iterator_type) - { + iterator& operator--() noexcept { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: std::advance(m_iterator_holder.sequence_iterator, -1); break; @@ -328,8 +288,7 @@ class iterator /// @brief A post-decrement operator of the iterator class /// @return iterator An iterator object which has been decremented. - iterator operator--(int) & noexcept - { + iterator operator--(int) & noexcept { auto result = *this; --(*this); return result; @@ -339,15 +298,12 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is equal to the other. /// @return false This iterator object is not equal to the other. - bool operator==(const iterator& rhs) const - { - if (m_inner_iterator_type != rhs.m_inner_iterator_type) - { + bool operator==(const iterator& rhs) const { + if (m_inner_iterator_type != rhs.m_inner_iterator_type) { throw fkyaml::exception("Cannot compare iterators of different container types."); } - if (m_inner_iterator_type == iterator_t::SEQUENCE) - { + if (m_inner_iterator_type == iterator_t::SEQUENCE) { return (m_iterator_holder.sequence_iterator == rhs.m_iterator_holder.sequence_iterator); } @@ -359,8 +315,7 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is not equal to the other. /// @return false This iterator object is equal to the other. - bool operator!=(const iterator& rhs) const - { + bool operator!=(const iterator& rhs) const { return !operator==(rhs); } @@ -368,15 +323,12 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is less than the other. /// @return false This iterator object is not less than the other. - bool operator<(const iterator& rhs) const - { - if (m_inner_iterator_type != rhs.m_inner_iterator_type) - { + bool operator<(const iterator& rhs) const { + if (m_inner_iterator_type != rhs.m_inner_iterator_type) { throw fkyaml::exception("Cannot compare iterators of different container types."); } - if (m_inner_iterator_type == iterator_t::MAPPING) - { + if (m_inner_iterator_type == iterator_t::MAPPING) { throw fkyaml::exception("Cannot compare order of iterators of the mapping container type"); } @@ -387,8 +339,7 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is either less than or equal to the other. /// @return false This iterator object is neither less than nor equal to the other. - bool operator<=(const iterator& rhs) const - { + bool operator<=(const iterator& rhs) const { return !rhs.operator<(*this); } @@ -396,8 +347,7 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is greater than the other. /// @return false This iterator object is not greater than the other. - bool operator>(const iterator& rhs) const - { + bool operator>(const iterator& rhs) const { return !operator<=(rhs); } @@ -405,25 +355,21 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is either greater than or equal to the other. /// @return false This iterator object is neither greater than nor equal to the other. - bool operator>=(const iterator& rhs) const - { + bool operator>=(const iterator& rhs) const { return !operator<(rhs); } public: /// @brief Get the type of the internal iterator implementation. /// @return iterator_t The type of the internal iterator implementation. - iterator_t type() const noexcept - { + iterator_t type() const noexcept { return m_inner_iterator_type; } /// @brief Get the key string of the YAML mapping node for the current iterator. /// @return const std::string& The key string of the YAML mapping node for the current iterator. - const typename ValueType::mapping_type::key_type& key() const - { - if (m_inner_iterator_type == iterator_t::SEQUENCE) - { + const typename ValueType::mapping_type::key_type& key() const { + if (m_inner_iterator_type == iterator_t::SEQUENCE) { throw fkyaml::exception("Cannot retrieve key from non-mapping iterators."); } @@ -432,8 +378,7 @@ class iterator /// @brief Get the reference of the YAML node for the current iterator. /// @return reference A reference to the YAML node for the current iterator. - reference value() noexcept - { + reference value() noexcept { return operator*(); } @@ -444,8 +389,6 @@ class iterator mutable iterator_holder m_iterator_holder {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_ITERATOR_HPP_ */ diff --git a/include/fkYAML/detail/macros/cpp_config_macros.hpp b/include/fkYAML/detail/macros/cpp_config_macros.hpp index c65950cb..fd31ae0b 100644 --- a/include/fkYAML/detail/macros/cpp_config_macros.hpp +++ b/include/fkYAML/detail/macros/cpp_config_macros.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -18,42 +18,42 @@ // Skip detection if the definitions listed below already exist. #if !defined(FK_YAML_HAS_CXX_20) && !defined(FK_YAML_HAS_CXX_17) && !defined(FK_YAML_HAS_CXX_14) && \ !defined(FK_YAML_CXX_11) - #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && MSVC_LANG >= 202002L) - #define FK_YAML_HAS_CXX_20 - #define FK_YAML_HAS_CXX_17 - #define FK_YAML_HAS_CXX_14 - #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) - #define FK_YAML_HAS_CXX_17 - #define FK_YAML_HAS_CXX_14 - #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) - #define FK_YAML_HAS_CXX_14 - #endif +#if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && MSVC_LANG >= 202002L) +#define FK_YAML_HAS_CXX_20 +#define FK_YAML_HAS_CXX_17 +#define FK_YAML_HAS_CXX_14 +#elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) +#define FK_YAML_HAS_CXX_17 +#define FK_YAML_HAS_CXX_14 +#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) +#define FK_YAML_HAS_CXX_14 +#endif - // C++11 is the minimum required version of the fkYAML library. - #define FK_YAML_HAS_CXX_11 +// C++11 is the minimum required version of the fkYAML library. +#define FK_YAML_HAS_CXX_11 #endif // switch usage of inline variables. Inline variables have been introduced since C++17. #if defined(FK_YAML_HAS_CXX_17) - #define FK_YAML_INLINE_VAR inline +#define FK_YAML_INLINE_VAR inline #else - #define FK_YAML_INLINE_VAR +#define FK_YAML_INLINE_VAR #endif #ifdef __has_include - #if __has_include() - // is available since C++20 - #include - #endif +#if __has_include() +// is available since C++20 +#include +#endif #endif // switch usage of char8_t. char8_t has been introduced since C++20 #if !defined(FK_YAML_HAS_CHAR8_T) - #if defined(FK_YAML_HAS_CXX_20) - #if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L - #define FK_YAML_HAS_CHAR8_T - #endif - #endif +#if defined(FK_YAML_HAS_CXX_20) +#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L +#define FK_YAML_HAS_CHAR8_T +#endif +#endif #endif #endif /* FK_YAML_DETAIL_MACROS_CPP_CONFIG_MACROS_HPP_ */ diff --git a/include/fkYAML/detail/macros/version_macros.hpp b/include/fkYAML/detail/macros/version_macros.hpp index db91c9f3..046ce438 100644 --- a/include/fkYAML/detail/macros/version_macros.hpp +++ b/include/fkYAML/detail/macros/version_macros.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,38 +10,43 @@ // Check version definitions if already defined. #if defined(FK_YAML_MAJOR_VERSION) && defined(FK_YAML_MINOR_VERSION) && defined(FK_YAML_PATCH_VERSION) - #if FK_YAML_MAJOR_VERSION != 0 || FK_YAML_MINOR_VERSION != 3 || FK_YAML_PATCH_VERSION != 4 - #warning Already included a different version of the fkYAML library! - #else - // define macros to skip defining macros down below. - #define FK_YAML_VERCHECK_SUCCEEDED - #endif +#if FK_YAML_MAJOR_VERSION != 0 || FK_YAML_MINOR_VERSION != 3 || FK_YAML_PATCH_VERSION != 5 +#warning Already included a different version of the fkYAML library! +#else +// define macros to skip defining macros down below. +#define FK_YAML_VERCHECK_SUCCEEDED +#endif #endif #ifndef FK_YAML_VERCHECK_SUCCEEDED - #define FK_YAML_MAJOR_VERSION 0 - #define FK_YAML_MINOR_VERSION 3 - #define FK_YAML_PATCH_VERSION 4 +#define FK_YAML_MAJOR_VERSION 0 +#define FK_YAML_MINOR_VERSION 3 +#define FK_YAML_PATCH_VERSION 5 + +#define FK_YAML_NAMESPACE_VERSION_CONCAT_IMPL(major, minor, patch) v##major##_##minor##_##patch + +#define FK_YAML_NAMESPACE_VERSION_CONCAT(major, minor, patch) FK_YAML_NAMESPACE_VERSION_CONCAT_IMPL(major, minor, patch) - #define FK_YAML_NAMESPACE_VERSION_CONCAT_IMPL(major, minor, patch) v##major##_##minor##_##patch +#define FK_YAML_NAMESPACE_VERSION \ + FK_YAML_NAMESPACE_VERSION_CONCAT(FK_YAML_MAJOR_VERSION, FK_YAML_MINOR_VERSION, FK_YAML_PATCH_VERSION) - #define FK_YAML_NAMESPACE_VERSION_CONCAT(major, minor, patch) \ - FK_YAML_NAMESPACE_VERSION_CONCAT_IMPL(major, minor, patch) +#define FK_YAML_NAMESPACE_BEGIN \ + namespace fkyaml { \ + inline namespace FK_YAML_NAMESPACE_VERSION { - #define FK_YAML_NAMESPACE_VERSION \ - FK_YAML_NAMESPACE_VERSION_CONCAT(FK_YAML_MAJOR_VERSION, FK_YAML_MINOR_VERSION, FK_YAML_PATCH_VERSION) +#define FK_YAML_NAMESPACE_END \ + } /* inline namespace FK_YAML_NAMESPACE_VERSION */ \ + } // namespace fkyaml - #define FK_YAML_NAMESPACE_BEGIN \ - namespace fkyaml \ - { \ - inline namespace FK_YAML_NAMESPACE_VERSION \ - { +#define FK_YAML_DETAIL_NAMESPACE_BEGIN \ + FK_YAML_NAMESPACE_BEGIN \ + namespace detail { - #define FK_YAML_NAMESPACE_END \ - } /* namespace (inline namespace) */ \ - } // namespace fkyaml +#define FK_YAML_DETAIL_NAMESPACE_END \ + } /* namespace detail */ \ + FK_YAML_NAMESPACE_END - #include +#include #endif // !defined(FK_YAML_VERCHECK_SUCCEEDED) diff --git a/include/fkYAML/detail/meta/detect.hpp b/include/fkYAML/detail/meta/detect.hpp index b37898f3..c9af4525 100644 --- a/include/fkYAML/detail/meta/detect.hpp +++ b/include/fkYAML/detail/meta/detect.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -16,16 +16,10 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A dummy struct to represent detection failure. -struct nonesuch -{ +struct nonesuch { nonesuch() = delete; ~nonesuch() = delete; nonesuch(const nonesuch&) = delete; @@ -35,8 +29,7 @@ struct nonesuch }; /// @brief namespace to implement detector type traits -namespace detector_impl -{ +namespace detector_impl { /// @brief A helper for general type detection. /// @tparam Default A type to represent detection failure. @@ -44,8 +37,7 @@ namespace detector_impl /// @tparam Op A type for desired operation type. /// @tparam Args Argument types passed to desired operation. template class Op, typename... Args> -struct detector : std::false_type -{ +struct detector : std::false_type { /// @brief A type which represents detection failure. using type = Default; }; @@ -55,8 +47,7 @@ struct detector : std::false_type /// @tparam Op A type for desired operation type. /// @tparam Args Argument types passed to desired operation. template class Op, typename... Args> -struct detector>, Op, Args...> : std::true_type -{ +struct detector>, Op, Args...> : std::true_type { /// @brief A detected type. using type = Op; }; @@ -82,8 +73,6 @@ using detected_t = typename detector_impl::detector template class Op, typename... Args> using is_detected_exact = std::is_same>; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_DETECT_HPP_ */ diff --git a/include/fkYAML/detail/meta/input_adapter_traits.hpp b/include/fkYAML/detail/meta/input_adapter_traits.hpp index e0398f57..a01e0046 100644 --- a/include/fkYAML/detail/meta/input_adapter_traits.hpp +++ b/include/fkYAML/detail/meta/input_adapter_traits.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -18,12 +18,7 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /////////////////////////////////////////// // Input Adapter API detection traits @@ -38,17 +33,13 @@ using fill_buffer_fn_t = decltype(std::declval().fill_buffer(std::declval -struct has_fill_buffer : std::false_type -{ -}; +struct has_fill_buffer : std::false_type {}; /// @brief A partial specialization of has_fill_buffer if InputAdapterType has get_character member function. /// @tparam InputAdapterType A type of a target input adapter. template struct has_fill_buffer::value>> - : std::true_type -{ -}; + : std::true_type {}; //////////////////////////////// // is_input_adapter traits @@ -58,19 +49,13 @@ struct has_fill_buffer -struct is_input_adapter : std::false_type -{ -}; +struct is_input_adapter : std::false_type {}; /// @brief A partial specialization of is_input_adapter if T is an input adapter type. /// @tparam InputAdapterType template -struct is_input_adapter::value>> : std::true_type -{ -}; - -} // namespace detail +struct is_input_adapter::value>> : std::true_type {}; -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_INPUT_ADAPTER_TRAITS_HPP_ */ diff --git a/include/fkYAML/detail/meta/node_traits.hpp b/include/fkYAML/detail/meta/node_traits.hpp index 347e5055..5aba7e74 100644 --- a/include/fkYAML/detail/meta/node_traits.hpp +++ b/include/fkYAML/detail/meta/node_traits.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -16,7 +16,6 @@ #include #include -/// @brief namespace for fkYAML library. FK_YAML_NAMESPACE_BEGIN // forward declaration for basic_node<...> @@ -26,9 +25,9 @@ template < template class ConverterType> class basic_node; -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_NAMESPACE_END + +FK_YAML_DETAIL_NAMESPACE_BEGIN ///////////////////////////// // is_basic_node traits @@ -37,9 +36,7 @@ namespace detail /// @brief A struct to check the template parameter class is a kind of basic_node template class. /// @tparam T A class to be checked if it's a kind of basic_node template class. template -struct is_basic_node : std::false_type -{ -}; +struct is_basic_node : std::false_type {}; /// @brief A partial specialization of is_basic_node for basic_node template class. /// @tparam SequenceType A type for sequence node value containers. @@ -55,9 +52,7 @@ template < template class Converter> struct is_basic_node< basic_node> - : std::true_type -{ -}; + : std::true_type {}; /////////////////////////////////// // is_node_ref_storage traits @@ -70,16 +65,12 @@ class node_ref_storage; /// @brief A struct to check the template parameter class is a kind of node_ref_storage_template class. /// @tparam T A type to be checked if it's a kind of node_ref_storage template class. template -struct is_node_ref_storage : std::false_type -{ -}; +struct is_node_ref_storage : std::false_type {}; /// @brief A partial specialization for node_ref_storage template class. /// @tparam T A template parameter type of node_ref_storage template class. template -struct is_node_ref_storage> : std::true_type -{ -}; +struct is_node_ref_storage> : std::true_type {}; /////////////////////////////////////////////////////// // basic_node conversion API representative types @@ -106,16 +97,13 @@ using to_node_funcion_t = decltype(T::to_node(std::declval()...)); /// @tparam T A target type passed to from_node function. /// @tparam typename N/A template -struct has_from_node : std::false_type -{ -}; +struct has_from_node : std::false_type {}; /// @brief A partial specialization of has_from_node if T is not a basic_node template instance type. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam T A target type passed to from_node function. template -struct has_from_node::value>> -{ +struct has_from_node::value>> { using converter = typename BasicNodeType::template value_converter_type; // NOLINTNEXTLINE(readability-identifier-naming) @@ -129,16 +117,13 @@ struct has_from_node::value>> /// @tparam T A target type passed to to_node function. /// @tparam typename N/A template -struct has_to_node : std::false_type -{ -}; +struct has_to_node : std::false_type {}; /// @brief A partial specialization of has_to_node if T is not a basic_node template instance type. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam T A target type passed to to_node function. template -struct has_to_node::value>> -{ +struct has_to_node::value>> { using converter = typename BasicNodeType::template value_converter_type; // NOLINTNEXTLINE(readability-identifier-naming) @@ -155,9 +140,7 @@ struct has_to_node::value>> /// @tparam CompatibleType A target type for compatibility check. /// @tparam typename N/A template -struct is_node_compatible_type_impl : std::false_type -{ -}; +struct is_node_compatible_type_impl : std::false_type {}; /// @brief A partial specialization of is_node_compatible_type_impl if CompatibleType is a complete type and is /// compatible for BasicNodeType. @@ -167,20 +150,14 @@ template struct is_node_compatible_type_impl< BasicNodeType, CompatibleType, enable_if_t, has_to_node>::value>> - : std::true_type -{ -}; + : std::true_type {}; /// @brief Type traits to check if CompatibleType is a compatible type for BasicNodeType. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam CompatibleType A target type for compatibility check. template -struct is_node_compatible_type : is_node_compatible_type_impl -{ -}; - -} // namespace detail +struct is_node_compatible_type : is_node_compatible_type_impl {}; -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_NODE_TRAITS_HPP_ */ diff --git a/include/fkYAML/detail/meta/stl_supplement.hpp b/include/fkYAML/detail/meta/stl_supplement.hpp index 098fd862..8d064d33 100644 --- a/include/fkYAML/detail/meta/stl_supplement.hpp +++ b/include/fkYAML/detail/meta/stl_supplement.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -16,15 +16,7 @@ #include -/** - * @namespace fkyaml - * @brief namespace for fkYAML library. - */ -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN ///////////////////////////////////////////////////////////////////////////////////////////////////////// // For contributors: @@ -57,9 +49,7 @@ using enable_if_t = typename std::enable_if::type; /// @sa https://en.cppreference.com/w/cpp/types/is_null_pointer /// @tparam T The type to be checked if it's equal to std::nullptr_t. template -struct is_null_pointer : std::is_same::type> -{ -}; +struct is_null_pointer : std::is_same::type> {}; /// @brief An alias template for std::remove_cv::type with C++11. /// @note std::remove_cv_t is available since C++14. @@ -107,24 +97,18 @@ using bool_constant = std::integral_constant; /// @sa https://en.cppreference.com/w/cpp/types/conjunction /// @tparam Traits Type traits to be checked if their ::value are all true. template -struct conjunction : std::true_type -{ -}; +struct conjunction : std::true_type {}; /// @brief A partial specialization of conjunction if only one Trait is given. /// @tparam Trait Type trait to be checked if its ::value is true. template -struct conjunction : Trait -{ -}; +struct conjunction : Trait {}; /// @brief A partial specialization of conjunction if more than one traits are given. /// @tparam First The first type trait to be checked if its ::value is true. /// @tparam Rest The rest of traits passed as another conjunction template arguments if First::value is true. template -struct conjunction : std::conditional, First>::type -{ -}; +struct conjunction : std::conditional, First>::type {}; /// @brief A simple implementation to use std::disjunction with C++11/C++14. /// @note @@ -133,39 +117,30 @@ struct conjunction : std::conditional -struct disjunction : std::false_type -{ -}; +struct disjunction : std::false_type {}; /// @brief A partial specialization of disjunction if only one Trait is given. /// @tparam Trait Type trait to be checked if its ::value is true. template -struct disjunction : Trait -{ -}; +struct disjunction : Trait {}; /// @brief A partial specialization of disjunction if more than one traits are given. /// @tparam First The first type trait to be checked if its ::value is true. /// @tparam Rest The rest of traits passed as another conjunction template arguments if First::value is false. template -struct disjunction : std::conditional>::type -{ -}; +struct disjunction : std::conditional>::type {}; /// @brief A simple implementation to use std::negation with C++11/C++14. /// @note std::negation is available since C++17. /// @sa https://en.cppreference.com/w/cpp/types/negation /// @tparam Trait Type trait whose ::value is negated. template -struct negation : std::integral_constant -{ -}; +struct negation : std::integral_constant {}; /// @brief A helper for void_t. /// @tparam Types Any types to be transformed to void type. template -struct make_void -{ +struct make_void { using type = void; }; @@ -201,8 +176,6 @@ using std::remove_cvref_t; #endif -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_STL_SUPPLEMENT_HPP_ */ diff --git a/include/fkYAML/detail/meta/type_traits.hpp b/include/fkYAML/detail/meta/type_traits.hpp index d924a56d..4c8d788f 100644 --- a/include/fkYAML/detail/meta/type_traits.hpp +++ b/include/fkYAML/detail/meta/type_traits.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -18,12 +18,7 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Type trait to check if T and U are comparable types. /// @tparam Comparator An object type to compare T and U objects. @@ -31,9 +26,7 @@ namespace detail /// @tparam U The other type for comparison. /// @tparam typename Placeholder for determining T and U are comparable types. template -struct is_comparable : std::false_type -{ -}; +struct is_comparable : std::false_type {}; /// @brief A partial specialization of is_comparable if T and U are comparable types. /// @tparam Comparator An object type to compare T and U objects. @@ -44,9 +37,7 @@ struct is_comparable< Comparator, T, U, void_t< decltype(std::declval()(std::declval(), std::declval())), - decltype(std::declval()(std::declval(), std::declval()))>> : std::true_type -{ -}; + decltype(std::declval()(std::declval(), std::declval()))>> : std::true_type {}; /// @brief Type trait to check if KeyType can be used as key type. /// @tparam Comparator An object type to compare T and U objects. @@ -60,9 +51,7 @@ using is_usable_as_key_type = typename std::conditional< /// @tparam IntegralType A type to be checked. /// @tparam typename N/A template -struct is_non_bool_integral : std::false_type -{ -}; +struct is_non_bool_integral : std::false_type {}; /// @brief A partial specialization of is_non_bool_integral if IntegralType is of non-boolean integral types. /// @tparam IntegralType A type to be checked. @@ -70,9 +59,7 @@ template struct is_non_bool_integral< IntegralType, enable_if_t, negation>>::value>> - : std::true_type -{ -}; + : std::true_type {}; /// @brief Type traits to check if Types are all signed arithmetic types. /// @tparam Types Types to check if they are all signed arithmetic types. @@ -90,9 +77,7 @@ using is_all_unsigned = conjunction...>; /// @tparam CompatibleIntegerType A compatible integer type. /// @tparam typename N/A template -struct is_compatible_integer_type_impl : std::false_type -{ -}; +struct is_compatible_integer_type_impl : std::false_type {}; /// @brief A partial specialization of is_compatible_integer_type_impl if TargetIntegerType and CompatibleIntegerType /// are compatible integer types. @@ -101,38 +86,29 @@ struct is_compatible_integer_type_impl : std::false_type template struct is_compatible_integer_type_impl< TargetIntegerType, CompatibleIntegerType, enable_if_t::value>> - : std::true_type -{ -}; + : std::true_type {}; /// @brief Type traits to check if TargetIntegerType and CompatibleIntegerType are compatible integer types. /// @tparam TargetIntegerType A target integer type. /// @tparam CompatibleIntegerType A compatible integer type. template -struct is_compatible_integer_type : is_compatible_integer_type_impl -{ -}; +struct is_compatible_integer_type : is_compatible_integer_type_impl {}; /// @brief Type traits to check if T is a complete type. /// @tparam T A type to be checked if a complete type. /// @tparam typename N/A template -struct is_complete_type : std::false_type -{ -}; +struct is_complete_type : std::false_type {}; /// @brief A partial specialization of is_complete_type if T is a complete type. /// @tparam T template -struct is_complete_type : std::true_type -{ -}; +struct is_complete_type : std::true_type {}; /// @brief A utility struct to generate static constant instance. /// @tparam T A target type for the resulting static constant instance. template -struct static_const -{ +struct static_const { static FK_YAML_INLINE_VAR constexpr T value {}; // NOLINT(readability-identifier-naming) }; @@ -147,8 +123,7 @@ constexpr T static_const::value; /// @brief A helper structure for tag dispatch. /// @tparam T A tag type. template -struct type_tag -{ +struct type_tag { /// @brief A tagged type. using type = T; }; @@ -161,8 +136,7 @@ struct get_head_type; /// @brief A specialization of get_head_type if variadic template has no arguments. /// @tparam N/A template <> -struct get_head_type<> -{ +struct get_head_type<> { /// @brief A head type using type = void; }; @@ -171,8 +145,7 @@ struct get_head_type<> /// @tparam First The first type in the arguments /// @tparam Rest The rest of the types in the arguments. template -struct get_head_type -{ +struct get_head_type { /// @brief A head type. using type = First; }; @@ -182,8 +155,6 @@ struct get_head_type template using head_type = typename get_head_type::type; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_TYPE_TRAITS_HPP_ */ diff --git a/include/fkYAML/detail/node_property.hpp b/include/fkYAML/detail/node_property.hpp index 721d5192..ddbf9029 100644 --- a/include/fkYAML/detail/node_property.hpp +++ b/include/fkYAML/detail/node_property.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -15,29 +15,20 @@ #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN +FK_YAML_DETAIL_NAMESPACE_BEGIN -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ - -enum class anchor_status_t -{ +enum class anchor_status_t { NONE, ANCHOR, ALIAS, }; -struct node_property -{ +struct node_property { std::string tag {}; anchor_status_t anchor_status {anchor_status_t::NONE}; std::string anchor {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_NODE_PROPERTY_HPP_ */ diff --git a/include/fkYAML/detail/node_ref_storage.hpp b/include/fkYAML/detail/node_ref_storage.hpp index e139f157..9e0892a2 100644 --- a/include/fkYAML/detail/node_ref_storage.hpp +++ b/include/fkYAML/detail/node_ref_storage.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -19,19 +19,13 @@ #include #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A temporal storage for basic_node class objects. /// @note This class makes it easier to handle lvalue basic_node objects in basic_node ctor with std::initializer_list. /// @tparam BasicNodeType A basic_node template instance type. template -class node_ref_storage -{ +class node_ref_storage { static_assert(is_basic_node::value, "node_ref_storage only accepts basic_node<...>"); using node_type = BasicNodeType; @@ -40,22 +34,19 @@ class node_ref_storage /// @brief Construct a new node ref storage object with an rvalue basic_node object. /// @param n An rvalue basic_node object. explicit node_ref_storage(node_type&& n) noexcept(std::is_nothrow_move_constructible::value) - : owned_value(std::move(n)) - { + : owned_value(std::move(n)) { } /// @brief Construct a new node ref storage object with an lvalue basic_node object. /// @param n An lvalue basic_node object. explicit node_ref_storage(const node_type& n) noexcept - : value_ref(&n) - { + : value_ref(&n) { } /// @brief Construct a new node ref storage object with a std::initializer_list object. /// @param init A std::initializer_list object. node_ref_storage(std::initializer_list init) - : owned_value(init) - { + : owned_value(init) { } /// @brief Construct a new node ref storage object with variadic template arguments @@ -63,8 +54,7 @@ class node_ref_storage /// @param args Arguments to construct a basic_node object. template ::value, int> = 0> node_ref_storage(Args&&... args) - : owned_value(std::forward(args)...) - { + : owned_value(std::forward(args)...) { } // allow only move construct/assignment @@ -76,15 +66,13 @@ class node_ref_storage public: /// @brief An arrow operator for node_ref_storage objects. /// @return const node_type* A constant pointer to a basic_node object. - const node_type* operator->() const noexcept - { + const node_type* operator->() const noexcept { return value_ref ? value_ref : &owned_value; } /// @brief Releases a basic_node object internally held. /// @return node_type A basic_node object internally held. - node_type release() const noexcept - { + node_type release() const noexcept { return value_ref ? *value_ref : std::move(owned_value); } @@ -95,8 +83,6 @@ class node_ref_storage const node_type* value_ref = nullptr; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_NODE_REF_STORAGE_HPP_ */ diff --git a/include/fkYAML/detail/output/serializer.hpp b/include/fkYAML/detail/output/serializer.hpp index f2e7942a..055d3100 100644 --- a/include/fkYAML/detail/output/serializer.hpp +++ b/include/fkYAML/detail/output/serializer.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -24,17 +24,12 @@ #include #include -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A basic implementation of serialization feature for YAML nodes. /// @tparam BasicNodeType A BasicNode template class instantiation. template -class basic_serializer -{ +class basic_serializer { static_assert(detail::is_basic_node::value, "basic_serializer only accepts basic_node<...>"); public: @@ -44,8 +39,7 @@ class basic_serializer /// @brief Serialize the given Node value. /// @param node A Node object to be serialized. /// @return std::string A serialization result of the given Node value. - std::string serialize(const BasicNodeType& node) - { + std::string serialize(const BasicNodeType& node) { std::string str {}; serialize_directives(node, str); serialize_node(node, 0, str); @@ -56,20 +50,16 @@ class basic_serializer /// @brief Serialize the directives if any is applied to the node. /// @param node The targe node. /// @param str A string to hold serialization result. - void serialize_directives(const BasicNodeType& node, std::string& str) - { - if (!node.mp_directive_set) - { + void serialize_directives(const BasicNodeType& node, std::string& str) { + if (!node.mp_directive_set) { return; } const auto& directives = node.mp_directive_set; - if (directives->is_version_specified) - { + if (directives->is_version_specified) { str += "%YAML "; - switch (directives->version) - { + switch (directives->version) { case yaml_version_t::VER_1_1: str += "1.1\n"; break; @@ -79,24 +69,20 @@ class basic_serializer } } - if (!directives->primary_handle_prefix.empty()) - { + if (!directives->primary_handle_prefix.empty()) { str += "%TAG ! "; str += directives->primary_handle_prefix; str += "\n"; } - if (!directives->secondary_handle_prefix.empty()) - { + if (!directives->secondary_handle_prefix.empty()) { str += "%TAG !! "; str += directives->secondary_handle_prefix; str += "\n"; } - if (!directives->named_handle_map.empty()) - { - for (const auto& itr : directives->named_handle_map) - { + if (!directives->named_handle_map.empty()) { + for (const auto& itr : directives->named_handle_map) { str += "%TAG "; str += itr.first; str += " "; @@ -112,22 +98,15 @@ class basic_serializer /// @param node A Node object to be serialized. /// @param cur_indent The current indent width /// @param str A string to hold serialization result. - void serialize_node(const BasicNodeType& node, const uint32_t cur_indent, std::string& str) - { - switch (node.type()) - { + void serialize_node(const BasicNodeType& node, const uint32_t cur_indent, std::string& str) { + switch (node.type()) { case node_t::SEQUENCE: - for (const auto& seq_item : node) - { - if (cur_indent > 0) - { - insert_indentation(cur_indent, str); - } + for (const auto& seq_item : node) { + insert_indentation(cur_indent, str); str += "-"; bool is_appended = try_append_alias(seq_item, true, str); - if (is_appended) - { + if (is_appended) { str += "\n"; continue; } @@ -136,44 +115,45 @@ class basic_serializer try_append_tag(seq_item, true, str); bool is_scalar = seq_item.is_scalar(); - if (is_scalar) - { + if (is_scalar) { str += " "; serialize_node(seq_item, cur_indent, str); str += "\n"; } - else - { + else { str += "\n"; serialize_node(seq_item, cur_indent + 2, str); } } break; case node_t::MAPPING: - for (auto itr = node.begin(); itr != node.end(); ++itr) - { - if (cur_indent > 0) - { - insert_indentation(cur_indent, str); - } + for (auto itr = node.begin(); itr != node.end(); ++itr) { + insert_indentation(cur_indent, str); bool is_appended = try_append_alias(itr.key(), false, str); - if (!is_appended) - { + if (!is_appended) { bool is_anchor_appended = try_append_anchor(itr.key(), false, str); bool is_tag_appended = try_append_tag(itr.key(), is_anchor_appended, str); - if (is_anchor_appended || is_tag_appended) - { + if (is_anchor_appended || is_tag_appended) { str += " "; } - serialize_node(itr.key(), cur_indent, str); + + bool is_container = !itr.key().is_scalar(); + if (is_container) { + str += "? "; + } + uint32_t indent = static_cast(get_cur_indent(str)); + serialize_node(itr.key(), indent, str); + if (is_container) { + // a newline code is already inserted in the above serialize_node() call. + insert_indentation(indent - 2, str); + } } str += ":"; is_appended = try_append_alias(*itr, true, str); - if (is_appended) - { + if (is_appended) { str += "\n"; continue; } @@ -182,14 +162,12 @@ class basic_serializer try_append_tag(*itr, true, str); bool is_scalar = itr->is_scalar(); - if (is_scalar) - { + if (is_scalar) { str += " "; serialize_node(*itr, cur_indent, str); str += "\n"; } - else - { + else { str += "\n"; serialize_node(*itr, cur_indent + 2, str); } @@ -215,8 +193,7 @@ class basic_serializer bool is_escaped = false; typename BasicNodeType::string_type str_val = get_string_node_value(node, is_escaped); - if (is_escaped) - { + if (is_escaped) { // There's no other token type with escapes than strings. // Also, escapes must be in double-quoted strings. str += '\"'; @@ -229,16 +206,14 @@ class basic_serializer lexical_analyzer lexer(std::move(adapter)); lexical_token_t token_type = lexer.get_next_token(); - if (token_type != lexical_token_t::STRING_VALUE) - { + if (token_type != lexical_token_t::STRING_VALUE) { // Surround a string value with double quotes to keep semantic equality. // Without them, serialized values will become non-string. (e.g., "1" -> 1) str += '\"'; str += str_val; str += '\"'; } - else - { + else { str += str_val; } break; @@ -246,12 +221,28 @@ class basic_serializer } } + /// @brief Get the current indentation width. + /// @param s The target string object. + /// @return The current indentation width. + std::size_t get_cur_indent(const std::string& s) const noexcept { + bool is_empty = s.empty(); + if (is_empty) { + return 0; + } + + std::size_t last_lf_pos = s.rfind('\n'); + return (last_lf_pos != std::string::npos) ? s.size() - last_lf_pos - 1 : s.size(); + } + /// @brief Insert indentation to the serialization result. - /// @param cur_indent The current indent width to be inserted. + /// @param indent The indent width to be inserted. /// @param str A string to hold serialization result. - void insert_indentation(const uint32_t cur_indent, std::string& str) const noexcept - { - str.append(cur_indent, ' '); + void insert_indentation(const uint32_t indent, std::string& str) const noexcept { + if (indent == 0) { + return; + } + + str.append(indent - get_cur_indent(str), ' '); } /// @brief Append an anchor property if it's available. Do nothing otherwise. @@ -259,12 +250,9 @@ class basic_serializer /// @param prepends_space Whether or not to prepend a space before an anchor property. /// @param str A string to hold serialization result. /// @return true if an anchor property has been appended, false otherwise. - bool try_append_anchor(const BasicNodeType& node, bool prepends_space, std::string& str) const - { - if (node.is_anchor()) - { - if (prepends_space) - { + bool try_append_anchor(const BasicNodeType& node, bool prepends_space, std::string& str) const { + if (node.is_anchor()) { + if (prepends_space) { str += " "; } str += "&" + node.get_anchor_name(); @@ -278,12 +266,9 @@ class basic_serializer /// @param prepends_space Whether or not to prepend a space before an alias property. /// @param str A string to hold serialization result. /// @return true if an alias property has been appended, false otherwise. - bool try_append_alias(const BasicNodeType& node, bool prepends_space, std::string& str) const - { - if (node.is_alias()) - { - if (prepends_space) - { + bool try_append_alias(const BasicNodeType& node, bool prepends_space, std::string& str) const { + if (node.is_alias()) { + if (prepends_space) { str += " "; } str += "*" + node.get_anchor_name(); @@ -296,12 +281,9 @@ class basic_serializer /// @param[in] node The target node which possibly has a tag name. /// @param[out] str A string to hold serialization result. /// @return true if a tag name has been appended, false otherwise. - bool try_append_tag(const BasicNodeType& node, bool prepends_space, std::string& str) const - { - if (node.has_tag_name()) - { - if (prepends_space) - { + bool try_append_tag(const BasicNodeType& node, bool prepends_space, std::string& str) const { + if (node.has_tag_name()) { + if (prepends_space) { str += " "; } str += node.get_tag_name(); @@ -314,8 +296,7 @@ class basic_serializer /// @param[in] node The target string YAML node. /// @param[out] is_escaped Whether or not the contents of an ouput string has been escaped. /// @return The (escaped) string node value. - typename BasicNodeType::string_type get_string_node_value(const BasicNodeType& node, bool& is_escaped) - { + typename BasicNodeType::string_type get_string_node_value(const BasicNodeType& node, bool& is_escaped) { FK_YAML_ASSERT(node.is_string()); using string_type = typename BasicNodeType::string_type; @@ -326,10 +307,8 @@ class basic_serializer string_type escaped {}; escaped.reserve(size); - for (size_t i = 0; i < size; i++) - { - switch (s[i]) - { + for (size_t i = 0; i < size; i++) { + switch (s[i]) { case 0x01: escaped += "\\u0001"; is_escaped = true; @@ -463,29 +442,25 @@ class basic_serializer is_escaped = true; break; default: - if (i + 1 < size && s[i] == char(0xC2u) && s[i + 1] == char(0x85u)) - { + if (i + 1 < size && s[i] == char(0xC2u) && s[i + 1] == char(0x85u)) { escaped += "\\N"; i++; is_escaped = true; break; } - if (i + 1 < size && s[i] == char(0xC2u) && s[i + 1] == char(0xA0u)) - { + if (i + 1 < size && s[i] == char(0xC2u) && s[i + 1] == char(0xA0u)) { escaped += "\\_"; i++; is_escaped = true; break; } - if (i + 2 < size && s[i] == char(0xE2u) && s[i + 1] == char(0x80u) && s[i + 2] == char(0xA8u)) - { + if (i + 2 < size && s[i] == char(0xE2u) && s[i + 1] == char(0x80u) && s[i + 2] == char(0xA8u)) { escaped += "\\L"; i += 2; is_escaped = true; break; } - if (i + 2 < size && s[i] == char(0xE2u) && s[i + 1] == char(0x80u) && s[i + 2] == char(0xA9u)) - { + if (i + 2 < size && s[i] == char(0xE2u) && s[i + 1] == char(0x80u) && s[i + 2] == char(0xA9u)) { escaped += "\\P"; i += 2; is_escaped = true; @@ -504,8 +479,6 @@ class basic_serializer std::string m_tmp_str_buff; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_OUTPUT_SERIALIZER_HPP_ */ diff --git a/include/fkYAML/detail/string_formatter.hpp b/include/fkYAML/detail/string_formatter.hpp index 2a3f7d7c..101dd5be 100644 --- a/include/fkYAML/detail/string_formatter.hpp +++ b/include/fkYAML/detail/string_formatter.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -18,23 +18,16 @@ #include -/// @namespace namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN +FK_YAML_DETAIL_NAMESPACE_BEGIN -/// @namespace namespace for internal implementation of fkYAML library. -namespace detail -{ - -inline std::string format(const char* fmt, ...) -{ +inline std::string format(const char* fmt, ...) { va_list vl; va_start(vl, fmt); int size = std::vsnprintf(nullptr, 0, fmt, vl); va_end(vl); // LCOV_EXCL_START - if (size < 0) - { + if (size < 0) { return ""; } // LCOV_EXCL_STOP @@ -48,8 +41,6 @@ inline std::string format(const char* fmt, ...) return std::string(buffer.get(), size); } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_STRING_FORMATTER_HPP_ */ diff --git a/include/fkYAML/detail/types/lexical_token_t.hpp b/include/fkYAML/detail/types/lexical_token_t.hpp index 2ac47cae..6d470332 100644 --- a/include/fkYAML/detail/types/lexical_token_t.hpp +++ b/include/fkYAML/detail/types/lexical_token_t.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -13,16 +13,10 @@ #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of lexical token types. -enum class lexical_token_t -{ +enum class lexical_token_t { END_OF_BUFFER, //!< the end of input buffer. EXPLICIT_KEY_PREFIX, //!< the character for explicit mapping key prefix `?`. KEY_SEPARATOR, //!< the key separater `:` @@ -47,8 +41,6 @@ enum class lexical_token_t END_OF_DOCUMENT, //!< the end of a YAML document specified by `...`. }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_TYPES_LEXICAL_TOKEN_T_HPP_ */ diff --git a/include/fkYAML/detail/types/node_t.hpp b/include/fkYAML/detail/types/node_t.hpp index b3d3cac2..4cb5f801 100644 --- a/include/fkYAML/detail/types/node_t.hpp +++ b/include/fkYAML/detail/types/node_t.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -16,16 +16,10 @@ #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of node value types. -enum class node_t : std::uint32_t -{ +enum class node_t : std::uint32_t { SEQUENCE, //!< sequence value type MAPPING, //!< mapping value type NULL_OBJECT, //!< null value type @@ -35,10 +29,8 @@ enum class node_t : std::uint32_t STRING, //!< string value type }; -inline const char* to_string(node_t t) noexcept -{ - switch (t) - { +inline const char* to_string(node_t t) noexcept { + switch (t) { case node_t::SEQUENCE: return "sequence"; case node_t::MAPPING: @@ -58,8 +50,6 @@ inline const char* to_string(node_t t) noexcept } } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_TYPES_NODE_T_HPP_ */ diff --git a/include/fkYAML/detail/types/yaml_version_t.hpp b/include/fkYAML/detail/types/yaml_version_t.hpp index 91c1374e..211b3fcb 100644 --- a/include/fkYAML/detail/types/yaml_version_t.hpp +++ b/include/fkYAML/detail/types/yaml_version_t.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -15,22 +15,14 @@ #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of YAML version types. -enum class yaml_version_t : std::uint32_t -{ +enum class yaml_version_t : std::uint32_t { VER_1_1, //!< YAML version 1.1 VER_1_2, //!< YAML version 1.2 }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_TYPES_YAML_VERSION_T_HPP_ */ diff --git a/include/fkYAML/exception.hpp b/include/fkYAML/exception.hpp index 5cd3b32f..f6cfe689 100644 --- a/include/fkYAML/exception.hpp +++ b/include/fkYAML/exception.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -12,6 +12,7 @@ #define FK_YAML_EXCEPTION_HPP_ #include +#include #include #include @@ -19,13 +20,11 @@ #include #include -/// @brief namespace for fkYAML library. FK_YAML_NAMESPACE_BEGIN /// @brief A base exception class used in fkYAML library. /// @sa https://fktn-k.github.io/fkYAML/api/exception/ -class exception : public std::exception -{ +class exception : public std::exception { public: /// @brief Construct a new exception object without any error messages. /// @sa https://fktn-k.github.io/fkYAML/api/exception/constructor/ @@ -34,10 +33,8 @@ class exception : public std::exception /// @brief Construct a new exception object with an error message. /// @param[in] msg An error message. /// @sa https://fktn-k.github.io/fkYAML/api/exception/constructor/ - explicit exception(const char* msg) noexcept - { - if (msg) - { + explicit exception(const char* msg) noexcept { + if (msg) { m_error_msg = msg; } } @@ -46,8 +43,7 @@ class exception : public std::exception /// @brief Returns an error message internally held. If nothing, a non-null, empty string will be returned. /// @return An error message internally held. The message might be empty. /// @sa https://fktn-k.github.io/fkYAML/api/exception/what/ - const char* what() const noexcept override - { + const char* what() const noexcept override { return m_error_msg.c_str(); } @@ -58,13 +54,13 @@ class exception : public std::exception /// @brief An exception class indicating an encoding error. /// @sa https://fktn-k.github.io/fkYAML/api/exception/invalid_encoding/ -class invalid_encoding : public exception -{ +class invalid_encoding : public exception { public: - template - explicit invalid_encoding(const char* msg, std::array u8) noexcept - : exception(generate_error_message(msg, u8).c_str()) - { + /// @brief Construct a new invalid_encoding object for UTF-8 related errors. + /// @param msg An error message. + /// @param u8 The UTF-8 character bytes. + explicit invalid_encoding(const char* msg, const std::initializer_list& u8) noexcept + : exception(generate_error_message(msg, u8).c_str()) { } /// @brief Construct a new invalid_encoding object for UTF-16 related errors. @@ -72,26 +68,23 @@ class invalid_encoding : public exception /// @param u16_h The first UTF-16 encoded element used for the UTF-8 encoding. /// @param u16_l The second UTF-16 encoded element used for the UTF-8 encoding. explicit invalid_encoding(const char* msg, std::array u16) noexcept - : exception(generate_error_message(msg, u16).c_str()) - { + : exception(generate_error_message(msg, u16).c_str()) { } /// @brief Construct a new invalid_encoding object for UTF-32 related errors. /// @param msg An error message. /// @param u32 The UTF-32 encoded element used for the UTF-8 encoding. explicit invalid_encoding(const char* msg, char32_t u32) noexcept - : exception(generate_error_message(msg, u32).c_str()) - { + : exception(generate_error_message(msg, u32).c_str()) { } private: - template - std::string generate_error_message(const char* msg, std::array u8) const noexcept - { - std::string formatted = detail::format("invalid_encoding: %s in=[ 0x%02x", msg, u8[0]); - for (std::size_t i = 1; i < N; i++) - { - formatted += detail::format(", 0x%02x", u8[i]); + std::string generate_error_message(const char* msg, const std::initializer_list& u8) const noexcept { + auto itr = u8.begin(); + auto end_itr = u8.end(); + std::string formatted = detail::format("invalid_encoding: %s in=[ 0x%02x", msg, *itr++); + while (itr != end_itr) { + formatted += detail::format(", 0x%02x", *itr++); } formatted += " ]"; return formatted; @@ -102,8 +95,7 @@ class invalid_encoding : public exception /// @param h The first UTF-16 encoded element used for the UTF-8 encoding. /// @param l The second UTF-16 encoded element used for the UTF-8 encoding. /// @return A generated error message. - std::string generate_error_message(const char* msg, std::array u16) const noexcept - { + std::string generate_error_message(const char* msg, std::array u16) const noexcept { // uint16_t is large enough for UTF-16 encoded elements. return detail::format("invalid_encoding: %s in=[ 0x%04x, 0x%04x ]", msg, uint16_t(u16[0]), uint16_t(u16[1])); } @@ -112,40 +104,34 @@ class invalid_encoding : public exception /// @param msg An error message. /// @param u32 The UTF-32 encoded element used for the UTF-8 encoding. /// @return A genereated error message. - std::string generate_error_message(const char* msg, char32_t u32) const noexcept - { + std::string generate_error_message(const char* msg, char32_t u32) const noexcept { // uint32_t is large enough for UTF-32 encoded elements. return detail::format("invalid_encoding: %s in=0x%08x", msg, uint32_t(u32)); } }; /// @brief An exception class indicating an error in parsing. -class parse_error : public exception -{ +class parse_error : public exception { public: explicit parse_error(const char* msg, std::size_t lines, std::size_t cols_in_line) noexcept - : exception(generate_error_message(msg, lines, cols_in_line).c_str()) - { + : exception(generate_error_message(msg, lines, cols_in_line).c_str()) { } private: - std::string generate_error_message(const char* msg, std::size_t lines, std::size_t cols_in_line) const noexcept - { + std::string generate_error_message(const char* msg, std::size_t lines, std::size_t cols_in_line) const noexcept { return detail::format("parse_error: %s (at line %zu, column %zu)", msg, lines, cols_in_line); } }; /// @brief An exception class indicating an invalid type conversion. /// @sa https://fktn-k.github.io/fkYAML/api/exception/type_error/ -class type_error : public exception -{ +class type_error : public exception { public: /// @brief Construct a new type_error object with an error message and a node type. /// @param[in] msg An error message. /// @param[in] type The type of a source node value. explicit type_error(const char* msg, detail::node_t type) noexcept - : exception(generate_error_message(msg, type).c_str()) - { + : exception(generate_error_message(msg, type).c_str()) { } private: @@ -153,48 +139,39 @@ class type_error : public exception /// @param msg An error message. /// @param type The type of a source node value. /// @return A generated error message. - std::string generate_error_message(const char* msg, detail::node_t type) const noexcept - { + std::string generate_error_message(const char* msg, detail::node_t type) const noexcept { return detail::format("type_error: %s type=%s", msg, detail::to_string(type)); } }; -class out_of_range : public exception -{ +class out_of_range : public exception { public: explicit out_of_range(int index) noexcept - : exception(generate_error_message(index).c_str()) - { + : exception(generate_error_message(index).c_str()) { } explicit out_of_range(const char* key) noexcept - : exception(generate_error_message(key).c_str()) - { + : exception(generate_error_message(key).c_str()) { } private: - std::string generate_error_message(int index) - { + std::string generate_error_message(int index) { return detail::format("out_of_range: index %d is out of range", index); } - std::string generate_error_message(const char* key) - { + std::string generate_error_message(const char* key) { return detail::format("out_of_range: key \'%s\' is not found.", key); } }; -class invalid_tag : public exception -{ +class invalid_tag : public exception { public: explicit invalid_tag(const char* msg, const char* tag) - : exception(generate_error_message(msg, tag).c_str()) - { + : exception(generate_error_message(msg, tag).c_str()) { } private: - std::string generate_error_message(const char* msg, const char* tag) - { + std::string generate_error_message(const char* msg, const char* tag) { return detail::format("invalid_tag: %s tag=%s", msg, tag); } }; diff --git a/include/fkYAML/node.hpp b/include/fkYAML/node.hpp index 8470fa87..d13ea610 100644 --- a/include/fkYAML/node.hpp +++ b/include/fkYAML/node.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -38,7 +38,6 @@ #include #include -/// @brief namespace for fkYAML library. FK_YAML_NAMESPACE_BEGIN /// @brief A class to store value of YAML nodes. @@ -48,8 +47,7 @@ template < template class MappingType = std::map, typename BooleanType = bool, typename IntegerType = std::int64_t, typename FloatNumberType = double, typename StringType = std::string, template class ConverterType = node_value_converter> -class basic_node -{ +class basic_node { public: /// @brief A type for iterators of basic_node containers. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/iterator/ @@ -118,18 +116,15 @@ class basic_node /// @details This union combines the different sotrage types for the YAML value types defined in @ref node_t. /// @note Container types are stored as pointers so that the size of this union will not exceed 64 bits by /// default. - union node_value - { + union node_value { /// @brief Constructs a new basic_node Value object for null types. node_value() = default; /// @brief Constructs a new basic_node value object with a node type. The default value for the specified /// type will be assigned. /// @param[in] type A node type. - explicit node_value(node_t type) - { - switch (type) - { + explicit node_value(node_t type) { + switch (type) { case node_t::SEQUENCE: p_sequence = create_object(); break; @@ -157,43 +152,34 @@ class basic_node /// @brief Destroys the existing Node value. This process is recursive if the specified node type is for /// containers. /// @param[in] type A Node type to determine the value to be destroyed. - void destroy(node_t type) - { - if (type == node_t::SEQUENCE || type == node_t::MAPPING) - { + void destroy(node_t type) { + if (type == node_t::SEQUENCE || type == node_t::MAPPING) { std::vector stack; - if (type == node_t::SEQUENCE) - { + if (type == node_t::SEQUENCE) { stack.reserve(p_sequence->size()); std::move(p_sequence->begin(), p_sequence->end(), std::back_inserter(stack)); } - else - { + else { stack.reserve(p_mapping->size()); - for (auto&& it : *p_mapping) - { + for (auto&& it : *p_mapping) { stack.push_back(std::move(it.second)); } } - while (!stack.empty()) - { + while (!stack.empty()) { basic_node current_node(std::move(stack.back())); stack.pop_back(); - if (current_node.is_sequence()) - { + if (current_node.is_sequence()) { std::move( current_node.m_node_value.p_sequence->begin(), current_node.m_node_value.p_sequence->end(), std::back_inserter(stack)); current_node.m_node_value.p_sequence->clear(); } - else if (current_node.is_mapping()) - { - for (auto&& it : *current_node.m_node_value.p_mapping) - { + else if (current_node.is_mapping()) { + for (auto&& it : *current_node.m_node_value.p_mapping) { stack.push_back(std::move(it.second)); } current_node.m_node_value.p_mapping->clear(); @@ -201,8 +187,7 @@ class basic_node } } - switch (type) - { + switch (type) { case node_t::SEQUENCE: destroy_object(p_sequence); p_sequence = nullptr; @@ -241,8 +226,7 @@ class basic_node /// @param[in] args A parameter pack for constructor arguments of the target object type. /// @return ObjType* An address of allocated memory on the heap. template - static ObjType* create_object(ArgTypes&&... args) - { + static ObjType* create_object(ArgTypes&&... args) { using AllocType = std::allocator; using AllocTraitsType = std::allocator_traits; @@ -264,8 +248,7 @@ class basic_node /// @tparam ObjType The target object type. /// @param[in] obj A pointer to the target object to be destroyed. template - static void destroy_object(ObjType* obj) - { + static void destroy_object(ObjType* obj) { FK_YAML_ASSERT(obj != nullptr); std::allocator alloc; std::allocator_traits::destroy(alloc, obj); @@ -282,8 +265,7 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/constructor/ explicit basic_node(const node_t type) : m_node_type(type), - m_node_value(type) - { + m_node_value(type) { } /// @brief Copy constructor of the basic_node class. @@ -292,10 +274,8 @@ class basic_node basic_node(const basic_node& rhs) : m_node_type(rhs.m_node_type), mp_directive_set(rhs.mp_directive_set), - m_prop(rhs.m_prop) - { - switch (m_node_type) - { + m_prop(rhs.m_prop) { + switch (m_node_type) { case node_t::SEQUENCE: m_node_value.p_sequence = create_object(*(rhs.m_node_value.p_sequence)); FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); @@ -329,10 +309,8 @@ class basic_node basic_node(basic_node&& rhs) noexcept : m_node_type(rhs.m_node_type), mp_directive_set(std::move(rhs.mp_directive_set)), - m_prop(std::move(rhs.m_prop)) - { - switch (m_node_type) - { + m_prop(std::move(rhs.m_prop)) { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(rhs.m_node_value.p_sequence != nullptr); m_node_value.p_sequence = rhs.m_node_value.p_sequence; @@ -384,8 +362,7 @@ class basic_node detail::disjunction>>::value, int> = 0> basic_node(CompatibleType&& val) noexcept( - noexcept(ConverterType::to_node(std::declval(), std::declval()))) - { + noexcept(ConverterType::to_node(std::declval(), std::declval()))) { ConverterType::to_node(*this, std::forward(val)); } @@ -397,34 +374,29 @@ class basic_node typename NodeRefStorageType, detail::enable_if_t::value, int> = 0> basic_node(const NodeRefStorageType& node_ref_storage) noexcept - : basic_node(node_ref_storage.release()) - { + : basic_node(node_ref_storage.release()) { } /// @brief Construct a new basic node object with std::initializer_list. /// @param[in] init A initializer list of basic_node objects. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/constructor/ - basic_node(initializer_list_t init) - { + basic_node(initializer_list_t init) { bool is_mapping = std::all_of(init.begin(), init.end(), [](const detail::node_ref_storage& node_ref) { return node_ref->is_sequence() && node_ref->size() == 2; }); - if (is_mapping) - { + if (is_mapping) { m_node_type = node_t::MAPPING; m_node_value.p_mapping = create_object(); - for (auto& elem_ref : init) - { + for (auto& elem_ref : init) { auto elem = elem_ref.release(); m_node_value.p_mapping->emplace( std::move((*(elem.m_node_value.p_sequence))[0]), std::move((*(elem.m_node_value.p_sequence))[1])); } } - else - { + else { m_node_type = node_t::SEQUENCE; m_node_value.p_sequence = create_object(init.begin(), init.end()); } @@ -445,8 +417,7 @@ class basic_node /// @return The resulting basic_node object deserialized from the input source. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/deserialize/ template - static basic_node deserialize(InputType&& input) - { + static basic_node deserialize(InputType&& input) { return deserializer_type().deserialize(detail::input_adapter(std::forward(input))); } @@ -457,8 +428,7 @@ class basic_node /// @return The resulting basic_node object deserialized from the pair of iterators. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/deserialize/ template - static basic_node deserialize(ItrType&& begin, ItrType&& end) - { + static basic_node deserialize(ItrType&& begin, ItrType&& end) { return deserializer_type().deserialize( detail::input_adapter(std::forward(begin), std::forward(end))); } @@ -467,16 +437,14 @@ class basic_node /// @param[in] node A basic_node object to be serialized. /// @return The resulting string object from the serialization of the node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/serialize/ - static std::string serialize(const basic_node& node) - { + static std::string serialize(const basic_node& node) { return serializer_type().serialize(node); } /// @brief A factory method for sequence basic_node objects without sequence_type objects. /// @return A YAML sequence node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/sequence/ - static basic_node sequence() - { + static basic_node sequence() { basic_node node; node.m_node_type = node_t::SEQUENCE; node.m_node_value.p_sequence = create_object(); @@ -488,8 +456,7 @@ class basic_node /// @param[in] seq A lvalue sequence node value. /// @return A YAML sequence node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/sequence/ - static basic_node sequence(const sequence_type& seq) - { + static basic_node sequence(const sequence_type& seq) { basic_node node; node.m_node_type = node_t::SEQUENCE; node.m_node_value.p_sequence = create_object(seq); @@ -501,8 +468,7 @@ class basic_node /// @param[in] seq A rvalue sequence node value. /// @return A YAML sequence node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/sequence/ - static basic_node sequence(sequence_type&& seq) - { + static basic_node sequence(sequence_type&& seq) { basic_node node; node.m_node_type = node_t::SEQUENCE; node.m_node_value.p_sequence = create_object(std::move(seq)); @@ -513,8 +479,7 @@ class basic_node /// @brief A factory method for mapping basic_node objects without mapping_type objects. /// @return A YAML mapping node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/mapping/ - static basic_node mapping() - { + static basic_node mapping() { basic_node node; node.m_node_type = node_t::MAPPING; node.m_node_value.p_mapping = create_object(); @@ -526,8 +491,7 @@ class basic_node /// @param[in] map A lvalue mapping node value. /// @return A YAML mapping node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/mapping/ - static basic_node mapping(const mapping_type& map) - { + static basic_node mapping(const mapping_type& map) { basic_node node; node.m_node_type = node_t::MAPPING; node.m_node_value.p_mapping = create_object(map); @@ -539,8 +503,7 @@ class basic_node /// @param[in] map A rvalue mapping node value. /// @return A YAML mapping node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/mapping/ - static basic_node mapping(mapping_type&& map) - { + static basic_node mapping(mapping_type&& map) { basic_node node; node.m_node_type = node_t::MAPPING; node.m_node_value.p_mapping = create_object(std::move(map)); @@ -553,10 +516,8 @@ class basic_node /// @param[in] anchor_node A basic_node object with an anchor name. /// @return An alias YAML node created from the given anchor node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/alias_of/ - static basic_node alias_of(const basic_node& anchor_node) - { - if (!anchor_node.has_anchor_name() || anchor_node.m_prop.anchor_status != detail::anchor_status_t::ANCHOR) - { + static basic_node alias_of(const basic_node& anchor_node) { + if (!anchor_node.has_anchor_name() || anchor_node.m_prop.anchor_status != detail::anchor_status_t::ANCHOR) { throw fkyaml::exception("Cannot create an alias without anchor name."); } @@ -571,8 +532,7 @@ class basic_node /// @param[in] rhs A lvalue basic_node object to be copied with. /// @return Reference to this basic_node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator=/ - basic_node& operator=(const basic_node& rhs) noexcept - { + basic_node& operator=(const basic_node& rhs) noexcept { basic_node(rhs).swap(*this); return *this; } @@ -581,8 +541,7 @@ class basic_node /// @param[in] rhs A rvalue basic_node object to be moved from. /// @return Reference to this basic_node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator=/ - basic_node& operator=(basic_node&& rhs) noexcept - { + basic_node& operator=(basic_node&& rhs) noexcept { basic_node(std::move(rhs)).swap(*this); return *this; } @@ -598,19 +557,15 @@ class basic_node detail::negation>, detail::is_node_compatible_type>::value, int> = 0> - basic_node& operator[](KeyType&& key) - { - if (is_scalar()) - { + basic_node& operator[](KeyType&& key) { + if (is_scalar()) { throw fkyaml::type_error("operator[] is unavailable for a scalar node.", m_node_type); } basic_node n = std::forward(key); - if (is_sequence()) - { - if (!n.is_integer()) - { + if (is_sequence()) { + if (!n.is_integer()) { throw fkyaml::type_error( "An argument of operator[] for sequence nodes must be an integer.", m_node_type); } @@ -633,19 +588,15 @@ class basic_node detail::negation>, detail::is_node_compatible_type>::value, int> = 0> - const basic_node& operator[](KeyType&& key) const - { - if (is_scalar()) - { + const basic_node& operator[](KeyType&& key) const { + if (is_scalar()) { throw fkyaml::type_error("operator[] is unavailable for a scalar node.", m_node_type); } basic_node node_key = std::forward(key); - if (is_sequence()) - { - if (!node_key.is_integer()) - { + if (is_sequence()) { + if (!node_key.is_integer()) { throw fkyaml::type_error( "An argument of operator[] for sequence nodes must be an integer.", m_node_type); } @@ -664,17 +615,13 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator[]/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - basic_node& operator[](KeyType&& key) - { - if (is_scalar()) - { + basic_node& operator[](KeyType&& key) { + if (is_scalar()) { throw fkyaml::type_error("operator[] is unavailable for a scalar node.", m_node_type); } - if (is_sequence()) - { - if (!key.is_integer()) - { + if (is_sequence()) { + if (!key.is_integer()) { throw fkyaml::type_error( "An argument of operator[] for sequence nodes must be an integer.", m_node_type); } @@ -693,17 +640,13 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator[]/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - const basic_node& operator[](KeyType&& key) const - { - if (is_scalar()) - { + const basic_node& operator[](KeyType&& key) const { + if (is_scalar()) { throw fkyaml::type_error("operator[] is unavailable for a scalar node.", m_node_type); } - if (is_sequence()) - { - if (!key.is_integer()) - { + if (is_sequence()) { + if (!key.is_integer()) { throw fkyaml::type_error( "An argument of operator[] for sequence nodes must be an integer.", m_node_type); } @@ -719,16 +662,13 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true if both types and values are equal, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_eq/ - bool operator==(const basic_node& rhs) const noexcept - { - if (m_node_type != rhs.m_node_type) - { + bool operator==(const basic_node& rhs) const noexcept { + if (m_node_type != rhs.m_node_type) { return false; } bool ret = false; - switch (m_node_type) - { + switch (m_node_type) { case node_t::SEQUENCE: ret = (*(m_node_value.p_sequence) == *(rhs.m_node_value.p_sequence)); break; @@ -762,8 +702,7 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true if either types or values are different, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_ne/ - bool operator!=(const basic_node& rhs) const noexcept - { + bool operator!=(const basic_node& rhs) const noexcept { return !operator==(rhs); } @@ -771,26 +710,21 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true this basic_node object is less than `rhs`. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_lt/ - bool operator<(const basic_node& rhs) const noexcept - { - if (operator==(rhs)) - { + bool operator<(const basic_node& rhs) const noexcept { + if (operator==(rhs)) { return false; } - if (uint32_t(m_node_type) < uint32_t(rhs.m_node_type)) - { + if (uint32_t(m_node_type) < uint32_t(rhs.m_node_type)) { return true; } - if (m_node_type != rhs.m_node_type) - { + if (m_node_type != rhs.m_node_type) { return false; } bool ret = false; - switch (m_node_type) - { + switch (m_node_type) { case node_t::SEQUENCE: ret = (*(m_node_value.p_sequence) < *(rhs.m_node_value.p_sequence)); break; @@ -822,8 +756,7 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true this basic_node object is less than or equal to `rhs`. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_le/ - bool operator<=(const basic_node& rhs) const noexcept - { + bool operator<=(const basic_node& rhs) const noexcept { return !rhs.operator<(*this); } @@ -831,8 +764,7 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true this basic_node object is greater than `rhs`. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_gt/ - bool operator>(const basic_node& rhs) const noexcept - { + bool operator>(const basic_node& rhs) const noexcept { return !operator<=(rhs); } @@ -840,8 +772,7 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true this basic_node object is greater than or equal to `rhs`. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_ge/ - bool operator>=(const basic_node& rhs) const noexcept - { + bool operator>=(const basic_node& rhs) const noexcept { return !operator<(rhs); } @@ -849,98 +780,85 @@ class basic_node /// @brief Returns the type of the current basic_node value. /// @return The type of the YAML node value. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/type/ - node_t type() const noexcept - { + node_t type() const noexcept { return m_node_type; } /// @brief Tests whether the current basic_node value is of sequence type. /// @return true if the type is sequence, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_sequence/ - bool is_sequence() const noexcept - { + bool is_sequence() const noexcept { return m_node_type == node_t::SEQUENCE; } /// @brief Tests whether the current basic_node value is of mapping type. /// @return true if the type is mapping, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_mapping/ - bool is_mapping() const noexcept - { + bool is_mapping() const noexcept { return m_node_type == node_t::MAPPING; } /// @brief Tests whether the current basic_node value is of null type. /// @return true if the type is null, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_null/ - bool is_null() const noexcept - { + bool is_null() const noexcept { return m_node_type == node_t::NULL_OBJECT; } /// @brief Tests whether the current basic_node value is of boolean type. /// @return true if the type is boolean, false otherwise /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_boolean/ - bool is_boolean() const noexcept - { + bool is_boolean() const noexcept { return m_node_type == node_t::BOOLEAN; } /// @brief Tests whether the current basic_node value is of integer type. /// @return true if the type is integer, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_integer/ - bool is_integer() const noexcept - { + bool is_integer() const noexcept { return m_node_type == node_t::INTEGER; } /// @brief Tests whether the current basic_node value is of float number type. /// @return true if the type is floating point number, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_float_number/ - bool is_float_number() const noexcept - { + bool is_float_number() const noexcept { return m_node_type == node_t::FLOAT_NUMBER; } /// @brief Tests whether the current basic_node value is of string type. /// @return true if the type is string, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_string/ - bool is_string() const noexcept - { + bool is_string() const noexcept { return m_node_type == node_t::STRING; } /// @brief Tests whether the current basic_node value is of scalar types. /// @return true if the type is scalar, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_scalar/ - bool is_scalar() const noexcept - { + bool is_scalar() const noexcept { return !is_sequence() && !is_mapping(); } /// @brief Tests whether the current basic_node is an anchor node. /// @return true if the current basic_node is an anchor node, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_anchor/ - bool is_anchor() const noexcept - { + bool is_anchor() const noexcept { return m_prop.anchor_status == detail::anchor_status_t::ANCHOR; } /// @brief Tests whether the current basic_node is an alias node. /// @return true if the current basic_node is an alias node, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_alias/ - bool is_alias() const noexcept - { + bool is_alias() const noexcept { return m_prop.anchor_status == detail::anchor_status_t::ALIAS; } /// @brief Tests whether the current basic_node value (sequence, mapping, string) is empty. /// @return true if the node value is empty, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/empty/ - bool empty() const - { - switch (m_node_type) - { + bool empty() const { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return m_node_value.p_sequence->empty(); @@ -958,10 +876,8 @@ class basic_node /// @brief Returns the size of the current basic_node value (sequence, mapping, string). /// @return The size of a node value. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/size/ - std::size_t size() const - { - switch (m_node_type) - { + std::size_t size() const { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return m_node_value.p_sequence->size(); @@ -987,10 +903,8 @@ class basic_node detail::negation>>, detail::is_node_compatible_type>>::value, int> = 0> - bool contains(KeyType&& key) const - { - switch (m_node_type) - { + bool contains(KeyType&& key) const { + switch (m_node_type) { case node_t::MAPPING: { FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); mapping_type& map = *m_node_value.p_mapping; @@ -1009,10 +923,8 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/contains/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - bool contains(KeyType&& key) const - { - switch (m_node_type) - { + bool contains(KeyType&& key) const { + switch (m_node_type) { case node_t::MAPPING: { FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); mapping_type& map = *m_node_value.p_mapping; @@ -1034,27 +946,22 @@ class basic_node detail::negation>, detail::is_node_compatible_type>::value, int> = 0> - basic_node& at(KeyType&& key) - { - if (is_scalar()) - { + basic_node& at(KeyType&& key) { + if (is_scalar()) { throw fkyaml::type_error("at() is unavailable for a scalar node.", m_node_type); } basic_node node_key = std::forward(key); - if (is_sequence()) - { - if (!node_key.is_integer()) - { + if (is_sequence()) { + if (!node_key.is_integer()) { throw fkyaml::type_error("An argument of at() for sequence nodes must be an integer.", m_node_type); } FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); int index = node_key.template get_value(); int size = static_cast(m_node_value.p_sequence->size()); - if (index >= size) - { + if (index >= size) { throw fkyaml::out_of_range(index); } return m_node_value.p_sequence->at(index); @@ -1062,8 +969,7 @@ class basic_node FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); bool is_found = m_node_value.p_mapping->find(node_key) != m_node_value.p_mapping->end(); - if (!is_found) - { + if (!is_found) { throw fkyaml::out_of_range(serialize(node_key).c_str()); } return m_node_value.p_mapping->at(node_key); @@ -1080,27 +986,22 @@ class basic_node detail::negation>, detail::is_node_compatible_type>::value, int> = 0> - const basic_node& at(KeyType&& key) const - { - if (is_scalar()) - { + const basic_node& at(KeyType&& key) const { + if (is_scalar()) { throw fkyaml::type_error("at() is unavailable for a scalar node.", m_node_type); } basic_node node_key = std::forward(key); - if (is_sequence()) - { - if (!node_key.is_integer()) - { + if (is_sequence()) { + if (!node_key.is_integer()) { throw fkyaml::type_error("An argument of at() for sequence nodes must be an integer.", m_node_type); } FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); int index = node_key.template get_value(); int size = static_cast(m_node_value.p_sequence->size()); - if (index >= size) - { + if (index >= size) { throw fkyaml::out_of_range(index); } return m_node_value.p_sequence->at(index); @@ -1108,8 +1009,7 @@ class basic_node FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); bool is_found = m_node_value.p_mapping->find(node_key) != m_node_value.p_mapping->end(); - if (!is_found) - { + if (!is_found) { throw fkyaml::out_of_range(serialize(node_key).c_str()); } return m_node_value.p_mapping->at(node_key); @@ -1122,25 +1022,20 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/at/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - basic_node& at(KeyType&& key) - { - if (is_scalar()) - { + basic_node& at(KeyType&& key) { + if (is_scalar()) { throw fkyaml::type_error("at() is unavailable for a scalar node.", m_node_type); } - if (is_sequence()) - { - if (!key.is_integer()) - { + if (is_sequence()) { + if (!key.is_integer()) { throw fkyaml::type_error("An argument of at() for sequence nodes must be an integer.", m_node_type); } FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); int index = std::forward(key).template get_value(); int size = static_cast(m_node_value.p_sequence->size()); - if (index >= size) - { + if (index >= size) { throw fkyaml::out_of_range(index); } return m_node_value.p_sequence->at(index); @@ -1148,8 +1043,7 @@ class basic_node FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); bool is_found = m_node_value.p_mapping->find(key) != m_node_value.p_mapping->end(); - if (!is_found) - { + if (!is_found) { throw fkyaml::out_of_range(serialize(key).c_str()); } return m_node_value.p_mapping->at(key); @@ -1162,25 +1056,20 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/at/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - const basic_node& at(KeyType&& key) const - { - if (is_scalar()) - { + const basic_node& at(KeyType&& key) const { + if (is_scalar()) { throw fkyaml::type_error("at() is unavailable for a scalar node.", m_node_type); } - if (is_sequence()) - { - if (!key.is_integer()) - { + if (is_sequence()) { + if (!key.is_integer()) { throw fkyaml::type_error("An argument of at() for sequence nodes must be an integer.", m_node_type); } FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); int index = std::forward(key).template get_value(); int size = static_cast(m_node_value.p_sequence->size()); - if (index >= size) - { + if (index >= size) { throw fkyaml::out_of_range(index); } return m_node_value.p_sequence->at(index); @@ -1188,8 +1077,7 @@ class basic_node FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); bool is_found = m_node_value.p_mapping->find(key) != m_node_value.p_mapping->end(); - if (!is_found) - { + if (!is_found) { throw fkyaml::out_of_range(serialize(key).c_str()); } return m_node_value.p_mapping->at(key); @@ -1198,8 +1086,7 @@ class basic_node /// @brief Get the YAML version specification for this basic_node object. /// @return The YAML version if any is applied to the basic_node object, `yaml_version_t::VER_1_2` otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/get_yaml_version/ - yaml_version_t get_yaml_version() const noexcept - { + yaml_version_t get_yaml_version() const noexcept { return (mp_directive_set && mp_directive_set->is_version_specified) ? mp_directive_set->version : yaml_version_t::VER_1_2; } @@ -1208,10 +1095,8 @@ class basic_node /// @note If no YAML directive /// @param[in] A version of the YAML format. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/set_yaml_version/ - void set_yaml_version(const yaml_version_t version) noexcept - { - if (!mp_directive_set) - { + void set_yaml_version(const yaml_version_t version) noexcept { + if (!mp_directive_set) { mp_directive_set = std::shared_ptr(new detail::directive_set()); } mp_directive_set->version = version; @@ -1221,8 +1106,7 @@ class basic_node /// @brief Check whether or not this basic_node object has already had any anchor name. /// @return true if ths basic_node has an anchor name, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/has_anchor_name/ - bool has_anchor_name() const noexcept - { + bool has_anchor_name() const noexcept { return m_prop.anchor_status != detail::anchor_status_t::NONE && !m_prop.anchor.empty(); } @@ -1231,10 +1115,8 @@ class basic_node /// object has any anchor name. /// @return The anchor name associated with the node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/get_anchor_name/ - const std::string& get_anchor_name() const - { - if (!has_anchor_name()) - { + const std::string& get_anchor_name() const { + if (!has_anchor_name()) { throw fkyaml::exception("No anchor name has been set."); } return m_prop.anchor; @@ -1244,8 +1126,7 @@ class basic_node /// @note If this basic_node object has already had any anchor name, the new anchor name will overwrite the old one. /// @param[in] anchor_name An anchor name. This should not be empty. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/add_anchor_name/ - void add_anchor_name(const std::string& anchor_name) - { + void add_anchor_name(const std::string& anchor_name) { m_prop.anchor_status = detail::anchor_status_t::ANCHOR; m_prop.anchor = anchor_name; } @@ -1254,8 +1135,7 @@ class basic_node /// @note If this basic_node object has already had any anchor name, the new anchor name will overwrite the old one. /// @param[in] anchor_name An anchor name. This should not be empty. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/add_anchor_name/ - void add_anchor_name(std::string&& anchor_name) - { + void add_anchor_name(std::string&& anchor_name) { m_prop.anchor_status = detail::anchor_status_t::ANCHOR; m_prop.anchor = std::move(anchor_name); } @@ -1263,8 +1143,7 @@ class basic_node /// @brief Check whether or not this basic_node object has already had any tag name. /// @return true if ths basic_node has a tag name, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/has_tag_name/ - bool has_tag_name() const noexcept - { + bool has_tag_name() const noexcept { return !m_prop.tag.empty(); } @@ -1273,10 +1152,8 @@ class basic_node /// object has any tag name. /// @return The tag name associated with the node. It may be empty. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/get_tag_name/ - const std::string& get_tag_name() const - { - if (!has_tag_name()) - { + const std::string& get_tag_name() const { + if (!has_tag_name()) { throw fkyaml::exception("No tag name has been set."); } return m_prop.tag; @@ -1286,8 +1163,7 @@ class basic_node /// @note If this basic_node object has already had any tag name, the new tag name will overwrite the old one. /// @param[in] tag_name A tag name to get associated with this basic_node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/add_tag_name/ - void add_tag_name(const std::string& tag_name) - { + void add_tag_name(const std::string& tag_name) { m_prop.tag = tag_name; } @@ -1295,8 +1171,7 @@ class basic_node /// @note If this basic_node object has already had any tag name, the new tag name will overwrite the old one. /// @param[in] tag_name A tag name to get associated with this basic_node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/add_tag_name/ - void add_tag_name(std::string&& tag_name) - { + void add_tag_name(std::string&& tag_name) { m_prop.tag = std::move(tag_name); } @@ -1313,8 +1188,7 @@ class basic_node std::is_default_constructible, detail::has_from_node>::value, int> = 0> T get_value() const noexcept( - noexcept(ConverterType::from_node(std::declval(), std::declval()))) - { + noexcept(ConverterType::from_node(std::declval(), std::declval()))) { auto ret = ValueType(); ConverterType::from_node(*this, ret); return ret; @@ -1325,8 +1199,7 @@ class basic_node /// @return Reference to the internally stored YAML node value. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/get_value_ref/ template ::value, int> = 0> - ReferenceType get_value_ref() - { + ReferenceType get_value_ref() { return get_value_ref_impl(static_cast>(nullptr)); } @@ -1340,16 +1213,14 @@ class basic_node detail::conjunction< std::is_reference, std::is_const>>::value, int> = 0> - ReferenceType get_value_ref() const - { + ReferenceType get_value_ref() const { return get_value_ref_impl(static_cast>(nullptr)); } /// @brief Swaps the internally stored data with the specified basic_node object. /// @param[in] rhs A basic_node object to be swapped with. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/swap/ - void swap(basic_node& rhs) noexcept - { + void swap(basic_node& rhs) noexcept { using std::swap; swap(m_node_type, rhs.m_node_type); swap(mp_directive_set, rhs.mp_directive_set); @@ -1368,10 +1239,8 @@ class basic_node /// basic_node object. Throws exception if the basic_node value is not of container types. /// @return An iterator to the first element of a YAML node value (either sequence or mapping). /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/begin/ - iterator begin() - { - switch (m_node_type) - { + iterator begin() { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return {detail::sequence_iterator_tag(), m_node_value.p_sequence->begin()}; @@ -1387,10 +1256,8 @@ class basic_node /// basic_node object. Throws exception if the basic_node value is not of container types. /// @return A constant iterator to the first element of a YAML node value (either sequence or mapping). /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/begin/ - const_iterator begin() const - { - switch (m_node_type) - { + const_iterator begin() const { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return {detail::sequence_iterator_tag(), m_node_value.p_sequence->begin()}; @@ -1406,10 +1273,8 @@ class basic_node /// basic_node object. Throws exception if the basic_node value is not of container types. /// @return An iterator to the past-the end element of a YAML node value (either sequence or mapping). /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/end/ - iterator end() - { - switch (m_node_type) - { + iterator end() { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return {detail::sequence_iterator_tag(), m_node_value.p_sequence->end()}; @@ -1425,10 +1290,8 @@ class basic_node /// basic_node object. Throws exception if the basic_node value is not of container types. /// @return A constant iterator to the past-the end element of a YAML node value (either sequence or mapping). /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/end/ - const_iterator end() const - { - switch (m_node_type) - { + const_iterator end() const { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return {detail::sequence_iterator_tag(), m_node_value.p_sequence->end()}; @@ -1444,10 +1307,8 @@ class basic_node /// @brief Returns reference to the sequence node value. /// @throw fkyaml::exception The node value is not a sequence. /// @return Reference to the sequence node value. - sequence_type& get_value_ref_impl(sequence_type* /*unused*/) - { - if (!is_sequence()) - { + sequence_type& get_value_ref_impl(sequence_type* /*unused*/) { + if (!is_sequence()) { throw fkyaml::type_error("The node value is not a sequence.", m_node_type); } return *(m_node_value.p_sequence); @@ -1456,10 +1317,8 @@ class basic_node /// @brief Returns constant reference to the sequence node value. /// @throw fkyaml::exception The node value is not a sequence. /// @return Constant reference to the sequence node value. - const sequence_type& get_value_ref_impl(const sequence_type* /*unused*/) const - { - if (!is_sequence()) - { + const sequence_type& get_value_ref_impl(const sequence_type* /*unused*/) const { + if (!is_sequence()) { throw fkyaml::type_error("The node value is not a sequence.", m_node_type); } return *(m_node_value.p_sequence); @@ -1468,10 +1327,8 @@ class basic_node /// @brief Returns reference to the mapping node value. /// @throw fkyaml::exception The node value is not a mapping. /// @return Reference to the mapping node value. - mapping_type& get_value_ref_impl(mapping_type* /*unused*/) - { - if (!is_mapping()) - { + mapping_type& get_value_ref_impl(mapping_type* /*unused*/) { + if (!is_mapping()) { throw fkyaml::type_error("The node value is not a mapping.", m_node_type); } return *(m_node_value.p_mapping); @@ -1480,10 +1337,8 @@ class basic_node /// @brief Returns constant reference to the mapping node value. /// @throw fkyaml::exception The node value is not a mapping. /// @return Constant reference to the mapping node value. - const mapping_type& get_value_ref_impl(const mapping_type* /*unused*/) const - { - if (!is_mapping()) - { + const mapping_type& get_value_ref_impl(const mapping_type* /*unused*/) const { + if (!is_mapping()) { throw fkyaml::type_error("The node value is not a mapping.", m_node_type); } return *(m_node_value.p_mapping); @@ -1492,10 +1347,8 @@ class basic_node /// @brief Returns reference to the boolean node value. /// @throw fkyaml::exception The node value is not a boolean. /// @return Reference to the boolean node value. - boolean_type& get_value_ref_impl(boolean_type* /*unused*/) - { - if (!is_boolean()) - { + boolean_type& get_value_ref_impl(boolean_type* /*unused*/) { + if (!is_boolean()) { throw fkyaml::type_error("The node value is not a boolean.", m_node_type); } return m_node_value.boolean; @@ -1504,10 +1357,8 @@ class basic_node /// @brief Returns reference to the boolean node value. /// @throw fkyaml::exception The node value is not a boolean. /// @return Constant reference to the boolean node value. - const boolean_type& get_value_ref_impl(const boolean_type* /*unused*/) const - { - if (!is_boolean()) - { + const boolean_type& get_value_ref_impl(const boolean_type* /*unused*/) const { + if (!is_boolean()) { throw fkyaml::type_error("The node value is not a boolean.", m_node_type); } return m_node_value.boolean; @@ -1516,10 +1367,8 @@ class basic_node /// @brief Returns reference to the integer node value. /// @throw fkyaml::exception The node value is not an integer. /// @return Reference to the integer node value. - integer_type& get_value_ref_impl(integer_type* /*unused*/) - { - if (!is_integer()) - { + integer_type& get_value_ref_impl(integer_type* /*unused*/) { + if (!is_integer()) { throw fkyaml::type_error("The node value is not an integer.", m_node_type); } return m_node_value.integer; @@ -1528,10 +1377,8 @@ class basic_node /// @brief Returns reference to the integer node value. /// @throw fkyaml::exception The node value is not an integer. /// @return Constant reference to the integer node value. - const integer_type& get_value_ref_impl(const integer_type* /*unused*/) const - { - if (!is_integer()) - { + const integer_type& get_value_ref_impl(const integer_type* /*unused*/) const { + if (!is_integer()) { throw fkyaml::type_error("The node value is not an integer.", m_node_type); } return m_node_value.integer; @@ -1540,10 +1387,8 @@ class basic_node /// @brief Returns reference to the floating point number node value. /// @throw fkyaml::exception The node value is not a floating point number. /// @return Reference to the floating point number node value. - float_number_type& get_value_ref_impl(float_number_type* /*unused*/) - { - if (!is_float_number()) - { + float_number_type& get_value_ref_impl(float_number_type* /*unused*/) { + if (!is_float_number()) { throw fkyaml::type_error("The node value is not a floating point number.", m_node_type); } return m_node_value.float_val; @@ -1552,10 +1397,8 @@ class basic_node /// @brief Returns reference to the floating point number node value. /// @throw fkyaml::exception The node value is not a floating point number. /// @return Constant reference to the floating point number node value. - const float_number_type& get_value_ref_impl(const float_number_type* /*unused*/) const - { - if (!is_float_number()) - { + const float_number_type& get_value_ref_impl(const float_number_type* /*unused*/) const { + if (!is_float_number()) { throw fkyaml::type_error("The node value is not a floating point number.", m_node_type); } return m_node_value.float_val; @@ -1564,10 +1407,8 @@ class basic_node /// @brief Returns reference to the string node value. /// @throw fkyaml::exception The node value is not a string. /// @return Reference to the string node value. - string_type& get_value_ref_impl(string_type* /*unused*/) - { - if (!is_string()) - { + string_type& get_value_ref_impl(string_type* /*unused*/) { + if (!is_string()) { throw fkyaml::type_error("The node value is not a string.", m_node_type); } return *(m_node_value.p_string); @@ -1576,10 +1417,8 @@ class basic_node /// @brief Returns reference to the string node value. /// @throw fkyaml::exception The node value is not a string. /// @return Constant reference to the string node value. - const string_type& get_value_ref_impl(const string_type* /*unused*/) const - { - if (!is_string()) - { + const string_type& get_value_ref_impl(const string_type* /*unused*/) const { + if (!is_string()) { throw fkyaml::type_error("The node value is not a string.", m_node_type); } return *(m_node_value.p_string); @@ -1606,8 +1445,7 @@ template < inline void swap( basic_node& lhs, basic_node& - rhs) noexcept(noexcept(lhs.swap(rhs))) -{ + rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); } @@ -1623,8 +1461,7 @@ template < inline std::ostream& operator<<( std::ostream& os, const basic_node& - n) -{ + n) { os << basic_node:: serialize(n); return os; @@ -1642,8 +1479,7 @@ template < template class ConverterType> inline std::istream& operator>>( std::istream& is, - basic_node& n) -{ + basic_node& n) { n = basic_node:: deserialize(is); return is; @@ -1654,19 +1490,16 @@ inline std::istream& operator>>( using node = basic_node<>; /// @brief namespace for user-defined literals for the fkYAML library. -inline namespace literals -{ +inline namespace literals { /// @brief namespace for user-defined literals for YAML node objects. -inline namespace yaml_literals -{ +inline namespace yaml_literals { /// @brief The user-defined string literal which deserializes a `char` array into a `node` object. /// @param s An input `char` array. /// @param n The size of `s`. /// @return The resulting `node` object deserialized from `s`. /// @sa https://fktn-k.github.io/fkYAML/api/operator_literal_yaml/ -inline fkyaml::node operator"" _yaml(const char* s, std::size_t n) -{ +inline fkyaml::node operator"" _yaml(const char* s, std::size_t n) { return fkyaml::node::deserialize((const char*)s, (const char*)s + n); } @@ -1675,8 +1508,7 @@ inline fkyaml::node operator"" _yaml(const char* s, std::size_t n) /// @param n The size of `s`. /// @return The resulting `node` object deserialized from `s`. /// @sa https://fktn-k.github.io/fkYAML/api/operator_literal_yaml/ -inline fkyaml::node operator"" _yaml(const char16_t* s, std::size_t n) -{ +inline fkyaml::node operator"" _yaml(const char16_t* s, std::size_t n) { return fkyaml::node::deserialize((const char16_t*)s, (const char16_t*)s + n); } @@ -1685,8 +1517,7 @@ inline fkyaml::node operator"" _yaml(const char16_t* s, std::size_t n) /// @param n The size of `s`. /// @return The resulting `node` object deserialized from `s`. /// @sa https://fktn-k.github.io/fkYAML/api/operator_literal_yaml/ -inline fkyaml::node operator"" _yaml(const char32_t* s, std::size_t n) -{ +inline fkyaml::node operator"" _yaml(const char32_t* s, std::size_t n) { return fkyaml::node::deserialize((const char32_t*)s, (const char32_t*)s + n); } @@ -1695,8 +1526,7 @@ inline fkyaml::node operator"" _yaml(const char32_t* s, std::size_t n) /// @param s An input `char8_t` array. /// @param n The size of `s`. /// @return The resulting `node` object deserialized from `s`. -inline fkyaml::node operator"" _yaml(const char8_t* s, std::size_t n) -{ +inline fkyaml::node operator"" _yaml(const char8_t* s, std::size_t n) { return fkyaml::node::deserialize((const char8_t*)s, (const char8_t*)s + n); } #endif diff --git a/include/fkYAML/node_value_converter.hpp b/include/fkYAML/node_value_converter.hpp index 96d3c940..abe03403 100644 --- a/include/fkYAML/node_value_converter.hpp +++ b/include/fkYAML/node_value_converter.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -23,8 +23,7 @@ FK_YAML_NAMESPACE_BEGIN /// @tparam ValueType A default target data type. /// @sa https://fktn-k.github.io/fkYAML/api/node_value_converter/ template -class node_value_converter -{ +class node_value_converter { public: /// @brief Convert a YAML node value into compatible native data. /// @tparam BasicNodeType A basic_node template instance type. @@ -35,8 +34,7 @@ class node_value_converter template static auto from_node(BasicNodeType&& n, TargetType& val) noexcept( noexcept(::fkyaml::from_node(std::forward(n), val))) - -> decltype(::fkyaml::from_node(std::forward(n), val), void()) - { + -> decltype(::fkyaml::from_node(std::forward(n), val), void()) { ::fkyaml::from_node(std::forward(n), val); } @@ -49,8 +47,7 @@ class node_value_converter template static auto to_node(BasicNodeType& n, TargetType&& val) noexcept( noexcept(::fkyaml::to_node(n, std::forward(val)))) - -> decltype(::fkyaml::to_node(n, std::forward(val))) - { + -> decltype(::fkyaml::to_node(n, std::forward(val))) { ::fkyaml::to_node(n, std::forward(val)); } }; diff --git a/include/fkYAML/ordered_map.hpp b/include/fkYAML/ordered_map.hpp index 42aeb3e7..aa8580d1 100644 --- a/include/fkYAML/ordered_map.hpp +++ b/include/fkYAML/ordered_map.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -21,7 +21,6 @@ #include #include -/// @brief namespace for fkYAML library. FK_YAML_NAMESPACE_BEGIN /// @brief A minimal map-like container which preserves insertion order. @@ -33,8 +32,7 @@ FK_YAML_NAMESPACE_BEGIN template < typename Key, typename Value, typename IgnoredCompare = std::less, typename Allocator = std::allocator>> -class ordered_map : public std::vector, Allocator> -{ +class ordered_map : public std::vector, Allocator> { public: /// A type for keys. using key_type = Key; @@ -57,16 +55,14 @@ class ordered_map : public std::vector, Allocator> /// @brief Construct a new ordered_map object. /// @sa https://fktn-k.github.io/fkYAML/api/ordered_map/constructor/ ordered_map() noexcept(noexcept(Container())) - : Container() - { + : Container() { } /// @brief Construct a new ordered_map object with an initializer list. /// @param init An initializer list to construct the inner container object. /// @sa https://fktn-k.github.io/fkYAML/api/ordered_map/constructor/ ordered_map(std::initializer_list init) - : Container {init} - { + : Container {init} { } public: @@ -78,8 +74,7 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - mapped_type& operator[](KeyType&& key) noexcept - { + mapped_type& operator[](KeyType&& key) noexcept { return emplace(std::forward(key), mapped_type()).first->second; } @@ -93,12 +88,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - std::pair emplace(KeyType&& key, const mapped_type& value) noexcept - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + std::pair emplace(KeyType&& key, const mapped_type& value) noexcept { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return {itr, false}; } } @@ -114,12 +106,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - mapped_type& at(KeyType&& key) - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + mapped_type& at(KeyType&& key) { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return itr->second; } } @@ -134,12 +123,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - const mapped_type& at(KeyType&& key) const - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + const mapped_type& at(KeyType&& key) const { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return itr->second; } } @@ -154,12 +140,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - iterator find(KeyType&& key) noexcept - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + iterator find(KeyType&& key) noexcept { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return itr; } } @@ -174,12 +157,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - const_iterator find(KeyType&& key) const noexcept - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + const_iterator find(KeyType&& key) const noexcept { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return itr; } } diff --git a/scripts/.gitignore b/scripts/.gitignore new file mode 100644 index 00000000..f85e3da5 --- /dev/null +++ b/scripts/.gitignore @@ -0,0 +1 @@ +/venv* diff --git a/scripts/check_amalgamation.bat b/scripts/check_amalgamation.bat new file mode 100644 index 00000000..8a382525 --- /dev/null +++ b/scripts/check_amalgamation.bat @@ -0,0 +1,24 @@ +@echo off +setlocal + +set CALLER_DIR=%cd% + +set SCRIPT_DIR=%~dp0 +cd $SCRIPT_DIR%\.. + +set SINGLE_HEADER_PATH=%SCRIPT_DIR%\..\single_include\fkYAML\node.hpp + +move %SINGLE_HEADER_PATH% %SINGLE_HEADER_PATH%~ > nul +call %SCRIPT_DIR%\run_amalgamation.bat > nul + +fc %SINGLE_HEADER_PATH% %SINGLE_HEADER_PATH%~ > nul +if %errorlevel% == 0 ( + echo Amalgamation check passed! +) else ( + echo Amalgamation required! + echo Please follow the guideline in the CONTRIBUTING.md file. +) + +move %SINGLE_HEADER_PATH%~ %SINGLE_HEADER_PATH% > nul + +cd %CALLER_DIR% diff --git a/scripts/check_amalgamation.sh b/scripts/check_amalgamation.sh new file mode 100755 index 00000000..d82ad311 --- /dev/null +++ b/scripts/check_amalgamation.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -eu + +ROOT_DIR="$(dirname "$0")/.." + +SINGLE_HEADER_PATH="$ROOT_DIR/single_include/fkYAML/node.hpp" +mv "$SINGLE_HEADER_PATH" "$SINGLE_HEADER_PATH~" +"$ROOT_DIR"/scripts/run_amalgamation.sh >/dev/null + +if cmp -s "$SINGLE_HEADER_PATH" "$SINGLE_HEADER_PATH~" ; then + echo Amalgamation check passed! +else + echo Amalgamation required. + echo Please follow the guideline in the CONTRIBUTING.md file. +fi + +mv "$SINGLE_HEADER_PATH~" "$SINGLE_HEADER_PATH" diff --git a/scripts/run_amalgamation.bat b/scripts/run_amalgamation.bat new file mode 100644 index 00000000..52ed9652 --- /dev/null +++ b/scripts/run_amalgamation.bat @@ -0,0 +1,18 @@ +@echo off +setlocal + +set CALLER_DIR=%cd% + +set SCRIPT_PATH=%~dp0 +set AMALGAMATION_TOOL_DIR=%SCRIPT_PATH%\..\tool\amalgamation +cd %SCRIPT_PATH%\.. + +python %AMALGAMATION_TOOL_DIR%\amalgamate.py -c %AMALGAMATION_TOOL_DIR%\fkYAML.json -s . --verbose=yes + +if %errorlevel% == 0 ( + echo Amalgamation succeeded! +) else ( + echo Amalgamation failed! +) + +cd %CALLER_DIR% diff --git a/scripts/run_amalgamation.sh b/scripts/run_amalgamation.sh new file mode 100755 index 00000000..cf71f77d --- /dev/null +++ b/scripts/run_amalgamation.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +ROOT_DIR="$(dirname "$0")"/.. +python3 "$ROOT_DIR"/tool/amalgamation/amalgamate.py -c "$ROOT_DIR"/tool/amalgamation/fkYAML.json -s . --verbose=yes diff --git a/scripts/run_clang_format.bat b/scripts/run_clang_format.bat new file mode 100644 index 00000000..ce38fcb1 --- /dev/null +++ b/scripts/run_clang_format.bat @@ -0,0 +1,28 @@ +@echo off +setlocal + +set CALLER_DIR=%cd% + +set SCRIPT_PATH=%~dp0 +cd %SCRIPT_PATH% + +set ROOT_PATH=%SCRIPT_PATH%\.. + +if not exist .\venv_clang_format\Scripts\clang-format.exe ( + python -m venv .\venv_clang_format + .\venv_clang_format\Scripts\pip.exe install clang-format==14.0.0 +) + +for /r %ROOT_PATH%\include %%f in (*.hpp) do ( + .\venv_clang_format\Scripts\clang-format.exe %%f -i +) + +for /r %ROOT_PATH%\test %%f in (*.cpp) do ( + .\venv_clang_format\Scripts\clang-format.exe %%f -i +) + +for /r %ROOT_PATH%\docs\examples %%f in (*.cpp) do ( + .\venv_clang_format\Scripts\clang-format.exe %%f -i +) + +cd %CALLER_DIR% diff --git a/scripts/run_clang_format.sh b/scripts/run_clang_format.sh new file mode 100755 index 00000000..8c738f8a --- /dev/null +++ b/scripts/run_clang_format.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +SCRIPT_DIR="$(dirname "$0")" +ROOT_DIR="$SCRIPT_DIR/.." + +# install the clang-format package if not installed yet. +if [ ! -e "$SCRIPT_DIR"/venv_clang_format/bin/clang-format ]; then + python3 -m venv "$SCRIPT_DIR/venv_clang_format" + "$SCRIPT_DIR"/venv_clang_format/bin/pip install clang-format==14.0.0 +fi + +for f in $(find "$ROOT_DIR/include" "$ROOT_DIR/test" "$ROOT_DIR/docs/examples" -type f -name "*.hpp" -o -name "*.cpp" | sort); do + "$SCRIPT_DIR"/venv_clang_format/bin/clang-format "$f" -i +done diff --git a/single_include/fkYAML/node.hpp b/single_include/fkYAML/node.hpp index 2f5007bc..46e2f0aa 100644 --- a/single_include/fkYAML/node.hpp +++ b/single_include/fkYAML/node.hpp @@ -1,6 +1,6 @@ /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -24,7 +24,7 @@ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -34,42 +34,47 @@ // Check version definitions if already defined. #if defined(FK_YAML_MAJOR_VERSION) && defined(FK_YAML_MINOR_VERSION) && defined(FK_YAML_PATCH_VERSION) - #if FK_YAML_MAJOR_VERSION != 0 || FK_YAML_MINOR_VERSION != 3 || FK_YAML_PATCH_VERSION != 4 - #warning Already included a different version of the fkYAML library! - #else - // define macros to skip defining macros down below. - #define FK_YAML_VERCHECK_SUCCEEDED - #endif +#if FK_YAML_MAJOR_VERSION != 0 || FK_YAML_MINOR_VERSION != 3 || FK_YAML_PATCH_VERSION != 5 +#warning Already included a different version of the fkYAML library! +#else +// define macros to skip defining macros down below. +#define FK_YAML_VERCHECK_SUCCEEDED +#endif #endif #ifndef FK_YAML_VERCHECK_SUCCEEDED - #define FK_YAML_MAJOR_VERSION 0 - #define FK_YAML_MINOR_VERSION 3 - #define FK_YAML_PATCH_VERSION 4 +#define FK_YAML_MAJOR_VERSION 0 +#define FK_YAML_MINOR_VERSION 3 +#define FK_YAML_PATCH_VERSION 5 - #define FK_YAML_NAMESPACE_VERSION_CONCAT_IMPL(major, minor, patch) v##major##_##minor##_##patch +#define FK_YAML_NAMESPACE_VERSION_CONCAT_IMPL(major, minor, patch) v##major##_##minor##_##patch - #define FK_YAML_NAMESPACE_VERSION_CONCAT(major, minor, patch) \ - FK_YAML_NAMESPACE_VERSION_CONCAT_IMPL(major, minor, patch) +#define FK_YAML_NAMESPACE_VERSION_CONCAT(major, minor, patch) FK_YAML_NAMESPACE_VERSION_CONCAT_IMPL(major, minor, patch) - #define FK_YAML_NAMESPACE_VERSION \ - FK_YAML_NAMESPACE_VERSION_CONCAT(FK_YAML_MAJOR_VERSION, FK_YAML_MINOR_VERSION, FK_YAML_PATCH_VERSION) +#define FK_YAML_NAMESPACE_VERSION \ + FK_YAML_NAMESPACE_VERSION_CONCAT(FK_YAML_MAJOR_VERSION, FK_YAML_MINOR_VERSION, FK_YAML_PATCH_VERSION) - #define FK_YAML_NAMESPACE_BEGIN \ - namespace fkyaml \ - { \ - inline namespace FK_YAML_NAMESPACE_VERSION \ - { +#define FK_YAML_NAMESPACE_BEGIN \ + namespace fkyaml { \ + inline namespace FK_YAML_NAMESPACE_VERSION { - #define FK_YAML_NAMESPACE_END \ - } /* namespace (inline namespace) */ \ - } // namespace fkyaml +#define FK_YAML_NAMESPACE_END \ + } /* inline namespace FK_YAML_NAMESPACE_VERSION */ \ + } // namespace fkyaml - // #include +#define FK_YAML_DETAIL_NAMESPACE_BEGIN \ + FK_YAML_NAMESPACE_BEGIN \ + namespace detail { + +#define FK_YAML_DETAIL_NAMESPACE_END \ + } /* namespace detail */ \ + FK_YAML_NAMESPACE_END + +// #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -87,42 +92,42 @@ // Skip detection if the definitions listed below already exist. #if !defined(FK_YAML_HAS_CXX_20) && !defined(FK_YAML_HAS_CXX_17) && !defined(FK_YAML_HAS_CXX_14) && \ !defined(FK_YAML_CXX_11) - #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && MSVC_LANG >= 202002L) - #define FK_YAML_HAS_CXX_20 - #define FK_YAML_HAS_CXX_17 - #define FK_YAML_HAS_CXX_14 - #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) - #define FK_YAML_HAS_CXX_17 - #define FK_YAML_HAS_CXX_14 - #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) - #define FK_YAML_HAS_CXX_14 - #endif - - // C++11 is the minimum required version of the fkYAML library. - #define FK_YAML_HAS_CXX_11 +#if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && MSVC_LANG >= 202002L) +#define FK_YAML_HAS_CXX_20 +#define FK_YAML_HAS_CXX_17 +#define FK_YAML_HAS_CXX_14 +#elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) +#define FK_YAML_HAS_CXX_17 +#define FK_YAML_HAS_CXX_14 +#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) +#define FK_YAML_HAS_CXX_14 +#endif + +// C++11 is the minimum required version of the fkYAML library. +#define FK_YAML_HAS_CXX_11 #endif // switch usage of inline variables. Inline variables have been introduced since C++17. #if defined(FK_YAML_HAS_CXX_17) - #define FK_YAML_INLINE_VAR inline +#define FK_YAML_INLINE_VAR inline #else - #define FK_YAML_INLINE_VAR +#define FK_YAML_INLINE_VAR #endif #ifdef __has_include - #if __has_include() - // is available since C++20 - #include - #endif +#if __has_include() +// is available since C++20 +#include +#endif #endif // switch usage of char8_t. char8_t has been introduced since C++20 #if !defined(FK_YAML_HAS_CHAR8_T) - #if defined(FK_YAML_HAS_CXX_20) - #if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L - #define FK_YAML_HAS_CHAR8_T - #endif - #endif +#if defined(FK_YAML_HAS_CXX_20) +#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L +#define FK_YAML_HAS_CHAR8_T +#endif +#endif #endif #endif /* FK_YAML_DETAIL_MACROS_CPP_CONFIG_MACROS_HPP_ */ @@ -133,7 +138,7 @@ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -146,12 +151,12 @@ // if FK_YAML_ASSERT is not user-defined. apply the default assert impl. #ifndef FK_YAML_ASSERT - #ifndef NDEBUG - #include - #define FK_YAML_ASSERT(x) assert(x) // NOLINT(cppcoreguidelines-macro-usage) - #else - #define FK_YAML_ASSERT(x) // NOLINT(cppcoreguidelines-macro-usage) - #endif +#ifndef NDEBUG +#include +#define FK_YAML_ASSERT(x) assert(x) // NOLINT(cppcoreguidelines-macro-usage) +#else +#define FK_YAML_ASSERT(x) // NOLINT(cppcoreguidelines-macro-usage) +#endif #endif #endif /* FK_YAML_DETAIL_ASSERT_HPP_ */ @@ -159,7 +164,7 @@ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -178,7 +183,7 @@ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -194,39 +199,23 @@ // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of YAML version types. -enum class yaml_version_t : std::uint32_t -{ +enum class yaml_version_t : std::uint32_t { VER_1_1, //!< YAML version 1.1 VER_1_2, //!< YAML version 1.2 }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_TYPES_YAML_VERSION_T_HPP_ */ -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementaions of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief The set of directives for a YAML document. -struct directive_set -{ +struct directive_set { /// The YAML version used for the YAML document. yaml_version_t version {yaml_version_t::VER_1_2}; /// Whether or not the YAML version has been specified. @@ -239,16 +228,14 @@ struct directive_set std::map named_handle_map {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_DIRECTIVE_SET_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -272,7 +259,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -298,7 +285,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -320,7 +307,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -337,15 +324,7 @@ FK_YAML_NAMESPACE_END // #include -/** - * @namespace fkyaml - * @brief namespace for fkYAML library. - */ -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN ///////////////////////////////////////////////////////////////////////////////////////////////////////// // For contributors: @@ -378,9 +357,7 @@ using enable_if_t = typename std::enable_if::type; /// @sa https://en.cppreference.com/w/cpp/types/is_null_pointer /// @tparam T The type to be checked if it's equal to std::nullptr_t. template -struct is_null_pointer : std::is_same::type> -{ -}; +struct is_null_pointer : std::is_same::type> {}; /// @brief An alias template for std::remove_cv::type with C++11. /// @note std::remove_cv_t is available since C++14. @@ -428,24 +405,18 @@ using bool_constant = std::integral_constant; /// @sa https://en.cppreference.com/w/cpp/types/conjunction /// @tparam Traits Type traits to be checked if their ::value are all true. template -struct conjunction : std::true_type -{ -}; +struct conjunction : std::true_type {}; /// @brief A partial specialization of conjunction if only one Trait is given. /// @tparam Trait Type trait to be checked if its ::value is true. template -struct conjunction : Trait -{ -}; +struct conjunction : Trait {}; /// @brief A partial specialization of conjunction if more than one traits are given. /// @tparam First The first type trait to be checked if its ::value is true. /// @tparam Rest The rest of traits passed as another conjunction template arguments if First::value is true. template -struct conjunction : std::conditional, First>::type -{ -}; +struct conjunction : std::conditional, First>::type {}; /// @brief A simple implementation to use std::disjunction with C++11/C++14. /// @note @@ -454,39 +425,30 @@ struct conjunction : std::conditional -struct disjunction : std::false_type -{ -}; +struct disjunction : std::false_type {}; /// @brief A partial specialization of disjunction if only one Trait is given. /// @tparam Trait Type trait to be checked if its ::value is true. template -struct disjunction : Trait -{ -}; +struct disjunction : Trait {}; /// @brief A partial specialization of disjunction if more than one traits are given. /// @tparam First The first type trait to be checked if its ::value is true. /// @tparam Rest The rest of traits passed as another conjunction template arguments if First::value is false. template -struct disjunction : std::conditional>::type -{ -}; +struct disjunction : std::conditional>::type {}; /// @brief A simple implementation to use std::negation with C++11/C++14. /// @note std::negation is available since C++17. /// @sa https://en.cppreference.com/w/cpp/types/negation /// @tparam Trait Type trait whose ::value is negated. template -struct negation : std::integral_constant -{ -}; +struct negation : std::integral_constant {}; /// @brief A helper for void_t. /// @tparam Types Any types to be transformed to void type. template -struct make_void -{ +struct make_void { using type = void; }; @@ -522,16 +484,14 @@ using std::remove_cvref_t; #endif -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_STL_SUPPLEMENT_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -550,7 +510,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -568,16 +528,10 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A dummy struct to represent detection failure. -struct nonesuch -{ +struct nonesuch { nonesuch() = delete; ~nonesuch() = delete; nonesuch(const nonesuch&) = delete; @@ -587,8 +541,7 @@ struct nonesuch }; /// @brief namespace to implement detector type traits -namespace detector_impl -{ +namespace detector_impl { /// @brief A helper for general type detection. /// @tparam Default A type to represent detection failure. @@ -596,8 +549,7 @@ namespace detector_impl /// @tparam Op A type for desired operation type. /// @tparam Args Argument types passed to desired operation. template class Op, typename... Args> -struct detector : std::false_type -{ +struct detector : std::false_type { /// @brief A type which represents detection failure. using type = Default; }; @@ -607,8 +559,7 @@ struct detector : std::false_type /// @tparam Op A type for desired operation type. /// @tparam Args Argument types passed to desired operation. template class Op, typename... Args> -struct detector>, Op, Args...> : std::true_type -{ +struct detector>, Op, Args...> : std::true_type { /// @brief A detected type. using type = Op; }; @@ -634,21 +585,14 @@ using detected_t = typename detector_impl::detector template class Op, typename... Args> using is_detected_exact = std::is_same>; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_DETECT_HPP_ */ // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Type trait to check if T and U are comparable types. /// @tparam Comparator An object type to compare T and U objects. @@ -656,9 +600,7 @@ namespace detail /// @tparam U The other type for comparison. /// @tparam typename Placeholder for determining T and U are comparable types. template -struct is_comparable : std::false_type -{ -}; +struct is_comparable : std::false_type {}; /// @brief A partial specialization of is_comparable if T and U are comparable types. /// @tparam Comparator An object type to compare T and U objects. @@ -669,9 +611,7 @@ struct is_comparable< Comparator, T, U, void_t< decltype(std::declval()(std::declval(), std::declval())), - decltype(std::declval()(std::declval(), std::declval()))>> : std::true_type -{ -}; + decltype(std::declval()(std::declval(), std::declval()))>> : std::true_type {}; /// @brief Type trait to check if KeyType can be used as key type. /// @tparam Comparator An object type to compare T and U objects. @@ -685,9 +625,7 @@ using is_usable_as_key_type = typename std::conditional< /// @tparam IntegralType A type to be checked. /// @tparam typename N/A template -struct is_non_bool_integral : std::false_type -{ -}; +struct is_non_bool_integral : std::false_type {}; /// @brief A partial specialization of is_non_bool_integral if IntegralType is of non-boolean integral types. /// @tparam IntegralType A type to be checked. @@ -695,9 +633,7 @@ template struct is_non_bool_integral< IntegralType, enable_if_t, negation>>::value>> - : std::true_type -{ -}; + : std::true_type {}; /// @brief Type traits to check if Types are all signed arithmetic types. /// @tparam Types Types to check if they are all signed arithmetic types. @@ -715,9 +651,7 @@ using is_all_unsigned = conjunction...>; /// @tparam CompatibleIntegerType A compatible integer type. /// @tparam typename N/A template -struct is_compatible_integer_type_impl : std::false_type -{ -}; +struct is_compatible_integer_type_impl : std::false_type {}; /// @brief A partial specialization of is_compatible_integer_type_impl if TargetIntegerType and CompatibleIntegerType /// are compatible integer types. @@ -726,38 +660,29 @@ struct is_compatible_integer_type_impl : std::false_type template struct is_compatible_integer_type_impl< TargetIntegerType, CompatibleIntegerType, enable_if_t::value>> - : std::true_type -{ -}; + : std::true_type {}; /// @brief Type traits to check if TargetIntegerType and CompatibleIntegerType are compatible integer types. /// @tparam TargetIntegerType A target integer type. /// @tparam CompatibleIntegerType A compatible integer type. template -struct is_compatible_integer_type : is_compatible_integer_type_impl -{ -}; +struct is_compatible_integer_type : is_compatible_integer_type_impl {}; /// @brief Type traits to check if T is a complete type. /// @tparam T A type to be checked if a complete type. /// @tparam typename N/A template -struct is_complete_type : std::false_type -{ -}; +struct is_complete_type : std::false_type {}; /// @brief A partial specialization of is_complete_type if T is a complete type. /// @tparam T template -struct is_complete_type : std::true_type -{ -}; +struct is_complete_type : std::true_type {}; /// @brief A utility struct to generate static constant instance. /// @tparam T A target type for the resulting static constant instance. template -struct static_const -{ +struct static_const { static FK_YAML_INLINE_VAR constexpr T value {}; // NOLINT(readability-identifier-naming) }; @@ -772,8 +697,7 @@ constexpr T static_const::value; /// @brief A helper structure for tag dispatch. /// @tparam T A tag type. template -struct type_tag -{ +struct type_tag { /// @brief A tagged type. using type = T; }; @@ -786,8 +710,7 @@ struct get_head_type; /// @brief A specialization of get_head_type if variadic template has no arguments. /// @tparam N/A template <> -struct get_head_type<> -{ +struct get_head_type<> { /// @brief A head type using type = void; }; @@ -796,8 +719,7 @@ struct get_head_type<> /// @tparam First The first type in the arguments /// @tparam Rest The rest of the types in the arguments. template -struct get_head_type -{ +struct get_head_type { /// @brief A head type. using type = First; }; @@ -807,16 +729,14 @@ struct get_head_type template using head_type = typename get_head_type::type; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_TYPE_TRAITS_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -828,6 +748,7 @@ FK_YAML_NAMESPACE_END #define FK_YAML_EXCEPTION_HPP_ #include +#include #include #include @@ -836,7 +757,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -855,23 +776,16 @@ FK_YAML_NAMESPACE_END // #include -/// @namespace namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN +FK_YAML_DETAIL_NAMESPACE_BEGIN -/// @namespace namespace for internal implementation of fkYAML library. -namespace detail -{ - -inline std::string format(const char* fmt, ...) -{ +inline std::string format(const char* fmt, ...) { va_list vl; va_start(vl, fmt); int size = std::vsnprintf(nullptr, 0, fmt, vl); va_end(vl); // LCOV_EXCL_START - if (size < 0) - { + if (size < 0) { return ""; } // LCOV_EXCL_STOP @@ -885,16 +799,14 @@ inline std::string format(const char* fmt, ...) return std::string(buffer.get(), size); } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_STRING_FORMATTER_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -911,16 +823,10 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of node value types. -enum class node_t : std::uint32_t -{ +enum class node_t : std::uint32_t { SEQUENCE, //!< sequence value type MAPPING, //!< mapping value type NULL_OBJECT, //!< null value type @@ -930,10 +836,8 @@ enum class node_t : std::uint32_t STRING, //!< string value type }; -inline const char* to_string(node_t t) noexcept -{ - switch (t) - { +inline const char* to_string(node_t t) noexcept { + switch (t) { case node_t::SEQUENCE: return "sequence"; case node_t::MAPPING: @@ -953,20 +857,16 @@ inline const char* to_string(node_t t) noexcept } } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_TYPES_NODE_T_HPP_ */ -/// @brief namespace for fkYAML library. FK_YAML_NAMESPACE_BEGIN /// @brief A base exception class used in fkYAML library. /// @sa https://fktn-k.github.io/fkYAML/api/exception/ -class exception : public std::exception -{ +class exception : public std::exception { public: /// @brief Construct a new exception object without any error messages. /// @sa https://fktn-k.github.io/fkYAML/api/exception/constructor/ @@ -975,10 +875,8 @@ class exception : public std::exception /// @brief Construct a new exception object with an error message. /// @param[in] msg An error message. /// @sa https://fktn-k.github.io/fkYAML/api/exception/constructor/ - explicit exception(const char* msg) noexcept - { - if (msg) - { + explicit exception(const char* msg) noexcept { + if (msg) { m_error_msg = msg; } } @@ -987,8 +885,7 @@ class exception : public std::exception /// @brief Returns an error message internally held. If nothing, a non-null, empty string will be returned. /// @return An error message internally held. The message might be empty. /// @sa https://fktn-k.github.io/fkYAML/api/exception/what/ - const char* what() const noexcept override - { + const char* what() const noexcept override { return m_error_msg.c_str(); } @@ -999,13 +896,13 @@ class exception : public std::exception /// @brief An exception class indicating an encoding error. /// @sa https://fktn-k.github.io/fkYAML/api/exception/invalid_encoding/ -class invalid_encoding : public exception -{ +class invalid_encoding : public exception { public: - template - explicit invalid_encoding(const char* msg, std::array u8) noexcept - : exception(generate_error_message(msg, u8).c_str()) - { + /// @brief Construct a new invalid_encoding object for UTF-8 related errors. + /// @param msg An error message. + /// @param u8 The UTF-8 character bytes. + explicit invalid_encoding(const char* msg, const std::initializer_list& u8) noexcept + : exception(generate_error_message(msg, u8).c_str()) { } /// @brief Construct a new invalid_encoding object for UTF-16 related errors. @@ -1013,26 +910,23 @@ class invalid_encoding : public exception /// @param u16_h The first UTF-16 encoded element used for the UTF-8 encoding. /// @param u16_l The second UTF-16 encoded element used for the UTF-8 encoding. explicit invalid_encoding(const char* msg, std::array u16) noexcept - : exception(generate_error_message(msg, u16).c_str()) - { + : exception(generate_error_message(msg, u16).c_str()) { } /// @brief Construct a new invalid_encoding object for UTF-32 related errors. /// @param msg An error message. /// @param u32 The UTF-32 encoded element used for the UTF-8 encoding. explicit invalid_encoding(const char* msg, char32_t u32) noexcept - : exception(generate_error_message(msg, u32).c_str()) - { + : exception(generate_error_message(msg, u32).c_str()) { } private: - template - std::string generate_error_message(const char* msg, std::array u8) const noexcept - { - std::string formatted = detail::format("invalid_encoding: %s in=[ 0x%02x", msg, u8[0]); - for (std::size_t i = 1; i < N; i++) - { - formatted += detail::format(", 0x%02x", u8[i]); + std::string generate_error_message(const char* msg, const std::initializer_list& u8) const noexcept { + auto itr = u8.begin(); + auto end_itr = u8.end(); + std::string formatted = detail::format("invalid_encoding: %s in=[ 0x%02x", msg, *itr++); + while (itr != end_itr) { + formatted += detail::format(", 0x%02x", *itr++); } formatted += " ]"; return formatted; @@ -1043,8 +937,7 @@ class invalid_encoding : public exception /// @param h The first UTF-16 encoded element used for the UTF-8 encoding. /// @param l The second UTF-16 encoded element used for the UTF-8 encoding. /// @return A generated error message. - std::string generate_error_message(const char* msg, std::array u16) const noexcept - { + std::string generate_error_message(const char* msg, std::array u16) const noexcept { // uint16_t is large enough for UTF-16 encoded elements. return detail::format("invalid_encoding: %s in=[ 0x%04x, 0x%04x ]", msg, uint16_t(u16[0]), uint16_t(u16[1])); } @@ -1053,40 +946,34 @@ class invalid_encoding : public exception /// @param msg An error message. /// @param u32 The UTF-32 encoded element used for the UTF-8 encoding. /// @return A genereated error message. - std::string generate_error_message(const char* msg, char32_t u32) const noexcept - { + std::string generate_error_message(const char* msg, char32_t u32) const noexcept { // uint32_t is large enough for UTF-32 encoded elements. return detail::format("invalid_encoding: %s in=0x%08x", msg, uint32_t(u32)); } }; /// @brief An exception class indicating an error in parsing. -class parse_error : public exception -{ +class parse_error : public exception { public: explicit parse_error(const char* msg, std::size_t lines, std::size_t cols_in_line) noexcept - : exception(generate_error_message(msg, lines, cols_in_line).c_str()) - { + : exception(generate_error_message(msg, lines, cols_in_line).c_str()) { } private: - std::string generate_error_message(const char* msg, std::size_t lines, std::size_t cols_in_line) const noexcept - { + std::string generate_error_message(const char* msg, std::size_t lines, std::size_t cols_in_line) const noexcept { return detail::format("parse_error: %s (at line %zu, column %zu)", msg, lines, cols_in_line); } }; /// @brief An exception class indicating an invalid type conversion. /// @sa https://fktn-k.github.io/fkYAML/api/exception/type_error/ -class type_error : public exception -{ +class type_error : public exception { public: /// @brief Construct a new type_error object with an error message and a node type. /// @param[in] msg An error message. /// @param[in] type The type of a source node value. explicit type_error(const char* msg, detail::node_t type) noexcept - : exception(generate_error_message(msg, type).c_str()) - { + : exception(generate_error_message(msg, type).c_str()) { } private: @@ -1094,48 +981,39 @@ class type_error : public exception /// @param msg An error message. /// @param type The type of a source node value. /// @return A generated error message. - std::string generate_error_message(const char* msg, detail::node_t type) const noexcept - { + std::string generate_error_message(const char* msg, detail::node_t type) const noexcept { return detail::format("type_error: %s type=%s", msg, detail::to_string(type)); } }; -class out_of_range : public exception -{ +class out_of_range : public exception { public: explicit out_of_range(int index) noexcept - : exception(generate_error_message(index).c_str()) - { + : exception(generate_error_message(index).c_str()) { } explicit out_of_range(const char* key) noexcept - : exception(generate_error_message(key).c_str()) - { + : exception(generate_error_message(key).c_str()) { } private: - std::string generate_error_message(int index) - { + std::string generate_error_message(int index) { return detail::format("out_of_range: index %d is out of range", index); } - std::string generate_error_message(const char* key) - { + std::string generate_error_message(const char* key) { return detail::format("out_of_range: key \'%s\' is not found.", key); } }; -class invalid_tag : public exception -{ +class invalid_tag : public exception { public: explicit invalid_tag(const char* msg, const char* tag) - : exception(generate_error_message(msg, tag).c_str()) - { + : exception(generate_error_message(msg, tag).c_str()) { } private: - std::string generate_error_message(const char* msg, const char* tag) - { + std::string generate_error_message(const char* msg, const char* tag) { return detail::format("invalid_tag: %s tag=%s", msg, tag); } }; @@ -1145,11 +1023,7 @@ FK_YAML_NAMESPACE_END #endif /* FK_YAML_EXCEPTION_HPP_ */ -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Convert a string YAML token to a ValueType object. /// @tparam ValueType A target value type. @@ -1160,10 +1034,8 @@ inline ValueType from_string(const std::basic_string& s, type_tag -inline std::nullptr_t from_string(const std::string& s, type_tag /*unused*/) -{ - if (s == "null" || s == "Null" || s == "NULL" || s == "~") - { +inline std::nullptr_t from_string(const std::string& s, type_tag /*unused*/) { + if (s == "null" || s == "Null" || s == "NULL" || s == "~") { return nullptr; } @@ -1173,15 +1045,12 @@ inline std::nullptr_t from_string(const std::string& s, type_tag /// @brief Specialization of from_string() for boolean values with std::string. /// @tparam N/A template <> -inline bool from_string(const std::string& s, type_tag /*unused*/) -{ - if (s == "true" || s == "True" || s == "TRUE") - { +inline bool from_string(const std::string& s, type_tag /*unused*/) { + if (s == "true" || s == "True" || s == "TRUE") { return true; } - if (s == "false" || s == "False" || s == "FALSE") - { + if (s == "false" || s == "False" || s == "FALSE") { return false; } @@ -1191,17 +1060,14 @@ inline bool from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for int values with std::string. /// @tparam N/A template <> -inline int from_string(const std::string& s, type_tag /*unused*/) -{ +inline int from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; long ret = 0; - try - { + try { ret = std::stoi(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into an int value."); } @@ -1211,17 +1077,14 @@ inline int from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for long values with std::string. /// @tparam N/A template <> -inline long from_string(const std::string& s, type_tag /*unused*/) -{ +inline long from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; long ret = 0; - try - { + try { ret = std::stol(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into a long value."); } @@ -1231,17 +1094,14 @@ inline long from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for long long values with std::string. /// @tparam N/A template <> -inline long long from_string(const std::string& s, type_tag /*unused*/) -{ +inline long long from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; long long ret = 0; - try - { + try { ret = std::stoll(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into a long long value."); } @@ -1256,11 +1116,9 @@ inline enable_if_t< is_non_bool_integral, std::is_signed, negation>, negation>, negation>>::value, SignedIntType> -from_string(const std::string& s, type_tag /*unused*/) -{ +from_string(const std::string& s, type_tag /*unused*/) { const auto tmp_ret = from_string(s, type_tag {}); - if (static_cast(std::numeric_limits::max()) < tmp_ret) - { + if (static_cast(std::numeric_limits::max()) < tmp_ret) { throw exception("Failed to convert a long long value into a SignedIntegerType value."); } @@ -1270,17 +1128,14 @@ from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for unsigned long values with std::string. /// @tparam N/A template <> -inline unsigned long from_string(const std::string& s, type_tag /*unused*/) -{ +inline unsigned long from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; unsigned long ret = 0; - try - { + try { ret = std::stoul(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into an unsigned long value."); } @@ -1290,17 +1145,14 @@ inline unsigned long from_string(const std::string& s, type_tag / /// @brief Specialization of from_string() for unsigned long long values with std::string. /// @tparam N/A template <> -inline unsigned long long from_string(const std::string& s, type_tag /*unused*/) -{ +inline unsigned long long from_string(const std::string& s, type_tag /*unused*/) { std::size_t idx = 0; unsigned long long ret = 0; - try - { + try { ret = std::stoull(s, &idx, 0); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into an unsigned long long value."); } @@ -1316,11 +1168,9 @@ inline enable_if_t< negation>, negation>>::value, UnsignedIntType> -from_string(const std::string& s, type_tag /*unused*/) -{ +from_string(const std::string& s, type_tag /*unused*/) { const auto tmp_ret = from_string(s, type_tag {}); - if (static_cast(std::numeric_limits::max()) < tmp_ret) - { + if (static_cast(std::numeric_limits::max()) < tmp_ret) { throw exception("Failed to convert an unsigned long long into an unsigned integer value."); } @@ -1330,33 +1180,27 @@ from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for float values with std::string. /// @tparam N/A template <> -inline float from_string(const std::string& s, type_tag /*unused*/) -{ - if (s == ".inf" || s == ".Inf" || s == ".INF") - { +inline float from_string(const std::string& s, type_tag /*unused*/) { + if (s == ".inf" || s == ".Inf" || s == ".INF") { return std::numeric_limits::infinity(); } - if (s == "-.inf" || s == "-.Inf" || s == "-.INF") - { + if (s == "-.inf" || s == "-.Inf" || s == "-.INF") { static_assert(std::numeric_limits::is_iec559, "IEEE 754 required."); return -1 * std::numeric_limits::infinity(); } - if (s == ".nan" || s == ".NaN" || s == ".NAN") - { + if (s == ".nan" || s == ".NaN" || s == ".NAN") { return std::nanf(""); } std::size_t idx = 0; float ret = 0.0f; - try - { + try { ret = std::stof(s, &idx); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into a float value."); } @@ -1366,49 +1210,41 @@ inline float from_string(const std::string& s, type_tag /*unused*/) /// @brief Specialization of from_string() for double values with std::string. /// @tparam N/A template <> -inline double from_string(const std::string& s, type_tag /*unused*/) -{ - if (s == ".inf" || s == ".Inf" || s == ".INF") - { +inline double from_string(const std::string& s, type_tag /*unused*/) { + if (s == ".inf" || s == ".Inf" || s == ".INF") { return std::numeric_limits::infinity(); } - if (s == "-.inf" || s == "-.Inf" || s == "-.INF") - { + if (s == "-.inf" || s == "-.Inf" || s == "-.INF") { static_assert(std::numeric_limits::is_iec559, "IEEE 754 required."); return -1 * std::numeric_limits::infinity(); } - if (s == ".nan" || s == ".NaN" || s == ".NAN") - { + if (s == ".nan" || s == ".NaN" || s == ".NAN") { return std::nan(""); } std::size_t idx = 0; double ret = 0.0; - try - { + try { ret = std::stod(s, &idx); } - catch (const std::exception& /*unused*/) - { + catch (const std::exception& /*unused*/) { throw exception("Failed to convert a string into a double value."); } return ret; } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_CONVERSIONS_FROM_STRING_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -1425,39 +1261,26 @@ FK_YAML_NAMESPACE_END // #include -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementaions of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A class which handles URI encodings. -class uri_encoding -{ +class uri_encoding { public: /// @brief Validates the encoding of the given character sequence. /// @param begin An iterator to the first element of the character sequence. /// @param end An iterator to the past-the-end element of the character sequence. /// @return true if all the characters are valid, false otherwise. - static bool validate(std::string::const_iterator begin, std::string::const_iterator end) noexcept - { - if (begin == end) - { + static bool validate(std::string::const_iterator begin, std::string::const_iterator end) noexcept { + if (begin == end) { return true; } std::string::const_iterator current = begin; - for (; current != end; ++current) - { - if (*current == '%') - { + for (; current != end; ++current) { + if (*current == '%') { bool are_valid_octets = validate_octets(++current, end); - if (!are_valid_octets) - { + if (!are_valid_octets) { return false; } @@ -1465,8 +1288,7 @@ class uri_encoding } bool is_allowed_character = validate_character(*current); - if (!is_allowed_character) - { + if (!is_allowed_character) { return false; } } @@ -1479,25 +1301,20 @@ class uri_encoding /// @param begin An iterator to the first octet. /// @param end An iterator to the past-the-end element of the whole character sequence. /// @return true if the octets are valid, false otherwise. - static bool validate_octets(std::string::const_iterator& begin, std::string::const_iterator& end) - { - for (int i = 0; i < 2; i++, ++begin) - { - if (begin == end) - { + static bool validate_octets(std::string::const_iterator& begin, std::string::const_iterator& end) { + for (int i = 0; i < 2; i++, ++begin) { + if (begin == end) { return false; } // Normalize a character for a-f/A-F comparison int octet = std::tolower(*begin); - if ('0' <= octet && octet <= '9') - { + if ('0' <= octet && octet <= '9') { continue; } - if ('a' <= octet && octet <= 'f') - { + if ('a' <= octet && octet <= 'f') { continue; } @@ -1510,15 +1327,13 @@ class uri_encoding /// @brief Verify if the given character is allowed as a URI character. /// @param c The target character. /// @return true if the given character is allowed as a URI character, false otherwise. - static bool validate_character(char c) - { + static bool validate_character(char c) { // Check if the current character is one of reserved/unreserved characters which are allowed for // use. See the following links for details: // * reserved characters: https://datatracker.ietf.org/doc/html/rfc3986#section-2.2 // * unreserved characters: https://datatracker.ietf.org/doc/html/rfc3986#section-2.3 - switch (c) - { + switch (c) { // reserved characters (gen-delims) case ':': case '/': @@ -1552,16 +1367,14 @@ class uri_encoding } }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_ENCODINGS_URI_ENCODING_HPP_ */ -// #include +// #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -1569,8 +1382,8 @@ FK_YAML_NAMESPACE_END /// /// @file -#ifndef FK_YAML_DETAIL_ENCODINGS_UTF_ENCODING_HPP_ -#define FK_YAML_DETAIL_ENCODINGS_UTF_ENCODING_HPP_ +#ifndef FK_YAML_DETAIL_ENCODINGS_UTF_ENCODINGS_HPP_ +#define FK_YAML_DETAIL_ENCODINGS_UTF_ENCODINGS_HPP_ #include #include @@ -1580,47 +1393,58 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ - -template -class utf_encoding; +FK_YAML_DETAIL_NAMESPACE_BEGIN ///////////////////////// // UTF-8 Encoding /// ///////////////////////// /// @brief A class which handles UTF-8 encodings. -class utf8_encoding -{ - using int_type = std::char_traits::int_type; +namespace utf8 { -public: - /// @brief Validates the encoding of a given byte array whose length is 1. - /// @param[in] byte_array The byte array to be validated. - /// @return true if a given byte array is valid, false otherwise. - static bool validate(std::array byte_array) noexcept - { - // U+0000..U+007F - return (0x00 <= byte_array[0] && byte_array[0] <= 0x7F); +/// @brief Query the number of UTF-8 character bytes with the first byte. +/// @param first_byte The first byte of a UTF-8 character. +/// @return The number of UTF-8 character bytes. +inline uint32_t get_num_bytes(uint8_t first_byte) { + // The first byte starts with 0b0XXX'XXXX -> 1-byte character + if (first_byte < 0x80) { + return 1; + } + // The first byte starts with 0b110X'XXXX -> 2-byte character + else if ((first_byte & 0xE0) == 0xC0) { + return 2; + } + // The first byte starts with 0b1110'XXXX -> 3-byte character + else if ((first_byte & 0xF0) == 0xE0) { + return 3; + } + // The first byte starts with 0b1111'0XXX -> 4-byte character + else if ((first_byte & 0xF8) == 0xF0) { + return 4; } - /// @brief Validates the encoding of a given byte array whose length is 2. - /// @param[in] byte_array The byte array to be validated. - /// @return true if a given byte array is valid, false otherwise. - static bool validate(std::array byte_array) noexcept - { + // The first byte starts with 0b10XX'XXXX or 0b1111'1XXX -> invalid + throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", {first_byte}); +} + +/// @brief Validates the encoding of a given byte array whose length is 1. +/// @param[in] byte_array The byte array to be validated. +/// @return true if a given byte array is valid, false otherwise. +inline bool validate(const std::initializer_list& byte_array) noexcept { + switch (byte_array.size()) { + case 1: + // U+0000..U+007F + return uint8_t(*(byte_array.begin())) <= uint8_t(0x7Fu); + case 2: { + auto itr = byte_array.begin(); + uint8_t first = *itr++; + uint8_t second = *itr; + // U+0080..U+07FF // 1st Byte: 0xC2..0xDF // 2nd Byte: 0x80..0xBF - if (0xC2 <= byte_array[0] && byte_array[0] <= 0xDF) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0xBF) - { + if (uint8_t(0xC2u) <= first && first <= uint8_t(0xDFu)) { + if (0x80 <= second && second <= 0xBF) { return true; } } @@ -1628,22 +1452,19 @@ class utf8_encoding // The rest of byte combinations are invalid. return false; } + case 3: { + auto itr = byte_array.begin(); + uint8_t first = *itr++; + uint8_t second = *itr++; + uint8_t third = *itr; - /// @brief Validates the encoding of a given byte array whose length is 3. - /// @param[in] byte_array The byte array to be validated. - /// @return true if a given byte array is valid, false otherwise. - static bool validate(std::array byte_array) noexcept - { // U+1000..U+CFFF: // 1st Byte: 0xE0..0xEC // 2nd Byte: 0x80..0xBF // 3rd Byte: 0x80..0xBF - if (0xE0 <= byte_array[0] && byte_array[0] <= 0xEC) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0xBF) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { + if (0xE0 <= first && first <= 0xEC) { + if (0x80 <= second && second <= 0xBF) { + if (0x80 <= third && third <= 0xBF) { return true; } } @@ -1654,12 +1475,9 @@ class utf8_encoding // 1st Byte: 0xED // 2nd Byte: 0x80..0x9F // 3rd Byte: 0x80..0xBF - if (byte_array[0] == 0xED) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0x9F) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { + if (first == 0xED) { + if (0x80 <= second && second <= 0x9F) { + if (0x80 <= third && third <= 0xBF) { return true; } } @@ -1670,12 +1488,9 @@ class utf8_encoding // 1st Byte: 0xEE..0xEF // 2nd Byte: 0x80..0xBF // 3rd Byte: 0x80..0xBF - if (byte_array[0] == 0xEE || byte_array[0] == 0xEF) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0xBF) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { + if (first == 0xEE || first == 0xEF) { + if (0x80 <= second && second <= 0xBF) { + if (0x80 <= third && third <= 0xBF) { return true; } } @@ -1685,25 +1500,22 @@ class utf8_encoding // The rest of byte combinations are invalid. return false; } + case 4: { + auto itr = byte_array.begin(); + uint8_t first = *itr++; + uint8_t second = *itr++; + uint8_t third = *itr++; + uint8_t fourth = *itr; - /// @brief Validates the encoding of a given byte array whose length is 4. - /// @param[in] byte_array The byte array to be validated. - /// @return true if a given byte array is valid, false otherwise. - static bool validate(std::array byte_array) noexcept - { // U+10000..U+3FFFF: // 1st Byte: 0xF0 // 2nd Byte: 0x90..0xBF // 3rd Byte: 0x80..0xBF // 4th Byte: 0x80..0xBF - if (byte_array[0] == 0xF0) - { - if (0x90 <= byte_array[1] && byte_array[1] <= 0xBF) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - if (0x80 <= byte_array[3] && byte_array[3] <= 0xBF) - { + if (first == 0xF0) { + if (0x90 <= second && second <= 0xBF) { + if (0x80 <= third && third <= 0xBF) { + if (0x80 <= fourth && fourth <= 0xBF) { return true; } } @@ -1716,14 +1528,10 @@ class utf8_encoding // 2nd Byte: 0x80..0xBF // 3rd Byte: 0x80..0xBF // 4th Byte: 0x80..0xBF - if (0xF1 <= byte_array[0] && byte_array[0] <= 0xF3) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0xBF) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - if (0x80 <= byte_array[3] && byte_array[3] <= 0xBF) - { + if (0xF1 <= first && first <= 0xF3) { + if (0x80 <= second && second <= 0xBF) { + if (0x80 <= third && third <= 0xBF) { + if (0x80 <= fourth && fourth <= 0xBF) { return true; } } @@ -1736,14 +1544,10 @@ class utf8_encoding // 2nd Byte: 0x80..0x8F // 3rd Byte: 0x80..0xBF // 4th Byte: 0x80..0xBF - if (byte_array[0] == 0xF4) - { - if (0x80 <= byte_array[1] && byte_array[1] <= 0x8F) - { - if (0x80 <= byte_array[2] && byte_array[2] <= 0xBF) - { - if (0x80 <= byte_array[3] && byte_array[3] <= 0xBF) - { + if (first == 0xF4) { + if (0x80 <= second && second <= 0x8F) { + if (0x80 <= third && third <= 0xBF) { + if (0x80 <= fourth && fourth <= 0xBF) { return true; } } @@ -1754,146 +1558,117 @@ class utf8_encoding // The rest of byte combinations are invalid. return false; } + default: // LCOV_EXCL_LINE + return false; // LCOV_EXCL_LINE + } +} - /// @brief Converts UTF-16 encoded characters to UTF-8 encoded bytes. - /// @param[in] utf16 UTF-16 encoded character(s). - /// @param[out] utf8_bytes UTF-8 encoded bytes. - /// @param[out] consumed_size The number of UTF-16 encoded characters used for the conversion. - /// @param[out] encoded_size The size of UTF-encoded bytes. - static void from_utf16( - std::array utf16, std::array& utf8_bytes, std::size_t& consumed_size, - std::size_t& encoded_size) - { - utf8_bytes.fill(0); - consumed_size = 0; - encoded_size = 0; - bool is_valid = false; - - if (utf16[0] < char16_t(0x80u)) - { - utf8_bytes[0] = static_cast(utf16[0] & 0x7Fu); - consumed_size = 1; - encoded_size = 1; - is_valid = true; - } - else if (utf16[0] <= char16_t(0x7FFu)) - { - uint16_t utf8_encoded = 0xC080u; - utf8_encoded |= static_cast((utf16[0] & 0x07C0u) << 2); - utf8_encoded |= static_cast(utf16[0] & 0x003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF00u) >> 8); - utf8_bytes[1] = static_cast(utf8_encoded & 0x00FFu); - consumed_size = 1; - encoded_size = 2; - is_valid = true; - } - else if (utf16[0] < char16_t(0xD800u) || char16_t(0xE000u) <= utf16[0]) - { - uint32_t utf8_encoded = 0xE08080u; - utf8_encoded |= static_cast((utf16[0] & 0xF000u) << 4); - utf8_encoded |= static_cast((utf16[0] & 0x0FC0u) << 2); - utf8_encoded |= static_cast(utf16[0] & 0x003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF0000u) >> 16); - utf8_bytes[1] = static_cast((utf8_encoded & 0x00FF00u) >> 8); - utf8_bytes[2] = static_cast(utf8_encoded & 0x0000FFu); - consumed_size = 1; - encoded_size = 3; - is_valid = true; - } - else if (utf16[0] <= char16_t(0xDBFFu) && char16_t(0xDC00u) <= utf16[1] && utf16[1] <= char16_t(0xDFFFu)) - { - // for surrogate pairs - uint32_t code_point = 0x10000u + ((utf16[0] & 0x03FFu) << 10) + (utf16[1] & 0x03FFu); - uint32_t utf8_encoded = 0xF0808080u; - utf8_encoded |= static_cast((code_point & 0x1C0000u) << 6); - utf8_encoded |= static_cast((code_point & 0x03F000u) << 4); - utf8_encoded |= static_cast((code_point & 0x000FC0u) << 2); - utf8_encoded |= static_cast(code_point & 0x00003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF000000u) >> 24); - utf8_bytes[1] = static_cast((utf8_encoded & 0x00FF0000u) >> 16); - utf8_bytes[2] = static_cast((utf8_encoded & 0x0000FF00u) >> 8); - utf8_bytes[3] = static_cast(utf8_encoded & 0x000000FFu); - consumed_size = 2; - encoded_size = 4; - is_valid = true; - } - - if (!is_valid) - { - throw invalid_encoding("Invalid UTF-16 encoding detected.", utf16); - } +/// @brief Converts UTF-16 encoded characters to UTF-8 encoded bytes. +/// @param[in] utf16 UTF-16 encoded character(s). +/// @param[out] utf8 UTF-8 encoded bytes. +/// @param[out] consumed_size The number of UTF-16 encoded characters used for the conversion. +/// @param[out] encoded_size The size of UTF-encoded bytes. +inline void from_utf16( + std::array utf16, std::array& utf8, std::size_t& consumed_size, + std::size_t& encoded_size) { + if (utf16[0] < char16_t(0x80u)) { + utf8[0] = static_cast(utf16[0] & 0x7Fu); + consumed_size = 1; + encoded_size = 1; + return; + } + else if (utf16[0] <= char16_t(0x7FFu)) { + uint16_t utf8_chunk = static_cast(0xC080u) | static_cast((utf16[0] & 0x07C0u) << 2) | + static_cast(utf16[0] & 0x003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF00u) >> 8); + utf8[1] = static_cast(utf8_chunk & 0x00FFu); + consumed_size = 1; + encoded_size = 2; + return; + } + else if (utf16[0] < char16_t(0xD800u) || char16_t(0xE000u) <= utf16[0]) { + uint32_t utf8_chunk = static_cast(0xE08080u) | static_cast((utf16[0] & 0xF000u) << 4) | + static_cast((utf16[0] & 0x0FC0u) << 2) | + static_cast(utf16[0] & 0x003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF0000u) >> 16); + utf8[1] = static_cast((utf8_chunk & 0x00FF00u) >> 8); + utf8[2] = static_cast(utf8_chunk & 0x0000FFu); + consumed_size = 1; + encoded_size = 3; + return; + } + else if (utf16[0] <= char16_t(0xDBFFu) && char16_t(0xDC00u) <= utf16[1] && utf16[1] <= char16_t(0xDFFFu)) { + // for surrogate pairs + uint32_t code_point = 0x10000u + ((utf16[0] & 0x03FFu) << 10) + (utf16[1] & 0x03FFu); + uint32_t utf8_chunk = + static_cast(0xF0808080u) | static_cast((code_point & 0x1C0000u) << 6) | + static_cast((code_point & 0x03F000u) << 4) | + static_cast((code_point & 0x000FC0u) << 2) | static_cast(code_point & 0x00003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF000000u) >> 24); + utf8[1] = static_cast((utf8_chunk & 0x00FF0000u) >> 16); + utf8[2] = static_cast((utf8_chunk & 0x0000FF00u) >> 8); + utf8[3] = static_cast(utf8_chunk & 0x000000FFu); + consumed_size = 2; + encoded_size = 4; + return; } - /// @brief Converts a UTF-32 encoded character to UTF-8 encoded bytes. - /// @param[in] utf32 A UTF-32 encoded character. - /// @param[out] utf8_bytes UTF-8 encoded bytes. - /// @param[in] encoded_size The size of UTF-encoded bytes. - static void from_utf32(const char32_t utf32, std::array& utf8_bytes, std::size_t& encoded_size) - { - utf8_bytes.fill(0); - encoded_size = 0; - bool is_valid = false; + throw invalid_encoding("Invalid UTF-16 encoding detected.", utf16); +} - if (utf32 < char32_t(0x80u)) - { - utf8_bytes[0] = static_cast(utf32 & 0x007F); - encoded_size = 1; - is_valid = true; - } - else if (utf32 <= char32_t(0x7FFu)) - { - uint16_t utf8_encoded = 0xC080u; - utf8_encoded |= static_cast((utf32 & 0x07C0u) << 2); - utf8_encoded |= static_cast(utf32 & 0x003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF00u) >> 8); - utf8_bytes[1] = static_cast(utf8_encoded & 0x00FFu); - encoded_size = 2; - is_valid = true; - } - else if (utf32 <= char32_t(0xFFFFu)) - { - uint32_t utf8_encoded = 0xE08080u; - utf8_encoded |= static_cast((utf32 & 0xF000u) << 4); - utf8_encoded |= static_cast((utf32 & 0x0FC0u) << 2); - utf8_encoded |= static_cast(utf32 & 0x003F); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF0000u) >> 16); - utf8_bytes[1] = static_cast((utf8_encoded & 0x00FF00u) >> 8); - utf8_bytes[2] = static_cast(utf8_encoded & 0x0000FFu); - encoded_size = 3; - is_valid = true; - } - else if (utf32 <= char32_t(0x10FFFFu)) - { - uint32_t utf8_encoded = 0xF0808080u; - utf8_encoded |= static_cast((utf32 & 0x1C0000u) << 6); - utf8_encoded |= static_cast((utf32 & 0x03F000u) << 4); - utf8_encoded |= static_cast((utf32 & 0x000FC0u) << 2); - utf8_encoded |= static_cast(utf32 & 0x00003Fu); - utf8_bytes[0] = static_cast((utf8_encoded & 0xFF000000u) >> 24); - utf8_bytes[1] = static_cast((utf8_encoded & 0x00FF0000u) >> 16); - utf8_bytes[2] = static_cast((utf8_encoded & 0x0000FF00u) >> 8); - utf8_bytes[3] = static_cast(utf8_encoded & 0x000000FFu); - encoded_size = 4; - is_valid = true; - } - - if (!is_valid) - { - throw invalid_encoding("Invalid UTF-32 encoding detected.", utf32); - } +/// @brief Converts a UTF-32 encoded character to UTF-8 encoded bytes. +/// @param[in] utf32 A UTF-32 encoded character. +/// @param[out] utf8 UTF-8 encoded bytes. +/// @param[in] encoded_size The size of UTF-encoded bytes. +inline void from_utf32(const char32_t utf32, std::array& utf8, std::size_t& encoded_size) { + if (utf32 < char32_t(0x80u)) { + utf8[0] = static_cast(utf32 & 0x007F); + encoded_size = 1; + return; + } + else if (utf32 <= char32_t(0x7FFu)) { + uint16_t utf8_chunk = static_cast(0xC080u) | static_cast((utf32 & 0x07C0u) << 2) | + static_cast(utf32 & 0x003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF00u) >> 8); + utf8[1] = static_cast(utf8_chunk & 0x00FFu); + encoded_size = 2; + return; + } + else if (utf32 <= char32_t(0xFFFFu)) { + uint32_t utf8_chunk = static_cast(0xE08080u) | static_cast((utf32 & 0xF000u) << 4) | + static_cast((utf32 & 0x0FC0u) << 2) | static_cast(utf32 & 0x003F); + utf8[0] = static_cast((utf8_chunk & 0xFF0000u) >> 16); + utf8[1] = static_cast((utf8_chunk & 0x00FF00u) >> 8); + utf8[2] = static_cast(utf8_chunk & 0x0000FFu); + encoded_size = 3; + return; + } + else if (utf32 <= char32_t(0x10FFFFu)) { + uint32_t utf8_chunk = static_cast(0xF0808080u) | static_cast((utf32 & 0x1C0000u) << 6) | + static_cast((utf32 & 0x03F000u) << 4) | + static_cast((utf32 & 0x000FC0u) << 2) | + static_cast(utf32 & 0x00003Fu); + utf8[0] = static_cast((utf8_chunk & 0xFF000000u) >> 24); + utf8[1] = static_cast((utf8_chunk & 0x00FF0000u) >> 16); + utf8[2] = static_cast((utf8_chunk & 0x0000FF00u) >> 8); + utf8[3] = static_cast(utf8_chunk & 0x000000FFu); + encoded_size = 4; + return; } -}; -} // namespace detail + throw invalid_encoding("Invalid UTF-32 encoding detected.", utf32); +} -FK_YAML_NAMESPACE_END +} // namespace utf8 -#endif /* FK_YAML_DETAIL_ENCODINGS_UTF_ENCODING_HPP_ */ +FK_YAML_DETAIL_NAMESPACE_END + +#endif /* FK_YAML_DETAIL_ENCODINGS_UTF_ENCODINGS_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -1914,7 +1689,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -1928,16 +1703,10 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of lexical token types. -enum class lexical_token_t -{ +enum class lexical_token_t { END_OF_BUFFER, //!< the end of input buffer. EXPLICIT_KEY_PREFIX, //!< the character for explicit mapping key prefix `?`. KEY_SEPARATOR, //!< the key separater `:` @@ -1962,77 +1731,56 @@ enum class lexical_token_t END_OF_DOCUMENT, //!< the end of a YAML document specified by `...`. }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_TYPES_LEXICAL_TOKEN_T_HPP_ */ -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN -namespace -{ +namespace { /// @brief Check if the given character is a digit. /// @note This function is needed to avoid assertion failures in `std::isdigit()` especially when compiled with MSVC. /// @param c A character to be checked. /// @return true if the given character is a digit, false otherwise. -inline bool is_digit(char c) -{ +inline bool is_digit(char c) { return ('0' <= c && c <= '9'); } -inline bool is_xdigit(char c) -{ +inline bool is_xdigit(char c) { return (('0' <= c && c <= '9') || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f')); } } // namespace -class scalar_scanner -{ +class scalar_scanner { public: - static lexical_token_t scan(const std::string& token) - { - switch (token.size()) - { + static lexical_token_t scan(const std::string& token) { + switch (token.size()) { case 0: return lexical_token_t::STRING_VALUE; case 1: - if (token[0] == '~') - { + if (token[0] == '~') { return lexical_token_t::NULL_VALUE; } break; case 4: - if (token == "null" || token == "Null" || token == "NULL") - { + if (token == "null" || token == "Null" || token == "NULL") { return lexical_token_t::NULL_VALUE; } - if (token == "true" || token == "True" || token == "TRUE") - { + if (token == "true" || token == "True" || token == "TRUE") { return lexical_token_t::BOOLEAN_VALUE; } if (token == ".inf" || token == ".Inf" || token == ".INF" || token == ".nan" || token == ".NaN" || - token == ".NAN") - { + token == ".NAN") { return lexical_token_t::FLOAT_NUMBER_VALUE; } break; case 5: - if (token == "false" || token == "False" || token == "FALSE") - { + if (token == "false" || token == "False" || token == "FALSE") { return lexical_token_t::BOOLEAN_VALUE; } - if (token == "-.inf" || token == "-.Inf" || token == "-.INF") - { + if (token == "-.inf" || token == "-.Inf" || token == "-.INF") { return lexical_token_t::FLOAT_NUMBER_VALUE; } break; @@ -2042,14 +1790,12 @@ class scalar_scanner } private: - static lexical_token_t scan_possible_number_token(const std::string& token) - { + static lexical_token_t scan_possible_number_token(const std::string& token) { std::string::const_iterator itr = token.begin(); std::size_t size = token.size(); FK_YAML_ASSERT(size > 0); - switch (*itr) - { + switch (*itr) { case '-': return (size > 1) ? scan_negative_number(++itr, --size) : lexical_token_t::STRING_VALUE; case '+': @@ -2071,34 +1817,28 @@ class scalar_scanner } } - static lexical_token_t scan_negative_number(std::string::const_iterator itr, std::size_t size) - { + static lexical_token_t scan_negative_number(std::string::const_iterator itr, std::size_t size) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { return (size > 1) ? scan_decimal_number(++itr, --size, false) : lexical_token_t::INTEGER_VALUE; } return lexical_token_t::STRING_VALUE; } - static lexical_token_t scan_after_zero_at_first(std::string::const_iterator itr, std::size_t size) - { + static lexical_token_t scan_after_zero_at_first(std::string::const_iterator itr, std::size_t size) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { // a token consisting of the beginning '0' and some following numbers, e.g., `0123`, is not an integer // according to https://yaml.org/spec/1.2.2/#10213-integer. return lexical_token_t::STRING_VALUE; } - switch (*itr) - { + switch (*itr) { case '.': { - if (size == 1) - { + if (size == 1) { return lexical_token_t::STRING_VALUE; } lexical_token_t ret = scan_after_decimal_point(++itr, --size, true); @@ -2115,20 +1855,16 @@ class scalar_scanner } static lexical_token_t scan_decimal_number( - std::string::const_iterator itr, std::size_t size, bool has_decimal_point) - { + std::string::const_iterator itr, std::size_t size, bool has_decimal_point) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { return (size > 1) ? scan_decimal_number(++itr, --size, has_decimal_point) : lexical_token_t::INTEGER_VALUE; } - switch (*itr) - { + switch (*itr) { case '.': { - if (has_decimal_point) - { + if (has_decimal_point) { // the token has more than one period, e.g., a semantic version `1.2.3`. return lexical_token_t::STRING_VALUE; } @@ -2145,12 +1881,10 @@ class scalar_scanner } static lexical_token_t scan_after_decimal_point( - std::string::const_iterator itr, std::size_t size, bool has_decimal_point) - { + std::string::const_iterator itr, std::size_t size, bool has_decimal_point) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { return (size > 1) ? scan_decimal_number(++itr, --size, has_decimal_point) : lexical_token_t::FLOAT_NUMBER_VALUE; } @@ -2159,18 +1893,15 @@ class scalar_scanner } static lexical_token_t scan_after_exponent( - std::string::const_iterator itr, std::size_t size, bool has_decimal_point) - { + std::string::const_iterator itr, std::size_t size, bool has_decimal_point) { FK_YAML_ASSERT(size > 0); - if (is_digit(*itr)) - { + if (is_digit(*itr)) { return (size > 1) ? scan_decimal_number(++itr, --size, has_decimal_point) : lexical_token_t::FLOAT_NUMBER_VALUE; } - switch (*itr) - { + switch (*itr) { case '+': case '-': return (size > 1) ? scan_decimal_number(++itr, --size, has_decimal_point) : lexical_token_t::STRING_VALUE; @@ -2179,12 +1910,10 @@ class scalar_scanner } } - static lexical_token_t scan_octal_number(std::string::const_iterator itr, std::size_t size) - { + static lexical_token_t scan_octal_number(std::string::const_iterator itr, std::size_t size) { FK_YAML_ASSERT(size > 0); - switch (*itr) - { + switch (*itr) { case '0': case '1': case '2': @@ -2199,28 +1928,24 @@ class scalar_scanner } } - static lexical_token_t scan_hexadecimal_number(std::string::const_iterator itr, std::size_t size) - { + static lexical_token_t scan_hexadecimal_number(std::string::const_iterator itr, std::size_t size) { FK_YAML_ASSERT(size > 0); - if (is_xdigit(*itr)) - { + if (is_xdigit(*itr)) { return (size > 1) ? scan_hexadecimal_number(++itr, --size) : lexical_token_t::INTEGER_VALUE; } return lexical_token_t::STRING_VALUE; } }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_SCALAR_SCANNER_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -2241,7 +1966,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -2262,12 +1987,7 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /////////////////////////////////////////// // Input Adapter API detection traits @@ -2282,17 +2002,13 @@ using fill_buffer_fn_t = decltype(std::declval().fill_buffer(std::declval -struct has_fill_buffer : std::false_type -{ -}; +struct has_fill_buffer : std::false_type {}; /// @brief A partial specialization of has_fill_buffer if InputAdapterType has get_character member function. /// @tparam InputAdapterType A type of a target input adapter. template struct has_fill_buffer::value>> - : std::true_type -{ -}; + : std::true_type {}; //////////////////////////////// // is_input_adapter traits @@ -2302,40 +2018,27 @@ struct has_fill_buffer -struct is_input_adapter : std::false_type -{ -}; +struct is_input_adapter : std::false_type {}; /// @brief A partial specialization of is_input_adapter if T is an input adapter type. /// @tparam InputAdapterType template -struct is_input_adapter::value>> : std::true_type -{ -}; - -} // namespace detail +struct is_input_adapter::value>> : std::true_type {}; -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_INPUT_ADAPTER_TRAITS_HPP_ */ // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A position tracker of the target buffer. -class position_tracker -{ +class position_tracker { private: /// @brief A set of information on the current position in an input buffer. - struct position - { + struct position { /// The current position from the beginning of an input buffer. std::size_t cur_pos {0}; /// The current position in the current line. @@ -2345,8 +2048,7 @@ class position_tracker }; public: - void set_target_buffer(const std::string& buffer) - { + void set_target_buffer(const std::string& buffer) { m_begin = m_last = buffer.begin(); m_end = buffer.end(); m_position = position {}; @@ -2355,23 +2057,19 @@ class position_tracker /// @brief Update the set of the current position informations. /// @note This function doesn't support cases where cur_pos has moved backward from the last call. /// @param cur_pos The iterator to the current element of the buffer. - void update_position(std::string::const_iterator cur_pos) - { + void update_position(std::string::const_iterator cur_pos) { m_position.cur_pos = static_cast(std::distance(m_begin, cur_pos)); m_position.lines_read += std::count(m_last, cur_pos, '\n'); m_last = cur_pos; - if (m_position.lines_read == 0) - { + if (m_position.lines_read == 0) { m_position.cur_pos_in_line = m_position.cur_pos; return; } std::size_t count = 0; - while (--cur_pos != m_begin) - { - if (*cur_pos == '\n') - { + while (--cur_pos != m_begin) { + if (*cur_pos == '\n') { break; } count++; @@ -2379,22 +2077,19 @@ class position_tracker m_position.cur_pos_in_line = count; } - std::size_t get_cur_pos() const noexcept - { + std::size_t get_cur_pos() const noexcept { return m_position.cur_pos; } /// @brief Get the current position in the current line. /// @return std::size_t The current position in the current line. - std::size_t get_cur_pos_in_line() const noexcept - { + std::size_t get_cur_pos_in_line() const noexcept { return m_position.cur_pos_in_line; } /// @brief Get the number of lines which have already been read. /// @return std::size_t The number of lines which have already been read. - std::size_t get_lines_read() const noexcept - { + std::size_t get_lines_read() const noexcept { return m_position.lines_read; } @@ -2409,9 +2104,7 @@ class position_tracker position m_position {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_POSITION_TRACKER_HPP_ */ @@ -2420,7 +2113,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -2440,7 +2133,6 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. FK_YAML_NAMESPACE_BEGIN // forward declaration for basic_node<...> @@ -2450,9 +2142,9 @@ template < template class ConverterType> class basic_node; -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_NAMESPACE_END + +FK_YAML_DETAIL_NAMESPACE_BEGIN ///////////////////////////// // is_basic_node traits @@ -2461,9 +2153,7 @@ namespace detail /// @brief A struct to check the template parameter class is a kind of basic_node template class. /// @tparam T A class to be checked if it's a kind of basic_node template class. template -struct is_basic_node : std::false_type -{ -}; +struct is_basic_node : std::false_type {}; /// @brief A partial specialization of is_basic_node for basic_node template class. /// @tparam SequenceType A type for sequence node value containers. @@ -2479,9 +2169,7 @@ template < template class Converter> struct is_basic_node< basic_node> - : std::true_type -{ -}; + : std::true_type {}; /////////////////////////////////// // is_node_ref_storage traits @@ -2494,16 +2182,12 @@ class node_ref_storage; /// @brief A struct to check the template parameter class is a kind of node_ref_storage_template class. /// @tparam T A type to be checked if it's a kind of node_ref_storage template class. template -struct is_node_ref_storage : std::false_type -{ -}; +struct is_node_ref_storage : std::false_type {}; /// @brief A partial specialization for node_ref_storage template class. /// @tparam T A template parameter type of node_ref_storage template class. template -struct is_node_ref_storage> : std::true_type -{ -}; +struct is_node_ref_storage> : std::true_type {}; /////////////////////////////////////////////////////// // basic_node conversion API representative types @@ -2530,16 +2214,13 @@ using to_node_funcion_t = decltype(T::to_node(std::declval()...)); /// @tparam T A target type passed to from_node function. /// @tparam typename N/A template -struct has_from_node : std::false_type -{ -}; +struct has_from_node : std::false_type {}; /// @brief A partial specialization of has_from_node if T is not a basic_node template instance type. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam T A target type passed to from_node function. template -struct has_from_node::value>> -{ +struct has_from_node::value>> { using converter = typename BasicNodeType::template value_converter_type; // NOLINTNEXTLINE(readability-identifier-naming) @@ -2553,16 +2234,13 @@ struct has_from_node::value>> /// @tparam T A target type passed to to_node function. /// @tparam typename N/A template -struct has_to_node : std::false_type -{ -}; +struct has_to_node : std::false_type {}; /// @brief A partial specialization of has_to_node if T is not a basic_node template instance type. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam T A target type passed to to_node function. template -struct has_to_node::value>> -{ +struct has_to_node::value>> { using converter = typename BasicNodeType::template value_converter_type; // NOLINTNEXTLINE(readability-identifier-naming) @@ -2579,9 +2257,7 @@ struct has_to_node::value>> /// @tparam CompatibleType A target type for compatibility check. /// @tparam typename N/A template -struct is_node_compatible_type_impl : std::false_type -{ -}; +struct is_node_compatible_type_impl : std::false_type {}; /// @brief A partial specialization of is_node_compatible_type_impl if CompatibleType is a complete type and is /// compatible for BasicNodeType. @@ -2591,21 +2267,15 @@ template struct is_node_compatible_type_impl< BasicNodeType, CompatibleType, enable_if_t, has_to_node>::value>> - : std::true_type -{ -}; + : std::true_type {}; /// @brief Type traits to check if CompatibleType is a compatible type for BasicNodeType. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam CompatibleType A target type for compatibility check. -template -struct is_node_compatible_type : is_node_compatible_type_impl -{ -}; - -} // namespace detail +template +struct is_node_compatible_type : is_node_compatible_type_impl {}; -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_META_NODE_TRAITS_HPP_ */ @@ -2616,35 +2286,21 @@ FK_YAML_NAMESPACE_END // #include -/** - * @namespace fkyaml - * @brief namespace for fkYAML library. - */ -FK_YAML_NAMESPACE_BEGIN - -/** - * @namespace detail - * @brief namespace for internal implementations of fkYAML library. - */ -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A class which lexically analizes YAML formatted inputs. /// @tparam BasicNodeType A type of the container for YAML values. template ::value, int> = 0> -class lexical_analyzer -{ +class lexical_analyzer { private: using char_traits_type = typename std::char_traits; - enum class block_style_indicator_t - { + enum class block_style_indicator_t { LITERAL, //!< keeps newlines inside the block as they are indicated by a pipe `|`. FOLDED, //!< replaces newlines inside the block with spaces indicated by a right angle bracket `>`. }; - enum class chomping_indicator_t - { + enum class chomping_indicator_t { STRIP, //!< excludes final line breaks and traiing empty lines indicated by `-`. KEEP, //!< preserves final line breaks but excludes trailing empty lines. no indicator means this type. CLIP, //!< preserves final line breaks and trailing empty lines indicated by `+`. @@ -2660,8 +2316,7 @@ class lexical_analyzer /// @tparam InputAdapterType The type of the input adapter. /// @param input_adapter An input adapter object. template ::value, int> = 0> - explicit lexical_analyzer(InputAdapterType&& input_adapter) - { + explicit lexical_analyzer(InputAdapterType&& input_adapter) { std::forward(input_adapter).fill_buffer(m_input_buffer); m_cur_itr = m_token_begin_itr = m_input_buffer.cbegin(); m_end_itr = m_input_buffer.cend(); @@ -2670,8 +2325,7 @@ class lexical_analyzer /// @brief Get the next lexical token type by scanning the left of the input buffer. /// @return lexical_token_t The next lexical token type. - lexical_token_t get_next_token() - { + lexical_token_t get_next_token() { skip_white_spaces_and_newline_codes(); m_token_begin_itr = m_cur_itr; @@ -2679,35 +2333,29 @@ class lexical_analyzer m_last_token_begin_pos = m_pos_tracker.get_cur_pos_in_line(); m_last_token_begin_line = m_pos_tracker.get_lines_read(); - if (m_cur_itr == m_end_itr) - { + if (m_cur_itr == m_end_itr) { return m_last_token_type = lexical_token_t::END_OF_BUFFER; } - switch (char current = *m_cur_itr) - { + switch (char current = *m_cur_itr) { case '?': - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { m_value_buffer = "?"; return m_last_token_type = lexical_token_t::STRING_VALUE; } - switch (*m_cur_itr) - { + switch (*m_cur_itr) { case ' ': return m_last_token_type = lexical_token_t::EXPLICIT_KEY_PREFIX; default: return m_last_token_type = scan_scalar(); } case ':': { // key separater - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { return m_last_token_type = lexical_token_t::KEY_SEPARATOR; } - switch (*m_cur_itr) - { + switch (*m_cur_itr) { case ' ': case '\t': case '\r': @@ -2718,8 +2366,7 @@ class lexical_analyzer case ']': case '{': case '}': - if (m_flow_context_depth > 0) - { + if (m_flow_context_depth > 0) { // the above characters are not "safe" to be followed in a flow context. // See https://yaml.org/spec/1.2.2/#733-plain-style for more details. break; @@ -2737,8 +2384,7 @@ class lexical_analyzer case '&': { // anchor prefix extract_anchor_name(); bool is_empty = m_value_buffer.empty(); - if (is_empty) - { + if (is_empty) { emit_error("anchor name must not be empty."); } return m_last_token_type = lexical_token_t::ANCHOR_PREFIX; @@ -2746,8 +2392,7 @@ class lexical_analyzer case '*': { // alias prefix extract_anchor_name(); bool is_empty = m_value_buffer.empty(); - if (is_empty) - { + if (is_empty) { emit_error("anchor name must not be empty."); } @@ -2763,19 +2408,16 @@ class lexical_analyzer return m_last_token_type = scan_directive(); case '-': { char next = *(m_cur_itr + 1); - if (next == ' ') - { + if (next == ' ') { // Move a cursor to the beginning of the next token. m_cur_itr += 2; return m_last_token_type = lexical_token_t::SEQUENCE_BLOCK_PREFIX; } bool is_available = (std::distance(m_cur_itr, m_end_itr) > 2); - if (is_available) - { + if (is_available) { m_cur_itr += 3; - if (std::equal(m_token_begin_itr, m_cur_itr, "---")) - { + if (std::equal(m_token_begin_itr, m_cur_itr, "---")) { return m_last_token_type = lexical_token_t::END_OF_DIRECTIVES; } } @@ -2787,8 +2429,7 @@ class lexical_analyzer ++m_cur_itr; return m_last_token_type = lexical_token_t::SEQUENCE_FLOW_BEGIN; case ']': // sequence flow end - if (m_flow_context_depth == 0) - { + if (m_flow_context_depth == 0) { emit_error("An invalid flow sequence ending."); } m_flow_context_depth--; @@ -2799,8 +2440,7 @@ class lexical_analyzer ++m_cur_itr; return m_last_token_type = lexical_token_t::MAPPING_FLOW_BEGIN; case '}': // mapping flow end - if (m_flow_context_depth == 0) - { + if (m_flow_context_depth == 0) { emit_error("An invalid flow mapping ending."); } m_flow_context_depth--; @@ -2817,10 +2457,8 @@ class lexical_analyzer return m_last_token_type = scan_scalar(); case '.': { bool is_available = (std::distance(m_cur_itr, m_end_itr) > 2); - if (is_available) - { - if (std::equal(m_cur_itr, m_cur_itr + 3, "...")) - { + if (is_available) { + if (std::equal(m_cur_itr, m_cur_itr + 3, "...")) { m_cur_itr += 3; return m_last_token_type = lexical_token_t::END_OF_DOCUMENT; } @@ -2849,39 +2487,33 @@ class lexical_analyzer /// @brief Get the beginning position of a last token. /// @return std::size_t The beginning position of a last token. - std::size_t get_last_token_begin_pos() const noexcept - { + std::size_t get_last_token_begin_pos() const noexcept { return m_last_token_begin_pos; } /// @brief Get the number of lines already processed. /// @return std::size_t The number of lines already processed. - std::size_t get_lines_processed() const noexcept - { + std::size_t get_lines_processed() const noexcept { return m_last_token_begin_line; } /// @brief Convert from string to null and get the converted value. /// @return std::nullptr_t A null value converted from one of the followings: "null", "Null", "NULL", "~". - std::nullptr_t get_null() const - { + std::nullptr_t get_null() const { return from_string(m_value_buffer, type_tag {}); } /// @brief Convert from string to boolean and get the converted value. /// @return true A string token is one of the followings: "true", "True", "TRUE". /// @return false A string token is one of the followings: "false", "False", "FALSE". - boolean_type get_boolean() const - { + boolean_type get_boolean() const { return from_string(m_value_buffer, type_tag {}); } /// @brief Convert from string to integer and get the converted value. /// @return integer_type An integer value converted from the source string. - integer_type get_integer() const - { - if (m_value_buffer.size() > 2 && m_value_buffer.rfind("0o", 0) != std::string::npos) - { + integer_type get_integer() const { + if (m_value_buffer.size() > 2 && m_value_buffer.rfind("0o", 0) != std::string::npos) { // Replace the prefix "0o" with "0" since STL functions can detect octal chars. // Note that the YAML specifies octal values start with the prefix "0o", not "0". // See https://yaml.org/spec/1.2.2/#1032-tag-resolution for more details. @@ -2892,15 +2524,13 @@ class lexical_analyzer /// @brief Convert from string to float number and get the converted value. /// @return float_number_type A float number value converted from the source string. - float_number_type get_float_number() const - { + float_number_type get_float_number() const { return from_string(m_value_buffer, type_tag {}); } /// @brief Get a scanned string value. /// @return const string_type& Constant reference to a scanned string. - const string_type& get_string() const noexcept - { + const string_type& get_string() const noexcept { // TODO: Provide support for different string types between nodes & inputs. static_assert(std::is_same::value, "Unsupported, different string types."); return m_value_buffer; @@ -2908,8 +2538,7 @@ class lexical_analyzer /// @brief Get the YAML version specification. /// @return const string_type& A YAML version specification. - const string_type& get_yaml_version() const - { + const string_type& get_yaml_version() const { FK_YAML_ASSERT(!m_value_buffer.empty() && m_value_buffer.size() == 3); FK_YAML_ASSERT(m_value_buffer == "1.1" || m_value_buffer == "1.2"); @@ -2918,16 +2547,14 @@ class lexical_analyzer /// @brief Get the YAML tag handle defined in the TAG directive. /// @return const std::string& A tag handle. - const std::string& get_tag_handle() const - { + const std::string& get_tag_handle() const { FK_YAML_ASSERT(!m_tag_handle.empty()); return m_tag_handle; } /// @brief Get the YAML tag prefix defined in the TAG directive. /// @return const std::string A tag prefix. - const std::string& get_tag_prefix() const - { + const std::string& get_tag_prefix() const { FK_YAML_ASSERT(!m_tag_prefix.empty()); return m_tag_prefix; } @@ -2936,22 +2563,18 @@ class lexical_analyzer /// @brief A utility function to convert a hexadecimal character to an integer. /// @param source A hexadecimal character ('0'~'9', 'A'~'F', 'a'~'f') /// @return char A integer converted from @a source. - char convert_hex_char_to_byte(char source) const - { - if ('0' <= source && source <= '9') - { + char convert_hex_char_to_byte(char source) const { + if ('0' <= source && source <= '9') { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) return static_cast(source - '0'); } - if ('A' <= source && source <= 'F') - { + if ('A' <= source && source <= 'F') { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) return static_cast(source - 'A' + 10); } - if ('a' <= source && source <= 'f') - { + if ('a' <= source && source <= 'f') { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) return static_cast(source - 'a' + 10); } @@ -2960,8 +2583,7 @@ class lexical_analyzer } /// @brief Skip until a newline code or a null character is found. - void scan_comment() - { + void scan_comment() { FK_YAML_ASSERT(*m_cur_itr == '#'); skip_until_line_end(); } @@ -2969,18 +2591,15 @@ class lexical_analyzer /// @brief Scan directives starting with the prefix '%' /// @note Currently, only %YAML directive is supported. If not, returns invalid or throws an exception. /// @return lexical_token_t The lexical token type for directives. - lexical_token_t scan_directive() - { + lexical_token_t scan_directive() { FK_YAML_ASSERT(*m_cur_itr == '%'); m_value_buffer.clear(); m_token_begin_itr = ++m_cur_itr; bool ends_loop = false; - while (!ends_loop && m_cur_itr != m_end_itr) - { - switch (*m_cur_itr) - { + while (!ends_loop && m_cur_itr != m_end_itr) { + switch (*m_cur_itr) { case ' ': case '\t': ends_loop = true; @@ -2997,20 +2616,16 @@ class lexical_analyzer m_value_buffer.assign(m_token_begin_itr, m_cur_itr); - if (m_value_buffer == "TAG") - { - if (!ends_loop) - { + if (m_value_buffer == "TAG") { + if (!ends_loop) { emit_error("There must be at least one white space between \"%TAG\" and tag info."); } skip_white_spaces(); return scan_tag_directive(); } - if (m_value_buffer == "YAML") - { - if (!ends_loop) - { + if (m_value_buffer == "YAML") { + if (!ends_loop) { emit_error("There must be at least one white space between \"%YAML\" and tag info."); } skip_white_spaces(); @@ -3023,8 +2638,7 @@ class lexical_analyzer /// @brief Scan a YAML tag directive. /// @return lexical_token_t The lexical token type for YAML tag directives. - lexical_token_t scan_tag_directive() - { + lexical_token_t scan_tag_directive() { m_tag_handle.clear(); m_tag_prefix.clear(); m_token_begin_itr = m_cur_itr; @@ -3033,50 +2647,41 @@ class lexical_analyzer // extract a tag handle // - if (*m_cur_itr != '!') - { + if (*m_cur_itr != '!') { emit_error("Tag handle must start with \'!\'."); } - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { emit_error("invalid TAG directive is found."); } - switch (*m_cur_itr) - { + switch (*m_cur_itr) { case ' ': case '\t': // primary handle (!) break; case '!': - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { emit_error("invalid TAG directive is found."); } - if (*m_cur_itr != ' ' && *m_cur_itr != '\t') - { + if (*m_cur_itr != ' ' && *m_cur_itr != '\t') { emit_error("invalid tag handle is found."); } break; default: { bool ends_loop = false; - do - { - switch (*m_cur_itr) - { + do { + switch (*m_cur_itr) { case ' ': case '\t': emit_error("invalid tag handle is found."); case '!': { - if (m_cur_itr + 1 == m_end_itr) - { + if (m_cur_itr + 1 == m_end_itr) { ends_loop = true; break; } char next = *(m_cur_itr + 1); - if (next != ' ' && next != '\t') - { + if (next != ' ' && next != '\t') { emit_error("invalid tag handle is found."); } ends_loop = true; @@ -3085,16 +2690,14 @@ class lexical_analyzer case '-': break; default: - if (!isalnum(*m_cur_itr)) - { + if (!isalnum(*m_cur_itr)) { // See https://yaml.org/spec/1.2.2/#rule-c-named-tag-handle for more details. emit_error("named handle can contain only numbers(0-9), alphabets(A-Z,a-z) and hyphens(-)."); } break; } - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { emit_error("invalid TAG directive is found."); } } while (!ends_loop); @@ -3111,8 +2714,7 @@ class lexical_analyzer // m_token_begin_itr = m_cur_itr; - switch (*m_cur_itr) - { + switch (*m_cur_itr) { // a tag prefix must not start with flow indicators to avoid ambiguity. // See https://yaml.org/spec/1.2.2/#rule-ns-global-tag-prefix for more details. case ',': @@ -3125,10 +2727,8 @@ class lexical_analyzer // extract the rest of a tag prefix. bool ends_loop = false; - do - { - switch (*m_cur_itr) - { + do { + switch (*m_cur_itr) { case ' ': case '\t': case '\r': @@ -3141,8 +2741,7 @@ class lexical_analyzer m_tag_prefix.assign(m_token_begin_itr, m_cur_itr); bool is_valid = uri_encoding::validate(m_tag_prefix.begin(), m_tag_prefix.end()); - if (!is_valid) - { + if (!is_valid) { emit_error("invalid URI character is found in a tag prefix."); } @@ -3152,16 +2751,13 @@ class lexical_analyzer /// @brief Scan a YAML version directive. /// @note Only 1.1 and 1.2 are supported. If not, throws an exception. /// @return lexical_token_t The lexical token type for YAML version directives. - lexical_token_t scan_yaml_version_directive() - { + lexical_token_t scan_yaml_version_directive() { m_value_buffer.clear(); m_token_begin_itr = m_cur_itr; bool ends_loop = false; - while (!ends_loop && m_cur_itr != m_end_itr) - { - switch (*m_cur_itr) - { + while (!ends_loop && m_cur_itr != m_end_itr) { + switch (*m_cur_itr) { case ' ': case '\t': case '\r': @@ -3176,8 +2772,7 @@ class lexical_analyzer m_value_buffer.assign(m_token_begin_itr, m_cur_itr); - if (m_value_buffer != "1.1" && m_value_buffer != "1.2") - { + if (m_value_buffer != "1.1" && m_value_buffer != "1.2") { emit_error("Only 1.1 and 1.2 can be specified as the YAML version."); } @@ -3185,18 +2780,15 @@ class lexical_analyzer } /// @brief Extracts an anchor name from the input and assigns the result to `m_value_buffer`. - void extract_anchor_name() - { + void extract_anchor_name() { FK_YAML_ASSERT(*m_cur_itr == '&' || *m_cur_itr == '*'); m_value_buffer.clear(); m_token_begin_itr = ++m_cur_itr; bool ends_loop = false; - for (; m_cur_itr != m_end_itr; ++m_cur_itr) - { - switch (*m_cur_itr) - { + for (; m_cur_itr != m_end_itr; ++m_cur_itr) { + switch (*m_cur_itr) { // anchor name must not contain white spaces, newline codes and flow indicators. // See https://yaml.org/spec/1.2.2/#692-node-anchors for more details. case ' ': @@ -3212,14 +2804,12 @@ class lexical_analyzer break; case ':': { auto next_itr = m_cur_itr + 1; - if (next_itr == m_end_itr) - { + if (next_itr == m_end_itr) { ++m_cur_itr; ends_loop = true; break; } - switch (*next_itr) - { + switch (*next_itr) { case ' ': case '\t': case '\r': @@ -3234,8 +2824,7 @@ class lexical_analyzer break; } - if (ends_loop) - { + if (ends_loop) { break; } } @@ -3244,14 +2833,12 @@ class lexical_analyzer } /// @brief Extracts a tag name from the input and assigns the result to `m_value_buffer`. - void extract_tag_name() - { + void extract_tag_name() { m_value_buffer.clear(); FK_YAML_ASSERT(*m_cur_itr == '!'); - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { // Just "!" is a non-specific tag. m_value_buffer = "!"; return; @@ -3260,8 +2847,7 @@ class lexical_analyzer bool is_verbatim = false; bool allows_another_tag_prefix = false; - switch (*m_cur_itr) - { + switch (*m_cur_itr) { case ' ': case '\r': case '\n': @@ -3284,15 +2870,12 @@ class lexical_analyzer bool is_named_handle = false; bool ends_loop = false; - do - { - if (++m_cur_itr == m_end_itr) - { + do { + if (++m_cur_itr == m_end_itr) { break; } - switch (*m_cur_itr) - { + switch (*m_cur_itr) { // Tag names must not contain spaces or newline codes. case ' ': case '\t': @@ -3301,8 +2884,7 @@ class lexical_analyzer ends_loop = true; break; case '!': - if (!allows_another_tag_prefix) - { + if (!allows_another_tag_prefix) { emit_error("invalid tag prefix (!) is found."); } @@ -3317,35 +2899,29 @@ class lexical_analyzer m_value_buffer.assign(m_token_begin_itr, m_cur_itr); - if (is_verbatim) - { + if (is_verbatim) { char last = m_value_buffer.back(); - if (last != '>') - { + if (last != '>') { emit_error("verbatim tag (!) must be ended with \'>\'."); } auto tag_begin = m_value_buffer.begin() + 2; auto tag_end = m_value_buffer.end() - 1; - if (tag_begin == tag_end) - { + if (tag_begin == tag_end) { emit_error("verbatim tag(!) must not be empty."); } bool is_valid_uri = uri_encoding::validate(tag_begin, tag_end); - if (!is_valid_uri) - { + if (!is_valid_uri) { emit_error("invalid URI character is found in a verbatim tag."); } return; } - if (is_named_handle) - { + if (is_named_handle) { char last = m_value_buffer.back(); - if (last == '!') - { + if (last == '!') { // Tag shorthand must be followed by a non-empty suffix. // See the "Tag Shorthands" section in https://yaml.org/spec/1.2.2/#691-node-tags. emit_error("named handle has no suffix."); @@ -3354,8 +2930,7 @@ class lexical_analyzer std::size_t last_tag_prefix_pos = m_value_buffer.find_last_of('!'); bool is_valid_uri = uri_encoding::validate(m_value_buffer.begin() + last_tag_prefix_pos + 1, m_value_buffer.end()); - if (!is_valid_uri) - { + if (!is_valid_uri) { emit_error("Invalid URI character is found in a named tag handle."); } } @@ -3363,18 +2938,15 @@ class lexical_analyzer /// @brief Scan a string token, either plain, single-quoted or double-quoted. /// @return lexical_token_t The lexical token type for strings. - lexical_token_t scan_scalar() - { + lexical_token_t scan_scalar() { m_value_buffer.clear(); bool needs_last_single_quote = false; bool needs_last_double_quote = false; - if (m_cur_itr == m_token_begin_itr) - { + if (m_cur_itr == m_token_begin_itr) { needs_last_single_quote = (*m_cur_itr == '\''); needs_last_double_quote = (*m_cur_itr == '\"'); - if (needs_last_double_quote || needs_last_single_quote) - { + if (needs_last_double_quote || needs_last_single_quote) { m_token_begin_itr = ++m_cur_itr; } } @@ -3382,8 +2954,7 @@ class lexical_analyzer lexical_token_t type = extract_string_token(needs_last_single_quote, needs_last_double_quote); FK_YAML_ASSERT(type == lexical_token_t::STRING_VALUE); - if (needs_last_single_quote || needs_last_double_quote) - { + if (needs_last_single_quote || needs_last_double_quote) { // just returned the extracted string value if quoted. return type; } @@ -3394,12 +2965,10 @@ class lexical_analyzer /// @brief Check if the given character is allowed in a single-quoted scalar token. /// @param c The character to be checked. /// @return true if the given character is allowed, false otherwise. - bool is_allowed_single(char c) - { + bool is_allowed_single(char c) { bool ret = false; - switch (c) - { + switch (c) { case '\r': case '\n': // TODO: Support multi-line string scalars. @@ -3408,15 +2977,13 @@ class lexical_analyzer case '\'': // If single quotation marks are repeated twice in a single-quoted string token, // they are considered as an escaped single quotation mark. - if (m_cur_itr + 1 == m_end_itr) - { + if (m_cur_itr + 1 == m_end_itr) { m_value_buffer.append(m_token_begin_itr, m_cur_itr++); m_token_begin_itr = m_cur_itr; break; } - if (*(m_cur_itr + 1) != '\'') - { + if (*(m_cur_itr + 1) != '\'') { m_value_buffer.append(m_token_begin_itr, m_cur_itr++); break; } @@ -3436,12 +3003,10 @@ class lexical_analyzer /// @brief Check if the given character is allowed in a double-quoted scalar token. /// @param c The character to be checked. /// @return true if the given character is allowed, false otherwise. - bool is_allowed_double(char c) - { + bool is_allowed_double(char c) { bool ret = false; - switch (c) - { + switch (c) { case '\r': case '\n': // TODO: Support multi-line string scalars. @@ -3457,8 +3022,7 @@ class lexical_analyzer // Handle escaped characters. // See https://yaml.org/spec/1.2.2/#57-escaped-characters for more details. c = *++m_cur_itr; - switch (c) - { + switch (c) { case 'a': m_value_buffer.push_back('\a'); break; @@ -3496,20 +3060,20 @@ class lexical_analyzer m_value_buffer.push_back('\\'); break; case 'N': // next line - utf8_encoding::from_utf32(0x85u, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(0x85u, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); break; case '_': // non-breaking space - utf8_encoding::from_utf32(0xA0u, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(0xA0u, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); break; case 'L': // line separator - utf8_encoding::from_utf32(0x2028u, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(0x2028u, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); break; case 'P': // paragraph separator - utf8_encoding::from_utf32(0x2029u, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(0x2029u, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); break; case 'x': handle_escaped_unicode(1); @@ -3535,12 +3099,10 @@ class lexical_analyzer /// @brief Check if the given character is allowed in a plain scalar token. /// @param c The character to be checked. /// @return true if the given character is allowed, false otherwise. - bool is_allowed_plain(char c) - { + bool is_allowed_plain(char c) { bool ret = false; - switch (c) - { + switch (c) { case '\r': case '\n': m_value_buffer.append(m_token_begin_itr, m_cur_itr); @@ -3554,10 +3116,8 @@ class lexical_analyzer // These characters are permitted when not inside a flow collection, and not inside an implicit key. // TODO: Support detection of implicit key context for this check. - if (m_flow_context_depth > 0) - { - switch (next) - { + if (m_flow_context_depth > 0) { + switch (next) { case '{': case '}': case '[': @@ -3568,30 +3128,25 @@ class lexical_analyzer break; } - if (is_appended) - { + if (is_appended) { break; } } // " :" is permitted in a plain style string token, but not when followed by a space. - if (next == ':') - { + if (next == ':') { char peeked = *(m_cur_itr + 2); - if (peeked == ' ') - { + if (peeked == ' ') { m_value_buffer.append(m_token_begin_itr, m_cur_itr++); is_appended = true; } - if (is_appended) - { + if (is_appended) { break; } } - switch (next) - { + switch (next) { case ' ': case '\r': case '\n': @@ -3612,8 +3167,7 @@ class lexical_analyzer // A colon as a key separator must be followed by // * a white space or // * a newline code. - switch (next) - { + switch (next) { case ' ': case '\t': case '\r': @@ -3633,8 +3187,7 @@ class lexical_analyzer case ']': case ',': // just regard the flow indicators as a normal character if plain but not inside a flow context. - if (m_flow_context_depth == 0) - { + if (m_flow_context_depth == 0) { ret = true; break; } @@ -3651,8 +3204,7 @@ class lexical_analyzer /// @brief Extracts a string token, either plain, single-quoted or double-quoted, from the input buffer. /// @return lexical_token_t The lexical token type for strings. - lexical_token_t extract_string_token(bool needs_last_single_quote, bool needs_last_double_quote) - { + lexical_token_t extract_string_token(bool needs_last_single_quote, bool needs_last_double_quote) { // change behaviors depending on the type of a comming string scalar token. // * single quoted // * double quoted @@ -3661,13 +3213,11 @@ class lexical_analyzer std::string check_filters {"\r\n\\"}; bool (lexical_analyzer::*pfn_is_allowed)(char) = nullptr; - if (needs_last_single_quote) - { + if (needs_last_single_quote) { check_filters.append("\'"); pfn_is_allowed = &lexical_analyzer::is_allowed_single; } - else if (needs_last_double_quote) - { + else if (needs_last_double_quote) { check_filters.append("\""); pfn_is_allowed = &lexical_analyzer::is_allowed_double; } @@ -3679,68 +3229,45 @@ class lexical_analyzer // scan the contents of a string scalar token. - for (; m_cur_itr != m_end_itr; m_cur_itr = (m_cur_itr == m_end_itr) ? m_cur_itr : ++m_cur_itr) - { + for (; m_cur_itr != m_end_itr; m_cur_itr = (m_cur_itr == m_end_itr) ? m_cur_itr : ++m_cur_itr) { char current = *m_cur_itr; + uint32_t num_bytes = utf8::get_num_bytes(static_cast(current)); + if (num_bytes == 1) { + auto ret = check_filters.find(current); + if (ret != std::string::npos) { + bool is_allowed = (this->*pfn_is_allowed)(current); + if (!is_allowed) { + return lexical_token_t::STRING_VALUE; + } - auto ret = check_filters.find(current); - if (ret != std::string::npos) - { - bool is_allowed = (this->*pfn_is_allowed)(current); - if (!is_allowed) - { - return lexical_token_t::STRING_VALUE; + continue; } - continue; - } - - uint8_t byte = static_cast(current); - - // Handle unescaped control characters. - if (byte <= 0x1F) - { - m_value_buffer.append(m_token_begin_itr, m_cur_itr); - handle_unescaped_control_char(current); - m_token_begin_itr = m_cur_itr + 1; - continue; - } - - // The other characters are already checked while creating an input handler. - - // Handle ASCII characters except control characters. - if (byte <= 0x7E) - { - continue; - } + uint8_t byte = static_cast(current); - // Handle 2-byte characters encoded in UTF-8. (U+0080..U+07FF) - if (byte <= 0xDF) - { - ++m_cur_itr; - continue; - } + // Handle unescaped control characters. + if (byte <= 0x1F) { + m_value_buffer.append(m_token_begin_itr, m_cur_itr); + handle_unescaped_control_char(current); + m_token_begin_itr = m_cur_itr + 1; + continue; + } - // Handle 3-byte characters encoded in UTF-8. (U+1000..U+D7FF,U+E000..U+FFFF) - if (byte <= 0xEF) - { - m_cur_itr += 2; continue; } - // Handle 4-byte characters encoded in UTF-8. (U+10000..U+FFFFF,U+100000..U+10FFFF) - m_cur_itr += 3; + // Multi-byte characters are already validated while creating an input handler. + // So just advance the iterator. + m_cur_itr += num_bytes - 1; } // Handle the end of input buffer. - if (needs_last_double_quote) - { + if (needs_last_double_quote) { emit_error("Invalid end of input buffer in a double-quoted string token."); } - if (needs_last_single_quote) - { + if (needs_last_single_quote) { emit_error("Invalid end of input buffer in a single-quoted string token."); } @@ -3754,26 +3281,21 @@ class lexical_analyzer /// @param indent The indent size specified for the given token. /// @return The lexical token type for strings. lexical_token_t scan_block_style_string_token( - block_style_indicator_t style, chomping_indicator_t chomp, std::size_t indent) - { + block_style_indicator_t style, chomping_indicator_t chomp, std::size_t indent) { m_value_buffer.clear(); // Handle leading all-space lines. - for (char current = 0; m_cur_itr != m_end_itr; ++m_cur_itr) - { + for (char current = 0; m_cur_itr != m_end_itr; ++m_cur_itr) { current = *m_cur_itr; - if (current == ' ') - { + if (current == ' ') { continue; } - if (current == '\r') - { + if (current == '\r') { current = *++m_cur_itr; } - if (current == '\n') - { + if (current == '\n') { m_value_buffer.push_back('\n'); continue; } @@ -3781,10 +3303,8 @@ class lexical_analyzer break; } - if (m_cur_itr == m_end_itr) - { - if (chomp != chomping_indicator_t::KEEP) - { + if (m_cur_itr == m_end_itr) { + if (chomp != chomping_indicator_t::KEEP) { m_value_buffer.clear(); } return lexical_token_t::STRING_VALUE; @@ -3794,22 +3314,18 @@ class lexical_analyzer std::size_t cur_indent = m_pos_tracker.get_cur_pos_in_line(); // TODO: preserve and compare the last indentation with `cur_indent` - if (indent == 0) - { + if (indent == 0) { indent = cur_indent; } - else if (cur_indent < indent) - { + else if (cur_indent < indent) { emit_error("A block style scalar is less indented than the indicated level."); } int chars_in_line = 0; bool is_extra_indented = false; - if (cur_indent > indent) - { + if (cur_indent > indent) { std::size_t diff = cur_indent - indent; - if (style == block_style_indicator_t::FOLDED) - { + if (style == block_style_indicator_t::FOLDED) { m_value_buffer.push_back('\n'); is_extra_indented = true; } @@ -3817,34 +3333,28 @@ class lexical_analyzer chars_in_line += static_cast(diff); } - for (char current = 0; m_cur_itr != m_end_itr; ++m_cur_itr) - { + for (char current = 0; m_cur_itr != m_end_itr; ++m_cur_itr) { current = *m_cur_itr; - if (current == '\r') - { + if (current == '\r') { // Ignore CR assuming the next character is LF. continue; } - if (current == '\n') - { - if (style == block_style_indicator_t::LITERAL) - { + if (current == '\n') { + if (style == block_style_indicator_t::LITERAL) { m_value_buffer.push_back(current); } else // block_style_indicator_t::FOLDED { - if (chars_in_line == 0) - { + if (chars_in_line == 0) { // Just append a newline if the current line is empty. m_value_buffer.push_back('\n'); is_extra_indented = false; continue; } - if (is_extra_indented) - { + if (is_extra_indented) { // A line being more indented is not folded. m_value_buffer.push_back('\n'); chars_in_line = 0; @@ -3855,26 +3365,21 @@ class lexical_analyzer // Append a newline if the next line is empty. bool is_end_of_token = false; bool is_next_empty = false; - for (std::size_t i = 0; i < indent; i++) - { - if (++m_cur_itr == m_end_itr) - { + for (std::size_t i = 0; i < indent; i++) { + if (++m_cur_itr == m_end_itr) { is_end_of_token = true; break; } current = *m_cur_itr; - if (current == ' ') - { + if (current == ' ') { continue; } - if (current == '\r') - { + if (current == '\r') { current = *++m_cur_itr; } - if (current == '\n') - { + if (current == '\n') { is_next_empty = true; break; } @@ -3883,20 +3388,17 @@ class lexical_analyzer break; } - if (is_end_of_token) - { + if (is_end_of_token) { m_value_buffer.push_back('\n'); break; } - if (is_next_empty) - { + if (is_next_empty) { m_value_buffer.push_back('\n'); continue; } - switch (char next = *(m_cur_itr + 1)) - { + switch (char next = *(m_cur_itr + 1)) { case '\r': { ++m_cur_itr; next = *++m_cur_itr; @@ -3927,10 +3429,8 @@ class lexical_analyzer // Handle indentation m_pos_tracker.update_position(m_cur_itr); cur_indent = m_pos_tracker.get_cur_pos_in_line(); - if (cur_indent < indent) - { - if (current != ' ') - { + if (cur_indent < indent) { + if (current != ' ') { // Interpret less indented non-space characters as the start of the next token. break; } @@ -3938,8 +3438,7 @@ class lexical_analyzer continue; } - if (style == block_style_indicator_t::FOLDED && chars_in_line == 0 && current == ' ') - { + if (style == block_style_indicator_t::FOLDED && chars_in_line == 0 && current == ' ') { // A line being more indented is not folded. m_value_buffer.push_back('\n'); is_extra_indented = true; @@ -3949,15 +3448,12 @@ class lexical_analyzer } // Manipulate the trailing line endings chomping indicator type. - switch (chomp) - { + switch (chomp) { case chomping_indicator_t::STRIP: - while (!m_value_buffer.empty()) - { + while (!m_value_buffer.empty()) { // Empty strings are handled above, so no check for the case. char last = m_value_buffer.back(); - if (last != '\n') - { + if (last != '\n') { break; } m_value_buffer.pop_back(); @@ -3965,17 +3461,14 @@ class lexical_analyzer break; case chomping_indicator_t::CLIP: { char last = m_value_buffer.back(); - if (last != '\n') - { + if (last != '\n') { // No need to chomp the trailing newlines. break; } - while (m_value_buffer.size() > 1) - { + while (m_value_buffer.size() > 1) { // Strings with only newlines are handled above, so no check for the case. char second_last = *(m_value_buffer.end() - 2); - if (second_last != '\n') - { + if (second_last != '\n') { break; } m_value_buffer.pop_back(); @@ -3991,12 +3484,10 @@ class lexical_analyzer /// @brief Handle unescaped control characters. /// @param c A target character. - void handle_unescaped_control_char(char c) - { + void handle_unescaped_control_char(char c) { FK_YAML_ASSERT(0x00 <= c && c <= 0x1F); - switch (c) - { + switch (c) { // 0x00(NULL) has already been handled above. case 0x01: emit_error("Control character U+0001 (SOH) must be escaped to \\u0001."); @@ -4064,30 +3555,26 @@ class lexical_analyzer /// @brief Unescape the given escaped unicode character. /// @param bytes_to_read The number of bytes to be read from the input buffer. - void handle_escaped_unicode(int bytes_to_read) - { + void handle_escaped_unicode(int bytes_to_read) { int read_size = bytes_to_read * 2; char32_t code_point = 0; - for (int i = read_size - 1; i >= 0; i--) - { + for (int i = read_size - 1; i >= 0; i--) { char four_bits = convert_hex_char_to_byte(*++m_cur_itr); // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) code_point |= static_cast(four_bits << (4 * i)); } // Treats the code point as a UTF-32 encoded character. - utf8_encoding::from_utf32(code_point, m_encode_buffer, m_encoded_size); - m_value_buffer.append(m_encode_buffer.data(), m_encoded_size); + utf8::from_utf32(code_point, m_encode_buffer, m_encoded_size); + m_value_buffer.append(reinterpret_cast(m_encode_buffer.data()), m_encoded_size); } /// @brief Gets the metadata of a following block style string scalar. /// @param chomp_type A variable to store the retrieved chomping style type. /// @param indent A variable to store the retrieved indent size. - void get_block_style_metadata(chomping_indicator_t& chomp_type, std::size_t& indent) - { + void get_block_style_metadata(chomping_indicator_t& chomp_type, std::size_t& indent) { chomp_type = chomping_indicator_t::CLIP; - switch (*++m_cur_itr) - { + switch (*++m_cur_itr) { case '-': chomp_type = chomping_indicator_t::STRIP; ++m_cur_itr; @@ -4100,14 +3587,12 @@ class lexical_analyzer break; } - if (*m_cur_itr == '0') - { + if (*m_cur_itr == '0') { emit_error("An indentation level for a block style scalar cannot be \'0\'"); } indent = 0; - if (std::isdigit(*m_cur_itr)) - { + if (std::isdigit(*m_cur_itr)) { indent = convert_hex_char_to_byte(*m_cur_itr++); } @@ -4116,19 +3601,15 @@ class lexical_analyzer } /// @brief Skip white spaces (half-width spaces and tabs) from the current position. - void skip_white_spaces() - { + void skip_white_spaces() { m_cur_itr = std::find_if_not(m_cur_itr, m_end_itr, [](char c) { return (c == ' ' || c == '\t'); }); } /// @brief Skip white spaces and newline codes (CR/LF) from the current position. - void skip_white_spaces_and_newline_codes() - { - if (m_cur_itr != m_end_itr) - { + void skip_white_spaces_and_newline_codes() { + if (m_cur_itr != m_end_itr) { m_cur_itr = std::find_if_not(m_cur_itr, m_end_itr, [](char c) { - switch (c) - { + switch (c) { case ' ': case '\t': case '\r': @@ -4142,19 +3623,14 @@ class lexical_analyzer } /// @brief Skip the rest in the current line. - void skip_until_line_end() - { - while (m_cur_itr != m_end_itr) - { - switch (*m_cur_itr) - { + void skip_until_line_end() { + while (m_cur_itr != m_end_itr) { + switch (*m_cur_itr) { case '\r': - if (++m_cur_itr == m_end_itr) - { + if (++m_cur_itr == m_end_itr) { return; } - if (*m_cur_itr == '\n') - { + if (*m_cur_itr == '\n') { ++m_cur_itr; } return; @@ -4170,8 +3646,7 @@ class lexical_analyzer /// @brief Emits an error with the given message. /// @param msg A message for the resulting error. - [[noreturn]] void emit_error(const char* msg) const - { + [[noreturn]] void emit_error(const char* msg) const { m_pos_tracker.update_position(m_cur_itr); throw fkyaml::parse_error(msg, m_pos_tracker.get_lines_read(), m_pos_tracker.get_cur_pos_in_line()); } @@ -4194,7 +3669,7 @@ class lexical_analyzer /// The last tag prefix std::string m_tag_prefix {}; /// A temporal buffer to store a UTF-8 encoded char sequence. - std::array m_encode_buffer {}; + std::array m_encode_buffer {}; /// The actual size of a UTF-8 encoded char sequence. std::size_t m_encoded_size {0}; /// The beginning position of the last lexical token. (zero origin) @@ -4213,16 +3688,14 @@ class lexical_analyzer float_number_type m_float_val {0.0}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_LEXICAL_ANALIZER_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -4246,7 +3719,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -4260,18 +3733,10 @@ FK_YAML_NAMESPACE_END // #include -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementaions of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of YAML directive sets. -enum class tag_t -{ +enum class tag_t { NON_SPECIFIC, //!< Represents a non-specific tag. CUSTOM_TAG, //!< Represents a cumstom tag SEQUENCE, //!< Represents a sequence tag. @@ -4283,23 +3748,14 @@ enum class tag_t STRING, //!< Represents a string tag. }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_TAG_T_HPP_ */ // #include -/// @namespace fkyaml -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @namespace detail -/// @brief namespace for internal implementaions of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN namespace /*default prefixes*/ { @@ -4309,32 +3765,26 @@ const std::string default_secondary_handle_prefix = "tag:yaml.org,2002:"; } // namespace -class tag_resolver -{ +class tag_resolver { public: /// @brief Resolve the input tag name into an expanded tag name prepended with a registered prefix. /// @param tag The input tag name. /// @return The type of a node deduced from the given tag name. - static tag_t resolve_tag(const std::string& tag, const std::shared_ptr& directives) - { + static tag_t resolve_tag(const std::string& tag, const std::shared_ptr& directives) { std::string normalized = normalize_tag_name(tag, directives); return convert_to_tag_type(normalized); } private: - static std::string normalize_tag_name(const std::string& tag, const std::shared_ptr& directives) - { - if (tag.empty()) - { + static std::string normalize_tag_name(const std::string& tag, const std::shared_ptr& directives) { + if (tag.empty()) { throw invalid_tag("tag must not be empty.", ""); } - if (tag[0] != '!') - { + if (tag[0] != '!') { throw invalid_tag("tag must start with \'!\'", tag.c_str()); } - if (tag.size() == 1) - { + if (tag.size() == 1) { // Non-specific tag ("!") will be interpreted as one of the following: // * tag:yaml.org,2002:seq // * tag:yaml.org,2002:map @@ -4345,27 +3795,22 @@ class tag_resolver } std::string normalized {"!<"}; - switch (tag[1]) - { + switch (tag[1]) { case '!': { // handle a secondary tag handle (!!suffix -> !<[secondary][suffix]>) bool is_null_or_empty = !directives || directives->secondary_handle_prefix.empty(); - if (is_null_or_empty) - { + if (is_null_or_empty) { normalized += default_secondary_handle_prefix + tag.substr(2); } - else - { + else { normalized += directives->secondary_handle_prefix + tag.substr(2); } break; } case '<': - if (tag[2] == '!') - { + if (tag[2] == '!') { bool is_null_or_empty = !directives || directives->primary_handle_prefix.empty(); - if (is_null_or_empty) - { + if (is_null_or_empty) { return normalized + default_primary_handle_prefix + tag.substr(3); } return normalized + directives->primary_handle_prefix + tag.substr(3); @@ -4378,22 +3823,19 @@ class tag_resolver auto tag_end_pos = tag.find_first_of('!', 1); // handle a named handle (!tag!suffix -> !<[tag][suffix]>) - if (tag_end_pos != std::string::npos) - { + if (tag_end_pos != std::string::npos) { // there must be a non-empty suffix. (already checked by the lexer.) FK_YAML_ASSERT(tag_end_pos < tag.size() - 1); bool is_null_or_empty = !directives || directives->named_handle_map.empty(); - if (is_null_or_empty) - { + if (is_null_or_empty) { throw invalid_tag("named handle has not been registered.", tag.c_str()); } // find the extracted named handle in the map. auto named_handle_itr = directives->named_handle_map.find(tag.substr(0, tag_end_pos + 1)); auto end_itr = directives->named_handle_map.end(); - if (named_handle_itr == end_itr) - { + if (named_handle_itr == end_itr) { throw invalid_tag("named handle has not been registered.", tag.c_str()); } @@ -4408,12 +3850,10 @@ class tag_resolver // handle a primary tag handle (!suffix -> !<[primary][suffix]>) bool is_null_or_empty = !directives || directives->primary_handle_prefix.empty(); - if (is_null_or_empty) - { + if (is_null_or_empty) { normalized += default_primary_handle_prefix + tag.substr(1); } - else - { + else { normalized += directives->primary_handle_prefix + tag.substr(1); } @@ -4425,48 +3865,37 @@ class tag_resolver return normalized; } - static tag_t convert_to_tag_type(const std::string& normalized) - { - if (normalized == "!") - { + static tag_t convert_to_tag_type(const std::string& normalized) { + if (normalized == "!") { return tag_t::NON_SPECIFIC; } - if (normalized.size() < 24 /* size of !") - { + if (normalized == "!") { return tag_t::SEQUENCE; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::MAPPING; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::NULL_VALUE; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::BOOLEAN; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::INTEGER; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::FLOATING_NUMBER; } - if (normalized == "!") - { + if (normalized == "!") { return tag_t::STRING; } @@ -4474,9 +3903,7 @@ class tag_resolver } }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_TAG_RESOLVER_HPP_ */ @@ -4491,18 +3918,12 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A class which provides the feature of deserializing YAML documents. /// @tparam BasicNodeType A type of the container for deserialized YAML values. template -class basic_deserializer -{ +class basic_deserializer { static_assert(is_basic_node::value, "basic_deserializer only accepts basic_node<...>"); /** A type for the target basic_node. */ @@ -4522,15 +3943,13 @@ class basic_deserializer /** A type for string node values. */ using string_type = typename node_type::string_type; - struct indentation - { + struct indentation { indentation() = default; indentation(std::size_t _line, std::size_t _indent, bool _is_explicit_key) : line(_line), indent(_indent), - is_explicit_key(_is_explicit_key) - { + is_explicit_key(_is_explicit_key) { } std::size_t line {0}; @@ -4547,8 +3966,7 @@ class basic_deserializer /// @param source A YAML-formatted source string. /// @return node_type A root YAML node deserialized from the source string. template ::value, int> = 0> - node_type deserialize(InputAdapterType&& input_adapter) - { + node_type deserialize(InputAdapterType&& input_adapter) { lexer_type lexer(std::forward(input_adapter)); lexical_token_t type {lexical_token_t::END_OF_BUFFER}; @@ -4558,6 +3976,16 @@ class basic_deserializer // parse directives first. deserialize_directives(lexer, root, type); + switch (type) { + case lexical_token_t::SEQUENCE_BLOCK_PREFIX: + case lexical_token_t::SEQUENCE_FLOW_BEGIN: + root = node_type::sequence(); + apply_directive_set(root); + break; + default: + break; + } + // parse YAML nodes recursively deserialize_node(lexer, type); @@ -4577,26 +4005,20 @@ class basic_deserializer /// @param lexer The lexical analyzer to be used. /// @param root The root YAML node. /// @param type The variable to store the last lexical token type. - void deserialize_directives(lexer_type& lexer, node_type& root, lexical_token_t& last_type) - { - for (;;) - { + void deserialize_directives(lexer_type& lexer, node_type& root, lexical_token_t& last_type) { + for (;;) { lexical_token_t type = lexer.get_next_token(); - switch (type) - { + switch (type) { case lexical_token_t::YAML_VER_DIRECTIVE: - if (!mp_directive_set) - { + if (!mp_directive_set) { mp_directive_set = std::shared_ptr(new directive_set()); } - if (!root.mp_directive_set) - { + if (!root.mp_directive_set) { root.mp_directive_set = mp_directive_set; } - if (mp_directive_set->is_version_specified) - { + if (mp_directive_set->is_version_specified) { throw parse_error( "YAML version cannot be specified more than once.", lexer.get_lines_processed(), @@ -4607,22 +4029,18 @@ class basic_deserializer mp_directive_set->is_version_specified = true; break; case lexical_token_t::TAG_DIRECTIVE: { - if (!mp_directive_set) - { + if (!mp_directive_set) { mp_directive_set = std::shared_ptr(new directive_set()); } - if (!root.mp_directive_set) - { + if (!root.mp_directive_set) { root.mp_directive_set = mp_directive_set; } const std::string& tag_handle = lexer.get_tag_handle(); - switch (tag_handle.size()) - { + switch (tag_handle.size()) { case 1: { bool is_already_specified = !mp_directive_set->primary_handle_prefix.empty(); - if (is_already_specified) - { + if (is_already_specified) { throw parse_error( "Primary handle cannot be specified more than once.", lexer.get_lines_processed(), @@ -4633,8 +4051,7 @@ class basic_deserializer } case 2: { bool is_already_specified = !mp_directive_set->secondary_handle_prefix.empty(); - if (is_already_specified) - { + if (is_already_specified) { throw parse_error( "Secondary handle cannot be specified more than once.", lexer.get_lines_processed(), @@ -4646,8 +4063,7 @@ class basic_deserializer default: { bool is_already_specified = !(mp_directive_set->named_handle_map.emplace(tag_handle, lexer.get_tag_prefix()).second); - if (is_already_specified) - { + if (is_already_specified) { throw parse_error( "The same named handle cannot be specified more than once.", lexer.get_lines_processed(), @@ -4675,31 +4091,26 @@ class basic_deserializer /// @brief Deserializes the YAML nodes recursively. /// @param lexer The lexical analyzer to be used. /// @param first_type The first lexical token type. - void deserialize_node(lexer_type& lexer, lexical_token_t first_type) - { + void deserialize_node(lexer_type& lexer, lexical_token_t first_type) { lexical_token_t type = first_type; std::size_t line = lexer.get_lines_processed(); std::size_t indent = lexer.get_last_token_begin_pos(); - do - { - switch (type) - { + do { + switch (type) { case lexical_token_t::END_OF_BUFFER: // This handles an empty input. break; case lexical_token_t::EXPLICIT_KEY_PREFIX: { bool needs_to_move_back = !m_indent_stack.empty() && indent < m_indent_stack.back().indent; - if (needs_to_move_back) - { + if (needs_to_move_back) { auto target_itr = std::find_if( // LCOV_EXCL_LINE m_indent_stack.rbegin(), m_indent_stack.rend(), [indent](const indentation& i) { return indent > i.indent; }); auto pop_num = std::distance(m_indent_stack.rbegin(), target_itr); - for (auto i = 0; i < pop_num; i++) - { + for (auto i = 0; i < pop_num; i++) { // move back to the previous container node. mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); @@ -4707,8 +4118,7 @@ class basic_deserializer } } - if (mp_current_node->is_null()) - { + if (mp_current_node->is_null()) { *mp_current_node = node_type::mapping(); } @@ -4716,8 +4126,7 @@ class basic_deserializer m_indent_stack.emplace_back(line, indent, true); type = lexer.get_next_token(); - if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) - { + if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) { m_indent_stack.emplace_back(lexer.get_lines_processed(), lexer.get_last_token_begin_pos(), false); mp_current_node = new node_type(node_t::SEQUENCE); apply_directive_set(*mp_current_node); @@ -4733,8 +4142,7 @@ class basic_deserializer } case lexical_token_t::KEY_SEPARATOR: { bool is_stack_empty = m_node_stack.empty(); - if (is_stack_empty) - { + if (is_stack_empty) { throw parse_error("A key separator found without key.", line, indent); } @@ -4747,8 +4155,7 @@ class basic_deserializer indent = lexer.get_last_token_begin_pos(); bool found_props = deserialize_node_properties(lexer, type, line, indent); - if (found_props && line == lexer.get_lines_processed()) - { + if (found_props && line == lexer.get_lines_processed()) { // defer applying node properties for the subsequent node on the same line. continue; } @@ -4758,19 +4165,15 @@ class basic_deserializer bool is_implicit_same_line = (line == old_line) && (m_indent_stack.empty() || old_indent > m_indent_stack.back().indent); - if (is_implicit_same_line) - { + if (is_implicit_same_line) { // a key separator for an implicit key with its value on the same line. continue; } - if (line > old_line) - { - if (m_needs_tag_impl) - { + if (line > old_line) { + if (m_needs_tag_impl) { tag_t tag_type = tag_resolver::resolve_tag(m_tag_name, mp_directive_set); - if (tag_type == tag_t::MAPPING) - { + if (tag_type == tag_t::MAPPING) { // set YAML node properties here to distinguish them from those for the first key node // as shown in the following snippet: // @@ -4787,8 +4190,7 @@ class basic_deserializer } } - switch (type) - { + switch (type) { case lexical_token_t::SEQUENCE_BLOCK_PREFIX: // a key separator preceeding block sequence entries *mp_current_node = node_type::sequence(); @@ -4820,20 +4222,17 @@ class basic_deserializer // handle explicit mapping key separators. - while (!m_indent_stack.back().is_explicit_key) - { + while (!m_indent_stack.back().is_explicit_key) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); m_indent_stack.pop_back(); } - if (m_node_stack.back()->is_sequence()) - { + if (m_node_stack.back()->is_sequence()) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); } - if (m_node_stack.back() == mp_current_node) - { + if (m_node_stack.back() == mp_current_node) { // This path is for nested explicit mapping keys like: // ```yaml // ? ? foo @@ -4851,8 +4250,7 @@ class basic_deserializer m_node_stack.push_back(m_node_stack.back()); m_indent_stack.back().is_explicit_key = false; - if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) - { + if (type == lexical_token_t::SEQUENCE_BLOCK_PREFIX) { *mp_current_node = node_type::sequence(); apply_directive_set(*mp_current_node); apply_node_properties(*mp_current_node); @@ -4862,6 +4260,7 @@ class basic_deserializer continue; } case lexical_token_t::VALUE_SEPARATOR: + FK_YAML_ASSERT(m_flow_context_depth > 0); break; // just ignore directives case lexical_token_t::YAML_VER_DIRECTIVE: @@ -4880,18 +4279,15 @@ class basic_deserializer continue; case lexical_token_t::SEQUENCE_BLOCK_PREFIX: - if (mp_current_node->is_sequence()) - { + if (mp_current_node->is_sequence()) { bool is_empty = mp_current_node->empty(); - if (is_empty) - { + if (is_empty) { m_indent_stack.emplace_back(line, indent, false); break; } // move back to the previous sequence if necessary. - while (!mp_current_node->is_sequence() || indent != m_indent_stack.back().indent) - { + while (!mp_current_node->is_sequence() || indent != m_indent_stack.back().indent) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); m_indent_stack.pop_back(); @@ -4899,15 +4295,8 @@ class basic_deserializer break; } - // if the current node is a mapping. - if (m_node_stack.empty()) - { - throw parse_error("Invalid sequence block prefix(- ) found.", line, indent); - } - // move back to the previous sequence if necessary. - while (!mp_current_node->is_sequence() || indent != m_indent_stack.back().indent) - { + while (!mp_current_node->is_sequence() || indent != m_indent_stack.back().indent) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); m_indent_stack.pop_back(); @@ -4925,23 +4314,32 @@ class basic_deserializer apply_directive_set(*mp_current_node); apply_node_properties(*mp_current_node); break; - case lexical_token_t::SEQUENCE_FLOW_END: + case lexical_token_t::SEQUENCE_FLOW_END: { FK_YAML_ASSERT(m_flow_context_depth > 0); --m_flow_context_depth; - mp_current_node = m_node_stack.back(); - m_node_stack.pop_back(); + bool is_stack_empty = m_node_stack.empty(); + if (!is_stack_empty) { + mp_current_node = m_node_stack.back(); + m_node_stack.pop_back(); + } break; + } case lexical_token_t::MAPPING_FLOW_BEGIN: ++m_flow_context_depth; *mp_current_node = node_type::mapping(); apply_directive_set(*mp_current_node); apply_node_properties(*mp_current_node); break; - case lexical_token_t::MAPPING_FLOW_END: + case lexical_token_t::MAPPING_FLOW_END: { FK_YAML_ASSERT(m_flow_context_depth > 0); --m_flow_context_depth; - mp_current_node = m_node_stack.back(); + bool is_stack_empty = m_node_stack.empty(); + if (!is_stack_empty) { + mp_current_node = m_node_stack.back(); + m_node_stack.pop_back(); + } break; + } case lexical_token_t::ALIAS_PREFIX: case lexical_token_t::NULL_VALUE: case lexical_token_t::BOOLEAN_VALUE: @@ -4949,8 +4347,7 @@ class basic_deserializer case lexical_token_t::FLOAT_NUMBER_VALUE: case lexical_token_t::STRING_VALUE: { bool do_continue = deserialize_scalar(lexer, indent, line, type); - if (do_continue) - { + if (do_continue) { continue; } break; @@ -4962,9 +4359,8 @@ class basic_deserializer break; } - lexical_token_t prev_type = type; type = lexer.get_next_token(); - indent = (prev_type == lexical_token_t::ANCHOR_PREFIX) ? indent : lexer.get_last_token_begin_pos(); + indent = lexer.get_last_token_begin_pos(); line = lexer.get_lines_processed(); } while (type != lexical_token_t::END_OF_BUFFER); } @@ -4976,25 +4372,20 @@ class basic_deserializer /// @param indent The variable to store the indent of either the first property or the last non-property token. /// @return true if any property is found, false otherwise. bool deserialize_node_properties( - lexer_type& lexer, lexical_token_t& last_type, std::size_t& line, std::size_t& indent) - { + lexer_type& lexer, lexical_token_t& last_type, std::size_t& line, std::size_t& indent) { std::set prop_types {lexical_token_t::ANCHOR_PREFIX, lexical_token_t::TAG_PREFIX}; lexical_token_t type = last_type; bool ends_loop {false}; - do - { - if (line < lexer.get_lines_processed()) - { + do { + if (line < lexer.get_lines_processed()) { break; } - switch (type) - { + switch (type) { case lexical_token_t::ANCHOR_PREFIX: { bool already_specified = prop_types.find(type) == prop_types.end(); - if (already_specified) - { + if (already_specified) { throw parse_error( "anchor name cannot be specified more than once to the same node.", lexer.get_lines_processed(), @@ -5005,8 +4396,7 @@ class basic_deserializer m_anchor_name = lexer.get_string(); m_needs_anchor_impl = true; - if (prop_types.size() == 1) - { + if (prop_types.size() == 1) { line = lexer.get_lines_processed(); indent = lexer.get_last_token_begin_pos(); } @@ -5015,8 +4405,7 @@ class basic_deserializer } case lexical_token_t::TAG_PREFIX: { bool already_specified = prop_types.find(type) == prop_types.end(); - if (already_specified) - { + if (already_specified) { throw parse_error( "tag name cannot be specified more than once to the same node.", lexer.get_lines_processed(), @@ -5027,8 +4416,7 @@ class basic_deserializer m_tag_name = lexer.get_string(); m_needs_tag_impl = true; - if (prop_types.size() == 1) - { + if (prop_types.size() == 1) { line = lexer.get_lines_processed(); indent = lexer.get_last_token_begin_pos(); } @@ -5040,15 +4428,13 @@ class basic_deserializer break; } - if (!ends_loop) - { + if (!ends_loop) { type = lexer.get_next_token(); } } while (!ends_loop); last_type = type; - if (prop_types.size() == 2) - { + if (prop_types.size() == 2) { line = lexer.get_lines_processed(); indent = lexer.get_last_token_begin_pos(); } @@ -5058,23 +4444,19 @@ class basic_deserializer /// @brief Add new key string to the current YAML node. /// @param key a key string to be added to the current YAML node. - void add_new_key(node_type&& key, const std::size_t indent, const std::size_t line) - { - if (!m_indent_stack.empty() && indent < m_indent_stack.back().indent) - { + void add_new_key(node_type&& key, const std::size_t indent, const std::size_t line) { + if (!m_indent_stack.empty() && indent < m_indent_stack.back().indent) { auto target_itr = std::find_if(m_indent_stack.rbegin(), m_indent_stack.rend(), [indent](const indentation& i) { return indent == i.indent; }); bool is_indent_valid = (target_itr != m_indent_stack.rend()); - if (!is_indent_valid) - { + if (!is_indent_valid) { throw parse_error("Detected invalid indentaion.", line, indent); } auto pop_num = std::distance(m_indent_stack.rbegin(), target_itr); - for (auto i = 0; i < pop_num; i++) - { + for (auto i = 0; i < pop_num; i++) { // move back to the previous container node. mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); @@ -5082,8 +4464,7 @@ class basic_deserializer } } - if (mp_current_node->is_sequence()) - { + if (mp_current_node->is_sequence()) { mp_current_node->template get_value_ref().emplace_back(node_type::mapping()); m_node_stack.push_back(mp_current_node); mp_current_node = &(mp_current_node->operator[](mp_current_node->size() - 1)); @@ -5091,16 +4472,15 @@ class basic_deserializer mapping_type& map = mp_current_node->template get_value_ref(); bool is_empty = map.empty(); - if (is_empty) - { - m_indent_stack.emplace_back(line, indent, false); + if (is_empty) { + if (m_flow_context_depth == 0) { + m_indent_stack.emplace_back(line, indent, false); + } } - else - { + else { // check key duplication in the current mapping if not empty. auto itr = map.find(key); - if (itr != map.end()) - { + if (itr != map.end()) { throw parse_error("Detected duplication in mapping keys.", line, indent); } } @@ -5112,18 +4492,15 @@ class basic_deserializer /// @brief Assign node value to the current node. /// @param node_value A rvalue node_type object to be assigned to the current node. - void assign_node_value(node_type&& node_value) noexcept - { - if (mp_current_node->is_sequence()) - { + void assign_node_value(node_type&& node_value) noexcept { + if (mp_current_node->is_sequence()) { mp_current_node->template get_value_ref().emplace_back(std::move(node_value)); return; } // a scalar node *mp_current_node = std::move(node_value); - if (!m_indent_stack.back().is_explicit_key) - { + if (m_flow_context_depth > 0 || !m_indent_stack.back().is_explicit_key) { mp_current_node = m_node_stack.back(); m_node_stack.pop_back(); } @@ -5135,17 +4512,14 @@ class basic_deserializer /// @param indent The last indent size. /// @param line The last line. /// @return The created YAML scalar node. - node_type create_scalar_node(lexer_type& lexer, lexical_token_t type, std::size_t indent, std::size_t line) - { + node_type create_scalar_node(lexer_type& lexer, lexical_token_t type, std::size_t indent, std::size_t line) { FK_YAML_ASSERT( type == lexical_token_t::NULL_VALUE || type == lexical_token_t::BOOLEAN_VALUE || type == lexical_token_t::INTEGER_VALUE || type == lexical_token_t::FLOAT_NUMBER_VALUE || type == lexical_token_t::STRING_VALUE || type == lexical_token_t::ALIAS_PREFIX); - if (m_needs_tag_impl) - { - if (type == lexical_token_t::ALIAS_PREFIX) - { + if (m_needs_tag_impl) { + if (type == lexical_token_t::ALIAS_PREFIX) { throw parse_error("Tag cannot be specified to alias nodes", line, indent); } @@ -5153,8 +4527,7 @@ class basic_deserializer FK_YAML_ASSERT(tag_type != tag_t::SEQUENCE && tag_type != tag_t::MAPPING); - switch (tag_type) - { + switch (tag_type) { case tag_t::NULL_VALUE: type = lexical_token_t::NULL_VALUE; break; @@ -5182,8 +4555,7 @@ class basic_deserializer } node_type node {}; - switch (type) - { + switch (type) { case lexical_token_t::NULL_VALUE: node = node_type(lexer.get_null()); break; @@ -5202,8 +4574,7 @@ class basic_deserializer case lexical_token_t::ALIAS_PREFIX: { const string_type& alias_name = lexer.get_string(); auto itr = m_anchor_table.find(alias_name); - if (itr == m_anchor_table.end()) - { + if (itr == m_anchor_table.end()) { throw parse_error("The given anchor name must appear prior to the alias node.", line, indent); } node = node_type::alias_of(m_anchor_table[alias_name]); @@ -5224,27 +4595,21 @@ class basic_deserializer /// @param indent The current indentation width. Can be updated in this function. /// @param line The number of processed lines. Can be updated in this function. /// @return true if next token has already been got, false otherwise. - bool deserialize_scalar(lexer_type& lexer, std::size_t& indent, std::size_t& line, lexical_token_t& type) - { + bool deserialize_scalar(lexer_type& lexer, std::size_t& indent, std::size_t& line, lexical_token_t& type) { node_type node = create_scalar_node(lexer, type, indent, line); - if (mp_current_node->is_mapping()) - { + if (mp_current_node->is_mapping()) { add_new_key(std::move(node), indent, line); return false; } type = lexer.get_next_token(); - if (type == lexical_token_t::KEY_SEPARATOR) - { - if (mp_current_node->is_scalar()) - { - if (line != lexer.get_lines_processed()) - { + if (type == lexical_token_t::KEY_SEPARATOR) { + if (mp_current_node->is_scalar()) { + if (line != lexer.get_lines_processed()) { // This path is for explicit mapping key separator(:) assign_node_value(std::move(node)); - if (!m_indent_stack.back().is_explicit_key) - { + if (!m_indent_stack.back().is_explicit_key) { m_indent_stack.pop_back(); } indent = lexer.get_last_token_begin_pos(); @@ -5253,8 +4618,7 @@ class basic_deserializer } indentation& last_indent = m_indent_stack.back(); - if (last_indent.line == line && !last_indent.is_explicit_key) - { + if (last_indent.line == line && !last_indent.is_explicit_key) { throw parse_error("multiple mapping keys are specified on the same line.", line, indent); } @@ -5263,8 +4627,7 @@ class basic_deserializer } add_new_key(std::move(node), indent, line); } - else - { + else { assign_node_value(std::move(node)); } indent = lexer.get_last_token_begin_pos(); @@ -5274,28 +4637,23 @@ class basic_deserializer /// @brief Set YAML directive properties to the given node. /// @param node A node_type object to be set YAML directive properties. - void apply_directive_set(node_type& node) noexcept - { - if (mp_directive_set) - { + void apply_directive_set(node_type& node) noexcept { + if (mp_directive_set) { node.mp_directive_set = mp_directive_set; } } /// @brief Set YAML node properties (anchor and/or tag names) to the given node. /// @param node A node type object to be set YAML node properties. - void apply_node_properties(node_type& node) - { - if (m_needs_anchor_impl) - { + void apply_node_properties(node_type& node) { + if (m_needs_anchor_impl) { node.add_anchor_name(m_anchor_name); m_anchor_table[m_anchor_name] = node; m_needs_anchor_impl = false; m_anchor_name.clear(); } - if (m_needs_tag_impl) - { + if (m_needs_tag_impl) { node.add_tag_name(m_tag_name); m_needs_tag_impl = false; m_tag_name.clear(); @@ -5304,8 +4662,7 @@ class basic_deserializer /// @brief Update the target YAML version with an input string. /// @param version_str A YAML version string. - yaml_version_t convert_yaml_version(const string_type& version_str) noexcept - { + yaml_version_t convert_yaml_version(const string_type& version_str) noexcept { return (version_str == "1.1") ? yaml_version_t::VER_1_1 : yaml_version_t::VER_1_2; } @@ -5332,16 +4689,14 @@ class basic_deserializer string_type m_tag_name {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_DESERIALIZER_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -5366,7 +4721,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -5385,7 +4740,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -5399,17 +4754,11 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Definition of Unicode encoding types /// @note Since fkYAML doesn't treat UTF-16/UTF-32 encoded characters per byte, endians do not matter. -enum class utf_encode_t -{ +enum class utf_encode_t { UTF_8, //!< UTF-8 UTF_16BE, //!< UTF-16 Big Endian UTF_16LE, //!< UTF-16 Little Endian @@ -5417,82 +4766,65 @@ enum class utf_encode_t UTF_32LE, //!< UTF-32 Little Endian }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_ENCODINGS_UTF_ENCODE_T_HPP_ */ // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Detect an encoding type for UTF-8 expected inputs. /// @note This function doesn't support the case where the first character is null. /// @param[in] bytes 4 bytes of an input character sequence. /// @param[out] has_bom Whether or not the input contains a BOM. /// @return A detected encoding type. -inline utf_encode_t detect_encoding_type(const std::array& bytes, bool& has_bom) noexcept -{ +inline utf_encode_t detect_encoding_type(const std::array& bytes, bool& has_bom) noexcept { has_bom = false; // Check if a BOM exists. - if (bytes[0] == uint8_t(0xEFu) && bytes[1] == uint8_t(0xBBu) && bytes[2] == uint8_t(0xBFu)) - { + if (bytes[0] == uint8_t(0xEFu) && bytes[1] == uint8_t(0xBBu) && bytes[2] == uint8_t(0xBFu)) { has_bom = true; return utf_encode_t::UTF_8; } - if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == uint8_t(0xFEu) && bytes[3] == uint8_t(0xFFu)) - { + if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == uint8_t(0xFEu) && bytes[3] == uint8_t(0xFFu)) { has_bom = true; return utf_encode_t::UTF_32BE; } - if (bytes[0] == uint8_t(0xFFu) && bytes[1] == uint8_t(0xFEu) && bytes[2] == 0 && bytes[3] == 0) - { + if (bytes[0] == uint8_t(0xFFu) && bytes[1] == uint8_t(0xFEu) && bytes[2] == 0 && bytes[3] == 0) { has_bom = true; return utf_encode_t::UTF_32LE; } - if (bytes[0] == uint8_t(0xFEu) && bytes[1] == uint8_t(0xFFu)) - { + if (bytes[0] == uint8_t(0xFEu) && bytes[1] == uint8_t(0xFFu)) { has_bom = true; return utf_encode_t::UTF_16BE; } - if (bytes[0] == uint8_t(0xFFu) && bytes[1] == uint8_t(0xFEu)) - { + if (bytes[0] == uint8_t(0xFFu) && bytes[1] == uint8_t(0xFEu)) { has_bom = true; return utf_encode_t::UTF_16LE; } // Test the first character assuming it's an ASCII character. - if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0 && 0 < bytes[3] && bytes[3] < uint8_t(0x80u)) - { + if (bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0 && 0 < bytes[3] && bytes[3] < uint8_t(0x80u)) { return utf_encode_t::UTF_32BE; } - if (0 < bytes[0] && bytes[0] < uint8_t(0x80u) && bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0) - { + if (0 < bytes[0] && bytes[0] < uint8_t(0x80u) && bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0) { return utf_encode_t::UTF_32LE; } - if (bytes[0] == 0 && 0 < bytes[1] && bytes[1] < uint8_t(0x80u)) - { + if (bytes[0] == 0 && 0 < bytes[1] && bytes[1] < uint8_t(0x80u)) { return utf_encode_t::UTF_16BE; } - if (0 < bytes[0] && bytes[0] < uint8_t(0x80u) && bytes[1] == 0) - { + if (0 < bytes[0] && bytes[0] < uint8_t(0x80u) && bytes[1] == 0) { return utf_encode_t::UTF_16LE; } @@ -5506,25 +4838,20 @@ inline utf_encode_t detect_encoding_type(const std::array& bytes, bo /// @param end The end of input iterators. /// @return A detected encoding type. template ())))> -inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& end) -{ +inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& end) { std::array bytes = {{0xFFu, 0xFFu, 0xFFu, 0xFFu}}; - switch (ElemSize) - { - case sizeof(char): { // this case covers char8_t as well when compiled with C++20 features. - for (std::size_t i = 0; i < 4 && begin + i != end; i++) - { + switch (ElemSize) { + case sizeof(char): { // this case covers char8_t as well when compiled with C++20 or better. + for (std::size_t i = 0; i < 4 && begin + i != end; i++) { bytes[i] = uint8_t(begin[i]); } bool has_bom = false; utf_encode_t encode_type = detect_encoding_type(bytes, has_bom); - if (has_bom) - { + if (has_bom) { // skip reading the BOM. - switch (encode_type) - { + switch (encode_type) { case utf_encode_t::UTF_8: std::advance(begin, 3); break; @@ -5542,12 +4869,10 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& return encode_type; } case sizeof(char16_t): { - if (begin == end) - { + if (begin == end) { return utf_encode_t::UTF_16BE; } - for (int i = 0; i < 2 && begin + i != end; i++) - { + for (int i = 0; i < 2 && begin + i != end; i++) { bytes[i * 2] = uint8_t((begin[i] & 0xFF00u) >> 8); bytes[i * 2 + 1] = uint8_t(begin[i] & 0xFFu); } @@ -5555,13 +4880,11 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& bool has_bom = false; utf_encode_t encode_type = detect_encoding_type(bytes, has_bom); - if (encode_type != utf_encode_t::UTF_16BE && encode_type != utf_encode_t::UTF_16LE) - { + if (encode_type != utf_encode_t::UTF_16BE && encode_type != utf_encode_t::UTF_16LE) { throw exception("char16_t characters must be encoded in the UTF-16 format."); } - if (has_bom) - { + if (has_bom) { // skip reading the BOM. std::advance(begin, 1); } @@ -5569,8 +4892,7 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& return encode_type; } case sizeof(char32_t): { - if (begin == end) - { + if (begin == end) { return utf_encode_t::UTF_32BE; } @@ -5582,13 +4904,11 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& bool has_bom = false; utf_encode_t encode_type = detect_encoding_type(bytes, has_bom); - if (encode_type != utf_encode_t::UTF_32BE && encode_type != utf_encode_t::UTF_32LE) - { + if (encode_type != utf_encode_t::UTF_32BE && encode_type != utf_encode_t::UTF_32LE) { throw exception("char32_t characters must be encoded in the UTF-32 format."); } - if (has_bom) - { + if (has_bom) { // skip reading the BOM. std::advance(begin, 1); } @@ -5600,15 +4920,12 @@ inline utf_encode_t detect_encoding_and_skip_bom(ItrType& begin, const ItrType& } } -inline utf_encode_t detect_encoding_and_skip_bom(std::FILE* file) noexcept -{ +inline utf_encode_t detect_encoding_and_skip_bom(std::FILE* file) noexcept { std::array bytes = {{0xFFu, 0xFFu, 0xFFu, 0xFFu}}; - for (std::size_t i = 0; i < 4; i++) - { + for (std::size_t i = 0; i < 4; i++) { char byte = 0; std::size_t size = std::fread(&byte, sizeof(char), 1, file); - if (size != sizeof(char)) - { + if (size != sizeof(char)) { break; } bytes[i] = uint8_t(byte & 0xFF); @@ -5619,10 +4936,8 @@ inline utf_encode_t detect_encoding_and_skip_bom(std::FILE* file) noexcept // move back to the beginning if a BOM doesn't exist. long offset = 0; - if (has_bom) - { - switch (encode_type) - { + if (has_bom) { + switch (encode_type) { case utf_encode_t::UTF_8: offset = 3; break; @@ -5641,16 +4956,13 @@ inline utf_encode_t detect_encoding_and_skip_bom(std::FILE* file) noexcept return encode_type; } -inline utf_encode_t detect_encoding_and_skip_bom(std::istream& is) noexcept -{ +inline utf_encode_t detect_encoding_and_skip_bom(std::istream& is) noexcept { std::array bytes = {{0xFFu, 0xFFu, 0xFFu, 0xFFu}}; - for (std::size_t i = 0; i < 4; i++) - { + for (std::size_t i = 0; i < 4; i++) { char ch = 0; is.read(&ch, 1); std::streamsize size = is.gcount(); - if (size != 1) - { + if (size != 1) { // without this, seekg() fails in the switch-case statement below. is.clear(); break; @@ -5663,10 +4975,8 @@ inline utf_encode_t detect_encoding_and_skip_bom(std::istream& is) noexcept // move back to the beginning if a BOM doesn't exist. std::streamoff offset = 0; - if (has_bom) - { - switch (encode_type) - { + if (has_bom) { + switch (encode_type) { case utf_encode_t::UTF_8: offset = 3; break; @@ -5685,27 +4995,20 @@ inline utf_encode_t detect_encoding_and_skip_bom(std::istream& is) noexcept return encode_type; } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_ENCODINGS_ENCODE_DETECTOR_HPP_ */ // #include -// #include +// #include // #include // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /////////////////////// // input_adapter // @@ -5718,8 +5021,8 @@ class iterator_input_adapter; /// @tparam IterType An iterator type. template class iterator_input_adapter< - IterType, enable_if_t::value_type>, char>::value>> -{ + IterType, + enable_if_t::value_type>, char>::value>> { public: /// @brief Construct a new iterator_input_adapter object. iterator_input_adapter() = default; @@ -5731,8 +5034,7 @@ class iterator_input_adapter< iterator_input_adapter(IterType begin, IterType end, utf_encode_t encode_type) noexcept : m_current(begin), m_end(end), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { } // allow only move construct/assignment like other input adapters. @@ -5744,12 +5046,10 @@ class iterator_input_adapter< /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { + void fill_buffer(std::string& buffer) { buffer.clear(); - switch (m_encode_type) - { + switch (m_encode_type) { case utf_encode_t::UTF_8: fill_buffer_utf8(buffer); break; @@ -5767,52 +5067,43 @@ class iterator_input_adapter< private: /// @brief The concrete implementation of fill_buffer() for UTF-8 encoded inputs. /// @param buffer A buffer to be filled with the input. - void fill_buffer_utf8(std::string& buffer) - { + void fill_buffer_utf8(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_8); IterType current = m_current; - while (current != m_end) - { - char first = *current++; - - // The first byte starts with 0b0XXX'XXXX -> 1-byte character - if ((first & 0xC0) == 0x80) - { - // The first byte must not start with 0b10XX'XXXX - std::array bytes {{first}}; - throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); - } - // The first byte starts with 0b110X'XXXX -> 2-byte character - else if ((first & 0xE0) == 0xC0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + while (current != m_end) { + uint8_t first = uint8_t(*current++); + uint32_t num_bytes = utf8::get_num_bytes(first); + + switch (num_bytes) { + case 2: { + std::initializer_list bytes {first, uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0b1110'XXXX -> 3-byte character - else if ((first & 0xF0) == 0xE0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 3: { + std::initializer_list bytes {first, uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0x1111'0XXX -> 4-byte character - else if ((first & 0xF8) == 0xF0) - { - std::array bytes { - {uint8_t(first), uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 4: { + std::initializer_list bytes { + first, uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; + } + case 1: + default: + break; } } @@ -5821,13 +5112,11 @@ class iterator_input_adapter< /// @brief The concrete implementation of get_character() for UTF-16 encoded inputs. /// @param buffer A buffer to be filled with the input. - void fill_buffer_utf16(std::string& buffer) - { + void fill_buffer_utf16(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_16BE || m_encode_type == utf_encode_t::UTF_16LE); int shift_bits[2] {0, 0}; - if (m_encode_type == utf_encode_t::UTF_16BE) - { + if (m_encode_type == utf_encode_t::UTF_16BE) { shift_bits[0] = 8; } else // m_encode_type == utf_encode_t::UTF_16LE @@ -5837,40 +5126,35 @@ class iterator_input_adapter< std::array encoded_buffer {{0, 0}}; std::size_t encoded_buf_size {0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (m_current != m_end || encoded_buf_size != 0) - { - while (m_current != m_end && encoded_buf_size < 2) - { - encoded_buffer[encoded_buf_size] = char16_t(uint8_t(*m_current++) << shift_bits[0]); - encoded_buffer[encoded_buf_size++] |= char16_t(uint8_t(*m_current++) << shift_bits[1]); + while (m_current != m_end || encoded_buf_size != 0) { + while (m_current != m_end && encoded_buf_size < 2) { + encoded_buffer[encoded_buf_size] = static_cast(uint8_t(*m_current++) << shift_bits[0]); + encoded_buffer[encoded_buf_size++] |= static_cast(uint8_t(*m_current++) << shift_bits[1]); } std::size_t consumed_size = 0; - utf8_encoding::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); + utf8::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); - if (consumed_size == 1) - { + if (consumed_size == 1) { encoded_buffer[0] = encoded_buffer[1]; encoded_buffer[1] = 0; } encoded_buf_size -= consumed_size; - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } /// @brief The concrete implementation of get_character() for UTF-32 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf32(std::string& buffer) - { + void fill_buffer_utf32(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_32BE || m_encode_type == utf_encode_t::UTF_32LE); int shift_bits[4] {0, 0, 0, 0}; - if (m_encode_type == utf_encode_t::UTF_32BE) - { + if (m_encode_type == utf_encode_t::UTF_32BE) { shift_bits[0] = 24; shift_bits[1] = 16; shift_bits[2] = 8; @@ -5882,19 +5166,18 @@ class iterator_input_adapter< shift_bits[3] = 24; } - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (m_current != m_end) - { - char32_t utf32 = char32_t(*m_current++ << shift_bits[0]); - utf32 |= char32_t(*m_current++ << shift_bits[1]); - utf32 |= char32_t(*m_current++ << shift_bits[2]); - utf32 |= char32_t(*m_current++ << shift_bits[3]); + while (m_current != m_end) { + char32_t utf32 = static_cast(*m_current++ << shift_bits[0]); + utf32 |= static_cast(*m_current++ << shift_bits[1]); + utf32 |= static_cast(*m_current++ << shift_bits[2]); + utf32 |= static_cast(*m_current++ << shift_bits[3]); - utf8_encoding::from_utf32(utf32, utf8_buffer, utf8_buf_size); + utf8::from_utf32(utf32, utf8_buffer, utf8_buf_size); - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } @@ -5914,8 +5197,7 @@ class iterator_input_adapter< template class iterator_input_adapter< IterType, - enable_if_t::value_type>, char8_t>::value>> -{ + enable_if_t::value_type>, char8_t>::value>> { public: /// @brief Construct a new iterator_input_adapter object. iterator_input_adapter() = default; @@ -5927,8 +5209,7 @@ class iterator_input_adapter< iterator_input_adapter(IterType begin, IterType end, utf_encode_t encode_type) noexcept : m_current(begin), m_end(end), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { // char8_t characters must be encoded in the UTF-8 format. // See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0482r6.html. FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_8); @@ -5943,55 +5224,45 @@ class iterator_input_adapter< /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { + void fill_buffer(std::string& buffer) { IterType current = m_current; - while (current != m_end) - { - char first = *current++; - - // The first byte starts with 0b0XXX'XXXX -> 1-byte character - if ((first & 0xC0) == 0x80) - { - // The first byte must not start with 0b10XX'XXXX - std::array bytes {{first}}; - throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); - } - // The first byte starts with 0b110X'XXXX -> 2-byte character - else if ((first & 0xE0) == 0xC0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + while (current != m_end) { + uint8_t first = static_cast(*current++); + uint32_t num_bytes = utf8::get_num_bytes(first); + + switch (num_bytes) { + case 2: { + std::initializer_list bytes {first, uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0b1110'XXXX -> 3-byte character - else if ((first & 0xF0) == 0xE0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 3: { + std::initializer_list bytes {first, uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0x1111'0XXX -> 4-byte character - else if ((first & 0xF8) == 0xF0) - { - std::array bytes { - {uint8_t(first), uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 4: { + std::initializer_list bytes { + first, uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; + } + case 1: + default: + break; } } - while (m_current != m_end) - { + while (m_current != m_end) { buffer.push_back(char(*m_current++)); } } @@ -6012,8 +5283,7 @@ class iterator_input_adapter< template class iterator_input_adapter< IterType, - enable_if_t::value_type>, char16_t>::value>> -{ + enable_if_t::value_type>, char16_t>::value>> { public: /// @brief Construct a new iterator_input_adapter object. iterator_input_adapter() = default; @@ -6025,8 +5295,7 @@ class iterator_input_adapter< iterator_input_adapter(IterType begin, IterType end, utf_encode_t encode_type) noexcept : m_current(begin), m_end(end), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_16BE || m_encode_type == utf_encode_t::UTF_16LE); } @@ -6039,35 +5308,32 @@ class iterator_input_adapter< /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { + void fill_buffer(std::string& buffer) { int shift_bits = (m_encode_type == utf_encode_t::UTF_16BE) ? 0 : 8; std::array encoded_buffer {{0, 0}}; std::size_t encoded_buf_size {0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (m_current != m_end || encoded_buf_size != 0) - { - while (m_current != m_end && encoded_buf_size < 2) - { + while (m_current != m_end || encoded_buf_size != 0) { + while (m_current != m_end && encoded_buf_size < 2) { char16_t tmp = *m_current++; - encoded_buffer[encoded_buf_size] = char16_t((tmp & 0x00FFu) << shift_bits); - encoded_buffer[encoded_buf_size++] |= char16_t((tmp & 0xFF00u) >> shift_bits); + encoded_buffer[encoded_buf_size++] = char16_t( + static_cast((tmp & 0x00FFu) << shift_bits) | + static_cast((tmp & 0xFF00u) >> shift_bits)); } std::size_t consumed_size = 0; - utf8_encoding::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); + utf8::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); - if (consumed_size == 1) - { + if (consumed_size == 1) { encoded_buffer[0] = encoded_buffer[1]; encoded_buffer[1] = 0; } encoded_buf_size -= consumed_size; - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } @@ -6085,8 +5351,7 @@ class iterator_input_adapter< template class iterator_input_adapter< IterType, - enable_if_t::value_type>, char32_t>::value>> -{ + enable_if_t::value_type>, char32_t>::value>> { public: /// @brief Construct a new iterator_input_adapter object. iterator_input_adapter() = default; @@ -6098,8 +5363,7 @@ class iterator_input_adapter< iterator_input_adapter(IterType begin, IterType end, utf_encode_t encode_type) noexcept : m_current(begin), m_end(end), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_32BE || m_encode_type == utf_encode_t::UTF_32LE); } @@ -6112,31 +5376,29 @@ class iterator_input_adapter< /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { + void fill_buffer(std::string& buffer) { int shift_bits[4] {0, 0, 0, 0}; - if (m_encode_type == utf_encode_t::UTF_32LE) - { + if (m_encode_type == utf_encode_t::UTF_32LE) { shift_bits[0] = 24; shift_bits[1] = 8; shift_bits[2] = 8; shift_bits[3] = 24; } - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (m_current != m_end) - { + while (m_current != m_end) { char32_t tmp = *m_current++; - char32_t utf32 = char32_t((tmp & 0xFF000000u) >> shift_bits[0]); - utf32 |= char32_t((tmp & 0x00FF0000u) >> shift_bits[1]); - utf32 |= char32_t((tmp & 0x0000FF00u) << shift_bits[2]); - utf32 |= char32_t((tmp & 0x000000FFu) << shift_bits[3]); + char32_t utf32 = char32_t( + static_cast((tmp & 0xFF000000u) >> shift_bits[0]) | + static_cast((tmp & 0x00FF0000u) >> shift_bits[1]) | + static_cast((tmp & 0x0000FF00u) << shift_bits[2]) | + static_cast((tmp & 0x000000FFu) << shift_bits[3])); - utf8_encoding::from_utf32(utf32, utf8_buffer, utf8_buf_size); + utf8::from_utf32(utf32, utf8_buffer, utf8_buf_size); - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } @@ -6150,8 +5412,7 @@ class iterator_input_adapter< }; /// @brief An input adapter for C-style file handles. -class file_input_adapter -{ +class file_input_adapter { public: /// @brief Construct a new file_input_adapter object. file_input_adapter() = default; @@ -6164,8 +5425,7 @@ class file_input_adapter /// @param encode_type The encoding type for this input adapter. explicit file_input_adapter(std::FILE* file, utf_encode_t encode_type) noexcept : m_file(file), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { } // allow only move construct/assignment @@ -6177,10 +5437,8 @@ class file_input_adapter /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { - switch (m_encode_type) - { + void fill_buffer(std::string& buffer) { + switch (m_encode_type) { case utf_encode_t::UTF_8: fill_buffer_utf8(buffer); break; @@ -6198,73 +5456,61 @@ class file_input_adapter private: /// @brief The concrete implementation of get_character() for UTF-8 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf8(std::string& buffer) - { + void fill_buffer_utf8(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_8); char tmp_buf[256] {}; std::size_t read_size = 0; - while ((read_size = std::fread(&tmp_buf[0], sizeof(char), sizeof(tmp_buf) / sizeof(tmp_buf[0]), m_file)) > 0) - { + while ((read_size = std::fread(&tmp_buf[0], sizeof(char), sizeof(tmp_buf) / sizeof(tmp_buf[0]), m_file)) > 0) { buffer.append(tmp_buf, read_size); } auto current = buffer.begin(); auto end = buffer.end(); - while (current != end) - { - char first = *current++; - - // The first byte starts with 0b0XXX'XXXX -> 1-byte character - if ((first & 0xC0) == 0x80) - { - // The first byte must not start with 0b10XX'XXXX - std::array bytes {{first}}; - throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); - } - // The first byte starts with 0b110X'XXXX -> 2-byte character - else if ((first & 0xE0) == 0xC0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + while (current != end) { + uint8_t first = static_cast(*current++); + uint32_t num_bytes = utf8::get_num_bytes(first); + + switch (num_bytes) { + case 2: { + std::initializer_list bytes {first, uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0b1110'XXXX -> 3-byte character - else if ((first & 0xF0) == 0xE0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 3: { + std::initializer_list bytes {first, uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0x1111'0XXX -> 4-byte character - else if ((first & 0xF8) == 0xF0) - { - std::array bytes { - {uint8_t(first), uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 4: { + std::initializer_list bytes { + first, uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; + } + case 1: + default: + break; } } } /// @brief The concrete implementation of get_character() for UTF-16 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf16(std::string& buffer) - { + void fill_buffer_utf16(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_16BE || m_encode_type == utf_encode_t::UTF_16LE); int shift_bits[2] {0, 0}; - if (m_encode_type == utf_encode_t::UTF_16BE) - { + if (m_encode_type == utf_encode_t::UTF_16BE) { shift_bits[0] = 8; } else // m_encode_type == utf_encode_t::UTF_16LE @@ -6275,40 +5521,36 @@ class file_input_adapter char chars[2] = {0, 0}; std::array encoded_buffer {{0, 0}}; std::size_t encoded_buf_size {0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (std::feof(m_file) == 0) - { - while (encoded_buf_size < 2 && std::fread(&chars[0], sizeof(char), 2, m_file) == 2) - { - encoded_buffer[encoded_buf_size] = char16_t(uint8_t(chars[0]) << shift_bits[0]); - encoded_buffer[encoded_buf_size++] |= char16_t(uint8_t(chars[1]) << shift_bits[1]); + while (std::feof(m_file) == 0) { + while (encoded_buf_size < 2 && std::fread(&chars[0], sizeof(char), 2, m_file) == 2) { + encoded_buffer[encoded_buf_size++] = char16_t( + static_cast(uint8_t(chars[0]) << shift_bits[0]) | + static_cast(uint8_t(chars[1]) << shift_bits[1])); } std::size_t consumed_size = 0; - utf8_encoding::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); + utf8::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); - if (consumed_size == 1) - { + if (consumed_size == 1) { encoded_buffer[0] = encoded_buffer[1]; encoded_buffer[1] = 0; } encoded_buf_size -= consumed_size; - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } /// @brief The concrete implementation of get_character() for UTF-32 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf32(std::string& buffer) - { + void fill_buffer_utf32(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_32BE || m_encode_type == utf_encode_t::UTF_32LE); int shift_bits[4] {0, 0, 0, 0}; - if (m_encode_type == utf_encode_t::UTF_32BE) - { + if (m_encode_type == utf_encode_t::UTF_32BE) { shift_bits[0] = 24; shift_bits[1] = 16; shift_bits[2] = 8; @@ -6321,25 +5563,24 @@ class file_input_adapter } char chars[4] = {0, 0, 0, 0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - while (std::feof(m_file) == 0) - { + while (std::feof(m_file) == 0) { std::size_t size = std::fread(&chars[0], sizeof(char), 4, m_file); - if (size != 4) - { + if (size != 4) { return; } - char32_t utf32 = char32_t(uint8_t(chars[0]) << shift_bits[0]); - utf32 |= char32_t(uint8_t(chars[1]) << shift_bits[1]); - utf32 |= char32_t(uint8_t(chars[2]) << shift_bits[2]); - utf32 |= char32_t(uint8_t(chars[3]) << shift_bits[3]); + char32_t utf32 = char32_t( + static_cast(uint8_t(chars[0]) << shift_bits[0]) | + static_cast(uint8_t(chars[1]) << shift_bits[1]) | + static_cast(uint8_t(chars[2]) << shift_bits[2]) | + static_cast(uint8_t(chars[3]) << shift_bits[3])); - utf8_encoding::from_utf32(utf32, utf8_buffer, utf8_buf_size); + utf8::from_utf32(utf32, utf8_buffer, utf8_buf_size); - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } } @@ -6351,8 +5592,7 @@ class file_input_adapter }; /// @brief An input adapter for streams -class stream_input_adapter -{ +class stream_input_adapter { public: /// @brief Construct a new stream_input_adapter object. stream_input_adapter() = default; @@ -6361,8 +5601,7 @@ class stream_input_adapter /// @param is A reference to the target input stream. explicit stream_input_adapter(std::istream& is, utf_encode_t encode_type) noexcept : m_istream(&is), - m_encode_type(encode_type) - { + m_encode_type(encode_type) { } // allow only move construct/assignment @@ -6374,10 +5613,8 @@ class stream_input_adapter /// @brief Get a character at the current position and move forward. /// @return std::char_traits::int_type A character or EOF. - void fill_buffer(std::string& buffer) - { - switch (m_encode_type) - { + void fill_buffer(std::string& buffer) { + switch (m_encode_type) { case utf_encode_t::UTF_8: fill_buffer_utf8(buffer); break; @@ -6395,74 +5632,62 @@ class stream_input_adapter private: /// @brief The concrete implementation of get_character() for UTF-8 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf8(std::string& buffer) - { + void fill_buffer_utf8(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_8); char tmp_buf[256] {}; - do - { + do { m_istream->read(&tmp_buf[0], 256); - std::size_t read_size = m_istream->gcount(); + std::size_t read_size = static_cast(m_istream->gcount()); buffer.append(tmp_buf, read_size); } while (!m_istream->eof()); auto current = buffer.begin(); auto end = buffer.end(); - while (current != end) - { - char first = *current++; - - // The first byte starts with 0b0XXX'XXXX -> 1-byte character - if ((first & 0xC0) == 0x80) - { - // The first byte must not start with 0b10XX'XXXX - std::array bytes {{first}}; - throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); - } - // The first byte starts with 0b110X'XXXX -> 2-byte character - else if ((first & 0xE0) == 0xC0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + while (current != end) { + uint8_t first = static_cast(*current++); + uint32_t num_bytes = utf8::get_num_bytes(first); + + switch (num_bytes) { + case 2: { + std::initializer_list bytes {first, uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0b1110'XXXX -> 3-byte character - else if ((first & 0xF0) == 0xE0) - { - std::array bytes {{uint8_t(first), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 3: { + std::initializer_list bytes {first, uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; } - // The first byte starts with 0x1111'0XXX -> 4-byte character - else if ((first & 0xF8) == 0xF0) - { - std::array bytes { - {uint8_t(first), uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}}; - bool is_valid = utf8_encoding::validate(bytes); - if (!is_valid) - { + case 4: { + std::initializer_list bytes { + first, uint8_t(*current++), uint8_t(*current++), uint8_t(*current++)}; + bool is_valid = utf8::validate(bytes); + if (!is_valid) { throw fkyaml::invalid_encoding("Invalid UTF-8 encoding.", bytes); } + break; + } + case 1: + default: + break; } } } /// @brief The concrete implementation of get_character() for UTF-16 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf16(std::string& buffer) - { + void fill_buffer_utf16(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_16BE || m_encode_type == utf_encode_t::UTF_16LE); int shift_bits[2] {0, 0}; - if (m_encode_type == utf_encode_t::UTF_16BE) - { + if (m_encode_type == utf_encode_t::UTF_16BE) { shift_bits[0] = 8; } else // m_encode_type == utf_encode_t::UTF_16LE @@ -6473,47 +5698,42 @@ class stream_input_adapter char chars[2] = {0, 0}; std::array encoded_buffer {{0, 0}}; std::size_t encoded_buf_size {0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - do - { - while (encoded_buf_size < 2) - { + do { + while (encoded_buf_size < 2) { m_istream->read(&chars[0], 2); std::streamsize size = m_istream->gcount(); - if (size != 2) - { + if (size != 2) { break; } - encoded_buffer[encoded_buf_size] = char16_t(uint8_t(chars[0]) << shift_bits[0]); - encoded_buffer[encoded_buf_size++] |= char16_t(uint8_t(chars[1]) << shift_bits[1]); + encoded_buffer[encoded_buf_size++] = char16_t( + static_cast(uint8_t(chars[0]) << shift_bits[0]) | + static_cast(uint8_t(chars[1]) << shift_bits[1])); }; std::size_t consumed_size = 0; - utf8_encoding::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); + utf8::from_utf16(encoded_buffer, utf8_buffer, consumed_size, utf8_buf_size); - if (consumed_size == 1) - { + if (consumed_size == 1) { encoded_buffer[0] = encoded_buffer[1]; encoded_buffer[1] = 0; } encoded_buf_size -= consumed_size; - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } while (!m_istream->eof()); } /// @brief The concrete implementation of get_character() for UTF-32 encoded inputs. /// @return A UTF-8 encoded byte at the current position, or EOF. - void fill_buffer_utf32(std::string& buffer) - { + void fill_buffer_utf32(std::string& buffer) { FK_YAML_ASSERT(m_encode_type == utf_encode_t::UTF_32BE || m_encode_type == utf_encode_t::UTF_32LE); int shift_bits[4] {0, 0, 0, 0}; - if (m_encode_type == utf_encode_t::UTF_32BE) - { + if (m_encode_type == utf_encode_t::UTF_32BE) { shift_bits[0] = 24; shift_bits[1] = 16; shift_bits[2] = 8; @@ -6526,26 +5746,25 @@ class stream_input_adapter } char chars[4] = {0, 0, 0, 0}; - std::array utf8_buffer {{0, 0, 0, 0}}; + std::array utf8_buffer {{0, 0, 0, 0}}; std::size_t utf8_buf_size {0}; - do - { + do { m_istream->read(&chars[0], 4); std::streamsize size = m_istream->gcount(); - if (size != 4) - { + if (size != 4) { return; } - char32_t utf32 = char32_t(uint8_t(chars[0]) << shift_bits[0]); - utf32 |= char32_t(uint8_t(chars[1]) << shift_bits[1]); - utf32 |= char32_t(uint8_t(chars[2]) << shift_bits[2]); - utf32 |= char32_t(uint8_t(chars[3]) << shift_bits[3]); + char32_t utf32 = char32_t( + static_cast(uint8_t(chars[0]) << shift_bits[0]) | + static_cast(uint8_t(chars[1]) << shift_bits[1]) | + static_cast(uint8_t(chars[2]) << shift_bits[2]) | + static_cast(uint8_t(chars[3]) << shift_bits[3])); - utf8_encoding::from_utf32(utf32, utf8_buffer, utf8_buf_size); + utf8::from_utf32(utf32, utf8_buffer, utf8_buf_size); - buffer.append(utf8_buffer.data(), utf8_buf_size); + buffer.append(reinterpret_cast(utf8_buffer.data()), utf8_buf_size); } while (!m_istream->eof()); } @@ -6566,8 +5785,7 @@ class stream_input_adapter /// @param end The end of iterators. /// @return iterator_input_adapter An iterator_input_adapter object for the target iterator type. template ())))> -inline iterator_input_adapter input_adapter(ItrType begin, ItrType end) -{ +inline iterator_input_adapter input_adapter(ItrType begin, ItrType end) { utf_encode_t encode_type = detect_encoding_and_skip_bom(begin, end); return iterator_input_adapter(begin, end, encode_type); } @@ -6577,14 +5795,12 @@ inline iterator_input_adapter input_adapter(ItrType begin, ItrType end) /// @tparam N A size of an array. /// @return decltype(input_adapter(array, array + N)) An iterator_input_adapter object for the target array. template -inline auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + (N - 1))) -{ +inline auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + (N - 1))) { return input_adapter(array, array + (N - 1)); } /// @brief A namespace to implement container_input_adapter_factory for internal use. -namespace input_adapter_factory -{ +namespace input_adapter_factory { using std::begin; using std::end; @@ -6593,16 +5809,13 @@ using std::end; /// @tparam ContainerType A container type. /// @tparam typename N/A template -struct container_input_adapter_factory -{ -}; +struct container_input_adapter_factory {}; /// @brief A partial specialization of container_input_adapter_factory if begin()/end() are available for ContainerType. /// @tparam ContainerType A container type. template struct container_input_adapter_factory< - ContainerType, void_t()), end(std::declval()))>> -{ + ContainerType, void_t()), end(std::declval()))>> { /// A type for resulting input adapter object. using adapter_type = decltype(input_adapter(begin(std::declval()), end(std::declval()))); @@ -6610,8 +5823,7 @@ struct container_input_adapter_factory< /// @brief A factory method of input adapter objects for the target container objects. /// @param container A container-like input object. /// @return adapter_type An iterator_input_adapter object. - static adapter_type create(const ContainerType& container) - { + static adapter_type create(const ContainerType& container) { return input_adapter(begin(container), end(container)); } }; @@ -6624,18 +5836,15 @@ struct container_input_adapter_factory< /// @return input_adapter_factory::container_input_adapter_factory::adapter_type template inline typename input_adapter_factory::container_input_adapter_factory::adapter_type input_adapter( - ContainerType&& container) -{ + ContainerType&& container) { return input_adapter_factory::container_input_adapter_factory::create(container); } /// @brief A factory method for file_input_adapter objects with C-style file handles. /// @param file A file handle. /// @return file_input_adapter A file_input_adapter object. -inline file_input_adapter input_adapter(std::FILE* file) -{ - if (!file) - { +inline file_input_adapter input_adapter(std::FILE* file) { + if (!file) { throw fkyaml::exception("Invalid FILE object pointer."); } utf_encode_t encode_type = detect_encoding_and_skip_bom(file); @@ -6645,22 +5854,19 @@ inline file_input_adapter input_adapter(std::FILE* file) /// @brief A factory method for stream_input_adapter objects with std::istream objects. /// @param stream An input stream. /// @return stream_input_adapter A stream_input_adapter object. -inline stream_input_adapter input_adapter(std::istream& stream) noexcept -{ +inline stream_input_adapter input_adapter(std::istream& stream) noexcept { utf_encode_t encode_type = detect_encoding_and_skip_bom(stream); return stream_input_adapter(stream, encode_type); } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_INPUT_INPUT_ADAPTER_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -6681,28 +5887,18 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A tag which tells Iterator will contain sequence value iterator. -struct sequence_iterator_tag -{ -}; +struct sequence_iterator_tag {}; /// @brief A tag which tells Iterator will contain mapping value iterator. -struct mapping_iterator_tag -{ -}; +struct mapping_iterator_tag {}; /// @brief The template definitions of type informations used in @ref Iterator class /// @tparam ValueType The type of iterated elements. template -struct iterator_traits -{ +struct iterator_traits { /// A type of iterated elements. using value_type = ValueType; /// A type to represent difference between iterators. @@ -6718,8 +5914,7 @@ struct iterator_traits /// @brief A specialization of @ref iterator_traits for constant value types. /// @tparam ValueType The type of iterated elements. template -struct iterator_traits -{ +struct iterator_traits { /// A type of iterated elements. using value_type = ValueType; /// A type to represent difference between iterators. @@ -6733,8 +5928,7 @@ struct iterator_traits }; /// @brief Definitions of iterator types for iterators internally held. -enum class iterator_t -{ +enum class iterator_t { SEQUENCE, //!< sequence iterator type. MAPPING, //!< mapping iterator type. }; @@ -6742,8 +5936,7 @@ enum class iterator_t /// @brief A class which holds iterators either of sequence or mapping type /// @tparam ValueType The type of iterated elements. template -class iterator -{ +class iterator { public: /// A type for iterator traits of instantiated @Iterator template class. using ItrTraitsType = iterator_traits; @@ -6768,8 +5961,7 @@ class iterator static_assert(is_basic_node::value, "Iterator only accepts basic_node<...>"); /// @brief The actual storage for iterators internally held in @ref Iterator. - struct iterator_holder - { + struct iterator_holder { /// A sequence iterator object. typename NonConstValueType::sequence_type::iterator sequence_iterator {}; /// A mapping iterator object. @@ -6780,26 +5972,22 @@ class iterator /// @brief Construct a new iterator object with sequence iterator object. /// @param[in] itr An sequence iterator object. iterator(sequence_iterator_tag /* unused */, const typename ValueType::sequence_type::iterator& itr) noexcept - : m_inner_iterator_type(iterator_t::SEQUENCE) - { + : m_inner_iterator_type(iterator_t::SEQUENCE) { m_iterator_holder.sequence_iterator = itr; } /// @brief Construct a new iterator object with mapping iterator object. /// @param[in] itr An mapping iterator object. iterator(mapping_iterator_tag /* unused */, const typename ValueType::mapping_type::iterator& itr) noexcept - : m_inner_iterator_type(iterator_t::MAPPING) - { + : m_inner_iterator_type(iterator_t::MAPPING) { m_iterator_holder.mapping_iterator = itr; } /// @brief Copy constructor of the iterator class. /// @param other An iterator object to be copied with. iterator(const iterator& other) noexcept - : m_inner_iterator_type(other.m_inner_iterator_type) - { - switch (m_inner_iterator_type) - { + : m_inner_iterator_type(other.m_inner_iterator_type) { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: m_iterator_holder.sequence_iterator = other.m_iterator_holder.sequence_iterator; break; @@ -6812,10 +6000,8 @@ class iterator /// @brief Move constructor of the iterator class. /// @param other An iterator object to be moved from. iterator(iterator&& other) noexcept - : m_inner_iterator_type(other.m_inner_iterator_type) - { - switch (m_inner_iterator_type) - { + : m_inner_iterator_type(other.m_inner_iterator_type) { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: m_iterator_holder.sequence_iterator = std::move(other.m_iterator_holder.sequence_iterator); break; @@ -6832,16 +6018,13 @@ class iterator /// @brief A copy assignment operator of the iterator class. /// @param rhs An iterator object to be copied with. /// @return iterator& Reference to this iterator object. - iterator& operator=(const iterator& rhs) noexcept - { - if (&rhs == this) - { + iterator& operator=(const iterator& rhs) noexcept { + if (&rhs == this) { return *this; } m_inner_iterator_type = rhs.m_inner_iterator_type; - switch (m_inner_iterator_type) - { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: m_iterator_holder.sequence_iterator = rhs.m_iterator_holder.sequence_iterator; break; @@ -6856,16 +6039,13 @@ class iterator /// @brief A move assignment operator of the iterator class. /// @param rhs An iterator object to be moved from. /// @return iterator& Reference to this iterator object. - iterator& operator=(iterator&& rhs) noexcept - { - if (&rhs == this) - { + iterator& operator=(iterator&& rhs) noexcept { + if (&rhs == this) { return *this; } m_inner_iterator_type = rhs.m_inner_iterator_type; - switch (m_inner_iterator_type) - { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: m_iterator_holder.sequence_iterator = std::move(rhs.m_iterator_holder.sequence_iterator); break; @@ -6879,10 +6059,8 @@ class iterator /// @brief An arrow operator of the iterator class. /// @return pointer A pointer to the BasicNodeType object internally referenced by the actual iterator object. - pointer operator->() noexcept - { - if (m_inner_iterator_type == iterator_t::SEQUENCE) - { + pointer operator->() noexcept { + if (m_inner_iterator_type == iterator_t::SEQUENCE) { return &(*(m_iterator_holder.sequence_iterator)); } @@ -6892,10 +6070,8 @@ class iterator /// @brief A dereference operator of the iterator class. /// @return reference Reference to the Node object internally referenced by the actual iterator object. - reference operator*() noexcept - { - if (m_inner_iterator_type == iterator_t::SEQUENCE) - { + reference operator*() noexcept { + if (m_inner_iterator_type == iterator_t::SEQUENCE) { return *(m_iterator_holder.sequence_iterator); } @@ -6906,10 +6082,8 @@ class iterator /// @brief A compound assignment operator by sum of the Iterator class. /// @param i The difference from this Iterator object with which it moves forward. /// @return Iterator& Reference to this Iterator object. - iterator& operator+=(difference_type i) noexcept - { - switch (m_inner_iterator_type) - { + iterator& operator+=(difference_type i) noexcept { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: std::advance(m_iterator_holder.sequence_iterator, i); break; @@ -6923,8 +6097,7 @@ class iterator /// @brief A plus operator of the iterator class. /// @param i The difference from this iterator object. /// @return iterator An iterator object which has been added @a i. - iterator operator+(difference_type i) const noexcept - { + iterator operator+(difference_type i) const noexcept { auto result = *this; result += i; return result; @@ -6932,10 +6105,8 @@ class iterator /// @brief An pre-increment operator of the iterator class. /// @return iterator& Reference to this iterator object. - iterator& operator++() noexcept - { - switch (m_inner_iterator_type) - { + iterator& operator++() noexcept { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: std::advance(m_iterator_holder.sequence_iterator, 1); break; @@ -6948,8 +6119,7 @@ class iterator /// @brief A post-increment opretor of the iterator class. /// @return iterator An iterator object which has been incremented. - iterator operator++(int) & noexcept - { + iterator operator++(int) & noexcept { auto result = *this; ++(*this); return result; @@ -6958,16 +6128,14 @@ class iterator /// @brief A compound assignment operator by difference of the iterator class. /// @param i The difference from this iterator object with which it moves backward. /// @return iterator& Reference to this iterator object. - iterator& operator-=(difference_type i) noexcept - { + iterator& operator-=(difference_type i) noexcept { return operator+=(-i); } /// @brief A minus operator of the iterator class. /// @param i The difference from this iterator object. /// @return iterator An iterator object from which has been subtracted @ i. - iterator operator-(difference_type i) noexcept - { + iterator operator-(difference_type i) noexcept { auto result = *this; result -= i; return result; @@ -6975,10 +6143,8 @@ class iterator /// @brief A pre-decrement operator of the iterator class. /// @return iterator& Reference to this iterator object. - iterator& operator--() noexcept - { - switch (m_inner_iterator_type) - { + iterator& operator--() noexcept { + switch (m_inner_iterator_type) { case iterator_t::SEQUENCE: std::advance(m_iterator_holder.sequence_iterator, -1); break; @@ -6991,8 +6157,7 @@ class iterator /// @brief A post-decrement operator of the iterator class /// @return iterator An iterator object which has been decremented. - iterator operator--(int) & noexcept - { + iterator operator--(int) & noexcept { auto result = *this; --(*this); return result; @@ -7002,15 +6167,12 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is equal to the other. /// @return false This iterator object is not equal to the other. - bool operator==(const iterator& rhs) const - { - if (m_inner_iterator_type != rhs.m_inner_iterator_type) - { + bool operator==(const iterator& rhs) const { + if (m_inner_iterator_type != rhs.m_inner_iterator_type) { throw fkyaml::exception("Cannot compare iterators of different container types."); } - if (m_inner_iterator_type == iterator_t::SEQUENCE) - { + if (m_inner_iterator_type == iterator_t::SEQUENCE) { return (m_iterator_holder.sequence_iterator == rhs.m_iterator_holder.sequence_iterator); } @@ -7022,8 +6184,7 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is not equal to the other. /// @return false This iterator object is equal to the other. - bool operator!=(const iterator& rhs) const - { + bool operator!=(const iterator& rhs) const { return !operator==(rhs); } @@ -7031,15 +6192,12 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is less than the other. /// @return false This iterator object is not less than the other. - bool operator<(const iterator& rhs) const - { - if (m_inner_iterator_type != rhs.m_inner_iterator_type) - { + bool operator<(const iterator& rhs) const { + if (m_inner_iterator_type != rhs.m_inner_iterator_type) { throw fkyaml::exception("Cannot compare iterators of different container types."); } - if (m_inner_iterator_type == iterator_t::MAPPING) - { + if (m_inner_iterator_type == iterator_t::MAPPING) { throw fkyaml::exception("Cannot compare order of iterators of the mapping container type"); } @@ -7050,8 +6208,7 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is either less than or equal to the other. /// @return false This iterator object is neither less than nor equal to the other. - bool operator<=(const iterator& rhs) const - { + bool operator<=(const iterator& rhs) const { return !rhs.operator<(*this); } @@ -7059,8 +6216,7 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is greater than the other. /// @return false This iterator object is not greater than the other. - bool operator>(const iterator& rhs) const - { + bool operator>(const iterator& rhs) const { return !operator<=(rhs); } @@ -7068,25 +6224,21 @@ class iterator /// @param rhs An iterator object to be compared with this iterator object. /// @return true This iterator object is either greater than or equal to the other. /// @return false This iterator object is neither greater than nor equal to the other. - bool operator>=(const iterator& rhs) const - { + bool operator>=(const iterator& rhs) const { return !operator<(rhs); } public: /// @brief Get the type of the internal iterator implementation. /// @return iterator_t The type of the internal iterator implementation. - iterator_t type() const noexcept - { + iterator_t type() const noexcept { return m_inner_iterator_type; } /// @brief Get the key string of the YAML mapping node for the current iterator. /// @return const std::string& The key string of the YAML mapping node for the current iterator. - const typename ValueType::mapping_type::key_type& key() const - { - if (m_inner_iterator_type == iterator_t::SEQUENCE) - { + const typename ValueType::mapping_type::key_type& key() const { + if (m_inner_iterator_type == iterator_t::SEQUENCE) { throw fkyaml::exception("Cannot retrieve key from non-mapping iterators."); } @@ -7095,8 +6247,7 @@ class iterator /// @brief Get the reference of the YAML node for the current iterator. /// @return reference A reference to the YAML node for the current iterator. - reference value() noexcept - { + reference value() noexcept { return operator*(); } @@ -7107,9 +6258,7 @@ class iterator mutable iterator_holder m_iterator_holder {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_ITERATOR_HPP_ */ @@ -7122,7 +6271,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -7138,37 +6287,28 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN -enum class anchor_status_t -{ +enum class anchor_status_t { NONE, ANCHOR, ALIAS, }; -struct node_property -{ +struct node_property { std::string tag {}; anchor_status_t anchor_status {anchor_status_t::NONE}; std::string anchor {}; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_NODE_PROPERTY_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -7190,19 +6330,13 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A temporal storage for basic_node class objects. /// @note This class makes it easier to handle lvalue basic_node objects in basic_node ctor with std::initializer_list. /// @tparam BasicNodeType A basic_node template instance type. template -class node_ref_storage -{ +class node_ref_storage { static_assert(is_basic_node::value, "node_ref_storage only accepts basic_node<...>"); using node_type = BasicNodeType; @@ -7211,22 +6345,19 @@ class node_ref_storage /// @brief Construct a new node ref storage object with an rvalue basic_node object. /// @param n An rvalue basic_node object. explicit node_ref_storage(node_type&& n) noexcept(std::is_nothrow_move_constructible::value) - : owned_value(std::move(n)) - { + : owned_value(std::move(n)) { } /// @brief Construct a new node ref storage object with an lvalue basic_node object. /// @param n An lvalue basic_node object. explicit node_ref_storage(const node_type& n) noexcept - : value_ref(&n) - { + : value_ref(&n) { } /// @brief Construct a new node ref storage object with a std::initializer_list object. /// @param init A std::initializer_list object. node_ref_storage(std::initializer_list init) - : owned_value(init) - { + : owned_value(init) { } /// @brief Construct a new node ref storage object with variadic template arguments @@ -7234,8 +6365,7 @@ class node_ref_storage /// @param args Arguments to construct a basic_node object. template ::value, int> = 0> node_ref_storage(Args&&... args) - : owned_value(std::forward(args)...) - { + : owned_value(std::forward(args)...) { } // allow only move construct/assignment @@ -7247,15 +6377,13 @@ class node_ref_storage public: /// @brief An arrow operator for node_ref_storage objects. /// @return const node_type* A constant pointer to a basic_node object. - const node_type* operator->() const noexcept - { + const node_type* operator->() const noexcept { return value_ref ? value_ref : &owned_value; } /// @brief Releases a basic_node object internally held. /// @return node_type A basic_node object internally held. - node_type release() const noexcept - { + node_type release() const noexcept { return value_ref ? *value_ref : std::move(owned_value); } @@ -7266,16 +6394,14 @@ class node_ref_storage const node_type* value_ref = nullptr; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_NODE_REF_STORAGE_HPP_ */ // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -7295,7 +6421,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -7319,12 +6445,7 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief Converts a ValueType object to a string YAML token. /// @tparam ValueType A source value type. @@ -7338,8 +6459,7 @@ inline void to_string(ValueType v, std::basic_string& s) noexcept; /// @param s A resulting string YAML token. /// @param (unused) nullptr template <> -inline void to_string(std::nullptr_t /*unused*/, std::string& s) noexcept -{ +inline void to_string(std::nullptr_t /*unused*/, std::string& s) noexcept { s = "null"; } @@ -7347,8 +6467,7 @@ inline void to_string(std::nullptr_t /*unused*/, std::string& s) noexcept /// @param s A resulting string YAML token. /// @param b A boolean source value. template <> -inline void to_string(bool b, std::string& s) noexcept -{ +inline void to_string(bool b, std::string& s) noexcept { s = b ? "true" : "false"; } @@ -7357,8 +6476,7 @@ inline void to_string(bool b, std::string& s) noexcept /// @param s A resulting string YAML token. /// @param i An integer source value. template -inline enable_if_t::value> to_string(IntegerType i, std::string& s) noexcept -{ +inline enable_if_t::value> to_string(IntegerType i, std::string& s) noexcept { s = std::to_string(i); } @@ -7367,22 +6485,17 @@ inline enable_if_t::value> to_string(IntegerTy /// @param s A resulting string YAML token. /// @param f A floating point number source value. template -inline enable_if_t::value> to_string(FloatType f, std::string& s) noexcept -{ - if (std::isnan(f)) - { +inline enable_if_t::value> to_string(FloatType f, std::string& s) noexcept { + if (std::isnan(f)) { s = ".nan"; return; } - if (std::isinf(f)) - { - if (f == std::numeric_limits::infinity()) - { + if (std::isinf(f)) { + if (f == std::numeric_limits::infinity()) { s = ".inf"; } - else - { + else { s = "-.inf"; } return; @@ -7393,9 +6506,7 @@ inline enable_if_t::value> to_string(FloatType s = oss.str(); } -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* TO__string_HPP_ */ @@ -7412,17 +6523,12 @@ FK_YAML_NAMESPACE_END // #include -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /// @brief A basic implementation of serialization feature for YAML nodes. /// @tparam BasicNodeType A BasicNode template class instantiation. template -class basic_serializer -{ +class basic_serializer { static_assert(detail::is_basic_node::value, "basic_serializer only accepts basic_node<...>"); public: @@ -7432,8 +6538,7 @@ class basic_serializer /// @brief Serialize the given Node value. /// @param node A Node object to be serialized. /// @return std::string A serialization result of the given Node value. - std::string serialize(const BasicNodeType& node) - { + std::string serialize(const BasicNodeType& node) { std::string str {}; serialize_directives(node, str); serialize_node(node, 0, str); @@ -7444,20 +6549,16 @@ class basic_serializer /// @brief Serialize the directives if any is applied to the node. /// @param node The targe node. /// @param str A string to hold serialization result. - void serialize_directives(const BasicNodeType& node, std::string& str) - { - if (!node.mp_directive_set) - { + void serialize_directives(const BasicNodeType& node, std::string& str) { + if (!node.mp_directive_set) { return; } const auto& directives = node.mp_directive_set; - if (directives->is_version_specified) - { + if (directives->is_version_specified) { str += "%YAML "; - switch (directives->version) - { + switch (directives->version) { case yaml_version_t::VER_1_1: str += "1.1\n"; break; @@ -7467,24 +6568,20 @@ class basic_serializer } } - if (!directives->primary_handle_prefix.empty()) - { + if (!directives->primary_handle_prefix.empty()) { str += "%TAG ! "; str += directives->primary_handle_prefix; str += "\n"; } - if (!directives->secondary_handle_prefix.empty()) - { + if (!directives->secondary_handle_prefix.empty()) { str += "%TAG !! "; str += directives->secondary_handle_prefix; str += "\n"; } - if (!directives->named_handle_map.empty()) - { - for (const auto& itr : directives->named_handle_map) - { + if (!directives->named_handle_map.empty()) { + for (const auto& itr : directives->named_handle_map) { str += "%TAG "; str += itr.first; str += " "; @@ -7500,22 +6597,15 @@ class basic_serializer /// @param node A Node object to be serialized. /// @param cur_indent The current indent width /// @param str A string to hold serialization result. - void serialize_node(const BasicNodeType& node, const uint32_t cur_indent, std::string& str) - { - switch (node.type()) - { + void serialize_node(const BasicNodeType& node, const uint32_t cur_indent, std::string& str) { + switch (node.type()) { case node_t::SEQUENCE: - for (const auto& seq_item : node) - { - if (cur_indent > 0) - { - insert_indentation(cur_indent, str); - } + for (const auto& seq_item : node) { + insert_indentation(cur_indent, str); str += "-"; bool is_appended = try_append_alias(seq_item, true, str); - if (is_appended) - { + if (is_appended) { str += "\n"; continue; } @@ -7524,44 +6614,45 @@ class basic_serializer try_append_tag(seq_item, true, str); bool is_scalar = seq_item.is_scalar(); - if (is_scalar) - { + if (is_scalar) { str += " "; serialize_node(seq_item, cur_indent, str); str += "\n"; } - else - { + else { str += "\n"; serialize_node(seq_item, cur_indent + 2, str); } } break; case node_t::MAPPING: - for (auto itr = node.begin(); itr != node.end(); ++itr) - { - if (cur_indent > 0) - { - insert_indentation(cur_indent, str); - } + for (auto itr = node.begin(); itr != node.end(); ++itr) { + insert_indentation(cur_indent, str); bool is_appended = try_append_alias(itr.key(), false, str); - if (!is_appended) - { + if (!is_appended) { bool is_anchor_appended = try_append_anchor(itr.key(), false, str); bool is_tag_appended = try_append_tag(itr.key(), is_anchor_appended, str); - if (is_anchor_appended || is_tag_appended) - { + if (is_anchor_appended || is_tag_appended) { str += " "; } - serialize_node(itr.key(), cur_indent, str); + + bool is_container = !itr.key().is_scalar(); + if (is_container) { + str += "? "; + } + uint32_t indent = static_cast(get_cur_indent(str)); + serialize_node(itr.key(), indent, str); + if (is_container) { + // a newline code is already inserted in the above serialize_node() call. + insert_indentation(indent - 2, str); + } } str += ":"; is_appended = try_append_alias(*itr, true, str); - if (is_appended) - { + if (is_appended) { str += "\n"; continue; } @@ -7570,14 +6661,12 @@ class basic_serializer try_append_tag(*itr, true, str); bool is_scalar = itr->is_scalar(); - if (is_scalar) - { + if (is_scalar) { str += " "; serialize_node(*itr, cur_indent, str); str += "\n"; } - else - { + else { str += "\n"; serialize_node(*itr, cur_indent + 2, str); } @@ -7603,8 +6692,7 @@ class basic_serializer bool is_escaped = false; typename BasicNodeType::string_type str_val = get_string_node_value(node, is_escaped); - if (is_escaped) - { + if (is_escaped) { // There's no other token type with escapes than strings. // Also, escapes must be in double-quoted strings. str += '\"'; @@ -7617,16 +6705,14 @@ class basic_serializer lexical_analyzer lexer(std::move(adapter)); lexical_token_t token_type = lexer.get_next_token(); - if (token_type != lexical_token_t::STRING_VALUE) - { + if (token_type != lexical_token_t::STRING_VALUE) { // Surround a string value with double quotes to keep semantic equality. // Without them, serialized values will become non-string. (e.g., "1" -> 1) str += '\"'; str += str_val; str += '\"'; } - else - { + else { str += str_val; } break; @@ -7634,12 +6720,28 @@ class basic_serializer } } + /// @brief Get the current indentation width. + /// @param s The target string object. + /// @return The current indentation width. + std::size_t get_cur_indent(const std::string& s) const noexcept { + bool is_empty = s.empty(); + if (is_empty) { + return 0; + } + + std::size_t last_lf_pos = s.rfind('\n'); + return (last_lf_pos != std::string::npos) ? s.size() - last_lf_pos - 1 : s.size(); + } + /// @brief Insert indentation to the serialization result. - /// @param cur_indent The current indent width to be inserted. + /// @param indent The indent width to be inserted. /// @param str A string to hold serialization result. - void insert_indentation(const uint32_t cur_indent, std::string& str) const noexcept - { - str.append(cur_indent, ' '); + void insert_indentation(const uint32_t indent, std::string& str) const noexcept { + if (indent == 0) { + return; + } + + str.append(indent - get_cur_indent(str), ' '); } /// @brief Append an anchor property if it's available. Do nothing otherwise. @@ -7647,12 +6749,9 @@ class basic_serializer /// @param prepends_space Whether or not to prepend a space before an anchor property. /// @param str A string to hold serialization result. /// @return true if an anchor property has been appended, false otherwise. - bool try_append_anchor(const BasicNodeType& node, bool prepends_space, std::string& str) const - { - if (node.is_anchor()) - { - if (prepends_space) - { + bool try_append_anchor(const BasicNodeType& node, bool prepends_space, std::string& str) const { + if (node.is_anchor()) { + if (prepends_space) { str += " "; } str += "&" + node.get_anchor_name(); @@ -7666,12 +6765,9 @@ class basic_serializer /// @param prepends_space Whether or not to prepend a space before an alias property. /// @param str A string to hold serialization result. /// @return true if an alias property has been appended, false otherwise. - bool try_append_alias(const BasicNodeType& node, bool prepends_space, std::string& str) const - { - if (node.is_alias()) - { - if (prepends_space) - { + bool try_append_alias(const BasicNodeType& node, bool prepends_space, std::string& str) const { + if (node.is_alias()) { + if (prepends_space) { str += " "; } str += "*" + node.get_anchor_name(); @@ -7684,12 +6780,9 @@ class basic_serializer /// @param[in] node The target node which possibly has a tag name. /// @param[out] str A string to hold serialization result. /// @return true if a tag name has been appended, false otherwise. - bool try_append_tag(const BasicNodeType& node, bool prepends_space, std::string& str) const - { - if (node.has_tag_name()) - { - if (prepends_space) - { + bool try_append_tag(const BasicNodeType& node, bool prepends_space, std::string& str) const { + if (node.has_tag_name()) { + if (prepends_space) { str += " "; } str += node.get_tag_name(); @@ -7702,8 +6795,7 @@ class basic_serializer /// @param[in] node The target string YAML node. /// @param[out] is_escaped Whether or not the contents of an ouput string has been escaped. /// @return The (escaped) string node value. - typename BasicNodeType::string_type get_string_node_value(const BasicNodeType& node, bool& is_escaped) - { + typename BasicNodeType::string_type get_string_node_value(const BasicNodeType& node, bool& is_escaped) { FK_YAML_ASSERT(node.is_string()); using string_type = typename BasicNodeType::string_type; @@ -7714,10 +6806,8 @@ class basic_serializer string_type escaped {}; escaped.reserve(size); - for (size_t i = 0; i < size; i++) - { - switch (s[i]) - { + for (size_t i = 0; i < size; i++) { + switch (s[i]) { case 0x01: escaped += "\\u0001"; is_escaped = true; @@ -7851,29 +6941,25 @@ class basic_serializer is_escaped = true; break; default: - if (i + 1 < size && s[i] == char(0xC2u) && s[i + 1] == char(0x85u)) - { + if (i + 1 < size && s[i] == char(0xC2u) && s[i + 1] == char(0x85u)) { escaped += "\\N"; i++; is_escaped = true; break; } - if (i + 1 < size && s[i] == char(0xC2u) && s[i + 1] == char(0xA0u)) - { + if (i + 1 < size && s[i] == char(0xC2u) && s[i + 1] == char(0xA0u)) { escaped += "\\_"; i++; is_escaped = true; break; } - if (i + 2 < size && s[i] == char(0xE2u) && s[i + 1] == char(0x80u) && s[i + 2] == char(0xA8u)) - { + if (i + 2 < size && s[i] == char(0xE2u) && s[i + 1] == char(0x80u) && s[i + 2] == char(0xA8u)) { escaped += "\\L"; i += 2; is_escaped = true; break; } - if (i + 2 < size && s[i] == char(0xE2u) && s[i + 1] == char(0x80u) && s[i + 2] == char(0xA9u)) - { + if (i + 2 < size && s[i] == char(0xE2u) && s[i + 1] == char(0x80u) && s[i + 2] == char(0xA9u)) { escaped += "\\P"; i += 2; is_escaped = true; @@ -7892,9 +6978,7 @@ class basic_serializer std::string m_tmp_str_buff; }; -} // namespace detail - -FK_YAML_NAMESPACE_END +FK_YAML_DETAIL_NAMESPACE_END #endif /* FK_YAML_DETAIL_OUTPUT_SERIALIZER_HPP_ */ @@ -7905,7 +6989,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -7923,7 +7007,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -7952,15 +7036,7 @@ FK_YAML_NAMESPACE_END // #include -/** - * @namespace fkyaml - * @brief namespace for fkYAML library. - */ -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /////////////////// // from_node // @@ -7971,10 +7047,8 @@ namespace detail /// @param n A basic_node object. /// @param s A sequence node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::sequence_type& s) -{ - if (!n.is_sequence()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::sequence_type& s) { + if (!n.is_sequence()) { throw type_error("The target node value type is not sequence type.", n.type()); } s = n.template get_value_ref(); @@ -7993,17 +7067,14 @@ template < has_from_node, negation, typename BasicNodeType::sequence_type>>>::value, int> = 0> -inline void from_node(const BasicNodeType& n, std::vector& s) -{ - if (!n.is_sequence()) - { +inline void from_node(const BasicNodeType& n, std::vector& s) { + if (!n.is_sequence()) { throw type_error("The target node value is not sequence type.", n.type()); } s.reserve(n.size()); - for (const auto& elem : n) - { + for (const auto& elem : n) { s.emplace_back(elem.template get_value()); } } @@ -8013,15 +7084,12 @@ inline void from_node(const BasicNodeType& n, std::vector& /// @param n A basic_node object. /// @param m A mapping node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::mapping_type& m) -{ - if (!n.is_mapping()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::mapping_type& m) { + if (!n.is_mapping()) { throw type_error("The target node value type is not mapping type.", n.type()); } - for (auto pair : n.template get_value_ref()) - { + for (auto pair : n.template get_value_ref()) { m.emplace(pair.first, pair.second); } } @@ -8035,15 +7103,12 @@ template < negation>, has_from_node, has_from_node>::value, int> = 0> -inline void from_node(const BasicNodeType& n, std::map& m) -{ - if (!n.is_mapping()) - { +inline void from_node(const BasicNodeType& n, std::map& m) { + if (!n.is_mapping()) { throw type_error("The target node value type is not mapping type.", n.type()); } - for (auto pair : n.template get_value_ref()) - { + for (auto pair : n.template get_value_ref()) { m.emplace( pair.first.template get_value(), pair.second.template get_value()); } @@ -8054,11 +7119,9 @@ inline void from_node(const BasicNodeType& n, std::map::value, int> = 0> -inline void from_node(const BasicNodeType& n, std::nullptr_t& null) -{ +inline void from_node(const BasicNodeType& n, std::nullptr_t& null) { // to ensure the target node value type is null. - if (!n.is_null()) - { + if (!n.is_null()) { throw type_error("The target node value type is not null type.", n.type()); } null = nullptr; @@ -8069,10 +7132,8 @@ inline void from_node(const BasicNodeType& n, std::nullptr_t& null) /// @param n A basic_node object. /// @param b A boolean node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::boolean_type& b) -{ - if (!n.is_boolean()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::boolean_type& b) { + if (!n.is_boolean()) { throw type_error("The target node value type is not boolean type.", n.type()); } b = n.template get_value_ref(); @@ -8083,10 +7144,8 @@ inline void from_node(const BasicNodeType& n, typename BasicNodeType::boolean_ty /// @param n A basic_node object. /// @param i An integer node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::integer_type& i) -{ - if (!n.is_integer()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::integer_type& i) { + if (!n.is_integer()) { throw type_error("The target node value type is not integer type.", n.type()); } i = n.template get_value_ref(); @@ -8104,22 +7163,18 @@ template < is_non_bool_integral, negation>>::value, int> = 0> -inline void from_node(const BasicNodeType& n, IntegerType& i) -{ - if (!n.is_integer()) - { +inline void from_node(const BasicNodeType& n, IntegerType& i) { + if (!n.is_integer()) { throw type_error("The target node value type is not integer type.", n.type()); } // under/overflow check. using node_int_type = typename BasicNodeType::integer_type; node_int_type tmp_int = n.template get_value_ref(); - if (tmp_int < static_cast(std::numeric_limits::min())) - { + if (tmp_int < static_cast(std::numeric_limits::min())) { throw exception("Integer value underflow detected."); } - if (static_cast(std::numeric_limits::max()) < tmp_int) - { + if (static_cast(std::numeric_limits::max()) < tmp_int) { throw exception("Integer value overflow detected."); } @@ -8131,10 +7186,8 @@ inline void from_node(const BasicNodeType& n, IntegerType& i) /// @param n A basic_node object. /// @param f A float number node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::float_number_type& f) -{ - if (!n.is_float_number()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::float_number_type& f) { + if (!n.is_float_number()) { throw type_error("The target node value type is not float number type.", n.type()); } f = n.template get_value_ref(); @@ -8152,20 +7205,16 @@ template < std::is_floating_point, negation>>::value, int> = 0> -inline void from_node(const BasicNodeType& n, FloatType& f) -{ - if (!n.is_float_number()) - { +inline void from_node(const BasicNodeType& n, FloatType& f) { + if (!n.is_float_number()) { throw type_error("The target node value type is not float number type.", n.type()); } auto tmp_float = n.template get_value_ref(); - if (tmp_float < std::numeric_limits::min()) - { + if (tmp_float < std::numeric_limits::min()) { throw exception("Floating point value underflow detected."); } - if (std::numeric_limits::max() < tmp_float) - { + if (std::numeric_limits::max() < tmp_float) { throw exception("Floating point value overflow detected."); } @@ -8177,10 +7226,8 @@ inline void from_node(const BasicNodeType& n, FloatType& f) /// @param n A basic_node object. /// @param s A string node value object. template ::value, int> = 0> -inline void from_node(const BasicNodeType& n, typename BasicNodeType::string_type& s) -{ - if (!n.is_string()) - { +inline void from_node(const BasicNodeType& n, typename BasicNodeType::string_type& s) { + if (!n.is_string()) { throw type_error("The target node value type is not string type.", n.type()); } s = n.template get_value_ref(); @@ -8201,10 +7248,8 @@ template < std::is_constructible, std::is_assignable>>::value, int> = 0> -inline void from_node(const BasicNodeType& n, CompatibleStringType& s) -{ - if (!n.is_string()) - { +inline void from_node(const BasicNodeType& n, CompatibleStringType& s) { + if (!n.is_string()) { throw type_error("The target node value type is not string type.", n.type()); } s = n.template get_value_ref(); @@ -8212,8 +7257,7 @@ inline void from_node(const BasicNodeType& n, CompatibleStringType& s) /// @brief A function object to call from_node functions. /// @note User-defined specialization is available by providing implementation **OUTSIDE** fkyaml namespace. -struct from_node_fn -{ +struct from_node_fn { /// @brief Call from_node function suitable for the given T type. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam T A target value type assigned from the basic_node object. @@ -8222,13 +7266,14 @@ struct from_node_fn /// @return decltype(from_node(n, std::forward(val))) void by default. User can set it to some other type. template auto operator()(const BasicNodeType& n, T&& val) const noexcept(noexcept(from_node(n, std::forward(val)))) - -> decltype(from_node(n, std::forward(val))) - { + -> decltype(from_node(n, std::forward(val))) { return from_node(n, std::forward(val)); } }; -} // namespace detail +FK_YAML_DETAIL_NAMESPACE_END + +FK_YAML_NAMESPACE_BEGIN #ifndef FK_YAML_HAS_CXX_17 // anonymous namespace to hold `from_node` functor. @@ -8252,7 +7297,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -8276,12 +7321,7 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. -FK_YAML_NAMESPACE_BEGIN - -/// @brief namespace for internal implementations of fkYAML library. -namespace detail -{ +FK_YAML_DETAIL_NAMESPACE_BEGIN /////////////////////////////////// // external_node_constructor // @@ -8298,15 +7338,13 @@ struct external_node_constructor; /// @brief The specialization of external_node_constructor for sequence nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with const lvalue sequence. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param s A lvalue sequence value. template ::value, int> = 0> - static void construct(BasicNodeType& n, const typename BasicNodeType::sequence_type& s) noexcept - { + static void construct(BasicNodeType& n, const typename BasicNodeType::sequence_type& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::SEQUENCE; n.m_node_value.p_sequence = BasicNodeType::template create_object(s); @@ -8317,8 +7355,7 @@ struct external_node_constructor /// @param n A basic_node object. /// @param s A rvalue sequence value. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::sequence_type&& s) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::sequence_type&& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::SEQUENCE; n.m_node_value.p_sequence = @@ -8328,15 +7365,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for mapping nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with const lvalue mapping. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param m A lvalue mapping value. template ::value, int> = 0> - static void construct(BasicNodeType& n, const typename BasicNodeType::mapping_type& m) noexcept - { + static void construct(BasicNodeType& n, const typename BasicNodeType::mapping_type& m) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::MAPPING; n.m_node_value.p_mapping = BasicNodeType::template create_object(m); @@ -8347,8 +7382,7 @@ struct external_node_constructor /// @param n A basic_node object. /// @param m A rvalue mapping value. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::mapping_type&& m) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::mapping_type&& m) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::MAPPING; n.m_node_value.p_mapping = @@ -8358,15 +7392,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for null nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with nullptr. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param (unused) nullptr template ::value, int> = 0> - static void construct(BasicNodeType& n, std::nullptr_t /*unused*/) noexcept - { + static void construct(BasicNodeType& n, std::nullptr_t /*unused*/) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::NULL_OBJECT; n.m_node_value.p_mapping = nullptr; @@ -8375,15 +7407,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for boolean scalar nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with boolean. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param b A boolean value. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::boolean_type b) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::boolean_type b) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::BOOLEAN; n.m_node_value.boolean = b; @@ -8392,15 +7422,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for integer scalar nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with integers. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param i An integer value. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::integer_type i) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::integer_type i) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::INTEGER; n.m_node_value.integer = i; @@ -8409,15 +7437,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for float number scalar nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with floating point numbers. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param f A floating point number. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::float_number_type f) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::float_number_type f) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::FLOAT_NUMBER; n.m_node_value.float_val = f; @@ -8426,15 +7452,13 @@ struct external_node_constructor /// @brief The specialization of external_node_constructor for string scalar nodes. template <> -struct external_node_constructor -{ +struct external_node_constructor { /// @brief Constructs a basic_node object with const lvalue strings. /// @tparam BasicNodeType A basic_node template instance type. /// @param n A basic_node object. /// @param s A constant lvalue string. template ::value, int> = 0> - static void construct(BasicNodeType& n, const typename BasicNodeType::string_type& s) noexcept - { + static void construct(BasicNodeType& n, const typename BasicNodeType::string_type& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::STRING; n.m_node_value.p_string = BasicNodeType::template create_object(s); @@ -8445,8 +7469,7 @@ struct external_node_constructor /// @param n A basic_node object. /// @param s A rvalue string. template ::value, int> = 0> - static void construct(BasicNodeType& n, typename BasicNodeType::string_type&& s) noexcept - { + static void construct(BasicNodeType& n, typename BasicNodeType::string_type&& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::STRING; n.m_node_value.p_string = @@ -8465,8 +7488,7 @@ struct external_node_constructor is_basic_node, negation>>::value, int> = 0> - static void construct(BasicNodeType& n, const CompatibleStringType& s) noexcept - { + static void construct(BasicNodeType& n, const CompatibleStringType& s) noexcept { n.m_node_value.destroy(n.m_node_type); n.m_node_type = node_t::STRING; n.m_node_value.p_string = BasicNodeType::template create_object(s); @@ -8489,8 +7511,7 @@ template < is_basic_node, std::is_same>>::value, int> = 0> -inline void to_node(BasicNodeType& n, T&& s) noexcept -{ +inline void to_node(BasicNodeType& n, T&& s) noexcept { external_node_constructor::construct(n, std::forward(s)); } @@ -8505,8 +7526,7 @@ template < conjunction< is_basic_node, std::is_same>>::value, int> = 0> -inline void to_node(BasicNodeType& n, T&& m) noexcept -{ +inline void to_node(BasicNodeType& n, T&& m) noexcept { external_node_constructor::construct(n, std::forward(m)); } @@ -8516,8 +7536,7 @@ inline void to_node(BasicNodeType& n, T&& m) noexcept template < typename BasicNodeType, typename NullType, enable_if_t, std::is_same>::value, int> = 0> -inline void to_node(BasicNodeType& n, NullType /*unused*/) -{ +inline void to_node(BasicNodeType& n, NullType /*unused*/) { external_node_constructor::construct(n, nullptr); } @@ -8531,8 +7550,7 @@ template < enable_if_t< conjunction, std::is_same>::value, int> = 0> -inline void to_node(BasicNodeType& n, T b) noexcept -{ +inline void to_node(BasicNodeType& n, T b) noexcept { external_node_constructor::construct(n, b); } @@ -8544,8 +7562,7 @@ inline void to_node(BasicNodeType& n, T b) noexcept template < typename BasicNodeType, typename T, enable_if_t, is_non_bool_integral>::value, int> = 0> -inline void to_node(BasicNodeType& n, T i) noexcept -{ +inline void to_node(BasicNodeType& n, T i) noexcept { external_node_constructor::construct(n, i); } @@ -8557,8 +7574,7 @@ inline void to_node(BasicNodeType& n, T i) noexcept template < typename BasicNodeType, typename T, enable_if_t, std::is_floating_point>::value, int> = 0> -inline void to_node(BasicNodeType& n, T f) noexcept -{ +inline void to_node(BasicNodeType& n, T f) noexcept { external_node_constructor::construct(n, f); } @@ -8574,8 +7590,7 @@ template < is_basic_node, negation>, std::is_constructible>::value, int> = 0> -inline void to_node(BasicNodeType& n, const T& s) -{ +inline void to_node(BasicNodeType& n, const T& s) { external_node_constructor::construct(n, s); } @@ -8584,15 +7599,13 @@ inline void to_node(BasicNodeType& n, const T& s) /// @param n A basic_node object. /// @param s An rvalue string node value. template ::value, int> = 0> -inline void to_node(BasicNodeType& n, typename BasicNodeType::string_type&& s) noexcept -{ +inline void to_node(BasicNodeType& n, typename BasicNodeType::string_type&& s) noexcept { external_node_constructor::construct(n, std::move(s)); } /// @brief A function object to call to_node functions. /// @note User-defined specialization is available by providing implementation **OUTSIDE** fkyaml namespace. -struct to_node_fn -{ +struct to_node_fn { /// @brief Call to_node function suitable for the given T type. /// @tparam BasicNodeType A basic_node template instance type. /// @tparam T A target value type assigned to the basic_node object. @@ -8601,13 +7614,14 @@ struct to_node_fn /// @return decltype(to_node(n, std::forward(val))) void by default. User can set it to some other type. template auto operator()(BasicNodeType& n, T&& val) const noexcept(noexcept(to_node(n, std::forward(val)))) - -> decltype(to_node(n, std::forward(val))) - { + -> decltype(to_node(n, std::forward(val))) { return to_node(n, std::forward(val)); } }; -} // namespace detail +FK_YAML_DETAIL_NAMESPACE_END + +FK_YAML_NAMESPACE_BEGIN #ifndef FK_YAML_HAS_CXX_17 // anonymous namespace to hold `to_node` functor. @@ -8635,8 +7649,7 @@ FK_YAML_NAMESPACE_BEGIN /// @tparam ValueType A default target data type. /// @sa https://fktn-k.github.io/fkYAML/api/node_value_converter/ template -class node_value_converter -{ +class node_value_converter { public: /// @brief Convert a YAML node value into compatible native data. /// @tparam BasicNodeType A basic_node template instance type. @@ -8647,8 +7660,7 @@ class node_value_converter template static auto from_node(BasicNodeType&& n, TargetType& val) noexcept( noexcept(::fkyaml::from_node(std::forward(n), val))) - -> decltype(::fkyaml::from_node(std::forward(n), val), void()) - { + -> decltype(::fkyaml::from_node(std::forward(n), val), void()) { ::fkyaml::from_node(std::forward(n), val); } @@ -8661,8 +7673,7 @@ class node_value_converter template static auto to_node(BasicNodeType& n, TargetType&& val) noexcept( noexcept(::fkyaml::to_node(n, std::forward(val)))) - -> decltype(::fkyaml::to_node(n, std::forward(val))) - { + -> decltype(::fkyaml::to_node(n, std::forward(val))) { ::fkyaml::to_node(n, std::forward(val)); } }; @@ -8674,7 +7685,7 @@ FK_YAML_NAMESPACE_END // #include /// _______ __ __ __ _____ __ __ __ /// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library -/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 /// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML /// /// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -8698,7 +7709,6 @@ FK_YAML_NAMESPACE_END // #include -/// @brief namespace for fkYAML library. FK_YAML_NAMESPACE_BEGIN /// @brief A minimal map-like container which preserves insertion order. @@ -8710,8 +7720,7 @@ FK_YAML_NAMESPACE_BEGIN template < typename Key, typename Value, typename IgnoredCompare = std::less, typename Allocator = std::allocator>> -class ordered_map : public std::vector, Allocator> -{ +class ordered_map : public std::vector, Allocator> { public: /// A type for keys. using key_type = Key; @@ -8734,16 +7743,14 @@ class ordered_map : public std::vector, Allocator> /// @brief Construct a new ordered_map object. /// @sa https://fktn-k.github.io/fkYAML/api/ordered_map/constructor/ ordered_map() noexcept(noexcept(Container())) - : Container() - { + : Container() { } /// @brief Construct a new ordered_map object with an initializer list. /// @param init An initializer list to construct the inner container object. /// @sa https://fktn-k.github.io/fkYAML/api/ordered_map/constructor/ ordered_map(std::initializer_list init) - : Container {init} - { + : Container {init} { } public: @@ -8755,8 +7762,7 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - mapped_type& operator[](KeyType&& key) noexcept - { + mapped_type& operator[](KeyType&& key) noexcept { return emplace(std::forward(key), mapped_type()).first->second; } @@ -8770,12 +7776,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - std::pair emplace(KeyType&& key, const mapped_type& value) noexcept - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + std::pair emplace(KeyType&& key, const mapped_type& value) noexcept { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return {itr, false}; } } @@ -8791,12 +7794,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - mapped_type& at(KeyType&& key) - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + mapped_type& at(KeyType&& key) { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return itr->second; } } @@ -8811,12 +7811,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - const mapped_type& at(KeyType&& key) const - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + const mapped_type& at(KeyType&& key) const { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return itr->second; } } @@ -8831,12 +7828,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - iterator find(KeyType&& key) noexcept - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + iterator find(KeyType&& key) noexcept { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return itr; } } @@ -8851,12 +7845,9 @@ class ordered_map : public std::vector, Allocator> template < typename KeyType, detail::enable_if_t::value, int> = 0> - const_iterator find(KeyType&& key) const noexcept - { - for (auto itr = this->begin(); itr != this->end(); ++itr) - { - if (m_compare(itr->first, key)) - { + const_iterator find(KeyType&& key) const noexcept { + for (auto itr = this->begin(); itr != this->end(); ++itr) { + if (m_compare(itr->first, key)) { return itr; } } @@ -8873,7 +7864,6 @@ FK_YAML_NAMESPACE_END #endif /* FK_YAML_ORDERED_MAP_HPP_ */ -/// @brief namespace for fkYAML library. FK_YAML_NAMESPACE_BEGIN /// @brief A class to store value of YAML nodes. @@ -8883,8 +7873,7 @@ template < template class MappingType = std::map, typename BooleanType = bool, typename IntegerType = std::int64_t, typename FloatNumberType = double, typename StringType = std::string, template class ConverterType = node_value_converter> -class basic_node -{ +class basic_node { public: /// @brief A type for iterators of basic_node containers. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/iterator/ @@ -8953,18 +7942,15 @@ class basic_node /// @details This union combines the different sotrage types for the YAML value types defined in @ref node_t. /// @note Container types are stored as pointers so that the size of this union will not exceed 64 bits by /// default. - union node_value - { + union node_value { /// @brief Constructs a new basic_node Value object for null types. node_value() = default; /// @brief Constructs a new basic_node value object with a node type. The default value for the specified /// type will be assigned. /// @param[in] type A node type. - explicit node_value(node_t type) - { - switch (type) - { + explicit node_value(node_t type) { + switch (type) { case node_t::SEQUENCE: p_sequence = create_object(); break; @@ -8992,43 +7978,34 @@ class basic_node /// @brief Destroys the existing Node value. This process is recursive if the specified node type is for /// containers. /// @param[in] type A Node type to determine the value to be destroyed. - void destroy(node_t type) - { - if (type == node_t::SEQUENCE || type == node_t::MAPPING) - { + void destroy(node_t type) { + if (type == node_t::SEQUENCE || type == node_t::MAPPING) { std::vector stack; - if (type == node_t::SEQUENCE) - { + if (type == node_t::SEQUENCE) { stack.reserve(p_sequence->size()); std::move(p_sequence->begin(), p_sequence->end(), std::back_inserter(stack)); } - else - { + else { stack.reserve(p_mapping->size()); - for (auto&& it : *p_mapping) - { + for (auto&& it : *p_mapping) { stack.push_back(std::move(it.second)); } } - while (!stack.empty()) - { + while (!stack.empty()) { basic_node current_node(std::move(stack.back())); stack.pop_back(); - if (current_node.is_sequence()) - { + if (current_node.is_sequence()) { std::move( current_node.m_node_value.p_sequence->begin(), current_node.m_node_value.p_sequence->end(), std::back_inserter(stack)); current_node.m_node_value.p_sequence->clear(); } - else if (current_node.is_mapping()) - { - for (auto&& it : *current_node.m_node_value.p_mapping) - { + else if (current_node.is_mapping()) { + for (auto&& it : *current_node.m_node_value.p_mapping) { stack.push_back(std::move(it.second)); } current_node.m_node_value.p_mapping->clear(); @@ -9036,8 +8013,7 @@ class basic_node } } - switch (type) - { + switch (type) { case node_t::SEQUENCE: destroy_object(p_sequence); p_sequence = nullptr; @@ -9076,8 +8052,7 @@ class basic_node /// @param[in] args A parameter pack for constructor arguments of the target object type. /// @return ObjType* An address of allocated memory on the heap. template - static ObjType* create_object(ArgTypes&&... args) - { + static ObjType* create_object(ArgTypes&&... args) { using AllocType = std::allocator; using AllocTraitsType = std::allocator_traits; @@ -9099,8 +8074,7 @@ class basic_node /// @tparam ObjType The target object type. /// @param[in] obj A pointer to the target object to be destroyed. template - static void destroy_object(ObjType* obj) - { + static void destroy_object(ObjType* obj) { FK_YAML_ASSERT(obj != nullptr); std::allocator alloc; std::allocator_traits::destroy(alloc, obj); @@ -9117,8 +8091,7 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/constructor/ explicit basic_node(const node_t type) : m_node_type(type), - m_node_value(type) - { + m_node_value(type) { } /// @brief Copy constructor of the basic_node class. @@ -9127,10 +8100,8 @@ class basic_node basic_node(const basic_node& rhs) : m_node_type(rhs.m_node_type), mp_directive_set(rhs.mp_directive_set), - m_prop(rhs.m_prop) - { - switch (m_node_type) - { + m_prop(rhs.m_prop) { + switch (m_node_type) { case node_t::SEQUENCE: m_node_value.p_sequence = create_object(*(rhs.m_node_value.p_sequence)); FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); @@ -9164,10 +8135,8 @@ class basic_node basic_node(basic_node&& rhs) noexcept : m_node_type(rhs.m_node_type), mp_directive_set(std::move(rhs.mp_directive_set)), - m_prop(std::move(rhs.m_prop)) - { - switch (m_node_type) - { + m_prop(std::move(rhs.m_prop)) { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(rhs.m_node_value.p_sequence != nullptr); m_node_value.p_sequence = rhs.m_node_value.p_sequence; @@ -9219,8 +8188,7 @@ class basic_node detail::disjunction>>::value, int> = 0> basic_node(CompatibleType&& val) noexcept( - noexcept(ConverterType::to_node(std::declval(), std::declval()))) - { + noexcept(ConverterType::to_node(std::declval(), std::declval()))) { ConverterType::to_node(*this, std::forward(val)); } @@ -9232,34 +8200,29 @@ class basic_node typename NodeRefStorageType, detail::enable_if_t::value, int> = 0> basic_node(const NodeRefStorageType& node_ref_storage) noexcept - : basic_node(node_ref_storage.release()) - { + : basic_node(node_ref_storage.release()) { } /// @brief Construct a new basic node object with std::initializer_list. /// @param[in] init A initializer list of basic_node objects. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/constructor/ - basic_node(initializer_list_t init) - { + basic_node(initializer_list_t init) { bool is_mapping = std::all_of(init.begin(), init.end(), [](const detail::node_ref_storage& node_ref) { return node_ref->is_sequence() && node_ref->size() == 2; }); - if (is_mapping) - { + if (is_mapping) { m_node_type = node_t::MAPPING; m_node_value.p_mapping = create_object(); - for (auto& elem_ref : init) - { + for (auto& elem_ref : init) { auto elem = elem_ref.release(); m_node_value.p_mapping->emplace( std::move((*(elem.m_node_value.p_sequence))[0]), std::move((*(elem.m_node_value.p_sequence))[1])); } } - else - { + else { m_node_type = node_t::SEQUENCE; m_node_value.p_sequence = create_object(init.begin(), init.end()); } @@ -9280,8 +8243,7 @@ class basic_node /// @return The resulting basic_node object deserialized from the input source. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/deserialize/ template - static basic_node deserialize(InputType&& input) - { + static basic_node deserialize(InputType&& input) { return deserializer_type().deserialize(detail::input_adapter(std::forward(input))); } @@ -9292,8 +8254,7 @@ class basic_node /// @return The resulting basic_node object deserialized from the pair of iterators. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/deserialize/ template - static basic_node deserialize(ItrType&& begin, ItrType&& end) - { + static basic_node deserialize(ItrType&& begin, ItrType&& end) { return deserializer_type().deserialize( detail::input_adapter(std::forward(begin), std::forward(end))); } @@ -9302,16 +8263,14 @@ class basic_node /// @param[in] node A basic_node object to be serialized. /// @return The resulting string object from the serialization of the node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/serialize/ - static std::string serialize(const basic_node& node) - { + static std::string serialize(const basic_node& node) { return serializer_type().serialize(node); } /// @brief A factory method for sequence basic_node objects without sequence_type objects. /// @return A YAML sequence node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/sequence/ - static basic_node sequence() - { + static basic_node sequence() { basic_node node; node.m_node_type = node_t::SEQUENCE; node.m_node_value.p_sequence = create_object(); @@ -9323,8 +8282,7 @@ class basic_node /// @param[in] seq A lvalue sequence node value. /// @return A YAML sequence node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/sequence/ - static basic_node sequence(const sequence_type& seq) - { + static basic_node sequence(const sequence_type& seq) { basic_node node; node.m_node_type = node_t::SEQUENCE; node.m_node_value.p_sequence = create_object(seq); @@ -9336,8 +8294,7 @@ class basic_node /// @param[in] seq A rvalue sequence node value. /// @return A YAML sequence node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/sequence/ - static basic_node sequence(sequence_type&& seq) - { + static basic_node sequence(sequence_type&& seq) { basic_node node; node.m_node_type = node_t::SEQUENCE; node.m_node_value.p_sequence = create_object(std::move(seq)); @@ -9348,8 +8305,7 @@ class basic_node /// @brief A factory method for mapping basic_node objects without mapping_type objects. /// @return A YAML mapping node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/mapping/ - static basic_node mapping() - { + static basic_node mapping() { basic_node node; node.m_node_type = node_t::MAPPING; node.m_node_value.p_mapping = create_object(); @@ -9361,8 +8317,7 @@ class basic_node /// @param[in] map A lvalue mapping node value. /// @return A YAML mapping node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/mapping/ - static basic_node mapping(const mapping_type& map) - { + static basic_node mapping(const mapping_type& map) { basic_node node; node.m_node_type = node_t::MAPPING; node.m_node_value.p_mapping = create_object(map); @@ -9374,8 +8329,7 @@ class basic_node /// @param[in] map A rvalue mapping node value. /// @return A YAML mapping node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/mapping/ - static basic_node mapping(mapping_type&& map) - { + static basic_node mapping(mapping_type&& map) { basic_node node; node.m_node_type = node_t::MAPPING; node.m_node_value.p_mapping = create_object(std::move(map)); @@ -9388,10 +8342,8 @@ class basic_node /// @param[in] anchor_node A basic_node object with an anchor name. /// @return An alias YAML node created from the given anchor node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/alias_of/ - static basic_node alias_of(const basic_node& anchor_node) - { - if (!anchor_node.has_anchor_name() || anchor_node.m_prop.anchor_status != detail::anchor_status_t::ANCHOR) - { + static basic_node alias_of(const basic_node& anchor_node) { + if (!anchor_node.has_anchor_name() || anchor_node.m_prop.anchor_status != detail::anchor_status_t::ANCHOR) { throw fkyaml::exception("Cannot create an alias without anchor name."); } @@ -9406,8 +8358,7 @@ class basic_node /// @param[in] rhs A lvalue basic_node object to be copied with. /// @return Reference to this basic_node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator=/ - basic_node& operator=(const basic_node& rhs) noexcept - { + basic_node& operator=(const basic_node& rhs) noexcept { basic_node(rhs).swap(*this); return *this; } @@ -9416,8 +8367,7 @@ class basic_node /// @param[in] rhs A rvalue basic_node object to be moved from. /// @return Reference to this basic_node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator=/ - basic_node& operator=(basic_node&& rhs) noexcept - { + basic_node& operator=(basic_node&& rhs) noexcept { basic_node(std::move(rhs)).swap(*this); return *this; } @@ -9433,19 +8383,15 @@ class basic_node detail::negation>, detail::is_node_compatible_type>::value, int> = 0> - basic_node& operator[](KeyType&& key) - { - if (is_scalar()) - { + basic_node& operator[](KeyType&& key) { + if (is_scalar()) { throw fkyaml::type_error("operator[] is unavailable for a scalar node.", m_node_type); } basic_node n = std::forward(key); - if (is_sequence()) - { - if (!n.is_integer()) - { + if (is_sequence()) { + if (!n.is_integer()) { throw fkyaml::type_error( "An argument of operator[] for sequence nodes must be an integer.", m_node_type); } @@ -9468,19 +8414,15 @@ class basic_node detail::negation>, detail::is_node_compatible_type>::value, int> = 0> - const basic_node& operator[](KeyType&& key) const - { - if (is_scalar()) - { + const basic_node& operator[](KeyType&& key) const { + if (is_scalar()) { throw fkyaml::type_error("operator[] is unavailable for a scalar node.", m_node_type); } basic_node node_key = std::forward(key); - if (is_sequence()) - { - if (!node_key.is_integer()) - { + if (is_sequence()) { + if (!node_key.is_integer()) { throw fkyaml::type_error( "An argument of operator[] for sequence nodes must be an integer.", m_node_type); } @@ -9499,17 +8441,13 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator[]/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - basic_node& operator[](KeyType&& key) - { - if (is_scalar()) - { + basic_node& operator[](KeyType&& key) { + if (is_scalar()) { throw fkyaml::type_error("operator[] is unavailable for a scalar node.", m_node_type); } - if (is_sequence()) - { - if (!key.is_integer()) - { + if (is_sequence()) { + if (!key.is_integer()) { throw fkyaml::type_error( "An argument of operator[] for sequence nodes must be an integer.", m_node_type); } @@ -9528,17 +8466,13 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator[]/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - const basic_node& operator[](KeyType&& key) const - { - if (is_scalar()) - { + const basic_node& operator[](KeyType&& key) const { + if (is_scalar()) { throw fkyaml::type_error("operator[] is unavailable for a scalar node.", m_node_type); } - if (is_sequence()) - { - if (!key.is_integer()) - { + if (is_sequence()) { + if (!key.is_integer()) { throw fkyaml::type_error( "An argument of operator[] for sequence nodes must be an integer.", m_node_type); } @@ -9554,16 +8488,13 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true if both types and values are equal, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_eq/ - bool operator==(const basic_node& rhs) const noexcept - { - if (m_node_type != rhs.m_node_type) - { + bool operator==(const basic_node& rhs) const noexcept { + if (m_node_type != rhs.m_node_type) { return false; } bool ret = false; - switch (m_node_type) - { + switch (m_node_type) { case node_t::SEQUENCE: ret = (*(m_node_value.p_sequence) == *(rhs.m_node_value.p_sequence)); break; @@ -9597,8 +8528,7 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true if either types or values are different, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_ne/ - bool operator!=(const basic_node& rhs) const noexcept - { + bool operator!=(const basic_node& rhs) const noexcept { return !operator==(rhs); } @@ -9606,26 +8536,21 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true this basic_node object is less than `rhs`. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_lt/ - bool operator<(const basic_node& rhs) const noexcept - { - if (operator==(rhs)) - { + bool operator<(const basic_node& rhs) const noexcept { + if (operator==(rhs)) { return false; } - if (uint32_t(m_node_type) < uint32_t(rhs.m_node_type)) - { + if (uint32_t(m_node_type) < uint32_t(rhs.m_node_type)) { return true; } - if (m_node_type != rhs.m_node_type) - { + if (m_node_type != rhs.m_node_type) { return false; } bool ret = false; - switch (m_node_type) - { + switch (m_node_type) { case node_t::SEQUENCE: ret = (*(m_node_value.p_sequence) < *(rhs.m_node_value.p_sequence)); break; @@ -9657,8 +8582,7 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true this basic_node object is less than or equal to `rhs`. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_le/ - bool operator<=(const basic_node& rhs) const noexcept - { + bool operator<=(const basic_node& rhs) const noexcept { return !rhs.operator<(*this); } @@ -9666,8 +8590,7 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true this basic_node object is greater than `rhs`. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_gt/ - bool operator>(const basic_node& rhs) const noexcept - { + bool operator>(const basic_node& rhs) const noexcept { return !operator<=(rhs); } @@ -9675,8 +8598,7 @@ class basic_node /// @param rhs A basic_node object to be compared with this basic_node object. /// @return true this basic_node object is greater than or equal to `rhs`. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/operator_ge/ - bool operator>=(const basic_node& rhs) const noexcept - { + bool operator>=(const basic_node& rhs) const noexcept { return !operator<(rhs); } @@ -9684,98 +8606,85 @@ class basic_node /// @brief Returns the type of the current basic_node value. /// @return The type of the YAML node value. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/type/ - node_t type() const noexcept - { + node_t type() const noexcept { return m_node_type; } /// @brief Tests whether the current basic_node value is of sequence type. /// @return true if the type is sequence, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_sequence/ - bool is_sequence() const noexcept - { + bool is_sequence() const noexcept { return m_node_type == node_t::SEQUENCE; } /// @brief Tests whether the current basic_node value is of mapping type. /// @return true if the type is mapping, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_mapping/ - bool is_mapping() const noexcept - { + bool is_mapping() const noexcept { return m_node_type == node_t::MAPPING; } /// @brief Tests whether the current basic_node value is of null type. /// @return true if the type is null, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_null/ - bool is_null() const noexcept - { + bool is_null() const noexcept { return m_node_type == node_t::NULL_OBJECT; } /// @brief Tests whether the current basic_node value is of boolean type. /// @return true if the type is boolean, false otherwise /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_boolean/ - bool is_boolean() const noexcept - { + bool is_boolean() const noexcept { return m_node_type == node_t::BOOLEAN; } /// @brief Tests whether the current basic_node value is of integer type. /// @return true if the type is integer, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_integer/ - bool is_integer() const noexcept - { + bool is_integer() const noexcept { return m_node_type == node_t::INTEGER; } /// @brief Tests whether the current basic_node value is of float number type. /// @return true if the type is floating point number, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_float_number/ - bool is_float_number() const noexcept - { + bool is_float_number() const noexcept { return m_node_type == node_t::FLOAT_NUMBER; } /// @brief Tests whether the current basic_node value is of string type. /// @return true if the type is string, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_string/ - bool is_string() const noexcept - { + bool is_string() const noexcept { return m_node_type == node_t::STRING; } /// @brief Tests whether the current basic_node value is of scalar types. /// @return true if the type is scalar, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_scalar/ - bool is_scalar() const noexcept - { + bool is_scalar() const noexcept { return !is_sequence() && !is_mapping(); } /// @brief Tests whether the current basic_node is an anchor node. /// @return true if the current basic_node is an anchor node, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_anchor/ - bool is_anchor() const noexcept - { + bool is_anchor() const noexcept { return m_prop.anchor_status == detail::anchor_status_t::ANCHOR; } /// @brief Tests whether the current basic_node is an alias node. /// @return true if the current basic_node is an alias node, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/is_alias/ - bool is_alias() const noexcept - { + bool is_alias() const noexcept { return m_prop.anchor_status == detail::anchor_status_t::ALIAS; } /// @brief Tests whether the current basic_node value (sequence, mapping, string) is empty. /// @return true if the node value is empty, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/empty/ - bool empty() const - { - switch (m_node_type) - { + bool empty() const { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return m_node_value.p_sequence->empty(); @@ -9793,10 +8702,8 @@ class basic_node /// @brief Returns the size of the current basic_node value (sequence, mapping, string). /// @return The size of a node value. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/size/ - std::size_t size() const - { - switch (m_node_type) - { + std::size_t size() const { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return m_node_value.p_sequence->size(); @@ -9822,10 +8729,8 @@ class basic_node detail::negation>>, detail::is_node_compatible_type>>::value, int> = 0> - bool contains(KeyType&& key) const - { - switch (m_node_type) - { + bool contains(KeyType&& key) const { + switch (m_node_type) { case node_t::MAPPING: { FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); mapping_type& map = *m_node_value.p_mapping; @@ -9844,10 +8749,8 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/contains/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - bool contains(KeyType&& key) const - { - switch (m_node_type) - { + bool contains(KeyType&& key) const { + switch (m_node_type) { case node_t::MAPPING: { FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); mapping_type& map = *m_node_value.p_mapping; @@ -9869,27 +8772,22 @@ class basic_node detail::negation>, detail::is_node_compatible_type>::value, int> = 0> - basic_node& at(KeyType&& key) - { - if (is_scalar()) - { + basic_node& at(KeyType&& key) { + if (is_scalar()) { throw fkyaml::type_error("at() is unavailable for a scalar node.", m_node_type); } basic_node node_key = std::forward(key); - if (is_sequence()) - { - if (!node_key.is_integer()) - { + if (is_sequence()) { + if (!node_key.is_integer()) { throw fkyaml::type_error("An argument of at() for sequence nodes must be an integer.", m_node_type); } FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); int index = node_key.template get_value(); int size = static_cast(m_node_value.p_sequence->size()); - if (index >= size) - { + if (index >= size) { throw fkyaml::out_of_range(index); } return m_node_value.p_sequence->at(index); @@ -9897,8 +8795,7 @@ class basic_node FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); bool is_found = m_node_value.p_mapping->find(node_key) != m_node_value.p_mapping->end(); - if (!is_found) - { + if (!is_found) { throw fkyaml::out_of_range(serialize(node_key).c_str()); } return m_node_value.p_mapping->at(node_key); @@ -9915,27 +8812,22 @@ class basic_node detail::negation>, detail::is_node_compatible_type>::value, int> = 0> - const basic_node& at(KeyType&& key) const - { - if (is_scalar()) - { + const basic_node& at(KeyType&& key) const { + if (is_scalar()) { throw fkyaml::type_error("at() is unavailable for a scalar node.", m_node_type); } basic_node node_key = std::forward(key); - if (is_sequence()) - { - if (!node_key.is_integer()) - { + if (is_sequence()) { + if (!node_key.is_integer()) { throw fkyaml::type_error("An argument of at() for sequence nodes must be an integer.", m_node_type); } FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); int index = node_key.template get_value(); int size = static_cast(m_node_value.p_sequence->size()); - if (index >= size) - { + if (index >= size) { throw fkyaml::out_of_range(index); } return m_node_value.p_sequence->at(index); @@ -9943,8 +8835,7 @@ class basic_node FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); bool is_found = m_node_value.p_mapping->find(node_key) != m_node_value.p_mapping->end(); - if (!is_found) - { + if (!is_found) { throw fkyaml::out_of_range(serialize(node_key).c_str()); } return m_node_value.p_mapping->at(node_key); @@ -9957,25 +8848,20 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/at/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - basic_node& at(KeyType&& key) - { - if (is_scalar()) - { + basic_node& at(KeyType&& key) { + if (is_scalar()) { throw fkyaml::type_error("at() is unavailable for a scalar node.", m_node_type); } - if (is_sequence()) - { - if (!key.is_integer()) - { + if (is_sequence()) { + if (!key.is_integer()) { throw fkyaml::type_error("An argument of at() for sequence nodes must be an integer.", m_node_type); } FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); int index = std::forward(key).template get_value(); int size = static_cast(m_node_value.p_sequence->size()); - if (index >= size) - { + if (index >= size) { throw fkyaml::out_of_range(index); } return m_node_value.p_sequence->at(index); @@ -9983,8 +8869,7 @@ class basic_node FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); bool is_found = m_node_value.p_mapping->find(key) != m_node_value.p_mapping->end(); - if (!is_found) - { + if (!is_found) { throw fkyaml::out_of_range(serialize(key).c_str()); } return m_node_value.p_mapping->at(key); @@ -9997,25 +8882,20 @@ class basic_node /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/at/ template < typename KeyType, detail::enable_if_t>::value, int> = 0> - const basic_node& at(KeyType&& key) const - { - if (is_scalar()) - { + const basic_node& at(KeyType&& key) const { + if (is_scalar()) { throw fkyaml::type_error("at() is unavailable for a scalar node.", m_node_type); } - if (is_sequence()) - { - if (!key.is_integer()) - { + if (is_sequence()) { + if (!key.is_integer()) { throw fkyaml::type_error("An argument of at() for sequence nodes must be an integer.", m_node_type); } FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); int index = std::forward(key).template get_value(); int size = static_cast(m_node_value.p_sequence->size()); - if (index >= size) - { + if (index >= size) { throw fkyaml::out_of_range(index); } return m_node_value.p_sequence->at(index); @@ -10023,8 +8903,7 @@ class basic_node FK_YAML_ASSERT(m_node_value.p_mapping != nullptr); bool is_found = m_node_value.p_mapping->find(key) != m_node_value.p_mapping->end(); - if (!is_found) - { + if (!is_found) { throw fkyaml::out_of_range(serialize(key).c_str()); } return m_node_value.p_mapping->at(key); @@ -10033,8 +8912,7 @@ class basic_node /// @brief Get the YAML version specification for this basic_node object. /// @return The YAML version if any is applied to the basic_node object, `yaml_version_t::VER_1_2` otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/get_yaml_version/ - yaml_version_t get_yaml_version() const noexcept - { + yaml_version_t get_yaml_version() const noexcept { return (mp_directive_set && mp_directive_set->is_version_specified) ? mp_directive_set->version : yaml_version_t::VER_1_2; } @@ -10043,10 +8921,8 @@ class basic_node /// @note If no YAML directive /// @param[in] A version of the YAML format. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/set_yaml_version/ - void set_yaml_version(const yaml_version_t version) noexcept - { - if (!mp_directive_set) - { + void set_yaml_version(const yaml_version_t version) noexcept { + if (!mp_directive_set) { mp_directive_set = std::shared_ptr(new detail::directive_set()); } mp_directive_set->version = version; @@ -10056,8 +8932,7 @@ class basic_node /// @brief Check whether or not this basic_node object has already had any anchor name. /// @return true if ths basic_node has an anchor name, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/has_anchor_name/ - bool has_anchor_name() const noexcept - { + bool has_anchor_name() const noexcept { return m_prop.anchor_status != detail::anchor_status_t::NONE && !m_prop.anchor.empty(); } @@ -10066,10 +8941,8 @@ class basic_node /// object has any anchor name. /// @return The anchor name associated with the node. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/get_anchor_name/ - const std::string& get_anchor_name() const - { - if (!has_anchor_name()) - { + const std::string& get_anchor_name() const { + if (!has_anchor_name()) { throw fkyaml::exception("No anchor name has been set."); } return m_prop.anchor; @@ -10079,8 +8952,7 @@ class basic_node /// @note If this basic_node object has already had any anchor name, the new anchor name will overwrite the old one. /// @param[in] anchor_name An anchor name. This should not be empty. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/add_anchor_name/ - void add_anchor_name(const std::string& anchor_name) - { + void add_anchor_name(const std::string& anchor_name) { m_prop.anchor_status = detail::anchor_status_t::ANCHOR; m_prop.anchor = anchor_name; } @@ -10089,8 +8961,7 @@ class basic_node /// @note If this basic_node object has already had any anchor name, the new anchor name will overwrite the old one. /// @param[in] anchor_name An anchor name. This should not be empty. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/add_anchor_name/ - void add_anchor_name(std::string&& anchor_name) - { + void add_anchor_name(std::string&& anchor_name) { m_prop.anchor_status = detail::anchor_status_t::ANCHOR; m_prop.anchor = std::move(anchor_name); } @@ -10098,8 +8969,7 @@ class basic_node /// @brief Check whether or not this basic_node object has already had any tag name. /// @return true if ths basic_node has a tag name, false otherwise. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/has_tag_name/ - bool has_tag_name() const noexcept - { + bool has_tag_name() const noexcept { return !m_prop.tag.empty(); } @@ -10108,10 +8978,8 @@ class basic_node /// object has any tag name. /// @return The tag name associated with the node. It may be empty. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/get_tag_name/ - const std::string& get_tag_name() const - { - if (!has_tag_name()) - { + const std::string& get_tag_name() const { + if (!has_tag_name()) { throw fkyaml::exception("No tag name has been set."); } return m_prop.tag; @@ -10121,8 +8989,7 @@ class basic_node /// @note If this basic_node object has already had any tag name, the new tag name will overwrite the old one. /// @param[in] tag_name A tag name to get associated with this basic_node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/add_tag_name/ - void add_tag_name(const std::string& tag_name) - { + void add_tag_name(const std::string& tag_name) { m_prop.tag = tag_name; } @@ -10130,8 +8997,7 @@ class basic_node /// @note If this basic_node object has already had any tag name, the new tag name will overwrite the old one. /// @param[in] tag_name A tag name to get associated with this basic_node object. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/add_tag_name/ - void add_tag_name(std::string&& tag_name) - { + void add_tag_name(std::string&& tag_name) { m_prop.tag = std::move(tag_name); } @@ -10148,8 +9014,7 @@ class basic_node std::is_default_constructible, detail::has_from_node>::value, int> = 0> T get_value() const noexcept( - noexcept(ConverterType::from_node(std::declval(), std::declval()))) - { + noexcept(ConverterType::from_node(std::declval(), std::declval()))) { auto ret = ValueType(); ConverterType::from_node(*this, ret); return ret; @@ -10160,8 +9025,7 @@ class basic_node /// @return Reference to the internally stored YAML node value. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/get_value_ref/ template ::value, int> = 0> - ReferenceType get_value_ref() - { + ReferenceType get_value_ref() { return get_value_ref_impl(static_cast>(nullptr)); } @@ -10175,16 +9039,14 @@ class basic_node detail::conjunction< std::is_reference, std::is_const>>::value, int> = 0> - ReferenceType get_value_ref() const - { + ReferenceType get_value_ref() const { return get_value_ref_impl(static_cast>(nullptr)); } /// @brief Swaps the internally stored data with the specified basic_node object. /// @param[in] rhs A basic_node object to be swapped with. /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/swap/ - void swap(basic_node& rhs) noexcept - { + void swap(basic_node& rhs) noexcept { using std::swap; swap(m_node_type, rhs.m_node_type); swap(mp_directive_set, rhs.mp_directive_set); @@ -10203,10 +9065,8 @@ class basic_node /// basic_node object. Throws exception if the basic_node value is not of container types. /// @return An iterator to the first element of a YAML node value (either sequence or mapping). /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/begin/ - iterator begin() - { - switch (m_node_type) - { + iterator begin() { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return {detail::sequence_iterator_tag(), m_node_value.p_sequence->begin()}; @@ -10222,10 +9082,8 @@ class basic_node /// basic_node object. Throws exception if the basic_node value is not of container types. /// @return A constant iterator to the first element of a YAML node value (either sequence or mapping). /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/begin/ - const_iterator begin() const - { - switch (m_node_type) - { + const_iterator begin() const { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return {detail::sequence_iterator_tag(), m_node_value.p_sequence->begin()}; @@ -10241,10 +9099,8 @@ class basic_node /// basic_node object. Throws exception if the basic_node value is not of container types. /// @return An iterator to the past-the end element of a YAML node value (either sequence or mapping). /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/end/ - iterator end() - { - switch (m_node_type) - { + iterator end() { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return {detail::sequence_iterator_tag(), m_node_value.p_sequence->end()}; @@ -10260,10 +9116,8 @@ class basic_node /// basic_node object. Throws exception if the basic_node value is not of container types. /// @return A constant iterator to the past-the end element of a YAML node value (either sequence or mapping). /// @sa https://fktn-k.github.io/fkYAML/api/basic_node/end/ - const_iterator end() const - { - switch (m_node_type) - { + const_iterator end() const { + switch (m_node_type) { case node_t::SEQUENCE: FK_YAML_ASSERT(m_node_value.p_sequence != nullptr); return {detail::sequence_iterator_tag(), m_node_value.p_sequence->end()}; @@ -10279,10 +9133,8 @@ class basic_node /// @brief Returns reference to the sequence node value. /// @throw fkyaml::exception The node value is not a sequence. /// @return Reference to the sequence node value. - sequence_type& get_value_ref_impl(sequence_type* /*unused*/) - { - if (!is_sequence()) - { + sequence_type& get_value_ref_impl(sequence_type* /*unused*/) { + if (!is_sequence()) { throw fkyaml::type_error("The node value is not a sequence.", m_node_type); } return *(m_node_value.p_sequence); @@ -10291,10 +9143,8 @@ class basic_node /// @brief Returns constant reference to the sequence node value. /// @throw fkyaml::exception The node value is not a sequence. /// @return Constant reference to the sequence node value. - const sequence_type& get_value_ref_impl(const sequence_type* /*unused*/) const - { - if (!is_sequence()) - { + const sequence_type& get_value_ref_impl(const sequence_type* /*unused*/) const { + if (!is_sequence()) { throw fkyaml::type_error("The node value is not a sequence.", m_node_type); } return *(m_node_value.p_sequence); @@ -10303,10 +9153,8 @@ class basic_node /// @brief Returns reference to the mapping node value. /// @throw fkyaml::exception The node value is not a mapping. /// @return Reference to the mapping node value. - mapping_type& get_value_ref_impl(mapping_type* /*unused*/) - { - if (!is_mapping()) - { + mapping_type& get_value_ref_impl(mapping_type* /*unused*/) { + if (!is_mapping()) { throw fkyaml::type_error("The node value is not a mapping.", m_node_type); } return *(m_node_value.p_mapping); @@ -10315,10 +9163,8 @@ class basic_node /// @brief Returns constant reference to the mapping node value. /// @throw fkyaml::exception The node value is not a mapping. /// @return Constant reference to the mapping node value. - const mapping_type& get_value_ref_impl(const mapping_type* /*unused*/) const - { - if (!is_mapping()) - { + const mapping_type& get_value_ref_impl(const mapping_type* /*unused*/) const { + if (!is_mapping()) { throw fkyaml::type_error("The node value is not a mapping.", m_node_type); } return *(m_node_value.p_mapping); @@ -10327,10 +9173,8 @@ class basic_node /// @brief Returns reference to the boolean node value. /// @throw fkyaml::exception The node value is not a boolean. /// @return Reference to the boolean node value. - boolean_type& get_value_ref_impl(boolean_type* /*unused*/) - { - if (!is_boolean()) - { + boolean_type& get_value_ref_impl(boolean_type* /*unused*/) { + if (!is_boolean()) { throw fkyaml::type_error("The node value is not a boolean.", m_node_type); } return m_node_value.boolean; @@ -10339,10 +9183,8 @@ class basic_node /// @brief Returns reference to the boolean node value. /// @throw fkyaml::exception The node value is not a boolean. /// @return Constant reference to the boolean node value. - const boolean_type& get_value_ref_impl(const boolean_type* /*unused*/) const - { - if (!is_boolean()) - { + const boolean_type& get_value_ref_impl(const boolean_type* /*unused*/) const { + if (!is_boolean()) { throw fkyaml::type_error("The node value is not a boolean.", m_node_type); } return m_node_value.boolean; @@ -10351,10 +9193,8 @@ class basic_node /// @brief Returns reference to the integer node value. /// @throw fkyaml::exception The node value is not an integer. /// @return Reference to the integer node value. - integer_type& get_value_ref_impl(integer_type* /*unused*/) - { - if (!is_integer()) - { + integer_type& get_value_ref_impl(integer_type* /*unused*/) { + if (!is_integer()) { throw fkyaml::type_error("The node value is not an integer.", m_node_type); } return m_node_value.integer; @@ -10363,10 +9203,8 @@ class basic_node /// @brief Returns reference to the integer node value. /// @throw fkyaml::exception The node value is not an integer. /// @return Constant reference to the integer node value. - const integer_type& get_value_ref_impl(const integer_type* /*unused*/) const - { - if (!is_integer()) - { + const integer_type& get_value_ref_impl(const integer_type* /*unused*/) const { + if (!is_integer()) { throw fkyaml::type_error("The node value is not an integer.", m_node_type); } return m_node_value.integer; @@ -10375,10 +9213,8 @@ class basic_node /// @brief Returns reference to the floating point number node value. /// @throw fkyaml::exception The node value is not a floating point number. /// @return Reference to the floating point number node value. - float_number_type& get_value_ref_impl(float_number_type* /*unused*/) - { - if (!is_float_number()) - { + float_number_type& get_value_ref_impl(float_number_type* /*unused*/) { + if (!is_float_number()) { throw fkyaml::type_error("The node value is not a floating point number.", m_node_type); } return m_node_value.float_val; @@ -10387,10 +9223,8 @@ class basic_node /// @brief Returns reference to the floating point number node value. /// @throw fkyaml::exception The node value is not a floating point number. /// @return Constant reference to the floating point number node value. - const float_number_type& get_value_ref_impl(const float_number_type* /*unused*/) const - { - if (!is_float_number()) - { + const float_number_type& get_value_ref_impl(const float_number_type* /*unused*/) const { + if (!is_float_number()) { throw fkyaml::type_error("The node value is not a floating point number.", m_node_type); } return m_node_value.float_val; @@ -10399,10 +9233,8 @@ class basic_node /// @brief Returns reference to the string node value. /// @throw fkyaml::exception The node value is not a string. /// @return Reference to the string node value. - string_type& get_value_ref_impl(string_type* /*unused*/) - { - if (!is_string()) - { + string_type& get_value_ref_impl(string_type* /*unused*/) { + if (!is_string()) { throw fkyaml::type_error("The node value is not a string.", m_node_type); } return *(m_node_value.p_string); @@ -10411,10 +9243,8 @@ class basic_node /// @brief Returns reference to the string node value. /// @throw fkyaml::exception The node value is not a string. /// @return Constant reference to the string node value. - const string_type& get_value_ref_impl(const string_type* /*unused*/) const - { - if (!is_string()) - { + const string_type& get_value_ref_impl(const string_type* /*unused*/) const { + if (!is_string()) { throw fkyaml::type_error("The node value is not a string.", m_node_type); } return *(m_node_value.p_string); @@ -10441,8 +9271,7 @@ template < inline void swap( basic_node& lhs, basic_node& - rhs) noexcept(noexcept(lhs.swap(rhs))) -{ + rhs) noexcept(noexcept(lhs.swap(rhs))) { lhs.swap(rhs); } @@ -10458,8 +9287,7 @@ template < inline std::ostream& operator<<( std::ostream& os, const basic_node& - n) -{ + n) { os << basic_node:: serialize(n); return os; @@ -10477,8 +9305,7 @@ template < template class ConverterType> inline std::istream& operator>>( std::istream& is, - basic_node& n) -{ + basic_node& n) { n = basic_node:: deserialize(is); return is; @@ -10489,19 +9316,16 @@ inline std::istream& operator>>( using node = basic_node<>; /// @brief namespace for user-defined literals for the fkYAML library. -inline namespace literals -{ +inline namespace literals { /// @brief namespace for user-defined literals for YAML node objects. -inline namespace yaml_literals -{ +inline namespace yaml_literals { /// @brief The user-defined string literal which deserializes a `char` array into a `node` object. /// @param s An input `char` array. /// @param n The size of `s`. /// @return The resulting `node` object deserialized from `s`. /// @sa https://fktn-k.github.io/fkYAML/api/operator_literal_yaml/ -inline fkyaml::node operator"" _yaml(const char* s, std::size_t n) -{ +inline fkyaml::node operator"" _yaml(const char* s, std::size_t n) { return fkyaml::node::deserialize((const char*)s, (const char*)s + n); } @@ -10510,8 +9334,7 @@ inline fkyaml::node operator"" _yaml(const char* s, std::size_t n) /// @param n The size of `s`. /// @return The resulting `node` object deserialized from `s`. /// @sa https://fktn-k.github.io/fkYAML/api/operator_literal_yaml/ -inline fkyaml::node operator"" _yaml(const char16_t* s, std::size_t n) -{ +inline fkyaml::node operator"" _yaml(const char16_t* s, std::size_t n) { return fkyaml::node::deserialize((const char16_t*)s, (const char16_t*)s + n); } @@ -10520,8 +9343,7 @@ inline fkyaml::node operator"" _yaml(const char16_t* s, std::size_t n) /// @param n The size of `s`. /// @return The resulting `node` object deserialized from `s`. /// @sa https://fktn-k.github.io/fkYAML/api/operator_literal_yaml/ -inline fkyaml::node operator"" _yaml(const char32_t* s, std::size_t n) -{ +inline fkyaml::node operator"" _yaml(const char32_t* s, std::size_t n) { return fkyaml::node::deserialize((const char32_t*)s, (const char32_t*)s + n); } @@ -10530,8 +9352,7 @@ inline fkyaml::node operator"" _yaml(const char32_t* s, std::size_t n) /// @param s An input `char8_t` array. /// @param n The size of `s`. /// @return The resulting `node` object deserialized from `s`. -inline fkyaml::node operator"" _yaml(const char8_t* s, std::size_t n) -{ +inline fkyaml::node operator"" _yaml(const char8_t* s, std::size_t n) { return fkyaml::node::deserialize((const char8_t*)s, (const char8_t*)s + n); } #endif diff --git a/test/cmake_add_subdirectory_test/project/main.cpp b/test/cmake_add_subdirectory_test/project/main.cpp index 5d59c6d2..d66044a1 100644 --- a/test/cmake_add_subdirectory_test/project/main.cpp +++ b/test/cmake_add_subdirectory_test/project/main.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,8 +10,7 @@ #include -int main() -{ +int main() { fkyaml::node node = fkyaml::node::deserialize("test: true"); std::cout << "test: " << node["test"].get_value() << std::endl; diff --git a/test/cmake_fetch_content_test/project/CMakeLists.txt b/test/cmake_fetch_content_test/project/CMakeLists.txt index efa36513..b3098768 100644 --- a/test/cmake_fetch_content_test/project/CMakeLists.txt +++ b/test/cmake_fetch_content_test/project/CMakeLists.txt @@ -6,7 +6,7 @@ include(FetchContent) FetchContent_Declare( fkYAML GIT_REPOSITORY https://github.com/fktn-k/fkYAML.git - GIT_TAG v0.3.4) + GIT_TAG v0.3.5) FetchContent_MakeAvailable(fkYAML) add_executable( diff --git a/test/cmake_fetch_content_test/project/main.cpp b/test/cmake_fetch_content_test/project/main.cpp index 5d59c6d2..d66044a1 100644 --- a/test/cmake_fetch_content_test/project/main.cpp +++ b/test/cmake_fetch_content_test/project/main.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,8 +10,7 @@ #include -int main() -{ +int main() { fkyaml::node node = fkyaml::node::deserialize("test: true"); std::cout << "test: " << node["test"].get_value() << std::endl; diff --git a/test/cmake_find_package_test/project/main.cpp b/test/cmake_find_package_test/project/main.cpp index 5d59c6d2..d66044a1 100644 --- a/test/cmake_find_package_test/project/main.cpp +++ b/test/cmake_find_package_test/project/main.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,8 +10,7 @@ #include -int main() -{ +int main() { fkyaml::node node = fkyaml::node::deserialize("test: true"); std::cout << "test: " << node["test"].get_value() << std::endl; diff --git a/test/cmake_target_include_directories_test/project/main.cpp b/test/cmake_target_include_directories_test/project/main.cpp index 5d59c6d2..d66044a1 100644 --- a/test/cmake_target_include_directories_test/project/main.cpp +++ b/test/cmake_target_include_directories_test/project/main.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,8 +10,7 @@ #include -int main() -{ +int main() { fkyaml::node node = fkyaml::node::deserialize("test: true"); std::cout << "test: " << node["test"].get_value() << std::endl; diff --git a/test/unit_test/CMakeLists.txt b/test/unit_test/CMakeLists.txt index 137164d5..6ec09554 100644 --- a/test/unit_test/CMakeLists.txt +++ b/test/unit_test/CMakeLists.txt @@ -209,7 +209,7 @@ add_executable( test_string_formatter.cpp test_tag_resolver_class.cpp test_uri_encoding_class.cpp - test_utf8_encoding_class.cpp + test_utf_encodings.cpp main.cpp ) diff --git a/test/unit_test/main.cpp b/test/unit_test/main.cpp index 59234cb8..7f5a8b71 100644 --- a/test/unit_test/main.cpp +++ b/test/unit_test/main.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani diff --git a/test/unit_test/test_custom_from_node.cpp b/test/unit_test/test_custom_from_node.cpp index e13644e4..fbde426b 100644 --- a/test/unit_test/test_custom_from_node.cpp +++ b/test/unit_test/test_custom_from_node.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -13,46 +13,38 @@ #include -namespace test -{ +namespace test { -struct novel -{ +struct novel { std::string title; std::string author; int year; }; -void from_node(const fkyaml::node& node, novel& novel) -{ +void from_node(const fkyaml::node& node, novel& novel) { novel.title = node["title"].get_value_ref(); novel.author = node["author"].get_value_ref(); novel.year = node["year"].get_value(); } -struct color -{ +struct color { int value; }; -bool operator<(const color& lhs, const color& rhs) -{ +bool operator<(const color& lhs, const color& rhs) { return lhs.value < rhs.value; } -void from_node(const fkyaml::node& node, color& color) -{ +void from_node(const fkyaml::node& node, color& color) { color.value = node["color"].get_value(); } -struct rgb -{ +struct rgb { int r; int g; int b; }; -void from_node(const fkyaml::node& node, rgb& rgb) -{ +void from_node(const fkyaml::node& node, rgb& rgb) { rgb.r = node["r"].get_value(); rgb.g = node["g"].get_value(); rgb.b = node["b"].get_value(); @@ -60,8 +52,7 @@ void from_node(const fkyaml::node& node, rgb& rgb) } // namespace test -TEST_CASE("FromNode_UserDefinedType") -{ +TEST_CASE("FromNode_UserDefinedType") { std::string input = "title: Robinson Crusoe\n" "author: Daniel Defoe\n" "year: 1678"; @@ -73,8 +64,7 @@ TEST_CASE("FromNode_UserDefinedType") REQUIRE(novel.year == 1678); } -TEST_CASE("FromNode_UserDefinedTypeVector") -{ +TEST_CASE("FromNode_UserDefinedTypeVector") { std::string input = "novels:\n" " - title: Robinson Crusoe\n" " author: Daniel Defoe\n" @@ -93,8 +83,7 @@ TEST_CASE("FromNode_UserDefinedTypeVector") REQUIRE(novels[1].year == 1818); } -TEST_CASE("FromNode_UserDefinedTypeVectorError") -{ +TEST_CASE("FromNode_UserDefinedTypeVectorError") { std::string input = "novels:\n" " - title: Robinson Crusoe\n" " author: Daniel Defoe\n" @@ -106,8 +95,7 @@ TEST_CASE("FromNode_UserDefinedTypeVectorError") REQUIRE_THROWS_AS(node.get_value>(), fkyaml::exception); } -TEST_CASE("FromNode_UserDefinedTypeMap") -{ +TEST_CASE("FromNode_UserDefinedTypeMap") { std::string input = "colors:\n" " ? color: 0xFFFFFF\n" " : r: 0xFF\n" @@ -138,8 +126,7 @@ TEST_CASE("FromNode_UserDefinedTypeMap") REQUIRE(colors.at(test::color {0x586776}).b == 0x76); } -TEST_CASE("FromNode_UserDefinedTypeMapError") -{ +TEST_CASE("FromNode_UserDefinedTypeMapError") { std::string input = "colors:\n" " ? color: 0xFFFFFF\n" " : r: 0xFF\n" diff --git a/test/unit_test/test_deserializer_class.cpp b/test/unit_test/test_deserializer_class.cpp index 85d06f57..4c7f08f0 100644 --- a/test/unit_test/test_deserializer_class.cpp +++ b/test/unit_test/test_deserializer_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,8 +10,7 @@ #include -TEST_CASE("Deserializer_EmptyInput") -{ +TEST_CASE("Deserializer_EmptyInput") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; @@ -20,13 +19,11 @@ TEST_CASE("Deserializer_EmptyInput") REQUIRE(root.empty()); } -TEST_CASE("Deserializer_KeySeparator") -{ +TEST_CASE("Deserializer_KeySeparator") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("normal key-value cases") - { + SECTION("normal key-value cases") { auto input_str = GENERATE( std::string("test: hoge"), std::string("test:\n foo: bar"), std::string("test:\n - foo\n - bar")); REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter(input_str))); @@ -34,16 +31,14 @@ TEST_CASE("Deserializer_KeySeparator") REQUIRE(root.size() == 1); } - SECTION("error cases") - { + SECTION("error cases") { auto input_str = GENERATE(std::string(": foo"), std::string("- : foo"), std::string("- - : foo")); REQUIRE_THROWS_AS( root = deserializer.deserialize(fkyaml::detail::input_adapter(input_str)), fkyaml::parse_error); } } -TEST_CASE("Deserializer_ValueSeparator") -{ +TEST_CASE("Deserializer_ValueSeparator") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; @@ -53,132 +48,111 @@ TEST_CASE("Deserializer_ValueSeparator") REQUIRE(root.size() == 1); } -TEST_CASE("Deserializer_NullValue") -{ +TEST_CASE("Deserializer_NullValue") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("key not in a sequence.") - { + SECTION("key not in a sequence.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("Null: test"))); REQUIRE(root.contains(nullptr)); } - SECTION("key in a sequence.") - { + SECTION("key in a sequence.") { auto input = GENERATE(std::string("test:\n - null: foo"), std::string("test:\n - null:\n - true")); REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter(input))); REQUIRE(root["test"][0].contains(nullptr)); } - SECTION("mapping value.") - { + SECTION("mapping value.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test: null"))); REQUIRE(root["test"].is_null()); } - SECTION("sequence value.") - { + SECTION("sequence value.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - null"))); REQUIRE(root["test"][0].is_null()); } } -TEST_CASE("Deserializer_BooleanValue") -{ +TEST_CASE("Deserializer_BooleanValue") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("key not in a sequence.") - { + SECTION("key not in a sequence.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("true: test"))); REQUIRE(root.contains(true)); } - SECTION("key in a sequence.") - { + SECTION("key in a sequence.") { auto input = GENERATE(std::string("test:\n - false: foo"), std::string("test:\n - false:\n - null")); REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter(input))); REQUIRE(root["test"][0].contains(false)); } - SECTION("mapping value.") - { + SECTION("mapping value.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test: TRUE"))); REQUIRE(root["test"].get_value() == true); } - SECTION("sequence value.") - { + SECTION("sequence value.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - False"))); REQUIRE(root["test"][0].get_value() == false); } } -TEST_CASE("Deserializer_IntegerKey") -{ +TEST_CASE("Deserializer_IntegerKey") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("key not in a sequence.") - { + SECTION("key not in a sequence.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("123: test"))); REQUIRE(root.contains(123)); } - SECTION("key in a sequence.") - { + SECTION("key in a sequence.") { auto input = GENERATE(std::string("test:\n - 123: foo"), std::string("test:\n - 123:\n - true")); REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter(input))); REQUIRE(root["test"][0].contains(123)); } - SECTION("mapping value.") - { + SECTION("mapping value.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test: 123"))); REQUIRE(root["test"].get_value() == 123); } - SECTION("sequence value.") - { + SECTION("sequence value.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - 123"))); REQUIRE(root["test"][0].get_value() == 123); } } -TEST_CASE("Deserializer_FloatingPointNumberKey") -{ +TEST_CASE("Deserializer_FloatingPointNumberKey") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("key not in a sequence.") - { + SECTION("key not in a sequence.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("3.14: test"))); REQUIRE(root.contains(3.14)); } - SECTION("key in a sequence.") - { + SECTION("key in a sequence.") { auto input = GENERATE(std::string("test:\n - .inf: foo"), std::string("test:\n - .inf:\n - true")); REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter(input))); REQUIRE(root["test"][0].contains(std::numeric_limits::infinity())); } - SECTION("mapping value.") - { + SECTION("mapping value.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test: .nan"))); REQUIRE(std::isnan(root["test"].get_value())); } - SECTION("sequence value.") - { + SECTION("sequence value.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - 1.23e-5"))); REQUIRE(root["test"][0].get_value() == 1.23e-5); } } -TEST_CASE("Deserializer_InvalidIndentation") -{ +TEST_CASE("Deserializer_InvalidIndentation") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; @@ -187,8 +161,7 @@ TEST_CASE("Deserializer_InvalidIndentation") fkyaml::parse_error); } -TEST_CASE("Deserializer_DuplicateKeys") -{ +TEST_CASE("Deserializer_DuplicateKeys") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; @@ -196,13 +169,11 @@ TEST_CASE("Deserializer_DuplicateKeys") root = deserializer.deserialize(fkyaml::detail::input_adapter("foo: bar\nfoo: baz")), fkyaml::parse_error); } -TEST_CASE("Deserializer_BlockSequence") -{ +TEST_CASE("Deserializer_BlockSequence") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("simple block sequence.") - { + SECTION("simple block sequence.") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - \'foo\'\n - bar"))); REQUIRE(root.is_mapping()); @@ -232,8 +203,7 @@ TEST_CASE("Deserializer_BlockSequence") REQUIRE(test_1_node.get_value_ref().compare("bar") == 0); } - SECTION("block sequence with nested mappings") - { + SECTION("block sequence with nested mappings") { REQUIRE_NOTHROW( root = deserializer.deserialize( fkyaml::detail::input_adapter("test:\n - foo: true\n bar: one\n - foo: false\n bar: two"))); @@ -285,8 +255,7 @@ TEST_CASE("Deserializer_BlockSequence") REQUIRE(test_1_bar_node.get_value_ref().compare("two") == 0); } - SECTION("block mapping with a comment in between") - { + SECTION("block mapping with a comment in between") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n # comment\n - item: 123"))); @@ -305,8 +274,7 @@ TEST_CASE("Deserializer_BlockSequence") REQUIRE(root["test"][0]["item"].get_value() == 123); } - SECTION("block mapping with a comment next to the key") - { + SECTION("block mapping with a comment next to the key") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("foo: # comment\n - bar\n"))); REQUIRE(root.is_mapping()); @@ -319,15 +287,29 @@ TEST_CASE("Deserializer_BlockSequence") REQUIRE(root["foo"][0].is_string()); REQUIRE(root["foo"][0].get_value_ref() == "bar"); } + + SECTION("root sequence") { + REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("- foo\n- 123\n- 3.14"))); + + REQUIRE(root.is_sequence()); + REQUIRE(root.size() == 3); + + REQUIRE(root[0].is_string()); + REQUIRE(root[0].get_value_ref() == "foo"); + + REQUIRE(root[1].is_integer()); + REQUIRE(root[1].get_value() == 123); + + REQUIRE(root[2].is_float_number()); + REQUIRE(root[2].get_value() == 3.14); + } } -TEST_CASE("Deserializer_BlockMapping") -{ +TEST_CASE("Deserializer_BlockMapping") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("simple block mapping") - { + SECTION("simple block mapping") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo: one\nbar: true\npi: 3.14"))); @@ -353,8 +335,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(pi_node.get_value() == 3.14); } - SECTION("nested block mapping") - { + SECTION("nested block mapping") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n bool: true\n foo: bar\n pi: 3.14"))); @@ -388,8 +369,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(test_pi_node.get_value() == 3.14); } - SECTION("block mapping with several nested children") - { + SECTION("block mapping with several nested children") { REQUIRE_NOTHROW( root = deserializer.deserialize( fkyaml::detail::input_adapter("foo:\n bar: baz\nqux: 123\nquux:\n corge: grault"))); @@ -416,8 +396,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["quux"]["corge"].get_value_ref() == "grault"); } - SECTION("block mapping with a more nested child") - { + SECTION("block mapping with a more nested child") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo:\n bar:\n baz: 123\nqux: true"))); @@ -441,8 +420,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["qux"].get_value() == true); } - SECTION("block mapping with a child block sequence") - { + SECTION("block mapping with a child block sequence") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo:\n - bar\n - 123\nbaz: qux"))); @@ -464,8 +442,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["baz"].get_value_ref() == "qux"); } - SECTION("block mapping with a block sequence of a single nested mapping") - { + SECTION("block mapping with a block sequence of a single nested mapping") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo:\n - bar: baz\nqux: corge"))); @@ -488,8 +465,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["qux"].get_value_ref() == "corge"); } - SECTION("block mapping with a block sequence of a block mapping with several key-value pairs") - { + SECTION("block mapping with a block sequence of a block mapping with several key-value pairs") { REQUIRE_NOTHROW( root = deserializer.deserialize( fkyaml::detail::input_adapter("foo:\n - bar: true\n baz: 123\nqux: corge"))); @@ -517,8 +493,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["qux"].get_value_ref() == "corge"); } - SECTION("block mapping with a block sequence of block mappings") - { + SECTION("block mapping with a block sequence of block mappings") { auto input_adapter = fkyaml::detail::input_adapter("stuff:\n" " - id: \"foo\"\n" " name: Foo\n" @@ -576,8 +551,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["stuff"][1]["name"].get_value_ref() == "Bar"); } - SECTION("block mapping with a block sequence of more nested block mappings") - { + SECTION("block mapping with a block sequence of more nested block mappings") { auto input_adapter = fkyaml::detail::input_adapter("stuff:\n" " - id: \"foo\"\n" " name: Foo\n" @@ -635,8 +609,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["stuff"][1]["name"].get_value_ref() == "Bar"); } - SECTION("block mapping with explicit block mappings") - { + SECTION("block mapping with explicit block mappings") { auto input_adapter = fkyaml::detail::input_adapter("null: 3.14\n" "foo:\n" " ? bar\n" @@ -687,8 +660,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root[key][1].get_value_ref() == "qux"); } - SECTION("block mapping with keys containing flow indicators") - { + SECTION("block mapping with keys containing flow indicators") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("Foo,Bar: true\nBaz[123]: 3.14"))); @@ -702,8 +674,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["Baz[123]"].get_value() == 3.14); } - SECTION("Flow indicators inside unquoted plain scalar values") - { + SECTION("Flow indicators inside unquoted plain scalar values") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("Foo: Bar, abc{abc"))); REQUIRE(root.is_mapping()); REQUIRE(root.size() == 1); @@ -743,8 +714,7 @@ TEST_CASE("Deserializer_BlockMapping") fkyaml::parse_error); } - SECTION("a comment right after a block mapping key.") - { + SECTION("a comment right after a block mapping key.") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("baz: # comment2\n qux: 123\n"))); @@ -760,8 +730,7 @@ TEST_CASE("Deserializer_BlockMapping") REQUIRE(root["baz"]["qux"].get_value() == 123); } - SECTION("mapping entries split across newlines") - { + SECTION("mapping entries split across newlines") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo:\n" " bar\n" @@ -802,13 +771,11 @@ TEST_CASE("Deserializer_BlockMapping") } } -TEST_CASE("Deserializer_FlowSequence") -{ +TEST_CASE("Deserializer_FlowSequence") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("simple flow sequence") - { + SECTION("simple flow sequence") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test: [ foo, bar ]"))); REQUIRE(root.is_mapping()); @@ -834,19 +801,31 @@ TEST_CASE("Deserializer_FlowSequence") REQUIRE(test_1_node.get_value_ref().compare("bar") == 0); } - SECTION("lack the beginning of a flow sequence") - { + SECTION("lack the beginning of a flow sequence") { REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter("test: ]")), fkyaml::parse_error); } + + SECTION("root flow sequence") { + REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("[foo,123,3.14]"))); + REQUIRE(root.is_sequence()); + REQUIRE(root.size() == 3); + + REQUIRE(root[0].is_string()); + REQUIRE(root[0].get_value_ref() == "foo"); + + REQUIRE(root[1].is_integer()); + REQUIRE(root[1].get_value() == 123); + + REQUIRE(root[2].is_float_number()); + REQUIRE(root[2].get_value() == 3.14); + } } -TEST_CASE("Deserializer_FlowMapping") -{ +TEST_CASE("Deserializer_FlowMapping") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("simple flow mapping") - { + SECTION("simple flow mapping") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("test: { bool: true, foo: bar, pi: 3.14 }"))); @@ -879,8 +858,7 @@ TEST_CASE("Deserializer_FlowMapping") REQUIRE(test_pi_node.get_value() == 3.14); } - SECTION("Correct traversal after deserializing flow mapping value") - { + SECTION("Correct traversal after deserializing flow mapping value") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("test: { foo: bar }\n" "sibling: a_string_val"))); @@ -907,13 +885,11 @@ TEST_CASE("Deserializer_FlowMapping") REQUIRE(sibling_node.get_value_ref().compare("a_string_val") == 0); } - SECTION("lack the beginning of a flow mapping") - { + SECTION("lack the beginning of a flow mapping") { REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter("test: }")), fkyaml::parse_error); } - SECTION("flow mapping with a flow sequence") - { + SECTION("flow mapping with a flow sequence") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test: {foo: [true,123]}"))); REQUIRE(root.is_mapping()); @@ -935,10 +911,24 @@ TEST_CASE("Deserializer_FlowMapping") REQUIRE(test_foo_node[1].is_integer()); REQUIRE(test_foo_node[1].get_value() == 123); } + + SECTION("root flow mapping") { + REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("{foo: 123, true: 3.14}"))); + + REQUIRE(root.is_mapping()); + REQUIRE(root.size() == 2); + REQUIRE(root.contains("foo")); + REQUIRE(root.contains(true)); + + REQUIRE(root["foo"].is_integer()); + REQUIRE(root["foo"].get_value() == 123); + + REQUIRE(root[true].is_float_number()); + REQUIRE(root[true].get_value() == 3.14); + } } -TEST_CASE("Deserializer_InputWithComment") -{ +TEST_CASE("Deserializer_InputWithComment") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; @@ -967,13 +957,11 @@ TEST_CASE("Deserializer_InputWithComment") REQUIRE(pi_node.get_value() == 3.14); } -TEST_CASE("Deserializer_YAMLVerDirective") -{ +TEST_CASE("Deserializer_YAMLVerDirective") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("YAML 1.1") - { + SECTION("YAML 1.1") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("%YAML 1.1\n---\nfoo: one"))); REQUIRE(root.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_1); @@ -988,8 +976,7 @@ TEST_CASE("Deserializer_YAMLVerDirective") REQUIRE(foo_node.get_value_ref().compare("one") == 0); } - SECTION("YAML 1.2") - { + SECTION("YAML 1.2") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("%YAML 1.2\n---\nfoo: one"))); REQUIRE(root.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_2); @@ -1004,8 +991,7 @@ TEST_CASE("Deserializer_YAMLVerDirective") REQUIRE(foo_node.get_value_ref().compare("one") == 0); } - SECTION("YAML directive in the content to be ignored") - { + SECTION("YAML directive in the content to be ignored") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo: bar\n%YAML 1.1\ntrue: 123"))); @@ -1022,20 +1008,17 @@ TEST_CASE("Deserializer_YAMLVerDirective") REQUIRE(root[true].get_value() == 123); } - SECTION("YAML directive more than once") - { + SECTION("YAML directive more than once") { REQUIRE_THROWS_AS( deserializer.deserialize(fkyaml::detail::input_adapter("%YAML 1.1\n%YAML 1.2\n")), fkyaml::parse_error); } } -TEST_CASE("Deserializer_TagDirective") -{ +TEST_CASE("Deserializer_TagDirective") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("primary tag handle") - { + SECTION("primary tag handle") { std::string input = "%TAG ! tag:test.com,2000:\n" "---\n" "foo: !local bar"; @@ -1051,8 +1034,7 @@ TEST_CASE("Deserializer_TagDirective") REQUIRE(root["foo"].get_tag_name() == "!local"); } - SECTION("primary tag handle more than once") - { + SECTION("primary tag handle more than once") { std::string input = "%TAG ! tag:test.com,2000:\n" "%TAG ! tag:test.com,2000:\n" "---\n" @@ -1060,8 +1042,7 @@ TEST_CASE("Deserializer_TagDirective") REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error); } - SECTION("secondary tag handle") - { + SECTION("secondary tag handle") { std::string input = "%TAG !! tag:test.com,2000:\n" "---\n" "foo: !!local bar"; @@ -1077,8 +1058,7 @@ TEST_CASE("Deserializer_TagDirective") REQUIRE(root["foo"].get_tag_name() == "!!local"); } - SECTION("secondary tag handle more than once") - { + SECTION("secondary tag handle more than once") { std::string input = "%TAG !! tag:test.com,2000:\n" "%TAG !! tag:test.com,2000:\n" "---\n" @@ -1086,8 +1066,7 @@ TEST_CASE("Deserializer_TagDirective") REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error); } - SECTION("named tag handles") - { + SECTION("named tag handles") { std::string input = "%TAG !e! tag:test.com,2000:\n" "%TAG !f! !foo-\n" "---\n" @@ -1111,8 +1090,7 @@ TEST_CASE("Deserializer_TagDirective") REQUIRE(root["baz"].get_tag_name() == "!f!local"); } - SECTION("named tag handle more than once") - { + SECTION("named tag handle more than once") { std::string input = "%TAG !e! tag:test.com,2000:\n" "%TAG !e! !foo-\n" "---\n" @@ -1121,8 +1099,7 @@ TEST_CASE("Deserializer_TagDirective") } } -TEST_CASE("Deserializer_InvalidDirective") -{ +TEST_CASE("Deserializer_InvalidDirective") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; @@ -1131,13 +1108,11 @@ TEST_CASE("Deserializer_InvalidDirective") REQUIRE(root.empty()); } -TEST_CASE("Deserializer_Anchor") -{ +TEST_CASE("Deserializer_Anchor") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("block sequence with anchored boolean scalar") - { + SECTION("block sequence with anchored boolean scalar") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - &anchor true\n - *anchor"))); @@ -1167,8 +1142,7 @@ TEST_CASE("Deserializer_Anchor") test_1_node.get_value() == test_0_node.get_value()); } - SECTION("block sequence with anchored integer scalar") - { + SECTION("block sequence with anchored integer scalar") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - &anchor -123\n - *anchor"))); @@ -1198,8 +1172,7 @@ TEST_CASE("Deserializer_Anchor") test_1_node.get_value() == test_0_node.get_value()); } - SECTION("block sequence with anchored floating point number scalar") - { + SECTION("block sequence with anchored floating point number scalar") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - &anchor 3.14\n - *anchor"))); @@ -1230,8 +1203,7 @@ TEST_CASE("Deserializer_Anchor") test_0_node.get_value()); } - SECTION("block sequence with anchored string scalar") - { + SECTION("block sequence with anchored string scalar") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n - &anchor foo\n - *anchor"))); @@ -1264,8 +1236,7 @@ TEST_CASE("Deserializer_Anchor") REQUIRE(test_1_node.get_value_ref().compare("foo") == 0); } - SECTION("block mapping with anchored boolean scalar") - { + SECTION("block mapping with anchored boolean scalar") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo: &anchor true\nbar: *anchor"))); @@ -1287,8 +1258,7 @@ TEST_CASE("Deserializer_Anchor") REQUIRE(bar_node.get_value() == foo_node.get_value()); } - SECTION("block mapping with anchored integer scalar") - { + SECTION("block mapping with anchored integer scalar") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo: &anchor -123\nbar: *anchor"))); @@ -1310,8 +1280,7 @@ TEST_CASE("Deserializer_Anchor") REQUIRE(bar_node.get_value() == foo_node.get_value()); } - SECTION("block mapping with anchored floating point number scalar") - { + SECTION("block mapping with anchored floating point number scalar") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo: &anchor 3.14\nbar: *anchor"))); @@ -1335,8 +1304,7 @@ TEST_CASE("Deserializer_Anchor") foo_node.get_value()); } - SECTION("block mapping with anchored string scalar") - { + SECTION("block mapping with anchored string scalar") { REQUIRE_NOTHROW( root = deserializer.deserialize(fkyaml::detail::input_adapter("foo: &anchor one\nbar: *anchor"))); @@ -1360,8 +1328,7 @@ TEST_CASE("Deserializer_Anchor") foo_node.get_value_ref()); } - SECTION("parse alias mapping key") - { + SECTION("parse alias mapping key") { REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("&anchor foo:\n *anchor: 123"))); REQUIRE(root.is_mapping()); @@ -1374,21 +1341,18 @@ TEST_CASE("Deserializer_Anchor") REQUIRE(root["foo"]["foo"].get_value() == 123); } - SECTION("multiple anchors specified") - { + SECTION("multiple anchors specified") { auto input = GENERATE(std::string("foo: &anchor &anchor2\n bar: baz"), std::string("&anchor &anchor2 foo: bar")); REQUIRE_THROWS_AS(root = deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error); } } -TEST_CASE("Deserializer_Tag") -{ +TEST_CASE("Deserializer_Tag") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("valid tags") - { + SECTION("valid tags") { std::string input = "str: !!str true\n" "int: ! 123\n" "nil: !!null null\n" @@ -1485,20 +1449,17 @@ TEST_CASE("Deserializer_Tag") REQUIRE(root["seq_flow"][1].get_value() == 3.14f); } - SECTION("multiple tags specified") - { + SECTION("multiple tags specified") { auto input = GENERATE(std::string("foo: !!map !!map\n bar: baz"), std::string("!!str !!bool true: 123")); REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter(input)), fkyaml::parse_error); } } -TEST_CASE("Deserializer_NodeProperties") -{ +TEST_CASE("Deserializer_NodeProperties") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; - SECTION("both tag and anchor specified") - { + SECTION("both tag and anchor specified") { auto input = GENERATE( std::string("foo: !!map &anchor\n bar: baz"), // tag -> anchor std::string("foo: &anchor !!map\n bar: baz") // anchor -> tag @@ -1521,21 +1482,18 @@ TEST_CASE("Deserializer_NodeProperties") REQUIRE(root["foo"]["bar"].get_value_ref() == "baz"); } - SECTION("alias node with tag") - { + SECTION("alias node with tag") { REQUIRE_THROWS_AS( deserializer.deserialize(fkyaml::detail::input_adapter("&anchor foo: !!str *anchor")), fkyaml::parse_error); } } -TEST_CASE("Deserializer_NoMachingAnchor") -{ +TEST_CASE("Deserializer_NoMachingAnchor") { fkyaml::detail::basic_deserializer deserializer; REQUIRE_THROWS_AS(deserializer.deserialize(fkyaml::detail::input_adapter("foo: *anchor")), fkyaml::parse_error); } -TEST_CASE("Deserializer_DocumentWithMarkers") -{ +TEST_CASE("Deserializer_DocumentWithMarkers") { fkyaml::detail::basic_deserializer deserializer; fkyaml::node root; diff --git a/test/unit_test/test_encode_detector.cpp b/test/unit_test/test_encode_detector.cpp index c5dc70ba..09303e0b 100644 --- a/test/unit_test/test_encode_detector.cpp +++ b/test/unit_test/test_encode_detector.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -16,22 +16,19 @@ #include #ifdef _MSC_VER - #define DISABLE_C4996 __pragma(warning(push)) __pragma(warning(disable : 4996)) - #define ENABLE_C4996 __pragma(warning(pop)) +#define DISABLE_C4996 __pragma(warning(push)) __pragma(warning(disable : 4996)) +#define ENABLE_C4996 __pragma(warning(pop)) #else - #define DISABLE_C4996 - #define ENABLE_C4996 +#define DISABLE_C4996 +#define ENABLE_C4996 #endif -TEST_CASE("EncodeDetector_DetectEncodingType") -{ - struct test_data_t - { +TEST_CASE("EncodeDetector_DetectEncodingType") { + struct test_data_t { test_data_t(std::array input_, fkyaml::detail::utf_encode_t encode_type_, bool has_bom_) : input(input_), encode_type(encode_type_), - has_bom(has_bom_) - { + has_bom(has_bom_) { } std::array input {}; @@ -88,14 +85,12 @@ TEST_CASE("EncodeDetector_DetectEncodingType") REQUIRE(has_bom == d.has_bom); } -TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") -{ +TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") { //////////////////////// // char iterators // //////////////////////// - SECTION("char iterators encoded in the UTF-8") - { + SECTION("char iterators encoded in the UTF-8") { std::string input {char(0x60u), char(0x61u), char(0x62u), char(0x63u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -104,8 +99,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char iterators encoded in the UTF-8(BOM)") - { + SECTION("char iterators encoded in the UTF-8(BOM)") { std::string input {char(0xEFu), char(0xBBu), char(0xBFu), char(0x60u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -114,8 +108,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input) + 3); } - SECTION("char iterators encoded in the UTF-16BE") - { + SECTION("char iterators encoded in the UTF-16BE") { std::string input {0, char(0x60u), 0, char(0x61u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -124,8 +117,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char iterators encoded in the UTF-16BE(BOM)") - { + SECTION("char iterators encoded in the UTF-16BE(BOM)") { std::string input {char(0xFEu), char(0xFFu), 0, char(0x60u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -134,8 +126,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input) + 2); } - SECTION("char iterators encoded in the UTF-16LE") - { + SECTION("char iterators encoded in the UTF-16LE") { std::string input {char(0x60u), 0, char(0x61u), 0}; auto begin = std::begin(input); auto end = std::end(input); @@ -144,8 +135,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char iterators encoded in the UTF-16LE(BOM)") - { + SECTION("char iterators encoded in the UTF-16LE(BOM)") { std::string input {char(0xFFu), char(0xFEu), char(0x60u), 0}; auto begin = std::begin(input); auto end = std::end(input); @@ -154,8 +144,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input) + 2); } - SECTION("char iterators encoded in the UTF-32BE") - { + SECTION("char iterators encoded in the UTF-32BE") { std::string input {0, 0, 0, char(0x60u), 0, 0, 0, char(0x61u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -164,8 +153,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char iterators encoded in the UTF-32BE(BOM)") - { + SECTION("char iterators encoded in the UTF-32BE(BOM)") { std::string input {0, 0, char(0xFEu), char(0xFFu), 0, 0, 0, char(0x60u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -174,8 +162,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input) + 4); } - SECTION("char iterators encoded in the UTF-32LE") - { + SECTION("char iterators encoded in the UTF-32LE") { std::string input {char(0x60u), 0, 0, 0, char(0x61u), 0, 0, 0}; auto begin = std::begin(input); auto end = std::end(input); @@ -184,8 +171,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char iterators encoded in the UTF-32LE(BOM)") - { + SECTION("char iterators encoded in the UTF-32LE(BOM)") { std::string input {char(0xFFu), char(0xFEu), 0, 0, char(0x60u), 0, 0, 0}; auto begin = std::begin(input); auto end = std::end(input); @@ -198,8 +184,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") // char16_t iterators // //////////////////////////// - SECTION("char16_t iterators encoded in the UTF-16BE") - { + SECTION("char16_t iterators encoded in the UTF-16BE") { std::u16string input {char16_t(0x0060u), char16_t(0x0061u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -208,8 +193,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char16_t iterators encoded in the UTF-16BE(BOM)") - { + SECTION("char16_t iterators encoded in the UTF-16BE(BOM)") { std::u16string input {char16_t(0xFEFFu), char16_t(0x0060u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -218,8 +202,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input) + 1); } - SECTION("char16_t iterators encoded in the UTF-16LE") - { + SECTION("char16_t iterators encoded in the UTF-16LE") { std::u16string input {char16_t(0x6000u), char16_t(0x6100u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -228,8 +211,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char16_t iterators encoded in the UTF-16LE(BOM)") - { + SECTION("char16_t iterators encoded in the UTF-16LE(BOM)") { std::u16string input {char16_t(0xFFFEu), char16_t(0x6000u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -238,8 +220,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input) + 1); } - SECTION("empty char16_t iterators") - { + SECTION("empty char16_t iterators") { std::u16string input = u""; auto begin = std::begin(input); auto end = std::end(input); @@ -248,8 +229,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char16_t iterators with invalid encoding") - { + SECTION("char16_t iterators with invalid encoding") { std::u16string input {char16_t(0x0000u), char16_t(0xFEFFu)}; auto begin = std::begin(input); auto end = std::end(input); @@ -260,8 +240,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") // char32_t iterators // //////////////////////////// - SECTION("char32_t iterators encoded in the UTF-32BE") - { + SECTION("char32_t iterators encoded in the UTF-32BE") { std::u32string input {char32_t(0x00000060u), char32_t(0x00000061u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -270,8 +249,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char32_t iterators encoded in the UTF-32BE(BOM)") - { + SECTION("char32_t iterators encoded in the UTF-32BE(BOM)") { std::u32string input {char32_t(0x0000FEFFu), char32_t(0x00000060u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -280,8 +258,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input) + 1); } - SECTION("char32_t iterators encoded in the UTF-32LE") - { + SECTION("char32_t iterators encoded in the UTF-32LE") { std::u32string input {char32_t(0x60000000u), char32_t(0x61000000u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -290,8 +267,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char32_t iterators encoded in the UTF-32LE(BOM)") - { + SECTION("char32_t iterators encoded in the UTF-32LE(BOM)") { std::u32string input {char32_t(0xFFFE0000u), char32_t(0x60000000u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -300,8 +276,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input) + 1); } - SECTION("empty char32_t iterators") - { + SECTION("empty char32_t iterators") { std::u32string input = U""; auto begin = std::begin(input); auto end = std::end(input); @@ -310,8 +285,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") REQUIRE(begin == std::begin(input)); } - SECTION("char32_t iterators with invalid encoding") - { + SECTION("char32_t iterators with invalid encoding") { std::u32string input {char32_t(0xFEFF0060u), char32_t(0x00610062u)}; auto begin = std::begin(input); auto end = std::end(input); @@ -322,8 +296,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") // FILE* object // ////////////////////// - SECTION("FILE* object with UTF-8 encoding") - { + SECTION("FILE* object with UTF-8 encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n.txt", "r"); ENABLE_C4996 @@ -336,8 +309,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-8(BOM) encoding") - { + SECTION("FILE* object with UTF-8(BOM) encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8bom.txt", "r"); ENABLE_C4996 @@ -350,8 +322,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-16BE encoding") - { + SECTION("FILE* object with UTF-16BE encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16ben.txt", "r"); ENABLE_C4996 @@ -364,8 +335,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-16BE(BOM) encoding") - { + SECTION("FILE* object with UTF-16BE(BOM) encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16bebom.txt", "r"); ENABLE_C4996 @@ -378,8 +348,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-16LE encoding") - { + SECTION("FILE* object with UTF-16LE encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16len.txt", "r"); ENABLE_C4996 @@ -392,8 +361,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-16LE(BOM) encoding") - { + SECTION("FILE* object with UTF-16LE(BOM) encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16lebom.txt", "r"); ENABLE_C4996 @@ -406,8 +374,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-32BE encoding") - { + SECTION("FILE* object with UTF-32BE encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32ben.txt", "r"); ENABLE_C4996 @@ -420,8 +387,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-32BE(BOM) encoding") - { + SECTION("FILE* object with UTF-32BE(BOM) encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32bebom.txt", "r"); ENABLE_C4996 @@ -434,8 +400,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-32LE encoding") - { + SECTION("FILE* object with UTF-32LE encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32len.txt", "r"); ENABLE_C4996 @@ -448,8 +413,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with UTF-32LE(BOM) encoding") - { + SECTION("FILE* object with UTF-32LE(BOM) encoding") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32lebom.txt", "r"); ENABLE_C4996 @@ -462,8 +426,7 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") std::fclose(p_file); } - SECTION("FILE* object with an empty input file") - { + SECTION("FILE* object with an empty input file") { DISABLE_C4996 std::FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/single_char_byte_input.txt", "r"); ENABLE_C4996 @@ -480,88 +443,77 @@ TEST_CASE("EncodeDetector_DetectEncodingAndSkipBom") // std::istream object // ///////////////////////////// - SECTION("std::istream with UTF-8 encoding") - { + SECTION("std::istream with UTF-8 encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_8); REQUIRE(ifs.tellg() == 0); } - SECTION("std::istream with UTF-8(BOM) encoding") - { + SECTION("std::istream with UTF-8(BOM) encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8bom.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_8); REQUIRE(ifs.tellg() == 3); } - SECTION("std::istream with UTF-16BE encoding") - { + SECTION("std::istream with UTF-16BE encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16ben.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_16BE); REQUIRE(ifs.tellg() == 0); } - SECTION("std::istream with UTF-16BE(BOM) encoding") - { + SECTION("std::istream with UTF-16BE(BOM) encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16bebom.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_16BE); REQUIRE(ifs.tellg() == 2); } - SECTION("std::istream with UTF-16LE encoding") - { + SECTION("std::istream with UTF-16LE encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16len.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_16LE); REQUIRE(ifs.tellg() == 0); } - SECTION("std::istream with UTF-16LE(BOM) encoding") - { + SECTION("std::istream with UTF-16LE(BOM) encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16lebom.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_16LE); REQUIRE(ifs.tellg() == 2); } - SECTION("std::istream with UTF-32BE encoding") - { + SECTION("std::istream with UTF-32BE encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32ben.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_32BE); REQUIRE(ifs.tellg() == 0); } - SECTION("std::istream with UTF-32BE(BOM) encoding") - { + SECTION("std::istream with UTF-32BE(BOM) encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32bebom.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_32BE); REQUIRE(ifs.tellg() == 4); } - SECTION("std::istream with UTF-32LE encoding") - { + SECTION("std::istream with UTF-32LE encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32len.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_32LE); REQUIRE(ifs.tellg() == 0); } - SECTION("std::istream with UTF-32LE(BOM) encoding") - { + SECTION("std::istream with UTF-32LE(BOM) encoding") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32lebom.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_32LE); REQUIRE(ifs.tellg() == 4); } - SECTION("std::istream with an empty input file") - { + SECTION("std::istream with an empty input file") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/single_char_byte_input.txt"); fkyaml::detail::utf_encode_t ret = fkyaml::detail::detect_encoding_and_skip_bom(ifs); REQUIRE(ret == fkyaml::detail::utf_encode_t::UTF_8); diff --git a/test/unit_test/test_exception_class.cpp b/test/unit_test/test_exception_class.cpp index ed6ec682..4e418793 100644 --- a/test/unit_test/test_exception_class.cpp +++ b/test/unit_test/test_exception_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -12,23 +12,19 @@ #include -TEST_CASE("Exception_DefaultCtor") -{ +TEST_CASE("Exception_DefaultCtor") { fkyaml::exception exception; REQUIRE(std::string(exception.what()).empty()); } -TEST_CASE("Exception_CtorWithMessage") -{ - SECTION("non-null message.") - { +TEST_CASE("Exception_CtorWithMessage") { + SECTION("non-null message.") { const char* message = "test error message."; fkyaml::exception exception(message); REQUIRE(std::string(exception.what()).compare(message) == 0); } - SECTION("null message.") - { + SECTION("null message.") { const char* message = nullptr; fkyaml::exception exception(message); REQUIRE(std::string(exception.what()).empty()); diff --git a/test/unit_test/test_from_string.cpp b/test/unit_test/test_from_string.cpp index b37850d8..0e63102a 100644 --- a/test/unit_test/test_from_string.cpp +++ b/test/unit_test/test_from_string.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -14,47 +14,38 @@ #include -TEST_CASE("FromString_Null") -{ - SECTION("valid string for the null value") - { +TEST_CASE("FromString_Null") { + SECTION("valid string for the null value") { auto input = GENERATE(std::string("null"), std::string("Null"), std::string("NULL"), std::string("~")); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == nullptr); } - SECTION("invalid string for the null value") - { + SECTION("invalid string for the null value") { std::string input("test"); REQUIRE_THROWS_AS( fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } } -TEST_CASE("FromString_Bool") -{ - SECTION("valid string for the true value") - { +TEST_CASE("FromString_Bool") { + SECTION("valid string for the true value") { auto input = GENERATE(std::string("true"), std::string("True"), std::string("TRUE")); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == true); } - SECTION("valid string for the false value") - { + SECTION("valid string for the false value") { auto input = GENERATE(std::string("false"), std::string("False"), std::string("FALSE")); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == false); } - SECTION("invalid string for the boolean values") - { + SECTION("invalid string for the boolean values") { std::string input("test"); REQUIRE_THROWS_AS(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } } -TEST_CASE("FromString_Integer") -{ - SECTION("char type") - { +TEST_CASE("FromString_Integer") { + SECTION("char type") { std::string input("-64"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == -64); @@ -62,8 +53,7 @@ TEST_CASE("FromString_Integer") REQUIRE_THROWS_AS(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("unsigned char type") - { + SECTION("unsigned char type") { std::string input("64"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == 64); @@ -72,8 +62,7 @@ TEST_CASE("FromString_Integer") fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("short type") - { + SECTION("short type") { std::string input("-15464"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == -15464); @@ -81,8 +70,7 @@ TEST_CASE("FromString_Integer") REQUIRE_THROWS_AS(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("unsigned short type") - { + SECTION("unsigned short type") { std::string input("15464"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == 15464); @@ -91,8 +79,7 @@ TEST_CASE("FromString_Integer") fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("int type") - { + SECTION("int type") { std::string input("-1154357464"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == -1154357464); @@ -100,8 +87,7 @@ TEST_CASE("FromString_Integer") REQUIRE_THROWS_AS(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("unsigned int type") - { + SECTION("unsigned int type") { std::string input("3154357464"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == 3154357464u); @@ -110,8 +96,7 @@ TEST_CASE("FromString_Integer") fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("long type") - { + SECTION("long type") { std::string input("-1154357464"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == -1154357464l); @@ -119,8 +104,7 @@ TEST_CASE("FromString_Integer") REQUIRE_THROWS_AS(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("unsigned long type") - { + SECTION("unsigned long type") { std::string input("317464"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == 317464ul); @@ -129,8 +113,7 @@ TEST_CASE("FromString_Integer") fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("long long type") - { + SECTION("long long type") { std::string input("-1154357464"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == -1154357464ll); @@ -139,8 +122,7 @@ TEST_CASE("FromString_Integer") fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}), fkyaml::exception); } - SECTION("unsigned long long type") - { + SECTION("unsigned long long type") { std::string input("3154357464"); REQUIRE(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == 3154357464ull); @@ -150,34 +132,29 @@ TEST_CASE("FromString_Integer") } } -TEST_CASE("FromString_Float") -{ - SECTION("positive infinity") - { +TEST_CASE("FromString_Float") { + SECTION("positive infinity") { auto input = GENERATE(std::string(".inf"), std::string(".Inf"), std::string(".INF")); REQUIRE( fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == std::numeric_limits::infinity()); } - SECTION("negative infinity") - { + SECTION("negative infinity") { auto input = GENERATE(std::string("-.inf"), std::string("-.Inf"), std::string("-.INF")); REQUIRE( fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == -1 * std::numeric_limits::infinity()); } - SECTION("NaN") - { + SECTION("NaN") { auto input = GENERATE(std::string(".nan"), std::string(".NaN"), std::string(".NAN")); float ret = 0.0f; REQUIRE_NOTHROW(ret = fkyaml::detail::from_string(input, fkyaml::detail::type_tag {})); REQUIRE(std::isnan(ret)); } - SECTION("valid string for a float value") - { + SECTION("valid string for a float value") { std::string input("3.14"); REQUIRE(std::abs(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) - 3.14f) < FLT_EPSILON); @@ -186,34 +163,29 @@ TEST_CASE("FromString_Float") } } -TEST_CASE("FromString_Double") -{ - SECTION("positive infinity") - { +TEST_CASE("FromString_Double") { + SECTION("positive infinity") { auto input = GENERATE(std::string(".inf"), std::string(".Inf"), std::string(".INF")); REQUIRE( fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == std::numeric_limits::infinity()); } - SECTION("negative infinity") - { + SECTION("negative infinity") { auto input = GENERATE(std::string("-.inf"), std::string("-.Inf"), std::string("-.INF")); REQUIRE( fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) == -1 * std::numeric_limits::infinity()); } - SECTION("NaN") - { + SECTION("NaN") { auto input = GENERATE(std::string(".nan"), std::string(".NaN"), std::string(".NAN")); double ret = 0.0; REQUIRE_NOTHROW(ret = fkyaml::detail::from_string(input, fkyaml::detail::type_tag {})); REQUIRE(std::isnan(ret)); } - SECTION("valid string for a double value") - { + SECTION("valid string for a double value") { std::string input("3.14"); REQUIRE(std::abs(fkyaml::detail::from_string(input, fkyaml::detail::type_tag {}) - 3.14) < DBL_EPSILON); diff --git a/test/unit_test/test_input_adapter.cpp b/test/unit_test/test_input_adapter.cpp index 1bca190c..392f76d0 100644 --- a/test/unit_test/test_input_adapter.cpp +++ b/test/unit_test/test_input_adapter.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -18,15 +18,15 @@ #include #ifdef FK_YAML_HAS_CXX_17 - #include +#include #endif #ifdef _MSC_VER - #define DISABLE_C4996 __pragma(warning(push)) __pragma(warning(disable : 4996)) - #define ENABLE_C4996 __pragma(warning(pop)) +#define DISABLE_C4996 __pragma(warning(push)) __pragma(warning(disable : 4996)) +#define ENABLE_C4996 __pragma(warning(pop)) #else - #define DISABLE_C4996 - #define ENABLE_C4996 +#define DISABLE_C4996 +#define ENABLE_C4996 #endif namespace /* file-scoped global variable for test cases */ @@ -36,54 +36,46 @@ constexpr char input_file_path[] = FK_YAML_TEST_DATA_DIR "/input_adapter_test_da } // namespace -TEST_CASE("InputAdapter_IteratorInputAdapterProvider") -{ +TEST_CASE("InputAdapter_IteratorInputAdapterProvider") { char input[] = "test"; - SECTION("C-style char array") - { + SECTION("C-style char array") { auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); } - SECTION("char pointers for beginning/end") - { + SECTION("char pointers for beginning/end") { auto input_adapter = fkyaml::detail::input_adapter(&input[0], &input[sizeof(input) - 1]); REQUIRE(std::is_same>::value); } - SECTION("C-style char16_t array") - { + SECTION("C-style char16_t array") { char16_t input16[] = u"test"; auto input_adapter = fkyaml::detail::input_adapter(input16); REQUIRE(std::is_same>::value); } - SECTION("C-style char32_t array") - { + SECTION("C-style char32_t array") { char32_t intput32[] = U"test"; auto input_adapter = fkyaml::detail::input_adapter(intput32); REQUIRE(std::is_same>::value); } - SECTION("std::string") - { + SECTION("std::string") { std::string input_str(input); auto input_adapter = fkyaml::detail::input_adapter(input_str); using iterator_type = typename std::string::iterator; REQUIRE(std::is_same>::value); } - SECTION("std::u16string") - { + SECTION("std::u16string") { std::u16string input_str(u"test"); auto input_adapter = fkyaml::detail::input_adapter(input_str); using iterator_type = typename std::u16string::iterator; REQUIRE(std::is_same>::value); } - SECTION("std::u32string") - { + SECTION("std::u32string") { std::u32string input_str(U"test"); auto input_adapter = fkyaml::detail::input_adapter(input_str); using iterator_type = typename std::u32string::iterator; @@ -91,16 +83,14 @@ TEST_CASE("InputAdapter_IteratorInputAdapterProvider") } #ifdef FK_YAML_HAS_CXX_17 - SECTION("std::string_view") - { + SECTION("std::string_view") { std::string_view input_str_view(input); auto input_adapter = fkyaml::detail::input_adapter(input_str_view); using iterator_type = typename std::string_view::iterator; REQUIRE(std::is_same>::value); } - SECTION("std::u16string_view") - { + SECTION("std::u16string_view") { using namespace std::string_view_literals; std::u16string_view input_str_view = u"test"sv; auto input_adapter = fkyaml::detail::input_adapter(input_str_view); @@ -108,8 +98,7 @@ TEST_CASE("InputAdapter_IteratorInputAdapterProvider") REQUIRE(std::is_same>::value); } - SECTION("std::u32string_view") - { + SECTION("std::u32string_view") { using namespace std::string_view_literals; std::u32string_view input_str_view = U"test"sv; auto input_adapter = fkyaml::detail::input_adapter(input_str_view); @@ -119,16 +108,14 @@ TEST_CASE("InputAdapter_IteratorInputAdapterProvider") #endif #ifdef FK_YAML_HAS_CXX_20 - SECTION("std::u32string") - { + SECTION("std::u32string") { std::u8string input_str(u8"test"); auto input_adapter = fkyaml::detail::input_adapter(input_str); using iterator_type = typename std::u8string::iterator; REQUIRE(std::is_same>::value); } - SECTION("std::u8string_view") - { + SECTION("std::u8string_view") { using namespace std::string_view_literals; std::u8string_view input_str_view = u8"test"sv; auto input_adapter = fkyaml::detail::input_adapter(input_str_view); @@ -138,16 +125,13 @@ TEST_CASE("InputAdapter_IteratorInputAdapterProvider") #endif } -TEST_CASE("InputAdapter_FileInputAdapterProvider") -{ - SECTION("invalid FILE object pointer") - { +TEST_CASE("InputAdapter_FileInputAdapterProvider") { + SECTION("invalid FILE object pointer") { FILE* p_file = nullptr; REQUIRE_THROWS_AS(fkyaml::detail::input_adapter(p_file), fkyaml::exception); } - SECTION("valid FILE object pointer") - { + SECTION("valid FILE object pointer") { DISABLE_C4996 FILE* p_file = std::fopen(input_file_path, "r"); ENABLE_C4996 @@ -160,22 +144,19 @@ TEST_CASE("InputAdapter_FileInputAdapterProvider") } } -TEST_CASE("InputAdapter_StreamInputAdapterProvider") -{ +TEST_CASE("InputAdapter_StreamInputAdapterProvider") { std::ifstream ifs(input_file_path); REQUIRE(ifs); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); } -TEST_CASE("InputAdapter_FillBuffer") -{ +TEST_CASE("InputAdapter_FillBuffer") { /////////////// // UTF-8 // /////////////// - SECTION("iterator_input_adapter for UTF-8 with a char array") - { + SECTION("iterator_input_adapter for UTF-8 with a char array") { char input[] = "test source."; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -198,8 +179,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[11] == '.'); } - SECTION("iterator_input_adapter for UTF-8 with std::string") - { + SECTION("iterator_input_adapter for UTF-8 with std::string") { std::string input = "test source."; auto input_adapter = fkyaml::detail::input_adapter(input); using itr_type = typename std::string::iterator; @@ -223,8 +203,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[11] == '.'); } - SECTION("file_input_adapter for UTF-8") - { + SECTION("file_input_adapter for UTF-8") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n.txt", "r"); ENABLE_C4996 @@ -248,8 +227,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-8") - { + SECTION("stream_input_adapter for UTF-8") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -272,8 +250,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-8(BOM) // //////////////////// - SECTION("iterator_input_adapter for UTF-8(BOM) with a char array") - { + SECTION("iterator_input_adapter for UTF-8(BOM) with a char array") { char input[] = { char(0xEFu), char(0xBBu), char(0xBFu), 't', 'e', 's', 't', ' ', 's', 'o', 'u', 'r', 'c', 'e', '.', 0}; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -297,8 +274,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[11] == '.'); } - SECTION("iterator_input_adapter for UTF-8(BOM) with std::string") - { + SECTION("iterator_input_adapter for UTF-8(BOM) with std::string") { char raw_input[] = { char(0xEFu), char(0xBBu), char(0xBFu), 't', 'e', 's', 't', ' ', 's', 'o', 'u', 'r', 'c', 'e', '.', 0}; std::string input = raw_input; @@ -324,8 +300,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[11] == '.'); } - SECTION("file_input_adapter for UTF-8(BOM)") - { + SECTION("file_input_adapter for UTF-8(BOM)") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8bom.txt", "r"); ENABLE_C4996 @@ -349,8 +324,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-8(BOM)") - { + SECTION("stream_input_adapter for UTF-8(BOM)") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8bom.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -373,8 +347,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-16BE // ////////////////// - SECTION("iterator_input_adapter for UTF-16BE with a char array") - { + SECTION("iterator_input_adapter for UTF-16BE with a char array") { char input[] = {0, 0x61, 0x30, 0x42, char(0xD8u), 0x40, char(0xDCu), 0x0B, 0, 0x52, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -394,8 +367,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16BE with std::string") - { + SECTION("iterator_input_adapter for UTF-16BE with std::string") { std::string input {0, 0x61, 0x30, 0x42, char(0xD8u), 0x40, char(0xDCu), 0x0B, 0, 0x52, 0, 0x5A}; auto input_adapter = fkyaml::detail::input_adapter(input); using itr_type = typename std::string::iterator; @@ -417,8 +389,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[9] == char(0x5Au)); } - SECTION("iterator_input_adapter for UTF-16BE with a char16_t array") - { + SECTION("iterator_input_adapter for UTF-16BE with a char16_t array") { char16_t input[] = {0x0061u, 0x3042u, 0xD840u, 0xDC0Bu, 0x0052u, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -438,8 +409,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16BE with std::u16string") - { + SECTION("iterator_input_adapter for UTF-16BE with std::u16string") { char16_t raw_input[] = {0x0061u, 0x3042u, 0xD840u, 0xDC0Bu, 0x0052u, 0x005Au, 0}; std::u16string input = raw_input; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -462,8 +432,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[9] == char(0x5Au)); } - SECTION("file_input_adapter for UTF-16BE") - { + SECTION("file_input_adapter for UTF-16BE") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16ben.txt", "r"); ENABLE_C4996 @@ -488,8 +457,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-16BE") - { + SECTION("stream_input_adapter for UTF-16BE") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16ben.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -513,8 +481,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-16BE(BOM) // /////////////////////// - SECTION("iterator_input_adapter for UTF-16BE(BOM) with a char array") - { + SECTION("iterator_input_adapter for UTF-16BE(BOM) with a char array") { char input[] = { char(0xFEu), char(0xFFu), 0, 0x61, 0x30, 0x42, char(0xD8u), 0x40, char(0xDCu), 0x0B, 0, 0x52, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -535,8 +502,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16BE(BOM) with std::string") - { + SECTION("iterator_input_adapter for UTF-16BE(BOM) with std::string") { std::string input { char(0xFEu), char(0xFFu), 0, 0x61, 0x30, 0x42, char(0xD8u), 0x40, char(0xDCu), 0x0B, 0, 0x52}; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -558,8 +524,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16BE(BOM) with a char16_t array") - { + SECTION("iterator_input_adapter for UTF-16BE(BOM) with a char16_t array") { char16_t input[] = {0xFEFFu, 0x0061u, 0x3042u, 0xD840u, 0xDC0Bu, 0x0052u, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -579,8 +544,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16BE(BOM) with std::u16string") - { + SECTION("iterator_input_adapter for UTF-16BE(BOM) with std::u16string") { char16_t raw_input[] = {0xFEFFu, 0x0061u, 0x3042u, 0xD840u, 0xDC0Bu, 0x0052u, 0}; std::u16string input = raw_input; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -602,8 +566,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("file_input_adapter for UTF-16BE(BOM)") - { + SECTION("file_input_adapter for UTF-16BE(BOM)") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16bebom.txt", "r"); ENABLE_C4996 @@ -628,8 +591,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-16BE(BOM)") - { + SECTION("stream_input_adapter for UTF-16BE(BOM)") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16bebom.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -652,8 +614,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-16LE // ////////////////// - SECTION("iterator_input_adapter for UTF-16LE with a char array") - { + SECTION("iterator_input_adapter for UTF-16LE with a char array") { char input[] = {0x61, 0, 0x42, 0x30, 0x40, char(0xD8u), 0x0B, char(0xDCu), 0x52, 0, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -673,8 +634,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16LE with std::string") - { + SECTION("iterator_input_adapter for UTF-16LE with std::string") { std::string input {0x61, 0, 0x42, 0x30, 0x40, char(0xD8u), 0x0B, char(0xDCu), 0x52, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); using itr_type = typename std::string::iterator; @@ -695,8 +655,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16LE with a char16_t array") - { + SECTION("iterator_input_adapter for UTF-16LE with a char16_t array") { char16_t input[] = {0x6100u, 0x4230u, 0x40D8u, 0x0BDCu, 0x5200u, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -716,8 +675,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16LE with std::u16string") - { + SECTION("iterator_input_adapter for UTF-16LE with std::u16string") { char16_t raw_input[] = {0x6100u, 0x4230u, 0x40D8u, 0x0BDCu, 0x5200u, 0}; std::u16string input = raw_input; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -739,8 +697,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("file_input_adapter for UTF-16LE") - { + SECTION("file_input_adapter for UTF-16LE") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16len.txt", "r"); ENABLE_C4996 @@ -765,8 +722,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-16LE") - { + SECTION("stream_input_adapter for UTF-16LE") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16len.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -790,8 +746,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-16LE(BOM) // /////////////////////// - SECTION("iterator_input_adapter for UTF-16LE(BOM) with a char array") - { + SECTION("iterator_input_adapter for UTF-16LE(BOM) with a char array") { char input[] = { char(0xFFu), char(0xFEu), 0x61, 0, 0x42, 0x30, 0x40, char(0xD8u), 0x0B, char(0xDCu), 0x52, 0, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -812,8 +767,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16LE(BOM) with std::string") - { + SECTION("iterator_input_adapter for UTF-16LE(BOM) with std::string") { std::string input { char(0xFFu), char(0xFEu), 0x61, 0, 0x42, 0x30, 0x40, char(0xD8u), 0x0B, char(0xDCu), 0x52, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -835,8 +789,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16LE(BOM) with a char16_t array") - { + SECTION("iterator_input_adapter for UTF-16LE(BOM) with a char16_t array") { char16_t input[] = {0xFFFEu, 0x6100u, 0x4230u, 0x40D8u, 0x0BDCu, 0x5200u, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -856,8 +809,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("iterator_input_adapter for UTF-16LE(BOM) with std::u16string") - { + SECTION("iterator_input_adapter for UTF-16LE(BOM) with std::u16string") { char16_t raw_input[] = {0xFFFEu, 0x6100u, 0x4230u, 0x40D8u, 0x0BDCu, 0x5200u, 0}; std::u16string input = raw_input; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -879,8 +831,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[8] == char(0x52u)); } - SECTION("file_input_adapter for UTF-16LE(BOM)") - { + SECTION("file_input_adapter for UTF-16LE(BOM)") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16lebom.txt", "r"); ENABLE_C4996 @@ -905,8 +856,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-16LE(BOM)") - { + SECTION("stream_input_adapter for UTF-16LE(BOM)") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf16lebom.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -930,8 +880,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-32BE // ////////////////// - SECTION("iterator_input_adapter for UTF-32BE with a char array") - { + SECTION("iterator_input_adapter for UTF-32BE with a char array") { char input[] = {0, 0, 0, 0x61, 0, 0, 0x30, 0x42, 0, 0x02, 0, 0x0B, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -950,8 +899,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32BE with std::string") - { + SECTION("iterator_input_adapter for UTF-32BE with std::string") { std::string input {0, 0, 0, 0x61, 0, 0, 0x30, 0x42, 0, 0x02, 0, 0x0B}; auto input_adapter = fkyaml::detail::input_adapter(input); using itr_type = typename std::string::iterator; @@ -971,8 +919,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32BE with a char32_t array") - { + SECTION("iterator_input_adapter for UTF-32BE with a char32_t array") { char32_t input[] = {0x00000061u, 0x00003042u, 0x0002000Bu, 0x00000000}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -991,8 +938,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32BE with std::u32string") - { + SECTION("iterator_input_adapter for UTF-32BE with std::u32string") { char32_t raw_input[] = {0x00000061u, 0x00003042u, 0x0002000Bu, 0x00000000}; std::u32string input = raw_input; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -1013,8 +959,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("file_input_adapter for UTF-32BE") - { + SECTION("file_input_adapter for UTF-32BE") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32ben.txt", "r"); ENABLE_C4996 @@ -1038,8 +983,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-32BE") - { + SECTION("stream_input_adapter for UTF-32BE") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32ben.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1062,8 +1006,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-32BE(BOM) // /////////////////////// - SECTION("iterator_input_adapter for UTF-32BE(BOM) with a char array") - { + SECTION("iterator_input_adapter for UTF-32BE(BOM) with a char array") { char input[] = {0, 0, char(0xFEu), char(0xFFu), 0, 0, 0, 0x61, 0, 0, 0x30, 0x42, 0, 0x02, 0, 0x0B, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -1082,8 +1025,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32BE(BOM) with std::string") - { + SECTION("iterator_input_adapter for UTF-32BE(BOM) with std::string") { std::string input {0, 0, char(0xFEu), char(0xFFu), 0, 0, 0, 0x61, 0, 0, 0x30, 0x42, 0, 0x02, 0, 0x0B}; auto input_adapter = fkyaml::detail::input_adapter(input); using itr_type = typename std::string::iterator; @@ -1103,8 +1045,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32BE(BOM) with a char32_t array") - { + SECTION("iterator_input_adapter for UTF-32BE(BOM) with a char32_t array") { char32_t input[] = {0x0000FEFFu, 0x00000061u, 0x00003042u, 0x0002000Bu, 0x00000000}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -1123,8 +1064,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32BE(BOM) with std::u32string") - { + SECTION("iterator_input_adapter for UTF-32BE(BOM) with std::u32string") { char32_t raw_input[] = {0x0000FEFFu, 0x00000061u, 0x00003042u, 0x0002000Bu, 0x00000000}; std::u32string input = raw_input; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -1145,8 +1085,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("file_input_adapter for UTF-32BE(BOM)") - { + SECTION("file_input_adapter for UTF-32BE(BOM)") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32bebom.txt", "r"); ENABLE_C4996 @@ -1170,8 +1109,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-32BE(BOM)") - { + SECTION("stream_input_adapter for UTF-32BE(BOM)") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32bebom.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1194,8 +1132,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-32LE // ////////////////// - SECTION("iterator_input_adapter for UTF-32LE with a char array") - { + SECTION("iterator_input_adapter for UTF-32LE with a char array") { char input[] = {0x61, 0, 0, 0, 0x42, 0x30, 0, 0, 0x0B, 0, 0x02, 0, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -1214,8 +1151,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32LE with std::string") - { + SECTION("iterator_input_adapter for UTF-32LE with std::string") { std::string input {0x61, 0, 0, 0, 0x42, 0x30, 0, 0, 0x0B, 0, 0x02, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); using itr_type = typename std::string::iterator; @@ -1235,8 +1171,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32LE with a char32_t array") - { + SECTION("iterator_input_adapter for UTF-32LE with a char32_t array") { char32_t input[] = {0x61000000u, 0x42300000u, 0x0B000200u, 0x00000000}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -1255,8 +1190,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32LE with std::u32string") - { + SECTION("iterator_input_adapter for UTF-32LE with std::u32string") { char32_t raw_input[] = {0x61000000u, 0x42300000u, 0x0B000200u, 0x00000000}; std::u32string input = raw_input; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -1277,8 +1211,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("file_input_adapter for UTF-32LE") - { + SECTION("file_input_adapter for UTF-32LE") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32len.txt", "r"); ENABLE_C4996 @@ -1302,8 +1235,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-32LE") - { + SECTION("stream_input_adapter for UTF-32LE") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32len.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1326,8 +1258,7 @@ TEST_CASE("InputAdapter_FillBuffer") // UTF-32LE(BOM) // /////////////////////// - SECTION("iterator_input_adapter for UTF-32LE(BOM) with a char array") - { + SECTION("iterator_input_adapter for UTF-32LE(BOM) with a char array") { char input[] = {char(0xFFu), char(0xFEu), 0, 0, 0x61, 0, 0, 0, 0x42, 0x30, 0, 0, 0x0B, 0, 0x02, 0, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -1346,8 +1277,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32LE(BOM) with std::string") - { + SECTION("iterator_input_adapter for UTF-32LE(BOM) with std::string") { std::string input {char(0xFFu), char(0xFEu), 0, 0, 0x61, 0, 0, 0, 0x42, 0x30, 0, 0, 0x0B, 0, 0x02, 0}; auto input_adapter = fkyaml::detail::input_adapter(input); using itr_type = typename std::string::iterator; @@ -1367,8 +1297,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32LE(BOM) with a char32_t array") - { + SECTION("iterator_input_adapter for UTF-32LE(BOM) with a char32_t array") { char32_t input[] = {0xFFFE0000u, 0x61000000u, 0x42300000u, 0x0B000200u, 0x00000000}; auto input_adapter = fkyaml::detail::input_adapter(input); REQUIRE(std::is_same>::value); @@ -1387,8 +1316,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("iterator_input_adapter for UTF-32LE(BOM) with std::u32string") - { + SECTION("iterator_input_adapter for UTF-32LE(BOM) with std::u32string") { char32_t raw_input[] = {0xFFFE0000u, 0x61000000u, 0x42300000u, 0x0B000200u, 0x00000000}; std::u32string input = raw_input; auto input_adapter = fkyaml::detail::input_adapter(input); @@ -1409,8 +1337,7 @@ TEST_CASE("InputAdapter_FillBuffer") REQUIRE(buffer[7] == char(0x8Bu)); } - SECTION("file_input_adapter for UTF-32LE(BOM)") - { + SECTION("file_input_adapter for UTF-32LE(BOM)") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32lebom.txt", "r"); ENABLE_C4996 @@ -1434,8 +1361,7 @@ TEST_CASE("InputAdapter_FillBuffer") std::fclose(p_file); } - SECTION("stream_input_adapter for UTF-32LE(BOM)") - { + SECTION("stream_input_adapter for UTF-32LE(BOM)") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf32lebom.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1455,14 +1381,12 @@ TEST_CASE("InputAdapter_FillBuffer") } } -TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") -{ +TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") { ///////////////////////////////// // UTF-8 1-Byte Characters // ///////////////////////////////// - SECTION("file_input_adapter with valid 1-byte UTF-8 encodings") - { + SECTION("file_input_adapter with valid 1-byte UTF-8 encodings") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_valid_1byte_char.txt", "r"); ENABLE_C4996 @@ -1481,8 +1405,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") std::fclose(p_file); } - SECTION("file_input_adapter with invalid 1-byte UTF-8 encodings") - { + SECTION("file_input_adapter with invalid 1-byte UTF-8 encodings") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_invalid_1byte_char.txt", "r"); ENABLE_C4996 @@ -1496,8 +1419,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") std::fclose(p_file); } - SECTION("stream_input_adapter with valid 1-byte UTF-8 encodings") - { + SECTION("stream_input_adapter with valid 1-byte UTF-8 encodings") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_valid_1byte_char.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1511,8 +1433,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") REQUIRE(buffer[2] == char(0x61u)); } - SECTION("stream_input_adapter with invalid 1-byte UTF-8 encodings") - { + SECTION("stream_input_adapter with invalid 1-byte UTF-8 encodings") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_invalid_1byte_char.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1525,8 +1446,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") // UTF-8 2-Byte Characters // ///////////////////////////////// - SECTION("file_input_adapter with valid 2-byte UTF-8 encodings") - { + SECTION("file_input_adapter with valid 2-byte UTF-8 encodings") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_valid_2byte_char.txt", "r"); ENABLE_C4996 @@ -1546,8 +1466,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") std::fclose(p_file); } - SECTION("file_input_adapter with invalid 2-byte UTF-8 encodings") - { + SECTION("file_input_adapter with invalid 2-byte UTF-8 encodings") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_invalid_2byte_char.txt", "r"); ENABLE_C4996 @@ -1561,8 +1480,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") std::fclose(p_file); } - SECTION("stream_input_adapter with valid 2-byte UTF-8 encodings") - { + SECTION("stream_input_adapter with valid 2-byte UTF-8 encodings") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_valid_2byte_char.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1577,8 +1495,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") REQUIRE(buffer[3] == char(0xBFu)); } - SECTION("stream_input_adapter with invalid 2-byte UTF-8 encodings") - { + SECTION("stream_input_adapter with invalid 2-byte UTF-8 encodings") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_invalid_2byte_char.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1591,8 +1508,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") // UTF-8 3-Byte Characters // ///////////////////////////////// - SECTION("file_input_adapter with valid 3-byte UTF-8 encodings") - { + SECTION("file_input_adapter with valid 3-byte UTF-8 encodings") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_valid_3byte_char.txt", "r"); ENABLE_C4996 @@ -1614,8 +1530,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") std::fclose(p_file); } - SECTION("file_input_adapter with invalid 3-byte UTF-8 encodings") - { + SECTION("file_input_adapter with invalid 3-byte UTF-8 encodings") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_invalid_3byte_char.txt", "r"); ENABLE_C4996 @@ -1629,8 +1544,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") std::fclose(p_file); } - SECTION("stream_input_adapter with valid 3-byte UTF-8 encodings") - { + SECTION("stream_input_adapter with valid 3-byte UTF-8 encodings") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_valid_3byte_char.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1647,8 +1561,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") REQUIRE(buffer[5] == char(0xBFu)); } - SECTION("stream_input_adapter with invalid 3-byte UTF-8 encodings") - { + SECTION("stream_input_adapter with invalid 3-byte UTF-8 encodings") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_invalid_3byte_char.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1661,8 +1574,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") // UTF-8 4-Byte Characters // ///////////////////////////////// - SECTION("file_input_adapter with valid 4-byte UTF-8 encodings") - { + SECTION("file_input_adapter with valid 4-byte UTF-8 encodings") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_valid_4byte_char.txt", "r"); ENABLE_C4996 @@ -1686,8 +1598,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") std::fclose(p_file); } - SECTION("file_input_adapter with invalid 4-byte UTF-8 encodings") - { + SECTION("file_input_adapter with invalid 4-byte UTF-8 encodings") { DISABLE_C4996 FILE* p_file = std::fopen(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_invalid_4byte_char.txt", "r"); ENABLE_C4996 @@ -1701,8 +1612,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") std::fclose(p_file); } - SECTION("stream_input_adapter with valid 4-byte UTF-8 encodings") - { + SECTION("stream_input_adapter with valid 4-byte UTF-8 encodings") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_valid_4byte_char.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); @@ -1721,8 +1631,7 @@ TEST_CASE("InputAdapter_FillBufferUTF8CharsValidation") REQUIRE(buffer[7] == char(0x80u)); } - SECTION("stream_input_adapter with invalid 4-byte UTF-8 encodings") - { + SECTION("stream_input_adapter with invalid 4-byte UTF-8 encodings") { std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/input_adapter_test_data_utf8n_invalid_4byte_char.txt"); auto input_adapter = fkyaml::detail::input_adapter(ifs); REQUIRE(std::is_same::value); diff --git a/test/unit_test/test_iterator_class.cpp b/test/unit_test/test_iterator_class.cpp index acb2a706..e183a9f7 100644 --- a/test/unit_test/test_iterator_class.cpp +++ b/test/unit_test/test_iterator_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,24 +10,21 @@ #include -TEST_CASE("Iterator_SequenceCtor") -{ +TEST_CASE("Iterator_SequenceCtor") { fkyaml::node sequence = fkyaml::node::sequence(); fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); REQUIRE(iterator.type() == fkyaml::detail::iterator_t::SEQUENCE); } -TEST_CASE("Iterator_MappingCtor") -{ +TEST_CASE("Iterator_MappingCtor") { fkyaml::node mapping = fkyaml::node::mapping(); fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); REQUIRE(iterator.type() == fkyaml::detail::iterator_t::MAPPING); } -TEST_CASE("Iterator_SequenceCopyCtor") -{ +TEST_CASE("Iterator_SequenceCopyCtor") { fkyaml::node sequence = fkyaml::node::sequence({fkyaml::node()}); fkyaml::detail::iterator copied( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -36,8 +33,7 @@ TEST_CASE("Iterator_SequenceCopyCtor") REQUIRE(iterator->is_null()); } -TEST_CASE("Iterator_MappingCopyCtor") -{ +TEST_CASE("Iterator_MappingCopyCtor") { fkyaml::node mapping = fkyaml::node::mapping({{"test", fkyaml::node()}}); fkyaml::detail::iterator copied( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -47,8 +43,7 @@ TEST_CASE("Iterator_MappingCopyCtor") REQUIRE(iterator.value().is_null()); } -TEST_CASE("Iterator_SequenceMoveCtor") -{ +TEST_CASE("Iterator_SequenceMoveCtor") { fkyaml::node sequence = {"test"}; fkyaml::detail::iterator moved( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -58,8 +53,7 @@ TEST_CASE("Iterator_SequenceMoveCtor") REQUIRE(iterator->get_value_ref().compare("test") == 0); } -TEST_CASE("Iterator_MappingMoveCtor") -{ +TEST_CASE("Iterator_MappingMoveCtor") { fkyaml::node mapping = fkyaml::node::mapping({{"test", fkyaml::node()}}); fkyaml::detail::iterator moved( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -69,31 +63,26 @@ TEST_CASE("Iterator_MappingMoveCtor") REQUIRE(iterator.value().is_null()); } -TEST_CASE("Iterator_AssignmentOperator") -{ - SECTION("self assignment.") - { +TEST_CASE("Iterator_AssignmentOperator") { + SECTION("self assignment.") { fkyaml::node sequence = fkyaml::node::sequence({fkyaml::node()}); fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); - SECTION("lvalue iterator") - { + SECTION("lvalue iterator") { iterator = *&iterator; REQUIRE(iterator.type() == fkyaml::detail::iterator_t::SEQUENCE); REQUIRE(iterator->is_null()); } - SECTION("rvalue iterator") - { + SECTION("rvalue iterator") { iterator = std::move(*&iterator); REQUIRE(iterator.type() == fkyaml::detail::iterator_t::SEQUENCE); REQUIRE(iterator->is_null()); } } - SECTION("sequence iterators") - { + SECTION("sequence iterators") { fkyaml::node copied_seq = {"test"}; fkyaml::detail::iterator copied_itr( fkyaml::detail::sequence_iterator_tag {}, copied_seq.get_value_ref().begin()); @@ -101,16 +90,14 @@ TEST_CASE("Iterator_AssignmentOperator") fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); - SECTION("lvalue iterator") - { + SECTION("lvalue iterator") { iterator = copied_itr; REQUIRE(iterator.type() == fkyaml::detail::iterator_t::SEQUENCE); REQUIRE(iterator->is_string()); REQUIRE(iterator->get_value_ref().compare("test") == 0); } - SECTION("rvalue iterator") - { + SECTION("rvalue iterator") { iterator = std::move(copied_itr); REQUIRE(iterator.type() == fkyaml::detail::iterator_t::SEQUENCE); REQUIRE(iterator->is_string()); @@ -118,8 +105,7 @@ TEST_CASE("Iterator_AssignmentOperator") } } - SECTION("mapping iterators") - { + SECTION("mapping iterators") { fkyaml::node copied_map = {{"key", "test"}}; fkyaml::detail::iterator copied_itr( fkyaml::detail::mapping_iterator_tag {}, copied_map.get_value_ref().begin()); @@ -127,8 +113,7 @@ TEST_CASE("Iterator_AssignmentOperator") fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, map.get_value_ref().begin()); - SECTION("lvalue iterator") - { + SECTION("lvalue iterator") { iterator = copied_itr; REQUIRE(iterator.type() == fkyaml::detail::iterator_t::MAPPING); REQUIRE(iterator.key().get_value_ref() == "key"); @@ -136,8 +121,7 @@ TEST_CASE("Iterator_AssignmentOperator") REQUIRE(iterator.value().get_value_ref().compare("test") == 0); } - SECTION("rvalue iterator") - { + SECTION("rvalue iterator") { iterator = std::move(copied_itr); REQUIRE(iterator.type() == fkyaml::detail::iterator_t::MAPPING); REQUIRE(iterator.key().get_value_ref() == "key"); @@ -147,18 +131,15 @@ TEST_CASE("Iterator_AssignmentOperator") } } -TEST_CASE("Iterator_ArrowOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_ArrowOperator") { + SECTION("sequence iterator") { fkyaml::node seq = {"test"}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, seq.get_value_ref().begin()); REQUIRE(iterator.operator->() == &(seq.get_value_ref().operator[](0))); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node map = {{"key", "test"}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, map.get_value_ref().begin()); @@ -166,18 +147,15 @@ TEST_CASE("Iterator_ArrowOperator") } } -TEST_CASE("Iterator_DereferenceOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_DereferenceOperator") { + SECTION("sequence iterator") { fkyaml::node seq = {"test"}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, seq.get_value_ref().begin()); REQUIRE(&(iterator.operator*()) == &(seq.get_value_ref().operator[](0))); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node map = fkyaml::node::mapping({{"key", "test"}}); fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, map.get_value_ref().begin()); @@ -185,10 +163,8 @@ TEST_CASE("Iterator_DereferenceOperator") } } -TEST_CASE("Iterator_CompoundAssignmentOperatorBySum") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_CompoundAssignmentOperatorBySum") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -197,8 +173,7 @@ TEST_CASE("Iterator_CompoundAssignmentOperatorBySum") REQUIRE(iterator->get_value() == true); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -209,10 +184,8 @@ TEST_CASE("Iterator_CompoundAssignmentOperatorBySum") } } -TEST_CASE("Iterator_PlusOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_PlusOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -221,8 +194,7 @@ TEST_CASE("Iterator_PlusOperator") REQUIRE(after_plus_itr->get_value() == true); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -233,10 +205,8 @@ TEST_CASE("Iterator_PlusOperator") } } -TEST_CASE("Iterator_PreIncrementOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_PreIncrementOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -245,8 +215,7 @@ TEST_CASE("Iterator_PreIncrementOperator") REQUIRE(iterator->get_value() == true); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -257,10 +226,8 @@ TEST_CASE("Iterator_PreIncrementOperator") } } -TEST_CASE("Iterator_PostIncrementOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_PostIncrementOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -269,8 +236,7 @@ TEST_CASE("Iterator_PostIncrementOperator") REQUIRE(iterator->get_value() == true); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -281,10 +247,8 @@ TEST_CASE("Iterator_PostIncrementOperator") } } -TEST_CASE("Iterator_CompoundAssignmentOperatorByDifference") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_CompoundAssignmentOperatorByDifference") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().end()); @@ -293,8 +257,7 @@ TEST_CASE("Iterator_CompoundAssignmentOperatorByDifference") REQUIRE(iterator->get_value() == true); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().end()); @@ -305,10 +268,8 @@ TEST_CASE("Iterator_CompoundAssignmentOperatorByDifference") } } -TEST_CASE("Iterator_MinusOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_MinusOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().end()); @@ -317,8 +278,7 @@ TEST_CASE("Iterator_MinusOperator") REQUIRE(after_minus_itr->get_value() == true); } - SECTION("mapping iterator.") - { + SECTION("mapping iterator.") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().end()); @@ -329,10 +289,8 @@ TEST_CASE("Iterator_MinusOperator") } } -TEST_CASE("Iterator_PreDecrementOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_PreDecrementOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().end()); @@ -341,8 +299,7 @@ TEST_CASE("Iterator_PreDecrementOperator") REQUIRE(iterator->get_value() == true); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().end()); @@ -353,10 +310,8 @@ TEST_CASE("Iterator_PreDecrementOperator") } } -TEST_CASE("Iterator_PostDecrementOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_PostDecrementOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().end()); @@ -365,8 +320,7 @@ TEST_CASE("Iterator_PostDecrementOperator") REQUIRE(iterator->get_value() == true); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().end()); @@ -377,10 +331,8 @@ TEST_CASE("Iterator_PostDecrementOperator") } } -TEST_CASE("Iterator_EqualToOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_EqualToOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -389,8 +341,7 @@ TEST_CASE("Iterator_EqualToOperator") REQUIRE(lhs == rhs); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator lhs( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -399,8 +350,7 @@ TEST_CASE("Iterator_EqualToOperator") REQUIRE(lhs == rhs); } - SECTION("equality check between different type iterators") - { + SECTION("equality check between different type iterators") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -411,10 +361,8 @@ TEST_CASE("Iterator_EqualToOperator") } } -TEST_CASE("Iterator_NotEqualToOperator") -{ - SECTION("sequence iterator.") - { +TEST_CASE("Iterator_NotEqualToOperator") { + SECTION("sequence iterator.") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -424,8 +372,7 @@ TEST_CASE("Iterator_NotEqualToOperator") REQUIRE(lhs != rhs); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator lhs( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -435,8 +382,7 @@ TEST_CASE("Iterator_NotEqualToOperator") REQUIRE(lhs != rhs); } - SECTION("equality check between different type iterators") - { + SECTION("equality check between different type iterators") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -447,10 +393,8 @@ TEST_CASE("Iterator_NotEqualToOperator") } } -TEST_CASE("Iterator_LessThanOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_LessThanOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -461,8 +405,7 @@ TEST_CASE("Iterator_LessThanOperator") REQUIRE(lhs < rhs); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator lhs( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -471,8 +414,7 @@ TEST_CASE("Iterator_LessThanOperator") REQUIRE_THROWS_AS(lhs < rhs, fkyaml::exception); } - SECTION("less-than check between different type iterators") - { + SECTION("less-than check between different type iterators") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -483,10 +425,8 @@ TEST_CASE("Iterator_LessThanOperator") } } -TEST_CASE("Iterator_LessThanOrEqualToOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_LessThanOrEqualToOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -500,8 +440,7 @@ TEST_CASE("Iterator_LessThanOrEqualToOperator") REQUIRE(lhs < rhs); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator lhs( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -510,8 +449,7 @@ TEST_CASE("Iterator_LessThanOrEqualToOperator") REQUIRE_THROWS_AS(lhs <= rhs, fkyaml::exception); } - SECTION("less-than-or-equal-to check between different type iterators") - { + SECTION("less-than-or-equal-to check between different type iterators") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -522,10 +460,8 @@ TEST_CASE("Iterator_LessThanOrEqualToOperator") } } -TEST_CASE("Iterator_GreaterThanOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_GreaterThanOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -536,8 +472,7 @@ TEST_CASE("Iterator_GreaterThanOperator") REQUIRE(lhs > rhs); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator lhs( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -546,8 +481,7 @@ TEST_CASE("Iterator_GreaterThanOperator") REQUIRE_THROWS_AS(lhs > rhs, fkyaml::exception); } - SECTION("greater-than check between different type iterators") - { + SECTION("greater-than check between different type iterators") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -558,10 +492,8 @@ TEST_CASE("Iterator_GreaterThanOperator") } } -TEST_CASE("Iterator_GreaterThanOrEqualToOperator") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_GreaterThanOrEqualToOperator") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -575,8 +507,7 @@ TEST_CASE("Iterator_GreaterThanOrEqualToOperator") REQUIRE(lhs >= rhs); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator lhs( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -585,8 +516,7 @@ TEST_CASE("Iterator_GreaterThanOrEqualToOperator") REQUIRE_THROWS_AS(lhs >= rhs, fkyaml::exception); } - SECTION("greater-than-or-equal-to check between different type iterators") - { + SECTION("greater-than-or-equal-to check between different type iterators") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator lhs( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -597,18 +527,15 @@ TEST_CASE("Iterator_GreaterThanOrEqualToOperator") } } -TEST_CASE("Iterator_TypeGetter") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_TypeGetter") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); REQUIRE(iterator.type() == fkyaml::detail::iterator_t::SEQUENCE); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -616,18 +543,15 @@ TEST_CASE("Iterator_TypeGetter") } } -TEST_CASE("Iterator_KeyGetter") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_KeyGetter") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); REQUIRE_THROWS_AS(iterator.key(), fkyaml::exception); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); @@ -636,10 +560,8 @@ TEST_CASE("Iterator_KeyGetter") } } -TEST_CASE("Iterator_ValueGetter") -{ - SECTION("sequence iterator") - { +TEST_CASE("Iterator_ValueGetter") { + SECTION("sequence iterator") { fkyaml::node sequence = {false, true}; fkyaml::detail::iterator iterator( fkyaml::detail::sequence_iterator_tag {}, sequence.get_value_ref().begin()); @@ -647,8 +569,7 @@ TEST_CASE("Iterator_ValueGetter") REQUIRE(iterator.value().get_value() == false); } - SECTION("mapping iterator") - { + SECTION("mapping iterator") { fkyaml::node mapping = {{"test0", false}, {"test1", true}}; fkyaml::detail::iterator iterator( fkyaml::detail::mapping_iterator_tag {}, mapping.get_value_ref().begin()); diff --git a/test/unit_test/test_lexical_analyzer_class.cpp b/test/unit_test/test_lexical_analyzer_class.cpp index e4220bdf..4b2454ec 100644 --- a/test/unit_test/test_lexical_analyzer_class.cpp +++ b/test/unit_test/test_lexical_analyzer_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -12,12 +12,10 @@ using lexer_t = fkyaml::detail::lexical_analyzer; -TEST_CASE("LexicalAnalyzer_YamlVersionDirective") -{ +TEST_CASE("LexicalAnalyzer_YamlVersionDirective") { fkyaml::detail::lexical_token_t token; - SECTION("valid YAML directive") - { + SECTION("valid YAML directive") { using value_pair_t = std::pair; auto value_pair = GENERATE( value_pair_t(std::string("%YAML 1.1 "), std::string("1.1")), @@ -41,8 +39,7 @@ TEST_CASE("LexicalAnalyzer_YamlVersionDirective") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("wrong YAML directive") - { + SECTION("wrong YAML directive") { auto buffer = GENERATE( std::string("%YUML 1.2"), std::string("%YANL 1.2 \r"), @@ -58,8 +55,7 @@ TEST_CASE("LexicalAnalyzer_YamlVersionDirective") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("invalid YAML directive value") - { + SECTION("invalid YAML directive value") { auto buffer = GENERATE( std::string("%YAML 1.3\n"), std::string("%YAML 2.0\n"), @@ -75,12 +71,10 @@ TEST_CASE("LexicalAnalyzer_YamlVersionDirective") } } -TEST_CASE("LexicalAnalyzer_TagDirective") -{ +TEST_CASE("LexicalAnalyzer_TagDirective") { fkyaml::detail::lexical_token_t token; - SECTION("primary tag handle") - { + SECTION("primary tag handle") { auto input = GENERATE(std::string("%TAG ! foo"), std::string("%TAG\t!\tfoo")); lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -92,8 +86,7 @@ TEST_CASE("LexicalAnalyzer_TagDirective") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("secondary tag handle") - { + SECTION("secondary tag handle") { auto input = GENERATE(std::string("%TAG !! foo"), std::string("%TAG\t!!\tfoo")); lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -105,8 +98,7 @@ TEST_CASE("LexicalAnalyzer_TagDirective") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("named tag handle") - { + SECTION("named tag handle") { auto input = GENERATE(std::string("%TAG !va1id-ta9! foo"), std::string("%TAG\t!va1id-ta9!\tfoo")); lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -118,8 +110,7 @@ TEST_CASE("LexicalAnalyzer_TagDirective") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("invalid TAG directive") - { + SECTION("invalid TAG directive") { auto buffer = GENERATE(std::string("%TUB"), std::string("%TAC"), std::string("%TAGE")); lexer_t lexer(fkyaml::detail::input_adapter(buffer)); @@ -129,8 +120,7 @@ TEST_CASE("LexicalAnalyzer_TagDirective") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("invalid tag handle") - { + SECTION("invalid tag handle") { auto input = GENERATE( std::string("%TAG foo bar"), std::string("%TAG !!abc bar"), @@ -148,8 +138,7 @@ TEST_CASE("LexicalAnalyzer_TagDirective") REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } - SECTION("invalid tag prefix") - { + SECTION("invalid tag prefix") { auto input = GENERATE( std::string("%TAG ! [invalid"), std::string("%TAG !! ]invalid"), @@ -163,16 +152,14 @@ TEST_CASE("LexicalAnalyzer_TagDirective") } } -TEST_CASE("LexicalAnalyzer_InvalidDirective") -{ +TEST_CASE("LexicalAnalyzer_InvalidDirective") { auto buffer = GENERATE(std::string("%TAG"), std::string("%YAML")); lexer_t lexer(fkyaml::detail::input_adapter(buffer)); REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } -TEST_CASE("LexicalAnalyzer_ReservedDirective") -{ +TEST_CASE("LexicalAnalyzer_ReservedDirective") { auto buffer = GENERATE(std::string("%TEST\r\n"), std::string("%1984\r "), std::string("%TEST4LIB\n"), std::string("%%ERROR")); @@ -185,14 +172,12 @@ TEST_CASE("LexicalAnalyzer_ReservedDirective") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } -TEST_CASE("LexicalAnalyzer_EmptyDirective") -{ +TEST_CASE("LexicalAnalyzer_EmptyDirective") { lexer_t lexer(fkyaml::detail::input_adapter("%")); REQUIRE(lexer.get_next_token() == fkyaml::detail::lexical_token_t::INVALID_DIRECTIVE); } -TEST_CASE("LexicalAnalyzer_EndOfDirectives") -{ +TEST_CASE("LexicalAnalyzer_EndOfDirectives") { lexer_t lexer(fkyaml::detail::input_adapter("%YAML 1.2\n---\nfoo: bar")); fkyaml::detail::lexical_token_t token; @@ -213,8 +198,7 @@ TEST_CASE("LexicalAnalyzer_EndOfDirectives") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } -TEST_CASE("LexicalAnalyzer_EndOfDocuments") -{ +TEST_CASE("LexicalAnalyzer_EndOfDocuments") { lexer_t lexer(fkyaml::detail::input_adapter("%YAML 1.2\n---\n...")); fkyaml::detail::lexical_token_t token; @@ -229,97 +213,83 @@ TEST_CASE("LexicalAnalyzer_EndOfDocuments") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } -TEST_CASE("LexicalAnalyzer_Colon") -{ +TEST_CASE("LexicalAnalyzer_Colon") { fkyaml::detail::lexical_token_t token; - SECTION("colon with half-width space") - { + SECTION("colon with half-width space") { lexer_t lexer(fkyaml::detail::input_adapter(": ")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with CR newline code") - { + SECTION("colon with CR newline code") { lexer_t lexer(fkyaml::detail::input_adapter(":\r")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with CRLF newline code") - { + SECTION("colon with CRLF newline code") { lexer_t lexer(fkyaml::detail::input_adapter(":\r\n")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with LF newline code") - { + SECTION("colon with LF newline code") { lexer_t lexer(fkyaml::detail::input_adapter(":\n")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with the end of the buffer") - { + SECTION("colon with the end of the buffer") { lexer_t lexer(fkyaml::detail::input_adapter(":")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with a comment and a CRLF newline code") - { + SECTION("colon with a comment and a CRLF newline code") { lexer_t lexer(fkyaml::detail::input_adapter(": # comment\r\n")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with a comment and a LF newline code") - { + SECTION("colon with a comment and a LF newline code") { lexer_t lexer(fkyaml::detail::input_adapter(": # comment\n")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with a comment and no newline code") - { + SECTION("colon with a comment and no newline code") { lexer_t lexer(fkyaml::detail::input_adapter(": # comment")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with many spaces and a CRLF newline code") - { + SECTION("colon with many spaces and a CRLF newline code") { lexer_t lexer(fkyaml::detail::input_adapter(": \r\n")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with many spaces and a LF newline code") - { + SECTION("colon with many spaces and a LF newline code") { lexer_t lexer(fkyaml::detail::input_adapter(": \n")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with many spaces and no newline code") - { + SECTION("colon with many spaces and no newline code") { lexer_t lexer(fkyaml::detail::input_adapter(": ")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::KEY_SEPARATOR); } - SECTION("colon with an always-safe character") - { + SECTION("colon with an always-safe character") { lexer_t lexer(fkyaml::detail::input_adapter(":test")); REQUIRE_NOTHROW(token = lexer.get_next_token()); REQUIRE(token == fkyaml::detail::lexical_token_t::STRING_VALUE); REQUIRE(lexer.get_string() == ":test"); } - SECTION("colon with a flow indicator in a non-flow context") - { + SECTION("colon with a flow indicator in a non-flow context") { auto input = GENERATE(std::string(":,"), std::string(":{"), std::string(":}"), std::string(":["), std::string(":]")); lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -328,8 +298,7 @@ TEST_CASE("LexicalAnalyzer_Colon") REQUIRE(lexer.get_string() == input); } - SECTION("colon with a flow indicator in a flow context") - { + SECTION("colon with a flow indicator in a flow context") { auto input = GENERATE( std::string("{:,"), std::string("{:{"), std::string("{:}"), std::string("{:["), std::string("{:]")); lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -340,12 +309,10 @@ TEST_CASE("LexicalAnalyzer_Colon") } } -TEST_CASE("LexicalAnalyzer_Null") -{ +TEST_CASE("LexicalAnalyzer_Null") { fkyaml::detail::lexical_token_t token; - SECTION("valid null scalar token") - { + SECTION("valid null scalar token") { auto buffer = GENERATE(std::string("null"), std::string("Null"), std::string("NULL"), std::string("~")); lexer_t lexer(fkyaml::detail::input_adapter(buffer)); @@ -355,20 +322,17 @@ TEST_CASE("LexicalAnalyzer_Null") REQUIRE(lexer.get_null() == nullptr); } - SECTION("invalid token for null scalar") - { + SECTION("invalid token for null scalar") { lexer_t lexer(fkyaml::detail::input_adapter("test")); REQUIRE_NOTHROW(lexer.get_next_token()); REQUIRE_THROWS_AS(lexer.get_null(), fkyaml::exception); } } -TEST_CASE("LexicalAnalyzer_BooleanTrue") -{ +TEST_CASE("LexicalAnalyzer_BooleanTrue") { fkyaml::detail::lexical_token_t token; - SECTION("valid boolean true scalar token") - { + SECTION("valid boolean true scalar token") { auto buffer = GENERATE(std::string("true"), std::string("True"), std::string("TRUE")); lexer_t lexer(fkyaml::detail::input_adapter(buffer)); @@ -378,20 +342,17 @@ TEST_CASE("LexicalAnalyzer_BooleanTrue") REQUIRE(lexer.get_boolean() == true); } - SECTION("invalid token for boolean true scalar") - { + SECTION("invalid token for boolean true scalar") { lexer_t lexer(fkyaml::detail::input_adapter("test")); REQUIRE_NOTHROW(lexer.get_next_token()); REQUIRE_THROWS_AS(lexer.get_boolean(), fkyaml::exception); } } -TEST_CASE("LexicalAnalyzer_BooleanFalse") -{ +TEST_CASE("LexicalAnalyzer_BooleanFalse") { fkyaml::detail::lexical_token_t token; - SECTION("valid boolean false scalar token") - { + SECTION("valid boolean false scalar token") { auto buffer = GENERATE(std::string("false"), std::string("False"), std::string("FALSE")); lexer_t lexer(fkyaml::detail::input_adapter(buffer)); @@ -401,20 +362,17 @@ TEST_CASE("LexicalAnalyzer_BooleanFalse") REQUIRE(lexer.get_boolean() == false); } - SECTION("invalid token for boolean true scalar") - { + SECTION("invalid token for boolean true scalar") { lexer_t lexer(fkyaml::detail::input_adapter("test")); REQUIRE_NOTHROW(lexer.get_next_token()); REQUIRE_THROWS_AS(lexer.get_boolean(), fkyaml::exception); } } -TEST_CASE("LexicalAnalyzer_Integer") -{ +TEST_CASE("LexicalAnalyzer_Integer") { fkyaml::detail::lexical_token_t token; - SECTION("valid integer scalar token") - { + SECTION("valid integer scalar token") { using value_pair_t = std::pair; auto value_pair = GENERATE( value_pair_t(std::string("-1234"), -1234), @@ -432,16 +390,14 @@ TEST_CASE("LexicalAnalyzer_Integer") REQUIRE(lexer.get_integer() == value_pair.second); } - SECTION("invalid token for integer scalar") - { + SECTION("invalid token for integer scalar") { lexer_t lexer(fkyaml::detail::input_adapter("test")); REQUIRE_NOTHROW(lexer.get_next_token()); REQUIRE_THROWS_AS(lexer.get_integer(), fkyaml::exception); } } -TEST_CASE("LexicalAnalyzer_OctalInteger") -{ +TEST_CASE("LexicalAnalyzer_OctalInteger") { using value_pair_t = std::pair; auto value_pair = GENERATE( value_pair_t(std::string("0o27"), 027), @@ -458,8 +414,7 @@ TEST_CASE("LexicalAnalyzer_OctalInteger") REQUIRE(lexer.get_integer() == value_pair.second); } -TEST_CASE("LexicalAnalyzer_HexadecimalInteger") -{ +TEST_CASE("LexicalAnalyzer_HexadecimalInteger") { using value_pair_t = std::pair; auto value_pair = GENERATE( value_pair_t(std::string("0xA04F"), 0xA04F), @@ -475,12 +430,10 @@ TEST_CASE("LexicalAnalyzer_HexadecimalInteger") REQUIRE(lexer.get_integer() == value_pair.second); } -TEST_CASE("LexicalAnalyzer_FloatingPointNumber") -{ +TEST_CASE("LexicalAnalyzer_FloatingPointNumber") { fkyaml::detail::lexical_token_t token; - SECTION("valid floating point number scalar token") - { + SECTION("valid floating point number scalar token") { using value_pair_t = std::pair; auto value_pair = GENERATE( value_pair_t(std::string("-1.234"), -1.234), @@ -498,23 +451,20 @@ TEST_CASE("LexicalAnalyzer_FloatingPointNumber") REQUIRE(lexer.get_float_number() == value_pair.second); } - SECTION("valid floating point number scalar token edge cases") - { + SECTION("valid floating point number scalar token edge cases") { auto input = GENERATE(std::string("0."), std::string("1.23e"), std::string("1.2e-z")); lexer_t lexer(fkyaml::detail::input_adapter(input)); REQUIRE_FALSE(lexer.get_next_token() == fkyaml::detail::lexical_token_t::FLOAT_NUMBER_VALUE); } - SECTION("invalid token for floating point number scalar") - { + SECTION("invalid token for floating point number scalar") { lexer_t lexer(fkyaml::detail::input_adapter("test")); REQUIRE_NOTHROW(lexer.get_next_token()); REQUIRE_THROWS_AS(lexer.get_float_number(), fkyaml::exception); } } -TEST_CASE("LexicalAnalyzer_Infinity") -{ +TEST_CASE("LexicalAnalyzer_Infinity") { auto buffer = GENERATE( std::string(".inf"), std::string(".Inf"), @@ -529,8 +479,7 @@ TEST_CASE("LexicalAnalyzer_Infinity") REQUIRE(std::isinf(lexer.get_float_number()) == true); } -TEST_CASE("LexicalAnalyzer_NaN") -{ +TEST_CASE("LexicalAnalyzer_NaN") { auto buffer = GENERATE(std::string(".nan"), std::string(".NaN"), std::string(".NAN")); lexer_t lexer(fkyaml::detail::input_adapter(buffer)); @@ -539,8 +488,7 @@ TEST_CASE("LexicalAnalyzer_NaN") REQUIRE(std::isnan(lexer.get_float_number()) == true); } -TEST_CASE("LexicalAnalyzer_String") -{ +TEST_CASE("LexicalAnalyzer_String") { using value_pair_t = std::pair; auto value_pair = GENERATE( value_pair_t(std::string("\"\""), fkyaml::node::string_type("")), @@ -630,8 +578,7 @@ TEST_CASE("LexicalAnalyzer_String") REQUIRE(lexer.get_string() == value_pair.second); } -TEST_CASE("LexicalAnalyzer_MultiByteCharString") -{ +TEST_CASE("LexicalAnalyzer_MultiByteCharString") { using char_traits_t = std::char_traits; auto mb_char = GENERATE( std::string {char_traits_t::to_char_type(0xC2), char_traits_t::to_char_type(0x80)}, @@ -688,8 +635,7 @@ TEST_CASE("LexicalAnalyzer_MultiByteCharString") REQUIRE(lexer.get_string() == mb_char); } -TEST_CASE("LexicalAnalyzer_EscapedUnicodeCharacter") -{ +TEST_CASE("LexicalAnalyzer_EscapedUnicodeCharacter") { using value_pair_t = std::pair; using char_traits_t = std::char_traits; auto value_pair = GENERATE( @@ -787,10 +733,8 @@ TEST_CASE("LexicalAnalyzer_EscapedUnicodeCharacter") REQUIRE(lexer.get_string() == value_pair.second); } -TEST_CASE("LexicalAnalyzer_InvalidString") -{ - SECTION("parse error") - { +TEST_CASE("LexicalAnalyzer_InvalidString") { + SECTION("parse error") { auto buffer = GENERATE( std::string("foo\\tbar"), std::string("\"test"), @@ -809,16 +753,14 @@ TEST_CASE("LexicalAnalyzer_InvalidString") REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } - SECTION("invalid encoding") - { + SECTION("invalid encoding") { std::string buffer = "\"\\U00110000\""; lexer_t lexer(fkyaml::detail::input_adapter(buffer)); REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::invalid_encoding); } } -TEST_CASE("LexicalAnalyzer_InvalidMultiByteCharString") -{ +TEST_CASE("LexicalAnalyzer_InvalidMultiByteCharString") { using char_traits_t = std::char_traits; auto mb_char = GENERATE( std::string {char_traits_t::to_char_type(0x80), char_traits_t::to_char_type(0x80)}, @@ -949,8 +891,7 @@ TEST_CASE("LexicalAnalyzer_InvalidMultiByteCharString") REQUIRE_THROWS_AS(lexer_t(std::move(input_adapter)), fkyaml::invalid_encoding); } -TEST_CASE("LexicalAnalyzer_UnescapedControlCharacter") -{ +TEST_CASE("LexicalAnalyzer_UnescapedControlCharacter") { auto unescaped_char = GENERATE( char(0x01), char(0x02), @@ -987,12 +928,10 @@ TEST_CASE("LexicalAnalyzer_UnescapedControlCharacter") REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } -TEST_CASE("LexicalAnalyzer_LiteralStringScalar") -{ +TEST_CASE("LexicalAnalyzer_LiteralStringScalar") { fkyaml::detail::lexical_token_t token; - SECTION("empty literal string scalar with strip chomping") - { + SECTION("empty literal string scalar with strip chomping") { const char input[] = "|-\r\n" " \r\n"; lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -1002,8 +941,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == ""); } - SECTION("empty literal string scalar with clip chomping") - { + SECTION("empty literal string scalar with clip chomping") { const char input[] = "|\r\n" " \r\n"; lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -1013,8 +951,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == ""); } - SECTION("empty literal string scalar with keep chomping") - { + SECTION("empty literal string scalar with keep chomping") { const char input[] = "|+\r\n" " \r\n"; lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -1024,8 +961,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == "\n"); } - SECTION("literal string scalar with 0 indent level.") - { + SECTION("literal string scalar with 0 indent level.") { const char input[] = "|0\n" "foo"; @@ -1033,8 +969,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } - SECTION("less indented literal string scalar") - { + SECTION("less indented literal string scalar") { const char input[] = "|2\n" " foo"; @@ -1042,8 +977,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } - SECTION("literal scalar with the first line being more indented than the indicated level") - { + SECTION("literal scalar with the first line being more indented than the indicated level") { const char input[] = "|2\n" " foo\n" " bar\n"; @@ -1054,8 +988,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == " foo\nbar\n"); } - SECTION("literal string scalar with CR+LF newlines") - { + SECTION("literal string scalar with CR+LF newlines") { const char input[] = "|\r\n" " foo\r\n" " bar\r\n"; @@ -1066,8 +999,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == "foo\nbar\n"); } - SECTION("literal string scalar with implicit indentation and strip chomping") - { + SECTION("literal string scalar with implicit indentation and strip chomping") { const char input[] = "|-\n" "\n" " foo\n" @@ -1082,8 +1014,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == "\nfoo\nbar\n\nbaz"); } - SECTION("literal string scalar with explicit indentation and strip chomping") - { + SECTION("literal string scalar with explicit indentation and strip chomping") { const char input[] = "|-2\n" " foo\n" " bar\n" @@ -1097,8 +1028,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == "foo\n bar\n\nbaz"); } - SECTION("literal string scalar with implicit indentation and clip chomping") - { + SECTION("literal string scalar with implicit indentation and clip chomping") { const char input[] = "|\n" "\n" " foo\n" @@ -1113,8 +1043,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == "\nfoo\nbar\n\nbaz\n"); } - SECTION("literal string scalar with explicit indentation and clip chomping") - { + SECTION("literal string scalar with explicit indentation and clip chomping") { const char input[] = "|2\n" " foo\n" " bar\n" @@ -1128,8 +1057,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == "foo\n bar\n\nbaz\n"); } - SECTION("literal string scalar with clip chomping and no trailing newlines") - { + SECTION("literal string scalar with clip chomping and no trailing newlines") { const char input[] = "|2\n" " foo\n" " bar\n" @@ -1142,8 +1070,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == "foo\n bar\n\nbaz"); } - SECTION("literal string scalar with implicit indentation and keep chomping") - { + SECTION("literal string scalar with implicit indentation and keep chomping") { const char input[] = "|+\n" "\n" " foo\n" @@ -1158,8 +1085,7 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") REQUIRE(lexer.get_string() == "\nfoo\nbar\n\nbaz\n\n"); } - SECTION("literal string scalar with explicit indentation and keep chomping") - { + SECTION("literal string scalar with explicit indentation and keep chomping") { const char input[] = "|+2\n" " foo\n" " bar\n" @@ -1174,12 +1100,10 @@ TEST_CASE("LexicalAnalyzer_LiteralStringScalar") } } -TEST_CASE("LexicalAnalyzer_FoldedString") -{ +TEST_CASE("LexicalAnalyzer_FoldedString") { fkyaml::detail::lexical_token_t token; - SECTION("empty folded string scalar with strip chomping") - { + SECTION("empty folded string scalar with strip chomping") { const char input[] = ">-\r\n" " \r\n"; lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -1189,8 +1113,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE(lexer.get_string() == ""); } - SECTION("empty folded string scalar with clip chomping") - { + SECTION("empty folded string scalar with clip chomping") { const char input[] = ">\r\n" " \r\n"; lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -1200,8 +1123,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE(lexer.get_string() == ""); } - SECTION("empty folded string scalar with keep chomping") - { + SECTION("empty folded string scalar with keep chomping") { const char input[] = ">+\r\n" " \r\n"; lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -1211,8 +1133,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE(lexer.get_string() == "\n"); } - SECTION("folded string scalar with 0 indent level") - { + SECTION("folded string scalar with 0 indent level") { const char input[] = "|0\n" "foo"; @@ -1220,8 +1141,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } - SECTION("less indented folded string scalar") - { + SECTION("less indented folded string scalar") { const char input[] = ">2\n" " foo"; @@ -1229,8 +1149,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } - SECTION("folded string scalar with the first line being more indented than the indicated level") - { + SECTION("folded string scalar with the first line being more indented than the indicated level") { const char input[] = ">2\n" " foo\n" " bar\n"; @@ -1241,8 +1160,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE(lexer.get_string() == "\n foo\nbar\n"); } - SECTION("folded string scalar with the non-first line being more indented than the indicated level") - { + SECTION("folded string scalar with the non-first line being more indented than the indicated level") { const char input[] = ">2\n" " foo\n" " bar\n"; @@ -1253,8 +1171,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE(lexer.get_string() == "foo\n bar\n"); } - SECTION("folded string scalar with windows style newlines") - { + SECTION("folded string scalar with windows style newlines") { const char input[] = ">\r\n" " foo\r\n" " \r\n" @@ -1268,8 +1185,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE(lexer.get_string() == "foo\n\nbar\n"); } - SECTION("folded string scalar with implicit indentation and strip chomping") - { + SECTION("folded string scalar with implicit indentation and strip chomping") { const char input[] = ">-\n" " foo\n" " bar\n" @@ -1282,8 +1198,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE(lexer.get_string() == "foo bar"); } - SECTION("folded string scalar with implicit indentation and clip chomping") - { + SECTION("folded string scalar with implicit indentation and clip chomping") { const char input[] = ">\n" " foo\n" " bar\n" @@ -1296,8 +1211,7 @@ TEST_CASE("LexicalAnalyzer_FoldedString") REQUIRE(lexer.get_string() == "foo bar\n"); } - SECTION("folded string scalar with implicit indentation and keep chomping") - { + SECTION("folded string scalar with implicit indentation and keep chomping") { const char input[] = ">+\n" " foo\n" " bar\n" @@ -1311,12 +1225,10 @@ TEST_CASE("LexicalAnalyzer_FoldedString") } } -TEST_CASE("LexicalAnalyzer_Anchor") -{ +TEST_CASE("LexicalAnalyzer_Anchor") { fkyaml::detail::lexical_token_t token; - SECTION("valid anchor name") - { + SECTION("valid anchor name") { auto input = GENERATE( std::string("&:anchor"), std::string("&:anchor "), @@ -1341,8 +1253,7 @@ TEST_CASE("LexicalAnalyzer_Anchor") REQUIRE_NOTHROW(lexer.get_string() == ":anchor"); } - SECTION("invalid anchor name") - { + SECTION("invalid anchor name") { auto input = GENERATE( std::string("&"), std::string("& "), @@ -1361,12 +1272,10 @@ TEST_CASE("LexicalAnalyzer_Anchor") } } -TEST_CASE("LexicalAnalyzer_Alias") -{ +TEST_CASE("LexicalAnalyzer_Alias") { fkyaml::detail::lexical_token_t token; - SECTION("valid anchor name") - { + SECTION("valid anchor name") { auto input = GENERATE( std::string("*:anchor"), std::string("*:anchor "), @@ -1387,8 +1296,7 @@ TEST_CASE("LexicalAnalyzer_Alias") REQUIRE_NOTHROW(lexer.get_string() == ":anchor"); } - SECTION("invalid anchor name") - { + SECTION("invalid anchor name") { auto input = GENERATE( std::string("*"), std::string("* "), @@ -1407,12 +1315,10 @@ TEST_CASE("LexicalAnalyzer_Alias") } } -TEST_CASE("LexicalAnalyzer_Tag") -{ +TEST_CASE("LexicalAnalyzer_Tag") { fkyaml::detail::lexical_token_t token; - SECTION("valid tag names") - { + SECTION("valid tag names") { auto input = GENERATE( std::string("! tag"), std::string("!\rtag"), @@ -1437,8 +1343,7 @@ TEST_CASE("LexicalAnalyzer_Tag") REQUIRE(lexer.get_string() == "tag"); } - SECTION("valid tag name (not followed by a value)") - { + SECTION("valid tag name (not followed by a value)") { auto input = GENERATE(std::string("!"), std::string("!!foo"), std::string("!foo!bar"), std::string("!foo")); lexer_t lexer(fkyaml::detail::input_adapter(input)); @@ -1447,8 +1352,7 @@ TEST_CASE("LexicalAnalyzer_Tag") REQUIRE(lexer.get_string() == input); } - SECTION("invalid tag names") - { + SECTION("invalid tag names") { auto input = GENERATE( std::string("!!f!oo tag"), std::string("! tag"), @@ -1464,15 +1368,13 @@ TEST_CASE("LexicalAnalyzer_Tag") } } -TEST_CASE("LexicalAnalyzer_ReservedIndicator") -{ +TEST_CASE("LexicalAnalyzer_ReservedIndicator") { auto buffer = GENERATE(std::string("@invalid"), std::string("`invalid")); lexer_t lexer(fkyaml::detail::input_adapter(buffer)); REQUIRE_THROWS_AS(lexer.get_next_token(), fkyaml::parse_error); } -TEST_CASE("LexicalAnalyzer_KeyBooleanValuePair") -{ +TEST_CASE("LexicalAnalyzer_KeyBooleanValuePair") { lexer_t lexer(fkyaml::detail::input_adapter("test: true")); fkyaml::detail::lexical_token_t token; @@ -1493,8 +1395,7 @@ TEST_CASE("LexicalAnalyzer_KeyBooleanValuePair") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } -TEST_CASE("LexicalAnalyzer_KeyIntegerValuePair") -{ +TEST_CASE("LexicalAnalyzer_KeyIntegerValuePair") { lexer_t lexer(fkyaml::detail::input_adapter("test: -5784")); fkyaml::detail::lexical_token_t token; @@ -1515,8 +1416,7 @@ TEST_CASE("LexicalAnalyzer_KeyIntegerValuePair") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } -TEST_CASE("LexicalAnalyzer_KeyFloatNumberValuePair") -{ +TEST_CASE("LexicalAnalyzer_KeyFloatNumberValuePair") { lexer_t lexer(fkyaml::detail::input_adapter("test: -5.58e-3")); fkyaml::detail::lexical_token_t token; @@ -1537,8 +1437,7 @@ TEST_CASE("LexicalAnalyzer_KeyFloatNumberValuePair") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } -TEST_CASE("LexicalAnalyzer_KeyStringValuePair") -{ +TEST_CASE("LexicalAnalyzer_KeyStringValuePair") { lexer_t lexer(fkyaml::detail::input_adapter("test: \"some value\"")); fkyaml::detail::lexical_token_t token; @@ -1559,12 +1458,10 @@ TEST_CASE("LexicalAnalyzer_KeyStringValuePair") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } -TEST_CASE("LexicalAnalyzer_FlowSequence") -{ +TEST_CASE("LexicalAnalyzer_FlowSequence") { fkyaml::detail::lexical_token_t token; - SECTION("simple flow sequence") - { + SECTION("simple flow sequence") { lexer_t lexer(fkyaml::detail::input_adapter("test: [ foo, bar ]")); REQUIRE_NOTHROW(token = lexer.get_next_token()); @@ -1598,8 +1495,7 @@ TEST_CASE("LexicalAnalyzer_FlowSequence") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("flow sequence with flow mapping child nodes") - { + SECTION("flow sequence with flow mapping child nodes") { lexer_t lexer(fkyaml::detail::input_adapter("test: [ { foo: one, bar: false }, { foo: two, bar: true } ]")); REQUIRE_NOTHROW(token = lexer.get_next_token()); @@ -1694,12 +1590,10 @@ TEST_CASE("LexicalAnalyzer_FlowSequence") } } -TEST_CASE("LexicalAnalyzer_FlowMapping") -{ +TEST_CASE("LexicalAnalyzer_FlowMapping") { fkyaml::detail::lexical_token_t token; - SECTION("simple flow mapping") - { + SECTION("simple flow mapping") { lexer_t lexer(fkyaml::detail::input_adapter("test: { bool: true, foo: bar, pi: 3.14 }")); REQUIRE_NOTHROW(token = lexer.get_next_token()); @@ -1765,8 +1659,7 @@ TEST_CASE("LexicalAnalyzer_FlowMapping") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("flow maping with a child mapping node") - { + SECTION("flow maping with a child mapping node") { lexer_t lexer(fkyaml::detail::input_adapter("test: {foo: bar baz}")); REQUIRE_NOTHROW(token = lexer.get_next_token()); @@ -1801,12 +1694,10 @@ TEST_CASE("LexicalAnalyzer_FlowMapping") } } -TEST_CASE("LexicalAnalyzer_BlockSequence") -{ +TEST_CASE("LexicalAnalyzer_BlockSequence") { fkyaml::detail::lexical_token_t token; - SECTION("simple block sequence") - { + SECTION("simple block sequence") { auto buffer = GENERATE(std::string("test:\n - foo\n - bar"), std::string("test:\r\n - foo\r\n - bar")); lexer_t lexer(fkyaml::detail::input_adapter(buffer)); @@ -1839,8 +1730,7 @@ TEST_CASE("LexicalAnalyzer_BlockSequence") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("block sequence with block mapping child nodes") - { + SECTION("block sequence with block mapping child nodes") { auto buffer = GENERATE( std::string("test:\r\n - foo: one\r\n bar: false\r\n - foo: two\r\n bar: true"), std::string("test:\n - foo: one\n bar: false\n - foo: two\n bar: true")); @@ -1918,12 +1808,10 @@ TEST_CASE("LexicalAnalyzer_BlockSequence") } } -TEST_CASE("LexicalAnalyzer_BlockMapping") -{ +TEST_CASE("LexicalAnalyzer_BlockMapping") { fkyaml::detail::lexical_token_t token; - SECTION("simple block mapping") - { + SECTION("simple block mapping") { lexer_t lexer(fkyaml::detail::input_adapter("test:\n bool: true\n foo: \'bar\'\n pi: 3.14")); REQUIRE_NOTHROW(token = lexer.get_next_token()); @@ -1977,8 +1865,7 @@ TEST_CASE("LexicalAnalyzer_BlockMapping") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("block mapping with a literal string scalar value") - { + SECTION("block mapping with a literal string scalar value") { lexer_t lexer(fkyaml::detail::input_adapter("test: |\n a literal scalar.\nfoo: \'bar\'\npi: 3.14")); REQUIRE_NOTHROW(token = lexer.get_next_token()); @@ -2024,8 +1911,7 @@ TEST_CASE("LexicalAnalyzer_BlockMapping") REQUIRE(token == fkyaml::detail::lexical_token_t::END_OF_BUFFER); } - SECTION("block mapping with a folded string scalar value") - { + SECTION("block mapping with a folded string scalar value") { lexer_t lexer(fkyaml::detail::input_adapter("test: >\n a literal scalar.\nfoo: \'bar\'\npi: 3.14")); REQUIRE_NOTHROW(token = lexer.get_next_token()); diff --git a/test/unit_test/test_node_class.cpp b/test/unit_test/test_node_class.cpp index 10f151d4..23d26faf 100644 --- a/test/unit_test/test_node_class.cpp +++ b/test/unit_test/test_node_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -20,73 +20,62 @@ #include #ifdef FK_YAML_HAS_CXX_17 - #include +#include #endif // // test cases for constructors // -TEST_CASE("Node_DefaultCtor") -{ +TEST_CASE("Node_DefaultCtor") { fkyaml::node node; REQUIRE(node.is_null()); } -TEST_CASE("Node_SequenceTypeCtor") -{ +TEST_CASE("Node_SequenceTypeCtor") { fkyaml::node node(fkyaml::node::node_t::SEQUENCE); REQUIRE(node.is_sequence()); REQUIRE(node.size() == 0); } -TEST_CASE("Node_MappingTypeCtor") -{ +TEST_CASE("Node_MappingTypeCtor") { fkyaml::node node(fkyaml::node::node_t::MAPPING); REQUIRE(node.is_mapping()); REQUIRE(node.size() == 0); } -TEST_CASE("Node_NullTypeCtor") -{ +TEST_CASE("Node_NullTypeCtor") { fkyaml::node node(fkyaml::node::node_t::NULL_OBJECT); REQUIRE(node.is_null()); } -TEST_CASE("Node_BooleanTypeCtor") -{ +TEST_CASE("Node_BooleanTypeCtor") { fkyaml::node node(fkyaml::node::node_t::BOOLEAN); REQUIRE(node.is_boolean()); REQUIRE(node.get_value_ref() == false); } -TEST_CASE("Node_IntegerTypeCtor") -{ +TEST_CASE("Node_IntegerTypeCtor") { fkyaml::node node(fkyaml::node::node_t::INTEGER); REQUIRE(node.is_integer()); REQUIRE(node.get_value_ref() == 0); } -TEST_CASE("Node_FloatNumberTypeCtor") -{ +TEST_CASE("Node_FloatNumberTypeCtor") { fkyaml::node node(fkyaml::node::node_t::FLOAT_NUMBER); REQUIRE(node.is_float_number()); REQUIRE(node.get_value_ref() == 0.0); } -TEST_CASE("Node_StringTypeCtor") -{ +TEST_CASE("Node_StringTypeCtor") { fkyaml::node node(fkyaml::node::node_t::STRING); REQUIRE(node.is_string()); REQUIRE(node.size() == 0); } -TEST_CASE("Node_ThrowingSpecializationTypeCtor") -{ - struct String - { - String() - { +TEST_CASE("Node_ThrowingSpecializationTypeCtor") { + struct String { + String() { throw fkyaml::exception(); } }; @@ -95,8 +84,7 @@ TEST_CASE("Node_ThrowingSpecializationTypeCtor") REQUIRE_THROWS_AS(NodeType(NodeType::node_t::STRING), fkyaml::exception); } -TEST_CASE("Node_SequenceCtor") -{ +TEST_CASE("Node_SequenceCtor") { fkyaml::node node(fkyaml::node::sequence_type {fkyaml::node(true), fkyaml::node(false)}); REQUIRE(node.type() == fkyaml::node::node_t::SEQUENCE); REQUIRE(node.is_sequence()); @@ -107,8 +95,7 @@ TEST_CASE("Node_SequenceCtor") REQUIRE(node[1].get_value_ref() == false); } -TEST_CASE("Node_MappingCtor") -{ +TEST_CASE("Node_MappingCtor") { fkyaml::node node(fkyaml::node::mapping_type {{"test", fkyaml::node(true)}}); REQUIRE(node.type() == fkyaml::node::node_t::MAPPING); REQUIRE(node.is_mapping()); @@ -118,39 +105,34 @@ TEST_CASE("Node_MappingCtor") REQUIRE(node["test"].get_value_ref() == true); } -TEST_CASE("Node_NullCtor") -{ +TEST_CASE("Node_NullCtor") { fkyaml::node node(nullptr); REQUIRE(node.type() == fkyaml::node::node_t::NULL_OBJECT); REQUIRE(node.is_null()); } -TEST_CASE("Node_BooleanCtor") -{ +TEST_CASE("Node_BooleanCtor") { fkyaml::node node(true); REQUIRE(node.type() == fkyaml::node::node_t::BOOLEAN); REQUIRE(node.is_boolean()); REQUIRE(node.get_value_ref() == true); } -TEST_CASE("Node_IntegerCtor") -{ +TEST_CASE("Node_IntegerCtor") { fkyaml::node node(23467); REQUIRE(node.type() == fkyaml::node::node_t::INTEGER); REQUIRE(node.is_integer()); REQUIRE(node.get_value_ref() == 23467); } -TEST_CASE("Node_FloatNumberCtor") -{ +TEST_CASE("Node_FloatNumberCtor") { fkyaml::node node(3.14); REQUIRE(node.type() == fkyaml::node::node_t::FLOAT_NUMBER); REQUIRE(node.is_float_number()); REQUIRE(node.get_value_ref() == 3.14); } -TEST_CASE("Node_StringCtor") -{ +TEST_CASE("Node_StringCtor") { auto node = GENERATE(fkyaml::node(std::string("test"))); REQUIRE(node.type() == fkyaml::node::node_t::STRING); REQUIRE(node.is_string()); @@ -159,8 +141,7 @@ TEST_CASE("Node_StringCtor") } #ifdef FK_YAML_HAS_CXX_17 -TEST_CASE("Node_StringViewCtor") -{ +TEST_CASE("Node_StringViewCtor") { using namespace std::string_view_literals; auto node = fkyaml::node("test"sv); REQUIRE(node.type() == fkyaml::node::node_t::STRING); @@ -170,8 +151,7 @@ TEST_CASE("Node_StringViewCtor") } #endif -TEST_CASE("Node_SequenceCopyCtor") -{ +TEST_CASE("Node_SequenceCopyCtor") { fkyaml::node n = "test"; fkyaml::node copied = {true, "test"}; @@ -191,8 +171,7 @@ TEST_CASE("Node_SequenceCopyCtor") REQUIRE(node[1].get_value_ref().compare("test") == 0); } -TEST_CASE("Node_MappingCopyCtor") -{ +TEST_CASE("Node_MappingCopyCtor") { fkyaml::node copied = {{"test0", 123}, {"test1", 3.14}}; fkyaml::node node(copied); REQUIRE(node.is_mapping()); @@ -208,15 +187,13 @@ TEST_CASE("Node_MappingCopyCtor") REQUIRE(node["test1"].get_value_ref() == 3.14); } -TEST_CASE("Node_NullCopyCtor") -{ +TEST_CASE("Node_NullCopyCtor") { fkyaml::node copied; fkyaml::node node(copied); REQUIRE(node.is_null()); } -TEST_CASE("Node_BooleanCopyCtor") -{ +TEST_CASE("Node_BooleanCopyCtor") { fkyaml::node copied = true; fkyaml::node node(copied); REQUIRE(node.is_boolean()); @@ -224,8 +201,7 @@ TEST_CASE("Node_BooleanCopyCtor") REQUIRE(node.get_value_ref() == true); } -TEST_CASE("Node_IntegerCopyCtor") -{ +TEST_CASE("Node_IntegerCopyCtor") { fkyaml::node copied = 123; fkyaml::node node(copied); REQUIRE(node.is_integer()); @@ -233,8 +209,7 @@ TEST_CASE("Node_IntegerCopyCtor") REQUIRE(node.get_value_ref() == 123); } -TEST_CASE("Node_FloatNumberCopyCtor") -{ +TEST_CASE("Node_FloatNumberCopyCtor") { fkyaml::node copied = 3.14; fkyaml::node node(copied); REQUIRE(node.is_float_number()); @@ -242,8 +217,7 @@ TEST_CASE("Node_FloatNumberCopyCtor") REQUIRE(node.get_value_ref() == 3.14); } -TEST_CASE("Node_StringCopyCtor") -{ +TEST_CASE("Node_StringCopyCtor") { fkyaml::node copied = "test"; fkyaml::node node(copied); REQUIRE(node.is_string()); @@ -253,8 +227,7 @@ TEST_CASE("Node_StringCopyCtor") REQUIRE(node.get_value_ref().compare("test") == 0); } -TEST_CASE("Node_AliasCopyCtor") -{ +TEST_CASE("Node_AliasCopyCtor") { fkyaml::node tmp = true; tmp.add_anchor_name("anchor_name"); fkyaml::node tmp_alias = fkyaml::node::alias_of(tmp); @@ -264,8 +237,7 @@ TEST_CASE("Node_AliasCopyCtor") REQUIRE(alias.get_value_ref() == true); } -TEST_CASE("Node_SequenceMoveCtor") -{ +TEST_CASE("Node_SequenceMoveCtor") { fkyaml::node moved = {true, "test"}; fkyaml::node node(std::move(moved)); REQUIRE(node.is_sequence()); @@ -283,8 +255,7 @@ TEST_CASE("Node_SequenceMoveCtor") REQUIRE(node[1].get_value_ref().compare("test") == 0); } -TEST_CASE("Node_MappingMoveCtor") -{ +TEST_CASE("Node_MappingMoveCtor") { fkyaml::node moved = {{"test0", 123}, {"test1", 3.14}}; fkyaml::node node(std::move(moved)); REQUIRE(node.is_mapping()); @@ -300,15 +271,13 @@ TEST_CASE("Node_MappingMoveCtor") REQUIRE(node["test1"].get_value_ref() == 3.14); } -TEST_CASE("Node_NullMoveCtor") -{ +TEST_CASE("Node_NullMoveCtor") { fkyaml::node moved; fkyaml::node node(std::move(moved)); REQUIRE(node.is_null()); } -TEST_CASE("Node_BooleanMoveCtor") -{ +TEST_CASE("Node_BooleanMoveCtor") { fkyaml::node moved = true; fkyaml::node node(std::move(moved)); REQUIRE(node.is_boolean()); @@ -316,8 +285,7 @@ TEST_CASE("Node_BooleanMoveCtor") REQUIRE(node.get_value_ref() == true); } -TEST_CASE("Node_IntegerMoveCtor") -{ +TEST_CASE("Node_IntegerMoveCtor") { fkyaml::node moved = 123; fkyaml::node node(std::move(moved)); REQUIRE(node.is_integer()); @@ -325,8 +293,7 @@ TEST_CASE("Node_IntegerMoveCtor") REQUIRE(node.get_value_ref() == 123); } -TEST_CASE("Node_FloatNumberMoveCtor") -{ +TEST_CASE("Node_FloatNumberMoveCtor") { fkyaml::node moved = 3.14; fkyaml::node node(std::move(moved)); REQUIRE(node.is_float_number()); @@ -334,8 +301,7 @@ TEST_CASE("Node_FloatNumberMoveCtor") REQUIRE(node.get_value_ref() == 3.14); } -TEST_CASE("Node_StringMoveCtor") -{ +TEST_CASE("Node_StringMoveCtor") { fkyaml::node moved = "test"; fkyaml::node node(std::move(moved)); REQUIRE(node.is_string()); @@ -345,8 +311,7 @@ TEST_CASE("Node_StringMoveCtor") REQUIRE(node.get_value_ref().compare("test") == 0); } -TEST_CASE("Node_AliasMoveCtor") -{ +TEST_CASE("Node_AliasMoveCtor") { fkyaml::node tmp = true; tmp.add_anchor_name("anchor_name"); fkyaml::node tmp_alias = fkyaml::node::alias_of(tmp); @@ -356,8 +321,7 @@ TEST_CASE("Node_AliasMoveCtor") REQUIRE(alias.get_value_ref() == true); } -TEST_CASE("Node_InitializerListCtor") -{ +TEST_CASE("Node_InitializerListCtor") { fkyaml::node node = { {"foo", 3.14}, {true, 123}, @@ -399,8 +363,7 @@ TEST_CASE("Node_InitializerListCtor") // test cases for serialization/deserialization features // -TEST_CASE("Node_Deserialize") -{ +TEST_CASE("Node_Deserialize") { char source[] = "foo: bar"; std::stringstream ss; ss << source; @@ -419,14 +382,12 @@ TEST_CASE("Node_Deserialize") REQUIRE(node["foo"].get_value_ref() == "bar"); } -TEST_CASE("Node_Serialize") -{ +TEST_CASE("Node_Serialize") { fkyaml::node node = fkyaml::node::deserialize("foo: bar"); REQUIRE(fkyaml::node::serialize(node) == "foo: bar\n"); } -TEST_CASE("Node_InsertionOperator") -{ +TEST_CASE("Node_InsertionOperator") { fkyaml::node node = {{"foo", 123}, {"bar", nullptr}, {"baz", true}}; std::stringstream ss; ss << node; @@ -434,8 +395,7 @@ TEST_CASE("Node_InsertionOperator") REQUIRE(ss.str() == "bar: null\nbaz: true\nfoo: 123\n"); } -TEST_CASE("Node_ExtractionOperator") -{ +TEST_CASE("Node_ExtractionOperator") { fkyaml::node node; std::ifstream ifs(FK_YAML_TEST_DATA_DIR "/extraction_operator_test_data.yml"); ifs >> node; @@ -447,10 +407,8 @@ TEST_CASE("Node_ExtractionOperator") REQUIRE(node["baz"].get_value() == true); } -TEST_CASE("Node_UserDefinedLiteralYaml") -{ - SECTION("char sequences literals with using fkyaml::literals") - { +TEST_CASE("Node_UserDefinedLiteralYaml") { + SECTION("char sequences literals with using fkyaml::literals") { using namespace fkyaml::literals; fkyaml::node node = "en: hello\njp: konnichiwa"_yaml; @@ -460,8 +418,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == "konnichiwa"); } - SECTION("char sequences literals with using fkyaml::yaml_literals") - { + SECTION("char sequences literals with using fkyaml::yaml_literals") { using namespace fkyaml::yaml_literals; fkyaml::node node = "en: hello\njp: konnichiwa"_yaml; @@ -471,8 +428,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == "konnichiwa"); } - SECTION("char sequences literals with using fkyaml::literals::yaml_literals") - { + SECTION("char sequences literals with using fkyaml::literals::yaml_literals") { using namespace fkyaml::literals::yaml_literals; fkyaml::node node = "en: hello\njp: konnichiwa"_yaml; @@ -482,8 +438,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == "konnichiwa"); } - SECTION("char sequences of u8\"\" literals with using fkyaml::literals") - { + SECTION("char sequences of u8\"\" literals with using fkyaml::literals") { using namespace fkyaml::literals; fkyaml::node node = u8"en: hello\njp: こんにちは"_yaml; @@ -493,8 +448,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == reinterpret_cast(u8"こんにちは")); } - SECTION("char sequences with u8 literal literals with using fkyaml::yaml_literals") - { + SECTION("char sequences with u8 literal literals with using fkyaml::yaml_literals") { using namespace fkyaml::yaml_literals; fkyaml::node node = u8"en: hello\njp: こんにちは"_yaml; @@ -504,8 +458,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == reinterpret_cast(u8"こんにちは")); } - SECTION("char sequences of u8 literal literals with using fkyaml::literals::yaml_literals") - { + SECTION("char sequences of u8 literal literals with using fkyaml::literals::yaml_literals") { using namespace fkyaml::literals::yaml_literals; fkyaml::node node = u8"en: hello\njp: こんにちは"_yaml; @@ -515,8 +468,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == reinterpret_cast(u8"こんにちは")); } - SECTION("char16_t sequences with using fkyaml::literals") - { + SECTION("char16_t sequences with using fkyaml::literals") { using namespace fkyaml::literals; fkyaml::node node = u"en: hello\njp: こんにちは"_yaml; @@ -526,8 +478,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == reinterpret_cast(u8"こんにちは")); } - SECTION("char16_t sequences with using fkyaml::yaml_literals") - { + SECTION("char16_t sequences with using fkyaml::yaml_literals") { using namespace fkyaml::yaml_literals; fkyaml::node node = u"en: hello\njp: こんにちは"_yaml; @@ -537,8 +488,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == reinterpret_cast(u8"こんにちは")); } - SECTION("char16_t sequences with using fkyaml::literals::yaml_literals") - { + SECTION("char16_t sequences with using fkyaml::literals::yaml_literals") { using namespace fkyaml::literals::yaml_literals; fkyaml::node node = u"en: hello\njp: こんにちは"_yaml; @@ -548,8 +498,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == reinterpret_cast(u8"こんにちは")); } - SECTION("char32_t sequences with using fkyaml::literals") - { + SECTION("char32_t sequences with using fkyaml::literals") { using namespace fkyaml::literals; fkyaml::node node = U"en: hello\njp: こんにちは"_yaml; @@ -559,8 +508,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == reinterpret_cast(u8"こんにちは")); } - SECTION("char32_t sequences with using fkyaml::yaml_literals") - { + SECTION("char32_t sequences with using fkyaml::yaml_literals") { using namespace fkyaml::yaml_literals; fkyaml::node node = U"en: hello\njp: こんにちは"_yaml; @@ -570,8 +518,7 @@ TEST_CASE("Node_UserDefinedLiteralYaml") REQUIRE(node["jp"].get_value_ref() == reinterpret_cast(u8"こんにちは")); } - SECTION("char32_t sequences with using fkyaml::literals::yaml_literals") - { + SECTION("char32_t sequences with using fkyaml::literals::yaml_literals") { using namespace fkyaml::literals::yaml_literals; fkyaml::node node = U"en: hello\njp: こんにちは"_yaml; @@ -586,58 +533,47 @@ TEST_CASE("Node_UserDefinedLiteralYaml") // test cases for factory methods // -TEST_CASE("Node_SequenceNodeFactory") -{ - SECTION("empty sequence") - { +TEST_CASE("Node_SequenceNodeFactory") { + SECTION("empty sequence") { fkyaml::node node = fkyaml::node::sequence(); REQUIRE(node.is_sequence()); REQUIRE(node.size() == 0); } - SECTION("non-empty sequence") - { + SECTION("non-empty sequence") { fkyaml::node::sequence_type seq(3); - SECTION("lvalue sequence") - { + SECTION("lvalue sequence") { fkyaml::node node = fkyaml::node::sequence(seq); REQUIRE(node.is_sequence()); REQUIRE(node.size() == 3); - for (int i = 0; i < 3; ++i) - { + for (int i = 0; i < 3; ++i) { REQUIRE(node[i].is_null()); } } - SECTION("lvalue sequence") - { + SECTION("lvalue sequence") { fkyaml::node node = fkyaml::node::sequence(std::move(seq)); REQUIRE(node.is_sequence()); REQUIRE(node.size() == 3); - for (int i = 0; i < 3; ++i) - { + for (int i = 0; i < 3; ++i) { REQUIRE(node[i].is_null()); } } } } -TEST_CASE("Node_MappingNodeFactory") -{ - SECTION("empty mapping") - { +TEST_CASE("Node_MappingNodeFactory") { + SECTION("empty mapping") { fkyaml::node node = fkyaml::node::mapping(); REQUIRE(node.is_mapping()); REQUIRE(node.size() == 0); } - SECTION("non-empty mapping node factory methods.") - { + SECTION("non-empty mapping node factory methods.") { fkyaml::node::mapping_type map {{"test", true}}; - SECTION("lvalue mapping") - { + SECTION("lvalue mapping") { fkyaml::node node = fkyaml::node::mapping(map); REQUIRE(node.is_mapping()); REQUIRE(node.size() == 1); @@ -645,8 +581,7 @@ TEST_CASE("Node_MappingNodeFactory") REQUIRE(node["test"].get_value_ref() == true); } - SECTION("rvalue mapping") - { + SECTION("rvalue mapping") { fkyaml::node node = fkyaml::node::mapping(std::move(map)); REQUIRE(node.is_mapping()); REQUIRE(node.size() == 1); @@ -656,16 +591,14 @@ TEST_CASE("Node_MappingNodeFactory") } } -TEST_CASE("Node_BooleanNodeFactory") -{ +TEST_CASE("Node_BooleanNodeFactory") { auto boolean = GENERATE(true, false); fkyaml::node node = boolean; REQUIRE(node.is_boolean()); REQUIRE(node.get_value_ref() == boolean); } -TEST_CASE("Node_IntegerNodeFactory") -{ +TEST_CASE("Node_IntegerNodeFactory") { auto integer = GENERATE( std::numeric_limits::min(), 0, @@ -675,8 +608,7 @@ TEST_CASE("Node_IntegerNodeFactory") REQUIRE(node.get_value_ref() == integer); } -TEST_CASE("Node_FloatNumberNodeFactory") -{ +TEST_CASE("Node_FloatNumberNodeFactory") { auto float_val = GENERATE( std::numeric_limits::min(), 3.141592, @@ -686,17 +618,14 @@ TEST_CASE("Node_FloatNumberNodeFactory") REQUIRE(node.get_value_ref() == float_val); } -TEST_CASE("Node_StringNodeFactory") -{ - SECTION("empty string") - { +TEST_CASE("Node_StringNodeFactory") { + SECTION("empty string") { fkyaml::node node = ""; REQUIRE(node.is_string()); REQUIRE(node.size() == 0); } - SECTION("lvalue string") - { + SECTION("lvalue string") { fkyaml::node::string_type str("test"); fkyaml::node node = str; REQUIRE(node.is_string()); @@ -704,8 +633,7 @@ TEST_CASE("Node_StringNodeFactory") REQUIRE(node.get_value_ref() == str); } - SECTION("rvalue string") - { + SECTION("rvalue string") { fkyaml::node node = "test"; REQUIRE(node.is_string()); REQUIRE(node.size() == 4); @@ -713,30 +641,25 @@ TEST_CASE("Node_StringNodeFactory") } } -TEST_CASE("Node_AliasNodeFactory") -{ +TEST_CASE("Node_AliasNodeFactory") { fkyaml::node anchor = "alias_test"; - SECTION("without anchor name.") - { + SECTION("without anchor name.") { REQUIRE_THROWS_AS(fkyaml::node::alias_of(anchor), fkyaml::exception); } - SECTION("with an empty anchor name") - { + SECTION("with an empty anchor name") { anchor.add_anchor_name(""); REQUIRE_THROWS_AS(fkyaml::node::alias_of(anchor), fkyaml::exception); } - SECTION("with an alias node") - { + SECTION("with an alias node") { anchor.add_anchor_name("anchor"); fkyaml::node alias = fkyaml::node::alias_of(anchor); REQUIRE_THROWS_AS(fkyaml::node::alias_of(alias), fkyaml::exception); } - SECTION("with an anchor node") - { + SECTION("with an anchor node") { anchor.add_anchor_name("anchor_name"); REQUIRE_NOTHROW(fkyaml::node::alias_of(anchor)); fkyaml::node alias = fkyaml::node::alias_of(anchor); @@ -749,116 +672,95 @@ TEST_CASE("Node_AliasNodeFactory") // test cases for subscript operators // -TEST_CASE("Node_SubscriptOperator") -{ - SECTION("mapping") - { +TEST_CASE("Node_SubscriptOperator") { + SECTION("mapping") { fkyaml::node::mapping_type map {{"test", fkyaml::node()}}; - SECTION("non-const string value") - { + SECTION("non-const string value") { fkyaml::node node = fkyaml::node::mapping(map); - SECTION("non-const lvalue string") - { + SECTION("non-const lvalue string") { std::string key = "test"; REQUIRE_NOTHROW(node[key]); REQUIRE(node[key].is_null()); } - SECTION("non-const rvalue string") - { + SECTION("non-const rvalue string") { REQUIRE_NOTHROW(node["test"]); REQUIRE(node["test"].is_null()); } } - SECTION("const string value") - { + SECTION("const string value") { const fkyaml::node node = fkyaml::node::mapping(map); std::string key = "test"; - SECTION("const lvalue string") - { + SECTION("const lvalue string") { REQUIRE_NOTHROW(node[key]); } - SECTION("const rvalue string") - { + SECTION("const rvalue string") { REQUIRE_NOTHROW(node["test"]); } } #ifdef FK_YAML_HAS_CXX_17 - SECTION("string view value") - { + SECTION("string view value") { std::string_view key = "test"; REQUIRE(map[key].is_null()); } #endif - SECTION("non-const string node") - { + SECTION("non-const string node") { fkyaml::node node = fkyaml::node::mapping(map); fkyaml::node node_key = "test"; - SECTION("non-const lvalue string node") - { + SECTION("non-const lvalue string node") { REQUIRE_NOTHROW(node[node_key]); } - SECTION("non-const rvalue string node") - { + SECTION("non-const rvalue string node") { REQUIRE_NOTHROW(node[std::move(node_key)]); } } - SECTION("const string node") - { + SECTION("const string node") { const fkyaml::node node = fkyaml::node::mapping(map); fkyaml::node node_key = "test"; - SECTION("non-const lvalue string node") - { + SECTION("non-const lvalue string node") { REQUIRE_NOTHROW(node[node_key]); } - SECTION("non-const rvalue string node") - { + SECTION("non-const rvalue string node") { REQUIRE_NOTHROW(node[std::move(node_key)]); } } } - SECTION("sequence") - { + SECTION("sequence") { fkyaml::node node = fkyaml::node::sequence(); node.get_value_ref().emplace_back(); - SECTION("non-const integer value") - { + SECTION("non-const integer value") { REQUIRE_NOTHROW(node[0]); } - SECTION("const integer value") - { + SECTION("const integer value") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node[0]); } - SECTION("non-const integer node") - { + SECTION("non-const integer node") { REQUIRE_NOTHROW(node[fkyaml::node(0)]); } - SECTION("const integer node") - { + SECTION("const integer node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node[fkyaml::node(0)]); } - SECTION("non-const node with a non-integer value") - { + SECTION("non-const node with a non-integer value") { REQUIRE_THROWS_AS(node[fkyaml::node::sequence_type()], fkyaml::type_error); REQUIRE_THROWS_AS(node[fkyaml::node::mapping_type()], fkyaml::type_error); REQUIRE_THROWS_AS(node[nullptr], fkyaml::type_error); @@ -867,8 +769,7 @@ TEST_CASE("Node_SubscriptOperator") REQUIRE_THROWS_AS(node[""], fkyaml::type_error); } - SECTION("const node with a non-integer value") - { + SECTION("const node with a non-integer value") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node[fkyaml::node::sequence_type()], fkyaml::type_error); REQUIRE_THROWS_AS(const_node[fkyaml::node::mapping_type()], fkyaml::type_error); @@ -878,8 +779,7 @@ TEST_CASE("Node_SubscriptOperator") REQUIRE_THROWS_AS(const_node[""], fkyaml::type_error); } - SECTION("non-const node with a non-integer node") - { + SECTION("non-const node with a non-integer node") { REQUIRE_THROWS_AS(node[fkyaml::node::sequence()], fkyaml::type_error); REQUIRE_THROWS_AS(node[fkyaml::node::mapping()], fkyaml::type_error); REQUIRE_THROWS_AS(node[fkyaml::node()], fkyaml::type_error); @@ -888,8 +788,7 @@ TEST_CASE("Node_SubscriptOperator") REQUIRE_THROWS_AS(node[fkyaml::node("")], fkyaml::type_error); } - SECTION("const node with a non-integer node") - { + SECTION("const node with a non-integer node") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node[fkyaml::node::sequence()], fkyaml::type_error); REQUIRE_THROWS_AS(const_node[fkyaml::node::mapping()], fkyaml::type_error); @@ -900,29 +799,24 @@ TEST_CASE("Node_SubscriptOperator") } } - SECTION("scalar") - { + SECTION("scalar") { auto node = GENERATE(fkyaml::node(), fkyaml::node(false), fkyaml::node(0), fkyaml::node(0.0), fkyaml::node("")); fkyaml::node node_key = 0; - SECTION("non-const node with an integer") - { + SECTION("non-const node with an integer") { REQUIRE_THROWS_AS(node[0], fkyaml::type_error); } - SECTION("const node with an integer") - { + SECTION("const node with an integer") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node[0], fkyaml::type_error); } - SECTION("non-const node with an integer node") - { + SECTION("non-const node with an integer node") { REQUIRE_THROWS_AS(node[node_key], fkyaml::type_error); } - SECTION("const node with an integer node") - { + SECTION("const node with an integer node") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node[node_key], fkyaml::type_error); } @@ -933,10 +827,8 @@ TEST_CASE("Node_SubscriptOperator") // test cases for operators for comparisons between nodes // -TEST_CASE("Node_EqualToOperator") -{ - SECTION("The same type and value") - { +TEST_CASE("Node_EqualToOperator") { + SECTION("The same type and value") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123, "foo"}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -948,8 +840,7 @@ TEST_CASE("Node_EqualToOperator") REQUIRE(params[0] == params[1]); } - SECTION("The same type but different values") - { + SECTION("The same type but different values") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {false, 456, "bar"}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"baz", 456}, {"qux", false}}}, @@ -960,8 +851,7 @@ TEST_CASE("Node_EqualToOperator") REQUIRE_FALSE(params[0] == params[1]); } - SECTION("Different types") - { + SECTION("Different types") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {{"foo", 123}, {"bar", true}}}, fkyaml::node {{true, 123, "foo"}, nullptr}, @@ -1009,10 +899,8 @@ TEST_CASE("Node_EqualToOperator") } } -TEST_CASE("Node_NotEqualToOperator") -{ - SECTION("The same type and value") - { +TEST_CASE("Node_NotEqualToOperator") { + SECTION("The same type and value") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123, "foo"}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1024,8 +912,7 @@ TEST_CASE("Node_NotEqualToOperator") REQUIRE_FALSE(params[0] != params[1]); } - SECTION("The same type but different values") - { + SECTION("The same type but different values") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {false, 456, "bar"}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"baz", 456}, {"qux", false}}}, @@ -1036,8 +923,7 @@ TEST_CASE("Node_NotEqualToOperator") REQUIRE(params[0] != params[1]); } - SECTION("Different types") - { + SECTION("Different types") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {{"foo", 123}, {"bar", true}}}, fkyaml::node {{true, 123, "foo"}, nullptr}, @@ -1085,10 +971,8 @@ TEST_CASE("Node_NotEqualToOperator") } } -TEST_CASE("Node_LessThanOperator") -{ - SECTION("The same type and value") - { +TEST_CASE("Node_LessThanOperator") { + SECTION("The same type and value") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123, "foo"}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1100,8 +984,7 @@ TEST_CASE("Node_LessThanOperator") REQUIRE_FALSE(params[0] < params[1]); } - SECTION("The same type and the target value is less than the compared one") - { + SECTION("The same type and the target value is less than the compared one") { auto params = GENERATE( fkyaml::node {{true, 123}, {true, 123, "foo"}}, fkyaml::node {{{"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1112,8 +995,7 @@ TEST_CASE("Node_LessThanOperator") REQUIRE(params[0] < params[1]); } - SECTION("The same type but the target value is greater than the compared one") - { + SECTION("The same type but the target value is greater than the compared one") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"bar", true}}}, @@ -1124,8 +1006,7 @@ TEST_CASE("Node_LessThanOperator") REQUIRE_FALSE(params[0] < params[1]); } - SECTION("The numeric value of the target type is less than that of the compared one") - { + SECTION("The numeric value of the target type is less than that of the compared one") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {{"foo", 123}, {"bar", true}}}, fkyaml::node {{true, 123, "foo"}, nullptr}, @@ -1151,8 +1032,7 @@ TEST_CASE("Node_LessThanOperator") REQUIRE(params[0] < params[1]); } - SECTION("The numeric value of the target type is greater than that of the compared one") - { + SECTION("The numeric value of the target type is greater than that of the compared one") { auto params = GENERATE( fkyaml::node {{{"foo", 123}, {"bar", true}}, {true, 123, "foo"}}, fkyaml::node {nullptr, {true, 123, "foo"}}, @@ -1179,10 +1059,8 @@ TEST_CASE("Node_LessThanOperator") } } -TEST_CASE("Node_LessThanOrEqualToOperator") -{ - SECTION("The same type and value") - { +TEST_CASE("Node_LessThanOrEqualToOperator") { + SECTION("The same type and value") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123, "foo"}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1194,8 +1072,7 @@ TEST_CASE("Node_LessThanOrEqualToOperator") REQUIRE(params[0] <= params[1]); } - SECTION("The same type and the target value is less than the compared one") - { + SECTION("The same type and the target value is less than the compared one") { auto params = GENERATE( fkyaml::node {{true, 123}, {true, 123, "foo"}}, fkyaml::node {{{"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1206,8 +1083,7 @@ TEST_CASE("Node_LessThanOrEqualToOperator") REQUIRE(params[0] <= params[1]); } - SECTION("The same type but the target value is greater than the compared one") - { + SECTION("The same type but the target value is greater than the compared one") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"bar", true}}}, @@ -1218,8 +1094,7 @@ TEST_CASE("Node_LessThanOrEqualToOperator") REQUIRE_FALSE(params[0] <= params[1]); } - SECTION("The numeric value of the target type is less than that of the compared one") - { + SECTION("The numeric value of the target type is less than that of the compared one") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {{"foo", 123}, {"bar", true}}}, fkyaml::node {{true, 123, "foo"}, nullptr}, @@ -1245,8 +1120,7 @@ TEST_CASE("Node_LessThanOrEqualToOperator") REQUIRE(params[0] <= params[1]); } - SECTION("The numeric value of the target type is greater than that of the compared one") - { + SECTION("The numeric value of the target type is greater than that of the compared one") { auto params = GENERATE( fkyaml::node {{{"foo", 123}, {"bar", true}}, {true, 123, "foo"}}, fkyaml::node {nullptr, {true, 123, "foo"}}, @@ -1273,10 +1147,8 @@ TEST_CASE("Node_LessThanOrEqualToOperator") } } -TEST_CASE("Node_GreaterThanOperator") -{ - SECTION("The same type and value") - { +TEST_CASE("Node_GreaterThanOperator") { + SECTION("The same type and value") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123, "foo"}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1288,8 +1160,7 @@ TEST_CASE("Node_GreaterThanOperator") REQUIRE_FALSE(params[0] > params[1]); } - SECTION("The same type and the target value is less than the compared one") - { + SECTION("The same type and the target value is less than the compared one") { auto params = GENERATE( fkyaml::node {{true, 123}, {true, 123, "foo"}}, fkyaml::node {{{"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1300,8 +1171,7 @@ TEST_CASE("Node_GreaterThanOperator") REQUIRE_FALSE(params[0] > params[1]); } - SECTION("The same type but the target value is greater than the compared one") - { + SECTION("The same type but the target value is greater than the compared one") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"bar", true}}}, @@ -1312,8 +1182,7 @@ TEST_CASE("Node_GreaterThanOperator") REQUIRE(params[0] > params[1]); } - SECTION("The numeric value of the target type is less than that of the compared one") - { + SECTION("The numeric value of the target type is less than that of the compared one") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {{"foo", 123}, {"bar", true}}}, fkyaml::node {{true, 123, "foo"}, nullptr}, @@ -1339,8 +1208,7 @@ TEST_CASE("Node_GreaterThanOperator") REQUIRE_FALSE(params[0] > params[1]); } - SECTION("The numeric value of the target type is greater than that of the compared one") - { + SECTION("The numeric value of the target type is greater than that of the compared one") { auto params = GENERATE( fkyaml::node {{{"foo", 123}, {"bar", true}}, {true, 123, "foo"}}, fkyaml::node {nullptr, {true, 123, "foo"}}, @@ -1367,10 +1235,8 @@ TEST_CASE("Node_GreaterThanOperator") } } -TEST_CASE("Node_GreaterThanOrEqualToOperator") -{ - SECTION("The same type and value") - { +TEST_CASE("Node_GreaterThanOrEqualToOperator") { + SECTION("The same type and value") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123, "foo"}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1382,8 +1248,7 @@ TEST_CASE("Node_GreaterThanOrEqualToOperator") REQUIRE(params[0] >= params[1]); } - SECTION("The same type and the target value is less than the compared one") - { + SECTION("The same type and the target value is less than the compared one") { auto params = GENERATE( fkyaml::node {{true, 123}, {true, 123, "foo"}}, fkyaml::node {{{"bar", true}}, {{"foo", 123}, {"bar", true}}}, @@ -1394,8 +1259,7 @@ TEST_CASE("Node_GreaterThanOrEqualToOperator") REQUIRE_FALSE(params[0] >= params[1]); } - SECTION("The same type but the target value is greater than the compared one") - { + SECTION("The same type but the target value is greater than the compared one") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {true, 123}}, fkyaml::node {{{"foo", 123}, {"bar", true}}, {{"bar", true}}}, @@ -1406,8 +1270,7 @@ TEST_CASE("Node_GreaterThanOrEqualToOperator") REQUIRE(params[0] >= params[1]); } - SECTION("The numeric value of the target type is less than that of the compared one") - { + SECTION("The numeric value of the target type is less than that of the compared one") { auto params = GENERATE( fkyaml::node {{true, 123, "foo"}, {{"foo", 123}, {"bar", true}}}, fkyaml::node {{true, 123, "foo"}, nullptr}, @@ -1433,8 +1296,7 @@ TEST_CASE("Node_GreaterThanOrEqualToOperator") REQUIRE_FALSE(params[0] >= params[1]); } - SECTION("The numeric value of the target type is greater than that of the compared one") - { + SECTION("The numeric value of the target type is greater than that of the compared one") { auto params = GENERATE( fkyaml::node {{{"foo", 123}, {"bar", true}}, {true, 123, "foo"}}, fkyaml::node {nullptr, {true, 123, "foo"}}, @@ -1465,8 +1327,7 @@ TEST_CASE("Node_GreaterThanOrEqualToOperator") // test cases for type property getter/checkers // -TEST_CASE("Node_Type") -{ +TEST_CASE("Node_Type") { using NodeTypePair = std::pair; auto type_pair = GENERATE( NodeTypePair(fkyaml::node::sequence(), fkyaml::node::node_t::SEQUENCE), @@ -1477,40 +1338,33 @@ TEST_CASE("Node_Type") NodeTypePair(fkyaml::node(0.0), fkyaml::node::node_t::FLOAT_NUMBER), NodeTypePair(fkyaml::node(""), fkyaml::node::node_t::STRING)); - SECTION("non-alias node types") - { + SECTION("non-alias node types") { REQUIRE(type_pair.first.type() == type_pair.second); } - SECTION("alias node types") - { + SECTION("alias node types") { type_pair.first.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(type_pair.first); REQUIRE(alias.type() == type_pair.second); } } -TEST_CASE("Node_IsSequence") -{ - SECTION("sequence node type") - { +TEST_CASE("Node_IsSequence") { + SECTION("sequence node type") { fkyaml::node node = fkyaml::node::sequence(); - SECTION("non-alias sequence node type") - { + SECTION("non-alias sequence node type") { REQUIRE(node.is_sequence()); } - SECTION("alias sequence node type") - { + SECTION("alias sequence node type") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE(alias.is_sequence()); } } - SECTION("non-sequence node types") - { + SECTION("non-sequence node types") { auto node = GENERATE( fkyaml::node::mapping(), fkyaml::node(), @@ -1519,13 +1373,11 @@ TEST_CASE("Node_IsSequence") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-sequence node types") - { + SECTION("non-alias non-sequence node types") { REQUIRE_FALSE(node.is_sequence()); } - SECTION("alias non-sequence node types.") - { + SECTION("alias non-sequence node types.") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_FALSE(alias.is_sequence()); @@ -1533,27 +1385,22 @@ TEST_CASE("Node_IsSequence") } } -TEST_CASE("Node_IsMapping") -{ - SECTION("mapping node type") - { +TEST_CASE("Node_IsMapping") { + SECTION("mapping node type") { fkyaml::node node = fkyaml::node::mapping(); - SECTION("non-alias mapping node type") - { + SECTION("non-alias mapping node type") { REQUIRE(node.is_mapping()); } - SECTION("alias mapping node type") - { + SECTION("alias mapping node type") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE(alias.is_mapping()); } } - SECTION("non-mapping node types") - { + SECTION("non-mapping node types") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node(), @@ -1562,13 +1409,11 @@ TEST_CASE("Node_IsMapping") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-mapping node types") - { + SECTION("non-alias non-mapping node types") { REQUIRE_FALSE(node.is_mapping()); } - SECTION("alias non-mapping node types.") - { + SECTION("alias non-mapping node types.") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_FALSE(alias.is_mapping()); @@ -1576,27 +1421,22 @@ TEST_CASE("Node_IsMapping") } } -TEST_CASE("Node_IsNull") -{ - SECTION("null node type") - { +TEST_CASE("Node_IsNull") { + SECTION("null node type") { fkyaml::node node = fkyaml::node(); - SECTION("non-alias null node type") - { + SECTION("non-alias null node type") { REQUIRE(node.is_null()); } - SECTION("alias null node type") - { + SECTION("alias null node type") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE(alias.is_null()); } } - SECTION("non-null node types") - { + SECTION("non-null node types") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -1605,13 +1445,11 @@ TEST_CASE("Node_IsNull") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-null node types") - { + SECTION("non-alias non-null node types") { REQUIRE_FALSE(node.is_null()); } - SECTION("alias non-null node types") - { + SECTION("alias non-null node types") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_FALSE(alias.is_null()); @@ -1619,27 +1457,22 @@ TEST_CASE("Node_IsNull") } } -TEST_CASE("Node_IsBoolean") -{ - SECTION("boolean node type") - { +TEST_CASE("Node_IsBoolean") { + SECTION("boolean node type") { fkyaml::node node = false; - SECTION("non-alias boolean node type") - { + SECTION("non-alias boolean node type") { REQUIRE(node.is_boolean()); } - SECTION("alias boolean node type") - { + SECTION("alias boolean node type") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE(alias.is_boolean()); } } - SECTION("non-boolean node types") - { + SECTION("non-boolean node types") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -1648,13 +1481,11 @@ TEST_CASE("Node_IsBoolean") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-boolean node types") - { + SECTION("non-alias non-boolean node types") { REQUIRE_FALSE(node.is_boolean()); } - SECTION("alias non-boolean node types") - { + SECTION("alias non-boolean node types") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_FALSE(alias.is_boolean()); @@ -1662,27 +1493,22 @@ TEST_CASE("Node_IsBoolean") } } -TEST_CASE("Node_IsInteger") -{ - SECTION("integer node type.") - { +TEST_CASE("Node_IsInteger") { + SECTION("integer node type.") { fkyaml::node node = 0; - SECTION("non-alias integer node type.") - { + SECTION("non-alias integer node type.") { REQUIRE(node.is_integer()); } - SECTION("alias integer node type.") - { + SECTION("alias integer node type.") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE(alias.is_integer()); } } - SECTION("non-integer node types.") - { + SECTION("non-integer node types.") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -1691,13 +1517,11 @@ TEST_CASE("Node_IsInteger") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-integer node types") - { + SECTION("non-alias non-integer node types") { REQUIRE_FALSE(node.is_integer()); } - SECTION("alias non-integer node types") - { + SECTION("alias non-integer node types") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_FALSE(alias.is_integer()); @@ -1705,27 +1529,22 @@ TEST_CASE("Node_IsInteger") } } -TEST_CASE("Node_IsFloatNumber") -{ - SECTION("float number node type") - { +TEST_CASE("Node_IsFloatNumber") { + SECTION("float number node type") { fkyaml::node node = 0.0; - SECTION("non-alias float number node type") - { + SECTION("non-alias float number node type") { REQUIRE(node.is_float_number()); } - SECTION("alias float number node type") - { + SECTION("alias float number node type") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE(alias.is_float_number()); } } - SECTION("non-float-number node types") - { + SECTION("non-float-number node types") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -1734,13 +1553,11 @@ TEST_CASE("Node_IsFloatNumber") fkyaml::node(0), fkyaml::node("")); - SECTION("non-alias non-float-number node types") - { + SECTION("non-alias non-float-number node types") { REQUIRE_FALSE(node.is_float_number()); } - SECTION("alias non-float-number node types") - { + SECTION("alias non-float-number node types") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_FALSE(alias.is_float_number()); @@ -1748,27 +1565,22 @@ TEST_CASE("Node_IsFloatNumber") } } -TEST_CASE("Node_IsString") -{ - SECTION("string node type") - { +TEST_CASE("Node_IsString") { + SECTION("string node type") { fkyaml::node node = ""; - SECTION("non-alias string node type") - { + SECTION("non-alias string node type") { REQUIRE(node.is_string()); } - SECTION("alias string node type") - { + SECTION("alias string node type") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE(alias.is_string()); } } - SECTION("non-string node types") - { + SECTION("non-string node types") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -1777,13 +1589,11 @@ TEST_CASE("Node_IsString") fkyaml::node(0), fkyaml::node(0.0)); - SECTION("non-alias non-string node types") - { + SECTION("non-alias non-string node types") { REQUIRE_FALSE(node.is_string()); } - SECTION("alias non-string node types") - { + SECTION("alias non-string node types") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_FALSE(alias.is_string()); @@ -1791,36 +1601,29 @@ TEST_CASE("Node_IsString") } } -TEST_CASE("Node_IsScalar") -{ - SECTION("scalar node types") - { +TEST_CASE("Node_IsScalar") { + SECTION("scalar node types") { auto node = GENERATE(fkyaml::node(), fkyaml::node(false), fkyaml::node(0), fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias scalar node types") - { + SECTION("non-alias scalar node types") { REQUIRE(node.is_scalar()); } - SECTION("alias scalar node types") - { + SECTION("alias scalar node types") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE(alias.is_scalar()); } } - SECTION("non-scalar node types") - { + SECTION("non-scalar node types") { auto node = GENERATE(fkyaml::node::sequence(), fkyaml::node::mapping()); - SECTION("non-alias non-scalar node types") - { + SECTION("non-alias non-scalar node types") { REQUIRE_FALSE(node.is_scalar()); } - SECTION("alias non-scalar node types") - { + SECTION("alias non-scalar node types") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_FALSE(alias.is_scalar()); @@ -1832,22 +1635,17 @@ TEST_CASE("Node_IsScalar") // test cases for emptiness checker // -TEST_CASE("Node_Empty") -{ - SECTION("container and string scalar nodes") - { - SECTION("empty node") - { +TEST_CASE("Node_Empty") { + SECTION("container and string scalar nodes") { + SECTION("empty node") { auto node = GENERATE(fkyaml::node::sequence(), fkyaml::node::mapping(), fkyaml::node("")); - SECTION("empty non-alias node") - { + SECTION("empty non-alias node") { REQUIRE_NOTHROW(node.empty()); REQUIRE(node.empty()); } - SECTION("empty alias node") - { + SECTION("empty alias node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.empty()); @@ -1855,21 +1653,18 @@ TEST_CASE("Node_Empty") } } - SECTION("non-empty node") - { + SECTION("non-empty node") { auto node = GENERATE( fkyaml::node::sequence(fkyaml::node::sequence_type(3)), fkyaml::node::mapping(fkyaml::node::mapping_type {{"test", fkyaml::node()}}), fkyaml::node("test")); - SECTION("non-empty non-alias node") - { + SECTION("non-empty non-alias node") { REQUIRE_NOTHROW(node.empty()); REQUIRE_FALSE(node.empty()); } - SECTION("non-empty alias node") - { + SECTION("non-empty alias node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.empty()); @@ -1878,30 +1673,25 @@ TEST_CASE("Node_Empty") } } - SECTION("non-string scalar nodes") - { + SECTION("non-string scalar nodes") { auto node = GENERATE(fkyaml::node(), fkyaml::node(false), fkyaml::node(0), fkyaml::node(0.0)); - SECTION("non-const non-alias node") - { + SECTION("non-const non-alias node") { REQUIRE_THROWS_AS(node.empty(), fkyaml::type_error); } - SECTION("const non-alias node") - { + SECTION("const non-alias node") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.empty(), fkyaml::type_error); } - SECTION("non-const alias node") - { + SECTION("non-const alias node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.empty(), fkyaml::type_error); } - SECTION("const alias node") - { + SECTION("const alias node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.empty(), fkyaml::type_error); @@ -1913,34 +1703,28 @@ TEST_CASE("Node_Empty") // test cases for mapping key existence checker // -TEST_CASE("Node_Contains") -{ - SECTION("mapping") - { +TEST_CASE("Node_Contains") { + SECTION("mapping") { fkyaml::node node = fkyaml::node::mapping({{"test", fkyaml::node()}}); - SECTION("mapping node with a string key") - { + SECTION("mapping node with a string key") { REQUIRE(node.contains("test")); } #ifdef FK_YAML_HAS_CXX_17 - SECTION("mapping node with a string view key") - { + SECTION("mapping node with a string view key") { using namespace std::string_view_literals; REQUIRE(node.contains("test"sv)); } #endif - SECTION("mapping node with a string node key") - { + SECTION("mapping node with a string node key") { fkyaml::node node_key = "test"; REQUIRE(node.contains(node_key)); } } - SECTION("non-mapping") - { + SECTION("non-mapping") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node(), @@ -1949,13 +1733,11 @@ TEST_CASE("Node_Contains") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-mapping node with a key") - { + SECTION("non-mapping node with a key") { REQUIRE_FALSE(node.contains("test")); } - SECTION("non-mapping node with a node key") - { + SECTION("non-mapping node with a node key") { fkyaml::node node_key = "test"; REQUIRE_FALSE(node.contains(node_key)); } @@ -1966,38 +1748,32 @@ TEST_CASE("Node_Contains") // test cases for container size getter // -TEST_CASE("Node_Size") -{ - SECTION("container and string scalar nodes") - { +TEST_CASE("Node_Size") { + SECTION("container and string scalar nodes") { auto node = GENERATE( fkyaml::node::sequence({fkyaml::node(), fkyaml::node(), fkyaml::node()}), fkyaml::node::mapping({{"test0", fkyaml::node()}, {"test1", fkyaml::node()}, {"test2", fkyaml::node()}}), fkyaml::node("tmp")); - SECTION("non-const non-alias node") - { + SECTION("non-const non-alias node") { REQUIRE_NOTHROW(node.size()); REQUIRE(node.size() == 3); } - SECTION("const node") - { + SECTION("const node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(node.size()); REQUIRE(const_node.size() == 3); } - SECTION("alias node") - { + SECTION("alias node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.size()); REQUIRE(alias.size() == 3); } - SECTION("const alias node") - { + SECTION("const alias node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.size()); @@ -2005,30 +1781,25 @@ TEST_CASE("Node_Size") } } - SECTION("non-string scalar nodes") - { + SECTION("non-string scalar nodes") { auto node = GENERATE(fkyaml::node(), fkyaml::node(false), fkyaml::node(0), fkyaml::node(0.0)); - SECTION("non-const non-alias node") - { + SECTION("non-const non-alias node") { REQUIRE_THROWS_AS(node.size(), fkyaml::type_error); } - SECTION("const non-alias node") - { + SECTION("const non-alias node") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.size(), fkyaml::type_error); } - SECTION("non-const alias node") - { + SECTION("non-const alias node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.size(), fkyaml::type_error); } - SECTION("const alias node") - { + SECTION("const alias node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.size(), fkyaml::type_error); @@ -2040,93 +1811,76 @@ TEST_CASE("Node_Size") // test cases for container element accessor with bounds checks // -TEST_CASE("Node_At") -{ - SECTION("mapping with existing key argument") - { +TEST_CASE("Node_At") { + SECTION("mapping with existing key argument") { fkyaml::node::mapping_type map {{"test", fkyaml::node()}}; - SECTION("non-const string value") - { + SECTION("non-const string value") { fkyaml::node node = fkyaml::node::mapping(map); - SECTION("non-const lvalue string value") - { + SECTION("non-const lvalue string value") { std::string key = "test"; REQUIRE_NOTHROW(node.at(key)); REQUIRE(node[key].is_null()); } - SECTION("non-const rvalue string value") - { + SECTION("non-const rvalue string value") { REQUIRE_NOTHROW(node.at("test")); REQUIRE(node.at("test").is_null()); } } - SECTION("const string value") - { + SECTION("const string value") { const fkyaml::node node = fkyaml::node::mapping(map); std::string key = "test"; - SECTION("const lvalue string node") - { + SECTION("const lvalue string node") { REQUIRE_NOTHROW(node.at(key)); } - SECTION("const rvalue string node") - { + SECTION("const rvalue string node") { REQUIRE_NOTHROW(node.at("test")); } } #ifdef FK_YAML_HAS_CXX_17 - SECTION("string view value") - { + SECTION("string view value") { std::string_view key = "test"; REQUIRE(map.at(key).is_null()); } #endif - SECTION("non-const string node") - { + SECTION("non-const string node") { fkyaml::node node = fkyaml::node::mapping(map); fkyaml::node node_key = "test"; - SECTION("non-const lvalue string node") - { + SECTION("non-const lvalue string node") { REQUIRE_NOTHROW(node.at(node_key)); } - SECTION("non-const rvalue string node") - { + SECTION("non-const rvalue string node") { REQUIRE_NOTHROW(node.at(std::move(node_key))); } } - SECTION("const string node") - { + SECTION("const string node") { const fkyaml::node node = fkyaml::node::mapping(map); fkyaml::node node_key = "test"; - SECTION("non-const lvalue string node") - { + SECTION("non-const lvalue string node") { REQUIRE_NOTHROW(node.at(node_key)); } - SECTION("non-const rvalue string node") - { + SECTION("non-const rvalue string node") { REQUIRE_NOTHROW(node.at(std::move(node_key))); } } } - SECTION("mapping with non-existing key argument") - { + SECTION("mapping with non-existing key argument") { fkyaml::node node = {{"foo", 123}}; - SECTION("compatible type objects") - { + SECTION("compatible type objects") { REQUIRE_THROWS_AS(node.at(fkyaml::node::sequence_type()), fkyaml::out_of_range); REQUIRE_THROWS_AS(node.at(fkyaml::node::mapping_type()), fkyaml::out_of_range); REQUIRE_THROWS_AS(node.at(nullptr), fkyaml::out_of_range); @@ -2136,8 +1890,7 @@ TEST_CASE("Node_At") REQUIRE_THROWS_AS(node.at("bar"), fkyaml::out_of_range); } - SECTION("compatible type objects (const)") - { + SECTION("compatible type objects (const)") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.at(fkyaml::node::sequence_type()), fkyaml::out_of_range); REQUIRE_THROWS_AS(const_node.at(fkyaml::node::mapping_type()), fkyaml::out_of_range); @@ -2148,8 +1901,7 @@ TEST_CASE("Node_At") REQUIRE_THROWS_AS(const_node.at("bar"), fkyaml::out_of_range); } - SECTION("basic_node objects") - { + SECTION("basic_node objects") { REQUIRE_THROWS_AS(node.at(fkyaml::node::sequence()), fkyaml::out_of_range); REQUIRE_THROWS_AS(node.at(fkyaml::node::mapping()), fkyaml::out_of_range); REQUIRE_THROWS_AS(node.at(fkyaml::node()), fkyaml::out_of_range); @@ -2159,8 +1911,7 @@ TEST_CASE("Node_At") REQUIRE_THROWS_AS(node.at(fkyaml::node("bar")), fkyaml::out_of_range); } - SECTION("basic_node objects (const)") - { + SECTION("basic_node objects (const)") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.at(fkyaml::node::sequence()), fkyaml::out_of_range); REQUIRE_THROWS_AS(const_node.at(fkyaml::node::mapping()), fkyaml::out_of_range); @@ -2172,41 +1923,34 @@ TEST_CASE("Node_At") } } - SECTION("sequence with integer argument") - { + SECTION("sequence with integer argument") { fkyaml::node node = fkyaml::node::sequence(); node.get_value_ref().emplace_back(); - SECTION("non-const integer value") - { + SECTION("non-const integer value") { REQUIRE_NOTHROW(node.at(0)); } - SECTION("const integer value") - { + SECTION("const integer value") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.at(0)); } - SECTION("non-const integer node") - { + SECTION("non-const integer node") { REQUIRE_NOTHROW(node.at(fkyaml::node(0))); } - SECTION("const integer node") - { + SECTION("const integer node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.at(fkyaml::node(0))); } } - SECTION("sequence with non-integer argument") - { + SECTION("sequence with non-integer argument") { fkyaml::node node = fkyaml::node::sequence(); node.get_value_ref().emplace_back(); - SECTION("non-const node with a non-integer value") - { + SECTION("non-const node with a non-integer value") { REQUIRE_THROWS_AS(node.at(fkyaml::node::sequence_type()), fkyaml::type_error); REQUIRE_THROWS_AS(node.at(fkyaml::node::mapping_type()), fkyaml::type_error); REQUIRE_THROWS_AS(node.at(nullptr), fkyaml::type_error); @@ -2215,8 +1959,7 @@ TEST_CASE("Node_At") REQUIRE_THROWS_AS(node.at(""), fkyaml::type_error); } - SECTION("const node with a non-integer value") - { + SECTION("const node with a non-integer value") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.at(fkyaml::node::sequence_type()), fkyaml::type_error); REQUIRE_THROWS_AS(const_node.at(fkyaml::node::mapping_type()), fkyaml::type_error); @@ -2226,8 +1969,7 @@ TEST_CASE("Node_At") REQUIRE_THROWS_AS(const_node.at(""), fkyaml::type_error); } - SECTION("non-const node with a non-integer node") - { + SECTION("non-const node with a non-integer node") { REQUIRE_THROWS_AS(node.at(fkyaml::node::mapping()), fkyaml::type_error); REQUIRE_THROWS_AS(node.at(fkyaml::node::sequence()), fkyaml::type_error); REQUIRE_THROWS_AS(node.at(fkyaml::node()), fkyaml::type_error); @@ -2235,8 +1977,7 @@ TEST_CASE("Node_At") REQUIRE_THROWS_AS(node.at(fkyaml::node("")), fkyaml::type_error); } - SECTION("const node with a non-integer node") - { + SECTION("const node with a non-integer node") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.at(fkyaml::node::mapping()), fkyaml::type_error); REQUIRE_THROWS_AS(const_node.at(fkyaml::node::sequence()), fkyaml::type_error); @@ -2246,48 +1987,40 @@ TEST_CASE("Node_At") } } - SECTION("sequence with out-of-bounds integer argument") - { + SECTION("sequence with out-of-bounds integer argument") { fkyaml::node node = fkyaml::node::sequence(); node.get_value_ref().emplace_back(); - SECTION("non-const argument") - { + SECTION("non-const argument") { REQUIRE_THROWS_AS(node.at(1), fkyaml::out_of_range); REQUIRE_THROWS_AS(node.at(fkyaml::node(1)), fkyaml::out_of_range); } - SECTION("const argument") - { + SECTION("const argument") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.at(1), fkyaml::out_of_range); REQUIRE_THROWS_AS(const_node.at(fkyaml::node(1)), fkyaml::out_of_range); } } - SECTION("scalar") - { + SECTION("scalar") { auto node = GENERATE(fkyaml::node(), fkyaml::node(false), fkyaml::node(0), fkyaml::node(0.0), fkyaml::node("")); fkyaml::node node_key = 0; - SECTION("non-const node with an integer.") - { + SECTION("non-const node with an integer.") { REQUIRE_THROWS_AS(node.at(0), fkyaml::type_error); } - SECTION("const node with an integer") - { + SECTION("const node with an integer") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.at(0), fkyaml::type_error); } - SECTION("non-const node with an integer node") - { + SECTION("non-const node with an integer node") { REQUIRE_THROWS_AS(node.at(node_key), fkyaml::type_error); } - SECTION("const node with an integer node") - { + SECTION("const node with an integer node") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.at(node_key), fkyaml::type_error); } @@ -2298,8 +2031,7 @@ TEST_CASE("Node_At") // test cases for YAML version property getter/setter // -TEST_CASE("Node_SetYamlVersion") -{ +TEST_CASE("Node_SetYamlVersion") { fkyaml::node node; node.set_yaml_version(fkyaml::node::yaml_version_t::VER_1_1); REQUIRE(node.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_1); @@ -2308,8 +2040,7 @@ TEST_CASE("Node_SetYamlVersion") REQUIRE(node.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_2); } -TEST_CASE("Node_GetYamlVersion") -{ +TEST_CASE("Node_GetYamlVersion") { fkyaml::node node; REQUIRE(node.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_2); @@ -2321,23 +2052,19 @@ TEST_CASE("Node_GetYamlVersion") // test cases for anchor related APIs // -TEST_CASE("Node_IsAlias") -{ +TEST_CASE("Node_IsAlias") { fkyaml::node node; - SECTION("without anchor name") - { + SECTION("without anchor name") { REQUIRE_FALSE(node.is_alias()); } - SECTION("an anchor node") - { + SECTION("an anchor node") { node.add_anchor_name("anchor"); REQUIRE_FALSE(node.is_alias()); } - SECTION("an alias node") - { + SECTION("an alias node") { fkyaml::node anchor; anchor.add_anchor_name("anchor"); node = fkyaml::node::alias_of(anchor); @@ -2345,23 +2072,19 @@ TEST_CASE("Node_IsAlias") } } -TEST_CASE("Node_IsAnchor") -{ +TEST_CASE("Node_IsAnchor") { fkyaml::node node; - SECTION("without anchor name") - { + SECTION("without anchor name") { REQUIRE_FALSE(node.is_anchor()); } - SECTION("an anchor node") - { + SECTION("an anchor node") { node.add_anchor_name("anchor"); REQUIRE(node.is_anchor()); } - SECTION("an alias node") - { + SECTION("an alias node") { fkyaml::node anchor; anchor.add_anchor_name("anchor"); node = fkyaml::node::alias_of(anchor); @@ -2369,60 +2092,50 @@ TEST_CASE("Node_IsAnchor") } } -TEST_CASE("Node_HasAnchorName") -{ +TEST_CASE("Node_HasAnchorName") { fkyaml::node node; - SECTION("without anchor name") - { + SECTION("without anchor name") { REQUIRE_FALSE(node.has_anchor_name()); } - SECTION("with anchor name") - { + SECTION("with anchor name") { node.add_anchor_name("anchor_name"); REQUIRE(node.has_anchor_name()); } } -TEST_CASE("Node_GetAnchorName") -{ +TEST_CASE("Node_GetAnchorName") { fkyaml::node node; - SECTION("without anchor name") - { + SECTION("without anchor name") { REQUIRE_THROWS_AS(node.get_anchor_name(), fkyaml::exception); } - SECTION("with anchor name") - { + SECTION("with anchor name") { node.add_anchor_name("anchor_name"); REQUIRE_NOTHROW(node.get_anchor_name()); REQUIRE(node.get_anchor_name().compare("anchor_name") == 0); } } -TEST_CASE("Node_AddAnchorName") -{ +TEST_CASE("Node_AddAnchorName") { fkyaml::node node; std::string anchor_name = "anchor_name"; - SECTION("lvalue anchor name") - { + SECTION("lvalue anchor name") { node.add_anchor_name(anchor_name); REQUIRE_NOTHROW(node.get_anchor_name()); REQUIRE(node.get_anchor_name().compare("anchor_name") == 0); } - SECTION("rvalue anchor name") - { + SECTION("rvalue anchor name") { node.add_anchor_name(std::move(anchor_name)); REQUIRE_NOTHROW(node.get_anchor_name()); REQUIRE(node.get_anchor_name().compare("anchor_name") == 0); } - SECTION("overwrite an existing anchor name") - { + SECTION("overwrite an existing anchor name") { node.add_anchor_name(anchor_name); node.add_anchor_name("overwritten_name"); REQUIRE_NOTHROW(node.get_anchor_name()); @@ -2435,60 +2148,50 @@ TEST_CASE("Node_AddAnchorName") // test cases for tag related APIs // -TEST_CASE("Node_HasTagName") -{ +TEST_CASE("Node_HasTagName") { fkyaml::node node; - SECTION("node with tag name") - { + SECTION("node with tag name") { node.add_tag_name("tag_name"); REQUIRE(node.has_tag_name()); } - SECTION("node without tag name") - { + SECTION("node without tag name") { REQUIRE_FALSE(node.has_tag_name()); } } -TEST_CASE("Node_GetTagName") -{ +TEST_CASE("Node_GetTagName") { fkyaml::node node; - SECTION("node without tag name.") - { + SECTION("node without tag name.") { REQUIRE_THROWS_AS(node.get_tag_name(), fkyaml::exception); } - SECTION("node with tag name.") - { + SECTION("node with tag name.") { node.add_tag_name("tag_name"); REQUIRE_NOTHROW(node.get_tag_name()); REQUIRE(node.get_tag_name() == "tag_name"); } } -TEST_CASE("Node_AddTagName") -{ +TEST_CASE("Node_AddTagName") { fkyaml::node node; std::string tag_name = "tag_name"; - SECTION("lvalue tag name") - { + SECTION("lvalue tag name") { node.add_tag_name(tag_name); REQUIRE_NOTHROW(node.get_tag_name()); REQUIRE(node.get_tag_name().compare("tag_name") == 0); } - SECTION("rvalue tag name") - { + SECTION("rvalue tag name") { node.add_tag_name(std::move(tag_name)); REQUIRE_NOTHROW(node.get_tag_name()); REQUIRE(node.get_tag_name().compare("tag_name") == 0); } - SECTION("overwrite an existing tag name") - { + SECTION("overwrite an existing tag name") { node.add_tag_name(tag_name); node.add_tag_name("overwritten_name"); REQUIRE_NOTHROW(node.get_tag_name()); @@ -2501,26 +2204,21 @@ TEST_CASE("Node_AddTagName") // test cases for value getters (copy) // -struct string_wrap -{ +struct string_wrap { string_wrap() = default; - string_wrap& operator=(const std::string& _str) - { + string_wrap& operator=(const std::string& _str) { str = _str; return *this; } std::string str; }; -TEST_CASE("Node_GetValue") -{ +TEST_CASE("Node_GetValue") { - SECTION("sequence") - { + SECTION("sequence") { fkyaml::node node(fkyaml::node::sequence_type {fkyaml::node(true), fkyaml::node(false)}); - SECTION("sequence value") - { + SECTION("sequence value") { auto seq = node.get_value(); REQUIRE(seq.size() == 2); REQUIRE(seq[0].is_boolean()); @@ -2529,8 +2227,7 @@ TEST_CASE("Node_GetValue") REQUIRE(seq[1].get_value() == false); } - SECTION("non-sequence value") - { + SECTION("non-sequence value") { REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); @@ -2541,12 +2238,10 @@ TEST_CASE("Node_GetValue") } } - SECTION("mapping") - { + SECTION("mapping") { fkyaml::node node(fkyaml::node::mapping_type {{"test", fkyaml::node(3.14)}, {"foo", fkyaml::node("bar")}}); - SECTION("mapping value") - { + SECTION("mapping value") { auto map = node.get_value(); REQUIRE(map.size() == 2); REQUIRE(map.find("test") != map.end()); @@ -2557,8 +2252,7 @@ TEST_CASE("Node_GetValue") REQUIRE(map.at("foo").get_value_ref() == "bar"); } - SECTION("non-mapping values") - { + SECTION("non-mapping values") { REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); @@ -2569,18 +2263,15 @@ TEST_CASE("Node_GetValue") } } - SECTION("null node value") - { + SECTION("null node value") { fkyaml::node node(nullptr); - SECTION("null value") - { + SECTION("null value") { auto null = node.get_value(); REQUIRE(null == nullptr); } - SECTION("non-null values") - { + SECTION("non-null values") { REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); @@ -2591,17 +2282,14 @@ TEST_CASE("Node_GetValue") } } - SECTION("boolean node value") - { + SECTION("boolean node value") { fkyaml::node node(true); - SECTION("boolean value") - { + SECTION("boolean value") { REQUIRE(node.get_value() == true); } - SECTION("non-boolean values") - { + SECTION("non-boolean values") { REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); @@ -2612,12 +2300,10 @@ TEST_CASE("Node_GetValue") } } - SECTION("integer node value") - { + SECTION("integer node value") { fkyaml::node node(123); - SECTION("integer values") - { + SECTION("integer values") { REQUIRE(node.get_value() == 123); REQUIRE(node.get_value() == 123); REQUIRE(node.get_value() == 123); @@ -2628,8 +2314,7 @@ TEST_CASE("Node_GetValue") // TODO: REQUIRE(node.get_value() == 123); } - SECTION("non-integer values") - { + SECTION("non-integer values") { REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); @@ -2639,38 +2324,32 @@ TEST_CASE("Node_GetValue") REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); } - SECTION("non-integer node value") - { + SECTION("non-integer node value") { fkyaml::node non_int_node(true); REQUIRE_THROWS_AS(non_int_node.get_value(), fkyaml::type_error); } - SECTION("underflowable integer type") - { + SECTION("underflowable integer type") { fkyaml::node negative_int_node(std::numeric_limits::min()); REQUIRE_THROWS_AS(negative_int_node.get_value(), fkyaml::exception); } - SECTION("overflowable integer type") - { + SECTION("overflowable integer type") { fkyaml::node large_int_node(std::numeric_limits::max()); REQUIRE_THROWS_AS(large_int_node.get_value(), fkyaml::exception); } } - SECTION("float number node value") - { + SECTION("float number node value") { fkyaml::node node(3.14); - SECTION("float number values") - { + SECTION("float number values") { REQUIRE(std::abs(node.get_value() - 3.14) < std::numeric_limits::epsilon()); REQUIRE(std::abs(node.get_value() - 3.14) < std::numeric_limits::epsilon()); REQUIRE(std::abs(node.get_value() - 3.14) < std::numeric_limits::epsilon()); } - SECTION("non-float-number values") - { + SECTION("non-float-number values") { REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); @@ -2680,54 +2359,46 @@ TEST_CASE("Node_GetValue") REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); } - SECTION("non-float-number node value") - { + SECTION("non-float-number node value") { fkyaml::node non_float_num_node(true); REQUIRE_THROWS_AS(non_float_num_node.get_value(), fkyaml::type_error); } - SECTION("underflowable float number type") - { + SECTION("underflowable float number type") { fkyaml::node negative_float_node(std::numeric_limits::min()); REQUIRE_THROWS_AS(negative_float_node.get_value(), fkyaml::exception); } - SECTION("overflowable float number type") - { + SECTION("overflowable float number type") { fkyaml::node large_float_node(std::numeric_limits::max()); REQUIRE_THROWS_AS(large_float_node.get_value(), fkyaml::exception); } } - SECTION("string node value") - { + SECTION("string node value") { fkyaml::node node("test"); - SECTION("string value") - { + SECTION("string value") { auto str = node.get_value(); REQUIRE(str.size() == 4); REQUIRE(str == "test"); } - SECTION("compatible string value") - { + SECTION("compatible string value") { auto str_wrap = node.get_value(); REQUIRE(str_wrap.str.size() == 4); REQUIRE(str_wrap.str == "test"); } #ifdef FK_YAML_HAS_CXX_17 - SECTION("string view") - { + SECTION("string view") { auto str_view = node.get_value(); REQUIRE(str_view.size() == 4); REQUIRE(str_view == "test"); } #endif - SECTION("non-string values") - { + SECTION("non-string values") { REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); REQUIRE_THROWS_AS(node.get_value(), fkyaml::type_error); @@ -2742,61 +2413,50 @@ TEST_CASE("Node_GetValue") // test cases for value reference getters // -TEST_CASE("Node_GetValueRefForSequence") -{ - SECTION("sequence") - { +TEST_CASE("Node_GetValueRefForSequence") { + SECTION("sequence") { fkyaml::node node = fkyaml::node::sequence(fkyaml::node::sequence_type {fkyaml::node(), fkyaml::node(), fkyaml::node()}); - SECTION("non-alias sequence node") - { + SECTION("non-alias sequence node") { REQUIRE_NOTHROW(node.get_value_ref()); REQUIRE(node.get_value_ref().size() == 3); - for (int i = 0; i < 3; ++i) - { + for (int i = 0; i < 3; ++i) { REQUIRE(node.get_value_ref()[i].is_null()); } } - SECTION("const non-alias sequence node") - { + SECTION("const non-alias sequence node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.get_value_ref()); REQUIRE(const_node.get_value_ref().size() == 3); - for (int i = 0; i < 3; ++i) - { + for (int i = 0; i < 3; ++i) { REQUIRE(node.get_value_ref()[i].is_null()); } } - SECTION("alias sequence node") - { + SECTION("alias sequence node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); REQUIRE(alias.get_value_ref().size() == 3); - for (int i = 0; i < 3; ++i) - { + for (int i = 0; i < 3; ++i) { REQUIRE(alias.get_value_ref()[i].is_null()); } } - SECTION("const alias sequence node") - { + SECTION("const alias sequence node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); REQUIRE(alias.get_value_ref().size() == 3); - for (int i = 0; i < 3; ++i) - { + for (int i = 0; i < 3; ++i) { REQUIRE(alias.get_value_ref()[i].is_null()); } } } - SECTION("non-sequence") - { + SECTION("non-sequence") { auto node = GENERATE( fkyaml::node::mapping(), fkyaml::node(), @@ -2805,26 +2465,22 @@ TEST_CASE("Node_GetValueRefForSequence") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-sequence nodes") - { + SECTION("non-alias non-sequence nodes") { REQUIRE_THROWS_AS(node.get_value_ref(), fkyaml::type_error); } - SECTION("const non-alias non-sequence nodes") - { + SECTION("const non-alias non-sequence nodes") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.get_value_ref(), fkyaml::type_error); } - SECTION("alias non-sequence nodes") - { + SECTION("alias non-sequence nodes") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); } - SECTION("const alias non-sequence nodes") - { + SECTION("const alias non-sequence nodes") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); @@ -2832,15 +2488,12 @@ TEST_CASE("Node_GetValueRefForSequence") } } -TEST_CASE("Node_GetValueRefForMapping") -{ - SECTION("mapping") - { +TEST_CASE("Node_GetValueRefForMapping") { + SECTION("mapping") { fkyaml::node node = fkyaml::node::mapping(fkyaml::node::mapping_type { {"test0", fkyaml::node()}, {"test1", fkyaml::node()}, {"test2", fkyaml::node()}}); - SECTION("non-alias mapping node") - { + SECTION("non-alias mapping node") { REQUIRE_NOTHROW(node.get_value_ref()); REQUIRE(node.get_value_ref().size() == 3); REQUIRE(node["test0"].is_null()); @@ -2848,8 +2501,7 @@ TEST_CASE("Node_GetValueRefForMapping") REQUIRE(node["test2"].is_null()); } - SECTION("const non-alias mapping node") - { + SECTION("const non-alias mapping node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.get_value_ref()); REQUIRE(const_node.get_value_ref().size() == 3); @@ -2858,8 +2510,7 @@ TEST_CASE("Node_GetValueRefForMapping") REQUIRE(const_node["test2"].is_null()); } - SECTION("alias mapping node") - { + SECTION("alias mapping node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); @@ -2869,8 +2520,7 @@ TEST_CASE("Node_GetValueRefForMapping") REQUIRE(alias["test2"].is_null()); } - SECTION("const alias mapping node") - { + SECTION("const alias mapping node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); @@ -2880,8 +2530,7 @@ TEST_CASE("Node_GetValueRefForMapping") } } - SECTION("non-mapping") - { + SECTION("non-mapping") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node(), @@ -2890,26 +2539,22 @@ TEST_CASE("Node_GetValueRefForMapping") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-mapping nodes") - { + SECTION("non-alias non-mapping nodes") { REQUIRE_THROWS_AS(node.get_value_ref(), fkyaml::type_error); } - SECTION("const non-alias non-mapping nodes") - { + SECTION("const non-alias non-mapping nodes") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.get_value_ref(), fkyaml::type_error); } - SECTION("alias non-mapping nodes") - { + SECTION("alias non-mapping nodes") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); } - SECTION("const alias non-mapping nodes") - { + SECTION("const alias non-mapping nodes") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); @@ -2917,35 +2562,29 @@ TEST_CASE("Node_GetValueRefForMapping") } } -TEST_CASE("Node_GetValueRefForBoolean") -{ - SECTION("boolean") - { +TEST_CASE("Node_GetValueRefForBoolean") { + SECTION("boolean") { fkyaml::node node = true; - SECTION("non-alias boolean node") - { + SECTION("non-alias boolean node") { REQUIRE_NOTHROW(node.get_value_ref()); REQUIRE(node.get_value_ref() == true); } - SECTION("const non-alias boolean node") - { + SECTION("const non-alias boolean node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.get_value_ref()); REQUIRE(const_node.get_value_ref() == true); } - SECTION("alias boolean node") - { + SECTION("alias boolean node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); REQUIRE(alias.get_value_ref() == true); } - SECTION("const alias boolean node") - { + SECTION("const alias boolean node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); @@ -2953,8 +2592,7 @@ TEST_CASE("Node_GetValueRefForBoolean") } } - SECTION("non-boolean") - { + SECTION("non-boolean") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -2963,26 +2601,22 @@ TEST_CASE("Node_GetValueRefForBoolean") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-boolean nodes") - { + SECTION("non-alias non-boolean nodes") { REQUIRE_THROWS_AS(node.get_value_ref(), fkyaml::type_error); } - SECTION("const non-alias non-boolean nodes") - { + SECTION("const non-alias non-boolean nodes") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.get_value_ref(), fkyaml::type_error); } - SECTION("alias non-boolean nodes") - { + SECTION("alias non-boolean nodes") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); } - SECTION("const alias non-boolean nodes") - { + SECTION("const alias non-boolean nodes") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); @@ -2990,36 +2624,30 @@ TEST_CASE("Node_GetValueRefForBoolean") } } -TEST_CASE("Node_GetValueRefForInteger") -{ - SECTION("integer") - { +TEST_CASE("Node_GetValueRefForInteger") { + SECTION("integer") { fkyaml::node::integer_type integer = -123; fkyaml::node node = integer; - SECTION("non-alias integer node") - { + SECTION("non-alias integer node") { REQUIRE_NOTHROW(node.get_value_ref()); REQUIRE(node.get_value_ref() == integer); } - SECTION("const non-alias integer node") - { + SECTION("const non-alias integer node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.get_value_ref()); REQUIRE(const_node.get_value_ref() == integer); } - SECTION("alias integer node") - { + SECTION("alias integer node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); REQUIRE(alias.get_value_ref() == integer); } - SECTION("const alias integer node") - { + SECTION("const alias integer node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); @@ -3027,8 +2655,7 @@ TEST_CASE("Node_GetValueRefForInteger") } } - SECTION("non-integer") - { + SECTION("non-integer") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -3037,26 +2664,22 @@ TEST_CASE("Node_GetValueRefForInteger") fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-alias non-integer nodes") - { + SECTION("non-alias non-integer nodes") { REQUIRE_THROWS_AS(node.get_value_ref(), fkyaml::type_error); } - SECTION("const non-alias non-integer nodes") - { + SECTION("const non-alias non-integer nodes") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.get_value_ref(), fkyaml::type_error); } - SECTION("alias non-integer nodes") - { + SECTION("alias non-integer nodes") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); } - SECTION("const alias non-integer nodes") - { + SECTION("const alias non-integer nodes") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); @@ -3064,36 +2687,30 @@ TEST_CASE("Node_GetValueRefForInteger") } } -TEST_CASE("Node_GetValueRefForFloatNumber") -{ - SECTION("floating point number") - { +TEST_CASE("Node_GetValueRefForFloatNumber") { + SECTION("floating point number") { fkyaml::node::float_number_type float_val = 123.45; fkyaml::node node = float_val; - SECTION("non-alias float number node") - { + SECTION("non-alias float number node") { REQUIRE_NOTHROW(node.get_value_ref()); REQUIRE(node.get_value_ref() == float_val); } - SECTION("const non-alias float number node") - { + SECTION("const non-alias float number node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.get_value_ref()); REQUIRE(const_node.get_value_ref() == float_val); } - SECTION("alias float number node") - { + SECTION("alias float number node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); REQUIRE(alias.get_value_ref() == float_val); } - SECTION("const alias float number node") - { + SECTION("const alias float number node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); @@ -3101,8 +2718,7 @@ TEST_CASE("Node_GetValueRefForFloatNumber") } } - SECTION("not floating point number") - { + SECTION("not floating point number") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -3111,26 +2727,22 @@ TEST_CASE("Node_GetValueRefForFloatNumber") fkyaml::node(0), fkyaml::node("")); - SECTION("non-alias non-float-number nodes") - { + SECTION("non-alias non-float-number nodes") { REQUIRE_THROWS_AS(node.get_value_ref(), fkyaml::type_error); } - SECTION("const non-alias non-float-number nodes") - { + SECTION("const non-alias non-float-number nodes") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.get_value_ref(), fkyaml::type_error); } - SECTION("alias non-float-number nodes") - { + SECTION("alias non-float-number nodes") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); } - SECTION("const alias non-float-number nodes") - { + SECTION("const alias non-float-number nodes") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); @@ -3138,36 +2750,30 @@ TEST_CASE("Node_GetValueRefForFloatNumber") } } -TEST_CASE("Node_GetValueRefForString") -{ - SECTION("string") - { +TEST_CASE("Node_GetValueRefForString") { + SECTION("string") { fkyaml::node::string_type str = "test"; fkyaml::node node = str; - SECTION("non-alias string node") - { + SECTION("non-alias string node") { REQUIRE_NOTHROW(node.get_value_ref()); REQUIRE(node.get_value_ref() == str); } - SECTION("const non-alias string node") - { + SECTION("const non-alias string node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.get_value_ref()); REQUIRE(const_node.get_value_ref() == str); } - SECTION("alias string node") - { + SECTION("alias string node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); REQUIRE(alias.get_value_ref() == str); } - SECTION("const alias string node") - { + SECTION("const alias string node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.get_value_ref()); @@ -3175,8 +2781,7 @@ TEST_CASE("Node_GetValueRefForString") } } - SECTION("non-string") - { + SECTION("non-string") { auto node = GENERATE( fkyaml::node::sequence(), fkyaml::node::mapping(), @@ -3185,26 +2790,22 @@ TEST_CASE("Node_GetValueRefForString") fkyaml::node(0), fkyaml::node(0.0)); - SECTION("non-alias non-string nodes") - { + SECTION("non-alias non-string nodes") { REQUIRE_THROWS_AS(node.get_value_ref(), fkyaml::type_error); } - SECTION("const non-alias non-string nodes") - { + SECTION("const non-alias non-string nodes") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.get_value_ref(), fkyaml::type_error); } - SECTION("alias non-string nodes") - { + SECTION("alias non-string nodes") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); } - SECTION("const alias non-string nodes") - { + SECTION("const alias non-string nodes") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_THROWS_AS(alias.get_value_ref(), fkyaml::type_error); @@ -3216,109 +2817,89 @@ TEST_CASE("Node_GetValueRefForString") // test cases for iterator getters // -TEST_CASE("Node_Begin") -{ - SECTION("container nodes") - { +TEST_CASE("Node_Begin") { + SECTION("container nodes") { auto node = GENERATE(fkyaml::node::sequence(), fkyaml::node::mapping()); - SECTION("non-const non-alias container node") - { + SECTION("non-const non-alias container node") { REQUIRE_NOTHROW(node.begin()); } - SECTION("const non-alias container node") - { + SECTION("const non-alias container node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.begin()); } - SECTION("non-const alias container node") - { + SECTION("non-const alias container node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.begin()); } - SECTION("non-const alias container node") - { + SECTION("non-const alias container node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.begin()); } } - SECTION("scalar nodes") - { + SECTION("scalar nodes") { auto node = GENERATE(fkyaml::node(), fkyaml::node(false), fkyaml::node(0), fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-const node") - { + SECTION("non-const node") { REQUIRE_THROWS_AS(node.begin(), fkyaml::type_error); } - SECTION("const node") - { + SECTION("const node") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.begin(), fkyaml::type_error); } } } -TEST_CASE("Node_End") -{ - SECTION("container nodes") - { +TEST_CASE("Node_End") { + SECTION("container nodes") { auto node = GENERATE(fkyaml::node::sequence(), fkyaml::node::mapping()); - SECTION("non-const non-alias container node") - { + SECTION("non-const non-alias container node") { REQUIRE_NOTHROW(node.end()); } - SECTION("const non-alias container node") - { + SECTION("const non-alias container node") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.end()); } - SECTION("non-const alias container node") - { + SECTION("non-const alias container node") { node.add_anchor_name("anchor_name"); fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.end()); } - SECTION("non-const alias container node") - { + SECTION("non-const alias container node") { node.add_anchor_name("anchor_name"); const fkyaml::node alias = fkyaml::node::alias_of(node); REQUIRE_NOTHROW(alias.end()); } - SECTION("non-const range-based for-loop compatibility") - { + SECTION("non-const range-based for-loop compatibility") { REQUIRE_NOTHROW(node.end()); } - SECTION("const range-based for-loop compatibility") - { + SECTION("const range-based for-loop compatibility") { const fkyaml::node const_node = node; REQUIRE_NOTHROW(const_node.end()); } } - SECTION("scalar nodes") - { + SECTION("scalar nodes") { auto node = GENERATE(fkyaml::node(), fkyaml::node(false), fkyaml::node(0), fkyaml::node(0.0), fkyaml::node("")); - SECTION("non-const throwing node") - { + SECTION("non-const throwing node") { REQUIRE_THROWS_AS(node.end(), fkyaml::type_error); } - SECTION("const throwing node") - { + SECTION("const throwing node") { const fkyaml::node const_node = node; REQUIRE_THROWS_AS(const_node.end(), fkyaml::type_error); } @@ -3329,8 +2910,7 @@ TEST_CASE("Node_End") // test cases for swap // -TEST_CASE("Node_Swap") -{ +TEST_CASE("Node_Swap") { fkyaml::node lhs_node = true; fkyaml::node rhs_node = 123; lhs_node.swap(rhs_node); @@ -3340,8 +2920,7 @@ TEST_CASE("Node_Swap") REQUIRE(rhs_node.get_value_ref() == true); } -TEST_CASE("Node_ADLSwap") -{ +TEST_CASE("Node_ADLSwap") { fkyaml::node lhs_node = true; fkyaml::node rhs_node = 123; diff --git a/test/unit_test/test_node_ref_storage_class.cpp b/test/unit_test/test_node_ref_storage_class.cpp index 114072dc..d2897ce6 100644 --- a/test/unit_test/test_node_ref_storage_class.cpp +++ b/test/unit_test/test_node_ref_storage_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -12,8 +12,7 @@ #include -TEST_CASE("NodeRefStorage_CtorWithLvalueNode") -{ +TEST_CASE("NodeRefStorage_CtorWithLvalueNode") { fkyaml::node node = true; fkyaml::detail::node_ref_storage storage(node); REQUIRE(node.is_boolean()); @@ -23,8 +22,7 @@ TEST_CASE("NodeRefStorage_CtorWithLvalueNode") REQUIRE(retrieved_node.get_value() == true); } -TEST_CASE("NodeRefStorage_CtorWithRvalueNode") -{ +TEST_CASE("NodeRefStorage_CtorWithRvalueNode") { fkyaml::node node = 3.14; fkyaml::detail::node_ref_storage storage(std::move(node)); REQUIRE(node.is_null()); @@ -34,8 +32,7 @@ TEST_CASE("NodeRefStorage_CtorWithRvalueNode") REQUIRE(retrieved_node.get_value() == 3.14); } -TEST_CASE("NodeRefStorage_ArrowOperator") -{ +TEST_CASE("NodeRefStorage_ArrowOperator") { fkyaml::node node = 123; fkyaml::detail::node_ref_storage storage(node); REQUIRE(storage->is_integer()); @@ -47,8 +44,7 @@ TEST_CASE("NodeRefStorage_ArrowOperator") REQUIRE(storage2->size() == 2); } -TEST_CASE("NodeRefStorage_Release") -{ +TEST_CASE("NodeRefStorage_Release") { fkyaml::node node = 123; fkyaml::detail::node_ref_storage storage(node); fkyaml::node released_node = storage.release(); diff --git a/test/unit_test/test_ordered_map_class.cpp b/test/unit_test/test_ordered_map_class.cpp index 1142bd0d..00184a3b 100644 --- a/test/unit_test/test_ordered_map_class.cpp +++ b/test/unit_test/test_ordered_map_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -12,14 +12,12 @@ #include -TEST_CASE("OrderedMap_DefaultCtor") -{ +TEST_CASE("OrderedMap_DefaultCtor") { fkyaml::ordered_map map; REQUIRE(map.empty()); } -TEST_CASE("OrderedMap_InitListCtor") -{ +TEST_CASE("OrderedMap_InitListCtor") { fkyaml::ordered_map map {{"foo", true}, {"bar", false}}; REQUIRE_FALSE(map.empty()); REQUIRE(map.size() == 2); @@ -36,8 +34,7 @@ TEST_CASE("OrderedMap_InitListCtor") REQUIRE(itr->second == false); } -TEST_CASE("OrderedMap_SubscriptOperator") -{ +TEST_CASE("OrderedMap_SubscriptOperator") { fkyaml::ordered_map map {{"foo", true}, {"buz", false}}; REQUIRE(map["foo"] == true); REQUIRE(map["bar"] == false); @@ -46,8 +43,7 @@ TEST_CASE("OrderedMap_SubscriptOperator") REQUIRE(map["buz"] == true); } -TEST_CASE("OrderedMap_Emplace") -{ +TEST_CASE("OrderedMap_Emplace") { fkyaml::ordered_map map; REQUIRE(map.emplace("foo", true).second == true); REQUIRE(map.emplace("foo", false).second == false); @@ -56,8 +52,7 @@ TEST_CASE("OrderedMap_Emplace") REQUIRE(map["bar"] == false); } -TEST_CASE("OrderedMap_NonConstAt") -{ +TEST_CASE("OrderedMap_NonConstAt") { fkyaml::ordered_map map; REQUIRE_THROWS_AS(map.at("foo"), fkyaml::exception); map.emplace("foo", true); @@ -67,8 +62,7 @@ TEST_CASE("OrderedMap_NonConstAt") REQUIRE_THROWS_AS(map.at(key), fkyaml::exception); } -TEST_CASE("OrderedMap_ConstAt") -{ +TEST_CASE("OrderedMap_ConstAt") { const fkyaml::ordered_map map; REQUIRE_THROWS_AS(map.at("foo"), fkyaml::exception); fkyaml::ordered_map map_ = map; @@ -83,8 +77,7 @@ TEST_CASE("OrderedMap_ConstAt") REQUIRE_THROWS_AS(map__.at(key), fkyaml::exception); } -TEST_CASE("OrderedMap_NonConstFind") -{ +TEST_CASE("OrderedMap_NonConstFind") { fkyaml::ordered_map map; REQUIRE(map.find("foo") == map.end()); map.emplace("foo", true); @@ -94,8 +87,7 @@ TEST_CASE("OrderedMap_NonConstFind") REQUIRE(map.find("bar") == map.end()); } -TEST_CASE("OrderedMap_ConstFind") -{ +TEST_CASE("OrderedMap_ConstFind") { const fkyaml::ordered_map map; REQUIRE(map.find("foo") == map.end()); fkyaml::ordered_map map_ = map; diff --git a/test/unit_test/test_position_tracker_class.cpp b/test/unit_test/test_position_tracker_class.cpp index b7d747c5..f9243405 100644 --- a/test/unit_test/test_position_tracker_class.cpp +++ b/test/unit_test/test_position_tracker_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -12,8 +12,7 @@ #include -TEST_CASE("PositionTracker_InitialState") -{ +TEST_CASE("PositionTracker_InitialState") { std::string input = "test"; fkyaml::detail::position_tracker pos_tracker {}; pos_tracker.set_target_buffer(input); @@ -23,12 +22,10 @@ TEST_CASE("PositionTracker_InitialState") REQUIRE(pos_tracker.get_lines_read() == 0); } -TEST_CASE("PositionTracker_MultipleLines") -{ +TEST_CASE("PositionTracker_MultipleLines") { fkyaml::detail::position_tracker pos_tracker {}; - SECTION("first character is not a newline code") - { + SECTION("first character is not a newline code") { std::string input = "test\nfoo"; pos_tracker.set_target_buffer(input); @@ -62,8 +59,7 @@ TEST_CASE("PositionTracker_MultipleLines") REQUIRE(pos_tracker.get_lines_read() == 1); } - SECTION("first character is a newline code") - { + SECTION("first character is a newline code") { std::string input = "\ntest\nfoo"; pos_tracker.set_target_buffer(input); diff --git a/test/unit_test/test_scalar_scanner_class.cpp b/test/unit_test/test_scalar_scanner_class.cpp index 193c97b3..9968f8b2 100644 --- a/test/unit_test/test_scalar_scanner_class.cpp +++ b/test/unit_test/test_scalar_scanner_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,19 +10,16 @@ #include -TEST_CASE("ScalarScanner_Empty") -{ +TEST_CASE("ScalarScanner_Empty") { REQUIRE(fkyaml::detail::scalar_scanner::scan("") == fkyaml::detail::lexical_token_t::STRING_VALUE); } -TEST_CASE("ScalarScanner_NullValue") -{ +TEST_CASE("ScalarScanner_NullValue") { auto token = GENERATE(std::string("~"), std::string("null"), std::string("Null"), std::string("NULL")); REQUIRE(fkyaml::detail::scalar_scanner::scan(token) == fkyaml::detail::lexical_token_t::NULL_VALUE); } -TEST_CASE("ScalarScanner_BooleanValue") -{ +TEST_CASE("ScalarScanner_BooleanValue") { auto token = GENERATE( std::string("true"), std::string("True"), @@ -33,8 +30,7 @@ TEST_CASE("ScalarScanner_BooleanValue") REQUIRE(fkyaml::detail::scalar_scanner::scan(token) == fkyaml::detail::lexical_token_t::BOOLEAN_VALUE); } -TEST_CASE("ScalarScanner_IntegerNumberValue") -{ +TEST_CASE("ScalarScanner_IntegerNumberValue") { auto token = GENERATE( std::string("-1234"), std::string("-853259"), @@ -52,8 +48,7 @@ TEST_CASE("ScalarScanner_IntegerNumberValue") REQUIRE(fkyaml::detail::scalar_scanner::scan(token) == fkyaml::detail::lexical_token_t::INTEGER_VALUE); } -TEST_CASE("ScalarScanner_FloatNumberValue") -{ +TEST_CASE("ScalarScanner_FloatNumberValue") { auto token = GENERATE( std::string(".inf"), std::string(".Inf"), @@ -73,8 +68,7 @@ TEST_CASE("ScalarScanner_FloatNumberValue") REQUIRE(fkyaml::detail::scalar_scanner::scan(token) == fkyaml::detail::lexical_token_t::FLOAT_NUMBER_VALUE); } -TEST_CASE("ScalarScanner_StringValue") -{ +TEST_CASE("ScalarScanner_StringValue") { auto token = GENERATE( std::string("abc"), std::string("0th"), diff --git a/test/unit_test/test_serializer_class.cpp b/test/unit_test/test_serializer_class.cpp index 446d28eb..c05905b0 100644 --- a/test/unit_test/test_serializer_class.cpp +++ b/test/unit_test/test_serializer_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -12,8 +12,7 @@ #include -TEST_CASE("Serializer_SequenceNode") -{ +TEST_CASE("Serializer_SequenceNode") { using node_str_pair_t = std::pair; auto node_str_pair = GENERATE( node_str_pair_t({true, false}, "- true\n- false\n"), @@ -22,8 +21,7 @@ TEST_CASE("Serializer_SequenceNode") REQUIRE(serializer.serialize(node_str_pair.first) == node_str_pair.second); } -TEST_CASE("Serializer_MappingNode") -{ +TEST_CASE("Serializer_MappingNode") { using node_str_pair_t = std::pair; auto node_str_pair = GENERATE( node_str_pair_t({{"foo", -1234}, {"bar", nullptr}}, "bar: null\nfoo: -1234\n"), @@ -32,31 +30,27 @@ TEST_CASE("Serializer_MappingNode") REQUIRE(serializer.serialize(node_str_pair.first) == node_str_pair.second); } -TEST_CASE("Serializer_NullNode") -{ +TEST_CASE("Serializer_NullNode") { fkyaml::detail::basic_serializer serializer; fkyaml::node node; REQUIRE(serializer.serialize(node) == "null"); } -TEST_CASE("Serializer_BooleanNode") -{ +TEST_CASE("Serializer_BooleanNode") { using node_str_pair_t = std::pair; auto node_str_pair = GENERATE(node_str_pair_t(false, "false"), node_str_pair_t(true, "true")); fkyaml::detail::basic_serializer serializer; REQUIRE(serializer.serialize(node_str_pair.first) == node_str_pair.second); } -TEST_CASE("Serializer_IntegerNode") -{ +TEST_CASE("Serializer_IntegerNode") { using node_str_pair_t = std::pair; auto node_str_pair = GENERATE(node_str_pair_t(-1234, "-1234"), node_str_pair_t(5678, "5678")); fkyaml::detail::basic_serializer serializer; REQUIRE(serializer.serialize(node_str_pair.first) == node_str_pair.second); } -TEST_CASE("SerializeClassTest_FloatNumberNode", "[SerializeClassTest]") -{ +TEST_CASE("SerializeClassTest_FloatNumberNode", "[SerializeClassTest]") { using node_str_pair_t = std::pair; auto node_str_pair = GENERATE( node_str_pair_t(3.14, "3.14"), @@ -68,8 +62,7 @@ TEST_CASE("SerializeClassTest_FloatNumberNode", "[SerializeClassTest]") REQUIRE(serializer.serialize(node_str_pair.first) == node_str_pair.second); } -TEST_CASE("Serializer_StringNode") -{ +TEST_CASE("Serializer_StringNode") { using node_str_pair_t = std::pair; const char NEXT_LINE[] = {char(0xC2u), char(0x85u), char(0)}; @@ -152,8 +145,21 @@ TEST_CASE("Serializer_StringNode") REQUIRE(serializer.serialize(node_str_pair.first) == node_str_pair.second); } -TEST_CASE("Serializer_AnchorNode") -{ +TEST_CASE("Serializer_MappingKeyNode") { + fkyaml::node map_key = {{true, 123}}; + fkyaml::node seq_key = {3.14, nullptr}; + fkyaml::node node = {{map_key, 3.14}, {seq_key, "foo"}}; + std::string expected = "? - 3.14\n" + " - null\n" + ": foo\n" + "? true: 123\n" + ": 3.14\n"; + + fkyaml::detail::basic_serializer serializer; + REQUIRE(serializer.serialize(node) == expected); +} + +TEST_CASE("Serializer_AnchorNode") { fkyaml::node node = {{"foo", 123}, {nullptr, {true, "bar", 3.14}}}; node[nullptr].add_anchor_name("A"); node[nullptr][2].add_anchor_name("B"); @@ -172,8 +178,7 @@ TEST_CASE("Serializer_AnchorNode") REQUIRE(serializer.serialize(node) == expected); } -TEST_CASE("Serializer_AliasNode") -{ +TEST_CASE("Serializer_AliasNode") { fkyaml::node node = {{"foo", 123}}; node["foo"].add_anchor_name("A"); node.get_value_ref().emplace(true, fkyaml::node::alias_of(node["foo"])); @@ -197,8 +202,7 @@ TEST_CASE("Serializer_AliasNode") REQUIRE(serializer.serialize(node) == expected); } -TEST_CASE("Serializer_TaggedNode") -{ +TEST_CASE("Serializer_TaggedNode") { fkyaml::node root = fkyaml::node::mapping(); fkyaml::node str_node("foo"); str_node.add_tag_name("!!str"); @@ -235,14 +239,12 @@ TEST_CASE("Serializer_TaggedNode") REQUIRE(serializer.serialize(root) == expected); } -TEST_CASE("Serializer_NodesWithDirectives") -{ +TEST_CASE("Serializer_NodesWithDirectives") { fkyaml::node root; fkyaml::detail::basic_deserializer deserializer; fkyaml::detail::basic_serializer serializer; - SECTION("YAML version 1.1") - { + SECTION("YAML version 1.1") { std::string expected = "%YAML 1.1\n" "---\n" "foo: 123\n"; @@ -251,8 +253,7 @@ TEST_CASE("Serializer_NodesWithDirectives") REQUIRE(serializer.serialize(root) == expected); } - SECTION("YAML version 1.2") - { + SECTION("YAML version 1.2") { std::string expected = "%YAML 1.2\n" "---\n" "foo: 123\n"; @@ -261,8 +262,7 @@ TEST_CASE("Serializer_NodesWithDirectives") REQUIRE(serializer.serialize(root) == expected); } - SECTION("primary handle prefix") - { + SECTION("primary handle prefix") { std::string expected = "%TAG ! tag:example.com,2000:\n" "---\n" "foo: 123\n"; @@ -271,8 +271,7 @@ TEST_CASE("Serializer_NodesWithDirectives") REQUIRE(serializer.serialize(root) == expected); } - SECTION("secondary handle prefix") - { + SECTION("secondary handle prefix") { std::string expected = "%TAG !! tag:example.com,2000:\n" "---\n" "foo: 123\n"; @@ -281,8 +280,7 @@ TEST_CASE("Serializer_NodesWithDirectives") REQUIRE(serializer.serialize(root) == expected); } - SECTION("named handles") - { + SECTION("named handles") { std::string expected = "%TAG !e! tag:example.com,2000:\n" "%TAG !t! !test-\n" "---\n" diff --git a/test/unit_test/test_string_formatter.cpp b/test/unit_test/test_string_formatter.cpp index b16d1ff2..b0d94e5d 100644 --- a/test/unit_test/test_string_formatter.cpp +++ b/test/unit_test/test_string_formatter.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,8 +10,7 @@ #include -TEST_CASE("StringFormatter_ValidMessageFormat") -{ +TEST_CASE("StringFormatter_ValidMessageFormat") { const char* p_label = "foo_label"; int result = 0; std::string formatted = fkyaml::detail::format("%s: ret=%d", p_label, result); diff --git a/test/unit_test/test_tag_resolver_class.cpp b/test/unit_test/test_tag_resolver_class.cpp index 32438b3d..bb787dbc 100644 --- a/test/unit_test/test_tag_resolver_class.cpp +++ b/test/unit_test/test_tag_resolver_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,15 +10,13 @@ #include -TEST_CASE("TagResolver_ResolveTag") -{ +TEST_CASE("TagResolver_ResolveTag") { using test_pair_t = std::pair; fkyaml::detail::tag_t tag_type {}; std::shared_ptr directives {}; - SECTION("valid tag name with default tag handle prefixes") - { + SECTION("valid tag name with default tag handle prefixes") { auto test_pair = GENERATE( test_pair_t {"!", fkyaml::detail::tag_t::NON_SPECIFIC}, test_pair_t {"!local", fkyaml::detail::tag_t::CUSTOM_TAG}, @@ -44,8 +42,7 @@ TEST_CASE("TagResolver_ResolveTag") REQUIRE(tag_type == test_pair.second); } - SECTION("valid tag name with non-default primary handle prefix") - { + SECTION("valid tag name with non-default primary handle prefix") { directives = std::shared_ptr(new fkyaml::detail::directive_set()); directives->primary_handle_prefix = "tag:example.com,2000:"; @@ -57,8 +54,7 @@ TEST_CASE("TagResolver_ResolveTag") REQUIRE(tag_type == test_pair.second); } - SECTION("valid tag name with non-default secondary handle prefix") - { + SECTION("valid tag name with non-default secondary handle prefix") { directives = std::shared_ptr(new fkyaml::detail::directive_set()); directives->secondary_handle_prefix = "tag:example.com,2000"; @@ -75,8 +71,7 @@ TEST_CASE("TagResolver_ResolveTag") REQUIRE(tag_type == test_pair.second); } - SECTION("valid tag name with named handles") - { + SECTION("valid tag name with named handles") { directives = std::shared_ptr(new fkyaml::detail::directive_set()); directives->named_handle_map.emplace("!yaml!", "tag:yaml.org,2002:"); directives->named_handle_map.emplace("!test0!", "!test-"); @@ -95,15 +90,13 @@ TEST_CASE("TagResolver_ResolveTag") REQUIRE(tag_type == test_pair.second); } - SECTION("invalid tag name with empty directive_set") - { + SECTION("invalid tag name with empty directive_set") { auto tag = GENERATE(std::string(""), std::string("invalid"), std::string("!invalid!tag")); REQUIRE_THROWS_AS(fkyaml::detail::tag_resolver::resolve_tag(tag, directives), fkyaml::invalid_tag); } - SECTION("invalid tag name with non-empty directive_set") - { + SECTION("invalid tag name with non-empty directive_set") { directives = std::shared_ptr(new fkyaml::detail::directive_set()); directives->named_handle_map.emplace("!valid!", "tag:example.com,2000"); diff --git a/test/unit_test/test_uri_encoding_class.cpp b/test/unit_test/test_uri_encoding_class.cpp index 1e90fcbf..1513cffd 100644 --- a/test/unit_test/test_uri_encoding_class.cpp +++ b/test/unit_test/test_uri_encoding_class.cpp @@ -1,6 +1,6 @@ // _______ __ __ __ _____ __ __ __ // | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 // |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML // // SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani @@ -10,10 +10,8 @@ #include -TEST_CASE("URIEncoding_Validate") -{ - SECTION("valid URI characters") - { +TEST_CASE("URIEncoding_Validate") { + SECTION("valid URI characters") { auto input = GENERATE( std::string(""), std::string("%00%99%AF%af"), @@ -26,8 +24,7 @@ TEST_CASE("URIEncoding_Validate") REQUIRE(fkyaml::detail::uri_encoding::validate(input.begin(), input.end())); } - SECTION("invalid URI characters") - { + SECTION("invalid URI characters") { auto input = GENERATE( std::string("%//"), std::string("%::"), diff --git a/test/unit_test/test_utf8_encoding_class.cpp b/test/unit_test/test_utf8_encoding_class.cpp deleted file mode 100644 index fcc457da..00000000 --- a/test/unit_test/test_utf8_encoding_class.cpp +++ /dev/null @@ -1,301 +0,0 @@ -// _______ __ __ __ _____ __ __ __ -// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) -// | __| _ < \_ _/| ___ | _ | |___ version 0.3.4 -// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML -// -// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani -// SPDX-License-Identifier: MIT - -#include -#include -#include - -#include - -#include - -TEST_CASE("UTF8Encoding_Validate") -{ - using int_type = std::char_traits::int_type; - - SECTION("1 byte character encoded in UTF-8") - { - using array_ret_pair_t = std::pair, bool>; - auto pair = GENERATE( - array_ret_pair_t({{-2}}, false), - array_ret_pair_t({{-1}}, false), - array_ret_pair_t({{0x00}}, true), - array_ret_pair_t({{0x01}}, true), - array_ret_pair_t({{0x02}}, true), - array_ret_pair_t({{0x7D}}, true), - array_ret_pair_t({{0x7E}}, true), - array_ret_pair_t({{0x7F}}, true), - array_ret_pair_t({{0x80}}, false), - array_ret_pair_t({{0x81}}, false)); - - REQUIRE(fkyaml::detail::utf8_encoding::validate(pair.first) == pair.second); - } - - SECTION("2 byte characters encoded in UTF-8") - { - using array_ret_pair_t = std::pair, bool>; - auto pair = GENERATE( - array_ret_pair_t({{0xC0, 0x80}}, false), - array_ret_pair_t({{0xC1, 0x80}}, false), - array_ret_pair_t({{0xC2, 0x7E}}, false), - array_ret_pair_t({{0xC2, 0x7F}}, false), - array_ret_pair_t({{0xC2, 0x80}}, true), - array_ret_pair_t({{0xC3, 0x81}}, true), - array_ret_pair_t({{0xD0, 0xA0}}, true), - array_ret_pair_t({{0xDE, 0xBE}}, true), - array_ret_pair_t({{0xDF, 0xBF}}, true), - array_ret_pair_t({{0xDF, 0xC0}}, false), - array_ret_pair_t({{0xDF, 0xC1}}, false), - array_ret_pair_t({{0xE0, 0xBF}}, false), - array_ret_pair_t({{0xE1, 0xBF}}, false)); - - REQUIRE(fkyaml::detail::utf8_encoding::validate(pair.first) == pair.second); - } - - SECTION("3 byte characters encoded in UTF-8") - { - using array_ret_pair_t = std::pair, bool>; - auto pair = GENERATE( - array_ret_pair_t({{0xDE, 0x80, 0x80}}, false), - array_ret_pair_t({{0xDF, 0x80, 0x80}}, false), - array_ret_pair_t({{0xE0, 0x7E, 0x80}}, false), - array_ret_pair_t({{0xE0, 0x7F, 0x80}}, false), - array_ret_pair_t({{0xE0, 0x80, 0x7E}}, false), - array_ret_pair_t({{0xE0, 0x80, 0x7F}}, false), - - array_ret_pair_t({{0xE0, 0x80, 0x80}}, true), - array_ret_pair_t({{0xE6, 0xA0, 0xA0}}, true), - array_ret_pair_t({{0xEC, 0xBF, 0xBF}}, true), - - array_ret_pair_t({{0xEC, 0xC0, 0xBF}}, false), - array_ret_pair_t({{0xEC, 0xC1, 0xBF}}, false), - array_ret_pair_t({{0xEC, 0xBF, 0xC0}}, false), - array_ret_pair_t({{0xEC, 0xBF, 0xC1}}, false), - - ////////////////////////////////////////////// - - array_ret_pair_t({{0xED, 0x7E, 0x80}}, false), - array_ret_pair_t({{0xED, 0x7F, 0x80}}, false), - array_ret_pair_t({{0xED, 0x80, 0x7E}}, false), - array_ret_pair_t({{0xED, 0x80, 0x7F}}, false), - - array_ret_pair_t({{0xED, 0x80, 0x80}}, true), - array_ret_pair_t({{0xED, 0x90, 0xA0}}, true), - array_ret_pair_t({{0xED, 0x9F, 0xBF}}, true), - - array_ret_pair_t({{0xED, 0xA0, 0xBF}}, false), - array_ret_pair_t({{0xED, 0xA1, 0xBF}}, false), - array_ret_pair_t({{0xED, 0x9F, 0xC0}}, false), - array_ret_pair_t({{0xED, 0x9F, 0xC1}}, false), - - ////////////////////////////////////////////// - - array_ret_pair_t({{0xEE, 0x7E, 0x80}}, false), - array_ret_pair_t({{0xEE, 0x7F, 0x80}}, false), - array_ret_pair_t({{0xEE, 0x80, 0x7E}}, false), - array_ret_pair_t({{0xEE, 0x80, 0x7F}}, false), - - array_ret_pair_t({{0xEE, 0x80, 0x80}}, true), - array_ret_pair_t({{0xEE, 0xA0, 0xA0}}, true), - array_ret_pair_t({{0xEF, 0xBF, 0xBF}}, true), - - array_ret_pair_t({{0xEF, 0xC0, 0xBF}}, false), - array_ret_pair_t({{0xEF, 0xC1, 0xBF}}, false), - array_ret_pair_t({{0xEF, 0xBF, 0xC0}}, false), - array_ret_pair_t({{0xEF, 0xBF, 0xC1}}, false), - array_ret_pair_t({{0xF0, 0xBF, 0xBF}}, false), - array_ret_pair_t({{0xF1, 0xBF, 0xBF}}, false)); - - REQUIRE(fkyaml::detail::utf8_encoding::validate(pair.first) == pair.second); - } - - SECTION("4 byte characters encoded in UTF-8") - { - using array_ret_pair_t = std::pair, bool>; - auto pair = GENERATE( - array_ret_pair_t({{0xDE, 0x90, 0x80, 0x80}}, false), - array_ret_pair_t({{0xDF, 0x90, 0x80, 0x80}}, false), - array_ret_pair_t({{0xE0, 0x8E, 0x80, 0x80}}, false), - array_ret_pair_t({{0xE0, 0x8F, 0x80, 0x80}}, false), - array_ret_pair_t({{0xE0, 0x90, 0x7E, 0x80}}, false), - array_ret_pair_t({{0xE0, 0x90, 0x7F, 0x80}}, false), - array_ret_pair_t({{0xE0, 0x90, 0x80, 0x7E}}, false), - array_ret_pair_t({{0xE0, 0x90, 0x80, 0x7F}}, false), - - array_ret_pair_t({{0xF0, 0x90, 0x80, 0x80}}, true), - array_ret_pair_t({{0xF0, 0xA8, 0xA0, 0xA0}}, true), - array_ret_pair_t({{0xF0, 0xBF, 0xBF, 0xBF}}, true), - - array_ret_pair_t({{0xF0, 0xC0, 0xBF, 0xBF}}, false), - array_ret_pair_t({{0xF0, 0xC1, 0xBF, 0xBF}}, false), - array_ret_pair_t({{0xF0, 0xBF, 0xC0, 0xBF}}, false), - array_ret_pair_t({{0xF0, 0xBF, 0xC1, 0xBF}}, false), - array_ret_pair_t({{0xF0, 0xBF, 0xBF, 0xC0}}, false), - array_ret_pair_t({{0xF0, 0xBF, 0xBF, 0xC1}}, false), - - //////////////////////////////////////////////////// - - array_ret_pair_t({{0xF1, 0x7E, 0x80, 0x80}}, false), - array_ret_pair_t({{0xF1, 0x7F, 0x80, 0x80}}, false), - array_ret_pair_t({{0xF1, 0x80, 0x7E, 0x80}}, false), - array_ret_pair_t({{0xF1, 0x80, 0x7F, 0x80}}, false), - array_ret_pair_t({{0xF1, 0x80, 0x80, 0x7E}}, false), - array_ret_pair_t({{0xF1, 0x80, 0x80, 0x7F}}, false), - - array_ret_pair_t({{0xF1, 0x80, 0x80, 0x80}}, true), - array_ret_pair_t({{0xF2, 0xA0, 0xA0, 0xA0}}, true), - array_ret_pair_t({{0xF3, 0xBF, 0xBF, 0xBF}}, true), - - array_ret_pair_t({{0xF3, 0xC0, 0xBF, 0xBF}}, false), - array_ret_pair_t({{0xF3, 0xC1, 0xBF, 0xBF}}, false), - array_ret_pair_t({{0xF3, 0xBF, 0xC0, 0xBF}}, false), - array_ret_pair_t({{0xF3, 0xBF, 0xC1, 0xBF}}, false), - array_ret_pair_t({{0xF3, 0xBF, 0xBF, 0xC0}}, false), - array_ret_pair_t({{0xF3, 0xBF, 0xBF, 0xC1}}, false), - - //////////////////////////////////////////////////// - - array_ret_pair_t({{0xF4, 0x7E, 0x80, 0x80}}, false), - array_ret_pair_t({{0xF4, 0x7F, 0x80, 0x80}}, false), - array_ret_pair_t({{0xF4, 0x80, 0x7E, 0x80}}, false), - array_ret_pair_t({{0xF4, 0x80, 0x7F, 0x80}}, false), - array_ret_pair_t({{0xF4, 0x80, 0x80, 0x7E}}, false), - array_ret_pair_t({{0xF4, 0x80, 0x80, 0x7F}}, false), - - array_ret_pair_t({{0xF4, 0x80, 0x80, 0x80}}, true), - array_ret_pair_t({{0xF4, 0x88, 0xA0, 0x80}}, true), - array_ret_pair_t({{0xF4, 0x8F, 0xBF, 0xBF}}, true), - - array_ret_pair_t({{0xF4, 0x90, 0xBF, 0xBF}}, false), - array_ret_pair_t({{0xF4, 0x91, 0xBF, 0xBF}}, false), - array_ret_pair_t({{0xF4, 0x8F, 0xC0, 0xBF}}, false), - array_ret_pair_t({{0xF4, 0x8F, 0xC1, 0xBF}}, false), - array_ret_pair_t({{0xF4, 0x8F, 0xBF, 0xC0}}, false), - array_ret_pair_t({{0xF4, 0x8F, 0xBF, 0xC1}}, false), - array_ret_pair_t({{0xF5, 0x8F, 0xBF, 0xBF}}, false), - array_ret_pair_t({{0xF6, 0x8F, 0xBF, 0xBF}}, false)); - - REQUIRE(fkyaml::detail::utf8_encoding::validate(pair.first) == pair.second); - } -} - -TEST_CASE("UTF8Encoding_FromUTF16") -{ - SECTION("valid UTF-16 character(s)") - { - struct test_params - { - std::array utf16; - std::array utf8_bytes; - std::size_t consumed_size; - std::size_t encoded_size; - }; - auto params = GENERATE( - test_params {{{char16_t(0x00u)}}, {{char(0x00u)}}, 1, 1}, - test_params {{{char16_t(0x01u)}}, {{char(0x01u)}}, 1, 1}, - test_params {{{char16_t(0x7Eu)}}, {{char(0x7Eu)}}, 1, 1}, - test_params {{{char16_t(0x7Fu)}}, {{char(0x7Fu)}}, 1, 1}, - test_params {{{char16_t(0x0080u)}}, {{char(0xC2u), char(0x80u)}}, 1, 2}, - test_params {{{char16_t(0x0081u)}}, {{char(0xC2u), char(0x81u)}}, 1, 2}, - test_params {{{char16_t(0x07FEu)}}, {{char(0xDFu), char(0xBEu)}}, 1, 2}, - test_params {{{char16_t(0x07FFu)}}, {{char(0xDFu), char(0xBFu)}}, 1, 2}, - test_params {{{char16_t(0x0800u)}}, {{char(0xE0u), char(0xA0u), char(0x80u)}}, 1, 3}, - test_params {{{char16_t(0x0801u)}}, {{char(0xE0u), char(0xA0u), char(0x81u)}}, 1, 3}, - test_params {{{char16_t(0xD7FEu)}}, {{char(0xEDu), char(0x9Fu), char(0xBEu)}}, 1, 3}, - test_params {{{char16_t(0xD7FFu)}}, {{char(0xEDu), char(0x9Fu), char(0xBFu)}}, 1, 3}, - test_params {{{char16_t(0xE000u)}}, {{char(0xEEu), char(0x80u), char(0x80u)}}, 1, 3}, - test_params {{{char16_t(0xE001u)}}, {{char(0xEEu), char(0x80u), char(0x81u)}}, 1, 3}, - test_params { - {{char16_t(0xD800u), char16_t(0xDC00u)}}, {{char(0xF0u), char(0x90u), char(0x80u), char(0x80u)}}, 2, 4}, - test_params { - {{char16_t(0xD801u), char16_t(0xDC00u)}}, {{char(0xF0u), char(0x90u), char(0x90u), char(0x80u)}}, 2, 4}, - test_params { - {{char16_t(0xD800u), char16_t(0xDC01u)}}, {{char(0xF0u), char(0x90u), char(0x80u), char(0x81u)}}, 2, 4}, - test_params { - {{char16_t(0xDBFEu), char16_t(0xDFFFu)}}, {{char(0xF4u), char(0x8Fu), char(0xAFu), char(0xBFu)}}, 2, 4}, - test_params { - {{char16_t(0xDBFFu), char16_t(0xDFFEu)}}, {{char(0xF4u), char(0x8Fu), char(0xBFu), char(0xBEu)}}, 2, 4}, - test_params { - {{char16_t(0xDBFFu), char16_t(0xDFFFu)}}, - {{char(0xF4u), char(0x8Fu), char(0xBFu), char(0xBFu)}}, - 2, - 4}); - - std::array utf8_bytes; - std::size_t consumed_size; - std::size_t encoded_size; - - fkyaml::detail::utf8_encoding::from_utf16(params.utf16, utf8_bytes, consumed_size, encoded_size); - - REQUIRE(utf8_bytes == params.utf8_bytes); - REQUIRE(consumed_size == params.consumed_size); - REQUIRE(encoded_size == params.encoded_size); - } - - SECTION("invalid UTF-16 character(s)") - { - auto utf16 = GENERATE( - std::array {{char16_t(0xDC00u), char16_t(0xDC00u)}}, - std::array {{char16_t(0xDBFFu), char16_t(0xDBFFu)}}, - std::array {{char16_t(0xDBFFu), char16_t(0xE000u)}}); - - std::array utf8_bytes; - std::size_t consumed_size; - std::size_t encoded_size; - - REQUIRE_THROWS_AS( - fkyaml::detail::utf8_encoding::from_utf16(utf16, utf8_bytes, consumed_size, encoded_size), - fkyaml::invalid_encoding); - } -} - -TEST_CASE("UTF8Encoding_FromUTF32") -{ - SECTION("valid UTF-32 character") - { - struct test_params - { - char32_t utf32; - std::array utf8_bytes; - std::size_t size; - }; - auto params = GENERATE( - test_params {0x00u, {{char(0x00u)}}, 1}, - test_params {0x01u, {{char(0x01u)}}, 1}, - test_params {0x7Eu, {{char(0x7Eu)}}, 1}, - test_params {0x7Fu, {{char(0x7Fu)}}, 1}, - test_params {0x0080u, {{char(0xC2u), char(0x80u)}}, 2}, - test_params {0x0081u, {{char(0xC2u), char(0x81u)}}, 2}, - test_params {0x07FEu, {{char(0xDFu), char(0xBEu)}}, 2}, - test_params {0x07FFu, {{char(0xDFu), char(0xBFu)}}, 2}, - test_params {0x0800u, {{char(0xE0u), char(0xA0u), char(0x80u)}}, 3}, - test_params {0x0801u, {{char(0xE0u), char(0xA0u), char(0x81u)}}, 3}, - test_params {0xFFFFu, {{char(0xEFu), char(0xBFu), char(0xBFu)}}, 3}, - test_params {0x010000u, {{char(0xF0u), char(0x90u), char(0x80u), char(0x80u)}}, 4}, - test_params {0x010001u, {{char(0xF0u), char(0x90u), char(0x80u), char(0x81u)}}, 4}, - test_params {0x10FFFEu, {{char(0xF4u), char(0x8Fu), char(0xBFu), char(0xBEu)}}, 4}, - test_params {0x10FFFFu, {{char(0xF4u), char(0x8Fu), char(0xBFu), char(0xBFu)}}, 4}); - - std::array utf8_bytes; - std::size_t size; - fkyaml::detail::utf8_encoding::from_utf32(params.utf32, utf8_bytes, size); - - REQUIRE(utf8_bytes == params.utf8_bytes); - REQUIRE(size == params.size); - } - - SECTION("invalid UTF-32 character") - { - char32_t utf32 = 0x110000u; - std::array utf8_bytes; - std::size_t encoded_size; - - REQUIRE_THROWS_AS( - fkyaml::detail::utf8_encoding::from_utf32(utf32, utf8_bytes, encoded_size), fkyaml::invalid_encoding); - } -} diff --git a/test/unit_test/test_utf_encodings.cpp b/test/unit_test/test_utf_encodings.cpp new file mode 100644 index 00000000..9cf2d24f --- /dev/null +++ b/test/unit_test/test_utf_encodings.cpp @@ -0,0 +1,356 @@ +// _______ __ __ __ _____ __ __ __ +// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) +// | __| _ < \_ _/| ___ | _ | |___ version 0.3.5 +// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML +// +// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani +// SPDX-License-Identifier: MIT + +#include +#include +#include + +#include + +#include + +TEST_CASE("UTF8_GetNumBytes") { + SECTION("valid bytes") { + using test_value_pair_t = std::pair; + auto pair = GENERATE( + test_value_pair_t(uint8_t(0u), 1u), + test_value_pair_t(uint8_t(0x7Fu), 1u), + test_value_pair_t(uint8_t(0xC0u), 2u), + test_value_pair_t(uint8_t(0xC1u), 2u), + test_value_pair_t(uint8_t(0xDFu), 2u), + test_value_pair_t(uint8_t(0xE0u), 3u), + test_value_pair_t(uint8_t(0xE1u), 3u), + test_value_pair_t(uint8_t(0xEFu), 3u), + test_value_pair_t(uint8_t(0xF0u), 4u), + test_value_pair_t(uint8_t(0xF1u), 4u)); + + REQUIRE(fkyaml::detail::utf8::get_num_bytes(pair.first) == pair.second); + } + + SECTION("invalid bytes") { + uint8_t byte = GENERATE(uint8_t(0x80u), uint8_t(0xF8u)); + REQUIRE_THROWS_AS(fkyaml::detail::utf8::get_num_bytes(byte), fkyaml::invalid_encoding); + } +} + +TEST_CASE("UTF8_Validate") { + SECTION("1 byte character encoded in UTF-8") { + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0x00u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0x01u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0x02u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0x7Du)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0x7Eu)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0x7Fu)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0x81u)}) == false); + } + + SECTION("2 byte characters encoded in UTF-8") { + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xC0u), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xC1u), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xC2u), uint8_t(0x7Eu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xC2u), uint8_t(0x7Fu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xC2u), uint8_t(0x80u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xC3u), uint8_t(0x81u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xD0u), uint8_t(0xA0u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xDEu), uint8_t(0xBEu)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xDFu), uint8_t(0xBFu)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xDFu), uint8_t(0xC0u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xDFu), uint8_t(0xC1u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0xBFu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xE1u), uint8_t(0xBFu)}) == false); + } + + SECTION("3 byte characters encoded in UTF-8") { + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xDEu), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xDFu), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x7Eu), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x7Fu), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x80u), uint8_t(0x7Eu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x80u), uint8_t(0x7Fu)}) == false); + + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x80u), uint8_t(0x80u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xE6u), uint8_t(0xA0u), uint8_t(0xA0u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xECu), uint8_t(0xBFu), uint8_t(0xBFu)}) == true); + + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xECu), uint8_t(0xC0u), uint8_t(0xBFu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xECu), uint8_t(0xC1u), uint8_t(0xBFu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xECu), uint8_t(0xBFu), uint8_t(0xC0u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xECu), uint8_t(0xBFu), uint8_t(0xC1u)}) == false); + + ////////////////////////////////////////////// + + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x7Eu), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x7Fu), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x80u), uint8_t(0x7Eu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x80u), uint8_t(0x7Fu)}) == false); + + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x80u), uint8_t(0x80u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x90u), uint8_t(0xA0u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x9Fu), uint8_t(0xBFu)}) == true); + + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0xA0u), uint8_t(0xBFu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0xA1u), uint8_t(0xBFu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x9Fu), uint8_t(0xC0u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEDu), uint8_t(0x9Fu), uint8_t(0xC1u)}) == false); + + ////////////////////////////////////////////// + + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEEu), uint8_t(0x7Eu), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEEu), uint8_t(0x7Fu), uint8_t(0x80u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEEu), uint8_t(0x80u), uint8_t(0x7Eu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEEu), uint8_t(0x80u), uint8_t(0x7Fu)}) == false); + + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEEu), uint8_t(0x80u), uint8_t(0x80u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEEu), uint8_t(0xA0u), uint8_t(0xA0u)}) == true); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEFu), uint8_t(0xBFu), uint8_t(0xBFu)}) == true); + + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEFu), uint8_t(0xC0u), uint8_t(0xBFu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEFu), uint8_t(0xC1u), uint8_t(0xBFu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEFu), uint8_t(0xBFu), uint8_t(0xC0u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xEFu), uint8_t(0xBFu), uint8_t(0xC1u)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + REQUIRE(fkyaml::detail::utf8::validate({uint8_t(0xF1u), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + } + + SECTION("4 byte characters encoded in UTF-8") { + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xDEu), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xDFu), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x8Eu), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x8Fu), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x90u), uint8_t(0x7Eu), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x90u), uint8_t(0x7Fu), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x7Eu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xE0u), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x7Fu)}) == false); + + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x80u)}) == true); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xA8u), uint8_t(0xA0u), uint8_t(0xA0u)}) == true); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xBFu), uint8_t(0xBFu), uint8_t(0xBFu)}) == true); + + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xC0u), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xC1u), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xBFu), uint8_t(0xC0u), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xBFu), uint8_t(0xC1u), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xBFu), uint8_t(0xBFu), uint8_t(0xC0u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF0u), uint8_t(0xBFu), uint8_t(0xBFu), uint8_t(0xC1u)}) == false); + + //////////////////////////////////////////////////// + + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF1u), uint8_t(0x7Eu), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF1u), uint8_t(0x7Fu), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF1u), uint8_t(0x80u), uint8_t(0x7Eu), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF1u), uint8_t(0x80u), uint8_t(0x7Fu), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF1u), uint8_t(0x80u), uint8_t(0x80u), uint8_t(0x7Eu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF1u), uint8_t(0x80u), uint8_t(0x80u), uint8_t(0x7Fu)}) == false); + + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF1u), uint8_t(0x80u), uint8_t(0x80u), uint8_t(0x80u)}) == true); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF2u), uint8_t(0xA0u), uint8_t(0xA0u), uint8_t(0xA0u)}) == true); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF3u), uint8_t(0xBFu), uint8_t(0xBFu), uint8_t(0xBFu)}) == true); + + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF3u), uint8_t(0xC0u), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF3u), uint8_t(0xC1u), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF3u), uint8_t(0xBFu), uint8_t(0xC0u), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF3u), uint8_t(0xBFu), uint8_t(0xC1u), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF3u), uint8_t(0xBFu), uint8_t(0xBFu), uint8_t(0xC0u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF3u), uint8_t(0xBFu), uint8_t(0xBFu), uint8_t(0xC1u)}) == false); + + //////////////////////////////////////////////////// + + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x7Eu), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x7Fu), uint8_t(0x80u), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x80u), uint8_t(0x7Eu), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x80u), uint8_t(0x7Fu), uint8_t(0x80u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x80u), uint8_t(0x80u), uint8_t(0x7Eu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x80u), uint8_t(0x80u), uint8_t(0x7Fu)}) == false); + + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x80u), uint8_t(0x80u), uint8_t(0x80u)}) == true); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x88u), uint8_t(0xA0u), uint8_t(0x80u)}) == true); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xBFu)}) == true); + + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x90u), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x91u), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xC0u), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xC1u), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xC0u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xC1u)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF5u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + REQUIRE( + fkyaml::detail::utf8::validate({uint8_t(0xF6u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xBFu)}) == false); + } +} + +TEST_CASE("UTF8_FromUTF16") { + SECTION("valid UTF-16 character(s)") { + struct test_params { + std::array utf16; + std::array utf8_bytes; + std::size_t consumed_size; + std::size_t encoded_size; + }; + auto params = GENERATE( + test_params {{{char16_t(0x00u)}}, {{uint8_t(0x00u)}}, 1, 1}, + test_params {{{char16_t(0x01u)}}, {{uint8_t(0x01u)}}, 1, 1}, + test_params {{{char16_t(0x7Eu)}}, {{uint8_t(0x7Eu)}}, 1, 1}, + test_params {{{char16_t(0x7Fu)}}, {{uint8_t(0x7Fu)}}, 1, 1}, + test_params {{{char16_t(0x0080u)}}, {{uint8_t(0xC2u), uint8_t(0x80u)}}, 1, 2}, + test_params {{{char16_t(0x0081u)}}, {{uint8_t(0xC2u), uint8_t(0x81u)}}, 1, 2}, + test_params {{{char16_t(0x07FEu)}}, {{uint8_t(0xDFu), uint8_t(0xBEu)}}, 1, 2}, + test_params {{{char16_t(0x07FFu)}}, {{uint8_t(0xDFu), uint8_t(0xBFu)}}, 1, 2}, + test_params {{{char16_t(0x0800u)}}, {{uint8_t(0xE0u), uint8_t(0xA0u), uint8_t(0x80u)}}, 1, 3}, + test_params {{{char16_t(0x0801u)}}, {{uint8_t(0xE0u), uint8_t(0xA0u), uint8_t(0x81u)}}, 1, 3}, + test_params {{{char16_t(0xD7FEu)}}, {{uint8_t(0xEDu), uint8_t(0x9Fu), uint8_t(0xBEu)}}, 1, 3}, + test_params {{{char16_t(0xD7FFu)}}, {{uint8_t(0xEDu), uint8_t(0x9Fu), uint8_t(0xBFu)}}, 1, 3}, + test_params {{{char16_t(0xE000u)}}, {{uint8_t(0xEEu), uint8_t(0x80u), uint8_t(0x80u)}}, 1, 3}, + test_params {{{char16_t(0xE001u)}}, {{uint8_t(0xEEu), uint8_t(0x80u), uint8_t(0x81u)}}, 1, 3}, + test_params { + {{char16_t(0xD800u), char16_t(0xDC00u)}}, + {{uint8_t(0xF0u), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x80u)}}, + 2, + 4}, + test_params { + {{char16_t(0xD801u), char16_t(0xDC00u)}}, + {{uint8_t(0xF0u), uint8_t(0x90u), uint8_t(0x90u), uint8_t(0x80u)}}, + 2, + 4}, + test_params { + {{char16_t(0xD800u), char16_t(0xDC01u)}}, + {{uint8_t(0xF0u), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x81u)}}, + 2, + 4}, + test_params { + {{char16_t(0xDBFEu), char16_t(0xDFFFu)}}, + {{uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xAFu), uint8_t(0xBFu)}}, + 2, + 4}, + test_params { + {{char16_t(0xDBFFu), char16_t(0xDFFEu)}}, + {{uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xBEu)}}, + 2, + 4}, + test_params { + {{char16_t(0xDBFFu), char16_t(0xDFFFu)}}, + {{uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xBFu)}}, + 2, + 4}); + + std::array utf8_bytes; + utf8_bytes.fill(0); + std::size_t consumed_size {0}; + std::size_t encoded_size {0}; + + fkyaml::detail::utf8::from_utf16(params.utf16, utf8_bytes, consumed_size, encoded_size); + + REQUIRE(utf8_bytes == params.utf8_bytes); + REQUIRE(consumed_size == params.consumed_size); + REQUIRE(encoded_size == params.encoded_size); + } + + SECTION("invalid UTF-16 character(s)") { + auto utf16 = GENERATE( + std::array {{char16_t(0xDC00u), char16_t(0xDC00u)}}, + std::array {{char16_t(0xDBFFu), char16_t(0xDBFFu)}}, + std::array {{char16_t(0xDBFFu), char16_t(0xE000u)}}); + + std::array utf8_bytes; + std::size_t consumed_size; + std::size_t encoded_size; + + REQUIRE_THROWS_AS( + fkyaml::detail::utf8::from_utf16(utf16, utf8_bytes, consumed_size, encoded_size), fkyaml::invalid_encoding); + } +} + +TEST_CASE("UTF8_FromUTF32") { + SECTION("valid UTF-32 character") { + struct test_params { + char32_t utf32; + std::array utf8_bytes; + std::size_t size; + }; + auto params = GENERATE( + test_params {0x00u, {{uint8_t(0x00u)}}, 1}, + test_params {0x01u, {{uint8_t(0x01u)}}, 1}, + test_params {0x7Eu, {{uint8_t(0x7Eu)}}, 1}, + test_params {0x7Fu, {{uint8_t(0x7Fu)}}, 1}, + test_params {0x0080u, {{uint8_t(0xC2u), uint8_t(0x80u)}}, 2}, + test_params {0x0081u, {{uint8_t(0xC2u), uint8_t(0x81u)}}, 2}, + test_params {0x07FEu, {{uint8_t(0xDFu), uint8_t(0xBEu)}}, 2}, + test_params {0x07FFu, {{uint8_t(0xDFu), uint8_t(0xBFu)}}, 2}, + test_params {0x0800u, {{uint8_t(0xE0u), uint8_t(0xA0u), uint8_t(0x80u)}}, 3}, + test_params {0x0801u, {{uint8_t(0xE0u), uint8_t(0xA0u), uint8_t(0x81u)}}, 3}, + test_params {0xFFFFu, {{uint8_t(0xEFu), uint8_t(0xBFu), uint8_t(0xBFu)}}, 3}, + test_params {0x010000u, {{uint8_t(0xF0u), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x80u)}}, 4}, + test_params {0x010001u, {{uint8_t(0xF0u), uint8_t(0x90u), uint8_t(0x80u), uint8_t(0x81u)}}, 4}, + test_params {0x10FFFEu, {{uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xBEu)}}, 4}, + test_params {0x10FFFFu, {{uint8_t(0xF4u), uint8_t(0x8Fu), uint8_t(0xBFu), uint8_t(0xBFu)}}, 4}); + + std::array utf8_bytes; + utf8_bytes.fill(0); + std::size_t size {0}; + fkyaml::detail::utf8::from_utf32(params.utf32, utf8_bytes, size); + + REQUIRE(utf8_bytes == params.utf8_bytes); + REQUIRE(size == params.size); + } + + SECTION("invalid UTF-32 character") { + char32_t utf32 = 0x110000u; + std::array utf8_bytes; + std::size_t encoded_size; + + REQUIRE_THROWS_AS(fkyaml::detail::utf8::from_utf32(utf32, utf8_bytes, encoded_size), fkyaml::invalid_encoding); + } +} diff --git a/tool/amalgamation/CHANGES.md b/tool/amalgamation/CHANGES.md index 4201734c..9c302a66 100644 --- a/tool/amalgamation/CHANGES.md +++ b/tool/amalgamation/CHANGES.md @@ -5,3 +5,4 @@ The following changes have been made to the code with respect to CRLF) after running amalgamation on Windows. diff --git a/tool/amalgamation/amalgamate.py b/tool/amalgamation/amalgamate.py index 0c177c7c..2fe35ded 100644 --- a/tool/amalgamation/amalgamate.py +++ b/tool/amalgamation/amalgamate.py @@ -91,7 +91,9 @@ def generate(self): t = TranslationUnit(file_path, self, True) amalgamation += t.content - with open(self.target, 'w') as f: + # Force newline codes to be LF. + # Without the parameter, they would be CRLF on Windows. + with open(self.target, 'w', newline='\n') as f: f.write(amalgamation) print("...done!\n") diff --git a/tool/natvis_generator/params.json b/tool/natvis_generator/params.json index fb8ca861..e2a9c041 100644 --- a/tool/natvis_generator/params.json +++ b/tool/natvis_generator/params.json @@ -1 +1 @@ -{ "version": "0.3.4" } +{ "version": "0.3.5" }