mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-08-03 12:50:02 +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:
parent
5b88afb276
commit
b5b0db8b8d
3 changed files with 75 additions and 0 deletions
|
@ -33,6 +33,7 @@ add_library(${PROJECT_NAME} STATIC
|
||||||
ip_utils.cpp
|
ip_utils.cpp
|
||||||
mime_split.cpp
|
mime_split.cpp
|
||||||
storage.cpp
|
storage.cpp
|
||||||
|
truncated_string.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME}
|
target_include_directories(${PROJECT_NAME}
|
||||||
|
|
48
src/tawashi/truncated_string.cpp
Normal file
48
src/tawashi/truncated_string.cpp
Normal 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
|
26
src/tawashi/truncated_string.hpp
Normal file
26
src/tawashi/truncated_string.hpp
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue