8 #include <cudf/strings/detail/utf8.hpp>
10 #include <cudf/utilities/export.hpp>
18 #ifndef CUDF_RUNTIME_JIT
19 #include <thrust/count.h>
20 #include <thrust/execution_policy.h>
23 #include <cuda/std/functional>
24 #include <cuda/std/utility>
29 namespace CUDF_EXPORT
cudf {
42 if ((str ==
nullptr) || (bytes == 0))
return 0;
43 auto ptr =
reinterpret_cast<uint8_t const*
>(str);
44 #ifndef CUDF_RUNTIME_JIT
45 return thrust::count_if(
46 thrust::seq, ptr, ptr + bytes, [](uint8_t chr) {
return is_begin_utf8_char(chr); });
49 auto const end = ptr + bytes;
51 chars += is_begin_utf8_char(*ptr++);
68 __device__
inline cuda::std::pair<size_type, size_type> bytes_to_character_position(
72 auto ptr = d_str.data();
73 auto const end_ptr = ptr + d_str.size_bytes();
74 while ((pos > 0) && (ptr < end_ptr)) {
75 auto const width = strings::detail::bytes_in_utf8_byte(
static_cast<uint8_t
>(*ptr));
92 static __constant__
char max_string_sentinel[5]{
"\xF7\xBF\xBF\xBF"};
117 char const* psentinel{
nullptr};
118 #if defined(__CUDA_ARCH__)
119 psentinel = &cudf::strings::detail::max_string_sentinel[0];
122 cudaGetSymbolAddress((
void**)&psentinel, cudf::strings::detail::max_string_sentinel));
124 return {psentinel, 4};
129 if (_length == UNKNOWN_STRING_LENGTH)
130 _length = strings::detail::characters_in_string(_data, _bytes);
136 __device__
inline string_view::const_iterator::const_iterator(
string_view const& str,
size_type pos)
137 : p{str.data()}, bytes{str.size_bytes()}, char_pos{pos}, byte_pos{str.byte_offset(pos)}
141 __device__
inline string_view::const_iterator::const_iterator(string_view
const& str,
144 : p{str.data()}, bytes{str.size_bytes()}, char_pos{pos}, byte_pos{offset}
148 __device__
inline string_view::const_iterator& string_view::const_iterator::operator++()
150 if (byte_pos < bytes) {
153 cuda::std::max(1, strings::detail::bytes_in_utf8_byte(
static_cast<uint8_t
>(p[byte_pos])));
159 __device__
inline string_view::const_iterator string_view::const_iterator::operator++(
int)
161 string_view::const_iterator tmp(*
this);
167 string_view::const_iterator::difference_type offset)
const
169 const_iterator tmp(*
this);
172 offset > 0 ? ++tmp : --tmp;
176 __device__
inline string_view::const_iterator& string_view::const_iterator::operator+=(
177 string_view::const_iterator::difference_type offset)
181 offset > 0 ? operator++() : operator--();
185 __device__
inline string_view::const_iterator& string_view::const_iterator::operator--()
188 if (byte_pos == char_pos) {
191 while (strings::detail::bytes_in_utf8_byte(
static_cast<uint8_t
>(p[--byte_pos])) == 0)
199 __device__
inline string_view::const_iterator string_view::const_iterator::operator--(
int)
201 string_view::const_iterator tmp(*
this);
206 __device__
inline string_view::const_iterator& string_view::const_iterator::operator-=(
207 string_view::const_iterator::difference_type offset)
211 offset > 0 ? operator--() : operator++();
216 string_view::const_iterator::difference_type offset)
const
218 const_iterator tmp(*
this);
221 offset > 0 ? --tmp : ++tmp;
225 __device__
inline string_view::const_iterator& string_view::const_iterator::move_to(
228 *
this += (new_pos - char_pos);
233 string_view::const_iterator
const& rhs)
const
235 return (p == rhs.p) && (char_pos == rhs.char_pos);
239 string_view::const_iterator
const& rhs)
const
241 return (p != rhs.p) || (char_pos != rhs.char_pos);
245 string_view::const_iterator
const& rhs)
const
247 return (p == rhs.p) && (char_pos < rhs.char_pos);
251 string_view::const_iterator
const& rhs)
const
253 return (p == rhs.p) && (char_pos <= rhs.char_pos);
257 string_view::const_iterator
const& rhs)
const
259 return (p == rhs.p) && (char_pos > rhs.char_pos);
263 string_view::const_iterator
const& rhs)
const
265 return (p == rhs.p) && (char_pos >= rhs.char_pos);
271 strings::detail::to_char_utf8(p +
byte_offset(), chr);
275 __device__
inline size_type string_view::const_iterator::position()
const {
return char_pos; }
277 __device__
inline size_type string_view::const_iterator::byte_offset()
const {
return byte_pos; }
279 __device__
inline string_view::const_iterator
string_view::begin()
const {
return {*
this, 0, 0}; }
290 if (offset >= _bytes)
return 0;
292 strings::detail::to_char_utf8(
data() + offset, chr);
299 return cuda::std::get<0>(strings::detail::bytes_to_character_position(*
this, pos));
310 auto const* ptr1 =
reinterpret_cast<unsigned char const*
>(this->
data());
311 auto const* ptr2 =
reinterpret_cast<unsigned char const*
>(
data);
312 if ((ptr1 == ptr2) && (bytes == len1))
return 0;
314 for (; (idx < len1) && (idx < bytes); ++idx) {
315 if (*ptr1 != *ptr2)
return static_cast<int32_t
>(*ptr1) -
static_cast<int32_t
>(*ptr2);
319 if (idx < len1)
return 1;
320 if (idx < bytes)
return -1;
347 return (rc == 0) || (rc < 0);
353 return (rc == 0) || (rc > 0);
363 template <
bool forward>
364 __device__
inline size_type string_view::find_impl(
char const* str,
369 if (!str || pos < 0) {
return npos; }
370 if (pos > 0 && pos >
length()) {
return npos; }
373 auto const itr =
begin() + pos;
374 auto const spos = itr.byte_offset();
376 (count >= 0) && ((pos + count) <
length()) ? (itr + count).byte_offset() :
size_bytes();
378 auto const find_length = (epos - spos) - bytes + 1;
381 auto ptr =
data() + (forward ? spos : (epos - bytes));
382 for (
size_type idx = 0; idx < find_length; ++idx) {
383 if (d_target.compare(ptr, bytes) == 0) {
384 return forward ? pos : character_offset(epos - bytes - idx);
387 pos += strings::detail::is_begin_utf8_char(*ptr);
388 forward ? ++ptr : --ptr;
398 return find_impl<true>(str, bytes, pos, count);
404 size_type chwidth = strings::detail::from_char_utf8(chr, str);
405 return find(str, chwidth, pos, count);
420 return find_impl<false>(str, bytes, pos, count);
426 size_type chwidth = strings::detail::from_char_utf8(chr, str);
427 return rfind(str, chwidth, pos, count);
434 auto const spos =
begin() + pos;
436 auto ss =
string_view{
data() + spos.byte_offset(), epos.byte_offset() - spos.byte_offset()};
438 if (_length != UNKNOWN_STRING_LENGTH) { ss._length = epos.position() - spos.position(); }
445 return strings::detail::characters_in_string(
data(), bytepos);
Handy iterator for navigating through encoded characters.
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
CUDF_HOST_DEVICE size_type size_bytes() const
Return the number of bytes in this string.
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.
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.
int32_t size_type
Row index type for columns and tables.
uint32_t char_utf8
UTF-8 characters are 1-4 bytes.
Class definition for cudf::string_view.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.