home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / groff-1.09-src.lha / src / amiga / groff-1.09 / include / stringclass.h < prev   
C/C++ Source or Header  |  1994-02-21  |  4KB  |  188 lines

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