added utils

This commit is contained in:
2026-01-03 21:14:46 +01:00
parent 569de543f0
commit 1460db6587
4 changed files with 74 additions and 4 deletions

View File

@@ -1,10 +1,12 @@
CXX = g++ CXX = g++
CXXFLAGS = -std=c++17 -Wall CXXFLAGS = -std=c++17 -Wall
TEST_LIBS = -lmd4c-html -lCatch2Main -lCatch2
LIBS = -lmd4c-html
all: blog all: blog
blog: main.o config.o blog: main.o config.o utils.o
$(CXX) $(CXXFLAGS) main.o config.o -o blog $(CXX) $(CXXFLAGS) main.o config.o -o blog $(LIBS)
main.o: main.cpp config.hpp main.o: main.cpp config.hpp
$(CXX) $(CXXFLAGS) -c main.cpp $(CXX) $(CXXFLAGS) -c main.cpp
@@ -12,8 +14,11 @@ main.o: main.cpp config.hpp
config.o: config.cpp config.hpp config.o: config.cpp config.hpp
$(CXX) $(CXXFLAGS) -c config.cpp $(CXX) $(CXXFLAGS) -c config.cpp
tests: tests.o config.o utils.o: utils.cpp utils.hpp
g++ -std=c++17 -Wall tests.o config.o -o tests -lCatch2Main -lCatch2 $(CXX) $(CXXFLAGS) -c utils.cpp
tests: tests.o config.o utils.o
g++ -std=c++17 -Wall tests.o config.o utils.o -o tests $(TEST_LIBS)
tests.o: tests.cpp config.hpp tests.o: tests.cpp config.hpp
$(CXX) $(CXXFLAGS) -c tests.cpp $(CXX) $(CXXFLAGS) -c tests.cpp

View File

@@ -1,4 +1,5 @@
#include "config.hpp" #include "config.hpp"
#include "utils.hpp"
#include <catch2/catch_all.hpp> #include <catch2/catch_all.hpp>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <string> #include <string>
@@ -14,3 +15,9 @@ TEST_CASE("Checking config file read", "[config]") {
REQUIRE(config.general.lang == "en"); REQUIRE(config.general.lang == "en");
REQUIRE(config.general.title == "Blog"); REQUIRE(config.general.title == "Blog");
} }
TEST_CASE("convert_string_to_set parses inline YAML string list") {
const std::string input = "[\"haha\", \"dupa\", \"xdd\"]";
const std::set<std::string> output = {"haha", "dupa", "xdd"};
REQUIRE(convert_string_to_set(input) == output);
}

37
src/utils.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include <set>
#include <sstream>
#include <string>
void trim(std::string &str) {
str.erase(str.find_last_not_of(' ') + 1);
str.erase(0, str.find_first_not_of(' '));
}
std::set<std::string> convert_string_to_set(std::string input) {
std::set<std::string> result;
std::string buffer;
size_t left, right;
left = input.find_first_of('[');
right = input.find_last_of(']');
if (left == std::string::npos || right == std::string::npos ||
right <= left) {
return result;
}
input = input.substr(left + 1, right - left - 1);
std::stringstream ss(input);
while (getline(ss, buffer, ',')) {
buffer.erase(0, buffer.find_first_not_of(" \t"));
buffer.erase(buffer.find_last_not_of(" \t") + 1);
if (buffer.size() >= 2 && buffer.front() == '"' && buffer.back() == '"') {
buffer = buffer.substr(1, buffer.size() - 2);
result.insert(buffer);
}
}
return result;
}

21
src/utils.hpp Normal file
View File

@@ -0,0 +1,21 @@
#include <set>
#include <string>
// trim string in-place, removing all whitespaces
void trim(std::string &str);
// Convert a string containing an inline YAML/JSON-style list of strings
// into a std::set<std::string>.
//
// Supported format (single line):
// ["haha", "dupa", "xdd"]
//
// Limitations:
// - no nested structures
// - no escaping inside strings
// - elements must be quoted
//
// Example:
// Input: ["haha", "dupa", "xdd"]
// Output: {"dupa", "haha", "xdd"} // sorted, unique
std::set<std::string> convert_string_to_set(std::string input);