foundation_helpers.mm 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #import "foundation_helpers.h"
  31. #import "core/string/ustring.h"
  32. #import <CoreFoundation/CFString.h>
  33. namespace conv {
  34. NSString *to_nsstring(const String &p_str) {
  35. return [[NSString alloc] initWithBytes:(const void *)p_str.ptr()
  36. length:p_str.length() * sizeof(char32_t)
  37. encoding:NSUTF32LittleEndianStringEncoding];
  38. }
  39. NSString *to_nsstring(const CharString &p_str) {
  40. return [[NSString alloc] initWithBytes:(const void *)p_str.ptr()
  41. length:p_str.length()
  42. encoding:NSUTF8StringEncoding];
  43. }
  44. String to_string(NSString *p_str) {
  45. CFStringRef str = (__bridge CFStringRef)p_str;
  46. CFStringEncoding fastest = CFStringGetFastestEncoding(str);
  47. // Sometimes, CFString will return a pointer to it's encoded data,
  48. // so we can create the string without allocating intermediate buffers.
  49. const char *p = CFStringGetCStringPtr(str, fastest);
  50. if (p) {
  51. switch (fastest) {
  52. case kCFStringEncodingASCII:
  53. return String::ascii(Span(p, CFStringGetLength(str)));
  54. case kCFStringEncodingUTF8:
  55. return String::utf8(p);
  56. case kCFStringEncodingUTF32LE:
  57. return String::utf32(Span((char32_t *)p, CFStringGetLength(str)));
  58. default:
  59. break;
  60. }
  61. }
  62. CFRange range = CFRangeMake(0, CFStringGetLength(str));
  63. CFIndex byte_len = 0;
  64. // Try to losslessly convert the string directly into a String's buffer to avoid intermediate allocations.
  65. CFIndex n = CFStringGetBytes(str, range, kCFStringEncodingUTF32LE, 0, NO, nil, 0, &byte_len);
  66. if (n == range.length) {
  67. String res;
  68. res.resize_uninitialized((byte_len / sizeof(char32_t)) + 1);
  69. res[n] = 0;
  70. n = CFStringGetBytes(str, range, kCFStringEncodingUTF32LE, 0, NO, (UInt8 *)res.ptrw(), res.length() * sizeof(char32_t), nil);
  71. return res;
  72. }
  73. return String::utf8(p_str.UTF8String);
  74. }
  75. } //namespace conv