Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ACE_SString Class Reference

A very Simple String ACE_SString class. This is not a general-purpose string class, and you should probably consider using ACE_CString is you don't understand why this class exists... More...

#include <SString.h>

Collaboration diagram for ACE_SString:

Collaboration graph
[legend]
List of all members.

Public Methods

 ACE_SString (ACE_Allocator *alloc=0)
 Default constructor. More...

 ACE_SString (const char *s, ACE_Allocator *alloc=0)
 Constructor that copies s into dynamically allocated memory. More...

 ACE_SString (const char *s, size_t len, ACE_Allocator *alloc=0)
 Constructor that copies len chars of @s into dynamically allocated memory (will NUL terminate the result). More...

 ACE_SString (const ACE_SString &)
 Copy constructor. More...

 ACE_SString (char c, ACE_Allocator *alloc=0)
 Constructor that copies c into dynamically allocated memory. More...

 ~ACE_SString (void)
 Default destructor. More...

char operator[] (size_t slot) const
 Return the <slot'th> character in the string (doesn't perform bounds checking). More...

char & operator[] (size_t slot)
 Return the <slot'th> character by reference in the string (doesn't perform bounds checking). More...

ACE_SString & operator= (const ACE_SString &)
 Assignment operator (does copy memory). More...

ACE_SString substring (size_t offset, ssize_t length=-1) const
ACE_SString substr (size_t offset, ssize_t length=-1) const
 Same as substring. More...

u_long hash (void) const
 Returns a hash value for this string. More...

size_t length (void) const
 Return the length of the string. More...

void rep (char *s)
 Set the underlying pointer. Since this does not copy memory or delete existing memory use with extreme caution!!! More...

const char * rep (void) const
 Get the underlying pointer. More...

const char * fast_rep (void) const
 Get the underlying pointer. More...

const char * c_str (void) const
 Same as STL String's <c_str> and <fast_rep>. More...

int strstr (const ACE_SString &s) const
 Comparison operator that will match substrings. Returns the slot of the first location that matches, else -1. More...

int find (const ACE_SString &str, int pos=0) const
 Find <str> starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos. More...

int find (const char *s, int pos=0) const
 Find <s> starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos. More...

int find (char c, int pos=0) const
 Find <c> starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos. More...

int rfind (char c, int pos=npos) const
 Find <c> starting at pos (counting from the end). Returns the slot of the first location that matches, else npos. More...

int operator== (const ACE_SString &s) const
 Equality comparison operator (must match entire string). More...

int operator< (const ACE_SString &s) const
 Less than comparison operator. More...

int operator> (const ACE_SString &s) const
 Greater than comparison operator. More...

int operator!= (const ACE_SString &s) const
 Inequality comparison operator. More...

int compare (const ACE_SString &s) const
 Performs a <strcmp>-style comparison. More...

void dump (void) const
 Dump the state of an object. More...


Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks. More...


Static Public Attributes

const int npos = -1
 No position constant. More...


Private Attributes

ACE_Allocatorallocator_
 Pointer to a memory allocator. More...

size_t len_
 Length of the ACE_SString (not counting the trailing '\0'). More...

char * rep_
 Pointer to data. More...


Detailed Description

A very Simple String ACE_SString class. This is not a general-purpose string class, and you should probably consider using ACE_CString is you don't understand why this class exists...

This class is optimized for efficiency, so it doesn't provide any internal locking. CAUTION: This class is only intended for use with applications that understand how it works. In particular, its destructor does not deallocate its memory when it is destroyed... We need this class since the ACE_Map_Manager requires an object that supports the operator== and operator!=. This class uses an ACE_Allocator to allocate memory. The user can make this a persistant class by providing an ACE_Allocator with a persistable memory pool.

Definition at line 117 of file SString.h.


Constructor & Destructor Documentation

ACE_SString::ACE_SString ACE_Allocator   alloc = 0
 

Default constructor.

Definition at line 382 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance, len_, ACE_Allocator::malloc, and rep_.

Referenced by substring.

00383   : allocator_ (alloc),
00384     len_ (0),
00385     rep_ (0)
00386 
00387 {
00388   ACE_TRACE ("ACE_SString::ACE_SString");
00389 
00390   if (this->allocator_ == 0)
00391     this->allocator_ = ACE_Allocator::instance ();
00392 
00393   this->len_ = 0;
00394   this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00395   this->rep_[this->len_] = '\0';
00396 }

ACE_SString::ACE_SString const char *    s,
ACE_Allocator   alloc = 0
 

Constructor that copies s into dynamically allocated memory.

Definition at line 415 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance, len_, ACE_Allocator::malloc, rep_, ACE_OS_String::strcpy, and ACE_OS_String::strlen.

00417   : allocator_ (alloc)
00418 {
00419   ACE_TRACE ("ACE_SString::ACE_SString");
00420 
00421   if (this->allocator_ == 0)
00422     this->allocator_ = ACE_Allocator::instance ();
00423 
00424   if (s == 0)
00425     {
00426       this->len_ = 0;
00427       this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00428       this->rep_[this->len_] = '\0';
00429     }
00430   else
00431     {
00432       this->len_ = ACE_OS::strlen (s);
00433       this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00434       ACE_OS::strcpy (this->rep_, s);
00435     }
00436 }

ACE_SString::ACE_SString const char *    s,
size_t    len,
ACE_Allocator   alloc = 0
 

Constructor that copies len chars of @s into dynamically allocated memory (will NUL terminate the result).

Definition at line 455 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance, len_, ACE_Allocator::malloc, ACE_OS_String::memcpy, and rep_.

00458   : allocator_ (alloc)
00459 {
00460   ACE_TRACE ("ACE_SString::ACE_SString");
00461 
00462   if (this->allocator_ == 0)
00463     this->allocator_ = ACE_Allocator::instance ();
00464 
00465   if (s == 0)
00466     {
00467       this->len_ = 0;
00468       this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00469       this->rep_[this->len_] = '\0';
00470     }
00471   else
00472     {
00473       this->len_ = len;
00474       this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00475       ACE_OS::memcpy (this->rep_, s, len);
00476       this->rep_[len] = '\0'; // Make sure to NUL terminate this!
00477     }
00478 }

ACE_SString::ACE_SString const ACE_SString &   
 

Copy constructor.

Definition at line 364 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance, len_, ACE_Allocator::malloc, ACE_OS_String::memcpy, and rep_.

00365   : allocator_ (s.allocator_),
00366     len_ (s.len_)
00367 {
00368   ACE_TRACE ("ACE_SString::ACE_SString");
00369 
00370   if (this->allocator_ == 0)
00371     this->allocator_ = ACE_Allocator::instance ();
00372 
00373   this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
00374   ACE_OS::memcpy ((void *) this->rep_,
00375                   (const void *) s.rep_,
00376                   this->len_);
00377   this->rep_[this->len_] = '\0';
00378 }

ACE_SString::ACE_SString char    c,
ACE_Allocator   alloc = 0
 

Constructor that copies c into dynamically allocated memory.

Definition at line 438 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::instance, len_, ACE_Allocator::malloc, and rep_.

00440   : allocator_ (alloc)
00441 {
00442   ACE_TRACE ("ACE_SString::ACE_SString");
00443 
00444   if (this->allocator_ == 0)
00445     this->allocator_ = ACE_Allocator::instance ();
00446 
00447   this->len_ = 1;
00448   this->rep_ = (char *) this->allocator_->malloc (this->len_ + 1);
00449   this->rep_[0] = c;
00450   this->rep_[this->len_] = '\0';
00451 }

ACE_INLINE ACE_SString::~ACE_SString void   
 

Default destructor.

Definition at line 54 of file SString.i.

00055 {
00056 }


Member Function Documentation

ACE_INLINE const char * ACE_SString::c_str void    const
 

Same as STL String's <c_str> and <fast_rep>.

Definition at line 104 of file SString.i.

References ACE_TRACE, and rep_.

00105 {
00106   ACE_TRACE ("ACE_SString::c_str");
00107   return this->rep_;
00108 }

ACE_INLINE int ACE_SString::compare const ACE_SString &    s const
 

Performs a <strcmp>-style comparison.

Definition at line 154 of file SString.i.

References ACE_TRACE, rep_, and ACE_OS_String::strcmp.

00155 {
00156   ACE_TRACE ("ACE_SString::compare");
00157   return ACE_OS::strcmp (this->rep_, s.rep_);
00158 }

void ACE_SString::dump void    const
 

Dump the state of an object.

Definition at line 357 of file SString.cpp.

References ACE_TRACE.

00358 {
00359   ACE_TRACE ("ACE_SString::dump");
00360 }

ACE_INLINE const char * ACE_SString::fast_rep void    const
 

Get the underlying pointer.

Definition at line 95 of file SString.i.

References ACE_TRACE, and rep_.

Referenced by operator<<.

00096 {
00097   ACE_TRACE ("ACE_SString::fast_rep");
00098   return this->rep_;
00099 }

ACE_INLINE int ACE_SString::find char    c,
int    pos = 0
const
 

Find <c> starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos.

Definition at line 172 of file SString.i.

References npos, rep_, ACE_OS_String::strchr, and substr.

00173 {
00174   char *substr = this->rep_ + pos;
00175   char *pointer = ACE_OS::strchr (substr, c);
00176   if (pointer == 0)
00177     return ACE_SString::npos;
00178   else
00179     return pointer - this->rep_;
00180 }

ACE_INLINE int ACE_SString::find const char *    s,
int    pos = 0
const
 

Find <s> starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos.

Definition at line 161 of file SString.i.

References npos, rep_, ACE_OS_String::strstr, and substr.

00162 {
00163   char *substr = this->rep_ + pos;
00164   char *pointer = ACE_OS::strstr (substr, s);
00165   if (pointer == 0)
00166     return ACE_SString::npos;
00167   else
00168     return pointer - this->rep_;
00169 }

ACE_INLINE int ACE_SString::find const ACE_SString &    str,
int    pos = 0
const
 

Find <str> starting at pos. Returns the slot of the first location that matches (will be >= pos), else npos.

Definition at line 191 of file SString.i.

References rep_.

Referenced by strstr.

00192 {
00193   return this->find (str.rep_, pos);
00194 }

ACE_INLINE u_long ACE_SString::hash void    const
 

Returns a hash value for this string.

Definition at line 210 of file SString.i.

References ACE::hash_pjw.

00211 {
00212   return ACE::hash_pjw (this->rep_);
00213 }

ACE_INLINE size_t ACE_SString::length void    const
 

Return the length of the string.

Definition at line 216 of file SString.i.

References ACE_TRACE, and len_.

Referenced by substr, and substring.

00217 {
00218   ACE_TRACE ("ACE_SString::length");
00219   return this->len_;
00220 }

ACE_INLINE int ACE_SString::operator!= const ACE_SString &    s const
 

Inequality comparison operator.

Definition at line 147 of file SString.i.

References ACE_TRACE.

00148 {
00149   ACE_TRACE ("ACE_SString::operator!=");
00150   return !(*this == s);
00151 }

ACE_INLINE int ACE_SString::operator< const ACE_SString &    s const
 

Less than comparison operator.

Definition at line 123 of file SString.i.

References ACE_TRACE, rep_, and ACE_OS_String::strcmp.

00124 {
00125   ACE_TRACE ("ACE_SString::operator <");
00126 
00127   return (this->rep_ && s.rep_)
00128     ? ACE_OS::strcmp (this->rep_, s.rep_) < 0
00129     : ((s.rep_) ? 1 : 0 );
00130 }

ACE_SString & ACE_SString::operator= const ACE_SString &   
 

Assignment operator (does copy memory).

Definition at line 483 of file SString.cpp.

References ACE_TRACE, allocator_, ACE_Allocator::free, len_, ACE_Allocator::malloc, rep_, and ACE_OS_String::strcpy.

00484 {
00485   ACE_TRACE ("ACE_SString::operator=");
00486   // Check for identify.
00487 
00488   if (this != &s)
00489     {
00490       // Only reallocate if we don't have enough space...
00491       if (this->len_ < s.len_)
00492         {
00493           this->allocator_->free (this->rep_);
00494           this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1);
00495         }
00496       this->len_ = s.len_;
00497       ACE_OS::strcpy (this->rep_, s.rep_);
00498     }
00499 
00500   return *this;
00501 }

ACE_INLINE int ACE_SString::operator== const ACE_SString &    s const
 

Equality comparison operator (must match entire string).

Definition at line 113 of file SString.i.

References ACE_TRACE, len_, rep_, and ACE_OS_String::strcmp.

00114 {
00115   ACE_TRACE ("ACE_SString::operator==");
00116   return this->len_ == s.len_
00117     && ACE_OS::strcmp (this->rep_, s.rep_) == 0;
00118 }

ACE_INLINE int ACE_SString::operator> const ACE_SString &    s const
 

Greater than comparison operator.

Definition at line 135 of file SString.i.

References ACE_TRACE, rep_, and ACE_OS_String::strcmp.

00136 {
00137   ACE_TRACE ("ACE_SString::operator >");
00138 
00139   return (this->rep_ && s.rep_)
00140     ? ACE_OS::strcmp (this->rep_, s.rep_) > 0
00141     : ((this->rep_) ? 1 : 0 );
00142 }

ACE_INLINE char & ACE_SString::operator[] size_t    slot
 

Return the <slot'th> character by reference in the string (doesn't perform bounds checking).

Definition at line 77 of file SString.i.

References ACE_TRACE, and rep_.

00078 {
00079   ACE_TRACE ("ACE_SString::operator[]");
00080   return this->rep_[slot];
00081 }

ACE_INLINE char ACE_SString::operator[] size_t    slot const
 

Return the <slot'th> character in the string (doesn't perform bounds checking).

Definition at line 68 of file SString.i.

References ACE_TRACE, and rep_.

00069 {
00070   ACE_TRACE ("ACE_SString::operator[]");
00071   return this->rep_[slot];
00072 }

ACE_INLINE const char * ACE_SString::rep void    const
 

Get the underlying pointer.

Definition at line 86 of file SString.i.

References ACE_TRACE, and rep_.

00087 {
00088   ACE_TRACE ("ACE_SString::rep");
00089   return this->rep_;
00090 }

void ACE_SString::rep char *    s
 

Set the underlying pointer. Since this does not copy memory or delete existing memory use with extreme caution!!!

Definition at line 401 of file SString.cpp.

References ACE_TRACE, len_, rep_, and ACE_OS_String::strlen.

00402 {
00403   ACE_TRACE ("ACE_SString::rep");
00404 
00405   this->rep_ = s;
00406 
00407   if (s == 0)
00408     this->len_ = 0;
00409   else
00410     this->len_ = ACE_OS::strlen (s);
00411 }

ACE_INLINE int ACE_SString::rfind char    c,
int    pos = npos
const
 

Find <c> starting at pos (counting from the end). Returns the slot of the first location that matches, else npos.

Definition at line 197 of file SString.i.

References npos, and rep_.

00198 {
00199   if (pos == ACE_SString::npos)
00200     pos = ACE_static_cast (int, this->len_);
00201 
00202   for (int i = pos - 1; i >= 0; i--)
00203     if (this->rep_[i] == c)
00204       return i;
00205 
00206   return ACE_SString::npos;
00207 }

ACE_INLINE int ACE_SString::strstr const ACE_SString &    s const
 

Comparison operator that will match substrings. Returns the slot of the first location that matches, else -1.

Definition at line 183 of file SString.i.

References ACE_TRACE, find, and rep_.

00184 {
00185   ACE_TRACE ("ACE_SString::strstr");
00186 
00187   return this->find (s.rep_);
00188 }

ACE_INLINE ACE_SString ACE_SString::substr size_t    offset,
ssize_t    length = -1
const
 

Same as substring.

Definition at line 59 of file SString.i.

References length, ssize_t, and substring.

Referenced by find.

00061 {
00062   return this->substring (offset, length);
00063 }

ACE_SString ACE_SString::substring size_t    offset,
ssize_t    length = -1
const
 

Return a substring given an offset and length, if length == -1 use rest of str return empty substring if offset or offset/length are invalid

Definition at line 505 of file SString.cpp.

References ACE_SString, len_, length, rep_, and ssize_t.

Referenced by substr.

00507 {
00508   ACE_SString nill;
00509   size_t count = length;
00510 
00511   // case 1. empty string
00512   if (len_ == 0)
00513     return nill;
00514 
00515   // case 2. start pos l
00516   if (offset >= len_)
00517     return nill;
00518 
00519   // get all remaining bytes
00520   if (length == -1)
00521     count = len_ - offset;
00522 
00523   return ACE_SString (&rep_[offset], count, this->allocator_);
00524 }


Member Data Documentation

ACE_SString::ACE_ALLOC_HOOK_DECLARE
 

Declare the dynamic allocation hooks.

Definition at line 221 of file SString.h.

ACE_Allocator* ACE_SString::allocator_ [private]
 

Pointer to a memory allocator.

Definition at line 225 of file SString.h.

Referenced by ACE_SString, and operator=.

size_t ACE_SString::len_ [private]
 

Length of the ACE_SString (not counting the trailing '\0').

Definition at line 228 of file SString.h.

Referenced by ACE_SString, length, operator=, operator==, rep, and substring.

const int ACE_SString::npos = -1 [static]
 

No position constant.

Definition at line 307 of file SString.cpp.

Referenced by find, and rfind.

char* ACE_SString::rep_ [private]
 

Pointer to data.

Definition at line 231 of file SString.h.

Referenced by ACE_SString, c_str, compare, fast_rep, find, operator<, operator=, operator==, operator>, operator[], rep, rfind, strstr, and substring.


The documentation for this class was generated from the following files:
Generated on Mon Jun 16 12:56:32 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002