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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/*
* Copyright © 2018 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "snek.h"
snek_poly_t
snek_string_make(char c)
{
char *new = snek_alloc(2);
if (new)
new[0] = c;
return snek_string_to_poly(new);
}
#ifdef SNEK_STRING_BUILD
snek_poly_t
snek_string_build(const char *s)
{
char *new = snek_alloc(strlen(s) + 1);
if (new)
strcpy(new, s);
return snek_string_to_poly(new);
}
#endif
snek_poly_t
snek_string_get(char *string, snek_poly_t p, bool report_error)
{
snek_soffset_t so = snek_poly_get_soffset(p);
snek_offset_t len = strlen(string);
snek_offset_t o;
o = (snek_offset_t) so;
if (so < 0)
o = len - (-so);
if (len <= o) {
if (report_error)
snek_error_value(p);
return SNEK_NULL;
}
return snek_string_make(string[o]);
}
static char *
snek_string_catn(char *a, snek_offset_t aoff, snek_offset_t alen,
char *b, snek_offset_t boff, snek_offset_t blen)
{
char *new;
snek_stack_push_string(a);
snek_stack_push_string(b);
new = snek_alloc(alen + blen + 1);
b = snek_stack_pop_string(b);
a = snek_stack_pop_string(a);
if (new) {
memcpy(new, a + aoff, alen);
memcpy(new + alen, b + boff, blen);
new[alen+blen] = '\0';
}
return new;
}
snek_poly_t
snek_string_cat(char *a, char *b)
{
return snek_string_to_poly(snek_string_catn(a, 0, strlen(a),
b, 0, strlen(b)));
}
#ifndef SNEK_NO_SLICE
char *
snek_string_slice(char *a, snek_slice_t *slice)
{
if (slice->identity)
return a;
snek_stack_push_string(a);
char *r = snek_alloc(slice->count + 1);
a = snek_stack_pop_string(a);
if (!r)
return NULL;
snek_offset_t i = 0;
for (; snek_slice_test(slice); snek_slice_step(slice))
r[i++] = a[slice->pos];
r[i] = '\0';
return r;
}
#endif
snek_poly_t
snek_string_times(char *a, snek_soffset_t b)
{
snek_offset_t alen = strlen(a);
char *s = snek_alloc(alen * b + 1);
if (s) {
char *t = s;
while (b--) {
memcpy(t, a, alen);
t += alen;
}
*t = '\0';
}
return snek_string_to_poly(s);
}
static snek_offset_t
snek_next_format(char *a)
{
char *percent = strchr(a, '%');
if (percent)
return percent - a;
return strlen(a);
}
static char *
snek_buf_realloc(char **str_p, snek_offset_t add)
{
char *old = *str_p;
char *new;
snek_offset_t len = old ? strlen(old) : 0;
snek_stack_push_string(old);
new = snek_alloc(len + add + 1);
old = snek_stack_pop_string(old);
if (!new)
return NULL;
memcpy(new, old, len);
new[len+add] = '\0';
*str_p = new;
return new + len;
}
static int
snek_buf_sprintc(int c, void *closure)
{
char **str_p = closure;
char *new;
if ((new = snek_buf_realloc(str_p, 1)) != NULL)
*new = c;
return 0;
}
static int
snek_buf_sprints(const char *s, void *closure)
{
char **str_p = closure;
char *new;
int len = strlen(s);
snek_stack_push_string(s);
new = snek_buf_realloc(str_p, len);
s = snek_stack_pop_string(s);
if (new)
memcpy(new, s, len);
return 0;
}
snek_poly_t
snek_string_interpolate(char *a, snek_poly_t poly)
{
snek_offset_t percent = 0;
char *result = NULL;
snek_offset_t o = 0;
bool is_list;
snek_offset_t size;
snek_buf_t buf = {
.put_c = snek_buf_sprintc,
.put_s = snek_buf_sprints,
.closure = &result
};
size = 1;
is_list = false;
if (snek_poly_type(poly) == snek_list) {
snek_list_t *list = snek_poly_to_list(poly);
#ifndef SNEK_NO_DICT
if (snek_list_type(list) != snek_list_dict)
#endif
{
is_list = true;
size = list->size;
}
}
while (a[percent]) {
snek_offset_t next = snek_next_format(a + percent) + percent;
snek_stack_push(poly);
snek_stack_push_string(a);
result = snek_string_catn(result, 0, result ? strlen(result) : 0,
a, percent, next-percent);
a = snek_stack_pop_string(a);
poly = snek_stack_pop();
percent = next;
if (a[percent] == '%') {
percent++;
char format = a[percent];
snek_stack_push(poly);
snek_stack_push_string(a);
if (format == '\0') {
o = size + 1;
} else {
percent++;
if (format == '%') {
snek_buf_sprintc('%', &result);
} else {
snek_poly_t *data = &poly;
if (is_list)
data = snek_list_data(snek_poly_to_list(poly));
if (o >= size)
o = size + 1;
else
snek_poly_format(&buf, data[o++], format);
}
}
a = snek_stack_pop_string(a);
poly = snek_stack_pop();
}
}
if (o != size)
return SNEK_INVALID;
return snek_string_to_poly(result);
}
#ifdef SNEK_BUILTIN_str
snek_poly_t
snek_builtin_str(snek_poly_t a)
{
char *result = NULL;
snek_buf_t buf = {
.put_c = snek_buf_sprintc,
.put_s = snek_buf_sprints,
.closure = &result
};
snek_poly_format(&buf, a, 's');
return snek_string_to_poly(result);
}
#endif
void
snek_stack_push_string(const char *s)
{
if (snek_is_pool_addr(s))
snek_stack_push(snek_string_to_poly((char *) s));
}
char *
snek_stack_pop_string(const char *s)
{
if (snek_is_pool_addr(s))
return snek_poly_to_string(snek_stack_pop());
return (char *) s;
}
snek_offset_t
snek_string_size(void *addr)
{
char *string = addr;
return (snek_offset_t) strlen(string) + 1;
}
void
snek_string_mark_move(void *addr)
{
(void) addr;
}
|