Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Don't trim when splitting POST body.

Kishore Nallan6dc2df237655
diff --git a/include/string_utils.h b/include/string_utils.h
index 89592ad3..3472b582 100644
--- a/include/string_utils.h
+++ b/include/string_utils.h
@@ -23,8 +23,9 @@ struct StringUtils {
// Adapted from: http://stackoverflow.com/a/236180/131050
static size_t split(const std::string& s, std::vector<std::string> & result, const std::string& delim,
- const bool keep_empty = false, const size_t start_index = 0,
- const size_t max_values = (std::numeric_limits<size_t>::max()-1)) {
+ const bool keep_empty = false, const bool trim_space = true,
+ const size_t start_index = 0,
+ const size_t max_values = std::numeric_limits<size_t>::max()) {
if (delim.empty()) {
result.push_back(s);
return s.size();
@@ -38,7 +39,9 @@ struct StringUtils {
std::string temp(substart, subend);
end_index += temp.size() + delim.size();
- temp = trim(temp);
+ if(trim_space) {
+ temp = trim(temp);
+ }
if (keep_empty || !temp.empty()) {
result.push_back(temp);
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 7086ffed..fc817a97 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -788,7 +788,7 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::shar
//LOG(INFO) << "req body %: " << (float(req->body_index)/req->body.size())*100;
std::vector<std::string> json_lines;
- StringUtils::split(req->body, json_lines, "\n", false);
+ StringUtils::split(req->body, json_lines, "\n", false, false);
//LOG(INFO) << "json_lines.size before: " << json_lines.size() << ", req->body_index: " << req->body_index;
diff --git a/test/string_utils_test.cpp b/test/string_utils_test.cpp
index 8f296b6f..ae5d7617 100644
--- a/test/string_utils_test.cpp
+++ b/test/string_utils_test.cpp
@@ -51,13 +51,13 @@ TEST(StringUtilsTest, ShouldSplitString) {
// restrict list of max_values
std::vector<std::string> lines_limited;
- size_t end_index = StringUtils::split("a b c d e f", lines_limited, " ", false, 0, 3);
+ size_t end_index = StringUtils::split("a b c d e f", lines_limited, " ", false, true, 0, 3);
ASSERT_EQ(3, lines_limited.size());
ASSERT_EQ(6, end_index);
// start from an arbitrary position in string
std::vector<std::string> lines_custom_start;
- end_index = StringUtils::split("a b c d e f", lines_custom_start, " ", false, 2, 100);
+ end_index = StringUtils::split("a b c d e f", lines_custom_start, " ", false, true, 2, 100);
ASSERT_EQ(5, lines_custom_start.size());
ASSERT_EQ(11, end_index);
@@ -66,6 +66,14 @@ TEST(StringUtilsTest, ShouldSplitString) {
StringUtils::split(comma_and_space, comma_space_parts, ",");
ASSERT_STREQ("foo", comma_space_parts[0].c_str());
ASSERT_STREQ("bar", comma_space_parts[1].c_str());
+
+ // preserve trailing space
+ std::string str_trailing_space = "foo\nbar ";
+ std::vector<std::string> trailing_space_parts;
+ StringUtils::split(str_trailing_space, trailing_space_parts, "\n", false, false);
+ ASSERT_EQ(2, trailing_space_parts.size());
+ ASSERT_EQ("foo", trailing_space_parts[0]);
+ ASSERT_EQ("bar ", trailing_space_parts[1]);
}
TEST(StringUtilsTest, ShouldTrimString) {