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) {}
45  ConvertingStringView(string_view sv) : sv_(sv) {}
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
gul14::ConvertingStringView
A string view that can automatically convert numbers into strings.
Definition: cat.h:41
gul14::basic_string_view< char >
gul14::ConvertingStringView::size
std::size_t size() const noexcept
Return the length of the string.
Definition: cat.h:68
gul14::ConvertingStringView::data
const char * data() const noexcept
Return a pointer to the internal character buffer.
Definition: cat.h:62
gul14::cat
std::string cat()
Efficiently concatenate an arbitrary number of strings and numbers.
Definition: cat.h:90
cat.h
Declaration of the overload set for cat() and of the associated class ConvertingStringView.
string_view.h
Provides a gul14::string_view that is fully compatible with C++17's std::string_view.
internal.h
Definition of macros used internally by GUL.
gul14
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
gul14::ConvertingStringView::length
std::size_t length() const noexcept
Return the length of the string.
Definition: cat.h:65