-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutil.php
137 lines (114 loc) · 4.13 KB
/
util.php
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
Brainfuck interpreter in PHP
Copyright (C) 2002 Daniel Lorch
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* fuck_text() generates brainfuck code from $text. The resulting code will use the current
* register p for looping and the register p+1 for the resulting character. Thus, make sure
* these two registers are zero (prepend "[-]>[-]<<" to clear the first two registers).
*
* I suggest you to use this function in conjunction with wordwrap:
*
* $bf = wordwrap(fuck_text("Hello World"), 75, "\n", 1));
*
* wich will generate nice, formatted output.
*
* @param string $text
*/
function fuck_text($text)
{
/* value of current pointer */
$value = 0;
$result = '';
for ($_t = 0; $_t < strlen($text); ++$_t) {
/* ordinal difference between current char and the one we want to have */
$diff = ord($text[$_t]) - $value;
/* it's easier like this than always computing this value - saves some cpu cycles*/
$value = ord($text[$_t]);
/* repeat current character */
if ($diff == 0) {
$result .= ">.<";
continue;
}
/* is it worth making a loop?
No. A bunch of + or - consume less space than the loop. */
if (abs($diff) < 10) {
/* output a bunch of + or - */
if ($diff > 0)
$result .= ">" . str_repeat("+", $diff);
else if ($diff < 0)
$result .= ">" . str_repeat("-", abs($diff));
} /* Yes, create a loop. This will make the resulting code more compact. */
else {
/* we strictly use ints, as PHP has some bugs with floating point operations
(even if no division is involved) */
$loop = (int)sqrt(abs($diff));
/* set loop counter */
$result .= str_repeat("+", $loop);
/* execute loop, then add reminder */
if ($diff > 0) {
$result .= "[->" . str_repeat("+", $loop) . "<]";
$result .= ">" . str_repeat("+", $diff - pow($loop, 2));
} else if ($diff < 0) {
$result .= "[->" . str_repeat("-", $loop) . "<]";
$result .= ">" . str_repeat("-", abs($diff) - pow($loop, 2));
}
} /* end: if loop */
$result .= ".<";
} /* end: for */
/* cleanup */
return str_replace("<>", "", $result);
}
/**
* This function checks whether the brackets are balanced.
*
* Make sure you get the return value correctly, as 0 means "OK" and not "false":
*
* positive return value: this many [ too much = this many ] missing
* zero: balanced
* negative return value: this many ] too much = this many [ missing
*
* @param string $bf Brainfuck source
* @return int
*/
function bf_brackets_balance($bf)
{
$histogram = count_chars($bf);
return $histogram[91] - $histogram[93];
}
/**
* This returns the real number of brainfuck commands when all other characters
* are ignored.
*
* @param string $bf
* @return int
*/
function bf_count_chars($bf)
{
return count(preg_split("/[\[\]+\-<>.,]/", $bf)) - 1;
}
/**
* removes all unnecessary characters from a brainfuck source
*
* only actual instructions are kept
*
* @param string $bf
* @return string
*/
function bf_reduce($bf)
{
preg_match_all("/[\[\]+\-<>.,]/", $bf, $matches);
return join("", $matches[0]);
}