ROOT  6.06/09
Reference Guide
RWrap_libcpp_string_view.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // Author: Philippe Canal, March 2015
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //===---------------------wrap string_view ----------------------------===//
13 // Wrapper header adapting the snapshot of sring_view.h to build without
14 // the libc++ infrastructure header files.
15 
16 #ifndef RWrap_libcpp_string_view_h
17 #define RWrap_libcpp_string_view_h
18 
19 // In case we are connected with a libc++ which defines those, we need
20 // to include it first, so we avoid being silently over-ridden.
21 
22 // To import a new version of the original file do:
23 //
24 /*
25  svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
26 
27  cat original/string_view | \
28  sed -e 's:_LIBCPP_BEGIN_NAMESPACE_LFTS:_ROOT_LIBCPP_BEGIN_NAMESPACE_LFTS:' \
29  -e 's:_LIBCPP_END_NAMESPACE_LFTS:_ROOT_LIBCPP_END_NAMESPACE_LFTS:' \
30  -e 's:#include <__debug>://#include <__debug>:' \
31  -e 's:#include <experimental/__config>://#include <experimental/__config>:' \
32  -e 's:__put_character_sequence:R__put_character_sequence:g' \
33  > core/base/inc/libcpp_string_view.h
34 */
35 
36 #include <string>
37 #include <algorithm>
38 #include <iterator>
39 #include <ostream>
40 #include <iomanip>
41 #include <stdexcept>
42 
43 #ifndef R__WIN32
44 
45 #define _ROOT_LIBCPP_BEGIN_NAMESPACE_LFTS \
46 namespace std { \
47 namespace experimental { inline namespace __ROOT {
48 #define _ROOT_LIBCPP_END_NAMESPACE_LFTS } } }
49 #else
50 
51 // Microsoft compiler does not support inline namespace yet.
52 #define _ROOT_LIBCPP_BEGIN_NAMESPACE_LFTS \
53 namespace std { \
54 namespace experimental { namespace __ROOT {
55 #define _ROOT_LIBCPP_END_NAMESPACE_LFTS } using namespace __ROOT; } }
56 
57 #endif
58 
59 
60 #ifndef _LIBCPP_BEGIN_NAMESPACE_STD
61 #define _LOCAL_LIBCPP_BEGIN_NAMESPACE_STD
62 #define _LIBCPP_BEGIN_NAMESPACE_STD namespace std {
63 #define _LIBCPP_END_NAMESPACE_STD }
64 #endif
65 
66 #ifndef _LIBCPP_CONSTEXPR
67 #define _LOCAL_LIBCPP_CONSTEXPR
68 #define _LIBCPP_CONSTEXPR constexpr
69 #endif
70 
71 #ifndef _VSTD
72 #define _LOCAL_VSTD
73 #define _VSTD ::std
74 #endif
75 
76 #ifndef _LIBCPP_INLINE_VISIBILITY
77 #define _LOCAL_LIBCPP_INLINE_VISIBILITY
78 #define _LIBCPP_INLINE_VISIBILITY inline
79 #endif
80 
81 #ifndef _LIBCPP_EXPLICIT
82 #define _LOCAL_LIBCPP_EXPLICIT
83 #define _LIBCPP_EXPLICIT explicit
84 #endif
85 
86 //#ifndef _LIBCPP_CONSTEXPR_AFTER_CXX11
87 //#define _LOCAL_LIBCPP_CONSTEXPR_AFTER_CXX11
88 //#define _LIBCPP_CONSTEXPR_AFTER_CXX11 constexpr
89 //#endif
90 
91 #ifdef _LIBCPP_STD_VER
92 #define _LOCAL_LIBCPP_STD_VER
93 #define _LIBCPP_STD_VER 11
94 #endif
95 
96 #ifndef _LIBCPP_TYPE_VIS_ONLY
97 #define _LOCAL_LIBCPP_TYPE_VIS_ONLY
98 #define _LIBCPP_TYPE_VIS_ONLY
99 #endif
100 
101 #ifndef _LIBCPP_CONSTEXPR_AFTER_CXX11
102 #define _LOCAL_LIBCPP_CONSTEXPR_AFTER_CXX11
103 #define _LIBCPP_CONSTEXPR_AFTER_CXX11
104 #endif
105 
106 #ifndef _NOEXCEPT
107 #define _LOCAL_NOEXCEPT
108 #define _NOEXCEPT
109 #endif
110 
111 #ifndef _LIBCPP_ASSERT
112 #define _LOCAL_LIBCPP_ASSERT
113 #define _LIBCPP_ASSERT(X,Y) ((void)0)
114 #endif
115 
116 /* Also used:
117  _LIBCPP_TYPE_VIS_ONLY
118  _LIBCPP_CONSTEXPR_AFTER_CXX11
119  */
120 
121 namespace std {
122 #ifdef _LOCAL_VSTD
123 inline namespace __ROOT {
124 #else
125 // libC++ wins.
126 inline namespace __1 {
127 #endif
128 
129  // template <class _Traits>
130  // struct _LIBCPP_HIDDEN __traits_eq
131  // {
132  // typedef typename _Traits::char_type char_type;
133  // _LIBCPP_INLINE_VISIBILITY
134  // bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
135  // {return _Traits::eq(__x, __y);}
136  // };
137 
138  template<class _CharT, class _Traits>
139  basic_ostream<_CharT, _Traits>&
140  R__put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
141  const _CharT* __str, size_t __len)
142  {
143 #if 0
144 //#ifndef _LIBCPP_NO_EXCEPTIONS
145  try
146  {
147 #endif // _LIBCPP_NO_EXCEPTIONS
148  typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
149  if (__s)
150  {
151  typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
152  if (__pad_and_output(_Ip(__os),
153  __str,
154  (__os.flags() & ios_base::adjustfield) == ios_base::left ?
155  __str + __len :
156  __str,
157  __str + __len,
158  __os,
159  __os.fill()).failed())
160  __os.setstate(ios_base::badbit | ios_base::failbit);
161  }
162 #if 0
163 //#ifndef _LIBCPP_NO_EXCEPTIONS
164  }
165  catch (...)
166  {
167  __os.__set_badbit_and_consider_rethrow();
168  }
169 #endif // _LIBCPP_NO_EXCEPTIONS
170  return __os;
171  }
172 
173 #ifdef _LOCAL_VSTD
174  // search
175 
176  template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
177  _ForwardIterator1
178  __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
179  _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
180  forward_iterator_tag, forward_iterator_tag)
181  {
182  if (__first2 == __last2)
183  return __first1; // Everything matches an empty sequence
184  while (true)
185  {
186  // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
187  while (true)
188  {
189  if (__first1 == __last1) // return __last1 if no element matches *__first2
190  return __last1;
191  if (__pred(*__first1, *__first2))
192  break;
193  ++__first1;
194  }
195  // *__first1 matches *__first2, now match elements after here
196  _ForwardIterator1 __m1 = __first1;
197  _ForwardIterator2 __m2 = __first2;
198  while (true)
199  {
200  if (++__m2 == __last2) // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
201  return __first1;
202  if (++__m1 == __last1) // Otherwise if source exhaused, pattern not found
203  return __last1;
204  if (!__pred(*__m1, *__m2)) // if there is a mismatch, restart with a new __first1
205  {
206  ++__first1;
207  break;
208  } // else there is a match, check next elements
209  }
210  }
211  }
212 
213  template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
214  _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1
215  __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
216  _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
217  random_access_iterator_tag, random_access_iterator_tag)
218  {
219  typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type _D1;
220  typedef typename std::iterator_traits<_RandomAccessIterator2>::difference_type _D2;
221  // Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
222  _D2 __len2 = __last2 - __first2;
223  if (__len2 == 0)
224  return __first1;
225  _D1 __len1 = __last1 - __first1;
226  if (__len1 < __len2)
227  return __last1;
228  const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
229  while (true)
230  {
231 #if !_LIBCPP_UNROLL_LOOPS
232  while (true)
233  {
234  if (__first1 == __s)
235  return __last1;
236  if (__pred(*__first1, *__first2))
237  break;
238  ++__first1;
239  }
240 #else // !_LIBCPP_UNROLL_LOOPS
241  for (_D1 __loop_unroll = (__s - __first1) / 4; __loop_unroll > 0; --__loop_unroll)
242  {
243  if (__pred(*__first1, *__first2))
244  goto __phase2;
245  if (__pred(*++__first1, *__first2))
246  goto __phase2;
247  if (__pred(*++__first1, *__first2))
248  goto __phase2;
249  if (__pred(*++__first1, *__first2))
250  goto __phase2;
251  ++__first1;
252  }
253  switch (__s - __first1)
254  {
255  case 3:
256  if (__pred(*__first1, *__first2))
257  break;
258  ++__first1;
259  case 2:
260  if (__pred(*__first1, *__first2))
261  break;
262  ++__first1;
263  case 1:
264  if (__pred(*__first1, *__first2))
265  break;
266  case 0:
267  return __last1;
268  }
269  __phase2:
270 #endif // !_LIBCPP_UNROLL_LOOPS
271  _RandomAccessIterator1 __m1 = __first1;
272  _RandomAccessIterator2 __m2 = __first2;
273 #if !_LIBCPP_UNROLL_LOOPS
274  while (true)
275  {
276  if (++__m2 == __last2)
277  return __first1;
278  ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
279  if (!__pred(*__m1, *__m2))
280  {
281  ++__first1;
282  break;
283  }
284  }
285 #else // !_LIBCPP_UNROLL_LOOPS
286  ++__m2;
287  ++__m1;
288  for (_D2 __loop_unroll = (__last2 - __m2) / 4; __loop_unroll > 0; --__loop_unroll)
289  {
290  if (!__pred(*__m1, *__m2))
291  goto __continue;
292  if (!__pred(*++__m1, *++__m2))
293  goto __continue;
294  if (!__pred(*++__m1, *++__m2))
295  goto __continue;
296  if (!__pred(*++__m1, *++__m2))
297  goto __continue;
298  ++__m1;
299  ++__m2;
300  }
301  switch (__last2 - __m2)
302  {
303  case 3:
304  if (!__pred(*__m1, *__m2))
305  break;
306  ++__m1;
307  ++__m2;
308  case 2:
309  if (!__pred(*__m1, *__m2))
310  break;
311  ++__m1;
312  ++__m2;
313  case 1:
314  if (!__pred(*__m1, *__m2))
315  break;
316  case 0:
317  return __first1;
318  }
319  __continue:
320  ++__first1;
321 #endif // !_LIBCPP_UNROLL_LOOPS
322  }
323  }
324 
325 #endif // _LOCAL_VSTD for __search
326 
327  template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
328  _LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1
329  __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
330  _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
331  {
332  for (; __first1 != __last1; ++__first1)
333  for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
334  if (__pred(*__first1, *__j))
335  return __first1;
336  return __last1;
337  }
338 
339  // __str_find
340  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
342  __str_find(const _CharT *__p, _SizeT __sz,
343  _CharT __c, _SizeT __pos) _NOEXCEPT
344  {
345  if (__pos >= __sz)
346  return __npos;
347  const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
348  if (__r == 0)
349  return __npos;
350  return static_cast<_SizeT>(__r - __p);
351 }
352 
353  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
355  __str_find(const _CharT *__p, _SizeT __sz,
356  const _CharT* __s, _SizeT __pos, _SizeT __n)
357  {
358  if (__pos > __sz || __sz - __pos < __n)
359  return __npos;
360  if (__n == 0)
361  return __pos;
362  const _CharT* __r =
363  _VSTD::__search(__p + __pos, __p + __sz,
364  __s, __s + __n, _Traits::eq,
365  random_access_iterator_tag(), random_access_iterator_tag());
366  if (__r == __p + __sz)
367  return __npos;
368  return static_cast<_SizeT>(__r - __p);
369  }
370 
371 
372  // __str_rfind
373 
374  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
376  __str_rfind(const _CharT *__p, _SizeT __sz,
377  _CharT __c, _SizeT __pos)
378  {
379  if (__sz < 1)
380  return __npos;
381  if (__pos < __sz)
382  ++__pos;
383  else
384  __pos = __sz;
385  for (const _CharT* __ps = __p + __pos; __ps != __p;)
386  {
387  if (_Traits::eq(*--__ps, __c))
388  return static_cast<_SizeT>(__ps - __p);
389  }
390  return __npos;
391  }
392 
393  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
395  __str_rfind(const _CharT *__p, _SizeT __sz,
396  const _CharT* __s, _SizeT __pos, _SizeT __n)
397  {
398  __pos = _VSTD::min(__pos, __sz);
399  if (__n < __sz - __pos)
400  __pos += __n;
401  else
402  __pos = __sz;
403  const _CharT* __r = _VSTD::__find_end(
404  __p, __p + __pos, __s, __s + __n, _Traits::eq,
405  random_access_iterator_tag(), random_access_iterator_tag());
406  if (__n > 0 && __r == __p + __pos)
407  return __npos;
408  return static_cast<_SizeT>(__r - __p);
409  }
410 
411  // __str_find_first_of
412  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
414  __str_find_first_of(const _CharT *__p, _SizeT __sz,
415  const _CharT* __s, _SizeT __pos, _SizeT __n)
416  {
417  if (__pos >= __sz || __n == 0)
418  return __npos;
419  const _CharT* __r = _VSTD::__find_first_of_ce
420  (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq );
421  if (__r == __p + __sz)
422  return __npos;
423  return static_cast<_SizeT>(__r - __p);
424  }
425 
426 
427  // __str_find_last_of
428  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
430  __str_find_last_of(const _CharT *__p, _SizeT __sz,
431  const _CharT* __s, _SizeT __pos, _SizeT __n)
432  {
433  if (__n != 0)
434  {
435  if (__pos < __sz)
436  ++__pos;
437  else
438  __pos = __sz;
439  for (const _CharT* __ps = __p + __pos; __ps != __p;)
440  {
441  const _CharT* __r = _Traits::find(__s, __n, *--__ps);
442  if (__r)
443  return static_cast<_SizeT>(__ps - __p);
444  }
445  }
446  return __npos;
447  }
448 
449 
450  // __str_find_first_not_of
451  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
453  __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
454  const _CharT* __s, _SizeT __pos, _SizeT __n)
455  {
456  if (__pos < __sz)
457  {
458  const _CharT* __pe = __p + __sz;
459  for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
460  if (_Traits::find(__s, __n, *__ps) == 0)
461  return static_cast<_SizeT>(__ps - __p);
462  }
463  return __npos;
464  }
465 
466 
467  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
469  __str_find_first_not_of(const _CharT *__p, _SizeT __sz,
470  _CharT __c, _SizeT __pos)
471  {
472  if (__pos < __sz)
473  {
474  const _CharT* __pe = __p + __sz;
475  for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
476  if (!_Traits::eq(*__ps, __c))
477  return static_cast<_SizeT>(__ps - __p);
478  }
479  return __npos;
480  }
481 
482 
483  // __str_find_last_not_of
484  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
486  __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
487  const _CharT* __s, _SizeT __pos, _SizeT __n)
488  {
489  if (__pos < __sz)
490  ++__pos;
491  else
492  __pos = __sz;
493  for (const _CharT* __ps = __p + __pos; __ps != __p;)
494  if (_Traits::find(__s, __n, *--__ps) == 0)
495  return static_cast<_SizeT>(__ps - __p);
496  return __npos;
497  }
498 
499 
500  template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
502  __str_find_last_not_of(const _CharT *__p, _SizeT __sz,
503  _CharT __c, _SizeT __pos)
504  {
505  if (__pos < __sz)
506  ++__pos;
507  else
508  __pos = __sz;
509  for (const _CharT* __ps = __p + __pos; __ps != __p;)
510  if (!_Traits::eq(*--__ps, __c))
511  return static_cast<_SizeT>(__ps - __p);
512  return __npos;
513  }
514 
515 } // namespace __1 or __ROOT
516 
517 } // namespace std
518 
519 // Now include the main meat
520 #include "libcpp_string_view.h"
521 
522 
523 // And undo
524 #ifdef _LOCAL_LIBCPP_BEGIN_NAMESPACE_LFTS
525 #undef _LIBCPP_BEGIN_NAMESPACE_LFTS
526 #undef _LIBCPP_END_NAMESPACE_LFTS
527 #endif
528 
529 #ifdef _LOCAL_LIBCPP_BEGIN_NAMESPACE_STD
530 #undef _LIBCPP_BEGIN_NAMESPACE_STD
531 #undef _LIBCPP_END_NAMESPACE_STD
532 #endif
533 
534 #ifdef _LOCAL_LIBCPP_CONSTEXPR
535 #undef _LIBCPP_CONSTEXPR
536 #endif
537 
538 #ifdef _LOCAL_VSTD
539 #undef _VSTD
540 #endif
541 
542 #ifdef _LOCAL_LIBCPP_INLINE_VISIBILITY
543 #undef _LIBCPP_INLINE_VISIBILITY
544 #endif
545 
546 #ifdef _LOCAL_LIBCPP_STD_VER
547 #undef _LIBCPP_STD_VER
548 #endif
549 
550 #ifdef _LOCAL_LIBCPP_TYPE_VIS_ONLY
551 #undef _LIBCPP_TYPE_VIS_ONLY
552 #endif
553 
554 #ifdef _LOCAL_LIBCPP_CONSTEXPR_AFTER_CXX11
555 #undef _LIBCPP_CONSTEXPR_AFTER_CXX11
556 #endif
557 
558 #ifdef _LOCAL_NOEXCEPT
559 #undef _NOEXCEPT
560 #endif
561 
562 #ifdef _LOCAL_LIBCPP_ASSERT
563 #undef _LIBCPP_ASSERT
564 #endif
565 
566 #endif // RWrap_libcpp_string_view_h
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY __str_find_first_not_of(const _CharT *__p, _SizeT __sz, const _CharT *__s, _SizeT __pos, _SizeT __n)
#define _LIBCPP_CONSTEXPR_AFTER_CXX11
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY __str_rfind(const _CharT *__p, _SizeT __sz, _CharT __c, _SizeT __pos)
_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1 __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag, random_access_iterator_tag)
STL namespace.
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY __str_find_first_of(const _CharT *__p, _SizeT __sz, const _CharT *__s, _SizeT __pos, _SizeT __n)
#define _LIBCPP_INLINE_VISIBILITY
_ForwardIterator1 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred, forward_iterator_tag, forward_iterator_tag)
_LIBCPP_CONSTEXPR_AFTER_CXX11 _ForwardIterator1 __find_first_of_ce(_ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
#define _NOEXCEPT
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY __str_find_last_of(const _CharT *__p, _SizeT __sz, const _CharT *__s, _SizeT __pos, _SizeT __n)
basic_ostream< _CharT, _Traits > & R__put_character_sequence(basic_ostream< _CharT, _Traits > &__os, const _CharT *__str, size_t __len)
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY __str_find(const _CharT *__p, _SizeT __sz, _CharT __c, _SizeT __pos) _NOEXCEPT
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY __str_find_last_not_of(const _CharT *__p, _SizeT __sz, const _CharT *__s, _SizeT __pos, _SizeT __n)