Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-19 08:08:32

0001 // Strings.
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 // General, reference counted and garbage collected strings.
0016 struct cl_heap_string : public cl_heap {
0017 private:
0018     unsigned long length;   // length (in characters)
0019     char data[1];       // the characters, plus a '\0' at the end
0020     // Standard allocation disabled.
0021     void* operator new (size_t size) = delete;
0022     // Standard deallocation disabled.
0023     void operator delete (void* ptr) = delete;
0024     // No default constructor.
0025     cl_heap_string ();
0026 private:
0027 // Friend declarations. They are for the compiler. Just ignore them.
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     // Conversion to simple string.
0040     // NOTE! The resulting pointer is valid only as long as the string
0041     // is live, i.e. you must keep the string in a variable until you
0042     // are done with the pointer to the characters.
0043     const char * asciz () const
0044     {
0045         return &((cl_heap_string*)pointer)->data[0];
0046     }
0047     // Return the length (number of characters).
0048     unsigned long size() const
0049     {
0050         return ((cl_heap_string*)pointer)->length;
0051     }
0052     // Return a specific character.
0053     char operator[] (unsigned long i) const
0054     {
0055         if (!(i < size())) throw runtime_exception(); // Range check.
0056         return ((cl_heap_string*)pointer)->data[i];
0057     }
0058     // New ANSI C++ compilers also want the following.
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     // Constructors.
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     // Assignment operators.
0071     cl_string& operator= (const cl_string&);
0072     cl_string& operator= (const char *);
0073     // Private pointer manipulations.
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 // Length.
0095 inline unsigned long strlen (const cl_string& str)
0096 {
0097     return str.size();
0098 }
0099 // Conversion to `const char *'.
0100 inline const char * asciz (const char * s) { return s; }
0101 inline const char * asciz (const cl_string& s) { return s.asciz(); }
0102 
0103 // Comparison.
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 // Private pointer manipulations. Never throw away a `struct cl_heap_string *'!
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 // Hash code.
0132 extern uintptr_t hashcode (const cl_string& str);
0133 
0134 // Output.
0135 extern void fprint (std::ostream& stream, const cl_string& str);
0136 CL_DEFINE_PRINT_OPERATOR(cl_string)
0137 
0138 // Input.
0139 
0140 // Reads a line. Up to delim. The delimiter character is not placed in the
0141 // resulting string. The delimiter character is kept in the input stream.
0142 // If EOF is encountered, the stream's eofbit is set.
0143 extern const cl_string cl_fget (std::istream& stream, char delim = '\n');
0144 
0145 // Reads a line. Up to delim. The delimiter character is not placed in the
0146 // resulting string. The delimiter character is extracted from the input stream.
0147 // If EOF is encountered, the stream's eofbit is set.
0148 extern const cl_string cl_fgetline (std::istream& stream, char delim = '\n');
0149 
0150 // Like above, but only up to n-1 characters. If n-1 characters were read
0151 // before the delimiter character was seen, the stream's failbit is set.
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 // Skips whitespace and then reads a non-whitespace string.
0156 // If stream.width() is greater than 0, at most stream.width()-1 non-whitespace
0157 // characters are read. When done, stream.width(0) is called.
0158 // If EOF is encountered, the stream's eofbit is set.
0159 extern std::istream& operator>> (std::istream& stream, cl_string& str);
0160 
0161 // Runtime typing support.
0162 extern cl_class cl_class_string;
0163 
0164 // Debugging support.
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 }  // namespace cln
0171 
0172 #endif /* _CL_STRING_H */