update dev300-m58
[ooovba.git] / soltools / support / simstr.cxx
blobe4bd6fd1c67f70f95b13871899343dbaa5ee6e9e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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"
35 #include <simstr.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_)
47 if (s_ == 0)
49 len = 0;
50 sz = new char[1];
51 *sz = 0;
53 else
55 len = strlen(s_);
56 sz = new char[len+1];
57 memcpy(sz,s_,len+1);
61 Simstr::Simstr(const char * anybytes, int nrOfBytes)
63 if (anybytes == 0)
65 len = 0;
66 sz = new char[1];
67 *sz = 0;
68 return;
71 int slen = static_cast<int>( strlen(anybytes) );
73 len = slen < nrOfBytes
74 ? slen
75 : nrOfBytes;
76 sz = new char[len+1];
77 memcpy( sz, anybytes, len );
78 *( sz + len ) = 0;
81 Simstr::Simstr(char c, int anzahl)
83 if (anzahl < 1)
85 len = 0;
86 sz = new char[1];
87 *sz = 0;
89 else
91 len = anzahl;
92 sz = new char[len+1];
93 memset(sz,c,anzahl);
94 sz[len] = 0;
98 Simstr::Simstr( const char * anybytes,
99 int firstBytesPos,
100 int nrOfBytes)
102 unsigned slen = strlen(anybytes);
103 if (anybytes == 0 || slen <= unsigned(firstBytesPos))
105 len = 0;
106 sz = new char[1];
107 *sz = 0;
109 else
111 int maxLen = slen - unsigned(firstBytesPos);
112 len = maxLen < nrOfBytes
113 ? maxLen
114 : nrOfBytes;
115 sz = new char[len+1];
116 memcpy(sz,anybytes+firstBytesPos,len);
117 *(sz+len) = 0;
122 Simstr::Simstr(const Simstr & S)
124 len = S.len;
125 sz = new char[len+1];
126 memcpy(sz,S.sz,len+1);
129 Simstr & Simstr::operator=(const Simstr & S)
131 if (sz == S.sz)
132 return *this;
134 delete [] sz;
136 len = S.len;
137 sz = new char[len+1];
138 memcpy(sz,S.sz,len+1);
140 return *this;
143 Simstr::~Simstr()
145 delete [] sz;
148 char &
149 Simstr::ch(int n)
151 static char nullCh = NULCH;
152 nullCh = NULCH;
153 if (n >= long(len) || n < 0)
154 return nullCh;
155 else
156 return sz[unsigned(n)];
159 const Simstr &
160 Simstr::null_()
162 static Simstr aNull_;
163 return aNull_;
167 Simstr
168 Simstr::operator+(const Simstr & S) const
170 Simstr ret = sz;
171 ret.push_back(S);
172 return ret;
175 Simstr &
176 Simstr::operator+=(const Simstr & S)
178 push_back(S);
179 return *this;
182 Simstr &
183 Simstr::operator+=(const char * s_)
185 Simstr a(s_);
186 push_back(a);
187 return *this;
191 // REL
193 bool
194 Simstr::operator==(const Simstr & S) const
195 { return !strcmp(sz,S.sz) ? true : false; }
197 bool
198 Simstr::operator!=(const Simstr & S) const
199 { return strcmp(sz,S.sz) ? true : false; }
201 bool
202 Simstr::operator<(const Simstr & S) const
203 { return (strcmp(sz,S.sz) < 0) ? true : false; }
205 bool
206 Simstr::operator>(const Simstr & S) const
207 { return (strcmp(sz,S.sz) > 0) ? true : false; }
209 bool
210 Simstr::operator<=(const Simstr & S) const
211 { return (strcmp(sz,S.sz) <= 0) ? true : false; }
213 bool
214 Simstr::operator>=(const Simstr & S) const
215 { return (strcmp(sz,S.sz) >= 0) ? true : false; }
220 // ************** LIST - Funktionen *****************
223 // Einzelzugriff
225 char
226 Simstr::get(int n) const { return (n >= len || n < 0) ? 0 : sz[n]; }
228 char
229 Simstr::get_front() const { return sz[0]; }
231 char
232 Simstr::get_back() const { return len ? sz[len-1] : 0; }
234 Simstr
235 Simstr::get(int startPos, int anzahl) const
237 if (startPos >= len || startPos < 0 || anzahl < 1)
238 return "";
240 int anz = len - startPos < anzahl ? len - startPos : anzahl;
242 Simstr ret(' ',anz);
243 memcpy(ret.sz, sz+startPos, anz);
244 return ret;
247 Simstr
248 Simstr::get_front(int anzahl) const
250 int anz = len < anzahl ? len : anzahl;
251 if (anz < 1)
252 return "";
254 Simstr ret(' ',anz);
255 memcpy(ret.sz, sz, anz);
256 return ret;
259 Simstr
260 Simstr::get_back(int anzahl) const
262 int anz = len < anzahl ? len : anzahl;
263 if (anz < 1)
264 return "";
265 int start = len-anz;
267 Simstr ret(' ',anz);
268 memcpy(ret.sz, sz+start, anz);
269 return ret;
272 Simstr
273 Simstr::get_first_token(char c) const
275 int posc = pos_first(c);
276 if (posc != NO_POS)
277 return get_front(posc);
278 else
279 return sz;
282 Simstr
283 Simstr::get_last_token(char c) const
285 int posc = pos_last(c);
286 if (posc != NO_POS)
287 return get_back(len-posc-1);
288 else
289 return sz;
294 // Insert
296 void
297 Simstr::insert(int pos, char c)
299 if (pos < 0 || pos > len)
300 return;
302 char * result = new char[len+2];
304 memcpy(result,sz,pos);
305 result[pos] = c;
306 memcpy(result+pos+1,sz+pos,len-pos+1);
308 delete [] sz;
309 sz = result;
310 len++;
313 void
314 Simstr::push_front(char c)
316 char * result = new char[len+2];
318 result[0] = c;
319 memcpy(result+1,sz,len+1);
321 delete [] sz;
322 sz = result;
323 len++;
326 void
327 Simstr::push_back(char c)
329 char * result = new char[len+2];
331 memcpy(result,sz,len);
332 result[len] = c;
333 result[len+1] = 0;
335 delete [] sz;
336 sz = result;
337 len++;
340 void
341 Simstr::insert(int pos, const Simstr & S)
343 if (pos < 0 || pos > len)
344 return;
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);
352 delete [] sz;
353 sz = result;
354 len += S.len;
357 void
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);
365 delete [] sz;
366 sz = result;
367 len += S.len;
370 void
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);
378 delete [] sz;
379 sz = result;
380 len += S.len;
384 // Remove
386 void
387 Simstr::remove(int pos, int anzahl)
389 if (pos >= len || pos < 0 || anzahl < 1)
390 return;
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);
399 delete [] sz;
400 sz = result;
401 len -= anz;
404 void
405 Simstr::remove_trailing_blanks()
407 int newlen = len-1;
408 for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {}
410 if (newlen < len-1)
411 remove ( newlen+1, len-newlen);
414 void
415 Simstr::pop_front(int anzahl)
417 if (anzahl < 1)
418 return;
419 int anz = len < anzahl ? len : anzahl;
421 char * result = new char[len-anz+1];
423 memcpy(result,sz+anz,len-anz+1);
425 delete [] sz;
426 sz = result;
427 len -= anz;
430 void
431 Simstr::pop_back(int anzahl)
433 if (anzahl < 1)
434 return;
436 int anz = len < anzahl ? len : anzahl;
438 char * result = new char[len-anz+1];
440 memcpy(result,sz,len-anz);
441 result[len-anz] = 0;
443 delete [] sz;
444 sz = result;
445 len -= anz;
448 void
449 Simstr::rem_back_from(int removeStartPos)
451 if (removeStartPos != NO_POS)
452 pop_back(len-removeStartPos);
455 void
456 Simstr::remove_all(char c)
458 if (!len)
459 return;
460 char * result = new char[len];
461 int i,j=0;
462 for (i = 0; i < len; i++)
463 if (sz[i] != c)
464 result[j++] = sz[i];
466 delete [] sz;
467 sz = new char[j+1];
468 memcpy(sz,result,j);
469 sz[j] = 0;
470 len = j;
471 delete [] result;
474 void
475 Simstr::remove_all(const Simstr & S)
477 int pos;
478 while ( (pos=pos_first(S)) != NO_POS )
479 remove(pos,S.len);
482 void
483 Simstr::strip(char c)
485 int start = 0;
486 if (c == ' ')
487 { // Sonderbehandlung: SPC entfernt auch TABs:
488 while ( start < len
489 ? sz[start] == ' '
490 || sz[start] == '\t'
491 : false )
492 start++;
494 else
496 while (start < len && sz[start] == c)
497 start++;
500 int ende = len-1;
501 if (c == ' ')
502 { // Sonderbehandlung: SPC entfernt auch TABs:
503 while ( ende >= start
504 ? sz[ende] == ' '
505 || sz[ende] == '\t'
506 : false )
507 ende--;
509 else
511 while (ende >= start && sz[ende] == c)
512 ende--;
514 *this = get(start,ende-start+1);
517 void
518 Simstr::empty()
520 if (len > 0)
522 delete [] sz;
523 sz = new char[1];
524 *sz = 0;
525 len = 0;
529 Simstr
530 Simstr::take_first_token(char c)
532 Simstr ret;
533 int pos = pos_first(c);
534 if (pos != NO_POS)
536 ret = get_front(pos);
537 pop_front(pos+1);
539 else
541 ret = sz;
542 delete [] sz;
543 sz = new char[1];
544 *sz = NULCH;
545 len = 0;
548 return ret;
551 Simstr
552 Simstr::take_last_token(char c)
554 Simstr ret;
555 int pos = pos_last(c);
556 if (pos != NO_POS)
558 ret = get_back(len-pos-1);
559 pop_back(len-pos);
561 else
563 ret = sz;
564 delete [] sz;
565 sz = new char[1];
566 *sz = NULCH;
567 len = 0;
570 return ret;
575 // Find
578 Simstr::pos_first(char c) const
580 int i = 0;
581 for (i = 0; i < len ? sz[i] != c : false; i++) ;
582 if (i >= len)
583 return NO_POS;
584 else
585 return i;
589 Simstr::pos_first_after( char c,
590 int startSearchPos) const
592 int i = 0;
593 if (startSearchPos >= i)
594 i = startSearchPos+1;
595 for (; i < len ? sz[i] != c : false; i++) ;
596 if (i >= len)
597 return NO_POS;
598 else
599 return i;
604 Simstr::pos_last(char c) const
606 int i = 0;
607 for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ;
608 if (i < 0)
609 return NO_POS;
610 else
611 return i;
615 Simstr::pos_first(const Simstr & S) const
617 char * ptr = strstr(sz,S.sz);
618 if (ptr)
619 return int(ptr-sz);
620 else
621 return NO_POS;
625 Simstr::pos_last(const Simstr & S) const
627 Simstr vgl;
628 int i;
629 for (i = len-S.len; i >= 0 ; i--)
631 vgl = get(i,S.len);
632 if (vgl == S)
633 break;
635 if (i >= 0)
636 return i;
637 else
638 return NO_POS;
642 Simstr::count(char c) const
644 int ret = 0;
645 for (int i =0; i < len; i++)
646 if (sz[i] == c)
647 ret++;
648 return ret;
651 bool
652 Simstr::is_no_text() const
654 if (!len)
655 return true;
657 int i;
658 for (i = 0; sz[i] <= 32 && i < len; i++) ;
659 if (i < len)
660 return false;
661 return true;
664 // Change
666 void
667 Simstr::replace(int pos, char c)
669 if (pos < 0 || pos >= len)
670 return;
671 else
672 sz[unsigned(pos)] = c;
675 void
676 Simstr::replace(int startPos, int anzahl, const Simstr & S)
678 if (startPos >= len || startPos < 0 || anzahl < 1)
679 return;
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);
689 delete [] sz;
690 sz = result;
691 len = len-anz+S.len;
694 void
695 Simstr::replace_all(char oldCh, char newCh)
697 for (int i=0; i < len; i++)
698 if (sz[i] == oldCh)
699 sz[i] = newCh;
702 void
703 Simstr::replace_all(const Simstr & oldS, const Simstr & newS)
705 Simstr vgl;
706 int i = 0;
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);
713 i += newS.len;
715 else
716 i++;
720 void
721 Simstr::to_lower()
723 for (int i = 0; i < len; i++)
724 sz[i] = (char) tolower(sz[i]);
729 // Simstr addition
730 Simstr
731 operator+(const char * str, const Simstr & S)
733 Simstr ret = S;
734 ret.push_front(str);
735 return ret;
738 Simstr
739 operator+(const Simstr & S, const char * str)
741 Simstr ret = S;
742 ret.push_back(str);
743 return ret;
746 Simstr
747 operator+(char c, const Simstr & S)
749 Simstr ret = S;
750 ret.push_front(c);
751 return ret;
754 Simstr
755 operator+(const Simstr & S, char c)
757 Simstr ret = S;
758 ret.push_back(c);
759 return ret;
763 // Simstr-Vergleiche mit char *
764 bool
765 operator==(const Simstr & S, const char * str)
767 return strcmp(S,str) == 0;
770 bool
771 operator!=(const Simstr & S, const char * str)
773 return strcmp(S,str) != 0;
776 bool
777 operator<(const Simstr & S, const char * str)
779 return strcmp(S,str) < 0;
782 bool
783 operator>(const Simstr & S, const char * str)
785 return strcmp(S,str) > 0;
788 bool
789 operator<=(const Simstr & S, const char * str)
791 return strcmp(S,str) <= 0;
794 bool
795 operator>=(const Simstr & S, const char * str)
797 return strcmp(S,str) >= 0;
800 bool
801 operator==(const char * str, const Simstr & S)
803 return strcmp(str,S) == 0;
806 bool
807 operator!=(const char * str, const Simstr & S)
809 return strcmp(str,S) != 0;
812 bool
813 operator<(const char * str, const Simstr & S)
815 return strcmp(str,S) < 0;
818 bool
819 operator>(const char * str, const Simstr & S)
821 return strcmp(str,S) > 0;
824 bool
825 operator<=(const char * str, const Simstr & S)
827 return strcmp(str,S) <= 0;
830 bool
831 operator>=(const char * str, const Simstr & S)
833 return strcmp(str,S) >= 0;