Use boost::string_view if available

This commit is contained in:
Jyrki Vesterinen 2017-05-19 21:47:19 +03:00
parent 5e86da67b6
commit 7ef277666a
3 changed files with 29 additions and 18 deletions

View file

@ -733,7 +733,7 @@ void pango_text::create_surface_buffer(const size_t size) const
}
bool pango_text::set_markup(utils::string_view text, PangoLayout& layout) {
return this->set_markup_helper(link_aware_ ? this->format_link_tokens(text.to_str()) : text, layout);
return this->set_markup_helper(link_aware_ ? this->format_link_tokens(text.to_string()) : text, layout);
}
std::string pango_text::format_link_tokens(const std::string & text) const {
@ -769,11 +769,11 @@ std::string pango_text::handle_token(const std::string & token) const
bool pango_text::set_markup_helper(utils::string_view text, PangoLayout& layout)
{
if(pango_parse_markup(text.str, text.size,
if(pango_parse_markup(text.data(), text.size(),
0, nullptr, nullptr, nullptr, nullptr)) {
/* Markup is valid so set it. */
pango_layout_set_markup(&layout, text.str, text.size);
pango_layout_set_markup(&layout, text.data(), text.size());
return true;
}
@ -785,14 +785,14 @@ bool pango_text::set_markup_helper(utils::string_view text, PangoLayout& layout)
* So only try to recover from broken ampersands, by simply replacing them
* with the escaped version.
*/
std::string semi_escaped{semi_escape_text(text.to_str())};
std::string semi_escaped{semi_escape_text(text.to_string())};
/*
* If at least one ampersand is replaced the semi-escaped string
* is longer than the original. If this isn't the case then the
* markup wasn't (only) broken by ampersands in the first place.
*/
if(text.size == semi_escaped.size()
if(text.size() == semi_escaped.size()
|| !pango_parse_markup(semi_escaped.c_str(), semi_escaped.size()
, 0, nullptr, nullptr, nullptr, nullptr)) {
@ -801,7 +801,7 @@ bool pango_text::set_markup_helper(utils::string_view text, PangoLayout& layout)
<< " text '" << text
<< "' has broken markup, set to normal text.\n";
this->set_text(_("The text contains invalid markup: ") + text.to_str(), false);
this->set_text(_("The text contains invalid markup: ") + text.to_string(), false);
return false;
}

View file

@ -407,8 +407,8 @@ std::pair<string_view, string_view> vertical_split(const std::string& val)
assert(split_point != 0);
return { string_view(val, 0, split_point),
string_view(val, split_point + 1, val.size() - (split_point + 1)) };
return { string_view(val.data(), split_point),
string_view(&val[split_point + 1], val.size() - (split_point + 1)) };
}
// Modify a number by string representing integer difference, or optionally %

View file

@ -26,29 +26,34 @@
#include <utility>
#include <vector>
#if BOOST_VERSION > 106100
#include <boost/utility/string_view.hpp>
#endif
class t_string;
namespace utils {
using string_map = std::map<std::string, t_string>;
// TODO: when https://github.com/aquileia/external has boost::string_view,
// replace this class with it.
#if BOOST_VERSION > 106100
using boost::string_view;
#else
class string_view
{
public:
const char* str;
const int size;
const int size_;
string_view(const std::string& str_, int offset, int size_)
: str(&str_[offset])
, size(size_)
string_view(const char* str_, size_t len)
: str(str_)
, size_(len)
{
}
string_view(const std::string& str_)
: str(str_.c_str())
, size(str_.size())
, size_(str_.size())
{
}
@ -62,14 +67,20 @@ public:
explicit operator std::string() const
{
return std::string(str, size);
return std::string(str, size_);
}
std::string to_str() const
std::string to_string() const
{
return std::string(str, size);
return std::string(str, size_);
}
size_t size() const
{
return size_;
}
};
#endif
bool isnewline(const char c);
bool portable_isspace(const char c);