forked from sysprog21/fibdrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_wk.h
78 lines (70 loc) · 1.74 KB
/
string_wk.h
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
#include <linux/string.h>
#define XOR_SWAP(a, b, type) \
do { \
type *__c = (a); \
type *__d = (b); \
*__c ^= *__d; \
*__d ^= *__c; \
*__c ^= *__d; \
} while (0)
static void __swap(void *a, void *b, size_t size)
{
if (a == b)
return;
switch (size) {
case 1:
XOR_SWAP(a, b, char);
break;
case 2:
XOR_SWAP(a, b, short);
break;
case 4:
XOR_SWAP(a, b, unsigned int);
break;
case 8:
XOR_SWAP(a, b, unsigned long);
break;
default:
/* Do nothing */
break;
}
}
typedef struct str {
char numberStr[128];
} str_t;
static void add_str(char *a, char *b, char *out)
{
size_t size_a = strlen(a), size_b = strlen(b);
int i, sum, carry = 0;
if (size_a >= size_b) {
for (i = 0; i < size_b; i++) {
sum = (a[i] - '0') + (b[i] - '0') + carry;
out[i] = '0' + sum % 10;
carry = sum / 10;
}
for (i = size_b; i < size_a; i++) {
sum = (a[i] - '0') + carry;
out[i] = '0' + sum % 10;
carry = sum / 10;
}
} else {
for (i = 0; i < size_a; i++) {
sum = (a[i] - '0') + (b[i] - '0') + carry;
out[i] = '0' + sum % 10;
carry = sum / 10;
}
for (i = size_a; i < size_b; i++) {
sum = (b[i] - '0') + carry;
out[i] = '0' + sum % 10;
carry = sum / 10;
}
}
if (carry)
out[i++] = '0' + carry;
out[i] = '\0';
}
static void reverse_str(char *str, size_t n)
{
for (int i = 0; i < (n >> 1); i++)
__swap(&str[i], &str[n - i - 1], sizeof(char));
}