forked from PlatONnetwork/pWASM-abigen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtil.cpp
More file actions
42 lines (39 loc) · 1.29 KB
/
StringUtil.cpp
File metadata and controls
42 lines (39 loc) · 1.29 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
//
// Created by zhou.yang on 2018/10/10.
//
#include "StringUtil.h"
namespace platon {
std::string randomString(int size) {
const static std::string alph = "0123456789abcdefghigklmnopqrstuvwxyz";
std::string str;
str.resize(size);
srand((unsigned)time(NULL));
for (size_t i = 0; i < size; ++i) {
str[i] = alph[rand() % size];
}
return str;
}
int split( const std::string & srcStr, std::vector<std::string> & destArray, const std::string & delimiter ){
if( srcStr.empty() ){
return 0;
}
std::string::size_type startPos = srcStr.find_first_not_of( delimiter );
size_t lengthOfDelimiter = delimiter.length();
while( std::string::npos != startPos ){
std::string::size_type nextPos = srcStr.find( delimiter, startPos );
std::string str;
if( std::string::npos != nextPos ){
str = srcStr.substr( startPos, nextPos - startPos );
nextPos += lengthOfDelimiter;
}
else{
str = srcStr.substr( startPos );
}
startPos = nextPos;
if( !str.empty() ){
destArray.push_back( str );
}
}
return destArray.size();
}
}