]> www.ginac.de Git - cln.git/blob - include/cln/string.h
Force link-time references despite optimizations done by g++.
[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 <string.h>
10
11 namespace cln {
12
13 // General, reference counted and garbage collected strings.
14 struct cl_heap_string : public cl_heap {
15 private:
16         unsigned long length;   // length (in characters)
17         char data[1];           // the characters, plus a '\0' at the end
18         // Standard allocation disabled.
19         void* operator new (size_t size) { (void)size; cl_abort(); return (void*)1; }
20         // Standard deallocation disabled.
21         void operator delete (void* ptr) { (void)ptr; cl_abort(); }
22         // No default constructor.
23         cl_heap_string ();
24 private:
25 // Friend declarations. They are for the compiler. Just ignore them.
26         friend class cl_string;
27         friend cl_heap_string* cl_make_heap_string (unsigned long len);
28         friend cl_heap_string* cl_make_heap_string (const char * s);
29         friend cl_heap_string* cl_make_heap_string (const char * ptr, unsigned long len);
30         friend const cl_string operator+ (const cl_string& str1, const cl_string& str2);
31         friend const cl_string operator+ (const char* str1, const cl_string& str2);
32         friend const cl_string operator+ (const cl_string& str1, const char* str2);
33 };
34
35 struct cl_string : public cl_gcpointer {
36 public:
37         // Conversion to simple string.
38         // NOTE! The resulting pointer is valid only as long as the string
39         // is live, i.e. you must keep the string in a variable until you
40         // are done with the pointer to the characters.
41         const char * asciz () const
42         {
43                 return &((cl_heap_string*)pointer)->data[0];
44         }
45         // Return the length (number of characters).
46         unsigned long length () const
47         {
48                 return ((cl_heap_string*)pointer)->length;
49         }
50         // Return a specific character.
51         char operator[] (unsigned long i) const
52         {
53                 if (!(i < length())) cl_abort(); // Range check.
54                 return ((cl_heap_string*)pointer)->data[i];
55         }
56         // New ANSI C++ compilers also want the following.
57         char operator[] (unsigned int i) const
58                 { return operator[]((unsigned long)i); }
59         char operator[] (long i) const
60                 { return operator[]((unsigned long)i); }
61         char operator[] (int i) const
62                 { return operator[]((unsigned long)i); }
63         // Constructors.
64         cl_string ();
65         cl_string (const cl_string&);
66         cl_string (const char * s);
67         cl_string (const char * ptr, unsigned long len);
68         // Assignment operators.
69         cl_string& operator= (const cl_string&);
70         cl_string& operator= (const char *);
71         // Private pointer manipulations.
72         operator cl_heap_string* () const;
73         cl_string (cl_heap_string* str) { pointer = str; }
74         cl_string (cl_private_thing p) : cl_gcpointer (p) {}
75 };
76 CL_DEFINE_COPY_CONSTRUCTOR2(cl_string,cl_gcpointer)
77 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_string,cl_string)
78 inline cl_string::cl_string (const char * s)
79 {
80         extern cl_heap_string* cl_make_heap_string (const char *);
81         pointer = cl_make_heap_string(s);
82 }
83 inline cl_string& cl_string::operator= (const char * s)
84 {
85         extern cl_heap_string* cl_make_heap_string (const char *);
86         cl_heap_string* tmp = cl_make_heap_string(s);
87         cl_dec_refcount(*this);
88         pointer = tmp;
89         return *this;
90 }
91
92 // Length.
93 inline unsigned long strlen (const cl_string& str)
94 {
95         return str.length();
96 }
97 // Conversion to `const char *'.
98 inline const char * asciz (const char * s) { return s; }
99 inline const char * asciz (const cl_string& s) { return s.asciz(); }
100
101 // Comparison.
102 inline bool equal (const cl_string& str1, const cl_string& str2)
103 {
104     return str1.length() == str2.length()
105            && !strcmp(str1.asciz(), str2.asciz());
106 }
107 inline bool equal (const char * str1, const cl_string& str2)
108 {
109     return !strcmp(str1, str2.asciz());
110 }
111 inline bool equal (const cl_string& str1, const char * str2)
112 {
113     return !strcmp(str1.asciz(), str2);
114 }
115
116 // Private pointer manipulations. Never throw away a `struct cl_heap_string *'!
117 inline cl_string::operator cl_heap_string* () const
118 {
119         cl_heap_string* hpointer = (cl_heap_string*)pointer;
120         cl_inc_refcount(*this);
121         return hpointer;
122 }
123 inline cl_string::cl_string ()
124 {
125         extern const cl_string cl_null_string;
126         pointer = (cl_heap_string*) cl_null_string;
127 }
128 CL_REQUIRE(cl_st_null)
129
130 // Hash code.
131 extern unsigned long hashcode (const cl_string& str);
132
133 // Output.
134 extern void fprint (std::ostream& stream, const cl_string& str);
135 CL_DEFINE_PRINT_OPERATOR(cl_string)
136
137 // Input.
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 (std::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 (std::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 (std::istream& stream, int n, char delim = '\n');
152 extern const cl_string cl_fgetline (std::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 std::istream& operator>> (std::istream& stream, cl_string& str);
159
160 // Runtime typing support.
161 extern cl_class cl_class_string;
162
163 // Debugging support.
164 #ifdef CL_DEBUG
165 extern int cl_string_debug_module;
166 CL_FORCE_LINK(cl_string_debug_dummy, cl_string_debug_module)
167 #endif
168
169 }  // namespace cln
170
171 #endif /* _CL_STRING_H */