/* This file is part of * ====================================================== * * LyX, The Document Processor * * Copyright (C) 1995 Matthias Ettrich * Copyright (C) 1995-1998 The LyX Team. *
*======================================================*/
// #include <assert.h> // Hmm, make depend crashes with this one in. (Asger) #include"LString.h" #include <stdio.h> #include <stdlib.h> #include <ctype.h>
staticconstunsignedshort xtra = 4; // The extra space is used to reduce the number of allocations // and copies required if a string is unshared. The performance // boost can be considerable -- in some tests using LyX I found // a 97% reduction in the runtime of operator+=(char) in some // code for writing LaTeX files using LStrings. // This was originally implemented using: // xtra = 4 - (sizeof(srep) + len) % 4; // where len is the length of the new string. // This was intended to ensure the string was always aligned // within 4-byte boundaries but after testing with xtra = 4, // and finding a significant improvement I decided to just // leave it at 4. ARRae.
lose();
p = &empty_rep;
empty_rep.n++; return *this;
}
char& LString::operator[](int i)
{ #ifdef DEVEL_VERSION if (i < 0 || i >= length()) {
fprintf(stderr,"LString::operator[]: Index out of range: '%s' %d\n", p->s, i);
abort();
} #endif
#ifndefconst charconst& LString::operator[](int i) const
{ #ifdef DEVEL_VERSION if (i < 0 || i >= length()) {
fprintf(stderr,"LString::operator[] const: Index out of range: '%s' i:%d.\n",p->s,i);
abort();
} #endif
return p->s[i];
} #endif/* ndef const */
LString &LString::operator+=(LString const & x)
{ if (x.empty()) return *this;
registerunsignedintconst len = length() + x.length(); if (p->n || p->e < x.length()) {
srep *np = (srep *) newchar[sizeof(srep) + len + xtra];
np->l = len;
np->n = 0;
np->e = xtra;
memcpy(np->s, p->s, length());
memcpy(np->s + length(), x.p->s, x.length() + 1);
lose(); // disconnect self
p = np;
} else { // in cases where x += x and x is short the // explicit setting of the '\0' stops any problems
memcpy(p->s + length(), x.p->s, x.length());
p->s[len] = '\0';
p->l += x.length();
p->e -= x.length();
}
return *this;
}
LString &LString::operator+=(charconst *x)
{ if (!x || *x==0) return *this;
if (i1==i2)
this->operator=(p->s[i1]); else { char *str = newchar[i2 - i1 +2]; int i; for (i=0; i1<=i2; str[i++] = p->s[i1++]);
str[i] = 0;
this->operator=(str); delete[] (char*)str;
} return *this;
}
// ale970405+lasgoutt-970425
LString LString::token(char delim, int n) const
{ int k=0, i;
LString tokbuf;
tokbuf = *this; // Find delimiter or end of string for (i = 0; i < tokbuf.length(); i++) { if (tokbuf[i] == delim) { if (n > 0) {
k = i+1;
n--;
} elsebreak;
}
}
// Return the token if not empty if (n == 0 && k<i){ return tokbuf.substring(k, i-1);
} else { return LString();
}
}
// this could probably be faster and/or cleaner, but it seems to work (JMarc) int LString::tokenPos(char delim, LString const &tok)
{ int i=0;
LString str = *this;
LString tmptok;
while (!str.empty()) {
str.split(tmptok, delim); if (tok==tmptok) return i;
i++;
} return -1;
}
LString& LString::split(LString & piece, char delim)
{ int i=0; // Find delimiter or end of string while (i<length() && p->s[i] != delim)
i++; // If not the first, we go for a substring if (i>0) {
piece = *this;
piece.substring(0, i-1);
} else
piece.clean();
if (i < length()-1)
this->substring(i+1, length()-1); else
clean(); return *this;
}
LString& LString::split(char delim)
{ int i=0; // Find delimiter or end of string while (i<length() && p->s[i] != delim)
i++;
if (i < length()-1)
this->substring(i+1, length()-1); else
clean(); return *this;
}
// ale970521
LString& LString::rsplit(LString & piece, char delim)
{ int i=length()-1; // Find delimiter or begin of string while (i>=0 && p->s[i] != delim)
i--; // If not the last, we go for a substring if (i < length()-1) {
piece = *this;
piece.substring(0, i-1);
this->substring(i+1, length()-1);
} else {
piece.clean();
clean();
} return *this;
}
LString& LString::strip(charconst c)
{ int i=length()-1; for (; i>=0 && p->s[i] == c; i--); if (i<0)
clean(); else
this->substring(0, i); return *this;
}
LString& LString::frontStrip(charconst c)
{ int i=0; while (i < length() && p->s[i] == c) i++; if (i > 0) if (i == length())
clean(); else
this->substring (i, length()-1); return *this;
}
if (temp.empty()) { // Last chunk, see if tail matches
temp = *this;
temp.substring(sl - chunk.length(), sl - 1); return temp == chunk;
} else { // Middle chunk, see if we can find a match bool match = false; while (!match && si<sl) {
temp = *this;
temp.substring(si, sl - 1);
match = temp.prefixIs(chunk.c_str());
si++;
}; if (!match) returnfalse;
si += chunk.length()-1;
pi += chunk.length(); if (si==sl && pi==pl-1) returntrue;
}
} elseif (operator[](si++) != pattern[pi++]) { returnfalse;
}
} if (pi < pl || si < sl) returnfalse; returntrue;
}
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.