user-gcc/src/string_view_cat.hpp

65 lines
1.8 KiB
C++

/* Copyright 2020, Michele Santullo
* This file is part of user-gcc.
*
* User-gcc 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.
*
* User-gcc 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 user-gcc. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <string_view>
namespace duck {
inline std::string to_string (std::string_view sv) {
return std::string((sv.empty() ? static_cast<const char*>("") : sv.data()), sv.size());
}
inline std::string to_string (const char* str) {
return std::string(str ? str : static_cast<const char*>(""));
}
inline std::string to_string (std::string str) {
return str;
}
inline std::string_view to_string_view (const char* str) noexcept {
return (str ? std::string_view{str} : std::string_view{""});
}
template <std::size_t S>
inline std::string_view to_string_view (const char (&str)[S]) noexcept {
return std::string_view(str, S);
}
inline std::string operator+ (std::string_view a, std::string_view b) {
std::string ret(to_string(a));
ret += b;
return ret;
}
inline std::string operator+ (std::string_view a, const std::string& b) {
std::string ret(to_string(a));
ret += b;
return ret;
}
inline std::string operator+ (std::string a, std::string_view b) {
a += b;
return a;
}
inline std::string operator+ (const char* a, std::string_view b) {
return to_string(a) + b;
}
} //namespace duck