-
Notifications
You must be signed in to change notification settings - Fork 0
/
dequote.c
91 lines (84 loc) · 1.8 KB
/
dequote.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "quote.h"
/**
* dequote - dequote a string
* @str: the string to dequote
* Return: If memory allocation fails, return NULL.
* Otherwise return a dequoted copy of str.
*/
char *dequote(const char *str)
{
char *new;
size_t len = 0, state_len;
quote_state_t state;
if (!str)
return (NULL);
new = malloc(sizeof(char) * (dequote_len(str) + 1));
if (!new)
return (NULL);
while (*str)
{
state = quote_state(*str);
str += (1 && (state & (QUOTE_DOUBLE | QUOTE_SINGLE | QUOTE_ESCAPE)));
state_len = quote_state_len(str, state);
if (state & QUOTE_DOUBLE)
{
for ( ; state_len; --state_len)
{
if (quote_state(*str++) & QUOTE_ESCAPE)
{
if (*str == '\n')
{
++str, --state_len;
continue;
}
if (_isspecial_double(*str))
++str, --state_len;
}
new[len++] = str[-1];
}
}
_memcpy(new + len, str, state_len);
len += state_len;
str += state_len;
str += (*str && (state & (QUOTE_DOUBLE | QUOTE_SINGLE)));
}
new[len] = '\0';
return (new);
}
/**
* dequote_len - compute the length of a string after dequoting
* @str: the string to evaluate
* Return: Return the length of str after dequoting
*/
size_t dequote_len(const char *str)
{
size_t len = 0, state_len;
quote_state_t state;
while (*str)
{
state = quote_state(*str);
str += (1 && (state & (QUOTE_DOUBLE | QUOTE_SINGLE | QUOTE_ESCAPE)));
state_len = quote_state_len(str, state);
if (state & QUOTE_DOUBLE)
{
for ( ; state_len; --state_len)
{
if (quote_state(*str++) & QUOTE_ESCAPE)
{
if (*str == '\n')
{
++str, --state_len;
continue;
}
if (_isspecial_double(*str))
++str, --state_len;
}
len++;
}
}
len += state_len;
str += state_len;
str += (*str && (state & (QUOTE_DOUBLE | QUOTE_SINGLE)));
}
return (len);
}