From a5263e0f927034885870a65053ef3908d1c6f885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Wed, 31 Oct 2007 22:40:34 +0000 Subject: [PATCH] introduce a header to forward declare std::string. hope there are only conforming implementaions out there ;-} git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21312 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BranchList.h | 2 + src/BufferView.h | 2 +- src/Changes.h | 1 + src/Color.h | 2 +- src/CutAndPaste.h | 4 +- src/Encoding.cpp | 2 + src/ErrorList.h | 1 + src/LaTeXFeatures.h | 3 +- src/Length.cpp | 7 ++ src/Length.h | 12 ++-- src/Lexer.h | 11 +-- src/MetricsInfo.h | 5 +- src/Paragraph.h | 3 +- src/SpellBase.cpp | 1 + src/SpellBase.h | 2 +- src/frontends/qt4/ColorCache.cpp | 2 + src/gettext.h | 2 +- src/lengthcommon.cpp | 3 +- src/lyxfind.h | 2 +- src/support/ExceptionMessage.h | 2 +- src/support/Makefile.am | 1 + src/support/docstring.cpp | 4 +- src/support/docstring.h | 66 +---------------- src/support/lstrings.cpp | 47 ++++++++++++ src/support/os.h | 5 +- src/support/os_unix.cpp | 1 + src/support/strfwd.h | 118 +++++++++++++++++++++++++++++++ src/support/textutils.h | 8 +-- src/support/types.h | 13 ---- src/support/unicode.cpp | 2 + src/support/unicode.h | 2 +- src/support/userinfo.h | 2 +- 32 files changed, 225 insertions(+), 113 deletions(-) create mode 100644 src/support/strfwd.h diff --git a/src/BranchList.h b/src/BranchList.h index 0da9fe261f..44f10213eb 100644 --- a/src/BranchList.h +++ b/src/BranchList.h @@ -32,6 +32,8 @@ #include "Color.h" +#include "support/docstring.h" + #include diff --git a/src/BufferView.h b/src/BufferView.h index 5ed29beecd..11efee4298 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -18,7 +18,7 @@ #include "Dimension.h" #include "update_flags.h" -#include "support/docstring.h" +#include "support/strfwd.h" #include "support/types.h" #include diff --git a/src/Changes.h b/src/Changes.h index 5305f55004..e08221e338 100644 --- a/src/Changes.h +++ b/src/Changes.h @@ -16,6 +16,7 @@ #define CHANGES_H #include "support/docstream.h" +#include "support/types.h" #include "support/lyxtime.h" #include diff --git a/src/Color.h b/src/Color.h index 05e670806e..d632bbce2f 100644 --- a/src/Color.h +++ b/src/Color.h @@ -21,7 +21,7 @@ #include "ColorCode.h" -#include "support/docstring.h" +#include "support/strfwd.h" #include diff --git a/src/CutAndPaste.h b/src/CutAndPaste.h index ff7aacd261..3864cb86b6 100644 --- a/src/CutAndPaste.h +++ b/src/CutAndPaste.h @@ -14,9 +14,11 @@ #ifndef CUTANDPASTE_H #define CUTANDPASTE_H -#include "support/docstring.h" #include "TextClass.h" +#include "support/types.h" +#include "support/docstring.h" + #include namespace lyx { diff --git a/src/Encoding.cpp b/src/Encoding.cpp index ca896d4c43..c95748bea1 100644 --- a/src/Encoding.cpp +++ b/src/Encoding.cpp @@ -23,6 +23,8 @@ #include "support/lstrings.h" #include "support/unicode.h" +#include + #include #ifndef CXX_GLOBAL_CSTD diff --git a/src/ErrorList.h b/src/ErrorList.h index e58c47d0f0..f881420816 100644 --- a/src/ErrorList.h +++ b/src/ErrorList.h @@ -12,6 +12,7 @@ #ifndef ERRORLIST_H #define ERRORLIST_H +#include "support/types.h" #include "support/docstring.h" #include diff --git a/src/LaTeXFeatures.h b/src/LaTeXFeatures.h index 878b466797..fa64d8f7fb 100644 --- a/src/LaTeXFeatures.h +++ b/src/LaTeXFeatures.h @@ -13,7 +13,6 @@ #ifndef LATEXFEATURES_H #define LATEXFEATURES_H - #include "OutputParams.h" #include "support/docstring.h" @@ -125,7 +124,7 @@ private: /// UsedFloats usedFloats_; /// - typedef std::map FileMap; + typedef std::map FileMap; /// FileMap IncludedFiles_; /** Buffer of the file being processed. diff --git a/src/Length.cpp b/src/Length.cpp index 49f1d633c4..cc45736570 100644 --- a/src/Length.cpp +++ b/src/Length.cpp @@ -59,6 +59,13 @@ Length::Length(string const & data) } +void Length::swap(Length & rhs) +{ + std::swap(val_, rhs.val_); + std::swap(unit_, rhs.unit_); +} + + string const Length::asString() const { ostringstream os; diff --git a/src/Length.h b/src/Length.h index a5dd75143f..7351557522 100644 --- a/src/Length.h +++ b/src/Length.h @@ -15,7 +15,7 @@ #ifndef LENGTH_H #define LENGTH_H -#include "support/docstring.h" +#include "support/strfwd.h" namespace lyx { @@ -67,12 +67,8 @@ public: /// "data" must be a decimal number, followed by a unit explicit Length(std::string const & data); - void swap(Length & rhs) - { - std::swap(val_, rhs.val_); - std::swap(unit_, rhs.unit_); - } - + /// + void swap(Length & rhs); /// double value() const; /// @@ -100,7 +96,7 @@ public: private: /// - double val_; + double val_; /// Length::UNIT unit_; }; diff --git a/src/Lexer.h b/src/Lexer.h index 6418639266..fbe27602c8 100644 --- a/src/Lexer.h +++ b/src/Lexer.h @@ -17,9 +17,7 @@ #ifndef LEXER_H #define LEXER_H -#include "support/docstring.h" - -#include +#include "support/strfwd.h" #include @@ -55,7 +53,7 @@ struct keyword_item { @see LyXRC.cpp for an example of usage. */ -class Lexer : boost::noncopyable { +class Lexer { public: /// Lexer(keyword_item *, int); @@ -175,6 +173,11 @@ public: static std::string const quoteString(std::string const &); private: + /// noncopiable + Lexer(Lexer const &); + void operator=(Lexer const &); + + /// class Pimpl; /// Pimpl * pimpl_; diff --git a/src/MetricsInfo.h b/src/MetricsInfo.h index 1275090702..5bb93b6e8c 100644 --- a/src/MetricsInfo.h +++ b/src/MetricsInfo.h @@ -14,7 +14,10 @@ #include "ColorCode.h" #include "FontInfo.h" -#include "support/docstring.h" + +#include "support/strfwd.h" +#include "support/types.h" + #include class BufferView; diff --git a/src/Paragraph.h b/src/Paragraph.h index 9de84bf057..81a77288a8 100644 --- a/src/Paragraph.h +++ b/src/Paragraph.h @@ -21,7 +21,8 @@ #include "insets/InsetCode.h" -#include "support/docstring.h" +#include "support/strfwd.h" +#include "support/types.h" // FIXME: would be nice to forward declare odocstream instead of // including this: #include "support/docstream.h" diff --git a/src/SpellBase.cpp b/src/SpellBase.cpp index 8c878d8a06..89734b613d 100644 --- a/src/SpellBase.cpp +++ b/src/SpellBase.cpp @@ -13,6 +13,7 @@ #include "SpellBase.h" #include "gettext.h" +#include "support/docstring.h" namespace lyx { diff --git a/src/SpellBase.h b/src/SpellBase.h index e4ec616bdc..75336376f3 100644 --- a/src/SpellBase.h +++ b/src/SpellBase.h @@ -13,7 +13,7 @@ #ifndef SPELL_BASE_H #define SPELL_BASE_H -#include "support/docstring.h" +#include "support/strfwd.h" namespace lyx { diff --git a/src/frontends/qt4/ColorCache.cpp b/src/frontends/qt4/ColorCache.cpp index 514246a18f..bfccf7a6f6 100644 --- a/src/frontends/qt4/ColorCache.cpp +++ b/src/frontends/qt4/ColorCache.cpp @@ -14,6 +14,8 @@ #include "Color.h" +#include + namespace lyx { const QColor grey40(0x66, 0x66, 0x66); diff --git a/src/gettext.h b/src/gettext.h index 0624d8be83..597a3a1abd 100644 --- a/src/gettext.h +++ b/src/gettext.h @@ -13,7 +13,7 @@ #ifndef GETTEXT_H #define GETTEXT_H -#include "support/docstring.h" +#include "support/strfwd.h" namespace lyx { diff --git a/src/lengthcommon.cpp b/src/lengthcommon.cpp index 9897dc3413..d4f16b0514 100644 --- a/src/lengthcommon.cpp +++ b/src/lengthcommon.cpp @@ -13,9 +13,10 @@ #include #include "Length.h" - #include "gettext.h" +#include + namespace lyx { diff --git a/src/lyxfind.h b/src/lyxfind.h index f9b45357e4..d23be0fec6 100644 --- a/src/lyxfind.h +++ b/src/lyxfind.h @@ -15,7 +15,7 @@ #ifndef LYXFIND_H #define LYXFIND_H -#include "support/docstring.h" +#include "support/strfwd.h" namespace lyx { diff --git a/src/support/ExceptionMessage.h b/src/support/ExceptionMessage.h index 9b752be029..1fa2564511 100644 --- a/src/support/ExceptionMessage.h +++ b/src/support/ExceptionMessage.h @@ -34,7 +34,7 @@ public: ExceptionMessage(ExceptionType type, docstring const & title, docstring const & details) : type_(type), title_(title), details_(details), - message_(to_utf8(title_ + '\n' + details_)) {} + message_(to_utf8(title_ + docstring::value_type('\n') + details_)) {} virtual const char * what() const throw() { return message_.c_str(); } virtual ~ExceptionMessage() throw() {} diff --git a/src/support/Makefile.am b/src/support/Makefile.am index f3315c5dc2..1a9dfe2d74 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -77,6 +77,7 @@ liblyxsupport_la_SOURCES = \ rename.cpp \ socktools.cpp \ socktools.h \ + strfwd.h \ std_istream.h \ std_ostream.h \ Systemcall.cpp \ diff --git a/src/support/docstring.cpp b/src/support/docstring.cpp index 8ea76f95ec..2baa974a0b 100644 --- a/src/support/docstring.cpp +++ b/src/support/docstring.cpp @@ -194,7 +194,9 @@ lyx::docstring operator+(char const * l, lyx::docstring const & r) lyx::docstring operator+(lyx::docstring const & l, char r) { BOOST_ASSERT(static_cast(r) < 0x80); - return l + lyx::docstring::value_type(r); + docstring s = l; + s += docstring::value_type(r); + return s; } diff --git a/src/support/docstring.h b/src/support/docstring.h index fc00e347c2..165772cdf2 100644 --- a/src/support/docstring.h +++ b/src/support/docstring.h @@ -13,7 +13,7 @@ #ifndef LYX_DOCSTRING_H #define LYX_DOCSTRING_H -#include "support/types.h" +#include "support/strfwd.h" #include @@ -22,70 +22,6 @@ namespace lyx { /// String type for storing the main text in UCS4 encoding typedef std::basic_string docstring; -/// Creates a docstring from a C string of ASCII characters -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 &); - -/// Creates a UTF8 string from a docstring. This should go eventually. -std::string const to_utf8(docstring const &); - -/// convert \p s from the encoding of the locale to ucs4. -docstring const from_local8bit(std::string const & s); - -/** - * Convert \p s from ucs4 to the encoding of the locale. - * This may fail and throw an exception, the caller is expected to act - * appropriately. - */ -std::string const to_local8bit(docstring const & s); - -/// convert \p s from the encoding of the file system to ucs4. -docstring const from_filesystem8bit(std::string const & s); - -/// convert \p s from ucs4 to the encoding of the file system. -std::string const to_filesystem8bit(docstring const & s); - -/// normalize \p s to precomposed form c -docstring const normalize_c(docstring const & s); - -/// Compare a docstring with a C string of ASCII characters -bool operator==(lyx::docstring const &, char const *); - -/// Compare a C string of ASCII characters with a docstring -inline bool operator==(char const * l, lyx::docstring const & r) { return r == l; } - -/// Compare a docstring with a C string of ASCII characters -inline bool operator!=(lyx::docstring const & l, char const * r) { return !(l == r); } - -/// Compare a C string of ASCII characters with a docstring -inline bool operator!=(char const * l, lyx::docstring const & r) { return !(r == l); } - -/// Concatenate a docstring and a C string of ASCII characters -lyx::docstring operator+(lyx::docstring const &, char const *); - -/// Concatenate a C string of ASCII characters and a docstring -lyx::docstring operator+(char const *, lyx::docstring const &); - -/// Concatenate a docstring and a single ASCII character -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); - } // namespace lyx diff --git a/src/support/lstrings.cpp b/src/support/lstrings.cpp index 8568b2f7e0..a7cd44dbc6 100644 --- a/src/support/lstrings.cpp +++ b/src/support/lstrings.cpp @@ -518,6 +518,19 @@ std::basic_string const subst_char(std::basic_string const & a, return tmp; } +/// Substitute all \a oldchar with \a newchar +docstring const subst_char(docstring const & a, + docstring::value_type oldchar, docstring::value_type newchar) +{ + docstring tmp(a); + docstring::iterator lit = tmp.begin(); + docstring::iterator end = tmp.end(); + for (; lit != end; ++lit) + if ((*lit) == oldchar) + (*lit) = newchar; + return tmp; +} + /// substitutes all instances of \a oldstr with \a newstr template inline @@ -536,6 +549,21 @@ String const subst_string(String const & a, return lstr; } +docstring const subst_string(docstring const & a, + docstring const & oldstr, docstring const & newstr) +{ + BOOST_ASSERT(!oldstr.empty()); + docstring lstr = a; + docstring::size_type i = 0; + docstring::size_type const olen = oldstr.length(); + while ((i = lstr.find(oldstr, i)) != string::npos) { + lstr.replace(i, olen, newstr); + i += newstr.length(); // We need to be sure that we dont + // use the same i over and over again. + } + return lstr; +} + } @@ -681,6 +709,25 @@ String const doSplit(String const & a, String & piece, Char delim) return tmp; } +template inline +docstring const doSplit(docstring const & a, docstring & piece, Char delim) +{ + docstring tmp; + typename docstring::size_type i = a.find(delim); + if (i == a.length() - 1) { + piece = a.substr(0, i); + } else if (i != docstring::npos) { + piece = a.substr(0, i); + tmp = a.substr(i + 1); + } else if (i == 0) { + piece.erase(); + tmp = a.substr(i + 1); + } else { + piece = a; + } + return tmp; +} + } diff --git a/src/support/os.h b/src/support/os.h index 11147e596b..899b948296 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -14,7 +14,8 @@ #ifndef OS_H #define OS_H -#include "support/docstring.h" +#include "support/strfwd.h" +#include namespace lyx { @@ -43,7 +44,7 @@ std::string const python(); /// Extract the path common to both @c p1 and @c p2. DBCS aware! /// \p p1, \p p2 and the return value are encoded in utf8. -docstring::size_type common_path(docstring const & p1, docstring const & p2); +std::size_t common_path(docstring const & p1, docstring const & p2); /// Converts a unix style path to host OS style. /// \p p and the return value are encoded in utf8. diff --git a/src/support/os_unix.cpp b/src/support/os_unix.cpp index f5e4c84102..e2c7725221 100644 --- a/src/support/os_unix.cpp +++ b/src/support/os_unix.cpp @@ -13,6 +13,7 @@ #include #include "support/os.h" +#include "support/docstring.h" #ifdef __APPLE__ #include diff --git a/src/support/strfwd.h b/src/support/strfwd.h new file mode 100644 index 0000000000..b216318215 --- /dev/null +++ b/src/support/strfwd.h @@ -0,0 +1,118 @@ +// -*- C++ -*- + +// Heavily inspired by /usr/include/c++/4.1/bits +// +// Copyright (C) 2001, 2002 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 2, or (at your option) +// any later version. + +#ifndef STRFWD_H +#define STRFWD_H + +namespace std { + +template struct char_traits; +template<> struct char_traits; +template<> struct char_traits; + +template class allocator; + +template class basic_string; + +typedef basic_string, allocator > string; + +} + + +#ifdef USE_WCHAR_T + +// Prefer this if possible because GNU libstdc++ has usable +// std::ctype locale facets but not +// std::ctype. gcc older than 3.4 is also missing +// usable std::char_traits. +namespace lyx { typedef wchar_t char_type; } + +#else + +#include +namepace lyx { typedef boost::uint32_t char_type; } + +#endif + + +namespace lyx { + +typedef std::basic_string, + std::allocator > docstring; + +/// Creates a docstring from a C string of ASCII characters +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 &); + +/// Creates a UTF8 string from a docstring. This should go eventually. +std::string const to_utf8(docstring const &); + +/// convert \p s from the encoding of the locale to ucs4. +docstring const from_local8bit(std::string const & s); + +/** + * Convert \p s from ucs4 to the encoding of the locale. + * This may fail and throw an exception, the caller is expected to act + * appropriately. + */ +std::string const to_local8bit(docstring const & s); + +/// convert \p s from the encoding of the file system to ucs4. +docstring const from_filesystem8bit(std::string const & s); + +/// convert \p s from ucs4 to the encoding of the file system. +std::string const to_filesystem8bit(docstring const & s); + +/// normalize \p s to precomposed form c +docstring const normalize_c(docstring const & s); + +/// Compare a docstring with a C string of ASCII characters +bool operator==(docstring const &, char const *); + +/// Compare a C string of ASCII characters with a docstring +inline bool operator==(char const * l, docstring const & r) { return r == l; } + +/// Compare a docstring with a C string of ASCII characters +inline bool operator!=(docstring const & l, char const * r) { return !(l == r); } + +/// Compare a C string of ASCII characters with a docstring +inline bool operator!=(char const * l, docstring const & r) { return !(r == l); } + +/// Concatenate a docstring and a C string of ASCII characters +docstring operator+(docstring const &, char const *); + +/// Concatenate a C string of ASCII characters and a docstring +docstring operator+(char const *, docstring const &); + +/// Concatenate a docstring and a single ASCII character +docstring operator+(docstring const & l, char r); + +/// Concatenate a single ASCII character and a docstring +docstring operator+(char l, docstring const & r); + +/// Append a C string of ASCII characters to a docstring +docstring & operator+=(docstring &, char const *); + +/// Append a single ASCII character to a docstring +docstring & operator+=(docstring & l, char r); + +} // namespace lyx + +#endif diff --git a/src/support/textutils.h b/src/support/textutils.h index 044d21625c..175beb7915 100644 --- a/src/support/textutils.h +++ b/src/support/textutils.h @@ -15,17 +15,13 @@ #ifndef TEXTUTILS_H #define TEXTUTILS_H -#include "support/types.h" +#include "support/strfwd.h" namespace lyx { /// return true if the char is a line separator -inline -bool isLineSeparatorChar(char_type c) -{ - return c == ' '; -} +inline bool isLineSeparatorChar(char_type c) { return c == ' '; } /// return true if a char is alphabetical (including accented chars) bool isLetterChar(char_type c); diff --git a/src/support/types.h b/src/support/types.h index 6151a676e1..cb1de177ac 100644 --- a/src/support/types.h +++ b/src/support/types.h @@ -16,23 +16,10 @@ #ifndef LYX_TYPES_H #define LYX_TYPES_H -#include - #include namespace lyx { - /// The type used to hold characters in paragraphs -#ifdef USE_WCHAR_T - // Prefer this if possible because GNU libstdc++ has usable - // std::ctype locale facets but not - // std::ctype. gcc older than 3.4 is also missing - // usable std::char_traits. - typedef wchar_t char_type; -#else - typedef boost::uint32_t char_type; -#endif - /// a type for positions used in paragraphs // needs to be signed for a while to hold the special value -1 that is // used there diff --git a/src/support/unicode.cpp b/src/support/unicode.cpp index 6c51e0f037..74c7b6fdc3 100644 --- a/src/support/unicode.cpp +++ b/src/support/unicode.cpp @@ -18,6 +18,8 @@ #include +#include + #include #include #include diff --git a/src/support/unicode.h b/src/support/unicode.h index 8fa4ff77d9..d26d606c85 100644 --- a/src/support/unicode.h +++ b/src/support/unicode.h @@ -13,7 +13,7 @@ #ifndef LYX_SUPPORT_UNICODE_H #define LYX_SUPPORT_UNICODE_H -#include "support/types.h" +#include "support/strfwd.h" #include diff --git a/src/support/userinfo.h b/src/support/userinfo.h index 4556618920..2c482f475c 100644 --- a/src/support/userinfo.h +++ b/src/support/userinfo.h @@ -12,7 +12,7 @@ #ifndef USERINFO_H #define USERINFO_H -#include "support/docstring.h" +#include "support/strfwd.h" namespace lyx {