<commit_before><?php

/**
 * URL utility class
 *
 * This class provides methods to deal with encoding and decoding url (percent encoded) strings.
 *
 * It was not possible to use PHP's built-in methods for this, because some clients don't like
 * encoding of certain characters.
 *
 * Specifically, it was found that GVFS (gnome's webdav client) does not like encoding of ( and
 * ). Since these are reserved, but don't have a reserved meaning in url, these characters are
 * kept as-is.
 * 
 * @package Sabre
 * @subpackage DAV
 * @copyright Copyright (C) 2007-2011 Rooftop Solutions. All rights reserved.
 * @author <NAME> (http://www.rooftopsolutions.nl/) 
 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
 */
class Sabre_DAV_URLUtil {

    /**
     * Encodes the path of a url.
     *
     * slashes (/) are treated as path-separators.
     * 
     * @param string $path 
     * @return string 
     */
    static function encodePath($path) {

    	$valid_chars = '/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.~()';
        $newStr = '';
        for( $i=0; isset($path[$i]); ++$i ) {
            if( strpos($valid_chars,($c=$path[$i]))===false ) $newStr .= '%'.sprintf('%02x',ord($c));
            else $newStr .= $c;
        }
        return $newStr;
    	
    }

    /**
     * Encodes a 1 segment of a path
     *
     * Slashes are considered part of the name, and are encoded as %2f
     * 
     * @param string $pathSegment 
     * @return string 
     */
    static function encodePathSegment($pathSegment) {

    	$valid_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.~()';
        $newStr = '';
        for( $i=0; isset($pathSegment[$i]); ++$i ) {
            if( strpos($valid_chars,($c=$pathSegment[$i]))===false ) $newStr .= '%'.sprintf('%02x',ord($c));
            else $newStr .= $c;
        }
        return $newStr;<commit_msg>Now using preg_replace_callback, making this function 20x faster for most strings. Fixes Issue #150.<commit_after><?php

/**
 * URL utility class
 *
 * This class provides methods to deal with encoding and decoding url (percent encoded) strings.
 *
 * It was not possible to use PHP's built-in methods for this, because some clients don't like
 * encoding of certain characters.
 *
 * Specifically, it was found that GVFS (gnome's webdav client) does not like encoding of ( and
 * ). Since these are reserved, but don't have a reserved meaning in url, these characters are
 * kept as-is.
 * 
 * @package Sabre
 * @subpackage DAV
 * @copyright Copyright (C) 2007-2011 Rooftop Solutions. All rights reserved.
 * @author <NAME> (http://www.rooftopsolutions.nl/) 
 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
 */
class Sabre_DAV_URLUtil {

    /**
     * Encodes the path of a url.
     *
     * slashes (/) are treated as path-separators.
     * 
     * @param string $path 
     * @return string 
     */
    static function encodePath($path) {

        return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)\/])/',function($match) {

            return '%'.sprintf('%02x',ord($match[0]));

        }, $path);
    	
    }

    /**
     * Encodes a 1 segment of a path
     *
     * Slashes are considered part of the name, and are encoded as %2f
     * 
     * @param string $pathSegment 
     * @return string 
     */
    static function encodePathSegment($pathSegment) {

        return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)])/',function($match) {

            return '%'.sprintf('%02x',ord($match[0]));

        }, $pathSegment);
{'fraction_non_alphanumeric': 0.1363510136073313, 'fraction_numerical': 0.016939738961399612, 'mean_word_length': 2.7019527235354572, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6168577', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1104, 'n_words': 332}