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"};
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
287 {
288  return const_iterator(*this, 0, 0);
289 }
290 
291 __device__ inline string_view::const_iterator string_view::end() const
292 {
293  return const_iterator(*this, length(), size_bytes());
294 }
295 // @endcond
296 
297 __device__ inline char_utf8 string_view::operator[](size_type pos) const
298 {
299  size_type offset = byte_offset(pos);
300  if (offset >= _bytes) return 0;
301  char_utf8 chr = 0;
302  strings::detail::to_char_utf8(data() + offset, chr);
303  return chr;
304 }
305 
306 __device__ inline size_type string_view::byte_offset(size_type pos) const
307 {
308  if (length() == size_bytes()) return pos;
309  return std::get<0>(strings::detail::bytes_to_character_position(*this, pos));
310 }
311 
312 __device__ inline int string_view::compare(string_view const& in) const
313 {
314  return compare(in.data(), in.size_bytes());
315 }
316 
317 __device__ inline int string_view::compare(char const* data, size_type bytes) const
318 {
319  size_type const len1 = size_bytes();
320  auto const* ptr1 = reinterpret_cast<unsigned char const*>(this->data());
321  auto const* ptr2 = reinterpret_cast<unsigned char const*>(data);
322  if ((ptr1 == ptr2) && (bytes == len1)) return 0;
323  size_type idx = 0;
324  for (; (idx < len1) && (idx < bytes); ++idx) {
325  if (*ptr1 != *ptr2) return static_cast<int32_t>(*ptr1) - static_cast<int32_t>(*ptr2);
326  ++ptr1;
327  ++ptr2;
328  }
329  if (idx < len1) return 1;
330  if (idx < bytes) return -1;
331  return 0;
332 }
333 
334 __device__ inline bool string_view::operator==(string_view const& rhs) const
335 {
336  return (size_bytes() == rhs.size_bytes()) && (compare(rhs) == 0);
337 }
338 
339 __device__ inline bool string_view::operator!=(string_view const& rhs) const
340 {
341  return compare(rhs) != 0;
342 }
343 
344 __device__ inline bool string_view::operator<(string_view const& rhs) const
345 {
346  return compare(rhs) < 0;
347 }
348 
349 __device__ inline bool string_view::operator>(string_view const& rhs) const
350 {
351  return compare(rhs) > 0;
352 }
353 
354 __device__ inline bool string_view::operator<=(string_view const& rhs) const
355 {
356  int rc = compare(rhs);
357  return (rc == 0) || (rc < 0);
358 }
359 
360 __device__ inline bool string_view::operator>=(string_view const& rhs) const
361 {
362  int rc = compare(rhs);
363  return (rc == 0) || (rc > 0);
364 }
365 
366 __device__ inline size_type string_view::find(string_view const& str,
367  size_type pos,
368  size_type count) const
369 {
370  return find(str.data(), str.size_bytes(), pos, count);
371 }
372 
373 template <bool forward>
374 __device__ inline size_type string_view::find_impl(char const* str,
375  size_type bytes,
376  size_type pos,
377  size_type count) const
378 {
379  auto const nchars = length();
380  if (!str || pos < 0 || pos > nchars) return npos;
381  if (count < 0) count = nchars;
382 
383  // use iterator to help reduce character/byte counting
384  auto itr = begin() + pos;
385  auto const spos = itr.byte_offset();
386  auto const epos = ((pos + count) < nchars) ? (itr + count).byte_offset() : size_bytes();
387 
388  auto const find_length = (epos - spos) - bytes + 1;
389 
390  auto ptr = data() + (forward ? spos : (epos - bytes));
391  for (size_type idx = 0; idx < find_length; ++idx) {
392  bool match = true;
393  for (size_type jdx = 0; match && (jdx < bytes); ++jdx) {
394  match = (ptr[jdx] == str[jdx]);
395  }
396  if (match) { return forward ? pos : character_offset(epos - bytes - idx); }
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)];
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)];
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 itr = begin() + pos;
446  auto const spos = itr.byte_offset();
447  auto const epos = count >= 0 ? (itr + count).byte_offset() : size_bytes();
448  return {data() + spos, epos - spos};
449 }
450 
451 __device__ inline size_type string_view::character_offset(size_type bytepos) const
452 {
453  if (length() == size_bytes()) return bytepos;
454  return strings::detail::characters_in_string(data(), bytepos);
455 }
456 
457 } // 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