Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / nsgmls / StringOf.C
1 /* $XConsortium: StringOf.C /main/1 1996/07/29 17:05:16 cde-hp $ */
2 // Copyright (c) 1994, 1996 James Clark
3 // See the file COPYING for copying permission.
4
5 #ifndef StringOf_DEF_INCLUDED
6 #define StringOf_DEF_INCLUDED 1
7
8 #include <string.h>
9 #include <stddef.h>
10
11 #ifdef SP_NAMESPACE
12 namespace SP_NAMESPACE {
13 #endif
14
15 template<class T>
16 String<T>::String(const T *ptr, size_t length)
17 : length_(length), alloc_(length)
18 {
19   if (length) {
20     ptr_ = new T[length];
21     memcpy(ptr_, ptr, length*sizeof(T));
22   }
23   else
24     ptr_ = 0;
25 }
26
27 template<class T>
28 String<T>::String()
29 : ptr_(0), length_(0), alloc_(0)
30 {
31 }
32
33 template<class T>
34 String<T>::String(const String<T> &s)
35 : length_(s.length_), alloc_(s.length_)
36 {
37   if (length_) {
38     ptr_ = new T[length_];
39     memcpy(ptr_, s.ptr_, length_*sizeof(T));
40   }
41   else
42     ptr_ = 0;
43 }
44
45 template<class T>
46 String<T> &String<T>::operator=(const String<T> &s)
47 {
48   if (&s != this) {
49     if (s.length_ > alloc_) {
50       if (ptr_)
51         delete [] ptr_;
52       ptr_ = new T[alloc_ = s.length_];
53     }
54     memcpy(ptr_, s.ptr_, s.length_*sizeof(T));
55     length_ = s.length_;
56   }
57   return *this;
58 }
59
60 template<class T>
61 String<T> &String<T>::insert(size_t i, const String<T> &s)
62 {
63   if (length_ + s.length_ > alloc_)
64     grow(s.length_);
65   for (size_t n = length_ - i; n > 0; n--)
66     ptr_[i + n - 1 + s.length_] = ptr_[i + n - 1];
67   length_ += s.length_;
68   memcpy(ptr_ + i, s.ptr_, s.length_*sizeof(T));
69   return *this;
70 }
71
72 template<class T>
73 String<T> &String<T>::append(const T *p, size_t length)
74 {
75   if (length_ + length > alloc_)
76     grow(length);
77   memcpy(ptr_ + length_, p, length*sizeof(T));
78   length_ += length;
79   return *this;
80 }
81
82 template<class T>
83 void String<T>::grow(size_t n)
84 {
85   if (alloc_ < n)
86     alloc_ += n + 16;
87   else
88     alloc_ += alloc_;
89   T *s = new T[alloc_];
90   memcpy(s, ptr_, length_*sizeof(T));
91   delete [] ptr_;
92   ptr_ = s;
93 }
94
95 template<class T>
96 void String<T>::swap(String<T> &to)
97 {
98   {
99     T *tem = to.ptr_;
100     to.ptr_ = ptr_;
101     ptr_ = tem;
102   }
103   {
104     size_t tem = to.length_;
105     to.length_ = length_;
106     length_ = tem;
107   }
108   {
109     size_t tem = to.alloc_;
110     to.alloc_ = alloc_;
111     alloc_ = tem;
112   }
113 }
114
115 template<class T>
116 String<T> &String<T>::assign(const T *p, size_t n)
117 {
118   if (alloc_ < n) {
119     if (ptr_)
120       delete [] ptr_;
121     ptr_ = new T[alloc_ = n];
122   }
123   length_ = n;
124   for(T *to = ptr_; n > 0; n--, to++, p++)
125     *to = *p;
126   return *this;
127 }
128
129 template<class T>
130 void String<T>::resize(size_t n)
131 {
132   if (alloc_ < n) {
133     T *oldPtr_ = ptr_;
134     ptr_ = new T[alloc_ = n];
135     if (length_ > 0) {
136       memcpy(ptr_, oldPtr_, length_*sizeof(T));
137       delete [] oldPtr_;
138     }
139   }
140   length_ = n;
141 }
142
143 #ifdef SP_NAMESPACE
144 }
145 #endif
146
147 #endif /* not StringOf_DEF_INCLUDED */