From 7ef277666a9dc0e17121803c5385b70d5542c0cb Mon Sep 17 00:00:00 2001 From: Jyrki Vesterinen Date: Fri, 19 May 2017 21:47:19 +0300 Subject: [PATCH] Use boost::string_view if available --- src/font/text.cpp | 12 ++++++------ src/serialization/string_utils.cpp | 4 ++-- src/serialization/string_utils.hpp | 31 ++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/font/text.cpp b/src/font/text.cpp index 56b791a417ac..bfd64d77d9ae 100644 --- a/src/font/text.cpp +++ b/src/font/text.cpp @@ -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; } diff --git a/src/serialization/string_utils.cpp b/src/serialization/string_utils.cpp index e45e412c4dba..06019a932e46 100644 --- a/src/serialization/string_utils.cpp +++ b/src/serialization/string_utils.cpp @@ -407,8 +407,8 @@ std::pair 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 % diff --git a/src/serialization/string_utils.hpp b/src/serialization/string_utils.hpp index 8f915cbcd3c9..169d81140379 100644 --- a/src/serialization/string_utils.hpp +++ b/src/serialization/string_utils.hpp @@ -26,29 +26,34 @@ #include #include +#if BOOST_VERSION > 106100 +#include +#endif + class t_string; namespace utils { using string_map = std::map; -// 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 @@ 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);