Skip to content

Commit cb3b1e6

Browse files
committed
Library reorganization and cleanup
This rearranges things to get this into slightly better shape and includes a basic makefile with libdmp and test targets so it should be a little easier to get up and running quickly. I try to be really clear about the early state of things.
1 parent 4b28177 commit cb3b1e6

File tree

10 files changed

+241
-117
lines changed

10 files changed

+241
-117
lines changed

Makefile

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Silly Makefile to build libdmp.a and test_dmp executable
2+
3+
PLATFORM=$(shell uname -s)
4+
5+
MINGW=0
6+
ifneq (,$(findstring MINGW32,$(PLATFORM)))
7+
MINGW=1
8+
endif
9+
ifneq (,$(findstring mingw,$(CROSS_COMPILE)))
10+
MINGW=1
11+
endif
12+
13+
rm=rm -f
14+
AR=ar cq
15+
RANLIB=ranlib
16+
17+
LIBNAME=libdmp.a
18+
19+
ifeq ($(MINGW),1)
20+
CC=gcc
21+
else
22+
CC=cc
23+
endif
24+
25+
INCLUDES=-Isrc -Iinclude
26+
27+
DEFINES= $(INCLUDES) -DSTDC -D_GNU_SOURCE $(EXTRA_DEFINES)
28+
CFLAGS= -g $(DEFINES) -Wall -Wextra -Wno-missing-field-initializers -std=c99 -O2 $(EXTRA_CFLAGS)
29+
30+
SRCS = $(wildcard src/*.c)
31+
32+
ifeq ($(MINGW),1)
33+
DEFINES += -DWIN32 -D_WIN32_WINNT=0x0501 -D__USE_MINGW_ANSI_STDIO=1
34+
else
35+
CFLAGS += -fPIC
36+
endif
37+
38+
OBJS = $(patsubst %.c,%.o,$(SRCS))
39+
40+
%.c.o:
41+
$(CC) $(CFLAGS) -c $*.c
42+
43+
default: $(LIBNAME)
44+
45+
$(LIBNAME): $(OBJS)
46+
$(rm) $@
47+
$(AR) $@ $(OBJS)
48+
$(RANLIB) $@
49+
50+
TESTSRCS = $(wildcard test/*.c)
51+
52+
test: dmp_test
53+
54+
dmp_test: $(LIBNAME) include/dmp.h $(TESTSRCS)
55+
$(CC) -o dmp_test $(CFLAGS) $(TESTSRCS) -L. -ldmp
56+
57+
clean:
58+
$(rm) -rf $(OBJS) $(LIBNAME) dmp_test *.dSYM

README.md

+84-49
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,95 @@ Port of google-diff-match-patch to C
22
====================================
33

44
This is a C language port of Neil Fraser's google-diff-match-patch code.
5-
His original code is available at:
65

7-
http://code.google.com/p/google-diff-match-patch/
8-
9-
That original code is Copyright (c) 2006 Google Inc. and licensed
10-
under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
11-
Please see the APACHE-LICENSE-2.0 file included with this code
12-
for details.
13-
14-
This code is available at:
15-
16-
https://github.com/arrbee/diff-match-patch-c/
17-
18-
It is Copyright (c) 2012 Russell Belfer <[email protected]> and licensed
19-
under the MIT License. See the included LICENSE file.
6+
Right now, this is focused on the `diff` part of `diff-match-patch`. It
7+
contains APIs to compare two blocks on text and return a structure
8+
containing the list of differences (as shared, inserted, and deleted
9+
sections).
10+
11+
Getting Started
12+
---------------
13+
14+
This library is in an early state, so be prepared to have to bend it to
15+
your will a bit. Public APIs are declared in `include/dmp.h`. A basic
16+
Makefile is included.
17+
18+
```sh
19+
$ make
20+
cc -g -Isrc -Iinclude -DSTDC -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -std=c99 -O2 -fPIC -c -o src/dmp.o src/dmp.c
21+
cc -g -Isrc -Iinclude -DSTDC -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -std=c99 -O2 -fPIC -c -o src/dmp_pool.o src/dmp_pool.c
22+
rm -f libdmp.a
23+
ar cq libdmp.a src/dmp.o src/dmp_pool.o
24+
ranlib libdmp.a
25+
26+
$ make test
27+
cc -o dmp_test -g -Isrc -Iinclude -DSTDC -D_GNU_SOURCE -Wall -Wextra -Wno-missing-field-initializers -std=c99 -O2 -fPIC test/dmp_test.c test/dmp_test_internals.c -L. -ldmp
28+
29+
$ ./dmp_test
30+
...done
31+
..done
32+
...................
33+
> "ax\x09"
34+
-"a", +"\xda\x80", ="x", -"\x09", +"\x00"
35+
< "\xda\x80x\x00"
36+
.
37+
> "1ayb2"
38+
-"1", ="a", -"y", ="b", -"2", +"xab"
39+
< "abxab"
40+
.
41+
> "abcy"
42+
+"xaxcx", ="abc", -"y"
43+
< "xaxcxabc"
44+
.done
45+
```
2046

21-
Example Usage
22-
-------------
47+
Example API Usage
48+
-----------------
2349

2450
All functions and structures used in this library are prefixed with
2551
`dmp_`. To generate a diff, you use a function to create a `dmp_diff`
2652
object which you can then access and manipulate via other functions.
2753

28-
Here is a silly little example that counts the total length of the
29-
"equal" runs from the diff.
54+
Here is a silly little example that counts the total length of the "equal"
55+
runs from the diff.
56+
3057
```c
31-
{
32-
dmp_diff *diff;
33-
int eq = 0;
34-
35-
if (dmp_diff_from_strs(&diff, NULL, "string 1", "string 2") != 0)
36-
handle_error();
37-
38-
dmp_diff_foreach(diff, how_equal, &eq);
39-
printf("Strings had %d equal bytes\n", eq);
40-
41-
dmp_diff_free(diff);
42-
}
43-
44-
int how_equal(
45-
void *ref, dmp_operation_t op, const void *data, uint32_t len)
46-
{
47-
int *sum = ref;
48-
if (op == DMP_DIFF_EQUAL)
49-
(*sum) += len;
50-
return 0;
51-
}
58+
{
59+
dmp_diff *diff;
60+
int eq = 0;
61+
62+
if (dmp_diff_from_strs(&diff, NULL, "string 1", "string 2") != 0)
63+
handle_error();
64+
65+
dmp_diff_foreach(diff, how_equal, &eq);
66+
printf("Strings had %d equal bytes\n", eq);
67+
68+
dmp_diff_free(diff);
69+
}
70+
71+
int how_equal(
72+
void *ref, dmp_operation_t op, const void *data, uint32_t len)
73+
{
74+
int *sum = ref;
75+
if (op == DMP_DIFF_EQUAL)
76+
(*sum) += len;
77+
return 0;
78+
}
5279
```
5380
5481
This shows the basic pattern of diff API usage:
82+
5583
1. Generate a diff
5684
2. Process the diff in some way
5785
3. Free the diff
5886
5987
Diff API
6088
--------
6189
62-
All public functions in the library that could fail return an `int`
63-
and will return 0 for success or -1 for failure. Functions which
64-
cannot fail will either have a void return or will return a specific
65-
other data type if they are simple data lookups.
90+
All public functions in the library that could fail return an `int` and
91+
will return 0 for success or -1 for failure. Functions which cannot fail
92+
will either have a void return or will return a specific other data type
93+
if they are simple data lookups.
6694
6795
Here are the main functions for generating and accessing diffs:
6896
@@ -152,15 +180,22 @@ extern int dmp_diff_foreach(
152180
Status
153181
------
154182

155-
At this point, the basic diff code works, although I haven't implemented all
156-
of the optimizations yet. I haven't written any of the diff formatting
157-
helpers from the original library yet, nor have I started on the match or
158-
patch related code yet.
183+
The library is currently at version **0.1.1**. There has only really been
184+
one iteration on the core functionality and then one minor update to
185+
reorganize and clean things up a bit.
186+
187+
At this point, the basic Myers diff code works, although I haven't
188+
implemented all of the optimizations from the upstream library yet. I
189+
haven't written any of the diff formatting helpers from the original
190+
library yet, nor have I started on the match or patch related code yet.
159191

160192
Copyright and License
161193
---------------------
162194

163-
The original Google Diff, Match and Patch Library is licensed under
195+
Copyright
196+
---------
197+
198+
The original **Google Diff, Match and Patch Library** is licensed under
164199
the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
165200
The full terms of that license are included here in the
166201
`APACHE-LICENSE-2.0` file.
@@ -177,7 +212,7 @@ the Expat License) which is included here in the `LICENSE` file.
177212

178213
C version of Diff, Match and Patch Library
179214

180-
Copyright (c) 2012 Russell Belfer <[email protected]>
215+
Copyright (c) Russell Belfer <[email protected]>
181216
<http://github.com/arrbee/google-diff-match-patch-c/>
182217

183218
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:

src/dmp.h renamed to include/dmp.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Original library is Copyright (c) 2006 Google Inc.
77
* http://code.google.com/p/google-diff-match-patch/
88
*
9-
* Copyright (c) 2012 Russell Belfer <[email protected]>
9+
* Copyright (c) Russell Belfer <[email protected]>
1010
* https://github.com/arrbee/google-diff-match-patch-c/
1111
*
1212
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -30,6 +30,8 @@
3030
#ifndef INCLUDE_H_dmp
3131
#define INCLUDE_H_dmp
3232

33+
#define DMP_VERSION "0.1.1"
34+
3335
#include <string.h>
3436
#include <stdint.h>
3537
#include <stdio.h>

src/dmp.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Original library is Copyright (c) 2006 Google Inc.
77
* http://code.google.com/p/google-diff-match-patch/
88
*
9-
* Copyright (c) 2012 Russell Belfer <[email protected]>
9+
* Copyright (c) Russell Belfer <[email protected]>
1010
* https://github.com/arrbee/google-diff-match-patch-c/
1111
*
1212
* See included LICENSE file for license details.
@@ -276,7 +276,7 @@ static int diff_bisect(
276276
front = (delta % 2 != 0);
277277
k1start = k1end = k2start = k2end = 0;
278278

279-
if (diff->v_alloc < v_length) {
279+
if ((int)diff->v_alloc < v_length) {
280280
size_t asize = v_length * sizeof(int);
281281
diff->v1 = diff->v1 ? realloc(diff->v1, asize) : malloc(asize);
282282
diff->v2 = diff->v2 ? realloc(diff->v2, asize) : malloc(asize);
@@ -300,7 +300,8 @@ static int diff_bisect(
300300

301301
/* advance the front contour */
302302
for (k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
303-
int k1off = v_offset + k1, x1, y1;
303+
int k1off = v_offset + k1;
304+
uint32_t x1, y1;
304305

305306
if (k1 == -d || (k1 != d && v1[k1off - 1] < v1[k1off + 1]))
306307
x1 = v1[k1off + 1];
@@ -320,7 +321,7 @@ static int diff_bisect(
320321
int k2off = v_offset + delta - k1;
321322
if (k2off >= 0 && k2off < v_length && v2[k2off] != -1) {
322323
/* mirror x2 onto top-left coordinate system */
323-
int x2 = (int)t1len - v2[k2off];
324+
uint32_t x2 = (int)t1len - v2[k2off];
324325
if (x1 >= x2)
325326
return diff_bisect_split(
326327
out, diff, opts, t1, x1, t1len, t2, y1, t2len);
@@ -330,7 +331,8 @@ static int diff_bisect(
330331

331332
/* advance the reverse contour */
332333
for (k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
333-
int k2off = v_offset + k2, x2, y2;
334+
int k2off = v_offset + k2;
335+
uint32_t x2, y2;
334336

335337
if (k2 == -d || (k2 != d && v2[k2off - 1] < v2[k2off + 1]))
336338
x2 = v2[k2off + 1];
@@ -351,7 +353,7 @@ static int diff_bisect(
351353
int k1off = v_offset + delta - k2;
352354
if (k1off >= 0 && k1off < v_length && v1[k1off] != -1) {
353355
/* mirror x2 onto top-left coordinate system */
354-
int x1 = v1[k1off], y1 = v_offset + x1 - k1off;
356+
uint32_t x1 = v1[k1off], y1 = v_offset + x1 - k1off;
355357
x2 = t1len - x2;
356358
if (x1 >= x2)
357359
return diff_bisect_split(

src/dmp_pool.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Utilities for maintaining a Linked list of diff records
55
*
6-
* Copyright (c) 2012 Russell Belfer <[email protected]>
6+
* Copyright (c) Russell Belfer <[email protected]>
77
* https://github.com/arrbee/google-diff-match-patch-c/
88
*
99
* See included LICENSE file for license details.

src/dmp_pool.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Utilities for maintaining a Linked list of diff records
55
*
6-
* Copyright (c) 2012 Russell Belfer <[email protected]>
6+
* Copyright (c) Russell Belfer <[email protected]>
77
* https://github.com/arrbee/google-diff-match-patch-c/
88
*
99
* See included LICENSE file for license details.

test/Makefile

-6
This file was deleted.

0 commit comments

Comments
 (0)