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.cpp,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 ************************************************************************/
31 /* NAME $Id: mzstring.cpp,v 1.5 2008-06-04 10:02:20 vg Exp $
33 * supposed to be used instead of std::string
37 * frog - Oct 8, 1998: Created.
41 #pragma implementation "mzstring.h"
44 //#include "stdafx.h" //
51 #pragma warning(push, 1)
57 //# include <crtdbg.h>
64 # define wsprintf sprintf
67 const int AllocSize
= 8;
69 inline int get_alloc_size(int len
)
71 return (len
+ AllocSize
- 1) / AllocSize
* AllocSize
;
90 void MzString::operator = (MzString
&s
)
95 if (n
> 0) memcpy(Data
, s
.Data
, n
);
101 void MzString::operator = (const char *s
)
108 if (n
> 0) memcpy(Data
, s
, n
);
114 void MzString::append(const char *s
, int slen
)
119 int new_len
= Length
+ slen
;
122 memcpy(Data
+ Length
, s
, slen
);
128 void MzString::append(MzString
const &s
)
131 append(s
.Data
, s
.length());
135 void MzString::append(const char *s
)
138 append(s
, strlen(s
));
142 int MzString::compare(const char *s
)
144 if (!Data
) return -1;
145 if (s
==NULL
) return 1;
148 return strcmp(Data
, s
);
152 int MzString::find(char ch
)
158 int MzString::find(char ch
, int pos
)
160 for (int i
= pos
; i
< Length
; i
++)
169 int MzString::rfind(char ch
)
171 return rfind(ch
, Length
- 1);
175 int MzString::rfind(char ch
, int pos
)
192 MzString
&MzString::operator += (char ch
)
199 MzString
&MzString::operator += (const char *str
)
206 MzString
&MzString::operator += (MzString
const &s
)
214 MzString
&MzString::operator << (const char *str
)
221 MzString
&MzString::operator << (char ch
)
228 MzString
&MzString::operator << (int i
)
232 wsprintf(str
, "%d", i
);
238 MzString
&MzString::operator << (long l
)
242 wsprintf(str
, "%ld", l
);
248 MzString
&MzString::operator << (MzString
const &s
)
255 char MzString::operator [] (int n
)
257 if (Data
&& 0 <= n
&& n
< Length
)
264 void MzString::replace(int pos
, char ch
)
266 if (Data
&& 0 <= pos
&& pos
< Length
)
271 //------------------------------------------------------------------------
275 bool MzString::allocate(int len
)
277 len
++; // In case we want to add a null.
288 int n
= get_alloc_size(len
);
289 char *p
= (char *)realloc(Data
, n
);
300 // In case we want to add a null.
301 int n
= get_alloc_size(len
);
302 Data
= (char *)malloc(n
);
314 bool MzString::resize(int len
)
316 return allocate(len
);