-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings.hpp
80 lines (76 loc) · 3 KB
/
Strings.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
#pragma once
#include <string>
#include <algorithm>
#include <cctype>
#include <cstring>
// C++ std::string functions
inline std::string tolower(std::string s) { std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))std::tolower); return s; }
inline void tolower_inplace(std::string& s) { std::transform(s.begin(), s.end(), s.begin(), (int(*)(int))std::tolower); }
inline std::string trim(const std::string& s)
{
size_t i = 0, j = s.size() - 1;
while (i <= j && std::isspace(s[i])) { ++i; }
while (i <= j && std::isspace(s[j])) { --j; }
return s.substr(i, j+1-i);
}
inline void trim_inplace(std::string& s)
{
size_t i = 0, j = s.size() - 1;
while (i <= j && std::isspace(s[i])) { ++i; }
while (i <= j && std::isspace(s[j])) { --j; }
s.erase(j+1).erase(0, i);
}
inline ssize_t read_int(const std::string& s)
{
if (s.size() == 0) { throw std::invalid_argument("Error: invalid integer"); }
size_t i = 0;
bool negative = s[0] == '-';
if (negative || s[0] == '+') { if (s.size() == 1) { throw std::invalid_argument("Error: invalid integer"); } i = 1; }
ssize_t _x = 0;
do
{
if (!std::isdigit(s[i])) { throw std::invalid_argument("Error: invalid integer"); }
int64_t temp = ((int64_t)_x) * 10 + (s[i++] - '0');
if (temp > INT_MAX) { throw std::invalid_argument("Error: invalid integer"); } // overflowed
_x = (int)temp;
}
while (i < s.size());
return negative ? -_x : _x;
}
// C-style string functions
inline char *trim(char *s) { while (std::isspace(*s)) { ++s; } size_t l = strlen(s); while (l && std::isspace(s[l - 1])) { s[--l] = 0; } return s; }
inline char *ltrim(char *s) { while (std::isspace(*s)) { ++s; } return s; }
inline const char *ltrim(const char *s) { while (std::isspace(*s)) { ++s; } return s; }
inline char *strchr(char *s, const char *cs) { while (*s) { const char *c = cs; while (*c) { if (*s == *c) { return s; } ++c; } ++s; } return nullptr; }
inline const char *strchr(const char *s, const char *cs) { while (*s) { const char *c = cs; while (*c) { if (*s == *c) { return s; } ++c; } ++s; } return nullptr; }
inline bool streq(const char *a, const char *b) { return std::strcmp(a, b) == 0; }
inline bool strieq(const char *a, const char *b)
{
while (*a && *b && std::tolower(*a) == std::tolower(*b)) { ++a; ++b; }
return *a == 0 && *b == 0;
}
inline bool str_read_int(const char *s, char **endptr, ssize_t *x)
{
bool negative = *s == '-';
if (negative || *s == '+') { ++s; }
if (!std::isdigit(*s)) { return false; }
ssize_t _x = 0;
do
{
int64_t temp = ((int64_t)_x) * 10 + (*s++ - '0');
if (temp > INT_MAX) { return false; } // overflowed
_x = (int)temp;
}
while (std::isdigit(*s));
*x = (negative ? -_x : _x);
*endptr = (char*)s;
return true;
}
inline bool str_skip_int(const char *s, char **endptr)
{
if (*s == '-' || *s == '+') { ++s; }
if (!std::isdigit(*s)) { return false; }
while (std::isdigit(*++s));
*endptr = (char*)s;
return true;
}