Skip to content

Commit

Permalink
Use boost::string_view if available
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkive committed Jun 2, 2017
1 parent 5e86da6 commit 7ef2776
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/font/text.cpp
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)) {

Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/serialization/string_utils.cpp
Expand Up @@ -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 %
Expand Down
31 changes: 21 additions & 10 deletions src/serialization/string_utils.hpp
Expand Up @@ -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())
{
}

Expand All @@ -62,14 +67,20 @@ class string_view

explicit operator std::string() const
{
return std::string(str, size);
return std::string(str, size_);
}

std::string to_string() const
{
return std::string(str, size_);
}

std::string to_str() const
size_t size() const
{
return std::string(str, size);
return size_;
}
};
#endif

bool isnewline(const char c);
bool portable_isspace(const char c);
Expand Down

0 comments on commit 7ef2776

Please sign in to comment.