blob: 9816075e62fea399b630948e4fd2323792878d82 (
plain)
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
|
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* u_mktemp() {
char pattern[] = "/tmp/monero_c_test_XXXXXXXXX";
char *template = malloc(strlen(pattern) + 1);
if (!template) {
perror("malloc failed");
exit(1);
}
strcpy(template, pattern);
if (mkdtemp(template) == NULL) {
perror("mkdtemp failed");
free(template);
exit(1);
}
return template;
}
char* u_cat(const char* a, const char* b) {
size_t len_a = strlen(a);
size_t len_b = strlen(b);
char* result = malloc(len_a + len_b + 1);
if (!result) {
perror("malloc failed");
exit(1);
}
memcpy(result, a, len_a);
memcpy(result + len_a, b, len_b + 1); // include null terminator
return result;
}
int u_wordcount(const char* str) {
if (!str) return 0;
int count = 0;
int in_word = 0;
for (size_t i = 0; str[i]; i++) {
// Japan smh ( = 0xE3 0x80 0x80)
int is_space = (str[i] == ' ') ||
((unsigned char)str[i] == 0xE3 &&
(unsigned char)str[i+1] == 0x80 &&
(unsigned char)str[i+2] == 0x80);
if (is_space) {
in_word = 0;
} else if (!in_word) {
in_word = 1;
count++;
}
}
return count;
}
|