Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move standalone UTF8 functions from string_view.hpp to utf8.hpp #10369

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conda/recipes/libcudf/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ test:
- test -f $PREFIX/include/cudf/strings/detail/fill.hpp
- test -f $PREFIX/include/cudf/strings/detail/json.hpp
- test -f $PREFIX/include/cudf/strings/detail/replace.hpp
- test -f $PREFIX/include/cudf/strings/detail/utf8.hpp
- test -f $PREFIX/include/cudf/strings/detail/utilities.hpp
- test -f $PREFIX/include/cudf/strings/extract.hpp
- test -f $PREFIX/include/cudf/strings/findall.hpp
Expand Down
121 changes: 121 additions & 0 deletions cpp/include/cudf/strings/detail/utf8.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <cudf/types.hpp>

/**
* @file
* @brief Standalone string functions.
*/

namespace cudf {

using char_utf8 = uint32_t; ///< UTF-8 characters are 1-4 bytes

namespace strings {
namespace detail {

/**
* @brief This will return true if passed the first byte of a UTF-8 character.
*
* @param byte Any byte from a valid UTF-8 character
* @return true if this the first byte of the character
*/
constexpr bool is_begin_utf8_char(uint8_t byte)
{
// The (0xC0 & 0x80) bit pattern identifies a continuation byte of a character.
return (byte & 0xC0) != 0x80;
}

/**
* @brief Returns the number of bytes in the specified character.
*
* @param character Single character
* @return Number of bytes
*/
constexpr size_type bytes_in_char_utf8(char_utf8 character)
{
return 1 + static_cast<size_type>((character & unsigned{0x0000FF00}) > 0) +
static_cast<size_type>((character & unsigned{0x00FF0000}) > 0) +
static_cast<size_type>((character & unsigned{0xFF000000}) > 0);
}

/**
* @brief Returns the number of bytes used to represent the provided byte.
*
* This could be 0 to 4 bytes. 0 is returned for intermediate bytes within a
* single character. For example, for the two-byte 0xC3A8 single character,
* the first byte would return 2 and the second byte would return 0.
*
* @param byte Byte from an encoded character.
* @return Number of bytes.
*/
constexpr size_type bytes_in_utf8_byte(uint8_t byte)
{
return 1 + static_cast<size_type>((byte & 0xF0) == 0xF0) // 4-byte character prefix
+ static_cast<size_type>((byte & 0xE0) == 0xE0) // 3-byte character prefix
+ static_cast<size_type>((byte & 0xC0) == 0xC0) // 2-byte character prefix
- static_cast<size_type>((byte & 0xC0) == 0x80); // intermediate byte
}

/**
* @brief Convert a char array into a char_utf8 value.
*
* @param str String containing encoded char bytes.
* @param[out] character Single char_utf8 value.
* @return The number of bytes in the character
*/
constexpr size_type to_char_utf8(const char* str, char_utf8& character)
{
size_type const chr_width = bytes_in_utf8_byte(static_cast<uint8_t>(*str));

character = static_cast<char_utf8>(*str++) & 0xFF;
if (chr_width > 1) {
character = character << 8;
character |= (static_cast<char_utf8>(*str++) & 0xFF); // << 8;
if (chr_width > 2) {
character = character << 8;
character |= (static_cast<char_utf8>(*str++) & 0xFF); // << 16;
if (chr_width > 3) {
character = character << 8;
character |= (static_cast<char_utf8>(*str++) & 0xFF); // << 24;
}
}
}
return chr_width;
}

/**
* @brief Place a char_utf8 value into a char array.
*
* @param character Single character
* @param[out] str Output array.
* @return The number of bytes in the character
*/
constexpr inline size_type from_char_utf8(char_utf8 character, char* str)
{
size_type const chr_width = bytes_in_char_utf8(character);
for (size_type idx = 0; idx < chr_width; ++idx) {
str[chr_width - idx - 1] = static_cast<char>(character) & 0xFF;
character = character >> 8;
}
return chr_width;
}

} // namespace detail
} // namespace strings
} // namespace cudf
3 changes: 2 additions & 1 deletion cpp/include/cudf/strings/string_view.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

#pragma once

#include <cudf/strings/detail/utf8.hpp>
#include <cudf/strings/string_view.hpp>

#ifndef __CUDA_ARCH__
Expand Down
94 changes: 1 addition & 93 deletions cpp/include/cudf/strings/string_view.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -333,96 +333,4 @@ class string_view {
__device__ [[nodiscard]] inline size_type character_offset(size_type bytepos) const;
};

namespace strings {
namespace detail {

/**
* @brief This will return true if passed the first byte of a UTF-8 character.
*
* @param byte Any byte from a valid UTF-8 character
* @return true if this the first byte of the character
*/
constexpr bool is_begin_utf8_char(uint8_t byte)
{
// The (0xC0 & 0x80) bit pattern identifies a continuation byte of a character.
return (byte & 0xC0) != 0x80;
}

/**
* @brief Returns the number of bytes in the specified character.
*
* @param character Single character
* @return Number of bytes
*/
constexpr size_type bytes_in_char_utf8(char_utf8 character)
{
return 1 + static_cast<size_type>((character & unsigned{0x0000FF00}) > 0) +
static_cast<size_type>((character & unsigned{0x00FF0000}) > 0) +
static_cast<size_type>((character & unsigned{0xFF000000}) > 0);
}

/**
* @brief Returns the number of bytes used to represent the provided byte.
*
* This could be 0 to 4 bytes. 0 is returned for intermediate bytes within a
* single character. For example, for the two-byte 0xC3A8 single character,
* the first byte would return 2 and the second byte would return 0.
*
* @param byte Byte from an encoded character.
* @return Number of bytes.
*/
constexpr size_type bytes_in_utf8_byte(uint8_t byte)
{
return 1 + static_cast<size_type>((byte & 0xF0) == 0xF0) // 4-byte character prefix
+ static_cast<size_type>((byte & 0xE0) == 0xE0) // 3-byte character prefix
+ static_cast<size_type>((byte & 0xC0) == 0xC0) // 2-byte character prefix
- static_cast<size_type>((byte & 0xC0) == 0x80); // intermediate byte
}

/**
* @brief Convert a char array into a char_utf8 value.
*
* @param str String containing encoded char bytes.
* @param[out] character Single char_utf8 value.
* @return The number of bytes in the character
*/
CUDF_HOST_DEVICE inline size_type to_char_utf8(const char* str, char_utf8& character)
{
size_type const chr_width = bytes_in_utf8_byte(static_cast<uint8_t>(*str));

character = static_cast<char_utf8>(*str++) & 0xFF;
if (chr_width > 1) {
character = character << 8;
character |= (static_cast<char_utf8>(*str++) & 0xFF); // << 8;
if (chr_width > 2) {
character = character << 8;
character |= (static_cast<char_utf8>(*str++) & 0xFF); // << 16;
if (chr_width > 3) {
character = character << 8;
character |= (static_cast<char_utf8>(*str++) & 0xFF); // << 24;
}
}
}
return chr_width;
}

/**
* @brief Place a char_utf8 value into a char array.
*
* @param character Single character
* @param[out] str Allocated char array with enough space to hold the encoded character.
* @return The number of bytes in the character
*/
CUDF_HOST_DEVICE inline size_type from_char_utf8(char_utf8 character, char* str)
{
size_type const chr_width = bytes_in_char_utf8(character);
for (size_type idx = 0; idx < chr_width; ++idx) {
str[chr_width - idx - 1] = static_cast<char>(character) & 0xFF;
character = character >> 8;
}
return chr_width;
}

} // namespace detail
} // namespace strings
} // namespace cudf