Skip to content

Commit 0d714f9

Browse files
committed
Use a string view.
1 parent ae242ef commit 0d714f9

14 files changed

+84
-46
lines changed

WORKSPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,10 @@ git_repository(
3737
remote = "https://boringssl.googlesource.com/boringssl",
3838
shallow_since = "1566966435 +0000",
3939
)
40+
41+
http_archive(
42+
name = "absl",
43+
sha256 = "8100085dada279bf3ee00cd064d43b5f55e5d913be0dfe2906f06f8f28d5b37e",
44+
strip_prefix = "abseil-cpp-20190808",
45+
urls = ["https://github.com/abseil/abseil-cpp/archive/20190808.tar.gz"],
46+
)

src/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ cc_library(
4949
"-lm",
5050
],
5151
deps = [
52+
"@absl//absl/strings",
5253
"@httpparser",
5354
],
5455
)
@@ -90,6 +91,7 @@ cc_library(
9091
],
9192
deps = [
9293
":common",
94+
"@absl//absl/strings",
9395
"@libev",
9496
],
9597
linkopts = [

src/apib_cpu_proc.cc

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
#include <cstring>
2222
#include <fstream>
2323

24+
#include "absl/strings/numbers.h"
2425
#include "src/apib_cpu.h"
2526
#include "src/apib_lines.h"
2627
#include "src/apib_time.h"
@@ -116,13 +117,21 @@ double cpu_GetMemoryUsage() {
116117
const auto v = line.nextToken(" ");
117118

118119
if ("MemTotal:" == n) {
119-
totalMem = std::stol(v);
120+
if (!absl::SimpleAtoi(v, &totalMem)) {
121+
return 0.0;
122+
}
120123
} else if ("MemFree:" == n) {
121-
freeMem = std::stol(v);
124+
if (!absl::SimpleAtoi(v, &freeMem)) {
125+
return 0.0;
126+
}
122127
} else if ("Buffers:" == n) {
123-
buffers = std::stol(v);
128+
if (!absl::SimpleAtoi(v, &buffers)) {
129+
return 0.0;
130+
}
124131
} else if ("Cached:" == n) {
125-
cache = std::stol(v);
132+
if (!absl::SimpleAtoi(v, &cache)) {
133+
return 0.0;
134+
}
126135
}
127136
}
128137

@@ -153,7 +162,7 @@ static int getTicks(CPUUsage* cpu) {
153162
int64_t idleCount = 0LL;
154163
int64_t nonIdleCount = 0LL;
155164
int i = 0;
156-
std::string tok;
165+
absl::string_view tok;
157166

158167
line.nextToken(" \t");
159168
do {
@@ -164,9 +173,13 @@ static int getTicks(CPUUsage* cpu) {
164173
We consider both to be idle CPU.
165174
The eigth is "steal", which is time lost to virtualization
166175
as a client -- that's idle two in our estimation */
167-
idleCount += std::stoll(tok);
176+
if (!absl::SimpleAtoi(tok, &idleCount)) {
177+
return 0;
178+
}
168179
} else {
169-
nonIdleCount += std::stoll(tok);
180+
if (!absl::SimpleAtoi(tok, &nonIdleCount)) {
181+
return 0;
182+
}
170183
}
171184
i++;
172185
}

src/apib_lines.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ bool LineState::next() {
120120
return true;
121121
}
122122

123-
std::string LineState::line() {
123+
absl::string_view LineState::line() {
124124
if (!lineComplete_) {
125125
return "";
126126
}
127-
return std::string(buf_ + lineStart_);
127+
return absl::string_view(buf_ + lineStart_);
128128
}
129129

130-
std::string LineState::nextToken(const std::string& toks) {
130+
absl::string_view LineState::nextToken(const std::string& toks) {
131131
if (!lineComplete_) {
132132
return "";
133133
}
@@ -147,7 +147,7 @@ std::string LineState::nextToken(const std::string& toks) {
147147
}
148148
}
149149

150-
return std::string(buf_ + tokStart_);
150+
return absl::string_view(buf_ + tokStart_);
151151
}
152152

153153
void LineState::skipMatches(const std::string& toks) {

src/apib_lines.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ limitations under the License.
2121
#include <ostream>
2222
#include <string>
2323

24+
#include "absl/strings/string_view.h"
25+
2426
namespace apib {
2527

2628
/*
@@ -49,11 +51,11 @@ class LineState {
4951
* present. */
5052
bool next();
5153
/* If NextLine returned non-zero, return a pointer to the entire line */
52-
std::string line();
54+
absl::string_view line();
5355
/* If NextLine returned non-zero, return the next token delimited by "toks"
5456
* like strtok. If "toks" is zn empty string, return everything until the end
5557
* of the current line. */
56-
std::string nextToken(const std::string& toks);
58+
absl::string_view nextToken(const std::string& toks);
5759
/* Move the token position to skip anything that's not whitespace */
5860
void skipMatches(const std::string& toks);
5961
/* Move any data remaining in the line to the start. Used if we didn't

src/apib_message.cc

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
#include <iostream>
2121
#include <regex>
2222

23+
#include "absl/strings/numbers.h"
2324
#include "src/apib_util.h"
2425

2526
using std::cerr;
@@ -74,9 +75,10 @@ int HttpMessage::parseStatus(LineState* buf) {
7475
return 1;
7576
}
7677

77-
const std::string line = buf->line();
78+
const auto line = buf->line();
7879
std::smatch matches;
79-
if (!std::regex_match(line, matches, statusLineRegex)) {
80+
const std::string lines(line);
81+
if (!std::regex_match(lines, matches, statusLineRegex)) {
8082
return -1;
8183
}
8284
assert(matches.size() == kStatusLineParts);
@@ -94,9 +96,10 @@ int HttpMessage::parseRequestLine(LineState* buf) {
9496
return 1;
9597
}
9698

97-
const std::string line = buf->line();
99+
const auto line = buf->line();
98100
std::smatch matches;
99-
if (!std::regex_match(line, matches, requestLineRegex)) {
101+
const std::string lines(line);
102+
if (!std::regex_match(lines, matches, requestLineRegex)) {
100103
return -1;
101104
}
102105
assert(matches.size() == kRequestLineParts);
@@ -128,10 +131,13 @@ void HttpMessage::finishHeaders() {
128131
}
129132
}
130133

131-
void HttpMessage::examineHeader(const std::string& name,
132-
const std::string& value) {
134+
void HttpMessage::examineHeader(const absl::string_view name,
135+
const absl::string_view value) {
133136
if (eqcase("Content-Length", name)) {
134-
contentLength = std::stol(value);
137+
int32_t newLength;
138+
if (absl::SimpleAtoi(value, &newLength)) {
139+
contentLength = newLength;
140+
}
135141
} else if (eqcase("Transfer-Encoding", name)) {
136142
chunked = eqcase("chunked", value) ? 1 : 0;
137143
} else if (eqcase("Connection", name)) {
@@ -144,7 +150,7 @@ int HttpMessage::parseHeaderLine(LineState* buf) {
144150
return 1;
145151
}
146152

147-
const std::string hl = buf->line();
153+
const auto hl = buf->line();
148154
if (hl.empty()) {
149155
// Empty line -- means end of headers!
150156
finishHeaders();
@@ -165,9 +171,9 @@ int HttpMessage::parseHeaderLine(LineState* buf) {
165171
examineHeader(matches[1], matches[3]);
166172
*/
167173

168-
const std::string name = buf->nextToken(":");
174+
const auto name = buf->nextToken(":");
169175
buf->skipMatches(" \t");
170-
const std::string value = buf->nextToken("");
176+
const auto value = buf->nextToken("");
171177
examineHeader(name, value);
172178

173179
return 0;
@@ -205,7 +211,7 @@ int HttpMessage::parseChunkHeader(LineState* buf) {
205211
}
206212

207213
// TODO regular expression for extra chunk header stuff like encoding!
208-
const long len = std::stol(buf->line(), 0, 16);
214+
const long len = std::stol(std::string(buf->line()), 0, 16);
209215
if (len == 0) {
210216
chunkState_ = kChunkEnd;
211217
} else {
@@ -256,7 +262,7 @@ int HttpMessage::parseTrailerLine(LineState* buf) {
256262
return 1;
257263
}
258264

259-
const std::string hl = buf->line();
265+
const auto hl = buf->line();
260266
if (hl.empty()) {
261267
// Empty line -- means end of everything!
262268
state = kMessageDone;
@@ -268,7 +274,8 @@ int HttpMessage::parseTrailerLine(LineState* buf) {
268274
}
269275

270276
std::smatch matches;
271-
if (!std::regex_match(hl, matches, headerLineRegex)) {
277+
const std::string hls(hl);
278+
if (!std::regex_match(hls, matches, headerLineRegex)) {
272279
cerr << "Invalid trailer line: \"" << hl << '\"' << endl;
273280
return -2;
274281
}

src/apib_message.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
#include <cstdint>
2121
#include <string>
2222

23+
#include "absl/strings/string_view.h"
2324
#include "src/apib_lines.h"
2425

2526
namespace apib {
@@ -90,7 +91,8 @@ class HttpMessage {
9091
int parseRequestLine(LineState* buf);
9192
void finishHeaders();
9293
int parseHeaderLine(LineState* buf);
93-
void examineHeader(const std::string& name, const std::string& value);
94+
void examineHeader(const absl::string_view name,
95+
const absl::string_view value);
9496
int parseLengthBody(LineState* buf);
9597
int parseChunkHeader(LineState* buf);
9698
int parseChunkBody(LineState* buf);

src/apib_mon.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ namespace apib {
4949

5050
MonServerConnection::MonServerConnection(int fd) : fd_(fd) {}
5151

52-
size_t MonServerConnection::sendBack(const std::string& msg) {
52+
size_t MonServerConnection::sendBack(const absl::string_view msg) {
5353
return write(fd_, msg.data(), msg.size());
5454
}
5555

56-
bool MonServerConnection::processCommand(const std::string& cmd,
56+
bool MonServerConnection::processCommand(const absl::string_view cmd,
5757
CPUUsage* lastUsage) {
5858
if (eqcase(cmd, "HELLO")) {
5959
sendBack("Hi!\n");

src/apib_mon.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
#include <string>
2222
#include <thread>
2323

24+
#include "absl/strings/string_view.h"
2425
#include "ev.h"
2526
#include "src/apib_cpu.h"
2627

@@ -49,8 +50,8 @@ class MonServerConnection {
4950
public:
5051
MonServerConnection(int fd);
5152
void socketLoop();
52-
bool processCommand(const std::string& cmd, CPUUsage* lastUsage);
53-
size_t sendBack(const std::string& msg);
53+
bool processCommand(const absl::string_view msg, CPUUsage* lastUsage);
54+
size_t sendBack(const absl::string_view msg);
5455

5556
private:
5657
int fd_;

src/apib_url.cc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ URLInfo::~URLInfo() {
7272
}
7373
}
7474

75-
int URLInfo::initHost(const std::string& hostname) {
75+
int URLInfo::initHost(const absl::string_view hostname) {
7676
struct addrinfo hints;
7777
struct addrinfo* results;
7878

@@ -83,7 +83,8 @@ int URLInfo::initHost(const std::string& hostname) {
8383
hints.ai_protocol = 0;
8484
hints.ai_flags = 0;
8585

86-
const int addrerr = getaddrinfo(hostname.c_str(), NULL, &hints, &results);
86+
std::string tmpHost(hostname);
87+
const int addrerr = getaddrinfo(tmpHost.c_str(), NULL, &hints, &results);
8788
if (addrerr) {
8889
return -1;
8990
}
@@ -104,12 +105,12 @@ int URLInfo::initHost(const std::string& hostname) {
104105
return 0;
105106
}
106107

107-
static std::string urlPart(const struct http_parser_url* pu,
108-
const std::string& urlstr, int part) {
108+
static absl::string_view urlPart(const struct http_parser_url* pu,
109+
const absl::string_view urlstr, int part) {
109110
return urlstr.substr(pu->field_data[part].off, pu->field_data[part].len);
110111
}
111112

112-
int URLInfo::init(const std::string& urlstr) {
113+
int URLInfo::init(const absl::string_view urlstr) {
113114
struct http_parser_url pu;
114115

115116
http_parser_url_init(&pu);
@@ -133,7 +134,7 @@ int URLInfo::init(const std::string& urlstr) {
133134
return -3;
134135
}
135136

136-
hostName_ = urlPart(&pu, urlstr, UF_HOST);
137+
hostName_ = std::string(urlPart(&pu, urlstr, UF_HOST));
137138

138139
if (pu.field_set & (1 << UF_PORT)) {
139140
port_ = pu.port;
@@ -148,7 +149,7 @@ int URLInfo::init(const std::string& urlstr) {
148149
if (pu.field_set & (1 << UF_PATH)) {
149150
const auto p = urlPart(&pu, urlstr, UF_PATH);
150151
path << p;
151-
pathOnly_ = p;
152+
pathOnly_ = std::string(p);
152153
} else {
153154
path << '/';
154155
pathOnly_ = "/";
@@ -157,7 +158,7 @@ int URLInfo::init(const std::string& urlstr) {
157158
if (pu.field_set & (1 << UF_QUERY)) {
158159
const auto q = urlPart(&pu, urlstr, UF_QUERY);
159160
path << '?' << q;
160-
query_ = q;
161+
query_ = std::string(q);
161162
}
162163

163164
if (pu.field_set & (1 << UF_FRAGMENT)) {

src/apib_url.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ limitations under the License.
2424
#include <string>
2525
#include <vector>
2626

27+
#include "absl/strings/string_view.h"
2728
#include "src/apib_rand.h"
2829

2930
namespace apib {
@@ -91,8 +92,8 @@ class URLInfo {
9192
size_t addressCount() const { return addresses_.size(); }
9293

9394
private:
94-
int init(const std::string& urlStr);
95-
int initHost(const std::string& hostName);
95+
int init(const absl::string_view urlStr);
96+
int initHost(const absl::string_view hostName);
9697

9798
std::vector<struct sockaddr*> addresses_;
9899
std::vector<socklen_t> addressLengths_;

src/apib_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ limitations under the License.
2121

2222
namespace apib {
2323

24-
bool eqcase(const std::string& s1, const std::string& s2) {
24+
bool eqcase(const absl::string_view s1, const absl::string_view s2) {
2525
if (s1.size() != s2.size()) {
2626
return false;
2727
}

src/apib_util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ limitations under the License.
2121
#include <iostream>
2222
#include <string>
2323

24+
#include "absl/strings/string_view.h"
25+
2426
namespace apib {
2527

2628
#define mandatoryAssert(e) \
@@ -30,7 +32,7 @@ namespace apib {
3032
abort(); \
3133
}
3234

33-
extern bool eqcase(const std::string& s1, const std::string& s2);
35+
extern bool eqcase(const absl::string_view s1, const absl::string_view s2);
3436

3537
} // namespace apib
3638

0 commit comments

Comments
 (0)