From 02930198a8b4d26403170b526d9e6357c23aeed7 Mon Sep 17 00:00:00 2001 From: Abs62 Date: Thu, 26 Sep 2019 19:40:39 +0300 Subject: [PATCH] DSL: Trim spaces in "s" and "url" tags --- dsl.cc | 4 ++-- filetype.cc | 21 ++++++++++----------- filetype.hh | 2 ++ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/dsl.cc b/dsl.cc index cd3d8581..37df6774 100644 --- a/dsl.cc +++ b/dsl.cc @@ -858,7 +858,7 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node ) else if ( node.tagName == GD_NATIVE_TO_WS( L"s" ) || node.tagName == GD_NATIVE_TO_WS( L"video" ) ) { - string filename = Utf8::encode( node.renderAsText() ); + string filename = Filetype::simplifyString( Utf8::encode( node.renderAsText() ), false ); string n = getDictionaryFilenames()[ 0 ] + ".files" + FsEncoding::separator() + @@ -999,7 +999,7 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node ) else if ( node.tagName == GD_NATIVE_TO_WS( L"url" ) ) { - string link = Html::escape( Utf8::encode( node.renderAsText() ) ); + string link = Html::escape( Filetype::simplifyString( Utf8::encode( node.renderAsText() ), false ) ); if( QUrl::fromEncoded( link.c_str() ).scheme().isEmpty() ) link = "http://" + link; diff --git a/filetype.cc b/filetype.cc index c8fa5fd1..c3b0391d 100644 --- a/filetype.cc +++ b/filetype.cc @@ -9,11 +9,19 @@ namespace Filetype { namespace { +/// Checks if the given string ends with the given substring +bool endsWith( string const & str, string const & tail ) +{ + return str.size() >= tail.size() && + str.compare( str.size() - tail.size(), tail.size(), tail ) == 0; +} + +} /// Removes any trailing or leading spaces and lowercases the string. /// The lowercasing is done simplistically, but it is enough for file /// extensions. -string simplifyString( string const & str ) +string simplifyString( string const & str, bool lowercase ) { string result; @@ -33,20 +41,11 @@ string simplifyString( string const & str ) result.reserve( endPos - beginPos ); while( beginPos < endPos ) - result.push_back( tolower( str[ beginPos++ ] ) ); + result.push_back( lowercase ? tolower( str[ beginPos++ ] ) : str[ beginPos++ ] ); return result; } -/// Checks if the given string ends with the given substring -bool endsWith( string const & str, string const & tail ) -{ - return str.size() >= tail.size() && - str.compare( str.size() - tail.size(), tail.size(), tail ) == 0; -} - -} - bool isNameOfSound( string const & name ) { string s = simplifyString( name ); diff --git a/filetype.hh b/filetype.hh index 35445c8b..f847b3de 100644 --- a/filetype.hh +++ b/filetype.hh @@ -11,6 +11,8 @@ namespace Filetype { using std::string; +/// Removes any trailing or leading spaces and may lowercases the string. +string simplifyString( string const & str, bool lowercase = true ); /// Returns true if the name resembles the one of a sound file (i.e. ends /// with .wav, .ogg and such). bool isNameOfSound( string const & );