c++ 头文件<cwchar>中常见函数的实现!!!
来源:互联网 发布:大数据金融论坛 编辑:程序博客网 时间:2024/05/16 16:06
c++ 头文件<cwchar>中常见函数的实现!!!
废话不多说了,代码说明了一切!!!(代码封装于名字空间mystd中)
#pragma once #ifndef MYSTD_CWCHAR_H#define MYSTD_CWCHAR_H#include<cassert> // assert #include<cstddef> //std::size_t// 在vs2012中调试通过,封装于名字空间mystd中//文件建议命名为"cwchar.h"(与标准库并不冲突)#define MYSTD_BEGIN namespace mystd {#define MYSTD_END }#ifdef __cplusplusMYSTD_BEGINtypedef std::size_t size_type;typedef wchar_t char_type;inline size_type wcslen(const char_type* wcs){ assert(wcs != 0); size_type count = 0; while(*wcs++) ++count; return count;}inline char_type* wcscat(char_type* destination,const char_type *source){assert(destination != 0 && source != 0);char_type *des = destination + mystd::wcslen(destination);while(*des++ = *source++);return destination;}inline char_type* wcsncat(char_type* destination,const char_type *source,size_type num){assert(destination != 0 && source != 0);char_type *des = destination + mystd::wcslen(destination);while(num-- && *source)*des++ = *source++;*des = 0; return destination;}inline char_type* wcscpy(char_type *destination,const char_type *source){assert(destination != 0 && source != 0);char_type *des = destination;while(*des++ = *source++);return destination;}inline char_type* wcsncpy(char_type *destination,const char_type *source,size_type num){assert(destination != 0 && source != 0);char_type *des = destination;while(num--)*des++ = *source++; return destination; // 可能不包含null wide character }inline int wcscmp(const char_type *wcs1,const char_type *wcs2){assert(wcs1 != 0 && wcs2 != 0);while(*wcs1 && *wcs1 == *wcs2)++wcs1, ++wcs2;return *wcs1 - *wcs2;}inline int wcsncmp(const char_type *wcs1,const char_type *wcs2,size_type num){assert(wcs1 != 0 && wcs2 != 0);while(num-- && *wcs1 && *wcs1 == *wcs2)++wcs1, ++wcs2;if(num == size_type(-1)) // 包含了num == 0的情况return 0;elsereturn *wcs1 - *wcs2;}inline const char_type* wmemchr(const char_type* pointer,char_type val,size_type num){ assert(pointer != 0);char_type *ptr = (char_type*)pointer; for(size_type i = 0; i < num; ++i){if(*ptr == val)break;++ptr;}return ptr;} inline char_type* wmemchr(char_type* pointer,char_type val,size_type num){assert(pointer != 0);return (char_type*)wmemchr((const char_type*)pointer,val,num);}inline int wmemcmp(const char_type *ptr_1,const char_type *ptr_2,size_type num){assert(ptr_1 != 0 && ptr_2 != 0);while(num-- && *ptr_1 == *ptr_2)++ptr_1, ++ptr_2;if(num == size_type(-1))return 0;elsereturn *ptr_1 - *ptr_2;}inline char_type* wmemset(char_type *pointer,char_type val,size_type num){assert(pointer != 0);char_type *ptr = pointer;while(num--)*ptr++ = val;return pointer;}inline char_type* wmemmove(char_type *destination,const char_type *source,size_type num){assert(destination != 0 && source != 0); if(destination == source || num == 0)return destination;char_type *des = (char_type*)destination; const char_type *src = (char_type*)source; if(des < src || des >= src + num) {while(num--)*des++ = *src++;return destination;}des += num;src += num;while(num--) // 倒序复制*--des = *--src;return destination;}inline char_type* wmemcpy(char_type *destination,const char_type *source,size_type num){assert(destination != 0 && source != 0);return mystd::wmemmove(destination,source,num);}inline bool w_is_inside(const char_type *wcs,char_type val) // 辅助函数,内部使用{assert(wcs != 0);while(*wcs){if(*wcs == val)return true;else ++wcs;}return false;}inline size_type wcsspn(const char_type *wcs1,const char_type *wcs2){assert(wcs1 != 0 && wcs2 != 0);size_type count = 0;while(*wcs1 && w_is_inside(wcs2,*wcs1))++count, ++wcs1;return count;}inline size_type wcscspn(const char_type *wcs1,const char_type *wcs2){assert(wcs1 != 0 && wcs2 != 0);size_type count = 0;while(*wcs1 && !w_is_inside(wcs2,*wcs1))++count, ++wcs1;return count;}inline const char_type* wcsstr(const char_type *wcs1,const char_type *wcs2){assert(wcs1 != 0 && wcs2 != 0);size_type len_1 = mystd::wcslen(wcs1);size_type len_2 = mystd::wcslen(wcs2);if(len_1 < len_2) return 0;const char_type *search_last = wcs1 + (len_1 - len_2);while(wcs1 <= search_last){if(mystd::wcsncmp(wcs1,wcs2,len_2) == 0)return wcs1;else++wcs1;}return 0;} inline char_type* wcsstr(char_type *wcs1,const char_type *wcs2){assert(wcs1 != 0 && wcs2 != 0);return (char_type*)mystd::wcsstr((const char_type*)wcs1,wcs2);}inline const char_type* wcschr(const char_type *wcs,char_type val){assert(wcs != 0);while(*wcs && *wcs != val)++wcs;if(*wcs)return wcs;elsereturn 0;}inline char_type* wcschr(char_type *wcs,char_type val){assert(wcs != 0);return (char_type*)mystd::wcschr((const char_type*)wcs,val);}inline const char_type* wcsrchr(const char_type *wcs,char_type val){ // val可能为null wide character assert(wcs != 0);size_type len = mystd::wcslen(wcs);const char_type *ptr = wcs + len;if(val == 0)return ptr;--ptr;while(len--)if(*ptr == val)return ptr;else--ptr;return 0; //无匹配的字符}inline char_type* wcsrchr(char_type *wcs,char_type val){ //val可能为null wide character assert(wcs != 0);return (char_type*)mystd::wcsrchr((const char_type*)wcs,val); // 转调} inline const char_type* wcspbrk(const char_type *wcs1,const char_type *wcs2){assert(wcs1 != 0 && wcs2 != 0);while(*wcs1 && !w_is_inside(wcs2,*wcs1))++wcs1;if(*wcs1 == 0)return 0;elsereturn wcs1;}inline char_type* wcspbrk(char_type *wcs1,const char_type *wcs2){assert(wcs1 != 0 && wcs2 != 0);return (char_type*)mystd::wcspbrk((const char_type*)wcs1,wcs2);}MYSTD_END // end of namespace mystd #endif // __cplusplus#endif // MYSTD_CWCHAR_H
下面是一个简单的测试程序
#include<iostream>#include"cwchar.h"#include<cwchar>#define STD mystd // 改为std调用标准库版本using std::endl;using std::cout;int main(){wchar_t buf[100];STD::wmemcpy(buf,L"hello world",sizeof(buf) / sizeof(wchar_t));unsigned count = STD::wcslen(buf);for(unsigned i = 0; i < count; ++i)cout<<(char)buf[i];cout<<endl;system("pause");return 0;}
0 0
- c++ 头文件<cwchar>中常见函数的实现!!!
- c/c++常见函数及其声明的头文件
- C/C++ 常见函数头文件
- C++头文件中常见的extern "C"
- 常见的C++函数头文件
- (C语言)头文件实现的函数
- c语言常用头文件中包含的函数
- 网络编程中常见的头文件
- 关于在头文件中实现函数的情况
- C语言笔记——常见的头文件及其包含的内置函数
- c++ 头文件<cstring>(或者string.h)中的常见函数的实现!
- CWCHAR
- c语言中头文件与函数文件
- C/C++中调用外部文件的函数时如何编写头文件
- cctype头文件中定义的函数
- 头文件中函数定义的缺点
- C语言中string.h头文件中函数详解
- C中头文件的作用
- javascript表单验证
- TS码流封装(一)
- c# 连接SQLite及SQLite使用小结
- java 学习 —— AWT布局组件
- 分享一个连接
- c++ 头文件<cwchar>中常见函数的实现!!!
- 不要停止学习
- 基于双链表 实现Java Queue队列
- leetcode--Triangle
- c语言中realloc()函数解析
- 自定义Android的Dialog
- window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法
- Android:ViewGroup事件分发机制
- [ATL/WTL]_[初级]_[常用的界面对象操作]