Skip to content

Commit 5f0701f

Browse files
committed
Preparation for <string_view>. More helper functions that can be shared between <string> and <string_view>. No functionality change
llvm-svn: 210002
1 parent 125be84 commit 5f0701f

File tree

1 file changed

+109
-62
lines changed

1 file changed

+109
-62
lines changed

libcxx/include/string

Lines changed: 109 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,81 @@ char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a)
990990

991991
// helper fns for basic_string
992992

993+
// __find
993994
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
994-
_SizeT _LIBCPP_INLINE_VISIBILITY __find_first_of(const _CharT *__p, _SizeT __sz,
995-
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
995+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
996+
__find(const _CharT *__p, _SizeT __sz,
997+
_CharT __c, _SizeT __pos) _NOEXCEPT
998+
{
999+
if (__pos >= __sz)
1000+
return __npos;
1001+
const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
1002+
if (__r == 0)
1003+
return __npos;
1004+
return static_cast<_SizeT>(__r - __p);
1005+
}
1006+
1007+
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1008+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1009+
__find(const _CharT *__p, _SizeT __sz,
1010+
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1011+
{
1012+
if (__pos > __sz || __sz - __pos < __n)
1013+
return __npos;
1014+
if (__n == 0)
1015+
return __pos;
1016+
// if (__n == 1)
1017+
// return _VSTD::__find<_CharT, _SizeT, _Traits, __npos>(__p, __sz, *__s, __pos);
1018+
const _CharT* __r =
1019+
_VSTD::search(__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq);
1020+
if (__r == __p + __sz)
1021+
return __npos;
1022+
return static_cast<_SizeT>(__r - __p);
1023+
}
1024+
1025+
1026+
// __rfind
1027+
1028+
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1029+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1030+
__rfind(const _CharT *__p, _SizeT __sz,
1031+
_CharT __c, _SizeT __pos) _NOEXCEPT
1032+
{
1033+
if (__sz < 1)
1034+
return __npos;
1035+
if (__pos < __sz)
1036+
++__pos;
1037+
else
1038+
__pos = __sz;
1039+
for (const _CharT* __ps = __p + __pos; __ps != __p;)
1040+
{
1041+
if (_Traits::eq(*--__ps, __c))
1042+
return static_cast<_SizeT>(__ps - __p);
1043+
}
1044+
return __npos;
1045+
}
1046+
1047+
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1048+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1049+
__rfind(const _CharT *__p, _SizeT __sz,
1050+
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1051+
{
1052+
__pos = _VSTD::min(__pos, __sz);
1053+
if (__n < __sz - __pos)
1054+
__pos += __n;
1055+
else
1056+
__pos = __sz;
1057+
const _CharT* __r = _VSTD::find_end(__p, __p + __pos, __s, __s + __n, _Traits::eq);
1058+
if (__n > 0 && __r == __p + __pos)
1059+
return __npos;
1060+
return static_cast<_SizeT>(__r - __p);
1061+
}
1062+
1063+
// __find_first_of
1064+
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1065+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1066+
__find_first_of(const _CharT *__p, _SizeT __sz,
1067+
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
9961068
{
9971069
if (__pos >= __sz || __n == 0)
9981070
return __npos;
@@ -1003,9 +1075,12 @@ _SizeT _LIBCPP_INLINE_VISIBILITY __find_first_of(const _CharT *__p, _SizeT __sz,
10031075
return static_cast<_SizeT>(__r - __p);
10041076
}
10051077

1078+
1079+
// __find_last_of
10061080
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1007-
_SizeT _LIBCPP_INLINE_VISIBILITY __find_last_of(const _CharT *__p, _SizeT __sz,
1008-
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1081+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1082+
__find_last_of(const _CharT *__p, _SizeT __sz,
1083+
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
10091084
{
10101085
if (__n != 0)
10111086
{
@@ -1024,9 +1099,11 @@ _SizeT _LIBCPP_INLINE_VISIBILITY __find_last_of(const _CharT *__p, _SizeT __sz,
10241099
}
10251100

10261101

1102+
// __find_first_not_of
10271103
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1028-
_SizeT _LIBCPP_INLINE_VISIBILITY __find_first_not_of(const _CharT *__p, _SizeT __sz,
1029-
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1104+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1105+
__find_first_not_of(const _CharT *__p, _SizeT __sz,
1106+
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
10301107
{
10311108
if (__pos < __sz)
10321109
{
@@ -1040,8 +1117,9 @@ _SizeT _LIBCPP_INLINE_VISIBILITY __find_first_not_of(const _CharT *__p, _SizeT _
10401117

10411118

10421119
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1043-
_SizeT _LIBCPP_INLINE_VISIBILITY __find_first_not_of(const _CharT *__p, _SizeT __sz,
1044-
_CharT __c, _SizeT __pos) _NOEXCEPT
1120+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1121+
__find_first_not_of(const _CharT *__p, _SizeT __sz,
1122+
_CharT __c, _SizeT __pos) _NOEXCEPT
10451123
{
10461124
if (__pos < __sz)
10471125
{
@@ -1054,9 +1132,11 @@ _SizeT _LIBCPP_INLINE_VISIBILITY __find_first_not_of(const _CharT *__p, _SizeT _
10541132
}
10551133

10561134

1135+
// __find_last_not_of
10571136
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1058-
_SizeT _LIBCPP_INLINE_VISIBILITY __find_last_not_of(const _CharT *__p, _SizeT __sz,
1059-
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1137+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1138+
__find_last_not_of(const _CharT *__p, _SizeT __sz,
1139+
const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
10601140
{
10611141
if (__pos < __sz)
10621142
++__pos;
@@ -1070,8 +1150,9 @@ _SizeT _LIBCPP_INLINE_VISIBILITY __find_last_not_of(const _CharT *__p, _SizeT __
10701150

10711151

10721152
template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1073-
_SizeT _LIBCPP_INLINE_VISIBILITY __find_last_not_of(const _CharT *__p, _SizeT __sz,
1074-
_CharT __c, _SizeT __pos) _NOEXCEPT
1153+
_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1154+
__find_last_not_of(const _CharT *__p, _SizeT __sz,
1155+
_CharT __c, _SizeT __pos) _NOEXCEPT
10751156
{
10761157
if (__pos < __sz)
10771158
++__pos;
@@ -3346,17 +3427,8 @@ basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
33463427
size_type __n) const _NOEXCEPT
33473428
{
33483429
_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
3349-
size_type __sz = size();
3350-
if (__pos > __sz || __sz - __pos < __n)
3351-
return npos;
3352-
if (__n == 0)
3353-
return __pos;
3354-
const value_type* __p = data();
3355-
const value_type* __r = _VSTD::search(__p + __pos, __p + __sz, __s, __s + __n,
3356-
__traits_eq<traits_type>());
3357-
if (__r == __p + __sz)
3358-
return npos;
3359-
return static_cast<size_type>(__r - __p);
3430+
return _VSTD::__find<value_type, size_type, traits_type, npos>
3431+
(data(), size(), __s, __pos, __n);
33603432
}
33613433

33623434
template<class _CharT, class _Traits, class _Allocator>
@@ -3365,7 +3437,8 @@ typename basic_string<_CharT, _Traits, _Allocator>::size_type
33653437
basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
33663438
size_type __pos) const _NOEXCEPT
33673439
{
3368-
return find(__str.data(), __pos, __str.size());
3440+
return _VSTD::__find<value_type, size_type, traits_type, npos>
3441+
(data(), size(), __str.data(), __pos, __str.size());
33693442
}
33703443

33713444
template<class _CharT, class _Traits, class _Allocator>
@@ -3375,22 +3448,17 @@ basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
33753448
size_type __pos) const _NOEXCEPT
33763449
{
33773450
_LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
3378-
return find(__s, __pos, traits_type::length(__s));
3451+
return _VSTD::__find<value_type, size_type, traits_type, npos>
3452+
(data(), size(), __s, __pos, traits_type::length(__s));
33793453
}
33803454

33813455
template<class _CharT, class _Traits, class _Allocator>
33823456
typename basic_string<_CharT, _Traits, _Allocator>::size_type
33833457
basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
33843458
size_type __pos) const _NOEXCEPT
33853459
{
3386-
size_type __sz = size();
3387-
if (__pos >= __sz)
3388-
return npos;
3389-
const value_type* __p = data();
3390-
const value_type* __r = traits_type::find(__p + __pos, __sz - __pos, __c);
3391-
if (__r == 0)
3392-
return npos;
3393-
return static_cast<size_type>(__r - __p);
3460+
return _VSTD::__find<value_type, size_type, traits_type, npos>
3461+
(data(), size(), __c, __pos);
33943462
}
33953463

33963464
// rfind
@@ -3402,18 +3470,8 @@ basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
34023470
size_type __n) const _NOEXCEPT
34033471
{
34043472
_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
3405-
size_type __sz = size();
3406-
__pos = _VSTD::min(__pos, __sz);
3407-
if (__n < __sz - __pos)
3408-
__pos += __n;
3409-
else
3410-
__pos = __sz;
3411-
const value_type* __p = data();
3412-
const value_type* __r = _VSTD::find_end(__p, __p + __pos, __s, __s + __n,
3413-
__traits_eq<traits_type>());
3414-
if (__n > 0 && __r == __p + __pos)
3415-
return npos;
3416-
return static_cast<size_type>(__r - __p);
3473+
return _VSTD::__rfind<value_type, size_type, traits_type, npos>
3474+
(data(), size(), __s, __pos, __n);
34173475
}
34183476

34193477
template<class _CharT, class _Traits, class _Allocator>
@@ -3422,7 +3480,8 @@ typename basic_string<_CharT, _Traits, _Allocator>::size_type
34223480
basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
34233481
size_type __pos) const _NOEXCEPT
34243482
{
3425-
return rfind(__str.data(), __pos, __str.size());
3483+
return _VSTD::__rfind<value_type, size_type, traits_type, npos>
3484+
(data(), size(), __str.data(), __pos, __str.size());
34263485
}
34273486

34283487
template<class _CharT, class _Traits, class _Allocator>
@@ -3432,29 +3491,17 @@ basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
34323491
size_type __pos) const _NOEXCEPT
34333492
{
34343493
_LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
3435-
return rfind(__s, __pos, traits_type::length(__s));
3494+
return _VSTD::__rfind<value_type, size_type, traits_type, npos>
3495+
(data(), size(), __s, __pos, traits_type::length(__s));
34363496
}
34373497

34383498
template<class _CharT, class _Traits, class _Allocator>
34393499
typename basic_string<_CharT, _Traits, _Allocator>::size_type
34403500
basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
34413501
size_type __pos) const _NOEXCEPT
34423502
{
3443-
size_type __sz = size();
3444-
if (__sz)
3445-
{
3446-
if (__pos < __sz)
3447-
++__pos;
3448-
else
3449-
__pos = __sz;
3450-
const value_type* __p = data();
3451-
for (const value_type* __ps = __p + __pos; __ps != __p;)
3452-
{
3453-
if (traits_type::eq(*--__ps, __c))
3454-
return static_cast<size_type>(__ps - __p);
3455-
}
3456-
}
3457-
return npos;
3503+
return _VSTD::__rfind<value_type, size_type, traits_type, npos>
3504+
(data(), size(), __c, __pos);
34583505
}
34593506

34603507
// find_first_of

0 commit comments

Comments
 (0)