-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringutil.h
111 lines (85 loc) · 1.69 KB
/
stringutil.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
#ifndef __SCI_STRINGUTIL_H_
#define __SCI_STRINGUTIL_H_
#include <string>
#include <ostream>
template<class V>
void split( V& v, const std::string& s, const std::string& d, bool collapse = false )
{
if( s.empty() )
{
return;
}
size_t oldpos = 0;
size_t pos = 0;
do
{
pos = s.find(d,oldpos);
if( !collapse || (pos-oldpos) > 0 )
{
std::string part = s.substr(oldpos,pos-oldpos);
v.insert(v.end(),part);
}
oldpos = pos + d.size();
} while ( pos != std::string::npos );
}
void do_memdump( std::ostream& o, const void* data, size_t len, uint64_t offset )
{
const unsigned char* ptr = static_cast<const unsigned char*>(data);
for( size_t i = 0; i < len; i += 16 )
{
o << std::noshowbase;
o << std::setw(8);
o << std::setfill('0');
o << std::hex << i+offset << ' ';
size_t to = std::min(len,i+16);
for( size_t j = i; j < to; ++j )
{
o << ' ';
o << std::setw(2);
o << std::setfill('0');
o << std::hex;
o << (unsigned)ptr[j];
if( (j+1) % 8 == 0 )
{
o << ' ';
}
}
o << " ";
for( size_t j = to; j < i+16; ++j )
{
o << " ";
if( (j+1) % 8 == 0 )
{
o << ' ';
}
}
for( size_t j = i; j < to; ++j )
{
const unsigned char c = ptr[j];
if(isprint(c) && c != '\n' && c != '\t' )
{
o << ptr[j];
}
else
{
o << '.';
}
if( (j+1) % 8 == 0 )
{
o << ' ';
}
}
o << '\n';
}
o << std::dec;
}
void memdump( std::ostream& o, const void* data, size_t len, const void* offset )
{
do_memdump(o,data,len,(uint64_t)offset);
}
void memdump( std::ostream& o, const void* data, size_t len )
{
do_memdump(o,data,len,0);
}
#endif // __SCI_STRINGUTIL_H_
// vim: tabstop=4 shiftwidth=4 noexpandtab