From b4382bad621962f301d14519601a62c88ce45eff Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 17 Apr 2013 15:17:36 +0100 Subject: [PATCH] Added String methods to create/return a std::string, and also String::toRawUTF8 method. --- .../Source/Utility/jucer_CodeHelpers.cpp | 2 +- .../codecs/juce_OggVorbisAudioFormat.cpp | 2 +- .../AAX/juce_AAX_Wrapper.cpp | 2 +- .../RTAS/juce_RTAS_Wrapper.cpp | 2 +- .../format_types/juce_VSTPluginFormat.cpp | 10 ++++---- modules/juce_core/maths/juce_MathsFunctions.h | 4 +--- modules/juce_core/native/juce_mac_Files.mm | 2 +- .../juce_core/native/juce_mac_SystemStats.mm | 2 +- .../juce_core/native/juce_posix_SharedCode.h | 2 +- .../juce_core/native/juce_win32_Network.cpp | 8 +++---- modules/juce_core/network/juce_URL.cpp | 4 ++-- modules/juce_core/text/juce_String.cpp | 13 +++++++++++ modules/juce_core/text/juce_String.h | 23 +++++++++++++++++-- .../native/juce_linux_Windowing.cpp | 4 ++-- 14 files changed, 55 insertions(+), 25 deletions(-) diff --git a/extras/Introjucer/Source/Utility/jucer_CodeHelpers.cpp b/extras/Introjucer/Source/Utility/jucer_CodeHelpers.cpp index 2c4edf2e45..2a7542e0bc 100644 --- a/extras/Introjucer/Source/Utility/jucer_CodeHelpers.cpp +++ b/extras/Introjucer/Source/Utility/jucer_CodeHelpers.cpp @@ -180,7 +180,7 @@ namespace CodeHelpers String addEscapeChars (const String& s) { MemoryOutputStream out; - writeEscapeChars (out, s.toUTF8().getAddress(), -1, -1, false, true, true); + writeEscapeChars (out, s.toRawUTF8(), -1, -1, false, true, true); return out.toUTF8(); } diff --git a/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp b/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp index d725420006..a9a2f1bbad 100644 --- a/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp +++ b/modules/juce_audio_formats/codecs/juce_OggVorbisAudioFormat.cpp @@ -418,7 +418,7 @@ private: const String s (metadata [name]); if (s.isNotEmpty()) - vorbis_comment_add_tag (&vc, vorbisName, const_cast (s.toUTF8().getAddress())); + vorbis_comment_add_tag (&vc, vorbisName, const_cast (s.toRawUTF8())); } JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OggWriter) diff --git a/modules/juce_audio_plugin_client/AAX/juce_AAX_Wrapper.cpp b/modules/juce_audio_plugin_client/AAX/juce_AAX_Wrapper.cpp index 3d8df7aa77..fe28f0056b 100644 --- a/modules/juce_audio_plugin_client/AAX/juce_AAX_Wrapper.cpp +++ b/modules/juce_audio_plugin_client/AAX/juce_AAX_Wrapper.cpp @@ -688,7 +688,7 @@ struct AAXClasses { AAX_IParameter* parameter = new AAX_CParameter (IndexAsParamID (parameterIndex), - audioProcessor.getParameterName (parameterIndex).toUTF8().getAddress(), + audioProcessor.getParameterName (parameterIndex).toRawUTF8(), audioProcessor.getParameter (parameterIndex), AAX_CLinearTaperDelegate(), AAX_CNumberDisplayDelegate(), diff --git a/modules/juce_audio_plugin_client/RTAS/juce_RTAS_Wrapper.cpp b/modules/juce_audio_plugin_client/RTAS/juce_RTAS_Wrapper.cpp index 91b3364e06..e6184a3cb9 100644 --- a/modules/juce_audio_plugin_client/RTAS/juce_RTAS_Wrapper.cpp +++ b/modules/juce_audio_plugin_client/RTAS/juce_RTAS_Wrapper.cpp @@ -926,7 +926,7 @@ public: JucePlugin_RTASProductId, JucePlugin_RTASCategory); - type->DefineTypeNames (createRTASName().toUTF8().getAddress()); + type->DefineTypeNames (createRTASName().toRawUTF8()); type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k); type->DefineStemFormats (getFormatForChans (channelConfigs [i][0] != 0 ? channelConfigs [i][0] : channelConfigs [i][1]), diff --git a/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp b/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp index b34c8ab72a..63941b4593 100644 --- a/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp +++ b/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp @@ -33,7 +33,7 @@ #if JUCE_MAC static bool makeFSRefFromPath (FSRef* destFSRef, const String& path) { - return FSPathMakeRef (reinterpret_cast (path.toUTF8().getAddress()), destFSRef, 0) == noErr; + return FSPathMakeRef (reinterpret_cast (path.toRawUTF8()), destFSRef, 0) == noErr; } #endif @@ -493,7 +493,7 @@ public: if (file.hasFileExtension (".vst")) { - const char* const utf8 = file.getFullPathName().toUTF8().getAddress(); + const char* const utf8 = file.getFullPathName().toRawUTF8(); if (CFURLRef url = CFURLCreateFromFileSystemRepresentation (0, (const UInt8*) utf8, strlen (utf8), file.isDirectory())) @@ -556,7 +556,7 @@ public: { FSRef fn; - if (FSPathMakeRef ((UInt8*) file.getFullPathName().toUTF8().getAddress(), &fn, 0) == noErr) + if (FSPathMakeRef ((UInt8*) file.getFullPathName().toRawUTF8(), &fn, 0) == noErr) { resFileId = FSOpenResFile (&fn, fsRdPerm); @@ -1245,7 +1245,7 @@ public: if (index == getCurrentProgram()) { if (getNumPrograms() > 0 && newName != getCurrentProgramName()) - dispatch (effSetProgramName, 0, 0, (void*) newName.substring (0, 24).toUTF8().getAddress(), 0.0f); + dispatch (effSetProgramName, 0, 0, (void*) newName.substring (0, 24).toRawUTF8(), 0.0f); } else { @@ -1834,7 +1834,7 @@ private: #if JUCE_MAC return (VstIntPtr) (void*) &module->parentDirFSSpec; #else - return (VstIntPtr) (pointer_sized_uint) module->fullParentDirectoryPathName.toUTF8().getAddress(); + return (VstIntPtr) (pointer_sized_uint) module->fullParentDirectoryPathName.toRawUTF8(); #endif } diff --git a/modules/juce_core/maths/juce_MathsFunctions.h b/modules/juce_core/maths/juce_MathsFunctions.h index f5c0647633..858e6ced49 100644 --- a/modules/juce_core/maths/juce_MathsFunctions.h +++ b/modules/juce_core/maths/juce_MathsFunctions.h @@ -300,13 +300,11 @@ inline int64 abs64 (const int64 n) noexcept //============================================================================== /** A predefined value for Pi, at double-precision. - @see float_Pi */ const double double_Pi = 3.1415926535897932384626433832795; -/** A predefined value for Pi, at sngle-precision. - +/** A predefined value for Pi, at single-precision. @see double_Pi */ const float float_Pi = 3.14159265358979323846f; diff --git a/modules/juce_core/native/juce_mac_Files.mm b/modules/juce_core/native/juce_mac_Files.mm index d9030c633e..aa7f2b23be 100644 --- a/modules/juce_core/native/juce_mac_Files.mm +++ b/modules/juce_core/native/juce_mac_Files.mm @@ -91,7 +91,7 @@ namespace FileHelpers FSRef ref; LSItemInfoRecord info; - return FSPathMakeRefWithOptions ((const UInt8*) path.toUTF8().getAddress(), kFSPathMakeRefDoNotFollowLeafSymlink, &ref, 0) == noErr + return FSPathMakeRefWithOptions ((const UInt8*) path.toRawUTF8(), kFSPathMakeRefDoNotFollowLeafSymlink, &ref, 0) == noErr && LSCopyItemInfoForRef (&ref, kLSRequestBasicFlagsOnly, &info) == noErr && (info.flags & kLSItemInfoIsInvisible) != 0; #endif diff --git a/modules/juce_core/native/juce_mac_SystemStats.mm b/modules/juce_core/native/juce_mac_SystemStats.mm index d7a48e55f1..2f2012f43f 100644 --- a/modules/juce_core/native/juce_mac_SystemStats.mm +++ b/modules/juce_core/native/juce_mac_SystemStats.mm @@ -38,7 +38,7 @@ void Logger::outputDebugString (const String& text) { // Would prefer to use std::cerr here, but avoiding it for // the moment, due to clang JIT linkage problems. - fputs (text.toUTF8().getAddress(), stderr); + fputs (text.toRawUTF8(), stderr); fputs ("\n", stderr); fflush (stderr); } diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index f1301186af..5f05a27542 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -880,7 +880,7 @@ void Thread::setCurrentThreadName (const String& name) [[NSThread currentThread] setName: juceStringToNS (name)]; } #elif JUCE_LINUX - prctl (PR_SET_NAME, name.toUTF8().getAddress(), 0, 0, 0); + prctl (PR_SET_NAME, name.toRawUTF8(), 0, 0, 0); #endif } diff --git a/modules/juce_core/native/juce_win32_Network.cpp b/modules/juce_core/native/juce_win32_Network.cpp index f023b1b7a1..58fba604e9 100644 --- a/modules/juce_core/native/juce_win32_Network.cpp +++ b/modules/juce_core/native/juce_win32_Network.cpp @@ -408,15 +408,15 @@ bool Process::openEmailWithAttachments (const String& targetEmailAddress, return false; MapiMessage message = { 0 }; - message.lpszSubject = (LPSTR) emailSubject.toUTF8().getAddress(); - message.lpszNoteText = (LPSTR) bodyText.toUTF8().getAddress(); + message.lpszSubject = (LPSTR) emailSubject.toRawUTF8(); + message.lpszNoteText = (LPSTR) bodyText.toRawUTF8(); MapiRecipDesc recip = { 0 }; recip.ulRecipClass = MAPI_TO; String targetEmailAddress_ (targetEmailAddress); if (targetEmailAddress_.isEmpty()) targetEmailAddress_ = " "; // (Windows Mail can't deal with a blank address) - recip.lpszName = (LPSTR) targetEmailAddress_.toUTF8().getAddress(); + recip.lpszName = (LPSTR) targetEmailAddress_.toRawUTF8(); message.nRecipCount = 1; message.lpRecips = &recip; @@ -429,7 +429,7 @@ bool Process::openEmailWithAttachments (const String& targetEmailAddress, for (int i = 0; i < filesToAttach.size(); ++i) { files[i].nPosition = (ULONG) -1; - files[i].lpszPathName = (LPSTR) filesToAttach[i].toUTF8().getAddress(); + files[i].lpszPathName = (LPSTR) filesToAttach[i].toRawUTF8(); } return mapiSendMail (0, 0, &message, MAPI_DIALOG | MAPI_LOGON_UI, 0) == SUCCESS_SUCCESS; diff --git a/modules/juce_core/network/juce_URL.cpp b/modules/juce_core/network/juce_URL.cpp index 1efeefac6b..c674d26ecd 100644 --- a/modules/juce_core/network/juce_URL.cpp +++ b/modules/juce_core/network/juce_URL.cpp @@ -415,7 +415,7 @@ String URL::removeEscapeChars (const String& s) // We need to operate on the string as raw UTF8 chars, and then recombine them into unicode // after all the replacements have been made, so that multi-byte chars are handled. - Array utf8 (result.toUTF8().getAddress(), (int) result.getNumBytesAsUTF8()); + Array utf8 (result.toRawUTF8(), (int) result.getNumBytesAsUTF8()); for (int i = 0; i < utf8.size(); ++i) { @@ -440,7 +440,7 @@ String URL::addEscapeChars (const String& s, const bool isParameter) const CharPointer_UTF8 legalChars (isParameter ? "_-.*!'()" : ",$_-.*!'()"); - Array utf8 (s.toUTF8().getAddress(), (int) s.getNumBytesAsUTF8()); + Array utf8 (s.toRawUTF8(), (int) s.getNumBytesAsUTF8()); for (int i = 0; i < utf8.size(); ++i) { diff --git a/modules/juce_core/text/juce_String.cpp b/modules/juce_core/text/juce_String.cpp index 146b89a89e..ddb63da0ee 100644 --- a/modules/juce_core/text/juce_String.cpp +++ b/modules/juce_core/text/juce_String.cpp @@ -346,6 +346,8 @@ String::String (const CharPointer_UTF8& start, const CharPointer_UTF8& end) : String::String (const CharPointer_UTF16& start, const CharPointer_UTF16& end) : text (StringHolder::createFromCharPointer (start, end)) {} String::String (const CharPointer_UTF32& start, const CharPointer_UTF32& end) : text (StringHolder::createFromCharPointer (start, end)) {} +String::String (const std::string& s) : text (StringHolder::createFromFixedLength (s.data(), s.size())) {} + String String::charToString (const juce_wchar character) { String result (PreallocationBytes (CharPointerType::getBytesRequiredFor (character))); @@ -2011,11 +2013,21 @@ CharPointer_UTF8 String::toUTF8() const { return StringEncodingConverter ::convert (*this); } CharPointer_UTF32 String::toUTF32() const { return StringEncodingConverter ::convert (*this); } +const char* String::toRawUTF8() const +{ + return toUTF8().getAddress(); +} + const wchar_t* String::toWideCharPointer() const { return StringEncodingConverter ::convert (*this).getAddress(); } +std::string String::toStdString() const +{ + return std::string (toRawUTF8()); +} + //============================================================================== template struct StringCopier @@ -2251,6 +2263,7 @@ public: expect (s3.indexOf (L"HIJK") == -1); expect (s3.indexOfIgnoreCase ("hij") == 7); expect (s3.indexOfIgnoreCase (L"hijk") == -1); + expect (s3.toStdString() == s3.toRawUTF8()); String s4 (s3); s4.append (String ("xyz123"), 3); diff --git a/modules/juce_core/text/juce_String.h b/modules/juce_core/text/juce_String.h index 74fad7412e..a7cb85c894 100644 --- a/modules/juce_core/text/juce_String.h +++ b/modules/juce_core/text/juce_String.h @@ -152,6 +152,9 @@ public: /** Creates a string from an ASCII character string */ String (const CharPointer_ASCII& text); + /** Creates a string from a UTF-8 encoded std::string. */ + String (const std::string&); + //============================================================================== /** Creates a string from a single character. */ static String charToString (juce_wchar character); @@ -1045,10 +1048,23 @@ public: To find out how many bytes you need to store this string as UTF-8, you can call CharPointer_UTF8::getBytesRequiredFor (myString.getCharPointer()) - @see getCharPointer, toUTF16, toUTF32 + @see toRawUTF8, getCharPointer, toUTF16, toUTF32 */ CharPointer_UTF8 toUTF8() const; + /** Returns a pointer to a UTF-8 version of this string. + + Because it returns a reference to the string's internal data, the pointer + that is returned must not be stored anywhere, as it can be deleted whenever the + string changes. + + To find out how many bytes you need to store this string as UTF-8, you can call + CharPointer_UTF8::getBytesRequiredFor (myString.getCharPointer()) + + @see getCharPointer, toUTF8, toUTF16, toUTF32 + */ + const char* toRawUTF8() const; + /** Returns a pointer to a UTF-16 version of this string. Because it returns a reference to the string's internal data, the pointer @@ -1086,6 +1102,9 @@ public: */ const wchar_t* toWideCharPointer() const; + /** */ + std::string toStdString() const; + //============================================================================== /** Creates a String from a UTF-8 encoded buffer. If the size is < 0, it'll keep reading until it hits a zero. @@ -1310,7 +1329,7 @@ JUCE_API bool JUCE_CALLTYPE operator<= (const String& string1, const String& str template std::basic_ostream & JUCE_CALLTYPE operator<< (std::basic_ostream & stream, const String& stringToWrite) { - return stream << stringToWrite.toUTF8().getAddress(); + return stream << stringToWrite.toRawUTF8(); } /** This operator allows you to write a juce String directly to std output streams. diff --git a/modules/juce_gui_basics/native/juce_linux_Windowing.cpp b/modules/juce_gui_basics/native/juce_linux_Windowing.cpp index e785f846bb..15ce7ed255 100644 --- a/modules/juce_gui_basics/native/juce_linux_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_linux_Windowing.cpp @@ -883,7 +883,7 @@ public: void setTitle (const String& title) { XTextProperty nameProperty; - char* strings[] = { const_cast (title.toUTF8().getAddress()) }; + char* strings[] = { const_cast (title.toRawUTF8()) }; ScopedXLock xlock; if (XStringListToTextProperty (strings, 1, &nameProperty)) @@ -2527,7 +2527,7 @@ private: xchangeProperty (evt.xselectionrequest.requestor, evt.xselectionrequest.property, targetType, 8, - dragState.textOrFiles.toUTF8().getAddress(), + dragState.textOrFiles.toRawUTF8(), dragState.textOrFiles.getNumBytesAsUTF8()); }