Skip to content
Permalink
Browse files

item: trim whitespaces from strings during construction

Related to #53
  • Loading branch information...
Tmplt committed May 22, 2018
1 parent 1857673 commit e543802c107124a71393523a473b669d18b8c27a
Showing with 32 additions and 3 deletions.
  1. +1 −1 src/core/item.cpp
  2. +10 −1 src/string.cpp
  3. +21 −1 src/string.hpp
@@ -20,7 +20,7 @@ int exacts_t::get_value(const std::map<string, int> &dict, const string &&key)
const string nonexacts_t::get_value(const std::map<string, string> &dict, const string &&key)
{
const auto elem = dict.find(key);
return elem == dict.cend() ? "" : elem->second;
return elem == dict.cend() ? "" : trim(elem->second);
}

bool exacts_t::operator==(const exacts_t &other) const
@@ -2,6 +2,16 @@

namespace bookwyrm {

string trim(const string &str, const string &delims)
{
size_t b = str.find_first_not_of(delims);
size_t e = str.find_last_not_of(delims);
if (b == string::npos || e == string::npos)
return "";

return string(str, b, e - b + 1);
}

string vector_to_string(const vector<string> &vec)
{
string retstring = "";
@@ -42,6 +52,5 @@ std::pair<string, string> split_at_first(const string &str, string &&sep)
return {left, right};
}


/* ns bookwyrm */
}
@@ -8,10 +8,30 @@ using std::vector;

namespace bookwyrm {

// TODO: improve this (and with string_views)?
/**
* @brief Trims leading and trailing characters from a string
* @param str String that should be trimmed
* @param delims List of delimiters to trim from the string (defaults to all whitespace)
*/
string trim(const string &str, const string &delims = " \t\n\r\v");

/**
* @brief Makes a list of a list of strings
* @param vec The vector of strings that should be made a list
*/
string vector_to_string(const vector<string> &vec);

/**
* @brief Splits a string into a vector at whitespaces
* @param str The string that should be split
*/
vector<string> split_string(const string &str);

/**
* @brief Splits a string in to at a given seperator
* @param str The string that should be split
* @param sep The seperator at which the given string should be split
*/
std::pair<string, string> split_at_first(const string &str, string &&sep);

/* ns bookwyrm */

0 comments on commit e543802

Please sign in to comment.
You can’t perform that action at this time.