-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringalgo.c
68 lines (50 loc) · 1.36 KB
/
stringalgo.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
//
// stringalgo.c
// Perceptron GLM NLP Tasks
//
// Created by husnu sensoy on 03/01/14.
// Copyright (c) 2014 husnu sensoy. All rights reserved.
//
#include "stringalgo.h"
#include <alloca.h>
int endswith(const char* str, const char* suffix) {
if (strlen(suffix) <= strlen(str)) {
unsigned long n = strlen(str) - strlen(suffix);
for (int i = 0; i < strlen(suffix); i++) {
// printf("%c %c\n", suffix[i], str[n+i]);
if (suffix[i] != str[n + i])
return 0;
}
return 1;
} else {
return 0;
}
}
// TODO: strtok does not release any memory ?
DArray* split(const char* str, const char *delims) {
DArray *tokens = DArray_create(sizeof (char*), 3);
check_mem(tokens);
char *strclone = (char *) alloca(strlen(str) + 1);
strcpy(strclone, str);
//char *strclone = strdup(str);
char *pch = strtok(strclone, delims);
while (pch != NULL) {
DArray_push(tokens, strdup(pch));
pch = strtok(NULL, delims);
}
return tokens;
error:
exit(1);
}
void join(char buffer[], int n, ...) {
va_list ap;
buffer[0] = '\0';
va_start(ap, n);
for (int j = 1; j <= n; j++) {
strcat(buffer, (va_arg(ap, char*)));
if (j < n)
strcat(buffer, " ");
}
va_end(ap);
// log_info("%s",result);
}