1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-06-07 00:51:41 +00:00

Implement truncated_string().

It will either return the full string if its length was
maxlen or less, or up to maxlen characters with
a "..." appended at the end. It will also replace any
\n or \r character with a literal "\n" or "\r"
respectively. Only line breaks are being replaced, and
this happens inside truncated_string() because truncating
only makes sense to me if you want to use it to
keep stuff short and in one single line.
This commit is contained in:
King_DuckZ 2017-06-08 21:32:04 +01:00
parent 5b88afb276
commit b5b0db8b8d
3 changed files with 75 additions and 0 deletions

View file

@ -33,6 +33,7 @@ add_library(${PROJECT_NAME} STATIC
ip_utils.cpp
mime_split.cpp
storage.cpp
truncated_string.cpp
)
target_include_directories(${PROJECT_NAME}

View file

@ -0,0 +1,48 @@
/* Copyright 2017, Michele Santullo
* This file is part of "tawashi".
*
* "tawashi" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "tawashi" 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "tawashi". If not, see <http://www.gnu.org/licenses/>.
*/
#include "truncated_string.hpp"
#include <cassert>
#include <algorithm>
#include <ciso646>
namespace tawashi {
std::string truncated_string (const boost::string_view& parString, std::size_t parMaxLen) {
const std::size_t char_length = std::min(parMaxLen, parString.size());
std::string retval;
retval.reserve(char_length);
std::size_t doubled_up = 0;
for (auto c : parString) {
const std::size_t len = ('\r' == c or '\n' == c ? 2 : 1);
const char replaced_chara = ('\r' == c ? 'r' : ('\n' == c ? 'n' : c));
const char addition[2] = { '\\', replaced_chara };
if (retval.size() + len > parMaxLen)
break;
retval.append(addition + (2 - len), len);
doubled_up += (len - 1);
}
assert(
retval.size() == std::min(parString.size() + doubled_up, parMaxLen) or
retval.size() + 1 == std::min(parString.size() + doubled_up, parMaxLen)
);
if (retval.size() - doubled_up < parString.size())
retval += "...";
return retval;
}
} //namespace tawashi

View file

@ -0,0 +1,26 @@
/* Copyright 2017, Michele Santullo
* This file is part of "tawashi".
*
* "tawashi" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "tawashi" 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "tawashi". If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <boost/utility/string_view.hpp>
#include <cstddef>
namespace tawashi {
std::string truncated_string (const boost::string_view& parString, std::size_t parMaxLen);
} //namespace tawashi