Loading...
Searching...
No Matches
string_literal.h
Go to the documentation of this file.
1#ifndef _BBM_STRING_LITERAL_H_
2#define _BBM_STRING_LITERAL_H_
3
4#include <ostream>
5#include <algorithm>
6
7/***********************************************************************/
8/*! \file string_litral.h
9 \brief Minimal implementaion for string literals
10************************************************************************/
11
12namespace bbm {
13
14 template<size_t N=1>
16 {
17 //! \brief Empty string
18 inline constexpr string_literal(void)
19 {
20 value[0] = '\0';
21 }
22
23 //! \brief constructor
24 inline constexpr string_literal(const char (&str)[N])
25 {
26 std::copy_n(str, N, value);
27 }
28
29 //! \brief Number of chars in the string_literal
30 static constexpr size_t size = N;
31
32 //! \brief true is empty (i.e., == '\0')
33 static constexpr bool empty = (N==1);
34
35 //! @{ \name iterator
36 inline constexpr const char* begin(void) const { return value; }
37 inline constexpr const char* end(void) const { return value + N - 1; }
38 //! @}
39
40 //! @{ \name compare two literals (constexpr)
41 template<size_t M>
42 inline constexpr bool operator==(string_literal<M> str) const
43 {
44 return (M == N) && std::equal(begin(), end(), str.value);
45 }
46
47 template<size_t M>
48 inline constexpr bool operator==(const char (&str)[M]) const
49 {
50 return operator==(string_literal<M>(str));
51 }
52
53 template<size_t M>
54 friend inline constexpr bool operator==(const char (&str)[M], string_literal<N> lit)
55 {
56 return (lit == string_literal<M>(str));
57 }
58 //! @}
59
60 //! @{ \name less operator (constexpr)
61 template<size_t M>
62 inline constexpr bool operator<(string_literal<M> str) const
63 {
64 return std::lexicographical_compare(begin(), end(), str.begin(), str.end());
65 }
66
67 template<size_t M>
68 inline constexpr bool operator<(const char (&str)[M]) const
69 {
70 return operator<(string_literal<M>(str));
71 }
72
73 template<size_t M>
74 friend inline constexpr bool operator<(const char (&str)[M], string_literal<N> lit)
75 {
76 return (lit < string_literal<M>(str));
77 }
78 //! @}
79
80 //! @{ \name concat two literals (constexpr)
81 template<size_t M>
82 inline constexpr string_literal<N+M-1> operator+(string_literal<M> str) const
83 {
84 char temp[N+M-1];
85 std::copy_n(value, N-1, temp);
86 std::copy_n(str.value, M, temp + N -1);
87 return string_literal<N+M-1>(temp);
88 }
89
90 template<size_t M>
91 inline constexpr auto operator+(const char (&str)[M]) const
92 {
93 return operator+(string_literal<M>(str));
94 }
95
96 template<size_t M>
97 friend inline constexpr auto operator+(const char (&str)[M], string_literal<N> lit)
98 {
99 return string_literal<M>(str) + lit;
100 }
101 //! @}
102
103 //! \brief get substring
104 template<size_t S, size_t LEN=N-1> requires (S+LEN < N)
105 inline constexpr auto substr(void) const
106 {
107 char temp[LEN+1];
108 std::copy_n(begin()+S, LEN, temp);
109 temp[LEN] = '\0';
110 return string_literal<LEN+1>(temp);
111 }
112
113 //! \brief cast to C string
114 inline constexpr operator const char*(void) const { return value; }
115
116 //! \brief forward 'value' to stream
117 friend std::ostream& operator<<(std::ostream& s, const string_literal<N>& str)
118 {
119 s << str.value;
120 return s;
121 }
122
123 char value[N];
124 };
125
126 /**********************************************************************/
127 /*! \brief custom literal operator for string_literals
128 **********************************************************************/
129 template<string_literal LIT>
130 constexpr decltype(LIT) operator""_sl(void)
131 {
132 return LIT;
133 }
134
135
136 /*** Implementation details for is_string_literal_impl ***/
137 namespace detail {
138
139 template<typename T> struct is_string_literal_impl : std::false_type {};
140 template<size_t N> struct is_string_literal_impl<string_literal<N>> : std::true_type {};
141
142 } // end detail namespace
143
144 /**********************************************************************/
145 /*! @{ \name is_string_literal type trait
146 **********************************************************************/
147 template<typename T>
148 using is_string_literal = bbm::detail::is_string_literal_impl<std::decay_t<T>>;
149
150 template<typename T>
152
153} // end bbm namespace
154
155#endif /* _BBM_STRING_LITERAL_H_ */
Definition: aggregatebsdf.h:29
bbm::detail::is_string_literal_impl< std::decay_t< T > > is_string_literal
Definition: string_literal.h:148
constexpr bool is_string_literal_v
Definition: string_literal.h:151
Definition: string_literal.h:16
constexpr auto operator+(const char(&str)[M]) const
Definition: string_literal.h:91
constexpr bool operator<(string_literal< M > str) const
Definition: string_literal.h:62
static constexpr bool empty
true is empty (i.e., == '\0')
Definition: string_literal.h:33
friend std::ostream & operator<<(std::ostream &s, const string_literal< N > &str)
forward 'value' to stream
Definition: string_literal.h:117
constexpr bool operator<(const char(&str)[M]) const
Definition: string_literal.h:68
constexpr string_literal< N+M-1 > operator+(string_literal< M > str) const
Definition: string_literal.h:82
friend constexpr bool operator==(const char(&str)[M], string_literal< N > lit)
Definition: string_literal.h:54
friend constexpr bool operator<(const char(&str)[M], string_literal< N > lit)
Definition: string_literal.h:74
friend constexpr auto operator+(const char(&str)[M], string_literal< N > lit)
Definition: string_literal.h:97
constexpr auto substr(void) const
get substring
Definition: string_literal.h:105
char value[N]
Definition: string_literal.h:123
constexpr bool operator==(const char(&str)[M]) const
Definition: string_literal.h:48
constexpr string_literal(const char(&str)[N])
constructor
Definition: string_literal.h:24
constexpr string_literal(void)
Empty string.
Definition: string_literal.h:18
static constexpr size_t size
Number of chars in the string_literal.
Definition: string_literal.h:30
constexpr const char * end(void) const
Definition: string_literal.h:37
constexpr bool operator==(string_literal< M > str) const
Definition: string_literal.h:42
constexpr const char * begin(void) const
Definition: string_literal.h:36