@@ -2,67 +2,95 @@ Port of google-diff-match-patch to C
2
2
====================================
3
3
4
4
This is a C language port of Neil Fraser's google-diff-match-patch code.
5
- His original code is available at:
6
5
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
+ ```
20
46
21
- Example Usage
22
- -------------
47
+ Example API Usage
48
+ -----------------
23
49
24
50
All functions and structures used in this library are prefixed with
25
51
` dmp_ ` . To generate a diff, you use a function to create a ` dmp_diff `
26
52
object which you can then access and manipulate via other functions.
27
53
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
+
30
57
``` 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
+ }
52
79
```
53
80
54
81
This shows the basic pattern of diff API usage:
82
+
55
83
1. Generate a diff
56
84
2. Process the diff in some way
57
85
3. Free the diff
58
86
59
87
Diff API
60
88
--------
61
89
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.
66
94
67
95
Here are the main functions for generating and accessing diffs:
68
96
@@ -152,15 +180,22 @@ extern int dmp_diff_foreach(
152
180
Status
153
181
------
154
182
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.
159
191
160
192
Copyright and License
161
193
---------------------
162
194
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
164
199
the [ Apache License 2.0] ( http://www.apache.org/licenses/LICENSE-2.0 ) .
165
200
The full terms of that license are included here in the
166
201
` APACHE-LICENSE-2.0 ` file.
@@ -177,7 +212,7 @@ the Expat License) which is included here in the `LICENSE` file.
177
212
178
213
C version of Diff, Match and Patch Library
179
214
180
- Copyright (c)
2012 Russell Belfer
< [email protected] >
215
+ Copyright (c) Russell Belfer
< [email protected] >
181
216
< http://github.com/arrbee/google-diff-match-patch-c/ >
182
217
183
218
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:
0 commit comments