This repository was archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary-search.php
More file actions
112 lines (97 loc) · 2.77 KB
/
Copy pathbinary-search.php
File metadata and controls
112 lines (97 loc) · 2.77 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Binary_Search class
*
* @package DynaMo
*/
namespace WP_Syntex\DynaMo;
/**
* Implements the search algorithm using a binary search, useful when no hash table is provided.
* See dcigettext.c in GNU gettext.
*
* @since 1.0
*/
class Binary_Search implements Search_Handler {
/**
* Stream handle.
*
* @var resource
*/
protected $handle;
/**
* An array containing the length and position of the original strings.
*
* @var \SplFixedArray<int>
*/
protected $originals_table;
/**
* An array containing the length and position of the translated strings.
*
* @var \SplFixedArray<int>
*/
protected $translations_table;
/**
* The total count of translated strings.
*
* @var int
*/
protected $total;
/**
* An array to store strings already read.
*
* @var string[]
*/
private $originals;
/**
* Constructor
*
* @since 1.0
*
* @param resource $handle Stream handle.
* @param \SplFixedArray<int> $originals_table An array containing the length and position of the original strings.
* @param \SplFixedArray<int> $translations_table An array containing the length and position of the translated strings.
* @param int $total The total count of translated strings.
*/
public function __construct( $handle, $originals_table, $translations_table, $total ) {
$this->handle = $handle;
$this->originals_table = $originals_table;
$this->translations_table = $translations_table;
$this->total = $total;
}
/**
* Returns the translation(s) given an original key.
*
* @since 1.0
*
* @param string $key The key of the string to translate (includes the context and singular string).
* @return string|false
*/
public function get_translation( $key ) {
$left = 0;
$right = $this->total;
while ( $left < $right ) {
$pos = (int) ( ( $left + $right ) / 2 );
$i = $pos * 2; // Due to position and length of strings stored alternately in the same flat array.
if ( ! isset( $this->originals[ $pos ] ) ) {
if ( $this->originals_table[ $i ] > 0 ) {
\fseek( $this->handle, (int) $this->originals_table[ $i + 1 ] );
$original = (string) \fread( $this->handle, (int) $this->originals_table[ $i ] );
$parts = explode( "\0", $original ); // Remove the plural forms for the comparison with the searched key.
$this->originals[ $pos ] = $parts[0];
} else {
$this->originals[ $pos ] = '';
}
}
$cmp = \strcmp( $key, $this->originals[ $pos ] );
if ( $cmp < 0 ) {
$right = $pos;
} elseif ( $cmp > 0 ) {
$left = $pos + 1;
} else {
\fseek( $this->handle, (int) $this->translations_table[ $i + 1 ] );
return \fread( $this->handle, (int) $this->translations_table[ $i ] );
}
}
return false;
}
}