From ead66c0679da1c7e774913143aaf3ce4be875621 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sun, 8 Oct 2006 09:44:26 +0000 Subject: [PATCH] Add operator += for ASCII C strings and single ASCII chars git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15274 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/docstring.C | 31 +++++++++++++++++++++++++++++++ src/support/docstring.h | 9 +++++++++ 2 files changed, 40 insertions(+) diff --git a/src/support/docstring.C b/src/support/docstring.C index 59ac6fedb7..ceffbc3832 100644 --- a/src/support/docstring.C +++ b/src/support/docstring.C @@ -40,6 +40,19 @@ docstring const from_ascii(std::string const & ascii) } +std::string const to_ascii(docstring const & ucs4) +{ + int const len = ucs4.length(); + std::string ascii; + ascii.resize(len); + for (int i = 0; i < len; ++i) { + BOOST_ASSERT(ucs4[i] < 0x80); + ascii[i] = static_cast(ucs4[i]); + } + return ascii; +} + + docstring const from_utf8(std::string const & utf8) { std::vector const ucs4 = @@ -109,6 +122,24 @@ lyx::docstring operator+(char l, lyx::docstring const & r) } +lyx::docstring operator+=(lyx::docstring & l, char const * r) +{ + for (char const * c = r; *c; ++c) { + BOOST_ASSERT(static_cast(*c) < 0x80); + l.push_back(*c); + } + return l; +} + + +lyx::docstring operator+=(lyx::docstring & l, char r) +{ + BOOST_ASSERT(static_cast(r) < 0x80); + l.push_back(r); + return l; +} + + #if (!defined(HAVE_WCHAR_T) || SIZEOF_WCHAR_T != 4) && defined(__GNUC__) // gcc does not have proper locale facets for lyx::char_type if diff --git a/src/support/docstring.h b/src/support/docstring.h index 7b248f6ab1..1cff5109a0 100644 --- a/src/support/docstring.h +++ b/src/support/docstring.h @@ -28,6 +28,9 @@ docstring const from_ascii(char const *); /// Creates a docstring from a std::string of ASCII characters docstring const from_ascii(std::string const &); +/// Creates a std::string of ASCII characters from a docstring +std::string const to_ascii(docstring const &); + /// Creates a docstring from a UTF8 string. This should go eventually. docstring const from_utf8(std::string const &); @@ -60,6 +63,12 @@ lyx::docstring operator+(lyx::docstring const & l, char r); /// Concatenate a single ASCII character and a docstring lyx::docstring operator+(char l, lyx::docstring const & r); +/// Append a C string of ASCII characters to a docstring +lyx::docstring operator+=(lyx::docstring &, char const *); + +/// Append a single ASCII character to a docstring +lyx::docstring operator+=(lyx::docstring & l, char r); + #if SIZEOF_WCHAR_T != 4 && defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ == 3 && __GNUC_MINOR__ < 4 // Missing char_traits methods in gcc 3.3 and older. Taken from gcc 4.2svn.