-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_deserialization.hpp
More file actions
100 lines (87 loc) · 2 KB
/
string_deserialization.hpp
File metadata and controls
100 lines (87 loc) · 2 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
#ifndef __CPPCODE_COMMON_STRING_DESERIALIZATION__
#define __CPPCODE_COMMON_STRING_DESERIALIZATION__
#include <iostream>
#include <cstdint>
namespace cppcode { namespace common {
std::vector<std::string> string_deserialize0(const std::string& str)
{
std::vector<std::string> deStr;
std::string tmpStr;
size_t len = str.size();
size_t pos = 0;
while (pos < len)
{
if ((len - pos) >= 4)
{
std::string segLen = str.substr(pos, 4);
size_t n = std::atoi(segLen.data());
pos += 4;
if (n == 9999)
{
tmpStr += str.substr(pos, 9998);
pos += 9998;
}
else
{
tmpStr += str.substr(pos, n);
deStr.push_back(std::move(tmpStr));
tmpStr.clear();
pos += n;
}
}
else
{
// meet invalid data, abort deserializing string.
break;
}
}
return deStr;
}
std::vector<std::string> string_deserialize1(const std::string& str)
{
std::vector<std::string> deStr;
if (str.empty())
{
return deStr;
}
std::ostringstream oss;
auto c = str.begin();
while (1)
{
if (c != str.end())
{
if (*c == '|')
{
if ((c + 1) == str.end())
{
break;
}
else if (*(c + 1) == '|')
{
// skip char
oss << *c;
c++;
}
else
{
// save string
deStr.push_back(oss.str());
oss.str(std::string());
}
}
else
{
oss << *c;
}
c++;
}
else
{
break;
}
}
deStr.push_back(oss.str());
return deStr;
}
}}
#endif