nss: upgrade to release 3.73
[LibreOffice.git] / hwpfilter / source / mzstring.cxx
blob462ed91ad3fc8bd33a50f5e53530d94cddd4e5bd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 /* PURPOSE
21 * supposed to be used instead of std::string
24 #include "mzstring.h"
26 #ifdef _WIN32
27 # if !defined WIN32_LEAN_AND_MEAN
28 # define WIN32_LEAN_AND_MEAN
29 # endif
30 # include <windows.h>
31 #endif
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #ifndef _WIN32
37 # define wsprintfA sprintf
38 #endif
40 const int AllocSize = 8;
42 static int get_alloc_size(int len)
44 return (len + AllocSize - 1) / AllocSize * AllocSize;
48 MzString::MzString()
50 Length = 0;
51 Allocated = 0;
52 Data = nullptr;
56 MzString::~MzString()
58 if (Data)
59 free(Data);
63 MzString &MzString::operator=(const MzString &s)
65 if(this == &s)
66 return *this;
68 int n = s.length();
69 if (allocate(n))
71 if (n > 0) memcpy(Data, s.Data, n);
72 Length = n;
74 return *this;
78 MzString &MzString::operator = (const char *s)
80 if (s == nullptr)
81 s = "";
82 int n = strlen(s);
83 if (allocate(n))
85 if (n > 0) memcpy(Data, s, n);
86 Length = n;
88 return *this;
92 void MzString::append(const char *s, int slen)
94 if(!s || slen <= 0)
95 return;
97 int new_len = Length + slen;
98 if (allocate(new_len))
100 memcpy(Data + Length, s, slen);
101 Length = new_len;
106 void MzString::append(MzString const &s)
108 if (s.Data)
109 append(s.Data, s.length());
113 void MzString::append(const char *s)
115 if (!s) return;
116 append(s, strlen(s));
120 int MzString::compare(const char *s)
122 if (!Data) return -1;
123 if (s==nullptr) return 1;
125 Data[Length] = 0;
126 return strcmp(Data, s);
130 int MzString::find(char ch)
132 return find(ch,0);
136 int MzString::find(char ch, int pos)
138 for (int i = pos; i < Length; i++)
140 if (Data[i] == ch)
141 return i;
143 return -1;
147 int MzString::rfind(char ch)
149 return rfind(ch, Length - 1);
153 int MzString::rfind(char ch, int pos)
155 if (pos >= Length)
156 return -1;
158 while (pos >= 0)
160 if (Data[pos] == ch)
161 return pos;
162 pos--;
164 return -1;
168 // << operator
169 MzString &MzString::operator << (const char *str)
171 append(str);
172 return *this;
176 MzString &MzString::operator << (char ch)
178 append(&ch, 1);
179 return *this;
183 MzString &MzString::operator << (int i)
185 char str[80];
187 wsprintfA(str, "%d", i);
188 append(str);
189 return *this;
193 MzString &MzString::operator << (tools::Long l)
195 char str[80];
197 wsprintfA(str, "%ld", l);
198 append(str);
199 return *this;
203 MzString &MzString::operator << (MzString const &s)
205 append(s);
206 return *this;
210 char MzString::operator [] (int n)
212 if (Data && 0 <= n && n < Length)
213 return Data[n];
215 return 0;
219 void MzString::replace(int pos, char ch)
221 if (Data && 0 <= pos && pos < Length)
222 Data[pos] = ch;
226 // Private Methods.
229 bool MzString::allocate(int len)
231 len++; // In case we want to add a null.
233 if (len < 0)
234 return false;
236 if (Data)
238 if (len < Allocated)
239 return true;
240 else
242 int n = get_alloc_size(len);
243 char *p = static_cast<char *>(realloc(Data, n));
244 if (p)
246 Data = p;
247 Allocated = n;
248 return true;
252 else
254 // In case we want to add a null.
255 int n = get_alloc_size(len);
256 Data = static_cast<char *>(malloc(n));
257 if (Data)
259 Allocated = n;
260 return true;
264 return false;
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */