11 // General, reference counted and garbage collected strings.
12 struct cl_heap_string : public cl_heap {
14 unsigned long length; // length (in characters)
15 char data[1]; // the characters, plus a '\0' at the end
16 // Standard allocation disabled.
17 void* operator new (size_t size) { (void)size; cl_abort(); return (void*)1; }
18 // Standard deallocation disabled.
19 void operator delete (void* ptr) { (void)ptr; cl_abort(); }
20 // No default constructor.
23 // Friend declarations. They are for the compiler. Just ignore them.
24 friend class cl_string;
25 friend cl_heap_string* cl_make_heap_string (unsigned long len);
26 friend cl_heap_string* cl_make_heap_string (const char * s);
27 friend cl_heap_string* cl_make_heap_string (const char * ptr, unsigned long len);
28 friend const cl_string operator+ (const cl_string& str1, const cl_string& str2);
29 friend const cl_string operator+ (const char* str1, const cl_string& str2);
30 friend const cl_string operator+ (const cl_string& str1, const char* str2);
33 struct cl_string : public cl_gcpointer {
35 // Conversion to simple string.
36 // NOTE! The resulting pointer is valid only as long as the string
37 // is live, i.e. you must keep the string in a variable until you
38 // are done with the pointer to the characters.
39 const char * asciz () const
41 return &((cl_heap_string*)pointer)->data[0];
43 // Return the length (number of characters).
44 unsigned long length () const
46 return ((cl_heap_string*)pointer)->length;
48 // Return a specific character.
49 char operator[] (unsigned long i) const
51 if (!(i < length())) cl_abort(); // Range check.
52 return ((cl_heap_string*)pointer)->data[i];
54 // New ANSI C++ compilers also want the following.
55 char operator[] (unsigned int i) const
56 { return operator[]((unsigned long)i); }
57 char operator[] (long i) const
58 { return operator[]((unsigned long)i); }
59 char operator[] (int i) const
60 { return operator[]((unsigned long)i); }
63 cl_string (const cl_string&);
64 cl_string (const char * s);
65 cl_string (const char * ptr, unsigned long len);
66 // Assignment operators.
67 cl_string& operator= (const cl_string&);
68 cl_string& operator= (const char *);
69 // Private pointer manipulations.
70 operator cl_heap_string* () const;
71 cl_string (cl_heap_string* str) { pointer = str; }
72 cl_string (cl_private_thing p) : cl_gcpointer (p) {}
74 CL_DEFINE_COPY_CONSTRUCTOR2(cl_string,cl_gcpointer)
75 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_string,cl_string)
76 inline cl_string::cl_string (const char * s)
78 extern cl_heap_string* cl_make_heap_string (const char *);
79 pointer = cl_make_heap_string(s);
81 inline cl_string& cl_string::operator= (const char * s)
83 extern cl_heap_string* cl_make_heap_string (const char *);
84 cl_heap_string* tmp = cl_make_heap_string(s);
85 cl_dec_refcount(*this);
91 inline unsigned long strlen (const cl_string& str)
95 // Conversion to `const char *'.
96 inline const char * asciz (const char * s) { return s; }
97 inline const char * asciz (const cl_string& s) { return s.asciz(); }
100 inline bool equal (const cl_string& str1, const cl_string& str2)
102 return str1.length() == str2.length()
103 && !strcmp(str1.asciz(), str2.asciz());
105 inline bool equal (const char * str1, const cl_string& str2)
107 return !strcmp(str1, str2.asciz());
109 inline bool equal (const cl_string& str1, const char * str2)
111 return !strcmp(str1.asciz(), str2);
114 // Private pointer manipulations. Never throw away a `struct cl_heap_string *'!
115 inline cl_string::operator cl_heap_string* () const
117 cl_heap_string* hpointer = (cl_heap_string*)pointer;
118 cl_inc_refcount(*this);
121 inline cl_string::cl_string ()
123 extern const cl_string cl_null_string;
124 pointer = (cl_heap_string*) cl_null_string;
126 CL_REQUIRE(cl_st_null)
129 extern unsigned long hashcode (const cl_string& str);
132 extern void fprint (cl_ostream stream, const cl_string& str);
133 CL_DEFINE_PRINT_OPERATOR(cl_string)
137 #ifdef CL_IO_IOSTREAM
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 (cl_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 (cl_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 (cl_istream stream, int n, char delim = '\n');
152 extern const cl_string cl_fgetline (cl_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 cl_istream operator>> (cl_istream stream, cl_string& str);
162 // Runtime typing support.
163 extern cl_class cl_class_string;
165 // Debugging support.
167 extern int cl_string_debug_module;
168 static void* const cl_string_debug_dummy[] = { &cl_string_debug_dummy,
169 &cl_string_debug_module
173 #endif /* _CL_STRING_H */