*
* LyX, The Document Processor
*
- * Copyright (C) 1995 Matthias Ettrich
- * Copyright (C) 1995-1999 The LyX Team.
+ * Copyright 1995 Matthias Ettrich
+ * Copyright 1995-2000 The LyX Team.
*
* ====================================================== */
#include "LAssert.h"
using std::min;
+using std::istream;
+using std::ostream;
// This class is supposed to be functionaly equivalent to a
// standard conformant string. This mean among others that we
// Lgb.
+
///////////////////////////////////////
// The internal string representation
///////////////////////////////////////
-#define NEW_ALLOC 1
struct lyxstring::Srep {
-#ifndef NEW_ALLOC
- ///
- static size_t const xtra = static_cast<size_t>(8);
-#endif
/// size
size_t sz;
/// Reference count
lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
{
-// can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
+ // can be called with p == 0 by
+ // lyxstring::assign(const value_type *, size_type)
sz = nsz;
ref = 1;
-#ifdef NEW_ALLOC
res = sz ? sz : 1;
-#else
- res = sz + xtra;
-#endif
s = new value_type[res + 1]; // add space for terminator
if (p && sz) {
// if sz = 0 nothing gets copied and we have an error
{
sz = nsz;
ref = 1;
-#ifdef NEW_ALLOC
res = sz ? sz : 1;
-#else
- res = sz + xtra;
-#endif
s = new value_type[res + 1]; // add space for terminator
memset(s, ch, sz);
if (!ch) {
void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
{
-// can be called with p == 0 by lyxstring::assign(const value_type *, size_type)
+ // can be called with p == 0
+ // by lyxstring::assign(const value_type *, size_type)
if (res < nsz) {
delete[] s;
sz = nsz;
-#ifdef NEW_ALLOC
res = sz ? sz : 1;
-#else
- res = sz + xtra;
-#endif
s = new value_type[res + 1]; // add space for terminator
} else {
sz = nsz;
sz = nsz;
if (res < nsz) {
delete[] s;
-#ifdef NEW_ALLOC
res = sz ? sz : 1;
-#else
- res = sz + xtra;
-#endif
s = new value_type[res + 1]; // add space for terminator
}
memset(s, ch, sz);
{
register unsigned int const len = sz + asz;
if (res < len) {
-#ifdef NEW_ALLOC
do {
res *= 2;
} while (res < len);
-#else
- res = len + xtra;
-#endif
value_type * tmp = new value_type[res + 1];
memcpy(tmp, s, sz);
memcpy(tmp + sz, p, asz);
s[sz] = c; // it is always room to put a value_type at the end
++sz;
if (res < sz) {
-#ifdef NEW_ALLOC
do {
res *= 2;
} while (res < sz);
-#else
- res = sz + xtra;
-#endif
value_type * tmp = new value_type[res + 1];
memcpy(tmp, s, sz);
delete[] s;
void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
- lyxstring::size_type n)
+ lyxstring::size_type n)
{
if (res < n + sz) {
-#ifdef NEW_ALLOC
do {
res *= 2;
} while (res < n + sz);
-#else
- res = sz + n + xtra;
-#endif
value_type * tmp = new value_type[res + 1];
memcpy(tmp, s, pos);
memcpy(tmp + pos, p, n);
- memcpy(tmp + pos + n, & s[pos], sz - pos);
+ memcpy(tmp + pos + n, &s[pos], sz - pos);
sz += n;
delete[] s;
s = tmp;
} else {
- memmove(s + pos + n, & s[pos], sz - pos);
+ memmove(s + pos + n, &s[pos], sz - pos);
memcpy(s + pos, p, n);
sz += n;
}
void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
- value_type const * p, size_type n2)
+ value_type const * p, size_type n2)
{
// can be called with p= 0 and n2= 0
n = min(sz - i, n);
memcpy(s + i, p, n2);
sz += n2;
} else {
-#ifdef NEW_ALLOC
do {
res *= 2;
} while (res < n2 + sz);
-#else
- res = sz + n2 + xtra;
-#endif
value_type * tmp = new value_type[res + 1];
memcpy(tmp, s, i);
memcpy(tmp + i, p, n2);
///////////////////////////////////////
// The lyxstring Invariant tester
///////////////////////////////////////
-#ifdef DEVEL_VERSION
+#ifdef ENABLE_ASSERTIONS
/** Testing of the lyxstring invariant
* By creating an object that tests the lyxstring invariant during its
helper();
}
+
lyxstringInvariant::~lyxstringInvariant()
{
helper();
// printf("lyxstringInvariant destructor completed\n");
}
+
void lyxstringInvariant::helper() const
{
// Some of these tests might look pointless but they are
#define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
#else
#define TestlyxstringInvariant(s)
-#endif //DEVEL_VERSION
+#endif /* ENABLE_ASSERTIONS */
///////////////////////////////////////
lyxstring::size_type const lyxstring::npos =
static_cast<lyxstring::size_type>(-1);
+
lyxstring::lyxstring()
{
static Srep empty_rep(0, "");
{
Assert(s && n < npos); // STD!
static Srep empty_rep(0, "");
- if (*s && n) { // s is not empty string and n > 0
- rep = new Srep(min(strlen(s), n), s);
+ if (n) { // n > 0
+ rep = new Srep(n, s);
} else {
++empty_rep.ref;
rep = &empty_rep;
lyxstring::iterator lyxstring::begin()
{
+ rep = rep->get_own_copy();
return rep->s;
}
lyxstring::iterator lyxstring::end()
{
+ rep = rep->get_own_copy();
return rep->s + rep->sz;
}
// Assignment
////////////////
-lyxstring & lyxstring::operator= (lyxstring const & x)
+lyxstring & lyxstring::operator=(lyxstring const & x)
{
TestlyxstringInvariant(this);
}
-lyxstring & lyxstring::operator= (value_type const * s)
+lyxstring & lyxstring::operator=(value_type const * s)
{
Assert(s); // OURS!
TestlyxstringInvariant(this);
lyxstring & lyxstring::assign(value_type const * s, size_type n)
{
- Assert(s); // OURS!
+ Assert(s && n < npos); // STD!
TestlyxstringInvariant(this);
- n = min(strlen(s), n);
if (rep->ref == 1) // recycle rep
rep->assign(n, s);
else {
- rep->ref--;
+ --rep->ref;
rep = new Srep(n, s);
}
return *this;
if (!*p || !n) return *this;
rep = rep->get_own_copy();
- rep->append(min(n, strlen(p)), p);
+ rep->append(n, p);
return *this;
}
lyxstring & lyxstring::append(value_type const * p)
{
Assert(p); // OURS!
- TestlyxstringInvariant(this);
-
- if (!*p) return *this;
- rep = rep->get_own_copy();
- rep->append(strlen(p), p);
- return *this;
+ return append(p, strlen(p));
}
return *this;
}
-// insert value_typeacters before (*this)[pos]
+// insert characters before (*this)[pos]
lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
{
lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
- size_type pos2, size_type n)
+ size_type pos2, size_type n)
{
Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
TestlyxstringInvariant(this);
if (*p && n) {
// insert nothing and you change nothing
rep = rep->get_own_copy();
- rep->insert(pos, p, min(n, strlen(p)));
+ rep->insert(pos, p, n);
}
return *this;
}
lyxstring & lyxstring::insert(size_type pos, value_type const * p)
{
Assert(p); // OURS!
- TestlyxstringInvariant(this);
-
- if (*p) {
- // insert nothing and you change nothing
- rep = rep->get_own_copy();
- rep->insert(pos, p, strlen(p));
- }
- return *this;
+ return insert(pos, p, strlen(p));
}
lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
- size_type n) const
+ size_type n) const
{
Assert(ptr); // OURS!
if (!rep->sz || !*ptr || i >= rep->sz) return npos;
lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
- size_type n) const
+ size_type n) const
{
Assert(ptr); // OURS!
TestlyxstringInvariant(this);
lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
- size_type i) const
+ size_type i) const
{
Assert(i < rep->sz); // OURS!
TestlyxstringInvariant(this);
}
-lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i,
- size_type n) const
+lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
+ size_type i,
+ size_type n) const
{
Assert(ptr && i < rep->sz); // OURS!
TestlyxstringInvariant(this);
if (!n) return npos;
for (size_type t = i; t < rep->sz; ++t) {
- if(memchr(ptr, rep->s[t], n) != 0) return t;
+ if (memchr(ptr, rep->s[t], n) != 0) return t;
}
return npos;
}
lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
- size_type i) const
+ size_type i) const
{
Assert(ptr && i < rep->sz); // OURS!
TestlyxstringInvariant(this);
lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
- size_type i) const
+ size_type i) const
{
TestlyxstringInvariant(this);
return npos;
}
-lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i,
- size_type n) const
+
+lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
+ size_type i,
+ size_type n) const
{
Assert(ptr); // OURS!
TestlyxstringInvariant(this);
size_type ii = min(rep->sz - 1, i);
for (int t = ii; t >= 0; --t) {
- if(memchr(ptr, rep->s[t], n) != 0) return t;
+ if (memchr(ptr, rep->s[t], n) != 0) return t;
}
return npos;
}
lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
- size_type i) const
+ size_type i) const
{
Assert(ptr); // OURS!
TestlyxstringInvariant(this);
if (!n) return (i < rep->sz) ? i : npos;
for (size_type t = i; t < rep->sz; ++t) {
- if(memchr(ptr, rep->s[t], n) == 0) return t;
+ if (memchr(ptr, rep->s[t], n) == 0) return t;
}
return npos;
}
size_type ii = min(rep->sz - 1, i);
for (int t = ii; t >= 0; --t) {
- if(memchr(ptr, rep->s[t], n) == 0) return t;
+ if (memchr(ptr, rep->s[t], n) == 0) return t;
}
return npos;
}
lyxstring & lyxstring::replace(size_type i, size_type n,
size_type n2, value_type c)
{
- Assert(i < rep->sz); // OURS!
+ Assert(i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
rep = rep->get_own_copy();
}
-int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const
+int lyxstring::compare(size_type pos, size_type n,
+ lyxstring const & str) const
{
Assert(pos <= rep->sz); // OURS!
TestlyxstringInvariant(this);
return is;
}
+
ostream & operator<<(ostream & o, lyxstring const & s)
{
return o.write(s.data(), s.length());
}
+
istream & getline(istream & is, lyxstring & s,
lyxstring::value_type delim)
{