1 // Copyright (c) 1993-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <TCollection_AsciiString.hxx>
17 #include <NCollection_UtfIterator.hxx>
18 #include <Standard.hxx>
19 #include <Standard_NegativeValue.hxx>
20 #include <Standard_NullObject.hxx>
21 #include <Standard_NumericError.hxx>
22 #include <Standard_OutOfRange.hxx>
23 #include <TCollection_ExtendedString.hxx>
24 #include <TCollection_HAsciiString.hxx>
29 // Shortcuts to standard allocate and reallocate functions
30 static inline Standard_PCharacter Allocate(const Standard_Size aLength)
32 return (Standard_PCharacter)Standard::Allocate (aLength);
34 static inline Standard_PCharacter Reallocate (Standard_Address aAddr,
35 const Standard_Size aLength)
37 return (Standard_PCharacter)Standard::Reallocate (aAddr, aLength);
39 static inline void Free (Standard_PCharacter aAddr)
41 Standard_Address aPtr = aAddr;
42 Standard::Free (aPtr);
45 // ----------------------------------------------------------------------------
46 // Create an empty AsciiString
47 // ----------------------------------------------------------------------------
48 TCollection_AsciiString::TCollection_AsciiString()
52 mystring = Allocate(mylength+1);
53 mystring[mylength] = '\0';
57 // ----------------------------------------------------------------------------
58 // Create an asciistring from a Standard_CString
59 // ----------------------------------------------------------------------------
60 TCollection_AsciiString::TCollection_AsciiString(const Standard_CString astring)
61 : mystring(0), mylength(0)
64 mylength = Standard_Integer( strlen(astring) );
65 mystring = Allocate(mylength+1);
66 strncpy(mystring,astring,mylength);
67 mystring[mylength] = '\0';
70 Standard_NullObject::Raise("TCollection_AsciiString : parameter 'astring'");
74 // ----------------------------------------------------------------------------
75 // Create an asciistring from a Standard_CString
76 // ----------------------------------------------------------------------------
77 TCollection_AsciiString::TCollection_AsciiString(const Standard_CString astring,
78 const Standard_Integer aLen )
79 : mystring(0), mylength(aLen)
82 mystring = Allocate(mylength+1);
83 strncpy( mystring , astring , mylength );
84 mystring [ mylength ] = '\0' ;
87 Standard_NullObject::Raise("TCollection_AsciiString : parameter 'astring'");
91 // ----------------------------------------------------------------------------
92 // Create an asciistring from a Standard_Character
93 // ----------------------------------------------------------------------------
94 TCollection_AsciiString::TCollection_AsciiString(const Standard_Character aChar)
97 if ( aChar != '\0' ) {
99 mystring = Allocate(2);
105 mystring = Allocate(mylength+1);
106 mystring[mylength] = '\0';
110 // ----------------------------------------------------------------------------
111 // Create an AsciiString from a filler
112 // ----------------------------------------------------------------------------
113 TCollection_AsciiString::TCollection_AsciiString(const Standard_Integer length,
114 const Standard_Character filler )
116 mystring = Allocate(length+1);
118 for (int i = 0 ; i < length ; i++) mystring[i] = filler;
119 mystring[length] = '\0';
122 // ----------------------------------------------------------------------------
123 // Create an AsciiString from an Integer
124 // ----------------------------------------------------------------------------
125 TCollection_AsciiString::TCollection_AsciiString(const Standard_Integer aValue)
129 mylength = Sprintf( t,"%d",aValue);
130 mystring = Allocate(mylength+1);
131 strncpy( mystring , t , mylength );
132 mystring[mylength] = '\0';
135 // ----------------------------------------------------------------------------
136 // Create an asciistring from a real
137 // ----------------------------------------------------------------------------
138 TCollection_AsciiString::TCollection_AsciiString(const Standard_Real aValue)
142 mylength = Sprintf( t,"%g",aValue);
143 mystring = Allocate(mylength+1);
144 strncpy( mystring , t , mylength );
145 mystring[mylength] = '\0';
148 // ----------------------------------------------------------------------------
149 // Create an asciistring from an asciistring
150 // ----------------------------------------------------------------------------
151 TCollection_AsciiString::TCollection_AsciiString(const TCollection_AsciiString& astring)
154 mylength = astring.mylength;
155 mystring = Allocate(mylength+1);
157 strncpy(mystring,astring.mystring,mylength);
158 mystring[mylength] = '\0';
161 // ----------------------------------------------------------------------------
162 // Create an asciistring from a character
163 // ----------------------------------------------------------------------------
164 TCollection_AsciiString::TCollection_AsciiString(
165 const TCollection_AsciiString& astring ,
166 const Standard_Character other )
169 mylength = astring.mylength + 1 ;
170 mystring = Allocate(mylength+1);
171 if ( astring.mylength ) {
172 strncpy( mystring , astring.mystring , astring.mylength ) ;
174 mystring[mylength-1] = other ;
175 mystring[mylength] = '\0' ;
178 // ----------------------------------------------------------------------------
179 // Create an asciistring from an asciistring
180 // ----------------------------------------------------------------------------
181 TCollection_AsciiString::TCollection_AsciiString(
182 const TCollection_AsciiString& astring ,
183 const Standard_CString other )
186 Standard_Integer otherlength = Standard_Integer( other ? strlen( other ) : 0 );
187 mylength = astring.mylength + otherlength ;
188 mystring = Allocate(mylength+1);
189 if ( astring.mylength ) {
190 strncpy( mystring , astring.mystring , astring.mylength ) ;
193 strncpy( mystring + astring.mylength, other, otherlength );
195 mystring[ mylength ] = '\0';
198 // ----------------------------------------------------------------------------
199 // Create an asciistring from an asciistring
200 // ----------------------------------------------------------------------------
201 TCollection_AsciiString::TCollection_AsciiString(
202 const TCollection_AsciiString& astring ,
203 const TCollection_AsciiString& other )
206 mylength = astring.mylength + other.mylength ;
207 mystring = Allocate(mylength+1);
208 if ( astring.mylength ) {
209 strncpy( mystring , astring.mystring , astring.mylength ) ;
211 if ( other.mylength ) {
212 strncpy( mystring + astring.mylength, other.mystring , other.mylength ) ;
214 mystring[mylength] = '\0' ;
217 //---------------------------------------------------------------------------
218 // Create an asciistring from an ExtendedString
219 //---------------------------------------------------------------------------
220 TCollection_AsciiString::TCollection_AsciiString(const TCollection_ExtendedString& astring,
221 const Standard_Character replaceNonAscii)
224 if (replaceNonAscii || astring.IsAscii()) {
225 mylength = astring.Length();
226 mystring = Allocate(mylength+1);
227 for(int i = 0; i < mylength; i++) {
228 Standard_ExtCharacter c = astring.Value(i+1);
229 mystring[i] = ( IsAnAscii(c) ? ToCharacter(c) : replaceNonAscii );
231 mystring[mylength] = '\0';
234 // create UTF-8 string
235 mylength = astring.LengthOfCString();
236 mystring = Allocate(mylength+1);
237 astring.ToUTF8CString(mystring);
241 //---------------------------------------------------------------------------
242 // Create an TCollection_AsciiString from a Standard_WideChar
243 //---------------------------------------------------------------------------
244 TCollection_AsciiString::TCollection_AsciiString (const Standard_WideChar* theStringUtf)
248 for (NCollection_UtfWideIter anIter (theStringUtf); *anIter != 0; ++anIter)
250 mylength += anIter.AdvanceBytesUtf8();
253 mystring = Allocate (mylength + 1);
254 mystring[mylength] = '\0';
255 NCollection_UtfWideIter anIterRead (theStringUtf);
256 for (Standard_Utf8Char* anIterWrite = mystring; *anIterRead != 0; ++anIterRead)
258 anIterWrite = anIterRead.GetUtf(anIterWrite);
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
265 void TCollection_AsciiString::AssignCat(const Standard_Integer other)
268 AssignCat(TCollection_AsciiString(other));
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
275 void TCollection_AsciiString::AssignCat(const Standard_Real other)
278 AssignCat(TCollection_AsciiString(other));
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
285 void TCollection_AsciiString::AssignCat(const Standard_Character other)
288 mystring = Reallocate (mystring, mylength + 2);
289 mystring[mylength] = other ;
291 mystring[mylength] = '\0';
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
298 void TCollection_AsciiString::AssignCat(const Standard_CString other)
301 Standard_Integer otherlength = Standard_Integer( strlen( other ));
303 Standard_Integer newlength = mylength+otherlength;
304 mystring = Reallocate (mystring, newlength + 1);
305 strncpy( mystring + mylength, other, otherlength+1 );
306 mylength = newlength;
310 Standard_NullObject::Raise("TCollection_AsciiString::Operator += parameter other");
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
317 void TCollection_AsciiString::AssignCat(const TCollection_AsciiString& other)
320 if (other.mylength) {
321 Standard_Integer newlength = mylength+other.mylength;
322 mystring = Reallocate (mystring, newlength + 1);
323 strncpy( mystring + mylength, other.mystring, other.mylength+1 );
324 mylength = newlength;
328 // ---------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
331 void TCollection_AsciiString::Capitalize()
333 if ( mylength ) mystring[0] = ::UpperCase(mystring[0]);
334 for (int i = 1; i < mylength; i++ )
335 mystring[i] = ::LowerCase(mystring[i]);
338 // ---------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
341 void TCollection_AsciiString::Center(const Standard_Integer Width ,
342 const Standard_Character Filler)
344 if(Width > mylength) {
345 Standard_Integer newlength = mylength + ((Width - mylength)/2);
346 LeftJustify(newlength,Filler);
347 RightJustify(Width,Filler);
349 else if (Width < 0) {
350 Standard_NegativeValue::Raise();
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
357 void TCollection_AsciiString::ChangeAll(const Standard_Character aChar,
358 const Standard_Character NewChar,
359 const Standard_Boolean CaseSensitive)
362 for (int i=0; i < mylength; i++)
363 if (mystring[i] == aChar) mystring[i] = NewChar;
366 Standard_Character anUpperChar = ::UpperCase(aChar);
367 for (int i=0; i < mylength; i++)
368 if (::UpperCase(mystring[i]) == anUpperChar) mystring[i] = NewChar;
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
375 void TCollection_AsciiString::Clear()
381 mystring = Allocate(mylength+1);
382 mystring[mylength] = '\0';
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
389 void TCollection_AsciiString::Copy(const Standard_CString fromwhere)
392 mylength = Standard_Integer( strlen( fromwhere ));
393 mystring = Reallocate (mystring, mylength + 1);
394 strncpy( mystring, fromwhere, mylength+1 );
398 mystring[mylength] = '\0';
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
405 void TCollection_AsciiString::Copy(const TCollection_AsciiString& fromwhere)
407 if (fromwhere.mystring) {
408 mylength = fromwhere.mylength;
409 mystring = Reallocate (mystring, mylength + 1);
410 strncpy( mystring, fromwhere.mystring, mylength+1 );
414 mystring[mylength] = '\0';
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
421 TCollection_AsciiString::~TCollection_AsciiString()
428 // ----------------------------------------------------------------------------
429 // FirstLocationInSet
430 // ----------------------------------------------------------------------------
431 Standard_Integer TCollection_AsciiString::FirstLocationInSet
432 (const TCollection_AsciiString& Set,
433 const Standard_Integer FromIndex,
434 const Standard_Integer ToIndex) const
436 if (mylength == 0 || Set.mylength == 0) return 0;
437 if (FromIndex > 0 && ToIndex <= mylength && FromIndex <= ToIndex ) {
438 for(int i = FromIndex-1 ; i < ToIndex; i++)
439 for(int j = 0; j < Set.mylength; j++)
440 if (mystring[i] == Set.mystring[j]) return i+1;
443 Standard_OutOfRange::Raise();
447 // ----------------------------------------------------------------------------
448 // FirstLocationNotInSet
449 // ----------------------------------------------------------------------------
450 Standard_Integer TCollection_AsciiString::FirstLocationNotInSet
451 (const TCollection_AsciiString& Set,
452 const Standard_Integer FromIndex,
453 const Standard_Integer ToIndex) const
455 if (mylength == 0 || Set.mylength == 0) return 0;
456 if (FromIndex > 0 && ToIndex <= mylength && FromIndex <= ToIndex ) {
457 Standard_Boolean find;
458 for (int i = FromIndex-1 ; i < ToIndex; i++) {
459 find = Standard_False;
460 for(int j = 0; j < Set.mylength; j++)
461 if (mystring[i] == Set.mystring[j]) find = Standard_True;
462 if (!find) return i+1;
466 Standard_OutOfRange::Raise();
470 //----------------------------------------------------------------------------
471 // Insert a character before 'where'th character
472 // ----------------------------------------------------------------------------
473 void TCollection_AsciiString::Insert(const Standard_Integer where,
474 const Standard_Character what)
476 if (where > mylength + 1 ) Standard_OutOfRange::Raise
477 ("TCollection_AsciiString::Insert : Parameter where is too big");
478 if (where < 1) Standard_OutOfRange::Raise
479 ("TCollection_AsciiString::Insert : Parameter where is too small");
481 mystring = Reallocate (mystring, mylength + 2);
482 if (where != mylength +1) {
483 for (int i=mylength-1; i >= where-1; i--)
484 mystring[i+1] = mystring[i];
486 mystring[where-1] = what;
488 mystring[mylength] = '\0';
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
494 void TCollection_AsciiString::Insert(const Standard_Integer where,
495 const Standard_CString what)
497 if (where <= mylength + 1 && where > 0) {
499 Standard_Integer whatlength = Standard_Integer( strlen( what ) );
500 Standard_Integer newlength = mylength + whatlength;
502 mystring = Reallocate (mystring, newlength + 1);
503 if (where != mylength +1) {
504 for (int i=mylength-1; i >= where-1; i--)
505 mystring[i+whatlength] = mystring[i];
507 for (int i=0; i < whatlength; i++)
508 mystring[where-1+i] = what[i];
510 mylength = newlength;
511 mystring[mylength] = '\0';
515 Standard_OutOfRange::Raise("TCollection_AsciiString::Insert : "
516 "Parameter where is invalid");
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
523 void TCollection_AsciiString::Insert(const Standard_Integer where,
524 const TCollection_AsciiString& what)
526 Standard_CString swhat = what.mystring;
527 if (where <= mylength + 1) {
528 Standard_Integer whatlength = what.mylength;
530 Standard_Integer newlength = mylength + whatlength;
532 mystring = Reallocate (mystring, newlength + 1);
534 if (where != mylength +1) {
535 for (int i=mylength-1; i >= where-1; i--)
536 mystring[i+whatlength] = mystring[i];
538 for (int i=0; i < whatlength; i++)
539 mystring[where-1+i] = swhat[i];
541 mylength = newlength;
542 mystring[mylength] = '\0';
546 Standard_OutOfRange::Raise("TCollection_AsciiString::Insert : "
547 "Parameter where is too big");
551 //------------------------------------------------------------------------
553 //------------------------------------------------------------------------
554 void TCollection_AsciiString::InsertAfter(const Standard_Integer Index,
555 const TCollection_AsciiString& what)
557 if (Index < 0 || Index > mylength) Standard_OutOfRange::Raise();
558 Insert(Index+1,what);
561 //------------------------------------------------------------------------
563 //------------------------------------------------------------------------
564 void TCollection_AsciiString::InsertBefore(const Standard_Integer Index,
565 const TCollection_AsciiString& what)
567 if (Index < 1 || Index > mylength) Standard_OutOfRange::Raise();
571 // ----------------------------------------------------------------------------
573 // ----------------------------------------------------------------------------
574 Standard_Boolean TCollection_AsciiString::IsEqual
575 (const Standard_CString other)const
578 return ( strncmp( other, mystring, mylength+1 ) == 0 );
580 Standard_NullObject::Raise("TCollection_AsciiString::Operator == "
581 "Parameter 'other'");
582 return Standard_False;
585 // ----------------------------------------------------------------------------
587 // ----------------------------------------------------------------------------
588 Standard_Boolean TCollection_AsciiString::IsEqual
589 (const TCollection_AsciiString& other)const
591 if (mylength != other.mylength) return Standard_False;
592 return ( strncmp( other.mystring, mystring, mylength ) == 0 );
595 // ----------------------------------------------------------------------------
597 // ----------------------------------------------------------------------------
598 Standard_Boolean TCollection_AsciiString::IsSameString (const TCollection_AsciiString& theString1,
599 const TCollection_AsciiString& theString2,
600 const Standard_Boolean theIsCaseSensitive)
602 const Standard_Integer aSize1 = theString1.Length();
603 if (aSize1 != theString2.Length())
605 return Standard_False;
608 if (theIsCaseSensitive)
610 return (strncmp (theString1.ToCString(), theString2.ToCString(), aSize1) == 0);
613 for (Standard_Integer aCharIter = 1; aCharIter <= aSize1; ++aCharIter)
615 if (toupper (theString1.Value (aCharIter)) != toupper (theString2.Value (aCharIter)))
617 return Standard_False;
620 return Standard_True;
623 // ----------------------------------------------------------------------------
625 // ----------------------------------------------------------------------------
626 Standard_Boolean TCollection_AsciiString::IsDifferent
627 (const Standard_CString other)const
630 return ( strncmp( other, mystring, mylength+1 ) != 0 );
632 Standard_NullObject::Raise("TCollection_AsciiString::Operator != "
633 "Parameter 'other'");
634 return Standard_False;
637 // ----------------------------------------------------------------------------
639 // ----------------------------------------------------------------------------
640 Standard_Boolean TCollection_AsciiString::IsDifferent
641 (const TCollection_AsciiString& other)const
644 if (mylength != other.mylength) return Standard_True;
645 return ( strncmp( other.mystring, mystring, mylength ) != 0 );
648 // ----------------------------------------------------------------------------
650 // ----------------------------------------------------------------------------
651 Standard_Boolean TCollection_AsciiString::IsLess
652 (const Standard_CString other)const
655 return ( strncmp( mystring, other, mylength+1 ) < 0 );
657 Standard_NullObject::Raise("TCollection_AsciiString::Operator < "
658 "Parameter 'other'");
659 return Standard_False;
662 // ----------------------------------------------------------------------------
664 // ----------------------------------------------------------------------------
665 Standard_Boolean TCollection_AsciiString::IsLess
666 (const TCollection_AsciiString& other)const
668 return ( strncmp( mystring, other.mystring, mylength+1 ) < 0 );
671 // ----------------------------------------------------------------------------
673 // ----------------------------------------------------------------------------
674 Standard_Boolean TCollection_AsciiString::IsGreater
675 (const Standard_CString other)const
678 return ( strncmp( mystring, other, mylength+1 ) > 0 );
680 Standard_NullObject::Raise("TCollection_AsciiString::Operator > "
681 "Parameter 'other'");
682 return Standard_False;
685 // ----------------------------------------------------------------------------
687 // ----------------------------------------------------------------------------
688 Standard_Boolean TCollection_AsciiString::IsGreater
689 (const TCollection_AsciiString& other)const
691 return ( strncmp( mystring, other.mystring, mylength+1 ) > 0 );
694 // ----------------------------------------------------------------------------
696 // ----------------------------------------------------------------------------
697 Standard_Boolean TCollection_AsciiString::StartsWith (const TCollection_AsciiString& theStartString) const
699 if (this == &theStartString)
704 return mylength >= theStartString.mylength
705 && strncmp (theStartString.mystring, mystring, theStartString.mylength) == 0;
708 // ----------------------------------------------------------------------------
710 // ----------------------------------------------------------------------------
711 Standard_Boolean TCollection_AsciiString::EndsWith (const TCollection_AsciiString& theEndString) const
713 if (this == &theEndString)
718 return mylength >= theEndString.mylength
719 && strncmp (theEndString.mystring, mystring + mylength - theEndString.mylength, theEndString.mylength) == 0;
722 // ----------------------------------------------------------------------------
724 // ----------------------------------------------------------------------------
725 Standard_Integer TCollection_AsciiString::IntegerValue()const
728 Standard_Integer value = (Standard_Integer)strtol(mystring,&ptr,10);
729 if (ptr != mystring) return value;
731 Standard_NumericError::Raise("TCollection_AsciiString::IntegerValue");
735 // ----------------------------------------------------------------------------
737 // ----------------------------------------------------------------------------
738 Standard_Boolean TCollection_AsciiString::IsIntegerValue()const
741 strtol(mystring,&ptr,10);
743 if (ptr != mystring) {
744 for (int i=int(ptr-mystring); i < mylength; i++) {
745 if (mystring[i] == '.') return Standard_False; // what about 'e','x',etc ???
747 return Standard_True;
749 return Standard_False;
752 // ----------------------------------------------------------------------------
754 // ----------------------------------------------------------------------------
755 Standard_Boolean TCollection_AsciiString::IsRealValue()const
758 Strtod(mystring,&ptr);
759 return (ptr != mystring);
762 // ----------------------------------------------------------------------------
764 // ----------------------------------------------------------------------------
765 Standard_Boolean TCollection_AsciiString::IsAscii()const
767 // LD : Debuggee le 26/11/98
768 // Cette fonction retournait TOUJOURS Standard_True !
769 for (int i=0; i < mylength; i++)
770 if (mystring[i] >= 127 || mystring[i] < ' ') return Standard_False;
771 return Standard_True;
774 //------------------------------------------------------------------------
776 //------------------------------------------------------------------------
777 void TCollection_AsciiString::LeftAdjust ()
780 for( i = 0 ; i < mylength ; i ++) if(!IsSpace(mystring[i])) break;
781 if( i > 0 ) Remove(1,i);
784 //------------------------------------------------------------------------
786 //------------------------------------------------------------------------
787 void TCollection_AsciiString::LeftJustify(const Standard_Integer Width,
788 const Standard_Character Filler)
790 if (Width > mylength) {
791 mystring = Reallocate (mystring, Width + 1);
792 for (int i = mylength; i < Width ; i++) mystring[i] = Filler;
794 mystring[mylength] = '\0';
796 else if (Width < 0) {
797 Standard_NegativeValue::Raise();
801 //------------------------------------------------------------------------
803 //------------------------------------------------------------------------
804 Standard_Integer TCollection_AsciiString::Location
805 (const Standard_Integer N ,
806 const Standard_Character C ,
807 const Standard_Integer FromIndex,
808 const Standard_Integer ToIndex ) const
810 if (FromIndex > 0 && ToIndex <= mylength && FromIndex <= ToIndex ) {
811 for(int i = FromIndex-1, count = 0; i <= ToIndex-1; i++) {
812 if(mystring[i] == C) {
814 if ( count == N ) return i+1;
819 Standard_OutOfRange::Raise();
823 //------------------------------------------------------------------------
825 //------------------------------------------------------------------------
826 Standard_Integer TCollection_AsciiString::Location
827 (const TCollection_AsciiString& what,
828 const Standard_Integer FromIndex,
829 const Standard_Integer ToIndex) const
831 if (mylength == 0 || what.mylength == 0) return 0;
832 if (ToIndex <= mylength && FromIndex > 0 && FromIndex <= ToIndex ) {
833 Standard_Integer i = FromIndex-1;
834 Standard_Integer k = 1;
835 Standard_Integer l = FromIndex-2;
836 Standard_Boolean Find = Standard_False;
837 while (!Find && i < ToIndex) {
838 if (mystring[i] == what.Value(k)) {
840 if ( k > what.mylength) Find = Standard_True;
843 if (k > 1) i--; // si on est en cours de recherche
849 if (Find) return l+2;
852 Standard_OutOfRange::Raise();
856 // ----------------------------------------------------------------------------
858 // ----------------------------------------------------------------------------
859 void TCollection_AsciiString::LowerCase()
861 for (int i=0; i < mylength; i++)
862 mystring[i] = ::LowerCase(mystring[i]);
865 //------------------------------------------------------------------------
867 //------------------------------------------------------------------------
868 void TCollection_AsciiString::Prepend(const TCollection_AsciiString& what)
873 // ----------------------------------------------------------------------------
875 // ----------------------------------------------------------------------------
876 Standard_Real TCollection_AsciiString::RealValue()const
879 Standard_Real value = Strtod(mystring,&ptr);
880 if (ptr != mystring) return value;
882 Standard_NumericError::Raise("TCollection_AsciiString::RealValue");
886 // ----------------------------------------------------------------------------
888 //--------------------------------------------------------------------------
889 void TCollection_AsciiString::Read(Standard_IStream& astream)
891 // get characters from astream
892 const Standard_Integer bufSize = 8190;
893 Standard_Character buffer[bufSize];
894 std::streamsize oldWidth = astream.width (bufSize);
896 astream.width( oldWidth );
899 mylength = Standard_Integer( strlen( buffer ));
900 mystring = Reallocate (mystring, mylength + 1);
901 strncpy(mystring,buffer,mylength);
902 mystring[mylength] = '\0';
906 //---------------------------------------------------------------------------
907 Standard_IStream& operator >> (Standard_IStream& astream,
908 TCollection_AsciiString& astring)
910 astring.Read(astream);
915 // ----------------------------------------------------------------------------
917 // ----------------------------------------------------------------------------
918 void TCollection_AsciiString::Print(Standard_OStream& astream)const
920 if(mystring) astream << mystring;
924 // ----------------------------------------------------------------------------
925 Standard_OStream& operator << (Standard_OStream& astream,
926 const TCollection_AsciiString& astring)
928 astring.Print(astream);
932 // ----------------------------------------------------------------------------
934 // ----------------------------------------------------------------------------
935 void TCollection_AsciiString::RemoveAll(const Standard_Character what,
936 const Standard_Boolean CaseSensitive)
938 if (mylength == 0) return;
941 for (int i=0; i < mylength; i++)
942 if (mystring[i] != what) mystring[c++] = mystring[i];
945 Standard_Character upperwhat = ::UpperCase(what);
946 for (int i=0; i < mylength; i++) {
947 if (::UpperCase(mystring[i]) != upperwhat) mystring[c++] = mystring[i];
951 mystring[mylength] = '\0';
954 // ----------------------------------------------------------------------------
956 // ----------------------------------------------------------------------------
957 void TCollection_AsciiString::RemoveAll(const Standard_Character what)
959 if (mylength == 0) return;
961 for (int i=0; i < mylength; i++)
962 if (mystring[i] != what) mystring[c++] = mystring[i];
964 mystring[mylength] = '\0';
967 // ----------------------------------------------------------------------------
969 // ----------------------------------------------------------------------------
970 void TCollection_AsciiString::Remove (const Standard_Integer where,
971 const Standard_Integer ahowmany)
973 if (where+ahowmany <= mylength+1) {
975 for(i = where+ahowmany-1, j = where-1; i < mylength; i++, j++)
976 mystring[j] = mystring[i];
977 mylength -= ahowmany;
978 mystring[mylength] = '\0';
981 Standard_OutOfRange::Raise("TCollection_AsciiString::Remove: "
982 "Too many characters to erase or invalid "
987 //------------------------------------------------------------------------
989 //------------------------------------------------------------------------
990 void TCollection_AsciiString::RightAdjust ()
993 for ( i = mylength-1 ; i >= 0 ; i--)
994 if(!IsSpace(mystring[i]))
997 Remove(i+2,mylength-(i+2)+1);
1000 //------------------------------------------------------------------------
1002 //------------------------------------------------------------------------
1003 void TCollection_AsciiString::RightJustify(const Standard_Integer Width,
1004 const Standard_Character Filler)
1006 Standard_Integer i ;
1007 Standard_Integer k ;
1008 if (Width > mylength) {
1009 mystring = Reallocate (mystring, Width + 1);
1011 for ( i = mylength-1, k = Width-1 ; i >= 0 ; i--, k--)
1012 mystring[k] = mystring[i];
1013 for(; k >= 0 ; k--) mystring[k] = Filler;
1015 mystring[mylength] = '\0';
1017 else if (Width < 0) {
1018 Standard_NegativeValue::Raise();
1022 // ----------------------------------------------------------------------------
1024 // ----------------------------------------------------------------------------
1025 Standard_Integer TCollection_AsciiString::Search
1026 (const Standard_CString what)const
1028 Standard_Integer size = Standard_Integer( what ? strlen( what ) : 0 );
1032 while ( i < mylength-size+1 ) {
1035 while (j < size && mystring[k++] == what[j++])
1036 if (j == size) return i;
1043 // ----------------------------------------------------------------------------
1045 // ----------------------------------------------------------------------------
1046 Standard_Integer TCollection_AsciiString::Search
1047 (const TCollection_AsciiString& what) const
1049 Standard_Integer size = what.mylength;
1050 Standard_CString swhat = what.mystring;
1054 while ( i < mylength-size+1 ) {
1057 while (j < size && mystring[k++] == swhat[j++])
1058 if (j == size) return i;
1065 // ----------------------------------------------------------------------------
1067 // ----------------------------------------------------------------------------
1068 Standard_Integer TCollection_AsciiString::SearchFromEnd
1069 (const Standard_CString what)const
1071 Standard_Integer size = Standard_Integer( what ? strlen( what ) : 0 );
1075 while ( i >= size-1 ) {
1078 while (j >= 0 && mystring[k--] == what[j--])
1079 if (j == -1) return i-size+3;
1086 // ----------------------------------------------------------------------------
1088 // ----------------------------------------------------------------------------
1089 Standard_Integer TCollection_AsciiString::SearchFromEnd
1090 (const TCollection_AsciiString& what)const
1092 int size = what.mylength;
1094 Standard_CString swhat = what.mystring;
1097 while ( i >= size-1 ) {
1100 while (j >= 0 && mystring[k--] == swhat[j--])
1101 if (j == -1) return i-size+3;
1107 // ----------------------------------------------------------------------------
1109 // ----------------------------------------------------------------------------
1110 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1111 const Standard_Character what)
1113 if (where > 0 && where <= mylength) {
1114 mystring[where-1] = what;
1117 Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1122 // ----------------------------------------------------------------------------
1124 // ----------------------------------------------------------------------------
1125 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1126 const Standard_CString what)
1128 if (where > 0 && where <= mylength+1) {
1129 Standard_Integer size = Standard_Integer( what ? strlen( what ) : 0 );
1130 size += (where - 1);
1131 if (size >= mylength) {
1132 mystring = Reallocate (mystring, size + 1);
1135 for (int i = where-1; i < size; i++)
1136 mystring[i] = what[i-(where-1)];
1137 mystring[mylength] = '\0';
1140 Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1145 // ----------------------------------------------------------------------------
1147 // ----------------------------------------------------------------------------
1148 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1149 const TCollection_AsciiString& what)
1151 if (where > 0 && where <= mylength+1) {
1152 Standard_Integer size = what.mylength;
1153 Standard_CString swhat = what.mystring;
1154 size += (where - 1);
1155 if (size >= mylength) {
1156 mystring = Reallocate (mystring, size + 1);
1159 for (int i = where-1; i < size; i++)
1160 mystring[i] = swhat[i-(where-1)];
1161 mystring[mylength] = '\0';
1164 Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1169 // ----------------------------------------------------------------------------
1172 // ----------------------------------------------------------------------------
1173 void TCollection_AsciiString::Split(const Standard_Integer where,
1174 TCollection_AsciiString& res)
1176 if (where >= 0 && where <= mylength) {
1177 res = &mystring[where] ;
1181 Standard_OutOfRange::Raise("TCollection_AsciiString::Split index");
1185 // ----------------------------------------------------------------------------
1187 // ----------------------------------------------------------------------------
1188 TCollection_AsciiString TCollection_AsciiString::Split
1189 (const Standard_Integer where)
1191 if (where >= 0 && where <= mylength) {
1192 TCollection_AsciiString res( &mystring[where] , mylength - where );
1196 Standard_OutOfRange::Raise("TCollection_AsciiString::Split index");
1197 TCollection_AsciiString res;
1201 // ----------------------------------------------------------------------------
1204 // ----------------------------------------------------------------------------
1205 void TCollection_AsciiString::SubString(const Standard_Integer FromIndex,
1206 const Standard_Integer ToIndex,
1207 TCollection_AsciiString& res) const
1210 if (ToIndex > mylength || FromIndex <= 0 || FromIndex > ToIndex )
1211 Standard_OutOfRange::Raise();
1212 Standard_Integer newlength = ToIndex-FromIndex+1;
1213 res.mystring =Reallocate (res.mystring, newlength + 1);
1214 strncpy( res.mystring, mystring + FromIndex - 1, newlength );
1215 res.mystring[newlength] = '\0';
1216 res.mylength = newlength;
1220 // ----------------------------------------------------------------------------
1223 // ----------------------------------------------------------------------------
1224 void TCollection_AsciiString::Token(const Standard_CString separators,
1225 const Standard_Integer whichone,
1226 TCollection_AsciiString& res)const
1228 res = Token( separators , whichone ) ;
1231 // ----------------------------------------------------------------------------
1233 // ----------------------------------------------------------------------------
1234 TCollection_AsciiString TCollection_AsciiString::Token
1235 (const Standard_CString separators,
1236 const Standard_Integer whichone) const
1239 Standard_NullObject::Raise("TCollection_AsciiString::Token : "
1240 "parameter 'separators'");
1242 Standard_Integer theOne ;
1243 Standard_Integer StringIndex = 0 ;
1244 Standard_Integer SeparatorIndex ;
1245 Standard_Integer BeginIndex=0 ;
1246 Standard_Integer EndIndex=0 ;
1248 // cout << "'" << mystring << "'" << endl ;
1249 for ( theOne = 0 ; theOne < whichone ; theOne++ ) {
1252 // cout << "theOne " << theOne << endl ;
1253 if ( StringIndex == mylength )
1255 for (; StringIndex < mylength && EndIndex == 0 ; StringIndex++ ) {
1256 SeparatorIndex = 0 ;
1257 // cout << "StringIndex " << StringIndex << endl ;
1258 while ( separators [ SeparatorIndex ] ) {
1259 if ( mystring [ StringIndex ] == separators [ SeparatorIndex ] ) {
1262 SeparatorIndex += 1 ;
1264 if ( separators [ SeparatorIndex ] != '\0' ) { // We have a Separator
1265 if ( BeginIndex && EndIndex == 0 ) {
1266 EndIndex = StringIndex ;
1267 // cout << "EndIndex " << EndIndex << " '" << SubString( BeginIndex , EndIndex ).ToCString() << "'" << endl ;
1271 else if ( BeginIndex == 0 ) { // We have not a Separator
1272 BeginIndex = StringIndex + 1 ;
1273 // cout << "BeginIndex " << BeginIndex << endl ;
1276 // cout << "BeginIndex " << BeginIndex << " EndIndex " << EndIndex << endl ;
1278 if ( BeginIndex == 0 )
1279 return TCollection_AsciiString("",0) ;
1280 if ( EndIndex == 0 )
1281 EndIndex = mylength ;
1282 // cout << "'" << SubString( BeginIndex , EndIndex ).ToCString() << "'" << endl ;
1283 return TCollection_AsciiString( &mystring [ BeginIndex - 1 ] ,
1284 EndIndex - BeginIndex + 1 ) ;
1287 // ----------------------------------------------------------------------------
1289 // ----------------------------------------------------------------------------
1290 void TCollection_AsciiString::Trunc(const Standard_Integer ahowmany)
1292 if (ahowmany < 0 || ahowmany > mylength)
1293 Standard_OutOfRange::Raise("TCollection_AsciiString::Trunc : "
1294 "parameter 'ahowmany'");
1295 mylength = ahowmany;
1296 mystring[mylength] = '\0';
1299 // ----------------------------------------------------------------------------
1301 // ----------------------------------------------------------------------------
1302 void TCollection_AsciiString::UpperCase()
1304 for (int i=0; i < mylength; i++)
1305 mystring[i] = ::UpperCase(mystring[i]);
1308 //------------------------------------------------------------------------
1310 //------------------------------------------------------------------------
1311 Standard_Integer TCollection_AsciiString::UsefullLength () const
1313 Standard_Integer i ;
1314 for ( i = mylength -1 ; i >= 0 ; i--)
1315 if (IsGraphic(mystring[i])) break;
1319 // ----------------------------------------------------------------------------
1321 // ----------------------------------------------------------------------------
1322 Standard_Character TCollection_AsciiString::Value
1323 (const Standard_Integer where)const
1325 if (where > 0 && where <= mylength) {
1326 return mystring[where-1];
1328 Standard_OutOfRange::Raise("TCollection_AsciiString::Value : parameter where");