General Utility Library for C++14  2.12
cat.h
Go to the documentation of this file.
1 
24 #ifndef GUL14_CAT_H_
25 #define GUL14_CAT_H_
26 
27 #include <string>
28 #include <type_traits>
29 
30 #include "gul14/internal.h"
31 #include "gul14/string_view.h"
32 
33 namespace gul14 {
34 
49 {
50 public:
51  ConvertingStringView(const std::string &str) : sv_(str) {}
53  ConvertingStringView(const char *str) : sv_(str) {}
54 
55  ConvertingStringView(char c) : str_(1, c), sv_(str_) {}
56  ConvertingStringView(int a) : str_(std::to_string(a)), sv_(str_) {}
57  ConvertingStringView(unsigned int a) : str_(std::to_string(a)), sv_(str_) {}
58  ConvertingStringView(long a) : str_(std::to_string(a)), sv_(str_) {}
59  ConvertingStringView(unsigned long a) : str_(std::to_string(a)), sv_(str_) {}
60  ConvertingStringView(long long a) : str_(std::to_string(a)), sv_(str_) {}
61  ConvertingStringView(unsigned long long a) : str_(std::to_string(a)), sv_(str_) {}
62  ConvertingStringView(float a) : str_(std::to_string(a)), sv_(str_) {}
63  ConvertingStringView(double a) : str_(std::to_string(a)), sv_(str_) {}
64 
69  const char *data() const noexcept { return sv_.data(); }
70 
72  std::size_t length() const noexcept { return sv_.length(); }
73 
75  std::size_t size() const noexcept { return sv_.size(); }
76 
78  explicit operator std::string() const { return std::string(sv_); };
79 
80 private:
81  std::string str_;
82  string_view sv_;
83 };
84 
97 inline std::string cat() { return std::string(); }
98 
100 inline std::string cat(const ConvertingStringView& s) { return std::string(s); }
101 
103 GUL_EXPORT
104 std::string cat(const ConvertingStringView& s1, const ConvertingStringView& s2);
105 
107 GUL_EXPORT
108 std::string cat(const ConvertingStringView& s1, const ConvertingStringView& s2,
109  const ConvertingStringView& s3);
110 
112 GUL_EXPORT
113 std::string cat(std::initializer_list<ConvertingStringView> pieces);
114 
116 template <typename... Args, typename = std::enable_if_t<(sizeof...(Args) > 3)>>
117 inline std::string cat(const Args&... args)
118 {
119  return cat({ args... }); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay): Impossible to remove that warning
120 }
121 
123 
124 } // namespace gul14
125 
126 #endif
A string view that can automatically convert numbers into strings.
Definition: cat.h:49
const char * data() const noexcept
Return a pointer to the internal character buffer.
Definition: cat.h:69
ConvertingStringView(int a)
Construct a ConvertingStringView from an integer.
Definition: cat.h:56
ConvertingStringView(long a)
Construct a ConvertingStringView from a long integer.
Definition: cat.h:58
ConvertingStringView(unsigned long long a)
Construct a ConvertingStringView from an unsigned long long integer.
Definition: cat.h:61
std::size_t size() const noexcept
Return the length of the string.
Definition: cat.h:75
ConvertingStringView(string_view sv)
Construct a ConvertingStringView from a string_view.
Definition: cat.h:52
ConvertingStringView(const std::string &str)
Construct a ConvertingStringView from a std::string.
Definition: cat.h:51
ConvertingStringView(long long a)
Construct a ConvertingStringView from a long long integer.
Definition: cat.h:60
ConvertingStringView(unsigned int a)
Construct a ConvertingStringView from an unsigned integer.
Definition: cat.h:57
ConvertingStringView(unsigned long a)
Construct a ConvertingStringView from an unsigned long integer.
Definition: cat.h:59
ConvertingStringView(double a)
Construct a ConvertingStringView from a double.
Definition: cat.h:63
ConvertingStringView(char c)
Construct a ConvertingStringView from a character.
Definition: cat.h:55
std::size_t length() const noexcept
Return the length of the string.
Definition: cat.h:72
ConvertingStringView(float a)
Construct a ConvertingStringView from a float.
Definition: cat.h:62
ConvertingStringView(const char *str)
Construct a ConvertingStringView from a const char *.
Definition: cat.h:53
std::string cat()
Efficiently concatenate an arbitrary number of strings and numbers.
Definition: cat.h:97
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
Provides a gul14::string_view that is fully compatible with C++17's std::string_view.