1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 * supposed to be used instead of std::string
39 #pragma warning(push, 1)
51 # define wsprintf sprintf
54 const int AllocSize
= 8;
56 inline int get_alloc_size(int len
)
58 return (len
+ AllocSize
- 1) / AllocSize
* AllocSize
;
77 MzString
&MzString::operator = (MzString
&s
)
82 if (n
> 0) memcpy(Data
, s
.Data
, n
);
89 MzString
&MzString::operator = (const char *s
)
96 if (n
> 0) memcpy(Data
, s
, n
);
103 void MzString::append(const char *s
, int slen
)
108 int new_len
= Length
+ slen
;
111 memcpy(Data
+ Length
, s
, slen
);
117 void MzString::append(MzString
const &s
)
120 append(s
.Data
, s
.length());
124 void MzString::append(const char *s
)
127 append(s
, strlen(s
));
131 int MzString::compare(const char *s
)
133 if (!Data
) return -1;
134 if (s
==NULL
) return 1;
137 return strcmp(Data
, s
);
141 int MzString::find(char ch
)
147 int MzString::find(char ch
, int pos
)
149 for (int i
= pos
; i
< Length
; i
++)
158 int MzString::rfind(char ch
)
160 return rfind(ch
, Length
- 1);
164 int MzString::rfind(char ch
, int pos
)
181 MzString
&MzString::operator += (char ch
)
188 MzString
&MzString::operator += (const char *str
)
195 MzString
&MzString::operator += (MzString
const &s
)
203 MzString
&MzString::operator << (const char *str
)
210 MzString
&MzString::operator << (char ch
)
217 MzString
&MzString::operator << (int i
)
221 wsprintf(str
, "%d", i
);
227 MzString
&MzString::operator << (long l
)
231 wsprintf(str
, "%ld", l
);
237 MzString
&MzString::operator << (MzString
const &s
)
244 char MzString::operator [] (int n
)
246 if (Data
&& 0 <= n
&& n
< Length
)
253 void MzString::replace(int pos
, char ch
)
255 if (Data
&& 0 <= pos
&& pos
< Length
)
260 //------------------------------------------------------------------------
264 bool MzString::allocate(int len
)
266 len
++; // In case we want to add a null.
277 int n
= get_alloc_size(len
);
278 char *p
= (char *)realloc(Data
, n
);
289 // In case we want to add a null.
290 int n
= get_alloc_size(len
);
291 Data
= (char *)malloc(n
);
303 bool MzString::resize(int len
)
305 return allocate(len
);
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */