PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/rxwandc/application/stat.rxwan.com/classes/model/stringhash.php

https://bitbucket.org/i1598/caiyun_stat
PHP | 32 lines | 23 code | 9 blank | 0 comment | 1 complexity | b86bd6d5b1169e9e00acf7873f606ebc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. defined('SYSPATH') or die('No direct script access.');
  3. class Model_Stringhash extends Model{
  4. public function getHashValue($str) {
  5. $bytes = $this->stringToByteArray($str);
  6. return array_reduce($bytes, array($this, 'hash'));
  7. }
  8. public function hash($p, $v) {
  9. return (($p << 5) - $p) + $v;
  10. }
  11. public function stringToByteArray($str) {
  12. preg_match_all('/(.)/s', $str, $bytes);
  13. $bytes=array_map(array($this, 'toByte'), $bytes[1]);
  14. return $bytes;
  15. }
  16. public function toByte($input) {
  17. $num = ord($input);
  18. if($num > 0x7F) {
  19. return 0 - (~($num - 1) & 0x7F);
  20. }
  21. return $num;
  22. }
  23. }