<commit_before>// Copyright 2004-present Facebook. All Rights Reserved.

#pragma once

#include <memory>
#include <sstream>
#include <unordered_map>
#include <vector>

#include <JavaScriptCore/JSContextRef.h>
#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSStringRef.h>
#include <JavaScriptCore/JSValueRef.h>

#include <folly/dynamic.h>

#include "noncopyable.h"

#if WITH_FBJSCEXTENSIONS
#include <jsc_stringref.h>
#endif

namespace facebook {
namespace react {

class Value;
class Context;

class JSException : public std::runtime_error {
public:
  explicit JSException(const char* msg)
    : std::runtime_error(msg)
    , stack_("") {}

  JSException(const char* msg, const char* stack)
    : std::runtime_error(msg)
    , stack_(stack) {}

  const std::string& getStack() const {
    return stack_;
  }

private:
  std::string stack_;
};


class String : public noncopyable {
public:
  explicit String(const char* utf8) :
    m_string(JSStringCreateWithUTF8CString(utf8))
  {}

  String(String&& other) :
    m_string(other.m_string)
  {}

  String(const String& other) :
    m_string(other.m_string)
  {
    if (m_string) {
      JSStringRetain(m_string);
    }
  }

  ~String() {
    if (m_string) {
      JSStringRelease(m_string);
    }
  }

  operator JSStringRef() const {
    return m_string;
  }

  // Length in characters
  size_t length() const {
    return JSStringGetLength(m_string);
  }

  // Length in bytes of a null-terminated utf8 encoded value
  size_t utf8Size() const {
    return JSStringGetMaximumUTF8CStringSize(m_string);
  }

  std::string str() const {
    size_t reserved = utf8Size();
    char* bytes = new char[reserved];
    size_t length = JSStringGetUTF8CString(m_string, bytes, reserved) - 1;
    std::unique_ptr<char[]> retainedBytes(bytes);
    return std::string(bytes, length);
  }

  // Assumes that utf8 is null terminated
  bool equals(const char* utf8) {
    return JSStringIsEqualToUTF8CString(m_string, utf8);
  }

  static String createExpectingAscii(const char* utf8, size_t len) {
  #if WITH_FBJSCEXTENSIONS
    return String(
      JSStringCreateWithUTF8CStringExpectAscii(utf8, len), true);
  #else
    return String(JSStringCreateWithUTF8CString(utf8), true);
  #endif
  }

  static String createExpectingAscii(std::string const &utf8) {
    return String::createExpectingAscii(utf8.c_str(), utf8.size());
  }
<commit_msg>if !WITH_JSCEXTENSIONS, append a \0<commit_after>// Copyright 2004-present Facebook. All Rights Reserved.

#pragma once

#include <memory>
#include <sstream>
#include <unordered_map>
#include <vector>

#include <JavaScriptCore/JSContextRef.h>
#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSStringRef.h>
#include <JavaScriptCore/JSValueRef.h>

#include <folly/dynamic.h>

#include "noncopyable.h"

#if WITH_FBJSCEXTENSIONS
#include <jsc_stringref.h>
#endif

namespace facebook {
namespace react {

class Value;
class Context;

class JSException : public std::runtime_error {
public:
  explicit JSException(const char* msg)
    : std::runtime_error(msg)
    , stack_("") {}

  JSException(const char* msg, const char* stack)
    : std::runtime_error(msg)
    , stack_(stack) {}

  const std::string& getStack() const {
    return stack_;
  }

private:
  std::string stack_;
};


class String : public noncopyable {
public:
  explicit String(const char* utf8) :
    m_string(JSStringCreateWithUTF8CString(utf8))
  {}

  String(String&& other) :
    m_string(other.m_string)
  {}

  String(const String& other) :
    m_string(other.m_string)
  {
    if (m_string) {
      JSStringRetain(m_string);
    }
  }

  ~String() {
    if (m_string) {
      JSStringRelease(m_string);
    }
  }

  operator JSStringRef() const {
    return m_string;
  }

  // Length in characters
  size_t length() const {
    return JSStringGetLength(m_string);
  }

  // Length in bytes of a null-terminated utf8 encoded value
  size_t utf8Size() const {
    return JSStringGetMaximumUTF8CStringSize(m_string);
  }

  std::string str() const {
    size_t reserved = utf8Size();
    char* bytes = new char[reserved];
    size_t length = JSStringGetUTF8CString(m_string, bytes, reserved) - 1;
    std::unique_ptr<char[]> retainedBytes(bytes);
    return std::string(bytes, length);
  }

  // Assumes that utf8 is null terminated
  bool equals(const char* utf8) {
    return JSStringIsEqualToUTF8CString(m_string, utf8);
  }

  static String createExpectingAscii(const char* utf8, size_t len) {
  #if WITH_FBJSCEXTENSIONS
    return String(JSStringCreateWithUTF8CStringExpectAscii(utf8, len), true);
  #else
    return createExpectingAscii(std::string(utf8, len));
  #endif
  }

  static String createExpectingAscii(std::string const &utf8) {
#if WITH_FBJSCEXTENSIONS
    return createExpectingAscii(utf8.c_str(), utf8.size());
#else
    return String(JSStringCreateWithUTF8CString(utf8.c_str()), true);
#endif
  }

{'fraction_non_alphanumeric': 0.11448979591836735, 'fraction_numerical': 0.010612244897959184, 'mean_word_length': 4.197242841993638, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6511481', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1660, 'n_words': 374}