forked from okbob/pltoolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.c
168 lines (137 loc) · 2.81 KB
/
diff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*************************************
* Author: Pavel Stehule @2010
*
* if you like this sw, then send postcard from your home on address
*
* Pavel Stehule
* Skalice 12
* 256 01 Benesov u Prahy
* Czech Republic
*
* licenced under BSD licence
*/
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Datum pst_lc_substring(PG_FUNCTION_ARGS);
Datum pst_diff_string(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pst_lc_substring);
PG_FUNCTION_INFO_V1(pst_diff_string);
static char *
LCSubstr(const char *S, const char *T)
{
int *LP, *LC;
int m = strlen(S);
int n = strlen(T);
int z = 0;
int i, j;
int pos = 0;
LP = palloc0((n + 1) * sizeof(int));
LC = palloc0((n + 1) * sizeof(int));
for (i = 1; i <= m; i++)
{
for (j = 1; j <= n; j++)
{
if (S[i-1] == T[j-1])
{
LC[j] = 1 + LP[j-1];
if (LC[j] > z)
{
z = LC[j];
pos = i;
}
}
else
LC[j] = 0;
}
memcpy(LP, LC, (n + 1) * sizeof(int));
}
pfree(LP);
pfree(LC);
if (pg_database_encoding_max_length() > 1)
{
char *result;
char *ptr;
const char *src;
/* copy to result only complete multibyte chars */
result = palloc(z + 1);
ptr = result;
src = &S[pos - z];
while (z > 0)
{
int len = pg_mblen(src);
int i;
if (len > z)
break;
for (i = 0; i < len; i++)
{
*ptr++ = *src++;
z--;
}
}
*ptr = '\0';
return result;
}
else
return pnstrdup(&S[pos - z], z);
}
Datum
pst_lc_substring(PG_FUNCTION_ARGS)
{
text *s = PG_GETARG_TEXT_P(0);
text *t = PG_GETARG_TEXT_P(1);
PG_RETURN_TEXT_P(cstring_to_text(LCSubstr(text_to_cstring(s), text_to_cstring(t))));
}
#define IS_EMPTY_STR(s) (*(s) == '\0')
static void
diff_string(StringInfo str, char *S, char *T)
{
char *lcs = LCSubstr(S, T);
char *p1,
*p2;
int len;
len = pg_mbstrlen(lcs);
if (len == 0)
{
if (!IS_EMPTY_STR(S) && !IS_EMPTY_STR(T))
{
appendStringInfo(str, "<del>%s</>", S);
appendStringInfo(str, "<ins>%s</>", T);
}
else if (!IS_EMPTY_STR(S))
appendStringInfo(str, "<del>%s</>", S);
else if (!IS_EMPTY_STR(T))
appendStringInfo(str, "<ins>%s</>", T);
pfree(lcs);
return;
}
p1 = strstr(S, lcs);
p2 = strstr(T, lcs);
Assert(p1 != NULL);
Assert(p2 != NULL);
*p1 = '\0';
*p2 = '\0';
diff_string(str, S, T);
appendStringInfoString(str, lcs);
diff_string(str, p1 + len, p2 + len);
pfree(lcs);
}
Datum
pst_diff_string(PG_FUNCTION_ARGS)
{
text *s = PG_GETARG_TEXT_P(0);
text *t = PG_GETARG_TEXT_P(1);
text *result;
StringInfoData str;
initStringInfo(&str);
diff_string(&str, text_to_cstring(s), text_to_cstring(t));
result = cstring_to_text_with_len(str.data, str.len);
pfree(str.data);
PG_RETURN_TEXT_P(result);
}