Skip to content

Commit

Permalink
Extract mutable_CFString_new
Browse files Browse the repository at this point in the history
From duplicate code in `rb_CFString_class_initialize_before_fork` and
`rb_str_append_normalized_ospath`.
  • Loading branch information
nobu committed Sep 26, 2024
1 parent 71b253c commit 2a65f4c
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ rb_str_encode_ospath(VALUE path)
# define NORMALIZE_UTF8PATH 1

# ifdef HAVE_WORKING_FORK
static CFMutableStringRef
mutable_CFString_new(CFStringRef *s, const char *ptr, long len)
{
const CFAllocatorRef alloc = kCFAllocatorDefault;
*s = CFStringCreateWithBytesNoCopy(alloc, (const UInt8 *)ptr, len,
kCFStringEncodingUTF8, FALSE,
kCFAllocatorNull);
return CFStringCreateMutableCopy(alloc, len, *s);
}

# define mutable_CFString_release(m, s) (CFRelease(m), CFRelease(s))

static void
rb_CFString_class_initialize_before_fork(void)
{
Expand All @@ -297,15 +309,9 @@ rb_CFString_class_initialize_before_fork(void)
/* Enough small but non-empty ASCII string to fit in NSTaggedPointerString. */
const char small_str[] = "/";
long len = sizeof(small_str) - 1;

const CFAllocatorRef alloc = kCFAllocatorDefault;
CFStringRef s = CFStringCreateWithBytesNoCopy(alloc,
(const UInt8 *)small_str,
len, kCFStringEncodingUTF8,
FALSE, kCFAllocatorNull);
CFMutableStringRef m = CFStringCreateMutableCopy(alloc, len, s);
CFRelease(m);
CFRelease(s);
CFStringRef s;
CFMutableStringRef m = mutable_CFString_new(&s, small_str, len);
mutable_CFString_release(m, s);
}
# endif /* HAVE_WORKING_FORK */

Expand All @@ -314,11 +320,8 @@ rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len)
{
CFIndex buflen = 0;
CFRange all;
CFStringRef s = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault,
(const UInt8 *)ptr, len,
kCFStringEncodingUTF8, FALSE,
kCFAllocatorNull);
CFMutableStringRef m = CFStringCreateMutableCopy(kCFAllocatorDefault, len, s);
CFStringRef s;
CFMutableStringRef m = mutable_CFString_new(&s, ptr, len);
long oldlen = RSTRING_LEN(str);

CFStringNormalize(m, kCFStringNormalizationFormC);
Expand All @@ -328,8 +331,7 @@ rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len)
CFStringGetBytes(m, all, kCFStringEncodingUTF8, '?', FALSE,
(UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
rb_str_set_len(str, oldlen + buflen);
CFRelease(m);
CFRelease(s);
mutable_CFString_release(m, s);
return str;
}

Expand Down

0 comments on commit 2a65f4c

Please sign in to comment.