aboutsummaryrefslogtreecommitdiffstats
path: root/sway/stringop.c
diff options
context:
space:
mode:
authorLibravatar taiyu <taiyu.len@gmail.com>2015-08-16 13:38:54 -0700
committerLibravatar taiyu <taiyu.len@gmail.com>2015-08-16 13:38:54 -0700
commit5b860c67c3d8fcded0f88715fc69053f81c10449 (patch)
tree762ecfc92de12eed31e8b7352e97af9fe609f045 /sway/stringop.c
parentFix pointer events properly (diff)
downloadsway-5b860c67c3d8fcded0f88715fc69053f81c10449.tar.gz
sway-5b860c67c3d8fcded0f88715fc69053f81c10449.tar.zst
sway-5b860c67c3d8fcded0f88715fc69053f81c10449.zip
fixed split [vh], small memory leak, unescape_strings handle \xnn
Diffstat (limited to 'sway/stringop.c')
-rw-r--r--sway/stringop.c67
1 files changed, 50 insertions, 17 deletions
diff --git a/sway/stringop.c b/sway/stringop.c
index bbc0bcdf..1dff97bf 100644
--- a/sway/stringop.c
+++ b/sway/stringop.c
@@ -53,8 +53,9 @@ char *strip_comments(char *str) {
53list_t *split_string(const char *str, const char *delims) { 53list_t *split_string(const char *str, const char *delims) {
54 list_t *res = create_list(); 54 list_t *res = create_list();
55 int i, j; 55 int i, j;
56 for (i = 0, j = 0; i < strlen(str) + 1; ++i) { 56 int len = strlen(str);
57 if (strchr(delims, str[i]) || i == strlen(str)) { 57 for (i = 0, j = 0; i < len + 1; ++i) {
58 if (strchr(delims, str[i]) || i == len) {
58 if (i - j == 0) { 59 if (i - j == 0) {
59 continue; 60 continue;
60 } 61 }
@@ -63,7 +64,7 @@ list_t *split_string(const char *str, const char *delims) {
63 left[i - j] = 0; 64 left[i - j] = 0;
64 list_add(res, left); 65 list_add(res, left);
65 j = i + 1; 66 j = i + 1;
66 while (j <= strlen(str) && str[j] && strchr(delims, str[j])) { 67 while (j <= len && str[j] && strchr(delims, str[j])) {
67 j++; 68 j++;
68 i++; 69 i++;
69 } 70 }
@@ -110,40 +111,72 @@ int unescape_string(char *string) {
110 for (i = 0; string[i]; ++i) { 111 for (i = 0; string[i]; ++i) {
111 if (string[i] == '\\') { 112 if (string[i] == '\\') {
112 --len; 113 --len;
114 int shift = 0;
113 switch (string[++i]) { 115 switch (string[++i]) {
114 case '0': 116 case '0':
115 string[i - 1] = '\0'; 117 string[i - 1] = '\0';
116 memmove(string + i, string + i + 1, len - i); 118 shift = 1;
117 break; 119 break;
118 case 'a': 120 case 'a':
119 string[i - 1] = '\a'; 121 string[i - 1] = '\a';
120 memmove(string + i, string + i + 1, len - i); 122 shift = 1;
121 break; 123 break;
122 case 'b': 124 case 'b':
123 string[i - 1] = '\b'; 125 string[i - 1] = '\b';
124 memmove(string + i, string + i + 1, len - i); 126 shift = 1;
125 break; 127 break;
126 case 't': 128 case 'f':
127 string[i - 1] = '\t'; 129 string[i - 1] = '\f';
128 memmove(string + i, string + i + 1, len - i); 130 shift = 1;
129 break; 131 break;
130 case 'n': 132 case 'n':
131 string[i - 1] = '\n'; 133 string[i - 1] = '\n';
132 memmove(string + i, string + i + 1, len - i); 134 shift = 1;
135 break;
136 case 'r':
137 string[i - 1] = '\r';
138 shift = 1;
139 break;
140 case 't':
141 string[i - 1] = '\t';
142 shift = 1;
133 break; 143 break;
134 case 'v': 144 case 'v':
135 string[i - 1] = '\v'; 145 string[i - 1] = '\v';
136 memmove(string + i, string + i + 1, len - i); 146 shift = 1;
137 break; 147 break;
138 case 'f': 148 case '\\':
139 string[i - 1] = '\f'; 149 shift = 1;
140 memmove(string + i, string + i + 1, len - i);
141 break; 150 break;
142 case 'r': 151 case '\'':
143 string[i - 1] = '\r'; 152 string[i - 1] = '\'';
144 memmove(string + i, string + i + 1, len - i); 153 shift = 1;
154 break;
155 case '\"':
156 string[i - 1] = '\"';
157 shift = 1;
158 break;
159 case '?':
160 string[i - 1] = '?';
161 shift = 1;
145 break; 162 break;
163 case 'x':
164 {
165 unsigned char c = 0;
166 shift = 1;
167 if (string[i+1] >= '0' && string[i+1] <= '9') {
168 shift = 2;
169 c = string[i+1] - '0';
170 if (string[i+2] >= '0' && string[i+2] <= '9') {
171 shift = 3;
172 c *= 0x10;
173 c += string[i+2] - '0';
174 }
175 }
176 string[i - 1] = c;
177 }
146 } 178 }
179 memmove(string + i, string + i + shift, len - i);
147 } 180 }
148 } 181 }
149 return len; 182 return len;