Line data Source code
1 : /* Copyright (C) 1989-2025 Free Software Foundation, Inc.
2 : Written by James Clark (jjc@jclark.com)
3 :
4 : This file is part of groff, the GNU roff typesetting system.
5 :
6 : groff is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation, either version 3 of the License, or
9 : (at your option) any later version.
10 :
11 : groff is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 :
19 : #ifndef GROFF_STRINGCLASS_H
20 : #define GROFF_STRINGCLASS_H
21 :
22 : #include <assert.h>
23 : #include <string.h> // memcmp(), strlen()
24 : #include <stdio.h> // FILE
25 :
26 : // Ensure that the first declaration of functions that are later
27 : // declared as inline declares them as inline.
28 :
29 : class string;
30 :
31 : inline string operator+(const string &, const string &);
32 : inline string operator+(const string &, const char *);
33 : inline string operator+(const char *, const string &);
34 : inline string operator+(const string &, char);
35 : inline string operator+(char, const string &);
36 : inline int operator==(const string &, const string &);
37 : inline int operator!=(const string &, const string &);
38 :
39 : class string {
40 : public:
41 : string();
42 : string(const string &);
43 : string(const char *);
44 : string(const char *, int);
45 : string(char);
46 :
47 : ~string();
48 :
49 : string &operator=(const string &);
50 : string &operator=(const char *);
51 : string &operator=(char);
52 :
53 : string &operator+=(const string &);
54 : string &operator+=(const char *);
55 : string &operator+=(char);
56 : void append(const char *, int);
57 :
58 : int length() const;
59 : int empty() const;
60 : int operator*() const;
61 :
62 : string substring(int i, int n) const;
63 :
64 : char &operator[](int);
65 : char operator[](int) const;
66 :
67 : void set_length(int i);
68 : const char *contents() const;
69 : int search(const char) const;
70 : int find(const char *) const;
71 : char *extract() const;
72 : size_t json_length() const;
73 : const char *json_extract() const;
74 : void json_dump() const;
75 : void remove_spaces();
76 : void clear();
77 : void move(string &);
78 :
79 : friend string operator+(const string &, const string &);
80 : friend string operator+(const string &, const char *);
81 : friend string operator+(const char *, const string &);
82 : friend string operator+(const string &, char);
83 : friend string operator+(char, const string &);
84 :
85 : friend int operator==(const string &, const string &);
86 : friend int operator!=(const string &, const string &);
87 : friend int operator<=(const string &, const string &);
88 : friend int operator<(const string &, const string &);
89 : friend int operator>=(const string &, const string &);
90 : friend int operator>(const string &, const string &);
91 :
92 : private:
93 : char *ptr;
94 : int len;
95 : int sz;
96 :
97 : string(const char *, int, const char *, int); // for use by operator+
98 : void grow1();
99 : };
100 :
101 :
102 1593272 : inline char &string::operator[](int i)
103 : {
104 1593272 : assert((i >= 0) && (i < len));
105 1593272 : return ptr[i];
106 : }
107 :
108 11437 : inline char string::operator[](int i) const
109 : {
110 11437 : assert((i >= 0) && (i < len));
111 11437 : return ptr[i];
112 : }
113 :
114 2686227 : inline int string::length() const
115 : {
116 2686227 : return len;
117 : }
118 :
119 428449 : inline int string::empty() const
120 : {
121 428449 : return (len == 0);
122 : }
123 :
124 : inline int string::operator*() const
125 : {
126 : return len;
127 : }
128 :
129 1118548 : inline const char *string::contents() const
130 : {
131 1118548 : return ptr;
132 : }
133 :
134 9 : inline string operator+(const string &s1, const string &s2)
135 : {
136 9 : return string(s1.ptr, s1.len, s2.ptr, s2.len);
137 : }
138 :
139 483 : inline string operator+(const string &s1, const char *s2)
140 : {
141 : return (0 /* nullptr */ == s2)
142 483 : ? s1 : string(s1.ptr, s1.len, s2, strlen(s2));
143 : }
144 :
145 210 : inline string operator+(const char *s1, const string &s2)
146 : {
147 : return (0 /* nullptr */ == s1)
148 210 : ? s2 : string(s1, strlen(s1), s2.ptr, s2.len);
149 : }
150 :
151 5966 : inline string operator+(const string &s, char c)
152 : {
153 5966 : return string(s.ptr, s.len, &c, 1);
154 : }
155 :
156 1165 : inline string operator+(char c, const string &s)
157 : {
158 1165 : return string(&c, 1, s.ptr, s.len);
159 : }
160 :
161 82803 : inline int operator==(const string &s1, const string &s2)
162 : {
163 82803 : return (s1.len == s2.len
164 82803 : && (s1.len == 0 || memcmp(s1.ptr, s2.ptr, s1.len) == 0));
165 : }
166 :
167 116507 : inline int operator!=(const string &s1, const string &s2)
168 : {
169 116507 : return (s1.len != s2.len
170 116507 : || (s1.len != 0 && memcmp(s1.ptr, s2.ptr, s1.len) != 0));
171 : }
172 :
173 9568 : inline string string::substring(int i, int n) const
174 : {
175 9568 : assert((i >= 0) && ((i + n) <= len));
176 9568 : return string(ptr + i, n);
177 : }
178 :
179 15290649 : inline string &string::operator+=(char c)
180 : {
181 15290649 : if (len >= sz)
182 618730 : grow1();
183 15290649 : ptr[len++] = c;
184 15290649 : return *this;
185 : }
186 :
187 : void put_string(const string &, FILE *);
188 :
189 : string as_string(int);
190 :
191 : #endif // GROFF_STRINGCLASS_H
192 :
193 : // Local Variables:
194 : // fill-column: 72
195 : // mode: C++
196 : // End:
197 : // vim: set cindent noexpandtab shiftwidth=2 textwidth=72:
|