Skip to content

Commit

Permalink
Base: Added toStdString and fromStdString, to convert std::string to/…
Browse files Browse the repository at this point in the history
…from QString,

assuming std::string is utf-8 encoded.
  • Loading branch information
eivindkv authored and wwmayer committed Sep 21, 2015
1 parent 61cbad8 commit 4d1ebec
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Base/Tools.h
Expand Up @@ -30,6 +30,7 @@
#include <iostream>
#include <vector>
#include <string>
#include <QString>

namespace Base
{
Expand Down Expand Up @@ -154,8 +155,23 @@ struct BaseExport Tools
static std::wstring widen(const std::string& str);
static std::string narrow(const std::wstring& str);
static std::string escapedUnicodeFromUtf8(const char *s);

/**
* @brief toStdString Convert a QString into a UTF-8 encoded std::string.
* @param s String to convert.
* @return A std::string encoded as UTF-8.
*/
static inline std::string toStdString(const QString& s) { QByteArray tmp = s.toUtf8(); return std::string(tmp.constData(), tmp.size()); }

/**
* @brief fromStdString Convert a std::string encoded as UTF-8 into a QString.
* @param s std::string, expected to be UTF-8 encoded.
* @return String represented as a QString.
*/
static inline QString fromStdString(const std::string & s) { return QString::fromUtf8(s.c_str(), s.size()); }
};


} // namespace Base

#endif // BASE_TOOLS_H

0 comments on commit 4d1ebec

Please sign in to comment.