Add some string utils; rework Benchmark

This commit is contained in:
Jean-Sébastien
2021-12-07 19:02:35 +01:00
parent d922a0dae1
commit 7207370426
4 changed files with 42 additions and 28 deletions
+1
View File
@@ -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>
+30
View File
@@ -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;