LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string_helper.h
Go to the documentation of this file.
1 #pragma once
2 #include <string>
3 #include <functional>
4 #include <regex>
5 
6 namespace lc {
7 
8  class StringHelper {
9 
10  public:
19  template<typename ... Args>
20  static std::string dim_value(std::string explicitValue, const std::string format, const Args ... args) {
21  /* assume we always have "<>" somewhere */
22  size_t pos = explicitValue.find("<>");
23  if (pos !=std::string::npos)
24  return explicitValue.replace(pos, 2, StringHelper::string_format(format, args...));
25 
26  /* "" */
27  if (explicitValue.empty())
28  return StringHelper::string_format(format, args...);
29 
30  if (explicitValue == " ")
31  return "";
32 
33  return explicitValue;
34  }
38  template<typename ... Args>
39  static std::string string_format(const std::string &format, Args ... args) {
40  size_t size = 1 + snprintf(nullptr, 0, format.c_str(), args ...);
41  std::unique_ptr<char[]> buf(new char[size]);
42  snprintf(buf.get(), size, format.c_str(), args ...);
43  return std::string(buf.get(), buf.get() + size);
44  }
45 
49  static bool isBlank(const std::string str) {
50  std::regex r("[\\s]");
51  std::string result = regex_replace(str, r, "");
52  return result.length()==0;
53  }
54 
58  struct cmpCaseInsensetive : std::binary_function<std::string, std::string, bool>
59  {
60  // case-independent (ci) compare_less binary function
61  struct nocase_compare : public std::binary_function<unsigned char,unsigned char,bool>
62  {
63  bool operator() (const unsigned char& c1, const unsigned char& c2) const {
64  return std::tolower (c1) < std::tolower (c2);
65  }
66  };
67  bool operator() (const std::string & s1, const std::string & s2) const {
68  return std::lexicographical_compare
69  (s1.begin (), s1.end (), // source range
70  s2.begin (), s2.end (), // dest range
71  nocase_compare ()); // comparison
72  }
73  };
74 
78  static std::string tolower(std::string data) {
79  std::transform(data.begin(), data.end(), data.begin(), ::tolower);
80  return data;
81  };
82  };
83 
84 
85 
86 
87 }
static std::string string_format(const std::string &format, Args...args)
Definition: string_helper.h:39
Definition: cadentity.h:12
static std::string tolower(std::string data)
Definition: string_helper.h:78
static std::string dim_value(std::string explicitValue, const std::string format, const Args...args)
Definition: string_helper.h:20
bool operator()(const std::string &s1, const std::string &s2) const
Definition: string_helper.h:67
bool operator()(const unsigned char &c1, const unsigned char &c2) const
Definition: string_helper.h:63
static bool isBlank(const std::string str)
Definition: string_helper.h:49