LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
qtutil.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include "sllconfig.h"
12 #include <QLatin1String>
13 
14 namespace LC::Util
15 {
42  template<typename Assoc>
43  auto Stlize (Assoc&& assoc) noexcept
44  {
45  struct Range
46  {
47  Assoc Assoc_;
48 
49  auto begin () const { return Assoc_.keyValueBegin (); }
50  auto end () const { return Assoc_.keyValueEnd (); }
51  };
52 
53  return Range { std::forward<Assoc> (assoc) };
54  }
55 
68  inline QByteArray AsByteArray (std::string_view view) noexcept
69  {
70  return QByteArray::fromRawData (view.data (), static_cast<int> (view.size ()));
71  }
72 
83  inline QByteArray ToByteArray (std::string_view view) noexcept
84  {
85  return { view.data (), static_cast<int> (view.size ()) };
86  }
87 
93  inline std::string_view AsStringView (const QByteArray& arr) noexcept
94  {
95  return { arr.constData (), static_cast<size_t> (arr.size ()) };
96  }
97 
98  template<typename T, typename S>
99  std::pair<T, T> BreakAt (const T& str, S c) noexcept
100  {
101  const auto pos = str.indexOf (c);
102  if (pos == -1)
103  return { str, {} };
104 
105  return { str.left (pos), str.mid (pos + 1) };
106  }
107 }
108 
109 namespace LC
110 {
111  constexpr QLatin1String operator"" _ql (const char *str, std::size_t size) noexcept
112  {
113  return QLatin1String { str, static_cast<int> (size) };
114  }
115 
116  constexpr QStringView operator"" _qsv (const char16_t *str, std::size_t size) noexcept
117  {
118  return QStringView { str, static_cast<qsizetype> (size) };
119  }
120 
121  inline QByteArray operator"" _qba (const char *str, std::size_t size) noexcept
122  {
123  return QByteArray::fromRawData (str, static_cast<int> (size));
124  }
125 }
sllconfig.h
LC::Util
Definition: icoreproxy.h:33
LC::Util::ToByteArray
QByteArray ToByteArray(std::string_view view) noexcept
Create a QByteArray with the data referenced by the view.
Definition: qtutil.h:89
LC::Util::AsStringView
std::string_view AsStringView(const QByteArray &arr) noexcept
Create a std::string_view referring the data within a QByteArray.
Definition: qtutil.h:99
LC::Util::Stlize
auto Stlize(Assoc &&assoc) noexcept
Converts an Qt's associative sequence assoc to an STL-like iteratable range.
Definition: qtutil.h:49
LC::Util::BreakAt
std::pair< T, T > BreakAt(const T &str, S c) noexcept
Definition: qtutil.h:105
LC::Util::AsByteArray
QByteArray AsByteArray(std::string_view view) noexcept
Convert the view into a QByteArray without copying.
Definition: qtutil.h:74
LC
Definition: constants.h:14