const_string.h 790 B

1234567891011121314151617181920212223242526272829303132333435
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include <QtCore/QString>
  9. #include <QtCore/QByteArray>
  10. #include <string_view>
  11. namespace base {
  12. class const_string final : public std::string_view {
  13. public:
  14. using std::string_view::string_view;
  15. [[nodiscard]] QString utf16() const {
  16. return QString::fromUtf8(data(), size());
  17. }
  18. [[nodiscard]] QByteArray utf8() const {
  19. return QByteArray::fromRawData(data(), size());
  20. }
  21. };
  22. } // namespace base
  23. [[nodiscard]] inline constexpr base::const_string operator""_cs(
  24. const char *data,
  25. std::size_t size) {
  26. return { data, size };
  27. }