File indexing completed on 2026-05-19 08:08:32
0001
0002
0003 #ifndef _CL_STRING_H
0004 #define _CL_STRING_H
0005
0006 #include "cln/object.h"
0007 #include "cln/io.h"
0008 #include "cln/exception.h"
0009 #include <cstring>
0010
0011 namespace cln {
0012
0013 struct cl_string;
0014
0015
0016 struct cl_heap_string : public cl_heap {
0017 private:
0018 unsigned long length;
0019 char data[1];
0020
0021 void* operator new (size_t size) = delete;
0022
0023 void operator delete (void* ptr) = delete;
0024
0025 cl_heap_string ();
0026 private:
0027
0028 friend class cl_string;
0029 friend cl_heap_string* cl_make_heap_string (unsigned long len);
0030 friend cl_heap_string* cl_make_heap_string (const char * s);
0031 friend cl_heap_string* cl_make_heap_string (const char * ptr, unsigned long len);
0032 friend const cl_string operator+ (const cl_string& str1, const cl_string& str2);
0033 friend const cl_string operator+ (const char* str1, const cl_string& str2);
0034 friend const cl_string operator+ (const cl_string& str1, const char* str2);
0035 };
0036
0037 struct cl_string : public cl_gcpointer {
0038 public:
0039
0040
0041
0042
0043 const char * asciz () const
0044 {
0045 return &((cl_heap_string*)pointer)->data[0];
0046 }
0047
0048 unsigned long size() const
0049 {
0050 return ((cl_heap_string*)pointer)->length;
0051 }
0052
0053 char operator[] (unsigned long i) const
0054 {
0055 if (!(i < size())) throw runtime_exception();
0056 return ((cl_heap_string*)pointer)->data[i];
0057 }
0058
0059 char operator[] (unsigned int i) const
0060 { return operator[]((unsigned long)i); }
0061 char operator[] (long i) const
0062 { return operator[]((unsigned long)i); }
0063 char operator[] (int i) const
0064 { return operator[]((unsigned long)i); }
0065
0066 cl_string ();
0067 cl_string (const cl_string&);
0068 cl_string (const char * s);
0069 cl_string (const char * ptr, unsigned long len);
0070
0071 cl_string& operator= (const cl_string&);
0072 cl_string& operator= (const char *);
0073
0074 operator cl_heap_string* () const;
0075 cl_string (cl_heap_string* str) { pointer = str; }
0076 cl_string (cl_private_thing p) : cl_gcpointer (p) {}
0077 };
0078 CL_DEFINE_COPY_CONSTRUCTOR2(cl_string,cl_gcpointer)
0079 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_string,cl_string)
0080 inline cl_string::cl_string (const char * s)
0081 {
0082 extern cl_heap_string* cl_make_heap_string (const char *);
0083 pointer = cl_make_heap_string(s);
0084 }
0085 inline cl_string& cl_string::operator= (const char * s)
0086 {
0087 extern cl_heap_string* cl_make_heap_string (const char *);
0088 cl_heap_string* tmp = cl_make_heap_string(s);
0089 cl_dec_refcount(*this);
0090 pointer = tmp;
0091 return *this;
0092 }
0093
0094
0095 inline unsigned long strlen (const cl_string& str)
0096 {
0097 return str.size();
0098 }
0099
0100 inline const char * asciz (const char * s) { return s; }
0101 inline const char * asciz (const cl_string& s) { return s.asciz(); }
0102
0103
0104 inline bool equal (const cl_string& str1, const cl_string& str2)
0105 {
0106 return str1.size() == str2.size()
0107 && !strcmp(str1.asciz(), str2.asciz());
0108 }
0109 inline bool equal (const char * str1, const cl_string& str2)
0110 {
0111 return !strcmp(str1, str2.asciz());
0112 }
0113 inline bool equal (const cl_string& str1, const char * str2)
0114 {
0115 return !strcmp(str1.asciz(), str2);
0116 }
0117
0118
0119 inline cl_string::operator cl_heap_string* () const
0120 {
0121 cl_heap_string* hpointer = (cl_heap_string*)pointer;
0122 cl_inc_refcount(*this);
0123 return hpointer;
0124 }
0125 inline cl_string::cl_string ()
0126 {
0127 static const cl_string cl_null_st(NULL, 0);
0128 pointer = (cl_heap_string*) cl_null_st;
0129 }
0130
0131
0132 extern uintptr_t hashcode (const cl_string& str);
0133
0134
0135 extern void fprint (std::ostream& stream, const cl_string& str);
0136 CL_DEFINE_PRINT_OPERATOR(cl_string)
0137
0138
0139
0140
0141
0142
0143 extern const cl_string cl_fget (std::istream& stream, char delim = '\n');
0144
0145
0146
0147
0148 extern const cl_string cl_fgetline (std::istream& stream, char delim = '\n');
0149
0150
0151
0152 extern const cl_string cl_fget (std::istream& stream, int n, char delim = '\n');
0153 extern const cl_string cl_fgetline (std::istream& stream, int n, char delim = '\n');
0154
0155
0156
0157
0158
0159 extern std::istream& operator>> (std::istream& stream, cl_string& str);
0160
0161
0162 extern cl_class cl_class_string;
0163
0164
0165 #ifdef CL_DEBUG
0166 extern int cl_string_debug_module;
0167 CL_FORCE_LINK(cl_string_debug_dummy, cl_string_debug_module)
0168 #endif
0169
0170 }
0171
0172 #endif