2 //Copyright: Matra Datavision 1993
3 //Created By M. MERCIEN Fev,25 1993
7 // OCC6794: if OptJr is ON, we need to ensure that memory allocations are done by 4 bytes words,
8 // in order to avoid writing into unallocated memory at the string end when it is copied
9 // by CSTRINGCOPY or ASCIISTRINGCOPY macros
10 // (though, due to OCC memory manager roundings of allocated memory, the problem appears
11 // only when MMGT_OPT is 0 and string size is greater than MMGT_THRESHOLD)
13 #define ROUNDMEM(len) (((len)+3)&~0x3)
15 #define ROUNDMEM(len) (len)
18 //#if defined(WNT) || defined(LIN)
22 #include <TCollection_AsciiString.ixx>
23 #include <Standard.hxx>
24 #include <Standard_NullObject.hxx>
25 #include <Standard_OutOfRange.hxx>
26 #include <Standard_NegativeValue.hxx>
27 #include <Standard_NumericError.hxx>
28 #include <Standard_ctype.hxx>
29 #include <Standard_String.hxx>
31 #include <TCollection_ExtendedString.hxx>
33 // ###### PLACER LE TYPE NON DEFINI strtol (portabilite) ######
38 long strtol(const char*, char**, int);
42 // Shortcuts to standard allocate and reallocate functions
43 static inline Standard_PCharacter Allocate(const Standard_Size aLength)
45 return (Standard_PCharacter)Standard::Allocate (aLength);
47 static inline Standard_PCharacter Reallocate (Standard_Address aAddr,
48 const Standard_Size aLength)
50 return (Standard_PCharacter)Standard::Reallocate (aAddr, aLength);
53 // ----------------------------------------------------------------------------
54 // Create an empty AsciiString
55 // ----------------------------------------------------------------------------
56 TCollection_AsciiString::TCollection_AsciiString()
60 mystring = Allocate(mylength+1);
61 mystring[mylength] = '\0';
65 // ----------------------------------------------------------------------------
66 // Create an asciistring from a Standard_CString
67 // ----------------------------------------------------------------------------
68 TCollection_AsciiString::TCollection_AsciiString(const Standard_CString astring)
73 STRINGLEN( astring , mylength ) ;
75 STRLEN(astring,mylength);
77 mystring = Allocate(ROUNDMEM(mylength+1));
79 CSTRINGCOPY( mystring , astring , mylength ) ;
81 STRCPY(mystring,astring,mylength);
82 mystring[mylength] = '\0';
86 Standard_NullObject::Raise("TCollection_AsciiString : parameter 'astring'");
90 // ----------------------------------------------------------------------------
91 // Create an asciistring from a Standard_CString
92 // ----------------------------------------------------------------------------
93 TCollection_AsciiString::TCollection_AsciiString(const Standard_CString astring,
94 const Standard_Integer aLen )
99 mystring = Allocate(ROUNDMEM(mylength+1));
100 CSTRINGCOPY( mystring , astring , mylength ) ;
101 mystring [ mylength ] = '\0' ;
104 Standard_NullObject::Raise("TCollection_AsciiString : parameter 'astring'");
108 // ----------------------------------------------------------------------------
109 // Create an asciistring from a Standard_Character
110 // ----------------------------------------------------------------------------
111 TCollection_AsciiString::TCollection_AsciiString(const Standard_Character aChar)
114 if ( aChar != '\0' ) {
116 mystring = Allocate(2);
122 mystring = Allocate(mylength+1);
123 mystring[mylength] = '\0';
127 // ----------------------------------------------------------------------------
128 // Create an AsciiString from a filler
129 // ----------------------------------------------------------------------------
130 TCollection_AsciiString::TCollection_AsciiString(const Standard_Integer length,
131 const Standard_Character filler )
133 mystring = Allocate(length+1);
135 for (int i = 0 ; i < length ; i++) mystring[i] = filler;
136 mystring[length] = '\0';
139 // ----------------------------------------------------------------------------
140 // Create an AsciiString from an Integer
141 // ----------------------------------------------------------------------------
142 TCollection_AsciiString::TCollection_AsciiString(const Standard_Integer aValue)
146 union { int bid ; // ?? to ensure alignment of t[] by double-word??
148 sprintf(&CHN.t[0],"%d",aValue);
150 STRINGLEN( CHN.t , mylength ) ;
152 STRLEN(CHN.t,mylength);
154 mystring = Allocate(ROUNDMEM(mylength+1));
156 ASCIISTRINGCOPY( mystring , CHN.t , mylength ) ;
158 STRCPY(mystring,CHN.t,mylength);
159 mystring[mylength] = '\0';
163 // ----------------------------------------------------------------------------
164 // Create an asciistring from a real
165 // ----------------------------------------------------------------------------
166 TCollection_AsciiString::TCollection_AsciiString(const Standard_Real aValue)
172 sprintf(&CHN.t[0],"%g",aValue);
174 STRINGLEN( CHN.t , mylength ) ;
176 STRLEN(CHN.t,mylength);
178 mystring = Allocate(ROUNDMEM(mylength+1));
180 ASCIISTRINGCOPY( mystring , CHN.t , mylength ) ;
182 STRCPY(mystring,CHN.t,mylength);
183 mystring[mylength] = '\0';
187 // ----------------------------------------------------------------------------
188 // Create an asciistring from an asciistring
189 // ----------------------------------------------------------------------------
190 TCollection_AsciiString::TCollection_AsciiString(const TCollection_AsciiString& astring)
194 mylength = astring.mylength;
195 mystring = Allocate(ROUNDMEM(mylength+1));
196 if ( astring.mystring ) {
198 ASCIISTRINGCOPY( mystring , astring.mystring , mylength ) ;
202 STRCPY(mystring,astring.mystring,mylength);
205 mystring[mylength] = '\0';
208 // ----------------------------------------------------------------------------
209 // Create an asciistring from a character
210 // ----------------------------------------------------------------------------
211 TCollection_AsciiString::TCollection_AsciiString(
212 const TCollection_AsciiString& astring ,
213 const Standard_Character other )
217 mylength = astring.mylength + 1 ;
218 mystring = Allocate(ROUNDMEM(mylength+1));
219 if ( astring.mystring ) {
220 ASCIISTRINGCOPY( mystring , astring.mystring , astring.mylength ) ;
222 mystring[mylength-1] = other ;
223 mystring[mylength] = '\0' ;
226 // ----------------------------------------------------------------------------
227 // Create an asciistring from an asciistring
228 // ----------------------------------------------------------------------------
229 TCollection_AsciiString::TCollection_AsciiString(
230 const TCollection_AsciiString& astring ,
231 const Standard_CString other )
234 Standard_Integer otherlength;
236 STRINGLEN( other , otherlength );
237 mylength = astring.mylength + otherlength ;
238 mystring = Allocate(ROUNDMEM(mylength+1));
239 if ( astring.mystring ) {
240 ASCIISTRINGCOPY( mystring , astring.mystring , astring.mylength ) ;
242 STRINGCAT( mystring , astring.mylength , other , otherlength ) ;
245 // ----------------------------------------------------------------------------
246 // Create an asciistring from an asciistring
247 // ----------------------------------------------------------------------------
248 TCollection_AsciiString::TCollection_AsciiString(
249 const TCollection_AsciiString& astring ,
250 const TCollection_AsciiString& other )
254 mylength = astring.mylength + other.mylength ;
255 mystring = Allocate(ROUNDMEM(mylength+1));
256 if ( astring.mystring ) {
257 ASCIISTRINGCOPY( mystring , astring.mystring , astring.mylength ) ;
259 if ( other.mystring ) {
260 STRINGCAT( mystring , astring.mylength , other.mystring , other.mylength ) ;
267 //---------------------------------------------------------------------------
268 // Create an asciistring from an ExtendedString
269 //---------------------------------------------------------------------------
270 TCollection_AsciiString::TCollection_AsciiString(const TCollection_ExtendedString& astring,
271 const Standard_Character replaceNonAscii)
274 if (replaceNonAscii || astring.IsAscii()) {
275 mylength = astring.Length();
276 mystring = Allocate(mylength+1);
277 for(int i = 0; i < mylength; i++) {
278 Standard_ExtCharacter c = astring.Value(i+1);
279 mystring[i] = ( IsAnAscii(c) ? ToCharacter(c) : replaceNonAscii );
281 mystring[mylength] = '\0';
284 Standard_SStream amsg;
285 amsg << "It's not an ascii string : " ;
287 Standard_OutOfRange::Raise(amsg);
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
294 void TCollection_AsciiString::AssignCat(const Standard_Integer other)
297 AssignCat(TCollection_AsciiString(other));
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
304 void TCollection_AsciiString::AssignCat(const Standard_Real other)
307 AssignCat(TCollection_AsciiString(other));
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
314 void TCollection_AsciiString::AssignCat(const Standard_Character other)
318 mystring = Reallocate((void*&)mystring,
322 mystring = Allocate(mylength+2);
324 mystring[mylength] = other ;
326 mystring[mylength] = '\0';
330 // ----------------------------------------------------------------------------
332 // ----------------------------------------------------------------------------
333 void TCollection_AsciiString::AssignCat(const Standard_CString other)
336 if (other[0] != '\0') {
337 Standard_Integer otherlength;
339 STRINGLEN( other , otherlength ) ;
341 STRLEN(other,otherlength);
343 Standard_Integer newlength = mylength+otherlength;
345 mystring = Reallocate((void*&)mystring,
346 ROUNDMEM(newlength+1));
348 STRINGCAT( mystring , mylength , other , otherlength ) ;
350 STRCAT(mystring,mylength,other,otherlength);
354 mystring = Allocate(ROUNDMEM(newlength+1));
356 CSTRINGCOPY( mystring , other , newlength ) ;
358 STRCPY(mystring,other,newlength);
361 mylength = newlength;
363 mystring[mylength] = '\0';
368 Standard_NullObject::Raise("TCollection_AsciiString::Operator += parameter other");
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
375 void TCollection_AsciiString::AssignCat(const TCollection_AsciiString& other)
378 Standard_Integer otherlength = other.mylength;
380 Standard_CString sother = other.mystring;
381 Standard_Integer newlength = mylength+otherlength;
383 mystring = Reallocate((void*&)mystring,
384 ROUNDMEM(newlength+1));
386 STRINGCAT( mystring , mylength , sother , otherlength ) ;
388 STRCAT(mystring,mylength,sother,otherlength);
392 mystring =Allocate(ROUNDMEM(newlength+1));
394 ASCIISTRINGCOPY( mystring , sother , newlength ) ;
396 STRCPY(mystring,sother,newlength);
399 mylength = newlength;
401 mystring[mylength] = '\0';
406 // ---------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
409 void TCollection_AsciiString::Capitalize()
411 for (int i=0; i < mylength; i++) {
412 if ( i==0 ) mystring[i] = toupper(mystring[i]);
413 else mystring[i] = tolower(mystring[i]);
417 // ---------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
420 void TCollection_AsciiString::Center(const Standard_Integer Width ,
421 const Standard_Character Filler)
423 if(Width > mylength) {
424 Standard_Integer newlength = mylength + ((Width - mylength)/2);
425 LeftJustify(newlength,Filler);
426 RightJustify(Width,Filler);
428 else if (Width < 0) {
429 Standard_NegativeValue::Raise();
433 // ----------------------------------------------------------------------------
435 // ----------------------------------------------------------------------------
436 void TCollection_AsciiString::ChangeAll(const Standard_Character aChar,
437 const Standard_Character NewChar,
438 const Standard_Boolean CaseSensitive)
441 for (int i=0; i < mylength; i++)
442 if (mystring[i] == aChar) mystring[i] = NewChar;
445 Standard_Character anUpperChar = toupper(aChar);
446 for (int i=0; i < mylength; i++)
447 if (toupper(mystring[i]) == anUpperChar) mystring[i] = NewChar;
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
454 void TCollection_AsciiString::Clear()
456 if (mystring) Standard::Free((void*&)mystring);
458 mystring = Allocate(mylength+1);
459 mystring[mylength] = '\0';
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
465 void TCollection_AsciiString::Copy(const Standard_CString fromwhere)
469 Standard_Integer newlength;
471 STRINGLEN( fromwhere , newlength ) ;
473 STRLEN(fromwhere,newlength);
477 mystring = Reallocate((void*&)mystring,
478 ROUNDMEM(newlength+1));
482 mystring = Allocate( ROUNDMEM(newlength + 1) );
485 CSTRINGCOPY( mystring , fromwhere , newlength ) ;
486 mylength = newlength;
488 STRCPY(mystring, fromwhere,newlength);
489 mystring[newlength] = '\0';
490 mylength = newlength;
496 mystring[mylength] = '\0';
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
504 void TCollection_AsciiString::Copy(const TCollection_AsciiString& fromwhere)
507 if (fromwhere.mystring) {
508 Standard_Integer newlength = fromwhere.mylength;
511 mystring = Reallocate((void*&)mystring,
512 ROUNDMEM(newlength+1));
516 mystring =Allocate(ROUNDMEM(newlength+1));
519 ASCIISTRINGCOPY( mystring , fromwhere.mystring , newlength ) ;
520 mylength = newlength;
522 STRCPY(mystring, fromwhere.mystring,newlength);
523 mylength = newlength;
524 mystring[mylength] = '\0';
530 mystring[mylength] = '\0';
536 // ----------------------------------------------------------------------------
538 // ----------------------------------------------------------------------------
539 void TCollection_AsciiString::Destroy()
541 if (mystring) Standard::Free((void*&)mystring);
545 // ----------------------------------------------------------------------------
546 // FirstLocationInSet
547 // ----------------------------------------------------------------------------
548 Standard_Integer TCollection_AsciiString::FirstLocationInSet
549 (const TCollection_AsciiString& Set,
550 const Standard_Integer FromIndex,
551 const Standard_Integer ToIndex) const
553 if (mylength == 0 || Set.mylength == 0) return 0;
554 if (FromIndex > 0 && ToIndex <= mylength && FromIndex <= ToIndex ) {
555 for(int i = FromIndex-1 ; i < ToIndex; i++)
556 for(int j = 0; j < Set.mylength; j++)
557 if (mystring[i] == Set.mystring[j]) return i+1;
560 Standard_OutOfRange::Raise();
564 // ----------------------------------------------------------------------------
565 // FirstLocationNotInSet
566 // ----------------------------------------------------------------------------
567 Standard_Integer TCollection_AsciiString::FirstLocationNotInSet
568 (const TCollection_AsciiString& Set,
569 const Standard_Integer FromIndex,
570 const Standard_Integer ToIndex) const
572 if (mylength == 0 || Set.mylength == 0) return 0;
573 if (FromIndex > 0 && ToIndex <= mylength && FromIndex <= ToIndex ) {
574 Standard_Boolean find;
575 for (int i = FromIndex-1 ; i < ToIndex; i++) {
576 find = Standard_False;
577 for(int j = 0; j < Set.mylength; j++)
578 if (mystring[i] == Set.mystring[j]) find = Standard_True;
579 if (!find) return i+1;
583 Standard_OutOfRange::Raise();
587 //----------------------------------------------------------------------------
588 // Insert a character before 'where'th character
589 // ----------------------------------------------------------------------------
590 void TCollection_AsciiString::Insert(const Standard_Integer where,
591 const Standard_Character what)
593 if (where > mylength + 1 ) Standard_OutOfRange::Raise
594 ("TCollection_AsciiString::Insert : Parameter where is too big");
595 if (where < 0) Standard_OutOfRange::Raise
596 ("TCollection_AsciiString::Insert : Parameter where is negative");
599 // Standard_Integer length = *(int*)((long)mystring-8);
600 // if((mylength+1) >> 4 > length-1 >> 4)
601 mystring = Reallocate((void*&)mystring,
605 mystring = Allocate(mylength+2);
607 if (where != mylength +1) {
608 for (int i=mylength-1; i >= where-1; i--)
609 mystring[i+1] = mystring[i];
611 mystring[where-1] = what;
613 mystring[mylength] = '\0';
616 // ----------------------------------------------------------------------------
618 // ----------------------------------------------------------------------------
619 void TCollection_AsciiString::Insert(const Standard_Integer where,
620 const Standard_CString what)
622 if (where <= mylength + 1) {
624 Standard_Integer whatlength;
626 STRINGLEN( what , whatlength );
628 STRLEN(what,whatlength);
630 Standard_Integer newlength = mylength + whatlength;
633 // Standard_Integer length = *(int*)((long)mystring-8);
634 // if(newlength >> 4 > length-1 >> 4)
635 mystring = Reallocate((void*&)mystring,
639 mystring =Allocate(newlength+1);
641 if (where != mylength +1) {
642 for (int i=mylength-1; i >= where-1; i--)
643 mystring[i+whatlength] = mystring[i];
645 for (int i=0; i < whatlength; i++)
646 mystring[where-1+i] = what[i];
648 mylength = newlength;
649 mystring[mylength] = '\0';
653 Standard_OutOfRange::Raise("TCollection_AsciiString::Insert : "
654 "Parameter where is too big");
658 // ----------------------------------------------------------------------------
660 // ----------------------------------------------------------------------------
661 void TCollection_AsciiString::Insert(const Standard_Integer where,
662 const TCollection_AsciiString& what)
664 Standard_CString swhat = what.mystring;
665 if (where <= mylength + 1) {
666 Standard_Integer whatlength = what.mylength;
668 Standard_Integer newlength = mylength + whatlength;
671 // Standard_Integer length = *(int*)((long)mystring-8);
672 // if(newlength >> 4 > length-1 >> 4)
673 mystring = Reallocate((void*&)mystring,
677 mystring =Allocate(newlength+1);
679 if (where != mylength +1) {
680 for (int i=mylength-1; i >= where-1; i--)
681 mystring[i+whatlength] = mystring[i];
683 for (int i=0; i < whatlength; i++)
684 mystring[where-1+i] = swhat[i];
686 mylength = newlength;
687 mystring[mylength] = '\0';
691 Standard_OutOfRange::Raise("TCollection_AsciiString::Insert : "
692 "Parameter where is too big");
696 //------------------------------------------------------------------------
698 //------------------------------------------------------------------------
699 void TCollection_AsciiString::InsertAfter(const Standard_Integer Index,
700 const TCollection_AsciiString& what)
702 if (Index < 0 || Index > mylength) Standard_OutOfRange::Raise();
703 Insert(Index+1,what);
706 //------------------------------------------------------------------------
708 //------------------------------------------------------------------------
709 void TCollection_AsciiString::InsertBefore(const Standard_Integer Index,
710 const TCollection_AsciiString& what)
712 if (Index < 1 || Index > mylength) Standard_OutOfRange::Raise();
716 // ----------------------------------------------------------------------------
718 // ----------------------------------------------------------------------------
719 Standard_Boolean TCollection_AsciiString::IsEqual
720 (const Standard_CString other)const
726 // STRINGLEN( other , otherlength );
728 STRLEN(other,otherlength);
729 if(mylength != otherlength) return Standard_False;
732 Standard_Boolean KEqual ;
733 // CSTRINGEQUAL( mystring , other , mylength , KEqual ) ;
735 LCSTRINGEQUAL( mystring , mylength , other , KEqual ) ;
739 STRINGLEN( other , otherlength );
740 if ( mylength != otherlength )
741 return Standard_False ;
742 return Standard_True;
744 for (int i = 0 ; i < mylength ; i++)
745 if (mystring[i] != other[i]) return Standard_False;
746 return Standard_True;
749 Standard_NullObject::Raise("TCollection_AsciiString::Operator == "
750 "Parameter 'other'");
751 return Standard_False;
754 // ----------------------------------------------------------------------------
756 // ----------------------------------------------------------------------------
757 Standard_Boolean TCollection_AsciiString::IsEqual
758 (const TCollection_AsciiString& other)const
761 if (mylength != other.mylength) return Standard_False;
763 Standard_Boolean KEqual ;
764 ASCIISTRINGEQUAL( mystring , other.mystring , mylength , KEqual ) ;
767 Standard_CString sother = other.mystring;
768 for (int i = 0 ; i < mylength ; i++)
769 if (mystring[i] != sother[i]) return Standard_False;
770 return Standard_True;
774 // ----------------------------------------------------------------------------
776 // ----------------------------------------------------------------------------
777 Standard_Boolean TCollection_AsciiString::IsEmpty() const
779 return (mylength == 0);
782 // ----------------------------------------------------------------------------
784 // ----------------------------------------------------------------------------
785 Standard_Boolean TCollection_AsciiString::IsDifferent
786 (const Standard_CString other)const
790 // STRINGLEN( other , otherlength );
792 STRLEN(other,otherlength);
793 if(mylength != otherlength) return Standard_True;
796 Standard_Boolean KEqual ;
797 // CSTRINGEQUAL( mystring , other , mylength , KEqual ) ;
799 LCSTRINGEQUAL( mystring , mylength , other , KEqual ) ;
802 // STRINGLEN( other , otherlength );
803 return Standard_True;
808 for (int i = 0 ; i < mylength ; i++)
809 if (mystring[i] != other[i]) return Standard_True;
810 return Standard_False;
811 if ( mylength != otherlength )
812 return Standard_False ;
813 return Standard_True;
816 Standard_NullObject::Raise("TCollection_AsciiString::Operator != "
817 "Parameter 'other'");
818 return Standard_False;
821 // ----------------------------------------------------------------------------
823 // ----------------------------------------------------------------------------
824 Standard_Boolean TCollection_AsciiString::IsDifferent
825 (const TCollection_AsciiString& other)const
828 if (mylength != other.mylength) return Standard_True;
830 Standard_Boolean KEqual ;
831 ASCIISTRINGEQUAL( mystring , other.mystring , mylength , KEqual ) ;
834 Standard_CString sother = other.mystring;
835 for (int i = 0 ; i < mylength ; i++)
836 if (mystring[i] != sother[i]) return Standard_True;
837 return Standard_False;
841 // ----------------------------------------------------------------------------
843 // ----------------------------------------------------------------------------
844 Standard_Boolean TCollection_AsciiString::IsLess
845 (const Standard_CString other)const
850 Standard_Boolean KLess ;
852 // STRINGLEN( other , otherlength );
853 // CSTRINGLESS( mystring , mylength , other , otherlength ,
854 // INF( mylength , otherlength ) , KLess ) ;
855 LCSTRINGLESS( mystring , mylength , other , KLess ) ;
858 Standard_Integer otherlength;
859 Standard_Integer i = 0, j = 0;
860 STRLEN(other,otherlength);
861 while ( i < mylength && j < otherlength) {
862 if (mystring[i] < other[j]) return Standard_True;
863 if (mystring[i] > other[j]) return Standard_False;
867 if (i == mylength && j < otherlength) return Standard_True;
868 return Standard_False;
871 Standard_NullObject::Raise("TCollection_AsciiString::Operator < "
872 "Parameter 'other'");
873 return Standard_False;
876 // ----------------------------------------------------------------------------
878 // ----------------------------------------------------------------------------
879 Standard_Boolean TCollection_AsciiString::IsLess
880 (const TCollection_AsciiString& other)const
884 Standard_Boolean KLess ;
885 ASCIISTRINGLESS( mystring , mylength , other.mystring , other.mylength ,
886 INF( mylength , other.mylength ) , KLess ) ;
890 Standard_Integer i = 0, j = 0;
891 Standard_Integer otherlength = other.mylength;
892 Standard_CString sother = other.mystring;
893 while ( i < mylength && j < otherlength) {
894 if (mystring[i] < sother[j]) return Standard_True;
895 if (mystring[i] > sother[j]) return Standard_False;
899 if (i == mylength && j < otherlength) return Standard_True;
900 return Standard_False;
904 // ----------------------------------------------------------------------------
906 // ----------------------------------------------------------------------------
907 Standard_Boolean TCollection_AsciiString::IsGreater
908 (const Standard_CString other)const
913 Standard_Boolean KGreater ;
915 // STRINGLEN( other , otherlength );
916 // CSTRINGGREATER( mystring , mylength , other , otherlength ,
917 // INF( mylength , otherlength ) , KGreater ) ;
918 LCSTRINGGREATER( mystring , mylength , other , KGreater ) ;
921 Standard_Integer otherlength;
922 Standard_Integer i = 0, j = 0;
923 STRLEN(other,otherlength);
924 while (i < mylength && j <= otherlength) {
925 if (mystring[i] < other[j]) return Standard_False;
926 if (mystring[i] > other[j]) return Standard_True;
930 if (j == otherlength && i < mylength) return Standard_True;
931 return Standard_False;
934 Standard_NullObject::Raise("TCollection_AsciiString::Operator > "
935 "Parameter 'other'");
936 return Standard_False;
939 // ----------------------------------------------------------------------------
941 // ----------------------------------------------------------------------------
942 Standard_Boolean TCollection_AsciiString::IsGreater
943 (const TCollection_AsciiString& other)const
947 Standard_Boolean KGreater ;
949 ASCIISTRINGGREATER( mystring , mylength , other.mystring , other.mylength,
950 INF( mylength , other.mylength ) , KGreater ) ;
953 Standard_Integer i = 0, j = 0;
954 Standard_Integer otherlength = other.mylength;
955 Standard_CString sother = other.mystring;
956 while (i < mylength && j <= otherlength) {
957 if (mystring[i] < sother[j]) return Standard_False;
958 if (mystring[i] > sother[j]) return Standard_True;
962 if (j == otherlength && i < mylength) return Standard_True;
963 return Standard_False;
967 // ----------------------------------------------------------------------------
969 // ----------------------------------------------------------------------------
970 Standard_Integer TCollection_AsciiString::IntegerValue()const
973 Standard_Integer value = 0;
975 value = (int)strtol(mystring,&ptr,10);
976 if (ptr != mystring) return value;
978 Standard_NumericError::Raise("TCollection_AsciiString::IntegerValue");
982 // ----------------------------------------------------------------------------
984 // ----------------------------------------------------------------------------
985 Standard_Boolean TCollection_AsciiString::IsIntegerValue()const
990 // Standard_Integer value = (int)strtol(mystring,&ptr,10);
992 strtol(mystring,&ptr,10);
994 if (ptr != mystring) {
995 for (int i=0; i < mylength; i++) {
996 if (mystring[i] == '.') return Standard_False;
998 return Standard_True;
1001 return Standard_False;
1004 // ----------------------------------------------------------------------------
1006 // ----------------------------------------------------------------------------
1007 Standard_Boolean TCollection_AsciiString::IsRealValue()const
1012 // Standard_Real value = strtod(mystring,&ptr);
1014 strtod(mystring,&ptr);
1016 if (ptr != mystring) return Standard_True;
1017 else return Standard_False;
1019 return Standard_False;
1022 // ----------------------------------------------------------------------------
1024 // ----------------------------------------------------------------------------
1025 Standard_Boolean TCollection_AsciiString::IsAscii()const
1027 // LD : Debuggee le 26/11/98
1028 // Cette fonction retournait TOUJOURS Standard_True !
1029 for (int i=0; i < mylength; i++)
1030 if (mystring[i] >= 127 || mystring[i] < ' ') return Standard_False;
1031 return Standard_True;
1034 //------------------------------------------------------------------------
1036 //------------------------------------------------------------------------
1037 void TCollection_AsciiString::LeftAdjust ()
1039 Standard_Integer i ;
1040 for( i = 0 ; i < mylength ; i ++) if(!IsSpace(mystring[i])) break;
1041 if( i > 0 ) Remove(1,i);
1044 //------------------------------------------------------------------------
1046 //------------------------------------------------------------------------
1047 void TCollection_AsciiString::LeftJustify(const Standard_Integer Width,
1048 const Standard_Character Filler)
1050 if (Width > mylength) {
1052 // Standard_Integer length = *(int*)((long)mystring-8);
1053 // if(Width >> 4 > length-1 >> 4)
1054 mystring = Reallocate((void*&)mystring,
1058 mystring = Allocate(Width+1);
1060 for (int i = mylength; i < Width ; i++) mystring[i] = Filler;
1062 mystring[mylength] = '\0';
1064 else if (Width < 0) {
1065 Standard_NegativeValue::Raise();
1069 //------------------------------------------------------------------------
1071 //------------------------------------------------------------------------
1072 Standard_Integer TCollection_AsciiString::Location
1073 (const Standard_Integer N ,
1074 const Standard_Character C ,
1075 const Standard_Integer FromIndex,
1076 const Standard_Integer ToIndex ) const
1078 if (FromIndex > 0 && ToIndex <= mylength && FromIndex <= ToIndex ) {
1079 for(int i = FromIndex-1, count = 0; i <= ToIndex-1; i++) {
1080 if(mystring[i] == C) {
1082 if ( count == N ) return i+1;
1087 Standard_OutOfRange::Raise();
1091 //------------------------------------------------------------------------
1093 //------------------------------------------------------------------------
1094 Standard_Integer TCollection_AsciiString::Location
1095 (const TCollection_AsciiString& what,
1096 const Standard_Integer FromIndex,
1097 const Standard_Integer ToIndex) const
1099 if (mylength == 0 || what.mylength == 0) return 0;
1100 if (ToIndex <= mylength && FromIndex > 0 && FromIndex <= ToIndex ) {
1101 Standard_Integer i = FromIndex-1;
1102 Standard_Integer k = 1;
1103 Standard_Integer l = FromIndex-2;
1104 Standard_Boolean Find = Standard_False;
1105 while (!Find && i < ToIndex) {
1106 if (mystring[i] == what.Value(k)) {
1108 if ( k > what.mylength) Find = Standard_True;
1111 if (k > 1) i--; // si on est en cours de recherche
1117 if (Find) return l+2;
1120 Standard_OutOfRange::Raise();
1124 // ----------------------------------------------------------------------------
1126 // ----------------------------------------------------------------------------
1127 void TCollection_AsciiString::LowerCase()
1129 for (int i=0; i < mylength; i++)
1130 mystring[i] = tolower(mystring[i]);
1133 //------------------------------------------------------------------------
1135 //------------------------------------------------------------------------
1136 void TCollection_AsciiString::Prepend(const TCollection_AsciiString& what)
1141 // ----------------------------------------------------------------------------
1143 // ----------------------------------------------------------------------------
1144 Standard_Real TCollection_AsciiString::RealValue()const
1147 Standard_Real value = 0;
1149 value = strtod(mystring,&ptr);
1150 if (ptr != mystring) return value;
1152 Standard_NumericError::Raise("TCollection_AsciiString::RealValue");
1156 // ----------------------------------------------------------------------------
1158 //--------------------------------------------------------------------------
1159 void TCollection_AsciiString::Read(Standard_IStream& astream)
1165 Standard_Character buffer[8190]; } CHN ;
1166 astream >> CHN.buffer; // Get characters from astream
1169 STRINGLEN( CHN.buffer , newlength );
1171 STRLEN(CHN.buffer,newlength);
1174 // Standard_Integer length = *(int*)((long)mystring-8);
1175 // if(newlength >> 4 > length-1 >> 4)
1176 mystring = Reallocate((void*&)mystring,
1177 ROUNDMEM(newlength+1));
1180 mystring =Allocate(ROUNDMEM(newlength+1));
1183 ASCIISTRINGCOPY( mystring , CHN.buffer , newlength ) ;
1184 mylength = newlength;
1186 STRCPY(mystring,CHN.buffer,newlength);
1187 mylength = newlength;
1188 mystring[mylength] = '\0';
1193 //---------------------------------------------------------------------------
1194 Standard_IStream& operator >> (Standard_IStream& astream,
1195 TCollection_AsciiString& astring)
1197 astring.Read(astream);
1202 // ----------------------------------------------------------------------------
1204 // ----------------------------------------------------------------------------
1205 void TCollection_AsciiString::Print(Standard_OStream& astream)const
1207 if(mystring) astream << mystring;
1211 // ----------------------------------------------------------------------------
1212 Standard_OStream& operator << (Standard_OStream& astream,
1213 const TCollection_AsciiString& astring)
1215 astring.Print(astream);
1219 // ----------------------------------------------------------------------------
1221 // ----------------------------------------------------------------------------
1222 void TCollection_AsciiString::RemoveAll(const Standard_Character what,
1223 const Standard_Boolean CaseSensitive)
1225 if (mylength == 0) return;
1227 if (CaseSensitive) {
1228 for (int i=0; i < mylength; i++)
1229 if (mystring[i] != what) mystring[c++] = mystring[i];
1232 Standard_Character upperwhat = toupper(what);
1233 for (int i=0; i < mylength; i++) {
1234 if (toupper(mystring[i]) != upperwhat) mystring[c++] = mystring[i];
1238 mystring[mylength] = '\0';
1241 // ----------------------------------------------------------------------------
1243 // ----------------------------------------------------------------------------
1244 void TCollection_AsciiString::RemoveAll(const Standard_Character what)
1246 if (mylength == 0) return;
1248 for (int i=0; i < mylength; i++)
1249 if (mystring[i] != what) mystring[c++] = mystring[i];
1251 mystring[mylength] = '\0';
1254 // ----------------------------------------------------------------------------
1256 // ----------------------------------------------------------------------------
1257 void TCollection_AsciiString::Remove (const Standard_Integer where,
1258 const Standard_Integer ahowmany)
1260 if (where+ahowmany <= mylength+1) {
1262 for(i = where+ahowmany-1, j = where-1; i < mylength; i++, j++)
1263 mystring[j] = mystring[i];
1264 mylength -= ahowmany;
1265 mystring[mylength] = '\0';
1268 Standard_OutOfRange::Raise("TCollection_AsciiString::Remove: "
1269 "Too many characters to erase or invalid "
1274 //------------------------------------------------------------------------
1276 //------------------------------------------------------------------------
1277 void TCollection_AsciiString::RightAdjust ()
1279 Standard_Integer i ;
1280 for ( i = mylength-1 ; i >= 0 ; i--)
1281 if(!IsSpace(mystring[i]))
1283 if( i < mylength-1 )
1284 Remove(i+2,mylength-(i+2)+1);
1287 //------------------------------------------------------------------------
1289 //------------------------------------------------------------------------
1290 void TCollection_AsciiString::RightJustify(const Standard_Integer Width,
1291 const Standard_Character Filler)
1293 Standard_Integer i ;
1294 Standard_Integer k ;
1295 if (Width > mylength) {
1297 // Standard_Integer length = *(int*)((long)mystring-8);
1298 // if(Width >> 4 > length-1 >> 4)
1299 mystring = Reallocate((void*&)mystring,
1303 mystring =Allocate(Width+1);
1305 for ( i = mylength-1, k = Width-1 ; i >= 0 ; i--, k--)
1306 mystring[k] = mystring[i];
1307 for(; k >= 0 ; k--) mystring[k] = Filler;
1309 mystring[mylength] = '\0';
1311 else if (Width < 0) {
1312 Standard_NegativeValue::Raise();
1316 // ----------------------------------------------------------------------------
1318 // ----------------------------------------------------------------------------
1319 Standard_Integer TCollection_AsciiString::Search
1320 (const Standard_CString what)const
1322 Standard_Integer size;
1324 STRINGLEN( what , size );
1331 Standard_Boolean find = Standard_False;
1332 while ( i < mylength-size+1 && !find) {
1335 while (j < size && mystring[k++] == what[j++])
1336 if (j == size) find = Standard_True;
1344 // ----------------------------------------------------------------------------
1346 // ----------------------------------------------------------------------------
1347 Standard_Integer TCollection_AsciiString::Search
1348 (const TCollection_AsciiString& what) const
1350 Standard_Integer size = what.mylength;
1351 Standard_CString swhat = what.mystring;
1355 Standard_Boolean find = Standard_False;
1356 while ( i < mylength-size+1 && !find) {
1359 while (j < size && mystring[k++] == swhat[j++])
1360 if (j == size) find = Standard_True;
1368 // ----------------------------------------------------------------------------
1370 // ----------------------------------------------------------------------------
1371 Standard_Integer TCollection_AsciiString::SearchFromEnd
1372 (const Standard_CString what)const
1376 STRINGLEN( what , size );
1383 Standard_Boolean find = Standard_False;
1384 while ( i >= size-1 && !find) {
1387 while (j >= 0 && mystring[k--] == what[j--])
1388 if (j == -1) find = Standard_True;
1390 if (find) return i-size+3;
1396 // ----------------------------------------------------------------------------
1398 // ----------------------------------------------------------------------------
1399 Standard_Integer TCollection_AsciiString::SearchFromEnd
1400 (const TCollection_AsciiString& what)const
1402 int size = what.mylength;
1404 Standard_CString swhat = what.mystring;
1407 Standard_Boolean find = Standard_False;
1408 while ( i >= size-1 && !find) {
1411 while (j >= 0 && mystring[k--] == swhat[j--])
1412 if (j == -1) find = Standard_True;
1414 if (find) return i-size+3;
1419 // ----------------------------------------------------------------------------
1421 // ----------------------------------------------------------------------------
1422 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1423 const Standard_Character what)
1425 if (where > 0 && where <= mylength) {
1426 mystring[where-1] = what;
1429 Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1434 // ----------------------------------------------------------------------------
1436 // ----------------------------------------------------------------------------
1437 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1438 const Standard_CString what)
1440 if (where > 0 && where <= mylength+1) {
1441 Standard_Integer size;
1443 STRINGLEN( what , size );
1447 size += (where - 1);
1448 if (size >= mylength){
1450 // Standard_Integer length = *(int*)((long)mystring-8);
1451 // if(size >> 4 > length-1 >> 4)
1452 mystring = Reallocate((void*&)mystring,
1456 mystring =Allocate(size+1);
1460 for (int i = where-1; i < size; i++)
1461 mystring[i] = what[i-(where-1)];
1462 mystring[mylength] = '\0';
1465 Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1470 // ----------------------------------------------------------------------------
1472 // ----------------------------------------------------------------------------
1473 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1474 const TCollection_AsciiString& what)
1476 if (where > 0 && where <= mylength+1) {
1477 Standard_Integer size = what.mylength;
1478 Standard_CString swhat = what.mystring;
1479 size += (where - 1);
1480 if (size >= mylength){
1482 // Standard_Integer length = *(int*)((long)mystring-8);
1483 // if(size >> 4 > length-1 >> 4)
1484 mystring = Reallocate((void*&)mystring,
1488 mystring =Allocate(size+1);
1492 for (int i = where-1; i < size; i++)
1493 mystring[i] = swhat[i-(where-1)];
1494 mystring[mylength] = '\0';
1497 Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1502 // ----------------------------------------------------------------------------
1505 // ----------------------------------------------------------------------------
1506 void TCollection_AsciiString::Split(const Standard_Integer where,
1507 TCollection_AsciiString& res)
1509 if (where >= 0 && where <= mylength) {
1510 res = &mystring[where] ;
1514 Standard_OutOfRange::Raise("TCollection_AsciiString::Split index");
1518 // ----------------------------------------------------------------------------
1520 // ----------------------------------------------------------------------------
1521 TCollection_AsciiString TCollection_AsciiString::Split
1522 (const Standard_Integer where)
1524 if (where >= 0 && where <= mylength) {
1526 TCollection_AsciiString res( &mystring[where] , mylength - where );
1528 TCollection_AsciiString res(&mystring[where]);
1533 Standard_OutOfRange::Raise("TCollection_AsciiString::Split index");
1534 TCollection_AsciiString res;
1538 // ----------------------------------------------------------------------------
1541 // ----------------------------------------------------------------------------
1542 void TCollection_AsciiString::SubString(const Standard_Integer FromIndex,
1543 const Standard_Integer ToIndex,
1544 TCollection_AsciiString& res) const
1547 if (ToIndex > mylength || FromIndex <= 0 || FromIndex > ToIndex )
1548 Standard_OutOfRange::Raise();
1549 Standard_Integer newlength = ToIndex-FromIndex+1;
1550 res.mystring =Allocate(ROUNDMEM(newlength+1));
1553 CSTRINGCOPY( res.mystring , &(mystring [ FromIndex - 1 ]) , newlength );
1555 memcpy(res.mystring, &(mystring[FromIndex-1]),newlength);
1557 res.mystring[newlength] = '\0';
1558 res.mylength = newlength;
1563 // ----------------------------------------------------------------------------
1565 // ----------------------------------------------------------------------------
1566 TCollection_AsciiString TCollection_AsciiString::SubString
1567 (const Standard_Integer FromIndex,
1568 const Standard_Integer ToIndex) const
1571 if (ToIndex > mylength || FromIndex <= 0 || FromIndex > ToIndex )
1572 Standard_OutOfRange::Raise();
1575 return TCollection_AsciiString( &mystring [ FromIndex - 1 ] ,
1576 ToIndex - FromIndex + 1 ) ;
1579 TCollection_AsciiString res;
1580 Standard_Integer newlength = ToIndex-FromIndex+1;
1581 res.mystring = Allocate(ROUNDMEM(newlength+1));
1583 CSTRINGCOPY( res.mystring , &(mystring [ FromIndex - 1 ]) , newlength );
1585 memcpy(res.mystring, &(mystring[FromIndex-1]),newlength);
1587 res.mystring[newlength] = '\0';
1588 res.mylength = newlength;
1594 // ----------------------------------------------------------------------------
1597 // ----------------------------------------------------------------------------
1598 void TCollection_AsciiString::Token(const Standard_CString separators,
1599 const Standard_Integer whichone,
1600 TCollection_AsciiString& res)const
1604 res = Token( separators , whichone ) ;
1608 Standard_NullObject::Raise("TCollection_AsciiString::Token : "
1609 "parameter 'separators'");
1612 Standard_CString buftmp =Allocate(mylength+1);
1613 Standard_Character aSep;
1615 Standard_Boolean isSepFound = Standard_False, otherSepFound;
1619 for (i = 0; i < whichone && j < mylength; i++) {
1620 isSepFound = Standard_False;
1624 // Avant de commencer il faut virer les saloperies devant
1626 otherSepFound = Standard_True;
1627 while (j < mylength && otherSepFound) {
1629 otherSepFound = Standard_False;
1630 aSep = separators[l];
1632 if (aSep == mystring[j]) {
1634 otherSepFound = Standard_True;
1637 aSep = separators[l++];
1640 if (otherSepFound) j++;
1643 while (!isSepFound && k < mylength && j<mylength ) {
1645 aSep = separators[l];
1647 while (aSep != 0 && !isSepFound) {
1648 if (aSep == mystring[j]) {
1650 isSepFound = Standard_True;
1653 buftmp[k] = mystring[j];
1656 aSep = separators[l];
1659 if(j==mylength) buftmp[k] = 0;
1665 Standard::Free((void*&)buftmp);
1668 res.mystring = buftmp;
1670 STRINGLEN( buftmp , res.mylength );
1672 STRLEN(buftmp,res.mylength);
1679 // ----------------------------------------------------------------------------
1681 // ----------------------------------------------------------------------------
1682 TCollection_AsciiString TCollection_AsciiString::Token
1683 (const Standard_CString separators,
1684 const Standard_Integer whichone) const
1687 Standard_NullObject::Raise("TCollection_AsciiString::Token : "
1688 "parameter 'separators'");
1691 Standard_Integer theOne ;
1692 Standard_Integer StringIndex = 0 ;
1693 Standard_Integer SeparatorIndex ;
1694 Standard_Integer BeginIndex=0 ;
1695 Standard_Integer EndIndex=0 ;
1697 // cout << "'" << mystring << "'" << endl ;
1698 for ( theOne = 0 ; theOne < whichone ; theOne++ ) {
1701 // cout << "theOne " << theOne << endl ;
1702 if ( StringIndex == mylength )
1704 for (; StringIndex < mylength && EndIndex == 0 ; StringIndex++ ) {
1705 SeparatorIndex = 0 ;
1706 // cout << "StringIndex " << StringIndex << endl ;
1707 while ( separators [ SeparatorIndex ] ) {
1708 if ( mystring [ StringIndex ] == separators [ SeparatorIndex ] ) {
1711 SeparatorIndex += 1 ;
1713 if ( separators [ SeparatorIndex ] != '\0' ) { // We have a Separator
1714 if ( BeginIndex && EndIndex == 0 ) {
1715 EndIndex = StringIndex ;
1716 // cout << "EndIndex " << EndIndex << " '" << SubString( BeginIndex , EndIndex ).ToCString() << "'" << endl ;
1720 else if ( BeginIndex == 0 ) { // We have not a Separator
1721 BeginIndex = StringIndex + 1 ;
1722 // cout << "BeginIndex " << BeginIndex << endl ;
1725 // cout << "BeginIndex " << BeginIndex << " EndIndex " << EndIndex << endl ;
1727 if ( BeginIndex == 0 )
1728 return TCollection_AsciiString("",0) ;
1729 if ( EndIndex == 0 )
1730 EndIndex = mylength ;
1731 // cout << "'" << SubString( BeginIndex , EndIndex ).ToCString() << "'" << endl ;
1732 return TCollection_AsciiString( &mystring [ BeginIndex - 1 ] ,
1733 EndIndex - BeginIndex + 1 ) ;
1735 // Hereafter : Why write simple when we can write complicated ! ...
1737 TCollection_AsciiString res;
1740 Standard_CString buftmp = Allocate(mylength+1);
1741 Standard_Character aSep;
1743 Standard_Boolean isSepFound = Standard_False, otherSepFound;
1747 for (i = 0; i < whichone && j < mylength; i++) {
1748 isSepFound = Standard_False;
1752 // Avant de commencer il faut virer les saloperies devant
1754 otherSepFound = Standard_True;
1755 while (j < mylength && otherSepFound) {
1757 otherSepFound = Standard_False;
1758 aSep = separators[l];
1760 if (aSep == mystring[j]) {
1762 otherSepFound = Standard_True;
1765 aSep = separators[l++];
1768 if (otherSepFound) j++;
1771 while (!isSepFound && k < mylength && j<mylength ) {
1773 aSep = separators[l];
1775 while (aSep != 0 && !isSepFound) {
1776 if (aSep == mystring[j]) {
1778 isSepFound = Standard_True;
1781 buftmp[k] = mystring[j];
1784 aSep = separators[l];
1787 if(j==mylength) buftmp[k] = 0;
1793 Standard::Free((void*&)buftmp);
1796 res.mystring = buftmp;
1798 STRINGLEN( buftmp , res.mylength );
1800 STRLEN(buftmp,res.mylength);
1807 // ----------------------------------------------------------------------------
1809 // ----------------------------------------------------------------------------
1810 void TCollection_AsciiString::Trunc(const Standard_Integer ahowmany)
1812 if (ahowmany < 0 || ahowmany > mylength)
1813 Standard_OutOfRange::Raise("TCollection_AsciiString::Trunc : "
1814 "parameter 'ahowmany'");
1815 mylength = ahowmany;
1816 mystring[mylength] = '\0';
1819 // ----------------------------------------------------------------------------
1821 // ----------------------------------------------------------------------------
1822 void TCollection_AsciiString::UpperCase()
1824 for (int i=0; i < mylength; i++)
1825 mystring[i] = toupper(mystring[i]);
1828 //------------------------------------------------------------------------
1830 //------------------------------------------------------------------------
1831 Standard_Integer TCollection_AsciiString::UsefullLength () const
1833 Standard_Integer i ;
1834 for ( i = mylength -1 ; i >= 0 ; i--)
1835 if (IsGraphic(mystring[i])) break;
1839 // ----------------------------------------------------------------------------
1841 // ----------------------------------------------------------------------------
1842 Standard_Character TCollection_AsciiString::Value
1843 (const Standard_Integer where)const
1845 if (where > 0 && where <= mylength) {
1846 if(mystring) return mystring[where-1];
1849 Standard_OutOfRange::Raise("TCollection_AsciiString::Value : parameter where");
1854 //------------------------------------------------------------------------
1856 //------------------------------------------------------------------------
1857 Standard_Boolean TCollection_AsciiString::ISSIMILAR
1858 (const TCollection_AsciiString& string1,
1859 const TCollection_AsciiString& string2)
1861 if ( string1.Length() != string2.Length() )
1862 return Standard_False ;
1863 return ::ISSIMILAR (string1.ToCString() ,
1865 string2.ToCString() );