tinympl  0.2
mini MPL library for C++11
string.hpp
1 // Copyright (C) 2013, Ennio Barbaro.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://sbabbi.github.io/tinympl for documentation.
8 //
9 // You are welcome to contact the author at:
10 // enniobarbaro@gmail.com
11 //
12 
13 #ifndef TINYMPL_STRING_HPP
14 #define TINYMPL_STRING_HPP
15 
16 #include <tinympl/algorithm.hpp>
17 #include <tinympl/sequence.hpp>
18 
19 namespace tinympl
20 {
21 
38 template<class T,T ... chars>
40 {
41  enum
42  {
43  size = sizeof ... (chars)
44  };
45 
46  enum
47  {
48  empty = (size == 0)
49  };
50 
51  typedef basic_string<T,chars...> type;
52  typedef T value_type;
53  typedef const T * const_pointer;
54 
56 
59  constexpr static value_type at(std::size_t i) {return c_str()[i];}
60 
62  constexpr static const_pointer c_str() {return v_;}
63 
65  constexpr static value_type front() {return at(0);}
66 
68  constexpr static value_type back() {return at(size - 1);}
69 
70 private:
71  template<T t>
72  using wrap = std::integral_constant<T,t>;
73 
74  template<class ... Us>
75  using unwrap = basic_string<T, Us::value ... >;
76 
77 public:
78 
80 
83  template<std::size_t pos,class Str>
84  using insert = tinympl::insert<pos,
85  Str,
86  basic_string<T,chars...> >;
87 
89 
92  template<std::size_t pos,T ... NewChars>
93  using insert_c = insert<pos, basic_string<T,NewChars...> >;
94 
96 
100  template<std::size_t pos,std::size_t count>
101  using erase = tinympl::erase<pos,pos + count,
102  basic_string<T,chars...> >;
103 
105 
108  template<class Str>
110 
112 
115  template<T ... NewChars>
116  using append_c = insert_c<size,NewChars...>;
117 
119  template<std::size_t pos, std::size_t count>
120  class substr
121  {
122  static_assert( pos <= size, "substr pos out of range ");
123  static_assert( pos + count <= size, "substr count out of range");
124 
125  public:
126  typedef typename tinympl::erase<0,pos,
128  };
129 
131 
134  template<std::size_t pos, std::size_t count, class Str>
135  class replace
136  {
137  static_assert( pos <= size, "substr pos out of range ");
138  static_assert( pos + count <= size, "substr count out of range");
139 
140  typedef typename erase<pos,count>::type str_cleared;
141 
142  public:
143  typedef typename str_cleared::template insert<pos,Str>::type type;
144  };
145 
147 
150  template<std::size_t pos, std::size_t count, T ... ts>
151  using replace_c = replace<pos,count, basic_string<T,ts...> >;
152 
154 
157  template<class OtherStr>
159  basic_string<T,chars...>,
160  OtherStr>;
161 
162 private:
163  template<std::size_t i,std::size_t count>
164  using unguarded_substr = substr<i, (i+count <= size ? count : size - i)>;
165 
166  template<class Str, int i, std::size_t s> struct find_impl :
167  std::conditional<
168  unguarded_substr<i,Str::size>::type::template
169  compare<Str>::value == 0,
170  std::integral_constant<int, i>,
171  find_impl<Str,i+1,s> >::type {};
172 
173  template<class Str, int s> struct find_impl<Str, s, s> : std::integral_constant<std::size_t, s> {};
174 
175  template<class Str,int i> struct rfind_impl :
176  std::conditional<
177  unguarded_substr<i,Str::size>::type::template
178  compare<Str>::value == 0,
179  std::integral_constant<std::size_t, i>,
180  rfind_impl<Str,i-1> >::type {};
181 
182  template<class Str, std::size_t s> struct find_impl< Str, -1, s> : std::integral_constant< std::size_t, s> {};
183 
184 public:
185 
187 
191  template<class Str>
192  using find = find_impl<Str,0,size>;
193 
195  template<class Str>
196  using rfind = rfind_impl<Str,size>;
197 
199  template<T ... ts> using find_c = find< basic_string<T,ts...> >;
200 
202  template<T ... ts> using rfind_c = rfind< basic_string<T,ts...> >;
203 
204 private:
205  static constexpr T v_[size + 1] = {chars ... , 0};
206 };
207 
215 template<class T,T ... chars>
216 constexpr T basic_string<T,chars...>::v_ [ size + 1];
217 
222 template<class T, const T * ptr>
224 {
225  template<T ... ts> struct extract
226  {
227  typedef typename std::conditional<
228  ptr[ sizeof ... (ts) ] == 0,
230  extract<ts..., ptr[sizeof ... (ts) ]>
231  >::type::type type;
232  };
233 public:
234  using type = typename extract<>::type;
235 };
236 
238 
242 template<const char * p>
243 using string = typename make_basic_string<char,p>::type;
244 
251 template<class T,T ... ts> struct as_sequence<basic_string<T,ts...> >
252 {
253 private:
254  template<class ... Args>
255  struct do_rebind
256  {
257  template<class U>
258  using check_t = std::is_same<typename U::value_type,T>;
259 
261  "basic_string: unable to rebind when the arguments are not of the same value_type" );
262 
263  typedef basic_string<T, Args::value ...> type;
264  };
265 
266 public:
268  template<class ... Ts> using rebind = typename do_rebind<Ts...>::type;
269 };
270 
271 }
272 
273 #endif // TINYMPL_STRING_HPP
Determine whether the vector is empty.
Definition: string.hpp:48
Return a new string constructed by replacing count characters starting at pos with the string Str...
Definition: string.hpp:135
Provide a customization points by allowing the user to specialize this class.
Definition: as_sequence.hpp:30
Determines whether every element in the sequence satisfies the given predicate.
Definition: all_of.hpp:31
Return a substring long count starting at position pos.
Definition: string.hpp:120
Construct a basic_string from a constexpr pointer to a null-terminated string.
Definition: string.hpp:223
find_impl< Str, 0, size > find
Return the index of the first character of the first occurrence of the substring Str, or size if Str is not a substring of this string.
Definition: string.hpp:192
basic_string< T, chars...> type
This type.
Definition: string.hpp:51
A vector of values of type T.
Definition: string.hpp:39
Counts the number of elements in a sequence equal to a given one.
Definition: count.hpp:35
Compares two sequences using the given comparator.
Definition: lexicographical_compare.hpp:37
Get the number of elements of a sequence.
Definition: size.hpp:27
static constexpr value_type back()
Return a copy of the last character.
Definition: string.hpp:68
static constexpr value_type front()
Return a copy of the first character.
Definition: string.hpp:65
static constexpr value_type at(std::size_t i)
Get the i-th character.
Definition: string.hpp:59
rfind< basic_string< T, ts...> > rfind_c
Return the index of the first character of the last occurrence of the substring ts..., or size if ts... is not a substring of this string.
Definition: string.hpp:202
The main sequence type.
Definition: sequence.hpp:28
Insert a subsequence into a given sequence at a given position.
Definition: insert.hpp:36
find< basic_string< T, ts...> > find_c
Return the index of the first character of the first occurrence of the substring ts..., or size if ts... is not a substring of this string.
Definition: string.hpp:199
The size of the vector.
Definition: string.hpp:43
static constexpr const_pointer c_str()
Return a pointer to a null-terminated C-style string.
Definition: string.hpp:62
rfind_impl< Str, size > rfind
Return the index of the first character of the last occurrence of the substring Str, or size if Str is not a substring of this string.
Definition: string.hpp:196
Remove a range in a given sequence.
Definition: erase.hpp:35
T value_type
The type of the string characters.
Definition: string.hpp:52