stringclass.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // -*- C++ -*-
  2. // Declarations etc. related to the string class, defined in libgroff/string.cc
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <assert.h>
  6. // Ensure that the first declaration of functions that are later
  7. // declared as inline declares them as inline.
  8. class string;
  9. inline string operator+(const string &, const string &);
  10. inline string operator+(const string &, const char *);
  11. inline string operator+(const char *, const string &);
  12. inline string operator+(const string &, char);
  13. inline string operator+(char, const string &);
  14. inline int operator==(const string &, const string &);
  15. inline int operator!=(const string &, const string &);
  16. class string
  17. {
  18. public:
  19. // ctors, dtor
  20. string();
  21. string(const string &);
  22. string(const char *);
  23. string(const char *, int);
  24. string(char);
  25. ~string();
  26. string &operator=(const string &);
  27. string &operator=(const char *);
  28. string &operator=(char);
  29. string &operator+=(const string &);
  30. string &operator+=(const char *);
  31. string &operator+=(char);
  32. void append(const char *, int);
  33. int length() const;
  34. int empty() const;
  35. int operator*() const;
  36. string substring(int i, int n) const;
  37. char &operator[](int);
  38. char operator[](int) const;
  39. void set_length(int i);
  40. const char *contents() const;
  41. int search(char) const;
  42. char *extract() const;
  43. void clear();
  44. void move(string &);
  45. friend string operator+(const string &, const string &);
  46. friend string operator+(const string &, const char *);
  47. friend string operator+(const char *, const string &);
  48. friend string operator+(const string &, char);
  49. friend string operator+(char, const string &);
  50. friend int operator==(const string &, const string &);
  51. friend int operator!=(const string &, const string &);
  52. friend int operator<=(const string &, const string &);
  53. friend int operator<(const string &, const string &);
  54. friend int operator>=(const string &, const string &);
  55. friend int operator>(const string &, const string &);
  56. private:
  57. char *ptr;
  58. int len;
  59. int sz;
  60. string(const char *, int, const char *, int); // for use by operator+
  61. void grow1();
  62. };
  63. inline char &
  64. string::operator[](int i)
  65. {
  66. assert(i >= 0 && i < len);
  67. return ptr[i];
  68. }
  69. inline char
  70. string::operator[](int i) const
  71. {
  72. assert(i >= 0 && i < len);
  73. return ptr[i];
  74. }
  75. inline int
  76. string::length() const
  77. {
  78. return len;
  79. }
  80. inline int
  81. string::empty() const
  82. {
  83. return len == 0;
  84. }
  85. inline int
  86. string::operator*() const
  87. {
  88. return len;
  89. }
  90. inline const char *
  91. string::contents() const
  92. {
  93. return ptr;
  94. }
  95. inline string
  96. operator+(const string &s1, const string &s2)
  97. {
  98. return string(s1.ptr, s1.len, s2.ptr, s2.len);
  99. }
  100. inline string
  101. operator+(const string &s1, const char *s2)
  102. {
  103. #ifdef __GNUG__
  104. if (s2 == 0)
  105. return s1;
  106. else
  107. return string(s1.ptr, s1.len, s2, strlen(s2));
  108. #else
  109. return s2 == 0 ? s1 : string(s1.ptr, s1.len, s2, strlen(s2));
  110. #endif
  111. }
  112. inline string
  113. operator+(const char *s1, const string &s2)
  114. {
  115. #ifdef __GNUG__
  116. if (s1 == 0)
  117. return s2;
  118. else
  119. return string(s1, strlen(s1), s2.ptr, s2.len);
  120. #else
  121. return s1 == 0 ? s2 : string(s1, strlen(s1), s2.ptr, s2.len);
  122. #endif
  123. }
  124. inline string
  125. operator+(const string &s, char c)
  126. {
  127. return string(s.ptr, s.len, &c, 1);
  128. }
  129. inline string
  130. operator+(char c, const string &s)
  131. {
  132. return string(&c, 1, s.ptr, s.len);
  133. }
  134. inline int
  135. operator==(const string &s1, const string &s2)
  136. {
  137. return (s1.len == s2.len
  138. && (s1.len == 0 || memcmp(s1.ptr, s2.ptr, s1.len) == 0));
  139. }
  140. inline int
  141. operator!=(const string &s1, const string &s2)
  142. {
  143. return (s1.len != s2.len
  144. || (s1.len != 0 && memcmp(s1.ptr, s2.ptr, s1.len) != 0));
  145. }
  146. inline string
  147. string::substring(int i, int n) const
  148. {
  149. assert(i >= 0 && i + n <= len);
  150. return string(ptr + i, n);
  151. }
  152. inline string &
  153. string::operator+=(char c)
  154. {
  155. if (len >= sz)
  156. grow1();
  157. ptr[len++] = c;
  158. return *this;
  159. }
  160. void put_string(const string &, FILE *);
  161. string as_string(int);