From 4acfd3a4930e68c9083920c9650c241160b99c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tomic=CC=8Cevic=CC=81?= Date: Sun, 21 Jun 2015 21:16:14 +0200 Subject: [PATCH] addded some basic utils for dealing with strings --- utils/string/all.hpp | 9 ++++++++ utils/string/intercalate.hpp | 32 +++++++++++++++++++++++++ utils/string/linereader.hpp | 35 ++++++++++++++++++++++++++++ utils/string/replace.hpp | 25 ++++++++++++++++++++ utils/string/split.hpp | 45 ++++++++++++++++++++++++++++++++++++ utils/sync/all.hpp | 6 +++++ utils/sync/spinlock.hpp | 23 ++++++++++-------- utils/utils.hpp | 7 ++++++ 8 files changed, 172 insertions(+), 10 deletions(-) create mode 100644 utils/string/all.hpp create mode 100644 utils/string/intercalate.hpp create mode 100644 utils/string/linereader.hpp create mode 100644 utils/string/replace.hpp create mode 100644 utils/string/split.hpp create mode 100644 utils/sync/all.hpp create mode 100644 utils/utils.hpp diff --git a/utils/string/all.hpp b/utils/string/all.hpp new file mode 100644 index 000000000..fc3e3f35f --- /dev/null +++ b/utils/string/all.hpp @@ -0,0 +1,9 @@ +#ifndef MEMGRAPH_UTILS_STRING_ALL_HPP +#define MEMGRAPH_UTILS_STRING_ALL_HPP + +#include "intercalate.hpp" +#include "replace.hpp" +#include "split.hpp" +#include "linereader.hpp" + +#endif diff --git a/utils/string/intercalate.hpp b/utils/string/intercalate.hpp new file mode 100644 index 000000000..5a4e3a7d2 --- /dev/null +++ b/utils/string/intercalate.hpp @@ -0,0 +1,32 @@ +#ifndef MEMGRAPH_UTILS_INTERCALATE_HPP +#define MEMGRAPH_UTILS_INTERCALATE_HPP + +#include +#include + +namespace utils +{ + +template +std::string intercalate(It first, It last, + const std::string& separator) +{ + if(first == last) + return ""; + + std::stringstream ss; + It second(first); + + // append the first N-1 elements with a separator + for(second++; second != last; ++first, ++second) + ss << *first << separator; + + // append the last element + ss << *first; + + return ss.str(); +} + +} + +#endif diff --git a/utils/string/linereader.hpp b/utils/string/linereader.hpp new file mode 100644 index 000000000..51060af60 --- /dev/null +++ b/utils/string/linereader.hpp @@ -0,0 +1,35 @@ +#ifndef MEMGRAPH_UTILS_LINEREADER_HPP +#define MEMGRAPH_UTILS_LINEREADER_HPP + +#include +#include +#include + +namespace utils +{ + +void linereader(std::istream& stream, + std::function cb) +{ + std::string line; + + while(std::getline(stream, line)) + cb(line); +} + +void linereader(const std::string& filename, + std::function cb) +{ + std::fstream fs(filename.c_str()); + + // should this throw an error? figure out how to handle this TODO + + if(fs.is_open() == false) + throw std::runtime_error("[ERROR] can't open file " + filename); + + linereader(fs, cb); +} + +} + +#endif diff --git a/utils/string/replace.hpp b/utils/string/replace.hpp new file mode 100644 index 000000000..ee5a1d1c7 --- /dev/null +++ b/utils/string/replace.hpp @@ -0,0 +1,25 @@ +#ifndef MEMGRAPH_UTILS_REPLACE_HPP +#define MEMGRAPH_UTILS_REPLACE_HPP + +#include + +namespace utils +{ + +// replaces all occurences of in with + +std::string replace(std::string src, + const std::string& match, + const std::string& replacement) +{ + for(size_t pos = src.find(match); + pos != std::string::npos; + pos = src.find(match, pos + replacement.size())) + src.erase(pos, match.length()).insert(pos, replacement); + + return src; +} + +} + +#endif diff --git a/utils/string/split.hpp b/utils/string/split.hpp new file mode 100644 index 000000000..fef1468f3 --- /dev/null +++ b/utils/string/split.hpp @@ -0,0 +1,45 @@ +#ifndef MEMGRAPH_UTILS_SPLIT_HPP +#define MEMGRAPH_UTILS_SPLIT_HPP + +#include +#include + +namespace utils +{ + +std::vector split(const std::string& src, + const std::string& delimiter) +{ + size_t index = 0; + std::vector res; + size_t n = src.find(delimiter, index); + + while(n != std::string::npos) + { + n = src.find(delimiter, index); + res.push_back(src.substr(index, n - index)); + index = n + delimiter.size(); + } + + return res; +} + + +// doesn't work with gcc even though it's only c++11... +// and it's slow as hell compared to the split implementation above +// (more powerful though) + +std::vector regex_split(const std::string& input, + const std::string& regex) +{ + auto rgx = std::regex(regex); + + std::sregex_token_iterator last, first + { input.begin(), input.end(), rgx, -1}; + + return { first, last }; +} + +} + +#endif diff --git a/utils/sync/all.hpp b/utils/sync/all.hpp new file mode 100644 index 000000000..02ac177b2 --- /dev/null +++ b/utils/sync/all.hpp @@ -0,0 +1,6 @@ +#ifndef MEMGRAPH_UTILS_SYNC_ALL_HPP +#define MEMGRAPH_UTILS_SYNC_ALL_HPP + +#include "spinlock.hpp" + +#endif diff --git a/utils/sync/spinlock.hpp b/utils/sync/spinlock.hpp index f9acb75af..3c7a686fd 100644 --- a/utils/sync/spinlock.hpp +++ b/utils/sync/spinlock.hpp @@ -7,19 +7,22 @@ class SpinLock { public: - void acquire() - { - while(lock.test_and_set(std::memory_order_acquire)) - usleep(250); - } - - void release() - { - lock.clear(std::memory_order_release); - } + void acquire(); + void release(); private: std::atomic_flag lock = ATOMIC_FLAG_INIT; }; +void SpinLock::acquire() +{ + while(lock.test_and_set(std::memory_order_acquire)) + usleep(250); +} + +void SpinLock::release() +{ + lock.clear(std::memory_order_release); +} + #endif diff --git a/utils/utils.hpp b/utils/utils.hpp new file mode 100644 index 000000000..5f9d4a269 --- /dev/null +++ b/utils/utils.hpp @@ -0,0 +1,7 @@ +#ifndef MEMGRAPH_UTILS_UTILS_HPP +#define MEMGRAPH_UTILS_UTILS_HPP + +#include "string/all.hpp" +#include "sync/all.hpp" + +#endif