-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.h
135 lines (110 loc) · 3.07 KB
/
helper.h
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
//
// Created by Junyi Hou on 2/3/21.
//
#ifndef JUICER_HELPER_H
#define JUICER_HELPER_H
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <locale>
enum class ResultType {
NO_SUCH_PROBLEM,
SYSTEM_ERROR,
COMPILE_ERROR,
ACCEPTED,
WRONG_ANSWER,
PRESENTATION_ERROR,
RUNTIME_ERROR,
TIME_LIMIT_EXCEED,
MEMORY_LIMIT_EXCEED,
OUTPUT_LIMIT_EXCEED,
NON_ZERO_EXIT_ERROR,
SEGMENTATION_FAULT,
FLOAT_POINT_EXCEPTION, // Divide by 0
VALIDATE_ERROR,
};
class ResultException : public std::exception {
public:
explicit ResultException(ResultType type, const std::string &details) {
result_type = type;
error_message = details;
}
ResultType what_type() {
return result_type;
}
const char *what() const noexcept override {
return error_message.c_str();
}
private:
ResultType result_type;
std::string error_message;
};
class NotImplementedException : public std::exception {
public:
explicit NotImplementedException(const char *error = "Function not yet implemented.") {
errorMessage = error;
}
const char *what() const noexcept override {
return errorMessage.c_str();
}
private:
std::string errorMessage;
};
namespace JuicerHelper {
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
ltrim(s);
return s;
}
// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
rtrim(s);
return s;
}
// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
trim(s);
return s;
}
static std::string read_file(const std::string &file_path) {
std::ifstream file(file_path, std::ios::binary);
std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
return str;
}
static void write_file(const std::string &file_path, const std::string &content) {
std::ofstream file(file_path, std::ios::binary | std::ios::trunc);
file << content;
file.close();
}
static std::vector<std::string> split(std::string &str) {
std::stringstream ss(str);
std::vector<std::string> result;
while (ss.good()) {
std::string substr;
getline(ss, substr, ',');
result.push_back(substr);
}
return result;
}
};
#endif //JUICER_HELPER_H