/contrib/groff/src/include/stringclass.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 196 lines · 141 code · 34 blank · 21 comment · 21 complexity · 47d4df778f3c21b05d4ac65d1837685c MD5 · raw file

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992, 2002 Free Software Foundation, Inc.
  3. Written by James Clark (jjc@jclark.com)
  4. This file is part of groff.
  5. groff is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with groff; see the file COPYING. If not, write to the Free Software
  15. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <assert.h>
  19. // Ensure that the first declaration of functions that are later
  20. // declared as inline declares them as inline.
  21. class string;
  22. inline string operator+(const string &, const string &);
  23. inline string operator+(const string &, const char *);
  24. inline string operator+(const char *, const string &);
  25. inline string operator+(const string &, char);
  26. inline string operator+(char, const string &);
  27. inline int operator==(const string &, const string &);
  28. inline int operator!=(const string &, const string &);
  29. class string {
  30. public:
  31. string();
  32. string(const string &);
  33. string(const char *);
  34. string(const char *, int);
  35. string(char);
  36. ~string();
  37. string &operator=(const string &);
  38. string &operator=(const char *);
  39. string &operator=(char);
  40. string &operator+=(const string &);
  41. string &operator+=(const char *);
  42. string &operator+=(char);
  43. void append(const char *, int);
  44. int length() const;
  45. int empty() const;
  46. int operator*() const;
  47. string substring(int i, int n) const;
  48. char &operator[](int);
  49. char operator[](int) const;
  50. void set_length(int i);
  51. const char *contents() const;
  52. int search(char) const;
  53. char *extract() const;
  54. void remove_spaces();
  55. void clear();
  56. void move(string &);
  57. friend string operator+(const string &, const string &);
  58. friend string operator+(const string &, const char *);
  59. friend string operator+(const char *, const string &);
  60. friend string operator+(const string &, char);
  61. friend string operator+(char, const string &);
  62. friend int operator==(const string &, const string &);
  63. friend int operator!=(const string &, const string &);
  64. friend int operator<=(const string &, const string &);
  65. friend int operator<(const string &, const string &);
  66. friend int operator>=(const string &, const string &);
  67. friend int operator>(const string &, const string &);
  68. private:
  69. char *ptr;
  70. int len;
  71. int sz;
  72. string(const char *, int, const char *, int); // for use by operator+
  73. void grow1();
  74. };
  75. inline char &string::operator[](int i)
  76. {
  77. assert(i >= 0 && i < len);
  78. return ptr[i];
  79. }
  80. inline char string::operator[](int i) const
  81. {
  82. assert(i >= 0 && i < len);
  83. return ptr[i];
  84. }
  85. inline int string::length() const
  86. {
  87. return len;
  88. }
  89. inline int string::empty() const
  90. {
  91. return len == 0;
  92. }
  93. inline int string::operator*() const
  94. {
  95. return len;
  96. }
  97. inline const char *string::contents() const
  98. {
  99. return ptr;
  100. }
  101. inline string operator+(const string &s1, const string &s2)
  102. {
  103. return string(s1.ptr, s1.len, s2.ptr, s2.len);
  104. }
  105. inline string operator+(const string &s1, const char *s2)
  106. {
  107. #ifdef __GNUG__
  108. if (s2 == 0)
  109. return s1;
  110. else
  111. return string(s1.ptr, s1.len, s2, strlen(s2));
  112. #else
  113. return s2 == 0 ? s1 : string(s1.ptr, s1.len, s2, strlen(s2));
  114. #endif
  115. }
  116. inline string operator+(const char *s1, const string &s2)
  117. {
  118. #ifdef __GNUG__
  119. if (s1 == 0)
  120. return s2;
  121. else
  122. return string(s1, strlen(s1), s2.ptr, s2.len);
  123. #else
  124. return s1 == 0 ? s2 : string(s1, strlen(s1), s2.ptr, s2.len);
  125. #endif
  126. }
  127. inline string operator+(const string &s, char c)
  128. {
  129. return string(s.ptr, s.len, &c, 1);
  130. }
  131. inline string operator+(char c, const string &s)
  132. {
  133. return string(&c, 1, s.ptr, s.len);
  134. }
  135. inline int 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 operator!=(const string &s1, const string &s2)
  141. {
  142. return (s1.len != s2.len
  143. || (s1.len != 0 && memcmp(s1.ptr, s2.ptr, s1.len) != 0));
  144. }
  145. inline string string::substring(int i, int n) const
  146. {
  147. assert(i >= 0 && i + n <= len);
  148. return string(ptr + i, n);
  149. }
  150. inline string &string::operator+=(char c)
  151. {
  152. if (len >= sz)
  153. grow1();
  154. ptr[len++] = c;
  155. return *this;
  156. }
  157. void put_string(const string &, FILE *);
  158. string as_string(int);