]> www.ginac.de Git - cln.git/blob - include/cln/string.h
64-bit mingw port: Define sintP, uintP in terms of 'intptr_t', not 'long'.
[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/exception.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; throw runtime_exception(); }
22         // Standard deallocation disabled.
23         void operator delete (void* ptr) { (void)ptr; throw runtime_exception(); }
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 size() 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 < size())) throw runtime_exception(); // 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.size();
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.size() == str2.size()
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         static const cl_string cl_null_st(NULL, 0);
128         pointer = (cl_heap_string*) cl_null_st;
129 }
130
131 // Hash code.
132 extern unsigned long hashcode (const cl_string& str);
133
134 // Output.
135 extern void fprint (std::ostream& stream, const cl_string& str);
136 CL_DEFINE_PRINT_OPERATOR(cl_string)
137
138 // Input.
139
140 // Reads a line. Up to delim. The delimiter character is not placed in the
141 // resulting string. The delimiter character is kept in the input stream.
142 // If EOF is encountered, the stream's eofbit is set.
143 extern const cl_string cl_fget (std::istream& stream, char delim = '\n');
144
145 // Reads a line. Up to delim. The delimiter character is not placed in the
146 // resulting string. The delimiter character is extracted from the input stream.
147 // If EOF is encountered, the stream's eofbit is set.
148 extern const cl_string cl_fgetline (std::istream& stream, char delim = '\n');
149
150 // Like above, but only up to n-1 characters. If n-1 characters were read
151 // before the delimiter character was seen, the stream's failbit is set.
152 extern const cl_string cl_fget (std::istream& stream, int n, char delim = '\n');
153 extern const cl_string cl_fgetline (std::istream& stream, int n, char delim = '\n');
154
155 // Skips whitespace and then reads a non-whitespace string.
156 // If stream.width() is greater than 0, at most stream.width()-1 non-whitespace
157 // characters are read. When done, stream.width(0) is called.
158 // If EOF is encountered, the stream's eofbit is set.
159 extern std::istream& operator>> (std::istream& stream, cl_string& str);
160
161 // Runtime typing support.
162 extern cl_class cl_class_string;
163
164 // Debugging support.
165 #ifdef CL_DEBUG
166 extern int cl_string_debug_module;
167 CL_FORCE_LINK(cl_string_debug_dummy, cl_string_debug_module)
168 #endif
169
170 }  // namespace cln
171
172 #endif /* _CL_STRING_H */