-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmstring.cpp
More file actions
101 lines (85 loc) · 2.32 KB
/
mstring.cpp
File metadata and controls
101 lines (85 loc) · 2.32 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
#include <iostream>
#include <string>
#include "mstring.hpp"
#include "mutil.h"
MString::MString()
{}
MString::MString(const char *s)
{
std::string in(s);
m_string = in;
}
MString::MString(std::string s) : m_string(s)
{}
MString::MString(MString &source)
{
m_string = source.str();
}
MString& MString::operator=(const MString& source)
{
m_string = source.m_string;
return *this;
}
MString::~MString()
{}
std::string MString::str(void)
{
return m_string;
}
std::vector<std::string> MString::split(std::string on)
{
std::vector<std::string> splitlist;
std::string::size_type current_pos = 0;
while (1) {
std::string::size_type pos = m_string.find(on, current_pos);
if (pos == std::string::npos) {
// Didn't find it
splitlist.push_back(m_string.substr(current_pos));
break;
} else if (pos == 0) {
splitlist.push_back(std::string(""));
} else {
splitlist.push_back(m_string.substr(current_pos, pos-current_pos));
}
current_pos = pos + on.size();
}
return splitlist;
}
std::string MString::tohex(void) {
char *c_hex = ::tohex((unsigned char*)m_string.c_str(),
m_string.length());
std::string hex(c_hex);
free(c_hex);
return hex;
}
std::ostream& operator<<(std::ostream& os, const MString& me) {
os << me.m_string;
return os;
}
// General functions
bool is_shell_metachar(unsigned char c) {
// Common shell metacharacters
const std::string metacharacters = ",|&;()<>{}[]$`'\"\\*?~!#^ \t\n/";
return metacharacters.find(c) != std::string::npos;
}
std::string
sane_elem(std::string& insane)
{
std::string sane = insane;
// Convert to lower case.
std::transform(sane.begin(), sane.end(), sane.begin(),
[](unsigned char c){ return ::tolower(c); });
// Convert any consecutive spaces to an underscore.
std::transform(sane.begin(), sane.end(), sane.begin(),
[](unsigned char c){ return c == ' ' ? '_' : c; });
// Screen out any unwanted characters.
sane.erase(
std::remove_if(sane.begin(), sane.end(),
[](unsigned char c){ return c > 127; }),
sane.end());
sane.erase(
std::remove_if(sane.begin(), sane.end(), is_shell_metachar),
sane.end()
);
return sane;
}