6 #include "cln/object.h"
8 #include "cln/exception.h"
15 // General, reference counted and garbage collected strings.
16 struct cl_heap_string : public cl_heap {
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.
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);
37 struct cl_string : public cl_gcpointer {
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
45 return &((cl_heap_string*)pointer)->data[0];
47 // Return the length (number of characters).
48 unsigned long size() const
50 return ((cl_heap_string*)pointer)->length;
52 // Return a specific character.
53 char operator[] (unsigned long i) const
55 if (!(i < size())) throw runtime_exception(); // Range check.
56 return ((cl_heap_string*)pointer)->data[i];
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); }
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) {}
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)
82 extern cl_heap_string* cl_make_heap_string (const char *);
83 pointer = cl_make_heap_string(s);
85 inline cl_string& cl_string::operator= (const char * s)
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);
95 inline unsigned long strlen (const cl_string& str)
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(); }
104 inline bool equal (const cl_string& str1, const cl_string& str2)
106 return str1.size() == str2.size()
107 && !strcmp(str1.asciz(), str2.asciz());
109 inline bool equal (const char * str1, const cl_string& str2)
111 return !strcmp(str1, str2.asciz());
113 inline bool equal (const cl_string& str1, const char * str2)
115 return !strcmp(str1.asciz(), str2);
118 // Private pointer manipulations. Never throw away a `struct cl_heap_string *'!
119 inline cl_string::operator cl_heap_string* () const
121 cl_heap_string* hpointer = (cl_heap_string*)pointer;
122 cl_inc_refcount(*this);
125 inline cl_string::cl_string ()
127 static const cl_string cl_null_st(NULL, 0);
128 pointer = (cl_heap_string*) cl_null_st;
132 extern unsigned long hashcode (const cl_string& str);
135 extern void fprint (std::ostream& stream, const cl_string& str);
136 CL_DEFINE_PRINT_OPERATOR(cl_string)
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');
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');
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');
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);
161 // Runtime typing support.
162 extern cl_class cl_class_string;
164 // Debugging support.
166 extern int cl_string_debug_module;
167 CL_FORCE_LINK(cl_string_debug_dummy, cl_string_debug_module)
172 #endif /* _CL_STRING_H */