-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtext_tools.hpp
92 lines (80 loc) · 2.54 KB
/
text_tools.hpp
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
#ifndef TEXT_TOOLS_HPP
#define TEXT_TOOLS_HPP
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdexcept>
#include <sstream>
#include <vector>
#include <cstdint>
using namespace std;
namespace hui{
class TextTools{
public:
static double string2double(string str){
return string2double(str.c_str());
}
static double string2double(const char str[]){
stringstream ss(str);
double y;
ss >> y;
if (ss.fail()){
throw runtime_error("Conversion to double type failed!\n");
}
return y;
}
static long string2long(string str){
return string2long(str.c_str());
}
static long string2long(const char str[]){
stringstream ss(str);
long y;
ss >> y;
if (ss.fail()){
throw runtime_error("Conversion to double type failed!\n");
}
return y;
}
static long string2uint64_t(const char str[]){
stringstream ss(str);
uint64_t y;
ss >> y;
if (ss.fail()){
throw runtime_error("Conversion to double type failed!\n");
}
return y;
}
static long string2uint64_t(string str){
return string2uint64_t(str.c_str());
}
static string long2string(long x){
ostringstream ss;
ss << x;
return ss.str();
}
static vector<string> split(const string &str, char delim){
stringstream ss(str);
string item;
vector<string> tokens;
while(std::getline(ss, item, delim)) {
tokens.push_back(item);
}
return tokens;
}
static vector<string> splitByWhiteSpace(const string &str){
stringstream ss(str);
string item;
string token;
vector<string> tokens;
while(std::getline(ss, item, '\t')) {
stringstream iss(item);
while(std::getline(iss, token, ' ')) {
tokens.push_back(token);
}
}
return tokens;
}
};
}
#endif