Skip to content

Commit 5062319

Browse files
committed
Factor out header line splitting into split_headerline in utils/strfuns.hpp
1 parent a4b5793 commit 5062319

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/util/strfuns.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ std::vector<std::string> lsl::splitandtrim(
2525
}
2626
return parts;
2727
}
28+
29+
bool lsl::split_headerline(char *buf, std::size_t bufsize, std::string &type, std::string &value) {
30+
char *end = buf + bufsize, *middle_it = nullptr;
31+
32+
buf = trim_begin(buf, end);
33+
// find the end of the header line, i.e. the end of the buffer or the start of a comment
34+
for (auto it = buf; it != end; ++it) {
35+
if(!*it || *it == ';') {
36+
end = it;
37+
break;
38+
}
39+
else if(*it == ':') middle_it = it;
40+
}
41+
42+
// no separator found?
43+
if (middle_it == nullptr) return false;
44+
45+
auto *value_begin = middle_it + 1;
46+
trim(value_begin, end);
47+
48+
// convert to lowercase
49+
for (auto it = buf; it != end; ++it)
50+
if (*it >= 'A' && *it <= 'Z') *it += 'a' - 'A';
51+
52+
type.assign(buf, trim_end(buf, middle_it));
53+
value.assign(value_begin, end);
54+
55+
return true;
56+
}

src/util/strfuns.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ template <typename IteratorType> void trim(IteratorType &begin, IteratorType &en
2525
begin = trim_begin(begin, end);
2626
}
2727

28+
/// split a header line ("Foo-Bar: 512 ; some comment") into type ("foo-bar") and value ("512")
29+
bool split_headerline(char *buf, std::size_t bufsize, std::string &type, std::string &value);
30+
2831
/// return a string with whitespace at the beginning and end trimmed
2932
inline std::string trim(const std::string &input) {
3033
std::string::const_iterator begin = input.begin(), end = input.end();

testing/int/stringfuncs.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,26 @@ TEST_CASE("trim functions", "[string][basic]") {
3434
CHECK(lsl::trim(testcase) == "Hello World\t\n 123");
3535
CHECK(lsl::trim("") == "");
3636
}
37+
38+
inline bool test_split_headerline(
39+
std::string str, const char *expected_key, const char *expected_value) {
40+
std::string key, value;
41+
bool result = lsl::split_headerline(&str[0], str.size(), key, value);
42+
INFO(str);
43+
CHECK(key == expected_key);
44+
CHECK(value == expected_value);
45+
return result;
46+
}
47+
48+
TEST_CASE("split_headerline", "[string][basic]") {
49+
std::string basic[] = {{"a:b"}, {" a : b \r\n"}, {"a: b;not c"}};
50+
for(auto &str: basic)
51+
CHECK(test_split_headerline(str, "a", "b"));
52+
53+
// empty line
54+
CHECK(!test_split_headerline("", "", ""));
55+
// empty key+val
56+
CHECK(test_split_headerline(":", "", ""));
57+
// comment in key (?!)
58+
CHECK(!test_split_headerline("wha;t:??", "", ""));
59+
}

0 commit comments

Comments
 (0)