1 // license:GPL-2.0+
2 // copyright-holders:Couriersud
3
4 #include "pstring.h"
5 #include "palloc.h"
6
7 #include <algorithm>
8 #include <atomic>
9 #include <stack>
10
11 template<typename F>
compare(const pstring_t & right) const12 int pstring_t<F>::compare(const pstring_t &right) const noexcept
13 {
14 #if 0
15 return m_str.compare(right.m_str);
16 #else
17 auto si = this->begin();
18 auto ri = right.begin();
19 const auto se = this->end();
20 const auto re = right.end();
21
22 while (si != se && ri != re && *si == *ri)
23 {
24 ++ri;
25 ++si;
26 }
27
28 if (si != se && ri != re)
29 return plib::narrow_cast<int>(*si) - plib::narrow_cast<int>(*ri);
30 if (si != se)
31 return 1;
32 if (ri != re)
33 return -1;
34 return 0;
35 #endif
36 }
37
38 template<typename F>
substr(size_type start,size_type nlen) const39 pstring_t<F> pstring_t<F>::substr(size_type start, size_type nlen) const
40 {
41 pstring_t ret;
42 auto ps = begin();
43 while (ps != end() && start > 0)
44 {
45 ++ps;
46 --start;
47 }
48 //FIXME: throw ?
49 if (ps != end())
50 {
51 auto pe = ps;
52 while (pe != end() && nlen > 0)
53 {
54 ++pe;
55 --nlen;
56 }
57 ret.m_str.assign(ps.p, pe.p);
58 }
59 return ret;
60 }
61
62 template<typename F>
substr(size_type start) const63 pstring_t<F> pstring_t<F>::substr(size_type start) const
64 {
65 pstring_t ret;
66 auto ps = begin();
67 while (ps != end() && start > 0)
68 {
69 ++ps;
70 --start;
71 }
72 //FIXME: throw ?
73 if (ps != end())
74 {
75 ret.m_str.assign(ps.p, end().p);
76 }
77 return ret;
78 }
79
80 template<typename F>
find(const pstring_t & search,size_type start) const81 typename pstring_t<F>::size_type pstring_t<F>::find(const pstring_t &search, size_type start) const noexcept
82 {
83 auto istart = std::next(begin(), static_cast<difference_type>(start));
84 for (; istart != end(); ++istart)
85 {
86 auto itc(istart);
87 auto cmp = search.begin();
88 while (itc != end() && cmp != search.end() && *itc == *cmp)
89 {
90 ++itc;
91 ++cmp;
92 }
93 if (cmp == search.end())
94 return start;
95 ++start;
96 }
97 return npos;
98 }
99
100 template<typename F>
find(code_t search,size_type start) const101 typename pstring_t<F>::size_type pstring_t<F>::find(code_t search, size_type start) const noexcept
102 {
103 auto i = std::next(begin(), static_cast<difference_type>(start));
104 for (; i != end(); ++i)
105 {
106 if (*i == search)
107 return start;
108 ++start;
109 }
110 return npos;
111 }
112
113 // ----------------------------------------------------------------------------------------
114 // template stuff ...
115 // ----------------------------------------------------------------------------------------
116
117 template struct pstring_t<pu8_traits>;
118 template struct pstring_t<putf8_traits>;
119 template struct pstring_t<putf16_traits>;
120 template struct pstring_t<putf32_traits>;
121 template struct pstring_t<pwchar_traits>;
122