<commit_before>// TODO
// * free memory when raising

#include "extconf.h"

#include "ruby.h"
#if HAVE_RUBY_ST_H
#include "ruby/st.h"
#elif HAVE_ST_H
#include "st.h"
#endif

#include "CoreFoundation/CoreFoundation.h"
#include "CoreFoundation/CFStream.h"
#include "CoreFoundation/CFPropertyList.h"

VALUE Xcodeproj = Qnil;


static VALUE
cfstr_to_str(const void *cfstr) {
  CFDataRef data = CFStringCreateExternalRepresentation(NULL, cfstr, kCFStringEncodingUTF8, 0);
  assert(data != NULL);
  long  len = (long)CFDataGetLength(data);
  char *buf = (char *)CFDataGetBytePtr(data);

  register VALUE str = rb_str_new(buf, len);

  // force UTF-8 encoding in Ruby 1.9+
  ID forceEncodingId = rb_intern("force_encoding");
  if (rb_respond_to(str, forceEncodingId)) {
    rb_funcall(str, forceEncodingId, 1, rb_str_new("UTF-8", 5));
  }

  return str;
}

// Coerces to String as well.
static CFStringRef
str_to_cfstr(VALUE str) {
  return CFStringCreateWithCString(NULL, RSTRING_PTR(rb_String(str)), kCFStringEncodingUTF8);
}

/* Generates a UUID. The original version is truncated, so this is not 100%
 * guaranteed to be unique. However, the `PBXObject#generate_uuid` method
 * checks that the UUID does not exist yet, in the project, before using it.
 *
 * @note Meant for internal use only.
 *
 * @return [String] A 24 characters long UUID.
 */
static VALUE
generate_uuid(void) {
  CFUUIDRef uuid = CFUUIDCreate(NULL);
  CFStringRef strRef = CFUUIDCreateString(NULL, uuid);
  CFRelease(uuid);<commit_msg>Fix CFDataRef memory leak in cfstr_to_str()<commit_after>// TODO
// * free memory when raising

#include "extconf.h"

#include "ruby.h"
#if HAVE_RUBY_ST_H
#include "ruby/st.h"
#elif HAVE_ST_H
#include "st.h"
#endif

#include "CoreFoundation/CoreFoundation.h"
#include "CoreFoundation/CFStream.h"
#include "CoreFoundation/CFPropertyList.h"

VALUE Xcodeproj = Qnil;


static VALUE
cfstr_to_str(const void *cfstr) {
  CFDataRef data = CFStringCreateExternalRepresentation(NULL, cfstr, kCFStringEncodingUTF8, 0);
  assert(data != NULL);
  long  len = (long)CFDataGetLength(data);
  char *buf = (char *)CFDataGetBytePtr(data);

  register VALUE str = rb_str_new(buf, len);

  // force UTF-8 encoding in Ruby 1.9+
  ID forceEncodingId = rb_intern("force_encoding");
  if (rb_respond_to(str, forceEncodingId)) {
    rb_funcall(str, forceEncodingId, 1, rb_str_new("UTF-8", 5));
  }

  CFRelease(data);
  return str;
}

// Coerces to String as well.
static CFStringRef
str_to_cfstr(VALUE str) {
  return CFStringCreateWithCString(NULL, RSTRING_PTR(rb_String(str)), kCFStringEncodingUTF8);
}

/* Generates a UUID. The original version is truncated, so this is not 100%
 * guaranteed to be unique. However, the `PBXObject#generate_uuid` method
 * checks that the UUID does not exist yet, in the project, before using it.
 *
 * @note Meant for internal use only.
 *
 * @return [String] A 24 characters long UUID.
 */
static VALUE
generate_uuid(void) {
  CFUUIDRef uuid = CFUUIDCreate(NULL);
  CFStringRef strRef = CFUUIDCreateString(NULL, uuid);
  CFRelease(uuid);
{'fraction_non_alphanumeric': 0.11292962356792144, 'fraction_numerical': 0.009165302782324058, 'mean_word_length': 4.957115009746588, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2108563', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1058, 'n_words': 293}