xref: /AOO41X/main/soltools/support/simstr.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_soltools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <simstr.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <string.h>  // strlen(), memcpy(), memset()
35*cdf0e10cSrcweir #include <ctype.h>   // tolower()
36*cdf0e10cSrcweir #include <limits.h>  // INT_MAX
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir const char NULCH = '\0';
39*cdf0e10cSrcweir const int  NO_POS = -1;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir Simstr::Simstr(const char * s_)
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir    if (s_ == 0)
45*cdf0e10cSrcweir       {
46*cdf0e10cSrcweir          len = 0;
47*cdf0e10cSrcweir          sz = new char[1];
48*cdf0e10cSrcweir          *sz = 0;
49*cdf0e10cSrcweir       }
50*cdf0e10cSrcweir    else
51*cdf0e10cSrcweir       {
52*cdf0e10cSrcweir          len = strlen(s_);
53*cdf0e10cSrcweir          sz = new char[len+1];
54*cdf0e10cSrcweir          memcpy(sz,s_,len+1);
55*cdf0e10cSrcweir       }
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir Simstr::Simstr(const char * anybytes, int  nrOfBytes)
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     if (anybytes == 0)
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir 	    len = 0;
63*cdf0e10cSrcweir 		sz = new char[1];
64*cdf0e10cSrcweir 		*sz = 0;
65*cdf0e10cSrcweir         return;
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     int slen = static_cast<int>( strlen(anybytes) );
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     len =  slen < nrOfBytes
71*cdf0e10cSrcweir                   ? slen
72*cdf0e10cSrcweir                   : nrOfBytes;
73*cdf0e10cSrcweir     sz = new char[len+1];
74*cdf0e10cSrcweir     memcpy( sz, anybytes, len );
75*cdf0e10cSrcweir     *( sz + len ) = 0;
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir Simstr::Simstr(char c, int anzahl)
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir    if (anzahl < 1)
81*cdf0e10cSrcweir       {
82*cdf0e10cSrcweir          len = 0;
83*cdf0e10cSrcweir          sz = new char[1];
84*cdf0e10cSrcweir          *sz = 0;
85*cdf0e10cSrcweir       }
86*cdf0e10cSrcweir    else
87*cdf0e10cSrcweir       {
88*cdf0e10cSrcweir          len = anzahl;
89*cdf0e10cSrcweir          sz = new char[len+1];
90*cdf0e10cSrcweir          memset(sz,c,anzahl);
91*cdf0e10cSrcweir          sz[len] = 0;
92*cdf0e10cSrcweir       }
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir Simstr::Simstr( const char *   anybytes,
96*cdf0e10cSrcweir 				int            firstBytesPos,
97*cdf0e10cSrcweir 				int            nrOfBytes)
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir    unsigned slen = strlen(anybytes);
100*cdf0e10cSrcweir    if (anybytes == 0 || slen <= unsigned(firstBytesPos))
101*cdf0e10cSrcweir 	  {
102*cdf0e10cSrcweir 		 len = 0;
103*cdf0e10cSrcweir 		 sz = new char[1];
104*cdf0e10cSrcweir 		 *sz = 0;
105*cdf0e10cSrcweir 	  }
106*cdf0e10cSrcweir    else
107*cdf0e10cSrcweir 	  {
108*cdf0e10cSrcweir          int maxLen = slen - unsigned(firstBytesPos);
109*cdf0e10cSrcweir          len =  maxLen < nrOfBytes
110*cdf0e10cSrcweir                   ? maxLen
111*cdf0e10cSrcweir                   : nrOfBytes;
112*cdf0e10cSrcweir          sz = new char[len+1];
113*cdf0e10cSrcweir          memcpy(sz,anybytes+firstBytesPos,len);
114*cdf0e10cSrcweir          *(sz+len) = 0;
115*cdf0e10cSrcweir       }
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir Simstr::Simstr(const Simstr & S)
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir    len = S.len;
122*cdf0e10cSrcweir    sz = new char[len+1];
123*cdf0e10cSrcweir    memcpy(sz,S.sz,len+1);
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir Simstr & Simstr::operator=(const Simstr & S)
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir    if (sz == S.sz)
129*cdf0e10cSrcweir       return *this;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir    delete [] sz;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir    len = S.len;
134*cdf0e10cSrcweir    sz = new char[len+1];
135*cdf0e10cSrcweir    memcpy(sz,S.sz,len+1);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir    return *this;
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir Simstr::~Simstr()
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir    delete [] sz;
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir char &
146*cdf0e10cSrcweir Simstr::ch(int  n)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir    static char nullCh = NULCH;
149*cdf0e10cSrcweir    nullCh = NULCH;
150*cdf0e10cSrcweir    if (n >= long(len) || n < 0)
151*cdf0e10cSrcweir       return nullCh;
152*cdf0e10cSrcweir    else
153*cdf0e10cSrcweir       return sz[unsigned(n)];
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir const Simstr &
157*cdf0e10cSrcweir Simstr::null_()
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir     static Simstr aNull_;
160*cdf0e10cSrcweir     return aNull_;
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir Simstr
165*cdf0e10cSrcweir Simstr::operator+(const Simstr & S) const
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir    Simstr ret = sz;
168*cdf0e10cSrcweir    ret.push_back(S);
169*cdf0e10cSrcweir    return ret;
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir Simstr &
173*cdf0e10cSrcweir Simstr::operator+=(const Simstr & S)
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir    push_back(S);
176*cdf0e10cSrcweir    return *this;
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir Simstr &
180*cdf0e10cSrcweir Simstr::operator+=(const char * s_)
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir     Simstr a(s_);
183*cdf0e10cSrcweir     push_back(a);
184*cdf0e10cSrcweir     return *this;
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir // REL
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir bool
191*cdf0e10cSrcweir Simstr::operator==(const Simstr & S) const
192*cdf0e10cSrcweir { return !strcmp(sz,S.sz) ? true : false; }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir bool
195*cdf0e10cSrcweir Simstr::operator!=(const Simstr & S) const
196*cdf0e10cSrcweir { return strcmp(sz,S.sz) ? true : false; }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir bool
199*cdf0e10cSrcweir Simstr::operator<(const Simstr & S) const
200*cdf0e10cSrcweir { return (strcmp(sz,S.sz) < 0) ? true : false; }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir bool
203*cdf0e10cSrcweir Simstr::operator>(const Simstr & S) const
204*cdf0e10cSrcweir { return (strcmp(sz,S.sz) > 0) ? true : false; }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir bool
207*cdf0e10cSrcweir Simstr::operator<=(const Simstr & S) const
208*cdf0e10cSrcweir { return (strcmp(sz,S.sz) <= 0) ? true : false; }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir bool
211*cdf0e10cSrcweir Simstr::operator>=(const Simstr & S) const
212*cdf0e10cSrcweir { return (strcmp(sz,S.sz) >= 0) ? true : false; }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir // **************          LIST - Funktionen        *****************
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir // Einzelzugriff
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir char
223*cdf0e10cSrcweir Simstr::get(int  n) const     { return (n >= len || n < 0) ? 0 : sz[n]; }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir char
226*cdf0e10cSrcweir Simstr::get_front() const        { return sz[0]; }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir char
229*cdf0e10cSrcweir Simstr::get_back() const        { return  len ? sz[len-1] : 0; }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir Simstr
232*cdf0e10cSrcweir Simstr::get(int  startPos, int  anzahl) const
233*cdf0e10cSrcweir {
234*cdf0e10cSrcweir    if (startPos >= len || startPos < 0 || anzahl < 1)
235*cdf0e10cSrcweir       return "";
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir    int anz = len - startPos < anzahl ? len - startPos : anzahl;
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir    Simstr ret(' ',anz);
240*cdf0e10cSrcweir    memcpy(ret.sz, sz+startPos, anz);
241*cdf0e10cSrcweir    return ret;
242*cdf0e10cSrcweir }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir Simstr
245*cdf0e10cSrcweir Simstr::get_front(int  anzahl) const
246*cdf0e10cSrcweir {
247*cdf0e10cSrcweir    int anz = len < anzahl ? len : anzahl;
248*cdf0e10cSrcweir    if (anz < 1)
249*cdf0e10cSrcweir       return "";
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir    Simstr ret(' ',anz);
252*cdf0e10cSrcweir    memcpy(ret.sz, sz, anz);
253*cdf0e10cSrcweir    return ret;
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir Simstr
257*cdf0e10cSrcweir Simstr::get_back(int  anzahl) const
258*cdf0e10cSrcweir {
259*cdf0e10cSrcweir    int anz = len < anzahl ? len : anzahl;
260*cdf0e10cSrcweir    if (anz < 1)
261*cdf0e10cSrcweir       return "";
262*cdf0e10cSrcweir    int start = len-anz;
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir    Simstr ret(' ',anz);
265*cdf0e10cSrcweir    memcpy(ret.sz, sz+start, anz);
266*cdf0e10cSrcweir    return ret;
267*cdf0e10cSrcweir }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir Simstr
270*cdf0e10cSrcweir Simstr::get_first_token(char c) const
271*cdf0e10cSrcweir {
272*cdf0e10cSrcweir    int posc = pos_first(c);
273*cdf0e10cSrcweir    if (posc != NO_POS)
274*cdf0e10cSrcweir       return get_front(posc);
275*cdf0e10cSrcweir    else
276*cdf0e10cSrcweir       return sz;
277*cdf0e10cSrcweir }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir Simstr
280*cdf0e10cSrcweir Simstr::get_last_token(char c) const
281*cdf0e10cSrcweir {
282*cdf0e10cSrcweir    int posc = pos_last(c);
283*cdf0e10cSrcweir    if (posc != NO_POS)
284*cdf0e10cSrcweir       return get_back(len-posc-1);
285*cdf0e10cSrcweir    else
286*cdf0e10cSrcweir       return sz;
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir // Insert
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir void
294*cdf0e10cSrcweir Simstr::insert(int  pos, char c)
295*cdf0e10cSrcweir {
296*cdf0e10cSrcweir    if (pos < 0 || pos > len)
297*cdf0e10cSrcweir       return;
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir    char * result = new char[len+2];
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir    memcpy(result,sz,pos);
302*cdf0e10cSrcweir    result[pos] = c;
303*cdf0e10cSrcweir    memcpy(result+pos+1,sz+pos,len-pos+1);
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir    delete [] sz;
306*cdf0e10cSrcweir    sz = result;
307*cdf0e10cSrcweir    len++;
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir void
311*cdf0e10cSrcweir Simstr::push_front(char c)
312*cdf0e10cSrcweir {
313*cdf0e10cSrcweir    char * result = new char[len+2];
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir    result[0] = c;
316*cdf0e10cSrcweir    memcpy(result+1,sz,len+1);
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir    delete [] sz;
319*cdf0e10cSrcweir    sz = result;
320*cdf0e10cSrcweir    len++;
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir void
324*cdf0e10cSrcweir Simstr::push_back(char c)
325*cdf0e10cSrcweir {
326*cdf0e10cSrcweir    char * result = new char[len+2];
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir    memcpy(result,sz,len);
329*cdf0e10cSrcweir    result[len] = c;
330*cdf0e10cSrcweir    result[len+1] = 0;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir    delete [] sz;
333*cdf0e10cSrcweir    sz = result;
334*cdf0e10cSrcweir    len++;
335*cdf0e10cSrcweir }
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir void
338*cdf0e10cSrcweir Simstr::insert(int  pos, const Simstr & S)
339*cdf0e10cSrcweir {
340*cdf0e10cSrcweir    if (pos < 0 || pos > len)
341*cdf0e10cSrcweir       return;
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir    char * result = new char[len+1+S.len];
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir    memcpy(result,sz,pos);
346*cdf0e10cSrcweir    memcpy(result+pos,S.sz,S.len);
347*cdf0e10cSrcweir    memcpy(result+pos+S.len,sz+pos,len-pos+1);
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir    delete [] sz;
350*cdf0e10cSrcweir    sz = result;
351*cdf0e10cSrcweir    len += S.len;
352*cdf0e10cSrcweir }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir void
355*cdf0e10cSrcweir Simstr::push_front(const Simstr & S)
356*cdf0e10cSrcweir {
357*cdf0e10cSrcweir    char * result = new char[len+1+S.len];
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir    memcpy(result,S.sz,S.len);
360*cdf0e10cSrcweir    memcpy(result+S.len,sz,len+1);
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir    delete [] sz;
363*cdf0e10cSrcweir    sz = result;
364*cdf0e10cSrcweir    len += S.len;
365*cdf0e10cSrcweir }
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir void
368*cdf0e10cSrcweir Simstr::push_back(const Simstr & S)
369*cdf0e10cSrcweir {
370*cdf0e10cSrcweir    char * result = new char[len+1+S.len];
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir    memcpy(result,sz,len);
373*cdf0e10cSrcweir    memcpy(result+len,S.sz,S.len+1);
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir    delete [] sz;
376*cdf0e10cSrcweir    sz = result;
377*cdf0e10cSrcweir    len += S.len;
378*cdf0e10cSrcweir }
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir // Remove
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir void
384*cdf0e10cSrcweir Simstr::remove(int  pos, int  anzahl)
385*cdf0e10cSrcweir {
386*cdf0e10cSrcweir    if (pos >= len || pos < 0 || anzahl < 1)
387*cdf0e10cSrcweir       return;
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir 	int anz = len - pos < anzahl ? len - pos : anzahl;
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir 	char * result = new char[len-anz+1];
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir 	memcpy(result,sz,pos);
394*cdf0e10cSrcweir    memcpy(result+pos,sz+pos+anz,len-pos-anz+1);
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir    delete [] sz;
397*cdf0e10cSrcweir    sz = result;
398*cdf0e10cSrcweir    len -= anz;
399*cdf0e10cSrcweir }
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir void
402*cdf0e10cSrcweir Simstr::remove_trailing_blanks()
403*cdf0e10cSrcweir {
404*cdf0e10cSrcweir 	int newlen = len-1;
405*cdf0e10cSrcweir 	for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {}
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir 	if (newlen < len-1)
408*cdf0e10cSrcweir     	remove ( newlen+1, len-newlen);
409*cdf0e10cSrcweir }
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir void
412*cdf0e10cSrcweir Simstr::pop_front(int  anzahl)
413*cdf0e10cSrcweir {
414*cdf0e10cSrcweir    if (anzahl < 1)
415*cdf0e10cSrcweir       return;
416*cdf0e10cSrcweir    int anz = len < anzahl ? len : anzahl;
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir    char * result = new char[len-anz+1];
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir    memcpy(result,sz+anz,len-anz+1);
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir    delete [] sz;
423*cdf0e10cSrcweir    sz = result;
424*cdf0e10cSrcweir    len -= anz;
425*cdf0e10cSrcweir }
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir void
428*cdf0e10cSrcweir Simstr::pop_back(int  anzahl)
429*cdf0e10cSrcweir {
430*cdf0e10cSrcweir    if (anzahl < 1)
431*cdf0e10cSrcweir       return;
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir    int anz = len < anzahl ? len : anzahl;
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir    char * result = new char[len-anz+1];
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir    memcpy(result,sz,len-anz);
438*cdf0e10cSrcweir    result[len-anz] = 0;
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir    delete [] sz;
441*cdf0e10cSrcweir    sz = result;
442*cdf0e10cSrcweir    len -= anz;
443*cdf0e10cSrcweir }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir void
446*cdf0e10cSrcweir Simstr::rem_back_from(int  removeStartPos)
447*cdf0e10cSrcweir {
448*cdf0e10cSrcweir    if (removeStartPos != NO_POS)
449*cdf0e10cSrcweir       pop_back(len-removeStartPos);
450*cdf0e10cSrcweir }
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir void
453*cdf0e10cSrcweir Simstr::remove_all(char c)
454*cdf0e10cSrcweir {
455*cdf0e10cSrcweir    if (!len)
456*cdf0e10cSrcweir       return;
457*cdf0e10cSrcweir    char * result = new char[len];
458*cdf0e10cSrcweir    int i,j=0;
459*cdf0e10cSrcweir    for (i = 0; i < len; i++)
460*cdf0e10cSrcweir        if (sz[i] != c)
461*cdf0e10cSrcweir           result[j++] = sz[i];
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir    delete [] sz;
464*cdf0e10cSrcweir    sz = new char[j+1];
465*cdf0e10cSrcweir    memcpy(sz,result,j);
466*cdf0e10cSrcweir    sz[j] = 0;
467*cdf0e10cSrcweir    len = j;
468*cdf0e10cSrcweir    delete [] result;
469*cdf0e10cSrcweir }
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir void
472*cdf0e10cSrcweir Simstr::remove_all(const Simstr & S)
473*cdf0e10cSrcweir {
474*cdf0e10cSrcweir    int  pos;
475*cdf0e10cSrcweir    while ( (pos=pos_first(S)) != NO_POS )
476*cdf0e10cSrcweir       remove(pos,S.len);
477*cdf0e10cSrcweir }
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir void
480*cdf0e10cSrcweir Simstr::strip(char c)
481*cdf0e10cSrcweir {
482*cdf0e10cSrcweir 	int start = 0;
483*cdf0e10cSrcweir    if (c == ' ')
484*cdf0e10cSrcweir    {  // Sonderbehandlung: SPC entfernt auch TABs:
485*cdf0e10cSrcweir    	while ( start < len
486*cdf0e10cSrcweir                  ?  sz[start] == ' '
487*cdf0e10cSrcweir                     || sz[start] == '\t'
488*cdf0e10cSrcweir                  :  false )
489*cdf0e10cSrcweir 	   	start++;
490*cdf0e10cSrcweir    }
491*cdf0e10cSrcweir    else
492*cdf0e10cSrcweir    {
493*cdf0e10cSrcweir    	while (start < len && sz[start] == c)
494*cdf0e10cSrcweir 	   	start++;
495*cdf0e10cSrcweir    }
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir 	int ende = len-1;
498*cdf0e10cSrcweir    if (c == ' ')
499*cdf0e10cSrcweir    {  // Sonderbehandlung: SPC entfernt auch TABs:
500*cdf0e10cSrcweir    	while ( ende >= start
501*cdf0e10cSrcweir                  ?  sz[ende] == ' '
502*cdf0e10cSrcweir                     || sz[ende] == '\t'
503*cdf0e10cSrcweir                  :  false  )
504*cdf0e10cSrcweir 	   	ende--;
505*cdf0e10cSrcweir    }
506*cdf0e10cSrcweir    else
507*cdf0e10cSrcweir    {
508*cdf0e10cSrcweir    	while (ende >= start && sz[ende] == c)
509*cdf0e10cSrcweir 	   	ende--;
510*cdf0e10cSrcweir    }
511*cdf0e10cSrcweir 	*this = get(start,ende-start+1);
512*cdf0e10cSrcweir }
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir void
515*cdf0e10cSrcweir Simstr::empty()
516*cdf0e10cSrcweir {
517*cdf0e10cSrcweir    if (len > 0)
518*cdf0e10cSrcweir    {
519*cdf0e10cSrcweir       delete [] sz;
520*cdf0e10cSrcweir       sz = new char[1];
521*cdf0e10cSrcweir       *sz = 0;
522*cdf0e10cSrcweir       len = 0;
523*cdf0e10cSrcweir    }
524*cdf0e10cSrcweir }
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir Simstr
527*cdf0e10cSrcweir Simstr::take_first_token(char c)
528*cdf0e10cSrcweir {
529*cdf0e10cSrcweir    Simstr ret;
530*cdf0e10cSrcweir    int pos = pos_first(c);
531*cdf0e10cSrcweir    if (pos != NO_POS)
532*cdf0e10cSrcweir       {
533*cdf0e10cSrcweir          ret = get_front(pos);
534*cdf0e10cSrcweir          pop_front(pos+1);
535*cdf0e10cSrcweir       }
536*cdf0e10cSrcweir    else
537*cdf0e10cSrcweir       {
538*cdf0e10cSrcweir          ret = sz;
539*cdf0e10cSrcweir          delete [] sz;
540*cdf0e10cSrcweir          sz = new char[1];
541*cdf0e10cSrcweir          *sz = NULCH;
542*cdf0e10cSrcweir          len = 0;
543*cdf0e10cSrcweir       }
544*cdf0e10cSrcweir 
545*cdf0e10cSrcweir    return ret;
546*cdf0e10cSrcweir }
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir Simstr
549*cdf0e10cSrcweir Simstr::take_last_token(char c)
550*cdf0e10cSrcweir {
551*cdf0e10cSrcweir    Simstr ret;
552*cdf0e10cSrcweir    int pos = pos_last(c);
553*cdf0e10cSrcweir    if (pos != NO_POS)
554*cdf0e10cSrcweir       {
555*cdf0e10cSrcweir          ret = get_back(len-pos-1);
556*cdf0e10cSrcweir          pop_back(len-pos);
557*cdf0e10cSrcweir       }
558*cdf0e10cSrcweir    else
559*cdf0e10cSrcweir       {
560*cdf0e10cSrcweir          ret = sz;
561*cdf0e10cSrcweir          delete [] sz;
562*cdf0e10cSrcweir          sz = new char[1];
563*cdf0e10cSrcweir          *sz = NULCH;
564*cdf0e10cSrcweir          len = 0;
565*cdf0e10cSrcweir       }
566*cdf0e10cSrcweir 
567*cdf0e10cSrcweir    return ret;
568*cdf0e10cSrcweir }
569*cdf0e10cSrcweir 
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir 
572*cdf0e10cSrcweir // Find
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir int
575*cdf0e10cSrcweir Simstr::pos_first(char c) const
576*cdf0e10cSrcweir {
577*cdf0e10cSrcweir    int i = 0;
578*cdf0e10cSrcweir    for (i = 0; i < len ? sz[i] != c : false; i++) ;
579*cdf0e10cSrcweir    if (i >= len)
580*cdf0e10cSrcweir       return NO_POS;
581*cdf0e10cSrcweir    else
582*cdf0e10cSrcweir       return i;
583*cdf0e10cSrcweir }
584*cdf0e10cSrcweir 
585*cdf0e10cSrcweir int
586*cdf0e10cSrcweir Simstr::pos_first_after( char           c,
587*cdf0e10cSrcweir                          int            startSearchPos) const
588*cdf0e10cSrcweir {
589*cdf0e10cSrcweir    int i = 0;
590*cdf0e10cSrcweir    if (startSearchPos >= i)
591*cdf0e10cSrcweir       i = startSearchPos+1;
592*cdf0e10cSrcweir    for (; i < len ? sz[i] != c : false; i++) ;
593*cdf0e10cSrcweir    if (i >= len)
594*cdf0e10cSrcweir       return NO_POS;
595*cdf0e10cSrcweir    else
596*cdf0e10cSrcweir       return i;
597*cdf0e10cSrcweir }
598*cdf0e10cSrcweir 
599*cdf0e10cSrcweir 
600*cdf0e10cSrcweir int
601*cdf0e10cSrcweir Simstr::pos_last(char c) const
602*cdf0e10cSrcweir {
603*cdf0e10cSrcweir    int i = 0;
604*cdf0e10cSrcweir    for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ;
605*cdf0e10cSrcweir    if (i < 0)
606*cdf0e10cSrcweir       return NO_POS;
607*cdf0e10cSrcweir    else
608*cdf0e10cSrcweir       return i;
609*cdf0e10cSrcweir }
610*cdf0e10cSrcweir 
611*cdf0e10cSrcweir int
612*cdf0e10cSrcweir Simstr::pos_first(const Simstr & S) const
613*cdf0e10cSrcweir {
614*cdf0e10cSrcweir    char * ptr = strstr(sz,S.sz);
615*cdf0e10cSrcweir    if (ptr)
616*cdf0e10cSrcweir       return int(ptr-sz);
617*cdf0e10cSrcweir    else
618*cdf0e10cSrcweir       return NO_POS;
619*cdf0e10cSrcweir }
620*cdf0e10cSrcweir 
621*cdf0e10cSrcweir int
622*cdf0e10cSrcweir Simstr::pos_last(const Simstr & S) const
623*cdf0e10cSrcweir {
624*cdf0e10cSrcweir    Simstr vgl;
625*cdf0e10cSrcweir    int i;
626*cdf0e10cSrcweir    for (i = len-S.len; i >= 0 ; i--)
627*cdf0e10cSrcweir       {
628*cdf0e10cSrcweir          vgl = get(i,S.len);
629*cdf0e10cSrcweir          if (vgl == S)
630*cdf0e10cSrcweir             break;
631*cdf0e10cSrcweir       }
632*cdf0e10cSrcweir    if (i >= 0)
633*cdf0e10cSrcweir       return i;
634*cdf0e10cSrcweir    else
635*cdf0e10cSrcweir       return NO_POS;
636*cdf0e10cSrcweir }
637*cdf0e10cSrcweir 
638*cdf0e10cSrcweir int
639*cdf0e10cSrcweir Simstr::count(char c) const
640*cdf0e10cSrcweir {
641*cdf0e10cSrcweir    int ret = 0;
642*cdf0e10cSrcweir    for (int i =0; i < len; i++)
643*cdf0e10cSrcweir 	  if (sz[i] == c)
644*cdf0e10cSrcweir 		 ret++;
645*cdf0e10cSrcweir    return ret;
646*cdf0e10cSrcweir }
647*cdf0e10cSrcweir 
648*cdf0e10cSrcweir bool
649*cdf0e10cSrcweir Simstr::is_no_text() const
650*cdf0e10cSrcweir {
651*cdf0e10cSrcweir    if (!len)
652*cdf0e10cSrcweir 	  return true;
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir    int i;
655*cdf0e10cSrcweir    for (i = 0; sz[i] <= 32 && i < len; i++) ;
656*cdf0e10cSrcweir    if (i < len)
657*cdf0e10cSrcweir 		return false;
658*cdf0e10cSrcweir 	return true;
659*cdf0e10cSrcweir }
660*cdf0e10cSrcweir 
661*cdf0e10cSrcweir // Change
662*cdf0e10cSrcweir 
663*cdf0e10cSrcweir void
664*cdf0e10cSrcweir Simstr::replace(int  pos, char c)
665*cdf0e10cSrcweir {
666*cdf0e10cSrcweir 	if (pos < 0 || pos >= len)
667*cdf0e10cSrcweir       return;
668*cdf0e10cSrcweir    else
669*cdf0e10cSrcweir       sz[unsigned(pos)] = c;
670*cdf0e10cSrcweir }
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir void
673*cdf0e10cSrcweir Simstr::replace(int  startPos, int  anzahl, const Simstr & S)
674*cdf0e10cSrcweir {
675*cdf0e10cSrcweir    if (startPos >= len || startPos < 0 || anzahl < 1)
676*cdf0e10cSrcweir       return;
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir    int anz = len - startPos < anzahl ? len - startPos : anzahl;
679*cdf0e10cSrcweir 
680*cdf0e10cSrcweir    char * result = new char[len-anz+S.len+1];
681*cdf0e10cSrcweir 
682*cdf0e10cSrcweir    memcpy(result,sz,startPos);
683*cdf0e10cSrcweir    memcpy(result+startPos, S.sz, S.len);
684*cdf0e10cSrcweir    memcpy(result+startPos+S.len, sz+startPos+anz, len-startPos-anz+1);
685*cdf0e10cSrcweir 
686*cdf0e10cSrcweir    delete [] sz;
687*cdf0e10cSrcweir    sz = result;
688*cdf0e10cSrcweir    len = len-anz+S.len;
689*cdf0e10cSrcweir }
690*cdf0e10cSrcweir 
691*cdf0e10cSrcweir void
692*cdf0e10cSrcweir Simstr::replace_all(char oldCh, char newCh)
693*cdf0e10cSrcweir {
694*cdf0e10cSrcweir    for (int i=0; i < len; i++)
695*cdf0e10cSrcweir       if (sz[i] == oldCh)
696*cdf0e10cSrcweir          sz[i] = newCh;
697*cdf0e10cSrcweir }
698*cdf0e10cSrcweir 
699*cdf0e10cSrcweir void
700*cdf0e10cSrcweir Simstr::replace_all(const Simstr & oldS, const Simstr & newS)
701*cdf0e10cSrcweir {
702*cdf0e10cSrcweir    Simstr vgl;
703*cdf0e10cSrcweir    int i = 0;
704*cdf0e10cSrcweir 	while (i <= len-oldS.len)
705*cdf0e10cSrcweir 		{
706*cdf0e10cSrcweir          vgl = get(i,oldS.len);
707*cdf0e10cSrcweir          if (strcmp(vgl.sz,oldS.sz) == 0)
708*cdf0e10cSrcweir             {
709*cdf0e10cSrcweir                replace(i,oldS.len,newS);
710*cdf0e10cSrcweir                i += newS.len;
711*cdf0e10cSrcweir             }
712*cdf0e10cSrcweir          else
713*cdf0e10cSrcweir             i++;
714*cdf0e10cSrcweir       }
715*cdf0e10cSrcweir }
716*cdf0e10cSrcweir 
717*cdf0e10cSrcweir void
718*cdf0e10cSrcweir Simstr::to_lower()
719*cdf0e10cSrcweir {
720*cdf0e10cSrcweir 	for (int i = 0; i < len; i++)
721*cdf0e10cSrcweir    	sz[i] = (char) tolower(sz[i]);
722*cdf0e10cSrcweir }
723*cdf0e10cSrcweir 
724*cdf0e10cSrcweir 
725*cdf0e10cSrcweir 
726*cdf0e10cSrcweir //   Simstr addition
727*cdf0e10cSrcweir Simstr
728*cdf0e10cSrcweir operator+(const char * str, const Simstr & S)
729*cdf0e10cSrcweir {
730*cdf0e10cSrcweir    Simstr ret = S;
731*cdf0e10cSrcweir    ret.push_front(str);
732*cdf0e10cSrcweir    return ret;
733*cdf0e10cSrcweir }
734*cdf0e10cSrcweir 
735*cdf0e10cSrcweir Simstr
736*cdf0e10cSrcweir operator+(const Simstr & S, const char * str)
737*cdf0e10cSrcweir {
738*cdf0e10cSrcweir    Simstr ret = S;
739*cdf0e10cSrcweir    ret.push_back(str);
740*cdf0e10cSrcweir    return ret;
741*cdf0e10cSrcweir }
742*cdf0e10cSrcweir 
743*cdf0e10cSrcweir Simstr
744*cdf0e10cSrcweir operator+(char c, const Simstr & S)
745*cdf0e10cSrcweir {
746*cdf0e10cSrcweir    Simstr ret = S;
747*cdf0e10cSrcweir    ret.push_front(c);
748*cdf0e10cSrcweir    return ret;
749*cdf0e10cSrcweir }
750*cdf0e10cSrcweir 
751*cdf0e10cSrcweir Simstr
752*cdf0e10cSrcweir operator+(const Simstr & S, char c)
753*cdf0e10cSrcweir {
754*cdf0e10cSrcweir    Simstr ret = S;
755*cdf0e10cSrcweir    ret.push_back(c);
756*cdf0e10cSrcweir    return ret;
757*cdf0e10cSrcweir }
758*cdf0e10cSrcweir 
759*cdf0e10cSrcweir 
760*cdf0e10cSrcweir // Simstr-Vergleiche mit char *
761*cdf0e10cSrcweir bool
762*cdf0e10cSrcweir operator==(const Simstr & S, const char * str)
763*cdf0e10cSrcweir {
764*cdf0e10cSrcweir    return strcmp(S,str) == 0;
765*cdf0e10cSrcweir }
766*cdf0e10cSrcweir 
767*cdf0e10cSrcweir bool
768*cdf0e10cSrcweir operator!=(const Simstr & S, const char * str)
769*cdf0e10cSrcweir {
770*cdf0e10cSrcweir    return strcmp(S,str) != 0;
771*cdf0e10cSrcweir }
772*cdf0e10cSrcweir 
773*cdf0e10cSrcweir bool
774*cdf0e10cSrcweir operator<(const Simstr & S, const char * str)
775*cdf0e10cSrcweir {
776*cdf0e10cSrcweir    return strcmp(S,str) < 0;
777*cdf0e10cSrcweir }
778*cdf0e10cSrcweir 
779*cdf0e10cSrcweir bool
780*cdf0e10cSrcweir operator>(const Simstr & S, const char * str)
781*cdf0e10cSrcweir {
782*cdf0e10cSrcweir    return strcmp(S,str) > 0;
783*cdf0e10cSrcweir }
784*cdf0e10cSrcweir 
785*cdf0e10cSrcweir bool
786*cdf0e10cSrcweir operator<=(const Simstr & S, const char * str)
787*cdf0e10cSrcweir {
788*cdf0e10cSrcweir    return strcmp(S,str) <= 0;
789*cdf0e10cSrcweir }
790*cdf0e10cSrcweir 
791*cdf0e10cSrcweir bool
792*cdf0e10cSrcweir operator>=(const Simstr & S, const char * str)
793*cdf0e10cSrcweir {
794*cdf0e10cSrcweir    return strcmp(S,str) >= 0;
795*cdf0e10cSrcweir }
796*cdf0e10cSrcweir 
797*cdf0e10cSrcweir bool
798*cdf0e10cSrcweir operator==(const char * str, const Simstr & S)
799*cdf0e10cSrcweir {
800*cdf0e10cSrcweir    return strcmp(str,S) == 0;
801*cdf0e10cSrcweir }
802*cdf0e10cSrcweir 
803*cdf0e10cSrcweir bool
804*cdf0e10cSrcweir operator!=(const char * str, const Simstr & S)
805*cdf0e10cSrcweir {
806*cdf0e10cSrcweir    return strcmp(str,S) != 0;
807*cdf0e10cSrcweir }
808*cdf0e10cSrcweir 
809*cdf0e10cSrcweir bool
810*cdf0e10cSrcweir operator<(const char * str, const Simstr & S)
811*cdf0e10cSrcweir {
812*cdf0e10cSrcweir    return strcmp(str,S) < 0;
813*cdf0e10cSrcweir }
814*cdf0e10cSrcweir 
815*cdf0e10cSrcweir bool
816*cdf0e10cSrcweir operator>(const char * str, const Simstr & S)
817*cdf0e10cSrcweir {
818*cdf0e10cSrcweir    return strcmp(str,S) > 0;
819*cdf0e10cSrcweir }
820*cdf0e10cSrcweir 
821*cdf0e10cSrcweir bool
822*cdf0e10cSrcweir operator<=(const char * str, const Simstr & S)
823*cdf0e10cSrcweir {
824*cdf0e10cSrcweir    return strcmp(str,S) <= 0;
825*cdf0e10cSrcweir }
826*cdf0e10cSrcweir 
827*cdf0e10cSrcweir bool
828*cdf0e10cSrcweir operator>=(const char * str, const Simstr & S)
829*cdf0e10cSrcweir {
830*cdf0e10cSrcweir    return strcmp(str,S) >= 0;
831*cdf0e10cSrcweir }
832*cdf0e10cSrcweir 
833*cdf0e10cSrcweir 
834