4 /* Copyright (C) 1989, 1990, 1991, 1992, 2001, 2002
5 Free Software Foundation, Inc.
6 Written by James Clark (jjc@jclark.com)
8 This file is part of groff.
10 groff is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 groff is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License along
21 with groff; see the file COPYING. If not, write to the Free Software
22 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include "stringclass.h"
28 static char *salloc(int len
, int *sizep
);
29 static void sfree(char *ptr
, int size
);
30 static char *sfree_alloc(char *ptr
, int size
, int len
, int *sizep
);
31 static char *srealloc(char *ptr
, int size
, int oldlen
, int newlen
, int *sizep
);
33 static char *salloc(int len
, int *sizep
)
40 return new char[*sizep
= len
*2];
43 static void sfree(char *ptr
, int)
48 static char *sfree_alloc(char *ptr
, int oldsz
, int len
, int *sizep
)
60 return new char[*sizep
= len
*2];
63 static char *srealloc(char *ptr
, int oldsz
, int oldlen
, int newlen
, int *sizep
)
65 if (oldsz
>= newlen
) {
75 char *p
= new char[*sizep
= newlen
*2];
76 if (oldlen
< newlen
&& oldlen
!= 0)
77 memcpy(p
, ptr
, oldlen
);
83 string::string() : ptr(0), len(0), sz(0)
87 string::string(const char *p
, int n
) : len(n
)
95 string::string(const char *p
)
104 ptr
= salloc(len
, &sz
);
109 string::string(char c
) : len(1)
111 ptr
= salloc(1, &sz
);
115 string::string(const string
&s
) : len(s
.len
)
117 ptr
= salloc(len
, &sz
);
119 memcpy(ptr
, s
.ptr
, len
);
127 string
&string::operator=(const string
&s
)
129 ptr
= sfree_alloc(ptr
, sz
, s
.len
, &sz
);
132 memcpy(ptr
, s
.ptr
, len
);
136 string
&string::operator=(const char *p
)
145 int slen
= strlen(p
);
146 ptr
= sfree_alloc(ptr
, sz
, slen
, &sz
);
153 string
&string::operator=(char c
)
155 ptr
= sfree_alloc(ptr
, sz
, 1, &sz
);
161 void string::move(string
&s
)
174 ptr
= srealloc(ptr
, sz
, len
, len
+ 1, &sz
);
177 string
&string::operator+=(const char *p
)
181 int newlen
= len
+ n
;
183 ptr
= srealloc(ptr
, sz
, len
, newlen
, &sz
);
184 memcpy(ptr
+ len
, p
, n
);
190 string
&string::operator+=(const string
&s
)
193 int newlen
= len
+ s
.len
;
195 ptr
= srealloc(ptr
, sz
, len
, newlen
, &sz
);
196 memcpy(ptr
+ len
, s
.ptr
, s
.len
);
202 void string::append(const char *p
, int n
)
205 int newlen
= len
+ n
;
207 ptr
= srealloc(ptr
, sz
, len
, newlen
, &sz
);
208 memcpy(ptr
+ len
, p
, n
);
213 string::string(const char *s1
, int n1
, const char *s2
, int n2
)
215 assert(n1
>= 0 && n2
>= 0);
222 ptr
= salloc(len
, &sz
);
228 memcpy(ptr
+ n1
, s2
, n2
);
233 int operator<=(const string
&s1
, const string
&s2
)
235 return (s1
.len
<= s2
.len
236 ? s1
.len
== 0 || memcmp(s1
.ptr
, s2
.ptr
, s1
.len
) <= 0
237 : s2
.len
!= 0 && memcmp(s1
.ptr
, s2
.ptr
, s2
.len
) < 0);
240 int operator<(const string
&s1
, const string
&s2
)
242 return (s1
.len
< s2
.len
243 ? s1
.len
== 0 || memcmp(s1
.ptr
, s2
.ptr
, s1
.len
) <= 0
244 : s2
.len
!= 0 && memcmp(s1
.ptr
, s2
.ptr
, s2
.len
) < 0);
247 int operator>=(const string
&s1
, const string
&s2
)
249 return (s1
.len
>= s2
.len
250 ? s2
.len
== 0 || memcmp(s1
.ptr
, s2
.ptr
, s2
.len
) >= 0
251 : s1
.len
!= 0 && memcmp(s1
.ptr
, s2
.ptr
, s1
.len
) > 0);
254 int operator>(const string
&s1
, const string
&s2
)
256 return (s1
.len
> s2
.len
257 ? s2
.len
== 0 || memcmp(s1
.ptr
, s2
.ptr
, s2
.len
) >= 0
258 : s1
.len
!= 0 && memcmp(s1
.ptr
, s2
.ptr
, s1
.len
) > 0);
261 void string::set_length(int i
)
265 ptr
= srealloc(ptr
, sz
, len
, i
, &sz
);
274 int string::search(char c
) const
276 char *p
= ptr
? (char *)memchr(ptr
, c
, len
) : NULL
;
277 return p
? p
- ptr
: -1;
280 // we silently strip nuls
282 char *string::extract() const
288 for (i
= 0; i
< n
; i
++)
291 char *q
= new char[n
+ 1 - nnuls
];
293 for (i
= 0; i
< n
; i
++)
300 void string::remove_spaces()
303 while (l
>= 0 && ptr
[l
] == ' ')
314 char *tmp
= new char[len
];
329 void put_string(const string
&s
, FILE *fp
)
331 int len
= s
.length();
332 const char *ptr
= s
.contents();
333 for (int i
= 0; i
< len
; i
++)
337 string
as_string(int i
)
339 static char buf
[INT_DIGITS
+ 2];
340 sprintf(buf
, "%d", i
);