<commit_before><?php namespace lang;

/**
 * Represents the runtime - that is, the PHP binary executing the
 * current process.
 *
 * @test  xp://net.xp_framework.unittest.core.SystemExitTest
 * @test  xp://net.xp_framework.unittest.core.RuntimeTest
 */
class Runtime {
  protected static 
    $instance   = null;
    
  protected
    $executable = null,
    $startup    = null;
  
  static function __static() {
    self::$instance= new self();
  }
  
  /**
   * Retrieve the runtime instance
   *
   * @return  lang.Runtime
   */
  public static function getInstance() {
    return self::$instance;
  }
  
  /**
   * Loads a dynamic library.
   *
   * @see     php://dl
   * @param   string name
   * @return  bool TRUE if the library was loaded, FALSE if it was already loaded
   * @throws  lang.IllegalAccessException in case library loading is prohibited
   * @throws  lang.ElementNotFoundException in case the library does not exist
   * @throws  lang.RuntimeError in case dl() fails
   */
  public function loadLibrary($name) {
    if (extension_loaded($name)) return false;
  
    // dl() will fatal if any of these are set - prevent this
    if (!(bool)ini_get('enable_dl') || (bool)ini_get('safe_mode')) {
      throw new IllegalAccessException(sprintf(
        'Loading libraries not permitted by system configuration [enable_dl= %s, safe_mode= %s]',
        ini_get('enable_dl'),
        ini_get('safe_mode')
      ));
    }
    
    // Qualify filename
    $path= rtrim(realpath(ini_get('extension_dir')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
    $filename= $name.'.'.PHP_SHLIB_SUFFIX;
    
    // Try php_<name>.<ext>, <name>.<ext>      
    if (file_exists($lib= $path.'php_'.$filename)) {
      // E.g. php_sybase_ct.dll
    } else if (file_exists($lib= $path.$filename)) {
      // E.g. sybase_ct.so
    } else {
      throw new ElementNotFoundException('Cannot find library "'.$name.'" in "'.$path.'"');
    }
    
    // Found library, try to load it. dl() expects given argument to not contain
    // a path and will fail with "Temporary module name should contain only 
    // filename" if it does.
    if (!dl(basename($lib))) {
      throw new RuntimeError('dl() failed for '.$lib);
    }
    return true;
  }

  /**
   * Returns the total amount of memory available to the runtime. If there
   * is no limit zero will be returned.
   *
   * @return  int bytes
   */
  public function memoryLimit() {
    return (int)ini_get('memory_limit');
  }

  /**
   * Returns the amount of memory currently allocated by the runtime
   *
   * @see     php://memory_get_peak_usage
   * @return  int bytes
   */
  public function memoryUsage() {
    return memory_get_usage();
  }

  /**
   * Returns the peak of of memory that has been allocated by the
   * runtime up until now.
   *
   * @see     php://memory_get_usage
   * @return  int bytes
   */
  public function peakMemoryUsage() {
    return memory_get_peak_usage();
  }

  /**
   * Check whether a given extension is available
   *
   * @see     php://extension_loaded
   * @param   string name
   * @return  bool
   */
  public function extensionAvailable($name) {
    return extension_loaded($name);
  }

  /**
   * Register a shutdown hook - a piece of code that will be run before
   * the runtime shuts down (e.g. with exit).
   *
   * @see     php://register_shutdown_function
   * @param   lang.Runnable r
   * @return  lang.Runnable the given runnable
   */
  public function addShutdownHook(Runnable $r) {
    register_shutdown_function([$r, 'run']);
    return $r;
  }
  
  /**
   * Stops execution by raising a SystemExit
   *
   * @see     xp://lang.SystemExit
   * @param   int code default 0
   * @param   string message default NULL
   */
  public static function halt($code= 0, $message= null) {
    throw new SystemExit($code, $message);
  }

  /**
   * Parse command line, stopping at first argument without "-"
   * or at "--" (php [options] -- [args...])
   *
   * @param   string[] arguments
   * @return  [:var]
   * @throws  lang.FormatException in case an unrecognized argument is encountered
   */
  public static function parseArguments($arguments) {
    $return= ['options' => new RuntimeOptions(), 'bootstrap' => null, 'main' => null];
    while (null !== ($argument= array_shift($arguments))) {
      if ('-' !== $argument{0}) {
        $return['bootstrap']= trim($argument, '"\'');;
        break;
      } else if ('--' === $argument) {
        $return['bootstrap']= trim(array_shift($arguments), '"\'');
        break;
      }
      switch ($argument{1}) {
        case 'q':     // quiet
        case 'n':     // No php.ini file will be used
        case 'C': {   // [cgi] Do not chdir to the script's directory
          $return['options']->withSwitch($argument{1});
          break;
        }

        case 'd': {
          if (2 === strlen($argument)) {
            sscanf(array_shift($arguments), "%[^=]=%[^\r]", $setting, $value);
          } else {
            sscanf($argument, "-d%[^=]=%[^\r]", $setting, $value);
            $setting= ltrim($setting, ' ');
          }
          $return['options']->withSetting($setting, $value, true);
          break;
        }

        default: {<commit_msg>Skip empty arguments<commit_after><?php namespace lang;

/**
 * Represents the runtime - that is, the PHP binary executing the
 * current process.
 *
 * @test  xp://net.xp_framework.unittest.core.SystemExitTest
 * @test  xp://net.xp_framework.unittest.core.RuntimeTest
 */
class Runtime {
  protected static 
    $instance   = null;
    
  protected
    $executable = null,
    $startup    = null;
  
  static function __static() {
    self::$instance= new self();
  }
  
  /**
   * Retrieve the runtime instance
   *
   * @return  lang.Runtime
   */
  public static function getInstance() {
    return self::$instance;
  }
  
  /**
   * Loads a dynamic library.
   *
   * @see     php://dl
   * @param   string name
   * @return  bool TRUE if the library was loaded, FALSE if it was already loaded
   * @throws  lang.IllegalAccessException in case library loading is prohibited
   * @throws  lang.ElementNotFoundException in case the library does not exist
   * @throws  lang.RuntimeError in case dl() fails
   */
  public function loadLibrary($name) {
    if (extension_loaded($name)) return false;
  
    // dl() will fatal if any of these are set - prevent this
    if (!(bool)ini_get('enable_dl') || (bool)ini_get('safe_mode')) {
      throw new IllegalAccessException(sprintf(
        'Loading libraries not permitted by system configuration [enable_dl= %s, safe_mode= %s]',
        ini_get('enable_dl'),
        ini_get('safe_mode')
      ));
    }
    
    // Qualify filename
    $path= rtrim(realpath(ini_get('extension_dir')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
    $filename= $name.'.'.PHP_SHLIB_SUFFIX;
    
    // Try php_<name>.<ext>, <name>.<ext>      
    if (file_exists($lib= $path.'php_'.$filename)) {
      // E.g. php_sybase_ct.dll
    } else if (file_exists($lib= $path.$filename)) {
      // E.g. sybase_ct.so
    } else {
      throw new ElementNotFoundException('Cannot find library "'.$name.'" in "'.$path.'"');
    }
    
    // Found library, try to load it. dl() expects given argument to not contain
    // a path and will fail with "Temporary module name should contain only 
    // filename" if it does.
    if (!dl(basename($lib))) {
      throw new RuntimeError('dl() failed for '.$lib);
    }
    return true;
  }

  /**
   * Returns the total amount of memory available to the runtime. If there
   * is no limit zero will be returned.
   *
   * @return  int bytes
   */
  public function memoryLimit() {
    return (int)ini_get('memory_limit');
  }

  /**
   * Returns the amount of memory currently allocated by the runtime
   *
   * @see     php://memory_get_peak_usage
   * @return  int bytes
   */
  public function memoryUsage() {
    return memory_get_usage();
  }

  /**
   * Returns the peak of of memory that has been allocated by the
   * runtime up until now.
   *
   * @see     php://memory_get_usage
   * @return  int bytes
   */
  public function peakMemoryUsage() {
    return memory_get_peak_usage();
  }

  /**
   * Check whether a given extension is available
   *
   * @see     php://extension_loaded
   * @param   string name
   * @return  bool
   */
  public function extensionAvailable($name) {
    return extension_loaded($name);
  }

  /**
   * Register a shutdown hook - a piece of code that will be run before
   * the runtime shuts down (e.g. with exit).
   *
   * @see     php://register_shutdown_function
   * @param   lang.Runnable r
   * @return  lang.Runnable the given runnable
   */
  public function addShutdownHook(Runnable $r) {
    register_shutdown_function([$r, 'run']);
    return $r;
  }
  
  /**
   * Stops execution by raising a SystemExit
   *
   * @see     xp://lang.SystemExit
   * @param   int code default 0
   * @param   string message default NULL
   */
  public static function halt($code= 0, $message= null) {
    throw new SystemExit($code, $message);
  }

  /**
   * Parse command line, stopping at first argument without "-"
   * or at "--" (php [options] -- [args...])
   *
   * @param   string[] arguments
   * @return  [:var]
   * @throws  lang.FormatException in case an unrecognized argument is encountered
   */
  public static function parseArguments($arguments) {
    $return= ['options' => new RuntimeOptions(), 'bootstrap' => null, 'main' => null];
    while (null !== ($argument= array_shift($arguments))) {
      if ('' === $argument) {
        continue;
      } else if ('-' !== $argument{0}) {
        $return['bootstrap']= trim($argument, '"\'');;
        break;
      } else if ('--' === $argument) {
        $return['bootstrap']= trim(array_shift($arguments), '"\'');
        break;
      }
      switch ($argument{1}) {
        case 'q':     // quiet
        case 'n':     // No php.ini file will be used
        case 'C': {   // [cgi] Do not chdir to the script's directory
          $return['options']->withSwitch($argument{1});
          break;
        }

        case 'd': {
          if (2 === strlen($argument)) {
            sscanf(array_shift($arguments), "%[^=]=%[^\r]", $setting, $value);
          } else {
            sscanf($argument, "-d%[^=]=%[^\r]", $setting, $value);
            $setting= ltrim($setting, ' ');
          }
          $return['options']->withSetting($setting, $value, true);
          break;
        }

        default: {
{'fraction_non_alphanumeric': 0.13900019080328183, 'fraction_numerical': 0.0011448196908986834, 'mean_word_length': 2.490842490842491, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, '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': '956228', 'n_tokens_mistral': 3237, 'n_tokens_neox': 3166, 'n_words': 924}