diff options
Diffstat (limited to 'xpcom/base/MacStringHelpers.mm')
-rw-r--r-- | xpcom/base/MacStringHelpers.mm | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/xpcom/base/MacStringHelpers.mm b/xpcom/base/MacStringHelpers.mm index cf0b03665d..30b447548a 100644 --- a/xpcom/base/MacStringHelpers.mm +++ b/xpcom/base/MacStringHelpers.mm @@ -12,24 +12,42 @@ namespace mozilla { -nsresult CopyCocoaStringToXPCOMString(NSString* aFrom, nsAString& aTo) { - NS_OBJC_BEGIN_TRY_BLOCK_RETURN; +void CopyNSStringToXPCOMString(const NSString* aFrom, nsAString& aTo) { + NS_OBJC_BEGIN_TRY_IGNORE_BLOCK; - NSUInteger len = [aFrom length]; - if (len > std::numeric_limits<nsAString::size_type>::max()) { - return NS_ERROR_OUT_OF_MEMORY; + if (!aFrom) { + aTo.Truncate(); + return; } - if (!aTo.SetLength(len, mozilla::fallible)) { - return NS_ERROR_OUT_OF_MEMORY; + NSUInteger len = [aFrom length]; + if (len > std::numeric_limits<nsAString::size_type>::max()) { + aTo.AllocFailed(std::numeric_limits<nsAString::size_type>::max()); } + aTo.SetLength(len); [aFrom getCharacters:reinterpret_cast<unichar*>(aTo.BeginWriting()) range:NSMakeRange(0, len)]; - return NS_OK; + NS_OBJC_END_TRY_IGNORE_BLOCK; +} - NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE); +NSString* XPCOMStringToNSString(const nsAString& aFrom) { + if (aFrom.IsEmpty()) { + return [NSString string]; + } + return [NSString stringWithCharacters:reinterpret_cast<const unichar*>( + aFrom.BeginReading()) + length:aFrom.Length()]; +} + +NSString* XPCOMStringToNSString(const nsACString& aFrom) { + if (aFrom.IsEmpty()) { + return [NSString string]; + } + return [[[NSString alloc] initWithBytes:aFrom.BeginReading() + length:aFrom.Length() + encoding:NSUTF8StringEncoding] autorelease]; } } // namespace mozilla |