Skip to content

Commit d441287

Browse files
Avoid static std::string
Replace static std::string objects with constexpr character arrays and use compile-time string length calculations.
1 parent 37399af commit d441287

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

httplib.h

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,10 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) {
20502050
callback(static_cast<time_t>(sec), static_cast<time_t>(usec));
20512051
}
20522052

2053+
template <size_t N> inline constexpr size_t str_len(const char (&)[N]) {
2054+
return N - 1;
2055+
}
2056+
20532057
inline bool is_numeric(const std::string &str) {
20542058
return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit);
20552059
}
@@ -2209,9 +2213,9 @@ inline const char *status_message(int status) {
22092213

22102214
inline std::string get_bearer_token_auth(const Request &req) {
22112215
if (req.has_header("Authorization")) {
2212-
static std::string BearerHeaderPrefix = "Bearer ";
2216+
constexpr auto bearer_header_prefix_len = detail::str_len("Bearer ");
22132217
return req.get_header_value("Authorization")
2214-
.substr(BearerHeaderPrefix.length());
2218+
.substr(bearer_header_prefix_len);
22152219
}
22162220
return "";
22172221
}
@@ -4637,10 +4641,8 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider,
46374641
}
46384642
}
46394643

4640-
static const std::string done_marker("0\r\n");
4641-
if (!write_data(strm, done_marker.data(), done_marker.size())) {
4642-
ok = false;
4643-
}
4644+
constexpr const char done_marker[] = "0\r\n";
4645+
if (!write_data(strm, done_marker, str_len(done_marker))) { ok = false; }
46444646

46454647
// Trailer
46464648
if (trailer) {
@@ -4652,8 +4654,8 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider,
46524654
}
46534655
}
46544656

4655-
static const std::string crlf("\r\n");
4656-
if (!write_data(strm, crlf.data(), crlf.size())) { ok = false; }
4657+
constexpr const char crlf[] = "\r\n";
4658+
if (!write_data(strm, crlf, str_len(crlf))) { ok = false; }
46574659
};
46584660

46594661
data_sink.done = [&](void) { done_with_trailer(nullptr); };
@@ -4895,11 +4897,11 @@ class MultipartFormDataParser {
48954897
return false;
48964898
}
48974899

4898-
static const std::string header_content_type = "Content-Type:";
4900+
constexpr const char header_content_type[] = "Content-Type:";
48994901

49004902
if (start_with_case_ignore(header, header_content_type)) {
49014903
file_.content_type =
4902-
trim_copy(header.substr(header_content_type.size()));
4904+
trim_copy(header.substr(str_len(header_content_type)));
49034905
} else {
49044906
static const std::regex re_content_disposition(
49054907
R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~",
@@ -4996,10 +4998,10 @@ class MultipartFormDataParser {
49964998
file_.content_type.clear();
49974999
}
49985000

4999-
bool start_with_case_ignore(const std::string &a,
5000-
const std::string &b) const {
5001-
if (a.size() < b.size()) { return false; }
5002-
for (size_t i = 0; i < b.size(); i++) {
5001+
bool start_with_case_ignore(const std::string &a, const char *b) const {
5002+
const auto b_len = strlen(b);
5003+
if (a.size() < b_len) { return false; }
5004+
for (size_t i = 0; i < b_len; i++) {
50035005
if (case_ignore::to_lower(a[i]) != case_ignore::to_lower(b[i])) {
50045006
return false;
50055007
}
@@ -5086,7 +5088,7 @@ class MultipartFormDataParser {
50865088
};
50875089

50885090
inline std::string random_string(size_t length) {
5089-
static const char data[] =
5091+
constexpr const char data[] =
50905092
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
50915093

50925094
// std::random_device might actually be deterministic on some
@@ -6085,7 +6087,7 @@ inline time_t BufferStream::duration() const { return 0; }
60856087
inline const std::string &BufferStream::get_buffer() const { return buffer; }
60866088

60876089
inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) {
6088-
static constexpr char marker[] = "/:";
6090+
constexpr const char marker[] = "/:";
60896091

60906092
// One past the last ending position of a path param substring
60916093
std::size_t last_param_end = 0;
@@ -6106,7 +6108,7 @@ inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) {
61066108
static_fragments_.push_back(
61076109
pattern.substr(last_param_end, marker_pos - last_param_end + 1));
61086110

6109-
const auto param_name_start = marker_pos + 2;
6111+
const auto param_name_start = marker_pos + str_len(marker);
61106112

61116113
auto sep_pos = pattern.find(separator, param_name_start);
61126114
if (sep_pos == std::string::npos) { sep_pos = pattern.length(); }

0 commit comments

Comments
 (0)