Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / base / MacStringHelpers.mm
blobcf0b03665d195d52388864de1937258c8f4a5e4d
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"
11 #include <limits>
13 namespace mozilla {
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;
21   }
23   if (!aTo.SetLength(len, mozilla::fallible)) {
24     return NS_ERROR_OUT_OF_MEMORY;
25   }
27   [aFrom getCharacters:reinterpret_cast<unichar*>(aTo.BeginWriting())
28                  range:NSMakeRange(0, len)];
30   return NS_OK;
32   NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
35 }  // namespace mozilla