CoolProp  6.6.0
An open-source fluid property and humid air property database
CPstrings.cpp
Go to the documentation of this file.
1 #include "CPstrings.h"
3 #include <cstdio>
4 #include <vector>
5 #include <string>
6 
7 std::string strjoin(const std::vector<std::string>& strings, const std::string& delim) {
8  // Empty input vector
9  if (strings.empty()) {
10  return "";
11  }
12 
13  std::string output = strings[0];
14  for (unsigned int i = 1; i < strings.size(); i++) {
15  output += format("%s%s", delim.c_str(), strings[i].c_str());
16  }
17  return output;
18 }
19 
20 std::vector<std::string> strsplit(const std::string& s, char del) {
21  std::vector<std::string> v;
22  std::string::const_iterator i1 = s.begin(), i2;
23  while (true) {
24  i2 = std::find(i1, s.end(), del);
25  v.push_back(std::string(i1, i2));
26  if (i2 == s.end()) break;
27  i1 = i2 + 1;
28  }
29  return v;
30 }
31 
32 #if defined(NO_FMTLIB)
33 std::string format(const char* fmt, ...) {
34  const int size = 512;
35  struct deleter
36  {
37  static void delarray(char* p) {
38  delete[] p;
39  }
40  }; // to use delete[]
41  shared_ptr<char> buffer(new char[size], deleter::delarray); // I'd prefer unique_ptr, but it's only available since c++11
42  va_list vl;
43  va_start(vl, fmt);
44  int nsize = vsnprintf(buffer.get(), size, fmt, vl);
45  if (size <= nsize) { //fail delete buffer and try again
46  buffer.reset(new char[++nsize], deleter::delarray); //+1 for /0
47  nsize = vsnprintf(buffer.get(), nsize, fmt, vl);
48  }
49  va_end(vl);
50  return buffer.get();
51 }
52 #endif
53 
54 #if defined(ENABLE_CATCH)
55 
57 # include <catch2/catch_all.hpp>
58 # include "CoolPropTools.h"
59 # include "CoolProp.h"
60 
61 TEST_CASE("Test endswith function", "[endswith]") {
62  REQUIRE(endswith("aaa", "-PengRobinson") == false);
63  REQUIRE(endswith("Ethylbenzene", "-PengRobinson") == false);
64  REQUIRE(endswith("Ethylbenzene-PengRobinson", "-PengRobinson") == true);
65  REQUIRE(endswith("Ethylbenzene", "Ethylbenzene") == true);
66 }
67 
68 #endif