Extract mutable_CFString_new
From duplicate code in `rb_CFString_class_initialize_before_fork` and `rb_str_append_normalized_ospath`.
This commit is contained in:
parent
71b253cdf3
commit
2a65f4c907
Notes:
git
2024-09-26 10:14:34 +00:00
34
file.c
34
file.c
@ -271,6 +271,18 @@ rb_str_encode_ospath(VALUE path)
|
|||||||
# define NORMALIZE_UTF8PATH 1
|
# define NORMALIZE_UTF8PATH 1
|
||||||
|
|
||||||
# ifdef HAVE_WORKING_FORK
|
# 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
|
static void
|
||||||
rb_CFString_class_initialize_before_fork(void)
|
rb_CFString_class_initialize_before_fork(void)
|
||||||
{
|
{
|
||||||
@ -297,15 +309,9 @@ rb_CFString_class_initialize_before_fork(void)
|
|||||||
/* Enough small but non-empty ASCII string to fit in NSTaggedPointerString. */
|
/* Enough small but non-empty ASCII string to fit in NSTaggedPointerString. */
|
||||||
const char small_str[] = "/";
|
const char small_str[] = "/";
|
||||||
long len = sizeof(small_str) - 1;
|
long len = sizeof(small_str) - 1;
|
||||||
|
CFStringRef s;
|
||||||
const CFAllocatorRef alloc = kCFAllocatorDefault;
|
CFMutableStringRef m = mutable_CFString_new(&s, small_str, len);
|
||||||
CFStringRef s = CFStringCreateWithBytesNoCopy(alloc,
|
mutable_CFString_release(m, s);
|
||||||
(const UInt8 *)small_str,
|
|
||||||
len, kCFStringEncodingUTF8,
|
|
||||||
FALSE, kCFAllocatorNull);
|
|
||||||
CFMutableStringRef m = CFStringCreateMutableCopy(alloc, len, s);
|
|
||||||
CFRelease(m);
|
|
||||||
CFRelease(s);
|
|
||||||
}
|
}
|
||||||
# endif /* HAVE_WORKING_FORK */
|
# endif /* HAVE_WORKING_FORK */
|
||||||
|
|
||||||
@ -314,11 +320,8 @@ rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len)
|
|||||||
{
|
{
|
||||||
CFIndex buflen = 0;
|
CFIndex buflen = 0;
|
||||||
CFRange all;
|
CFRange all;
|
||||||
CFStringRef s = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault,
|
CFStringRef s;
|
||||||
(const UInt8 *)ptr, len,
|
CFMutableStringRef m = mutable_CFString_new(&s, ptr, len);
|
||||||
kCFStringEncodingUTF8, FALSE,
|
|
||||||
kCFAllocatorNull);
|
|
||||||
CFMutableStringRef m = CFStringCreateMutableCopy(kCFAllocatorDefault, len, s);
|
|
||||||
long oldlen = RSTRING_LEN(str);
|
long oldlen = RSTRING_LEN(str);
|
||||||
|
|
||||||
CFStringNormalize(m, kCFStringNormalizationFormC);
|
CFStringNormalize(m, kCFStringNormalizationFormC);
|
||||||
@ -328,8 +331,7 @@ rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len)
|
|||||||
CFStringGetBytes(m, all, kCFStringEncodingUTF8, '?', FALSE,
|
CFStringGetBytes(m, all, kCFStringEncodingUTF8, '?', FALSE,
|
||||||
(UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
|
(UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
|
||||||
rb_str_set_len(str, oldlen + buflen);
|
rb_str_set_len(str, oldlen + buflen);
|
||||||
CFRelease(m);
|
mutable_CFString_release(m, s);
|
||||||
CFRelease(s);
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user