Skip to content

Commit

Permalink
add missing features and some optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
yudonglin committed Jan 27, 2024
1 parent 3780af5 commit 0d0a500
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 155 deletions.
2 changes: 1 addition & 1 deletion vns-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ project (vns)
file(GLOB SOURCES "libs/*/*.h" "libs/*/*.cpp" "libs/*/*.hpp")

# Add source to this project's executable.
add_executable (vns ${SOURCES} "vns.cpp" "vns.h" "processor.cpp" "naming.cpp" "naming.h" "exceptions.h" "content.h" "content.cpp" "contentNext.h" "contentNext.cpp" "processor.h" "functions.h" "tests.h" "tests.cpp" "functions.cpp" "compiler.h" "compiler.cpp" "version.h")
add_executable (vns ${SOURCES} "vns.cpp" "vns.h" "processor.cpp" "naming.cpp" "naming.h" "content.h" "content.cpp" "contentNext.h" "contentNext.cpp" "processor.h" "functions.h" "tests.h" "tests.cpp" "functions.cpp" "compiler.h" "compiler.cpp" "version.h")

if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET vns PROPERTY CXX_STANDARD 20)
Expand Down
2 changes: 0 additions & 2 deletions vns-cpp/content.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#ifndef CONTENT_H
#define CONTENT_H
#include <string>
#include <vector>
#include <unordered_map>
#include <string>
#include <vector>
#include "contentNext.h"

Expand Down
2 changes: 1 addition & 1 deletion vns-cpp/contentNext.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ContentNext
[[nodiscard]] bool has_multi_targets() const;
[[nodiscard]] bool is_null() const;
[[nodiscard]] std::unordered_map<std::string, ContentNextValueType> to_map() const;
nlohmann::json to_json() const;
[[nodiscard]] nlohmann::json to_json() const;

private:
std::string type_;
Expand Down
18 changes: 0 additions & 18 deletions vns-cpp/exceptions.h

This file was deleted.

41 changes: 33 additions & 8 deletions vns-cpp/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "functions.h"
#include <algorithm>
#include <sstream>

bool ichar_equals(char a, char b)
bool ichar_equals(const char a, const char b)
{
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
Expand All @@ -12,34 +13,34 @@ bool iequals(std::string_view lhs, std::string_view rhs)
return std::ranges::equal(lhs, rhs, ichar_equals);
}

// trim from start (in place)
// trim from start
std::string ltrim(std::string s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch)
s.erase(s.begin(), std::ranges::find_if(s.begin(), s.end(), [](const unsigned char ch)
{
return !std::isspace(ch);
}));
return s;
}

// trim from end (in place)
// trim from end
std::string rtrim(std::string s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch)
s.erase(std::find_if(s.rbegin(), s.rend(), [](const unsigned char ch)
{
return !std::isspace(ch);
}).base(), s.end());
return s;
}

// trim from both ends (in place)
std::string trim(std::string s)
// trim from both ends
std::string trim(const std::string& s)
{
return rtrim(ltrim(s));
}

// convert string to lower case
std::string toLowerCase(const std::string& input)
std::string to_lower(const std::string& input)
{
std::string result = input;

Expand All @@ -50,3 +51,27 @@ std::string toLowerCase(const std::string& input)

return result;
}

// split by given character
std::vector<std::string> split(const std::string& str, const char c)
{
std::string s;
std::stringstream ss(str);
std::vector<std::string> result;

while (getline(ss, s, c))
{
if (!s.empty())
{
result.push_back(s);
}
}

return result;
}

// split by space
std::vector<std::string> split(const std::string& str)
{
return split(str, ' ');
}
17 changes: 12 additions & 5 deletions vns-cpp/functions.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
#include <vector>

bool ichar_equals(char, char);

bool iequals(std::string_view, std::string_view);

// trim from start (in place)
// trim from start
std::string ltrim(std::string);

// trim from end (in place)
// trim from end
std::string rtrim(std::string);

// trim from both ends (in place)
std::string trim(std::string);
// trim from both ends
std::string trim(const std::string&);

// convert string to lower case
std::string toLowerCase(const std::string&);
std::string to_lower(const std::string&);

// split by given character
std::vector<std::string> split(const std::string&, char);

// split by space
std::vector<std::string> split(const std::string&);

#endif
31 changes: 18 additions & 13 deletions vns-cpp/naming.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "naming.h"
#include <ranges>
#include <sstream>

// Constructor
Naming::Naming(const std::string& the_name)
Expand All @@ -16,53 +17,57 @@ Naming::Naming(const std::string& the_name)
}

// Get combined name and tags as a string
std::string Naming::ToString() const
std::string Naming::to_string() const
{
std::string result = name_;
std::stringstream result;
result << name_;
for (const auto& tag : tags_)
{
result += "&" + tag;
result << "&" << tag;
}
return result;
return result.str();
}

// Accessor for name
std::string Naming::GetName() const
std::string Naming::get_name() const
{
return name_;
}

// Accessor for tags
std::unordered_set<std::string> Naming::GetTags() const
std::unordered_set<std::string> Naming::get_tags() const
{
return tags_;
}

// If contains a tag
bool Naming::ContainsTag(const std::string& tag) const
bool Naming::contains_tag(const std::string& tag) const
{
return tags_.contains(tag);
}

// Insert a tag
void Naming::InsertTag(const std::string& tag)
void Naming::insert_tag(const std::string& tag)
{
tags_.insert(tag);
}

// Erase a tag
void Naming::EraseTag(const std::string& tag)
void Naming::erase_tag(const std::string& tag)
{
tags_.erase(tag);
if (tags_.contains(tag))
{
tags_.erase(tag);
}
}

// Check if two Naming objects or a Naming object and a string refer to the same character
bool Naming::Equal(const std::variant<Naming, std::string>& o, const bool must_be_the_same) const
bool Naming::equal(const std::variant<Naming, std::string>& o, const bool must_be_the_same) const
{
const Naming other = std::holds_alternative<std::string>(o)
? Naming(std::get<std::string>(o))
: std::get<Naming>(o);
if (name_ == other.GetName())
if (name_ == other.get_name())
{
return true;
}
Expand All @@ -73,7 +78,7 @@ bool Naming::Equal(const std::variant<Naming, std::string>& o, const bool must_b
{
if (std::ranges::find(value.begin(), value.end(), name_) != value.end())
{
return std::ranges::find(value.begin(), value.end(), other.GetName()) != value.end();
return std::ranges::find(value.begin(), value.end(), other.get_name()) != value.end();
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions vns-cpp/naming.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@ class Naming
{
std::string name_;
std::unordered_set<std::string> tags_;
inline static std::unordered_map<std::string, std::vector<std::string>> DATABASE_ = {};
inline static std::unordered_map<std::string, std::vector<std::string>> DATABASE_;

public:
Naming(const std::string&);
[[nodiscard]] std::string ToString() const;
[[nodiscard]] std::string GetName() const;
[[nodiscard]] std::unordered_set<std::string> GetTags() const;
[[nodiscard]] bool ContainsTag(const std::string&) const;
void InsertTag(const std::string&);
void EraseTag(const std::string&);
[[nodiscard]] std::string to_string() const;
[[nodiscard]] std::string get_name() const;
[[nodiscard]] std::unordered_set<std::string> get_tags() const;
[[nodiscard]] bool contains_tag(const std::string&) const;
void insert_tag(const std::string&);
void erase_tag(const std::string&);
[[nodiscard]] bool equal(const std::variant<Naming, std::string>&, bool must_be_the_same = false) const;
// Class method to access the database
static std::unordered_map<std::string, std::vector<std::string>>& GetDatabase()
static std::unordered_map<std::string, std::vector<std::string>>& get_database()
{
return DATABASE_;
}

[[nodiscard]] bool Equal(const std::variant<Naming, std::string>&, bool must_be_the_same = false) const;
};
#endif
Loading

0 comments on commit 0d0a500

Please sign in to comment.