/* This file is part of
- * ======================================================
- *
+ * ======================================================
+ *
* LyX, The Document Processor
- *
+ *
* Copyright 1995 Matthias Ettrich
* Copyright 1995-2000 The LyX Team.
*
size_t res;
/// Data. At least 1 char for trailing null.
lyxstring::value_type * s;
-
+
///
Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
///
--ref;
return new Srep(sz, s);
}
-
+
///
void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
///
sz = 0;
}
}
-
+
void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
{
memcpy(tmp + i + n2, &s[i + n], sz - i);
delete[] s;
s = tmp;
- sz += n2;
+ sz += n2;
}
}
///////////////////////////////////////
// The lyxstring Invariant tester
///////////////////////////////////////
+
+// There are no know bugs in lyxstring now, and it have been
+// tested for a long time. so we disable the invariant checker. (Lgb)
+#undef ENABLE_ASSERTIONS
#ifdef ENABLE_ASSERTIONS
/** Testing of the lyxstring invariant
* By creating an object that tests the lyxstring invariant during its
* construction *and* its deconstruction we greatly simplify our code.
- * Calling TestlyxstringInvariant() upon entry to an lyxstring method
+ * Calling TestlyxstringInvariant() upon entry to an lyxstring method
* will test the invariant upon entry to the code. If the Asserts fail
* then we know from the stack trace that the corruption occurred *before*
* entry to this method. We can also be sure it didn't happen in any of
// test every last little thing we *know* should be true.
// I may have missed a test or two, so feel free to fill
// in the gaps. ARRae.
+ using lyx::Assert;
Assert(object);
Assert(object->rep);
Assert(object->rep->s); // s is never 0
lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
{
- Assert(pos <= x.rep->sz); // STD!
+ lyx::Assert(pos <= x.rep->sz); // STD!
if (pos == 0 && n >= x.length()) { // this is the default
x.rep->ref++;
rep = x.rep;
lyxstring::lyxstring(value_type const * s, size_type n)
{
- Assert(s && n < npos); // STD!
+ lyx::Assert(s && n < npos); // STD!
static Srep empty_rep(0, "");
if (n) { // n > 0
rep = new Srep(n, s);
lyxstring::lyxstring(value_type const * s)
{
- Assert(s); // STD!
+ lyx::Assert(s); // STD!
static Srep empty_rep(0, "");
if (*s) { // s is not empty string
rep = new Srep(strlen(s), s);
lyxstring::lyxstring(size_type n, value_type c)
{
- Assert(n < npos); // STD!
+ lyx::Assert(n < npos); // STD!
rep = new Srep(n, c);
}
///////////////////////
lyxstring::size_type lyxstring::size() const
-{
+{
return rep->sz;
}
void lyxstring::resize(size_type n, value_type c)
{
- Assert(n <= npos); // STD!
+ lyx::Assert(n <= npos); // STD!
TestlyxstringInvariant(this);
// This resets sz to res_arg
lyxstring & lyxstring::operator=(value_type const * s)
{
- Assert(s); // OURS!
+ lyx::Assert(s); // OURS!
TestlyxstringInvariant(this);
// printf("lyxstring::operator= (value_type const *)\n");
rep = x.rep; // share representation
return *this;
}
-
+
lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
{
- Assert(pos <= x.rep->sz); // STD!
+ lyx::Assert(pos <= x.rep->sz); // STD!
TestlyxstringInvariant(this);
return assign(x.substr(pos, n));
}
-
+
lyxstring & lyxstring::assign(value_type const * s, size_type n)
{
- Assert(s && n < npos); // STD!
+ lyx::Assert(s && n < npos); // STD!
TestlyxstringInvariant(this);
if (rep->ref == 1) // recycle rep
}
return *this;
}
-
+
lyxstring & lyxstring::assign(value_type const * s)
{
- Assert(s); // OURS!
+ lyx::Assert(s); // OURS!
TestlyxstringInvariant(this);
return assign(s, strlen(s));
lyxstring::const_reference lyxstring::operator[](size_type pos) const
{
- Assert(pos <= rep->sz); // OURS!
+#if 0
+ // This is actually what the standard requires,
+ lyx::Assert(pos <= rep->sz); // OURS!
static char helper = '\0';
return pos == rep->sz ? helper : rep->s[pos];
+#else
+ // but we use this one since it is stricter
+ // and more according to the real intent of std::string.
+ lyx::Assert(pos < rep->sz); // OURS!
+ return rep->s[pos];
+#endif
}
lyxstring::reference lyxstring::operator[](size_type pos)
{
- Assert(pos < rep->sz); // OURS!
+ lyx::Assert(pos < rep->sz); // OURS!
TestlyxstringInvariant(this);
rep = rep->get_own_copy();
lyxstring::const_reference lyxstring::at(size_type n) const
{
- Assert(n < rep->sz); // STD!
+ lyx::Assert(n < rep->sz); // STD!
return rep->s[n];
}
lyxstring::reference lyxstring::at(size_type n)
{
- Assert(n < rep->sz); // STD!
+ lyx::Assert(n < rep->sz); // STD!
TestlyxstringInvariant(this);
rep = rep->get_own_copy();
lyxstring & lyxstring::operator+=(value_type const * x)
{
- Assert(x); // OURS!
+ lyx::Assert(x); // OURS!
TestlyxstringInvariant(this);
return append(x);
lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
{
- Assert(pos <= x.rep->sz); // STD!
+ lyx::Assert(pos <= x.rep->sz); // STD!
TestlyxstringInvariant(this);
return append(x.substr(pos, n));
lyxstring & lyxstring::append(value_type const * p, size_type n)
{
- Assert(p); // OURS!
+ lyx::Assert(p); // OURS!
TestlyxstringInvariant(this);
if (!*p || !n) return *this;
lyxstring & lyxstring::append(value_type const * p)
{
- Assert(p); // OURS!
+ lyx::Assert(p); // OURS!
return append(p, strlen(p));
}
lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
size_type pos2, size_type n)
{
- Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
+ lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
TestlyxstringInvariant(this);
rep = rep->get_own_copy();
lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
{
- Assert(p); // OURS!
+ lyx::Assert(p); // OURS!
TestlyxstringInvariant(this);
if (*p && n) {
lyxstring & lyxstring::insert(size_type pos, value_type const * p)
{
- Assert(p); // OURS!
+ lyx::Assert(p); // OURS!
return insert(pos, p, strlen(p));
}
insert(p - begin(), first, last - first);
}
-
+
////////////////
// Find
////////////////
-
- // All the below find functions should be verified,
- // it is very likely that I have mixed up or interpreted
- // some of the parameters wrong, also some of the funcs can surely
- // be written more effectively.
+
+ // All the below find functions should be verified,
+ // it is very likely that I have mixed up or interpreted
+ // some of the parameters wrong, also some of the funcs can surely
+ // be written more effectively.
lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
{
if (!rep->sz || i >= rep->sz) return npos;
-
+
TestlyxstringInvariant(this);
size_type n = a.length();
lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
size_type n) const
{
- Assert(ptr); // OURS!
+ lyx::Assert(ptr); // OURS!
if (!rep->sz || !*ptr || i >= rep->sz) return npos;
-
+
TestlyxstringInvariant(this);
// What is "n" here? is it the number of value_types to use in ptr
lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
{
- Assert(s); // OURS!
+ lyx::Assert(s); // OURS!
if (!rep->sz || i >= rep->sz) return npos;
-
+
TestlyxstringInvariant(this);
if (!s || !*s) return npos;
TestlyxstringInvariant(this);
- for (size_type t = 0; t + i < rep->sz; ++t) {
- if (rep->s[t + i] == c) return t + i;
+ for (size_type t = 0; t + i < rep->sz; ++t) {
+ if (rep->s[t + i] == c) return t + i;
}
- return npos;
+ return npos;
}
}
if (equal) return t;
}
- } while(t-- > 0);
+ } while (t-- > 0);
return npos;
}
lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
size_type n) const
{
- Assert(ptr); // OURS!
+ lyx::Assert(ptr); // OURS!
TestlyxstringInvariant(this);
n = min(n, strlen(ptr));
lyxstring::size_type lyxstring::rfind(value_type const * ptr,
size_type i) const
{
- Assert(ptr); // OURS!
+ lyx::Assert(ptr); // OURS!
if (!ptr || !*ptr) return npos;
return rfind(ptr, i, strlen(ptr));
lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
size_type i) const
{
- Assert(i < rep->sz); // OURS!
+ lyx::Assert(i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
for (size_type t = i; t < rep->sz; ++t) {
size_type i,
size_type n) const
{
- Assert(ptr && i < rep->sz); // OURS!
+ lyx::Assert(ptr && i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
if (!n) return npos;
lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
size_type i) const
{
- Assert(ptr && i < rep->sz); // OURS!
+ lyx::Assert(ptr && i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
for (size_type t = i; t < rep->sz; ++t) {
lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
{
- Assert(i < rep->sz); // OURS!
+ lyx::Assert(i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
for (size_type t = i; t < rep->sz; ++t) {
size_type i,
size_type n) const
{
- Assert(ptr); // OURS!
+ lyx::Assert(ptr); // OURS!
TestlyxstringInvariant(this);
if (!n) return npos;
lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
size_type i) const
{
- Assert(ptr); // OURS!
+ lyx::Assert(ptr); // OURS!
TestlyxstringInvariant(this);
size_type ii = min(rep->sz - 1, i);
TestlyxstringInvariant(this);
if (!rep->sz) return npos;
- Assert(i < rep->sz);
+ lyx::Assert(i <= rep->sz);
for (size_type t = i; t < rep->sz; ++t) {
if (a.find(rep->s[t]) == npos) return t;
}
size_type i,
size_type n) const
{
- Assert(ptr && i < rep->sz); // OURS!
+ lyx::Assert(ptr && i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
if (!n) return (i < rep->sz) ? i : npos;
lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
size_type i) const
{
- Assert(ptr && i < rep->sz); // OURS!
+ lyx::Assert(ptr && i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
for (size_type t = i; t < rep->sz; ++t) {
size_type i) const
{
if (!rep->sz) return npos;
- Assert(i < rep->sz); // OURS!
+ lyx::Assert(i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
for (size_type t = i; t < rep->sz; ++t) {
size_type i,
size_type n) const
{
- Assert(ptr); // OURS!
+ lyx::Assert(ptr); // OURS!
TestlyxstringInvariant(this);
if (!n) return npos;
lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
size_type i) const
{
- Assert(ptr); // OURS!
+ lyx::Assert(ptr); // OURS!
TestlyxstringInvariant(this);
size_type ii = min(rep->sz - 1, i);
lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
{
- Assert(i <= rep->sz); // OURS!
+ lyx::Assert(i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
return replace(i, n, x, 0, x.rep->sz);
lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
size_type i2, size_type n2)
{
- Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
+ lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
TestlyxstringInvariant(this);
rep = rep->get_own_copy();
lyxstring & lyxstring::replace(size_type i, size_type n,
value_type const * p, size_type n2)
{
- Assert(p && i < rep->sz); // OURS!
+ lyx::Assert(p && i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
rep = rep->get_own_copy();
lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
{
- Assert(p && i < rep->sz); // OURS!
+ lyx::Assert(p && i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
lyxstring & lyxstring::replace(size_type i, size_type n,
size_type n2, value_type c)
{
- Assert(i <= rep->sz); // OURS!
+ lyx::Assert(i <= rep->sz); // OURS!
TestlyxstringInvariant(this);
rep = rep->get_own_copy();
}
+/// FY! FY! FY! go away !
+lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
+{
+ return replace(i, n, 1, c);
+}
+
+
lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
{
TestlyxstringInvariant(this);
- return replace(i - begin(), i2 - i, str);
+ return replace(i - begin(), i2 - i, str);
}
lyxstring & lyxstring::replace(iterator i, iterator i2,
value_type const * p, size_type n)
{
- Assert(p); // OURS!
+ lyx::Assert(p); // OURS!
TestlyxstringInvariant(this);
return replace(i - begin(), i2 - i, p, n);
lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
{
- Assert(p); // OURS!
+ lyx::Assert(p); // OURS!
TestlyxstringInvariant(this);
return replace(i - begin(), i2 - i, p);
return replace(i - begin(), i2 - i, n, c);
}
-
+
lyxstring & lyxstring::replace(iterator i, iterator i2,
iterator j, iterator j2)
lyxstring & lyxstring::erase(size_type i, size_type n)
{
- Assert(i <= rep->sz); // STD!
+ lyx::Assert(i <= rep->sz); // STD!
TestlyxstringInvariant(this);
rep = rep->get_own_copy();
lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
size_type pos) const
{
- Assert(buf); // OURS!
- Assert(pos <= rep->sz); // STD!
+ lyx::Assert(buf); // OURS!
+ lyx::Assert(pos <= rep->sz); // STD!
TestlyxstringInvariant(this);
register int nn = min(len, length() - pos);
if (!*s) return 1;
// since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
- // remember that n can very well be a lot larger than rep->sz
- // so we have to ensure that n is no larger than rep->sz
- n = min(n, rep->sz);
+ // remember that n can very well be a lot larger than rep->sz
+ // so we have to ensure that n is no larger than rep->sz
+ n = min(n, rep->sz);
n2 = min(n2, slen);
- if (n == n2)
+ if (n == n2)
return memcmp(&(rep->s[pos]), s, n);
int c = memcmp(&(rep->s[pos]), s, min(n, n2));
if (c)
int lyxstring::compare(value_type const * s) const
{
- Assert(s); //OURS!
+ lyx::Assert(s); //OURS!
TestlyxstringInvariant(this);
int n = (!s) ? 0 : strlen(s);
return internal_compare(0, rep->sz, s, n, n);
int lyxstring::compare(size_type pos, size_type n,
lyxstring const & str) const
{
- Assert(pos <= rep->sz); // OURS!
+ lyx::Assert(pos <= rep->sz); // OURS!
TestlyxstringInvariant(this);
return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
}
int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
size_type pos2, size_type n2) const
{
- Assert(pos <= rep->sz); // OURS!
- Assert(pos2 <= str.rep->sz); // OURS!
+ lyx::Assert(pos <= rep->sz); // OURS!
+ lyx::Assert(pos2 <= str.rep->sz); // OURS!
TestlyxstringInvariant(this);
return internal_compare(pos, n,
str.rep->s + pos2,
int lyxstring::compare(size_type pos, size_type n, value_type const * s,
size_type n2) const
{
- Assert(s && pos <= rep->sz); // OURS!
+ lyx::Assert(s && pos <= rep->sz); // OURS!
TestlyxstringInvariant(this);
return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
}
// i = index, n = length
lyxstring lyxstring::substr(size_type i, size_type n) const
{
- Assert(i <= rep->sz); // STD!
+ lyx::Assert(i <= rep->sz); // STD!
TestlyxstringInvariant(this);
return lyxstring(*this, i, n);
bool operator==(lyxstring::value_type const * a, lyxstring const & b)
{
- Assert(a); // OURS!
+ lyx::Assert(a); // OURS!
return b.compare(a) == 0;
}
bool operator==(lyxstring const & a, lyxstring::value_type const * b)
{
- Assert(b); // OURS!
+ lyx::Assert(b); // OURS!
return a.compare(b) == 0;
}
bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
{
- Assert(a); // OURS!
+ lyx::Assert(a); // OURS!
return b.compare(a) != 0;
}
bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
{
- Assert(b); // OURS!
+ lyx::Assert(b); // OURS!
return a.compare(b) != 0;
}
bool operator>(lyxstring::value_type const * a, lyxstring const & b)
{
- Assert(a); // OURS!
+ lyx::Assert(a); // OURS!
return b.compare(a) < 0; // since we reverse the parameters
}
bool operator>(lyxstring const & a, lyxstring::value_type const * b)
{
- Assert(b); // OURS!
+ lyx::Assert(b); // OURS!
return a.compare(b) > 0;
}
bool operator<(lyxstring::value_type const * a, lyxstring const & b)
{
- Assert(a); // OURS!
+ lyx::Assert(a); // OURS!
return b.compare(a) > 0; // since we reverse the parameters
}
bool operator<(lyxstring const & a, lyxstring::value_type const * b)
{
- Assert(b); // OURS!
+ lyx::Assert(b); // OURS!
return a.compare(b) < 0;
}
bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
{
- Assert(a); // OURS!
+ lyx::Assert(a); // OURS!
return b.compare(a) <= 0; // since we reverse the parameters
}
bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
{
- Assert(b); // OURS!
+ lyx::Assert(b); // OURS!
return a.compare(b) >= 0;
}
bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
{
- Assert(a); // OURS!
+ lyx::Assert(a); // OURS!
return b.compare(a) >= 0; // since we reverse the parameters
}
bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
{
- Assert(b); // OURS!
+ lyx::Assert(b); // OURS!
return a.compare(b) <= 0;
}
lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
{
- Assert(a); // OURS!
+ lyx::Assert(a); // OURS!
lyxstring tmp(a);
tmp += b;
return tmp;
lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
{
- Assert(b); // OURS!
+ lyx::Assert(b); // OURS!
lyxstring tmp(a);
tmp += b;
return tmp;
#include <iostream>
+#if 0
istream & operator>>(istream & is, lyxstring & s)
{
-#if 1
- // very bad solution
- char * nome = new char[1024];
- is >> nome;
- lyxstring tmp(nome);
- delete [] nome;
- if (!tmp.empty()) s = tmp;
-#else
// better solution
- int w = is.widdth(0);
+ int w = is.width(0);
s.clear();
char c = 0;
while (is.get(c)) {
s += c;
if (--w == 1) break;
}
- if (s.empty()) is.setstate(ios::failbit);
+ if (s.empty()) is.setstate(std::ios::failbit);
+ return is;
+}
+#else
+istream & operator>>(istream & is, lyxstring & str)
+{
+ typedef istream istream_type;
+ typedef int int_type;
+ typedef std::streambuf streambuf_type;
+ typedef string string_type;
+ typedef string::size_type size_type;
+ size_type extracted = 0;
+
+#if 0
+ istream_type::sentry cerb(is, false);
+ if (cerb) {
+#else
+ if (is.ipfx0()) {
#endif
+ str.erase();
+ std::streamsize w = is.width();
+ size_type n;
+ n = w > 0 ? static_cast<size_type>(w) : str.max_size();
+
+ int_type const eof = EOF;
+ streambuf_type * sb = is.rdbuf();
+ int_type c = sb->sgetc();
+
+ while (extracted < n
+ && c != eof && !isspace(c)) {
+ str += c;
+ ++extracted;
+ c = sb->snextc();
+ }
+ if (c == eof)
+ is.setstate(std::ios::eofbit);
+ is.width(0);
+ }
+#if 1
+ is.isfx();
+#endif
+ if (!extracted)
+ is.setstate(std::ios::failbit);
return is;
}
+#endif
ostream & operator<<(ostream & o, lyxstring const & s)
// very bad solution
char tmp = 0;
s.erase();
- while(is) {
+ while (is) {
is.get(tmp);
if (tmp != delim) {
s += tmp;