-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve: strip sstream which is extremely heavy & update dependencies (…
…#1)
- Loading branch information
1 parent
fdaf610
commit cb77683
Showing
5 changed files
with
24 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,39 @@ | ||
#include "properties.h" | ||
|
||
#include <utility> | ||
#include <sstream> | ||
|
||
static std::string strip(const std::string &input) { | ||
auto start_it = input.begin(); | ||
auto end_it = input.rbegin(); | ||
while (std::isspace(*start_it)) | ||
++start_it; | ||
while (std::isspace(*end_it)) | ||
++end_it; | ||
static std::string strip(const std::string_view &input) { | ||
auto start_it = std::find_if_not(input.begin(), input.end(), std::isspace); | ||
auto end_it = std::find_if_not(input.rbegin(), input.rend(), std::isspace); | ||
return std::string(start_it, end_it.base()); | ||
} | ||
|
||
Properties::Properties(std::unordered_map<std::string, std::string> const &properties) { | ||
this->properties = properties; | ||
Properties::Properties(std::unordered_map<std::string, std::string> properties) : | ||
properties(std::move(properties)) { | ||
} | ||
|
||
std::string Properties::get(const std::string &key) const { | ||
auto value = properties.find(key); | ||
if (value == properties.end()) { | ||
if (auto value = properties.find(key); value == properties.end()) { | ||
return ""; | ||
} else { | ||
return value->second; | ||
} | ||
return value->second; | ||
} | ||
|
||
Properties *Properties::load(Chunk *chunk) { | ||
std::string text = std::string(static_cast<char *>(chunk->getData()), chunk->getLength()); | ||
std::istringstream ss{text}; | ||
std::string_view text{static_cast<char *>(chunk->getData()), | ||
static_cast<size_t>(chunk->getLength())}; | ||
|
||
std::unordered_map<std::string, std::string> properties; | ||
std::string line; | ||
|
||
while (std::getline(ss, line)) { | ||
size_t split = line.find('='); | ||
if (split == std::string::npos) { | ||
continue; | ||
for (size_t start = 0, end; start != std::string::npos; start = end) { | ||
end = text.find_first_of('\n', start + 1); | ||
if (end != std::string::npos) { | ||
auto line = text.substr(start, end); | ||
if (size_t split = line.find('='); split != std::string::npos) { | ||
properties[strip(line.substr(0, split))] = strip(line.substr(split + 1)); | ||
} | ||
} | ||
|
||
properties[strip(line.substr(0, split))] = strip(line.substr(split + 1)); | ||
} | ||
|
||
return new Properties(properties); | ||
return new Properties(std::move(properties)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters