1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "MacStringHelpers.h"
8 #include "nsObjCExceptions.h"
10 #include "mozilla/IntegerTypeTraits.h"
15 void CopyNSStringToXPCOMString(const NSString* aFrom, nsAString& aTo) {
16 NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
23 NSUInteger len = [aFrom length];
24 if (len > std::numeric_limits<nsAString::size_type>::max()) {
25 aTo.AllocFailed(std::numeric_limits<nsAString::size_type>::max());
29 [aFrom getCharacters:reinterpret_cast<unichar*>(aTo.BeginWriting())
30 range:NSMakeRange(0, len)];
32 NS_OBJC_END_TRY_IGNORE_BLOCK;
35 NSString* XPCOMStringToNSString(const nsAString& aFrom) {
36 if (aFrom.IsEmpty()) {
37 return [NSString string];
39 return [NSString stringWithCharacters:reinterpret_cast<const unichar*>(
41 length:aFrom.Length()];
44 NSString* XPCOMStringToNSString(const nsACString& aFrom) {
45 if (aFrom.IsEmpty()) {
46 return [NSString string];
48 return [[[NSString alloc] initWithBytes:aFrom.BeginReading()
50 encoding:NSUTF8StringEncoding] autorelease];
53 } // namespace mozilla