groff before CVS: release 1.06
[s-roff.git] / include / stringclass.h
blob371bb443bdca012ec6e687eb8987f8b278c0ae69
1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff 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 groff 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <string.h>
22 #include <stdio.h>
23 #include <assert.h>
25 class string {
26 public:
27 string();
28 string(const string &);
29 string(const char *);
30 string(const char *, int);
31 string(char);
33 ~string();
35 string &operator=(const string &);
36 string &operator=(const char *);
37 string &operator=(char);
39 string &operator+=(const string &);
40 string &operator+=(const char *);
41 string &operator+=(char);
42 void append(const char *, int);
44 int length() const;
45 int empty() const;
46 int operator*() const;
48 string substring(int i, int n) const;
50 char &operator[](int);
51 char operator[](int) const;
53 void set_length(int i);
54 const char *contents() const;
55 int search(char) const;
56 char *extract() const;
57 void clear();
58 void move(string &);
60 friend string operator+(const string &, const string &);
61 friend string operator+(const string &, const char *);
62 friend string operator+(const char *, const string &);
63 friend string operator+(const string &, char);
64 friend string operator+(char, const string &);
66 friend int operator==(const string &, const string &);
67 friend int operator!=(const string &, const string &);
68 friend int operator<=(const string &, const string &);
69 friend int operator<(const string &, const string &);
70 friend int operator>=(const string &, const string &);
71 friend int operator>(const string &, const string &);
73 private:
74 char *ptr;
75 int len;
76 int sz;
78 string(const char *, int, const char *, int); // for use by operator+
79 void grow1();
83 inline char &string::operator[](int i)
85 assert(i >= 0 && i < len);
86 return ptr[i];
89 inline char string::operator[](int i) const
91 assert(i >= 0 && i < len);
92 return ptr[i];
95 inline int string::length() const
97 return len;
100 inline int string::empty() const
102 return len == 0;
105 inline int string::operator*() const
107 return len;
110 inline const char *string::contents() const
112 return ptr;
115 inline string operator+(const string &s1, const string &s2)
117 return string(s1.ptr, s1.len, s2.ptr, s2.len);
120 inline string operator+(const string &s1, const char *s2)
122 if (s2 == 0)
123 return s1;
124 else
125 return string(s1.ptr, s1.len, s2, strlen(s2));
128 inline string operator+(const char *s1, const string &s2)
130 if (s1 == 0)
131 return s2;
132 else
133 return string(s1, strlen(s1), s2.ptr, s2.len);
136 inline string operator+(const string &s, char c)
138 return string(s.ptr, s.len, &c, 1);
141 inline string operator+(char c, const string &s)
143 return string(&c, 1, s.ptr, s.len);
146 inline int operator==(const string &s1, const string &s2)
148 return (s1.len == s2.len
149 && (s1.len == 0 || memcmp(s1.ptr, s2.ptr, s1.len) == 0));
152 inline int operator!=(const string &s1, const string &s2)
154 return (s1.len != s2.len
155 || (s1.len != 0 && memcmp(s1.ptr, s2.ptr, s1.len) != 0));
158 inline string string::substring(int i, int n) const
160 assert(i >= 0 && i + n <= len);
161 return string(ptr + i, n);
164 inline string &string::operator+=(char c)
166 if (len >= sz)
167 grow1();
168 ptr[len++] = c;
169 return *this;
172 void put_string(const string &, FILE *);
174 string as_string(int);