YSTest: string.hpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
string.hpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2012 - 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_STRING_HPP_
29 #define YB_INC_YSTDEX_STRING_HPP_ 1
30
31 #include "container.hpp" // for ../ydef.h, ystdex::to_array,
32 // std::underlying_type;
33 #include <libdefect/string.h> // for std::char_traits, std::initializer_list,
34 // and std::to_string;
35 #include <cstdio> // for std::vsnprintf
36 #include <cstdarg>
37
38 namespace ystdex
39 {
40
45 template<typename _tString>
46 struct string_traits
47 {
48 typedef typename std::remove_reference<_tString>::type string_type;
49 typedef typename string_type::value_type value_type;
50 typedef typename std::char_traits<value_type> traits_type;
51 typedef value_type* pointer;
52 typedef const value_type* const_pointer;
53 typedef std::initializer_list<value_type> initializer;
54 };
55
56
68 template<class _tString>
69 inline _tString&
70 ltrim(_tString&& str, typename string_traits<_tString>::const_pointer t
71 = &to_array<typename string_traits<_tString>::value_type>("\n\r\t\v ")[0])
72 {
73 return str.erase(0, str.find_first_not_of(t));
74 }
75
81 template<class _tString>
82 inline _tString&
83 rtrim(_tString&& str, typename string_traits<_tString>::const_pointer t
84 = &to_array<typename string_traits<_tString>::value_type>("\n\r\t\v ")[0])
85 {
86 return str.erase(str.find_last_not_of(t) + 1);
87 }
88
94 template<class _tString>
95 inline _tString&
96 trim(_tString&& str, typename string_traits<_tString>::const_pointer t
97 = &to_array<typename string_traits<_tString>::value_type>("\n\r\t\v ")[0])
98 {
99 return ystdex::ltrim(ystdex::rtrim(str, t));
100 }
101
109 template<typename _tString>
110 inline _tString
111 get_mid(const _tString& str, typename _tString::size_type l = 1)
112 {
113 yassume(!(str.size() < l * 2));
114
115 return str.substr(l, str.size() - l * 2);
116 }
117 template<typename _tString>
118 inline _tString
119 get_mid(const _tString& str, typename _tString::size_type l,
120 typename _tString::size_type r)
121 {
122 yassume(!(str.size() < l + r));
123
124 return str.substr(l, str.size() - l - r);
125 }
127
133 template<typename _fPred, typename _fInsert, typename _tIn>
134 _tIn
135 split(_tIn b, _tIn e, _fPred is_delim, _fInsert insert)
136 {
137 _tIn i(b);
138
139 while(b != e)
140 {
141 if(is_delim(*b) && i != b)
142 {
143 insert(i, b);
144 i = b;
145 }
146 ++b;
147 }
148 if(i != b)
149 insert(i, b);
150 return i;
151 }
157 template<typename _fPred, typename _fInsert, typename _tRange>
158 inline void
159 split(_tRange&& c, _fPred is_delim, _fInsert insert)
160 {
161 split(begin(c), end(c), is_delim, insert);
162 }
163
171 inline std::string
172 to_string(unsigned char val)
173 {
174 return std::to_string(unsigned(val));
175 }
176 inline std::string
177 to_string(unsigned short val)
178 {
179 return std::to_string(unsigned(val));
180 }
181 template<typename _type>
182 inline std::string
183 to_string(_type val, typename
184 std::enable_if<std::is_enum<_type>::value, int>::type = 0)
185 {
186 using std::to_string;
187 using ystdex::to_string;
188
189 return to_string(typename std::underlying_type<_type>::type(val));
190 }
192
193
200 template<typename _tChar>
201 std::basic_string<_tChar>
202 sfmt(const _tChar* fmt, ...)
203 {
204 std::va_list args;
205
206 va_start(args, fmt);
207
208 std::string str(static_cast<size_t>(std::vsnprintf(nullptr, 0, fmt, args)),
209 _tChar());
210
211 std::vsprintf(&str[0], fmt, args);
212 va_end(args);
213 return str;
214 }
215
221 template
222 #if defined _WIN32 && !defined __USE_MINGW_ANSI_STDIO
223 YB_ATTR(format (ms_printf, 1, 2))
224 #else
225 YB_ATTR(format (printf, 1, 2))
226 #endif
227 std::string
228 sfmt<char>(const char*, ...);
229
230 } // namespace ystdex;
231
232 #endif
233