xref: /onnv-gate/usr/src/cmd/man/src/util/nsgmls.src/include/StringOf.h (revision 0:68f95e015346)
1 // Copyright (c) 1994, 1996 James Clark
2 // See the file COPYING for copying permission.
3 #pragma ident	"%Z%%M%	%I%	%E% SMI"
4 
5 #ifndef StringOf_INCLUDED
6 #define StringOf_INCLUDED 1
7 
8 // The file is called StringOf to distinguish it from string.h on
9 // case-insensitive file systems.
10 
11 // This offers a subset of the interface offered by the standard C++
12 // basic_string class as defined in the Jan 96 WP.
13 // Code in SP currently assumes that size_type is size_t.
14 
15 #include <stddef.h>
16 #include <string.h>
17 #include "Boolean.h"
18 
19 #ifdef SP_NAMESPACE
20 namespace SP_NAMESPACE {
21 #endif
22 
23 template<class T>
24 class String {
25 public:
26   typedef size_t size_type;
27   typedef T *iterator;
28   typedef const T *const_iterator;
29   String();
~String()30   ~String() { if (ptr_) delete [] ptr_; }
31   String(const T *, size_t);
32   String(const String<T> &);
33   String<T> &operator=(const String<T> &);
size()34   size_t size() const { return length_; }
35   String<T> &assign(const T *, size_t);
36   String<T> &insert(size_t i, const String<T> &s);
37   void swap(String<T> &str);
38   T operator[](size_t i) const { return ptr_[i]; }
39   T &operator[](size_t i) { return ptr_[i]; }
begin()40   iterator begin() { return ptr_; }
begin()41   const_iterator begin() const { return ptr_; }
data()42   const T *data() const { return ptr_; }
43   String<T> &operator+=(T c) {
44     if (length_ >= alloc_)
45       grow(1);
46     ptr_[length_++] = c;
47     return *this;
48   }
49   String<T> &operator+=(const String<T> &s) {
50     append(s.ptr_, s.length_);
51     return *this;
52   }
53   String<T> &append(const T *, size_t);
54   Boolean operator==(const String<T> &s) const {
55     return (length_ == s.length_
56 	    && (length_ == 0
57 		|| (*ptr_ == *s.ptr_
58 		    && (memcmp(ptr_ + 1, s.ptr_ + 1, (length_ - 1)*sizeof(T))
59 			== 0))));
60   }
61   Boolean operator!=(const String<T> &str) const {
62     return !(*this == str);
63   }
64   void resize(size_t n);
65 private:
66   void grow(size_t);
67   T *ptr_;
68   size_t length_;
69   size_t alloc_;
70 };
71 
72 #ifdef SP_NAMESPACE
73 }
74 #endif
75 
76 #endif /* not StringOf_INCLUDED */
77 
78 #ifdef SP_DEFINE_TEMPLATES
79 #include "StringOf.cxx"
80 #endif
81