view misc/strip.hpp @ 607:f8616f9fc9d5

Misc: merge executable_path into one function
author David Demelier <markand@malikania.fr>
date Thu, 29 Jun 2017 09:20:24 +0200
parents c79a501782b0
children
line wrap: on
line source

/*
 * strip.hpp -- remove leading and trailing spaces
 *
 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/**
 * \file strip.hpp
 * \brief Strip function
 */

#include <algorithm>
#include <cctype>

/**
 * Remove leading and trailing spaces.
 *
 * \param str the string
 * \return the removed white spaces
 */
std::string strip(std::string str)
{
    auto test = [] (char c) { return !std::isspace(c); };

    str.erase(str.begin(), std::find_if(str.begin(), str.end(), test));
    str.erase(std::find_if(str.rbegin(), str.rend(), test).base(), str.end());

    return str;
}