string_view.cuh
1 /*
2  * Copyright (c) 2019-2025, 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_RUNTIME_JIT
30 #include <thrust/count.h>
31 #include <thrust/execution_policy.h>
32 #endif
33 
34 #include <cuda/std/functional>
35 #include <cuda/std/utility>
36 
37 // This file should only include device code logic.
38 // Host-only or host/device code should be defined in the string_view.hpp header file.
39 
40 namespace CUDF_EXPORT cudf {
41 namespace strings {
42 namespace detail {
43 
51 __device__ inline size_type characters_in_string(char const* str, size_type bytes)
52 {
53  if ((str == nullptr) || (bytes == 0)) return 0;
54  auto ptr = reinterpret_cast<uint8_t const*>(str);
55 #ifndef CUDF_RUNTIME_JIT
56  return thrust::count_if(
57  thrust::seq, ptr, ptr + bytes, [](uint8_t chr) { return is_begin_utf8_char(chr); });
58 #else
59  size_type chars = 0;
60  auto const end = ptr + bytes;
61  while (ptr < end) {
62  chars += is_begin_utf8_char(*ptr++);
63  }
64  return chars;
65 #endif
66 }
67 
79 __device__ inline cuda::std::pair<size_type, size_type> bytes_to_character_position(
80  string_view d_str, size_type pos)
81 {
82  size_type bytes = 0;
83  auto ptr = d_str.data();
84  auto const end_ptr = ptr + d_str.size_bytes();
85  while ((pos > 0) && (ptr < end_ptr)) {
86  auto const width = strings::detail::bytes_in_utf8_byte(static_cast<uint8_t>(*ptr));
87  if (width) { --pos; }
88  bytes += width;
89  ++ptr;
90  }
91  return {bytes, pos};
92 }
93 
103 static __constant__ char max_string_sentinel[5]{"\xF7\xBF\xBF\xBF"}; // NOLINT
104 } // namespace detail
105 } // namespace strings
106 
115 CUDF_HOST_DEVICE inline string_view string_view::min() { return {}; }
116 
126 CUDF_HOST_DEVICE inline string_view string_view::max()
127 {
128  char const* psentinel{nullptr};
129 #if defined(__CUDA_ARCH__)
130  psentinel = &cudf::strings::detail::max_string_sentinel[0];
131 #else
133  cudaGetSymbolAddress((void**)&psentinel, cudf::strings::detail::max_string_sentinel));
134 #endif
135  return {psentinel, 4};
136 }
137 
138 __device__ inline size_type string_view::length() const
139 {
140  if (_length == UNKNOWN_STRING_LENGTH)
141  _length = strings::detail::characters_in_string(_data, _bytes);
142  return _length;
143 }
144 
145 // @cond
146 // this custom iterator knows about UTF8 encoding
147 __device__ inline string_view::const_iterator::const_iterator(string_view const& str, size_type pos)
148  : p{str.data()}, bytes{str.size_bytes()}, char_pos{pos}, byte_pos{str.byte_offset(pos)}
149 {
150 }
151 
152 __device__ inline string_view::const_iterator::const_iterator(string_view const& str,
153  size_type pos,
154  size_type offset)
155  : p{str.data()}, bytes{str.size_bytes()}, char_pos{pos}, byte_pos{offset}
156 {
157 }
158 
159 __device__ inline string_view::const_iterator& string_view::const_iterator::operator++()
160 {
161  if (byte_pos < bytes) {
162  // max is used to prevent an infinite loop on invalid UTF-8 data
163  byte_pos +=
164  cuda::std::max(1, strings::detail::bytes_in_utf8_byte(static_cast<uint8_t>(p[byte_pos])));
165  }
166  ++char_pos;
167  return *this;
168 }
169 
170 __device__ inline string_view::const_iterator string_view::const_iterator::operator++(int)
171 {
172  string_view::const_iterator tmp(*this);
173  operator++();
174  return tmp;
175 }
176 
177 __device__ inline string_view::const_iterator string_view::const_iterator::operator+(
178  string_view::const_iterator::difference_type offset) const
179 {
180  const_iterator tmp(*this);
181  size_type adjust = abs(offset);
182  while (adjust-- > 0)
183  offset > 0 ? ++tmp : --tmp;
184  return tmp;
185 }
186 
187 __device__ inline string_view::const_iterator& string_view::const_iterator::operator+=(
188  string_view::const_iterator::difference_type offset)
189 {
190  size_type adjust = abs(offset);
191  while (adjust-- > 0)
192  offset > 0 ? operator++() : operator--();
193  return *this;
194 }
195 
196 __device__ inline string_view::const_iterator& string_view::const_iterator::operator--()
197 {
198  if (byte_pos > 0) {
199  if (byte_pos == char_pos) {
200  --byte_pos;
201  } else {
202  while (strings::detail::bytes_in_utf8_byte(static_cast<uint8_t>(p[--byte_pos])) == 0)
203  ;
204  }
205  }
206  --char_pos;
207  return *this;
208 }
209 
210 __device__ inline string_view::const_iterator string_view::const_iterator::operator--(int)
211 {
212  string_view::const_iterator tmp(*this);
213  operator--();
214  return tmp;
215 }
216 
217 __device__ inline string_view::const_iterator& string_view::const_iterator::operator-=(
218  string_view::const_iterator::difference_type offset)
219 {
220  size_type adjust = abs(offset);
221  while (adjust-- > 0)
222  offset > 0 ? operator--() : operator++();
223  return *this;
224 }
225 
226 __device__ inline string_view::const_iterator string_view::const_iterator::operator-(
227  string_view::const_iterator::difference_type offset) const
228 {
229  const_iterator tmp(*this);
230  size_type adjust = abs(offset);
231  while (adjust-- > 0)
232  offset > 0 ? --tmp : ++tmp;
233  return tmp;
234 }
235 
236 __device__ inline string_view::const_iterator& string_view::const_iterator::move_to(
237  size_type new_pos)
238 {
239  *this += (new_pos - char_pos); // more efficient than recounting from the start
240  return *this;
241 }
242 
243 __device__ inline bool string_view::const_iterator::operator==(
244  string_view::const_iterator const& rhs) const
245 {
246  return (p == rhs.p) && (char_pos == rhs.char_pos);
247 }
248 
249 __device__ inline bool string_view::const_iterator::operator!=(
250  string_view::const_iterator const& rhs) const
251 {
252  return (p != rhs.p) || (char_pos != rhs.char_pos);
253 }
254 
255 __device__ inline bool string_view::const_iterator::operator<(
256  string_view::const_iterator const& rhs) const
257 {
258  return (p == rhs.p) && (char_pos < rhs.char_pos);
259 }
260 
261 __device__ inline bool string_view::const_iterator::operator<=(
262  string_view::const_iterator const& rhs) const
263 {
264  return (p == rhs.p) && (char_pos <= rhs.char_pos);
265 }
266 
267 __device__ inline bool string_view::const_iterator::operator>(
268  string_view::const_iterator const& rhs) const
269 {
270  return (p == rhs.p) && (char_pos > rhs.char_pos);
271 }
272 
273 __device__ inline bool string_view::const_iterator::operator>=(
274  string_view::const_iterator const& rhs) const
275 {
276  return (p == rhs.p) && (char_pos >= rhs.char_pos);
277 }
278 
279 __device__ inline char_utf8 string_view::const_iterator::operator*() const
280 {
281  char_utf8 chr = 0;
282  strings::detail::to_char_utf8(p + byte_offset(), chr);
283  return chr;
284 }
285 
286 __device__ inline size_type string_view::const_iterator::position() const { return char_pos; }
287 
288 __device__ inline size_type string_view::const_iterator::byte_offset() const { return byte_pos; }
289 
290 __device__ inline string_view::const_iterator string_view::begin() const { return {*this, 0, 0}; }
291 
292 __device__ inline string_view::const_iterator string_view::end() const
293 {
294  return {*this, length(), size_bytes()};
295 }
296 // @endcond
297 
298 __device__ inline char_utf8 string_view::operator[](size_type pos) const
299 {
300  size_type offset = byte_offset(pos);
301  if (offset >= _bytes) return 0;
302  char_utf8 chr = 0;
303  strings::detail::to_char_utf8(data() + offset, chr);
304  return chr;
305 }
306 
307 __device__ inline size_type string_view::byte_offset(size_type pos) const
308 {
309  if (length() == size_bytes()) return pos;
310  return cuda::std::get<0>(strings::detail::bytes_to_character_position(*this, pos));
311 }
312 
313 __device__ inline int string_view::compare(string_view const& in) const
314 {
315  return compare(in.data(), in.size_bytes());
316 }
317 
318 __device__ inline int string_view::compare(char const* data, size_type bytes) const
319 {
320  size_type const len1 = size_bytes();
321  auto const* ptr1 = reinterpret_cast<unsigned char const*>(this->data());
322  auto const* ptr2 = reinterpret_cast<unsigned char const*>(data);
323  if ((ptr1 == ptr2) && (bytes == len1)) return 0;
324  size_type idx = 0;
325  for (; (idx < len1) && (idx < bytes); ++idx) {
326  if (*ptr1 != *ptr2) return static_cast<int32_t>(*ptr1) - static_cast<int32_t>(*ptr2);
327  ++ptr1;
328  ++ptr2;
329  }
330  if (idx < len1) return 1;
331  if (idx < bytes) return -1;
332  return 0;
333 }
334 
335 __device__ inline bool string_view::operator==(string_view const& rhs) const
336 {
337  return (size_bytes() == rhs.size_bytes()) && (compare(rhs) == 0);
338 }
339 
340 __device__ inline bool string_view::operator!=(string_view const& rhs) const
341 {
342  return compare(rhs) != 0;
343 }
344 
345 __device__ inline bool string_view::operator<(string_view const& rhs) const
346 {
347  return compare(rhs) < 0;
348 }
349 
350 __device__ inline bool string_view::operator>(string_view const& rhs) const
351 {
352  return compare(rhs) > 0;
353 }
354 
355 __device__ inline bool string_view::operator<=(string_view const& rhs) const
356 {
357  int rc = compare(rhs);
358  return (rc == 0) || (rc < 0);
359 }
360 
361 __device__ inline bool string_view::operator>=(string_view const& rhs) const
362 {
363  int rc = compare(rhs);
364  return (rc == 0) || (rc > 0);
365 }
366 
367 __device__ inline size_type string_view::find(string_view const& str,
368  size_type pos,
369  size_type count) const
370 {
371  return find(str.data(), str.size_bytes(), pos, count);
372 }
373 
374 template <bool forward>
375 __device__ inline size_type string_view::find_impl(char const* str,
376  size_type bytes,
377  size_type pos,
378  size_type count) const
379 {
380  if (!str || pos < 0) { return npos; }
381  if (pos > 0 && pos > length()) { return npos; }
382 
383  // use iterator to help reduce character/byte counting
384  auto const itr = begin() + pos;
385  auto const spos = itr.byte_offset();
386  auto const epos =
387  (count >= 0) && ((pos + count) < length()) ? (itr + count).byte_offset() : size_bytes();
388 
389  auto const find_length = (epos - spos) - bytes + 1;
390  auto const d_target = string_view{str, bytes};
391 
392  auto ptr = data() + (forward ? spos : (epos - bytes));
393  for (size_type idx = 0; idx < find_length; ++idx) {
394  if (d_target.compare(ptr, bytes) == 0) {
395  return forward ? pos : character_offset(epos - bytes - idx);
396  }
397  // use pos to record the current find position
398  pos += strings::detail::is_begin_utf8_char(*ptr);
399  forward ? ++ptr : --ptr;
400  }
401  return npos;
402 }
403 
404 __device__ inline size_type string_view::find(char const* str,
405  size_type bytes,
406  size_type pos,
407  size_type count) const
408 {
409  return find_impl<true>(str, bytes, pos, count);
410 }
411 
412 __device__ inline size_type string_view::find(char_utf8 chr, size_type pos, size_type count) const
413 {
414  char str[sizeof(char_utf8)]; // NOLINT
415  size_type chwidth = strings::detail::from_char_utf8(chr, str);
416  return find(str, chwidth, pos, count);
417 }
418 
419 __device__ inline size_type string_view::rfind(string_view const& str,
420  size_type pos,
421  size_type count) const
422 {
423  return rfind(str.data(), str.size_bytes(), pos, count);
424 }
425 
426 __device__ inline size_type string_view::rfind(char const* str,
427  size_type bytes,
428  size_type pos,
429  size_type count) const
430 {
431  return find_impl<false>(str, bytes, pos, count);
432 }
433 
434 __device__ inline size_type string_view::rfind(char_utf8 chr, size_type pos, size_type count) const
435 {
436  char str[sizeof(char_utf8)]; // NOLINT
437  size_type chwidth = strings::detail::from_char_utf8(chr, str);
438  return rfind(str, chwidth, pos, count);
439 }
440 
441 // parameters are character position values
442 __device__ inline string_view string_view::substr(size_type pos, size_type count) const
443 {
444  if (pos < 0 || pos >= length()) { return string_view{}; }
445  auto const spos = begin() + pos;
446  auto const epos = count >= 0 ? (spos + count) : const_iterator{*this, _length, size_bytes()};
447  auto ss = string_view{data() + spos.byte_offset(), epos.byte_offset() - spos.byte_offset()};
448  // this potentially saves redundant character counting downstream
449  if (_length != UNKNOWN_STRING_LENGTH) { ss._length = epos.position() - spos.position(); }
450  return ss;
451 }
452 
453 __device__ inline size_type string_view::character_offset(size_type bytepos) const
454 {
455  if (length() == size_bytes()) return bytepos;
456  return strings::detail::characters_in_string(data(), bytepos);
457 }
458 
459 } // namespace CUDF_EXPORT cudf
Handy iterator for navigating through encoded characters.
Definition: string_view.hpp:75
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 ...
CUDF_HOST_DEVICE string_view()
Default constructor represents an empty 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: host_udf.hpp:37
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