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.

171 lines
5.6 KiB

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