2016-04-16 17:51:25 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-04-16 17:51:25 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-04-16 17:51:25 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <QtCore/QString>
|
|
|
|
#include <QtCore/QByteArray>
|
|
|
|
|
|
|
|
namespace codegen {
|
|
|
|
namespace common {
|
|
|
|
|
|
|
|
// This is a simple wrapper around (const char*, size).
|
|
|
|
// Not null-terminated! It does not hold any ownership.
|
|
|
|
class ConstUtf8String {
|
|
|
|
public:
|
2016-04-18 20:33:43 +00:00
|
|
|
explicit ConstUtf8String(const char *string, int size = -1) : string_(string) {
|
2016-04-16 17:51:25 +00:00
|
|
|
if (size < 0) {
|
|
|
|
size = strlen(string);
|
|
|
|
}
|
|
|
|
size_ = size;
|
|
|
|
}
|
|
|
|
ConstUtf8String(const char *string, const char *end) : ConstUtf8String(string, end - string) {
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray toByteArray() const {
|
|
|
|
return QByteArray(string_, size_);
|
|
|
|
}
|
|
|
|
std::string toStdString() const {
|
|
|
|
return std::string(string_, size_);
|
|
|
|
}
|
|
|
|
QString toStringUnchecked() const {
|
|
|
|
return QString::fromUtf8(string_, size_);
|
|
|
|
}
|
|
|
|
bool empty() const {
|
|
|
|
return size_ == 0;
|
|
|
|
}
|
|
|
|
const char *data() const {
|
|
|
|
return string_;
|
|
|
|
}
|
|
|
|
int size() const {
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
const char *end() const {
|
|
|
|
return data() + size();
|
|
|
|
}
|
|
|
|
ConstUtf8String mid(int pos, int size = -1) {
|
|
|
|
return ConstUtf8String(string_ + pos, std::max(std::min(size, size_ - pos), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char *string_;
|
|
|
|
int size_;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace common
|
|
|
|
} // namespace codegen
|