1 // Created on: 1992-12-15
2 // Created by: Mireille MERCIEN
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #ifndef _TCollection_HAsciiString_HeaderFile
18 #define _TCollection_HAsciiString_HeaderFile
20 #include <Standard.hxx>
21 #include <Standard_Type.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <Standard_Transient.hxx>
25 #include <Standard_CString.hxx>
26 #include <Standard_Character.hxx>
27 #include <Standard_Integer.hxx>
28 #include <Standard_Real.hxx>
29 #include <Standard_Boolean.hxx>
30 #include <Standard_OStream.hxx>
31 class Standard_NullObject;
32 class Standard_OutOfRange;
33 class Standard_NumericError;
34 class Standard_NegativeValue;
35 class TCollection_AsciiString;
36 class TCollection_HExtendedString;
39 class TCollection_HAsciiString;
40 DEFINE_STANDARD_HANDLE(TCollection_HAsciiString, Standard_Transient)
42 //! A variable-length sequence of ASCII characters
43 //! (normal 8-bit character type). It provides editing
44 //! operations with built-in memory management to
45 //! make HAsciiString objects easier to use than ordinary character arrays.
46 //! HAsciiString objects are handles to strings.
47 //! - HAsciiString strings may be shared by several objects.
48 //! - You may use an AsciiString object to get the actual string.
49 //! Note: HAsciiString objects use an AsciiString string as a field.
50 class TCollection_HAsciiString : public Standard_Transient
56 //! Initializes a HAsciiString to an empty AsciiString.
57 Standard_EXPORT TCollection_HAsciiString();
59 //! Initializes a HAsciiString with a CString.
60 Standard_EXPORT TCollection_HAsciiString(const Standard_CString message);
62 //! Initializes a HAsciiString with a single character.
63 Standard_EXPORT TCollection_HAsciiString(const Standard_Character aChar);
65 //! Initializes a HAsciiString with <length> space allocated.
66 //! and filled with <filler>.This is useful for buffers.
67 Standard_EXPORT TCollection_HAsciiString(const Standard_Integer length, const Standard_Character filler);
69 //! Initializes a HAsciiString with an integer value
70 Standard_EXPORT TCollection_HAsciiString(const Standard_Integer value);
72 //! Initializes a HAsciiString with a real value
73 Standard_EXPORT TCollection_HAsciiString(const Standard_Real value);
75 //! Initializes a HAsciiString with a AsciiString.
76 Standard_EXPORT TCollection_HAsciiString(const TCollection_AsciiString& aString);
78 //! Initializes a HAsciiString with a HAsciiString.
79 Standard_EXPORT TCollection_HAsciiString(const Handle(TCollection_HAsciiString)& aString);
81 //! Initializes a HAsciiString with a HExtendedString.
82 //! If replaceNonAscii is non-null charecter, it will be used
83 //! in place of any non-ascii character found in the source string.
84 //! Otherwise, creates UTF-8 unicode string.
85 Standard_EXPORT TCollection_HAsciiString(const Handle(TCollection_HExtendedString)& aString, const Standard_Character replaceNonAscii);
87 //! Appends <other> to me.
88 void AssignCat (const Standard_CString other);
90 //! Appends <other> to me.
91 //! Example: aString = aString + anotherString
92 void AssignCat (const Handle(TCollection_HAsciiString)& other);
94 //! Converts the first character into its corresponding
95 //! upper-case character and the other characters into lowercase.
101 Standard_EXPORT void Capitalize();
103 //! Creates a new string by concatenation of this
104 //! ASCII string and the other ASCII string.
106 //! aString = aString + anotherString
107 //! aString = aString + "Dummy"
108 //! aString contains "I say "
109 //! aString = aString + "Hello " + "Dolly"
110 //! gives "I say Hello Dolly"
111 //! Warning: To catenate more than one CString, you must put a String before.
112 //! So the following example is WRONG !
113 //! aString = "Hello " + "Dolly" THIS IS NOT ALLOWED
114 //! This rule is applicable to AssignCat (operator +=) too.
115 Standard_EXPORT Handle(TCollection_HAsciiString) Cat (const Standard_CString other) const;
117 //! Creates a new string by concatenation of this
118 //! ASCII string and the other ASCII string.
119 //! Example: aString = aString + anotherString
120 Standard_EXPORT Handle(TCollection_HAsciiString) Cat (const Handle(TCollection_HAsciiString)& other) const;
122 //! Modifies this ASCII string so that its length
123 //! becomes equal to Width and the new characters
124 //! are equal to Filler. New characters are added
125 //! both at the beginning and at the end of this string.
126 //! If Width is less than the length of this ASCII string, nothing happens.
128 //! Handle(TCollection_HAsciiString)
131 //! TCollection_HAsciiString
133 //! myAlphabet->Center(9,' ');
134 //! assert ( !strcmp(
135 //! myAlphabet->ToCString(),
137 Standard_EXPORT void Center (const Standard_Integer Width, const Standard_Character Filler);
139 //! Replaces all characters equal to aChar by
140 //! NewChar in this ASCII string. The substitution is
141 //! case sensitive if CaseSensitive is true (default value).
142 //! If you do not use the default case sensitive
143 //! option, it does not matter whether aChar is upper-case or not.
145 //! Handle(TCollection_HAsciiString)
147 //! TCollection_HAsciiString
149 //! myMistake->ChangeAll('H','F');
150 //! assert ( !strcmp(
151 //! myMistake->ToCString(),
153 Standard_EXPORT void ChangeAll (const Standard_Character aChar, const Standard_Character NewChar, const Standard_Boolean CaseSensitive = Standard_True);
155 //! Removes all characters contained in <me>.
156 //! This produces an empty HAsciiString.
157 Standard_EXPORT void Clear();
159 //! Returns the index of the first character of <me> that is
160 //! present in <Set>.
161 //! The search begins to the index FromIndex and ends to the
162 //! the index ToIndex.
163 //! Returns zero if failure.
164 //! Raises an exception if FromIndex or ToIndex is out of range
167 //! me = "aabAcAa", S = "Aa", FromIndex = 1, Toindex = 7
172 Standard_EXPORT Standard_Integer FirstLocationInSet (const Handle(TCollection_HAsciiString)& Set, const Standard_Integer FromIndex, const Standard_Integer ToIndex) const;
174 //! Returns the index of the first character of <me>
175 //! that is not present in the set <Set>.
176 //! The search begins to the index FromIndex and ends to the
177 //! the index ToIndex in <me>.
178 //! Returns zero if failure.
179 //! Raises an exception if FromIndex or ToIndex is out of range.
182 //! me = "aabAcAa", S = "Aa", FromIndex = 1, Toindex = 7
187 Standard_EXPORT Standard_Integer FirstLocationNotInSet (const Handle(TCollection_HAsciiString)& Set, const Standard_Integer FromIndex, const Standard_Integer ToIndex) const;
189 //! Insert a Character at position <where>.
191 //! aString contains "hy not ?"
192 //! aString.Insert(1,'W'); gives "Why not ?"
193 //! aString contains "Wh"
194 //! aString.Insert(3,'y'); gives "Why"
195 //! aString contains "Way"
196 //! aString.Insert(2,'h'); gives "Why"
197 Standard_EXPORT void Insert (const Standard_Integer where, const Standard_Character what);
199 //! Insert a HAsciiString at position <where>.
200 Standard_EXPORT void Insert (const Standard_Integer where, const Standard_CString what);
202 //! Insert a HAsciiString at position <where>.
203 Standard_EXPORT void Insert (const Standard_Integer where, const Handle(TCollection_HAsciiString)& what);
205 //! Inserts the other ASCII string a after a specific index in the string <me>
208 //! me = "cde" , Index = 0 , other = "ab"
210 //! me = "abcde" , other = "ab"
211 Standard_EXPORT void InsertAfter (const Standard_Integer Index, const Handle(TCollection_HAsciiString)& other);
213 //! Inserts the other ASCII string a before a specific index in the string <me>
214 //! Raises an exception if Index is out of bounds
217 //! me = "cde" , Index = 1 , other = "ab"
219 //! me = "abcde" , other = "ab"
220 Standard_EXPORT void InsertBefore (const Standard_Integer Index, const Handle(TCollection_HAsciiString)& other);
222 //! Returns True if the string <me> contains zero character
223 Standard_EXPORT Standard_Boolean IsEmpty() const;
225 //! Returns TRUE if <me> is 'ASCII' less than <other>.
226 Standard_EXPORT Standard_Boolean IsLess (const Handle(TCollection_HAsciiString)& other) const;
228 //! Returns TRUE if <me> is 'ASCII' greater than <other>.
229 Standard_EXPORT Standard_Boolean IsGreater (const Handle(TCollection_HAsciiString)& other) const;
231 //! Converts a HAsciiString containing a numeric expression to
233 //! Example: "215" returns 215.
234 Standard_EXPORT Standard_Integer IntegerValue() const;
236 //! Returns True if the string contains an integer value.
237 Standard_EXPORT Standard_Boolean IsIntegerValue() const;
239 //! Returns True if the string contains a real value.
240 Standard_EXPORT Standard_Boolean IsRealValue() const;
242 //! Returns True if the string contains only ASCII characters
243 //! between ' ' and '~'.
244 //! This means no control character and no extended ASCII code.
245 Standard_EXPORT Standard_Boolean IsAscii() const;
247 //! Returns True if the string S not contains same characters than
249 Standard_EXPORT Standard_Boolean IsDifferent (const Handle(TCollection_HAsciiString)& S) const;
251 //! Returns True if the string S contains same characters than the
253 Standard_EXPORT Standard_Boolean IsSameString (const Handle(TCollection_HAsciiString)& S) const;
255 //! Returns True if the string S contains same characters than the
257 Standard_EXPORT Standard_Boolean IsSameString (const Handle(TCollection_HAsciiString)& S, const Standard_Boolean CaseSensitive) const;
259 //! Removes all space characters in the begining of the string
260 Standard_EXPORT void LeftAdjust();
263 //! Length becomes equal to Width and the new characters are
265 //! if Width < Length nothing happens
266 //! Raises an exception if Width is less than zero
269 //! me = "abcdef" , Width = 9 , Filler = ' '
272 Standard_EXPORT void LeftJustify (const Standard_Integer Width, const Standard_Character Filler);
274 //! Returns number of characters in <me>.
275 //! This is the same functionality as 'strlen' in C.
276 Standard_Integer Length() const;
278 //! returns an index in the string <me> of the first occurence
279 //! of the string S in the string <me> from the starting index
280 //! FromIndex to the ending index ToIndex
281 //! returns zero if failure
282 //! Raises an exception if FromIndex or ToIndex is out of range.
285 //! me = "aabAaAa", S = "Aa", FromIndex = 1, ToIndex = 7
290 Standard_EXPORT Standard_Integer Location (const Handle(TCollection_HAsciiString)& other, const Standard_Integer FromIndex, const Standard_Integer ToIndex) const;
292 //! Returns the index of the nth occurence of the character C
293 //! in the string <me> from the starting index FromIndex to the
294 //! ending index ToIndex.
295 //! Returns zero if failure.
296 //! Raises an exception if FromIndex or ToIndex is out of range
299 //! me = "aabAa", N = 3, C = 'a', FromIndex = 1, ToIndex = 5
303 Standard_EXPORT Standard_Integer Location (const Standard_Integer N, const Standard_Character C, const Standard_Integer FromIndex, const Standard_Integer ToIndex) const;
305 //! Converts <me> to its lower-case equivalent.
306 Standard_EXPORT void LowerCase();
308 //! Inserts the other string at the begining of the string <me>
311 //! me = "cde" , S = "ab"
313 //! me = "abcde" , S = "ab"
314 Standard_EXPORT void Prepend (const Handle(TCollection_HAsciiString)& other);
316 //! Prints this string on the stream <astream>.
317 Standard_EXPORT void Print (Standard_OStream& astream) const;
319 //! Converts a string containing a numeric expression to a Real.
321 //! "215" returns 215.0.
322 //! "3.14159267" returns 3.14159267.
323 Standard_EXPORT Standard_Real RealValue() const;
325 //! Remove all the occurences of the character C in the string
328 //! me = "HellLLo", C = 'L' , CaseSensitive = True
331 Standard_EXPORT void RemoveAll (const Standard_Character C, const Standard_Boolean CaseSensitive);
333 //! Removes every <what> characters from <me>
334 Standard_EXPORT void RemoveAll (const Standard_Character what);
336 //! Erases <ahowmany> characters from position <where>,
337 //! <where> included.
339 //! aString contains "Hello"
340 //! aString.Erase(2,2) erases 2 characters from position 1
341 //! This gives "Hlo".
342 Standard_EXPORT void Remove (const Standard_Integer where, const Standard_Integer ahowmany = 1);
344 //! Removes all space characters at the end of the string.
345 Standard_EXPORT void RightAdjust();
348 //! Length becomes equal to Width and the new characters are
350 //! if Width < Length nothing happens
351 //! Raises an exception if Width is less than zero
354 //! me = "abcdef" , Width = 9 , Filler = ' '
357 Standard_EXPORT void RightJustify (const Standard_Integer Width, const Standard_Character Filler);
359 //! Searches a CString in <me> from the beginning
360 //! and returns position of first item <what> matching.
361 //! It returns -1 if not found.
363 //! aString contains "Sample single test"
364 //! aString.Search("le") returns 5
365 Standard_EXPORT Standard_Integer Search (const Standard_CString what) const;
367 //! Searches a String in <me> from the beginning
368 //! and returns position of first item <what> matching.
369 //! it returns -1 if not found.
370 Standard_EXPORT Standard_Integer Search (const Handle(TCollection_HAsciiString)& what) const;
372 //! Searches a CString in a String from the end
373 //! and returns position of first item <what> matching.
374 //! It returns -1 if not found.
376 //! aString contains "Sample single test"
377 //! aString.SearchFromEnd("le") returns 12
378 Standard_EXPORT Standard_Integer SearchFromEnd (const Standard_CString what) const;
380 //! Searches a HAsciiString in another HAsciiString from the end
381 //! and returns position of first item <what> matching.
382 //! It returns -1 if not found.
383 Standard_EXPORT Standard_Integer SearchFromEnd (const Handle(TCollection_HAsciiString)& what) const;
385 //! Replaces one character in the string at position <where>.
386 //! If <where> is less than zero or greater than the length of <me>
387 //! an exception is raised.
389 //! aString contains "Garbake"
390 //! astring.Replace(6,'g') gives <me> = "Garbage"
391 Standard_EXPORT void SetValue (const Standard_Integer where, const Standard_Character what);
393 //! Replaces a part of <me> in the string at position <where>.
394 //! If <where> is less than zero or greater than the length of <me>
395 //! an exception is raised.
397 //! aString contains "Garbake"
398 //! astring.Replace(6,'g') gives <me> = "Garbage"
399 Standard_EXPORT void SetValue (const Standard_Integer where, const Standard_CString what);
401 //! Replaces a part of <me> by another string.
402 Standard_EXPORT void SetValue (const Standard_Integer where, const Handle(TCollection_HAsciiString)& what);
404 //! Splits a HAsciiString into two sub-strings.
406 //! aString contains "abcdefg"
407 //! aString.Split(3) gives <me> = "abc" and returns "defg"
408 Standard_EXPORT Handle(TCollection_HAsciiString) Split (const Standard_Integer where);
410 //! Creation of a sub-string of the string <me>.
411 //! The sub-string starts to the index Fromindex and ends
412 //! to the index ToIndex.
413 //! Raises an exception if ToIndex or FromIndex is out of
417 //! me = "abcdefg", ToIndex=3, FromIndex=6
422 Standard_EXPORT Handle(TCollection_HAsciiString) SubString (const Standard_Integer FromIndex, const Standard_Integer ToIndex) const;
424 //! Returns pointer to string (char *)
425 //! This is useful for some casual manipulations
426 //! Because this "char *" is 'const', you can't modify its contents.
427 Standard_CString ToCString() const;
429 //! Extracts <whichone> token from <me>.
430 //! By default, the <separators> is set to space and tabulation.
431 //! By default, the token extracted is the first one (whichone = 1).
432 //! <separators> contains all separators you need.
433 //! If no token indexed by <whichone> is found, it returns an empty String.
435 //! aString contains "This is a message"
436 //! aString.Token() returns "This"
437 //! aString.Token(" ",4) returns "message"
438 //! aString.Token(" ",2) returns "is"
439 //! aString.Token(" ",9) returns ""
440 //! Other separators than space character and tabulation are allowed
441 //! aString contains "1234; test:message , value"
442 //! aString.Token("; :,",4) returns "value"
443 //! aString.Token("; :,",2) returns "test"
444 Standard_EXPORT Handle(TCollection_HAsciiString) Token (const Standard_CString separators = " \t", const Standard_Integer whichone = 1) const;
446 //! Truncates <me> to <ahowmany> characters.
447 //! Example: me = "Hello Dolly" -> Trunc(3) -> me = "Hel"
448 Standard_EXPORT void Trunc (const Standard_Integer ahowmany);
450 //! Converts <me> to its upper-case equivalent.
451 Standard_EXPORT void UpperCase();
453 //! Length of the string ignoring all spaces (' ') and the
454 //! control character at the end.
455 Standard_EXPORT Standard_Integer UsefullLength() const;
457 //! Returns character at position <where> in <me>.
458 //! If <where> is less than zero or greater than the lenght of
459 //! <me>, an exception is raised.
461 //! aString contains "Hello"
462 //! aString.Value(2) returns 'e'
463 Standard_EXPORT Standard_Character Value (const Standard_Integer where) const;
465 //! Returns the field myString.
466 const TCollection_AsciiString& String() const;
468 Standard_EXPORT Standard_Boolean IsSameState (const Handle(TCollection_HAsciiString)& other) const;
473 DEFINE_STANDARD_RTTIEXT(TCollection_HAsciiString,Standard_Transient)
483 TCollection_AsciiString myString;
489 #include <TCollection_HAsciiString.lxx>
495 #endif // _TCollection_HAsciiString_HeaderFile