Debian Codegraph @

drivers/apple/foundation_helpers.mm

raw
1 /**************************************************************************/
2 /* foundation_helpers.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "foundation_helpers.h"
32
33 #import "core/string/ustring.h"
34
35 #import <CoreFoundation/CFString.h>
36
37 namespace conv {
38
39 NSString *to_nsstring(const String &p_str) {
40 return [[NSString alloc] initWithBytes:(const void *)p_str.ptr()
41 length:p_str.length() * sizeof(char32_t)
42 encoding:NSUTF32LittleEndianStringEncoding];
43 }
44
45 NSString *to_nsstring(const CharString &p_str) {
46 return [[NSString alloc] initWithBytes:(const void *)p_str.ptr()
47 length:p_str.length()
48 encoding:NSUTF8StringEncoding];
49 }
50
51 String to_string(NSString *p_str) {
52 CFStringRef str = (__bridge CFStringRef)p_str;
53 CFStringEncoding fastest = CFStringGetFastestEncoding(str);
54 // Sometimes, CFString will return a pointer to it's encoded data,
55 // so we can create the string without allocating intermediate buffers.
56 const char *p = CFStringGetCStringPtr(str, fastest);
57 if (p) {
58 switch (fastest) {
59 case kCFStringEncodingASCII:
60 return String::ascii(Span(p, CFStringGetLength(str)));
61 case kCFStringEncodingUTF8:
62 return String::utf8(p);
63 case kCFStringEncodingUTF32LE:
64 return String::utf32(Span((char32_t *)p, CFStringGetLength(str)));
65 default:
66 break;
67 }
68 }
69
70 CFRange range = CFRangeMake(0, CFStringGetLength(str));
71 CFIndex byte_len = 0;
72 // Try to losslessly convert the string directly into a String's buffer to avoid intermediate allocations.
73 CFIndex n = CFStringGetBytes(str, range, kCFStringEncodingUTF32LE, 0, NO, nil, 0, &byte_len);
74 if (n == range.length) {
75 String res;
76 res.resize_uninitialized((byte_len / sizeof(char32_t)) + 1);
77 res[n] = 0;
78 n = CFStringGetBytes(str, range, kCFStringEncodingUTF32LE, 0, NO, (UInt8 *)res.ptrw(), res.length() * sizeof(char32_t), nil);
79 return res;
80 }
81
82 return String::utf8(p_str.UTF8String);
83 }
84
85 } //namespace conv