]> www.ginac.de Git - cln.git/blob - include/cl_string.h
ec177c57af0281028a62b565b3b670fcf3122611
[cln.git] / include / cl_string.h
1 // Strings.
2
3 #ifndef _CL_STRING_H
4 #define _CL_STRING_H
5
6 #include "cl_object.h"
7 #include "cl_io.h"
8 #include "cl_abort.h"
9 #include <string.h>
10
11 // General, reference counted and garbage collected strings.
12 struct cl_heap_string : public cl_heap {
13 private:
14         unsigned long length;   // length (in characters)
15         char data[1];           // the characters, plus a '\0' at the end
16         // Standard allocation disabled.
17         void* operator new (size_t size) { (void)size; cl_abort(); return (void*)1; }
18         // Standard deallocation disabled.
19         void operator delete (void* ptr) { (void)ptr; cl_abort(); }
20         // No default constructor.
21         cl_heap_string ();
22 private:
23 // Friend declarations. They are for the compiler. Just ignore them.
24         friend class cl_string;
25         friend cl_heap_string* cl_make_heap_string (unsigned long len);
26         friend cl_heap_string* cl_make_heap_string (const char * s);
27         friend cl_heap_string* cl_make_heap_string (const char * ptr, unsigned long len);
28         friend const cl_string operator+ (const cl_string& str1, const cl_string& str2);
29         friend const cl_string operator+ (const char* str1, const cl_string& str2);
30         friend const cl_string operator+ (const cl_string& str1, const char* str2);
31 };
32
33 struct cl_string : public cl_gcpointer {
34 public:
35         // Conversion to simple string.
36         // NOTE! The resulting pointer is valid only as long as the string
37         // is live, i.e. you must keep the string in a variable until you
38         // are done with the pointer to the characters.
39         const char * asciz () const
40         {
41                 return &((cl_heap_string*)pointer)->data[0];
42         }
43         // Return the length (number of characters).
44         unsigned long length () const
45         {
46                 return ((cl_heap_string*)pointer)->length;
47         }
48         // Return a specific character.
49         char operator[] (unsigned long i) const
50         {
51                 if (!(i < length())) cl_abort(); // Range check.
52                 return ((cl_heap_string*)pointer)->data[i];
53         }
54         // New ANSI C++ compilers also want the following.
55         char operator[] (unsigned int i) const
56                 { return operator[]((unsigned long)i); }
57         char operator[] (long i) const
58                 { return operator[]((unsigned long)i); }
59         char operator[] (int i) const
60                 { return operator[]((unsigned long)i); }
61         // Constructors.
62         cl_string ();
63         cl_string (const cl_string&);
64         cl_string (const char * s);
65         cl_string (const char * ptr, unsigned long len);
66         // Assignment operators.
67         cl_string& operator= (const cl_string&);
68         cl_string& operator= (const char *);
69         // Private pointer manipulations.
70         operator cl_heap_string* () const;
71         cl_string (cl_heap_string* str) { pointer = str; }
72         cl_string (cl_private_thing p) : cl_gcpointer (p) {}
73 };
74 CL_DEFINE_COPY_CONSTRUCTOR2(cl_string,cl_gcpointer)
75 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_string,cl_string)
76 inline cl_string::cl_string (const char * s)
77 {
78         extern cl_heap_string* cl_make_heap_string (const char *);
79         pointer = cl_make_heap_string(s);
80 }
81 inline cl_string& cl_string::operator= (const char * s)
82 {
83         extern cl_heap_string* cl_make_heap_string (const char *);
84         cl_heap_string* tmp = cl_make_heap_string(s);
85         cl_dec_refcount(*this);
86         pointer = tmp;
87         return *this;
88 }
89
90 // Length.
91 inline unsigned long strlen (const cl_string& str)
92 {
93         return str.length();
94 }
95 // Conversion to `const char *'.
96 inline const char * asciz (const char * s) { return s; }
97 inline const char * asciz (const cl_string& s) { return s.asciz(); }
98
99 // Comparison.
100 inline bool equal (const cl_string& str1, const cl_string& str2)
101 {
102     return str1.length() == str2.length()
103            && !strcmp(str1.asciz(), str2.asciz());
104 }
105 inline bool equal (const char * str1, const cl_string& str2)
106 {
107     return !strcmp(str1, str2.asciz());
108 }
109 inline bool equal (const cl_string& str1, const char * str2)
110 {
111     return !strcmp(str1.asciz(), str2);
112 }
113
114 // Private pointer manipulations. Never throw away a `struct cl_heap_string *'!
115 inline cl_string::operator cl_heap_string* () const
116 {
117         cl_heap_string* hpointer = (cl_heap_string*)pointer;
118         cl_inc_refcount(*this);
119         return hpointer;
120 }
121 inline cl_string::cl_string ()
122 {
123         extern const cl_string cl_null_string;
124         pointer = (cl_heap_string*) cl_null_string;
125 }
126 CL_REQUIRE(cl_st_null)
127
128 // Hash code.
129 extern unsigned long hashcode (const cl_string& str);
130
131 // Output.
132 extern void fprint (cl_ostream stream, const cl_string& str);
133 CL_DEFINE_PRINT_OPERATOR(cl_string)
134
135 // Input.
136
137 #ifdef CL_IO_IOSTREAM
138
139 // Reads a line. Up to delim. The delimiter character is not placed in the
140 // resulting string. The delimiter character is kept in the input stream.
141 // If EOF is encountered, the stream's eofbit is set.
142 extern const cl_string cl_fget (cl_istream stream, char delim = '\n');
143
144 // Reads a line. Up to delim. The delimiter character is not placed in the
145 // resulting string. The delimiter character is extracted from the input stream.
146 // If EOF is encountered, the stream's eofbit is set.
147 extern const cl_string cl_fgetline (cl_istream stream, char delim = '\n');
148
149 // Like above, but only up to n-1 characters. If n-1 characters were read
150 // before the delimiter character was seen, the stream's failbit is set.
151 extern const cl_string cl_fget (cl_istream stream, int n, char delim = '\n');
152 extern const cl_string cl_fgetline (cl_istream stream, int n, char delim = '\n');
153
154 // Skips whitespace and then reads a non-whitespace string.
155 // If stream.width() is greater than 0, at most stream.width()-1 non-whitespace
156 // characters are read. When done, stream.width(0) is called.
157 // If EOF is encountered, the stream's eofbit is set.
158 extern cl_istream operator>> (cl_istream stream, cl_string& str);
159
160 #endif
161
162 // Runtime typing support.
163 extern cl_class cl_class_string;
164
165 // Debugging support.
166 #ifdef CL_DEBUG
167 extern int cl_string_debug_module;
168 static void* const cl_string_debug_dummy[] = { &cl_string_debug_dummy,
169         &cl_string_debug_module
170 };
171 #endif
172
173 #endif /* _CL_STRING_H */