Turi Create  4.0
string_internals.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_STRING_INTERNALS_H_
7 #define TURI_STRING_INTERNALS_H_
8 
9 namespace turi { namespace gl_string_internal {
10 
11 static constexpr size_t npos = size_t(-1);
12 
14 static inline
15 int _compare(const char* __s1, const char* __s2, size_t __n) noexcept {
16  for (; __n; --__n, ++__s1, ++__s2) {
17  if (*__s1 < *__s2)
18  return -1;
19  if (*__s2 < *__s1)
20  return 1;
21  }
22  return 0;
23 }
24 
26 static inline
27 const char* _find(const char* __s, size_t __n, const char& __a) noexcept {
28  for (; __n; --__n) {
29  if (*__s == __a)
30  return __s;
31  ++__s;
32  }
33  return nullptr;
34 }
35 
37 static inline
38 size_t str_find(const char *p, size_t sz, char c, size_t pos) noexcept {
39  if (pos >= sz)
40  return npos;
41  const char* r = _find(p + pos, sz - pos, c);
42  if (r == 0)
43  return npos;
44  return static_cast<size_t>(r - p);
45 }
46 
48 static inline
49 size_t str_find(const char *p, size_t sz, const char* s, size_t pos, size_t n) noexcept {
50  if (pos > sz || sz - pos < n)
51  return npos;
52  if (n == 0)
53  return pos;
54  const char* r = std::search(p + pos, p + sz, s, s + n);
55  if (r == p + sz)
56  return npos;
57  return static_cast<size_t>(r - p);
58 }
59 
60 // str_rfind
62 static inline size_t str_rfind(const char *p, size_t sz, char c, size_t pos) noexcept {
63  if (sz < 1)
64  return npos;
65  if (pos < sz)
66  ++pos;
67  else
68  pos = sz;
69 
70  for (const char* ps = p + pos; ps != p;) {
71  if (*--ps == c)
72  return static_cast<size_t>(ps - p);
73  }
74 
75  return npos;
76 }
77 
79 static inline size_t str_rfind(
80  const char *p, size_t sz, const char* s, size_t pos, size_t n) noexcept {
81 
82  pos = std::min(pos, sz);
83  if (n < sz - pos)
84  pos += n;
85  else
86  pos = sz;
87 
88  const char* r = std::find_end(p, p + pos, s, s + n);
89 
90  if (n > 0 && r == p + pos)
91  return npos;
92 
93  return static_cast<size_t>(r - p);
94 }
95 
96 // str_find_first_of
98 static inline size_t str_find_first_of(
99  const char *p, size_t sz, const char* s, size_t pos, size_t n) noexcept {
100 
101  if (pos >= sz || n == 0)
102  return npos;
103  const char* r = std::find_first_of(p + pos, p + sz, s, s + n);
104  if (r == p + sz)
105  return npos;
106  return static_cast<size_t>(r - p);
107 }
108 
109 
110 // str_find_last_of
112 static inline size_t str_find_last_of(
113  const char *p, size_t sz, const char* s, size_t pos, size_t n) noexcept {
114 
115  if (n != 0) {
116  if (pos < sz)
117  ++pos;
118  else
119  pos = sz;
120  for (const char* ps = p + pos; ps != p;) {
121  const char* r = _find(s, n, *--ps);
122  if (r)
123  return static_cast<size_t>(ps - p);
124  }
125  }
126 
127  return npos;
128 }
129 
130 // str_find_first_not_of
132 static inline size_t str_find_first_not_of(
133  const char *p, size_t sz, const char* s, size_t pos, size_t n) noexcept {
134  if (pos < sz) {
135  const char* pe = p + sz;
136  for (const char* ps = p + pos; ps != pe; ++ps)
137  if (_find(s, n, *ps) == 0)
138  return static_cast<size_t>(ps - p);
139  }
140  return npos;
141 }
142 
143 
145 static inline size_t str_find_first_not_of(
146  const char *p, size_t sz, char c, size_t pos) noexcept {
147 
148  if (pos < sz) {
149  const char* pe = p + sz;
150  for (const char* ps = p + pos; ps != pe; ++ps)
151  if(*ps != c)
152  return static_cast<size_t>(ps - p);
153  }
154  return npos;
155 }
156 
157 // str_find_last_not_of
159 static inline size_t str_find_last_not_of(
160  const char *p, size_t sz, const char* s, size_t pos, size_t n) noexcept {
161 
162  if (pos < sz)
163  ++pos;
164  else
165  pos = sz;
166 
167  for (const char* ps = p + pos; ps != p;)
168  if (_find(s, n, *--ps) == 0)
169  return static_cast<size_t>(ps - p);
170  return npos;
171 }
172 
174 static inline size_t str_find_last_not_of(
175  const char *p, size_t sz, char c, size_t pos) noexcept {
176 
177  if (pos < sz)
178  ++pos;
179  else
180  pos = sz;
181 
182  for (const char* ps = p + pos; ps != p;)
183  if ((*--ps) != c)
184  return static_cast<size_t>(ps - p);
185  return npos;
186 }
187 
189 static inline int compare(const char* s1, const char* s2, size_t n) {
190  for (; n; --n, ++s1, ++s2) {
191  if (*s1 < *s2) return -1;
192  if (*s2 < *s1) return 1;
193  }
194  return 0;
195 }
196 
197 }}
198 
199 #endif /* TURI_STRING_INTERNALS_H_ */
#define GL_HOT_INLINE_FLATTEN