forked from kbinani/libvsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtil.hpp
More file actions
241 lines (223 loc) · 7.49 KB
/
StringUtil.hpp
File metadata and controls
241 lines (223 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* StringUtil.h
* Copyright © 2012 kbinani
*
* This file is part of libvsq.
*
* libvsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* libvsq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef __StringUtil_h__
#define __StringUtil_h__
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <iomanip>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <functional>
#include <cctype>
using namespace std;
/**
* @brief 文字列関連のユーティリティ
*/
class StringUtil
{
public:
/**
* @brief parseInt メソッドが投げる例外
*/
class IntegerParseException : public std::exception{
};
/**
* @brief parseFloat メソッドが投げる例外
*/
class FloatParseException : public std::exception{
};
public:
/**
* 文字列を区切り文字で分割する
* @param delimiter 区切り文字
* @param text 文字列
* @param limit 区切る回数の最大値
* @return 区切られた文字列のリスト
*/
static vector<string> explode( string delimiter, string text, string::size_type limit = string::npos, string escape = "" )
{
vector<string> result;
string::size_type searchFrom = 0;
string::size_type delimiterIndex = getDelimiterIndex( text, delimiter, escape, searchFrom );
while( delimiterIndex != string::npos ){
string token = text.substr( searchFrom, delimiterIndex - searchFrom );
result.push_back( token );
searchFrom = delimiterIndex + delimiter.length();
if( result.size() + 1 == limit ){
break;
}
delimiterIndex = getDelimiterIndex( text, delimiter, escape, searchFrom );
}
result.push_back( text.substr( searchFrom ) );
return result;
}
/**
* 含まれる文字列を全て置換する
* @param text 処理対象の文字列
* @param search 検索する文字列
* @param replace 置換する文字列
* @return 置換後の文字列
*/
static string replace( string text, string search, string replace )
{
if( search == replace ){
return text;
}
string result = text;
string::size_type index = result.find( search, 0 );
int searchLength = search.length();
int replaceLength = replace.length();
while( string::npos != index ){
result.replace( index, searchLength, replace );
index = result.find( search, index - searchLength + replaceLength + 1 );
}
return result;
}
/**
* @brief 文字列を整数に変換する
* @param text 変換する文字列
* @return 変換後の数値
*/
template<typename T>
static T parseInt( const std::string &text, int baseNumber = 10 )throw( IntegerParseException ){
if( baseNumber == 10 ){
try{
return boost::lexical_cast<T>( text );
}catch( boost::bad_lexical_cast &e ){
throw IntegerParseException();
}
}else{
char *endptr;
T result = (T)strtol( text.c_str(), &endptr, baseNumber );
if( *endptr != '\0' ){
throw IntegerParseException();
}else{
return result;
}
}
}
/**
* @brief 文字列を浮動小数点数に変換する
* @param text 変換する文字列
* @return 変換後の数値
*/
template<typename T>
static T parseFloat( const std::string &text ){
try{
return boost::lexical_cast<T>( text );
}catch( boost::bad_lexical_cast & ){
throw FloatParseException();
}
}
/**
* @brief 整数を文字列に変換する
* @param value 変換する数値
* @return 変換後の文字列
*/
static std::string toString( int value, int baseNumber = 10 ){
ostringstream oss;
oss << uppercase << setbase( baseNumber ) << value;
return oss.str();
}
/**
* @brief 数値を文字列に変換する
* @param value 変換する数値
* @param format 変換時のフォーマット
* @return 変換後の文字列
*/
template<typename T>
static string toString( T value, const string &format ){
return (boost::format( format ) % value).str();
}
/**
* @brief アルファベットの大文字を小文字に変換する
* @param value 変換する文字列
* @return 結果文字列
*/
static std::string toLower( std::string value ){
std::string result = value;
transform( result.begin(), result.end(), result.begin(), _toLower );
return result;
}
/**
* @brief 文字列を指定回数繰り返した文字列を取得する
* @param value 繰り返す文字列
* @param count 繰り返す回数
* @return 結果文字列
*/
static std::string repeat( const std::string &value, int count ){
ostringstream result;
for( int i = 0; i < count; i++ ){
result << value;
}
return result.str();
}
/**
* @brief Trim leading and trailing blank characters.
* @param value Source string.
*/
static std::string trim(const std::string &s){
std::string::const_iterator left
= std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(::isspace)));
std::string::const_reverse_iterator right
= std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(::isspace)));
return (left < right.base()) ? std::string(left, right.base()) : std::string();
}
private:
StringUtil()
{
}
/**
* @brief 文字列から、エスケープ文字を考慮してデリミタ文字列の位置を探す
* @param text 文字列
* @param delimiter デリミタ(区切り)文字
* @param escape エスケープ文字
* @param searchFrom 検索開始インデックス
* @return デリミタ文字列の位置。見つからなければ string::npos を返す
*/
static string::size_type getDelimiterIndex( string &text, string &delimiter, string &escape, string::size_type searchFrom )
{
if( escape.length() == 0 ){
return (int)text.find( delimiter, searchFrom );
}else{
while( searchFrom < text.length() ){
int draft = (int)text.find( delimiter, searchFrom );
if( draft == string::npos ){
return string::npos;
}
int expectedEscapeIndex = draft - escape.length();
if( 0 <= expectedEscapeIndex ){
// エスケープ文字が存在する可能性がある
if( text.substr( expectedEscapeIndex, escape.length() ) == escape ){
// エスケープ文字だった場合
searchFrom = draft + 1;
}else{
return draft;
}
}else{
// エスケープ文字が存在し得ない
return draft;
}
}
return string::npos;
}
}
static char _toLower( char c ){
return tolower( c );
}
};
#endif