[gparted] Extract repeated code into trim_trailing_new_line() (!105)



commit 1fbc8988ff4311554fb31f8bc490434d8b6635db
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Fri Aug 12 08:38:16 2022 +0100

    Extract repeated code into trim_trailing_new_line() (!105)
    
    Create function to replace repeated code which optionally removes
    trailing new line character from a string.
    
    Closes !105 - Update used btrfs file system commands, new minimum is
                  btrfs-progs 4.5

 include/Utils.h              |  1 +
 src/DMRaid.cc                |  5 +----
 src/Dialog_Partition_Info.cc |  8 +-------
 src/FS_Info.cc               | 15 +++++----------
 src/Utils.cc                 | 11 +++++++++++
 src/btrfs.cc                 |  7 +------
 6 files changed, 20 insertions(+), 27 deletions(-)
---
diff --git a/include/Utils.h b/include/Utils.h
index d887250e..6d31d7d0 100644
--- a/include/Utils.h
+++ b/include/Utils.h
@@ -170,6 +170,7 @@ public:
                                         , const Glib::ustring & pattern
                                         ) ;
        static Glib::ustring trim( const Glib::ustring & src, const Glib::ustring & c = " \t\r\n" ) ;
+       static Glib::ustring trim_trailing_new_line(const Glib::ustring& src);
        static Glib::ustring last_line( const Glib::ustring & src );
        static Glib::ustring get_lang() ;
        static void tokenize( const Glib::ustring& str,
diff --git a/src/DMRaid.cc b/src/DMRaid.cc
index 25fec802..cc1cdae9 100644
--- a/src/DMRaid.cc
+++ b/src/DMRaid.cc
@@ -572,11 +572,8 @@ std::vector<Glib::ustring> DMRaid::lookup_dmraid_members(const Glib::ustring& ar
        Glib::ustring error;
        Utils::execute_command("udevadm info --query=name " + Glib::shell_quote(array),
                               output, error, true);
-
        // Strip terminating new line from output.
-       size_t len = output.length();
-       if (len > 0 && output[len-1] == '\n')
-               output.resize(len-1);
+       output = Utils::trim_trailing_new_line(output);
 
        if (output.empty())
                return members;  // Empty vector
diff --git a/src/Dialog_Partition_Info.cc b/src/Dialog_Partition_Info.cc
index 0f8c3f36..5d659843 100644
--- a/src/Dialog_Partition_Info.cc
+++ b/src/Dialog_Partition_Info.cc
@@ -105,13 +105,7 @@ Dialog_Partition_Info::Dialog_Partition_Info( const Partition & partition ) : pa
                        if ( concatenated_messages.size() > 0 )
                                concatenated_messages += "\n\n";
 
-                       Glib::ustring::size_type take = messages[i].size();
-                       if ( take > 0 )
-                       {
-                               if ( messages[i][take-1] == '\n' )
-                                       take -- ;  //Skip optional trailing new line
-                               concatenated_messages += "<i>" + messages[i].substr( 0, take ) + "</i>";
-                       }
+                       concatenated_messages += "<i>" + Utils::trim_trailing_new_line(messages[i]) + "</i>";
                }
                Gtk::Box *vbox = manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
                vbox ->set_border_width( 5 ) ;
diff --git a/src/FS_Info.cc b/src/FS_Info.cc
index 4bd22165..47776470 100644
--- a/src/FS_Info.cc
+++ b/src/FS_Info.cc
@@ -289,17 +289,12 @@ bool FS_Info::run_blkid_update_cache_one_label( FS_Entry & fs_entry )
        if ( ! success )
                return false;
 
-       size_t len = output.length();
-       if ( len > 0 && output[len-1] == '\n' )
-       {
-               // Output is either the label with a terminating new line or zero bytes
-               // when the file system has no label.  Strip optional trailing new line
-               // from blkid output.
-               output.resize( len-1 );
-       }
-       // Update cache entry with the read label.
+       // Output from blkid is either the label with a trailing new line character or
+       // zero bytes when the file system has no label.  Update the cache entry in both
+       // cases as the label was successfully read even if it didn't exist so is zero
+       // characters long.
        fs_entry.have_label = true;
-       fs_entry.label = output;
+       fs_entry.label = Utils::trim_trailing_new_line(output);
        return true;
 }
 
diff --git a/src/Utils.cc b/src/Utils.cc
index 8089b198..20922675 100644
--- a/src/Utils.cc
+++ b/src/Utils.cc
@@ -809,6 +809,17 @@ Glib::ustring Utils::trim( const Glib::ustring & src, const Glib::ustring & c /*
        return src.substr(p1, (p2-p1)+1);
 }
 
+
+// Return string with optional trailing new line character removed.
+Glib::ustring Utils::trim_trailing_new_line(const Glib::ustring& src)
+{
+       Glib::ustring::size_type len = src.length();
+       if (len > 0 && src[len-1] == '\n')
+               len --;
+       return src.substr(0, len);
+}
+
+
 // Return portion of string after the last carriage return character or
 // the whole string when there is no carriage return character.
 Glib::ustring Utils::last_line( const Glib::ustring & src )
diff --git a/src/btrfs.cc b/src/btrfs.cc
index e8ad9862..bb80d529 100644
--- a/src/btrfs.cc
+++ b/src/btrfs.cc
@@ -357,12 +357,7 @@ void btrfs::read_label(Partition& partition)
                return;
        }
 
-       // Strip terminating new line from output.
-       size_t len = output.length();
-       if (len > 0 && output[len-1] == '\n')
-               output.resize(len-1);
-
-       partition.set_filesystem_label(output);
+       partition.set_filesystem_label(Utils::trim_trailing_new_line(output));
 }
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]