man/l_roff-char.7.in: fix Bernd Warken (Dave Kemper, Werner Lemberg)..
[s-roff.git] / include / stringclass.h
blobdf9138aa874957768175f0bdf74f9f8e65ec797b
1 /*@
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
10 * version.
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
15 * for more details.
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
24 #include "config.h"
26 #include <assert.h>
27 #include <stdio.h>
28 #include <string.h>
30 // Ensure that the first declaration of functions that are later
31 // declared as inline declares them as inline.
33 class string;
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 &);
43 class string
45 char *ptr;
46 int len;
47 int sz;
49 string(const char *, int, const char *, int); // for use by operator+
50 void grow1();
52 public:
53 string();
54 string(const string &);
55 string(const char *);
56 string(const char *, int);
57 string(char);
59 ~string();
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);
70 int length() const;
71 int empty() const;
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;
83 void remove_spaces();
84 void clear();
85 void move(string &);
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);
104 return ptr[i];
107 inline char string::operator[](int i) const
109 assert(i >= 0 && i < len);
110 return ptr[i];
113 inline int string::length() const
115 return len;
118 inline int string::empty() const
120 return len == 0;
123 inline int string::operator*() const
125 return len;
128 inline const char *string::contents() const
130 return ptr;
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)
178 if (len >= sz)
179 grow1();
180 ptr[len++] = c;
181 return *this;
184 void put_string(const string &, FILE *);
186 string as_string(int);
188 #endif // _STRINGCLASS_H
189 // s-it2-mode