---恢复内容开始---
(1)首先了解下字符集
ansi, unicode/utf-16,utf-8,utf-32
1字节 2字节 1-4字节 4字节
(2)微软的字符数据类型 wchar_t,由编译器开关/Zc:wchar_t 决定开启,默认开启,wchar_t t =L"t";
微软的开发团队还在 winNT.h中定义了 CHAR WCHAR等数据类型
TCHAR WinNT.h中
#ifdef UNICODE // r_winnt
#ifndef _TCHAR_DEFINED
typedef WCHAR TCHAR, *PTCHAR;
typedef WCHAR TBYTE , *PTBYTE ;
#define _TCHAR_DEFINED
#endif /* !_TCHAR_DEFINED */
typedef LPWSTR LPTCH, PTCH;
typedef LPWSTR PTSTR, LPTSTR;
typedef LPCWSTR PCTSTR, LPCTSTR;
typedef LPWSTR LP;
#define __TEXT(quote) L##quote // r_winnt
#else /* UNICODE */ // r_winnt
#ifndef _TCHAR_DEFINED
typedef char TCHAR, *PTCHAR;
typedef unsigned char TBYTE , *PTBYTE ;
#define _TCHAR_DEFINED
#endif /* !_TCHAR_DEFINED */
typedef LPSTR LPTCH, PTCH;
typedef LPSTR PTSTR, LPTSTR;
typedef LPCSTR PCTSTR, LPCTSTR;
#define __TEXT(quote) quote // r_winnt
#endif
现在TCHAR就变成了通用字符了,__TEXT 也变成了通用宏
(3)window中的UNICODE和ANSI函数
除去原来为支持win16的函数,所有windows函数都会提供两个版本,类似*W的unicode版本和类似*A的ansi版本,但是所有ansi版本都是通过unicode版本实现的。
(4)c运行库中的unicode 版本和 ansi版本单独实现 String.h中
#ifdef _UNICODE
#define _tcslen wcslen
#else
#define _tcslen strlen
#endif
*所以UNICODE和 _UNICODE要同时定义咯
(5)新的字符串安全函数
errno_t __cdecl wcscpy_s(_Out_z_cap_(_DstSize) wchar_t * _Dst, _In_ rsize_t _DstSize, _In_z_ const wchar_t * _Src);
__countof()返回数组中元素的个数,stdlib.h中
如果未指定处理函数,errno_t 并不返回,而是弹出assert对话框,如果想返回处理流程是这样的
先定义一个处理函数原型为void InvalidParameterHandler(const wchar_t* expression,
const wchar_t* function,
const wchar_t* file,
unsigned int line,
uintptr_t pReserved);参数分别是调用失败描述,函数名,文件名,行,列(debug模式下有效)
_set_invalid_parameter_handler注册处理函数
_CrtSetReportMode(_CRT_ASSERT,0).禁用c运行时弹出的assert对话框。
(6)c运行库添加的新函数 ,提供更多的控制
HRESULT StringCchCat(
LPTSTR pszDest, size_t cchDest, LPCTSTR pszSrc ); Cch _coutof 获取字符数 如果是Cb则使用sizeof获取
(7) ShlwApi中提供了大量好用的字符串处理函数 StrFomatKbSize StrFormatByteSize等
总结
- 将文本想像为字符的数组
- 用通用字符TCHAR/PTHRAR表示字符和字符串
- 用TEXT _T宏来表示字符,且统一风格
- 修改与字符串有关的运算 #define chmalloc(ncount) malloc(ncout*sizeof(TCHAR))
- 避免使用printf系列函数
- UNICODE _UNICODE同时指定
字符串处理函数
- 始终使用带_s或StringCch前缀的函数,后者控制截断
- 使用/GS /RTCs来自动检测缓冲区溢出
- 比较字符串文件名,路径等使用CompareStringOrdinal,用户字符串使用CompareString
(8)字符串转化 MutiByteToWideByte 和 WideByteToMutiByte
(9)关于动态库
void StringReverseA();
void StringReverseW();
#ifdefi UNICODE
typedef StringReverse StringReverseW;
#else
typedef StringReverse StringReverseA;
#endif
(10)判断文本是 ansi 或者 unicode IsTextUnicode,该函数并不精准