1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
33 #include <string.h> // strlen(), memcpy(), memset()
34 #include <ctype.h> // tolower()
35 #include <limits.h> // INT_MAX
37 const char NULCH
= '\0';
38 const int NO_POS
= -1;
41 Simstr::Simstr(const char * s_
)
51 len
= (int)strlen(s_
);
57 Simstr::Simstr(const char * anybytes
, int nrOfBytes
)
67 int slen
= static_cast<int>( strlen(anybytes
) );
69 len
= slen
< nrOfBytes
73 memcpy( sz
, anybytes
, len
);
77 Simstr::Simstr(char c
, int anzahl
)
94 Simstr::Simstr( const char * anybytes
,
98 unsigned slen
= (unsigned)strlen(anybytes
);
99 if (anybytes
== 0 || slen
<= unsigned(firstBytesPos
))
107 int maxLen
= slen
- unsigned(firstBytesPos
);
108 len
= maxLen
< nrOfBytes
111 sz
= new char[len
+1];
112 memcpy(sz
,anybytes
+firstBytesPos
,len
);
118 Simstr::Simstr(const Simstr
& S
)
121 sz
= new char[len
+1];
122 memcpy(sz
,S
.sz
,len
+1);
125 Simstr
& Simstr::operator=(const Simstr
& S
)
133 sz
= new char[len
+1];
134 memcpy(sz
,S
.sz
,len
+1);
147 static char nullCh
= NULCH
;
149 if (n
>= long(len
) || n
< 0)
152 return sz
[unsigned(n
)];
158 static Simstr aNull_
;
164 Simstr::operator+(const Simstr
& S
) const
172 Simstr::operator+=(const Simstr
& S
)
179 Simstr::operator+=(const char * s_
)
190 Simstr::operator==(const Simstr
& S
) const
191 { return !strcmp(sz
,S
.sz
) ? true : false; }
194 Simstr::operator!=(const Simstr
& S
) const
195 { return strcmp(sz
,S
.sz
) ? true : false; }
198 Simstr::operator<(const Simstr
& S
) const
199 { return (strcmp(sz
,S
.sz
) < 0) ? true : false; }
202 Simstr::operator>(const Simstr
& S
) const
203 { return (strcmp(sz
,S
.sz
) > 0) ? true : false; }
206 Simstr::operator<=(const Simstr
& S
) const
207 { return (strcmp(sz
,S
.sz
) <= 0) ? true : false; }
210 Simstr::operator>=(const Simstr
& S
) const
211 { return (strcmp(sz
,S
.sz
) >= 0) ? true : false; }
216 // ************** LIST - Funktionen *****************
222 Simstr::get(int n
) const { return (n
>= len
|| n
< 0) ? 0 : sz
[n
]; }
225 Simstr::get_front() const { return sz
[0]; }
228 Simstr::get_back() const { return len
? sz
[len
-1] : 0; }
231 Simstr::get(int startPos
, int anzahl
) const
233 if (startPos
>= len
|| startPos
< 0 || anzahl
< 1)
236 int anz
= len
- startPos
< anzahl
? len
- startPos
: anzahl
;
239 memcpy(ret
.sz
, sz
+startPos
, anz
);
244 Simstr::get_front(int anzahl
) const
246 int anz
= len
< anzahl
? len
: anzahl
;
251 memcpy(ret
.sz
, sz
, anz
);
256 Simstr::get_back(int anzahl
) const
258 int anz
= len
< anzahl
? len
: anzahl
;
264 memcpy(ret
.sz
, sz
+start
, anz
);
269 Simstr::get_first_token(char c
) const
271 int posc
= pos_first(c
);
273 return get_front(posc
);
279 Simstr::get_last_token(char c
) const
281 int posc
= pos_last(c
);
283 return get_back(len
-posc
-1);
293 Simstr::insert(int pos
, char c
)
295 if (pos
< 0 || pos
> len
)
298 char * result
= new char[len
+2];
300 memcpy(result
,sz
,pos
);
302 memcpy(result
+pos
+1,sz
+pos
,len
-pos
+1);
310 Simstr::push_front(char c
)
312 char * result
= new char[len
+2];
315 memcpy(result
+1,sz
,len
+1);
323 Simstr::push_back(char c
)
325 char * result
= new char[len
+2];
327 memcpy(result
,sz
,len
);
337 Simstr::insert(int pos
, const Simstr
& S
)
339 if (pos
< 0 || pos
> len
)
342 char * result
= new char[len
+1+S
.len
];
344 memcpy(result
,sz
,pos
);
345 memcpy(result
+pos
,S
.sz
,S
.len
);
346 memcpy(result
+pos
+S
.len
,sz
+pos
,len
-pos
+1);
354 Simstr::push_front(const Simstr
& S
)
356 char * result
= new char[len
+1+S
.len
];
358 memcpy(result
,S
.sz
,S
.len
);
359 memcpy(result
+S
.len
,sz
,len
+1);
367 Simstr::push_back(const Simstr
& S
)
369 char * result
= new char[len
+1+S
.len
];
371 memcpy(result
,sz
,len
);
372 memcpy(result
+len
,S
.sz
,S
.len
+1);
383 Simstr::remove(int pos
, int anzahl
)
385 if (pos
>= len
|| pos
< 0 || anzahl
< 1)
388 int anz
= len
- pos
< anzahl
? len
- pos
: anzahl
;
390 char * result
= new char[len
-anz
+1];
392 memcpy(result
,sz
,pos
);
393 memcpy(result
+pos
,sz
+pos
+anz
,len
-pos
-anz
+1);
401 Simstr::remove_trailing_blanks()
404 for ( ; newlen
> 1 && sz
[newlen
] <= 32; --newlen
) {}
407 remove ( newlen
+1, len
-newlen
);
411 Simstr::pop_front(int anzahl
)
415 int anz
= len
< anzahl
? len
: anzahl
;
417 char * result
= new char[len
-anz
+1];
419 memcpy(result
,sz
+anz
,len
-anz
+1);
427 Simstr::pop_back(int anzahl
)
432 int anz
= len
< anzahl
? len
: anzahl
;
434 char * result
= new char[len
-anz
+1];
436 memcpy(result
,sz
,len
-anz
);
445 Simstr::rem_back_from(int removeStartPos
)
447 if (removeStartPos
!= NO_POS
)
448 pop_back(len
-removeStartPos
);
452 Simstr::remove_all(char c
)
456 char * result
= new char[len
];
458 for (i
= 0; i
< len
; i
++)
471 Simstr::remove_all(const Simstr
& S
)
474 while ( (pos
=pos_first(S
)) != NO_POS
)
479 Simstr::strip(char c
)
483 { // Sonderbehandlung: SPC entfernt auch TABs:
492 while (start
< len
&& sz
[start
] == c
)
498 { // Sonderbehandlung: SPC entfernt auch TABs:
499 while ( ende
>= start
507 while (ende
>= start
&& sz
[ende
] == c
)
510 *this = get(start
,ende
-start
+1);
526 Simstr::take_first_token(char c
)
529 int pos
= pos_first(c
);
532 ret
= get_front(pos
);
548 Simstr::take_last_token(char c
)
551 int pos
= pos_last(c
);
554 ret
= get_back(len
-pos
-1);
574 Simstr::pos_first(char c
) const
577 for (i
= 0; i
< len
? sz
[i
] != c
: false; i
++) ;
585 Simstr::pos_first_after( char c
,
586 int startSearchPos
) const
589 if (startSearchPos
>= i
)
590 i
= startSearchPos
+1;
591 for (; i
< len
? sz
[i
] != c
: false; i
++) ;
600 Simstr::pos_last(char c
) const
603 for (i
= len
-1; i
>= 0 ? sz
[i
] != c
: false; i
--) ;
611 Simstr::pos_first(const Simstr
& S
) const
613 char * ptr
= strstr(sz
,S
.sz
);
621 Simstr::pos_last(const Simstr
& S
) const
625 for (i
= len
-S
.len
; i
>= 0 ; i
--)
638 Simstr::count(char c
) const
641 for (int i
=0; i
< len
; i
++)
648 Simstr::is_no_text() const
654 for (i
= 0; i
< len
&& sz
[i
] <= 32 ; i
++) ;
663 Simstr::replace(int pos
, char c
)
665 if (pos
< 0 || pos
>= len
)
668 sz
[unsigned(pos
)] = c
;
672 Simstr::replace(int startPos
, int anzahl
, const Simstr
& S
)
674 if (startPos
>= len
|| startPos
< 0 || anzahl
< 1)
677 int anz
= len
- startPos
< anzahl
? len
- startPos
: anzahl
;
679 char * result
= new char[len
-anz
+S
.len
+1];
681 memcpy(result
,sz
,startPos
);
682 memcpy(result
+startPos
, S
.sz
, S
.len
);
683 memcpy(result
+startPos
+S
.len
, sz
+startPos
+anz
, len
-startPos
-anz
+1);
691 Simstr::replace_all(char oldCh
, char newCh
)
693 for (int i
=0; i
< len
; i
++)
699 Simstr::replace_all(const Simstr
& oldS
, const Simstr
& newS
)
703 while (i
<= len
-oldS
.len
)
705 vgl
= get(i
,oldS
.len
);
706 if (strcmp(vgl
.sz
,oldS
.sz
) == 0)
708 replace(i
,oldS
.len
,newS
);
719 for (int i
= 0; i
< len
; i
++)
720 sz
[i
] = (char) tolower(sz
[i
]);
727 operator+(const char * str
, const Simstr
& S
)
735 operator+(const Simstr
& S
, const char * str
)
743 operator+(char c
, const Simstr
& S
)
751 operator+(const Simstr
& S
, char c
)
759 // Simstr-Vergleiche mit char *
761 operator==(const Simstr
& S
, const char * str
)
763 return strcmp(S
,str
) == 0;
767 operator!=(const Simstr
& S
, const char * str
)
769 return strcmp(S
,str
) != 0;
773 operator<(const Simstr
& S
, const char * str
)
775 return strcmp(S
,str
) < 0;
779 operator>(const Simstr
& S
, const char * str
)
781 return strcmp(S
,str
) > 0;
785 operator<=(const Simstr
& S
, const char * str
)
787 return strcmp(S
,str
) <= 0;
791 operator>=(const Simstr
& S
, const char * str
)
793 return strcmp(S
,str
) >= 0;
797 operator==(const char * str
, const Simstr
& S
)
799 return strcmp(str
,S
) == 0;
803 operator!=(const char * str
, const Simstr
& S
)
805 return strcmp(str
,S
) != 0;
809 operator<(const char * str
, const Simstr
& S
)
811 return strcmp(str
,S
) < 0;
815 operator>(const char * str
, const Simstr
& S
)
817 return strcmp(str
,S
) > 0;
821 operator<=(const char * str
, const Simstr
& S
)
823 return strcmp(str
,S
) <= 0;
827 operator>=(const char * str
, const Simstr
& S
)
829 return strcmp(str
,S
) >= 0;
833 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */