Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / ace / SString.cpp
blobabd86b337cb5313ad454355a5c0683b4bdf22b60
1 #include "ace/Malloc_T.h"
2 #include "ace/OS_Memory.h"
3 #include "ace/SString.h"
4 #include "ace/OS_NS_string.h"
5 #include "ace/Numeric_Limits.h"
7 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
8 // FUZZ: disable check_for_streams_include
9 # include "ace/streams.h"
10 #endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
12 #if !defined (__ACE_INLINE__)
13 #include "ace/SString.inl"
14 #endif /* __ACE_INLINE__ */
16 // ************************************************************
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
21 ACE_OSTREAM_TYPE &
22 operator<< (ACE_OSTREAM_TYPE &os, const ACE_CString &cs)
24 if (cs.fast_rep () != 0)
25 os << cs.fast_rep ();
26 return os;
29 ACE_OSTREAM_TYPE &
30 operator<< (ACE_OSTREAM_TYPE &os, const ACE_WString &ws)
32 // @@ Need to figure out how to print the "wide" string
33 // on platforms that don't support "wide" strings.
34 #if defined (ACE_HAS_WCHAR)
35 os << ACE_Wide_To_Ascii (ws.fast_rep ()).char_rep ();
36 #else
37 ACE_UNUSED_ARG (ws);
38 os << "(*non-printable string*)";
39 #endif
40 return os;
43 ACE_OSTREAM_TYPE &
44 operator<< (ACE_OSTREAM_TYPE &os, const ACE_SString &ss)
46 if (ss.fast_rep () != 0)
47 os << ss.fast_rep ();
48 return os;
50 #endif /* !ACE_LACKS_IOSTREAM_TOTALLY */
52 // *****************************************************************
54 char *
55 ACE_NS_WString::char_rep () const
57 ACE_TRACE ("ACE_NS_WString::char_rep");
58 if (this->len_ == 0)
59 return 0;
60 else
62 char *t = 0;
64 #if defined (ACE_HAS_ALLOC_HOOKS)
65 ACE_ALLOCATOR_RETURN (t,
66 static_cast<char*>(ACE_Allocator::instance()->malloc (sizeof (char) * (this->len_ + 1))),
67 0);
68 #else
69 ACE_NEW_RETURN (t,
70 char[this->len_ + 1],
71 0);
72 #endif
74 for (size_type i = 0; i < this->len_; ++i)
75 // Note that this cast may lose data if wide chars are
76 // actually used!
77 t[i] = char (this->rep_[i]);
79 t[this->len_] = '\0';
80 return t;
84 ACE_UINT16 *
85 ACE_NS_WString::ushort_rep () const
87 ACE_TRACE ("ACE_NS_WString::ushort_rep");
88 if (this->len_ <= 0)
89 return 0;
90 else
92 ACE_UINT16 *t = 0;
94 #if defined (ACE_HAS_ALLOC_HOOKS)
95 ACE_ALLOCATOR_RETURN (t,
96 static_cast<ACE_UINT16*> (ACE_Allocator::instance()->malloc(sizeof(ACE_UINT16) * (this->len_ + 1))),
97 0);
98 #else
99 ACE_NEW_RETURN (t,
100 ACE_UINT16[this->len_ + 1],
102 #endif
104 for (size_type i = 0; i < this->len_; ++i)
105 // Note that this cast may lose data if wide chars are
106 // actually used!
107 t[i] = (ACE_UINT16)this->rep_[i];
109 t[this->len_] = 0;
110 return t;
114 ACE_NS_WString::ACE_NS_WString (const char *s,
115 ACE_Allocator *alloc)
116 : ACE_WString (alloc)
118 if (s == 0)
119 return;
121 this->len_ = this->buf_len_ = ACE_OS::strlen (s);
123 if (this->buf_len_ == 0)
124 return;
126 ACE_ALLOCATOR (this->rep_,
127 (ACE_WSTRING_TYPE *)
128 this->allocator_->malloc ((this->buf_len_ + 1) *
129 sizeof (ACE_WSTRING_TYPE)));
130 this->release_ = 1;
131 for (size_type i = 0; i <= this->buf_len_; ++i)
132 this->rep_[i] = s[i];
135 #if defined (ACE_WSTRING_HAS_USHORT_SUPPORT)
136 ACE_NS_WString::ACE_NS_WString (const ACE_UINT16 *s,
137 size_type len,
138 ACE_Allocator *alloc)
139 : ACE_WString (alloc)
141 if (s == 0)
142 return;
144 this->buf_len_ = len;
146 if (this->buf_len_ == 0)
147 return;
149 ACE_ALLOCATOR (this->rep_,
150 (ACE_WSTRING_TYPE *)
151 this->allocator_->malloc ((this->buf_len_) *
152 sizeof (ACE_WSTRING_TYPE)));
153 this->release_ = 1;
154 for (size_type i = 0; i < this->buf_len_; ++i)
155 this->rep_[i] = s[i];
157 #endif /* ACE_WSTRING_HAS_USHORT_SUPPORT */
159 // *****************************************************************
161 ACE_SString::size_type const ACE_SString::npos =
162 ACE_Numeric_Limits<ACE_SString::size_type>::max ();
164 ACE_ALLOC_HOOK_DEFINE(ACE_SString)
166 void
167 ACE_SString::dump () const
169 #if defined (ACE_HAS_DUMP)
170 ACE_TRACE ("ACE_SString::dump");
171 #endif /* ACE_HAS_DUMP */
174 // Copy constructor.
176 ACE_SString::ACE_SString (const ACE_SString &s)
177 : allocator_ (s.allocator_),
178 len_ (s.len_)
180 ACE_TRACE ("ACE_SString::ACE_SString");
182 if (this->allocator_ == 0)
183 this->allocator_ = ACE_Allocator::instance ();
185 this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
186 ACE_OS::memcpy ((void *) this->rep_,
187 (const void *) s.rep_,
188 this->len_);
189 this->rep_[this->len_] = '\0';
192 // Default constructor.
194 ACE_SString::ACE_SString (ACE_Allocator *alloc)
195 : allocator_ (alloc),
196 len_ (0),
197 rep_ (0)
200 ACE_TRACE ("ACE_SString::ACE_SString");
202 if (this->allocator_ == 0)
203 this->allocator_ = ACE_Allocator::instance ();
205 this->len_ = 0;
206 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
207 this->rep_[this->len_] = '\0';
210 // Set the underlying pointer (does not copy memory).
212 void
213 ACE_SString::rep (char *s)
215 ACE_TRACE ("ACE_SString::rep");
217 this->rep_ = s;
219 if (s == 0)
220 this->len_ = 0;
221 else
222 this->len_ = ACE_OS::strlen (s);
225 // Constructor that actually copies memory.
227 ACE_SString::ACE_SString (const char *s,
228 ACE_Allocator *alloc)
229 : allocator_ (alloc)
231 ACE_TRACE ("ACE_SString::ACE_SString");
233 if (this->allocator_ == 0)
234 this->allocator_ = ACE_Allocator::instance ();
236 if (s == 0)
238 this->len_ = 0;
239 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
240 this->rep_[this->len_] = '\0';
242 else
244 this->len_ = ACE_OS::strlen (s);
245 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
246 ACE_OS::strcpy (this->rep_, s);
250 ACE_SString::ACE_SString (char c,
251 ACE_Allocator *alloc)
252 : allocator_ (alloc)
254 ACE_TRACE ("ACE_SString::ACE_SString");
256 if (this->allocator_ == 0)
257 this->allocator_ = ACE_Allocator::instance ();
259 this->len_ = 1;
260 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
261 this->rep_[0] = c;
262 this->rep_[this->len_] = '\0';
265 // Constructor that actually copies memory.
267 ACE_SString::ACE_SString (const char *s,
268 size_type len,
269 ACE_Allocator *alloc)
270 : allocator_ (alloc)
272 ACE_TRACE ("ACE_SString::ACE_SString");
274 if (this->allocator_ == 0)
275 this->allocator_ = ACE_Allocator::instance ();
277 if (s == 0)
279 this->len_ = 0;
280 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
281 this->rep_[this->len_] = '\0';
283 else
285 this->len_ = len;
286 this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
287 ACE_OS::memcpy (this->rep_, s, len);
288 this->rep_[len] = '\0'; // Make sure to NUL terminate this!
292 // Assignment operator (does copy memory).
294 ACE_SString &
295 ACE_SString::operator= (const ACE_SString &s)
297 ACE_TRACE ("ACE_SString::operator=");
298 // Check for identify.
300 if (this != &s)
302 // Only reallocate if we don't have enough space...
303 if (this->len_ < s.len_)
305 this->allocator_->free (this->rep_);
306 this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
308 this->len_ = s.len_;
309 ACE_OS::strcpy (this->rep_, s.rep_);
312 return *this;
315 // Return substring.
316 ACE_SString
317 ACE_SString::substring (size_type offset,
318 size_type length) const
320 size_t count = length;
322 // case 1. empty string
323 if (len_ == 0)
324 return ACE_SString ();
326 // case 2. start pos l
327 if (offset >= len_)
328 return ACE_SString ();
330 // get all remaining bytes
331 if (length == npos || count > (this->len_ - offset))
332 count = len_ - offset;
334 return ACE_SString (&rep_[offset], count, this->allocator_);
337 // ************************************************************
339 #if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
340 template char ACE_String_Base<char>::NULL_String_;
341 template ACE_WSTRING_TYPE ACE_String_Base<ACE_WSTRING_TYPE>::NULL_String_;
342 #endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */
344 ACE_END_VERSIONED_NAMESPACE_DECL