PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/phoronix-test-suite/pts-core/objects/pts_strings.php

#
PHP | 653 lines | 568 code | 52 blank | 33 comment | 60 complexity | 786af78299d6fe7c0ec03ebcfa39fc94 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Phoronix Test Suite
  4. URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
  5. Copyright (C) 2010 - 2012, Phoronix Media
  6. Copyright (C) 2010 - 2012, Michael Larabel
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // TODO XXX: some of these functions may be better optimized using the ctype functions: http://us.php.net/manual/en/ref.ctype.php
  19. class pts_strings
  20. {
  21. const CHAR_LETTER = 2;
  22. const CHAR_NUMERIC = 4;
  23. const CHAR_DECIMAL = 8;
  24. const CHAR_SPACE = 16;
  25. const CHAR_DASH = 32;
  26. const CHAR_UNDERSCORE = 64;
  27. const CHAR_COLON = 128;
  28. const CHAR_COMMA = 256;
  29. const CHAR_SLASH = 512;
  30. const CHAR_AT = 1024;
  31. const CHAR_PLUS = 2048;
  32. const CHAR_SEMICOLON = 4096;
  33. const CHAR_EQUAL = 8192;
  34. public static function is_url($string)
  35. {
  36. $components = parse_url($string);
  37. return $components != false && isset($components['scheme']) && isset($components['host']);
  38. }
  39. public static function is_version($string)
  40. {
  41. // Only numeric or decimal, and at least a decimal (not int)
  42. return pts_strings::string_only_contains($string, (pts_strings::CHAR_NUMERIC | pts_strings::CHAR_DECIMAL)) && pts_strings::string_contains($string, pts_strings::CHAR_DECIMAL);
  43. }
  44. public static function trim_search_query($value)
  45. {
  46. $search_break_characters = array('@', '(', '/', '+', '[', '<', '/');
  47. for($i = 0, $x = strlen($value); $i < $x; $i++)
  48. {
  49. if(in_array($value[$i], $search_break_characters))
  50. {
  51. $value = substr($value, 0, $i);
  52. break;
  53. }
  54. }
  55. if((is_numeric($value) && substr($value, 0, 2) != '0x') || $value == null)
  56. {
  57. return;
  58. }
  59. // Remove multiplier if prepended to string
  60. $multiplier = strpos($value, ' x ');
  61. if($multiplier !== false && is_numeric(substr($value, 0, $multiplier)))
  62. {
  63. $value = substr($value, ($multiplier + 3));
  64. }
  65. $value = str_replace('& ', null, $value);
  66. if(substr($value, -1) == '.')
  67. {
  68. $value = substr($value, 0, -1);
  69. }
  70. if(($w = stripos($value, 'WARNING')) !== false)
  71. {
  72. // to get rid of Scheisse like 'Gtk-WARNING **: Unable'
  73. $value = substr($value, 0, strrpos($value, ' ', (0 - (strlen($value) - $w))));
  74. }
  75. // Remove other beginning or ending words based upon conditions
  76. $words = explode(' ', trim($value));
  77. $c = count($words);
  78. switch($c)
  79. {
  80. case 1:
  81. if(isset($words[0][2]) && in_array(substr($words[0], -2), array('MB', 'GB', '0W')))
  82. {
  83. // Just searching a disk / memory size or a power supply wattage
  84. $words = array();
  85. }
  86. break;
  87. default:
  88. $last_index = ($c - 1);
  89. if(strpos($words[$last_index], 'v1') !== false || strpos($words[$last_index], 'MB') !== false || strpos($words[$last_index], 'GB') !== false)
  90. {
  91. // Version number being appended to product (some mobos) or the MB/GB size for GPUs
  92. array_pop($words);
  93. }
  94. else if(strpos($words[0], 'GB') !== false)
  95. {
  96. // Likely disk size in front of string
  97. array_shift($words);
  98. }
  99. break;
  100. }
  101. return implode(' ', $words);
  102. }
  103. public static function string_bool($string)
  104. {
  105. // Used for evaluating if the user inputted a string that evaluates to true
  106. return in_array(strtolower($string), array('true', '1'));
  107. }
  108. public static function add_trailing_slash($path)
  109. {
  110. if(PTS_IS_CLIENT && phodevi::is_windows() && strpos($path, ':\\') === 1)
  111. {
  112. return $path . (substr($path, -1) == '\\' ? null : '\\');
  113. }
  114. else
  115. {
  116. return $path . (substr($path, -1) == '/' ? null : '/');
  117. }
  118. }
  119. public static function trim_explode($delimiter, $to_explode)
  120. {
  121. return empty($to_explode) ? array() : array_map('trim', explode($delimiter, $to_explode));
  122. }
  123. public static function comma_explode($to_explode)
  124. {
  125. return self::trim_explode(',', $to_explode);
  126. }
  127. public static function colon_explode($to_explode)
  128. {
  129. return self::trim_explode(':', $to_explode);
  130. }
  131. public static function first_in_string($string, $delimited_by = ' ')
  132. {
  133. return ($t = strpos($string, $delimited_by)) ? substr($string, 0, $t) : $string;
  134. }
  135. public static function last_in_string($string, $delimited_by = ' ')
  136. {
  137. return ($t = strrpos($string, $delimited_by)) ? substr($string, ($t + 1)) : $string;
  138. }
  139. public static function array_list_to_string($array, $bold_items = false, $append_to_end = null)
  140. {
  141. $count = count($array);
  142. if($bold_items)
  143. {
  144. foreach($array as &$item)
  145. {
  146. $item = '<strong>' . $item . '</strong>';
  147. }
  148. }
  149. if($count > 1)
  150. {
  151. $temp = array_pop($array);
  152. array_push($array, 'and ' . $temp);
  153. }
  154. return implode(($count > 2 ? ', ' : ' ') . ' ', $array) . ($append_to_end != null ? ' ' . $append_to_end . ($count > 1 ? 's' : null) : null);
  155. }
  156. public static function has_in_string($string, $r)
  157. {
  158. $has_in_string = false;
  159. foreach($r as $string_to_check)
  160. {
  161. if(strpos($string, $string_to_check) !== false)
  162. {
  163. $has_in_string = $string_to_check;
  164. break;
  165. }
  166. }
  167. return $has_in_string;
  168. }
  169. public static function has_in_istring($string, $r)
  170. {
  171. $has_in_string = false;
  172. foreach($r as $string_to_check)
  173. {
  174. if(stripos($string, $string_to_check) !== false)
  175. {
  176. $has_in_string = $string_to_check;
  177. break;
  178. }
  179. }
  180. return $has_in_string;
  181. }
  182. public static function random_characters($length)
  183. {
  184. $random = null;
  185. for($i = 0; $i < $length; $i++)
  186. {
  187. $random .= chr(rand(65, 90));
  188. }
  189. return $random;
  190. }
  191. public static function find_longest_string(&$string_r)
  192. {
  193. if(!is_array($string_r))
  194. {
  195. return $string_r;
  196. }
  197. $longest_string = null;
  198. $longest_string_length = 0;
  199. foreach($string_r as $one_string)
  200. {
  201. if(is_array($one_string))
  202. {
  203. $one_string = self::find_longest_string($one_string);
  204. }
  205. $one_string = strval($one_string);
  206. if(isset($one_string[$longest_string_length]))
  207. {
  208. $longest_string = $one_string;
  209. $longest_string_length = strlen($one_string);
  210. }
  211. }
  212. return $longest_string;
  213. }
  214. public static function char_is_of_type($char, $attributes)
  215. {
  216. $i = ord($char);
  217. if(($attributes & self::CHAR_LETTER) && (($i > 64 && $i < 91) || ($i > 96 && $i < 123)))
  218. {
  219. $is_of_type = true;
  220. }
  221. else if(($attributes & self::CHAR_NUMERIC) && $i > 47 && $i < 58)
  222. {
  223. $is_of_type = true;
  224. }
  225. else if(($attributes & self::CHAR_DECIMAL) && $i == 46)
  226. {
  227. $is_of_type = true;
  228. }
  229. else if(($attributes & self::CHAR_DASH) && $i == 45)
  230. {
  231. $is_of_type = true;
  232. }
  233. else if(($attributes & self::CHAR_UNDERSCORE) && $i == 95)
  234. {
  235. $is_of_type = true;
  236. }
  237. else if(($attributes & self::CHAR_COLON) && $i == 58)
  238. {
  239. $is_of_type = true;
  240. }
  241. else if(($attributes & self::CHAR_SPACE) && $i == 32)
  242. {
  243. $is_of_type = true;
  244. }
  245. else if(($attributes & self::CHAR_COMMA) && $i == 44)
  246. {
  247. $is_of_type = true;
  248. }
  249. else if(($attributes & self::CHAR_AT) && $i == 64)
  250. {
  251. $is_of_type = true;
  252. }
  253. else if(($attributes & self::CHAR_PLUS) && $i == 43)
  254. {
  255. $is_of_type = true;
  256. }
  257. else if(($attributes & self::CHAR_SEMICOLON) && $i == 59)
  258. {
  259. $is_of_type = true;
  260. }
  261. else if(($attributes & self::CHAR_EQUAL) && $i == 61)
  262. {
  263. $is_of_type = true;
  264. }
  265. else if(($attributes & self::CHAR_SLASH) && ($i == 47 || $i == 92))
  266. {
  267. $is_of_type = true;
  268. }
  269. else
  270. {
  271. $is_of_type = false;
  272. }
  273. return $is_of_type;
  274. }
  275. public static function trim_spaces($str)
  276. {
  277. // get rid of multiple/redundant spaces that are next to each other
  278. $new_str = null;
  279. for($i = strlen($str); $i > 0; $i--)
  280. {
  281. // 32 is a ASCII space
  282. if(ord($str[($i - 1)]) != 32 || ($i < 2 || ord($str[($i - 2)]) != 32))
  283. {
  284. $new_str = $str[$i - 1] . $new_str;
  285. }
  286. }
  287. return trim($new_str);
  288. }
  289. public static function remove_redundant($string, $redundant_char)
  290. {
  291. $prev_char = $string[0];
  292. for($i = 1, $l = strlen($string); $i < $l; $i++)
  293. {
  294. $this_char = $string[$i];
  295. if($this_char == $redundant_char && $prev_char == $redundant_char)
  296. {
  297. $string[($i - 1)] = null;
  298. }
  299. $prev_char = $this_char;
  300. }
  301. return trim($string);
  302. }
  303. public static function strip_string($str)
  304. {
  305. // Clean a string containing hardware information of some common things to change/strip out
  306. $change_phrases = array(
  307. 'MCH' => 'Memory Controller Hub',
  308. 'AMD' => 'Advanced Micro Devices',
  309. 'MSI' => 'MICRO-STAR INTERNATIONAL',
  310. 'SiS' => 'Silicon Integrated Systems',
  311. 'Abit' => 'http://www.abit.com.tw/',
  312. 'ASUS' => 'ASUSTeK',
  313. 'HP' => 'Hewlett-Packard',
  314. 'NVIDIA' => 'nVidia',
  315. 'HDD' => 'HARDDISK',
  316. 'Intel' => 'Intel64',
  317. 'HD' => 'High Definition',
  318. 'IGP' => array('Integrated Graphics Controller', 'Express Graphics Controller', 'Integrated Graphics Device', 'Chipset Integrated')
  319. );
  320. foreach($change_phrases as $new_phrase => $original_phrase)
  321. {
  322. $str = str_ireplace($original_phrase, $new_phrase, $str);
  323. }
  324. $remove_phrases = array('incorporation', 'corporation', 'corp.', 'invalid', 'technologies', 'technology', 'version', 'computer', 'To Be Filled By', 'ODM', 'O.E.M.', 'Desktop Reference Platform', 'small form factor', 'convertible', 'group', 'chipset', 'community', 'reference', 'communications', 'semiconductor', 'processor', 'host bridge', 'adapter', 'CPU', 'platform', 'international', 'express', 'graphics', 'none', 'electronics', 'integrated', 'alternate', 'quad-core', 'memory', 'series', 'network', 'motherboard', 'serverengines', 'Manufacturer', 'x86/mmx/sse2', '/AGP/SSE/3DNOW!', '/AGP/SSE2', 'controller', '(extreme graphics innovation)', 'pci-e_gfx and ht3 k8 part', 'pci-e_gfx and ht1 k8 part', 'Northbridge only', 'dual slot', 'dual-core', 'dual core', 'microsystems', 'not specified', 'single slot', 'genuine', 'unknown device', 'systemberatung', 'gmbh', 'graphics adapter', 'video device', 'http://', 'www.', '.com', '.tw/', '/pci/sse2/3dnow!', '/pcie/sse2', '/pci/sse2', 'balloon', 'network connection', 'ethernet', 'limited.', ' system', 'compliant', 'co. ltd', 'co.', 'ltd.', 'LTD ', 'Ž', '(r)', '(tm)', 'inc.', 'inc', '6.00 PG', ',', '\'', '_ ', '_ ', 'corp', 'product name', 'base board', 'pci to pci', ' bridge', ' release ', 'with Radeon HD');
  325. $str = str_ireplace($remove_phrases, ' ', $str);
  326. if(($w = stripos($str, 'WARNING')) !== false)
  327. {
  328. // to get rid of Scheisse like 'Gtk-WARNING **: Unable'
  329. $str = substr($str, 0, strrpos($str, ' ', (0 - (strlen($str) - $w))));
  330. }
  331. $str = pts_strings::trim_spaces($str);
  332. // Fixes an AMD string issue like 'FX -4100' due to stripping (TM) from in between characters, possibly other cases too
  333. $str = str_replace(' -', '-', $str);
  334. return $str;
  335. }
  336. public static function remove_line_timestamps($log)
  337. {
  338. // Try to strip out timestamps from lines like Xorg.0.log and dmesg, e.g.:
  339. // [ 326.390358] EXT4-fs (dm-1): initial error at 1306235400: ext4_journal_start_sb:251
  340. $log = explode(PHP_EOL, $log);
  341. foreach($log as &$line)
  342. {
  343. if(substr($line, 0, 1) == '[' && ($t = strpos($line, '] ', 2)) !== false)
  344. {
  345. $encased_segment = trim(substr($line, 1, ($t - 1)));
  346. if(is_numeric($encased_segment))
  347. {
  348. $line = substr($line, ($t + 2));
  349. }
  350. }
  351. }
  352. $log = implode(PHP_EOL, $log);
  353. return $log;
  354. }
  355. public static function remove_lines_containing($contents, $contains)
  356. {
  357. foreach($contains as $needle)
  358. {
  359. while(($x = stripos($contents, $needle)) !== false)
  360. {
  361. $affected_line_begins = strrpos($contents, PHP_EOL, (0 - strlen($contents) + $x));
  362. $affected_line_ends = strpos($contents, PHP_EOL, $x);
  363. $contents = substr($contents, 0, $affected_line_begins) . ($affected_line_ends === false ? null : substr($contents, $affected_line_ends));
  364. }
  365. }
  366. return $contents;
  367. }
  368. public static function pts_version_to_codename($version)
  369. {
  370. $version = substr($version, 0, 3);
  371. $codenames = array(
  372. '1.0' => 'Trondheim',
  373. '1.2' => 'Malvik',
  374. '1.4' => 'Orkdal',
  375. '1.6' => 'Tydal',
  376. '1.8' => 'Selbu',
  377. '2.0' => 'Sandtorg',
  378. '2.2' => 'Bardu',
  379. '2.4' => 'Lenvik',
  380. '2.6' => 'Lyngen',
  381. '2.8' => 'Torsken',
  382. '2.9' => 'Iveland', // early PTS3 development work
  383. '3.0' => 'Iveland',
  384. '3.2' => 'Grimstad',
  385. '3.4' => 'Lillesand',
  386. '3.6' => 'Arendal',
  387. '3.8' => 'Bygland'
  388. );
  389. return isset($codenames[$version]) ? $codenames[$version] : null;
  390. }
  391. public static function parse_week_string($week_string, $delimiter = ' ')
  392. {
  393. $return_array = array();
  394. foreach(array('S', 'M', 'T', 'W', 'TH', 'F', 'S') as $day_int => $day_char)
  395. {
  396. if($week_string[$day_int] == 1)
  397. {
  398. array_push($return_array, $day_char);
  399. }
  400. }
  401. return implode($delimiter, $return_array);
  402. }
  403. public static function remove_from_string($string, $attributes)
  404. {
  405. $string_r = str_split($string);
  406. $new_string = null;
  407. foreach($string_r as $char)
  408. {
  409. if(pts_strings::char_is_of_type($char, $attributes) == false)
  410. {
  411. $new_string .= $char;
  412. }
  413. }
  414. return $new_string;
  415. }
  416. public static function keep_in_string($string, $attributes)
  417. {
  418. $string_r = str_split($string);
  419. $new_string = null;
  420. foreach($string_r as $char)
  421. {
  422. if(pts_strings::char_is_of_type($char, $attributes) == true)
  423. {
  424. $new_string .= $char;
  425. }
  426. }
  427. return $new_string;
  428. }
  429. public static function string_only_contains($string, $attributes)
  430. {
  431. $string_r = str_split($string);
  432. foreach($string_r as $char)
  433. {
  434. if(pts_strings::char_is_of_type($char, $attributes) == false)
  435. {
  436. return false;
  437. }
  438. }
  439. return true;
  440. }
  441. public static function string_contains($string, $attributes)
  442. {
  443. $string_r = str_split($string);
  444. foreach($string_r as $char)
  445. {
  446. if(pts_strings::char_is_of_type($char, $attributes) == true)
  447. {
  448. return true;
  449. }
  450. }
  451. return false;
  452. }
  453. public static function times_occurred($string, $attributes)
  454. {
  455. $string_r = str_split($string);
  456. $times_matched = 0;
  457. foreach($string_r as $char)
  458. {
  459. if(pts_strings::char_is_of_type($char, $attributes) == true)
  460. {
  461. $times_matched++;
  462. }
  463. }
  464. return $times_matched;
  465. }
  466. public static function proximity_match($search, $match_to)
  467. {
  468. // Proximity search in $search string for * against $match_to
  469. $search = explode('*', $search);
  470. $is_match = true;
  471. if(count($search) == 1)
  472. {
  473. $is_match = false;
  474. }
  475. for($i = 0; $i < count($search) && $is_match && !empty($search[$i]); $i++)
  476. {
  477. if(($match_point = strpos($match_to, $search[$i])) !== false && ($i > 0 || $match_point == 0))
  478. {
  479. $match_to = substr($match_to, ($match_point + strlen($search[$i])));
  480. }
  481. else
  482. {
  483. $is_match = false;
  484. }
  485. }
  486. return $is_match;
  487. }
  488. public static function result_quantifier_to_string($quantifier)
  489. {
  490. $quantifiers = array('MAX' => 'Maximum', 'MIN' => 'Minimum', 'NULL' => null, 'AVG' => 'Average');
  491. return isset($quantifiers[$quantifier]) ? $quantifiers[$quantifier] : 'Average';
  492. }
  493. public static function format_time($time, $input_format = 'SECONDS', $standard_version = true, $round_to = 0)
  494. {
  495. switch($input_format)
  496. {
  497. case 'MINUTES':
  498. $time_in_seconds = $time * 60;
  499. break;
  500. case 'SECONDS':
  501. default:
  502. $time_in_seconds = $time;
  503. break;
  504. }
  505. if($round_to > 0)
  506. {
  507. $time_in_seconds += $round_to - ($time_in_seconds % $round_to);
  508. }
  509. $formatted_time = array();
  510. if($time_in_seconds > 0)
  511. {
  512. $time_r = array();
  513. $time_r[0] = array(floor($time_in_seconds / 3600), 'Hour');
  514. $time_r[1] = array(floor(($time_in_seconds % 3600) / 60), 'Minute');
  515. $time_r[2] = array($time_in_seconds % 60, 'Second');
  516. foreach($time_r as $time_segment)
  517. {
  518. if($time_segment[0] > 0)
  519. {
  520. $formatted_part = $time_segment[0];
  521. if($standard_version)
  522. {
  523. $formatted_part .= ' ' . $time_segment[1];
  524. if($time_segment[0] > 1)
  525. {
  526. $formatted_part .= 's';
  527. }
  528. }
  529. else
  530. {
  531. $formatted_part .= strtolower(substr($time_segment[1], 0, 1));
  532. }
  533. array_push($formatted_time, $formatted_part);
  534. }
  535. }
  536. }
  537. return implode(($standard_version ? ', ' : null), $formatted_time);
  538. }
  539. public static function days_ago_format_string($days_ago)
  540. {
  541. if($days_ago < 30)
  542. {
  543. $days_ago .= ' day' . ($days_ago > 1 ? 's': null);
  544. }
  545. else
  546. {
  547. $days_ago = floor($days_ago / 30);
  548. if($days_ago >= 12)
  549. {
  550. $year = floor($days_ago / 12);
  551. $months = $days_ago % 12;
  552. $days_ago = $year . ' year' . ($year > 1 ? 's': null);
  553. if($months > 0)
  554. {
  555. $days_ago .= ', ' . $months . ' month' . ($months > 1 ? 's': null);
  556. }
  557. }
  558. else
  559. {
  560. $days_ago = $days_ago . ' month' . ($days_ago > 1 ? 's': null);
  561. }
  562. }
  563. return $days_ago;
  564. }
  565. public static function time_stamp_to_string($time_stamp, $string_type)
  566. {
  567. $stamp_half = explode(' ', trim($time_stamp));
  568. $date = explode('-', $stamp_half[0]);
  569. $time = explode(':', (isset($stamp_half[1]) ? $stamp_half[1] : '00:00:00'));
  570. return date($string_type, mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]));
  571. }
  572. public static function system_category_to_openbenchmark_category($category)
  573. {
  574. $categories = array('Graphics' => 'GPU', 'Processor' => 'CPU', 'System' => 'CPU', 'File-System' => 'File System');
  575. return isset($categories[$category]) ? $categories[$category] : $category;
  576. }
  577. }
  578. ?>