General Utility Library for C++14  2.7
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 #include "gul14/internal.h"
30 #include "gul14/string_view.h"
31 
32 namespace gul14 {
33 
42 {
43 public:
44  ConvertingStringView(const std::string &str) : sv_(str) {}
46  ConvertingStringView(const char *str) : sv_(str) {}
47 
48  ConvertingStringView(char c) : str_(1, c), sv_(str_) {}
49  ConvertingStringView(int a) : str_(std::to_string(a)), sv_(str_) {}
50  ConvertingStringView(unsigned int a) : str_(std::to_string(a)), sv_(str_) {}
51  ConvertingStringView(long a) : str_(std::to_string(a)), sv_(str_) {}
52  ConvertingStringView(unsigned long a) : str_(std::to_string(a)), sv_(str_) {}
53  ConvertingStringView(long long a) : str_(std::to_string(a)), sv_(str_) {}
54  ConvertingStringView(unsigned long long a) : str_(std::to_string(a)), sv_(str_) {}
55  ConvertingStringView(float a) : str_(std::to_string(a)), sv_(str_) {}
56  ConvertingStringView(double a) : str_(std::to_string(a)), sv_(str_) {}
57 
62  const char *data() const noexcept { return sv_.data(); }
63 
65  std::size_t length() const noexcept { return sv_.length(); }
66 
68  std::size_t size() const noexcept { return sv_.size(); }
69 
71  explicit operator std::string() const { return std::string(sv_); };
72 
73 private:
74  std::string str_;
75  string_view sv_;
76 };
77 
90 inline std::string cat() { return std::string(); }
91 
93 inline std::string cat(const ConvertingStringView& s) { return std::string(s); }
94 
96 GUL_EXPORT
97 std::string cat(const ConvertingStringView& s1, const ConvertingStringView& s2);
98 
100 GUL_EXPORT
101 std::string cat(const ConvertingStringView& s1, const ConvertingStringView& s2,
102  const ConvertingStringView& s3);
103 
105 GUL_EXPORT
106 std::string cat(std::initializer_list<ConvertingStringView> pieces);
107 
109 template <typename... Args, typename = std::enable_if_t<(sizeof...(Args) > 3)>>
110 inline std::string cat(const Args&... args)
111 {
112  return cat({ args... }); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay): Impossible to remove that warning
113 }
114 
115 } // namespace gul14
116 
117 #endif
A string view that can automatically convert numbers into strings.
Definition: cat.h:42
const char * data() const noexcept
Return a pointer to the internal character buffer.
Definition: cat.h:62
ConvertingStringView(int a)
Construct a ConvertingStringView from an integer.
Definition: cat.h:49
ConvertingStringView(long a)
Construct a ConvertingStringView from a long integer.
Definition: cat.h:51
ConvertingStringView(unsigned long long a)
Construct a ConvertingStringView from an unsigned long long integer.
Definition: cat.h:54
std::size_t size() const noexcept
Return the length of the string.
Definition: cat.h:68
ConvertingStringView(string_view sv)
Construct a ConvertingStringView from a string_view.
Definition: cat.h:45
ConvertingStringView(const std::string &str)
Construct a ConvertingStringView from a std::string.
Definition: cat.h:44
ConvertingStringView(long long a)
Construct a ConvertingStringView from a long long integer.
Definition: cat.h:53
ConvertingStringView(unsigned int a)
Construct a ConvertingStringView from an unsigned integer.
Definition: cat.h:50
ConvertingStringView(unsigned long a)
Construct a ConvertingStringView from an unsigned long integer.
Definition: cat.h:52
ConvertingStringView(double a)
Construct a ConvertingStringView from a double.
Definition: cat.h:56
ConvertingStringView(char c)
Construct a ConvertingStringView from a character.
Definition: cat.h:48
std::size_t length() const noexcept
Return the length of the string.
Definition: cat.h:65
ConvertingStringView(float a)
Construct a ConvertingStringView from a float.
Definition: cat.h:55
ConvertingStringView(const char *str)
Construct a ConvertingStringView from a const char *.
Definition: cat.h:46
Definition of macros used internally by GUL.
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
std::string cat()
Efficiently concatenate an arbitrary number of strings and numbers.
Definition: cat.h:90
Provides a gul14::string_view that is fully compatible with C++17's std::string_view.