Integration of OCCT 6.5.0 from SVN
[occt.git] / src / TCollection / TCollection_AsciiString.cxx
1
2 //Copyright:      Matra Datavision 1993
3 //Created By      M. MERCIEN Fev,25 1993
4 //
5
6 #define OptJr 1
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)
12 #ifdef OptJr
13 #define ROUNDMEM(len) (((len)+3)&~0x3)
14 #else
15 #define ROUNDMEM(len) (len)
16 #endif
17
18 //#if defined(WNT) || defined(LIN)
19 #include <stdio.h>
20 //#endif
21
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>
30
31 #include <TCollection_ExtendedString.hxx>
32
33 // ###### PLACER LE TYPE NON DEFINI strtol (portabilite) ######
34 #ifdef WNT
35 # include <string.h>
36 #else
37 extern "C" {
38      long strtol(const char*, char**, int);
39 }
40 #endif  // WNT
41
42 // Shortcuts to standard allocate and reallocate functions
43 static inline Standard_PCharacter Allocate(const Standard_Size aLength)
44 {
45   return (Standard_PCharacter)Standard::Allocate (aLength);
46 }
47 static inline Standard_PCharacter Reallocate (Standard_Address aAddr,
48                                               const Standard_Size aLength)
49 {
50   return (Standard_PCharacter)Standard::Reallocate (aAddr, aLength);
51 }
52
53 // ----------------------------------------------------------------------------
54 // Create an empty AsciiString
55 // ----------------------------------------------------------------------------
56 TCollection_AsciiString::TCollection_AsciiString()
57 {
58   mylength = 0;
59   
60   mystring = Allocate(mylength+1);
61   mystring[mylength] = '\0';
62 }
63
64
65 // ----------------------------------------------------------------------------
66 // Create an asciistring from a Standard_CString
67 // ----------------------------------------------------------------------------
68 TCollection_AsciiString::TCollection_AsciiString(const Standard_CString astring)
69      : mystring(0)
70 {
71   if (astring) {
72 #if OptJr
73     STRINGLEN( astring , mylength ) ;
74 #else
75     STRLEN(astring,mylength);
76 #endif
77     mystring = Allocate(ROUNDMEM(mylength+1));
78 #if OptJr
79     CSTRINGCOPY( mystring , astring , mylength ) ;
80 #else
81     STRCPY(mystring,astring,mylength);
82     mystring[mylength] = '\0';
83 #endif
84   }
85   else {
86     Standard_NullObject::Raise("TCollection_AsciiString : parameter 'astring'");
87   }
88 }
89
90 // ----------------------------------------------------------------------------
91 // Create an asciistring from a Standard_CString
92 // ----------------------------------------------------------------------------
93 TCollection_AsciiString::TCollection_AsciiString(const Standard_CString astring,
94                                                  const Standard_Integer aLen )
95      : mystring(0)
96 {
97   if (astring) {
98     mylength = aLen ;
99     mystring = Allocate(ROUNDMEM(mylength+1));
100     CSTRINGCOPY( mystring , astring , mylength ) ;
101     mystring [ mylength ] = '\0' ;
102   }
103   else {
104     Standard_NullObject::Raise("TCollection_AsciiString : parameter 'astring'");
105   }
106 }
107
108 // ----------------------------------------------------------------------------
109 // Create an asciistring from a Standard_Character
110 // ----------------------------------------------------------------------------
111 TCollection_AsciiString::TCollection_AsciiString(const Standard_Character aChar)
112      : mystring(0)
113 {
114   if ( aChar != '\0' ) {
115     mylength    = 1;
116     mystring    = Allocate(2);
117     mystring[0] = aChar;
118     mystring[1] = '\0';
119   }
120   else {
121     mylength = 0;
122     mystring = Allocate(mylength+1);
123     mystring[mylength] = '\0';
124   }
125 }
126
127 // ----------------------------------------------------------------------------
128 // Create an AsciiString from a filler
129 // ----------------------------------------------------------------------------
130 TCollection_AsciiString::TCollection_AsciiString(const Standard_Integer length,
131                                                  const Standard_Character filler )
132 {
133   mystring = Allocate(length+1);
134   mylength = length;
135   for (int i = 0 ; i < length ; i++) mystring[i] = filler;
136   mystring[length] = '\0';
137 }
138
139 // ----------------------------------------------------------------------------
140 // Create an AsciiString from an Integer
141 // ----------------------------------------------------------------------------
142 TCollection_AsciiString::TCollection_AsciiString(const Standard_Integer aValue)
143      : mystring(0)
144 {
145
146   union { int bid ;      // ?? to ensure alignment of t[] by double-word??
147           char t [13]; } CHN ; 
148   sprintf(&CHN.t[0],"%d",aValue);
149 #if OptJr
150   STRINGLEN( CHN.t , mylength ) ;
151 #else
152   STRLEN(CHN.t,mylength);
153 #endif
154   mystring = Allocate(ROUNDMEM(mylength+1));
155 #if OptJr
156   ASCIISTRINGCOPY( mystring , CHN.t , mylength ) ;
157 #else
158   STRCPY(mystring,CHN.t,mylength);
159   mystring[mylength] = '\0';
160 #endif
161 }
162
163 // ----------------------------------------------------------------------------
164 // Create an asciistring from a real
165 // ----------------------------------------------------------------------------
166 TCollection_AsciiString::TCollection_AsciiString(const Standard_Real aValue)
167      : mystring(0)
168 {
169
170   union { int bid ;
171           char t [50]; } CHN ;
172   sprintf(&CHN.t[0],"%g",aValue);
173 #if OptJr
174   STRINGLEN( CHN.t , mylength ) ;
175 #else
176   STRLEN(CHN.t,mylength);
177 #endif
178   mystring = Allocate(ROUNDMEM(mylength+1));
179 #if OptJr
180   ASCIISTRINGCOPY( mystring , CHN.t , mylength ) ;
181 #else
182   STRCPY(mystring,CHN.t,mylength);
183   mystring[mylength] = '\0';
184 #endif
185 }
186
187 // ----------------------------------------------------------------------------
188 // Create an asciistring from an asciistring
189 // ----------------------------------------------------------------------------
190 TCollection_AsciiString::TCollection_AsciiString(const TCollection_AsciiString& astring)
191      : mystring(0)
192 {
193
194   mylength = astring.mylength;
195   mystring = Allocate(ROUNDMEM(mylength+1));
196   if ( astring.mystring ) {
197 #if OptJr
198     ASCIISTRINGCOPY( mystring , astring.mystring , mylength ) ;
199   }
200   else
201 #else
202     STRCPY(mystring,astring.mystring,mylength);
203   }
204 #endif
205   mystring[mylength] = '\0';
206 }
207
208 // ----------------------------------------------------------------------------
209 // Create an asciistring from a character
210 // ----------------------------------------------------------------------------
211 TCollection_AsciiString::TCollection_AsciiString(
212                          const TCollection_AsciiString& astring ,
213                          const Standard_Character other )
214      : mystring(0)
215 {
216
217   mylength = astring.mylength + 1 ;
218   mystring = Allocate(ROUNDMEM(mylength+1));  
219   if ( astring.mystring ) {
220     ASCIISTRINGCOPY( mystring , astring.mystring , astring.mylength ) ;
221   }
222   mystring[mylength-1] = other ;
223   mystring[mylength] = '\0' ;
224 }
225
226 // ----------------------------------------------------------------------------
227 // Create an asciistring from an asciistring
228 // ----------------------------------------------------------------------------
229 TCollection_AsciiString::TCollection_AsciiString(
230                          const TCollection_AsciiString& astring ,
231                          const Standard_CString other )
232      : mystring(0)
233 {
234  Standard_Integer otherlength;
235
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 ) ;
241   }
242   STRINGCAT( mystring , astring.mylength , other , otherlength ) ;
243 }
244
245 // ----------------------------------------------------------------------------
246 // Create an asciistring from an asciistring
247 // ----------------------------------------------------------------------------
248 TCollection_AsciiString::TCollection_AsciiString(
249                          const TCollection_AsciiString& astring ,
250                          const TCollection_AsciiString& other )
251      : mystring(0)
252 {
253
254   mylength = astring.mylength + other.mylength ;
255   mystring = Allocate(ROUNDMEM(mylength+1));
256   if ( astring.mystring ) {
257     ASCIISTRINGCOPY( mystring , astring.mystring , astring.mylength ) ;
258   }
259   if ( other.mystring ) {
260     STRINGCAT( mystring , astring.mylength , other.mystring , other.mylength ) ;
261   }
262   if ( mylength == 0 )
263     mystring[0] = '\0' ;
264
265 }
266
267 //---------------------------------------------------------------------------
268 //  Create an asciistring from an ExtendedString 
269 //---------------------------------------------------------------------------
270 TCollection_AsciiString::TCollection_AsciiString(const TCollection_ExtendedString& astring,
271                                                  const Standard_Character replaceNonAscii) 
272      : mystring(0)
273 {
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 );
280     }
281     mystring[mylength] = '\0';
282   }
283   else {
284     Standard_SStream amsg;
285     amsg << "It's not an ascii string : " ;
286     astring.Print(amsg);
287     Standard_OutOfRange::Raise(amsg);
288   }
289 }
290
291 // ----------------------------------------------------------------------------
292 // AssignCat
293 // ----------------------------------------------------------------------------
294 void TCollection_AsciiString::AssignCat(const Standard_Integer other)
295 {
296
297   AssignCat(TCollection_AsciiString(other));
298
299 }
300
301 // ----------------------------------------------------------------------------
302 // AssignCat
303 // ----------------------------------------------------------------------------
304 void TCollection_AsciiString::AssignCat(const Standard_Real other)
305 {
306
307   AssignCat(TCollection_AsciiString(other));
308
309 }
310
311 // ----------------------------------------------------------------------------
312 // AssignCat
313 // ----------------------------------------------------------------------------
314 void TCollection_AsciiString::AssignCat(const Standard_Character other)
315 {
316   if (other != '\0') {
317     if (mystring) {
318       mystring = Reallocate((void*&)mystring,
319                                                         mylength+2);
320     }
321     else {
322       mystring = Allocate(mylength+2);
323     }
324     mystring[mylength] = other ;
325     mylength += 1;
326     mystring[mylength] = '\0';
327   }
328 }
329
330 // ----------------------------------------------------------------------------
331 // AssignCat
332 // ----------------------------------------------------------------------------
333 void TCollection_AsciiString::AssignCat(const Standard_CString other)
334 {
335   if (other) {
336     if (other[0] != '\0') {
337       Standard_Integer otherlength;
338 #if OptJr
339       STRINGLEN( other , otherlength ) ;
340 #else
341       STRLEN(other,otherlength);
342 #endif
343       Standard_Integer newlength = mylength+otherlength;
344       if (mystring) {
345           mystring = Reallocate((void*&)mystring,
346                                                                ROUNDMEM(newlength+1));
347 #if OptJr
348         STRINGCAT( mystring , mylength , other , otherlength ) ;
349 #else
350         STRCAT(mystring,mylength,other,otherlength);
351 #endif
352       }
353       else {
354         mystring = Allocate(ROUNDMEM(newlength+1));
355 #if OptJr
356         CSTRINGCOPY( mystring , other , newlength ) ;
357 #else
358         STRCPY(mystring,other,newlength);
359 #endif
360       }
361       mylength = newlength;
362 #if !OptJr
363       mystring[mylength] = '\0';
364 #endif
365     }
366   }
367   else {
368     Standard_NullObject::Raise("TCollection_AsciiString::Operator += parameter other");
369   }
370 }
371
372 // ----------------------------------------------------------------------------
373 // AssignCat
374 // ----------------------------------------------------------------------------
375 void TCollection_AsciiString::AssignCat(const TCollection_AsciiString& other)
376 {
377
378   Standard_Integer otherlength = other.mylength; 
379   if (otherlength) {
380     Standard_CString sother = other.mystring;
381     Standard_Integer newlength = mylength+otherlength;
382     if (mystring) {
383         mystring = Reallocate((void*&)mystring,
384                                                            ROUNDMEM(newlength+1));
385 #if OptJr
386       STRINGCAT( mystring , mylength , sother , otherlength ) ;
387 #else
388       STRCAT(mystring,mylength,sother,otherlength);
389 #endif
390     }
391     else {
392       mystring =Allocate(ROUNDMEM(newlength+1));
393 #if OptJr
394       ASCIISTRINGCOPY( mystring , sother , newlength ) ;
395 #else
396       STRCPY(mystring,sother,newlength);
397 #endif
398     }
399     mylength = newlength;
400 #if !OptJr
401     mystring[mylength] = '\0';
402 #endif
403   }
404 }
405
406 // ---------------------------------------------------------------------------
407 // Capitalize
408 // ----------------------------------------------------------------------------
409 void TCollection_AsciiString::Capitalize()
410 {
411   for (int i=0; i < mylength; i++) {
412     if  ( i==0 ) mystring[i] = toupper(mystring[i]);
413     else         mystring[i] = tolower(mystring[i]);    
414   }
415 }
416
417 // ---------------------------------------------------------------------------
418 // Center
419 // ----------------------------------------------------------------------------
420 void TCollection_AsciiString::Center(const Standard_Integer Width ,
421                                      const Standard_Character Filler) 
422 {
423   if(Width > mylength) {
424     Standard_Integer newlength = mylength + ((Width - mylength)/2);
425     LeftJustify(newlength,Filler);
426     RightJustify(Width,Filler);
427   }
428   else if (Width < 0) {
429     Standard_NegativeValue::Raise();
430   }
431 }
432
433 // ----------------------------------------------------------------------------
434 // ChangeAll
435 // ----------------------------------------------------------------------------
436 void TCollection_AsciiString::ChangeAll(const Standard_Character aChar,
437                                         const Standard_Character NewChar,
438                                         const Standard_Boolean CaseSensitive)
439 {
440   if (CaseSensitive){
441     for (int i=0; i < mylength; i++)
442       if (mystring[i] == aChar) mystring[i] = NewChar;
443   }
444   else{
445     Standard_Character anUpperChar = toupper(aChar);
446     for (int i=0; i < mylength; i++)
447       if (toupper(mystring[i]) == anUpperChar) mystring[i] = NewChar;
448   }
449 }
450
451 // ----------------------------------------------------------------------------
452 // Clear
453 // ----------------------------------------------------------------------------
454 void TCollection_AsciiString::Clear()
455 {
456   if (mystring) Standard::Free((void*&)mystring);
457   mylength = 0;
458   mystring = Allocate(mylength+1);
459   mystring[mylength] = '\0';
460 }
461
462 // ----------------------------------------------------------------------------
463 // Copy
464 // ----------------------------------------------------------------------------
465 void TCollection_AsciiString::Copy(const Standard_CString fromwhere)
466 {
467
468   if (fromwhere) {
469     Standard_Integer newlength;
470 #if OptJr
471     STRINGLEN( fromwhere , newlength ) ;
472 #else
473     STRLEN(fromwhere,newlength);
474 #endif
475     if (mystring) {
476       
477       mystring = Reallocate((void*&)mystring,
478                                                         ROUNDMEM(newlength+1));
479      
480     }
481     else {
482       mystring = Allocate( ROUNDMEM(newlength + 1) );
483     }
484 #if OptJr
485     CSTRINGCOPY( mystring , fromwhere , newlength ) ;
486     mylength = newlength;
487 #else
488     STRCPY(mystring, fromwhere,newlength);
489     mystring[newlength] = '\0';
490     mylength = newlength;
491 #endif
492   }
493   else {
494     if (mystring) {
495       mylength = 0;
496       mystring[mylength] = '\0';
497     }
498   }
499 }
500
501 // ----------------------------------------------------------------------------
502 // Copy
503 // ----------------------------------------------------------------------------
504 void TCollection_AsciiString::Copy(const TCollection_AsciiString& fromwhere)
505 {
506
507   if (fromwhere.mystring) {
508     Standard_Integer newlength = fromwhere.mylength;
509     if (mystring) {
510       
511       mystring = Reallocate((void*&)mystring,
512                                                         ROUNDMEM(newlength+1));
513       
514     }
515     else {
516       mystring =Allocate(ROUNDMEM(newlength+1));
517     }
518 #if OptJr
519     ASCIISTRINGCOPY( mystring , fromwhere.mystring , newlength ) ;
520     mylength = newlength;
521 #else
522     STRCPY(mystring, fromwhere.mystring,newlength);
523     mylength = newlength;
524     mystring[mylength] = '\0';
525 #endif
526   }
527   else {
528     if (mystring) {
529       mylength = 0;
530       mystring[mylength] = '\0';
531     }
532   }
533
534 }
535
536 // ----------------------------------------------------------------------------
537 // Destroy
538 // ----------------------------------------------------------------------------
539 void TCollection_AsciiString::Destroy()
540 {
541   if (mystring) Standard::Free((void*&)mystring);
542   mystring = 0L;
543 }
544
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
552 {
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;
558     return 0;
559   }
560   Standard_OutOfRange::Raise();
561   return 0;
562 }
563
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
571 {
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;
580     }
581     return 0;
582   }
583   Standard_OutOfRange::Raise();
584   return 0;
585 }
586
587 //----------------------------------------------------------------------------
588 // Insert a character before 'where'th character
589 // ----------------------------------------------------------------------------
590 void TCollection_AsciiString::Insert(const Standard_Integer where,
591                                      const Standard_Character what)
592 {
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");
597   
598   if (mystring) {
599 //    Standard_Integer length = *(int*)((long)mystring-8);
600 //    if((mylength+1) >> 4 > length-1 >> 4)
601     mystring = Reallocate((void*&)mystring,
602                                                        mylength+2);
603   }
604   else {
605     mystring = Allocate(mylength+2);
606   }
607   if (where != mylength +1) {
608     for (int i=mylength-1; i >= where-1; i--)
609       mystring[i+1] = mystring[i];
610   }
611   mystring[where-1] = what;
612   mylength++;
613   mystring[mylength] = '\0';
614 }
615
616 // ----------------------------------------------------------------------------
617 // Insert
618 // ----------------------------------------------------------------------------
619 void TCollection_AsciiString::Insert(const Standard_Integer where,
620                                      const Standard_CString what)
621 {
622   if (where <= mylength + 1) {
623     if(what) {
624       Standard_Integer whatlength;
625 #if OptJr
626       STRINGLEN( what , whatlength );
627 #else
628       STRLEN(what,whatlength);
629 #endif
630       Standard_Integer newlength = mylength + whatlength;
631       
632       if (mystring) {
633 //      Standard_Integer length = *(int*)((long)mystring-8);
634 //      if(newlength >> 4 > length-1 >> 4)
635         mystring = Reallocate((void*&)mystring,
636                                                            newlength+1);
637       }
638       else {
639         mystring =Allocate(newlength+1);
640       }
641       if (where != mylength +1) {
642         for (int i=mylength-1; i >= where-1; i--)
643           mystring[i+whatlength] = mystring[i];
644       }
645       for (int i=0; i < whatlength; i++)
646         mystring[where-1+i] = what[i];
647       
648       mylength = newlength;
649       mystring[mylength] = '\0';
650     }
651   }
652   else {
653     Standard_OutOfRange::Raise("TCollection_AsciiString::Insert : "
654                                "Parameter where is too big");
655   }
656 }
657
658 // ----------------------------------------------------------------------------
659 // Insert
660 // ----------------------------------------------------------------------------
661 void TCollection_AsciiString::Insert(const Standard_Integer where,
662                                      const TCollection_AsciiString& what)
663 {
664   Standard_CString swhat = what.mystring;
665   if (where <= mylength + 1) {
666     Standard_Integer whatlength = what.mylength;
667     if(whatlength) {
668       Standard_Integer newlength = mylength + whatlength;
669       
670       if (mystring) {
671 //      Standard_Integer length = *(int*)((long)mystring-8);
672 //      if(newlength >> 4 > length-1 >> 4)
673         mystring = Reallocate((void*&)mystring,
674                                                            newlength+1);
675       }
676       else {
677         mystring =Allocate(newlength+1);
678       }
679       if (where != mylength +1) {
680         for (int i=mylength-1; i >= where-1; i--)
681           mystring[i+whatlength] = mystring[i];
682       }
683       for (int i=0; i < whatlength; i++)
684         mystring[where-1+i] = swhat[i];
685       
686       mylength = newlength;
687       mystring[mylength] = '\0';
688     }
689   }
690   else {
691     Standard_OutOfRange::Raise("TCollection_AsciiString::Insert : "
692                                "Parameter where is too big");
693   }
694 }
695
696 //------------------------------------------------------------------------
697 //  InsertAfter
698 //------------------------------------------------------------------------
699 void TCollection_AsciiString::InsertAfter(const Standard_Integer Index,
700                                           const TCollection_AsciiString& what)
701 {
702    if (Index < 0 || Index > mylength) Standard_OutOfRange::Raise();
703    Insert(Index+1,what);
704 }
705
706 //------------------------------------------------------------------------
707 //  InsertBefore
708 //------------------------------------------------------------------------
709 void TCollection_AsciiString::InsertBefore(const Standard_Integer Index,
710                                            const TCollection_AsciiString& what)
711 {
712    if (Index < 1 || Index > mylength) Standard_OutOfRange::Raise();
713    Insert(Index,what);
714 }
715
716 // ----------------------------------------------------------------------------
717 // IsEqual
718 // ----------------------------------------------------------------------------
719 Standard_Boolean TCollection_AsciiString::IsEqual
720                                         (const Standard_CString other)const
721 {
722   int otherlength;
723
724   if (other) {
725 #if OptJr
726 //    STRINGLEN( other , otherlength );
727 #else
728     STRLEN(other,otherlength);
729     if(mylength != otherlength) return Standard_False;
730 #endif
731 #if OptJr
732     Standard_Boolean KEqual ;
733 //    CSTRINGEQUAL( mystring , other , mylength , KEqual ) ;
734     if ( mystring ) {
735       LCSTRINGEQUAL( mystring , mylength , other , KEqual ) ;
736       return KEqual ;
737     }
738
739     STRINGLEN( other , otherlength );
740     if ( mylength != otherlength )
741       return Standard_False ;
742     return Standard_True;
743 #else
744     for (int i = 0 ; i < mylength ; i++)
745       if (mystring[i] != other[i]) return Standard_False;
746     return Standard_True;
747 #endif
748   }
749   Standard_NullObject::Raise("TCollection_AsciiString::Operator == "
750                              "Parameter 'other'");
751   return Standard_False;
752 }
753
754 // ----------------------------------------------------------------------------
755 // IsEqual
756 // ----------------------------------------------------------------------------
757 Standard_Boolean TCollection_AsciiString::IsEqual
758                                 (const TCollection_AsciiString& other)const
759 {
760
761   if (mylength != other.mylength) return Standard_False;
762 #if OptJr
763   Standard_Boolean KEqual ;
764   ASCIISTRINGEQUAL( mystring , other.mystring , mylength , KEqual ) ;
765   return KEqual ;
766 #else
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;
771 #endif
772 }
773
774 // ----------------------------------------------------------------------------
775 // IsEmpty
776 // ----------------------------------------------------------------------------
777 Standard_Boolean TCollection_AsciiString::IsEmpty() const
778 {
779   return (mylength == 0);
780 }
781
782 // ----------------------------------------------------------------------------
783 // IsDifferent
784 // ----------------------------------------------------------------------------
785 Standard_Boolean TCollection_AsciiString::IsDifferent
786                                         (const Standard_CString other)const
787 {
788  if (other) {
789 #if OptJr
790 //   STRINGLEN( other , otherlength );
791 #else
792    STRLEN(other,otherlength);
793    if(mylength != otherlength) return Standard_True;
794 #endif
795 #if OptJr
796    Standard_Boolean KEqual ;
797 //   CSTRINGEQUAL( mystring , other , mylength , KEqual ) ;
798    if ( mystring ) {
799      LCSTRINGEQUAL( mystring , mylength , other , KEqual ) ;
800      return !KEqual ;
801    }
802 //   STRINGLEN( other , otherlength );
803    return Standard_True;
804
805 #else
806  int otherlength;
807
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;
814 #endif
815  }
816  Standard_NullObject::Raise("TCollection_AsciiString::Operator != "
817                             "Parameter 'other'");
818  return Standard_False;
819 }
820
821 // ----------------------------------------------------------------------------
822 // IsDifferent
823 // ----------------------------------------------------------------------------
824 Standard_Boolean TCollection_AsciiString::IsDifferent
825                                 (const TCollection_AsciiString& other)const
826 {
827
828   if (mylength != other.mylength) return Standard_True;
829 #if OptJr
830   Standard_Boolean KEqual ;
831   ASCIISTRINGEQUAL( mystring , other.mystring , mylength , KEqual ) ;
832   return !KEqual ;
833 #else
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;
838 #endif
839 }
840
841 // ----------------------------------------------------------------------------
842 // IsLess
843 // ----------------------------------------------------------------------------
844 Standard_Boolean TCollection_AsciiString::IsLess
845                                         (const Standard_CString other)const
846 {
847   if (other) {
848 #if OptJr
849     
850     Standard_Boolean KLess ;
851
852 //    STRINGLEN( other , otherlength );
853 //    CSTRINGLESS( mystring , mylength , other , otherlength ,
854 //                 INF( mylength , otherlength ) , KLess ) ;
855     LCSTRINGLESS( mystring , mylength , other , KLess ) ;
856     return KLess ;
857 #else
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;
864       i++ ; 
865       j++;
866     }
867     if (i == mylength && j < otherlength) return Standard_True;
868     return Standard_False;
869 #endif
870   }
871   Standard_NullObject::Raise("TCollection_AsciiString::Operator < "
872                              "Parameter 'other'");
873   return Standard_False;
874 }
875
876 // ----------------------------------------------------------------------------
877 // IsLess
878 // ----------------------------------------------------------------------------
879 Standard_Boolean TCollection_AsciiString::IsLess
880                                 (const TCollection_AsciiString& other)const
881 {
882 #if OptJr
883   
884   Standard_Boolean KLess ;
885   ASCIISTRINGLESS( mystring , mylength , other.mystring , other.mylength ,
886                    INF( mylength , other.mylength ) , KLess ) ;
887   return KLess ;
888
889 #else
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;
896     i++ ; 
897     j++;
898   }
899   if (i == mylength && j < otherlength) return Standard_True;
900   return Standard_False;
901 #endif
902 }
903
904 // ----------------------------------------------------------------------------
905 // IsGreater
906 // ----------------------------------------------------------------------------
907 Standard_Boolean TCollection_AsciiString::IsGreater
908                                         (const Standard_CString other)const
909 {
910   if (other) {
911 #if OptJr
912     
913     Standard_Boolean KGreater ;
914
915 //    STRINGLEN( other , otherlength );
916 //    CSTRINGGREATER( mystring , mylength , other , otherlength ,
917 //                    INF( mylength , otherlength ) , KGreater ) ;
918     LCSTRINGGREATER( mystring , mylength , other , KGreater ) ;
919     return KGreater ;
920 #else
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;
927       i++ ; 
928       j++;
929     }
930     if (j == otherlength && i < mylength) return Standard_True;
931     return Standard_False;
932 #endif
933   }
934   Standard_NullObject::Raise("TCollection_AsciiString::Operator > "
935                              "Parameter 'other'");
936   return Standard_False;
937 }
938
939 // ----------------------------------------------------------------------------
940 // IsGreater
941 // ----------------------------------------------------------------------------
942 Standard_Boolean TCollection_AsciiString::IsGreater
943                                 (const TCollection_AsciiString& other)const
944 {
945 #if OptJr
946     
947     Standard_Boolean KGreater ;
948
949     ASCIISTRINGGREATER( mystring , mylength , other.mystring , other.mylength,
950                         INF( mylength , other.mylength ) , KGreater ) ;
951     return KGreater ;
952 #else
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;
959     i++ ; 
960     j++;
961   }
962   if (j == otherlength && i < mylength) return Standard_True;
963   return Standard_False;
964 #endif
965 }
966
967 // ----------------------------------------------------------------------------
968 // IntegerValue
969 // ----------------------------------------------------------------------------
970 Standard_Integer TCollection_AsciiString::IntegerValue()const
971 {
972   char *ptr;
973   Standard_Integer value = 0; 
974   if(mystring) {
975     value = (int)strtol(mystring,&ptr,10); 
976     if (ptr != mystring) return value;
977   }
978   Standard_NumericError::Raise("TCollection_AsciiString::IntegerValue");
979   return value;
980 }
981
982 // ----------------------------------------------------------------------------
983 // IsIntegerValue
984 // ----------------------------------------------------------------------------
985 Standard_Boolean TCollection_AsciiString::IsIntegerValue()const
986 {
987   char *ptr;
988   if(mystring) {
989 //#ifdef DEB
990 //    Standard_Integer value = (int)strtol(mystring,&ptr,10); 
991 //#else
992     strtol(mystring,&ptr,10); 
993 //#endif
994     if (ptr != mystring) {
995       for (int i=0; i < mylength; i++) {
996         if (mystring[i] == '.') return Standard_False;
997       }
998       return Standard_True;
999     }
1000   }
1001   return Standard_False;
1002 }
1003
1004 // ----------------------------------------------------------------------------
1005 // IsRealValue
1006 // ----------------------------------------------------------------------------
1007 Standard_Boolean TCollection_AsciiString::IsRealValue()const
1008 {
1009   char *ptr;
1010   if(mystring) {
1011 //#ifdef DEB
1012 //    Standard_Real value = strtod(mystring,&ptr);
1013 //#else
1014     strtod(mystring,&ptr);
1015 //#endif
1016     if (ptr != mystring) return Standard_True;
1017     else                 return Standard_False; 
1018   }
1019   return Standard_False;
1020 }
1021
1022 // ----------------------------------------------------------------------------
1023 // IsAscii
1024 // ----------------------------------------------------------------------------
1025 Standard_Boolean TCollection_AsciiString::IsAscii()const
1026 {
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;
1032 }
1033
1034 //------------------------------------------------------------------------
1035 //  LeftAdjust
1036 //------------------------------------------------------------------------
1037 void TCollection_AsciiString::LeftAdjust ()
1038 {
1039    Standard_Integer i ;
1040    for( i = 0 ; i < mylength ; i ++) if(!IsSpace(mystring[i])) break;
1041    if( i > 0 ) Remove(1,i);
1042 }
1043
1044 //------------------------------------------------------------------------
1045 //  LeftJustify
1046 //------------------------------------------------------------------------
1047 void TCollection_AsciiString::LeftJustify(const Standard_Integer Width,
1048                                           const Standard_Character Filler)
1049 {
1050    if (Width > mylength) {
1051      if (mystring) {
1052 //       Standard_Integer length = *(int*)((long)mystring-8);
1053 //       if(Width >> 4 > length-1 >> 4)
1054        mystring = Reallocate((void*&)mystring,
1055                                                           Width+1);
1056      }
1057      else {
1058        mystring = Allocate(Width+1);
1059      }
1060      for (int i = mylength; i < Width ; i++) mystring[i] = Filler;
1061      mylength = Width;
1062      mystring[mylength] = '\0';
1063    }
1064    else if (Width < 0) {
1065      Standard_NegativeValue::Raise();
1066    }
1067 }
1068
1069 //------------------------------------------------------------------------
1070 //  Location
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
1077 {
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) {
1081          count++;
1082          if ( count == N ) return i+1;
1083        }
1084      }
1085      return 0 ;
1086    }
1087    Standard_OutOfRange::Raise();
1088    return 0 ;
1089 }
1090
1091 //------------------------------------------------------------------------
1092 //  Location
1093 //------------------------------------------------------------------------
1094 Standard_Integer TCollection_AsciiString::Location
1095                                 (const TCollection_AsciiString& what,
1096                                  const Standard_Integer         FromIndex,
1097                                  const Standard_Integer         ToIndex) const
1098 {
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)) {
1107         k++;
1108         if ( k > what.mylength) Find = Standard_True;
1109       }
1110       else {
1111         if (k > 1) i--;    // si on est en cours de recherche 
1112         k = 1;
1113         l = i;
1114       }
1115       i++;
1116     }
1117     if (Find) return l+2;
1118     else      return 0;
1119   }
1120   Standard_OutOfRange::Raise();
1121   return 0;
1122 }
1123
1124 // ----------------------------------------------------------------------------
1125 // LowerCase
1126 // ----------------------------------------------------------------------------
1127 void TCollection_AsciiString::LowerCase()
1128 {
1129   for (int i=0; i < mylength; i++)
1130     mystring[i] = tolower(mystring[i]);
1131 }
1132
1133 //------------------------------------------------------------------------
1134 //  Prepend
1135 //------------------------------------------------------------------------
1136 void TCollection_AsciiString::Prepend(const TCollection_AsciiString& what)
1137 {
1138   Insert(1,what);
1139 }
1140
1141 // ----------------------------------------------------------------------------
1142 // RealValue
1143 // ----------------------------------------------------------------------------
1144 Standard_Real TCollection_AsciiString::RealValue()const
1145 {
1146   char *ptr;
1147   Standard_Real value = 0;
1148   if(mystring) {
1149     value = strtod(mystring,&ptr);
1150     if (ptr != mystring) return value;
1151   }
1152   Standard_NumericError::Raise("TCollection_AsciiString::RealValue");
1153   return value;
1154 }
1155
1156 // ----------------------------------------------------------------------------
1157 // Read
1158 //--------------------------------------------------------------------------
1159 void TCollection_AsciiString::Read(Standard_IStream& astream)
1160 {
1161
1162  int newlength;
1163
1164  union { int bid ;
1165          Standard_Character buffer[8190]; } CHN ;
1166  astream >> CHN.buffer;               // Get characters from astream
1167
1168 #if OptJr
1169  STRINGLEN( CHN.buffer , newlength );
1170 #else
1171  STRLEN(CHN.buffer,newlength);
1172 #endif
1173  if (mystring) {
1174 //   Standard_Integer length = *(int*)((long)mystring-8);
1175 //   if(newlength >> 4 > length-1 >> 4)
1176    mystring = Reallocate((void*&)mystring,
1177                                                       ROUNDMEM(newlength+1));
1178  }
1179  else {
1180    mystring =Allocate(ROUNDMEM(newlength+1));
1181  }
1182 #if OptJr
1183  ASCIISTRINGCOPY( mystring , CHN.buffer , newlength ) ;
1184  mylength = newlength;
1185 #else
1186  STRCPY(mystring,CHN.buffer,newlength);
1187  mylength = newlength;
1188  mystring[mylength] = '\0';
1189 #endif
1190 }
1191
1192
1193 //---------------------------------------------------------------------------
1194 Standard_IStream& operator >> (Standard_IStream& astream,
1195                                TCollection_AsciiString& astring)
1196 {
1197   astring.Read(astream);
1198   return astream;
1199 }
1200
1201
1202 // ----------------------------------------------------------------------------
1203 // Print
1204 // ----------------------------------------------------------------------------
1205 void TCollection_AsciiString::Print(Standard_OStream& astream)const
1206 {
1207   if(mystring) astream << mystring;
1208 }
1209
1210
1211 // ----------------------------------------------------------------------------
1212 Standard_OStream& operator << (Standard_OStream& astream,
1213                                const TCollection_AsciiString& astring)
1214 {
1215   astring.Print(astream);
1216   return astream;
1217 }
1218
1219 // ----------------------------------------------------------------------------
1220 // RemoveAll
1221 // ----------------------------------------------------------------------------
1222 void TCollection_AsciiString::RemoveAll(const Standard_Character what,
1223                                         const Standard_Boolean CaseSensitive)
1224 {   
1225   if (mylength == 0) return;
1226   int c = 0;
1227   if (CaseSensitive) {
1228     for (int i=0; i < mylength; i++)
1229       if (mystring[i] != what) mystring[c++] = mystring[i];
1230   }
1231   else {
1232     Standard_Character upperwhat = toupper(what);
1233     for (int i=0; i < mylength; i++) { 
1234       if (toupper(mystring[i]) != upperwhat) mystring[c++] = mystring[i];
1235     }
1236   }
1237   mylength = c;
1238   mystring[mylength] = '\0';
1239 }
1240
1241 // ----------------------------------------------------------------------------
1242 // RemoveAll
1243 // ----------------------------------------------------------------------------
1244 void TCollection_AsciiString::RemoveAll(const Standard_Character what)
1245 {
1246   if (mylength == 0) return;
1247   int c = 0;
1248   for (int i=0; i < mylength; i++)
1249     if (mystring[i] != what) mystring[c++] = mystring[i];
1250   mylength = c;
1251   mystring[mylength] = '\0';
1252 }
1253
1254 // ----------------------------------------------------------------------------
1255 // Remove
1256 // ----------------------------------------------------------------------------
1257 void TCollection_AsciiString::Remove (const Standard_Integer where,
1258                                       const Standard_Integer ahowmany)
1259 {
1260  if (where+ahowmany <= mylength+1) {
1261    int i,j;
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';
1266  }
1267  else {
1268   Standard_OutOfRange::Raise("TCollection_AsciiString::Remove: "
1269                              "Too many characters to erase or invalid "
1270                              "starting value.");
1271  }
1272 }
1273
1274 //------------------------------------------------------------------------
1275 //  RightAdjust
1276 //------------------------------------------------------------------------
1277 void TCollection_AsciiString::RightAdjust ()
1278 {
1279   Standard_Integer i ;
1280   for ( i = mylength-1 ; i >= 0 ; i--)
1281     if(!IsSpace(mystring[i]))
1282       break;
1283   if( i < mylength-1 )
1284     Remove(i+2,mylength-(i+2)+1);
1285 }
1286
1287 //------------------------------------------------------------------------
1288 //  RightJustify
1289 //------------------------------------------------------------------------
1290 void TCollection_AsciiString::RightJustify(const Standard_Integer Width,
1291                                            const Standard_Character Filler)
1292 {
1293   Standard_Integer i ;
1294   Standard_Integer k ;
1295   if (Width > mylength) {
1296     if (mystring) {
1297 //      Standard_Integer length = *(int*)((long)mystring-8);
1298 //      if(Width >> 4 > length-1 >> 4)
1299       mystring = Reallocate((void*&)mystring,
1300                                                          Width+1);
1301     }
1302     else {
1303       mystring =Allocate(Width+1);
1304     }
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;
1308     mylength = Width;
1309     mystring[mylength] = '\0';
1310   }
1311   else if (Width < 0) {
1312     Standard_NegativeValue::Raise();
1313   }
1314 }
1315
1316 // ----------------------------------------------------------------------------
1317 // Search
1318 // ----------------------------------------------------------------------------
1319 Standard_Integer TCollection_AsciiString::Search
1320                                         (const Standard_CString what)const
1321 {
1322   Standard_Integer size;
1323 #if OptJr
1324   STRINGLEN( what , size );
1325 #else
1326   STRLEN(what,size);
1327 #endif
1328   if (size) {
1329     int k,j;
1330     int i = 0;
1331     Standard_Boolean find = Standard_False; 
1332     while ( i < mylength-size+1 && !find) {
1333       k = i++;
1334       j = 0;
1335       while (j < size && mystring[k++] == what[j++])
1336         if (j == size) find = Standard_True;
1337     }
1338     if (find)  return i;
1339   }
1340   return -1;
1341 }
1342
1343
1344 // ----------------------------------------------------------------------------
1345 // Search
1346 // ----------------------------------------------------------------------------
1347 Standard_Integer TCollection_AsciiString::Search
1348                                 (const TCollection_AsciiString& what) const
1349 {
1350   Standard_Integer size = what.mylength;
1351   Standard_CString swhat = what.mystring;  
1352   if (size) {
1353     int k,j;
1354     int i = 0;
1355     Standard_Boolean find = Standard_False; 
1356     while ( i < mylength-size+1 && !find) {
1357       k = i++;
1358       j = 0;
1359       while (j < size && mystring[k++] == swhat[j++])
1360         if (j == size) find = Standard_True;
1361     }
1362     if (find)  return i;
1363   }
1364   return -1;
1365 }
1366
1367
1368 // ----------------------------------------------------------------------------
1369 // SearchFromEnd
1370 // ----------------------------------------------------------------------------
1371 Standard_Integer TCollection_AsciiString::SearchFromEnd
1372                                         (const Standard_CString what)const
1373 {
1374   int size;
1375 #if OptJr
1376   STRINGLEN( what , size );
1377 #else
1378   STRLEN(what,size);
1379 #endif
1380   if (size) {
1381     int k,j;
1382     int i = mylength-1;
1383     Standard_Boolean find = Standard_False; 
1384     while ( i >= size-1 && !find) {
1385       k = i--;
1386       j = size-1;
1387       while (j >= 0 && mystring[k--] == what[j--])
1388         if (j == -1) find = Standard_True;
1389     }
1390     if (find)  return i-size+3;
1391   }
1392   return -1;
1393 }
1394
1395
1396 // ----------------------------------------------------------------------------
1397 // SearchFromEnd
1398 // ----------------------------------------------------------------------------
1399 Standard_Integer TCollection_AsciiString::SearchFromEnd
1400                                 (const TCollection_AsciiString& what)const
1401 {
1402   int size = what.mylength;
1403   if (size) {
1404     Standard_CString swhat = what.mystring;  
1405     int k,j;
1406     int i = mylength-1;
1407     Standard_Boolean find = Standard_False; 
1408     while ( i >= size-1 && !find) {
1409       k = i--;
1410       j = size-1;
1411       while (j >= 0 && mystring[k--] == swhat[j--])
1412         if (j == -1) find = Standard_True;
1413     }
1414     if (find)  return i-size+3;
1415   }
1416   return -1;
1417 }
1418
1419 // ----------------------------------------------------------------------------
1420 // SetValue
1421 // ----------------------------------------------------------------------------
1422 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1423                                        const Standard_Character what)
1424 {
1425   if (where > 0 && where <= mylength) {
1426     mystring[where-1] = what;
1427   }
1428   else {
1429     Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1430                                "parameter where");
1431   }
1432 }
1433
1434 // ----------------------------------------------------------------------------
1435 // SetValue
1436 // ----------------------------------------------------------------------------
1437 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1438                                        const Standard_CString what)
1439 {
1440  if (where > 0 && where <= mylength+1) {
1441    Standard_Integer size;
1442 #if OptJr
1443    STRINGLEN( what , size );
1444 #else
1445    STRLEN(what,size);
1446 #endif
1447    size += (where - 1);  
1448    if (size >= mylength){
1449      if (mystring) {
1450 //       Standard_Integer length = *(int*)((long)mystring-8);
1451 //       if(size >> 4 > length-1 >> 4)
1452        mystring = Reallocate((void*&)mystring,
1453                                                           size+1);
1454      }
1455      else {
1456        mystring =Allocate(size+1);
1457      }
1458      mylength = size;
1459    } 
1460    for (int i = where-1; i < size; i++)
1461      mystring[i] = what[i-(where-1)];
1462    mystring[mylength] = '\0';
1463  }
1464  else {
1465    Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1466                               "parameter where");
1467  }
1468 }
1469
1470 // ----------------------------------------------------------------------------
1471 // SetValue
1472 // ----------------------------------------------------------------------------
1473 void TCollection_AsciiString::SetValue(const Standard_Integer where,
1474                                        const TCollection_AsciiString& what)
1475 {
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){
1481      if (mystring) {
1482 //       Standard_Integer length = *(int*)((long)mystring-8);
1483 //       if(size >> 4 > length-1 >> 4)
1484        mystring = Reallocate((void*&)mystring,
1485                                                           size+1);
1486      }
1487      else {
1488        mystring =Allocate(size+1);
1489      }
1490      mylength = size;
1491    } 
1492    for (int i = where-1; i < size; i++)
1493      mystring[i] = swhat[i-(where-1)];
1494    mystring[mylength] = '\0';
1495  }
1496  else {
1497    Standard_OutOfRange::Raise("TCollection_AsciiString::SetValue : "
1498                               "parameter where");
1499  }
1500 }
1501
1502 // ----------------------------------------------------------------------------
1503 // Split
1504 // Private
1505 // ----------------------------------------------------------------------------
1506 void TCollection_AsciiString::Split(const Standard_Integer where,
1507                                     TCollection_AsciiString& res)
1508 {
1509   if (where >= 0 && where <= mylength) {
1510     res = &mystring[where] ;
1511     Trunc(where);
1512     return ;
1513   }
1514   Standard_OutOfRange::Raise("TCollection_AsciiString::Split index");
1515   return ;
1516 }
1517
1518 // ----------------------------------------------------------------------------
1519 // Split
1520 // ----------------------------------------------------------------------------
1521 TCollection_AsciiString TCollection_AsciiString::Split
1522                                                 (const Standard_Integer where)
1523 {
1524   if (where >= 0 && where <= mylength) {
1525 #if OptJr
1526     TCollection_AsciiString res( &mystring[where] , mylength - where );
1527 #else
1528     TCollection_AsciiString res(&mystring[where]);
1529 #endif
1530     Trunc(where);
1531     return res;
1532   }
1533   Standard_OutOfRange::Raise("TCollection_AsciiString::Split index");
1534   TCollection_AsciiString res;
1535   return res;
1536 }
1537
1538 // ----------------------------------------------------------------------------
1539 // SubString
1540 // Private
1541 // ----------------------------------------------------------------------------
1542 void TCollection_AsciiString::SubString(const Standard_Integer FromIndex,
1543                                         const Standard_Integer ToIndex,
1544                                         TCollection_AsciiString& res) const
1545 {
1546
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)); 
1551
1552 #if OptJr
1553   CSTRINGCOPY( res.mystring , &(mystring [ FromIndex - 1 ]) , newlength );
1554 #else
1555   memcpy(res.mystring, &(mystring[FromIndex-1]),newlength);
1556 #endif
1557   res.mystring[newlength] = '\0';
1558   res.mylength = newlength;
1559   return ;
1560 }
1561
1562 #if !OptJr
1563 // ----------------------------------------------------------------------------
1564 // SubString
1565 // ----------------------------------------------------------------------------
1566 TCollection_AsciiString TCollection_AsciiString::SubString
1567                                 (const Standard_Integer FromIndex,
1568                                  const Standard_Integer ToIndex) const
1569 {
1570
1571   if (ToIndex > mylength || FromIndex <= 0 || FromIndex > ToIndex )
1572     Standard_OutOfRange::Raise();
1573
1574 #if OptJr
1575   return TCollection_AsciiString( &mystring [ FromIndex - 1 ] ,
1576                                   ToIndex - FromIndex + 1 ) ;
1577
1578 #else
1579   TCollection_AsciiString res;
1580   Standard_Integer newlength = ToIndex-FromIndex+1;
1581   res.mystring = Allocate(ROUNDMEM(newlength+1)); 
1582 #if OptJr
1583   CSTRINGCOPY( res.mystring , &(mystring [ FromIndex - 1 ]) , newlength );
1584 #else
1585   memcpy(res.mystring, &(mystring[FromIndex-1]),newlength);
1586 #endif
1587   res.mystring[newlength] = '\0';
1588   res.mylength = newlength;
1589   return res;
1590 #endif
1591 }
1592 #endif
1593
1594 // ----------------------------------------------------------------------------
1595 // Token
1596 // Private
1597 // ----------------------------------------------------------------------------
1598 void TCollection_AsciiString::Token(const Standard_CString separators,
1599                                     const Standard_Integer whichone,
1600                                     TCollection_AsciiString& res)const
1601 {
1602   
1603 #if OptJr
1604   res = Token( separators , whichone ) ;
1605
1606 #else
1607   if (!separators)
1608     Standard_NullObject::Raise("TCollection_AsciiString::Token : "
1609                                "parameter 'separators'");
1610
1611   int              i,j,k,l;
1612   Standard_CString buftmp =Allocate(mylength+1);
1613   Standard_Character aSep;
1614   
1615   Standard_Boolean isSepFound   = Standard_False, otherSepFound;
1616   
1617   j = 0;
1618   
1619   for (i = 0; i < whichone && j < mylength; i++) {
1620     isSepFound   = Standard_False;
1621     k = 0;
1622     buftmp[0] = 0;
1623     
1624     // Avant de commencer il faut virer les saloperies devant
1625     //
1626     otherSepFound = Standard_True;
1627     while (j < mylength && otherSepFound) {
1628       l    = 0;
1629       otherSepFound = Standard_False;
1630       aSep = separators[l];
1631       while(aSep != 0) {
1632         if (aSep == mystring[j]) {
1633           aSep = 0;
1634           otherSepFound = Standard_True;
1635         }
1636         else {
1637           aSep = separators[l++];
1638         }
1639       }
1640       if (otherSepFound) j++;
1641     }
1642     
1643     while (!isSepFound && k < mylength && j<mylength ) {
1644       l    = 0;
1645       aSep = separators[l];
1646       
1647       while (aSep != 0 && !isSepFound) {
1648         if (aSep == mystring[j]) {
1649           buftmp[k] = 0;
1650           isSepFound = Standard_True;
1651         }
1652         else {
1653           buftmp[k] = mystring[j];
1654         }
1655         l++;
1656         aSep = separators[l];
1657       }
1658       j++; k++;
1659       if(j==mylength) buftmp[k] = 0;
1660     }
1661   }
1662
1663   if (i < whichone) {
1664     buftmp[0] = 0;
1665     Standard::Free((void*&)buftmp);
1666   }
1667   else {
1668     res.mystring = buftmp;
1669 #if OptJr
1670     STRINGLEN( buftmp , res.mylength );
1671 #else
1672     STRLEN(buftmp,res.mylength);
1673 #endif
1674   }
1675 #endif
1676   return ;
1677 }
1678
1679 // ----------------------------------------------------------------------------
1680 // Token
1681 // ----------------------------------------------------------------------------
1682 TCollection_AsciiString TCollection_AsciiString::Token
1683                                         (const Standard_CString separators,
1684                                          const Standard_Integer whichone) const
1685 {
1686   if (!separators)
1687     Standard_NullObject::Raise("TCollection_AsciiString::Token : "
1688                                "parameter 'separators'");
1689
1690 #if OptJr
1691   Standard_Integer theOne ;
1692   Standard_Integer StringIndex = 0 ;
1693   Standard_Integer SeparatorIndex ;
1694   Standard_Integer BeginIndex=0 ;
1695   Standard_Integer EndIndex=0 ;
1696
1697 //  cout << "'" << mystring <<  "'" << endl ;
1698   for ( theOne = 0 ; theOne < whichone ; theOne++ ) {
1699      BeginIndex = 0 ;
1700      EndIndex = 0 ;
1701 //     cout << "theOne " << theOne << endl ;
1702      if ( StringIndex == mylength )
1703        break ;
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 ] ) {
1709                break ;
1710              }
1711              SeparatorIndex += 1 ;
1712            }
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 ;
1717             break ;
1718           }
1719         }
1720         else if ( BeginIndex == 0 ) {               // We have not a Separator
1721           BeginIndex = StringIndex + 1 ;
1722 //          cout << "BeginIndex " << BeginIndex << endl ;
1723         }
1724      }
1725 //     cout << "BeginIndex " << BeginIndex << " EndIndex " << EndIndex << endl ;
1726   }
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 ) ;
1734
1735 // Hereafter : Why write simple when we can write complicated ! ... 
1736 #else
1737   TCollection_AsciiString res;
1738   
1739   int                        i,j,k,l;
1740   Standard_CString buftmp = Allocate(mylength+1);
1741   Standard_Character       aSep;
1742   
1743   Standard_Boolean isSepFound   = Standard_False, otherSepFound;
1744   
1745   j = 0;
1746   
1747   for (i = 0; i < whichone && j < mylength; i++) {
1748     isSepFound   = Standard_False;
1749     k = 0;
1750     buftmp[0] = 0;
1751     
1752     // Avant de commencer il faut virer les saloperies devant
1753     //
1754     otherSepFound = Standard_True;
1755     while (j < mylength && otherSepFound) {
1756       l    = 0;
1757       otherSepFound = Standard_False;
1758       aSep = separators[l];
1759       while(aSep != 0) {
1760         if (aSep == mystring[j]) {
1761           aSep = 0;
1762           otherSepFound = Standard_True;
1763         }
1764         else {
1765           aSep = separators[l++];
1766         }
1767       }
1768       if (otherSepFound) j++;
1769     }
1770     
1771     while (!isSepFound && k < mylength && j<mylength ) {
1772       l    = 0;
1773       aSep = separators[l];
1774       
1775       while (aSep != 0 && !isSepFound) {
1776         if (aSep == mystring[j]) {
1777           buftmp[k] = 0;
1778           isSepFound = Standard_True;
1779         }
1780         else {
1781           buftmp[k] = mystring[j];
1782         }
1783         l++;
1784         aSep = separators[l];
1785       }
1786       j++; k++;
1787       if(j==mylength) buftmp[k] = 0;
1788     }
1789   }
1790   
1791   if (i < whichone) {
1792     buftmp[0] = 0;
1793     Standard::Free((void*&)buftmp);
1794   }
1795   else {
1796     res.mystring = buftmp;
1797 #if OptJr
1798     STRINGLEN( buftmp , res.mylength );
1799 #else
1800     STRLEN(buftmp,res.mylength);
1801 #endif
1802   }
1803   return res;
1804 #endif
1805 }
1806
1807 // ----------------------------------------------------------------------------
1808 // Trunc
1809 // ----------------------------------------------------------------------------
1810 void TCollection_AsciiString::Trunc(const Standard_Integer ahowmany)
1811 {
1812   if (ahowmany < 0 || ahowmany > mylength)
1813     Standard_OutOfRange::Raise("TCollection_AsciiString::Trunc : "
1814                                "parameter 'ahowmany'");
1815   mylength = ahowmany;
1816   mystring[mylength] = '\0';
1817 }
1818
1819 // ----------------------------------------------------------------------------
1820 // UpperCase
1821 // ----------------------------------------------------------------------------
1822 void TCollection_AsciiString::UpperCase()
1823 {
1824   for (int i=0; i < mylength; i++)
1825     mystring[i] = toupper(mystring[i]);
1826 }
1827
1828 //------------------------------------------------------------------------
1829 //  UsefullLength
1830 //------------------------------------------------------------------------
1831 Standard_Integer TCollection_AsciiString::UsefullLength () const
1832 {
1833   Standard_Integer i ;
1834   for ( i = mylength -1 ; i >= 0 ; i--) 
1835     if (IsGraphic(mystring[i])) break;
1836   return i+1;
1837 }
1838
1839 // ----------------------------------------------------------------------------
1840 // Value
1841 // ----------------------------------------------------------------------------
1842 Standard_Character TCollection_AsciiString::Value
1843                                         (const Standard_Integer where)const
1844 {
1845  if (where > 0 && where <= mylength) {
1846    if(mystring) return mystring[where-1];
1847    else                 return '\0';
1848  }
1849  Standard_OutOfRange::Raise("TCollection_AsciiString::Value : parameter where");
1850  return '\0';
1851 }
1852
1853
1854 //------------------------------------------------------------------------
1855 //  ISSIMILAR
1856 //------------------------------------------------------------------------
1857 Standard_Boolean TCollection_AsciiString::ISSIMILAR
1858                                 (const TCollection_AsciiString& string1,
1859                                  const TCollection_AsciiString& string2)
1860 {
1861   if ( string1.Length() != string2.Length() )
1862     return Standard_False ;
1863   return ::ISSIMILAR (string1.ToCString() ,
1864                       string1.Length() ,
1865                       string2.ToCString() );
1866 }