Enhance regexp_label method to handle unicode characters

Prompted by Bug #662537 - Ext4 unicode labels not shown correctly
This commit is contained in:
Curtis Gedak 2011-11-01 13:08:36 -06:00
parent 2ae36153ee
commit b6f1c56fb1
2 changed files with 20 additions and 18 deletions

View File

@ -147,8 +147,9 @@ public:
Glib::ustring & output,
Glib::ustring & error,
bool use_C_locale = false ) ;
static Glib::ustring regexp_label( const Glib::ustring & text,
const Glib::ustring & regular_sub_expression ) ;
static Glib::ustring regexp_label( const Glib::ustring & text
, const Glib::ustring & pattern
) ;
static Glib::ustring fat_compliant_label( const Glib::ustring & label ) ;
static Glib::ustring create_mtoolsrc_file( char file_name[],
const char drive_letter, const Glib::ustring & device_path ) ;

View File

@ -20,7 +20,7 @@
#include <sstream>
#include <iomanip>
#include <regex.h>
#include <glibmm/regex.h>
#include <locale.h>
@ -327,23 +327,24 @@ int Utils::execute_command( const Glib::ustring & command,
return exit_status ;
}
Glib::ustring Utils::regexp_label( const Glib::ustring & text,
const Glib::ustring & regular_sub_expression )
Glib::ustring Utils::regexp_label( const Glib::ustring & text
, const Glib::ustring & pattern
)
{
//Extract text from a regular sub-expression. E.g., "text we don't want (text we want)"
Glib::ustring label = "";
regex_t preg ;
int nmatch = 2 ;
regmatch_t pmatch[ 2 ] ;
int rc = regcomp( &preg, regular_sub_expression .c_str(), REG_EXTENDED | REG_ICASE | REG_NEWLINE ) ;
if(rc == 0)
{
if (regexec(&preg, text.c_str(), nmatch, pmatch, 0) == 0)
label = text .substr( pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so ) ;
//Extract text from a regular sub-expression or pattern.
// E.g., "text we don't want (text we want)"
std::vector<Glib::ustring> results;
Glib::RefPtr<Glib::Regex> myregexp =
Glib::Regex::create( pattern
, Glib::REGEX_CASELESS | Glib::REGEX_MULTILINE
);
regfree(&preg);
}
return label ;
results = myregexp ->split( text );
if ( results .size() >= 2 )
return results[ 1 ] ;
else
return "" ;
}
Glib::ustring Utils::fat_compliant_label( const Glib::ustring & label )