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