From 0b2ec56ae3af9f794910d5adebddd7a27e21b723 Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Sun, 1 Oct 2017 10:44:35 -0700 Subject: [PATCH] Simplify wxMacConvertNewlines helper functions One is not used at all, two are only used in one place. Make the remaining two return a wxString instead of taking a wxString pointer parameter. --- include/wx/osx/core/cfstring.h | 8 ++----- src/osx/carbon/dataobj.cpp | 8 +++++-- src/osx/cocoa/textctrl.mm | 15 ++++++------ src/osx/core/cfstring.cpp | 43 +++++----------------------------- src/osx/iphone/textctrl.mm | 16 ++++++------- 5 files changed, 29 insertions(+), 61 deletions(-) diff --git a/include/wx/osx/core/cfstring.h b/include/wx/osx/core/cfstring.h index 372a3c7978..b3e814ac5d 100644 --- a/include/wx/osx/core/cfstring.h +++ b/include/wx/osx/core/cfstring.h @@ -26,12 +26,8 @@ class WXDLLIMPEXP_FWD_BASE wxString; -WXDLLIMPEXP_BASE void wxMacConvertNewlines13To10( wxString *data ) ; -WXDLLIMPEXP_BASE void wxMacConvertNewlines10To13( wxString *data ) ; - -WXDLLIMPEXP_BASE void wxMacConvertNewlines13To10( char * data ) ; -WXDLLIMPEXP_BASE void wxMacConvertNewlines13To10( wxChar16 * data ) ; -WXDLLIMPEXP_BASE void wxMacConvertNewlines10To13( char * data ) ; +WXDLLIMPEXP_BASE wxString wxMacConvertNewlines13To10(const wxString& data); +WXDLLIMPEXP_BASE wxString wxMacConvertNewlines10To13(const wxString& data); WXDLLIMPEXP_BASE wxUint32 wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding) ; WXDLLIMPEXP_BASE wxFontEncoding wxMacGetFontEncFromSystemEnc(wxUint32 encoding) ; diff --git a/src/osx/carbon/dataobj.cpp b/src/osx/carbon/dataobj.cpp index eac1fb9106..059d766350 100644 --- a/src/osx/carbon/dataobj.cpp +++ b/src/osx/carbon/dataobj.cpp @@ -489,11 +489,15 @@ bool wxDataObject::GetFromPasteboard( void * pb ) if (dataFormat.GetType() == wxDF_TEXT) { - wxMacConvertNewlines13To10((char*) buf); + for (char* p = static_cast(buf); *p; p++) + if (*p == '\r') + *p = '\n'; } else if (dataFormat.GetType() == wxDF_UNICODETEXT) { - wxMacConvertNewlines13To10((wxChar16*) buf); + for (wxChar16* p = static_cast(buf); *p; p++) + if (*p == '\r') + *p = '\n'; } SetData( flavorFormat, flavorDataSize, buf ); transferred = true; diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index ca7641d9c3..590815b108 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -788,22 +788,22 @@ void wxNSTextViewControl::insertText(NSString* str, WXWidget slf, void *_cmd) wxString wxNSTextViewControl::GetStringValue() const { + wxString result; if (m_textView) { - wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding()); - wxMacConvertNewlines13To10( &result ) ; - return result; + result = wxMacConvertNewlines13To10( + wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding())); } - return wxEmptyString; + return result; } + void wxNSTextViewControl::SetStringValue( const wxString &str) { - wxString st = str; - wxMacConvertNewlines10To13( &st ); wxMacEditHelper helper(m_textView); if (m_textView) { + wxString st(wxMacConvertNewlines10To13(str)); [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; if ( m_wxPeer->HasFlag(wxTE_AUTO_URL) ) { @@ -984,8 +984,7 @@ void wxNSTextViewControl::ShowPosition(long pos) void wxNSTextViewControl::WriteText(const wxString& str) { - wxString st = str; - wxMacConvertNewlines10To13( &st ); + wxString st(wxMacConvertNewlines10To13(str)); wxMacEditHelper helper(m_textView); NSEvent* formerEvent = m_lastKeyDownEvent; m_lastKeyDownEvent = nil; diff --git a/src/osx/core/cfstring.cpp b/src/osx/core/cfstring.cpp index 68c43592e3..e11d286c31 100644 --- a/src/osx/core/cfstring.cpp +++ b/src/osx/core/cfstring.cpp @@ -24,46 +24,17 @@ #include -void wxMacConvertNewlines13To10( char * data ) -{ - char * buf = data ; - while( (buf=strchr(buf,0x0d)) != NULL ) - { - *buf = 0x0a ; - buf++ ; - } -} - -void wxMacConvertNewlines13To10( wxChar16 * data ) -{ - for ( ; *data; ++data ) - { - if ( *data == 0x0d ) - *data = 0x0a; - } -} - -void wxMacConvertNewlines10To13( char * data ) -{ - char * buf = data ; - while( (buf=strchr(buf,0x0a)) != NULL ) - { - *buf = 0x0d ; - buf++ ; - } -} - const wxString sCR((wxChar)13); const wxString sLF((wxChar)10); -void wxMacConvertNewlines13To10( wxString * data ) +wxString wxMacConvertNewlines13To10(const wxString& data) { - data->Replace( sCR,sLF); + return wxString(data)->Replace(sCR, sLF); } -void wxMacConvertNewlines10To13( wxString * data ) +wxString wxMacConvertNewlines10To13(const wxString& data) { - data->Replace( sLF,sCR); + return wxString(data)->Replace(sLF, sCR); } wxUint32 wxMacGetSystemEncFromFontEnc(wxFontEncoding encoding) @@ -614,8 +585,7 @@ wxCFStringRef::wxCFStringRef( const wxString &st , wxFontEncoding WXUNUSED_IN_UN } else { - wxString str = st ; - wxMacConvertNewlines13To10( &str ) ; + wxString str(wxMacConvertNewlines13To10(st)); #if wxUSE_UNICODE #if wxUSE_UNICODE_WCHAR // native = wchar_t 4 bytes for us @@ -700,8 +670,7 @@ wxString wxCFStringRef::AsString( CFStringRef ref, wxFontEncoding WXUNUSED_IN_UN #endif delete[] buf ; - wxMacConvertNewlines10To13( &result); - return result ; + return wxMacConvertNewlines10To13(result); } wxString wxCFStringRef::AsString(wxFontEncoding encoding) const diff --git a/src/osx/iphone/textctrl.mm b/src/osx/iphone/textctrl.mm index 989a4d73d7..c3cfe7d596 100644 --- a/src/osx/iphone/textctrl.mm +++ b/src/osx/iphone/textctrl.mm @@ -336,23 +336,24 @@ bool wxUITextViewControl::CanFocus() const wxString wxUITextViewControl::GetStringValue() const { + wxString result; if (m_textView) { - wxString result = wxCFStringRef::AsString([m_textView text], m_wxPeer->GetFont().GetEncoding()); - wxMacConvertNewlines13To10( &result ) ; - return result; + result = wxMacConvertNewlines13To10( + wxCFStringRef::AsString([m_textView text], m_wxPeer->GetFont().GetEncoding())); } - return wxEmptyString; + return result; } void wxUITextViewControl::SetStringValue( const wxString &str) { - wxString st = str; - wxMacConvertNewlines10To13( &st ); wxMacEditHelper helper(m_textView); if (m_textView) + { + wxString st(wxMacConvertNewlines10To13(str)); [m_textView setText: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; + } } void wxUITextViewControl::Copy() @@ -419,8 +420,7 @@ void wxUITextViewControl::SetSelection( long from , long to ) void wxUITextViewControl::WriteText(const wxString& str) { - wxString st = str; - wxMacConvertNewlines10To13( &st ); + wxString st(wxMacConvertNewlines10To13(str)); wxMacEditHelper helper(m_textView); wxCFStringRef insert( st , m_wxPeer->GetFont().GetEncoding() );