QStringView: add toNSString() / toCFString()

Use it in QString and QCocoaMenuItem.

As a drive-by, s,OS X,\macos,g.

[ChangeLog][QtCore][QStringView] Added toNSString(), toCFString()
on macOS.

Change-Id: Ib05818015a4be11a0d72d4487fb82c580d27854e
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Marc Mutz 2020-05-06 15:02:04 +02:00
parent dd4e96df52
commit 93bc6be21d
3 changed files with 42 additions and 7 deletions

View File

@ -197,7 +197,7 @@ NSData *QByteArray::toRawNSData() const
Constructs a new QString containing a copy of the \a string CFString.
\note this function is only available on OS X and iOS.
\note this function is only available on \macos and iOS.
*/
QString QString::fromCFString(CFStringRef string)
{
@ -222,11 +222,24 @@ QString QString::fromCFString(CFStringRef string)
Creates a CFString from a QString. The caller owns the CFString and is
responsible for releasing it.
\note this function is only available on OS X and iOS.
\note this function is only available on \macos and iOS.
*/
CFStringRef QString::toCFString() const
{
return CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(unicode()), length());
return QStringView{*this}.toCFString();
}
/*!
\since 6.0
Creates a CFString from this QStringView. The caller owns the CFString and is
responsible for releasing it.
\note this function is only available on \macos and iOS.
*/
CFStringRef QStringView::toCFString() const
{
return CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(data()), size());
}
/*!
@ -234,7 +247,7 @@ CFStringRef QString::toCFString() const
Constructs a new QString containing a copy of the \a string NSString.
\note this function is only available on OS X and iOS.
\note this function is only available on \macos and iOS.
*/
QString QString::fromNSString(const NSString *string)
{
@ -251,11 +264,23 @@ QString QString::fromNSString(const NSString *string)
Creates a NSString from a QString. The NSString is autoreleased.
\note this function is only available on OS X and iOS.
\note this function is only available on \macos and iOS.
*/
NSString *QString::toNSString() const
{
return [NSString stringWithCharacters: reinterpret_cast<const UniChar*>(unicode()) length: length()];
return QStringView{*this}.toNSString();
}
/*!
\since 6.0
Creates an NSString from this QStringView. The NSString is autoreleased.
\note this function is only available on \macos and iOS.
*/
NSString *QStringView::toNSString() const
{
return [NSString stringWithCharacters:reinterpret_cast<const UniChar*>(data()) length:size()];
}
// ----------------------------------------------------------------------------

View File

@ -51,6 +51,11 @@
#include <string>
#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
Q_FORWARD_DECLARE_CF_TYPE(CFString);
Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
#endif
QT_BEGIN_NAMESPACE
class QString;
@ -215,6 +220,11 @@ public:
: QStringView(str.data(), qsizetype(str.size())) {}
Q_REQUIRED_RESULT inline QString toString() const; // defined in qstring.h
#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
// defined in qcore_foundation.mm
Q_REQUIRED_RESULT Q_CORE_EXPORT CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED;
Q_REQUIRED_RESULT Q_CORE_EXPORT NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED;
#endif
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR qsizetype size() const noexcept { return m_size; }
Q_REQUIRED_RESULT const_pointer data() const noexcept { return reinterpret_cast<const_pointer>(m_data); }

View File

@ -103,7 +103,7 @@ NSString *keySequenceToKeyEqivalent(const QKeySequence &accel)
// Similar to qt_mac_removePrivateUnicode change the delete key so the symbol is correctly seen in native menubar
if (cocoa_key.unicode() == NSDeleteFunctionKey)
cocoa_key = NSDeleteCharacter;
return [NSString stringWithCharacters:&cocoa_key.unicode() length:1];
return QStringView{&cocoa_key, 1}.toNSString();
}
// return the cocoa modifier mask for the QKeySequence (currently only looks at the first one).