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))
170 cout <<"UTF8 decoding failure..." <<endl;
176 Standard_NullObject::Raise("TCollection_ExtendedString : "
177 "parameter 'astring'");
181 //---------------------------------------------------------------------------
182 // Create an ExtendedString from an ExtString
183 //--------------------------------------------------------------------------
184 TCollection_ExtendedString::TCollection_ExtendedString
185 (const Standard_ExtString astring)
190 EXTSTRINGLEN( astring , mylength ) ;
192 EXTSTRLEN(astring,mylength);
194 mystring = Allocate(ROUNDMEM((mylength+1)*2));
196 EXTSTRINGCOPY( mystring , astring , mylength );
198 STRCPY(mystring,astring,mylength);
199 mystring[mylength] = '\0';
203 Standard_NullObject::Raise("TCollection_ExtendedString : null parameter ");
207 // ----------------------------------------------------------------------------
208 // Create an asciistring from a Standard_Character
209 // ----------------------------------------------------------------------------
210 TCollection_ExtendedString::TCollection_ExtendedString
211 (const Standard_Character aChar)
213 if ( aChar != '\0' ) {
215 mystring = Allocate(2*2);
216 mystring[0] = ToExtCharacter(aChar);
222 mystring = Allocate((mylength+1)*2);
223 mystring[mylength] = '\0';
227 //--------------------------------------------------------------------------
228 // Create a string from a ExtCharacter
229 // ----------------------------------------------------------------------------
230 TCollection_ExtendedString::TCollection_ExtendedString
231 (const Standard_ExtCharacter aChar)
234 mystring = Allocate(2*2);
239 // ----------------------------------------------------------------------------
240 // Create an AsciiString from a filler
241 // ----------------------------------------------------------------------------
242 TCollection_ExtendedString::TCollection_ExtendedString
243 (const Standard_Integer length,
244 const Standard_ExtCharacter filler )
246 mystring = Allocate((length+1)*2);
248 for (int i = 0 ; i < length ; i++) mystring[i] = filler;
249 mystring[length] = '\0';
252 // ----------------------------------------------------------------------------
253 // Create a String from an Integer
254 // ----------------------------------------------------------------------------
255 TCollection_ExtendedString::TCollection_ExtendedString
256 (const Standard_Integer aValue)
260 Sprintf(&CHN.t[0],"%d",aValue);
262 STRINGLEN( CHN.t , mylength ) ;
264 STRLEN(CHN.t,mylength);
266 mystring = Allocate((mylength+1)*2);
267 for (int i = 0 ; i < mylength ; i++) mystring[i] = ToExtCharacter(CHN.t[i]);
268 mystring[mylength] = '\0';
271 // ----------------------------------------------------------------------------
272 // Create a String from a real
273 // ----------------------------------------------------------------------------
274 TCollection_ExtendedString::TCollection_ExtendedString
275 (const Standard_Real aValue)
279 Sprintf(&CHN.t[0],"%g",aValue);
281 STRINGLEN( CHN.t , mylength ) ;
283 STRLEN(CHN.t,mylength);
285 mystring = Allocate((mylength+1)*2);
286 for (int i = 0 ; i < mylength ; i++) mystring[i] = ToExtCharacter(CHN.t[i]);
287 mystring[mylength] = '\0';
290 //-----------------------------------------------------------------------------
291 // create an extendedstring from an extendedstring
292 // ----------------------------------------------------------------------------
293 TCollection_ExtendedString::TCollection_ExtendedString
294 (const TCollection_ExtendedString& astring)
297 mylength = astring.mylength;
298 mystring = Allocate(ROUNDMEM((mylength+1)*2));
299 if (astring.mystring)
301 EXTENDEDSTRINGCOPY( mystring , astring.mystring , mylength );
303 STRCPY(mystring,astring.mystring,mylength);
304 mystring[mylength] = '\0';
308 //---------------------------------------------------------------------------
309 // Create an extendedstring from an AsciiString
310 //---------------------------------------------------------------------------
311 TCollection_ExtendedString::TCollection_ExtendedString
312 (const TCollection_AsciiString& astring)
314 mylength = astring.Length();
315 mystring = Allocate((mylength+1)*2);
317 Standard_CString aCString = astring.ToCString() ;
318 for (Standard_Integer i = 0; i <= mylength ; i++)
319 mystring[i] = ToExtCharacter( aCString[i] );
321 for (Standard_Integer i = 0; i < mylength ; i++)
322 mystring[i] = ToExtCharacter(astring.Value(i+1));
323 mystring[mylength] = '\0';
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
330 void TCollection_ExtendedString::AssignCat
331 (const TCollection_ExtendedString& other)
333 Standard_Integer otherlength = other.mylength;
335 Standard_ExtString sother = other.mystring;
336 Standard_Integer newlength = mylength +otherlength;
338 mystring = Reallocate((void*&)mystring,
339 ROUNDMEM((newlength+1)*2));
341 // if ( ((long ) ( &mystring[ mylength ] ) & 3) == 0 ) {
342 EXTENDEDSTRINGCAT( mystring , mylength , sother , otherlength ) ;
345 // STRCAT( mystring , mylength , sother , otherlength + 1 ) ;
347 STRCAT(mystring,mylength,sother,otherlength);
351 mystring = Allocate(ROUNDMEM((newlength+1)*2));
353 EXTENDEDSTRINGCOPY( mystring , sother , newlength );
355 STRCPY(mystring,sother,newlength);
358 mylength = newlength;
360 mystring[mylength] = '\0';
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
368 TCollection_ExtendedString TCollection_ExtendedString::Cat
369 (const TCollection_ExtendedString& other) const
371 Standard_ExtString sother = other.mystring;
372 const Standard_Integer otherlength = sother ? other.mylength : 0;
373 const Standard_Integer newlength = mylength + otherlength;
375 // ensure rounding allocated memory to 4 bytes
376 TCollection_ExtendedString res (newlength | 0x1, 0);
377 res.mylength = newlength;
379 TCollection_ExtendedString res (newlength, 0);
384 EXTENDEDSTRINGCOPY( res.mystring , mystring , mylength );
385 // if ( ((long ) ( &res.mystring[ mylength ] ) & 3) == 0 ) {
386 EXTENDEDSTRINGCAT( res.mystring , mylength , sother , otherlength ) ;
389 // STRCAT( res.mystring , mylength , sother , otherlength + 1 ) ;
391 if (mylength > 0) STRCPY (res.mystring, mystring, mylength);
392 STRCPY (&res.mystring[mylength], sother, otherlength);
393 res.mystring[newlength] = '\0';
396 else if (mylength > 0) {
397 // TCollection_ExtendedString res;
398 // res.mystring = (Standard_ExtString)Standard::Allocate((mylength+1)*2);
400 EXTENDEDSTRINGCOPY( res.mystring , mystring , mylength );
402 STRCPY (res.mystring, mystring,mylength);
403 res.mystring[res.mylength] = '\0';
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
412 void TCollection_ExtendedString::ChangeAll(const Standard_ExtCharacter aChar,
413 const Standard_ExtCharacter NewChar)
415 for (int i = 0 ; i < mylength; i++)
416 if (mystring[i] == aChar) mystring[i] = NewChar;
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
422 void TCollection_ExtendedString::Clear()
424 if (mystring) Standard::Free((void*&)mystring);
427 mystring = Allocate((mylength+1)*2);
428 mystring[mylength] = '\0';
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
434 void TCollection_ExtendedString::Copy (const TCollection_ExtendedString& fromwhere)
437 if (fromwhere.mystring) {
438 Standard_Integer newlength = fromwhere.mylength;
440 mystring = Reallocate((void*&)mystring, ROUNDMEM(( newlength + 1)*2 ));
443 mystring = Allocate(ROUNDMEM((newlength+1)*2));
445 mylength = newlength;
447 EXTENDEDSTRINGCOPY( mystring , fromwhere.mystring , newlength );
449 STRCPY(mystring, fromwhere.mystring,newlength);
450 mystring[mylength] = '\0';
456 mystring[mylength] = '\0';
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
464 void TCollection_ExtendedString::Destroy()
466 if (mystring) Standard::Free((void*&)mystring);
470 //----------------------------------------------------------------------------
471 // Insert a character before 'where'th character
472 // ----------------------------------------------------------------------------
473 void TCollection_ExtendedString::Insert(const Standard_Integer where,
474 const Standard_ExtCharacter what)
476 if (where > mylength + 1 )
477 Standard_OutOfRange::Raise("TCollection_ExtendedString::Insert : "
478 "Parameter where is too big");
480 Standard_OutOfRange::Raise("TCollection_ExtendedString::Insert : "
481 "Parameter where is negative");
484 mystring = Reallocate((void*&)mystring,
488 mystring = Allocate((mylength+2)*2);
490 if (where != mylength +1) {
491 for (int i=mylength-1; i >= where-1; i--)
492 mystring[i+1] = mystring[i];
494 mystring[where-1] = what;
496 mystring[mylength] = '\0';
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
502 void TCollection_ExtendedString::Insert(const Standard_Integer where,
503 const TCollection_ExtendedString& what)
505 Standard_ExtString swhat = what.mystring;
506 if (where <= mylength + 1) {
507 Standard_Integer whatlength = what.mylength;
509 Standard_Integer newlength = mylength + whatlength;
512 mystring = Reallocate(
513 (void*&)mystring,(newlength+1)*2);
516 mystring = Allocate((newlength+1)*2);
518 if (where != mylength +1) {
519 for (int i=mylength-1; i >= where-1; i--)
520 mystring[i+whatlength] = mystring[i];
522 for (int i=0; i < whatlength; i++)
523 mystring[where-1+i] = swhat[i];
525 mylength = newlength;
526 mystring[mylength] = '\0';
530 Standard_OutOfRange::Raise("TCollection_ExtendedString::Insert : "
531 "Parameter where is too big");
536 // ----------------------------------------------------------------------------
538 // ----------------------------------------------------------------------------
539 Standard_Boolean TCollection_ExtendedString::IsEqual
540 (const Standard_ExtString other) const
542 // Standard_Integer otherlength ;
544 // EXTSTRINGLEN( other , otherlength )
545 // if ( mylength != otherlength ) return Standard_False;
546 Standard_Boolean KEqual ;
547 LEXTSTRINGEQUAL( mystring , mylength , other , KEqual ) ;
551 Standard_Boolean TCollection_ExtendedString::NoIsEqual
552 (const Standard_ExtString other) const
553 {return Standard_False ;}
556 // ----------------------------------------------------------------------------
558 // ----------------------------------------------------------------------------
559 Standard_Boolean TCollection_ExtendedString::IsEqual
560 (const TCollection_ExtendedString& other) const
562 if (mylength != other.mylength) return Standard_False;
563 Standard_ExtString sother = other.mystring;
565 Standard_Boolean KEqual ;
566 EXTENDEDSTRINGEQUAL( mystring , sother , mylength , KEqual ) ;
569 for (int i = 0 ; i < mylength ; i++)
570 if (mystring[i] != sother[i]) return Standard_False;
571 return Standard_True;
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
579 Standard_Boolean TCollection_ExtendedString::IsDifferent
580 (const Standard_ExtString other ) const
582 // Standard_Integer otherlength ;
584 // EXTSTRINGLEN( other , otherlength )
585 // if ( mylength != otherlength ) return Standard_True;
586 Standard_Boolean KEqual ;
587 LEXTSTRINGEQUAL( mystring , mylength , other , KEqual ) ;
591 Standard_Boolean TCollection_ExtendedString::NoIsDifferent
592 (const Standard_ExtString other ) const
593 {return Standard_False ;}
596 // ----------------------------------------------------------------------------
598 // ----------------------------------------------------------------------------
599 Standard_Boolean TCollection_ExtendedString::IsDifferent
600 (const TCollection_ExtendedString& other) const
602 if (mylength != other.mylength) return Standard_True;
603 Standard_ExtString sother = other.mystring;
605 Standard_Boolean KEqual ;
606 EXTENDEDSTRINGEQUAL( mystring , sother , mylength , KEqual ) ;
609 for (int i = 0 ; i < mylength ; i++)
610 if (mystring[i] != sother[i]) return Standard_True;
611 return Standard_False;
616 // ----------------------------------------------------------------------------
618 // ----------------------------------------------------------------------------
619 Standard_Boolean TCollection_ExtendedString::IsLess
620 (const Standard_ExtString other) const
622 // Standard_Integer otherlength ;
624 // EXTSTRINGLEN( other , otherlength )
625 Standard_Boolean KLess ;
626 // EXTSTRINGLESS( mystring , mylength , other , otherlength ,
627 // INF( mylength , otherlength ) , KLess ) ;
628 LEXTSTRINGLESS( mystring , mylength , other , KLess ) ;
632 Standard_Boolean TCollection_ExtendedString::NoIsLess
633 (const Standard_ExtString other) const
634 {return Standard_False ;}
637 // ----------------------------------------------------------------------------
639 // ----------------------------------------------------------------------------
640 Standard_Boolean TCollection_ExtendedString::IsLess
641 (const TCollection_ExtendedString& other) const
644 Standard_Integer otherlength = other.mylength;
645 Standard_ExtString sother = other.mystring;
647 Standard_Boolean KLess ;
648 EXTENDEDSTRINGLESS( mystring , mylength , sother , otherlength ,
649 INF( mylength , otherlength ) , KLess ) ;
652 Standard_Integer i = 0, j = 0;
653 while ( i < mylength && j < otherlength) {
654 if (mystring[i] < sother[j]) return Standard_True;
655 if (mystring[i] > sother[j]) return Standard_False;
659 if (i == mylength && j < otherlength) return Standard_True;
660 return Standard_False;
665 // ----------------------------------------------------------------------------
667 // ----------------------------------------------------------------------------
668 Standard_Boolean TCollection_ExtendedString::IsGreater
669 (const Standard_ExtString other) const
671 // Standard_Integer otherlength ;
673 // EXTSTRINGLEN( other , otherlength )
674 Standard_Boolean KGreater ;
675 // EXTSTRINGGREATER( mystring , mylength , other , otherlength ,
676 // INF( mylength , otherlength ) , KGreater ) ;
677 LEXTSTRINGGREATER( mystring , mylength , other , KGreater ) ;
681 Standard_Boolean TCollection_ExtendedString::NoIsGreater
682 (const Standard_ExtString other) const
683 {return Standard_False ;}
686 // ----------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------
689 Standard_Boolean TCollection_ExtendedString::IsGreater
690 (const TCollection_ExtendedString& other) const
692 Standard_Integer otherlength = other.mylength;
693 Standard_ExtString sother = other.mystring;
695 Standard_Boolean KGreater ;
696 EXTENDEDSTRINGGREATER( mystring , mylength , sother , otherlength ,
697 INF( mylength , otherlength ) , KGreater ) ;
700 Standard_Integer i = 0, j = 0;
701 while (i < mylength && j <= otherlength) {
702 if (mystring[i] < sother[j]) return Standard_False;
703 if (mystring[i] > sother[j]) return Standard_True;
707 if (j == otherlength && i < mylength) return Standard_True;
708 return Standard_False;
712 // ----------------------------------------------------------------------------
714 // ----------------------------------------------------------------------------
715 Standard_Boolean TCollection_ExtendedString::IsAscii() const
717 for( Standard_Integer i = 0 ; i < mylength ; i++)
718 if (!IsAnAscii(mystring[i])) return Standard_False;
719 return Standard_True;
722 //------------------------------------------------------------------------
724 // ----------------------------------------------------------------------------
725 Standard_Integer TCollection_ExtendedString::Length() const
730 // ----------------------------------------------------------------------------
732 // ----------------------------------------------------------------------------
733 void TCollection_ExtendedString::Print(Standard_OStream& astream) const
735 // ASCII symbols (including extended Ascii) are printed as is;
736 // other Unicode characters are encoded as SGML numeric character references
737 for (Standard_Integer i = 0 ; i < mylength ; i++) {
738 Standard_ExtCharacter c = mystring[i];
740 astream << ToCharacter(c);
742 astream << "&#" << c << ";";
747 // ----------------------------------------------------------------------------
748 Standard_OStream& operator << (Standard_OStream& astream,
749 const TCollection_ExtendedString& astring)
751 astring.Print(astream);
755 // ----------------------------------------------------------------------------
757 // ----------------------------------------------------------------------------
758 void TCollection_ExtendedString::RemoveAll(const Standard_ExtCharacter what)
761 if (mylength == 0) return;
763 for (int i=0; i < mylength; i++)
764 if (mystring[i] != what) mystring[c++] = mystring[i];
766 mystring[mylength] = '\0';
769 // ----------------------------------------------------------------------------
771 // ----------------------------------------------------------------------------
772 void TCollection_ExtendedString::Remove (const Standard_Integer where,
773 const Standard_Integer ahowmany)
775 if (where+ahowmany <= mylength+1) {
777 for (i = where+ahowmany-1, j = where-1; i < mylength; i++, j++)
778 mystring[j] = mystring[i];
779 mylength -= ahowmany;
780 mystring[mylength] = '\0';
783 Standard_OutOfRange::Raise("TCollection_ExtendedString::Remove: "
784 "Too many characters to erase or "
785 "invalid starting value.");
788 // ----------------------------------------------------------------------------
790 // ----------------------------------------------------------------------------
791 Standard_Integer TCollection_ExtendedString::Search
792 (const TCollection_ExtendedString& what) const
794 Standard_Integer size = what.mylength;
795 Standard_ExtString swhat = what.mystring;
799 Standard_Boolean find = Standard_False;
800 while ( i < mylength-size+1 && !find) {
803 while (j < size && mystring[k++] == swhat[j++])
804 if (j == size) find = Standard_True;
811 // ----------------------------------------------------------------------------
813 // ----------------------------------------------------------------------------
814 Standard_Integer TCollection_ExtendedString::SearchFromEnd
815 (const TCollection_ExtendedString& what) const
817 Standard_Integer size = what.mylength;
819 Standard_ExtString swhat = what.mystring;
822 Standard_Boolean find = Standard_False;
823 while ( i >= size-1 && !find) {
826 while (j >= 0 && mystring[k--] == swhat[j--])
827 if (j == -1) find = Standard_True;
829 if (find) return i-size+3;
834 // ----------------------------------------------------------------------------
836 // ----------------------------------------------------------------------------
837 void TCollection_ExtendedString::SetValue(const Standard_Integer where,
838 const Standard_ExtCharacter what)
840 if (where > 0 && where <= mylength) {
841 mystring[where-1] = what;
844 Standard_OutOfRange::Raise("TCollection_ExtendedString::SetValue : parameter where");
848 // ----------------------------------------------------------------------------
850 // ----------------------------------------------------------------------------
851 void TCollection_ExtendedString::SetValue
852 (const Standard_Integer where,
853 const TCollection_ExtendedString& what)
855 if (where > 0 && where <= mylength+1) {
856 Standard_Integer size = what.mylength;
857 Standard_ExtString swhat = what.mystring;
859 if (size >= mylength){
861 mystring = Reallocate ((void*&)mystring,(size+1)*2);
864 mystring = Allocate((size+1)*2);
868 for (int i = where-1; i < size; i++)
869 mystring[i] = swhat[i-(where-1)];
870 mystring[mylength] = '\0';
873 Standard_OutOfRange::Raise("TCollection_ExtendedString::SetValue : "
877 // ----------------------------------------------------------------------------
879 // ----------------------------------------------------------------------------
880 TCollection_ExtendedString TCollection_ExtendedString::Split
881 (const Standard_Integer where)
883 if (where >= 0 && where < mylength) {
884 TCollection_ExtendedString res(&mystring[where]);
888 Standard_OutOfRange::Raise("TCollection_ExtendedString::Split index");
889 TCollection_ExtendedString res;
893 // ----------------------------------------------------------------------------
895 // ----------------------------------------------------------------------------
896 TCollection_ExtendedString TCollection_ExtendedString::Token
897 (const Standard_ExtString separators,
898 const Standard_Integer whichone) const
900 TCollection_ExtendedString res("");
902 Standard_NullObject::Raise("TCollection_ExtendedString::Token : "
903 "parameter 'separators'");
906 Standard_PExtCharacter buftmp = Allocate((mylength+1)*2);
907 Standard_ExtCharacter aSep;
909 Standard_Boolean isSepFound = Standard_False, otherSepFound;
913 for (i = 0; i < whichone && j < mylength; i++) {
914 isSepFound = Standard_False;
918 // Avant de commencer il faut virer les saloperies devant
920 otherSepFound = Standard_True;
921 while (j < mylength && otherSepFound) {
923 otherSepFound = Standard_False;
924 aSep = separators[l];
926 if (aSep == mystring[j]) {
928 otherSepFound = Standard_True;
931 aSep = separators[l++];
934 if (otherSepFound) j++;
937 while (!isSepFound && k < mylength && j<mylength ) {
939 aSep = separators[l];
941 while (aSep != 0 && !isSepFound) {
942 if (aSep == mystring[j]) {
944 isSepFound = Standard_True;
947 buftmp[k] = mystring[j];
950 aSep = separators[l];
953 if(j==mylength) buftmp[k] = 0;
959 Standard::Free((void*&)buftmp);
962 res.mystring = buftmp;
964 EXTSTRINGLEN( buftmp , res.mylength ) ;
966 EXTSTRLEN(buftmp,res.mylength);
972 // ----------------------------------------------------------------------------
974 // ----------------------------------------------------------------------------
975 const Standard_ExtString TCollection_ExtendedString::ToExtString() const
977 if(mystring) return mystring;
978 return NULL_EXTSTRING;
981 // ----------------------------------------------------------------------------
983 // ----------------------------------------------------------------------------
984 void TCollection_ExtendedString::Trunc(const Standard_Integer ahowmany)
986 if (ahowmany < 0 || ahowmany > mylength)
987 Standard_OutOfRange::Raise("TCollection_ExtendedString::Trunc : "
988 "parameter 'ahowmany'");
990 mystring[mylength] = '\0';
993 // ----------------------------------------------------------------------------
995 // ----------------------------------------------------------------------------
996 Standard_ExtCharacter TCollection_ExtendedString::Value
997 (const Standard_Integer where) const
999 if (where > 0 && where <= mylength) {
1000 if(mystring) return mystring[where-1];
1003 Standard_OutOfRange::Raise("TCollection_ExtendedString::Value : "
1009 //----------------------------------------------------------------------------
1010 // Convert CString (including multibyte case) to UniCode representation
1011 //----------------------------------------------------------------------------
1012 Standard_Boolean TCollection_ExtendedString::ConvertToUnicode
1013 (const Standard_CString aStr)
1015 Standard_Boolean aRes = Standard_True;
1016 short * p = mystring;
1018 while (aStr[i] != '\0') {
1019 if((aStr[i] & 0x80) == 0x00) //1byte => 1 symb - Lat1
1020 {*p++ = ToExtCharacter(aStr[i]); i++;}
1021 else if((aStr[i] & 0xE0) == 0xC0 &&
1023 (aStr[i+1] & 0xC0) == 0x80)) {//2 bytes => 1 symb
1024 *p++ = ConvertToUnicode2B((unsigned char*)&aStr[i]);
1026 } else if((aStr[i] & 0xF0) == 0xE0 &&
1027 ((aStr[i+1] && (aStr[i+1] & 0xC0) == 0x80)) &&
1028 (aStr[i+2] && (aStr[i+2] & 0xC0) == 0x80)) {
1029 *p++ = ConvertToUnicode3B((unsigned char*)&aStr[i]);
1031 } else { //unsupported case ==> not UTF8
1032 aRes = Standard_False;
1039 //----------------------------------------------------------------------------
1040 // Returns expected CString length in UTF8 coding.
1041 //----------------------------------------------------------------------------
1042 Standard_Integer TCollection_ExtendedString::LengthOfCString() const
1044 Standard_Integer i=0, aLen=0;
1045 while(mystring[i]) {
1046 if((mystring[i] & 0xFF80) == 0)
1048 else if((mystring[i] & 0xF800) == 0)
1057 //----------------------------------------------------------------------------
1058 // Converts the internal <mystring> to UTF8 coding and returns length of the
1060 //----------------------------------------------------------------------------
1061 Standard_Integer TCollection_ExtendedString::ToUTF8CString(Standard_PCharacter& theCString) const
1063 Standard_Integer i=0, j=0;
1064 unsigned char a,b,c;
1065 while(mystring[i]) {
1066 if((mystring[i] & 0xFF80) == 0) {
1067 theCString[j++] = (char)mystring[i];
1069 else if((mystring[i] & 0xF800) == 0) {
1070 b = (unsigned char)mystring[i];//yyzzzzzz
1071 c = (unsigned char)mystring[i];//yyzzzzzz
1072 a = (unsigned char)(mystring[i]>>8);//00000yyy
1076 a |= 0xC0;//110yyy00;
1080 theCString[j++] = a;
1081 theCString[j++] = b;
1083 b = (unsigned char)mystring[i];//yyzzzzzz
1084 c = (unsigned char)mystring[i];//yyzzzzzz
1085 unsigned char d = a = (unsigned char)(mystring[i]>>8);//xxxxyyyy
1087 c |= 0x80;//10zzzzzz
1091 b = (b & 0x3F) | 0x80;//10yyyyyy
1093 a |= 0xE0;//1110xxxx
1094 theCString[j++] = a;
1095 theCString[j++] = b;
1096 theCString[j++] = c;
1100 theCString[j] = 0x00;
1103 //=======================================================================
1104 //function : Allocate
1106 //=======================================================================
1107 Standard_PExtCharacter Allocate(const Standard_Size aLength)
1109 Standard_PExtCharacter pChar;
1111 pChar=(Standard_PExtCharacter)Standard::Allocate(aLength);
1115 //=======================================================================
1116 //function : Reallocate
1118 //=======================================================================
1119 Standard_PExtCharacter Reallocate(Standard_Address aAddr,
1120 const Standard_Size aLength)
1122 Standard_PExtCharacter pChar;
1124 pChar= (Standard_PExtCharacter)Standard::Reallocate(aAddr, aLength);