File: Approx.xs

package info (click to toggle)
libstring-approx-perl 3.28-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: ansic: 1,338; perl: 411; makefile: 3
file content (234 lines) | stat: -rw-r--r-- 5,925 bytes parent folder | download | duplicates (6)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "patchlevel.h"
#ifdef __cplusplus
}
#endif

#include "apse.h"

#if PATCHLEVEL < 5
# define PL_na na
#endif

MODULE = String::Approx         PACKAGE = String::Approx                

PROTOTYPES: DISABLE

apse_t*
new(CLASS, pattern, ...)
        char*           CLASS
        SV*             pattern
    CODE:
        apse_t*         ap;
        apse_size_t     edit_distance;
        IV pattern_size = sv_len(pattern);
        if (items == 2)
                edit_distance = ((pattern_size-1)/10)+1;
        else if (items == 3)
                edit_distance = (apse_size_t)SvIV(ST(2));
        else {
                warn("Usage: new(pattern[, edit_distance])\n");
                XSRETURN_UNDEF;
        }
        ap = apse_create((unsigned char *)SvPV(pattern, PL_na),
                         pattern_size, edit_distance);
        if (ap) {
                RETVAL = ap;
        } else {
                warn("unable to allocate");
                XSRETURN_UNDEF;
        }
    OUTPUT:
        RETVAL

void
DESTROY(ap)
        apse_t*         ap
    CODE:
        apse_destroy(ap);

apse_bool_t
match(ap, text)
        apse_t*         ap
        SV*             text
    CODE:
        RETVAL = apse_match(ap,
                            (unsigned char *)SvPV(text, PL_na),
                            sv_len(text));
    OUTPUT:
        RETVAL

apse_bool_t
match_next(ap, text)
        apse_t*         ap
        SV*             text
    CODE:
        RETVAL = apse_match_next(ap,
                                 (unsigned char *)SvPV(text, PL_na),
                                 sv_len(text));
    OUTPUT:
        RETVAL

apse_ssize_t
index(ap, text)
        apse_t*         ap
        SV*             text
    CODE:
        RETVAL = apse_index(ap,
                            (unsigned char *)SvPV(text, PL_na),
                            sv_len(text));
    OUTPUT:
        RETVAL

void
slice(ap, text)
        apse_t*         ap
        SV*             text
    PREINIT:
        apse_size_t     match_begin;
        apse_size_t     match_size;
    PPCODE:
	if (ap->use_minimal_distance) {
	  apse_slice(ap,
		     (unsigned char *)SvPV(text, PL_na),
		     (apse_size_t)sv_len(text),
		     &match_begin,
		     &match_size);
	  EXTEND(sp, 3);
	  PUSHs(sv_2mortal(newSViv(match_begin)));
	  PUSHs(sv_2mortal(newSViv(match_size)));
	  PUSHs(sv_2mortal(newSViv(ap->edit_distance)));
	} else if (apse_slice(ap,
			      (unsigned char *)SvPV(text, PL_na),
			      (apse_size_t)sv_len(text),
			      &match_begin,
			      &match_size)) {
	  EXTEND(sp, 2);
	  PUSHs(sv_2mortal(newSViv(match_begin)));
	  PUSHs(sv_2mortal(newSViv(match_size)));
        }

void
slice_next(ap, text)
        apse_t*         ap
        SV*             text
    PREINIT:
        apse_size_t     match_begin;
        apse_size_t     match_size;
    PPCODE:
        if (apse_slice_next(ap,
                            (unsigned char *)SvPV(text, PL_na),
                            sv_len(text),
                            &match_begin,
                            &match_size)) {
                EXTEND(sp, 2);
                PUSHs(sv_2mortal(newSViv(match_begin)));
                PUSHs(sv_2mortal(newSViv(match_size)));
		if (ap->use_minimal_distance) {
		    EXTEND(sp, 1);
		    PUSHs(sv_2mortal(newSViv(ap->edit_distance)));
		}
        }

void
set_greedy(ap)
        apse_t*         ap
    CODE:
        apse_set_greedy(ap, 1);

apse_bool_t
set_caseignore_slice(ap, ...)
        apse_t* ap
    PREINIT:
        apse_size_t     offset;
        apse_size_t     size;
        apse_bool_t     ignore;
    CODE:
        offset = items < 2 ? 0 : (apse_size_t)SvIV(ST(1));
        size   = items < 3 ? ap->pattern_size : (apse_size_t)SvIV(ST(2));
        ignore = items < 4 ? 1 : (apse_bool_t)SvIV(ST(3));
        RETVAL = apse_set_caseignore_slice(ap, offset, size, ignore);
    OUTPUT:
        RETVAL

apse_bool_t
set_insertions(ap, insertions)
        apse_t*         ap
        apse_size_t     insertions = SvUV($arg);
    CODE:
        RETVAL = apse_set_insertions(ap, insertions);
    OUTPUT:
        RETVAL

apse_bool_t
set_deletions(ap, deletions)
        apse_t*         ap
        apse_size_t     deletions = SvUV($arg);
    CODE:
        RETVAL = apse_set_deletions(ap, deletions);
    OUTPUT:
        RETVAL

apse_bool_t
set_substitutions(ap, substitutions)
        apse_t*         ap
        apse_size_t     substitutions = SvUV($arg);
    CODE:
        RETVAL = apse_set_substitutions(ap, substitutions);
    OUTPUT:
        RETVAL

apse_bool_t
set_edit_distance(ap, edit_distance)
        apse_t*         ap
        apse_size_t     edit_distance = SvUV($arg);
    CODE:
        RETVAL = apse_set_edit_distance(ap, edit_distance);
    OUTPUT:
        RETVAL

apse_size_t
get_edit_distance(ap)
        apse_t*         ap
    CODE:
        ST(0) = sv_newmortal();
        sv_setiv(ST(0), apse_get_edit_distance(ap));

apse_bool_t
set_text_initial_position(ap, text_initial_position)
        apse_t*         ap
        apse_size_t     text_initial_position = SvUV($arg);
    CODE:
        RETVAL = apse_set_text_initial_position(ap, text_initial_position);
    OUTPUT:
        RETVAL

apse_bool_t
set_text_final_position(ap, text_final_position)
        apse_t*         ap
        apse_size_t     text_final_position = SvUV($arg);
    CODE:
        RETVAL = apse_set_text_final_position(ap, text_final_position);
    OUTPUT:
        RETVAL

apse_bool_t
set_text_position_range(ap, text_position_range)
        apse_t*         ap
        apse_size_t     text_position_range = SvUV($arg);
    CODE:
        RETVAL = apse_set_text_position_range(ap, text_position_range);
    OUTPUT:
        RETVAL

void
set_minimal_distance(ap, b)
        apse_t*         ap
	apse_bool_t	b
    CODE:
        apse_set_minimal_distance(ap, b);