Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

lyx::string Class Reference

A string class for LyX. More...

#include <lyxstring.h>

List of all members.

Public Types

typedef char value_type
typedef value_typepointer
typedef value_typereference
typedef value_type const & const_reference
typedef size_t size_type
typedef int difference_type
typedef value_typeiterator
typedef const value_typeconst_iterator

Public Methods

iterator begin ()
const_iterator begin () const
iterator end ()
const_iterator end () const
 string ()
 string x;#. More...

 string (string const &, size_type pos=0, size_type n=npos)
 string x(string ...)#. More...

 string (value_type const *, size_type n)
 string x("abc", 2) -> "ab"#. More...

 string (value_type const *)
 string (size_type n, value_type c)
 string(5, 'n') -> "nnnnn". More...

template<class InputIterator>  string (InputIterator begin, InputIterator end)
 ~string ()
size_type size () const
 number of characters. More...

size_type max_size () const
 largest possible string. More...

size_type length () const
bool empty () const
void resize (size_type n, value_type c)
void resize (size_type n)
size_type capacity () const
 size of the memory (in number of elements) allocated. More...

void reserve (size_type res_arg=0)
string & operator= (string const &)
string & operator= (value_type const *)
string & operator= (value_type)
string & assign (string const &)
string & assign (string const &, size_type pos, size_type n)
string & assign (value_type const *p, size_type n)
string & assign (value_type const *p)
string & assign (size_type n, value_type c)
string & assign (const_iterator first, const_iterator last)
const_reference operator[] (size_type) const
 unchecked access. More...

reference operator[] (size_type)
 unchecked access. More...

const_reference at (size_type) const
 checked access. More...

reference at (size_type)
 checked access. More...

string & operator+= (string const &)
string & operator+= (value_type const *)
string & operator+= (value_type)
void push_back (value_type)
string & append (string const &)
string & append (string const &, size_type pos, size_type n)
string & append (value_type const *, size_type n)
string & append (value_type const *)
string & append (size_type n, value_type)
string & append (iterator first, iterator last)
string & insert (size_type pos, string const &)
string & insert (size_type pos, string const &, size_type pos2, size_type n)
string & insert (size_type pos, value_type const *p, size_type n)
string & insert (size_type pos, value_type const *p)
string & insert (size_type pos, size_type n, value_type c)
iterator insert (iterator p, value_type c)
void insert (iterator p, size_type n, value_type c)
void insert (iterator p, iterator first, iterator last)
size_type find (string const &, size_type i=0) const
size_type find (value_type const *p, size_type i, size_type n) const
size_type find (value_type const *p, size_type i=0) const
size_type find (value_type c, size_type i=0) const
size_type rfind (string const &, size_type i=npos) const
size_type rfind (value_type const *p, size_type i, size_type n) const
size_type rfind (value_type const *p, size_type i=npos) const
size_type rfind (value_type c, size_type i=npos) const
size_type find_first_of (string const &, size_type i=0) const
size_type find_first_of (value_type const *p, size_type i, size_type n) const
size_type find_first_of (value_type const *p, size_type i=0) const
size_type find_first_of (value_type c, size_type i=0) const
size_type find_last_of (string const &, size_type i=npos) const
size_type find_last_of (value_type const *p, size_type i, size_type n) const
size_type find_last_of (value_type const *p, size_type i=npos) const
size_type find_last_of (value_type c, size_type i=npos) const
size_type find_first_not_of (string const &, size_type i=0) const
size_type find_first_not_of (value_type const *p, size_type i, size_type n) const
size_type find_first_not_of (value_type const *p, size_type i=0) const
size_type find_first_not_of (value_type c, size_type i=0) const
size_type find_last_not_of (string const &, size_type i=npos) const
size_type find_last_not_of (value_type const *p, size_type i, size_type n) const
size_type find_last_not_of (value_type const *p, size_type i=npos) const
size_type find_last_not_of (value_type c, size_type i=npos) const
string & replace (size_type i, size_type n, string const &str)
string & replace (size_type i, size_type n, string const &s, size_type i2, size_type n2)
string & replace (size_type i, size_type n, value_type const *p, size_type n2)
string & replace (size_type i, size_type n, value_type const *p)
string & replace (size_type i, size_type n, size_type n2, value_type c)
string & replace (size_type i, size_type n, value_type c)
 FY! FY! FY! go away ! More...

string & replace (iterator i, iterator i2, const string &str)
string & replace (iterator i, iterator i2, value_type const *p, size_type n)
string & replace (iterator i, iterator i2, value_type const *p)
string & replace (iterator i, iterator i2, size_type n, value_type c)
string & replace (iterator i, iterator i2, iterator j, iterator j2)
void swap (string &str)
string & erase (size_type i=0, size_type n=npos)
 Erase n chars from position i. More...

string & clear ()
iterator erase (iterator i)
iterator erase (iterator first, iterator last)
value_type const * c_str () const
value_type const * data () const
size_type copy (value_type *buf, size_type len, size_type pos=0) const
int compare (string const &str) const
int compare (value_type const *p) const
int compare (size_type pos, size_type n, string const &str) const
int compare (size_type pos, size_type n, string const &str, size_type pos2, size_type n2) const
int compare (size_type pos, size_type n, value_type const *p, size_type n2=npos) const
string substr (size_type i=0, size_type n=npos) const

Static Public Attributes

const size_type npos
 "all characters" marker. More...


Private Methods

int internal_compare (size_type pos, size_type n, value_type const *s, size_type slen, size_type n2) const
 Compare this with s. works with embedded '\0' chars also. More...


Private Attributes

Sreprep
 A string is a pointer to it's representation. More...


Friends

struct Srep


Detailed Description

A string class for LyX.

This is a permanent String class. It is modeled closely after the C++ STL string class. In comparison with STL string lyxstring lack support for reverse iterators and allocators, also char_traits is not used. In most other senses it is written to be a drop in replacement for STL string (or as a transition tool). So documentation for STL string should be valid for lyxstring too.

Notes for usage:

When you declare an lyxstring, it is initially empty. There is no need to do things like lyxstring a= "";#, especially not in constructors.

If you want to use a default empty lyxstring as a parameter, use

void foo(lyxstring par = lyxstring()); // Correct#

rather than

void foo(lyxstring par = ""); // WRONG!# void foo(lyxstring par = 0); // WRONG!#

(The last one is only wrong because some compilers can't handle it.)

Methods that take an index as parameter all follow this rule: Valid indexes go from 0 to length()-1. \begin{tabular}{rl} Correct: & foo.substring(0, length()-1);# \ Wrong: & bar.substring(0, length());# \end{tabular}

It is important that you declare lyxstring as const if possible, because some methods are much more efficient in const versions.

If you want to check whether a string is empty, do

if (foo.empty()) something right#

rather than something along the lines of

if (!foo) completely wrong#

When you want to copy an lyxstring, just do

lyxstring a, b = "String";# a = b; // That's it!#

not something like

lyxstring a, b = "String";# a = b.copy(); // This leaks. // and does not work either. #

The class automatically handles deep copying when required.

Definition at line 89 of file lyxstring.h.


Member Typedef Documentation

typedef const value_type* lyx::string::const_iterator
 

Definition at line 114 of file lyxstring.h.

typedef value_type const& lyx::string::const_reference
 

Definition at line 103 of file lyxstring.h.

typedef int lyx::string::difference_type
 

Definition at line 109 of file lyxstring.h.

typedef value_type* lyx::string::iterator
 

Definition at line 112 of file lyxstring.h.

typedef value_type* lyx::string::pointer
 

Definition at line 97 of file lyxstring.h.

typedef value_type& lyx::string::reference
 

Definition at line 100 of file lyxstring.h.

typedef size_t lyx::string::size_type
 

Definition at line 106 of file lyxstring.h.

Referenced by find, find_first_not_of, find_first_of, find_last_not_of, find_last_of, insert, length, max_size, lyx::string::Srep::replace, resize, lyx::string::Srep::resize, and rfind.

typedef char lyx::string::value_type
 

Definition at line 94 of file lyxstring.h.

Referenced by append, lyx::string::Srep::append, lyx::string::Srep::assign, insert, lyx::string::Srep::insert, operator=, lyx::string::Srep::push_back, replace, lyx::string::Srep::replace, lyx::string::Srep::resize, and lyx::string::Srep::Srep.


Constructor & Destructor Documentation

lyx::string::string  
 

string x;#.

Definition at line 402 of file lyxstring.C.

References rep, and Srep.

Referenced by clear, and substr.

00403 {
00404         static Srep empty_rep(0, "");
00405         ++empty_rep.ref;
00406         rep = &empty_rep;
00407 }

lyx::string::string string const &   ,
size_type    pos = 0,
size_type    n = npos
 

string x(string ...)#.

Definition at line 410 of file lyxstring.C.

References lyx::support::Assert, rep, and Srep.

00411 {
00412         Assert(pos <= x.rep->sz); // STD!
00413         if (pos == 0 && n >= x.length()) { // this is the default
00414                 x.rep->ref++;
00415                 rep = x.rep;
00416         } else {
00417                 rep = new Srep(min(n, x.rep->sz - pos), &(x.rep->s[pos]));
00418         }
00419 }

lyx::string::string value_type const *   ,
size_type    n
 

string x("abc", 2) -> "ab"#.

Definition at line 422 of file lyxstring.C.

References lyx::support::Assert, npos, rep, and Srep.

00423 {
00424         Assert(s && n < npos); // STD!
00425         static Srep empty_rep(0, "");
00426         if (n) { // n > 0
00427                 rep = new Srep(n, s);
00428         } else {
00429                 ++empty_rep.ref;
00430                 rep = &empty_rep;
00431         }
00432 }

lyx::string::string value_type const *   
 

Definition at line 435 of file lyxstring.C.

References lyx::support::Assert, rep, and Srep.

00436 {
00437         Assert(s); // STD!
00438         static Srep empty_rep(0, "");
00439         if (*s) { // s is not empty string
00440                 rep = new Srep(strlen(s), s);
00441         } else {
00442                 ++empty_rep.ref;
00443                 rep = &empty_rep;
00444         }
00445 }

lyx::string::string size_type    n,
value_type    c
 

string(5, 'n') -> "nnnnn".

Definition at line 448 of file lyxstring.C.

References lyx::support::Assert, npos, rep, and Srep.

00449 {
00450         Assert(n < npos); // STD!
00451         rep = new Srep(n, c);
00452 }

template<class InputIterator>
lyx::string::string InputIterator    begin,
InputIterator    end
[inline]
 

Definition at line 172 of file lyxstring.h.

References begin, end, and push_back.

00172                                                        {
00173                 while (begin != end) {
00174                         push_back(*begin);
00175                         ++begin;
00176                 }
00177         }

lyx::string::~string  
 

Definition at line 465 of file lyxstring.C.

References lyx::string::Srep::ref, and rep.

00466 {
00467         if (--rep->ref == 0) delete rep;
00468 }


Member Function Documentation

string & lyx::string::append iterator    first,
iterator    last
 

Definition at line 801 of file lyxstring.C.

References lyx::string::Srep::append, lyx::string::Srep::get_own_copy, rep, and TeststringInvariant.

00802 {
00803         TeststringInvariant(this);
00804 
00805         rep = rep->get_own_copy();
00806         rep->append(last - first, first);
00807         return *this;
00808 }

string & lyx::string::append size_type    n,
value_type   
 

Definition at line 788 of file lyxstring.C.

References lyx::string::Srep::append, lyx::string::Srep::get_own_copy, rep, TeststringInvariant, and value_type.

00789 {
00790         TeststringInvariant(this);
00791 
00792         value_type * tmp = new value_type[n];
00793         memset(tmp, c, n);
00794         rep = rep->get_own_copy();
00795         rep->append(n, tmp);
00796         delete[] tmp;
00797         return *this;
00798 }

string & lyx::string::append value_type const *   
 

Definition at line 781 of file lyxstring.C.

References append, and lyx::support::Assert.

00782 {
00783         Assert(p); // OURS!
00784         return append(p, strlen(p));
00785 }

string & lyx::string::append value_type const *   ,
size_type    n
 

Definition at line 769 of file lyxstring.C.

References lyx::string::Srep::append, lyx::support::Assert, lyx::string::Srep::get_own_copy, rep, and TeststringInvariant.

00770 {
00771         Assert(p); // OURS!
00772         TeststringInvariant(this);
00773 
00774         if (!*p || !n) return *this;
00775         rep = rep->get_own_copy();
00776         rep->append(n, p);
00777         return *this;
00778 }

string & lyx::string::append string const &   ,
size_type    pos,
size_type    n
 

Definition at line 760 of file lyxstring.C.

References append, lyx::support::Assert, and TeststringInvariant.

00761 {
00762         Assert(pos <= x.rep->sz); // STD!
00763         TeststringInvariant(this);
00764 
00765         return append(x.substr(pos, n));
00766 }

string & lyx::string::append string const &   
 

Definition at line 749 of file lyxstring.C.

References lyx::string::Srep::append, lyx::string::Srep::get_own_copy, rep, and TeststringInvariant.

Referenced by append, and operator+=.

00750 {
00751         TeststringInvariant(this);
00752 
00753         if (x.empty()) return *this;
00754         rep = rep->get_own_copy();
00755         rep->append(x.length(), x.rep->s);
00756         return *this;
00757 }

string & lyx::string::assign const_iterator    first,
const_iterator    last
 

Definition at line 653 of file lyxstring.C.

References lyx::string::Srep::assign, lyx::string::Srep::get_own_copy, rep, and TeststringInvariant.

00654 {
00655         TeststringInvariant(this);
00656 
00657         rep = rep->get_own_copy();
00658         rep->assign(last - first, first);
00659         return *this;
00660 }

string & lyx::string::assign size_type    n,
value_type    c
 

Definition at line 643 of file lyxstring.C.

References lyx::string::Srep::assign, lyx::string::Srep::get_own_copy, rep, and TeststringInvariant.

00644 {
00645         TeststringInvariant(this);
00646 
00647         rep = rep->get_own_copy();
00648         rep->assign(n, ch);
00649         return *this;
00650 }

string & lyx::string::assign value_type const *    p
 

Definition at line 634 of file lyxstring.C.

References lyx::support::Assert, assign, and TeststringInvariant.

00635 {
00636         Assert(s); // OURS!
00637         TeststringInvariant(this);
00638 
00639         return assign(s, strlen(s));
00640 }

string & lyx::string::assign value_type const *    p,
size_type    n
 

Definition at line 619 of file lyxstring.C.

References lyx::support::Assert, lyx::string::Srep::assign, npos, lyx::string::Srep::ref, rep, Srep, and TeststringInvariant.

00620 {
00621         Assert(s && n < npos); // STD!
00622         TeststringInvariant(this);
00623 
00624         if (rep->ref == 1) // recycle rep
00625                 rep->assign(n, s);
00626         else {
00627                 --rep->ref;
00628                 rep = new Srep(n, s);
00629         }
00630         return *this;
00631 }

string & lyx::string::assign string const &   ,
size_type    pos,
size_type    n
 

Definition at line 610 of file lyxstring.C.

References lyx::support::Assert, assign, and TeststringInvariant.

00611 {
00612         Assert(pos <= x.rep->sz); // STD!
00613         TeststringInvariant(this);
00614 
00615         return assign(x.substr(pos, n));
00616 }

string & lyx::string::assign string const &   
 

Definition at line 599 of file lyxstring.C.

References lyx::string::Srep::ref, rep, and TeststringInvariant.

Referenced by assign, and operator=.

00600 {
00601         TeststringInvariant(this);
00602 
00603         x.rep->ref++; // protect against ``st = st''
00604         if (--rep->ref == 0) delete rep;
00605         rep = x.rep; // share representation
00606         return *this;
00607 }

string::reference lyx::string::at size_type   
 

checked access.

Definition at line 700 of file lyxstring.C.

References lyx::support::Assert, lyx::string::Srep::get_own_copy, rep, lyx::string::Srep::s, and TeststringInvariant.

00701 {
00702         Assert(n < rep->sz); // STD!
00703         TeststringInvariant(this);
00704 
00705         rep = rep->get_own_copy();
00706         return rep->s[n];
00707 }

string::const_reference lyx::string::at size_type    const
 

checked access.

Definition at line 693 of file lyxstring.C.

References lyx::support::Assert, rep, and lyx::string::Srep::s.

00694 {
00695         Assert(n < rep->sz); // STD!
00696         return rep->s[n];
00697 }

string::const_iterator lyx::string::begin   const
 

Definition at line 481 of file lyxstring.C.

References rep, and lyx::string::Srep::s.

00482 {
00483         return rep->s;
00484 }

string::iterator lyx::string::begin  
 

Definition at line 474 of file lyxstring.C.

References lyx::string::Srep::get_own_copy, rep, and lyx::string::Srep::s.

Referenced by erase, insert, replace, and string.

00475 {
00476         rep = rep->get_own_copy();
00477         return rep->s;
00478 }

string::value_type const * lyx::string::c_str   const
 

Definition at line 1448 of file lyxstring.C.

References length, rep, and lyx::string::Srep::s.

01449 {
01450         rep->s[length()] = '\0';
01451         return rep->s;
01452 }

string::size_type lyx::string::capacity   const
 

size of the memory (in number of elements) allocated.

Definition at line 546 of file lyxstring.C.

References rep, and lyx::string::Srep::res.

00547 {
00548         return rep->res;
00549 }

string& lyx::string::clear   [inline]
 

Definition at line 470 of file lyxstring.h.

References erase, npos, and string.

00470                          {
00471                 return erase(0, npos);
00472         }

int lyx::string::compare size_type    pos,
size_type    n,
value_type const *    p,
size_type    n2 = npos
const
 

Definition at line 1541 of file lyxstring.C.

References lyx::support::Assert, internal_compare, and TeststringInvariant.

01543 {
01544         Assert(s && pos <= rep->sz); // OURS!
01545         TeststringInvariant(this);
01546         return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2);
01547 }

int lyx::string::compare size_type    pos,
size_type    n,
string const &    str,
size_type    pos2,
size_type    n2
const
 

Definition at line 1529 of file lyxstring.C.

References lyx::support::Assert, internal_compare, and TeststringInvariant.

01531 {
01532         Assert(pos <= rep->sz); // OURS!
01533         Assert(pos2 <= str.rep->sz); // OURS!
01534         TeststringInvariant(this);
01535         return internal_compare(pos, n,
01536                                 str.rep->s + pos2,
01537                                 str.rep->sz - pos2, n2);
01538 }

int lyx::string::compare size_type    pos,
size_type    n,
string const &    str
const
 

Definition at line 1520 of file lyxstring.C.

References lyx::support::Assert, internal_compare, and TeststringInvariant.

01522 {
01523         Assert(pos <= rep->sz); // OURS!
01524         TeststringInvariant(this);
01525         return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz);
01526 }

int lyx::string::compare value_type const *    p const
 

Definition at line 1511 of file lyxstring.C.

References lyx::support::Assert, internal_compare, rep, lyx::string::Srep::sz, and TeststringInvariant.

01512 {
01513         Assert(s); //OURS!
01514         TeststringInvariant(this);
01515         int n = (!s) ? 0 : strlen(s);
01516         return internal_compare(0, rep->sz, s, n, n);
01517 }

int lyx::string::compare string const &    str const
 

Definition at line 1503 of file lyxstring.C.

References internal_compare, rep, lyx::string::Srep::sz, and TeststringInvariant.

01504 {
01505         TeststringInvariant(this);
01506         return internal_compare(0, rep->sz, str.rep->s,
01507                                 str.rep->sz, str.rep->sz);
01508 }

string::size_type lyx::string::copy value_type   buf,
size_type    len,
size_type    pos = 0
const
 

Definition at line 1461 of file lyxstring.C.

References lyx::support::Assert, length, rep, lyx::string::Srep::s, and TeststringInvariant.

01463 {
01464         Assert(buf); // OURS!
01465         Assert(pos <= rep->sz); // STD!
01466         TeststringInvariant(this);
01467 
01468         register int nn = min(len, length() - pos);
01469         memcpy(buf, &(rep->s[pos]), nn);
01470         return nn;
01471 }

string::value_type const * lyx::string::data   const
 

Definition at line 1455 of file lyxstring.C.

References rep, and lyx::string::Srep::s.

01456 {
01457         return rep->s;
01458 }

bool lyx::string::empty   const [inline]
 

Definition at line 192 of file lyxstring.h.

References size.

Referenced by createInset, initSymbols, InsetBibitem::InsetBibitem, and isEndOfData.

00192 { return size() == 0; }

string::const_iterator lyx::string::end   const
 

Definition at line 494 of file lyxstring.C.

References rep, lyx::string::Srep::s, and lyx::string::Srep::sz.

00495 {
00496         return rep->s + rep->sz;
00497 }

string::iterator lyx::string::end  
 

Definition at line 487 of file lyxstring.C.

References lyx::string::Srep::get_own_copy, rep, lyx::string::Srep::s, and lyx::string::Srep::sz.

Referenced by string.

00488 {
00489         rep = rep->get_own_copy();
00490         return rep->s + rep->sz;
00491 }

string::iterator lyx::string::erase iterator    first,
iterator    last
 

Definition at line 1435 of file lyxstring.C.

References begin, erase, and TeststringInvariant.

01436 {
01437         TeststringInvariant(this);
01438 
01439         erase(first - begin(), last - first);
01440         return begin(); // BUG
01441 }

string::iterator lyx::string::erase iterator    i
 

Definition at line 1424 of file lyxstring.C.

References begin, erase, and TeststringInvariant.

01425 {
01426         TeststringInvariant(this);
01427 
01428         // what iterator is this supposed to return?
01429         // the iterator after the one erased
01430         erase(i - begin(), 1);
01431         return begin(); // BUG
01432 }

string & lyx::string::erase size_type    i = 0,
size_type    n = npos
 

Erase n chars from position i.

Definition at line 1407 of file lyxstring.C.

References lyx::support::Assert, lyx::string::Srep::get_own_copy, rep, lyx::string::Srep::s, lyx::string::Srep::sz, and TeststringInvariant.

Referenced by clear, and erase.

01408 {
01409         Assert(i <= rep->sz); // STD!
01410         TeststringInvariant(this);
01411 
01412         rep = rep->get_own_copy();
01413         if (i == 0 && n >= rep->sz) {
01414                 rep->sz = 0;
01415         } else {
01416                 n = min(n, rep->sz - i);
01417                 memmove(&(rep->s[i]), &(rep->s[i + n]), rep->sz - i - n);
01418                 rep->sz -= n;
01419         }
01420         return *this;
01421 }

string::size_type lyx::string::find value_type    c,
size_type    i = 0
const
 

Definition at line 972 of file lyxstring.C.

References npos, rep, lyx::string::Srep::s, size_type, lyx::string::Srep::sz, and TeststringInvariant.

00973 {
00974         if (!rep->sz || i >= rep->sz) return npos;
00975 
00976         TeststringInvariant(this);
00977 
00978         for (size_type t = 0; t + i < rep->sz; ++t) {
00979                 if (rep->s[t + i] == c) return t + i;
00980         }
00981         return npos;
00982 }

string::size_type lyx::string::find value_type const *    p,
size_type    i = 0
const
 

Definition at line 960 of file lyxstring.C.

References lyx::support::Assert, find, npos, rep, lyx::string::Srep::sz, and TeststringInvariant.

00961 {
00962         Assert(s); // OURS!
00963         if (!rep->sz || i >= rep->sz) return