Skip to content

Commit

Permalink
Use string_view all over the place and rip out more boost stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
plorkyeran authored and CoffeeFlux committed Nov 24, 2023
1 parent 163bb27 commit 36bfd29
Show file tree
Hide file tree
Showing 36 changed files with 251 additions and 230 deletions.
19 changes: 8 additions & 11 deletions libaegisub/common/charset_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

/// @file charset_conv.cpp
/// @brief Wrapper for libiconv to present a more C++-friendly API
/// @ingroup libaegisub
#include <libaegisub/charset_conv.h>

#include <cerrno>
#include <cstdint>
#include <libaegisub/string.h>
#include "charset_6937.h"

#include <cassert>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/range/algorithm.hpp>

#include <libaegisub/charset_conv.h>
#include <cassert>
#include <cerrno>
#include <cstdint>
#include <iconv.h>

#include "charset_6937.h"

// Check if we can use advanced fallback capabilities added in GNU's iconv
// implementation
#if !defined(_LIBICONV_VERSION) || _LIBICONV_VERSION < 0x010A || defined(LIBICONV_PLUG)
Expand Down Expand Up @@ -267,7 +264,7 @@ Iconv::Iconv(const char *source, const char *dest)
: cd(iconv_open(dest, source))
{
if (cd == iconv_invalid)
throw UnsupportedConversion(std::string("Cannot convert from ") + source + " to " + dest);
throw UnsupportedConversion(agi::Str("Cannot convert from ", source, " to ", dest));
}

Iconv::~Iconv() {
Expand Down
61 changes: 61 additions & 0 deletions libaegisub/include/libaegisub/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2022, Thomas Goyne <[email protected]>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#include <string>

namespace agi {
namespace detail {
inline void AppendStr(std::string& target, std::initializer_list<std::string_view> strs) {
size_t size = 0;
for (auto str : strs)
size += str.size();
target.reserve(target.size() + size);
for (auto str : strs)
target.append(str);
}
}

template<typename Range>
void AppendJoin(std::string& str, std::string_view sep, Range const& range)
{
auto begin = std::begin(range);
auto end = std::end(range);
if (begin == end) return;

str += *begin++;
while (begin != end) {
str += sep;
str += *begin++;
}
}

template<typename Range>
std::string Join(std::string_view sep, Range const& range) {
std::string str;
AppendJoin(str, sep, range);
return str;
}

template<typename... Args>
std::string Str(Args&&... args) {
std::string str;
detail::AppendStr(str, {args...});
return str;
}

template<typename... Args>
void AppendStr(std::string& str, Args&&... args) {
detail::AppendStr(str, {args...});
}
} // namespace agi
5 changes: 3 additions & 2 deletions src/ass_attachment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <libaegisub/ass/uuencode.h>
#include <libaegisub/file_mapping.h>
#include <libaegisub/io.h>
#include <libaegisub/string.h>

#include <boost/algorithm/string/predicate.hpp>

Expand All @@ -43,8 +44,8 @@ AssAttachment::AssAttachment(std::filesystem::path const& name, AssEntryGroup gr

agi::read_file_mapping file(name);
auto buff = file.read();
entry_data = (group == AssEntryGroup::FONT ? "fontname: " : "filename: ") + filename.get() + "\r\n";
entry_data = entry_data.get() + agi::ass::UUEncode(buff, buff + file.size());
entry_data = agi::Str(group == AssEntryGroup::FONT ? "fontname: " : "filename: ", filename.get(), "\r\n",
agi::ass::UUEncode(buff, buff + file.size()));
}

size_t AssAttachment::GetSize() const {
Expand Down
12 changes: 4 additions & 8 deletions src/ass_dialogue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@
//
// Aegisub Project http://www.aegisub.org/

/// @file ass_dialogue.cpp
/// @brief Class for dialogue lines in subtitles
/// @ingroup subs_storage

#include "ass_dialogue.h"
#include "subtitle_format.h"
#include "utils.h"

#include <libaegisub/of_type_adaptor.h>
#include <libaegisub/split.h>
#include <libaegisub/string.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/spirit/include/karma_generate.hpp>
Expand Down Expand Up @@ -79,7 +75,7 @@ class tokenizer {

std::string_view next_tok() {
if (pos.eof())
throw SubtitleFormatParseError("Failed parsing line: " + std::string(str));
throw SubtitleFormatParseError(agi::Str("Failed parsing line: ", str));
return *pos++;
}

Expand Down Expand Up @@ -269,7 +265,7 @@ void AssDialogue::StripTags() {
static std::string get_text(std::unique_ptr<AssDialogueBlock> &d) { return d->GetText(); }
void AssDialogue::UpdateText(std::vector<std::unique_ptr<AssDialogueBlock>>& blocks) {
if (blocks.empty()) return;
Text = join(blocks | transformed(get_text), "");
Text = agi::Join("", blocks | transformed(get_text));
}

bool AssDialogue::CollidesWith(const AssDialogue *target) const {
Expand All @@ -280,5 +276,5 @@ bool AssDialogue::CollidesWith(const AssDialogue *target) const {
static std::string get_text_p(AssDialogueBlock *d) { return d->GetText(); }
std::string AssDialogue::GetStrippedText() const {
auto blocks = ParseTags();
return join(blocks | agi::of_type<AssDialogueBlockPlain>() | transformed(get_text_p), "");
return agi::Join("", blocks | agi::of_type<AssDialogueBlockPlain>() | transformed(get_text_p));
}
12 changes: 6 additions & 6 deletions src/ass_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void AssFile::InsertAttachment(std::filesystem::path const& filename) {
Attachments.emplace_back(filename, group);
}

std::string AssFile::GetScriptInfo(std::string const& key) const {
std::string_view AssFile::GetScriptInfo(std::string_view key) const {
for (auto const& info : Info) {
if (boost::iequals(key, info.Key()))
return info.Value();
Expand All @@ -116,11 +116,11 @@ std::string AssFile::GetScriptInfo(std::string const& key) const {
return "";
}

int AssFile::GetScriptInfoAsInt(std::string const& key) const {
return atoi(GetScriptInfo(key).c_str());
int AssFile::GetScriptInfoAsInt(std::string_view key) const {
return atoi(GetScriptInfo(key).data());
}

void AssFile::SetScriptInfo(std::string const& key, std::string const& value) {
void AssFile::SetScriptInfo(std::string_view key, std::string_view value) {
for (auto it = Info.begin(); it != Info.end(); ++it) {
if (boost::iequals(key, it->Key())) {
if (value.empty())
Expand Down Expand Up @@ -226,14 +226,14 @@ void AssFile::Sort(EntryList<AssDialogue> &lst, CompFunc comp, std::set<AssDialo
}
}

uint32_t AssFile::AddExtradata(std::string const& key, std::string const& value) {
uint32_t AssFile::AddExtradata(std::string_view key, std::string_view value) {
for (auto const& data : Extradata) {
// perform brute-force deduplication by simple key and value comparison
if (key == data.key && value == data.value) {
return data.id;
}
}
Extradata.push_back(ExtradataEntry{next_extradata_id, key, value});
Extradata.push_back(ExtradataEntry{next_extradata_id, std::string(key), std::string(value)});
return next_extradata_id++; // return old value, then post-increment
}

Expand Down
8 changes: 4 additions & 4 deletions src/ass_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ class AssFile {
/// @param[in] h Height
void GetResolution(int &w,int &h) const;
/// Get the value in a [Script Info] key as int, or 0 if it is not present
int GetScriptInfoAsInt(std::string const& key) const;
int GetScriptInfoAsInt(std::string_view key) const;
/// Get the value in a [Script Info] key as string.
std::string GetScriptInfo(std::string const& key) const;
std::string_view GetScriptInfo(std::string_view key) const;
/// Set the value of a [Script Info] key. Adds it if it doesn't exist.
void SetScriptInfo(std::string const& key, std::string const& value);
void SetScriptInfo(std::string_view key, std::string_view value);

/// @brief Add a new extradata entry
/// @param key Class identifier/owner for the extradata
/// @param value Data for the extradata
/// @return ID of the created entry
uint32_t AddExtradata(std::string const& key, std::string const& value);
uint32_t AddExtradata(std::string_view key, std::string_view value);
/// Fetch all extradata entries from a list of IDs
std::vector<ExtradataEntry> GetExtradata(std::vector<uint32_t> const& id_list) const;
/// Remove unreferenced extradata entries
Expand Down
8 changes: 4 additions & 4 deletions src/ass_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class AssInfo final : public AssEntry {

public:
AssInfo(AssInfo const& o) = default;
AssInfo(std::string key, std::string value) : key(std::move(key)), value(std::move(value)) { }
AssInfo(std::string_view key, std::string_view value) : key(key), value(value) { }

AssEntryGroup Group() const override { return AssEntryGroup::INFO; }
std::string GetEntryData() const { return key + ": " + value; }

std::string Key() const { return key; }
std::string Value() const { return value; }
void SetValue(std::string const& new_value) { value = new_value; }
std::string_view Key() const { return key; }
std::string_view Value() const { return value; }
void SetValue(std::string_view new_value) { value = new_value; }
};
3 changes: 2 additions & 1 deletion src/ass_override.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <libaegisub/exception.h>
#include <libaegisub/format.h>
#include <libaegisub/split.h>
#include <libaegisub/string.h>

#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp>
Expand Down Expand Up @@ -411,7 +412,7 @@ void AssDialogueBlockOverride::AddTag(std::string const& tag) {

static std::string tag_str(AssOverrideTag const& t) { return t; }
std::string AssDialogueBlockOverride::GetText() {
text = "{" + join(Tags | transformed(tag_str), std::string()) + "}";
text = agi::Str("{", agi::Join("", Tags | transformed(tag_str)), "}");
return text;
}

Expand Down
16 changes: 8 additions & 8 deletions src/ass_style_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <libaegisub/io.h>
#include <libaegisub/line_iterator.h>
#include <libaegisub/path.h>
#include <libaegisub/string.h>

#include <boost/algorithm/string/predicate.hpp>

Expand All @@ -50,10 +51,10 @@ void AssStyleStorage::Save() const {
agi::fs::CreateDirectory(file.parent_path());

agi::io::Save out(file);
out.Get() << "\xEF\xBB\xBF";
out.Get() << "\xEF\xBB\xBF"; // UTF-8 BOM

for (auto const& cur : style)
out.Get() << cur->GetEntryData() << std::endl;
out.Get() << cur->GetEntryData() << "\n";
}

void AssStyleStorage::Load(std::filesystem::path const& filename) {
Expand All @@ -75,9 +76,8 @@ void AssStyleStorage::Load(std::filesystem::path const& filename) {
}
}

void AssStyleStorage::LoadCatalog(std::string const& catalogname) {
auto filename = config::path->Decode("?user/catalog/" + catalogname + ".sty");
Load(filename);
void AssStyleStorage::LoadCatalog(std::string_view catalogname) {
Load(config::path->Decode(agi::Str("?user/catalog/", catalogname, ".sty")));
}

void AssStyleStorage::Delete(int idx) {
Expand All @@ -91,7 +91,7 @@ std::vector<std::string> AssStyleStorage::GetNames() {
return names;
}

AssStyle *AssStyleStorage::GetStyle(std::string const& name) {
AssStyle *AssStyleStorage::GetStyle(std::string_view name) {
for (auto& cur : style) {
if (boost::iequals(cur->name, name))
return cur.get();
Expand All @@ -106,9 +106,9 @@ std::vector<std::string> AssStyleStorage::GetCatalogs() {
return catalogs;
}

bool AssStyleStorage::CatalogExists(std::string const& catalogname) {
bool AssStyleStorage::CatalogExists(std::string_view catalogname) {
if (catalogname.empty()) return false;
auto filename = config::path->Decode("?user/catalog/" + catalogname + ".sty");
auto filename = config::path->Decode(agi::Str("?user/catalog/", catalogname, ".sty"));
return agi::fs::FileExists(filename);
}

Expand Down
6 changes: 3 additions & 3 deletions src/ass_style_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AssStyleStorage {
/// Get the style with the given name
/// @param name Case-insensitive style name
/// @return Style or nullptr if the requested style is not found
AssStyle *GetStyle(std::string const& name);
AssStyle *GetStyle(std::string_view name);

/// Save stored styles to a file
void Save() const;
Expand All @@ -74,14 +74,14 @@ class AssStyleStorage {

/// Load stored styles from a file in the default location
/// @param catalogname Basename for the catalog file. Does not have to exist.
void LoadCatalog(std::string const& catalogname);
void LoadCatalog(std::string_view catalogname);

/// Make a list of all existing style catalogs in the default location
static std::vector<std::string> GetCatalogs();

/// Check whether the name catalog exists in the default location
/// @param catalogname Basename for the catalog file to check for.
static bool CatalogExists(std::string const& catalogname);
static bool CatalogExists(std::string_view catalogname);

/// Insert all styles into a file, replacing existing styles with the same names
/// @param file File to replace styles in
Expand Down
8 changes: 5 additions & 3 deletions src/async_video_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static std::unique_ptr<SubtitlesProvider> get_subs_provider(wxEvtHandler *evt_ha
}
}

AsyncVideoProvider::AsyncVideoProvider(std::filesystem::path const& video_filename, std::string const& colormatrix, wxEvtHandler *parent, agi::BackgroundRunner *br)
AsyncVideoProvider::AsyncVideoProvider(std::filesystem::path const& video_filename, std::string_view colormatrix, wxEvtHandler *parent, agi::BackgroundRunner *br)
: worker(agi::dispatch::Create())
, subs_provider(get_subs_provider(parent, br))
, source_provider(VideoProviderFactory::GetProvider(video_filename, colormatrix, br))
Expand Down Expand Up @@ -208,8 +208,10 @@ std::shared_ptr<VideoFrame> AsyncVideoProvider::GetFrame(int frame, double time,
return ret;
}

void AsyncVideoProvider::SetColorSpace(std::string const& matrix) {
worker->Async([=] { source_provider->SetColorSpace(matrix); });
void AsyncVideoProvider::SetColorSpace(std::string_view matrix) {
worker->Async([this, matrix = std::string(matrix)]() {
source_provider->SetColorSpace(matrix);
});
}

wxDEFINE_EVENT(EVT_FRAME_READY, FrameReadyEvent);
Expand Down
4 changes: 2 additions & 2 deletions src/async_video_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class AsyncVideoProvider {
std::shared_ptr<VideoFrame> GetFrame(int frame, double time, bool raw = false);

/// Ask the video provider to change YCbCr matricies
void SetColorSpace(std::string const& matrix);
void SetColorSpace(std::string_view matrix);

int GetFrameCount() const { return source_provider->GetFrameCount(); }
int GetWidth() const { return source_provider->GetWidth(); }
Expand All @@ -127,7 +127,7 @@ class AsyncVideoProvider {
/// @brief Constructor
/// @param videoFileName File to open
/// @param parent Event handler to send FrameReady events to
AsyncVideoProvider(std::filesystem::path const& filename, std::string const& colormatrix, wxEvtHandler *parent, agi::BackgroundRunner *br);
AsyncVideoProvider(std::filesystem::path const& filename, std::string_view colormatrix, wxEvtHandler *parent, agi::BackgroundRunner *br);
~AsyncVideoProvider();
};

Expand Down
3 changes: 2 additions & 1 deletion src/audio_colorscheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
#include "options.h"

#include <libaegisub/exception.h>
#include <libaegisub/string.h>

AudioColorScheme::AudioColorScheme(int prec, std::string const& scheme_name, int audio_rendering_style)
: palette((3<<prec) + 3)
, factor((size_t)1<<prec)
{
std::string opt_base = "Colour/Schemes/" + scheme_name + "/";
std::string opt_base = agi::Str("Colour/Schemes/", scheme_name, "/");
switch (static_cast<AudioRenderingStyle>(audio_rendering_style))
{
case AudioStyle_Normal: opt_base += "Normal/"; break;
Expand Down
Loading

0 comments on commit 36bfd29

Please sign in to comment.