1 // Copyright (c) 1998-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.
20 #include <PCollection_HAsciiString.ixx>
21 #include <PCollection_HExtendedString.hxx>
22 #include <TCollection_HAsciiString.hxx>
23 #include <Standard_NumericError.hxx>
24 #include <Standard_NegativeValue.hxx>
25 #include <Standard_OutOfRange.hxx>
30 #if defined(HAVE_STRING_H)
34 #if defined(HAVE_STDLIB_H)
37 #if defined(HAVE_LIBC_H)
41 //------------------------------------
42 // Conversion functions and variables
43 //------------------------------------
46 static char cnvbuf[MAXLENGTH];
47 static Standard_Integer cnvint;
48 static Standard_Real cnvreal;
50 //-----------------------------------------------------------------------
52 //-----------------------------------------------------------------------
53 static int intstr(Standard_Integer V,Standard_CString F)
56 return strlen(cnvbuf);
59 //-----------------------------------------------------------------------
61 //----------------------------------------------------------------------
62 static int realstr(Standard_Real V, Standard_CString F)
65 return strlen(cnvbuf);
68 //-----------------------------------------------------------------------
69 // Create : from a CString
70 //-----------------------------------------------------------------------
71 PCollection_HAsciiString::PCollection_HAsciiString(const Standard_CString S)
72 : Data((Standard_Integer) strlen(S))
74 for( Standard_Integer i = 0 ; i < Data.Length() ; i++)
75 Data.SetValue(i, S[i]) ;
78 //------------------------------------------------------------------------
79 // Create from an AsciiString of TCollection
80 //------------------------------------------------------------------------
81 PCollection_HAsciiString::PCollection_HAsciiString
82 (const TCollection_AsciiString& S):Data(S.Length())
84 for( Standard_Integer i = 1; i <= Data.Length() ; i++)
85 Data.SetValue(i-1, S.Value(i)) ;
88 //------------------------------------------------------------------------
89 // Create from a Character
90 //------------------------------------------------------------------------
91 PCollection_HAsciiString::PCollection_HAsciiString(const Standard_Character C)
97 //------------------------------------------------------------------------
98 // Create from a range of an HAsciiString of PCollection
99 //------------------------------------------------------------------------
100 PCollection_HAsciiString::PCollection_HAsciiString
101 (const Handle(PCollection_HAsciiString)& S,
102 const Standard_Integer FromIndex, const Standard_Integer ToIndex) : Data(ToIndex-FromIndex+1)
104 for( Standard_Integer i = 0 , k = FromIndex; i < Data.Length() ; i++, k++)
105 Data.SetValue(i, S->Value(k));
108 //------------------------------------------------------------------------
109 // Create from an HExtendedString from PCollection
110 //------------------------------------------------------------------------
111 PCollection_HAsciiString::PCollection_HAsciiString
112 (const Handle(PCollection_HExtendedString)& S) : Data(S->Length())
114 if (!S->IsAscii()) Standard_OutOfRange::Raise();
115 for( Standard_Integer i = 1; i <= Data.Length() ; i++) {
116 // convert the character i of S
117 Standard_Character val = ToCharacter(S->Value(i)) ;
118 Data.SetValue(i-1,val ) ;
122 //------------------------------------------------------------------------
123 // Create from a Real by converting it
124 //------------------------------------------------------------------------
125 PCollection_HAsciiString::PCollection_HAsciiString
126 (const Standard_Real R , const Standard_CString F) : Data(realstr(R,F))
128 for( Standard_Integer i =0 ; i < Data.Length() ; i++)
129 Data.SetValue(i,cnvbuf[i] );
132 //------------------------------------------------------------------------
133 // Create from an Integer by converting it
134 //------------------------------------------------------------------------
135 PCollection_HAsciiString::PCollection_HAsciiString
136 (const Standard_Integer I, const Standard_CString F) : Data(intstr(I,F))
138 for( Standard_Integer i = 0 ; i < Data.Length() ; i++)
139 Data.SetValue(i,cnvbuf[i]) ;
143 //------------------------------------------------------------------------
145 //------------------------------------------------------------------------
146 void PCollection_HAsciiString::Append
147 (const Handle(PCollection_HAsciiString)& S)
149 InsertAfter(Length(),S);
152 //------------------------------------------------------------------------
154 //------------------------------------------------------------------------
155 void PCollection_HAsciiString::Capitalize ()
157 for (Standard_Integer i = 0 ; i < Length() ; i++) {
158 if( i == 0) Data.SetValue(i, UpperCase(Data(i)) );
159 else Data.SetValue(i, LowerCase(Data(i)) );
163 //------------------------------------------------------------------------
165 //------------------------------------------------------------------------
166 void PCollection_HAsciiString::Center
167 (const Standard_Integer Width, const Standard_Character Filler)
169 if (Width < 0) Standard_NegativeValue::Raise();
170 Standard_Integer size1 = Length();
172 Standard_Integer size2 = size1 + ((Width - size1)/2);
173 LeftJustify(size2,Filler);
174 RightJustify(Width,Filler);
178 //------------------------------------------------------------------------
180 //------------------------------------------------------------------------
181 void PCollection_HAsciiString::ChangeAll
182 (const Standard_Character C, const Standard_Character NewC, const Standard_Boolean CaseSensitive)
184 for( Standard_Integer i = 0 ; i < Data.Length(); i++) {
186 if(Data(i) == C) Data.SetValue(i, NewC);
189 if(UpperCase(Data(i)) == UpperCase(C)) Data.SetValue(i, NewC);
194 //------------------------------------------------------------------------
196 //------------------------------------------------------------------------
197 void PCollection_HAsciiString::Clear ()
202 //------------------------------------------------------------------------
204 //------------------------------------------------------------------------
205 TCollection_AsciiString PCollection_HAsciiString::Convert() const
207 Standard_Integer L = Length();
208 TCollection_AsciiString TString (L,' ');
209 for (Standard_Integer i = 1 ; i <= L ; i++) {
210 TString.SetValue(i,Value(i));
215 //------------------------------------------------------------------------
216 // FirstLocationInSet
217 //------------------------------------------------------------------------
218 Standard_Integer PCollection_HAsciiString::FirstLocationInSet
219 (const Handle(PCollection_HAsciiString)& Set,
220 const Standard_Integer FromIndex,
221 const Standard_Integer ToIndex) const
223 if (Length() == 0 || Set->Length() == 0) return 0;
224 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
225 Standard_OutOfRange::Raise();
226 for(Standard_Integer i = FromIndex-1 ; i <= ToIndex-1; i++)
227 for(Standard_Integer j = 1; j <= Set->Length(); j++)
228 if(Data(i) == Set->Value(j)) return (i+1);
232 //------------------------------------------------------------------------
233 // FirstLocationNotInset
234 //------------------------------------------------------------------------
235 Standard_Integer PCollection_HAsciiString::FirstLocationNotInSet
236 (const Handle(PCollection_HAsciiString)& Set, const Standard_Integer FromIndex,
237 const Standard_Integer ToIndex) const
239 if (Length() == 0 || Set->Length() == 0) return 0;
240 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
241 Standard_OutOfRange::Raise();
242 Standard_Boolean find;
243 for(Standard_Integer i = FromIndex-1 ; i <= ToIndex-1; i++) {
244 find = Standard_False;
245 for(Standard_Integer j = 1; j <= Set->Length(); j++) {
246 if (Data(i) == Set->Value(j)) find = Standard_True;
248 if (!find) return (i+1);
253 //------------------------------------------------------------------------
255 //------------------------------------------------------------------------
256 void PCollection_HAsciiString::InsertAfter
257 (const Standard_Integer Index, const Handle(PCollection_HAsciiString)& S)
260 Standard_Integer size1 = Length();
261 Standard_Integer size2 = S->Length();
263 if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
265 Data.Resize(size1+size2);
266 for( i = size1-1 ; i >= Index; i--) Data.SetValue(size2+i,Data(i));
267 for( i = 1 ; i <= size2; i++) Data.SetValue(Index+i-1,S->Value(i));
270 //------------------------------------------------------------------------
272 //------------------------------------------------------------------------
273 void PCollection_HAsciiString::InsertBefore
274 (const Standard_Integer Index, const Handle(PCollection_HAsciiString)& S)
277 Standard_Integer size1 = Length();
278 Standard_Integer size2 = S->Length();
280 if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
282 Data.Resize(size1+size2);
283 for( i = size1-1 ; i >= Index-1 ; i--)
284 Data.SetValue(size2+i,Data(i));
285 for( i = 1 ; i <= size2; i++) Data.SetValue(Index+i-2, S->Value(i));
288 //------------------------------------------------------------------------
290 //------------------------------------------------------------------------
291 Standard_Integer PCollection_HAsciiString::IntegerValue () const
293 if (!IsIntegerValue()) Standard_NumericError::Raise();
297 //------------------------------------------------------------------------
299 //------------------------------------------------------------------------
300 Standard_Boolean PCollection_HAsciiString::IsDifferent
301 (const Handle(PCollection_HAsciiString)& S) const
303 Standard_Integer size = Length();
304 if( size != S->Length()) return Standard_True;
305 Standard_Integer i = 1 ;
306 Standard_Boolean different = Standard_False;
307 while (i <= size && !different) {
308 if (Data(i-1) != S->Value(i)) different = Standard_True;
314 //------------------------------------------------------------------------
316 //------------------------------------------------------------------------
317 Standard_Boolean PCollection_HAsciiString::IsEmpty () const
319 return (Data.Length() == 0);
322 //------------------------------------------------------------------------
324 //------------------------------------------------------------------------
325 Standard_Boolean PCollection_HAsciiString::IsGreater
326 (const Handle(PCollection_HAsciiString)& S) const
328 TCollection_AsciiString TMe = Convert();
329 TCollection_AsciiString TS = S->Convert();
330 return TMe.IsGreater(TS);
333 //------------------------------------------------------------------------
335 //------------------------------------------------------------------------
336 Standard_Boolean PCollection_HAsciiString::IsIntegerValue () const
339 #if defined(__osf__) || defined(DECOSF1)
341 #pragma pointer_size (save)
342 #pragma pointer_size (long)
344 #pragma pointer_size (restore)
352 if ( Data.Length() > MAXLENGTH ) return Standard_False;
354 Handle(TCollection_HAsciiString) astring;
355 astring = new TCollection_HAsciiString (this->Convert());
356 astring->LeftAdjust();
357 astring->RightAdjust();
358 for(i = 0; i < astring->Length(); i++)
359 cnvbuf[i] = astring->Value(i+1);
361 cnvint = strtol(cnvbuf,&ptr,10);
362 if (ptr < cnvbuf+astring->Length()) return Standard_False;
363 else return Standard_True;
366 //------------------------------------------------------------------------
368 //------------------------------------------------------------------------
369 Standard_Boolean PCollection_HAsciiString::IsLess
370 (const Handle(PCollection_HAsciiString)& S) const
372 TCollection_AsciiString TMe = Convert();
373 TCollection_AsciiString TS = S->Convert();
374 return TMe.IsLess(TS);
377 //------------------------------------------------------------------------
379 //------------------------------------------------------------------------
380 Standard_Boolean PCollection_HAsciiString::IsRealValue () const
383 #if defined(__osf__) || defined(DECOSF1)
385 #pragma pointer_size (save)
386 #pragma pointer_size (long)
388 #pragma pointer_size (restore)
396 if ( Data.Length() > MAXLENGTH ) return Standard_False;
398 Handle(TCollection_HAsciiString) astring;
399 astring = new TCollection_HAsciiString (this->Convert());
400 astring->LeftAdjust();
401 astring->RightAdjust();
402 for(i = 0; i < astring->Length(); i++)
403 cnvbuf[i] = astring->Value(i+1);
405 cnvreal = strtod(cnvbuf,&ptr);
406 if (ptr < cnvbuf+astring->Length()) return Standard_False;
407 else return Standard_True;
410 //------------------------------------------------------------------------
412 //------------------------------------------------------------------------
413 Standard_Boolean PCollection_HAsciiString::IsSameString
414 (const Handle(PCollection_HAsciiString)& S) const
417 Standard_Integer size1 = Length();
418 if( size1 != S->Length()) return Standard_False;
419 for( i = 1 ; i <= size1; i++) {
420 if(Data(i-1) != S->Value(i)) return Standard_False;
422 return Standard_True;
425 //------------------------------------------------------------------------
427 //------------------------------------------------------------------------
428 Standard_Boolean PCollection_HAsciiString::IsSameString
429 (const Handle(PCollection_HAsciiString)& S, const Standard_Boolean CaseSensitive) const
431 Standard_Integer size1 = Length();
432 if( size1 != S->Length()) return Standard_False;
433 for( Standard_Integer i = 1 ; i <= size1; i++) {
435 if(Data(i-1) != S->Value(i)) return Standard_False;
438 if(UpperCase(Data(i-1)) != UpperCase(S->Value(i))) return Standard_False;
441 return Standard_True;
444 //------------------------------------------------------------------------
446 //------------------------------------------------------------------------
447 void PCollection_HAsciiString::LeftAdjust ()
450 for(i = 0 ; i < Data.Length() ; i ++) if(!IsSpace(Data(i))) break;
451 if( i > 0 ) Remove(1,i);
454 //------------------------------------------------------------------------
456 //------------------------------------------------------------------------
457 void PCollection_HAsciiString::LeftJustify
458 (const Standard_Integer Width, const Standard_Character Filler)
460 if (Width < 0) Standard_NegativeValue::Raise();
461 Standard_Integer size1 = Length();
464 for(Standard_Integer i = size1; i < Width ; i++) Data.SetValue(i, Filler);
468 //------------------------------------------------------------------------
470 //------------------------------------------------------------------------
471 Standard_Integer PCollection_HAsciiString::Length () const
473 return Data.Length();
476 //------------------------------------------------------------------------
478 //------------------------------------------------------------------------
479 Standard_Integer PCollection_HAsciiString::Location
480 (const Standard_Integer N, const Standard_Character C,
481 const Standard_Integer FromIndex, const Standard_Integer ToIndex) const
483 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
484 Standard_OutOfRange::Raise();
485 for(Standard_Integer i = FromIndex-1, count = 0; i <= ToIndex-1; i++)
488 if ( count == N ) return (i+1);
493 //------------------------------------------------------------------------
495 //------------------------------------------------------------------------
496 Standard_Integer PCollection_HAsciiString::Location
497 (const Handle(PCollection_HAsciiString)& S, const Standard_Integer FromIndex,
498 const Standard_Integer ToIndex) const
500 if (Length() == 0 || S->Length() == 0) return 0;
501 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
502 Standard_OutOfRange::Raise();
503 for(Standard_Integer i = FromIndex-1, k = 1, l = FromIndex-2; i < ToIndex; i++){
504 if(Data(i) == S->Value(k)) {
506 if ( k > S->Length()) return l + 2;
516 //------------------------------------------------------------------------
518 //------------------------------------------------------------------------
519 void PCollection_HAsciiString::Lowercase ()
521 for( Standard_Integer i = 0 ; i < Data.Length() ; i++) {
522 Data.SetValue(i, LowerCase(Data(i)));
526 //------------------------------------------------------------------------
528 //------------------------------------------------------------------------
529 void PCollection_HAsciiString::Prepend
530 (const Handle(PCollection_HAsciiString)& S)
536 //------------------------------------------------------------------------
538 //------------------------------------------------------------------------
539 void PCollection_HAsciiString::Print (Standard_OStream& S) const
541 Standard_Integer len = Data.Length() ;
542 for(Standard_Integer i = 0; i < len ; i++) {
547 //------------------------------------------------------------------------
549 //------------------------------------------------------------------------
550 Standard_Real PCollection_HAsciiString::RealValue () const
552 if(!IsRealValue()) Standard_NumericError::Raise();
556 //------------------------------------------------------------------------
558 //------------------------------------------------------------------------
559 void PCollection_HAsciiString::Remove (const Standard_Integer Index)
561 if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
565 //------------------------------------------------------------------------
567 //------------------------------------------------------------------------
568 void PCollection_HAsciiString::Remove
569 (const Standard_Integer FromIndex, const Standard_Integer ToIndex)
571 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
572 Standard_OutOfRange::Raise();
573 Standard_Integer size1 = Length();
574 for( Standard_Integer i = ToIndex, j = FromIndex-1; i < size1 ; i++, j++)
575 Data.SetValue(j,Data(i));
576 Data.Resize(size1-(ToIndex-FromIndex+1));
579 //------------------------------------------------------------------------
581 //------------------------------------------------------------------------
582 void PCollection_HAsciiString::RemoveAll
583 (const Standard_Character C, const Standard_Boolean CaseSensitive)
587 Standard_Integer size1 = Length();
588 for( i = 0, j = 0; i < size1 ; i++){
590 if(Data(i) == C) continue;
593 if(UpperCase(Data(i)) == UpperCase(C)) continue;
595 Data.SetValue(j++, Data(i));
600 //------------------------------------------------------------------------
602 //------------------------------------------------------------------------
603 void PCollection_HAsciiString::RightAdjust ()
606 for( i = Data.Length()-1 ; i >= 0 ; i--) if(!IsSpace(Data(i))) break;
607 if( i < Data.Length()-1 ) Remove(i+2,Data.Length());
610 //------------------------------------------------------------------------
612 //------------------------------------------------------------------------
613 void PCollection_HAsciiString::RightJustify
614 (const Standard_Integer Width, const Standard_Character Filler)
618 if (Width < 0) Standard_NegativeValue::Raise();
619 Standard_Integer size1 = Length();
622 for ( i = size1-1, k = Width-1 ; i >= 0 ; i--, k--)
623 Data.SetValue(k, Data(i));
624 for(; k >= 0 ; k--) Data.SetValue(k, Filler);
628 //------------------------------------------------------------------------
630 //------------------------------------------------------------------------
631 void PCollection_HAsciiString::SetValue
632 (const Standard_Integer Index, const Handle(PCollection_HAsciiString)& S)
635 Standard_Integer size1 = Length();
636 Standard_Integer size2 = S->Length();
637 Standard_Integer size3 = size2 + Index - 1;
639 if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
641 if(size1 != size3) Data.Resize(size3);
642 for( Standard_Integer i = 1 ; i <= size2; i++) Data.SetValue(Index+i-2, S->Value(i));
645 //------------------------------------------------------------------------
647 //------------------------------------------------------------------------
648 void PCollection_HAsciiString::SetValue
649 (const Standard_Integer Index, const Standard_Character C)
651 if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
655 //------------------------------------------------------------------------
657 //------------------------------------------------------------------------
658 Handle(PCollection_HAsciiString) PCollection_HAsciiString::Split
659 (const Standard_Integer Index)
661 if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
662 Handle(PCollection_HAsciiString) S2;
663 if (Index != Length()) {
664 S2 = SubString(Index+1,Length());
669 S2 = new PCollection_HAsciiString("");
671 S2 = new (os_segment::of(this)) PCollection_HAsciiString("");
677 //------------------------------------------------------------------------
679 //------------------------------------------------------------------------
680 Handle(PCollection_HAsciiString) PCollection_HAsciiString::SubString
681 (const Standard_Integer FromIndex, const Standard_Integer ToIndex) const
683 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
684 Standard_OutOfRange::Raise();
685 Handle(PCollection_HAsciiString) S1;
686 Handle(PCollection_HAsciiString) S2;
689 S1 = new PCollection_HAsciiString(S2,FromIndex,ToIndex);
691 S1 = new (os_segment::of(this)) PCollection_HAsciiString(S2,FromIndex,ToIndex);
697 //------------------------------------------------------------------------
699 //------------------------------------------------------------------------
700 Handle(PCollection_HAsciiString) PCollection_HAsciiString::Token
701 (const Standard_CString separators ,
702 const Standard_Integer whichone) const
704 TCollection_AsciiString TMe = Convert();
705 Handle(PCollection_HAsciiString)
707 TheToken = new PCollection_HAsciiString(TMe.Token(separators,whichone));
709 TheToken = new (os_segment::of(this)) PCollection_HAsciiString(TMe.Token(separators,whichone));
714 //------------------------------------------------------------------------
716 //------------------------------------------------------------------------
717 void PCollection_HAsciiString::Uppercase ()
719 for( Standard_Integer i = 0 ; i < Data.Length() ; i++)
720 Data.SetValue(i, UpperCase(Data(i)));
723 //------------------------------------------------------------------------
725 //------------------------------------------------------------------------
726 Standard_Integer PCollection_HAsciiString::UsefullLength () const
729 for( i = Data.Length() -1 ; i >= 0 ; i--)
730 if(IsGraphic(Data(i))) break;
734 //------------------------------------------------------------------------
736 //------------------------------------------------------------------------
737 Standard_Character PCollection_HAsciiString::Value (const Standard_Integer Index) const
739 if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
740 return Data(Index-1);
745 //------------------------------------------------------------------------
747 //------------------------------------------------------------------------
748 void PCollection_HAsciiString::Assign(const DBC_VArrayOfCharacter& TheField)
754 //------------------------------------------------------------------------
756 //------------------------------------------------------------------------
757 void PCollection_HAsciiString::ShallowDump(Standard_OStream& S) const
759 S << "begin class HAsciiString " << endl;
760 ::ShallowDump(Data, S);
761 S << "end class HAsciiString" << endl;