Numworks Epsilon  1.4.1
Graphing Calculator Operating System
vstr.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython 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 
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <assert.h>
31 
32 #include "py/mpconfig.h"
33 #include "py/runtime.h"
34 #include "py/mpprint.h"
35 
36 // returned value is always at least 1 greater than argument
37 #define ROUND_ALLOC(a) (((a) & ((~0U) - 7)) + 8)
38 
39 // Init the vstr so it allocs exactly given number of bytes. Set length to zero.
40 void vstr_init(vstr_t *vstr, size_t alloc) {
41  if (alloc < 1) {
42  alloc = 1;
43  }
44  vstr->alloc = alloc;
45  vstr->len = 0;
46  vstr->buf = m_new(char, vstr->alloc);
47  vstr->fixed_buf = false;
48 }
49 
50 // Init the vstr so it allocs exactly enough ram to hold a null-terminated
51 // string of the given length, and set the length.
52 void vstr_init_len(vstr_t *vstr, size_t len) {
53  vstr_init(vstr, len + 1);
54  vstr->len = len;
55 }
56 
57 void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
58  vstr->alloc = alloc;
59  vstr->len = 0;
60  vstr->buf = buf;
61  vstr->fixed_buf = true;
62 }
63 
64 void vstr_init_print(vstr_t *vstr, size_t alloc, mp_print_t *print) {
65  vstr_init(vstr, alloc);
66  print->data = vstr;
68 }
69 
70 void vstr_clear(vstr_t *vstr) {
71  if (!vstr->fixed_buf) {
72  m_del(char, vstr->buf, vstr->alloc);
73  }
74  vstr->buf = NULL;
75 }
76 
77 vstr_t *vstr_new(size_t alloc) {
78  vstr_t *vstr = m_new_obj(vstr_t);
79  vstr_init(vstr, alloc);
80  return vstr;
81 }
82 
83 void vstr_free(vstr_t *vstr) {
84  if (vstr != NULL) {
85  if (!vstr->fixed_buf) {
86  m_del(char, vstr->buf, vstr->alloc);
87  }
88  m_del_obj(vstr_t, vstr);
89  }
90 }
91 
92 // Extend vstr strictly by requested size, return pointer to newly added chunk.
93 char *vstr_extend(vstr_t *vstr, size_t size) {
94  if (vstr->fixed_buf) {
95  // We can't reallocate, and the caller is expecting the space to
96  // be there, so the only safe option is to raise an exception.
98  }
99  char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
100  char *p = new_buf + vstr->alloc;
101  vstr->alloc += size;
102  vstr->buf = new_buf;
103  return p;
104 }
105 
106 STATIC void vstr_ensure_extra(vstr_t *vstr, size_t size) {
107  if (vstr->len + size > vstr->alloc) {
108  if (vstr->fixed_buf) {
109  // We can't reallocate, and the caller is expecting the space to
110  // be there, so the only safe option is to raise an exception.
112  }
113  size_t new_alloc = ROUND_ALLOC((vstr->len + size) + 16);
114  char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
115  vstr->alloc = new_alloc;
116  vstr->buf = new_buf;
117  }
118 }
119 
120 void vstr_hint_size(vstr_t *vstr, size_t size) {
121  vstr_ensure_extra(vstr, size);
122 }
123 
124 char *vstr_add_len(vstr_t *vstr, size_t len) {
125  vstr_ensure_extra(vstr, len);
126  char *buf = vstr->buf + vstr->len;
127  vstr->len += len;
128  return buf;
129 }
130 
131 // Doesn't increase len, just makes sure there is a null byte at the end
133  // If there's no more room, add single byte
134  if (vstr->alloc == vstr->len) {
135  vstr_extend(vstr, 1);
136  }
137  vstr->buf[vstr->len] = '\0';
138  return vstr->buf;
139 }
140 
141 void vstr_add_byte(vstr_t *vstr, byte b) {
142  byte *buf = (byte*)vstr_add_len(vstr, 1);
143  buf[0] = b;
144 }
145 
147 #if MICROPY_PY_BUILTINS_STR_UNICODE
148  // TODO: Can this be simplified and deduplicated?
149  // Is it worth just calling vstr_add_len(vstr, 4)?
150  if (c < 0x80) {
151  byte *buf = (byte*)vstr_add_len(vstr, 1);
152  *buf = (byte)c;
153  } else if (c < 0x800) {
154  byte *buf = (byte*)vstr_add_len(vstr, 2);
155  buf[0] = (c >> 6) | 0xC0;
156  buf[1] = (c & 0x3F) | 0x80;
157  } else if (c < 0x10000) {
158  byte *buf = (byte*)vstr_add_len(vstr, 3);
159  buf[0] = (c >> 12) | 0xE0;
160  buf[1] = ((c >> 6) & 0x3F) | 0x80;
161  buf[2] = (c & 0x3F) | 0x80;
162  } else {
163  assert(c < 0x110000);
164  byte *buf = (byte*)vstr_add_len(vstr, 4);
165  buf[0] = (c >> 18) | 0xF0;
166  buf[1] = ((c >> 12) & 0x3F) | 0x80;
167  buf[2] = ((c >> 6) & 0x3F) | 0x80;
168  buf[3] = (c & 0x3F) | 0x80;
169  }
170 #else
171  vstr_add_byte(vstr, c);
172 #endif
173 }
174 
175 void vstr_add_str(vstr_t *vstr, const char *str) {
176  vstr_add_strn(vstr, str, strlen(str));
177 }
178 
179 void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
180  vstr_ensure_extra(vstr, len);
181  memmove(vstr->buf + vstr->len, str, len);
182  vstr->len += len;
183 }
184 
185 STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
186  size_t l = vstr->len;
187  if (byte_pos > l) {
188  byte_pos = l;
189  }
190  if (byte_len > 0) {
191  // ensure room for the new bytes
192  vstr_ensure_extra(vstr, byte_len);
193  // copy up the string to make room for the new bytes
194  memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos);
195  // increase the length
196  vstr->len += byte_len;
197  }
198  return vstr->buf + byte_pos;
199 }
200 
201 void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
202  char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
203  *s = b;
204 }
205 
206 void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
207  // TODO UNICODE
208  char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
209  *s = chr;
210 }
211 
212 void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
213  vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
214 }
215 
216 void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
217  if (len > vstr->len) {
218  vstr->len = 0;
219  } else {
220  vstr->len -= len;
221  }
222 }
223 
224 void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
225  if (byte_pos >= vstr->len) {
226  return;
227  } else if (byte_pos + bytes_to_cut >= vstr->len) {
228  vstr->len = byte_pos;
229  } else {
230  memmove(vstr->buf + byte_pos, vstr->buf + byte_pos + bytes_to_cut, vstr->len - byte_pos - bytes_to_cut);
231  vstr->len -= bytes_to_cut;
232  }
233 }
234 
235 void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
236  va_list ap;
237  va_start(ap, fmt);
238  vstr_vprintf(vstr, fmt, ap);
239  va_end(ap);
240 }
241 
242 void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
243  mp_print_t print = {vstr, (mp_print_strn_t)vstr_add_strn};
244  mp_vprintf(&print, fmt, ap);
245 }
void vstr_init_print(vstr_t *vstr, size_t alloc, mp_print_t *print)
Definition: vstr.c:64
#define va_end(ap)
Definition: stdarg.h:8
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
Definition: runtime.c:1448
Definition: misc.h:142
#define assert(e)
Definition: assert.h:9
STATIC char * vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len)
Definition: vstr.c:185
mp_print_strn_t print_strn
Definition: mpprint.h:52
#define m_del(type, ptr, num)
Definition: misc.h:77
char * buf
Definition: misc.h:145
vstr_t * vstr_new(size_t alloc)
Definition: vstr.c:77
void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr)
Definition: vstr.c:206
void vstr_add_byte(vstr_t *vstr, byte b)
Definition: vstr.c:141
void vstr_clear(vstr_t *vstr)
Definition: vstr.c:70
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len)
Definition: vstr.c:179
size_t len
Definition: misc.h:144
void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut)
Definition: vstr.c:212
#define STATIC
Definition: mpconfig.h:1178
c(generic_all_nodes)
void vstr_printf(vstr_t *vstr, const char *fmt,...)
Definition: vstr.c:235
#define m_del_obj(type, ptr)
Definition: misc.h:80
size_t strlen(const char *s)
Definition: strlen.c:3
void vstr_free(vstr_t *vstr)
Definition: vstr.c:83
void * data
Definition: mpprint.h:51
#define NULL
Definition: stddef.h:4
void * memmove(void *dst, const void *src, size_t n)
Definition: memmove.c:3
#define ROUND_ALLOC(a)
Definition: vstr.c:37
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf)
Definition: vstr.c:57
void vstr_add_char(vstr_t *vstr, unichar c)
Definition: vstr.c:146
void vstr_add_str(vstr_t *vstr, const char *str)
Definition: vstr.c:175
void vstr_hint_size(vstr_t *vstr, size_t size)
Definition: vstr.c:120
void vstr_init(vstr_t *vstr, size_t alloc)
Definition: vstr.c:40
char * vstr_null_terminated_str(vstr_t *vstr)
Definition: vstr.c:132
char * vstr_add_len(vstr_t *vstr, size_t len)
Definition: vstr.c:124
void(* mp_print_strn_t)(void *data, const char *str, size_t len)
Definition: mpprint.h:48
unsigned char byte
Definition: misc.h:37
#define m_renew(type, ptr, old_num, new_num)
Definition: misc.h:75
void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b)
Definition: vstr.c:201
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut)
Definition: vstr.c:224
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap)
Definition: vstr.c:242
void vstr_cut_tail_bytes(vstr_t *vstr, size_t len)
Definition: vstr.c:216
__builtin_va_list va_list
Definition: stdarg.h:4
int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args)
Definition: mpprint.c:388
void vstr_init_len(vstr_t *vstr, size_t len)
Definition: vstr.c:52
bool fixed_buf
Definition: misc.h:146
char * vstr_extend(vstr_t *vstr, size_t size)
Definition: vstr.c:93
#define va_start(ap, last)
Definition: stdarg.h:6
#define m_new_obj(type)
Definition: misc.h:60
STATIC void vstr_ensure_extra(vstr_t *vstr, size_t size)
Definition: vstr.c:106
uint unichar
Definition: misc.h:119
size_t alloc
Definition: misc.h:143
#define m_new(type, num)
Definition: misc.h:57
const mp_obj_type_t mp_type_RuntimeError