Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #16 | Small enhancements #18

Merged
merged 8 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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