-
Notifications
You must be signed in to change notification settings - Fork 526
/
string_util.h
165 lines (144 loc) · 4.61 KB
/
string_util.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (c) 2020 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Project: curve
* Created Date: Tuesday September 4th 2018
* Author: hzsunjianliang
*/
// Copyright (c) 2015, Baidu.com, Inc. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: [email protected]
#ifndef SRC_COMMON_STRING_UTIL_H_
#define SRC_COMMON_STRING_UTIL_H_
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include <glog/logging.h>
#include <string>
#include <vector>
#include <stdexcept>
namespace curve {
namespace common {
inline void SplitString(const std::string& full,
const std::string& delim,
std::vector<std::string>* result) {
result->clear();
if (full.empty()) {
return;
}
std::string tmp;
std::string::size_type pos_begin = full.find_first_not_of(delim);
std::string::size_type comma_pos = 0;
while (pos_begin != std::string::npos) {
comma_pos = full.find(delim, pos_begin);
if (comma_pos != std::string::npos) {
tmp = full.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + delim.length();
} else {
tmp = full.substr(pos_begin);
pos_begin = comma_pos;
}
if (!tmp.empty()) {
result->push_back(tmp);
tmp.clear();
}
}
}
inline bool StringToUl(const std::string &value, uint32_t *out) noexcept {
try {
*out = std::stoul(value);
return true;
} catch (std::invalid_argument &e) {
LOG(ERROR) << "decode string:{" << value << "} to number err:"
<< e.what();
return false;
} catch (std::out_of_range &e) {
LOG(ERROR) << "decode string:{" << value << "} to number err:"
<< e.what();
return false;
}
}
inline bool StringToUll(const std::string &value, uint64_t *out) noexcept {
try {
*out = std::stoull(value);
return true;
} catch (std::invalid_argument &e) {
LOG(ERROR) << "decode string:{" << value << "} to number err:"
<< e.what();
return false;
} catch (std::out_of_range &e) {
LOG(ERROR) << "decode string:{" << value << "} to number err:"
<< e.what();
return false;
}
}
inline bool StringToInt(const std::string &value, int32_t *out) noexcept {
try {
*out = std::stoi(value);
return true;
} catch (std::invalid_argument &e) {
LOG(ERROR) << "decode string:{" << value << "} to number err:"
<< e.what();
return false;
} catch (std::out_of_range &e) {
LOG(ERROR) << "decode string:{" << value << "} to number err:"
<< e.what();
return false;
}
}
inline bool StringStartWith(const std::string& value,
const std::string& starting) {
return value.rfind(starting, 0) == 0;
}
inline bool StringEndsWith(const std::string& value,
const std::string& ending) {
if (ending.size() > value.size())
return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
inline bool StringToTime(const std::string& value, uint64_t* expireTime) {
*expireTime = 0;
auto length = value.length();
if (0 == length) {
return false;
}
uint64_t base;
switch (value[length - 1]) {
case 's': base = 1; break;
case 'm': base = 60; break;
case 'h': base = 3600; break;
case 'd': base = 24 * 3600; break;
case 'M': base = 30 * 24 * 3600; break;
case 'y': base = 365 * 24 * 3600; break;
default: base = 0;
}
uint64_t num;
if (0 == base || !StringToUll(value.substr(0, length - 1), &num)) {
return false;
}
*expireTime = num * base;
return true;
}
inline std::string ToHexString(void* p) {
std::ostringstream oss;
oss << p;
return oss.str();
}
} // namespace common
} // namespace curve
#endif // SRC_COMMON_STRING_UTIL_H_