65.9K
CodeProject 正在变化。 阅读更多。
Home

C++ 简单 UTF 库

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.75/5 (3投票s)

2022年10月6日

MIT

1分钟阅读

viewsIcon

6396

downloadIcon

66

这是一个关于 C++ 头文件 only 库的项目,它填补了 C++17 标准在 Unicode 字符串支持方面的空白。该库旨在专门处理 Unicode,不支持其他编码。

SUTFCPP 是一个 C++ 头文件 only 库,它填补了 C++17 标准在 Unicode 字符串支持方面的空白。标准没有为不同宽度的字符串之间的转换提供任何帮助,也没有提供按代码点迭代的工具。该库旨在专门处理 Unicode,不支持其他编码。主要特性如下:

  • 易于使用:该库是头文件 only
  • 体积小:由少量头文件组成,没有依赖项
  • 跨平台:支持 MSVC、GCC 和 CLANG C++17 编译器
  • 具有编译时支持

该库实现了两层 API

  • 用于代码点和代码单元操作的低层 API
    namespace sutf
    {
    // get iterator of next code point
    constexpr it_t code_point_next(it_t it) noexcept;
    
    // read code point in current position
    constexpr uint_t code_point_read(it_t it) noexcept;
    
    // write code point in current position and return position of next code point
    constexpr it_t code_point_write(it_t it, uint_t cp) noexcept;
    
    // calculate number of code points between two iterators
    constexpr uint_t code_point_count(it_t it, const it_t last) noexcept;
    
    // calculate number of code points in specified buffer 
    constexpr uint_t code_point_count(const type_t& str) noexcept;
    
    // convert code points on 'in' type to 'out' type.
    constexpr out_t code_point_convert(in_t src, const in_t last, out_t dst) noexcept;
    
    // count how many code units occupies a given code point
    constexpr uint_t code_unit_count<codeuint_t>(uint_t cp) noexcept;
    
    // count how many code units occupies a given code point range
    constexpr uint_t code_unit_count<codeuint_t>(it_t it, const it_t last) noexcept;
    
    // count how many code units occupies a given buffer 
    constexpr uint_t code_unit_count<codeuint_t>(const type_t& str) noexcept
    } // namespace sutf
  • 用于字符串和缓冲区的更高级 API
    namespace sutf
    {
    // convert string of any type to UTF8 string
    string to_string(const string_t& str);
    
    // convert string of any type to UTF16/UTF32 string (platform specific)
    wstring to_wstring(const string_t& str);
    
    // convert string of any type to UTF8 string
    u8string to_u8string(const string_t& str);
    
    // convert string of any type to UTF16 string
    u16string to_u16string(const string_t& str);
    
    // convert string of any type to UTF32 string
    u32string to_u32string(const string_t& str);
    
    // convert string of any type to string of specified type
    basic_string<char_t> to_anystring(basic_string<char_t> str);
    
    // convert code unit buffer of 'src' type to code unit buffer of 'dst' type
    uint_t convert(const src_t& src, dst_t& dst);
    
    } // namespace sutf

实现

集成

#include <sutfcpplib/utf_codepoint.h>  // Include only code unit and codepoint support
// or
#include <slimcpplib/utf_string.h>     // Include full UTF support

using namespace std::literals;

字符串字面量声明

// Define Unicode literal 'A©←😂B®✅🥳':
auto str_utf8 = u8"\U00000041\U000000a9\U00002190\U0001f602\
 U00000042\U000000ae\U00002705\U0001f973"sv; // UTF-8 string 
auto str_utf16_or_utf32 = L"\U00000041\U000000a9\U00002190\
 U0001f602\U00000042\U000000ae\U00002705\U0001f973"sv; // UTF-16/UTF-32 string
auto str_utf16 = u"\U00000041\U000000a9\U00002190\U0001f602\
 U00000042\U000000ae\U00002705\U0001f973"sv; // UTF-16 string
auto str_utf32 = U"\U00000041\U000000a9\U00002190\U0001f602\
 U00000042\U000000ae\U00002705\U0001f973"sv; // UTF-32 string

限制

  • 高级函数,例如 to_string()convert(),在其实现中使用内存分配,因此不能在编译时表达式中使用,这符合 C++17 标准。
  • 低级函数不会检查代码点是否符合 Unicode 标准,始终假定输入缓冲区与代码点对齐,并且输出缓冲区有足够的空间。

示例

  • main.cpp - 带有注释的库主接口使用示例
© . All rights reserved.