Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
xbe | efe3422 | 2014-03-16 00:14:26 -0700 | [diff] [blame] | 27 | #include <stdbool.h> |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdarg.h> |
| 30 | #include <string.h> |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 31 | #include <assert.h> |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 32 | #include "mpconfig.h" |
Paul Sokolovsky | 59c675a | 2014-06-21 22:43:22 +0300 | [diff] [blame] | 33 | #include "misc.h" |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 34 | |
| 35 | // returned value is always at least 1 greater than argument |
| 36 | #define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8) |
| 37 | |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 38 | void vstr_init(vstr_t *vstr, int alloc) { |
Damien George | 8cd72bd | 2014-03-31 17:10:59 +0100 | [diff] [blame] | 39 | if (alloc < 2) { |
| 40 | // need at least 1 byte for the null byte at the end |
| 41 | alloc = 2; |
| 42 | } |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 43 | vstr->alloc = alloc; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 44 | vstr->len = 0; |
| 45 | vstr->buf = m_new(char, vstr->alloc); |
| 46 | if (vstr->buf == NULL) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 47 | vstr->had_error = true; |
| 48 | return; |
| 49 | } |
| 50 | vstr->buf[0] = 0; |
| 51 | vstr->had_error = false; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 52 | vstr->fixed_buf = false; |
| 53 | } |
| 54 | |
| 55 | void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf) { |
| 56 | assert(alloc > 0); // need at least room for the null byte |
| 57 | vstr->alloc = alloc; |
| 58 | vstr->len = 0; |
| 59 | vstr->buf = buf; |
| 60 | vstr->buf[0] = 0; |
| 61 | vstr->had_error = false; |
| 62 | vstr->fixed_buf = true; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void vstr_clear(vstr_t *vstr) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 66 | if (!vstr->fixed_buf) { |
| 67 | m_del(char, vstr->buf, vstr->alloc); |
| 68 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 69 | vstr->buf = NULL; |
| 70 | } |
| 71 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 72 | vstr_t *vstr_new(void) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 73 | vstr_t *vstr = m_new(vstr_t, 1); |
| 74 | if (vstr == NULL) { |
| 75 | return NULL; |
| 76 | } |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 77 | vstr_init(vstr, 32); |
| 78 | return vstr; |
| 79 | } |
| 80 | |
| 81 | vstr_t *vstr_new_size(int alloc) { |
| 82 | vstr_t *vstr = m_new(vstr_t, 1); |
| 83 | if (vstr == NULL) { |
| 84 | return NULL; |
| 85 | } |
| 86 | vstr_init(vstr, alloc); |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 87 | return vstr; |
| 88 | } |
| 89 | |
| 90 | void vstr_free(vstr_t *vstr) { |
| 91 | if (vstr != NULL) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 92 | if (!vstr->fixed_buf) { |
| 93 | m_del(char, vstr->buf, vstr->alloc); |
| 94 | } |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 95 | m_del_obj(vstr_t, vstr); |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | void vstr_reset(vstr_t *vstr) { |
| 100 | vstr->len = 0; |
| 101 | vstr->buf[0] = 0; |
| 102 | vstr->had_error = false; |
| 103 | } |
| 104 | |
| 105 | bool vstr_had_error(vstr_t *vstr) { |
| 106 | return vstr->had_error; |
| 107 | } |
| 108 | |
| 109 | char *vstr_str(vstr_t *vstr) { |
| 110 | if (vstr->had_error) { |
| 111 | return NULL; |
| 112 | } |
| 113 | return vstr->buf; |
| 114 | } |
| 115 | |
| 116 | int vstr_len(vstr_t *vstr) { |
| 117 | if (vstr->had_error) { |
| 118 | return 0; |
| 119 | } |
| 120 | return vstr->len; |
| 121 | } |
| 122 | |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 123 | // Extend vstr strictly to by requested size, return pointer to newly added chunk |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 124 | char *vstr_extend(vstr_t *vstr, int size) { |
| 125 | if (vstr->fixed_buf) { |
| 126 | return NULL; |
| 127 | } |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 128 | char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size); |
| 129 | if (new_buf == NULL) { |
| 130 | vstr->had_error = true; |
| 131 | return NULL; |
| 132 | } |
| 133 | char *p = new_buf + vstr->alloc; |
| 134 | vstr->alloc += size; |
| 135 | vstr->buf = new_buf; |
| 136 | return p; |
| 137 | } |
| 138 | |
| 139 | // Shrink vstr to be given size |
| 140 | bool vstr_set_size(vstr_t *vstr, int size) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 141 | if (vstr->fixed_buf) { |
| 142 | return false; |
| 143 | } |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 144 | char *new_buf = m_renew(char, vstr->buf, vstr->alloc, size); |
| 145 | if (new_buf == NULL) { |
| 146 | vstr->had_error = true; |
| 147 | return false; |
| 148 | } |
| 149 | vstr->buf = new_buf; |
| 150 | vstr->alloc = vstr->len = size; |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | // Shrink vstr allocation to its actual length |
| 155 | bool vstr_shrink(vstr_t *vstr) { |
| 156 | return vstr_set_size(vstr, vstr->len); |
| 157 | } |
| 158 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 159 | STATIC bool vstr_ensure_extra(vstr_t *vstr, int size) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 160 | if (vstr->len + size + 1 > vstr->alloc) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 161 | if (vstr->fixed_buf) { |
| 162 | return false; |
| 163 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 164 | int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2); |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 165 | char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc); |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 166 | if (new_buf == NULL) { |
| 167 | vstr->had_error = true; |
| 168 | return false; |
| 169 | } |
| 170 | vstr->alloc = new_alloc; |
| 171 | vstr->buf = new_buf; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | void vstr_hint_size(vstr_t *vstr, int size) { |
| 177 | // it's not an error if we fail to allocate for the size hint |
| 178 | bool er = vstr->had_error; |
| 179 | vstr_ensure_extra(vstr, size); |
| 180 | vstr->had_error = er; |
| 181 | } |
| 182 | |
| 183 | char *vstr_add_len(vstr_t *vstr, int len) { |
| 184 | if (vstr->had_error || !vstr_ensure_extra(vstr, len)) { |
| 185 | return NULL; |
| 186 | } |
| 187 | char *buf = vstr->buf + vstr->len; |
| 188 | vstr->len += len; |
| 189 | vstr->buf[vstr->len] = 0; |
| 190 | return buf; |
| 191 | } |
| 192 | |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 193 | void vstr_add_byte(vstr_t *vstr, byte b) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 194 | byte *buf = (byte*)vstr_add_len(vstr, 1); |
| 195 | if (buf == NULL) { |
| 196 | return; |
| 197 | } |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 198 | buf[0] = b; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void vstr_add_char(vstr_t *vstr, unichar c) { |
Chris Angelico | 2ba2299 | 2014-06-04 05:28:12 +1000 | [diff] [blame^] | 202 | // TODO: Can this be simplified and deduplicated? |
| 203 | // Is it worth just calling vstr_add_len(vstr, 4)? |
| 204 | if (c < 0x80) { |
| 205 | byte *buf = (byte*)vstr_add_len(vstr, 1); |
| 206 | if (buf == NULL) { |
| 207 | return; |
| 208 | } |
| 209 | *buf = (byte)c; |
| 210 | } else if (c < 0x800) { |
| 211 | byte *buf = (byte*)vstr_add_len(vstr, 2); |
| 212 | if (buf == NULL) { |
| 213 | return; |
| 214 | } |
| 215 | buf[0] = (c >> 6) | 0xC0; |
| 216 | buf[1] = (c & 0x3F) | 0x80; |
| 217 | } else if (c < 0x10000) { |
| 218 | byte *buf = (byte*)vstr_add_len(vstr, 3); |
| 219 | if (buf == NULL) { |
| 220 | return; |
| 221 | } |
| 222 | buf[0] = (c >> 12) | 0xE0; |
| 223 | buf[1] = ((c >> 6) & 0x3F) | 0x80; |
| 224 | buf[2] = (c & 0x3F) | 0x80; |
| 225 | } else { |
| 226 | assert(c < 0x110000); |
| 227 | byte *buf = (byte*)vstr_add_len(vstr, 4); |
| 228 | if (buf == NULL) { |
| 229 | return; |
| 230 | } |
| 231 | buf[0] = (c >> 18) | 0xF0; |
| 232 | buf[1] = ((c >> 12) & 0x3F) | 0x80; |
| 233 | buf[2] = ((c >> 6) & 0x3F) | 0x80; |
| 234 | buf[3] = (c & 0x3F) | 0x80; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 235 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void vstr_add_str(vstr_t *vstr, const char *str) { |
| 239 | vstr_add_strn(vstr, str, strlen(str)); |
| 240 | } |
| 241 | |
| 242 | void vstr_add_strn(vstr_t *vstr, const char *str, int len) { |
| 243 | if (vstr->had_error || !vstr_ensure_extra(vstr, len)) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 244 | // if buf is fixed, we got here because there isn't enough room left |
| 245 | // so just try to copy as much as we can, with room for null byte |
| 246 | if (vstr->fixed_buf && vstr->len + 1 < vstr->alloc) { |
| 247 | len = vstr->alloc - vstr->len - 1; |
| 248 | goto copy; |
| 249 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 250 | return; |
| 251 | } |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 252 | copy: |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 253 | memmove(vstr->buf + vstr->len, str, len); |
| 254 | vstr->len += len; |
| 255 | vstr->buf[vstr->len] = 0; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | void vstr_add_le16(vstr_t *vstr, unsigned short v) { |
| 260 | byte *buf = (byte*)vstr_add_len(vstr, 2); |
| 261 | if (buf == NULL) { |
| 262 | return; |
| 263 | } |
| 264 | encode_le16(buf, v); |
| 265 | } |
| 266 | |
| 267 | void vstr_add_le32(vstr_t *vstr, unsigned int v) { |
| 268 | byte *buf = (byte*)vstr_add_len(vstr, 4); |
| 269 | if (buf == NULL) { |
| 270 | return; |
| 271 | } |
| 272 | encode_le32(buf, v); |
| 273 | } |
| 274 | */ |
| 275 | |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 276 | char *vstr_ins_blank_bytes(vstr_t *vstr, uint byte_pos, uint byte_len) { |
| 277 | if (vstr->had_error) { |
| 278 | return NULL; |
| 279 | } |
| 280 | uint l = vstr->len; |
| 281 | if (byte_pos > l) { |
| 282 | byte_pos = l; |
| 283 | } |
| 284 | if (byte_len > 0) { |
| 285 | // ensure room for the new bytes |
| 286 | if (!vstr_ensure_extra(vstr, byte_len)) { |
| 287 | return NULL; |
| 288 | } |
Damien George | ecd58ae | 2014-03-15 16:54:06 +0000 | [diff] [blame] | 289 | // copy up the string to make room for the new bytes; +1 for the null byte |
| 290 | memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos + 1); |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 291 | // increase the length |
| 292 | vstr->len += byte_len; |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 293 | } |
| 294 | return vstr->buf + byte_pos; |
| 295 | } |
| 296 | |
| 297 | void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b) { |
| 298 | char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1); |
| 299 | if (s != NULL) { |
| 300 | *s = b; |
| 301 | } |
| 302 | } |
| 303 | |
Damien George | ecd58ae | 2014-03-15 16:54:06 +0000 | [diff] [blame] | 304 | void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr) { |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 305 | // TODO UNICODE |
Damien George | ecd58ae | 2014-03-15 16:54:06 +0000 | [diff] [blame] | 306 | char *s = vstr_ins_blank_bytes(vstr, char_pos, 1); |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 307 | if (s != NULL) { |
| 308 | *s = chr; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut) { |
| 313 | vstr_cut_out_bytes(vstr, 0, bytes_to_cut); |
| 314 | } |
| 315 | |
| 316 | void vstr_cut_tail_bytes(vstr_t *vstr, uint len) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 317 | if (vstr->had_error) { |
| 318 | return; |
| 319 | } |
| 320 | if (len > vstr->len) { |
| 321 | vstr->len = 0; |
| 322 | } else { |
| 323 | vstr->len -= len; |
| 324 | } |
Damien George | f64086f | 2014-01-22 23:18:50 +0000 | [diff] [blame] | 325 | vstr->buf[vstr->len] = 0; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 328 | void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut) { |
| 329 | if (vstr->had_error || byte_pos >= vstr->len) { |
| 330 | return; |
| 331 | } else if (byte_pos + bytes_to_cut >= vstr->len) { |
| 332 | vstr->len = byte_pos; |
| 333 | vstr->buf[vstr->len] = 0; |
| 334 | } else { |
| 335 | // move includes +1 for null byte at the end |
| 336 | memmove(vstr->buf + byte_pos, vstr->buf + byte_pos + bytes_to_cut, vstr->len - byte_pos - bytes_to_cut + 1); |
| 337 | vstr->len -= bytes_to_cut; |
| 338 | } |
| 339 | } |
| 340 | |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 341 | void vstr_printf(vstr_t *vstr, const char *fmt, ...) { |
Damien | 2f06c57 | 2013-11-03 18:20:56 +0000 | [diff] [blame] | 342 | va_list ap; |
| 343 | va_start(ap, fmt); |
| 344 | vstr_vprintf(vstr, fmt, ap); |
| 345 | va_end(ap); |
| 346 | } |
| 347 | |
| 348 | void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 349 | if (vstr->had_error || !vstr_ensure_extra(vstr, strlen(fmt))) { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | while (1) { |
| 354 | // try to print in the allocated space |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 355 | // need to make a copy of the va_list because we may call vsnprintf multiple times |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 356 | int size = vstr->alloc - vstr->len; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 357 | va_list ap2; |
| 358 | va_copy(ap2, ap); |
| 359 | int n = vsnprintf(vstr->buf + vstr->len, size, fmt, ap2); |
| 360 | va_end(ap2); |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 361 | |
| 362 | // if that worked, return |
| 363 | if (n > -1 && n < size) { |
| 364 | vstr->len += n; |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | // else try again with more space |
| 369 | if (n > -1) { // glibc 2.1 |
| 370 | // n + 1 is precisely what is needed |
| 371 | if (!vstr_ensure_extra(vstr, n + 1)) { |
| 372 | return; |
| 373 | } |
| 374 | } else { // glibc 2.0 |
| 375 | // increase to twice the old size |
| 376 | if (!vstr_ensure_extra(vstr, size * 2)) { |
| 377 | return; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 382 | |
| 383 | /** testing *****************************************************/ |
| 384 | |
| 385 | /* |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 386 | int main(void) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 387 | vstr_t *vstr = vstr_new(); |
| 388 | int i; |
| 389 | for (i = 0; i < 10; i++) { |
| 390 | vstr_printf(vstr, "%d) this is a test %d %s\n", i, 1234, "'a string'"); |
| 391 | vstr_add_str(vstr, "-----"); |
| 392 | vstr_printf(vstr, "this is another test %d %s\n", 1234, "'a second string'"); |
| 393 | printf("%s", vstr->buf); |
| 394 | } |
| 395 | } |
| 396 | */ |