1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: simstr.cxx,v $
10 * $Revision: 1.5.16.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_soltools.hxx"
37 #include <string.h> // strlen(), memcpy(), memset()
38 #include <ctype.h> // tolower()
39 #include <limits.h> // INT_MAX
41 const char NULCH
= '\0';
42 const int NO_POS
= -1;
45 Simstr::Simstr(const char * s_
)
61 Simstr::Simstr(const char * anybytes
, int nrOfBytes
)
71 int slen
= static_cast<int>( strlen(anybytes
) );
73 len
= slen
< nrOfBytes
77 memcpy( sz
, anybytes
, len
);
81 Simstr::Simstr(char c
, int anzahl
)
98 Simstr::Simstr( const char * anybytes
,
102 unsigned slen
= strlen(anybytes
);
103 if (anybytes
== 0 || slen
<= unsigned(firstBytesPos
))
111 int maxLen
= slen
- unsigned(firstBytesPos
);
112 len
= maxLen
< nrOfBytes
115 sz
= new char[len
+1];
116 memcpy(sz
,anybytes
+firstBytesPos
,len
);
122 Simstr::Simstr(const Simstr
& S
)
125 sz
= new char[len
+1];
126 memcpy(sz
,S
.sz
,len
+1);
129 Simstr
& Simstr::operator=(const Simstr
& S
)
137 sz
= new char[len
+1];
138 memcpy(sz
,S
.sz
,len
+1);
151 static char nullCh
= NULCH
;
153 if (n
>= long(len
) || n
< 0)
156 return sz
[unsigned(n
)];
162 static Simstr aNull_
;
168 Simstr::operator+(const Simstr
& S
) const
176 Simstr::operator+=(const Simstr
& S
)
183 Simstr::operator+=(const char * s_
)
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
) ? 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; }
214 Simstr::operator>=(const Simstr
& S
) const
215 { return (strcmp(sz
,S
.sz
) >= 0) ? true : false; }
220 // ************** LIST - Funktionen *****************
226 Simstr::get(int n
) const { return (n
>= len
|| n
< 0) ? 0 : sz
[n
]; }
229 Simstr::get_front() const { return sz
[0]; }
232 Simstr::get_back() const { return len
? sz
[len
-1] : 0; }
235 Simstr::get(int startPos
, int anzahl
) const
237 if (startPos
>= len
|| startPos
< 0 || anzahl
< 1)
240 int anz
= len
- startPos
< anzahl
? len
- startPos
: anzahl
;
243 memcpy(ret
.sz
, sz
+startPos
, anz
);
248 Simstr::get_front(int anzahl
) const
250 int anz
= len
< anzahl
? len
: anzahl
;
255 memcpy(ret
.sz
, sz
, anz
);
260 Simstr::get_back(int anzahl
) const
262 int anz
= len
< anzahl
? len
: anzahl
;
268 memcpy(ret
.sz
, sz
+start
, anz
);
273 Simstr::get_first_token(char c
) const
275 int posc
= pos_first(c
);
277 return get_front(posc
);
283 Simstr::get_last_token(char c
) const
285 int posc
= pos_last(c
);
287 return get_back(len
-posc
-1);
297 Simstr::insert(int pos
, char c
)
299 if (pos
< 0 || pos
> len
)
302 char * result
= new char[len
+2];
304 memcpy(result
,sz
,pos
);
306 memcpy(result
+pos
+1,sz
+pos
,len
-pos
+1);
314 Simstr::push_front(char c
)
316 char * result
= new char[len
+2];
319 memcpy(result
+1,sz
,len
+1);
327 Simstr::push_back(char c
)
329 char * result
= new char[len
+2];
331 memcpy(result
,sz
,len
);
341 Simstr::insert(int pos
, const Simstr
& S
)
343 if (pos
< 0 || pos
> len
)
346 char * result
= new char[len
+1+S
.len
];
348 memcpy(result
,sz
,pos
);
349 memcpy(result
+pos
,S
.sz
,S
.len
);
350 memcpy(result
+pos
+S
.len
,sz
+pos
,len
-pos
+1);
358 Simstr::push_front(const Simstr
& S
)
360 char * result
= new char[len
+1+S
.len
];
362 memcpy(result
,S
.sz
,S
.len
);
363 memcpy(result
+S
.len
,sz
,len
+1);
371 Simstr::push_back(const Simstr
& S
)
373 char * result
= new char[len
+1+S
.len
];
375 memcpy(result
,sz
,len
);
376 memcpy(result
+len
,S
.sz
,S
.len
+1);
387 Simstr::remove(int pos
, int anzahl
)
389 if (pos
>= len
|| pos
< 0 || anzahl
< 1)
392 int anz
= len
- pos
< anzahl
? len
- pos
: anzahl
;
394 char * result
= new char[len
-anz
+1];
396 memcpy(result
,sz
,pos
);
397 memcpy(result
+pos
,sz
+pos
+anz
,len
-pos
-anz
+1);
405 Simstr::remove_trailing_blanks()
408 for ( ; newlen
> 1 && sz
[newlen
] <= 32; --newlen
) {}
411 remove ( newlen
+1, len
-newlen
);
415 Simstr::pop_front(int anzahl
)
419 int anz
= len
< anzahl
? len
: anzahl
;
421 char * result
= new char[len
-anz
+1];
423 memcpy(result
,sz
+anz
,len
-anz
+1);
431 Simstr::pop_back(int anzahl
)
436 int anz
= len
< anzahl
? len
: anzahl
;
438 char * result
= new char[len
-anz
+1];
440 memcpy(result
,sz
,len
-anz
);
449 Simstr::rem_back_from(int removeStartPos
)
451 if (removeStartPos
!= NO_POS
)
452 pop_back(len
-removeStartPos
);
456 Simstr::remove_all(char c
)
460 char * result
= new char[len
];
462 for (i
= 0; i
< len
; i
++)
475 Simstr::remove_all(const Simstr
& S
)
478 while ( (pos
=pos_first(S
)) != NO_POS
)
483 Simstr::strip(char c
)
487 { // Sonderbehandlung: SPC entfernt auch TABs:
496 while (start
< len
&& sz
[start
] == c
)
502 { // Sonderbehandlung: SPC entfernt auch TABs:
503 while ( ende
>= start
511 while (ende
>= start
&& sz
[ende
] == c
)
514 *this = get(start
,ende
-start
+1);
530 Simstr::take_first_token(char c
)
533 int pos
= pos_first(c
);
536 ret
= get_front(pos
);
552 Simstr::take_last_token(char c
)
555 int pos
= pos_last(c
);
558 ret
= get_back(len
-pos
-1);
578 Simstr::pos_first(char c
) const
581 for (i
= 0; i
< len
? sz
[i
] != c
: false; i
++) ;
589 Simstr::pos_first_after( char c
,
590 int startSearchPos
) const
593 if (startSearchPos
>= i
)
594 i
= startSearchPos
+1;
595 for (; i
< len
? sz
[i
] != c
: false; i
++) ;
604 Simstr::pos_last(char c
) const
607 for (i
= len
-1; i
>= 0 ? sz
[i
] != c
: false; i
--) ;
615 Simstr::pos_first(const Simstr
& S
) const
617 char * ptr
= strstr(sz
,S
.sz
);
625 Simstr::pos_last(const Simstr
& S
) const
629 for (i
= len
-S
.len
; i
>= 0 ; i
--)
642 Simstr::count(char c
) const
645 for (int i
=0; i
< len
; i
++)
652 Simstr::is_no_text() const
658 for (i
= 0; sz
[i
] <= 32 && i
< len
; i
++) ;
667 Simstr::replace(int pos
, char c
)
669 if (pos
< 0 || pos
>= len
)
672 sz
[unsigned(pos
)] = c
;
676 Simstr::replace(int startPos
, int anzahl
, const Simstr
& S
)
678 if (startPos
>= len
|| startPos
< 0 || anzahl
< 1)
681 int anz
= len
- startPos
< anzahl
? len
- startPos
: anzahl
;
683 char * result
= new char[len
-anz
+S
.len
+1];
685 memcpy(result
,sz
,startPos
);
686 memcpy(result
+startPos
, S
.sz
, S
.len
);
687 memcpy(result
+startPos
+S
.len
, sz
+startPos
+anz
, len
-startPos
-anz
+1);
695 Simstr::replace_all(char oldCh
, char newCh
)
697 for (int i
=0; i
< len
; i
++)
703 Simstr::replace_all(const Simstr
& oldS
, const Simstr
& newS
)
707 while (i
<= len
-oldS
.len
)
709 vgl
= get(i
,oldS
.len
);
710 if (strcmp(vgl
.sz
,oldS
.sz
) == 0)
712 replace(i
,oldS
.len
,newS
);
723 for (int i
= 0; i
< len
; i
++)
724 sz
[i
] = (char) tolower(sz
[i
]);
731 operator+(const char * str
, const Simstr
& S
)
739 operator+(const Simstr
& S
, const char * str
)
747 operator+(char c
, const Simstr
& S
)
755 operator+(const Simstr
& S
, char c
)
763 // Simstr-Vergleiche mit char *
765 operator==(const Simstr
& S
, const char * str
)
767 return strcmp(S
,str
) == 0;
771 operator!=(const Simstr
& S
, const char * str
)
773 return strcmp(S
,str
) != 0;
777 operator<(const Simstr
& S
, const char * str
)
779 return strcmp(S
,str
) < 0;
783 operator>(const Simstr
& S
, const char * str
)
785 return strcmp(S
,str
) > 0;
789 operator<=(const Simstr
& S
, const char * str
)
791 return strcmp(S
,str
) <= 0;
795 operator>=(const Simstr
& S
, const char * str
)
797 return strcmp(S
,str
) >= 0;
801 operator==(const char * str
, const Simstr
& S
)
803 return strcmp(str
,S
) == 0;
807 operator!=(const char * str
, const Simstr
& S
)
809 return strcmp(str
,S
) != 0;
813 operator<(const char * str
, const Simstr
& S
)
815 return strcmp(str
,S
) < 0;
819 operator>(const char * str
, const Simstr
& S
)
821 return strcmp(str
,S
) > 0;
825 operator<=(const char * str
, const Simstr
& S
)
827 return strcmp(str
,S
) <= 0;
831 operator>=(const char * str
, const Simstr
& S
)
833 return strcmp(str
,S
) >= 0;