b311480e |
1 | // Copyright (c) 1993-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
7fd59977 |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
7fd59977 |
14 | |
fb0b0531 |
15 | #include <TCollection_ExtendedString.hxx> |
42cf5bc1 |
16 | |
fb0b0531 |
17 | #include <NCollection_UtfIterator.hxx> |
7fd59977 |
18 | #include <Standard.hxx> |
19 | #include <Standard_ExtString.hxx> |
42cf5bc1 |
20 | #include <Standard_NegativeValue.hxx> |
7fd59977 |
21 | #include <Standard_NullObject.hxx> |
42cf5bc1 |
22 | #include <Standard_NumericError.hxx> |
7fd59977 |
23 | #include <Standard_OutOfRange.hxx> |
7fd59977 |
24 | #include <TCollection_AsciiString.hxx> |
25 | |
6286195c |
26 | #include <algorithm> |
971c281b |
27 | #include <cctype> |
42cf5bc1 |
28 | #include <cstdio> |
7fd59977 |
29 | |
c885cfda |
30 | namespace |
31 | { |
fb0b0531 |
32 | //! Allocate string buffer (automatically adding extra symbol for NULL-termination). |
33 | static Standard_ExtCharacter* allocateExtChars (const Standard_Size theLength) |
c885cfda |
34 | { |
fb0b0531 |
35 | return (Standard_ExtCharacter* )Standard::Allocate ((theLength + 1) * sizeof(Standard_ExtCharacter)); |
c885cfda |
36 | } |
37 | |
fb0b0531 |
38 | //! Re-allocate string buffer (automatically adding extra symbol for NULL-termination). |
39 | static Standard_ExtCharacter* reallocateExtChars (Standard_Address theAddr, |
40 | const Standard_Size theLength) |
c885cfda |
41 | { |
fb0b0531 |
42 | return (Standard_ExtCharacter* )Standard::Reallocate (theAddr, (theLength + 1) * sizeof(Standard_ExtCharacter)); |
c885cfda |
43 | } |
7fd59977 |
44 | |
c885cfda |
45 | static const Standard_ExtCharacter NULL_EXTSTRING[1] = {0}; |
7fd59977 |
46 | |
fb0b0531 |
47 | //! Returns the number of 16-bit code units in Unicode string |
48 | template<typename T> |
49 | static Standard_Integer nbSymbols (const T* theUtfString) |
50 | { |
51 | Standard_Integer aNbCodeUnits = 0; |
52 | for (NCollection_UtfIterator<T> anIter (theUtfString); *anIter != 0; ++anIter) |
53 | { |
54 | aNbCodeUnits += anIter.AdvanceCodeUnitsUtf16(); |
55 | } |
56 | return aNbCodeUnits; |
57 | } |
58 | |
59 | //! Convert from wchar_t* to extended string. |
60 | //! Default implementation when size of wchar_t and extended char is different (e.g. Linux / UNIX). |
61 | template<size_t CharSize> |
62 | inline Standard_ExtCharacter* Standard_UNUSED fromWideString (const Standard_WideChar* theUtfString, |
63 | Standard_Integer& theLength) |
64 | { |
65 | theLength = nbSymbols (theUtfString); |
66 | Standard_ExtCharacter* aString = allocateExtChars (theLength); |
67 | NCollection_UtfWideIter anIterRead (theUtfString); |
68 | for (Standard_ExtCharacter* anIterWrite = aString; *anIterRead != 0; ++anIterRead) |
69 | { |
70 | anIterWrite = anIterRead.GetUtf (anIterWrite); |
71 | } |
72 | aString[theLength] = '\0'; |
73 | return aString; |
74 | } |
75 | |
76 | //! Use memcpy() conversion when size is the same (e.g. on Windows). |
77 | template<> |
78 | inline Standard_ExtCharacter* Standard_UNUSED fromWideString<sizeof(Standard_ExtCharacter)> (const Standard_WideChar* theUtfString, |
79 | Standard_Integer& theLength) |
80 | { |
81 | for (theLength = 0; theUtfString[theLength] != L'\0'; ++theLength) {} |
82 | Standard_ExtCharacter* aString = allocateExtChars (theLength); |
83 | const Standard_Integer aSize = theLength * sizeof(Standard_ExtCharacter); |
84 | memcpy (aString, theUtfString, aSize); |
85 | aString[theLength] = '\0'; |
86 | return aString; |
87 | } |
88 | |
7fd59977 |
89 | } |
90 | |
91 | //----------------------------------------------------------------------------- |
92 | // Create an empty ExtendedString |
93 | // ---------------------------------------------------------------------------- |
94 | TCollection_ExtendedString::TCollection_ExtendedString() |
fb0b0531 |
95 | : mystring (allocateExtChars (0)), |
96 | mylength (0) |
7fd59977 |
97 | { |
fb0b0531 |
98 | mystring[0] = '\0'; |
7fd59977 |
99 | } |
100 | |
101 | //---------------------------------------------------------------------------- |
102 | // Create an ExtendedString from a Standard_CString |
103 | //---------------------------------------------------------------------------- |
104 | TCollection_ExtendedString::TCollection_ExtendedString |
fb0b0531 |
105 | (const Standard_CString theString, |
29cb310a |
106 | const Standard_Boolean isMultiByte) |
fb0b0531 |
107 | : mystring (NULL), |
108 | mylength (0) |
109 | { |
110 | if (theString == NULL) |
111 | { |
9775fa61 |
112 | throw Standard_NullObject("TCollection_ExtendedString : null parameter "); |
fb0b0531 |
113 | } |
114 | |
115 | if (isMultiByte) |
116 | { |
117 | mylength = nbSymbols (theString); |
118 | mystring = allocateExtChars (mylength); |
119 | mystring[mylength] = '\0'; |
120 | if (ConvertToUnicode (theString)) |
121 | { |
122 | return; |
7fd59977 |
123 | } |
124 | } |
fb0b0531 |
125 | |
126 | mylength = (int)strlen(theString); |
127 | mystring = reallocateExtChars (mystring, mylength); |
128 | for (int aCharIter = 0; aCharIter < mylength; ++aCharIter) |
129 | { |
130 | mystring[aCharIter] = ToExtCharacter (theString[aCharIter]); |
7fd59977 |
131 | } |
fb0b0531 |
132 | mystring[mylength] = '\0'; |
7fd59977 |
133 | } |
134 | |
135 | //--------------------------------------------------------------------------- |
136 | // Create an ExtendedString from an ExtString |
137 | //-------------------------------------------------------------------------- |
fb0b0531 |
138 | TCollection_ExtendedString::TCollection_ExtendedString (const Standard_ExtString theString) |
139 | : mystring (NULL), |
140 | mylength (0) |
7fd59977 |
141 | { |
fb0b0531 |
142 | if (theString == NULL) |
143 | { |
9775fa61 |
144 | throw Standard_NullObject("TCollection_ExtendedString : null parameter "); |
7fd59977 |
145 | } |
fb0b0531 |
146 | |
147 | for (mylength = 0; theString[mylength] != '\0'; ++mylength) {} |
148 | mystring = allocateExtChars (mylength); |
149 | const Standard_Integer aSizeBytes = mylength * sizeof(Standard_ExtCharacter); |
150 | memcpy (mystring, theString, aSizeBytes); |
151 | mystring[mylength] = '\0'; |
152 | } |
153 | |
154 | // ---------------------------------------------------------------------------- |
155 | // TCollection_ExtendedString |
156 | // ---------------------------------------------------------------------------- |
157 | TCollection_ExtendedString::TCollection_ExtendedString (const Standard_WideChar* theStringUtf) |
158 | : mystring (NULL), |
159 | mylength (0) |
160 | { |
161 | if (theStringUtf == NULL) |
162 | { |
9775fa61 |
163 | throw Standard_NullObject("TCollection_ExtendedString : null parameter "); |
fb0b0531 |
164 | } |
165 | |
166 | mystring = fromWideString<sizeof(Standard_WideChar)> (theStringUtf, mylength); |
7fd59977 |
167 | } |
168 | |
169 | // ---------------------------------------------------------------------------- |
170 | // Create an asciistring from a Standard_Character |
171 | // ---------------------------------------------------------------------------- |
172 | TCollection_ExtendedString::TCollection_ExtendedString |
173 | (const Standard_Character aChar) |
174 | { |
175 | if ( aChar != '\0' ) { |
176 | mylength = 1; |
fb0b0531 |
177 | mystring = allocateExtChars (1); |
7fd59977 |
178 | mystring[0] = ToExtCharacter(aChar); |
fb0b0531 |
179 | mystring[1] = '\0'; |
7fd59977 |
180 | } |
181 | else { |
182 | // mystring = 0L; |
183 | mylength = 0; |
fb0b0531 |
184 | mystring = allocateExtChars (0); |
185 | mystring[0] = '\0'; |
7fd59977 |
186 | } |
187 | } |
188 | |
189 | //-------------------------------------------------------------------------- |
190 | // Create a string from a ExtCharacter |
191 | // ---------------------------------------------------------------------------- |
192 | TCollection_ExtendedString::TCollection_ExtendedString |
193 | (const Standard_ExtCharacter aChar) |
194 | { |
195 | mylength = 1; |
fb0b0531 |
196 | mystring = allocateExtChars (1); |
7fd59977 |
197 | mystring[0] = aChar; |
fb0b0531 |
198 | mystring[1] = '\0'; |
7fd59977 |
199 | } |
200 | |
201 | // ---------------------------------------------------------------------------- |
202 | // Create an AsciiString from a filler |
203 | // ---------------------------------------------------------------------------- |
204 | TCollection_ExtendedString::TCollection_ExtendedString |
205 | (const Standard_Integer length, |
206 | const Standard_ExtCharacter filler ) |
207 | { |
fb0b0531 |
208 | mystring = allocateExtChars (length); |
7fd59977 |
209 | mylength = length; |
210 | for (int i = 0 ; i < length ; i++) mystring[i] = filler; |
fb0b0531 |
211 | mystring[mylength] = '\0'; |
7fd59977 |
212 | } |
213 | |
214 | // ---------------------------------------------------------------------------- |
215 | // Create a String from an Integer |
216 | // ---------------------------------------------------------------------------- |
217 | TCollection_ExtendedString::TCollection_ExtendedString |
218 | (const Standard_Integer aValue) |
219 | { |
220 | union {int bid ; |
221 | char t [13];} CHN ; |
91322f44 |
222 | Sprintf(&CHN.t[0],"%d",aValue); |
105aae76 |
223 | mylength = (int)strlen(CHN.t); |
fb0b0531 |
224 | mystring = allocateExtChars (mylength); |
7fd59977 |
225 | for (int i = 0 ; i < mylength ; i++) mystring[i] = ToExtCharacter(CHN.t[i]); |
fb0b0531 |
226 | mystring[mylength] = '\0'; |
7fd59977 |
227 | } |
228 | |
229 | // ---------------------------------------------------------------------------- |
230 | // Create a String from a real |
231 | // ---------------------------------------------------------------------------- |
232 | TCollection_ExtendedString::TCollection_ExtendedString |
233 | (const Standard_Real aValue) |
234 | { |
235 | union {int bid ; |
236 | char t [50];} CHN ; |
91322f44 |
237 | Sprintf(&CHN.t[0],"%g",aValue); |
105aae76 |
238 | mylength = (int)strlen( CHN.t ); |
fb0b0531 |
239 | mystring = allocateExtChars (mylength); |
7fd59977 |
240 | for (int i = 0 ; i < mylength ; i++) mystring[i] = ToExtCharacter(CHN.t[i]); |
fb0b0531 |
241 | mystring[mylength] = '\0'; |
7fd59977 |
242 | } |
243 | |
244 | //----------------------------------------------------------------------------- |
245 | // create an extendedstring from an extendedstring |
246 | // ---------------------------------------------------------------------------- |
247 | TCollection_ExtendedString::TCollection_ExtendedString |
248 | (const TCollection_ExtendedString& astring) |
249 | { |
fb0b0531 |
250 | const Standard_Integer aSizeBytes = astring.mylength * sizeof(Standard_ExtCharacter); |
7fd59977 |
251 | mylength = astring.mylength; |
fb0b0531 |
252 | mystring = allocateExtChars (astring.mylength); |
253 | memcpy (mystring, astring.mystring, aSizeBytes); |
254 | mystring[mylength] = '\0'; |
7fd59977 |
255 | } |
256 | |
257 | //--------------------------------------------------------------------------- |
258 | // Create an extendedstring from an AsciiString |
259 | //--------------------------------------------------------------------------- |
260 | TCollection_ExtendedString::TCollection_ExtendedString |
fb0b0531 |
261 | (const TCollection_AsciiString& theString) |
7fd59977 |
262 | { |
fb0b0531 |
263 | mylength = nbSymbols (theString.ToCString()); |
264 | mystring = allocateExtChars (mylength); |
265 | mystring[mylength] = '\0'; |
266 | if (ConvertToUnicode (theString.ToCString())) |
267 | { |
268 | return; |
269 | } |
270 | |
271 | mylength = theString.Length(); |
272 | mystring = reallocateExtChars (mystring, mylength); |
273 | Standard_CString aCString = theString.ToCString(); |
274 | for (Standard_Integer aCharIter = 0; aCharIter <= mylength; ++aCharIter) |
d9ff84e8 |
275 | { |
fb0b0531 |
276 | mystring[aCharIter] = ToExtCharacter (aCString[aCharIter]); |
d9ff84e8 |
277 | } |
fb0b0531 |
278 | mystring[mylength] = '\0'; |
7fd59977 |
279 | } |
280 | |
281 | // ---------------------------------------------------------------------------- |
282 | // AssignCat |
283 | // ---------------------------------------------------------------------------- |
fb0b0531 |
284 | void TCollection_ExtendedString::AssignCat (const TCollection_ExtendedString& theOther) |
7fd59977 |
285 | { |
fb0b0531 |
286 | if (theOther.mylength == 0) |
287 | { |
288 | return; |
289 | } |
290 | |
291 | const Standard_Integer anOtherLength = theOther.mylength; |
292 | const Standard_Integer aNewlength = mylength + anOtherLength; |
293 | if (mystring != NULL) |
294 | { |
295 | mystring = reallocateExtChars (mystring, aNewlength); |
296 | memcpy (mystring + mylength, theOther.mystring, anOtherLength * sizeof(Standard_ExtCharacter)); |
7fd59977 |
297 | } |
fb0b0531 |
298 | else |
299 | { |
300 | mystring = allocateExtChars (aNewlength); |
301 | memcpy (mystring, theOther.mystring, anOtherLength * sizeof(Standard_ExtCharacter)); |
302 | } |
303 | mylength = aNewlength; |
304 | mystring[mylength] = '\0'; |
7fd59977 |
305 | } |
306 | |
307 | // ---------------------------------------------------------------------------- |
308 | // Cat |
309 | // ---------------------------------------------------------------------------- |
310 | TCollection_ExtendedString TCollection_ExtendedString::Cat |
311 | (const TCollection_ExtendedString& other) const |
312 | { |
29cb310a |
313 | TCollection_ExtendedString res( mylength + other.mylength, 0 ); |
314 | if ( mylength > 0 ) |
315 | memcpy( res.mystring, mystring, mylength*2 ); |
316 | if ( other.mylength > 0 ) |
317 | memcpy( res.mystring + mylength, other.mystring, other.mylength*2 ); |
7fd59977 |
318 | return res; |
319 | } |
320 | |
321 | // ---------------------------------------------------------------------------- |
322 | // ChangeAll |
323 | // ---------------------------------------------------------------------------- |
324 | void TCollection_ExtendedString::ChangeAll(const Standard_ExtCharacter aChar, |
325 | const Standard_ExtCharacter NewChar) |
326 | { |
327 | for (int i = 0 ; i < mylength; i++) |
328 | if (mystring[i] == aChar) mystring[i] = NewChar; |
329 | } |
330 | |
331 | // ---------------------------------------------------------------------------- |
332 | // Clear |
333 | // ---------------------------------------------------------------------------- |
334 | void TCollection_ExtendedString::Clear() |
335 | { |
fb0b0531 |
336 | if (mylength == 0) |
337 | { |
338 | return; |
339 | } |
340 | |
341 | Standard::Free (mystring); |
7fd59977 |
342 | mylength = 0; |
fb0b0531 |
343 | mystring = allocateExtChars (mylength); |
344 | mystring[0] = '\0'; |
7fd59977 |
345 | } |
346 | |
347 | // ---------------------------------------------------------------------------- |
348 | // Copy |
349 | // ---------------------------------------------------------------------------- |
350 | void TCollection_ExtendedString::Copy (const TCollection_ExtendedString& fromwhere) |
351 | { |
352 | |
353 | if (fromwhere.mystring) { |
fb0b0531 |
354 | const Standard_Integer newlength = fromwhere.mylength; |
355 | const Standard_Integer aSizeBytes = newlength * sizeof(Standard_ExtCharacter); |
356 | if (mystring != NULL) |
357 | { |
358 | mystring = reallocateExtChars (mystring, newlength); |
7fd59977 |
359 | } |
360 | else { |
fb0b0531 |
361 | mystring = allocateExtChars (newlength); |
7fd59977 |
362 | } |
363 | mylength = newlength; |
fb0b0531 |
364 | memcpy (mystring, fromwhere.mystring, aSizeBytes); |
365 | mystring[mylength] = '\0'; |
7fd59977 |
366 | } |
fb0b0531 |
367 | else |
368 | { |
369 | if (mystring != 0) |
370 | { |
7fd59977 |
371 | mylength = 0; |
fb0b0531 |
372 | mystring[0] = '\0'; |
7fd59977 |
373 | } |
374 | } |
375 | } |
376 | |
6286195c |
377 | // ---------------------------------------------------------------------------- |
378 | // Swap |
379 | // ---------------------------------------------------------------------------- |
380 | void TCollection_ExtendedString::Swap (TCollection_ExtendedString& theOther) |
381 | { |
382 | std::swap (mystring, theOther.mystring); |
383 | std::swap (mylength, theOther.mylength); |
384 | } |
385 | |
7fd59977 |
386 | // ---------------------------------------------------------------------------- |
387 | // Destroy |
388 | // ---------------------------------------------------------------------------- |
fb0b0531 |
389 | TCollection_ExtendedString::~TCollection_ExtendedString() |
7fd59977 |
390 | { |
547702a1 |
391 | if (mystring) Standard::Free(mystring); |
7fd59977 |
392 | mystring = 0L; |
393 | } |
394 | |
395 | //---------------------------------------------------------------------------- |
396 | // Insert a character before 'where'th character |
397 | // ---------------------------------------------------------------------------- |
398 | void TCollection_ExtendedString::Insert(const Standard_Integer where, |
399 | const Standard_ExtCharacter what) |
400 | { |
401 | if (where > mylength + 1 ) |
9775fa61 |
402 | throw Standard_OutOfRange("TCollection_ExtendedString::Insert : " |
403 | "Parameter where is too big"); |
7fd59977 |
404 | if (where < 0) |
9775fa61 |
405 | throw Standard_OutOfRange("TCollection_ExtendedString::Insert : " |
406 | "Parameter where is negative"); |
7fd59977 |
407 | |
fb0b0531 |
408 | if (mystring != NULL) |
409 | { |
410 | mystring = reallocateExtChars (mystring, mylength + 1); |
7fd59977 |
411 | } |
412 | else { |
fb0b0531 |
413 | mystring = allocateExtChars (mylength + 1); |
7fd59977 |
414 | } |
415 | if (where != mylength +1) { |
416 | for (int i=mylength-1; i >= where-1; i--) |
417 | mystring[i+1] = mystring[i]; |
418 | } |
419 | mystring[where-1] = what; |
420 | mylength++; |
fb0b0531 |
421 | mystring[mylength] = '\0'; |
7fd59977 |
422 | } |
423 | |
424 | // ---------------------------------------------------------------------------- |
425 | // Insert |
426 | // ---------------------------------------------------------------------------- |
427 | void TCollection_ExtendedString::Insert(const Standard_Integer where, |
428 | const TCollection_ExtendedString& what) |
429 | { |
430 | Standard_ExtString swhat = what.mystring; |
431 | if (where <= mylength + 1) { |
432 | Standard_Integer whatlength = what.mylength; |
433 | if(whatlength) { |
434 | Standard_Integer newlength = mylength + whatlength; |
435 | |
436 | if (mystring) { |
fb0b0531 |
437 | mystring = reallocateExtChars (mystring, newlength); |
7fd59977 |
438 | } |
439 | else { |
fb0b0531 |
440 | mystring = allocateExtChars (newlength); |
7fd59977 |
441 | } |
442 | if (where != mylength +1) { |
443 | for (int i=mylength-1; i >= where-1; i--) |
444 | mystring[i+whatlength] = mystring[i]; |
445 | } |
446 | for (int i=0; i < whatlength; i++) |
447 | mystring[where-1+i] = swhat[i]; |
448 | |
449 | mylength = newlength; |
fb0b0531 |
450 | mystring[mylength] = '\0'; |
7fd59977 |
451 | } |
452 | } |
453 | else { |
9775fa61 |
454 | throw Standard_OutOfRange("TCollection_ExtendedString::Insert : " |
455 | "Parameter where is too big"); |
7fd59977 |
456 | } |
457 | } |
458 | |
7fd59977 |
459 | // ---------------------------------------------------------------------------- |
460 | // IsEqual |
461 | // ---------------------------------------------------------------------------- |
462 | Standard_Boolean TCollection_ExtendedString::IsEqual |
463 | (const Standard_ExtString other) const |
464 | { |
29cb310a |
465 | return ( memcmp( mystring, other, (mylength+1)*2 ) == 0 ); |
7fd59977 |
466 | } |
7fd59977 |
467 | |
468 | // ---------------------------------------------------------------------------- |
469 | // IsEqual |
470 | // ---------------------------------------------------------------------------- |
471 | Standard_Boolean TCollection_ExtendedString::IsEqual |
472 | (const TCollection_ExtendedString& other) const |
473 | { |
29cb310a |
474 | return ( memcmp( mystring, other.mystring, (mylength+1)*2 ) == 0 ); |
7fd59977 |
475 | } |
476 | |
7fd59977 |
477 | // ---------------------------------------------------------------------------- |
478 | // IsDifferent |
479 | // ---------------------------------------------------------------------------- |
480 | Standard_Boolean TCollection_ExtendedString::IsDifferent |
481 | (const Standard_ExtString other ) const |
482 | { |
29cb310a |
483 | return ( memcmp( mystring, other, (mylength+1)*2 ) != 0 ); |
7fd59977 |
484 | } |
7fd59977 |
485 | |
486 | // ---------------------------------------------------------------------------- |
487 | // IsDifferent |
488 | // ---------------------------------------------------------------------------- |
489 | Standard_Boolean TCollection_ExtendedString::IsDifferent |
490 | (const TCollection_ExtendedString& other) const |
491 | { |
29cb310a |
492 | return ( memcmp( mystring, other.mystring, (mylength+1)*2 ) != 0 ); |
7fd59977 |
493 | } |
494 | |
7fd59977 |
495 | // ---------------------------------------------------------------------------- |
496 | // IsLess |
497 | // ---------------------------------------------------------------------------- |
498 | Standard_Boolean TCollection_ExtendedString::IsLess |
499 | (const Standard_ExtString other) const |
500 | { |
29cb310a |
501 | return ( memcmp( mystring, other, (mylength+1)*2 ) < 0 ); |
7fd59977 |
502 | } |
7fd59977 |
503 | |
504 | // ---------------------------------------------------------------------------- |
505 | // IsLess |
506 | // ---------------------------------------------------------------------------- |
507 | Standard_Boolean TCollection_ExtendedString::IsLess |
508 | (const TCollection_ExtendedString& other) const |
509 | { |
29cb310a |
510 | return ( memcmp( mystring, other.mystring, (mylength+1)*2 ) < 0 ); |
7fd59977 |
511 | } |
512 | |
7fd59977 |
513 | // ---------------------------------------------------------------------------- |
514 | // IsGreater |
515 | // ---------------------------------------------------------------------------- |
516 | Standard_Boolean TCollection_ExtendedString::IsGreater |
517 | (const Standard_ExtString other) const |
518 | { |
29cb310a |
519 | return ( memcmp( mystring, other, (mylength+1)*2 ) > 0 ); |
7fd59977 |
520 | } |
7fd59977 |
521 | |
522 | // ---------------------------------------------------------------------------- |
523 | // IsGreater |
524 | // ---------------------------------------------------------------------------- |
525 | Standard_Boolean TCollection_ExtendedString::IsGreater |
526 | (const TCollection_ExtendedString& other) const |
527 | { |
29cb310a |
528 | return ( memcmp( mystring, other.mystring, (mylength+1)*2 ) > 0 ); |
7fd59977 |
529 | } |
530 | |
fb0b0531 |
531 | // ---------------------------------------------------------------------------- |
532 | // StartsWith |
533 | // ---------------------------------------------------------------------------- |
534 | Standard_Boolean TCollection_ExtendedString::StartsWith (const TCollection_ExtendedString& theStartString) const |
535 | { |
536 | if (this == &theStartString) |
537 | { |
538 | return true; |
539 | } |
540 | |
541 | return mylength >= theStartString.mylength |
542 | && memcmp (theStartString.mystring, mystring, theStartString.mylength) == 0; |
543 | } |
544 | |
545 | // ---------------------------------------------------------------------------- |
546 | // EndsWith |
547 | // ---------------------------------------------------------------------------- |
548 | Standard_Boolean TCollection_ExtendedString::EndsWith (const TCollection_ExtendedString& theEndString) const |
549 | { |
550 | if (this == &theEndString) |
551 | { |
552 | return true; |
553 | } |
554 | |
555 | return mylength >= theEndString.mylength |
556 | && memcmp (theEndString.mystring, mystring + mylength - theEndString.mylength, theEndString.mylength) == 0; |
557 | } |
558 | |
7fd59977 |
559 | // ---------------------------------------------------------------------------- |
560 | // IsAscii |
561 | // ---------------------------------------------------------------------------- |
562 | Standard_Boolean TCollection_ExtendedString::IsAscii() const |
563 | { |
564 | for( Standard_Integer i = 0 ; i < mylength ; i++) |
565 | if (!IsAnAscii(mystring[i])) return Standard_False; |
566 | return Standard_True; |
567 | } |
568 | |
569 | //------------------------------------------------------------------------ |
570 | // Length |
571 | // ---------------------------------------------------------------------------- |
572 | Standard_Integer TCollection_ExtendedString::Length() const |
573 | { |
574 | return mylength; |
575 | } |
576 | |
577 | // ---------------------------------------------------------------------------- |
578 | // Print |
579 | // ---------------------------------------------------------------------------- |
580 | void TCollection_ExtendedString::Print(Standard_OStream& astream) const |
581 | { |
582 | // ASCII symbols (including extended Ascii) are printed as is; |
583 | // other Unicode characters are encoded as SGML numeric character references |
584 | for (Standard_Integer i = 0 ; i < mylength ; i++) { |
585 | Standard_ExtCharacter c = mystring[i]; |
586 | if ( IsAnAscii(c) ) |
587 | astream << ToCharacter(c); |
588 | else |
589 | astream << "&#" << c << ";"; |
590 | } |
591 | } |
592 | |
593 | |
594 | // ---------------------------------------------------------------------------- |
595 | Standard_OStream& operator << (Standard_OStream& astream, |
596 | const TCollection_ExtendedString& astring) |
597 | { |
598 | astring.Print(astream); |
599 | return astream; |
600 | } |
601 | |
602 | // ---------------------------------------------------------------------------- |
603 | // RemoveAll |
604 | // ---------------------------------------------------------------------------- |
605 | void TCollection_ExtendedString::RemoveAll(const Standard_ExtCharacter what) |
606 | |
607 | { |
608 | if (mylength == 0) return; |
609 | int c = 0; |
610 | for (int i=0; i < mylength; i++) |
611 | if (mystring[i] != what) mystring[c++] = mystring[i]; |
612 | mylength = c; |
613 | mystring[mylength] = '\0'; |
614 | } |
615 | |
616 | // ---------------------------------------------------------------------------- |
617 | // Remove |
618 | // ---------------------------------------------------------------------------- |
619 | void TCollection_ExtendedString::Remove (const Standard_Integer where, |
620 | const Standard_Integer ahowmany) |
621 | { |
622 | if (where+ahowmany <= mylength+1) { |
623 | int i,j; |
624 | for (i = where+ahowmany-1, j = where-1; i < mylength; i++, j++) |
625 | mystring[j] = mystring[i]; |
626 | mylength -= ahowmany; |
627 | mystring[mylength] = '\0'; |
628 | } |
629 | else |
9775fa61 |
630 | throw Standard_OutOfRange("TCollection_ExtendedString::Remove: " |
631 | "Too many characters to erase or " |
632 | "invalid starting value."); |
7fd59977 |
633 | } |
634 | |
635 | // ---------------------------------------------------------------------------- |
636 | // Search |
637 | // ---------------------------------------------------------------------------- |
638 | Standard_Integer TCollection_ExtendedString::Search |
639 | (const TCollection_ExtendedString& what) const |
640 | { |
641 | Standard_Integer size = what.mylength; |
642 | Standard_ExtString swhat = what.mystring; |
643 | if (size) { |
644 | int k,j; |
645 | int i = 0; |
646 | Standard_Boolean find = Standard_False; |
647 | while ( i < mylength-size+1 && !find) { |
648 | k = i++; |
649 | j = 0; |
650 | while (j < size && mystring[k++] == swhat[j++]) |
651 | if (j == size) find = Standard_True; |
652 | } |
653 | if (find) return i; |
654 | } |
655 | return -1; |
656 | } |
657 | |
658 | // ---------------------------------------------------------------------------- |
659 | // SearchFromEnd |
660 | // ---------------------------------------------------------------------------- |
661 | Standard_Integer TCollection_ExtendedString::SearchFromEnd |
662 | (const TCollection_ExtendedString& what) const |
663 | { |
664 | Standard_Integer size = what.mylength; |
665 | if (size) { |
666 | Standard_ExtString swhat = what.mystring; |
667 | int k,j; |
668 | int i = mylength-1; |
669 | Standard_Boolean find = Standard_False; |
670 | while ( i >= size-1 && !find) { |
671 | k = i--; |
672 | j = size-1; |
673 | while (j >= 0 && mystring[k--] == swhat[j--]) |
674 | if (j == -1) find = Standard_True; |
675 | } |
676 | if (find) return i-size+3; |
677 | } |
678 | return -1; |
679 | } |
680 | |
681 | // ---------------------------------------------------------------------------- |
682 | // SetValue |
683 | // ---------------------------------------------------------------------------- |
684 | void TCollection_ExtendedString::SetValue(const Standard_Integer where, |
685 | const Standard_ExtCharacter what) |
686 | { |
687 | if (where > 0 && where <= mylength) { |
688 | mystring[where-1] = what; |
689 | } |
690 | else { |
9775fa61 |
691 | throw Standard_OutOfRange("TCollection_ExtendedString::SetValue : parameter where"); |
7fd59977 |
692 | } |
693 | } |
694 | |
695 | // ---------------------------------------------------------------------------- |
696 | // SetValue |
697 | // ---------------------------------------------------------------------------- |
698 | void TCollection_ExtendedString::SetValue |
699 | (const Standard_Integer where, |
700 | const TCollection_ExtendedString& what) |
701 | { |
702 | if (where > 0 && where <= mylength+1) { |
703 | Standard_Integer size = what.mylength; |
704 | Standard_ExtString swhat = what.mystring; |
705 | size += (where - 1); |
706 | if (size >= mylength){ |
707 | if (mystring) { |
fb0b0531 |
708 | mystring = reallocateExtChars (mystring, size); |
7fd59977 |
709 | } |
710 | else { |
fb0b0531 |
711 | mystring = allocateExtChars (size); |
7fd59977 |
712 | } |
713 | mylength = size; |
714 | } |
715 | for (int i = where-1; i < size; i++) |
716 | mystring[i] = swhat[i-(where-1)]; |
717 | mystring[mylength] = '\0'; |
718 | } |
719 | else |
9775fa61 |
720 | throw Standard_OutOfRange("TCollection_ExtendedString::SetValue : " |
721 | "parameter where"); |
7fd59977 |
722 | } |
723 | |
724 | // ---------------------------------------------------------------------------- |
725 | // Split |
726 | // ---------------------------------------------------------------------------- |
727 | TCollection_ExtendedString TCollection_ExtendedString::Split |
728 | (const Standard_Integer where) |
729 | { |
730 | if (where >= 0 && where < mylength) { |
731 | TCollection_ExtendedString res(&mystring[where]); |
732 | Trunc(where); |
733 | return res; |
734 | } |
9775fa61 |
735 | throw Standard_OutOfRange("TCollection_ExtendedString::Split index"); |
7fd59977 |
736 | } |
737 | |
738 | // ---------------------------------------------------------------------------- |
739 | // Token |
740 | // ---------------------------------------------------------------------------- |
741 | TCollection_ExtendedString TCollection_ExtendedString::Token |
742 | (const Standard_ExtString separators, |
743 | const Standard_Integer whichone) const |
744 | { |
29cb310a |
745 | TCollection_ExtendedString res; |
7fd59977 |
746 | if (!separators) |
9775fa61 |
747 | throw Standard_NullObject("TCollection_ExtendedString::Token : " |
748 | "parameter 'separators'"); |
7fd59977 |
749 | |
750 | int i,j,k,l; |
fb0b0531 |
751 | Standard_PExtCharacter buftmp = allocateExtChars (mylength); |
7fd59977 |
752 | Standard_ExtCharacter aSep; |
753 | |
754 | Standard_Boolean isSepFound = Standard_False, otherSepFound; |
755 | |
756 | j = 0; |
757 | |
758 | for (i = 0; i < whichone && j < mylength; i++) { |
759 | isSepFound = Standard_False; |
760 | k = 0; |
761 | buftmp[0] = 0; |
762 | |
763 | // Avant de commencer il faut virer les saloperies devant |
764 | // |
765 | otherSepFound = Standard_True; |
766 | while (j < mylength && otherSepFound) { |
767 | l = 0; |
768 | otherSepFound = Standard_False; |
769 | aSep = separators[l]; |
770 | while(aSep != 0) { |
771 | if (aSep == mystring[j]) { |
772 | aSep = 0; |
773 | otherSepFound = Standard_True; |
774 | } |
775 | else { |
776 | aSep = separators[l++]; |
777 | } |
778 | } |
779 | if (otherSepFound) j++; |
780 | } |
781 | |
782 | while (!isSepFound && k < mylength && j<mylength ) { |
783 | l = 0; |
784 | aSep = separators[l]; |
785 | |
786 | while (aSep != 0 && !isSepFound) { |
787 | if (aSep == mystring[j]) { |
788 | buftmp[k] = 0; |
789 | isSepFound = Standard_True; |
790 | } |
791 | else { |
792 | buftmp[k] = mystring[j]; |
793 | } |
794 | l++; |
795 | aSep = separators[l]; |
796 | } |
797 | j++; k++; |
798 | if(j==mylength) buftmp[k] = 0; |
799 | } |
800 | } |
801 | |
802 | if (i < whichone) { |
803 | buftmp[0] = 0; |
547702a1 |
804 | Standard::Free(buftmp); |
7fd59977 |
805 | } |
806 | else { |
547702a1 |
807 | Standard::Free(res.mystring); |
7fd59977 |
808 | res.mystring = buftmp; |
29cb310a |
809 | for ( res.mylength=0; buftmp[res.mylength]; ++res.mylength ); |
fb0b0531 |
810 | res.mystring[res.mylength] = '\0'; |
7fd59977 |
811 | } |
812 | return res; |
813 | } |
814 | |
815 | // ---------------------------------------------------------------------------- |
816 | // ToExtString |
817 | // ---------------------------------------------------------------------------- |
487bf1ce |
818 | Standard_ExtString TCollection_ExtendedString::ToExtString() const |
7fd59977 |
819 | { |
820 | if(mystring) return mystring; |
821 | return NULL_EXTSTRING; |
822 | } |
823 | |
824 | // ---------------------------------------------------------------------------- |
825 | // Trunc |
826 | // ---------------------------------------------------------------------------- |
827 | void TCollection_ExtendedString::Trunc(const Standard_Integer ahowmany) |
828 | { |
829 | if (ahowmany < 0 || ahowmany > mylength) |
9775fa61 |
830 | throw Standard_OutOfRange("TCollection_ExtendedString::Trunc : " |
831 | "parameter 'ahowmany'"); |
7fd59977 |
832 | mylength = ahowmany; |
833 | mystring[mylength] = '\0'; |
834 | } |
835 | |
836 | // ---------------------------------------------------------------------------- |
837 | // Value |
838 | // ---------------------------------------------------------------------------- |
839 | Standard_ExtCharacter TCollection_ExtendedString::Value |
840 | (const Standard_Integer where) const |
841 | { |
842 | if (where > 0 && where <= mylength) { |
843 | if(mystring) return mystring[where-1]; |
844 | else return 0; |
845 | } |
9775fa61 |
846 | throw Standard_OutOfRange("TCollection_ExtendedString::Value : " |
847 | "parameter where"); |
7fd59977 |
848 | } |
849 | |
850 | |
7fd59977 |
851 | //---------------------------------------------------------------------------- |
852 | // Convert CString (including multibyte case) to UniCode representation |
853 | //---------------------------------------------------------------------------- |
fb0b0531 |
854 | Standard_Boolean TCollection_ExtendedString::ConvertToUnicode (const Standard_CString theStringUtf) |
855 | { |
856 | NCollection_Utf8Iter anIterRead (theStringUtf); |
857 | Standard_ExtCharacter* anIterWrite = mystring; |
858 | if (*anIterRead == 0) |
859 | { |
860 | *anIterWrite = '\0'; |
861 | return Standard_True; |
862 | } |
863 | |
864 | for (; *anIterRead != 0; ++anIterRead) |
865 | { |
866 | if (!anIterRead.IsValid()) |
867 | { |
868 | return Standard_False; |
7fd59977 |
869 | } |
fb0b0531 |
870 | |
871 | anIterWrite = anIterRead.GetUtf (anIterWrite); |
7fd59977 |
872 | } |
fb0b0531 |
873 | return Standard_True; |
7fd59977 |
874 | } |
fb0b0531 |
875 | |
7fd59977 |
876 | //---------------------------------------------------------------------------- |
877 | // Returns expected CString length in UTF8 coding. |
878 | //---------------------------------------------------------------------------- |
879 | Standard_Integer TCollection_ExtendedString::LengthOfCString() const |
880 | { |
fb0b0531 |
881 | Standard_Integer aSizeBytes = 0; |
882 | for (NCollection_Utf16Iter anIter (mystring); *anIter != 0; ++anIter) |
883 | { |
884 | aSizeBytes += anIter.AdvanceBytesUtf8(); |
7fd59977 |
885 | } |
fb0b0531 |
886 | return aSizeBytes; |
7fd59977 |
887 | } |
888 | |
889 | //---------------------------------------------------------------------------- |
890 | // Converts the internal <mystring> to UTF8 coding and returns length of the |
891 | // out CString. |
892 | //---------------------------------------------------------------------------- |
893 | Standard_Integer TCollection_ExtendedString::ToUTF8CString(Standard_PCharacter& theCString) const |
894 | { |
fb0b0531 |
895 | NCollection_Utf16Iter anIterRead (mystring); |
896 | Standard_Utf8Char* anIterWrite = theCString; |
897 | if (*anIterRead == 0) |
898 | { |
899 | *anIterWrite = '\0'; |
900 | return 0; |
901 | } |
902 | |
903 | for (; *anIterRead != 0; ++anIterRead) |
904 | { |
905 | anIterWrite = anIterRead.GetUtf (anIterWrite); |
7fd59977 |
906 | } |
fb0b0531 |
907 | *anIterWrite = '\0'; |
908 | return Standard_Integer(anIterWrite - theCString); |
7fd59977 |
909 | } |