diff --git a/Makefile.am b/Makefile.am index baf3c4f984409365a61e8b2a36ca6911e3af6422..58b68f02b76eaede9b3ceb30bc7e818e8c1e7e1b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -102,6 +102,7 @@ libmedialibrary_la_SOURCES = \ src/utils/Directory.cpp \ src/utils/Filename.cpp \ src/utils/ModificationsNotifier.cpp \ + src/utils/Strings.cpp \ src/utils/Url.cpp \ $(NULL) @@ -161,6 +162,7 @@ noinst_HEADERS = \ src/utils/Directory.h \ src/utils/Filename.h \ src/utils/ModificationsNotifier.h \ + src/utils/Strings.h \ src/utils/SWMRLock.h \ src/utils/Url.h \ src/VideoTrack.h \ diff --git a/src/utils/Strings.cpp b/src/utils/Strings.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19856e50ec4b8f9805e6dee496d088a87c24b574 --- /dev/null +++ b/src/utils/Strings.cpp @@ -0,0 +1,59 @@ +/***************************************************************************** + * Media Library + ***************************************************************************** + * Copyright (C) 2018 Hugo Beauzée-Luyssen, Videolabs + * + * Authors: Hugo Beauzée-Luyssen + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "Strings.h" + +#include +#include + +namespace medialibrary +{ +namespace utils +{ +namespace str +{ + +std::string& trim( std::string& value ) +{ + value.erase( begin( value ), std::find_if( begin( value ), end( value ), []( char c ) { + return isspace( c ) == false; + })); + value.erase( std::find_if( value.rbegin(), value.rend(), []( char c ) { + return isspace( c ) == false; + }).base(), value.end() ); + return value; +} + +std::string trim( const std::string& value ) +{ + std::string v = value; + trim( v ); + return v; +} + +} +} +} diff --git a/src/utils/Strings.h b/src/utils/Strings.h new file mode 100644 index 0000000000000000000000000000000000000000..8efc6677302f499d6f7452a5a0dbdce0e4925ba3 --- /dev/null +++ b/src/utils/Strings.h @@ -0,0 +1,46 @@ +/***************************************************************************** + * Media Library + ***************************************************************************** + * Copyright (C) 2018 Hugo Beauzée-Luyssen, Videolabs + * + * Authors: Hugo Beauzée-Luyssen + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#pragma once + +#include + +namespace medialibrary +{ +namespace utils +{ +namespace str +{ + +/** + * @brief trim Trim the provided string in place + */ +std::string& trim(std::string& value ); + +/** + * @brief trim Returns a copy of the trimmed provided string + */ +std::string trim( const std::string& value ); + +} +} +} diff --git a/test/unittest/MiscTests.cpp b/test/unittest/MiscTests.cpp index 18eee06d887baeaaf98cfc0cfddfb53cb1db674c..cdd56bed60ed9aa409cc68b2db25a3fb5fd77817 100644 --- a/test/unittest/MiscTests.cpp +++ b/test/unittest/MiscTests.cpp @@ -29,6 +29,7 @@ #include "Tests.h" #include "database/SqliteTools.h" #include "database/SqliteConnection.h" +#include "utils/Strings.h" #include "Artist.h" #include "Media.h" @@ -48,6 +49,15 @@ TEST_F( Misc, FileExtensions ) } } +TEST_F( Misc, TrimString ) +{ + ASSERT_EQ( utils::str::trim( "hello world" ), "hello world" ); + ASSERT_EQ( utils::str::trim( " spaaaaaace " ), "spaaaaaace" ); + ASSERT_EQ( utils::str::trim( "\tfluffy\notters \t\n" ), "fluffy\notters" ); + ASSERT_EQ( utils::str::trim( " " ), "" ); + ASSERT_EQ( utils::str::trim( "" ), "" ); +} + class DbModel : public testing::Test { protected: