1999-10-02 16:21:10 +00:00
|
|
|
/* This file is part of
|
2002-03-21 17:09:55 +00:00
|
|
|
* ======================================================
|
|
|
|
*
|
1999-10-02 16:21:10 +00:00
|
|
|
* LyX, The Document Processor
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
1999-12-16 06:43:25 +00:00
|
|
|
* Copyright 1995 Matthias Ettrich
|
2000-03-16 04:29:22 +00:00
|
|
|
* Copyright 1995-2000 The LyX Team.
|
1999-10-02 16:21:10 +00:00
|
|
|
*
|
1999-11-15 10:54:16 +00:00
|
|
|
* ====================================================== */
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation "lyxstring.h"
|
|
|
|
#endif
|
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
#include "lyxstring.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cctype>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "LAssert.h"
|
|
|
|
|
2001-01-10 14:09:56 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
1999-10-19 15:06:30 +00:00
|
|
|
using std::min;
|
2000-11-06 15:47:22 +00:00
|
|
|
using std::istream;
|
|
|
|
using std::ostream;
|
1999-10-19 15:06:30 +00:00
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
// This class is supposed to be functionaly equivalent to a
|
|
|
|
// standard conformant string. This mean among others that we
|
|
|
|
// are useing the same requirements. Before you change anything
|
|
|
|
// in this file consult me and/or the standard to discover the
|
|
|
|
// right behavior.
|
|
|
|
|
1999-11-09 22:20:24 +00:00
|
|
|
// Asserts with a STD! are required by the standard.
|
|
|
|
// Asserts with a OURS! are added by me.
|
1999-11-26 06:57:35 +00:00
|
|
|
// Some asserts could still be missing and some of the existing
|
|
|
|
// ones might be wrong or not needed.
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
// Reference count has been checked, empty_rep removed and
|
|
|
|
// introduced again in a similar guise. Where is empty_rep _really_
|
|
|
|
// needed?
|
|
|
|
|
1999-11-26 06:57:35 +00:00
|
|
|
// We are missing a couple of imporant things from the standard:
|
|
|
|
// reverse iterators and methods taking InputIterators as paramters.
|
|
|
|
// Also the methods returning iterators is returning the wrong value.
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
// All the different find functions need a good look over.
|
|
|
|
// I have so far not tested them extensively and would be
|
|
|
|
// happy if others took the time to have a peek.
|
|
|
|
|
1999-11-26 06:57:35 +00:00
|
|
|
// Space allocation of string.
|
|
|
|
// I have tried to do this very simple without using any special tricks.
|
|
|
|
// Earlier we used a fixed value to enlarge the string with this would
|
|
|
|
// cause a lot of reallocations with large strings (especially if
|
|
|
|
// push_back was used) and wasting space for very small strings.
|
|
|
|
// I have now changed the allocation to use a doubling of reserved
|
|
|
|
// space until it is large enough. So far tests show a small speed
|
|
|
|
// increase and a noticable memory saving.
|
|
|
|
|
1999-11-09 22:20:24 +00:00
|
|
|
// Lgb.
|
1999-10-02 16:21:10 +00:00
|
|
|
|
1999-12-13 21:59:26 +00:00
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// The internal string representation
|
|
|
|
///////////////////////////////////////
|
1999-10-02 16:21:10 +00:00
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
struct lyxstring::Srep {
|
|
|
|
/// size
|
1999-11-05 06:02:34 +00:00
|
|
|
size_t sz;
|
1999-10-22 02:26:50 +00:00
|
|
|
/// Reference count
|
1999-11-05 06:02:34 +00:00
|
|
|
size_t ref;
|
1999-10-22 02:26:50 +00:00
|
|
|
/// The total amount of data reserved for this representaion
|
1999-11-05 06:02:34 +00:00
|
|
|
size_t res;
|
1999-10-22 02:26:50 +00:00
|
|
|
/// Data. At least 1 char for trailing null.
|
|
|
|
lyxstring::value_type * s;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
///
|
|
|
|
Srep(lyxstring::size_type nsz, const lyxstring::value_type * p);
|
|
|
|
///
|
|
|
|
Srep(lyxstring::size_type nsz, lyxstring::value_type ch);
|
|
|
|
///
|
|
|
|
~Srep() { delete[] s; }
|
|
|
|
///
|
1999-11-26 06:57:35 +00:00
|
|
|
Srep * get_own_copy() {
|
|
|
|
if (ref == 1) return this;
|
|
|
|
--ref;
|
|
|
|
return new Srep(sz, s);
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
///
|
|
|
|
void assign(lyxstring::size_type nsz, const lyxstring::value_type * p);
|
|
|
|
///
|
|
|
|
void assign(lyxstring::size_type nsz, lyxstring::value_type ch);
|
|
|
|
///
|
|
|
|
void append(lyxstring::size_type asz, const lyxstring::value_type * p);
|
|
|
|
///
|
|
|
|
void push_back(lyxstring::value_type c);
|
|
|
|
///
|
|
|
|
void insert(lyxstring::size_type pos,
|
|
|
|
const lyxstring::value_type * p,
|
|
|
|
lyxstring::size_type n);
|
|
|
|
///
|
|
|
|
void resize(lyxstring::size_type n, lyxstring::value_type c);
|
|
|
|
///
|
|
|
|
void reserve(lyxstring::size_type res_arg);
|
|
|
|
///
|
|
|
|
void replace(lyxstring::size_type i, lyxstring::size_type n,
|
|
|
|
lyxstring::value_type const * p, lyxstring::size_type n2);
|
1999-10-02 16:21:10 +00:00
|
|
|
private:
|
1999-10-22 02:26:50 +00:00
|
|
|
Srep(const Srep &);
|
|
|
|
Srep & operator=(const Srep &);
|
1999-10-02 16:21:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::Srep::Srep(lyxstring::size_type nsz, const value_type * p)
|
|
|
|
{
|
1999-12-16 06:43:25 +00:00
|
|
|
// can be called with p == 0 by
|
|
|
|
// lyxstring::assign(const value_type *, size_type)
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
sz = nsz;
|
|
|
|
ref = 1;
|
1999-11-26 06:57:35 +00:00
|
|
|
res = sz ? sz : 1;
|
1999-10-02 16:21:10 +00:00
|
|
|
s = new value_type[res + 1]; // add space for terminator
|
|
|
|
if (p && sz) {
|
|
|
|
// if sz = 0 nothing gets copied and we have an error
|
|
|
|
memcpy(s, p, sz);
|
|
|
|
} else {
|
|
|
|
// possibly allows for large but empty string
|
|
|
|
sz = 0; // this line should be redundant
|
|
|
|
s[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::Srep::Srep(lyxstring::size_type nsz, value_type ch)
|
|
|
|
{
|
|
|
|
sz = nsz;
|
|
|
|
ref = 1;
|
1999-11-26 06:57:35 +00:00
|
|
|
res = sz ? sz : 1;
|
1999-10-02 16:21:10 +00:00
|
|
|
s = new value_type[res + 1]; // add space for terminator
|
|
|
|
memset(s, ch, sz);
|
|
|
|
if (!ch) {
|
|
|
|
// if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
|
|
|
|
// allows for large but empty string
|
|
|
|
sz = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
void lyxstring::Srep::assign(lyxstring::size_type nsz, const value_type * p)
|
|
|
|
{
|
1999-12-16 06:43:25 +00:00
|
|
|
// can be called with p == 0
|
|
|
|
// by lyxstring::assign(const value_type *, size_type)
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
if (res < nsz) {
|
|
|
|
delete[] s;
|
|
|
|
sz = nsz;
|
1999-11-26 06:57:35 +00:00
|
|
|
res = sz ? sz : 1;
|
1999-10-02 16:21:10 +00:00
|
|
|
s = new value_type[res + 1]; // add space for terminator
|
|
|
|
} else {
|
|
|
|
sz = nsz;
|
|
|
|
}
|
|
|
|
if (p && sz) {
|
|
|
|
// if sz = 0 nothing gets copied and we have an error
|
|
|
|
memcpy(s, p, sz);
|
|
|
|
} else {
|
|
|
|
// stops segfaults
|
|
|
|
sz = 0; // this line should be redundant
|
|
|
|
s[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::Srep::assign(lyxstring::size_type nsz, value_type ch)
|
|
|
|
{
|
|
|
|
sz = nsz;
|
|
|
|
if (res < nsz) {
|
|
|
|
delete[] s;
|
1999-11-26 06:57:35 +00:00
|
|
|
res = sz ? sz : 1;
|
1999-10-02 16:21:10 +00:00
|
|
|
s = new value_type[res + 1]; // add space for terminator
|
|
|
|
}
|
|
|
|
memset(s, ch, sz);
|
|
|
|
if (!ch) {
|
|
|
|
// if ch == '\0' strlen(lyxstring.c_str()) == 0 so sz = 0
|
|
|
|
// allows for a large empty string
|
|
|
|
sz = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::Srep::append(lyxstring::size_type asz, const value_type * p)
|
|
|
|
{
|
|
|
|
register unsigned int const len = sz + asz;
|
|
|
|
if (res < len) {
|
1999-11-26 06:57:35 +00:00
|
|
|
do {
|
|
|
|
res *= 2;
|
|
|
|
} while (res < len);
|
1999-10-02 16:21:10 +00:00
|
|
|
value_type * tmp = new value_type[res + 1];
|
|
|
|
memcpy(tmp, s, sz);
|
|
|
|
memcpy(tmp + sz, p, asz);
|
|
|
|
sz += asz;
|
|
|
|
delete[] s;
|
|
|
|
s = tmp;
|
|
|
|
} else {
|
|
|
|
memcpy(s + sz, p, asz);
|
|
|
|
sz += asz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::Srep::push_back(value_type c)
|
|
|
|
{
|
|
|
|
s[sz] = c; // it is always room to put a value_type at the end
|
|
|
|
++sz;
|
|
|
|
if (res < sz) {
|
1999-11-26 06:57:35 +00:00
|
|
|
do {
|
|
|
|
res *= 2;
|
|
|
|
} while (res < sz);
|
1999-10-02 16:21:10 +00:00
|
|
|
value_type * tmp = new value_type[res + 1];
|
|
|
|
memcpy(tmp, s, sz);
|
|
|
|
delete[] s;
|
|
|
|
s = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p,
|
1999-12-16 06:43:25 +00:00
|
|
|
lyxstring::size_type n)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
if (res < n + sz) {
|
1999-11-26 06:57:35 +00:00
|
|
|
do {
|
|
|
|
res *= 2;
|
|
|
|
} while (res < n + sz);
|
1999-10-02 16:21:10 +00:00
|
|
|
value_type * tmp = new value_type[res + 1];
|
|
|
|
memcpy(tmp, s, pos);
|
|
|
|
memcpy(tmp + pos, p, n);
|
1999-12-07 00:44:53 +00:00
|
|
|
memcpy(tmp + pos + n, &s[pos], sz - pos);
|
1999-10-02 16:21:10 +00:00
|
|
|
sz += n;
|
|
|
|
delete[] s;
|
|
|
|
s = tmp;
|
|
|
|
} else {
|
1999-12-07 00:44:53 +00:00
|
|
|
memmove(s + pos + n, &s[pos], sz - pos);
|
1999-10-02 16:21:10 +00:00
|
|
|
memcpy(s + pos, p, n);
|
|
|
|
sz += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::Srep::resize(size_type n, value_type c)
|
|
|
|
{
|
|
|
|
// This resets sz to res_arg
|
|
|
|
res = min(n, npos - 2); // We keep no xtra when we resize
|
|
|
|
value_type * tmp = new value_type[res + 1];
|
|
|
|
memcpy(tmp, s, min(sz, res));
|
|
|
|
if (res > sz)
|
|
|
|
memset(tmp + sz, c, res - sz);
|
|
|
|
delete[] s;
|
|
|
|
sz = res;
|
|
|
|
s = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::Srep::reserve(lyxstring::size_type res_arg)
|
|
|
|
{
|
|
|
|
// This keeps the old sz, but
|
|
|
|
// increases res with res_arg
|
|
|
|
res += res_arg;
|
|
|
|
value_type * tmp = new value_type[res + 1];
|
|
|
|
memcpy(tmp, s, sz);
|
|
|
|
delete[] s;
|
|
|
|
s = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n,
|
1999-12-15 06:12:28 +00:00
|
|
|
value_type const * p, size_type n2)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
1999-11-15 10:54:16 +00:00
|
|
|
// can be called with p= 0 and n2= 0
|
1999-10-02 16:21:10 +00:00
|
|
|
n = min(sz - i, n);
|
|
|
|
sz -= n;
|
|
|
|
if (res >= n2 + sz) {
|
|
|
|
memmove(s + i + n2, &s[i + n], sz - i);
|
|
|
|
memcpy(s + i, p, n2);
|
|
|
|
sz += n2;
|
|
|
|
} else {
|
1999-11-26 06:57:35 +00:00
|
|
|
do {
|
|
|
|
res *= 2;
|
|
|
|
} while (res < n2 + sz);
|
1999-10-02 16:21:10 +00:00
|
|
|
value_type * tmp = new value_type[res + 1];
|
|
|
|
memcpy(tmp, s, i);
|
|
|
|
memcpy(tmp + i, p, n2);
|
|
|
|
memcpy(tmp + i + n2, &s[i + n], sz - i);
|
|
|
|
delete[] s;
|
|
|
|
s = tmp;
|
2002-03-21 17:09:55 +00:00
|
|
|
sz += n2;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
///////////////////////////////////////
|
|
|
|
// The lyxstring Invariant tester
|
|
|
|
///////////////////////////////////////
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2001-07-27 20:27:56 +00:00
|
|
|
// 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
|
1999-12-16 14:16:42 +00:00
|
|
|
#ifdef ENABLE_ASSERTIONS
|
1999-10-22 02:26:50 +00:00
|
|
|
|
|
|
|
/** 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.
|
2002-03-21 17:09:55 +00:00
|
|
|
* Calling TestlyxstringInvariant() upon entry to an lyxstring method
|
1999-10-22 02:26:50 +00:00
|
|
|
* 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
|
|
|
|
* the tested lyxstring methods. It is therefore likely to be due to some
|
|
|
|
* other external force.
|
|
|
|
* Several lyxstring methods have multiple exit points which would otherwise
|
|
|
|
* require us to insert a separate test before each return. But since we
|
|
|
|
* created an object its destructor will be called upon exit (any exit!).
|
|
|
|
* We thus get testing at both start and end of a method with one line of
|
|
|
|
* code at the head of a method. More importantly, we get good testing
|
|
|
|
* everytime we run the code.
|
|
|
|
* NOTE: just because we test the invariant doesn't mean we can forget
|
|
|
|
* about testing pre and post conditions specific to any given method.
|
|
|
|
* This test simply proves that the lyxstring/Srep is in a valid state it
|
|
|
|
* does *not* prove that the method did what it was supposed to.
|
|
|
|
*/
|
|
|
|
class lyxstringInvariant {
|
|
|
|
public:
|
|
|
|
lyxstringInvariant(lyxstring const *);
|
|
|
|
~lyxstringInvariant();
|
|
|
|
private:
|
|
|
|
void helper() const;
|
|
|
|
lyxstring const * object;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// To test if this scheme works "as advertised" uncomment the printf's in
|
|
|
|
// the constructor and destructor below and then uncomment the printf and the
|
|
|
|
// call to TestlyxstringInvariant() in lyxstring::operator=(char const *).
|
|
|
|
// The correct output when LyX has been recompiled and run is:
|
|
|
|
// lyxstringInvariant constructor
|
|
|
|
// lyxstring::operator=(char const *)
|
|
|
|
// lyxstringInvariant constructor
|
|
|
|
// lyxstringInvariant destructor completed
|
|
|
|
// lyxstringInvariant destructor completed
|
|
|
|
// NOTE: The easiest way to catch this snippet of the output is to wait for
|
|
|
|
// the splash screen to disappear and then open and close Help->Credits
|
|
|
|
//
|
|
|
|
lyxstringInvariant::lyxstringInvariant(lyxstring const * ls) : object(ls)
|
|
|
|
{
|
|
|
|
// printf("lyxstringInvariant constructor\n");
|
|
|
|
helper();
|
|
|
|
}
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstringInvariant::~lyxstringInvariant()
|
|
|
|
{
|
|
|
|
helper();
|
|
|
|
// printf("lyxstringInvariant destructor completed\n");
|
|
|
|
}
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
void lyxstringInvariant::helper() const
|
|
|
|
{
|
|
|
|
// Some of these tests might look pointless but they are
|
|
|
|
// all part of the invariant and if we want to make sure
|
|
|
|
// we have a bullet proof implementation then we need to
|
|
|
|
// 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.
|
2001-04-25 01:39:27 +00:00
|
|
|
using lyx::Assert;
|
1999-10-22 02:26:50 +00:00
|
|
|
Assert(object);
|
|
|
|
Assert(object->rep);
|
|
|
|
Assert(object->rep->s); // s is never 0
|
1999-11-26 06:57:35 +00:00
|
|
|
Assert(object->rep->res); // res cannot be 0
|
1999-11-05 06:02:34 +00:00
|
|
|
Assert(object->rep->sz <= object->rep->res);
|
1999-10-22 02:26:50 +00:00
|
|
|
Assert(object->rep->ref >= 1); // its in use so it must be referenced
|
1999-11-15 10:54:16 +00:00
|
|
|
Assert(object->rep->ref < 1UL << (8UL * sizeof(object->rep->ref) - 1));
|
1999-10-22 02:26:50 +00:00
|
|
|
// if it does ever == then we should be generating a new copy
|
|
|
|
// and starting again. (Is char always 8-bits?)
|
|
|
|
}
|
|
|
|
#define TestlyxstringInvariant(s) lyxstringInvariant lyxstring_invariant(s);
|
|
|
|
#else
|
|
|
|
#define TestlyxstringInvariant(s)
|
1999-12-16 14:16:42 +00:00
|
|
|
#endif /* ENABLE_ASSERTIONS */
|
1999-10-22 02:26:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////
|
|
|
|
// Constructors and Deconstructors.
|
|
|
|
///////////////////////////////////////
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
lyxstring::size_type const lyxstring::npos =
|
|
|
|
static_cast<lyxstring::size_type>(-1);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
lyxstring::lyxstring()
|
|
|
|
{
|
|
|
|
static Srep empty_rep(0, "");
|
|
|
|
++empty_rep.ref;
|
|
|
|
rep = &empty_rep;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(pos <= x.rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
if (pos == 0 && n >= x.length()) { // this is the default
|
|
|
|
x.rep->ref++;
|
|
|
|
rep = x.rep;
|
|
|
|
} else {
|
|
|
|
rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::lyxstring(value_type const * s, size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(s && n < npos); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
static Srep empty_rep(0, "");
|
2000-09-26 13:54:57 +00:00
|
|
|
if (n) { // n > 0
|
2000-09-26 13:10:34 +00:00
|
|
|
rep = new Srep(n, s);
|
1999-10-02 16:21:10 +00:00
|
|
|
} else {
|
|
|
|
++empty_rep.ref;
|
|
|
|
rep = &empty_rep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::lyxstring(value_type const * s)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(s); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
static Srep empty_rep(0, "");
|
1999-10-19 16:48:35 +00:00
|
|
|
if (*s) { // s is not empty string
|
1999-10-02 16:21:10 +00:00
|
|
|
rep = new Srep(strlen(s), s);
|
|
|
|
} else {
|
|
|
|
++empty_rep.ref;
|
|
|
|
rep = &empty_rep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::lyxstring(size_type n, value_type c)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(n < npos); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
rep = new Srep(n, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:19:48 +00:00
|
|
|
lyxstring::lyxstring(const_iterator first, const_iterator last)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
rep = new Srep(last - first, first);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring::~lyxstring()
|
|
|
|
{
|
|
|
|
if (--rep->ref == 0) delete rep;
|
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
///////////////////////
|
|
|
|
// Iterators
|
|
|
|
///////////////////////
|
|
|
|
|
|
|
|
lyxstring::iterator lyxstring::begin()
|
|
|
|
{
|
2000-05-31 17:08:11 +00:00
|
|
|
rep = rep->get_own_copy();
|
1999-10-02 16:21:10 +00:00
|
|
|
return rep->s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::const_iterator lyxstring::begin() const
|
|
|
|
{
|
|
|
|
return rep->s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::iterator lyxstring::end()
|
|
|
|
{
|
2000-05-31 17:08:11 +00:00
|
|
|
rep = rep->get_own_copy();
|
1999-10-02 16:21:10 +00:00
|
|
|
return rep->s + rep->sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::const_iterator lyxstring::end() const
|
|
|
|
{
|
|
|
|
return rep->s + rep->sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
reverse_iterator lyxstring::rbegin()
|
|
|
|
{
|
|
|
|
return reverse_iterator( end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const_reverse_iterator lyxstring::rbegin() const
|
|
|
|
{
|
|
|
|
return const_reverse_iterator( end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
reverse_iterator lyxstring::rend()
|
|
|
|
{
|
|
|
|
return reverse_iterator( begin() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const_reverse_iterator lyxstring::rend() const
|
|
|
|
{
|
|
|
|
return const_reverse_iterator( begin() );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
///////////////////////
|
|
|
|
// Size and Capacity
|
|
|
|
///////////////////////
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring::size_type lyxstring::size() const
|
2002-03-21 17:09:55 +00:00
|
|
|
{
|
1999-10-22 02:26:50 +00:00
|
|
|
return rep->sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
void lyxstring::resize(size_type n, value_type c)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(n <= npos); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
// This resets sz to res_arg
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->resize(n, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::capacity() const
|
|
|
|
{
|
|
|
|
return rep->res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::reserve(size_type res_arg)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->reserve(res_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// Assignment
|
|
|
|
////////////////
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
lyxstring & lyxstring::operator=(lyxstring const & x)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return assign(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
lyxstring & lyxstring::operator=(value_type const * s)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(s); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
1999-11-15 10:54:16 +00:00
|
|
|
// printf("lyxstring::operator= (value_type const *)\n");
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
return assign(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::operator=(value_type c)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
value_type s[1];
|
|
|
|
s[0] = c;
|
|
|
|
if (rep->ref == 1) // recycle rep
|
|
|
|
rep->assign(1, s);
|
|
|
|
else {
|
|
|
|
rep->ref--;
|
|
|
|
rep = new Srep(1, s);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::assign(lyxstring const & x)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
x.rep->ref++; // protect against ``st = st''
|
|
|
|
if (--rep->ref == 0) delete rep;
|
|
|
|
rep = x.rep; // share representation
|
|
|
|
return *this;
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(pos <= x.rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return assign(x.substr(pos, n));
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
lyxstring & lyxstring::assign(value_type const * s, size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(s && n < npos); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
if (rep->ref == 1) // recycle rep
|
|
|
|
rep->assign(n, s);
|
|
|
|
else {
|
2000-09-26 13:54:57 +00:00
|
|
|
--rep->ref;
|
1999-10-02 16:21:10 +00:00
|
|
|
rep = new Srep(n, s);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
lyxstring & lyxstring::assign(value_type const * s)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(s); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return assign(s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::assign(size_type n, value_type ch)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->assign(n, ch);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:19:48 +00:00
|
|
|
lyxstring & lyxstring::assign(const_iterator first, const_iterator last)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->assign(last - first, first);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
// Element Access
|
|
|
|
////////////////////
|
|
|
|
|
|
|
|
lyxstring::const_reference lyxstring::operator[](size_type pos) const
|
|
|
|
{
|
2001-05-29 09:50:02 +00:00
|
|
|
#if 0
|
2002-04-06 12:42:42 +00:00
|
|
|
// This is actually what the standard requires,
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(pos <= rep->sz); // OURS!
|
1999-11-09 23:52:04 +00:00
|
|
|
static char helper = '\0';
|
|
|
|
return pos == rep->sz ? helper : rep->s[pos];
|
2001-05-29 09:50:02 +00:00
|
|
|
#else
|
2002-04-06 12:42:42 +00:00
|
|
|
// but we use this one since it is stricter
|
|
|
|
// and more according to the real intent of std::string.
|
2001-05-29 09:50:02 +00:00
|
|
|
lyx::Assert(pos < rep->sz); // OURS!
|
|
|
|
return rep->s[pos];
|
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::reference lyxstring::operator[](size_type pos)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(pos < rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
return rep->s[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::const_reference lyxstring::at(size_type n) const
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(n < rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
return rep->s[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::reference lyxstring::at(size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(n < rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
return rep->s[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
// Insert
|
|
|
|
/////////////
|
|
|
|
|
|
|
|
lyxstring & lyxstring::operator+=(lyxstring const & x)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return append(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::operator+=(value_type const * x)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(x); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return append(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::operator+=(value_type c)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
push_back(c);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::push_back(value_type c)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::append(lyxstring const & x)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
if (x.empty()) return *this;
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->append(x.length(), x.rep->s);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(pos <= x.rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return append(x.substr(pos, n));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::append(value_type const * p, size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(p); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
if (!*p || !n) return *this;
|
|
|
|
rep = rep->get_own_copy();
|
2000-09-26 13:54:57 +00:00
|
|
|
rep->append(n, p);
|
1999-10-02 16:21:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::append(value_type const * p)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(p); // OURS!
|
2000-09-26 13:54:57 +00:00
|
|
|
return append(p, strlen(p));
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::append(size_type n, value_type c)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
value_type * tmp = new value_type[n];
|
|
|
|
memset(tmp, c, n);
|
|
|
|
rep = rep->get_own_copy();
|
1999-10-23 20:52:00 +00:00
|
|
|
rep->append(n, tmp);
|
1999-10-02 16:21:10 +00:00
|
|
|
delete[] tmp;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::append(iterator first, iterator last)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->append(last - first, first);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
// insert characters before (*this)[pos]
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
lyxstring & lyxstring::insert(size_type pos, lyxstring const & x)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return insert(pos, x, 0, x.rep->sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::insert(size_type pos, lyxstring const & x,
|
1999-12-16 06:43:25 +00:00
|
|
|
size_type pos2, size_type n)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->insert(pos, &(x.rep->s[pos2]), min(n, x.rep->sz));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(p); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
if (*p && n) {
|
|
|
|
// insert nothing and you change nothing
|
|
|
|
rep = rep->get_own_copy();
|
2000-09-26 13:54:57 +00:00
|
|
|
rep->insert(pos, p, n);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::insert(size_type pos, value_type const * p)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(p); // OURS!
|
2000-09-26 13:54:57 +00:00
|
|
|
return insert(pos, p, strlen(p));
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::insert(size_type pos, size_type n, value_type c)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
value_type * tmp = new value_type[n];
|
|
|
|
memset(tmp, c, n);
|
|
|
|
rep->insert(pos, tmp, n);
|
|
|
|
delete[] tmp;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::iterator lyxstring::insert(iterator p, value_type c)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
// what iterator is this supposed to return??
|
|
|
|
size_type tmp = p - begin();
|
|
|
|
insert(p - begin(), 1, c);
|
|
|
|
return begin() + tmp + 1; // ??
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::insert(iterator p, size_type n , value_type c)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
insert(p - begin(), n , c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lyxstring::insert(iterator p, iterator first, iterator last)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
insert(p - begin(), first, last - first);
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
////////////////
|
|
|
|
// Find
|
|
|
|
////////////////
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
|
|
// 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.
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const
|
|
|
|
{
|
1999-10-27 23:21:38 +00:00
|
|
|
if (!rep->sz || i >= rep->sz) return npos;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
2001-01-20 15:37:54 +00:00
|
|
|
size_type n = a.length();
|
|
|
|
if (!n) return npos;
|
|
|
|
for (size_type t = i; rep->sz - t >= n; ++t) {
|
1999-10-02 16:21:10 +00:00
|
|
|
// search until (*this)[i] == a[0]
|
|
|
|
if (rep->s[t] == a[0]) {
|
|
|
|
// check if the rest of the value_types match
|
|
|
|
bool equal = true;
|
2001-01-20 15:37:54 +00:00
|
|
|
for (size_type j = 1; j < n; ++j) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (rep->s[t + j] != a[j]) {
|
|
|
|
equal = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (equal) return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i,
|
1999-12-16 06:43:25 +00:00
|
|
|
size_type n) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr); // OURS!
|
1999-11-02 16:41:28 +00:00
|
|
|
if (!rep->sz || !*ptr || i >= rep->sz) return npos;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
// What is "n" here? is it the number of value_types to use in ptr
|
|
|
|
// or does "i" and "n" togeter form a substring to search
|
|
|
|
// for ptr in? For now I will assume that "n" tells the length
|
|
|
|
// of ptr. (Lgb)
|
|
|
|
n = min(n, strlen(ptr));
|
2001-01-20 15:37:54 +00:00
|
|
|
if (!n) return npos;
|
1999-11-03 13:04:22 +00:00
|
|
|
for (size_type t = i; rep->sz - t >= n; ++t) {
|
1999-10-02 16:21:10 +00:00
|
|
|
// search until (*this)[i] == a[0]
|
|
|
|
if (rep->s[t] == ptr[0]) {
|
|
|
|
// check if the rest of the value_types match
|
|
|
|
bool equal = true;
|
2001-01-20 15:37:54 +00:00
|
|
|
for (size_type j = 1; j < n; ++j) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (rep->s[t + j] != ptr[j]) {
|
|
|
|
equal = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (equal) return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(s); // OURS!
|
1999-11-02 06:42:25 +00:00
|
|
|
if (!rep->sz || i >= rep->sz) return npos;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
if (!s || !*s) return npos;
|
|
|
|
return find(s, i, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find(value_type c, size_type i) const
|
|
|
|
{
|
1999-11-02 16:41:28 +00:00
|
|
|
if (!rep->sz || i >= rep->sz) return npos;
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
for (size_type t = 0; t + i < rep->sz; ++t) {
|
|
|
|
if (rep->s[t + i] == c) return t + i;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
return npos;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
2001-01-20 15:37:54 +00:00
|
|
|
size_type n = a.length();
|
|
|
|
if (!n || rep->sz < n)
|
|
|
|
return npos;
|
|
|
|
|
|
|
|
size_type t = min(rep->sz - n, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
do {
|
2001-01-20 15:37:54 +00:00
|
|
|
if (rep->s[t] == a[0]) {
|
|
|
|
// check if the rest of the value_types match
|
|
|
|
bool equal = true;
|
|
|
|
for (size_type j = 1; j < n; ++j) {
|
|
|
|
if (rep->s[t + j] != a[j]) {
|
|
|
|
equal = false;
|
|
|
|
break;
|
|
|
|
}
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
2001-01-20 15:37:54 +00:00
|
|
|
if (equal) return t;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
2001-12-05 08:04:20 +00:00
|
|
|
} while (t-- > 0);
|
1999-10-02 16:21:10 +00:00
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i,
|
1999-12-16 06:43:25 +00:00
|
|
|
size_type n) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
2001-01-20 15:37:54 +00:00
|
|
|
n = min(n, strlen(ptr));
|
|
|
|
if (!n || rep->sz < n)
|
|
|
|
return npos;
|
|
|
|
|
|
|
|
size_type t = min(rep->sz - n, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
do {
|
2001-01-20 15:37:54 +00:00
|
|
|
if (rep->s[t] == ptr[0]) {
|
|
|
|
// check if the rest of the value_types match
|
|
|
|
bool equal = true;
|
|
|
|
for (size_type j = 1; j < n; ++j) {
|
|
|
|
if (rep->s[t + j] != ptr[j]) {
|
|
|
|
equal = false;
|
|
|
|
break;
|
|
|
|
}
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
2001-01-20 15:37:54 +00:00
|
|
|
if (equal) return t;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
2001-01-20 15:37:54 +00:00
|
|
|
} while (t-- > 0);
|
1999-10-02 16:21:10 +00:00
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
lyxstring::size_type lyxstring::rfind(value_type const * ptr,
|
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
|
2001-01-20 15:37:54 +00:00
|
|
|
if (!ptr || !*ptr) return npos;
|
|
|
|
return rfind(ptr, i, strlen(ptr));
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
2001-01-10 14:09:56 +00:00
|
|
|
size_type const sz = rep->sz;
|
|
|
|
if (sz < 1) return npos;
|
|
|
|
size_type ii = min(sz - 1, i);
|
|
|
|
do {
|
|
|
|
if (rep->s[ii] == c) return ii;
|
|
|
|
} while (ii-- > 0);
|
|
|
|
return npos;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_first_of(lyxstring const & a,
|
1999-12-07 00:44:53 +00:00
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
for (size_type t = i; t < rep->sz; ++t) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (a.find(rep->s[t]) != npos) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
|
|
|
|
size_type i,
|
|
|
|
size_type n) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr && i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
if (!n) return npos;
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
for (size_type t = i; t < rep->sz; ++t) {
|
2000-11-04 10:00:12 +00:00
|
|
|
if (memchr(ptr, rep->s[t], n) != 0) return t;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_first_of(value_type const * ptr,
|
1999-12-07 00:44:53 +00:00
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr && i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
for (size_type t = i; t < rep->sz; ++t) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (strchr(ptr, rep->s[t]) != 0) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
for (size_type t = i; t < rep->sz; ++t) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (rep->s[t] == c) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_last_of(lyxstring const & a,
|
1999-12-07 00:44:53 +00:00
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type ii = min(rep->sz - 1, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
for (int t = ii; t >= 0; --t) {
|
|
|
|
if (a.find(rep->s[t]) != npos) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
|
|
|
|
size_type i,
|
|
|
|
size_type n) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
if (!n) return npos;
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type ii = min(rep->sz - 1, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
for (int t = ii; t >= 0; --t) {
|
2000-11-04 10:00:12 +00:00
|
|
|
if (memchr(ptr, rep->s[t], n) != 0) return t;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_last_of(value_type const * ptr,
|
1999-12-07 00:44:53 +00:00
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type ii = min(rep->sz - 1, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
for (int t = ii; t >= 0; --t) {
|
|
|
|
if (strchr(ptr, rep->s[t]) != 0) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_last_of(value_type c, size_type i) const
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
if (!rep->sz) return npos;
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type ii = min(rep->sz - 1, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
for (int t = ii; t >= 0; --t) {
|
|
|
|
if (rep->s[t] == c) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_first_not_of(lyxstring const & a,
|
1999-11-05 06:02:34 +00:00
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
if (!rep->sz) return npos;
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz);
|
1999-11-03 13:04:22 +00:00
|
|
|
for (size_type t = i; t < rep->sz; ++t) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (a.find(rep->s[t]) == npos) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
|
|
|
|
size_type i,
|
|
|
|
size_type n) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr && i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
if (!n) return (i < rep->sz) ? i : npos;
|
|
|
|
for (size_type t = i; t < rep->sz; ++t) {
|
2000-11-04 10:00:12 +00:00
|
|
|
if (memchr(ptr, rep->s[t], n) == 0) return t;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr,
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr && i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
for (size_type t = i; t < rep->sz; ++t) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (strchr(ptr, rep->s[t]) == 0) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring::size_type lyxstring::find_first_not_of(value_type c,
|
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
if (!rep->sz) return npos;
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
for (size_type t = i; t < rep->sz; ++t) {
|
1999-10-02 16:21:10 +00:00
|
|
|
if (rep->s[t] != c) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_last_not_of(lyxstring const & a,
|
1999-10-22 02:26:50 +00:00
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type ii = min(rep->sz - 1, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
for (int t = ii; t >= 0; --t) {
|
|
|
|
if (a.find(rep->s[t]) == npos) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-20 01:53:59 +00:00
|
|
|
lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
|
|
|
|
size_type i,
|
|
|
|
size_type n) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-10-20 01:53:59 +00:00
|
|
|
if (!n) return npos;
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type ii = min(rep->sz - 1, i);
|
1999-11-04 01:40:20 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
for (int t = ii; t >= 0; --t) {
|
2000-11-04 10:00:12 +00:00
|
|
|
if (memchr(ptr, rep->s[t], n) == 0) return t;
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr,
|
1999-10-22 02:26:50 +00:00
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(ptr); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type ii = min(rep->sz - 1, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
for (int t = ii; t >= 0; --t) {
|
|
|
|
if (strchr(ptr, rep->s[t]) == 0) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring::size_type lyxstring::find_last_not_of(value_type c,
|
|
|
|
size_type i) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-03 13:04:22 +00:00
|
|
|
size_type ii = min(rep->sz - 1, i);
|
1999-10-02 16:21:10 +00:00
|
|
|
for (int t = ii; t >= 0; --t) {
|
|
|
|
if (rep->s[t] != c) return t;
|
|
|
|
}
|
|
|
|
return npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
// Replace
|
|
|
|
/////////////////
|
|
|
|
|
|
|
|
lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
1999-11-09 22:20:24 +00:00
|
|
|
return replace(i, n, x, 0, x.rep->sz);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-09 22:20:24 +00:00
|
|
|
lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x,
|
1999-10-22 02:26:50 +00:00
|
|
|
size_type i2, size_type n2)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz && i2 <= x.rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->replace(i, min(n, rep->sz), &(x.rep->s[i2]), min(n2, x.rep->sz));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring & lyxstring::replace(size_type i, size_type n,
|
|
|
|
value_type const * p, size_type n2)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(p && i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
rep->replace(i, min(n, rep->sz), p, min(n2, strlen(p)));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(p && i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring & lyxstring::replace(size_type i, size_type n,
|
|
|
|
size_type n2, value_type c)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
value_type * tmp = new value_type[n2];
|
|
|
|
memset(tmp, c, n2);
|
|
|
|
rep->replace(i, min(n, rep->sz), tmp, n2);
|
|
|
|
delete[] tmp;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-25 00:06:33 +00:00
|
|
|
/// FY! FY! FY! go away !
|
|
|
|
lyxstring & lyxstring::replace(size_type i, size_type n, value_type c)
|
|
|
|
{
|
|
|
|
return replace(i, n, 1, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
return replace(i - begin(), i2 - i, str);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::replace(iterator i, iterator i2,
|
1999-10-22 02:26:50 +00:00
|
|
|
value_type const * p, size_type n)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(p); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return replace(i - begin(), i2 - i, p, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(p); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return replace(i - begin(), i2 - i, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring & lyxstring::replace(iterator i, iterator i2,
|
|
|
|
size_type n , value_type c)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return replace(i - begin(), i2 - i, n, c);
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring & lyxstring::replace(iterator i, iterator i2,
|
|
|
|
iterator j, iterator j2)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return replace(i - begin(), i2 - i, j, j2 - j);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-15 14:45:17 +00:00
|
|
|
void lyxstring::swap(lyxstring & str)
|
|
|
|
{
|
|
|
|
if (rep == str.rep) return;
|
|
|
|
Srep * tmp = str.rep;
|
|
|
|
str.rep = rep;
|
|
|
|
rep = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
lyxstring & lyxstring::erase(size_type i, size_type n)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
rep = rep->get_own_copy();
|
|
|
|
if (i == 0 && n >= rep->sz) {
|
|
|
|
rep->sz = 0;
|
|
|
|
} else {
|
|
|
|
n = min(n, rep->sz - i);
|
|
|
|
memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
|
|
|
|
rep->sz -= n;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::iterator lyxstring::erase(iterator i)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
// what iterator is this supposed to return?
|
|
|
|
// the iterator after the one erased
|
|
|
|
erase(i - begin(), 1);
|
|
|
|
return begin(); // BUG
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::iterator lyxstring::erase(iterator first, iterator last)
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
erase(first - begin(), last - first);
|
|
|
|
return begin(); // BUG
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////
|
|
|
|
// Conversion to C-style Strings
|
|
|
|
/////////////////////////////////////
|
|
|
|
|
|
|
|
lyxstring::value_type const * lyxstring::c_str() const
|
|
|
|
{
|
|
|
|
rep->s[length()] = '\0';
|
|
|
|
return rep->s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring::value_type const * lyxstring::data() const
|
|
|
|
{
|
|
|
|
return rep->s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-22 02:26:50 +00:00
|
|
|
lyxstring::size_type lyxstring::copy(value_type * buf, size_type len,
|
|
|
|
size_type pos) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(buf); // OURS!
|
|
|
|
lyx::Assert(pos <= rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
register int nn = min(len, length() - pos);
|
|
|
|
memcpy(buf, &(rep->s[pos]), nn);
|
|
|
|
return nn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
// Comparisons
|
|
|
|
////////////////////
|
|
|
|
|
|
|
|
// Compare funcs should be verified.
|
1999-11-05 06:02:34 +00:00
|
|
|
|
|
|
|
int lyxstring::internal_compare(size_type pos, size_type n,
|
|
|
|
value_type const * s,
|
|
|
|
size_type slen, size_type n2) const
|
|
|
|
{
|
|
|
|
if ((rep->sz == 0 || n == 0) && (!*s || n2 == 0)) return 0;
|
|
|
|
if (!*s) return 1;
|
1999-11-15 10:54:16 +00:00
|
|
|
// since n > n2, min(n, n2) == 0, c == 0 (stops segfault also)
|
1999-11-05 06:02:34 +00:00
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
// 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);
|
1999-11-05 06:02:34 +00:00
|
|
|
n2 = min(n2, slen);
|
2002-03-21 17:09:55 +00:00
|
|
|
if (n == n2)
|
1999-11-05 06:02:34 +00:00
|
|
|
return memcmp(&(rep->s[pos]), s, n);
|
1999-11-15 10:54:16 +00:00
|
|
|
int c = memcmp(&(rep->s[pos]), s, min(n, n2));
|
1999-11-05 06:02:34 +00:00
|
|
|
if (c)
|
|
|
|
return c;
|
|
|
|
if (n < n2)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
|
|
|
int lyxstring::compare(lyxstring const & str) const
|
|
|
|
{
|
|
|
|
TestlyxstringInvariant(this);
|
1999-11-05 06:02:34 +00:00
|
|
|
return internal_compare(0, rep->sz, str.rep->s,
|
|
|
|
str.rep->sz, str.rep->sz);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxstring::compare(value_type const * s) const
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(s); //OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
1999-11-05 06:02:34 +00:00
|
|
|
int n = (!s) ? 0 : strlen(s);
|
|
|
|
return internal_compare(0, rep->sz, s, n, n);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
int lyxstring::compare(size_type pos, size_type n,
|
|
|
|
lyxstring const & str) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(pos <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
1999-11-05 06:02:34 +00:00
|
|
|
return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxstring::compare(size_type pos, size_type n, lyxstring const & str,
|
1999-11-05 06:02:34 +00:00
|
|
|
size_type pos2, size_type n2) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(pos <= rep->sz); // OURS!
|
|
|
|
lyx::Assert(pos2 <= str.rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
1999-11-05 06:02:34 +00:00
|
|
|
return internal_compare(pos, n,
|
|
|
|
str.rep->s + pos2,
|
|
|
|
str.rep->sz - pos2, n2);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int lyxstring::compare(size_type pos, size_type n, value_type const * s,
|
1999-11-05 06:02:34 +00:00
|
|
|
size_type n2) const
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(s && pos <= rep->sz); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
1999-11-05 06:02:34 +00:00
|
|
|
return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
|
1999-10-02 16:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
// Substrings
|
|
|
|
/////////////////
|
|
|
|
|
|
|
|
// i = index, n = length
|
|
|
|
lyxstring lyxstring::substr(size_type i, size_type n) const
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(i <= rep->sz); // STD!
|
1999-10-02 16:21:10 +00:00
|
|
|
TestlyxstringInvariant(this);
|
|
|
|
|
|
|
|
return lyxstring(*this, i, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
// String operators, non member functions
|
|
|
|
/////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool operator==(lyxstring const & a, lyxstring const & b)
|
|
|
|
{
|
|
|
|
return a.compare(b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(lyxstring::value_type const * a, lyxstring const & b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(a); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return b.compare(a) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator==(lyxstring const & a, lyxstring::value_type const * b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(b); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return a.compare(b) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(lyxstring const & a, lyxstring const & b)
|
|
|
|
{
|
|
|
|
return a.compare(b) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(lyxstring::value_type const * a, lyxstring const & b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(a); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return b.compare(a) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=(lyxstring const & a, lyxstring::value_type const * b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(b); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return a.compare(b) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator>(lyxstring const & a, lyxstring const & b)
|
|
|
|
{
|
|
|
|
return a.compare(b) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator>(lyxstring::value_type const * a, lyxstring const & b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(a); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return b.compare(a) < 0; // since we reverse the parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator>(lyxstring const & a, lyxstring::value_type const * b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(b); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return a.compare(b) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<(lyxstring const & a, lyxstring const & b)
|
|
|
|
{
|
|
|
|
return a.compare(b) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<(lyxstring::value_type const * a, lyxstring const & b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(a); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return b.compare(a) > 0; // since we reverse the parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<(lyxstring const & a, lyxstring::value_type const * b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(b); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return a.compare(b) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator>=(lyxstring const & a, lyxstring const & b)
|
|
|
|
{
|
|
|
|
return a.compare(b) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator>=(lyxstring::value_type const * a, lyxstring const & b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(a); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return b.compare(a) <= 0; // since we reverse the parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator>=(lyxstring const & a, lyxstring::value_type const * b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(b); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return a.compare(b) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<=(lyxstring const & a, lyxstring const & b)
|
|
|
|
{
|
|
|
|
return a.compare(b) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<=(lyxstring::value_type const * a, lyxstring const & b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(a); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return b.compare(a) >= 0; // since we reverse the parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator<=(lyxstring const & a, lyxstring::value_type const * b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(b); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
return a.compare(b) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring operator+(lyxstring const & a, lyxstring const & b)
|
|
|
|
{
|
|
|
|
lyxstring tmp(a);
|
|
|
|
tmp += b;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(a); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
lyxstring tmp(a);
|
|
|
|
tmp += b;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring operator+(lyxstring::value_type a, lyxstring const & b)
|
|
|
|
{
|
|
|
|
lyxstring tmp;
|
|
|
|
tmp += a;
|
|
|
|
tmp += b;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b)
|
|
|
|
{
|
2001-04-25 01:39:27 +00:00
|
|
|
lyx::Assert(b); // OURS!
|
1999-10-02 16:21:10 +00:00
|
|
|
lyxstring tmp(a);
|
|
|
|
tmp += b;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lyxstring operator+(lyxstring const & a, lyxstring::value_type b)
|
|
|
|
{
|
|
|
|
lyxstring tmp(a);
|
|
|
|
tmp += b;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
1999-11-15 14:45:17 +00:00
|
|
|
|
|
|
|
void swap(lyxstring & str1, lyxstring & str2)
|
|
|
|
{
|
|
|
|
str1.swap(str2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2002-05-01 22:17:09 +00:00
|
|
|
#if 0
|
1999-10-02 16:21:10 +00:00
|
|
|
istream & operator>>(istream & is, lyxstring & s)
|
|
|
|
{
|
1999-11-22 16:19:48 +00:00
|
|
|
// better solution
|
2002-04-06 12:42:42 +00:00
|
|
|
int w = is.width(0);
|
1999-11-22 16:19:48 +00:00
|
|
|
s.clear();
|
|
|
|
char c = 0;
|
|
|
|
while (is.get(c)) {
|
|
|
|
if (isspace(c)) { is.putback(c); break; }
|
|
|
|
s += c;
|
|
|
|
if (--w == 1) break;
|
|
|
|
}
|
2002-04-06 12:42:42 +00:00
|
|
|
if (s.empty()) is.setstate(std::ios::failbit);
|
1999-10-02 16:21:10 +00:00
|
|
|
return is;
|
|
|
|
}
|
2002-05-01 22:17:09 +00:00
|
|
|
#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;
|
|
|
|
|
|
|
|
// istream_type::sentry cerb(is, false);
|
|
|
|
// if (cerb) {
|
|
|
|
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 (!extracted)
|
|
|
|
is.setstate(std::ios::failbit);
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
#endif
|
1999-10-02 16:21:10 +00:00
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
ostream & operator<<(ostream & o, lyxstring const & s)
|
|
|
|
{
|
|
|
|
return o.write(s.data(), s.length());
|
|
|
|
}
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
istream & getline(istream & is, lyxstring & s,
|
1999-10-13 16:13:30 +00:00
|
|
|
lyxstring::value_type delim)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
|
|
|
// very bad solution
|
1999-11-04 01:40:20 +00:00
|
|
|
char tmp = 0;
|
1999-10-02 16:21:10 +00:00
|
|
|
s.erase();
|
2001-12-05 08:04:20 +00:00
|
|
|
while (is) {
|
1999-11-04 01:40:20 +00:00
|
|
|
is.get(tmp);
|
1999-10-02 16:21:10 +00:00
|
|
|
if (tmp != delim) {
|
|
|
|
s += tmp;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return is;
|
|
|
|
}
|
2001-01-20 15:37:54 +00:00
|
|
|
|
|
|
|
#ifdef TEST_MAIN
|
|
|
|
int main() {
|
|
|
|
lyxstring a = "abcac";
|
|
|
|
cout << a.rfind("ab") << endl;
|
|
|
|
cout << a.rfind("c") << endl;
|
|
|
|
cout << a.rfind("d") << endl;
|
|
|
|
}
|
|
|
|
#endif
|