StringOf.C 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  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.
  26. #ifndef StringOf_DEF_INCLUDED
  27. #define StringOf_DEF_INCLUDED 1
  28. #include <string.h>
  29. #include <stddef.h>
  30. #ifdef SP_NAMESPACE
  31. namespace SP_NAMESPACE {
  32. #endif
  33. template<class T>
  34. String<T>::String(const T *ptr, size_t length)
  35. : length_(length), alloc_(length)
  36. {
  37. if (length) {
  38. ptr_ = new T[length];
  39. memcpy(ptr_, ptr, length*sizeof(T));
  40. }
  41. else
  42. ptr_ = 0;
  43. }
  44. template<class T>
  45. String<T>::String()
  46. : ptr_(0), length_(0), alloc_(0)
  47. {
  48. }
  49. template<class T>
  50. String<T>::String(const String<T> &s)
  51. : length_(s.length_), alloc_(s.length_)
  52. {
  53. if (length_) {
  54. ptr_ = new T[length_];
  55. memcpy(ptr_, s.ptr_, length_*sizeof(T));
  56. }
  57. else
  58. ptr_ = 0;
  59. }
  60. template<class T>
  61. String<T> &String<T>::operator=(const String<T> &s)
  62. {
  63. if (&s != this) {
  64. if (s.length_ > alloc_) {
  65. if (ptr_)
  66. delete [] ptr_;
  67. ptr_ = new T[alloc_ = s.length_];
  68. }
  69. memcpy(ptr_, s.ptr_, s.length_*sizeof(T));
  70. length_ = s.length_;
  71. }
  72. return *this;
  73. }
  74. template<class T>
  75. String<T> &String<T>::insert(size_t i, const String<T> &s)
  76. {
  77. if (length_ + s.length_ > alloc_)
  78. grow(s.length_);
  79. for (size_t n = length_ - i; n > 0; n--)
  80. ptr_[i + n - 1 + s.length_] = ptr_[i + n - 1];
  81. length_ += s.length_;
  82. memcpy(ptr_ + i, s.ptr_, s.length_*sizeof(T));
  83. return *this;
  84. }
  85. template<class T>
  86. String<T> &String<T>::append(const T *p, size_t length)
  87. {
  88. if (length_ + length > alloc_)
  89. grow(length);
  90. memcpy(ptr_ + length_, p, length*sizeof(T));
  91. length_ += length;
  92. return *this;
  93. }
  94. template<class T>
  95. void String<T>::grow(size_t n)
  96. {
  97. if (alloc_ < n)
  98. alloc_ += n + 16;
  99. else
  100. alloc_ += alloc_;
  101. T *s = new T[alloc_];
  102. memcpy(s, ptr_, length_*sizeof(T));
  103. delete [] ptr_;
  104. ptr_ = s;
  105. }
  106. template<class T>
  107. void String<T>::swap(String<T> &to)
  108. {
  109. {
  110. T *tem = to.ptr_;
  111. to.ptr_ = ptr_;
  112. ptr_ = tem;
  113. }
  114. {
  115. size_t tem = to.length_;
  116. to.length_ = length_;
  117. length_ = tem;
  118. }
  119. {
  120. size_t tem = to.alloc_;
  121. to.alloc_ = alloc_;
  122. alloc_ = tem;
  123. }
  124. }
  125. template<class T>
  126. String<T> &String<T>::assign(const T *p, size_t n)
  127. {
  128. if (alloc_ < n) {
  129. if (ptr_)
  130. delete [] ptr_;
  131. ptr_ = new T[alloc_ = n];
  132. }
  133. length_ = n;
  134. for(T *to = ptr_; n > 0; n--, to++, p++)
  135. *to = *p;
  136. return *this;
  137. }
  138. template<class T>
  139. void String<T>::resize(size_t n)
  140. {
  141. if (alloc_ < n) {
  142. T *oldPtr_ = ptr_;
  143. ptr_ = new T[alloc_ = n];
  144. if (length_ > 0) {
  145. memcpy(ptr_, oldPtr_, length_*sizeof(T));
  146. delete [] oldPtr_;
  147. }
  148. }
  149. length_ = n;
  150. }
  151. #ifdef SP_NAMESPACE
  152. }
  153. #endif
  154. #endif /* not StringOf_DEF_INCLUDED */