Commit 81fea597 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

utils: Add a trim helper function

parent 906e9fd3
......@@ -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 \
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2018 Hugo Beauzée-Luyssen, Videolabs
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* 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 <algorithm>
#include <cctype>
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;
}
}
}
}
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2018 Hugo Beauzée-Luyssen, Videolabs
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* 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 <string>
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 );
}
}
}
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment