qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
toxstring.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2017-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "toxstring.h"
21 
22 #include <QByteArray>
23 #include <QString>
24 
25 #include <cassert>
26 #include <climits>
27 
37 ToxString::ToxString(const QString& text)
38  : ToxString(text.toUtf8())
39 {
40 }
41 
46 ToxString::ToxString(const QByteArray& text)
47  : string(text)
48 {
49 }
50 
56 ToxString::ToxString(const uint8_t* text, size_t length)
57 {
58  assert(length <= INT_MAX);
59  string = QByteArray(reinterpret_cast<const char*>(text), length);
60 }
61 
66 const uint8_t* ToxString::data() const
67 {
68  return reinterpret_cast<const uint8_t*>(string.constData());
69 }
70 
75 size_t ToxString::size() const
76 {
77  return string.size();
78 }
79 
84 QString ToxString::getQString() const
85 {
86  return QString::fromUtf8(string);
87 }
88 
93 QByteArray ToxString::getBytes() const
94 {
95  return QByteArray(string);
96 }
ToxString::getQString
QString getQString() const
Gets the string as QString.
Definition: toxstring.cpp:84
ToxString::getBytes
QByteArray getBytes() const
getBytes Gets the bytes of the string.
Definition: toxstring.cpp:93
toxstring.h
ToxString
Helper to convert safely between strings in the c-toxcore representation and QString.
Definition: toxstring.h:27
ToxString::size
size_t size() const
Get the number of bytes in the string.
Definition: toxstring.cpp:75
ToxString::ToxString
ToxString(const QString &text)
Creates a ToxString from a QString.
Definition: toxstring.cpp:37
ToxString::data
const uint8_t * data() const
Returns a pointer to the beginning of the string data.
Definition: toxstring.cpp:66