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_HExtendedString.ixx>
21 #include <PCollection_HAsciiString.hxx>
22 #include <Standard_ExtString.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 // Create from an ExtendedString of TCollection
43 //------------------------------------------------------------------------
44 PCollection_HExtendedString::PCollection_HExtendedString
45 (const TCollection_ExtendedString& S):Data(S.Length())
47 for( Standard_Integer i = 1; i <= Data.Length() ; i++)
48 Data.SetValue(i-1, S.Value(i)) ;
51 //------------------------------------------------------------------------
52 // Create from a ExtCharacter
53 //------------------------------------------------------------------------
54 PCollection_HExtendedString::PCollection_HExtendedString
55 (const Standard_ExtCharacter C): Data(1)
60 //------------------------------------------------------------------------
61 // Create from a range of an HExtendedString of PCollection
62 //------------------------------------------------------------------------
63 PCollection_HExtendedString::PCollection_HExtendedString
64 (const Handle(PCollection_HExtendedString)& S,
65 const Standard_Integer FromIndex, const Standard_Integer ToIndex) : Data(ToIndex-FromIndex+1)
67 for( Standard_Integer i = 0 , k = FromIndex; i < Data.Length() ; i++, k++)
68 Data.SetValue(i, S->Value(k));
71 //-----------------------------------------------------------------------
72 // Create : from a CString
73 //-----------------------------------------------------------------------
74 PCollection_HExtendedString::PCollection_HExtendedString(const Standard_CString S)
77 for( Standard_Integer i = 0 ; i < Data.Length() ; i++) {
78 Standard_ExtCharacter val = ToExtCharacter(S[i]);
79 Data.SetValue(i, val) ;
83 //------------------------------------------------------------------------
84 // Create from an HAsciiString from PCollection
85 //------------------------------------------------------------------------
86 PCollection_HExtendedString::PCollection_HExtendedString
87 (const Handle(PCollection_HAsciiString)& S) : Data(S->Length())
89 for( Standard_Integer i = 1; i <= Data.Length() ; i++) {
90 // convert the character i of S
91 Standard_ExtCharacter val = ToExtCharacter(S->Value(i)) ;
92 Data.SetValue(i-1,val );
96 //------------------------------------------------------------------------
98 //------------------------------------------------------------------------
99 void PCollection_HExtendedString::Append
100 (const Handle(PCollection_HExtendedString)& S)
102 InsertAfter(Length(),S);
105 //------------------------------------------------------------------------
107 //------------------------------------------------------------------------
108 void PCollection_HExtendedString::Center
109 (const Standard_Integer Width, const Standard_ExtCharacter Filler)
111 if (Width < 0) Standard_NegativeValue::Raise();
112 Standard_Integer size1 = Length();
114 Standard_Integer size2 = size1 + ((Width - size1)/2);
115 LeftJustify(size2,Filler);
116 RightJustify(Width,Filler);
120 //------------------------------------------------------------------------
122 //------------------------------------------------------------------------
123 void PCollection_HExtendedString::ChangeAll
124 (const Standard_ExtCharacter C, const Standard_ExtCharacter NewC)
126 for( Standard_Integer i = 0 ; i < Data.Length(); i++) {
127 if(Data(i) == C) Data.SetValue(i, NewC);
131 //------------------------------------------------------------------------
133 //------------------------------------------------------------------------
134 void PCollection_HExtendedString::Clear ()
139 //------------------------------------------------------------------------
141 //------------------------------------------------------------------------
142 TCollection_ExtendedString PCollection_HExtendedString::Convert() const
144 Standard_Integer L = Length();
145 TCollection_ExtendedString TString (L,' ');
146 for (Standard_Integer i = 1 ; i <= L ; i++) {
147 TString.SetValue(i,Value(i));
152 //------------------------------------------------------------------------
153 // FirstLocationInSet
154 //------------------------------------------------------------------------
155 Standard_Integer PCollection_HExtendedString::FirstLocationInSet
156 (const Handle(PCollection_HExtendedString)& Set,
157 const Standard_Integer FromIndex,
158 const Standard_Integer ToIndex) const
160 if (Length() == 0 || Set->Length() == 0) return 0;
161 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
162 Standard_OutOfRange::Raise();
163 for(Standard_Integer i = FromIndex-1 ; i <= ToIndex-1; i++)
164 for(Standard_Integer j = 1; j <= Set->Length(); j++)
165 if(Data(i) == Set->Value(j)) return (i+1);
169 //------------------------------------------------------------------------
170 // FirstLocationNotInset
171 //------------------------------------------------------------------------
172 Standard_Integer PCollection_HExtendedString::FirstLocationNotInSet
173 (const Handle(PCollection_HExtendedString)& Set, const Standard_Integer FromIndex,
174 const Standard_Integer ToIndex) const
176 if (Length() == 0 || Set->Length() == 0) return 0;
177 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
178 Standard_OutOfRange::Raise();
179 Standard_Boolean find;
180 for(Standard_Integer i = FromIndex-1 ; i <= ToIndex-1; i++) {
181 find = Standard_False;
182 for(Standard_Integer j = 1; j <= Set->Length(); j++) {
183 if (Data(i) == Set->Value(j)) find = Standard_True;
185 if (!find) return (i+1);
190 //------------------------------------------------------------------------
192 //------------------------------------------------------------------------
193 void PCollection_HExtendedString::InsertAfter
194 (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
197 Standard_Integer size1 = Length();
198 Standard_Integer size2 = S->Length();
200 if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
202 Data.Resize(size1+size2);
203 for( i = size1-1 ; i >= Index; i--) Data.SetValue(size2+i,Data(i));
204 for( i = 1 ; i <= size2; i++) Data.SetValue(Index+i-1,S->Value(i));
207 //------------------------------------------------------------------------
209 //------------------------------------------------------------------------
210 void PCollection_HExtendedString::InsertBefore
211 (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
214 Standard_Integer size1 = Length();
215 Standard_Integer size2 = S->Length();
217 if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
219 Data.Resize(size1+size2);
220 for( i = size1-1 ; i >= Index-1 ; i--)
221 Data.SetValue(size2+i,Data(i));
222 for( i = 1 ; i <= size2; i++) Data.SetValue(Index+i-2, S->Value(i));
225 //------------------------------------------------------------------------
227 //------------------------------------------------------------------------
228 Standard_Boolean PCollection_HExtendedString::IsAscii() const
230 for( Standard_Integer i = 0; i < Data.Length() ; i++) {
231 if (!IsAnAscii(Data(i))) return Standard_False;
233 return Standard_True;
236 //------------------------------------------------------------------------
238 //------------------------------------------------------------------------
239 Standard_Boolean PCollection_HExtendedString::IsDifferent
240 (const Handle(PCollection_HExtendedString)& S) const
242 Standard_Integer size = Length();
243 if( size != S->Length()) return Standard_True;
244 Standard_Integer i = 1 ;
245 Standard_Boolean different = Standard_False;
246 while (i <= size && !different) {
247 if (Data(i-1) != S->Value(i)) different = Standard_True;
253 //------------------------------------------------------------------------
255 //------------------------------------------------------------------------
256 Standard_Boolean PCollection_HExtendedString::IsEmpty () const
258 return (Data.Length() == 0);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
264 Standard_Boolean PCollection_HExtendedString::IsLess(
265 const Handle(PCollection_HExtendedString)& other) const
267 Standard_Integer mysize = Data.Length();
268 Standard_Integer size = other->Length();
269 Standard_Integer i = 0;
270 Standard_Integer j = 1;
271 while (i < mysize && j <= size) {
272 if (Data(i) < other->Value(j)) return (Standard_True);
273 if (Data(i) > other->Value(j)) return (Standard_False);
277 if (i == mysize && j <= size) return (Standard_True);
278 return (Standard_False);
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
284 Standard_Boolean PCollection_HExtendedString::IsGreater
285 (const Handle(PCollection_HExtendedString)& other) const
287 Standard_Integer mysize = Data.Length();
288 Standard_Integer size = other->Length();
289 Standard_Integer i = 0;
290 Standard_Integer j = 1;
291 while (i < mysize && j <= size) {
292 if (Data(i) < other->Value(j)) return (Standard_False);
293 if (Data(i) > other->Value(j)) return (Standard_True);
297 if (j == size && j < mysize) return (Standard_True);
298 return (Standard_False);
301 //------------------------------------------------------------------------
303 //------------------------------------------------------------------------
304 Standard_Boolean PCollection_HExtendedString::IsSameString
305 (const Handle(PCollection_HExtendedString)& S) const
307 Standard_Integer size1 = Length();
308 if( size1 != S->Length()) return Standard_False;
309 for( Standard_Integer i = 1 ; i <= size1; i++) {
310 if(Data(i-1) != S->Value(i)) return Standard_False;
312 return Standard_True;
315 //------------------------------------------------------------------------
317 //------------------------------------------------------------------------
318 void PCollection_HExtendedString::LeftAdjust ()
322 if (!IsAscii()) Standard_OutOfRange::Raise();
323 for ( i = 1 ; i <= Length() ; i ++) {
324 if (!IsSpace((Standard_Character)Value(i))) break;
326 if( i > 1 ) Remove(1,i-1);
329 //------------------------------------------------------------------------
331 //------------------------------------------------------------------------
332 void PCollection_HExtendedString::LeftJustify
333 (const Standard_Integer Width, const Standard_ExtCharacter Filler)
335 if (Width < 0) Standard_NegativeValue::Raise();
336 Standard_Integer size1 = Length();
339 for(Standard_Integer i = size1; i < Width ; i++) Data.SetValue(i, Filler);
343 //------------------------------------------------------------------------
345 //------------------------------------------------------------------------
346 Standard_Integer PCollection_HExtendedString::Length () const
348 return Data.Length();
351 //------------------------------------------------------------------------
353 //------------------------------------------------------------------------
354 Standard_Integer PCollection_HExtendedString::Location
355 (const Standard_Integer N, const Standard_ExtCharacter C,
356 const Standard_Integer FromIndex, const Standard_Integer ToIndex) const
358 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
359 Standard_OutOfRange::Raise();
360 for(Standard_Integer i = FromIndex-1, count = 0; i <= ToIndex-1; i++)
363 if ( count == N ) return (i+1);
368 //------------------------------------------------------------------------
370 //------------------------------------------------------------------------
371 Standard_Integer PCollection_HExtendedString::Location
372 (const Handle(PCollection_HExtendedString)& S, const Standard_Integer FromIndex,
373 const Standard_Integer ToIndex) const
375 if (Length() == 0 || S->Length() == 0) return 0;
376 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
377 Standard_OutOfRange::Raise();
378 for(Standard_Integer i = FromIndex-1, k = 1, l = FromIndex-2; i < ToIndex; i++){
379 if(Data(i) == S->Value(k)) {
381 if ( k > S->Length()) return l + 2;
391 //------------------------------------------------------------------------
393 //------------------------------------------------------------------------
394 void PCollection_HExtendedString::Prepend
395 (const Handle(PCollection_HExtendedString)& S)
401 //------------------------------------------------------------------------
403 //------------------------------------------------------------------------
404 void PCollection_HExtendedString::Print (Standard_OStream& S) const
406 Standard_Integer len = Data.Length() ;
407 for(Standard_Integer i = 0; i < len ; i++) {
414 //------------------------------------------------------------------------
416 //------------------------------------------------------------------------
417 void PCollection_HExtendedString::Remove (const Standard_Integer Index)
419 if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
423 //------------------------------------------------------------------------
425 //------------------------------------------------------------------------
426 void PCollection_HExtendedString::Remove
427 (const Standard_Integer FromIndex, const Standard_Integer ToIndex)
429 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
430 Standard_OutOfRange::Raise();
431 Standard_Integer size1 = Length();
432 for( Standard_Integer i = ToIndex, j = FromIndex-1; i < size1 ; i++, j++)
433 Data.SetValue(j,Data(i));
434 Data.Resize(size1-(ToIndex-FromIndex+1));
437 //------------------------------------------------------------------------
439 //------------------------------------------------------------------------
440 void PCollection_HExtendedString::RemoveAll (const Standard_ExtCharacter C)
444 Standard_Integer size1 = Length();
445 for( i = 0, j = 0; i < size1 ; i++) {
446 if (Data(i) == C) continue;
447 Data.SetValue(j++, Data(i));
452 //------------------------------------------------------------------------
454 //------------------------------------------------------------------------
455 void PCollection_HExtendedString::RightAdjust ()
458 if (! IsAscii()) Standard_OutOfRange::Raise();
459 for ( i = Length() ; i >= 1 ; i --) {
460 if (!IsSpace((Standard_Character)Value(i))) break;
462 if( i < Length() ) Remove(i+1,Length());
465 //------------------------------------------------------------------------
467 //------------------------------------------------------------------------
468 void PCollection_HExtendedString::RightJustify
469 (const Standard_Integer Width, const Standard_ExtCharacter Filler)
473 if (Width < 0) Standard_NegativeValue::Raise();
474 Standard_Integer size1 = Length();
477 for ( i = size1-1, k = Width-1 ; i >= 0 ; i--, k--)
478 Data.SetValue(k, Data(i));
479 for(; k >= 0 ; k--) Data.SetValue(k, Filler);
483 //------------------------------------------------------------------------
485 //------------------------------------------------------------------------
486 void PCollection_HExtendedString::SetValue
487 (const Standard_Integer Index, const Standard_ExtCharacter C)
489 if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
493 //------------------------------------------------------------------------
495 //------------------------------------------------------------------------
496 void PCollection_HExtendedString::SetValue
497 (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
500 Standard_Integer size1 = Length();
501 Standard_Integer size2 = S->Length();
502 Standard_Integer size3 = size2 + Index - 1;
504 if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
506 if(size1 != size3) Data.Resize(size3);
507 for( Standard_Integer i = 1 ; i <= size2; i++) Data.SetValue(Index+i-2, S->Value(i));
510 //------------------------------------------------------------------------
512 //------------------------------------------------------------------------
513 Handle(PCollection_HExtendedString) PCollection_HExtendedString::Split
514 (const Standard_Integer Index)
516 if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
517 Handle(PCollection_HExtendedString) S2;
518 if (Index != Length()) {
519 S2 = SubString(Index+1,Length());
524 Handle(PCollection_HAsciiString) s = new PCollection_HAsciiString("");
525 S2 = new PCollection_HExtendedString(s);
527 Handle(PCollection_HAsciiString) s = new (os_segment::of(this)) PCollection_HAsciiString("");
528 S2 = new (os_segment::of(this)) PCollection_HExtendedString(s);
534 //------------------------------------------------------------------------
536 //------------------------------------------------------------------------
537 Handle(PCollection_HExtendedString) PCollection_HExtendedString::SubString
538 (const Standard_Integer FromIndex, const Standard_Integer ToIndex) const
540 if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex )
541 Standard_OutOfRange::Raise();
542 Handle(PCollection_HExtendedString) S1;
543 Handle(PCollection_HExtendedString) S2;
546 S1 = new PCollection_HExtendedString(S2,FromIndex,ToIndex);
548 S1 = new (os_segment::of(this)) PCollection_HExtendedString(S2,FromIndex,ToIndex);
553 //------------------------------------------------------------------------
555 //------------------------------------------------------------------------
556 Standard_Integer PCollection_HExtendedString::UsefullLength () const
559 if (! IsAscii()) Standard_OutOfRange::Raise();
560 for( i = Length() ; i >= 1 ; i--)
561 if (IsGraphic((Standard_Character)Value(i))) break;
565 //------------------------------------------------------------------------
567 //------------------------------------------------------------------------
568 Standard_ExtCharacter PCollection_HExtendedString::Value
569 (const Standard_Integer Index) const
571 if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
572 return Data(Index-1);
575 //------------------------------------------------------------------------
577 //------------------------------------------------------------------------
578 void PCollection_HExtendedString::ShallowDump(Standard_OStream& S) const
580 S << "begin class HExtendedString " << endl;
581 ::ShallowDump(Data, S);
582 S << "end class HExtendedString" << endl;
586 //------------------------------------------------------------------------
588 //------------------------------------------------------------------------
589 void PCollection_HExtendedString::Assign
590 (const DBC_VArrayOfExtCharacter& TheField)