Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : :
21 : : #include <sistr.hxx>
22 : :
23 : : // The following two header-files declare
24 : : // standard ANSI-C++ functions. They may be replaced
25 : : // by the corresponding header-file-names of the
26 : : // actually used runtime library.
27 : : #include <string.h> // strlen(), memcpy(), memset()
28 : : #include <ctype.h> // tolower()
29 : : #include <limits.h> // INT_MAX
30 : :
31 : : #if (_MSC_VER >=1400)
32 : : #pragma warning(disable:4365)
33 : : #endif
34 : :
35 : : const char NULCH = '\0';
36 : : const int NO_POS = -1;
37 : :
38 : :
39 : 4572 : Simstr::Simstr(const char * str_)
40 : : {
41 [ + + ]: 4572 : if (str_ == 0)
42 : : {
43 : 2696 : len = 0;
44 : 2696 : sz = new char[1];
45 : 2696 : *sz = 0;
46 : : }
47 : : else
48 : : {
49 : 1876 : len = strlen(str_);
50 : 1876 : sz = new char[len+1];
51 : 1876 : memcpy(sz,str_,len+1);
52 : : }
53 : 4572 : }
54 : :
55 : 0 : Simstr::Simstr( const char * anybytes,
56 : : int firstBytesPos,
57 : 0 : int nrOfBytes)
58 : : {
59 : 0 : unsigned slen = strlen(anybytes);
60 [ # # ][ # # ]: 0 : if (anybytes == 0 || slen <= unsigned(firstBytesPos))
61 : : {
62 : 0 : len = 0;
63 : 0 : sz = new char[1];
64 : 0 : *sz = 0;
65 : : }
66 : : else
67 : : {
68 : 0 : int maxLen = slen - unsigned(firstBytesPos);
69 : : len = maxLen < nrOfBytes
70 : : ? maxLen
71 [ # # ]: 0 : : nrOfBytes;
72 : 0 : sz = new char[len+1];
73 : 0 : memcpy(sz,anybytes+firstBytesPos,len);
74 : 0 : *(sz+len) = 0;
75 : : }
76 : 0 : }
77 : :
78 : :
79 : 0 : Simstr::Simstr(const Simstr & S)
80 : : {
81 : 0 : len = S.len;
82 : 0 : sz = new char[len+1];
83 : 0 : memcpy(sz,S.sz,len+1);
84 : 0 : }
85 : :
86 : 2012 : Simstr & Simstr::operator=(const Simstr & S)
87 : : {
88 [ - + ]: 2012 : if (sz == S.sz)
89 : 0 : return *this;
90 : :
91 [ + - ]: 2012 : delete [] sz;
92 : :
93 : 2012 : len = S.len;
94 : 2012 : sz = new char[len+1];
95 : 2012 : memcpy(sz,S.sz,len+1);
96 : :
97 : 2012 : return *this;
98 : : }
99 : :
100 : 4024 : Simstr::~Simstr()
101 : : {
102 [ + - ]: 4024 : delete [] sz;
103 [ - + ]: 4024 : }
104 : :
105 : : Simstr
106 : 0 : Simstr::operator+(const Simstr & S) const
107 : : {
108 : 0 : Simstr ret = sz;
109 [ # # ]: 0 : ret.push_back(S);
110 : 0 : return ret;
111 : : }
112 : :
113 : : Simstr &
114 : 0 : Simstr::operator+=(const Simstr & S)
115 : : {
116 : 0 : push_back(S);
117 : 0 : return *this;
118 : : }
119 : :
120 : :
121 : : // REL
122 : :
123 : : bool
124 : 12 : Simstr::operator==(const Simstr & S) const
125 : 12 : { return !strcmp(sz,S.sz) ? true : false; }
126 : :
127 : : bool
128 : 18 : Simstr::operator!=(const Simstr & S) const
129 : 18 : { return strcmp(sz,S.sz) ? true : false; }
130 : :
131 : : bool
132 : 0 : Simstr::operator<(const Simstr & S) const
133 : 0 : { return (strcmp(sz,S.sz) < 0) ? true : false; }
134 : :
135 : : bool
136 : 0 : Simstr::operator>(const Simstr & S) const
137 : 0 : { return (strcmp(sz,S.sz) > 0) ? true : false; }
138 : :
139 : : bool
140 : 0 : Simstr::operator<=(const Simstr & S) const
141 : 0 : { return (strcmp(sz,S.sz) <= 0) ? true : false; }
142 : :
143 : : bool
144 : 0 : Simstr::operator>=(const Simstr & S) const
145 : 0 : { return (strcmp(sz,S.sz) >= 0) ? true : false; }
146 : :
147 : :
148 : :
149 : :
150 : : // ************** LIST - Funktionen *****************
151 : :
152 : : // Insert
153 : :
154 : : void
155 : 0 : Simstr::push_front(char c)
156 : : {
157 : 0 : char * result = new char[len+2];
158 : :
159 : 0 : result[0] = c;
160 : 0 : memcpy(result+1,sz,len+1);
161 : :
162 [ # # ]: 0 : delete [] sz;
163 : 0 : sz = result;
164 : 0 : len++;
165 : 0 : }
166 : :
167 : : void
168 : 0 : Simstr::push_back(char c)
169 : : {
170 : 0 : char * result = new char[len+2];
171 : :
172 : 0 : memcpy(result,sz,len);
173 : 0 : result[len] = c;
174 : 0 : result[len+1] = 0;
175 : :
176 [ # # ]: 0 : delete [] sz;
177 : 0 : sz = result;
178 : 0 : len++;
179 : 0 : }
180 : :
181 : : void
182 : 0 : Simstr::push_front(const Simstr & S)
183 : : {
184 : 0 : char * result = new char[len+1+S.len];
185 : :
186 : 0 : memcpy(result,S.sz,S.len);
187 : 0 : memcpy(result+S.len,sz,len+1);
188 : :
189 [ # # ]: 0 : delete [] sz;
190 : 0 : sz = result;
191 : 0 : len += S.len;
192 : 0 : }
193 : :
194 : : void
195 : 0 : Simstr::push_back(const Simstr & S)
196 : : {
197 : 0 : char * result = new char[len+1+S.len];
198 : :
199 : 0 : memcpy(result,sz,len);
200 : 0 : memcpy(result+len,S.sz,S.len+1);
201 : :
202 [ # # ]: 0 : delete [] sz;
203 : 0 : sz = result;
204 : 0 : len += S.len;
205 : 0 : }
206 : :
207 : :
208 : : // Remove
209 : :
210 : : void
211 : 0 : Simstr::remove(int pos, int anzahl)
212 : : {
213 [ # # ][ # # ]: 0 : if (pos >= len || pos < 0 || anzahl < 1)
[ # # ]
214 : 0 : return;
215 : :
216 : 0 : int anz = len - pos < anzahl ? len - pos : anzahl;
217 : :
218 : 0 : char * result = new char[len-anz+1];
219 : :
220 : 0 : memcpy(result,sz,pos);
221 : 0 : memcpy(result+pos,sz+pos+anz,len-pos-anz+1);
222 : :
223 [ # # ]: 0 : delete [] sz;
224 : 0 : sz = result;
225 : 0 : len -= anz;
226 : : }
227 : :
228 : : void
229 : 562 : Simstr::remove_trailing_blanks()
230 : : {
231 : 562 : int newlen = len-1;
232 [ + - ][ - + ]: 562 : for ( ; newlen > 1 && sz[newlen] <= 32; --newlen ) {}
[ - + ]
233 : :
234 [ - + ]: 562 : if (newlen < len-1)
235 : 0 : remove ( newlen+1, len-newlen);
236 : 562 : }
237 : :
238 : : // Find
239 : :
240 : : int
241 : 0 : Simstr::pos_first(char c) const
242 : : {
243 : 0 : int i = 0;
244 [ # # ][ # # ]: 0 : for (i = 0; i < len ? sz[i] != c : false; i++) ;
[ # # ]
245 [ # # ]: 0 : if (i >= len)
246 : 0 : return NO_POS;
247 : : else
248 : 0 : return i;
249 : : }
250 : :
251 : : int
252 : 0 : Simstr::pos_last(char c) const
253 : : {
254 : 0 : int i = 0;
255 [ # # ][ # # ]: 0 : for (i = len-1; i >= 0 ? sz[i] != c : false; i--) ;
[ # # ]
256 [ # # ]: 0 : if (i < 0)
257 : 0 : return NO_POS;
258 : : else
259 : 0 : return i;
260 : : }
261 : :
262 : : bool
263 : 0 : Simstr::is_no_text() const
264 : : {
265 [ # # ]: 0 : if (!len)
266 : 0 : return true;
267 : :
268 : : int i;
269 [ # # ][ # # ]: 0 : for (i = 0; i < len && sz[i] <= 32 ; i++) ;
[ # # ]
270 [ # # ]: 0 : if (i < len)
271 : 0 : return false;
272 : 0 : return true;
273 : : }
274 : :
275 : : // Change
276 : :
277 : : void
278 : 0 : Simstr::replace_all(char oldCh, char newCh)
279 : : {
280 [ # # ]: 0 : for (int i=0; i < len; i++)
281 [ # # ]: 0 : if (sz[i] == oldCh)
282 : 0 : sz[i] = newCh;
283 : 0 : }
284 : :
285 : : // Simstr addition
286 : : Simstr
287 : 0 : operator+(const char * str, const Simstr & S)
288 : : {
289 : 0 : Simstr ret = S;
290 [ # # ][ # # ]: 0 : ret.push_front(str);
291 : 0 : return ret;
292 : : }
293 : :
294 : : Simstr
295 : 0 : operator+(const Simstr & S, const char * str)
296 : : {
297 : 0 : Simstr ret = S;
298 [ # # ][ # # ]: 0 : ret.push_back(str);
299 : 0 : return ret;
300 : : }
301 : :
302 : : Simstr
303 : 0 : operator+(char c, const Simstr & S)
304 : : {
305 : 0 : Simstr ret = S;
306 [ # # ]: 0 : ret.push_front(c);
307 : 0 : return ret;
308 : : }
309 : :
310 : : Simstr
311 : 0 : operator+(const Simstr & S, char c)
312 : : {
313 : 0 : Simstr ret = S;
314 [ # # ]: 0 : ret.push_back(c);
315 : 0 : return ret;
316 : : }
317 : :
318 : :
319 : : // Simstr-Vergleiche mit char *
320 : : bool
321 : 0 : operator==(const Simstr & S, const char * str)
322 : : {
323 : 0 : return strcmp(S,str) == 0;
324 : : }
325 : :
326 : : bool
327 : 0 : operator!=(const Simstr & S, const char * str)
328 : : {
329 : 0 : return strcmp(S,str) != 0;
330 : : }
331 : :
332 : : bool
333 : 0 : operator<(const Simstr & S, const char * str)
334 : : {
335 : 0 : return strcmp(S,str) < 0;
336 : : }
337 : :
338 : : bool
339 : 0 : operator>(const Simstr & S, const char * str)
340 : : {
341 : 0 : return strcmp(S,str) > 0;
342 : : }
343 : :
344 : : bool
345 : 0 : operator<=(const Simstr & S, const char * str)
346 : : {
347 : 0 : return strcmp(S,str) <= 0;
348 : : }
349 : :
350 : : bool
351 : 0 : operator>=(const Simstr & S, const char * str)
352 : : {
353 : 0 : return strcmp(S,str) >= 0;
354 : : }
355 : :
356 : : bool
357 : 0 : operator==(const char * str, const Simstr & S)
358 : : {
359 : 0 : return strcmp(str,S) == 0;
360 : : }
361 : :
362 : : bool
363 : 0 : operator!=(const char * str, const Simstr & S)
364 : : {
365 : 0 : return strcmp(str,S) != 0;
366 : : }
367 : :
368 : : bool
369 : 0 : operator<(const char * str, const Simstr & S)
370 : : {
371 : 0 : return strcmp(str,S) < 0;
372 : : }
373 : :
374 : : bool
375 : 0 : operator>(const char * str, const Simstr & S)
376 : : {
377 : 0 : return strcmp(str,S) > 0;
378 : : }
379 : :
380 : : bool
381 : 0 : operator<=(const char * str, const Simstr & S)
382 : : {
383 : 0 : return strcmp(str,S) <= 0;
384 : : }
385 : :
386 : : bool
387 : 0 : operator>=(const char * str, const Simstr & S)
388 : : {
389 : 0 : return strcmp(str,S) >= 0;
390 : : }
391 : :
392 : :
393 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|