Speed up TextFileWriter a bit
Write the text and the newline separately rather than appending the newline to the text to eliminate a copy of the text.
This commit is contained in:
parent
4366ece827
commit
7a5c92aaee
2 changed files with 25 additions and 13 deletions
|
@ -30,14 +30,23 @@
|
||||||
#include <boost/algorithm/string/predicate.hpp>
|
#include <boost/algorithm/string/predicate.hpp>
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define NEWLINE "\r\n"
|
||||||
|
#else
|
||||||
|
#define NEWLINE "\n"
|
||||||
|
#endif
|
||||||
|
|
||||||
TextFileWriter::TextFileWriter(agi::fs::path const& filename, std::string encoding)
|
TextFileWriter::TextFileWriter(agi::fs::path const& filename, std::string encoding)
|
||||||
: file(new agi::io::Save(filename, true))
|
: file(new agi::io::Save(filename, true))
|
||||||
, ostr(file->Get())
|
, ostr(file->Get())
|
||||||
|
, newline(NEWLINE)
|
||||||
{
|
{
|
||||||
if (encoding.empty())
|
if (encoding.empty())
|
||||||
encoding = OPT_GET("App/Save Charset")->GetString();
|
encoding = OPT_GET("App/Save Charset")->GetString();
|
||||||
if (boost::iequals(encoding, "utf-8"))
|
if (boost::iequals(encoding, "utf-8")) {
|
||||||
conv = agi::make_unique<agi::charset::IconvWrapper>("utf-8", encoding.c_str(), true);
|
conv = agi::make_unique<agi::charset::IconvWrapper>("utf-8", encoding.c_str(), true);
|
||||||
|
newline = conv->Convert(newline);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Write the BOM
|
// Write the BOM
|
||||||
|
@ -48,7 +57,10 @@ TextFileWriter::TextFileWriter(agi::fs::path const& filename, std::string encodi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TextFileWriter::TextFileWriter(std::ostream &ostr) : ostr(ostr) {
|
TextFileWriter::TextFileWriter(std::ostream &ostr)
|
||||||
|
: ostr(ostr)
|
||||||
|
, newline(NEWLINE)
|
||||||
|
{
|
||||||
WriteLineToFile("\xEF\xBB\xBF", false);
|
WriteLineToFile("\xEF\xBB\xBF", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,15 +68,14 @@ TextFileWriter::~TextFileWriter() {
|
||||||
// Explicit empty destructor required with a unique_ptr to an incomplete class
|
// Explicit empty destructor required with a unique_ptr to an incomplete class
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextFileWriter::WriteLineToFile(std::string line, bool addLineBreak) {
|
void TextFileWriter::WriteLineToFile(std::string const& line, bool addLineBreak) {
|
||||||
if (addLineBreak)
|
if (conv) {
|
||||||
#ifdef _WIN32
|
auto converted = conv->Convert(line);
|
||||||
line += "\r\n";
|
ostr.write(converted.data(), converted.size());
|
||||||
#else
|
}
|
||||||
line += "\n";
|
else
|
||||||
#endif
|
ostr.write(line.data(), line.size());
|
||||||
|
|
||||||
if (conv)
|
if (addLineBreak)
|
||||||
line = conv->Convert(line);
|
ostr.write(newline.data(), newline.size());
|
||||||
ostr.write(line.data(), line.size());
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,11 +33,12 @@ class TextFileWriter {
|
||||||
std::unique_ptr<agi::io::Save> file;
|
std::unique_ptr<agi::io::Save> file;
|
||||||
std::unique_ptr<agi::charset::IconvWrapper> conv;
|
std::unique_ptr<agi::charset::IconvWrapper> conv;
|
||||||
std::ostream &ostr;
|
std::ostream &ostr;
|
||||||
|
std::string newline;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TextFileWriter(agi::fs::path const& filename, std::string encoding="");
|
TextFileWriter(agi::fs::path const& filename, std::string encoding="");
|
||||||
TextFileWriter(std::ostream &ostr);
|
TextFileWriter(std::ostream &ostr);
|
||||||
~TextFileWriter();
|
~TextFileWriter();
|
||||||
|
|
||||||
void WriteLineToFile(std::string line, bool addLineBreak=true);
|
void WriteLineToFile(std::string const& line, bool addLineBreak=true);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue