2 * Copyright (c) 2014 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
4 * Copyright (C) 1989 - 1992, 2002 Free Software Foundation, Inc.
5 * Written by James Clark (jjc@jclark.com)
7 * This is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2, or (at your option) any later
12 * This is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * You should have received a copy of the GNU General Public License along
18 * with groff; see the file COPYING. If not, write to the Free Software
19 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef _STRINGCLASS_H
22 #define _STRINGCLASS_H
30 // Ensure that the first declaration of functions that are later
31 // declared as inline declares them as inline.
35 inline string
operator+(const string
&, const string
&);
36 inline string
operator+(const string
&, const char *);
37 inline string
operator+(const char *, const string
&);
38 inline string
operator+(const string
&, char);
39 inline string
operator+(char, const string
&);
40 inline int operator==(const string
&, const string
&);
41 inline int operator!=(const string
&, const string
&);
49 string(const char *, int, const char *, int); // for use by operator+
54 string(const string
&);
56 string(const char *, int);
61 string
&operator=(const string
&);
62 string
&operator=(const char *);
63 string
&operator=(char);
65 string
&operator+=(const string
&);
66 string
&operator+=(const char *);
67 string
&operator+=(char);
68 void append(const char *, int);
72 int operator*() const;
74 string
substring(int i
, int n
) const;
76 char &operator[](int);
77 char operator[](int) const;
79 void set_length(int i
);
80 const char *contents() const;
81 int search(char) const;
82 char *extract() const;
87 friend string
operator+(const string
&, const string
&);
88 friend string
operator+(const string
&, const char *);
89 friend string
operator+(const char *, const string
&);
90 friend string
operator+(const string
&, char);
91 friend string
operator+(char, const string
&);
93 friend int operator==(const string
&, const string
&);
94 friend int operator!=(const string
&, const string
&);
95 friend int operator<=(const string
&, const string
&);
96 friend int operator<(const string
&, const string
&);
97 friend int operator>=(const string
&, const string
&);
98 friend int operator>(const string
&, const string
&);
101 inline char &string::operator[](int i
)
103 assert(i
>= 0 && i
< len
);
107 inline char string::operator[](int i
) const
109 assert(i
>= 0 && i
< len
);
113 inline int string::length() const
118 inline int string::empty() const
123 inline int string::operator*() const
128 inline const char *string::contents() const
133 inline string
operator+(const string
&s1
, const string
&s2
)
135 return string(s1
.ptr
, s1
.len
, s2
.ptr
, s2
.len
);
138 inline string
operator+(const string
&s1
, const char *s2
)
140 return (s2
== NULL
) ? s1
: string(s1
.ptr
, s1
.len
, s2
, strlen(s2
));
143 inline string
operator+(const char *s1
, const string
&s2
)
145 return (s1
== NULL
) ? s2
: string(s1
, strlen(s1
), s2
.ptr
, s2
.len
);
148 inline string
operator+(const string
&s
, char c
)
150 return string(s
.ptr
, s
.len
, &c
, 1);
153 inline string
operator+(char c
, const string
&s
)
155 return string(&c
, 1, s
.ptr
, s
.len
);
158 inline int operator==(const string
&s1
, const string
&s2
)
160 return (s1
.len
== s2
.len
161 && (s1
.len
== 0 || memcmp(s1
.ptr
, s2
.ptr
, s1
.len
) == 0));
164 inline int operator!=(const string
&s1
, const string
&s2
)
166 return (s1
.len
!= s2
.len
167 || (s1
.len
!= 0 && memcmp(s1
.ptr
, s2
.ptr
, s1
.len
) != 0));
170 inline string
string::substring(int i
, int n
) const
172 assert(i
>= 0 && i
+ n
<= len
);
173 return string(ptr
+ i
, n
);
176 inline string
&string::operator+=(char c
)
184 void put_string(const string
&, FILE *);
186 string
as_string(int);
188 #endif // _STRINGCLASS_H