Debloatify some stuff

This commit is contained in:
Thomas Goyne 2014-12-26 11:00:23 -08:00
parent 76afcdafa1
commit 010f6c4f79
8 changed files with 72 additions and 43 deletions

View File

@ -21,9 +21,7 @@
#include <libaegisub/util.h>
#include <algorithm>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/range/adaptor/filtered.hpp>
namespace agi {
Time::Time(int time) : time(util::mid(0, time, 10 * 60 * 60 * 1000 - 1)) { }
@ -31,7 +29,7 @@ Time::Time(int time) : time(util::mid(0, time, 10 * 60 * 60 * 1000 - 1)) { }
Time::Time(std::string const& text) {
int after_decimal = -1;
int current = 0;
for (char c : text | boost::adaptors::filtered(boost::is_any_of(",.0123456789:"))) {
for (char c : text) {
if (c == ':') {
time = time * 60 + current;
current = 0;
@ -41,6 +39,8 @@ Time::Time(std::string const& text) {
current = 0;
after_decimal = 100;
}
else if (c < '0' || c > '9')
continue;
else if (after_decimal < 0) {
current *= 10;
current += c - '0';
@ -80,21 +80,21 @@ int Time::GetTimeSeconds() const { return (time % 60000) / 1000; }
int Time::GetTimeMiliseconds() const { return (time % 1000); }
int Time::GetTimeCentiseconds() const { return (time % 1000) / 10; }
SmpteFormatter::SmpteFormatter(vfr::Framerate fps, std::string sep)
SmpteFormatter::SmpteFormatter(vfr::Framerate fps, char sep)
: fps(std::move(fps))
, sep(std::move(sep))
, sep(sep)
{
}
std::string SmpteFormatter::ToSMPTE(Time time) const {
int h=0, m=0, s=0, f=0;
fps.SmpteAtTime(time, &h, &m, &s, &f);
return format("%02d%s%02d%s%02d%s%02d", h, sep, m, sep, s, sep, f);
return format("%02d%c%02d%c%02d%c%02d", h, sep, m, sep, s, sep, f);
}
Time SmpteFormatter::FromSMPTE(std::string const& str) const {
std::vector<std::string> toks;
boost::split(toks, str, boost::is_any_of(sep));
boost::split(toks, str, [=](char c) { return c == sep; });
if (toks.size() != 4) return 0;
int h, m, s, f;

View File

@ -228,6 +228,63 @@ bool formatter<Char>::parse_next() {
return true;
}
template<typename Char>
Char formatter<Char>::next_format() {
this->pending = false;
if (this->width < 0) {
this->out.fill(' ');
this->out.setf(std::ios::left, std::ios::adjustfield);
this->width = -this->width;
}
this->out.width(this->width);
this->out.precision(this->precision < 0 ? 6 : this->precision);
Char c = *this->fmt_cur ? this->fmt_cur[0] : 's';
if (c >= 'A' && c <= 'Z') {
this->out.setf(std::ios::uppercase);
c += 'a' - 'A';
}
switch (c) {
case 'c':
this->out.setf(std::ios::dec, std::ios::basefield);
break;
case 'd': case 'i':
this->out.setf(std::ios::dec, std::ios::basefield);
break;
case 'o':
this->out.setf(std::ios::oct, std::ios::basefield);
break;
case 'x':
this->out.setf(std::ios::hex, std::ios::basefield);
break;
case 'u':
this->out.setf(std::ios::dec, std::ios::basefield);
break;
case 'e':
this->out.setf(std::ios::scientific, std::ios::floatfield);
this->out.setf(std::ios::dec, std::ios::basefield);
break;
case 'f':
this->out.setf(std::ios::fixed, std::ios::floatfield);
break;
case 'g':
this->out.setf(std::ios::dec, std::ios::basefield);
this->out.flags(this->out.flags() & ~std::ios::floatfield);
break;
case 'p':
this->out.setf(std::ios::hex, std::ios::basefield);
break;
default: // s and other
this->out.setf(std::ios::boolalpha);
break;
}
this->fmt = *this->fmt_cur ? this->fmt_cur + 1 : this->fmt_cur;
return c;
}
template<typename Char>
formatter<Char>::~formatter() {
// Write remaining formatting string

View File

@ -27,10 +27,10 @@ class SmpteFormatter {
/// Frame rate to use
vfr::Framerate fps;
/// Separator character
std::string sep;
char sep;
public:
SmpteFormatter(vfr::Framerate fps, std::string sep=":");
SmpteFormatter(vfr::Framerate fps, char sep=':');
/// Convert an Time to a SMPTE timecode
std::string ToSMPTE(Time time) const;

View File

@ -94,6 +94,7 @@ class formatter : formatter_state<Char> {
boost::io::basic_ios_all_saver<Char> saver;
bool parse_next();
Char next_format();
public:
formatter(std::basic_ostream<Char>& out, const Char *fmt)
@ -115,68 +116,41 @@ public:
this->read_precision = false;
return;
}
this->pending = false;
if (this->width < 0) {
this->out.fill(' ');
this->out.setf(std::ios::left, std::ios::adjustfield);
this->width = -this->width;
}
this->out.width(this->width);
this->out.precision(this->precision < 0 ? 6 : this->precision);
Char c = *this->fmt_cur ? this->fmt_cur[0] : 's';
if (c >= 'A' && c <= 'Z') {
this->out.setf(std::ios::uppercase);
c += 'a' - 'A';
}
Char c = next_format();
switch (c) {
case 'c':
this->out.setf(std::ios::dec, std::ios::basefield);
this->out << runtime_cast<Char>(value);
break;
case 'd': case 'i':
this->out.setf(std::ios::dec, std::ios::basefield);
this->out << runtime_cast<intmax_t>(value);
break;
case 'o':
this->out.setf(std::ios::oct, std::ios::basefield);
this->out << runtime_cast<intmax_t>(value);
break;
case 'x':
this->out.setf(std::ios::hex, std::ios::basefield);
this->out << runtime_cast<intmax_t>(value);
break;
case 'u':
this->out.setf(std::ios::dec, std::ios::basefield);
this->out << runtime_cast<uintmax_t>(value);
break;
case 'e':
this->out.setf(std::ios::scientific, std::ios::floatfield);
this->out.setf(std::ios::dec, std::ios::basefield);
this->out << runtime_cast<double>(value);
break;
case 'f':
this->out.setf(std::ios::fixed, std::ios::floatfield);
this->out << runtime_cast<double>(value);
break;
case 'g':
this->out.setf(std::ios::dec, std::ios::basefield);
this->out.flags(this->out.flags() & ~std::ios::floatfield);
this->out << runtime_cast<double>(value);
break;
case 'p':
this->out.setf(std::ios::hex, std::ios::basefield);
this->out << runtime_cast<const void *>(value);
break;
default: // s and other
this->out.setf(std::ios::boolalpha);
writer<Char, typename std::decay<T>::type>::write(this->out, this->precision, value);
break;
}
this->fmt = *this->fmt_cur ? this->fmt_cur + 1 : this->fmt_cur;
}
};

View File

@ -129,7 +129,7 @@ void AssKaraoke::ParseSyllables(AssDialogue *line, Syllable &syl) {
else {
std::string& otext = syl.ovr_tags[syl.text.size()];
// Merge adjacent override tags
boost::trim_right_if(text, boost::is_any_of("}"));
boost::trim_right_if(text, [](char c) { return c == '}'; });
if (!in_tag)
otext += "{";

View File

@ -45,7 +45,6 @@
#include <libaegisub/scoped_ptr.h>
#include <ctime>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <functional>
@ -322,7 +321,7 @@ void DoCheck(bool interactive) {
if (line.empty()) continue;
std::vector<std::string> parsed;
boost::split(parsed, line, boost::is_any_of("|"));
boost::split(parsed, line, [](char c) { return c == '|'; });
if (parsed.size() != 6) continue;
if (atoi(parsed[1].c_str()) <= GetSVNRevision())

View File

@ -65,7 +65,7 @@ void EncoreSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& fi
// Encore wants ; for NTSC and : for PAL
// The manual suggests no other frame rates are supported
agi::SmpteFormatter ft(fps, fps.NeedsDropFrames() ? ";" : ":");
agi::SmpteFormatter ft(fps, fps.NeedsDropFrames() ? ';' : ':');
// Write lines
int i = 0;

View File

@ -41,7 +41,6 @@
#include <libaegisub/make_unique.h>
#include <libaegisub/util.h>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/filesystem/path.hpp>
@ -107,7 +106,7 @@ std::unique_ptr<VideoProvider> CreateDummyVideoProvider(agi::fs::path const& fil
std::vector<std::string> toks;
auto const& fields = filename.string().substr(7);
boost::split(toks, fields, boost::is_any_of(":"));
boost::split(toks, fields, [](char c) { return c == ':'; });
if (toks.size() != 8)
throw VideoOpenError("Too few fields in dummy video parameter list");