MCPcopy Create free account
hub / github.com/couchbase/fleece / nsstring_slice

Method nsstring_slice

ObjC/slice+CoreFoundation.cc:93–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected