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 nsresult CopyCocoaStringToXPCOMString(NSString* aFrom, nsAString& aTo) {
16 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
18 NSUInteger len = [aFrom length];
19 if (len > std::numeric_limits<nsAString::size_type>::max()) {
20 return NS_ERROR_OUT_OF_MEMORY;
23 if (!aTo.SetLength(len, mozilla::fallible)) {
24 return NS_ERROR_OUT_OF_MEMORY;
27 [aFrom getCharacters:reinterpret_cast<unichar*>(aTo.BeginWriting())
28 range:NSMakeRange(0, len)];
32 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
35 } // namespace mozilla