19 #include <cudf/strings/detail/utf8.hpp>
21 #include <cudf/utilities/export.hpp>
30 #include <thrust/count.h>
31 #include <thrust/execution_policy.h>
39 namespace CUDF_EXPORT
cudf {
52 if ((str ==
nullptr) || (bytes == 0))
return 0;
53 auto ptr =
reinterpret_cast<uint8_t const*
>(str);
55 return thrust::count_if(
56 thrust::seq, ptr, ptr + bytes, [](uint8_t chr) {
return is_begin_utf8_char(chr); });
59 auto const end = ptr + bytes;
61 chars += is_begin_utf8_char(*ptr++);
78 __device__
inline std::pair<size_type, size_type> bytes_to_character_position(string_view d_str,
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));
102 static __constant__
char max_string_sentinel[5]{
"\xF7\xBF\xBF\xBF"};
127 char const* psentinel{
nullptr};
128 #if defined(__CUDA_ARCH__)
129 psentinel = &cudf::strings::detail::max_string_sentinel[0];
132 cudaGetSymbolAddress((
void**)&psentinel, cudf::strings::detail::max_string_sentinel));
134 return {psentinel, 4};
139 if (_length == UNKNOWN_STRING_LENGTH)
140 _length = strings::detail::characters_in_string(_data, _bytes);
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)}
151 __device__
inline string_view::const_iterator::const_iterator(string_view
const& str,
154 : p{str.data()}, bytes{str.size_bytes()}, char_pos{pos}, byte_pos{offset}
158 __device__
inline string_view::const_iterator& string_view::const_iterator::operator++()
160 if (byte_pos < bytes)
161 byte_pos += strings::detail::bytes_in_utf8_byte(
static_cast<uint8_t
>(p[byte_pos]));
166 __device__
inline string_view::const_iterator string_view::const_iterator::operator++(
int)
168 string_view::const_iterator tmp(*
this);
174 string_view::const_iterator::difference_type offset)
const
176 const_iterator tmp(*
this);
179 offset > 0 ? ++tmp : --tmp;
183 __device__
inline string_view::const_iterator& string_view::const_iterator::operator+=(
184 string_view::const_iterator::difference_type offset)
188 offset > 0 ? operator++() : operator--();
192 __device__
inline string_view::const_iterator& string_view::const_iterator::operator--()
195 if (byte_pos == char_pos) {
198 while (strings::detail::bytes_in_utf8_byte(
static_cast<uint8_t
>(p[--byte_pos])) == 0)
206 __device__
inline string_view::const_iterator string_view::const_iterator::operator--(
int)
208 string_view::const_iterator tmp(*
this);
213 __device__
inline string_view::const_iterator& string_view::const_iterator::operator-=(
214 string_view::const_iterator::difference_type offset)
218 offset > 0 ? operator--() : operator++();
223 string_view::const_iterator::difference_type offset)
const
225 const_iterator tmp(*
this);
228 offset > 0 ? --tmp : ++tmp;
232 __device__
inline string_view::const_iterator& string_view::const_iterator::move_to(
235 *
this += (new_pos - char_pos);
240 string_view::const_iterator
const& rhs)
const
242 return (p == rhs.p) && (char_pos == rhs.char_pos);
246 string_view::const_iterator
const& rhs)
const
248 return (p != rhs.p) || (char_pos != rhs.char_pos);
252 string_view::const_iterator
const& rhs)
const
254 return (p == rhs.p) && (char_pos < rhs.char_pos);
258 string_view::const_iterator
const& rhs)
const
260 return (p == rhs.p) && (char_pos <= rhs.char_pos);
264 string_view::const_iterator
const& rhs)
const
266 return (p == rhs.p) && (char_pos > rhs.char_pos);
270 string_view::const_iterator
const& rhs)
const
272 return (p == rhs.p) && (char_pos >= rhs.char_pos);
278 strings::detail::to_char_utf8(p +
byte_offset(), chr);
282 __device__
inline size_type string_view::const_iterator::position()
const {
return char_pos; }
284 __device__
inline size_type string_view::const_iterator::byte_offset()
const {
return byte_pos; }
288 return const_iterator(*
this, 0, 0);
300 if (offset >= _bytes)
return 0;
302 strings::detail::to_char_utf8(
data() + offset, chr);
309 return std::get<0>(strings::detail::bytes_to_character_position(*
this, pos));
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;
324 for (; (idx < len1) && (idx < bytes); ++idx) {
325 if (*ptr1 != *ptr2)
return static_cast<int32_t
>(*ptr1) -
static_cast<int32_t
>(*ptr2);
329 if (idx < len1)
return 1;
330 if (idx < bytes)
return -1;
357 return (rc == 0) || (rc < 0);
363 return (rc == 0) || (rc > 0);
373 template <
bool forward>
374 __device__
inline size_type string_view::find_impl(
char const* str,
379 auto const nchars =
length();
380 if (!str || pos < 0 || pos > nchars)
return npos;
381 if (count < 0) count = nchars;
384 auto itr =
begin() + pos;
385 auto const spos = itr.byte_offset();
388 auto const find_length = (epos - spos) - bytes + 1;
390 auto ptr =
data() + (forward ? spos : (epos - bytes));
391 for (
size_type idx = 0; idx < find_length; ++idx) {
393 for (
size_type jdx = 0; match && (jdx < bytes); ++jdx) {
394 match = (ptr[jdx] == str[jdx]);
396 if (match) {
return forward ? pos : character_offset(epos - bytes - idx); }
398 pos += strings::detail::is_begin_utf8_char(*ptr);
399 forward ? ++ptr : --ptr;
409 return find_impl<true>(str, bytes, pos, count);
415 size_type chwidth = strings::detail::from_char_utf8(chr, str);
416 return find(str, chwidth, pos, count);
431 return find_impl<false>(str, bytes, pos, count);
437 size_type chwidth = strings::detail::from_char_utf8(chr, str);
438 return rfind(str, chwidth, pos, count);
445 auto const itr =
begin() + pos;
446 auto const spos = itr.byte_offset();
448 return {
data() + spos, epos - spos};
454 return strings::detail::characters_in_string(
data(), bytepos);
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 ...
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.