libcamgm
Public Types | Public Member Functions | Private Attributes | Related Functions | List of all members
ca_mgm::C_Str Class Reference

#include <String.hpp>

Public Types

typedef std::string::size_type size_type
 

Public Member Functions

 C_Str ()
 
 C_Str (char *c_str_r)
 
 C_Str (const char *c_str_r)
 
 C_Str (const std::string &str_r)
 
bool isNull () const
 
bool empty () const
 
size_type size () const
 
 operator const char * () const
 
const char * c_str () const
 

Private Attributes

const char *const _val
 
size_type _sze
 

Related Functions

(Note that these are not member functions.)

std::ostream & operator<< (std::ostream &str, const C_Str &obj)
 

Detailed Description

Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string.

bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
{ return( ::strncmp( str_r.c_str(), prefix_r.c_str(), prefix_r.size() ) == 0 ); }

Called with a plain char* as argument, the std::string is created form for nothing. The implementation actually does not use the std::string.

Best would be to implement hasPrefix for each combination of char* and std::string arguments:

bool hasPrefix( const std::string & str_r, const std::string & prefix_r )
{ return( ::strncmp( str_r.c_str(), prefix_r.c_str(), prefix_r.size() ) == 0 ); }
bool hasPrefix( const std::string & str_r, const char * prefix_r )
{ return( !prefix_r || ::strncmp( str_r.c_str(), prefix_r, ::strlen(prefix_r) ) == 0 ); }
bool hasPrefix( const char * str_r, const std::string & prefix_r )
{ return( str_r ? ::strncmp( str_r, prefix_r.c_str(), prefix_r.size() ) == 0 : prefix_r.empty() ); }
bool hasPrefix( const char * str_r, const char * prefix_r )
{ return( str && prefix_r ? ::strncmp( str_r, prefix_r, ::strlen(prefix_r) ) == 0
: !((str_r && *str_r) || (prefix_r && *prefix_r)); }

This is where C_Str can help. Constructible from std::string and char*, it reduces the std::string to it's char*. At the same time it converts (char*)0 into an "" string.

bool hasPrefix( const C_Str & str_r, const C_Str & prefix_r )
{ return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }

Member Typedef Documentation

typedef std::string::size_type ca_mgm::C_Str::size_type

Constructor & Destructor Documentation

ca_mgm::C_Str::C_Str ( )
inline
ca_mgm::C_Str::C_Str ( char *  c_str_r)
inline
ca_mgm::C_Str::C_Str ( const char *  c_str_r)
inline
ca_mgm::C_Str::C_Str ( const std::string &  str_r)
inline

Member Function Documentation

const char* ca_mgm::C_Str::c_str ( ) const
inline
bool ca_mgm::C_Str::empty ( ) const
inline

References _val.

bool ca_mgm::C_Str::isNull ( ) const
inline

References _val.

ca_mgm::C_Str::operator const char * ( ) const
inline

References c_str().

size_type ca_mgm::C_Str::size ( ) const
inline

Friends And Related Function Documentation

std::ostream & operator<< ( std::ostream &  str,
const C_Str obj 
)
related

Stream output

References c_str().

Member Data Documentation

size_type ca_mgm::C_Str::_sze
mutableprivate

Referenced by size().

const char* const ca_mgm::C_Str::_val
private

Referenced by c_str(), empty(), isNull(), and size().


The documentation for this class was generated from the following file: