Skip to content

Commit

Permalink
Issue #7, #16, #17 | Small enhancements (#18)
Browse files Browse the repository at this point in the history
* Issue #7 | Fix state insolvency, add more debug code, add YO_ prefix to internal macros

* Issue #7 | Try to fix the GitHub Action pipeline

* Committing clang-format changes

* Issue #7 | Trigger clang-format and fix logo and name

* Issue #0 | General readability enhancements

* Committing clang-format changes

* Issue #17 | Add examples of library optimisation to README.md

---------

Co-authored-by: Clang Robot <robot@example.com>
Co-authored-by: GitHub Actions <actions@github.com>
  • Loading branch information
3 people committed Jul 3, 2023
1 parent c789968 commit 7116ab5
Show file tree
Hide file tree
Showing 35 changed files with 615 additions and 378 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}

- uses: DoozyX/clang-format-lint-action@v0.16.2
with:
source: '.'
clangFormatVersion: 16
style: file
inplace: True

- uses: EndBug/add-and-commit@v9
with:
author_name: Clang Robot
author_email: robot@example.com
author_name: GitHub Actions
author_email: actions@github.com
message: 'Committing clang-format changes'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -61,4 +64,3 @@ jobs:
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ add_subdirectory(deps/rapidyaml)
set(LIB_NAME yaml-optimizer-lib)

# Add optimization library as a dependency
add_subdirectory(src)
add_subdirectory(optimizer)

if (YO_BUILD_TESTS)
add_subdirectory(test)
Expand Down
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,72 @@ The project aims to provide a fast and convenient way to optimise large YAML con
- [Lyra](https://github.com/bfgroup/Lyra.git): A simple-to-use, composing, header-only, command line arguments parser for C++ 11 and beyond.
- [fmt](https://github.com/fmtlib/fmt): A formatting library providing a fast and safe alternative to C stdio and C++ iostreams.

## Examples

Currently library only supports optimizing fully identical nodes:

```yaml
# YAML example with references and anchors

# Define an anchor for a nested sequence
- top_level:
- nested:
- - Apple
- Orange
- Banana

# Define an anchor for a nested mapping
- another_level:
- even_deeper:
- further:
- key1: value1
key2: value2

# Use the anchors in different parts of the YAML
- other_level:
- some_key:
- Apple
- Orange
- Banana
- additional_key:
key1: value1
key2: value2

# Additional data
- SomeList:
- 1
- 2
- 3
- 4
- SomeValue: true
```

So the result would be (note that indentations and comments are currently removed):

```yaml
- top_level:
- nested:
- &anchor_0
- Apple
- Orange
- Banana
- another_level:
- even_deeper:
- further:
- &anchor_1
key1: value1
key2: value2
- other_level:
- some_key: *anchor_0
- additional_key: *anchor_1
- SomeList:
- 1
- 2
- 3
- 4
- SomeValue: true
```

## Roadmap

- [x] Replace identical blocks with anchors to the first one
Expand Down
3 changes: 2 additions & 1 deletion src/CMakeLists.txt → optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_library(${LIB_NAME} STATIC optimizer.cpp)
file(GLOB LIB_OPTIMIZER_SOURCES "src/**/*.cpp")
add_library(${LIB_NAME} STATIC ${LIB_OPTIMIZER_SOURCES})

target_include_directories(${LIB_NAME} PUBLIC inc)

Expand Down
19 changes: 19 additions & 0 deletions optimizer/inc/debug/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "debug/exception.h"
#include "debug/throw.h"

#include <sstream>

#define YO_DEBUG_ASSERT(condition) YO_DEBUG_ASSERT_WITH_MSG(condition, "");

#define YO_DEBUG_ASSERT_WITH_MSG(condition, description, ...) \
do \
{ \
if (!(condition)) \
{ \
YO_THROW(YamlOptimizerError, \
"Assertion failed: {} in file " __FILE__ " at line {}", \
fmt::format(description, ##__VA_ARGS__), __LINE__) \
} \
} while (false);
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
class YamlOptimizerError : public std::runtime_error
{
public:
YamlOptimizerError(std::string const& what) : std::runtime_error(what) {}
YamlOptimizerError(std::string const& what);
};
4 changes: 2 additions & 2 deletions src/inc/debug/print.hpp → optimizer/inc/debug/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#ifndef YO_DEBUG

#define DEBUG_PRINT(...)
#define YO_DEBUG_PRINT(...)

#else

#include <fmt/format.h>

#define DEBUG_PRINT(message, ...) \
#define YO_DEBUG_PRINT(message, ...) \
fmt::print("[DEBUG] " message "\n", ##__VA_ARGS__)

#endif // YO_DEBUG
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class YamlOptimizer
*/
void dump(std::string const& filename);

NAMED_PRIVATE_SECTION(Input data)
private:
/* clang-format off */
NAMED_PRIVATE_SECTION(Input data)

const OptimizationSettings settings_;

Expand All @@ -68,7 +70,7 @@ class YamlOptimizer
static constexpr std::string_view BOM{"\xEF\xBB\xBF"};
bool is_utf8 = false;

NAMED_PRIVATE_SECTION(Processed data)
NAMED_PRIVATE_SECTION(Processed data)

struct NodeInfo
{
Expand All @@ -79,27 +81,28 @@ class YamlOptimizer
ryml::Tree tree_;
std::size_t anchor_count_ = 0;

NAMED_PRIVATE_SECTION(Tree pre - processing utils)
NAMED_PRIVATE_SECTION(Tree pre-processing utils)

void get_info();
std::size_t get_info_impl(ryml::ConstNodeRef const& node);

NAMED_PRIVATE_SECTION(Node utils)
NAMED_PRIVATE_SECTION(Node utils)

bool nodes_equal(ryml::ConstNodeRef const& a,
ryml::ConstNodeRef const& b) const;
bool long_types_equal(ryml::ConstNodeRef const& a,
ryml::ConstNodeRef const& b) const;

NAMED_PRIVATE_SECTION(IO utils)
NAMED_PRIVATE_SECTION(IO utils)

void write_to_ostream(std::ostream& os) const;
ryml::substr get_clean_content(std::string& content);

#ifdef YO_DEBUG
NAMED_PRIVATE_SECTION(Debug utils)
NAMED_PRIVATE_SECTION(Debug utils)

void debug_print_data() const;

#endif // YO_DEBUG
};
/* clang-format on */
}; // class YamlOptimizer
File renamed without changes.
11 changes: 11 additions & 0 deletions optimizer/inc/utils/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <istream>
#include <string>

namespace io_utils
{

std::string get_file_content(std::istream& is);

} // namespace io_utils
29 changes: 29 additions & 0 deletions optimizer/inc/utils/node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "ryml.hpp"

namespace node_utils
{

void set_reference(ryml::NodeRef& node, ryml::csubstr anchor);

std::size_t
get_next_valid_id_after_content_removal(ryml::NodeRef node,
std::size_t max_possible_id);

std::string to_string(const ryml::ConstNodeRef& node);

class NodeDiff
{
ryml::ConstNodeRef lhs_;
ryml::ConstNodeRef rhs_;

public:
NodeDiff(const ryml::ConstNodeRef& lhs, const ryml::ConstNodeRef& rhs);

std::string str() const;

friend std::ostream& operator<<(std::ostream& os, NodeDiff const& diff);
};

} // namespace node_utils
55 changes: 55 additions & 0 deletions optimizer/inc/utils/platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

/* clang-format off */
#if defined(__APPLE__) && defined(__MACH__)
// Apple platforms
#define YO_APPLE

#include <TargetConditionals.h>
#if TARGET_OS_MAC
// macOS
#define YO_MACOS
#endif

#if TARGET_OS_IOS
// iOS
#define YO_IOS
#endif

#if TARGET_OS_TV
// tvOS
#define YO_TVOS
#endif

#if TARGET_OS_WATCH
// watchOS
#define YO_WATCHOS
#endif
#elif defined(_WIN32) || defined(_WIN64)
// Windows platforms
#define YO_WINDOWS

#ifdef _WIN32
#define YO_WINDOWS32
#endif

#ifdef _WIN64
#define YO_WINDOWS64
#endif
#elif defined(__linux__)
// Linux platform
#define YO_LINUX
#elif defined(__ANDROID__)
// Android platform
#define YO_ANDROID
#elif defined(__FreeBSD__)
// FreeBSD platform
#define YO_FREEBSD
#elif defined(__unix__) || defined(__unix)
// Generic Unix platform
#define YO_UNIX
#else
// Unsupported platform
#error Unsupported platform
#endif
/* clang-format off */
27 changes: 27 additions & 0 deletions optimizer/inc/utils/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <string>

namespace string_utils
{

// Function to check if a string starts with a given substring
bool starts_with(std::string_view str, std::string_view start);

// Function to check if a string ends with a given substring
bool ends_with(std::string_view str, std::string_view end);

class StringDiff
{
std::string lhs_;
std::string rhs_;

public:
StringDiff(const std::string& lhs, const std::string& rhs);

std::string str() const;

friend std::ostream& operator<<(std::ostream& os, StringDiff const& diff);
}; // class StringDiff

} // namespace string_utils
6 changes: 6 additions & 0 deletions optimizer/src/debug/exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "debug/exception.h"

YamlOptimizerError::YamlOptimizerError(std::string const& what)
: std::runtime_error(what)
{
}
Loading

0 comments on commit 7116ab5

Please sign in to comment.