<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  /**
   * A preference
   *
   * <pre> 
   * Accept-Language: en,de;q=0.5
   * Accept-Language: en-UK;q=0.7, en-US;q=0.6, no;q=1.0, dk;q=0.8
   * Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
   * </pre>
   *
   * @test     xp://net.xp_framework.unittest.scriptlet.PreferenceTest
   */
  class Preference extends Object {
    protected $list= array();

    /**
     * Parses the "Accept" header
     *
     * @param  string[] accept
     * @param  [:float] list
     */
    protected function preferenceOf($accept) {
      $values= array();
      $significance= 1.0;
      foreach ($accept as $t) {
        if (FALSE === ($p= strpos($t, ';'))) {
          $value= ltrim($t, ' ');

          // RFC 2626, Section 3.9: "HTTP/1.1 applications MUST NOT generate more than
          // three digits after the decimal point". Use 5 digits, allowing for 100 q-
          // values with 1 > q > 0.999. Common user-agents supply 3 to 5 values.
          $q= ($significance-= 0.00001);
        } else {
          $value= ltrim(substr($t, 0, $p), ' ');
          $q= (float)ltrim(substr($t, $p), '; q=');
        }
        $values[$value]= $q;
      }
      
      arsort($values, SORT_NUMERIC);
      return $values;
    }

    /**
     * Constructor.
     *
     * @param   var arg
     */
    public function __construct($arg) {
      if (is_array($arg)) {
        $this->list= $this->preferenceOf($arg);
      } else {
        $this->list= $this->preferenceOf(explode(',', $arg));
      }
    }

    public function preferred() {
      return key($this->list);
    }

    public function all() {
      return array_keys($this->list);
    }

    /**
     * Match this preference
     *
     * @param  string[] supported
     * @param  string
     */
    public function match($supported) {
      $str= implode(' ', $supported);
      foreach ($this->list as $preference => $q) {
        if (strstr($preference, '*')) {
          if (preg_match('#('.strtr(preg_quote($preference, '#'), array('\*' => '[^ ]+')).')#', $str, $matches)) return $matches[1];
        } else {
          if (FALSE !== ($p= array_search($preference, $supported))) return $supported[$p];
        }
      }
      return NULL;
    }
  }
?><commit_msg>- Optimization: Only use preg_match()<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  /**
   * A preference
   *
   * <pre> 
   * Accept-Language: en,de;q=0.5
   * Accept-Language: en-UK;q=0.7, en-US;q=0.6, no;q=1.0, dk;q=0.8
   * Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
   * </pre>
   *
   * @test     xp://net.xp_framework.unittest.scriptlet.PreferenceTest
   */
  class Preference extends Object {
    protected $list= array();

    /**
     * Parses the "Accept" header
     *
     * @param  string[] accept
     * @param  [:float] list
     */
    protected function preferenceOf($accept) {
      $values= array();
      $significance= 1.0;
      foreach ($accept as $t) {
        if (FALSE === ($p= strpos($t, ';'))) {
          $value= ltrim($t, ' ');

          // RFC 2626, Section 3.9: "HTTP/1.1 applications MUST NOT generate more than
          // three digits after the decimal point". Use 5 digits, allowing for 100 q-
          // values with 1 > q > 0.999. Common user-agents supply 3 to 5 values.
          $q= ($significance-= 0.00001);
        } else {
          $value= ltrim(substr($t, 0, $p), ' ');
          $q= (float)ltrim(substr($t, $p), '; q=');
        }
        $values[$value]= $q;
      }
      
      arsort($values, SORT_NUMERIC);
      return $values;
    }

    /**
     * Constructor.
     *
     * @param   var arg
     */
    public function __construct($arg) {
      if (is_array($arg)) {
        $this->list= $this->preferenceOf($arg);
      } else {
        $this->list= $this->preferenceOf(explode(',', $arg));
      }
    }

    public function preferred() {
      return key($this->list);
    }

    public function all() {
      return array_keys($this->list);
    }

    /**
     * Match this preference
     *
     * @param  string[] supported
     * @param  string
     */
    public function match($supported) {
      $str= implode(' ', $supported);
      foreach ($this->list as $preference => $q) {
        if (preg_match('#('.strtr(preg_quote($preference, '#'), array('\*' => '[^ ]+')).')#', $str, $matches)) return $matches[1];
      }
      return NULL;
    }
  }
?>
{'fraction_non_alphanumeric': 0.18226711560044892, 'fraction_numerical': 0.021997755331088664, 'mean_word_length': 2.023066485753053, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 31, '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': '7479933', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1510, 'n_words': 346}