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
27 # if !defined WIN32_LEAN_AND_MEAN
28 # define WIN32_LEAN_AND_MEAN
37 # define wsprintfA sprintf
40 const int AllocSize
= 8;
42 static int get_alloc_size(int len
)
44 return (len
+ AllocSize
- 1) / AllocSize
* AllocSize
;
63 MzString
&MzString::operator=(const MzString
&s
)
71 if (n
> 0) memcpy(Data
, s
.Data
, n
);
78 MzString
&MzString::operator = (const char *s
)
85 if (n
> 0) memcpy(Data
, s
, n
);
92 void MzString::append(const char *s
, int slen
)
97 int new_len
= Length
+ slen
;
98 if (allocate(new_len
))
100 memcpy(Data
+ Length
, s
, slen
);
106 void MzString::append(MzString
const &s
)
109 append(s
.Data
, s
.length());
113 void MzString::append(const char *s
)
116 append(s
, strlen(s
));
120 int MzString::compare(const char *s
)
122 if (!Data
) return -1;
123 if (s
==nullptr) return 1;
126 return strcmp(Data
, s
);
130 int MzString::find(char ch
)
136 int MzString::find(char ch
, int pos
)
138 for (int i
= pos
; i
< Length
; i
++)
147 int MzString::rfind(char ch
)
149 return rfind(ch
, Length
- 1);
153 int MzString::rfind(char ch
, int pos
)
169 MzString
&MzString::operator << (const char *str
)
176 MzString
&MzString::operator << (char ch
)
183 MzString
&MzString::operator << (int i
)
187 wsprintfA(str
, "%d", i
);
193 MzString
&MzString::operator << (tools::Long l
)
197 wsprintfA(str
, "%ld", l
);
203 MzString
&MzString::operator << (MzString
const &s
)
210 char MzString::operator [] (int n
)
212 if (Data
&& 0 <= n
&& n
< Length
)
219 void MzString::replace(int pos
, char ch
)
221 if (Data
&& 0 <= pos
&& pos
< Length
)
229 bool MzString::allocate(int len
)
231 len
++; // In case we want to add a null.
242 int n
= get_alloc_size(len
);
243 char *p
= static_cast<char *>(realloc(Data
, n
));
254 // In case we want to add a null.
255 int n
= get_alloc_size(len
);
256 Data
= static_cast<char *>(malloc(n
));
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */