2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: StringOf.C /main/1 1996/07/29 17:05:16 cde-hp $ */
24 // Copyright (c) 1994, 1996 James Clark
25 // See the file COPYING for copying permission.
27 #ifndef StringOf_DEF_INCLUDED
28 #define StringOf_DEF_INCLUDED 1
34 namespace SP_NAMESPACE {
38 String<T>::String(const T *ptr, size_t length)
39 : length_(length), alloc_(length)
43 memcpy(ptr_, ptr, length*sizeof(T));
51 : ptr_(0), length_(0), alloc_(0)
56 String<T>::String(const String<T> &s)
57 : length_(s.length_), alloc_(s.length_)
60 ptr_ = new T[length_];
61 memcpy(ptr_, s.ptr_, length_*sizeof(T));
68 String<T> &String<T>::operator=(const String<T> &s)
71 if (s.length_ > alloc_) {
74 ptr_ = new T[alloc_ = s.length_];
76 memcpy(ptr_, s.ptr_, s.length_*sizeof(T));
83 String<T> &String<T>::insert(size_t i, const String<T> &s)
85 if (length_ + s.length_ > alloc_)
87 for (size_t n = length_ - i; n > 0; n--)
88 ptr_[i + n - 1 + s.length_] = ptr_[i + n - 1];
90 memcpy(ptr_ + i, s.ptr_, s.length_*sizeof(T));
95 String<T> &String<T>::append(const T *p, size_t length)
97 if (length_ + length > alloc_)
99 memcpy(ptr_ + length_, p, length*sizeof(T));
105 void String<T>::grow(size_t n)
111 T *s = new T[alloc_];
112 memcpy(s, ptr_, length_*sizeof(T));
118 void String<T>::swap(String<T> &to)
126 size_t tem = to.length_;
127 to.length_ = length_;
131 size_t tem = to.alloc_;
138 String<T> &String<T>::assign(const T *p, size_t n)
143 ptr_ = new T[alloc_ = n];
146 for(T *to = ptr_; n > 0; n--, to++, p++)
152 void String<T>::resize(size_t n)
156 ptr_ = new T[alloc_ = n];
158 memcpy(ptr_, oldPtr_, length_*sizeof(T));
169 #endif /* not StringOf_DEF_INCLUDED */