YSTest: cstring.h 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
cstring.h
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2009 - 2013.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_YSTDEX_CSTRING_H_
29 #define YB_INC_YSTDEX_CSTRING_H_ 1
30
31 #include "../ydef.h"
32 #include <cstdlib>
33 #include <type_traits>
34 #include <cstring>
35 #include <string> // for std::char_traits;
36 #include <cctype>
37
38 namespace ystdex
39 {
40
41 //非 ISO C/C++ 扩展库函数。
42 //
43
48 YB_API YB_PURE size_t
49 strlen_n(const char*);
50
57 YB_API char*
58 strcpy_n(char*, const char*);
59
66 YB_API char*
67 strcpycat(char*, const char*, const char*);
68
76 YB_API char*
77 strcatdup(const char*, const char*, void*(*)(size_t) = std::malloc);
78
79
84 template<typename _tChar>
85 yconstexpr YB_STATELESS bool
86 is_null(_tChar c)
87 {
88 return std::char_traits<_tChar>::eq(c, _tChar());
89 }
90
91
107 template<typename _tChar>
108 YB_PURE size_t
109 ntctslen(const _tChar* s)
110 {
111 yconstraint(s);
112
113 const _tChar* p(s);
114
115 while(!ystdex::is_null(*p))
116 ++p;
117 return p - s;
118 }
119
127 template<typename _tChar>
128 YB_PURE typename std::char_traits<_tChar>::int_type
129 ntctscmp(const _tChar* s1, const _tChar* s2)
130 {
131 yconstraint(s1),
132 yconstraint(s2);
133
134 typename std::char_traits<_tChar>::int_type d(0);
135
136 while(!ystdex::is_null(d = *s1 - *s2))
137 yunseq(++s1, ++s2);
138 return d;
139 }
140
148 template<typename _tChar>
149 YB_PURE typename std::char_traits<_tChar>::int_type
150 ntctsicmp(const _tChar* s1, const _tChar* s2)
151 {
152 yconstraint(s1),
153 yconstraint(s2);
154
155 typename std::char_traits<_tChar>::int_type d(0);
156
157 while(!ystdex::is_null(d = std::tolower(*s1) - std::tolower(*s2)))
158 yunseq(++s1, ++s2);
159 return d;
160 }
161
162
170 template<typename _tChar>
171 yconstexpr YB_PURE size_t
172 const_ntctslen(const _tChar* s)
173 {
174 return ystdex::is_null(*s) ? 0 : ystdex::const_ntctslen(s + 1) + 1;
175 }
176
183 template<typename _tChar>
184 yconstexpr YB_PURE size_t
185 const_ntctscnt(const _tChar* s, _tChar c)
186 {
187 return ystdex::is_null(*s) ? 0 : ystdex::const_ntctscnt(s + 1, c)
188 + std::char_traits<_tChar>::eq(*s, c);
189 }
190
198 template<typename _tChar>
199 yconstexpr YB_PURE typename std::char_traits<_tChar>::int_type
200 const_ntctscmp(const _tChar* s1, const _tChar* s2)
201 {
202 return !std::char_traits<_tChar>::eq(*s1, *s2) || ystdex::is_null(*s1)
203 || ystdex::is_null(*s2) ? *s1 - *s2
204 : ystdex::const_ntctscmp(s1 + 1, s2 + 1);
205 }
213 template<typename _tChar>
214 yconstexpr YB_PURE typename std::char_traits<_tChar>::int_type
215 const_ntctscmp(const _tChar* s1, const _tChar* s2, size_t n)
216 {
217 return n == 0 ? _tChar() : (!std::char_traits<_tChar>::eq(*s1, *s2)
218 || ystdex::is_null(*s1) || ystdex::is_null(*s2) ? *s1 - *s2
219 : ystdex::const_ntctscmp(s1 + 1, s2 + 1, n - 1));
220 }
221
229 template<typename _tChar>
230 yconstexpr YB_PURE size_t
231 const_ntctschr(const _tChar* s, _tChar c)
232 {
233 return ystdex::is_null(*s) || std::char_traits<_tChar>::eq(*s, c)
234 ? 0 : ystdex::const_ntctschr(s + 1, c) + 1;
235 }
236
244 template<typename _tChar>
245 yconstexpr YB_PURE size_t
246 const_ntctschrn(const _tChar* s, _tChar c, size_t n)
247 {
248 return n == 0 || ystdex::is_null(*s) ? 0 : (std::char_traits<_tChar>
249 ::eq(*s, c) ? ystdex::const_ntctschrn(s + 1, c, n - 1) + (n != 1)
250 : ystdex::const_ntctschrn(s + 1, c, n) + 1);
251 }
252
260 template<typename _tChar>
261 yconstexpr YB_PURE size_t
262 const_ntctsstr(const _tChar* s1, const _tChar* s2)
263 {
264 return ystdex::is_null(*s1) ? 0 : (ystdex::const_ntctscmp(s1, s2,
265 ystdex::const_ntctslen(s2)) == _tChar()? 0
266 : ystdex::const_ntctsstr(s1 + 1, s2) + 1);
267 }
268
269 } // namespace ystdex;
270
271 #endif
272