1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 * supposed to be used instead of std::string
30 #pragma warning(push, 1)
42 # define wsprintf sprintf
45 const int AllocSize
= 8;
47 inline int get_alloc_size(int len
)
49 return (len
+ AllocSize
- 1) / AllocSize
* AllocSize
;
68 MzString
&MzString::operator = (MzString
&s
)
73 if (n
> 0) memcpy(Data
, s
.Data
, n
);
80 MzString
&MzString::operator = (const char *s
)
87 if (n
> 0) memcpy(Data
, s
, n
);
94 void MzString::append(const char *s
, int slen
)
99 int new_len
= Length
+ slen
;
102 memcpy(Data
+ Length
, s
, slen
);
108 void MzString::append(MzString
const &s
)
111 append(s
.Data
, s
.length());
115 void MzString::append(const char *s
)
118 append(s
, strlen(s
));
122 int MzString::compare(const char *s
)
124 if (!Data
) return -1;
125 if (s
==NULL
) return 1;
128 return strcmp(Data
, s
);
132 int MzString::find(char ch
)
138 int MzString::find(char ch
, int pos
)
140 for (int i
= pos
; i
< Length
; i
++)
149 int MzString::rfind(char ch
)
151 return rfind(ch
, Length
- 1);
155 int MzString::rfind(char ch
, int pos
)
172 MzString
&MzString::operator += (char ch
)
179 MzString
&MzString::operator += (const char *str
)
186 MzString
&MzString::operator += (MzString
const &s
)
194 MzString
&MzString::operator << (const char *str
)
201 MzString
&MzString::operator << (char ch
)
208 MzString
&MzString::operator << (int i
)
212 wsprintf(str
, "%d", i
);
218 MzString
&MzString::operator << (long l
)
222 wsprintf(str
, "%ld", l
);
228 MzString
&MzString::operator << (MzString
const &s
)
235 char MzString::operator [] (int n
)
237 if (Data
&& 0 <= n
&& n
< Length
)
244 void MzString::replace(int pos
, char ch
)
246 if (Data
&& 0 <= pos
&& pos
< Length
)
255 bool MzString::allocate(int len
)
257 len
++; // In case we want to add a null.
268 int n
= get_alloc_size(len
);
269 char *p
= static_cast<char *>(realloc(Data
, n
));
280 // In case we want to add a null.
281 int n
= get_alloc_size(len
);
282 Data
= static_cast<char *>(malloc(n
));
294 bool MzString::resize(int len
)
296 return allocate(len
);
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */