addded some basic utils for dealing with strings

This commit is contained in:
Dominik Tomičević 2015-06-21 21:16:14 +02:00
parent b4a6ca3487
commit 4acfd3a493
8 changed files with 172 additions and 10 deletions

9
utils/string/all.hpp Normal file
View File

@ -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

View File

@ -0,0 +1,32 @@
#ifndef MEMGRAPH_UTILS_INTERCALATE_HPP
#define MEMGRAPH_UTILS_INTERCALATE_HPP
#include <sstream>
#include <string>
namespace utils
{
template <typename It>
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

View File

@ -0,0 +1,35 @@
#ifndef MEMGRAPH_UTILS_LINEREADER_HPP
#define MEMGRAPH_UTILS_LINEREADER_HPP
#include <functional>
#include <fstream>
#include <string>
namespace utils
{
void linereader(std::istream& stream,
std::function<void(const std::string&)> cb)
{
std::string line;
while(std::getline(stream, line))
cb(line);
}
void linereader(const std::string& filename,
std::function<void(const std::string&)> 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

25
utils/string/replace.hpp Normal file
View File

@ -0,0 +1,25 @@
#ifndef MEMGRAPH_UTILS_REPLACE_HPP
#define MEMGRAPH_UTILS_REPLACE_HPP
#include <string>
namespace utils
{
// replaces all occurences of <match> in <src> with <replacement>
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

45
utils/string/split.hpp Normal file
View File

@ -0,0 +1,45 @@
#ifndef MEMGRAPH_UTILS_SPLIT_HPP
#define MEMGRAPH_UTILS_SPLIT_HPP
#include <vector>
#include <regex>
namespace utils
{
std::vector<std::string> split(const std::string& src,
const std::string& delimiter)
{
size_t index = 0;
std::vector<std::string> 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<std::string> 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

6
utils/sync/all.hpp Normal file
View File

@ -0,0 +1,6 @@
#ifndef MEMGRAPH_UTILS_SYNC_ALL_HPP
#define MEMGRAPH_UTILS_SYNC_ALL_HPP
#include "spinlock.hpp"
#endif

View File

@ -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

7
utils/utils.hpp Normal file
View File

@ -0,0 +1,7 @@
#ifndef MEMGRAPH_UTILS_UTILS_HPP
#define MEMGRAPH_UTILS_UTILS_HPP
#include "string/all.hpp"
#include "sync/all.hpp"
#endif