// -*- C++ -*-
-/* This file is part of
- * ======================================================
- *
- * LyX, The Document Processor
- *
- * Copyright 1995 Matthias Ettrich
- * Copyright 1995-1999 The LyX Team.
+/**
+ * \file lyxstring.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
*
- * ====================================================== */
+ * \author Lars Gullik Bjønnes
+ *
+ * Full author contact details are available in file CREDITS
+ */
// This one is heavily based on the string class in The C++
// Programming Language by Bjarne Stroustrup
// right behavior.
#ifndef LYXSTRING_H
-#define LYXSTRING_H
-
-#ifdef __GNUG__
-#pragma interface
-#endif
+#define LYXSTRING_H
-#include "LOstream.h"
-#include "LIstream.h"
+#include <iosfwd>
#if 0
#include <iterator>
#include <cstring> // for size_t
+namespace lyx {
+
/** A string class for LyX
-
+
This is a permanent String class. It is modeled closely after the C++ STL
string class. In comparison with STL string lyxstring lack support for
reverse iterators and allocators, also char_traits is not used. In most
do things like #lyxstring a= "";#, especially not in constructors.
If you want to use a default empty lyxstring as a parameter, use
-
- #void foo(lyxstring par = lyxstring()); // Correct#
-
+
+ #void foo(lyxstring par = lyxstring()); // Correct#
+
rather than
- #void foo(lyxstring par = ""); // WRONG!#
- #void foo(lyxstring par = 0); // WRONG!#
+ #void foo(lyxstring par = ""); // WRONG!#
+ #void foo(lyxstring par = 0); // WRONG!#
(The last one is only wrong because some compilers can't handle it.)
Correct: & #foo.substring(0, length()-1);# \\
Wrong: & #bar.substring(0, length());#
\end{tabular}
-
+
It is important that you declare lyxstring as const if possible, because
some methods are much more efficient in const versions.
-
+
If you want to check whether a string is empty, do
- #if (foo.empty()) something right#
+ #if (foo.empty()) something right#
rather than something along the lines of
- #if (!foo) completely wrong#
+ #if (!foo) completely wrong#
When you want to copy an lyxstring, just do
-
+
#lyxstring a, b = "String";#
#a = b; // That's it!#
not something like
-
- #lyxstring a, b = "String";#
- #a = b.copy(); // This leaks. // and does not work either. #
-
+
+ #lyxstring a, b = "String";#
+ #a = b.copy(); // This leaks. // and does not work either. #
+
The class automatically handles deep copying when required.
*/
-class lyxstring {
+class string {
public:
- /**@name Typedefs */
- //@{
-
+ /* Typedefs */
+
///
typedef char value_type;
const_reference> const_reverse_iterator;
#endif
- //@}
///
iterator begin();
///
const_reverse_iterator rend() const;
#endif
- /**@name Constructors and Deconstructors. lyxstring has the same
+ /* Constructors and Deconstructors. lyxstring has the same
constructors as STL, except the one using iterators, one other
difference is that lyxstring, do not allow the optional allocator
parameter. */
- //@{
-
+
/// "all characters" marker
static const size_type npos;
- /// #lyxstring x;#
- lyxstring();
-
- /// #lyxstring x(lyxstring ...)#
- lyxstring(lyxstring const &, size_type pos = 0, size_type n = npos);
-
- /// #lyxstring x("abc", 2) -> "ab"#
- lyxstring(value_type const *, size_type n);
-
- /// #lyxstring x("abc")#
- lyxstring(value_type const *);
-
- /// lyxstring(5, 'n') -> "nnnnn"
- lyxstring(size_type n, value_type c);
+ /// #string x;#
+ string();
-#if 1
+ /// #string x(string ...)#
+ string(string const &, size_type pos = 0, size_type n = npos);
+
+ /// #string x("abc", 2) -> "ab"#
+ string(value_type const *, size_type n);
+
+ // #string x("abc")#
+ string(value_type const *);
+
+ /// string(5, 'n') -> "nnnnn"
+ string(size_type n, value_type c);
+
+#if 0
///
- lyxstring(const_iterator first, const_iterator last);
+ string(const_iterator first, const_iterator last);
#else
///
template<class InputIterator>
- lyxstring::lyxstring(InputIterator begin, InputIterator end) {
+ string(InputIterator begin, InputIterator end) {
while (begin != end) {
- push_back((*begin));
+ push_back(*begin);
++begin;
}
}
#endif
///
- ~lyxstring();
-
- //@}
+ ~string();
-
- /**@name Size and Capacity */
- //@{
-
/// number of characters
size_type size() const;
///
void reserve(size_type res_arg = 0);
-
- //@}
-
- /**@name Assignment */
- //@{
///
- lyxstring & operator=(lyxstring const &);
-
+ string & operator=(string const &);
+
///
- lyxstring & operator=(value_type const *);
-
+ string & operator=(value_type const *);
+
///
- lyxstring & operator=(value_type);
+ string & operator=(value_type);
///
- lyxstring & assign(lyxstring const &);
-
+ string & assign(string const &);
+
///
- lyxstring & assign(lyxstring const &, size_type pos, size_type n);
-
+ string & assign(string const &, size_type pos, size_type n);
+
///
- lyxstring & assign(value_type const * p, size_type n);
-
+ string & assign(value_type const * p, size_type n);
+
///
- lyxstring & assign(value_type const * p);
-
+ string & assign(value_type const * p);
+
///
- lyxstring & assign(size_type n, value_type c);
+ string & assign(size_type n, value_type c);
#if 1
///
- lyxstring & assign(const_iterator first, const_iterator last);
+ string & assign(const_iterator first, const_iterator last);
#else
///
template<class InputIterator>
- lyxstring & assign(InputIterator begin, InputIterator end) {
+ string & assign(InputIterator begin, InputIterator end) {
clear;
while (begin != end) {
push_back((*begin));
}
}
#endif
- //@}
- /**@name Element Access. Since lyxstring does not use exceptions,
- an abort is called on out-of-range access to at(). */
-
/// unchecked access
const_reference operator[](size_type) const;
-
+
/// unchecked access
reference operator[](size_type);
-
+
/// checked access
const_reference at(size_type) const;
-
+
/// checked access
reference at(size_type);
- //@}
-
- /**@name Insert */
- //@{
-
// add characters after (*this)[length()-1]:
-
+
///
- lyxstring & operator+=(lyxstring const &);
-
+ string & operator+=(string const &);
+
///
- lyxstring & operator+=(value_type const *);
-
+ string & operator+=(value_type const *);
+
///
- lyxstring & operator+=(value_type);
+ string & operator+=(value_type);
///
void push_back(value_type);
///
- lyxstring & append(lyxstring const &);
+ string & append(string const &);
///
- lyxstring & append(lyxstring const &, size_type pos, size_type n);
+ string & append(string const &, size_type pos, size_type n);
///
- lyxstring & append(value_type const *, size_type n);
+ string & append(value_type const *, size_type n);
///
- lyxstring & append(value_type const *);
+ string & append(value_type const *);
///
- lyxstring & append(size_type n, value_type);
+ string & append(size_type n, value_type);
#if 1
///
- lyxstring & append(iterator first, iterator last);
+ string & append(iterator first, iterator last);
#else
///
template<class InputIterator>
- lyxstring & append(InputIterator begin, InputIterator end) {
+ string & append(InputIterator begin, InputIterator end) {
while (begin != end) {
push_back((*begin));
++begin;
// insert characters before (*this)[pos]:
///
- lyxstring & insert(size_type pos, lyxstring const &);
-
+ string & insert(size_type pos, string const &);
+
///
- lyxstring & insert(size_type pos, lyxstring const &,
+ string & insert(size_type pos, string const &,
size_type pos2, size_type n);
-
+
///
- lyxstring & insert(size_type pos, value_type const * p,
+ string & insert(size_type pos, value_type const * p,
size_type n);
///
- lyxstring & insert(size_type pos, value_type const * p);
+ string & insert(size_type pos, value_type const * p);
///
- lyxstring & insert(size_type pos, size_type n, value_type c);
+ string & insert(size_type pos, size_type n, value_type c);
// insert characters before p
}
}
#endif
-
- //@}
-
- /**@name Find */
- //@{
///
- size_type find(lyxstring const &, size_type i = 0) const;
-
+ size_type find(string const &, size_type i = 0) const;
+
///
size_type find(value_type const * p,
size_type i, size_type n) const;
-
+
///
size_type find(value_type const * p, size_type i = 0) const;
-
+
///
size_type find(value_type c, size_type i = 0) const;
///
- size_type rfind(lyxstring const &, size_type i = npos) const;
-
+ size_type rfind(string const &, size_type i = npos) const;
+
///
size_type rfind(value_type const * p, size_type i, size_type n) const;
-
+
///
size_type rfind(value_type const * p, size_type i = npos) const;
-
+
///
size_type rfind(value_type c, size_type i = npos) const;
///
- size_type find_first_of(lyxstring const &, size_type i = 0) const;
-
+ size_type find_first_of(string const &, size_type i = 0) const;
+
///
size_type find_first_of(value_type const * p, size_type i,
size_type n) const;
-
+
///
size_type find_first_of(value_type const * p, size_type i = 0) const;
-
+
///
size_type find_first_of(value_type c, size_type i = 0) const;
///
- size_type find_last_of(lyxstring const &, size_type i = npos) const;
-
+ size_type find_last_of(string const &, size_type i = npos) const;
+
///
size_type find_last_of(value_type const * p, size_type i,
size_type n) const;
-
+
///
size_type find_last_of(value_type const * p, size_type i = npos) const;
-
+
///
size_type find_last_of(value_type c, size_type i = npos) const;
///
- size_type find_first_not_of(lyxstring const &, size_type i = 0) const;
-
+ size_type find_first_not_of(string const &, size_type i = 0) const;
+
///
size_type find_first_not_of(value_type const * p, size_type i,
size_type n) const;
-
+
///
size_type find_first_not_of(value_type const * p,
size_type i = 0) const;
-
+
///
size_type find_first_not_of(value_type c, size_type i = 0) const;
///
- size_type find_last_not_of(lyxstring const &,
+ size_type find_last_not_of(string const &,
size_type i = npos) const;
-
+
///
size_type find_last_not_of(value_type const * p, size_type i,
size_type n) const;
-
+
///
size_type find_last_not_of(value_type const * p,
size_type i = npos) const;
-
+
///
size_type find_last_not_of(value_type c, size_type i = npos) const;
- //*}
-
-
- /**@name Replace */
- //@{
-
// replace [(*this)[i], (*this)[i+n]] with other characters:
///
- lyxstring & replace(size_type i, size_type n, lyxstring const & str);
+ string & replace(size_type i, size_type n, string const & str);
///
- lyxstring & replace(size_type i, size_type n, lyxstring const & s,
+ string & replace(size_type i, size_type n, string const & s,
size_type i2, size_type n2);
///
- lyxstring & replace(size_type i, size_type n, value_type const * p,
+ string & replace(size_type i, size_type n, value_type const * p,
size_type n2);
///
- lyxstring & replace(size_type i, size_type n, value_type const * p);
+ string & replace(size_type i, size_type n, value_type const * p);
///
- lyxstring & replace(size_type i, size_type n,
+ string & replace(size_type i, size_type n,
size_type n2, value_type c);
+ /// FY! FY! FY! go away !
+ string & replace(size_type i, size_type n,
+ value_type c);
+
///
- lyxstring & replace(iterator i, iterator i2, const lyxstring & str);
+ string & replace(iterator i, iterator i2, const string & str);
///
- lyxstring & replace(iterator i, iterator i2,
+ string & replace(iterator i, iterator i2,
value_type const * p, size_type n);
///
- lyxstring & replace(iterator i, iterator i2, value_type const * p);
+ string & replace(iterator i, iterator i2, value_type const * p);
///
- lyxstring & replace(iterator i, iterator i2,
+ string & replace(iterator i, iterator i2,
size_type n , value_type c);
///
- lyxstring & replace(iterator i, iterator i2, iterator j, iterator j2);
+ string & replace(iterator i, iterator i2, iterator j, iterator j2);
///
- void swap(lyxstring & str);
-
+ void swap(string & str);
+
/// Erase n chars from position i.
- lyxstring & erase(size_type i = 0, size_type n = npos);
+ string & erase(size_type i = 0, size_type n = npos);
///
- lyxstring & clear() {
+ string & clear() {
return erase(0, npos);
}
///
iterator erase(iterator first, iterator last);
- //@}
-
-
- /**@name Conversion to C-style Strings */
- //@{
-
- ///
+ ///
value_type const * c_str() const;
- /** Note that this is STL compilant, so you can not assume
+ /* Note that this is STL compilant, so you can not assume
that the returned array has a trailing '\0'. */
value_type const * data() const;
- /** This one returns a verbatim copy. Not the trailing '\0'
+ /* This one returns a verbatim copy. Not the trailing '\0'
The caller must provide a buffer with engough room.
*/
size_type copy(value_type * buf, size_type len,
size_type pos = 0) const;
- //@}
-
- /**@name Comparisons. Combined > and == */
- //@{
-
///
- int compare(lyxstring const & str) const;
+ int compare(string const & str) const;
///
int compare(value_type const * p) const;
///
- int compare(size_type pos, size_type n, lyxstring const & str) const;
+ int compare(size_type pos, size_type n, string const & str) const;
///
- int compare(size_type pos, size_type n, lyxstring const & str,
+ int compare(size_type pos, size_type n, string const & str,
size_type pos2, size_type n2) const;
///
int compare(size_type pos, size_type n, value_type const * p,
size_type n2 = npos) const;
-
- //@}
-
-
- /**@name Substrings */
- //@{
///
- lyxstring substr(size_type i = 0, size_type n = npos) const;
-
- //@}
+ string substr(size_type i = 0, size_type n = npos) const;
private:
// These three operators can be used to discover erronous use of
// in. Use them for debugging only (or perhaps not even then.)
// Lgb.
//
- //lyxstring & operator+(int);
+ //string & operator+(int);
//
- //lyxstring & operator=(int);
+ //string & operator=(int);
//
- //lyxstring & operator+=(int);
+ //string & operator+=(int);
/// Compare this with s. works with embedded '\0' chars also.
int internal_compare(size_type pos, size_type n,
value_type const * s,
size_type slen, size_type n2) const;
-
+
/// Forward declaration of the string representation
struct Srep;
// DEC cxx requires this.
/// A string is a pointer to it's representation
Srep * rep;
- /** Note: The empty_rep is a local static in each function that
- benefits from one. There is no "global" empty srep but lyxstring
+ /* Note: The empty_rep is a local static in each function that
+ benefits from one. There is no "global" empty srep but string
doesn't need one (no code actually relies upon a single
empty srep).
This overcomes *all* "static initialization" problems,
#ifdef ENABLE_ASSERTIONS
/// lyxstringInvariant is used to test the lyxstring Invariant
- friend class lyxstringInvariant;
+ friend class stringInvariant;
#endif
};
// The usual comparison operators ==, !=, >, <, >=, <= are
// provided for lyxstrings
-bool operator==(lyxstring const &, lyxstring const &);
-bool operator==(lyxstring::value_type const *, lyxstring const &);
-bool operator==(lyxstring const &, lyxstring::value_type const *);
+bool operator==(string const &, string const &);
+bool operator==(string::value_type const *, string const &);
+bool operator==(string const &, string::value_type const *);
-bool operator!=(lyxstring const &, lyxstring const &);
-bool operator!=(lyxstring::value_type const *, lyxstring const &);
-bool operator!=(lyxstring const &, lyxstring::value_type const *);
+bool operator!=(string const &, string const &);
+bool operator!=(string::value_type const *, string const &);
+bool operator!=(string const &, string::value_type const *);
-bool operator>(lyxstring const &, lyxstring const &);
-bool operator>(lyxstring::value_type const *, lyxstring const &);
-bool operator>(lyxstring const &, lyxstring::value_type const *);
+bool operator>(string const &, string const &);
+bool operator>(string::value_type const *, string const &);
+bool operator>(string const &, string::value_type const *);
-bool operator<(lyxstring const &, lyxstring const &);
-bool operator<(lyxstring::value_type const *, lyxstring const &);
-bool operator<(lyxstring const &, lyxstring::value_type const *);
+bool operator<(string const &, string const &);
+bool operator<(string::value_type const *, string const &);
+bool operator<(string const &, string::value_type const *);
-bool operator>=(lyxstring const &, lyxstring const &);
-bool operator>=(lyxstring::value_type const *, lyxstring const &);
-bool operator>=(lyxstring const &, lyxstring::value_type const *);
+bool operator>=(string const &, string const &);
+bool operator>=(string::value_type const *, string const &);
+bool operator>=(string const &, string::value_type const *);
-bool operator<=(lyxstring const &, lyxstring const &);
-bool operator<=(lyxstring::value_type const *, lyxstring const &);
-bool operator<=(lyxstring const &, lyxstring::value_type const *);
+bool operator<=(string const &, string const &);
+bool operator<=(string::value_type const *, string const &);
+bool operator<=(string const &, string::value_type const *);
// Concatenation
-lyxstring operator+(lyxstring const & a, lyxstring const & b);
-lyxstring operator+(char const * a, lyxstring const & b);
-lyxstring operator+(lyxstring::value_type a, lyxstring const & b);
-lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b);
-lyxstring operator+(lyxstring const & a, lyxstring::value_type b);
+string operator+(string const & a, string const & b);
+string operator+(char const * a, string const & b);
+string operator+(string::value_type a, string const & b);
+string operator+(string const & a, string::value_type const * b);
+string operator+(string const & a, string::value_type b);
+
+void swap(string & s1, string & s2);
-void swap(lyxstring & s1, lyxstring & s2);
+std::istream & operator>>(std::istream &, string &);
+std::ostream & operator<<(std::ostream &, string const &);
+std::istream & getline(std::istream &, string &, string::value_type delim = '\n');
-istream & operator>>(istream &, lyxstring &);
-ostream & operator<<(ostream &, lyxstring const &);
-istream & getline(istream &, lyxstring &, lyxstring::value_type delim = '\n');
+} // namespace lyx
#endif