1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mzstring.h,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
42 /** @name MzString class
44 It was supposed to be used instead of std::string.
48 When you declare an MzString, it is initially empty. There is no need to
49 do things like #MzString a = "";#, especially not in constructors.
51 If you want to use a default empty MzString as a parameter, use
53 #void foo(MzString par = MzString()); // Correct#
57 #void foo(MzString par = ""); // WRONG!#
58 #void foo(MzString par = 0); // WRONG!#
60 (The last one is only wrong because some compilers can't handle it.)
62 Methods that take an index as parameter all follow this rule: Valid indexes
63 go from 0 to length()-1.
65 Correct: & #foo.substr(0, length()-1);# \\
66 Wrong: & #bar.substr(0, length());#
69 It is important that you declare MzStrings as const if possible, because
70 some methods are much more efficient in const versions.
72 If you want to check whether a string is empty, do
74 #if (foo.empty()) something right#
76 rather than something along the lines of
78 #if (!foo) completely wrong#
80 When you use the #.copy()# method, MzString calls "#new []#", so you have to
81 release the memory with #delete[]#. Don't preallocate memory.
83 When you want to copy an MzString, just do
85 #MzString a, b = "String";#
86 #a = b; // That's it!#
90 #MzString a, b = "String";#
93 The class automatically handles deep copying when required.
99 MzString(); // Create an empty string
100 // if len = 0, len becomes s.length)
101 MzString(MzString
const &s
, int len
= 0);
105 const char* c_str() const;
106 operator char*() { return (char *)c_str(); }
108 // If it is not posible to use the constructor with an initial
109 // allocation size, use the following member to set the size.
110 bool resize(int len
);
113 void operator = (MzString
&s
);
114 void operator = (const char *s
);
117 MzString
&operator += (char);
118 MzString
&operator += (const char *);
119 MzString
&operator += (MzString
const &);
121 MzString
&operator << (const char *);
122 MzString
&operator << (char);
123 MzString
&operator << (unsigned char c
) { return *this<<(char)c
; }
124 MzString
&operator << (int);
125 MzString
&operator << (long);
126 MzString
&operator << (short i
) { return *this<<(int)i
; }
127 MzString
&operator << (MzString
const &);
128 /* MzString &operator << (MzString *s) { return *this<<*s; }
131 char operator >> (char &c);
133 // Access to specific characters
134 //char &operator [] (int n);
135 char operator [] (int n
);
140 // 0 : 'this' is equal to 's'.
141 // -1 : 'this' is less than 's'.
142 // 1 : 'this' is greater than 's'.
143 int compare(const char *s
);
145 // Searching for parts
147 int find (char c
, int pos
);
149 int find (char *, int pos
);
151 int rfind (char c
, int pos
);
154 void replace(int, char c
);
156 void append (MzString
const &s
);
157 void append (const char *s
);
158 void append (const char *s
, int n
);
161 int Length
; // Current Length
162 int Allocated
; // Total space allocated
163 char *Data
; // The actual contents
165 // Allocate some space for the data.
166 // Delete Data if it has been allocated.
167 bool allocate(int len
);
170 inline int MzString::length() const
176 inline const char* MzString::c_str() const
180 Data
[Length
] = '\0'; // We always leave room for this.
181 return (const char *)Data
;
188 // Non friend, non member operators
190 #endif /* _MZSTRING_H_ */