I can recommend to take a look at Neil Fraser’s code and articles:
Currently available in Java,
JavaScript, C++ and Python. Regardless
of language, each library features the
same API and the same functionality.
All versions also have comprehensive
test harnesses.
Neil Fraser: Diff Strategies – for theory and implementation notes