[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-ddrescue] [PATCH 2/7] treewide: Replace size() with empty() where a
From: |
Rosen Penev |
Subject: |
[Bug-ddrescue] [PATCH 2/7] treewide: Replace size() with empty() where appropriate |
Date: |
Mon, 14 Oct 2019 13:59:41 -0700 |
>From Clang:
The emptiness of a container should be checked using the empty() method
instead of the size() method. It is not guaranteed that size() is a
constant-time function, and it is generally more efficient and also shows
clearer intent to use empty(). Furthermore some containers may implement
the empty() method but not implement the size() method. Using empty()
whenever possible makes it easier to switch to another container in the
future.
Signed-off-by: Rosen Penev <address@hidden>
---
arg_parser.cc | 4 ++--
command_mode.cc | 6 +++---
ddrescuelog.cc | 2 +-
fillbook.cc | 2 +-
genbook.cc | 2 +-
main.cc | 2 +-
main_common.cc | 2 +-
mapbook.cc | 2 +-
mapfile.cc | 4 ++--
rescuebook.cc | 2 +-
10 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/arg_parser.cc b/arg_parser.cc
index ea32fde..7cdd708 100644
--- a/arg_parser.cc
+++ b/arg_parser.cc
@@ -167,7 +167,7 @@ Arg_parser::Arg_parser( const int argc, const char * const
argv[],
else non_options.push_back( argv[argind++] );
}
}
- if( error_.size() ) data.clear();
+ if( !error_.empty() ) data.clear();
else
{
for( unsigned i = 0; i < non_options.size(); ++i )
@@ -190,7 +190,7 @@ Arg_parser::Arg_parser( const char * const opt, const char
* const arg,
{ if( opt[2] ) parse_long_option( opt, arg, options, argind ); }
else
parse_short_option( opt, arg, options, argind );
- if( error_.size() ) data.clear();
+ if( !error_.empty() ) data.clear();
}
else data.push_back( Record( opt ) );
}
diff --git a/command_mode.cc b/command_mode.cc
index ec6a389..2120a80 100644
--- a/command_mode.cc
+++ b/command_mode.cc
@@ -108,10 +108,10 @@ int Rescuebook::do_commands( const int ides, const int
odes )
while( true )
{
const int c = std::fgetc( stdin );
- if( c == '\n' ) { if( command.size() ) break; else continue; }
+ if( c == '\n' ) { if( !command.empty() ) break; else continue; }
if( c == EOF ) { command = "f"; break; } // discard partial command
if( !std::isspace( c ) ) command += c;
- else if( command.size() && !std::isspace( command[command.size()-1] ) )
+ else if( !command.empty() && !std::isspace( command[command.size()-1] ) )
command += ' ';
}
if( command == "q" ) break;
@@ -133,7 +133,7 @@ int Rescuebook::do_commands( const int ides, const int odes
)
if( tmp <= 0 ) std::fputs( "done\n", stdout );
else
{
- if( final_msg().size() )
+ if( !final_msg().empty() )
{ printf( "error: %s%s%s\n", final_msg().c_str(),
( final_errno() > 0 ) ? ": " : "",
( final_errno() > 0 ) ? std::strerror( final_errno() ) : ""
);
diff --git a/ddrescuelog.cc b/ddrescuelog.cc
index 562d35f..a17336b 100644
--- a/ddrescuelog.cc
+++ b/ddrescuelog.cc
@@ -595,7 +595,7 @@ int main( const int argc, const char * const argv[] )
{ 0 , 0, Arg_parser::no } };
const Arg_parser parser( argc, argv, options );
- if( parser.error().size() ) // bad option
+ if( !parser.error().empty() ) // bad option
{ show_error( parser.error().c_str(), 0, true ); return 1; }
int argind = 0;
diff --git a/fillbook.cc b/fillbook.cc
index 7a3479f..3a69836 100644
--- a/fillbook.cc
+++ b/fillbook.cc
@@ -204,7 +204,7 @@ int Fillbook::do_fill( const int odes )
compact_sblock_vector();
if( !update_mapfile( odes_, true ) && retval == 0 ) retval = 1;
}
- if( final_msg().size() ) show_error( final_msg().c_str(), final_errno() );
+ if( !final_msg().empty() ) show_error( final_msg().c_str(), final_errno() );
if( close( odes_ ) != 0 )
{ show_error( "Error closing outfile", errno );
if( retval == 0 ) retval = 1; }
diff --git a/genbook.cc b/genbook.cc
index f818bf4..7540216 100644
--- a/genbook.cc
+++ b/genbook.cc
@@ -204,7 +204,7 @@ int Genbook::do_generate( const int odes )
compact_sblock_vector();
if( !update_mapfile( -1, true ) && retval == 0 ) retval = 1;
}
- if( final_msg().size() ) show_error( final_msg().c_str(), final_errno() );
+ if( !final_msg().empty() ) show_error( final_msg().c_str(), final_errno() );
if( retval ) return retval; // errors have priority over signals
if( signaled ) return signaled_exit();
return 0;
diff --git a/main.cc b/main.cc
index 5ed4d78..de0071f 100644
--- a/main.cc
+++ b/main.cc
@@ -812,7 +812,7 @@ int main( const int argc, const char * const argv[] )
{ 0 , 0, Arg_parser::no } };
const Arg_parser parser( argc, argv, options );
- if( parser.error().size() ) // bad option
+ if( !parser.error().empty() ) // bad option
{ show_error( parser.error().c_str(), 0, true ); return 1; }
int argind = 0;
diff --git a/main_common.cc b/main_common.cc
index c37dd8d..2fdf134 100644
--- a/main_common.cc
+++ b/main_common.cc
@@ -114,7 +114,7 @@ bool check_types( std::string & types, const char * const
opt_name,
if( !Sblock::isstatus( types[i] ) )
{ error = true; break; }
}
- if( !types.size() || error )
+ if( types.empty() || error )
{
char buf[80];
snprintf( buf, sizeof buf, "Invalid type for '%s' option.", opt_name );
diff --git a/mapbook.cc b/mapbook.cc
index 4bb4e8c..bb35b4b 100644
--- a/mapbook.cc
+++ b/mapbook.cc
@@ -77,7 +77,7 @@ bool Mapbook::emergency_save()
const char * const p = std::getenv( "HOME" );
if( p ) { home_name = p; home_name += '/'; home_name += dead_name; }
}
- if( home_name.size() &&
+ if( !home_name.empty() &&
filename() != home_name && save_mapfile( home_name.c_str() ) )
return true;
show_error( "Emergency save failed." );
diff --git a/mapfile.cc b/mapfile.cc
index bbac1aa..ba4a4b8 100644
--- a/mapfile.cc
+++ b/mapfile.cc
@@ -243,7 +243,7 @@ bool Mapfile::read_mapfile( const int
default_sblock_status, const bool ro )
( size > 0 || ( size == 0 && pos == 0 ) ) )
{
const Sblock sb( pos, size, Sblock::Status( ch ) );
- const long long end = sblock_vector.size() ?
+ const long long end = !sblock_vector.empty() ?
sblock_vector.back().end() : 0;
if( sb.pos() != end )
{
@@ -276,7 +276,7 @@ int Mapfile::write_mapfile( FILE * f, const bool timestamp,
if( !f ) { f = std::fopen( filename_, "w" ); if( !f ) return false; }
write_file_header( f, "Mapfile" );
if( timestamp ) write_timestamp( f );
- if( current_msg.size() ) std::fprintf( f, "# %s\n", current_msg.c_str() );
+ if( !current_msg.empty() ) std::fprintf( f, "# %s\n", current_msg.c_str() );
char buf[80] = { 0 }; // comment
if( annotate_domainp )
snprintf( buf, sizeof buf, "\t# %sB", format_num( current_pos_ ) );
diff --git a/rescuebook.cc b/rescuebook.cc
index 4f442af..8674277 100644
--- a/rescuebook.cc
+++ b/rescuebook.cc
@@ -943,7 +943,7 @@ int Rescuebook::do_rescue( const int ides, const int odes )
compact_sblock_vector();
if( !update_mapfile( odes_, true ) && retval == 0 ) retval = 1;
}
- if( final_msg().size() ) show_error( final_msg().c_str(), final_errno() );
+ if( !final_msg().empty() ) show_error( final_msg().c_str(), final_errno() );
if( close( odes_ ) != 0 )
{ show_error( "Error closing outfile", errno );
if( retval == 0 ) retval = 1; }
--
2.17.1
[Bug-ddrescue] [PATCH 3/7] [clang-tidy] Remove const from superfluous locations, Rosen Penev, 2019/10/14
Re: [Bug-ddrescue] [PATCH 1/7] tree: Remove stdint.h from two unnecessary places, Antonio Diaz Diaz, 2019/10/15