Add some string utils; rework Benchmark
This commit is contained in:
@@ -22,6 +22,7 @@ const char ABACUS_VERSION[20] = "ABACUS_0a";
|
||||
// Standard includes
|
||||
#include <cmath>
|
||||
#include <complex> // for complex number algebra
|
||||
#include <algorithm> // for count
|
||||
#include <string>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@@ -37,8 +37,38 @@ const DP MACHINE_EPS_SQ = pow(MACHINE_EPS, 2.0);
|
||||
|
||||
namespace ABACUS {
|
||||
|
||||
// Inexplicably missing string functions in standard library:
|
||||
|
||||
std::string replace(const std::string& str, const std::string& from, const std::string& to) {
|
||||
std::string repl = str;
|
||||
size_t start_pos = repl.find(from);
|
||||
if(start_pos < std::string::npos)
|
||||
repl.replace(start_pos, from.length(), to);
|
||||
return repl;
|
||||
}
|
||||
|
||||
std::string replace_all(const std::string& str, const std::string& from, const std::string& to) {
|
||||
std::string repl = str;
|
||||
if(from.empty())
|
||||
return repl;
|
||||
size_t start_pos = 0;
|
||||
while((start_pos = repl.find(from, start_pos)) != std::string::npos) {
|
||||
repl.replace(start_pos, from.length(), to);
|
||||
start_pos += to.length();
|
||||
}
|
||||
return repl;
|
||||
}
|
||||
|
||||
|
||||
// File checks:
|
||||
|
||||
inline unsigned int count_lines(std::string filename)
|
||||
{
|
||||
std::ifstream infile(filename);
|
||||
return(std::count(std::istreambuf_iterator<char>(infile),
|
||||
std::istreambuf_iterator<char>(), '\n'));
|
||||
}
|
||||
|
||||
inline bool file_exists (const char* filename)
|
||||
{
|
||||
std::fstream file;
|
||||
|
||||
Reference in New Issue
Block a user