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.
{ return( ::strncmp( str_r, prefix_r, prefix_r.size() ) == 0 ); }
- Todo:
- Check whether to replace by boost::string_ref
Definition at line 90 of file String.h.