You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.6 KiB

25 years ago
  1. // Strings.
  2. #ifndef _CL_STRING_H
  3. #define _CL_STRING_H
  4. #include "cl_object.h"
  5. #include "cl_io.h"
  6. #include "cl_abort.h"
  7. #include <string.h>
  8. // General, reference counted and garbage collected strings.
  9. struct cl_heap_string : public cl_heap {
  10. private:
  11. unsigned long length; // length (in characters)
  12. char data[1]; // the characters, plus a '\0' at the end
  13. // Standard allocation disabled.
  14. void* operator new (size_t size) { (void)size; cl_abort(); return (void*)1; }
  15. // Standard deallocation disabled.
  16. void operator delete (void* ptr) { (void)ptr; cl_abort(); }
  17. // No default constructor.
  18. cl_heap_string ();
  19. private:
  20. // Friend declarations. They are for the compiler. Just ignore them.
  21. friend class cl_string;
  22. friend cl_heap_string* cl_make_heap_string (unsigned long len);
  23. friend cl_heap_string* cl_make_heap_string (const char * s);
  24. friend cl_heap_string* cl_make_heap_string (const char * ptr, unsigned long len);
  25. friend const cl_string operator+ (const cl_string& str1, const cl_string& str2);
  26. friend const cl_string operator+ (const char* str1, const cl_string& str2);
  27. friend const cl_string operator+ (const cl_string& str1, const char* str2);
  28. };
  29. struct cl_string : public cl_gcpointer {
  30. public:
  31. // Conversion to simple string.
  32. // NOTE! The resulting pointer is valid only as long as the string
  33. // is live, i.e. you must keep the string in a variable until you
  34. // are done with the pointer to the characters.
  35. const char * asciz () const
  36. {
  37. return &((cl_heap_string*)pointer)->data[0];
  38. }
  39. // Return the length (number of characters).
  40. unsigned long length () const
  41. {
  42. return ((cl_heap_string*)pointer)->length;
  43. }
  44. // Return a specific character.
  45. char operator[] (unsigned long i) const
  46. {
  47. if (!(i < length())) cl_abort(); // Range check.
  48. return ((cl_heap_string*)pointer)->data[i];
  49. }
  50. // New ANSI C++ compilers also want the following.
  51. char operator[] (unsigned int i) const
  52. { return operator[]((unsigned long)i); }
  53. char operator[] (long i) const
  54. { return operator[]((unsigned long)i); }
  55. char operator[] (int i) const
  56. { return operator[]((unsigned long)i); }
  57. // Constructors.
  58. cl_string ();
  59. cl_string (const cl_string&);
  60. cl_string (const char * s);
  61. cl_string (const char * ptr, unsigned long len);
  62. // Assignment operators.
  63. cl_string& operator= (const cl_string&);
  64. cl_string& operator= (const char *);
  65. // Private pointer manipulations.
  66. operator cl_heap_string* () const;
  67. cl_string (cl_heap_string* str) { pointer = str; }
  68. cl_string (cl_private_thing p) : cl_gcpointer (p) {}
  69. };
  70. CL_DEFINE_COPY_CONSTRUCTOR2(cl_string,cl_gcpointer)
  71. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_string,cl_string)
  72. inline cl_string::cl_string (const char * s)
  73. {
  74. extern cl_heap_string* cl_make_heap_string (const char *);
  75. pointer = cl_make_heap_string(s);
  76. }
  77. inline cl_string& cl_string::operator= (const char * s)
  78. {
  79. extern cl_heap_string* cl_make_heap_string (const char *);
  80. cl_heap_string* tmp = cl_make_heap_string(s);
  81. cl_dec_refcount(*this);
  82. pointer = tmp;
  83. return *this;
  84. }
  85. // Length.
  86. inline unsigned long strlen (const cl_string& str)
  87. {
  88. return str.length();
  89. }
  90. // Conversion to `const char *'.
  91. inline const char * asciz (const char * s) { return s; }
  92. inline const char * asciz (const cl_string& s) { return s.asciz(); }
  93. // Comparison.
  94. inline bool equal (const cl_string& str1, const cl_string& str2)
  95. {
  96. return str1.length() == str2.length()
  97. && !strcmp(str1.asciz(), str2.asciz());
  98. }
  99. inline bool equal (const char * str1, const cl_string& str2)
  100. {
  101. return !strcmp(str1, str2.asciz());
  102. }
  103. inline bool equal (const cl_string& str1, const char * str2)
  104. {
  105. return !strcmp(str1.asciz(), str2);
  106. }
  107. // Private pointer manipulations. Never throw away a `struct cl_heap_string *'!
  108. inline cl_string::operator cl_heap_string* () const
  109. {
  110. cl_heap_string* hpointer = (cl_heap_string*)pointer;
  111. cl_inc_refcount(*this);
  112. return hpointer;
  113. }
  114. inline cl_string::cl_string ()
  115. {
  116. extern const cl_string cl_null_string;
  117. pointer = (cl_heap_string*) cl_null_string;
  118. }
  119. CL_REQUIRE(cl_st_null)
  120. // Hash code.
  121. extern unsigned long hashcode (const cl_string& str);
  122. // Output.
  123. extern void fprint (cl_ostream stream, const cl_string& str);
  124. CL_DEFINE_PRINT_OPERATOR(cl_string)
  125. // Input.
  126. #ifdef CL_IO_IOSTREAM
  127. // Reads a line. Up to delim. The delimiter character is not placed in the
  128. // resulting string. The delimiter character is kept in the input stream.
  129. // If EOF is encountered, the stream's eofbit is set.
  130. extern const cl_string cl_fget (cl_istream stream, char delim = '\n');
  131. // Reads a line. Up to delim. The delimiter character is not placed in the
  132. // resulting string. The delimiter character is extracted from the input stream.
  133. // If EOF is encountered, the stream's eofbit is set.
  134. extern const cl_string cl_fgetline (cl_istream stream, char delim = '\n');
  135. // Like above, but only up to n-1 characters. If n-1 characters were read
  136. // before the delimiter character was seen, the stream's failbit is set.
  137. extern const cl_string cl_fget (cl_istream stream, int n, char delim = '\n');
  138. extern const cl_string cl_fgetline (cl_istream stream, int n, char delim = '\n');
  139. // Skips whitespace and then reads a non-whitespace string.
  140. // If stream.width() is greater than 0, at most stream.width()-1 non-whitespace
  141. // characters are read. When done, stream.width(0) is called.
  142. // If EOF is encountered, the stream's eofbit is set.
  143. extern cl_istream operator>> (cl_istream stream, cl_string& str);
  144. #endif
  145. // Runtime typing support.
  146. extern cl_class cl_class_string;
  147. // Debugging support.
  148. #ifdef CL_DEBUG
  149. extern int cl_string_debug_module;
  150. static void* const cl_string_debug_dummy[] = { &cl_string_debug_dummy,
  151. &cl_string_debug_module
  152. };
  153. #endif
  154. #endif /* _CL_STRING_H */