string_view.cuh
1 /*
2  * Copyright (c) 2019-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cudf/strings/detail/utf8.hpp>
21 #include <cudf/utilities/export.hpp>
22 
23 #ifndef __CUDA_ARCH__
24 #include <cudf/utilities/error.hpp>
25 #endif
26 
27 // This is defined when including this header in a https://github.com/NVIDIA/jitify
28 // or jitify2 source file. The jitify cannot include thrust headers at this time.
29 #ifndef CUDF_JIT_UDF
30 #include <thrust/count.h>
31 #include <thrust/execution_policy.h>
32 #endif
33 
34 #include <algorithm>
35 
36 // This file should only include device code logic.
37 // Host-only or host/device code should be defined in the string_view.hpp header file.
38 
39 namespace CUDF_EXPORT cudf {
40 namespace strings {
41 namespace detail {
42 
50 __device__ inline size_type characters_in_string(char const* str, size_type bytes)
51 {
52  if ((str == nullptr) || (bytes == 0)) return 0;
53  auto ptr = reinterpret_cast<uint8_t const*>(str);
54 #ifndef CUDF_JIT_UDF
55  return thrust::count_if(
56  thrust::seq, ptr, ptr + bytes, [](uint8_t chr) { return is_begin_utf8_char(chr); });
57 #else
58  size_type chars = 0;
59  auto const end = ptr + bytes;
60  while (ptr < end) {
61  chars += is_begin_utf8_char(*ptr++);
62  }
63  return chars;
64 #endif
65 }
66 
78 __device__ inline std::pair<size_type, size_type> bytes_to_character_position(string_view d_str,
79  size_type pos)
80 {
81  size_type bytes = 0;
82  auto ptr = d_str.data();
83  auto const end_ptr = ptr + d_str.size_bytes();
84  while ((pos > 0) && (ptr < end_ptr)) {
85  auto const width = strings::detail::bytes_in_utf8_byte(static_cast<uint8_t>(*ptr));
86  if (width) { --pos; }
87  bytes += width;
88  ++ptr;
89  }
90  return {bytes, pos};
91 }
92 
102 static __constant__ char max_string_sentinel[5]{"\xF7\xBF\xBF\xBF"}; // NOLINT
103 } // namespace detail
104 } // namespace strings
105 
114 CUDF_HOST_DEVICE inline string_view string_view::min() { return {}; }
115 
125 CUDF_HOST_DEVICE inline string_view string_view::max()
126 {
127  char const* psentinel{nullptr};
128 #if defined(__CUDA_ARCH__)
129  psentinel = &cudf::strings::detail::max_string_sentinel[0];
130 #else
132  cudaGetSymbolAddress((void**)&psentinel, cudf::strings::detail::max_string_sentinel));
133 #endif
134  return {psentinel, 4};
135 }
136 
137 __device__ inline size_type string_view::length() const
138 {
139  if (_length == UNKNOWN_STRING_LENGTH)
140  _length = strings::detail::characters_in_string(_data, _bytes);
141  return _length;
142 }
143 
144 // @cond
145 // this custom iterator knows about UTF8 encoding
146 __device__ inline string_view::const_iterator::const_iterator(string_view const& str, size_type pos)
147  : p{str.data()}, bytes{str.size_bytes()}, char_pos{pos}, byte_pos{str.byte_offset(pos)}
148 {
149 }
150 
151 __device__ inline string_view::const_iterator::const_iterator(string_view const& str,
152  size_type pos,
153  size_type offset)
154  : p{str.data()}, bytes{str.size_bytes()}, char_pos{pos}, byte_pos{offset}
155 {
156 }
157 
158 __device__ inline string_view::const_iterator& string_view::const_iterator::operator++()
159 {
160  if (byte_pos < bytes)
161  byte_pos += strings::detail::bytes_in_utf8_byte(static_cast<uint8_t>(p[byte_pos]));
162  ++char_pos;
163  return *this;
164 }
165 
166 __device__ inline string_view::const_iterator string_view::const_iterator::operator++(int)
167 {
168  string_view::const_iterator tmp(*this);
169  operator++();
170  return tmp;
171 }
172 
173 __device__ inline string_view::const_iterator string_view::const_iterator::operator+(
174  string_view::const_iterator::difference_type offset) const
175 {
176  const_iterator tmp(*this);
177  size_type adjust = abs(offset);
178  while (adjust-- > 0)
179  offset > 0 ? ++tmp : --tmp;
180  return tmp;
181 }
182 
183 __device__ inline string_view::const_iterator& string_view::const_iterator::operator+=(
184  string_view::const_iterator::difference_type offset)
185 {
186  size_type adjust = abs(offset);
187  while (adjust-- > 0)
188  offset > 0 ? operator++() : operator--();
189  return *this;
190 }
191 
192 __device__ inline string_view::const_iterator& string_view::const_iterator::operator--()
193 {
194  if (byte_pos > 0) {
195  if (byte_pos == char_pos) {
196  --byte_pos;
197  } else {
198  while (strings::detail::bytes_in_utf8_byte(static_cast<uint8_t>(p[--byte_pos])) == 0)
199  ;
200  }
201  }
202  --char_pos;
203  return *this;
204 }
205 
206 __device__ inline string_view::const_iterator string_view::const_iterator::operator--(int)
207 {
208  string_view::const_iterator tmp(*this);
209  operator--();
210  return tmp;
211 }
212 
213 __device__ inline string_view::const_iterator& string_view::const_iterator::operator-=(
214  string_view::const_iterator::difference_type offset)
215 {
216  size_type adjust = abs(offset);
217  while (adjust-- > 0)
218  offset > 0 ? operator--() : operator++();
219  return *this;
220 }
221 
222 __device__ inline string_view::const_iterator string_view::const_iterator::operator-(
223  string_view::const_iterator::difference_type offset) const
224 {
225  const_iterator tmp(*this);
226  size_type adjust = abs(offset);
227  while (adjust-- > 0)
228  offset > 0 ? --tmp : ++tmp;
229  return tmp;
230 }
231 
232 __device__ inline string_view::const_iterator& string_view::const_iterator::move_to(
233  size_type new_pos)
234 {
235  *this += (new_pos - char_pos); // more efficient than recounting from the start
236  return *this;
237 }
238 
239 __device__ inline bool string_view::const_iterator::operator==(
240  string_view::const_iterator const& rhs) const
241 {
242  return (p == rhs.p) && (char_pos == rhs.char_pos);
243 }
244 
245 __device__ inline bool string_view::const_iterator::operator!=(
246  string_view::const_iterator const& rhs) const
247 {
248  return (p != rhs.p) || (char_pos != rhs.char_pos);
249 }
250 
251 __device__ inline bool string_view::const_iterator::operator<(
252  string_view::const_iterator const& rhs) const
253 {
254  return (p == rhs.p) && (char_pos < rhs.char_pos);
255 }
256 
257 __device__ inline bool string_view::const_iterator::operator<=(
258  string_view::const_iterator const& rhs) const
259 {
260  return (p == rhs.p) && (char_pos <= rhs.char_pos);
261 }
262 
263 __device__ inline bool string_view::const_iterator::operator>(
264  string_view::const_iterator const& rhs) const
265 {
266  return (p == rhs.p) && (char_pos > rhs.char_pos);
267 }
268 
269 __device__ inline bool string_view::const_iterator::operator>=(
270  string_view::const_iterator const& rhs) const
271 {
272  return (p == rhs.p) && (char_pos >= rhs.char_pos);
273 }
274 
275 __device__ inline char_utf8 string_view::const_iterator::operator*() const
276 {
277  char_utf8 chr = 0;
278  strings::detail::to_char_utf8(p + byte_offset(), chr);
279  return chr;
280 }
281 
282 __device__ inline size_type string_view::const_iterator::position() const { return char_pos; }
283 
284 __device__ inline size_type string_view::const_iterator::byte_offset() const { return byte_pos; }
285 
286 __device__ inline string_view::const_iterator string_view::begin() const { return {*this, 0, 0}; }
287 
288 __device__ inline string_view::const_iterator string_view::end() const
289 {
290  return {*this, length(), size_bytes()};
291 }
292 // @endcond
293 
294 __device__ inline char_utf8 string_view::operator[](size_type pos) const
295 {
296  size_type offset = byte_offset(pos);
297  if (offset >= _bytes) return 0;
298  char_utf8 chr = 0;
299  strings::detail::to_char_utf8(data() + offset, chr);
300  return chr;
301 }
302 
303 __device__ inline size_type string_view::byte_offset(size_type pos) const
304 {
305  if (length() == size_bytes()) return pos;
306  return std::get<0>(strings::detail::bytes_to_character_position(*this, pos));
307 }
308 
309 __device__ inline int string_view::compare(string_view const& in) const
310 {
311  return compare(in.data(), in.size_bytes());
312 }
313 
314 __device__ inline int string_view::compare(char const* data, size_type bytes) const
315 {
316  size_type const len1 = size_bytes();
317  auto const* ptr1 = reinterpret_cast<unsigned char const*>(this->data());
318  auto const* ptr2 = reinterpret_cast<unsigned char const*>(data);
319  if ((ptr1 == ptr2) && (bytes == len1)) return 0;
320  size_type idx = 0;
321  for (; (idx < len1) && (idx < bytes); ++idx) {
322  if (*ptr1 != *ptr2) return static_cast<int32_t>(*ptr1) - static_cast<int32_t>(*ptr2);
323  ++ptr1;
324  ++ptr2;
325  }
326  if (idx < len1) return 1;
327  if (idx < bytes) return -1;
328  return 0;
329 }
330 
331 __device__ inline bool string_view::operator==(string_view const& rhs) const
332 {
333  return (size_bytes() == rhs.size_bytes()) && (compare(rhs) == 0);
334 }
335 
336 __device__ inline bool string_view::operator!=(string_view const& rhs) const
337 {
338  return compare(rhs) != 0;
339 }
340 
341 __device__ inline bool string_view::operator<(string_view const& rhs) const
342 {
343  return compare(rhs) < 0;
344 }
345 
346 __device__ inline bool string_view::operator>(string_view const& rhs) const
347 {
348  return compare(rhs) > 0;
349 }
350 
351 __device__ inline bool string_view::operator<=(string_view const& rhs) const
352 {
353  int rc = compare(rhs);
354  return (rc == 0) || (rc < 0);
355 }
356 
357 __device__ inline bool string_view::operator>=(string_view const& rhs) const
358 {
359  int rc = compare(rhs);
360  return (rc == 0) || (rc > 0);
361 }
362 
363 __device__ inline size_type string_view::find(string_view const& str,
364  size_type pos,
365  size_type count) const
366 {
367  return find(str.data(), str.size_bytes(), pos, count);
368 }
369 
370 template <bool forward>
371 __device__ inline size_type string_view::find_impl(char const* str,
372  size_type bytes,
373  size_type pos,
374  size_type count) const
375 {
376  auto const nchars = length();
377  if (!str || pos < 0 || pos > nchars) return npos;
378  if (count < 0) count = nchars;
379 
380  // use iterator to help reduce character/byte counting
381  auto itr = begin() + pos;
382  auto const spos = itr.byte_offset();
383  auto const epos = ((pos + count) < nchars) ? (itr + count).byte_offset() : size_bytes();
384 
385  auto const find_length = (epos - spos) - bytes + 1;
386 
387  auto ptr = data() + (forward ? spos : (epos - bytes));
388  for (size_type idx = 0; idx < find_length; ++idx) {
389  bool match = true;
390  for (size_type jdx = 0; match && (jdx < bytes); ++jdx) {
391  match = (ptr[jdx] == str[jdx]);
392  }
393  if (match) { return forward ? pos : character_offset(epos - bytes - idx); }
394  // use pos to record the current find position
395  pos += strings::detail::is_begin_utf8_char(*ptr);
396  forward ? ++ptr : --ptr;
397  }
398  return npos;
399 }
400 
401 __device__ inline size_type string_view::find(char const* str,
402  size_type bytes,
403  size_type pos,
404  size_type count) const
405 {
406  return find_impl<true>(str, bytes, pos, count);
407 }
408 
409 __device__ inline size_type string_view::find(char_utf8 chr, size_type pos, size_type count) const
410 {
411  char str[sizeof(char_utf8)]; // NOLINT
412  size_type chwidth = strings::detail::from_char_utf8(chr, str);
413  return find(str, chwidth, pos, count);
414 }
415 
416 __device__ inline size_type string_view::rfind(string_view const& str,
417  size_type pos,
418  size_type count) const
419 {
420  return rfind(str.data(), str.size_bytes(), pos, count);
421 }
422 
423 __device__ inline size_type string_view::rfind(char const* str,
424  size_type bytes,
425  size_type pos,
426  size_type count) const
427 {
428  return find_impl<false>(str, bytes, pos, count);
429 }
430 
431 __device__ inline size_type string_view::rfind(char_utf8 chr, size_type pos, size_type count) const
432 {
433  char str[sizeof(char_utf8)]; // NOLINT
434  size_type chwidth = strings::detail::from_char_utf8(chr, str);
435  return rfind(str, chwidth, pos, count);
436 }
437 
438 // parameters are character position values
439 __device__ inline string_view string_view::substr(size_type pos, size_type count) const
440 {
441  if (pos < 0 || pos >= length()) { return string_view{}; }
442  auto const itr = begin() + pos;
443  auto const spos = itr.byte_offset();
444  auto const epos = count >= 0 ? (itr + count).byte_offset() : size_bytes();
445  return {data() + spos, epos - spos};
446 }
447 
448 __device__ inline size_type string_view::character_offset(size_type bytepos) const
449 {
450  if (length() == size_bytes()) return bytepos;
451  return strings::detail::characters_in_string(data(), bytepos);
452 }
453 
454 } // namespace CUDF_EXPORT cudf
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:44
CUDF_HOST_DEVICE size_type size_bytes() const
Return the number of bytes in this string.
Definition: string_view.hpp:51
size_type rfind(string_view const &str, size_type pos=0, size_type count=-1) const
Returns the character position of the last occurrence where the argument str is found in this string ...
size_type length() const
Return the number of characters in this string.
string_view substr(size_type start, size_type length) const
Return a sub-string of this string. The original string and device memory must still be maintained fo...
bool operator==(string_view const &rhs) const
Returns true if rhs matches this string exactly.
const_iterator end() const
Return new iterator pointing past the end of this string.
int compare(string_view const &str) const
Comparing target string with this string. Each character is compared as a UTF-8 code-point value.
bool operator>=(string_view const &rhs) const
Returns true if rhs matches or is ordered before this string.
CUDF_HOST_DEVICE char const * data() const
Return a pointer to the internal device array.
Definition: string_view.hpp:63
size_type byte_offset(size_type pos) const
Return the byte offset from data() for a given character position.
const_iterator begin() const
Return new iterator pointing to the beginning of this string.
char_utf8 operator[](size_type pos) const
Return single UTF-8 character at the given character position.
bool operator!=(string_view const &rhs) const
Returns true if rhs does not match this string.
size_type find(string_view const &str, size_type pos=0, size_type count=-1) const
Returns the character position of the first occurrence where the argument str is found in this string...
bool operator<=(string_view const &rhs) const
Returns true if this string matches or is ordered before rhs.
bool operator<(string_view const &rhs) const
Returns true if this string is ordered before rhs.
bool operator>(string_view const &rhs) const
Returns true if rhs is ordered before this string.
static cudf::size_type const npos
No-position value.
bool operator==(polymorphic_allocator< T > const &lhs, polymorphic_allocator< U > const &rhs)
bool operator!=(polymorphic_allocator< T > const &lhs, polymorphic_allocator< U > const &rhs)
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator-(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE bool operator>=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE bool operator<=(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator*(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE bool operator>(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE fixed_point< Rep1, Rad1 > operator+(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
CUDF_HOST_DEVICE bool operator<(fixed_point< Rep1, Rad1 > const &lhs, fixed_point< Rep1, Rad1 > const &rhs)
#define CUDF_CUDA_TRY(call)
Error checking macro for CUDA runtime API functions.
Definition: error.hpp:264
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:95
uint32_t char_utf8
UTF-8 characters are 1-4 bytes.
Definition: string_view.hpp:31
cuDF interfaces
Definition: aggregation.hpp:35
Class definition for cudf::string_view.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:32