-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.php
More file actions
90 lines (70 loc) · 2.53 KB
/
binary.php
File metadata and controls
90 lines (70 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function search($needle, $dir) {
$splitter = '\x0A';
$splitter2 = '\t';
$step = 1000000;
$remain = '';
$handle = fopen($dir, 'r');
while (!feof($handle)) {
$content = fread($handle, $step);
$lastIndex = strrpos($content, $splitter);
$lastPart = substr($content, $lastIndex - $step);
$content = substr($content, 0, $lastIndex + 4 - $step);
$firstIndex = strpos($content, $splitter);
$firstPart = substr($content, 0, $firstIndex + 4);
$content = substr($content, $firstIndex + 4);
$remain .= $firstPart . $lastPart;
$content = explode($splitter, $content);
array_pop($content);
for($i = 0; $i < count($content); $i++) {
$content[$i] = explode($splitter2, $content[$i]);
}
$less = 0;
$more = count($content) - 1;
if (strnatcmp($content[$more][0], $needle) > 0) {
while($less <= $more) {
$middle = $less + floor(($more - $less) / 2);
$compare = strnatcmp($content[$middle][0], $needle);
if (strnatcmp($content[$less][0], $needle) === 0) {
return $content[$less][1];
}
if (strnatcmp($content[$more][0], $needle) === 0) {
return $content[$more][1];
}
if ($compare > 0) {
$more = $middle - 1;
} elseif ($compare < 0) {
$less = $middle + 1;
} elseif ($compare === 0) {
return $content[$middle][1];
}
}
}
}
$remain = substr($remain, 4);
$content = $remain;
$content = explode($splitter, $content);
array_pop($content);
for($i = 0; $i < count($content); $i++) {
$content[$i] = explode($splitter2, $content[$i]);
}
$less = 0;
$more = count($content) - 1;
while($less <= $more) {
$middle = $less + floor(($more - $less) / 2);
$compare = strnatcmp($content[$middle][0], $needle);
if (strnatcmp($content[$less][0], $needle) === 0) {
return $content[$less][1];
}
if (strnatcmp($content[$more][0], $needle) === 0) {
return $content[$more][1];
}
if ($compare > 0) {
$more = $middle - 1;
} elseif ($compare < 0) {
$less = $middle + 1;
} elseif ($compare === 0) {
return $content[$middle][1];
}
}
return 'undef';
}