1 // Copyright (c) 1993-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
22 // OCC6794: if OptJr is ON, we need to ensure that memory allocations are done by 4 bytes words,
23 // in order to avoid writing into unallocated memory at the string end when it is copied
24 // by CSTRINGCOPY or ASCIISTRINGCOPY macros
25 // (though, due to OCC memory manager roundings of allocated memory, the problem appears
26 // only when MMGT_OPT is 0 and string size is greater than MMGT_THRESHOLD)
28 #define ROUNDMEM(len) (((len)+3)&~0x3)
30 #define ROUNDMEM(len) (len)
32 //#if defined(WNT) || defined(LIN)
36 #include <TCollection_ExtendedString.ixx>
37 #include <Standard.hxx>
38 #include <Standard_ExtString.hxx>
39 #include <Standard_NullObject.hxx>
40 #include <Standard_OutOfRange.hxx>
41 #include <Standard_ctype.hxx>
42 #include <Standard_String.hxx>
43 #include <TCollection_AsciiString.hxx>
46 Standard_PExtCharacter Allocate(const Standard_Size aLength);
49 Standard_PExtCharacter Reallocate(Standard_Address aAddr,
50 const Standard_Size aLength);
52 Standard_EXPORT short NULL_EXTSTRING[1] = {0};
54 //============================== input value have len = 2 bytes ====
55 inline Standard_ExtCharacter ConvertToUnicode2B (unsigned char *p)
63 Standard_ExtCharacter chr;
66 unsigned char l = *(p+1);
71 l &= h;// l is defined
80 //============================== input value have len = 3 bytes ====
81 inline Standard_ExtCharacter ConvertToUnicode3B (unsigned char *p)
83 // *p, *(p+1), *(p+2) =>0 , 1, 2
89 Standard_ExtCharacter chr;
93 unsigned char h = *(p+1);//h = 10yyyyyy
94 unsigned char l = *(p+2);//h = 10zzzzzz
99 l &= h; // yyzzzzzz - l is defined
102 unsigned char a = *p;// a = 1110xxxx
104 a |= 0x0F;//a = xxxx1111
107 h |= 0xF0; //1111yyyy
108 a &= h; // a = xxxxyyyy
109 EL.hl.h = a; //h is defined
112 //============================== returns number of symbols in UTF8 string ====
113 inline Standard_Integer nbSymbols(const Standard_CString aStr) {
114 Standard_Integer aLen = 0;// length in symbols
116 while (aStr[i] != '\0') {
117 if((aStr[i] & 0x80) == 0x00) //1byte => 1 symb - Lat1
119 else if((aStr[i] & 0xE0) == 0xC0 &&
121 (aStr[i+1] & 0xC0) == 0x80)) {//2 bytes => 1 symb
124 } else if((aStr[i] & 0xF0) == 0xE0 &&
125 ((aStr[i+1] && (aStr[i+1] & 0xC0) == 0x80)) &&
126 (aStr[i+2] && (aStr[i+2] & 0xC0) == 0x80)) {
135 //-----------------------------------------------------------------------------
136 // Create an empty ExtendedString
137 // ----------------------------------------------------------------------------
138 TCollection_ExtendedString::TCollection_ExtendedString()
142 mystring = Allocate((mylength+1)*2);
143 mystring[mylength] = '\0';
146 //----------------------------------------------------------------------------
147 // Create an ExtendedString from a Standard_CString
148 //----------------------------------------------------------------------------
149 TCollection_ExtendedString::TCollection_ExtendedString
150 (const Standard_CString astring,
151 const Standard_Boolean isMultiByte)
156 STRINGLEN( astring , mylength ) ;
158 STRLEN(astring,mylength);
160 mystring = Allocate((mylength+1)*2);
161 for (int i = 0 ; i < mylength ; i++)
162 mystring[i] = ToExtCharacter(astring[i]);
163 mystring[mylength] = '\0';
166 mylength = nbSymbols(astring);
167 mystring = Allocate(ROUNDMEM((mylength+1)*2));
168 if(!ConvertToUnicode (astring))
171 cout <<"UTF8 decoding failure..." <<endl;
177 Standard_NullObject::Raise("TCollection_ExtendedString : "
178 "parameter 'astring'");
182 //---------------------------------------------------------------------------
183 // Create an ExtendedString from an ExtString
184 //--------------------------------------------------------------------------
185 TCollection_ExtendedString::TCollection_ExtendedString
186 (const Standard_ExtString astring)
191 EXTSTRINGLEN( astring , mylength ) ;
193 EXTSTRLEN(astring,mylength);
195 mystring = Allocate(ROUNDMEM((mylength+1)*2));
197 EXTSTRINGCOPY( mystring , astring , mylength );
199 STRCPY(mystring,astring,mylength);
200 mystring[mylength] = '\0';
204 Standard_NullObject::Raise("TCollection_ExtendedString : null parameter ");
208 // ----------------------------------------------------------------------------
209 // Create an asciistring from a Standard_Character
210 // ----------------------------------------------------------------------------
211 TCollection_ExtendedString::TCollection_ExtendedString
212 (const Standard_Character aChar)
214 if ( aChar != '\0' ) {
216 mystring = Allocate(2*2);
217 mystring[0] = ToExtCharacter(aChar);
223 mystring = Allocate((mylength+1)*2);
224 mystring[mylength] = '\0';
228 //--------------------------------------------------------------------------
229 // Create a string from a ExtCharacter
230 // ----------------------------------------------------------------------------
231 TCollection_ExtendedString::TCollection_ExtendedString
232 (const Standard_ExtCharacter aChar)
235 mystring = Allocate(2*2);
240 // ----------------------------------------------------------------------------
241 // Create an AsciiString from a filler
242 // ----------------------------------------------------------------------------
243 TCollection_ExtendedString::TCollection_ExtendedString
244 (const Standard_Integer length,
245 const Standard_ExtCharacter filler )
247 mystring = Allocate((length+1)*2);
249 for (int i = 0 ; i < length ; i++) mystring[i] = filler;
250 mystring[length] = '\0';
253 // ----------------------------------------------------------------------------
254 // Create a String from an Integer
255 // ----------------------------------------------------------------------------
256 TCollection_ExtendedString::TCollection_ExtendedString
257 (const Standard_Integer aValue)
261 Sprintf(&CHN.t[0],"%d",aValue);
263 STRINGLEN( CHN.t , mylength ) ;
265 STRLEN(CHN.t,mylength);
267 mystring = Allocate((mylength+1)*2);
268 for (int i = 0 ; i < mylength ; i++) mystring[i] = ToExtCharacter(CHN.t[i]);
269 mystring[mylength] = '\0';
272 // ----------------------------------------------------------------------------
273 // Create a String from a real
274 // ----------------------------------------------------------------------------
275 TCollection_ExtendedString::TCollection_ExtendedString
276 (const Standard_Real aValue)
280 Sprintf(&CHN.t[0],"%g",aValue);
282 STRINGLEN( CHN.t , mylength ) ;
284 STRLEN(CHN.t,mylength);
286 mystring = Allocate((mylength+1)*2);
287 for (int i = 0 ; i < mylength ; i++) mystring[i] = ToExtCharacter(CHN.t[i]);
288 mystring[mylength] = '\0';
291 //-----------------------------------------------------------------------------
292 // create an extendedstring from an extendedstring
293 // ----------------------------------------------------------------------------
294 TCollection_ExtendedString::TCollection_ExtendedString
295 (const TCollection_ExtendedString& astring)
298 mylength = astring.mylength;
299 mystring = Allocate(ROUNDMEM((mylength+1)*2));
300 if (astring.mystring)
302 EXTENDEDSTRINGCOPY( mystring , astring.mystring , mylength );
304 STRCPY(mystring,astring.mystring,mylength);
305 mystring[mylength] = '\0';
309 //---------------------------------------------------------------------------
310 // Create an extendedstring from an AsciiString
311 //---------------------------------------------------------------------------
312 TCollection_ExtendedString::TCollection_ExtendedString
313 (const TCollection_AsciiString& astring)
315 mylength = astring.Length();
316 mystring = Allocate((mylength+1)*2);
318 Standard_CString aCString = astring.ToCString() ;
319 for (Standard_Integer i = 0; i <= mylength ; i++)
320 mystring[i] = ToExtCharacter( aCString[i] );
322 for (Standard_Integer i = 0; i < mylength ; i++)
323 mystring[i] = ToExtCharacter(astring.Value(i+1));
324 mystring[mylength] = '\0';
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
331 void TCollection_ExtendedString::AssignCat
332 (const TCollection_ExtendedString& other)
334 Standard_Integer otherlength = other.mylength;
336 Standard_ExtString sother = other.mystring;
337 Standard_Integer newlength = mylength +otherlength;
339 mystring = Reallocate((void*&)mystring,
340 ROUNDMEM((newlength+1)*2));
342 // if ( ((long ) ( &mystring[ mylength ] ) & 3) == 0 ) {
343 EXTENDEDSTRINGCAT( mystring , mylength , sother , otherlength ) ;
346 // STRCAT( mystring , mylength , sother , otherlength + 1 ) ;
348 STRCAT(mystring,mylength,sother,otherlength);
352 mystring = Allocate(ROUNDMEM((newlength+1)*2));
354 EXTENDEDSTRINGCOPY( mystring , sother , newlength );
356 STRCPY(mystring,sother,newlength);
359 mylength = newlength;
361 mystring[mylength] = '\0';
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
369 TCollection_ExtendedString TCollection_ExtendedString::Cat
370 (const TCollection_ExtendedString& other) const
372 Standard_ExtString sother = other.mystring;
373 const Standard_Integer otherlength = sother ? other.mylength : 0;
374 const Standard_Integer newlength = mylength + otherlength;
376 // ensure rounding allocated memory to 4 bytes
377 TCollection_ExtendedString res (newlength | 0x1, 0);
378 res.mylength = newlength;
380 TCollection_ExtendedString res (newlength, 0);
385 EXTENDEDSTRINGCOPY( res.mystring , mystring , mylength );
386 // if ( ((long ) ( &res.mystring[ mylength ] ) & 3) == 0 ) {
387 EXTENDEDSTRINGCAT( res.mystring , mylength , sother , otherlength ) ;
390 // STRCAT( res.mystring , mylength , sother , otherlength + 1 ) ;
392 if (mylength > 0) STRCPY (res.mystring, mystring, mylength);
393 STRCPY (&res.mystring[mylength], sother, otherlength);
394 res.mystring[newlength] = '\0';
397 else if (mylength > 0) {
398 // TCollection_ExtendedString res;
399 // res.mystring = (Standard_ExtString)Standard::Allocate((mylength+1)*2);
401 EXTENDEDSTRINGCOPY( res.mystring , mystring , mylength );
403 STRCPY (res.mystring, mystring,mylength);
404 res.mystring[res.mylength] = '\0';
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
413 void TCollection_ExtendedString::ChangeAll(const Standard_ExtCharacter aChar,
414 const Standard_ExtCharacter NewChar)
416 for (int i = 0 ; i < mylength; i++)
417 if (mystring[i] == aChar) mystring[i] = NewChar;
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
423 void TCollection_ExtendedString::Clear()
425 if (mystring) Standard::Free((void*&)mystring);
428 mystring = Allocate((mylength+1)*2);
429 mystring[mylength] = '\0';
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
435 void TCollection_ExtendedString::Copy (const TCollection_ExtendedString& fromwhere)
438 if (fromwhere.mystring) {
439 Standard_Integer newlength = fromwhere.mylength;
441 mystring = Reallocate((void*&)mystring, ROUNDMEM(( newlength + 1)*2 ));
444 mystring = Allocate(ROUNDMEM((newlength+1)*2));
446 mylength = newlength;
448 EXTENDEDSTRINGCOPY( mystring , fromwhere.mystring , newlength );
450 STRCPY(mystring, fromwhere.mystring,newlength);
451 mystring[mylength] = '\0';
457 mystring[mylength] = '\0';
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
465 void TCollection_ExtendedString::Destroy()
467 if (mystring) Standard::Free((void*&)mystring);
471 //----------------------------------------------------------------------------
472 // Insert a character before 'where'th character
473 // ----------------------------------------------------------------------------
474 void TCollection_ExtendedString::Insert(const Standard_Integer where,
475 const Standard_ExtCharacter what)
477 if (where > mylength + 1 )
478 Standard_OutOfRange::Raise("TCollection_ExtendedString::Insert : "
479 "Parameter where is too big");
481 Standard_OutOfRange::Raise("TCollection_ExtendedString::Insert : "
482 "Parameter where is negative");
485 mystring = Reallocate((void*&)mystring,
489 mystring = Allocate((mylength+2)*2);
491 if (where != mylength +1) {
492 for (int i=mylength-1; i >= where-1; i--)
493 mystring[i+1] = mystring[i];
495 mystring[where-1] = what;
497 mystring[mylength] = '\0';
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
503 void TCollection_ExtendedString::Insert(const Standard_Integer where,
504 const TCollection_ExtendedString& what)
506 Standard_ExtString swhat = what.mystring;
507 if (where <= mylength + 1) {
508 Standard_Integer whatlength = what.mylength;
510 Standard_Integer newlength = mylength + whatlength;
513 mystring = Reallocate(
514 (void*&)mystring,(newlength+1)*2);
517 mystring = Allocate((newlength+1)*2);
519 if (where != mylength +1) {
520 for (int i=mylength-1; i >= where-1; i--)
521 mystring[i+whatlength] = mystring[i];
523 for (int i=0; i < whatlength; i++)
524 mystring[where-1+i] = swhat[i];
526 mylength = newlength;
527 mystring[mylength] = '\0';
531 Standard_OutOfRange::Raise("TCollection_ExtendedString::Insert : "
532 "Parameter where is too big");
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
540 Standard_Boolean TCollection_ExtendedString::IsEqual
541 (const Standard_ExtString other) const
543 // Standard_Integer otherlength ;
545 // EXTSTRINGLEN( other , otherlength )
546 // if ( mylength != otherlength ) return Standard_False;
547 Standard_Boolean KEqual ;
548 LEXTSTRINGEQUAL( mystring , mylength , other , KEqual ) ;
552 Standard_Boolean TCollection_ExtendedString::NoIsEqual
553 (const Standard_ExtString other) const
554 {return Standard_False ;}
557 // ----------------------------------------------------------------------------
559 // ----------------------------------------------------------------------------
560 Standard_Boolean TCollection_ExtendedString::IsEqual
561 (const TCollection_ExtendedString& other) const
563 if (mylength != other.mylength) return Standard_False;
564 Standard_ExtString sother = other.mystring;
566 Standard_Boolean KEqual ;
567 EXTENDEDSTRINGEQUAL( mystring , sother , mylength , KEqual ) ;
570 for (int i = 0 ; i < mylength ; i++)
571 if (mystring[i] != sother[i]) return Standard_False;
572 return Standard_True;
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
580 Standard_Boolean TCollection_ExtendedString::IsDifferent
581 (const Standard_ExtString other ) const
583 // Standard_Integer otherlength ;
585 // EXTSTRINGLEN( other , otherlength )
586 // if ( mylength != otherlength ) return Standard_True;
587 Standard_Boolean KEqual ;
588 LEXTSTRINGEQUAL( mystring , mylength , other , KEqual ) ;
592 Standard_Boolean TCollection_ExtendedString::NoIsDifferent
593 (const Standard_ExtString other ) const
594 {return Standard_False ;}
597 // ----------------------------------------------------------------------------
599 // ----------------------------------------------------------------------------
600 Standard_Boolean TCollection_ExtendedString::IsDifferent
601 (const TCollection_ExtendedString& other) const
603 if (mylength != other.mylength) return Standard_True;
604 Standard_ExtString sother = other.mystring;
606 Standard_Boolean KEqual ;
607 EXTENDEDSTRINGEQUAL( mystring , sother , mylength , KEqual ) ;
610 for (int i = 0 ; i < mylength ; i++)
611 if (mystring[i] != sother[i]) return Standard_True;
612 return Standard_False;
617 // ----------------------------------------------------------------------------
619 // ----------------------------------------------------------------------------
620 Standard_Boolean TCollection_ExtendedString::IsLess
621 (const Standard_ExtString other) const
623 // Standard_Integer otherlength ;
625 // EXTSTRINGLEN( other , otherlength )
626 Standard_Boolean KLess ;
627 // EXTSTRINGLESS( mystring , mylength , other , otherlength ,
628 // INF( mylength , otherlength ) , KLess ) ;
629 LEXTSTRINGLESS( mystring , mylength , other , KLess ) ;
633 Standard_Boolean TCollection_ExtendedString::NoIsLess
634 (const Standard_ExtString other) const
635 {return Standard_False ;}
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
641 Standard_Boolean TCollection_ExtendedString::IsLess
642 (const TCollection_ExtendedString& other) const
645 Standard_Integer otherlength = other.mylength;
646 Standard_ExtString sother = other.mystring;
648 Standard_Boolean KLess ;
649 EXTENDEDSTRINGLESS( mystring , mylength , sother , otherlength ,
650 INF( mylength , otherlength ) , KLess ) ;
653 Standard_Integer i = 0, j = 0;
654 while ( i < mylength && j < otherlength) {
655 if (mystring[i] < sother[j]) return Standard_True;
656 if (mystring[i] > sother[j]) return Standard_False;
660 if (i == mylength && j < otherlength) return Standard_True;
661 return Standard_False;
666 // ----------------------------------------------------------------------------
668 // ----------------------------------------------------------------------------
669 Standard_Boolean TCollection_ExtendedString::IsGreater
670 (const Standard_ExtString other) const
672 // Standard_Integer otherlength ;
674 // EXTSTRINGLEN( other , otherlength )
675 Standard_Boolean KGreater ;
676 // EXTSTRINGGREATER( mystring , mylength , other , otherlength ,
677 // INF( mylength , otherlength ) , KGreater ) ;
678 LEXTSTRINGGREATER( mystring , mylength , other , KGreater ) ;
682 Standard_Boolean TCollection_ExtendedString::NoIsGreater
683 (const Standard_ExtString other) const
684 {return Standard_False ;}
687 // ----------------------------------------------------------------------------
689 // ----------------------------------------------------------------------------
690 Standard_Boolean TCollection_ExtendedString::IsGreater
691 (const TCollection_ExtendedString& other) const
693 Standard_Integer otherlength = other.mylength;
694 Standard_ExtString sother = other.mystring;
696 Standard_Boolean KGreater ;
697 EXTENDEDSTRINGGREATER( mystring , mylength , sother , otherlength ,
698 INF( mylength , otherlength ) , KGreater ) ;
701 Standard_Integer i = 0, j = 0;
702 while (i < mylength && j <= otherlength) {
703 if (mystring[i] < sother[j]) return Standard_False;
704 if (mystring[i] > sother[j]) return Standard_True;
708 if (j == otherlength && i < mylength) return Standard_True;
709 return Standard_False;
713 // ----------------------------------------------------------------------------
715 // ----------------------------------------------------------------------------
716 Standard_Boolean TCollection_ExtendedString::IsAscii() const
718 for( Standard_Integer i = 0 ; i < mylength ; i++)
719 if (!IsAnAscii(mystring[i])) return Standard_False;
720 return Standard_True;
723 //------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
726 Standard_Integer TCollection_ExtendedString::Length() const
731 // ----------------------------------------------------------------------------
733 // ----------------------------------------------------------------------------
734 void TCollection_ExtendedString::Print(Standard_OStream& astream) const
736 // ASCII symbols (including extended Ascii) are printed as is;
737 // other Unicode characters are encoded as SGML numeric character references
738 for (Standard_Integer i = 0 ; i < mylength ; i++) {
739 Standard_ExtCharacter c = mystring[i];
741 astream << ToCharacter(c);
743 astream << "&#" << c << ";";
748 // ----------------------------------------------------------------------------
749 Standard_OStream& operator << (Standard_OStream& astream,
750 const TCollection_ExtendedString& astring)
752 astring.Print(astream);
756 // ----------------------------------------------------------------------------
758 // ----------------------------------------------------------------------------
759 void TCollection_ExtendedString::RemoveAll(const Standard_ExtCharacter what)
762 if (mylength == 0) return;
764 for (int i=0; i < mylength; i++)
765 if (mystring[i] != what) mystring[c++] = mystring[i];
767 mystring[mylength] = '\0';
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
773 void TCollection_ExtendedString::Remove (const Standard_Integer where,
774 const Standard_Integer ahowmany)
776 if (where+ahowmany <= mylength+1) {
778 for (i = where+ahowmany-1, j = where-1; i < mylength; i++, j++)
779 mystring[j] = mystring[i];
780 mylength -= ahowmany;
781 mystring[mylength] = '\0';
784 Standard_OutOfRange::Raise("TCollection_ExtendedString::Remove: "
785 "Too many characters to erase or "
786 "invalid starting value.");
789 // ----------------------------------------------------------------------------
791 // ----------------------------------------------------------------------------
792 Standard_Integer TCollection_ExtendedString::Search
793 (const TCollection_ExtendedString& what) const
795 Standard_Integer size = what.mylength;
796 Standard_ExtString swhat = what.mystring;
800 Standard_Boolean find = Standard_False;
801 while ( i < mylength-size+1 && !find) {
804 while (j < size && mystring[k++] == swhat[j++])
805 if (j == size) find = Standard_True;
812 // ----------------------------------------------------------------------------
814 // ----------------------------------------------------------------------------
815 Standard_Integer TCollection_ExtendedString::SearchFromEnd
816 (const TCollection_ExtendedString& what) const
818 Standard_Integer size = what.mylength;
820 Standard_ExtString swhat = what.mystring;
823 Standard_Boolean find = Standard_False;
824 while ( i >= size-1 && !find) {
827 while (j >= 0 && mystring[k--] == swhat[j--])
828 if (j == -1) find = Standard_True;
830 if (find) return i-size+3;
835 // ----------------------------------------------------------------------------
837 // ----------------------------------------------------------------------------
838 void TCollection_ExtendedString::SetValue(const Standard_Integer where,
839 const Standard_ExtCharacter what)
841 if (where > 0 && where <= mylength) {
842 mystring[where-1] = what;
845 Standard_OutOfRange::Raise("TCollection_ExtendedString::SetValue : parameter where");
849 // ----------------------------------------------------------------------------
851 // ----------------------------------------------------------------------------
852 void TCollection_ExtendedString::SetValue
853 (const Standard_Integer where,
854 const TCollection_ExtendedString& what)
856 if (where > 0 && where <= mylength+1) {
857 Standard_Integer size = what.mylength;
858 Standard_ExtString swhat = what.mystring;
860 if (size >= mylength){
862 mystring = Reallocate ((void*&)mystring,(size+1)*2);
865 mystring = Allocate((size+1)*2);
869 for (int i = where-1; i < size; i++)
870 mystring[i] = swhat[i-(where-1)];
871 mystring[mylength] = '\0';
874 Standard_OutOfRange::Raise("TCollection_ExtendedString::SetValue : "
878 // ----------------------------------------------------------------------------
880 // ----------------------------------------------------------------------------
881 TCollection_ExtendedString TCollection_ExtendedString::Split
882 (const Standard_Integer where)
884 if (where >= 0 && where < mylength) {
885 TCollection_ExtendedString res(&mystring[where]);
889 Standard_OutOfRange::Raise("TCollection_ExtendedString::Split index");
890 TCollection_ExtendedString res;
894 // ----------------------------------------------------------------------------
896 // ----------------------------------------------------------------------------
897 TCollection_ExtendedString TCollection_ExtendedString::Token
898 (const Standard_ExtString separators,
899 const Standard_Integer whichone) const
901 TCollection_ExtendedString res("");
903 Standard_NullObject::Raise("TCollection_ExtendedString::Token : "
904 "parameter 'separators'");
907 Standard_PExtCharacter buftmp = Allocate((mylength+1)*2);
908 Standard_ExtCharacter aSep;
910 Standard_Boolean isSepFound = Standard_False, otherSepFound;
914 for (i = 0; i < whichone && j < mylength; i++) {
915 isSepFound = Standard_False;
919 // Avant de commencer il faut virer les saloperies devant
921 otherSepFound = Standard_True;
922 while (j < mylength && otherSepFound) {
924 otherSepFound = Standard_False;
925 aSep = separators[l];
927 if (aSep == mystring[j]) {
929 otherSepFound = Standard_True;
932 aSep = separators[l++];
935 if (otherSepFound) j++;
938 while (!isSepFound && k < mylength && j<mylength ) {
940 aSep = separators[l];
942 while (aSep != 0 && !isSepFound) {
943 if (aSep == mystring[j]) {
945 isSepFound = Standard_True;
948 buftmp[k] = mystring[j];
951 aSep = separators[l];
954 if(j==mylength) buftmp[k] = 0;
960 Standard::Free((void*&)buftmp);
963 res.mystring = buftmp;
965 EXTSTRINGLEN( buftmp , res.mylength ) ;
967 EXTSTRLEN(buftmp,res.mylength);
973 // ----------------------------------------------------------------------------
975 // ----------------------------------------------------------------------------
976 const Standard_ExtString TCollection_ExtendedString::ToExtString() const
978 if(mystring) return mystring;
979 return NULL_EXTSTRING;
982 // ----------------------------------------------------------------------------
984 // ----------------------------------------------------------------------------
985 void TCollection_ExtendedString::Trunc(const Standard_Integer ahowmany)
987 if (ahowmany < 0 || ahowmany > mylength)
988 Standard_OutOfRange::Raise("TCollection_ExtendedString::Trunc : "
989 "parameter 'ahowmany'");
991 mystring[mylength] = '\0';
994 // ----------------------------------------------------------------------------
996 // ----------------------------------------------------------------------------
997 Standard_ExtCharacter TCollection_ExtendedString::Value
998 (const Standard_Integer where) const
1000 if (where > 0 && where <= mylength) {
1001 if(mystring) return mystring[where-1];
1004 Standard_OutOfRange::Raise("TCollection_ExtendedString::Value : "
1010 //----------------------------------------------------------------------------
1011 // Convert CString (including multibyte case) to UniCode representation
1012 //----------------------------------------------------------------------------
1013 Standard_Boolean TCollection_ExtendedString::ConvertToUnicode
1014 (const Standard_CString aStr)
1016 Standard_Boolean aRes = Standard_True;
1017 short * p = mystring;
1019 while (aStr[i] != '\0') {
1020 if((aStr[i] & 0x80) == 0x00) //1byte => 1 symb - Lat1
1021 {*p++ = ToExtCharacter(aStr[i]); i++;}
1022 else if((aStr[i] & 0xE0) == 0xC0 &&
1024 (aStr[i+1] & 0xC0) == 0x80)) {//2 bytes => 1 symb
1025 *p++ = ConvertToUnicode2B((unsigned char*)&aStr[i]);
1027 } else if((aStr[i] & 0xF0) == 0xE0 &&
1028 ((aStr[i+1] && (aStr[i+1] & 0xC0) == 0x80)) &&
1029 (aStr[i+2] && (aStr[i+2] & 0xC0) == 0x80)) {
1030 *p++ = ConvertToUnicode3B((unsigned char*)&aStr[i]);
1032 } else { //unsupported case ==> not UTF8
1033 aRes = Standard_False;
1040 //----------------------------------------------------------------------------
1041 // Returns expected CString length in UTF8 coding.
1042 //----------------------------------------------------------------------------
1043 Standard_Integer TCollection_ExtendedString::LengthOfCString() const
1045 Standard_Integer i=0, aLen=0;
1046 while(mystring[i]) {
1047 if((mystring[i] & 0xFF80) == 0)
1049 else if((mystring[i] & 0xF800) == 0)
1058 //----------------------------------------------------------------------------
1059 // Converts the internal <mystring> to UTF8 coding and returns length of the
1061 //----------------------------------------------------------------------------
1062 Standard_Integer TCollection_ExtendedString::ToUTF8CString(Standard_PCharacter& theCString) const
1064 Standard_Integer i=0, j=0;
1065 unsigned char a,b,c;
1066 while(mystring[i]) {
1067 if((mystring[i] & 0xFF80) == 0) {
1068 theCString[j++] = (char)mystring[i];
1070 else if((mystring[i] & 0xF800) == 0) {
1071 b = (unsigned char)mystring[i];//yyzzzzzz
1072 c = (unsigned char)mystring[i];//yyzzzzzz
1073 a = (unsigned char)(mystring[i]>>8);//00000yyy
1077 a |= 0xC0;//110yyy00;
1081 theCString[j++] = a;
1082 theCString[j++] = b;
1084 b = (unsigned char)mystring[i];//yyzzzzzz
1085 c = (unsigned char)mystring[i];//yyzzzzzz
1086 unsigned char d = a = (unsigned char)(mystring[i]>>8);//xxxxyyyy
1088 c |= 0x80;//10zzzzzz
1092 b = (b & 0x3F) | 0x80;//10yyyyyy
1094 a |= 0xE0;//1110xxxx
1095 theCString[j++] = a;
1096 theCString[j++] = b;
1097 theCString[j++] = c;
1101 theCString[j] = 0x00;
1104 //=======================================================================
1105 //function : Allocate
1107 //=======================================================================
1108 Standard_PExtCharacter Allocate(const Standard_Size aLength)
1110 Standard_PExtCharacter pChar;
1112 pChar=(Standard_PExtCharacter)Standard::Allocate(aLength);
1116 //=======================================================================
1117 //function : Reallocate
1119 //=======================================================================
1120 Standard_PExtCharacter Reallocate(Standard_Address aAddr,
1121 const Standard_Size aLength)
1123 Standard_PExtCharacter pChar;
1125 pChar= (Standard_PExtCharacter)Standard::Reallocate(aAddr, aLength);