FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hack-codegen/lib/utils.php at master · ayoe/hack-codegen · GitHub
ayoe
/
hack-codegen
Public
forked from
hhvm/hack-codegen
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
hack-codegen
/
lib
/
utils.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
267 lines (237 loc) · 5.65 KB
Breadcrumbs
hack-codegen
/
lib
/
utils.php
Copy path
File metadata and controls
267 lines (237 loc) · 5.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<
?
hh
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
namespace
Facebook\HackCodegen
;
/**
* Remove the 'HH\' prefix from typehint strings
* and from strings produced by var_export().
*/
function
strip_hh_prefix
(
string
$str
,
bool
$nonobject_types_only
=
false
):
string
{
if
(
!
is_int
(
stripos
(
$str
,
'
HH
\\
'
))) {
//
Bail out early if $str doesn't contain 'HH\'
return
$str
;
}
$nonobject_types
=
ImmSet
{
'
bool
'
,
'
boolean
'
,
'
int
'
,
'
integer
'
,
'
float
'
,
'
double
'
,
'
real
'
,
'
num
'
,
'
string
'
,
'
resource
'
,
'
mixed
'
,
'
void
'
,
'
this
'
,
'
arraykey
'
,
};
$len
=
strlen
(
$str
);
$in_literal
=
'
'
;
$out
=
'
'
;
$c
=
'
'
;
for
(
$i
=
0
;
$i
<
$len
;
++
$i
) {
$prev
=
$c
;
$c
=
$str
[
$i
];
if
(
$in_literal
) {
if
(
$c
===
'
\\
'
) {
$out
.=
$c
;
++
$i
;
if
(
$i
>=
$len
)
break
;
$c
=
$str
[
$i
];
$out
.=
$c
;
continue
;
}
if
(
$c
===
$in_literal
) {
$in_literal
=
'
'
;
}
}
else
{
if
((
$c
===
'
H
'
||
$c
===
'
h
'
)
&&
strtoupper
(
substr
(
$str
,
$i
,
3
))
===
"
HH
\\
"
&&
!
ctype_alnum
(
$prev
)
&&
$prev
!==
'
_
'
&&
$prev
!==
'
\\
'
) {
if
(
$nonobject_types_only
) {
$sub
=
substr
(
$str
,
$i
+
3
,
9
);
$sub_len
=
strlen
(
$sub
);
$k
=
0
;
for
(;
$k
<
$sub_len
;
++
$k
) {
$sub_c
=
$sub
[
$k
];
if
(
!
ctype_alnum
(
$sub_c
)
&&
$sub_c
!==
'
_
'
&&
$sub_c
!==
'
\\
'
) {
break
;
}
}
$sub
=
strtolower
(
substr
(
$sub
,
0
,
$k
));
$strip
=
(
$nonobject_types
->
contains(
$sub
));
}
else
{
$strip
=
true
;
}
if
(
$strip
) {
$i
+=
2
;
$c
=
'
\\
'
;
continue
;
}
}
if
(
$c
===
'
\'
'
||
$c
===
'
"
'
) {
$in_literal
=
$c
;
}
}
$out
.=
$c
;
}
return
$out
;
}
function
difference_render_fast
(
$old
,
$new
) {
//
UNSAFE_BLOCK
//
split the source text into arrays of lines
$t1
=
explode
(
"
\n
"
,
$old
);
$x
=
array_pop
(
$t1
);
if
(
$x
>
'
'
) {
$t1
[]
=
"
$x
\n\\
No newline at end of file
"
;
}
$t2
=
explode
(
"
\n
"
,
$new
);
$x
=
array_pop
(
$t2
);
if
(
$x
>
'
'
) {
$t2
[]
=
"
$x
\n\\
No newline at end of file
"
;
}
//
build a reverse-index array using the line as key and line number as
//
value don't store blank lines, so they won't be targets of the shortest
//
distance search
foreach
(
$t1
as
$i
=>
$x
) {
if
(
$x
>
'
'
) {
$r1
[
$x
][]
=
$i
;
}
}
foreach
(
$t2
as
$i
=>
$x
) {
if
(
$x
>
'
'
) {
$r2
[
$x
][]
=
$i
;
}
}
//
start at beginning of each list
$a1
=
0
;
$a2
=
0
;
$actions
=
array
();
//
walk this loop until we reach the end of one of the lists
while
(
$a1
<
count
(
$t1
)
&&
$a2
<
count
(
$t2
)) {
//
if we have a common element, save it and go to the next
if
(
$t1
[
$a1
]
==
$t2
[
$a2
]) {
$actions
[]
=
4
;
$a1
++
;
$a2
++
;
continue
;
}
//
otherwise, find the shortest move (Manhattan-distance) from the
//
current location
$best1
=
count
(
$t1
);
$best2
=
count
(
$t2
);
$s1
=
$a1
;
$s2
=
$a2
;
while
((
$s1
+
$s2
-
$a1
-
$a2
)
<
(
$best1
+
$best2
-
$a1
-
$a2
)) {
$d
=
-
1
;
foreach
((
array
) @
$r1
[
$t2
[
$s2
]]
as
$n
) {
if
(
$n
>=
$s1
) {
$d
=
$n
;
break
;
}
}
if
(
$d
>=
$s1
&&
(
$d
+
$s2
-
$a1
-
$a2
)
<
(
$best1
+
$best2
-
$a1
-
$a2
)) {
$best1
=
$d
;
$best2
=
$s2
;
}
$d
=
-
1
;
foreach
((
array
) @
$r2
[
$t1
[
$s1
]]
as
$n
) {
if
(
$n
>=
$s2
) {
$d
=
$n
;
break
;
}
}
if
(
$d
>=
$s2
&&
(
$s1
+
$d
-
$a1
-
$a2
)
<
(
$best1
+
$best2
-
$a1
-
$a2
)) {
$best1
=
$s1
;
$best2
=
$d
;
}
$s1
++
;
$s2
++
;
}
//
deleted elements
while
(
$a1
<
$best1
) {
$actions
[]
=
1
;
$a1
++
;
}
//
added elements
while
(
$a2
<
$best2
) {
$actions
[]
=
2
;
$a2
++
;
}
}
//
we've reached the end of one list, now walk to the end of the other
//
deleted elements
while
(
$a1
<
count
(
$t1
)) {
$actions
[]
=
1
;
$a1
++
;
}
//
added elements
while
(
$a2
<
count
(
$t2
)) {
$actions
[]
=
2
;
$a2
++
;
}
//
and this marks our ending point
$actions
[]
=
8
;
//
now, let's follow the path we just took and report the added/deleted
//
elements into $out.
$op
=
0
;
$x0
=
$x1
=
0
;
$y0
=
$y1
=
0
;
$out
=
array
();
foreach
(
$actions
as
$act
) {
if
(
$act
==
1
) {
$op
|=
$act
;
$x1
++
;
continue
;
}
if
(
$act
==
2
) {
$op
|=
$act
;
$y1
++
;
continue
;
}
if
(
$op
>
0
) {
$xstr
=
(
$x1
==
(
$x0
+
1
)) ?
$x1
: (
$x0
+
1
)
.
"
,
$x1
"
;
$ystr
=
(
$y1
==
(
$y0
+
1
)) ?
$y1
: (
$y0
+
1
)
.
"
,
$y1
"
;
if
(
$op
==
1
) {
$out
[]
=
"
{$xstr}
d
{$y1}
"
;
}
else
if
(
$op
==
3
) {
$out
[]
=
"
{$xstr}
c
{$ystr}
"
;
}
//
deleted elems
while
(
$x0
<
$x1
) {
$out
[]
=
'
-
'
.
$t1
[
$x0
];
$x0
++
;
}
if
(
$op
==
2
) {
$out
[]
=
"
{$x1}
a
{$ystr}
"
;
}
else
if
(
$op
==
3
) {
$out
[]
=
'
---
'
;
}
//
added elems
while
(
$y0
<
$y1
) {
$out
[]
=
'
+
'
.
$t2
[
$y0
];
$y0
++
;
}
}
$x1
++
;
$x0
=
$x1
;
$y1
++
;
$y0
=
$y1
;
$op
=
0
;
}
$out
[]
=
'
'
;
return
join
(
"
\n
"
,
$out
);
}
/**
* Returns the first argument which is not strictly null, or `null' if there
* are no such arguments.
*/
function
coalesce
(...
$args
) {
foreach
(
$args
as
$arg
) {
if
(
$arg
!==
null
) {
return
$arg
;
}
}
return
null
;
}
Back
|
FazBrowse Home
|
New Git URL