-
Notifications
You must be signed in to change notification settings - Fork 274
Massive speedup to file_equal and file_to_string #1584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,26 @@ namespace cppwinrt | |
| { | ||
| inline std::string file_to_string(std::string const& filename) | ||
| { | ||
| std::ifstream file(filename, std::ios::binary); | ||
| return static_cast<std::stringstream const&>(std::stringstream() << file.rdbuf()).str(); | ||
| std::ifstream file(filename, std::ios::binary | std::ios::ate); | ||
| if (!file) { return{}; } | ||
|
|
||
| const auto stream_size = file.tellg(); | ||
| if (stream_size == std::ifstream::pos_type(-1)) | ||
| { | ||
| return {}; | ||
| } | ||
|
|
||
| file.seekg(0); | ||
|
|
||
| auto size = static_cast<std::size_t>(stream_size); | ||
| std::string result(size, '\0'); | ||
| file.read(result.data(), size); | ||
| if (!file) | ||
| { | ||
| result.resize(static_cast<std::size_t>(file.gcount())); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| template <typename T> | ||
|
|
@@ -218,18 +236,15 @@ namespace cppwinrt | |
|
|
||
| bool file_equal(std::string const& filename) const | ||
| { | ||
| if (!std::filesystem::exists(filename)) | ||
| // Non-throwing file_size returns uintmax_t(-1) on errors, which shouldn't ever be the size of m_first or m_second | ||
| std::error_code ec; | ||
| if (std::filesystem::file_size(filename, ec) != m_first.size() + m_second.size()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be ? https://en.cppreference.com/cpp/filesystem/file_size
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed, the non throwing overload returns -1 on error, which will make the comparison fail. |
||
| { | ||
| return false; | ||
| } | ||
|
|
||
| auto file = file_to_string(filename); | ||
|
|
||
| if (file.size() != m_first.size() + m_second.size()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!std::equal(m_first.begin(), m_first.end(), file.begin(), file.begin() + m_first.size())) | ||
| { | ||
| return false; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.