6 #include "cln/object.h"
13 // General, reference counted and garbage collected strings.
14 struct cl_heap_string : public cl_heap {
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.
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);
35 struct cl_string : public cl_gcpointer {
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
43 return &((cl_heap_string*)pointer)->data[0];
45 // Return the length (number of characters).
46 unsigned long length () const
48 return ((cl_heap_string*)pointer)->length;
50 // Return a specific character.
51 char operator[] (unsigned long i) const
53 if (!(i < length())) cl_abort(); // Range check.
54 return ((cl_heap_string*)pointer)->data[i];
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); }
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) {}
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)
80 extern cl_heap_string* cl_make_heap_string (const char *);
81 pointer = cl_make_heap_string(s);
83 inline cl_string& cl_string::operator= (const char * s)
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);
93 inline unsigned long strlen (const cl_string& str)
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(); }
102 inline bool equal (const cl_string& str1, const cl_string& str2)
104 return str1.length() == str2.length()
105 && !strcmp(str1.asciz(), str2.asciz());
107 inline bool equal (const char * str1, const cl_string& str2)
109 return !strcmp(str1, str2.asciz());
111 inline bool equal (const cl_string& str1, const char * str2)
113 return !strcmp(str1.asciz(), str2);
116 // Private pointer manipulations. Never throw away a `struct cl_heap_string *'!
117 inline cl_string::operator cl_heap_string* () const
119 cl_heap_string* hpointer = (cl_heap_string*)pointer;
120 cl_inc_refcount(*this);
123 inline cl_string::cl_string ()
125 extern const cl_string cl_null_string;
126 pointer = (cl_heap_string*) cl_null_string;
128 CL_REQUIRE(cl_st_null)
131 extern unsigned long hashcode (const cl_string& str);
134 extern void fprint (std::ostream& stream, const cl_string& str);
135 CL_DEFINE_PRINT_OPERATOR(cl_string)
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');
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');
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');
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);
160 // Runtime typing support.
161 extern cl_class cl_class_string;
163 // Debugging support.
165 extern int cl_string_debug_module;
166 CL_FORCE_LINK(cl_string_debug_dummy, cl_string_debug_module)
171 #endif /* _CL_STRING_H */