SUNphi  1.0
String.hpp
Go to the documentation of this file.
1 #ifndef _STRING_HPP
2 #define _STRING_HPP
3 
4 /// \file String.hpp
5 ///
6 /// \brief Header file to support string operations
7 
8 #include <cstring>
9 #include <string>
10 
11 namespace SUNphi
12 {
13  /// Provides a string as member of a struct
14  ///
15  /// The list of chars is provided as template argument
16  template <char...Chars>
18  {
19  /// List of chars passed as a template in a char array
20  const char str[sizeof...(Chars)]{Chars...};
21  };
22 
23  /// Provides a static \c name() method to a class, returning STR
24 #define PROVIDE_NAME(STR)
25  /*! Returns the name of the type */
26  static constexpr const char* name()
27  {
28  return
29  STR;
30  }
31 
32  /// Returns the c-string of the passed quantity
33  ///
34  /// Case in which the input is already a c-string
35  inline const char* cString(const char* in) ///< Input
36  {
37  return
38  in;
39  }
40 
41  /// Returns the c-string of the passed quantity
42  ///
43  /// Case in which the input is a c++ string
44  inline const char* cString(const std::string& in) ///< Input
45  {
46  return
47  in.c_str();
48  }
49 
50  /// Returns a substring out of the passed chars, among position beg and end
51  inline std::string substrBetweenPos(const std::string& str, ///< Substring to split
52  const size_t& beg, ///< Beginning position
53  const size_t& end) ///< End position
54  {
55  /// Length to return
56  const size_t len=
57  end-beg;
58 
59  return
60  (end>beg and end!=std::string::npos and beg!=std::string::npos)?
61  str.substr(beg,len):
62  "";
63  }
64 
65  /// Length of a char
66  constexpr size_t __strLength(char)
67  {
68  return
69  1;
70  }
71 
72  /// Length of a std::string
73  inline size_t __strLength(const std::string& str)
74  {
75  return
76  str.length();
77  }
78 
79  /// Length of a c-string
80  inline size_t __strLength(const char* str)
81  {
82  return
83  strlen(str);
84  }
85 
86  /// Returns a substring out of the passed delimiters
87  template <typename T>
88  inline std::string substrBetween(const std::string& str, ///< Substring to split
89  const T& i, ///< Beginning token
90  const T& f) ///< End token
91  {
92  /// Position of beginning token
93  const size_t iPos=
94  str.find(i);
95 
96  /// Beginning of the substring
97  const size_t beg=
98  (iPos==std::string::npos)?
99  iPos:
100  iPos+__strLength(i);
101 
102  /// End of the substring
103  const size_t end=
104  str.find(f);
105 
106  return
107  substrBetweenPos(str,beg,end);
108  }
109 }
110 
111 #endif
const char str[sizeof...(Chars)]
List of chars passed as a template in a char array.
Definition: String.hpp:20
const char * cString(const std::string &in)
Definition: String.hpp:44
const char * cString(const char *in)
Definition: String.hpp:35
std::string substrBetweenPos(const std::string &str, const size_t &beg, const size_t &end)
Returns a substring out of the passed chars, among position beg and end.
Definition: String.hpp:51