Browse Source

[Minor] Remove optional as function_view has nullable semantics

tags/3.1
Vsevolod Stakhov 2 years ago
parent
commit
63a6ecd9e6
1 changed files with 14 additions and 7 deletions
  1. 14
    7
      src/libmime/mime_string.hxx

+ 14
- 7
src/libmime/mime_string.hxx View File

#include <string> #include <string>
#include <string_view> #include <string_view>
#include <memory> #include <memory>
#include <optional>
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
const Allocator& alloc = Allocator()) noexcept : const Allocator& alloc = Allocator()) noexcept :
basic_mime_string(st.data(), st.size(), alloc) {} basic_mime_string(st.data(), st.size(), alloc) {}


/**
* Creates a string with a filter function. It is calee responsibility to
* ensure that the filter functor survives long enough to work with a string
* @param str
* @param sz
* @param filt
* @param alloc
*/
basic_mime_string(const T* str, std::size_t sz, basic_mime_string(const T* str, std::size_t sz,
filter_type &&filt, filter_type &&filt,
const Allocator& alloc = Allocator()) noexcept : const Allocator& alloc = Allocator()) noexcept :
* @return * @return
*/ */
[[nodiscard]] auto assign_if_valid(storage_type &&other) -> bool { [[nodiscard]] auto assign_if_valid(storage_type &&other) -> bool {
if (filter_func.has_value()) {
if (filter_func) {
/* No way */ /* No way */
return false; return false;
} }
auto assign_copy(const storage_type &other) { auto assign_copy(const storage_type &other) {
storage.clear(); storage.clear();


if (filter_func.has_value()) {
if (filter_func) {
append_c_string_filtered(other.data(), other.size()); append_c_string_filtered(other.data(), other.size());
} }
else { else {
} }


auto append(const T* str, std::size_t size) -> std::size_t { auto append(const T* str, std::size_t size) -> std::size_t {
if (filter_func.has_value()) {
if (filter_func) {
return append_c_string_filtered(str, size); return append_c_string_filtered(str, size);
} }
else { else {
private: private:
mime_string_flags flags = mime_string_flags::MIME_STRING_DEFAULT; mime_string_flags flags = mime_string_flags::MIME_STRING_DEFAULT;
storage_type storage; storage_type storage;
std::optional<filter_type> filter_func;
filter_type filter_func;


auto append_c_string_unfiltered(const T* str, std::size_t len) -> std::size_t { auto append_c_string_unfiltered(const T* str, std::size_t len) -> std::size_t {
/* This is fast path */ /* This is fast path */
flags = flags | mime_string_flags::MIME_STRING_SEEN_INVALID; flags = flags | mime_string_flags::MIME_STRING_SEEN_INVALID;
} }
else { else {
if (filter_func.has_value()) {
uc = filter_func.value()(uc);
if (filter_func) {
uc = filter_func(uc);
} }


if (uc == 0) { if (uc == 0) {

Loading…
Cancel
Save