Skip to content

Commit f2eb4ab

Browse files
authored
Merge pull request #65 from Dajvid/devel
io CHANGE more efficient way of finding endtag in nc_read_until
2 parents 7f60f43 + 15dad86 commit f2eb4ab

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

src/io.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint
197197
struct timespec *ts_act_timeout, char **result)
198198
{
199199
char *chunk = NULL;
200-
size_t size, count = 0, r, len;
200+
size_t size, count = 0, r, len, i, matched = 0;
201201

202202
assert(session);
203203
assert(endtag);
@@ -223,7 +223,7 @@ nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint
223223
}
224224

225225
/* resize buffer if needed */
226-
if (count == size) {
226+
if ((count + (len - matched)) >= size) {
227227
/* get more memory */
228228
size = size + BUFFERSIZE;
229229
chunk = realloc(chunk, (size + 1) * sizeof *chunk);
@@ -234,21 +234,28 @@ nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint
234234
}
235235

236236
/* get another character */
237-
r = nc_read(session, &(chunk[count]), 1, inact_timeout, ts_act_timeout);
238-
if (r != 1) {
237+
r = nc_read(session, &(chunk[count]), len - matched, inact_timeout, ts_act_timeout);
238+
if (r != len - matched) {
239239
free(chunk);
240240
return -1;
241241
}
242242

243-
count++;
243+
count += len - matched;
244244

245-
/* check endtag */
246-
if (count >= len) {
247-
if (!strncmp(endtag, &(chunk[count - len]), len)) {
248-
/* endtag found */
245+
for (i = len - matched; i > 0; i--) {
246+
if (!strncmp(&endtag[matched], &(chunk[count - i]), i)) {
247+
/*part of endtag found */
248+
matched += i;
249249
break;
250+
} else {
251+
matched = 0;
250252
}
251253
}
254+
255+
/* whole endtag found */
256+
if (matched == len) {
257+
break;
258+
}
252259
}
253260

254261
/* terminating null byte */
@@ -1264,3 +1271,4 @@ nc_realloc(void *ptr, size_t size)
12641271

12651272
return ret;
12661273
}
1274+

0 commit comments

Comments
 (0)