StringHelpers.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef MERCURYDPM_STRING_HELPERS_H
6 #define MERCURYDPM_STRING_HELPERS_H
7 
8 #include "Math/ExtendedMath.h"
9 
10 #include <string>
11 #include <memory>
12 
13 namespace helpers
14 {
19 
23  void getLineFromStringStream(std::istream& in, std::stringstream& out);
24 
30  bool isNext(std::istream& is, const std::string& name);
31 
37  bool compare(std::istream& is, const std::string& s);
38 
43  bool lookAhead(std::istream& is, const std::string& name, int number);
44 
48  std::string toString(Mdouble value, unsigned precision);
49 
50  template<typename T>
52  {
53  std::ostringstream stm;
54  stm << n;
55  return stm.str();
56  }
57 
58  template<typename T>
59  std::string toString(const std::vector<T>& vec)
60  {
61  std::ostringstream stm;
62  for (const auto val : vec) {
63  stm << val << ' ';
64  }
65  return stm.str();
66  }
67 
68  template <typename... Args>
70  {
71  int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
72  if (size_s <= 0) {
73  logger(ERROR, "snprintf() failed");
74  }
75  auto size = static_cast<size_t>(size_s);
76  std::unique_ptr<char[]> buf(new char[size]);
77  std::snprintf(buf.get(), size, format.c_str(), args...);
78  return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
79  }
80 }
81 
82 #endif // MERCURYDPM_STRING_HELPERS_H
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
double Mdouble
Definition: GeneralDefine.h:13
#define MERCURYDPM_DEPRECATED
Definition: GeneralDefine.h:16
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:32
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
RealScalar s
Definition: level1_cplx_impl.h:130
squared absolute value
Definition: GlobalFunctions.h:87
val
Definition: calibrate.py:119
args
Definition: compute_granudrum_aor.py:143
Definition: CommandLineHelpers.h:11
bool lookAhead(std::istream &is, const std::string &name, int number)
Checks if the nth argument from the current position in the input stream equals a certain string....
Definition: StringHelpers.cc:86
std::string lower(std::string s)
returns the input string after converting upper-case characters to lower case
Definition: StringHelpers.cc:11
bool isNext(std::istream &is, const std::string &name)
Checks if the next argument in the input stream equals a certain string. When true,...
Definition: StringHelpers.cc:57
void getLineFromStringStream(std::istream &in, std::stringstream &out)
Reads a line from one stringstream into another, and prepares the latter for reading in.
Definition: StringHelpers.cc:41
std::string stringFormat(const std::string &format, Args... args)
Definition: StringHelpers.h:69
std::string toString(Mdouble value, unsigned precision)
converts a floating point number into a string with a given precision
Definition: StringHelpers.cc:17
MERCURYDPM_DEPRECATED bool compare(std::istream &is, const std::string &s)
Checks if the next argument in the input stream is a certain string.
Definition: StringHelpers.cc:69
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
string name
Definition: plotDoE.py:33
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:217
std::ofstream out("Result.txt")