c++11 标准模板(STL)本地化库 - 平面类别 - 在字符编码间转换,包括 UTF-8、UTF-16、UTF-32 (七)

news2024/10/6 14:33:19

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

平面类别

在字符编码间转换,包括 UTF-8、UTF-16、UTF-32

std::codecvt
template<

    class InternT,
    class ExternT,
    class State

> class codecvt;

std::codecvt 封装字符串的转换,包括宽和多字节,从一种编码到另一种。通过 std::basic_fstream<CharT> 进行的所有 I/O 操作都使用流中感染的 std::codecvt<CharT, char, std::mbstate_t> 本地环境平面。

继承图

标准库提供以下独立(本地环境无关)特化:

定义于头文件 <locale>

std::codecvt<char, char, std::mbstate_t>恒等转换
std::codecvt<char16_t, char, std::mbstate_t>在 UTF-16 和 UTF-8 间转换 (C++11 起)(C++20 中弃用)
std::codecvt<char16_t, char8_t, std::mbstate_t>在 UTF-16 和 UTF-8 间转换 (C++20 起)
std::codecvt<char32_t, char, std::mbstate_t>在 UTF-32 和 UTF-8 间转换 (C++11 起)(C++20 中弃用)
std::codecvt<char32_t, char8_t, std::mbstate_t>在 UTF-32 和 UTF-8 间转换 (C++20 起)
std::codecvt<wchar_t, char, std::mbstate_t>在系统原生宽和单字节窄字符集间转换

另外, C++ 程序中构造每个的 locale 对象实现其自身的四个特化的( locale 限定)版本。

成员类型

成员类型定义
intern_typeInternT
extern_typeExternT
state_typeState

调用 do_max_length & 计算转换成给定的 internT 缓冲区会消耗的 externT 字符串长度

std::codecvt<InternT,ExternT,State>::length, 
std::codecvt<InternT,ExternT,State>::do_length
public:

int length( StateT& state,
            const ExternT* from,
            const ExternT* from_end,

            std::size_t max ) const;
(1)
protected:

virtual int do_length( StateT& state,
                       const ExternT* from,
                       const ExternT* from_end,

                       std::size_t max ) const;
(2)

1) 公开成员函数,调用最终导出类的成员函数 do_length

2) 给定初始转换状态 state ,试图转换来自 [from, from_end) 所定义的字符数组的 externT 字符,为至多 maxinternT 字符,并返回这种转换会消耗的 externT 字符数。如同以对某虚构的 [to, to+max) 输出缓冲区执行 do_in(state, from, from_end, from, to, to+max, to) 一般修改 state

返回值

假如以 do_in() 转换直至消耗所有 from_end-from 个字符,或产生 maxinternT 字符,或出现转换错误,则会消耗的 externT 字符数。

非转换特化 std::codecvt<char, char, std::mbstate_t> 返回 std::min(max, from_end-from) 。

调用示例 linux

#include <locale>
#include <string>
#include <iostream>

int main()
{
    // 窄多字节编码
    std::string s = "z\u00df\u6c34\U0001d10b";
    std::mbstate_t mb = std::mbstate_t();
    std::cout << "Only the first " <<
              std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
                  std::locale("en_US.utf8")
              ).length(mb, &s[0], &s[s.size()], 2)
              << " bytes out of " << s.size() << " would be consumed "
              " to produce the first 2 characters" << std::endl;

    return 0;
}

输出

Only the first 3 bytes out of 10 would be consumed  to produce the first 2 characters

调用示例 windows

#include <locale>
#include <iostream>
#include <vector>
#include <Windows.h>
#include <string>

std::vector<std::wstring> locals;

BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
    locals.push_back(pStr);
    return TRUE;
}

std::string stows(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

int main()
{
    EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);

    for (std::vector<std::wstring>::const_iterator str = locals.begin();
            str != locals.end(); ++str)
    {
        std::string str1 = "z\u00df\u6c34\U0001d10b";
        std::mbstate_t mbstate = std::mbstate_t();
        std::wcout << *str ;
        std::cout << "  Only the first " <<
                  std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
                      std::locale(stows(*str))
                  ).length(mbstate, &str1[0], &str1[str1.size()], 2)
                  << " bytes out of " << str1.size() << " would be consumed "
                  " to produce the first 2 characters" << std::endl;
    }

    return 0;
}

输出

de-DE_phoneb  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
es-ES_tradnl  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
hu-HU_technl  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
ja-JP_radstr  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
ka-GE_modern  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
x-IV_mathan  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-CN_phoneb  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-CN_stroke  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-HK_radstr  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-MO_radstr  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-MO_stroke  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-SG_phoneb  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-SG_stroke  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-TW_pronun  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters
zh-TW_radstr  Only the first 2 bytes out of 6 would be consumed  to produce the first 2 characters

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1601405.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

03.卸载MySQL

卸载MySQL 1.Windows卸载MySQL8 停止服务 用命令停止或者在服务中停止都可以 net stop mysql&#xff08;服务名字可以去服务里面看一下&#xff09;控制面板卸载MySQL 卸载MySQL8.0的程序可以和其他桌面应用程序一样直接在控制面板选择卸载程序&#xff0c;并在程序列表中…

nacos配置mysql(windows)

nacos默认是使用的内置数据库derby ,可通过配置修改成mysql,修改成mysql之后&#xff0c;之前配置在derby的数据会丢失 本文使用mysql版本为8.0.22 nacos版本为2.3.1 在mysql里面先创建一个数据库test(名称自定义&#xff0c;和后面配置文件里面的一样就好了) 在上面创建的数据…

【学习笔记】Python大数据处理与分析——pandas数据分析

一、pandas中的对象 1、Series对象 由两个相互关联的数组(values, index)组成&#xff0c;前者&#xff08;又称主数组&#xff09;存储数据&#xff0c;后者存储values内每个元素对应关联的标签。 import numpy as np import pandas as pds1 pd.Series([1, 3, 5, 7])print(…

数据结构排序算法

排序也称排序算法(SortAlgorithm)&#xff0c;排序是将一组数据&#xff0c;依指定的顺序进行排列的过程。 分类 内部排序【使用内存】 指将需要处理的所有数据都加载到内部存储器中进行排序插入排序 直接插入排序希尔排序 选择排序 简单选择排序堆排序 交换排序 冒泡排序快速…

【随笔】Git 高级篇 -- 远程仓库提交本地记录 git push(三十)

&#x1f48c; 所属专栏&#xff1a;【Git】 &#x1f600; 作  者&#xff1a;我是夜阑的狗&#x1f436; &#x1f680; 个人简介&#xff1a;一个正在努力学技术的CV工程师&#xff0c;专注基础和实战分享 &#xff0c;欢迎咨询&#xff01; &#x1f496; 欢迎大…

大学生前端学习第一天:了解前端

引言&#xff1a; 哈喽&#xff0c;各位大学生们&#xff0c;大家好呀&#xff0c;在本篇博客&#xff0c;我们将引入一个新的板块学习&#xff0c;那就是前端&#xff0c;关于前端&#xff0c;GPT是这样描述的&#xff1a;前端通常指的是Web开发中用户界面的部分&#xff0c;…

35. UE5 RPG制作火球术技能

接下来&#xff0c;我们将制作技能了&#xff0c;总算迈进了一大步。首先回顾一下之前是如何实现技能触发的&#xff0c;然后再进入正题。 如果想实现我之前的触发方式的&#xff0c;请看此栏目的31-33篇文章&#xff0c;讲解了实现逻辑&#xff0c;这里总结一下&#xff1a; …

用于半监督的图扩散网络 笔记

1 Title Graph Neural Diffusion Networks for Semi-supervised Learning&#xff08;Wei Ye, Zexi Huang, Yunqi Hong, and Ambuj Singh&#xff09;【2022】 2 Conclusion This paper proposes a new graph neural network called GND-Nets (for Graph Neural Diffu…

1W 6KVDC 隔离双输出 DC/DC 电源模块 ——TPJ 系列

TPJ一款有超高隔离电压的电源模块&#xff0c;主要用于医疗仪器和设备&#xff0c;特别在安全设备的应用中起着相当重要的作用&#xff0c; 它的绝缘设计完全能满足对隔离电压要求超过6000V的应用&#xff0c;在额定负载1W的情况下&#xff0c;工作温度范围为–40℃到 105℃&am…

转换为elementUI提示方法为uni-app的showToast提示

// 转换为elementUI提示方法为uni-app的showToast提示---------------------------------------- // 一般提示 Vue.prototype.$message function(title) {title && uni.showToast({icon: none,title}); }; // 成功提示 Vue.prototype.$message.success (title) > …

项目管理利器 Git

一、序言 今天聊聊 Git。 二、开发的问题 在开发项目时&#xff0c;我们的代码都是直接放在本地的机器上的。如果本地机器出现了问题&#xff0c;怎么办&#xff1f;在企业中&#xff0c;开发项目都是团队协作&#xff0c;一个团队共同维护一个项目该如何处理&#xff1f;团…

采用4G、5G实现无线视频监控,流量过大费用高,如何降低网络流量?

目录 一、高清视频监控中使用的4G和5G介绍 &#xff08;一&#xff09;4G物联网卡&#xff1a; 1、数据传输与稳定性 2、应用与优势 &#xff08;二&#xff09;5G物联网卡&#xff1a; 1、数据传输与速率 2、应用场景 二、4G/5G流量池 三、视频监控的流量使用 …

rk3588 安卓调试

rknn装上了android系统&#xff0c;用type-c usb连接上电脑&#xff0c;设备管理器发现了rk3588&#xff0c;但是Android Studio没有发现设备 后来怀疑是驱动没有安装&#xff0c;我用的驱动下载地址&#xff1a; 瑞芯微Rockchip驱动安装助手(适用于RK3308 RK3399等) Mcuzone…

SSH协议的优缺点

SSH&#xff08;Secure Shell&#xff09;是一种用于在计算机网络上进行安全远程访问和执行命令的协议。提供加密通信通道&#xff0c;防止敏感信息在传输过程中被窃听或篡改。SSH还支持文件传输和端口转发等功能&#xff0c;使其成为广泛使用的安全远程管理工具。 1. 安全远程…

Centos7下载配置jdk18与maven3.9.6【图文教程】

个人记录 进入目录 cd /usr/local/JDK下载与配置 OpenJDK官网 下载安装 wget https://download.java.net/openjdk/jdk18/ri/openjdk-1836_linux-x64_bin.tar.gz解压 tar -zxvf openjdk-1836_linux-x64_bin.tar.gz ls ls jdk-18/编辑配置文件 vim /etc/profile配置环境变…

YOLOv8 目标检测项目实操

一 yolov8 背景介绍 YOLOv8是一种尖端的、最先进的(SOTA)模型&#xff0c;建立在以前 YOLO 版本的成功基础上&#xff0c;并引入了新的特性和改进&#xff0c;以进一步提高性能和灵活性。YOLOv8被设计为快速、准确、易于使用&#xff0c;这使它成为一个很好的选择&#xff0c;…

如何在企业微信中更换新的企业主体

企业微信变更主体有什么作用&#xff1f; 做过企业运营的小伙伴都知道&#xff0c;很多时候经常会遇到现有的企业需要注销&#xff0c;切换成新的企业进行经营的情况&#xff0c;但是原来企业申请的企业微信上面却积累了很多客户&#xff0c;肯定不能直接丢弃&#xff0c;所以这…

IDM2024破解版 IDM软件破解注册序列号 idm教程 idm序列激活永久授权 Internet Download Manager网络下载加速神器

你是不是感觉下载东西资源的时候&#xff0c;下载的非常慢&#xff0c;即便是五十兆的光纤依旧慢、是不是想下载网页上的视频但不知如何进行下载……这些问题是否一直在困扰着您&#xff0c;今日小编特意我大家带来了这款IDM 2024破解版。 众所周知&#xff0c;IDM是一款功能强…

函数模板(C++)

目录 一、介绍 二、注意事项 三、排序函数 1、交换函数模板 2、排序算法 3、打印函数 4、测试函数 四、普通函数与函数模板 区别 调用规则 五、模板局限性 六、类模板 类模板与函数模板区别 1、类模板没有自动类型推导使用方式 2、类模板在模板参数列表中可以有默认…

对桥接模式的理解

目录 一、背景二、桥接模式的demo1、类型A&#xff08;形状类型&#xff09;2、类型B&#xff08;颜色类型&#xff09;3、需求&#xff1a;类型A要使用类型B&#xff08;如&#xff1a;红色的方形&#xff09;4、Spring的方式 一、背景 在《对装饰器模式的理解》中&#xff0…