Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92601063
PhutilProseDiff.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Nov 21, 21:47
Size
1 KB
Mime Type
text/x-php
Expires
Sat, Nov 23, 21:47 (2 d)
Engine
blob
Format
Raw Data
Handle
22467772
Attached To
rPHU libphutil
PhutilProseDiff.php
View Options
<?php
final
class
PhutilProseDiff
extends
Phobject
{
private
$parts
=
array
();
public
function
addPart
(
$type
,
$text
)
{
$this
->
parts
[]
=
array
(
'type'
=>
$type
,
'text'
=>
$text
,
);
return
$this
;
}
public
function
getParts
()
{
return
$this
->
parts
;
}
public
function
reorderParts
()
{
// Reorder sequences of removed and added sections to put all the "-"
// parts together first, then all the "+" parts together. This produces
// a more human-readable result than intermingling them.
$o_run
=
array
();
$n_run
=
array
();
$result
=
array
();
foreach
(
$this
->
parts
as
$part
)
{
$type
=
$part
[
'type'
];
switch
(
$type
)
{
case
'-'
:
$o_run
[]
=
$part
;
break
;
case
'+'
:
$n_run
[]
=
$part
;
break
;
default
:
foreach
(
$o_run
as
$o
)
{
$result
[]
=
$o
;
}
foreach
(
$n_run
as
$n
)
{
$result
[]
=
$n
;
}
$result
[]
=
$part
;
$o_run
=
array
();
$n_run
=
array
();
break
;
}
}
foreach
(
$o_run
as
$o
)
{
$result
[]
=
$o
;
}
foreach
(
$n_run
as
$n
)
{
$result
[]
=
$n
;
}
// Now, combine consecuitive runs of the same type of change (like a
// series of "-" parts) into a single run.
$combined
=
array
();
$last
=
null
;
$last_text
=
null
;
foreach
(
$result
as
$part
)
{
$type
=
$part
[
'type'
];
if
(
$last
!==
$type
)
{
$combined
[]
=
array
(
'type'
=>
$last
,
'text'
=>
$last_text
,
);
$last_text
=
null
;
$last
=
$type
;
}
$last_text
.=
$part
[
'text'
];
}
if
(
$last_text
!==
null
)
{
$combined
[]
=
array
(
'type'
=>
$last
,
'text'
=>
$last_text
,
);
}
$this
->
parts
=
$combined
;
return
$this
;
}
}
Event Timeline
Log In to Comment