2 * \file qstring_helper.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
10 * A collection of unicode conversion functions, using iconv.
15 #include "support/qstring_helpers.h"
17 #include "support/debug.h"
18 #include "support/docstring.h"
19 #include "support/qstring_helpers.h"
28 LyXErr & operator<<(LyXErr & err, QString const & str)
30 return err << fromqstr(str);
34 QString toqstr(char const * str)
36 return QString::fromUtf8(str);
39 QString toqstr(std::string const & str)
41 return toqstr(str.c_str());
45 QString toqstr(docstring const & ucs4)
47 // If possible we let qt do the work, since this version does not
48 // need to be superfast.
51 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
52 return QString::fromStdU32String(reinterpret_cast<std::u32string const &>(ucs4));
54 return QString::fromUcs4((uint const *)ucs4.data(), ucs4.length());
58 QString toqstr(char_type ucs4)
60 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
61 return QString::fromStdU32String(std::u32string(1, ucs4));
63 union { char_type c; uint i; } u = { ucs4 };
64 return QString::fromUcs4(&u.i, 1);
68 docstring qstring_to_ucs4(QString const & qstr)
72 QVector<uint> const ucs4 = qstr.toUcs4();
73 return docstring((char_type const *)(ucs4.constData()), ucs4.size());
76 std::string fromqstr(QString const & str)
78 return str.isEmpty() ? std::string() : std::string(str.toUtf8());
81 QString charFilterRegExp(QString const & filter)
84 for (QChar const & c : filter) {
86 re += "["+ QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
88 re += QRegExp::escape(c);
93 QString charFilterRegExpC(QString const & filter)
96 for (QChar const & c : filter) {
98 re += "["+ QRegExp::escape(c) + QRegExp::escape(c.toUpper()) + "]";
100 re += QRegExp::escape(c);
105 QString locLengthString(QString const & str)
109 return res.replace(QString("."), loc.decimalPoint());
113 docstring locLengthDocString(docstring const str)
115 return qstring_to_ucs4(locLengthString(toqstr(str)));
119 QString unlocLengthString(QString const & str)
123 return res.replace(loc.decimalPoint(), QString("."));