Bump for 3.6-28
[LibreOffice.git] / soltools / support / simstr.cxx
blobd1e0f41cd404a425223cdb0cf2241ee4c99d41e1
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 ************************************************************************/
31 #include <simstr.hxx>
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_)
43 if (s_ == 0)
45 len = 0;
46 sz = new char[1];
47 *sz = 0;
49 else
51 len = (int)strlen(s_);
52 sz = new char[len+1];
53 memcpy(sz,s_,len+1);
57 Simstr::Simstr(const char * anybytes, int nrOfBytes)
59 if (anybytes == 0)
61 len = 0;
62 sz = new char[1];
63 *sz = 0;
64 return;
67 int slen = static_cast<int>( strlen(anybytes) );
69 len = slen < nrOfBytes
70 ? slen
71 : nrOfBytes;
72 sz = new char[len+1];
73 memcpy( sz, anybytes, len );
74 *( sz + len ) = 0;
77 Simstr::Simstr(char c, int anzahl)
79 if (anzahl < 1)
81 len = 0;
82 sz = new char[1];
83 *sz = 0;
85 else
87 len = anzahl;
88 sz = new char[len+1];
89 memset(sz,c,anzahl);
90 sz[len] = 0;
94 Simstr::Simstr( const char * anybytes,
95 int firstBytesPos,
96 int nrOfBytes)
98 unsigned slen = (unsigned)strlen(anybytes);
99 if (anybytes == 0 || slen <= unsigned(firstBytesPos))
101 len = 0;
102 sz = new char[1];
103 *sz = 0;
105 else
107 int maxLen = slen - unsigned(firstBytesPos);
108 len = maxLen < nrOfBytes
109 ? maxLen
110 : nrOfBytes;
111 sz = new char[len+1];
112 memcpy(sz,anybytes+firstBytesPos,len);
113 *(sz+len) = 0;
118 Simstr::Simstr(const Simstr & S)
120 len = S.len;
121 sz = new char[len+1];
122 memcpy(sz,S.sz,len+1);
125 Simstr & Simstr::operator=(const Simstr & S)
127 if (sz == S.sz)
128 return *this;
130 delete [] sz;
132 len = S.len;
133 sz = new char[len+1];
134 memcpy(sz,S.sz,len+1);
136 return *this;
139 Simstr::~Simstr()
141 delete [] sz;
144 char &
145 Simstr::ch(int n)
147 static char nullCh = NULCH;
148 nullCh = NULCH;
149 if (n >= long(len) || n < 0)
150 return nullCh;
151 else
152 return sz[unsigned(n)];
155 const Simstr &
156 Simstr::null_()
158 static Simstr aNull_;
159 return aNull_;
163 Simstr
164 Simstr::operator+(const Simstr & S) const
166 Simstr ret = sz;
167 ret.push_back(S);
168 return ret;
171 Simstr &
172 Simstr::operator+=(const Simstr & S)
174 push_back(S);
175 return *this;
178 Simstr &
179 Simstr::operator+=(const char * s_)
181 Simstr a(s_);
182 push_back(a);
183 return *this;
187 // REL
189 bool
190 Simstr::operator==(const Simstr & S) const
191 { return !strcmp(sz,S.sz) ? true : false; }
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) < 0) ? 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; }
216 // ************** LIST - Funktionen *****************
219 // Einzelzugriff
221 char
222 Simstr::get(int n) const { return (n >= len || n < 0) ? 0 : sz[n]; }
224 char
225 Simstr::get_front() const { return sz[0]; }
227 char
228 Simstr::get_back() const { return len ? sz[len-1] : 0; }
230 Simstr
231 Simstr::get(int startPos, int anzahl) const
233 if (startPos >= len || startPos < 0 || anzahl < 1)
234 return "";
236 int anz = len - startPos < anzahl ? len - startPos : anzahl;
238 Simstr ret(' ',anz);
239 memcpy(ret.sz, sz+startPos, anz);
240 return ret;
243 Simstr
244 Simstr::get_front(int anzahl) const
246 int anz = len < anzahl ? len : anzahl;
247 if (anz < 1)
248 return "";
250 Simstr ret(' ',anz);
251 memcpy(ret.sz, sz, anz);
252 return ret;
255 Simstr
256 Simstr::get_back(int anzahl) const
258 int anz = len < anzahl ? len : anzahl;
259 if (anz < 1)
260 return "";
261 int start = len-anz;
263 Simstr ret(' ',anz);
264 memcpy(ret.sz, sz+start, anz);
265 return ret;
268 Simstr
269 Simstr::get_first_token(char c) const
271 int posc = pos_first(c);
272 if (posc != NO_POS)
273 return get_front(posc);
274 else
275 return sz;
278 Simstr
279 Simstr::get_last_token(char c) const
281 int posc = pos_last(c);
282 if (posc != NO_POS)
283 return get_back(len-posc-1);
284 else
285 return sz;
290 // Insert
292 void
293 Simstr::insert(int pos, char c)
295 if (pos < 0 || pos > len)
296 return;
298 char * result = new char[len+2];
300 memcpy(result,sz,pos);
301 result[pos] = c;
302 memcpy(result+pos+1,sz+pos,len-pos+1);
304 delete [] sz;
305 sz = result;
306 len++;
309 void
310 Simstr::push_front(char c)
312 char * result = new char[len+2];
314 result[0] = c;
315 memcpy(result+1,sz,len+1);
317 delete [] sz;
318 sz = result;
319 len++;
322 void
323 Simstr::push_back(char c)
325 char * result = new char[len+2];
327 memcpy(result,sz,len);
328 result[len] = c;
329 result[len+1] = 0;
331 delete [] sz;
332 sz = result;
333 len++;
336 void
337 Simstr::insert(int pos, const Simstr & S)
339 if (pos < 0 || pos > len)
340 return;
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);
348 delete [] sz;
349 sz = result;
350 len += S.len;
353 void
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);
361 delete [] sz;
362 sz = result;
363 len += S.len;
366 void
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);
374 delete [] sz;
375 sz = result;
376 len += S.len;
380 // Remove
382 void
383 Simstr::remove(int pos, int anzahl)
385 if (pos >= len || pos < 0 || anzahl < 1)
386 return;
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);
395 delete [] sz;
396 sz = result;
397 len -= anz;
400 void
401 Simstr::remove_trailing_blanks()
403 int newlen = len-1;
404 for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {}
406 if (newlen < len-1)
407 remove ( newlen+1, len-newlen);
410 void
411 Simstr::pop_front(int anzahl)
413 if (anzahl < 1)
414 return;
415 int anz = len < anzahl ? len : anzahl;
417 char * result = new char[len-anz+1];
419 memcpy(result,sz+anz,len-anz+1);
421 delete [] sz;
422 sz = result;
423 len -= anz;
426 void
427 Simstr::pop_back(int anzahl)
429 if (anzahl < 1)
430 return;
432 int anz = len < anzahl ? len : anzahl;
434 char * result = new char[len-anz+1];
436 memcpy(result,sz,len-anz);
437 result[len-anz] = 0;
439 delete [] sz;
440 sz = result;
441 len -= anz;
444 void
445 Simstr::rem_back_from(int removeStartPos)
447 if (removeStartPos != NO_POS)
448 pop_back(len-removeStartPos);
451 void
452 Simstr::remove_all(char c)
454 if (!len)
455 return;
456 char * result = new char[len];
457 int i,j=0;
458 for (i = 0; i < len; i++)
459 if (sz[i] != c)
460 result[j++] = sz[i];
462 delete [] sz;
463 sz = new char[j+1];
464 memcpy(sz,result,j);
465 sz[j] = 0;
466 len = j;
467 delete [] result;
470 void
471 Simstr::remove_all(const Simstr & S)
473 int pos;
474 while ( (pos=pos_first(S)) != NO_POS )
475 remove(pos,S.len);
478 void
479 Simstr::strip(char c)
481 int start = 0;
482 if (c == ' ')
483 { // Sonderbehandlung: SPC entfernt auch TABs:
484 while ( start < len
485 ? sz[start] == ' '
486 || sz[start] == '\t'
487 : false )
488 start++;
490 else
492 while (start < len && sz[start] == c)
493 start++;
496 int ende = len-1;
497 if (c == ' ')
498 { // Sonderbehandlung: SPC entfernt auch TABs:
499 while ( ende >= start
500 ? sz[ende] == ' '
501 || sz[ende] == '\t'
502 : false )
503 ende--;
505 else
507 while (ende >= start && sz[ende] == c)
508 ende--;
510 *this = get(start,ende-start+1);
513 void
514 Simstr::empty()
516 if (len > 0)
518 delete [] sz;
519 sz = new char[1];
520 *sz = 0;
521 len = 0;
525 Simstr
526 Simstr::take_first_token(char c)
528 Simstr ret;
529 int pos = pos_first(c);
530 if (pos != NO_POS)
532 ret = get_front(pos);
533 pop_front(pos+1);
535 else
537 ret = sz;
538 delete [] sz;
539 sz = new char[1];
540 *sz = NULCH;
541 len = 0;
544 return ret;
547 Simstr
548 Simstr::take_last_token(char c)
550 Simstr ret;
551 int pos = pos_last(c);
552 if (pos != NO_POS)
554 ret = get_back(len-pos-1);
555 pop_back(len-pos);
557 else
559 ret = sz;
560 delete [] sz;
561 sz = new char[1];
562 *sz = NULCH;
563 len = 0;
566 return ret;
571 // Find
574 Simstr::pos_first(char c) const
576 int i = 0;
577 for (i = 0; i < len ? sz[i] != c : false; i++) ;
578 if (i >= len)
579 return NO_POS;
580 else
581 return i;
585 Simstr::pos_first_after( char c,
586 int startSearchPos) const
588 int i = 0;
589 if (startSearchPos >= i)
590 i = startSearchPos+1;
591 for (; i < len ? sz[i] != c : false; i++) ;
592 if (i >= len)
593 return NO_POS;
594 else
595 return i;
600 Simstr::pos_last(char c) const
602 int i = 0;
603 for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ;
604 if (i < 0)
605 return NO_POS;
606 else
607 return i;
611 Simstr::pos_first(const Simstr & S) const
613 char * ptr = strstr(sz,S.sz);
614 if (ptr)
615 return int(ptr-sz);
616 else
617 return NO_POS;
621 Simstr::pos_last(const Simstr & S) const
623 Simstr vgl;
624 int i;
625 for (i = len-S.len; i >= 0 ; i--)
627 vgl = get(i,S.len);
628 if (vgl == S)
629 break;
631 if (i >= 0)
632 return i;
633 else
634 return NO_POS;
638 Simstr::count(char c) const
640 int ret = 0;
641 for (int i =0; i < len; i++)
642 if (sz[i] == c)
643 ret++;
644 return ret;
647 bool
648 Simstr::is_no_text() const
650 if (!len)
651 return true;
653 int i;
654 for (i = 0; i < len && sz[i] <= 32 ; i++) ;
655 if (i < len)
656 return false;
657 return true;
660 // Change
662 void
663 Simstr::replace(int pos, char c)
665 if (pos < 0 || pos >= len)
666 return;
667 else
668 sz[unsigned(pos)] = c;
671 void
672 Simstr::replace(int startPos, int anzahl, const Simstr & S)
674 if (startPos >= len || startPos < 0 || anzahl < 1)
675 return;
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);
685 delete [] sz;
686 sz = result;
687 len = len-anz+S.len;
690 void
691 Simstr::replace_all(char oldCh, char newCh)
693 for (int i=0; i < len; i++)
694 if (sz[i] == oldCh)
695 sz[i] = newCh;
698 void
699 Simstr::replace_all(const Simstr & oldS, const Simstr & newS)
701 Simstr vgl;
702 int i = 0;
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);
709 i += newS.len;
711 else
712 i++;
716 void
717 Simstr::to_lower()
719 for (int i = 0; i < len; i++)
720 sz[i] = (char) tolower(sz[i]);
725 // Simstr addition
726 Simstr
727 operator+(const char * str, const Simstr & S)
729 Simstr ret = S;
730 ret.push_front(str);
731 return ret;
734 Simstr
735 operator+(const Simstr & S, const char * str)
737 Simstr ret = S;
738 ret.push_back(str);
739 return ret;
742 Simstr
743 operator+(char c, const Simstr & S)
745 Simstr ret = S;
746 ret.push_front(c);
747 return ret;
750 Simstr
751 operator+(const Simstr & S, char c)
753 Simstr ret = S;
754 ret.push_back(c);
755 return ret;
759 // Simstr-Vergleiche mit char *
760 bool
761 operator==(const Simstr & S, const char * str)
763 return strcmp(S,str) == 0;
766 bool
767 operator!=(const Simstr & S, const char * str)
769 return strcmp(S,str) != 0;
772 bool
773 operator<(const Simstr & S, const char * str)
775 return strcmp(S,str) < 0;
778 bool
779 operator>(const Simstr & S, const char * str)
781 return strcmp(S,str) > 0;
784 bool
785 operator<=(const Simstr & S, const char * str)
787 return strcmp(S,str) <= 0;
790 bool
791 operator>=(const Simstr & S, const char * str)
793 return strcmp(S,str) >= 0;
796 bool
797 operator==(const char * str, const Simstr & S)
799 return strcmp(str,S) == 0;
802 bool
803 operator!=(const char * str, const Simstr & S)
805 return strcmp(str,S) != 0;
808 bool
809 operator<(const char * str, const Simstr & S)
811 return strcmp(str,S) < 0;
814 bool
815 operator>(const char * str, const Simstr & S)
817 return strcmp(str,S) > 0;
820 bool
821 operator<=(const char * str, const Simstr & S)
823 return strcmp(str,S) <= 0;
826 bool
827 operator>=(const char * str, const Simstr & S)
829 return strcmp(str,S) >= 0;
833 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */