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