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
|
/*
* SPL - The SPL Programming Language
* Copyright (C) 2004, 2005 Clifford Wolf <clifford@clifford.at>
*
* 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 2 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* string.c: SPL string implementation
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include "spl.h"
#include "compat.h"
#define newlen(a,b) (a+b < a || a+b >= ~0U >> 1 ? ~0U : a+b)
struct spl_string *spl_string_new(int flags, struct spl_string *left, struct spl_string *right, char *text, struct spl_code *code)
{
struct spl_string *s = calloc(1, sizeof(struct spl_string));
if (flags & SPL_STRING_NOAUTOGET) {
s->left = left;
s->right = right;
} else {
s->left = spl_string_get(left);
s->right = spl_string_get(right);
}
if (s->left) {
s->left_len = s->left->total_len;
if (s->left->flags & SPL_STRING_UTF8)
s->flags |= SPL_STRING_UTF8;
}
if (s->right) {
s->right_len = s->right->total_len;
if (s->right->flags & SPL_STRING_UTF8)
s->flags |= SPL_STRING_UTF8;
}
s->flags = flags & ~SPL_STRING_NOAUTOGET;
s->ref_counter = 1;
if (text) {
int i = 0;
while (text[i])
if (text[i++] & 0x80)
s->flags |= SPL_STRING_UTF8;
s->text = text;
s->text_len = i;
}
if (code) {
s->code = spl_code_get(code);
s->flags |= SPL_STRING_STATIC;
}
s->total_len = newlen(s->total_len, s->left_len);
s->total_len = newlen(s->total_len, s->text_len);
s->total_len = newlen(s->total_len, s->right_len);
if (s->flags & SPL_STRING_DOTCAT)
s->total_len = newlen(s->total_len, 1);
return s;
}
struct spl_string *spl_string_printf(int flags, struct spl_string *left, struct spl_string *right, const char *fmt, ...)
{
char *text = 0;
if (fmt) {
va_list ap;
va_start(ap, fmt);
my_vasprintf(&text, fmt, ap);
va_end(ap);
}
return spl_string_new(flags, left, right, text, 0);
}
struct spl_string *spl_string_split(struct spl_string *s, unsigned int offset, unsigned int len)
{
if (!s) {
assert(len == 0);
return 0;
}
assert(offset+len <= s->total_len);
if (len < 128) {
char *text = spl_string(s);
return spl_string_new(0, 0, 0, my_strndup(text+offset, len), 0);
} else {
struct spl_string *r = spl_string_new(0, s, 0, 0, 0);
r->left_offset = offset;
r->left_len = len;
r->total_len = len;
return r;
}
}
struct spl_string *spl_string_get(struct spl_string *s)
{
if (s)
s->ref_counter++;
return s;
}
void spl_string_put(struct spl_string *s)
{
if (s && --(s->ref_counter) == 0) {
spl_string_put(s->left);
spl_string_put(s->right);
spl_code_put(s->code);
if ((s->flags & SPL_STRING_STATIC) == 0 && s->text)
free(s->text);
free(s);
}
}
static char *spl_string_write(struct spl_string *s, int *flags_p, char *t, unsigned int offset, unsigned int len)
{
if (!s) return t;
assert(offset+len <= s->total_len);
if (s->left_len > offset) {
unsigned int left_len = s->left_len - offset;
if (left_len > len)
left_len = len;
if (left_len) {
t = spl_string_write(s->left, flags_p, t, s->left_offset + offset, left_len);
len -= left_len;
}
offset = 0;
} else
offset -= s->left_len;
if ((s->flags & SPL_STRING_DOTCAT) != 0) {
if (offset) {
offset--;
} else {
*(t++) = '.';
len--;
}
}
if (s->text_len > offset) {
unsigned int text_len = s->text_len - offset;
if (text_len > len)
text_len = len;
if (text_len) {
int found_utf8 = 0;
for (unsigned int i=0; i<text_len; i++)
if ((t[i] = s->text[offset+i]) & 0x80)
found_utf8 = 1;
if (found_utf8)
*flags_p |= SPL_STRING_UTF8;
len -= text_len;
t += text_len;
}
offset = 0;
} else
offset -= s->text_len;
return spl_string_write(s->right, flags_p, t, offset, len);
}
char *spl_string(struct spl_string *s)
{
if (!s) return "";
if (!s->left && !s->right) return s->text ? s->text : "";
s->flags &= ~SPL_STRING_UTF8;
assert(s->total_len != ~0U);
char *newtext = malloc(s->total_len + 1);
*(spl_string_write(s, &s->flags, newtext, 0, s->total_len)) = 0;
spl_string_put(s->left);
spl_string_put(s->right);
spl_code_put(s->code);
s->left = s->right = 0;
s->left_offset = s->right_offset = 0;
s->left_len = s->right_len = 0;
s->code = 0;
if ( (s->flags & SPL_STRING_STATIC) == 0 && s->text )
free(s->text);
s->text_len = s->total_len;
s->text = newtext;
s->flags = s->flags & ~(SPL_STRING_STATIC|SPL_STRING_DOTCAT);
return newtext;
}
|