/* 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
///////////////////////
lyxstring::size_type lyxstring::size() const
-{
+{
return rep->sz;
}
rep = x.rep; // share representation
return *this;
}
-
+
lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
{
return assign(x.substr(pos, n));
}
-
+
lyxstring & lyxstring::assign(value_type const * s, size_type n)
{
}
return *this;
}
-
+
lyxstring & lyxstring::assign(value_type const * s)
{
lyxstring::const_reference lyxstring::operator[](size_type pos) const
{
+#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
}
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();
{
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
{
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;
}
}
+/// 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);
}
return replace(i - begin(), i2 - i, n, c);
}
-
+
lyxstring & lyxstring::replace(iterator i, iterator i2,
iterator j, iterator j2)
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)
#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;