• This repository has been archived on 07/Dec/2018
  • Stars
    star
    507
  • Rank 87,068 (Top 2 %)
  • Language
    PHP
  • Created over 13 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A PHP implementation of a Fine granularity Diff engine: Diff can be computed up to character-level
Files
-----

finediff.php    : where the FineDiff class is defined, standalone, no dependencies

viewdiff-ex.php : demo page, render diff in HTML, show opcodes and other stats
sample_from.txt : sample text file for demo
sample_to.txt   : sample text file for demo
[Text_Diff]     : required for viewdiff-ex.php demo page,
                  available at http://download.pear.php.net/package/Text_Diff-1.1.1.tgz

viewdiff.php    : demo page, plainly render diff in HTML

Demo can be seen online at:
http://www.raymondhill.net/finediff/


Main Class: FineDiff
--------------------

A class which implements a high-granularity (though selectable) diff engine.
Up to character-level diffs can be computed.

A diff is described by a string of opcodes, which can be stored
and combined later with the left hand string to re-create the
right-hand string.

The code was started from scratch, with particular attention to
performance. The key to the performance of the FineDiff engine is
to incrementally increase the granularity.


Usage
-----

The simplest way to create a diff of two strings is as follow:

  include 'finediff.php';
  $opcodes = FineDiff::getDiffOpcodes($from_text, $to_text /, default granularity is set to character */);
  // store opcodes for later use...

Later, $to_text can be re-created from $from_text using $opcodes as follow:

  include 'finediff.php';
  $to_text = FineDiff::renderToTextFromOpcodes($from_text, $opcodes);

If you wish a different granularity from the default one, you can use
one of the provided stock granularity stacks:

  FineDiff::$paragraphGranularity
  FineDiff::$sentenceGranularity
  FineDiff::$wordGranularity
  FineDiff::$characterGranularity (default)

A basic HTML renderer is provided:

  echo FineDiff::renderDiffToHTMLFromOpcodes($from_text, $opcodes);


Customize
---------

It is possible to customize the engine by providing a custom "granularity stack"
at your own risk.

It is also possible to provide a custom renderer through a user supplied callback
function/method:

  FineDiff::renderFromOpcodes($from, $opcodes, $callback);


FAQ
---

* Does it work with UTF-8?
  As of now, the code assume single-byte characters. To use UTF-8 text, you can
  always convert the encoding using mb_convert_encoding():
    ...
    $from_text = mb_convert_encoding($from_text_utf8, 'HTML-ENTITIES', 'UTF-8');
    $to_text = mb_convert_encoding($to_text_utf8, 'HTML-ENTITIES', 'UTF-8');
    $diff_opcodes = FineDiff::getDiffOpcodes($from_text, $to_text);
    ...

  If ever you want to re-generate the $to_text_utf8 from the $from_text_utf8:
    ...
    $from_text = mb_convert_encoding($from_text_utf8, 'HTML-ENTITIES', 'UTF-8');
    $to_text = FineDiff::renderToTextFromOpcodes($from_text, $diff_opcodes);
    $to_text_utf8 = mb_convert_encoding($to_text, 'UTF-8', 'HTML-ENTITIES');
    ....	


License
-------

Copyright (c) 2011 Raymond Hill (http://raymondhill.net/blog/?p=441)

Licensed under The MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

More Repositories

1

uBlock

uBlock Origin - An efficient blocker for Chromium and Firefox. Fast and lean.
JavaScript
45,548
star
2

uMatrix

uMatrix: Point and click matrix to filter net requests according to source, destination and type
JavaScript
4,552
star
3

httpswitchboard

Point & click to forbid/allow any class of requests made by your browser. Use it to block scripts, iframes, ads, facebook, etc.
JavaScript
1,328
star
4

Javascript-Voronoi

A Javascript implementation of Fortune's algorithm to compute Voronoi cells
JavaScript
1,013
star
5

cronexpr

Cron expression parser in Go language (golang)
Go
682
star
6

uBO-Extra

A companion extension to uBlock Origin
JavaScript
655
star
7

uBO-Scope

A tool to measure over time your own exposure to third parties on the web
JavaScript
252
star
8

uBlock-for-firefox-legacy

uBlock Origin for Firefox legacy-based browsers.
JavaScript
200
star
9

publicsuffixlist.js

A JavaScript utility to make use of Mozilla Foundation's Public Suffix List
JavaScript
92
star
10

ccaptioner

An extension to assign a text track to a video element in a web page
JavaScript
62
star
11

yamd5.js

Yet another javascript MD5 hasher: Fastest out there (for not-small strings). Support for Unicode strings. Natively incremental.
JavaScript
60
star
12

lz4-wasm

LZ4 block format encoder/decoder: a WebAssembly implementation
WebAssembly
58
star
13

sessbench

Browser session benchmarker
JavaScript
52
star
14

jigsawpuzzle-rhill

Jigsaw puzzle game in Javascript
JavaScript
50
star
15

chromium-websocket-wrapper

A WebSocket wrapper to expose websocket connection attempts to the chrome.webRequest API
JavaScript
37
star
16

pageloadspeed

A simple benchmark tool to measure page load speed
HTML
34
star
17

Javascript-Text-Highlighter

Text highlight in Javascript / Can highlight across HTML tags
JavaScript
34
star
18

cablegatesearch.net

Cablegate's cables: Full-text search web site
PHP
33
star
19

efatmarker

This javascript library allows you to add a text highlighter to your web pages, which gives the users of your site the ability to share with others their own highlighted text passages on your web pages.
JavaScript
30
star
20

rayoid

Atari ST game I wrote end of 1992: 1 player = asteroids-like arcade game, 2-player via modem = real-time strategy-like game
18
star
21

paxml2kpxxml

Password Agent XML file to Keepass X XML file
Perl
12
star
22

jsawpuzzle

A browser extension to create and solve jigsaw puzzles
JavaScript
11
star
23

obj-vs-set-vs-map

Just a benchmark to measure performance of Set(), Map() versus Object.create(null)
HTML
11
star