| 91 | |
| 92 | |
| 93 | nsstring_slice::nsstring_slice(CFStringRef str) |
| 94 | :_needsFree(false) |
| 95 | { |
| 96 | if (!str) |
| 97 | return; |
| 98 | // First try to use a direct pointer to the bytes: |
| 99 | auto cstr = CFStringGetCStringPtr(str, kCFStringEncodingUTF8); |
| 100 | if (cstr) { |
| 101 | set(cstr, strlen(cstr)); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | CFIndex lengthInChars = CFStringGetLength(str); |
| 106 | if (size_t(lengthInChars) <= sizeof(_local)) { |
| 107 | // Next try to copy the UTF-8 into a smallish stack-based buffer: |
| 108 | set(&_local, sizeof(_local)); |
| 109 | if (getBytes(str, lengthInChars)) |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | // Otherwise malloc a buffer to copy the UTF-8 into: |
| 114 | auto maxByteCount = CFStringGetMaximumSizeForEncoding(lengthInChars, kCFStringEncodingUTF8); |
| 115 | set(newBytes(maxByteCount), maxByteCount); |
| 116 | _needsFree = true; |
| 117 | if (!getBytes(str, lengthInChars)) |
| 118 | throw std::runtime_error("couldn't get NSString bytes"); |
| 119 | } |
| 120 | |
| 121 | nsstring_slice::~nsstring_slice() { |
| 122 | if (_needsFree) |
nothing calls this directly
no outgoing calls
no test coverage detected