libzypp 17.31.23
zypp::C_Str Class Reference

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

#include <base/String.h>

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)
 
 C_Str (const boost::string_ref &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)
 Stream output.
 

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)); }
String related utilities and Regular expression matching.
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:1027

This is where C_Str can help. Constructible from std::string and char*, it reduces the std::string to its 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 ); }
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string.
Definition: String.h:91
size_type size() const
Definition: String.h:108
Todo:
Check whether to replace by boost::string_ref

Definition at line 90 of file String.h.

Member Typedef Documentation

◆ size_type

typedef std::string::size_type zypp::C_Str::size_type

Definition at line 93 of file String.h.

Constructor & Destructor Documentation

◆ C_Str() [1/5]

zypp::C_Str::C_Str ( )
inline

Definition at line 96 of file String.h.

◆ C_Str() [2/5]

zypp::C_Str::C_Str ( char *  c_str_r)
inline

Definition at line 97 of file String.h.

◆ C_Str() [3/5]

zypp::C_Str::C_Str ( const char *  c_str_r)
inline

Definition at line 98 of file String.h.

◆ C_Str() [4/5]

zypp::C_Str::C_Str ( const std::string &  str_r)
inline

Definition at line 99 of file String.h.

◆ C_Str() [5/5]

zypp::C_Str::C_Str ( const boost::string_ref &  str_r)
inline

Definition at line 100 of file String.h.

Member Function Documentation

◆ isNull()

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

Definition at line 106 of file String.h.

◆ empty()

bool zypp::C_Str::empty ( ) const
inline

Definition at line 107 of file String.h.

◆ size()

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

Definition at line 108 of file String.h.

◆ operator const char *()

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

Definition at line 115 of file String.h.

◆ c_str()

const char * zypp::C_Str::c_str ( ) const
inline

Definition at line 116 of file String.h.

Friends And Related Function Documentation

◆ operator<<()

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

Stream output.

Definition at line 124 of file String.h.

Member Data Documentation

◆ _val

const char* const zypp::C_Str::_val
private

Definition at line 119 of file String.h.

◆ _sze

size_type zypp::C_Str::_sze
mutableprivate

Definition at line 120 of file String.h.


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