Bug 1448772 - Avoid back-and-forth UTF-16 to UTF-8 to UTF-16 conversions in xpcom/base/MacHelpers.mm. r=mstange

MozReview-Commit-ID: LQ4ZMJzy5WI

--HG--
extra : rebase_source : 85320f3e0bcef7711517ecc1e5b7bc5ec074bda5
This commit is contained in:
Henri Sivonen 2018-03-26 10:49:02 +03:00
Родитель 482fb13afc
Коммит 8402e1f7a2
5 изменённых файлов: 67 добавлений и 27 удалений

Просмотреть файл

@ -7,6 +7,8 @@
#include "mac_utils.h"
#include "nsXPCOM.h"
#include "mozilla/MacStringHelpers.h"
#include "mozilla/Unused.h"
void GetObjCExceptionInfo(void* inException, nsACString& outString)
{
@ -14,28 +16,15 @@ void GetObjCExceptionInfo(void* inException, nsACString& outString)
NSString* name = [e name];
NSString* reason = [e reason];
unsigned int nameLength = [name length];
unsigned int reasonLength = [reason length];
unichar* nameBuffer = (unichar*)moz_xmalloc(sizeof(unichar) * (nameLength + 1));
if (!nameBuffer)
return;
unichar* reasonBuffer = (unichar*)moz_xmalloc(sizeof(unichar) * (reasonLength + 1));
if (!reasonBuffer) {
free(nameBuffer);
return;
}
nsAutoString nameStr;
nsAutoString reasonStr;
[name getCharacters:nameBuffer];
[reason getCharacters:reasonBuffer];
nameBuffer[nameLength] = '\0';
reasonBuffer[reasonLength] = '\0';
mozilla::Unused << mozilla::CopyCocoaStringToXPCOMString(name, nameStr);
mozilla::Unused << mozilla::CopyCocoaStringToXPCOMString(reason, reasonStr);
outString.AssignLiteral("\nObj-C Exception data:\n");
AppendUTF16toUTF8(reinterpret_cast<const char16_t*>(nameBuffer), outString);
AppendUTF16toUTF8(nameStr, outString);
outString.AppendLiteral(": ");
AppendUTF16toUTF8(reinterpret_cast<const char16_t*>(reasonBuffer), outString);
free(nameBuffer);
free(reasonBuffer);
AppendUTF16toUTF8(reasonStr, outString);
}

Просмотреть файл

@ -6,6 +6,7 @@
#include "nsString.h"
#include "MacHelpers.h"
#include "MacStringHelpers.h"
#include "nsObjCExceptions.h"
#import <Foundation/Foundation.h>
@ -25,14 +26,7 @@ GetSelectedCityInfo(nsAString& aCountryCode)
return NS_ERROR_FAILURE;
}
const char* countryCodeUTF8 = [(NSString*)countryCode UTF8String];
if (!countryCodeUTF8) {
return NS_ERROR_FAILURE;
}
AppendUTF8toUTF16(countryCodeUTF8, aCountryCode);
return NS_OK;
return mozilla::CopyCocoaStringToXPCOMString((NSString*)countryCode, aCountryCode);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}

Просмотреть файл

@ -0,0 +1,20 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_MacStringHelpers_h
#define mozilla_MacStringHelpers_h
#include "nsString.h"
#import <Foundation/Foundation.h>
namespace mozilla {
nsresult CopyCocoaStringToXPCOMString(NSString* aFrom, nsAString& aTo);
} // namespace Mozilla
#endif

Просмотреть файл

@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "MacStringHelpers.h"
#include "nsObjCExceptions.h"
#include "mozilla/IntegerTypeTraits.h"
namespace mozilla {
nsresult
CopyCocoaStringToXPCOMString(NSString* aFrom, nsAString& aTo)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NSUInteger len = [aFrom length];
if (len > MaxValue<nsAString::size_type>::value) {
return NS_ERROR_OUT_OF_MEMORY;
}
if (!aTo.SetLength(len, mozilla::fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
[aFrom getCharacters:reinterpret_cast<unichar*>(aTo.BeginWriting()) range:NSMakeRange(0, len)];
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
} // namespace Mozilla

Просмотреть файл

@ -34,9 +34,11 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
]
EXPORTS.mozilla += [
'MacHelpers.h',
'MacStringHelpers.h',
]
UNIFIED_SOURCES += [
'MacHelpers.mm',
'MacStringHelpers.mm',
]
XPIDL_MODULE = 'xpcom_base'