User:SignpostMarv Martin/is uuid comparison
regex
<php> function is_uuid_regex($string) { return (bool)preg_match( '/^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/S' ,$string); } </php>
ctype_xdigit function
<php> if(function_exists('ctype_xdigit') === false) { function ctype_xdigit($string) { return (($translated = strtr($string,array( '0'=>, '1'=>, '2'=>, '3'=>, '4'=>, '5'=>, '6'=>, '7'=>, '8'=>, '9'=>, 'a'=>, 'b'=>, 'c'=>, 'd'=>, 'e'=>, 'f'=>, ))) !== false && strlen($translated) !== 0); } } </php>
no regex
<php> function is_uuid_SignpostMarv_Martin($string) { $match = true; if(strlen($string) !== 36) { $match = false; } else if(strpos($string,'-') !== 8) { $match = false; } else if(strpos($string,'-',24) !== false) { $match = false; } else if(strpos($string,'-',9) !== 13) { $match = false; } else if(strpos($string,'-',13) !== 13) { $match = false; } else if(strpos($string,'-',14) !== 18) { $match = false; } else if(strpos($string,'-',18) !== 18) { $match = false; } else if(strpos($string,'-',19) !== 23) { $match = false; } else if(strpos($string,'-',23) !== 23) { $match = false; } else if(ctype_xdigit(str_replace('-',,$string)) === false) { $match = false; } return $match; } </php>
nested ternary
<php> function is_uuid_nested_ternary_SignpostMarv_Martin($string) {
return (strlen($string) !== 36) ? false : ( (strpos($string,'-') !== 8) ? false : ( (strpos($string,'-',24) !== false) ? false : ( (strpos($string,'-',9) !== 13) ? false : ( (strpos($string,'-',13) !== 13) ? false : ( (strpos($string,'-',14) !== 18) ? false : ( (strpos($string,'-',18) !== 18) ? false : ( (strpos($string,'-',19) !== 23) ? false : ( (strpos($string,'-',23) !== 23) ? false : ctype_xdigit(str_replace('-',,$string)) ) ) ) ) ) ) ) ) ;
} </php>
benchmark
<php> require_once('Benchmark/Iterate.php'); function benchmark_is_uuid($string,$loop,array $funcs) { $results = array(); $maxkeylen = null; foreach($funcs as $func) { $benchmark = new Benchmark_Iterate; $results[$func] = array( 'mean' => 0.0, 'common' => 0.0, 'shortest' => 0.0, 'longest' => 0.0, 'result' => null, ); if(isset($maxkeylen) === false) { foreach(array_keys($results[$func]) as $k) { $maxkeylen = (strlen($k) > $maxkeylen) ? strlen($k) : $maxkeylen; } } $results[$func]['result'] = call_user_func_array($func,array($string)) ? 'TRUE' : 'FALSE'; $benchmark->run($loop,$func,$string); $result = $benchmark->get(); unset($benchmark); $results[$func]['mean'] = $result['mean']; unset($result['mean'],$result['iterations']); $result_foo = array(); foreach($result as $v) { if(isset($result_foo[(string)$v]) === false) { $result_foo[(string)$v] = 0; } ++$result_foo[(string)$v]; } unset($result); arsort($result_foo); $results[$func]['common'] = key($result_foo); $times = array_keys($result_foo); unset($result_foo); sort($times); $results[$func]['shortest'] = current($times); $results[$func]['longest'] = end($times); unset($times); } echo $string; foreach($results as $func => $result) { echo "\n",$func; foreach($result as $k=>$v) { echo "\n\t",$k,': ',str_repeat(' ',$maxkeylen - strlen($k)),$v; } echo "\n\n"; } echo "\n"; unset($results); } function batch_benchmark_is_uuid(array $strings,$loop,array $funcs) { foreach($strings as $string) { benchmark_is_uuid($string,$loop,$funcs); } } ini_set('max_execution_time',0); define('NULL_KEY','00000000-0000-0000-0000-000000000000',true); header('Content-Type:text/plain'); batch_benchmark_is_uuid(array( NULL_KEY, '000000000000000000000000000000000000', '00000000-0000-0000-0000-00000000000#', '00000000-f000-0000-0000-000000000000', '00000000-z000-0000-0000-000000000000', '00000000--000-0000-0000-000000000000', ': I thin-k I have it, but I\'m unsure', ), 10000, array( 'is_uuid_regex', 'is_uuid_nested_ternary_SignpostMarv_Martin', 'is_uuid_nested_ternary_SignpostMarv_Martin', ) ); </php>