| 1 |
% This file is part of MetaPost; |
| 2 |
% the MetaPost program is in the public domain. |
| 3 |
% See the <Show version...> code in mpost.w for more info. |
| 4 |
|
| 5 |
\font\tenlogo=logo10 % font used for the METAFONT logo |
| 6 |
\font\logos=logosl10 |
| 7 |
\def\MF{{\tenlogo META}\-{\tenlogo FONT}} |
| 8 |
\def\MP{{\tenlogo META}\-{\tenlogo POST}} |
| 9 |
\def\pct!{{\char`\%}} % percent sign in ordinary text |
| 10 |
\def\psqrt#1{\sqrt{\mathstrut#1}} |
| 11 |
|
| 12 |
|
| 13 |
@* String handling. |
| 14 |
|
| 15 |
|
| 16 |
@ First, we will need some stuff from other files. |
| 17 |
@c |
| 18 |
#include "mpconfig.h" |
| 19 |
#include <w2c/config.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
#include <stdarg.h> |
| 24 |
#include <assert.h> |
| 25 |
#ifdef HAVE_UNISTD_H |
| 26 |
# include <unistd.h> /* for access */ |
| 27 |
#endif |
| 28 |
#include <time.h> /* for struct tm \& co */ |
| 29 |
#include "mpstrings.h" /* internal header */ |
| 30 |
|
| 31 |
@ Then there is some stuff we need to prepare ourselves. |
| 32 |
|
| 33 |
@(mpstrings.h@>= |
| 34 |
#ifndef MPSTRINGS_H |
| 35 |
#define MPSTRINGS_H 1 |
| 36 |
#include "mplib.h" |
| 37 |
#include "mplibps.h" /* external header */ |
| 38 |
#include "mplibsvg.h" /* external header */ |
| 39 |
#include "mpmp.h" /* internal header */ |
| 40 |
#include "mppsout.h" /* internal header */ |
| 41 |
#include "mpsvgout.h" /* internal header */ |
| 42 |
#include "mpmath.h" /* internal header */ |
| 43 |
@<Definitions@> |
| 44 |
#endif |
| 45 |
|
| 46 |
@ Here are the functions needed for the avl construction. |
| 47 |
|
| 48 |
@<Definitions@>= |
| 49 |
void *copy_strings_entry (const void *p); |
| 50 |
|
| 51 |
@ An earlier version of this function used |strncmp|, but that produces |
| 52 |
wrong results in some cases. |
| 53 |
@c |
| 54 |
#define STRCMP_RESULT(a) ((a)<0 ? -1 : ((a)>0 ? 1 : 0)) |
| 55 |
static int comp_strings_entry (void *p, const void *pa, const void *pb) { |
| 56 |
const mp_lstring *a = (const mp_lstring *) pa; |
| 57 |
const mp_lstring *b = (const mp_lstring *) pb; |
| 58 |
size_t l; |
| 59 |
unsigned char *s,*t; |
| 60 |
(void) p; |
| 61 |
s = a->str; |
| 62 |
t = b->str; |
| 63 |
l = (a->len<=b->len ? a->len : b->len); |
| 64 |
while ( l-->0 ) { |
| 65 |
if ( *s!=*t) |
| 66 |
return STRCMP_RESULT(*s-*t); |
| 67 |
s++; t++; |
| 68 |
} |
| 69 |
return STRCMP_RESULT((int)(a->len-b->len)); |
| 70 |
} |
| 71 |
void *copy_strings_entry (const void *p) { |
| 72 |
mp_string ff; |
| 73 |
const mp_lstring *fp; |
| 74 |
fp = (const mp_lstring *) p; |
| 75 |
ff = malloc (sizeof (mp_lstring)); |
| 76 |
if (ff == NULL) |
| 77 |
return NULL; |
| 78 |
ff->str = malloc (fp->len + 1); |
| 79 |
if (ff->str == NULL) { |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
memcpy ((char *) ff->str, (char *) fp->str, fp->len + 1); |
| 83 |
ff->len = fp->len; |
| 84 |
ff->refs = 0; |
| 85 |
return ff; |
| 86 |
} |
| 87 |
static void *delete_strings_entry (void *p) { |
| 88 |
mp_string ff = (mp_string) p; |
| 89 |
mp_xfree (ff->str); |
| 90 |
mp_xfree (ff); |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
@ Actually creating strings is done by |make_string|, but in order to |
| 95 |
do so it needs a way to create a new, empty string structure. |
| 96 |
|
| 97 |
@ @c |
| 98 |
static mp_string new_strings_entry (MP mp) { |
| 99 |
mp_string ff; |
| 100 |
ff = mp_xmalloc (mp, 1, sizeof (mp_lstring)); |
| 101 |
ff->str = NULL; |
| 102 |
ff->len = 0; |
| 103 |
ff->refs = 0; |
| 104 |
return ff; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
@ Some even more low-level functions are these: |
| 109 |
|
| 110 |
@<Definitions@>= |
| 111 |
extern int mp_xstrcmp (const char *a, const char *b); |
| 112 |
extern char *mp_xstrdup (MP mp, const char *s); |
| 113 |
extern char *mp_xstrldup (MP mp, const char *s, size_t l); |
| 114 |
extern char *mp_strdup (const char *p); |
| 115 |
extern char *mp_strldup (const char *p, size_t l); |
| 116 |
|
| 117 |
@ @c |
| 118 |
char *mp_strldup (const char *p, size_t l) { |
| 119 |
char *r, *s; |
| 120 |
if (p == NULL) |
| 121 |
return NULL; |
| 122 |
r = malloc ((size_t) (l * sizeof (char) + 1)); |
| 123 |
if (r == NULL) |
| 124 |
return NULL; |
| 125 |
s = memcpy (r, p, (size_t) (l)); |
| 126 |
*(s + l) = '\0'; |
| 127 |
return s; |
| 128 |
} |
| 129 |
char *mp_strdup (const char *p) { |
| 130 |
if (p == NULL) |
| 131 |
return NULL; |
| 132 |
return mp_strldup (p, strlen (p)); |
| 133 |
} |
| 134 |
|
| 135 |
@ @c |
| 136 |
int mp_xstrcmp (const char *a, const char *b) { |
| 137 |
if (a == NULL && b == NULL) |
| 138 |
return 0; |
| 139 |
if (a == NULL) |
| 140 |
return -1; |
| 141 |
if (b == NULL) |
| 142 |
return 1; |
| 143 |
return strcmp (a, b); |
| 144 |
} |
| 145 |
char *mp_xstrldup (MP mp, const char *s, size_t l) { |
| 146 |
char *w; |
| 147 |
if (s == NULL) |
| 148 |
return NULL; |
| 149 |
w = mp_strldup (s, l); |
| 150 |
if (w == NULL) { |
| 151 |
mp_fputs ("Out of memory!\n", mp->err_out); |
| 152 |
mp->history = mp_system_error_stop; |
| 153 |
mp_jump_out (mp); |
| 154 |
} |
| 155 |
return w; |
| 156 |
} |
| 157 |
char *mp_xstrdup (MP mp, const char *s) { |
| 158 |
if (s == NULL) |
| 159 |
return NULL; |
| 160 |
return mp_xstrldup (mp, s, strlen (s)); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
@ @c |
| 165 |
void mp_initialize_strings (MP mp) { |
| 166 |
mp->strings = avl_create (comp_strings_entry, |
| 167 |
copy_strings_entry, |
| 168 |
delete_strings_entry, malloc, free, NULL); |
| 169 |
mp->cur_string = NULL; |
| 170 |
mp->cur_length = 0; |
| 171 |
mp->cur_string_size = 0; |
| 172 |
} |
| 173 |
|
| 174 |
@ @c |
| 175 |
void mp_dealloc_strings (MP mp) { |
| 176 |
if (mp->strings != NULL) |
| 177 |
avl_destroy (mp->strings); |
| 178 |
mp->strings = NULL; |
| 179 |
mp_xfree (mp->cur_string); |
| 180 |
mp->cur_string = NULL; |
| 181 |
mp->cur_length = 0; |
| 182 |
mp->cur_string_size = 0; |
| 183 |
} |
| 184 |
|
| 185 |
@ Here are the definitions: |
| 186 |
@<Definitions@>= |
| 187 |
extern void mp_initialize_strings (MP mp); |
| 188 |
extern void mp_dealloc_strings (MP mp); |
| 189 |
|
| 190 |
@ Most printing is done from |char *|s, but sometimes not. Here are |
| 191 |
functions that convert an internal string into a |char *| for use |
| 192 |
by the printing routines, and vice versa. |
| 193 |
|
| 194 |
@<Definitions@>= |
| 195 |
char *mp_str (MP mp, mp_string s); |
| 196 |
mp_string mp_rtsl (MP mp, const char *s, size_t l); |
| 197 |
mp_string mp_rts (MP mp, const char *s); |
| 198 |
mp_string mp_make_string (MP mp); |
| 199 |
|
| 200 |
@ @c |
| 201 |
char *mp_str (MP mp, mp_string ss) { |
| 202 |
(void) mp; |
| 203 |
return (char *) ss->str; |
| 204 |
} |
| 205 |
|
| 206 |
@ @c |
| 207 |
mp_string mp_rtsl (MP mp, const char *s, size_t l) { |
| 208 |
mp_string str, nstr; |
| 209 |
str = new_strings_entry (mp); |
| 210 |
str->str = (unsigned char *)mp_xstrldup (mp, s, l); |
| 211 |
str->len = l; |
| 212 |
nstr = (mp_string) avl_find (str, mp->strings); |
| 213 |
if (nstr == NULL) { /* not yet known */ |
| 214 |
assert (avl_ins (str, mp->strings, avl_false) > 0); |
| 215 |
nstr = (mp_string) avl_find (str, mp->strings); |
| 216 |
} |
| 217 |
(void)delete_strings_entry(str); |
| 218 |
add_str_ref(nstr); |
| 219 |
return nstr; |
| 220 |
} |
| 221 |
|
| 222 |
@ @c |
| 223 |
mp_string mp_rts (MP mp, const char *s) { |
| 224 |
return mp_rtsl (mp, s, strlen (s)); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
@ Strings are created by appending character codes to |cur_string|. |
| 229 |
The |append_char| macro, defined here, does not check to see if the |
| 230 |
buffer overflows; this test is supposed to be |
| 231 |
made before |append_char| is used. |
| 232 |
|
| 233 |
To test if there is room to append |l| more characters to |cur_string|, |
| 234 |
we shall write |str_room(l)|, which tries to make sure there is enough room |
| 235 |
in the |cur_string|. |
| 236 |
|
| 237 |
@<Definitions@>= |
| 238 |
#define EXTRA_STRING 500 |
| 239 |
#define append_char(A) do { \ |
| 240 |
str_room(1); \ |
| 241 |
*(mp->cur_string+mp->cur_length)=(unsigned char)(A); \ |
| 242 |
mp->cur_length++; \ |
| 243 |
} while (0) |
| 244 |
#define str_room(wsize) do { \ |
| 245 |
size_t nsize; \ |
| 246 |
if ((mp->cur_length+(size_t)wsize) > mp->cur_string_size) { \ |
| 247 |
nsize = mp->cur_string_size + mp->cur_string_size / 5 + EXTRA_STRING; \ |
| 248 |
if (nsize < (size_t)(wsize)) { \ |
| 249 |
nsize = (size_t)wsize + EXTRA_STRING; \ |
| 250 |
} \ |
| 251 |
mp->cur_string = (unsigned char *) mp_xrealloc(mp, mp->cur_string, (unsigned)nsize, sizeof(unsigned char)); \ |
| 252 |
memset (mp->cur_string+mp->cur_length,0,(nsize-mp->cur_length)); \ |
| 253 |
mp->cur_string_size = nsize; \ |
| 254 |
} \ |
| 255 |
} while (0) |
| 256 |
|
| 257 |
|
| 258 |
@ At the very start of the metapost run and each time after |
| 259 |
|make_string| has stored a new string in the avl tree, the |
| 260 |
|cur_string| variable has to be prepared so that it will be ready to |
| 261 |
start creating a new string. The initial size is fairly arbitrary, but |
| 262 |
setting it a little higher than expected helps prevent |reallocs|. |
| 263 |
|
| 264 |
@<Definitions@>= |
| 265 |
void mp_reset_cur_string (MP mp); |
| 266 |
|
| 267 |
@ @c |
| 268 |
void mp_reset_cur_string (MP mp) { |
| 269 |
mp_xfree (mp->cur_string); |
| 270 |
mp->cur_length = 0; |
| 271 |
mp->cur_string_size = 63; |
| 272 |
mp->cur_string = (unsigned char *) mp_xmalloc (mp, 64, sizeof (unsigned char)); |
| 273 |
memset (mp->cur_string, 0, 64); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
@ \MP's string expressions are implemented in a brute-force way: Every |
| 278 |
new string or substring that is needed is simply stored into the string pool. |
| 279 |
Space is eventually reclaimed using the aid of a simple system system |
| 280 |
of reference counts. |
| 281 |
@^reference counts@> |
| 282 |
|
| 283 |
The number of references to string number |s| will be |s->refs|. The |
| 284 |
special value |s->refs=MAX_STR_REF=127| is used to denote an unknown |
| 285 |
positive number of references; such strings will never be recycled. If |
| 286 |
a string is ever referred to more than 126 times, simultaneously, we |
| 287 |
put it in this category. |
| 288 |
|
| 289 |
@<Definitions@>= |
| 290 |
#define MAX_STR_REF 127 /* ``infinite'' number of references */ |
| 291 |
#define add_str_ref(A) { if ( (A)->refs < MAX_STR_REF ) ((A)->refs)++; } |
| 292 |
|
| 293 |
@ Here's what we do when a string reference disappears: |
| 294 |
|
| 295 |
@<Definitions@>= |
| 296 |
#define delete_str_ref(A) do { \ |
| 297 |
if ( (A)->refs < MAX_STR_REF ) { \ |
| 298 |
if ( (A)->refs > 1 ) ((A)->refs)--; \ |
| 299 |
else mp_flush_string(mp, (A)); \ |
| 300 |
} \ |
| 301 |
} while (0) |
| 302 |
|
| 303 |
@ @<Definitions@>= |
| 304 |
void mp_flush_string (MP mp, mp_string s); |
| 305 |
|
| 306 |
@ @c |
| 307 |
void mp_flush_string (MP mp, mp_string s) { |
| 308 |
if (s->refs == 0) { |
| 309 |
mp->strs_in_use--; |
| 310 |
mp->pool_in_use = mp->pool_in_use - (integer) s->len; |
| 311 |
(void) avl_del (s, mp->strings, NULL); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
@ Some C literals that are used as values cannot be simply added, |
| 317 |
their reference count has to be set such that they can not be flushed. |
| 318 |
|
| 319 |
@c |
| 320 |
mp_string mp_intern (MP mp, const char *s) { |
| 321 |
mp_string r; |
| 322 |
r = mp_rts (mp, s); |
| 323 |
r->refs = MAX_STR_REF; |
| 324 |
return r; |
| 325 |
} |
| 326 |
|
| 327 |
@ @<Definitions@>= |
| 328 |
mp_string mp_intern (MP mp, const char *s); |
| 329 |
|
| 330 |
|
| 331 |
@ Once a sequence of characters has been appended to |cur_string|, it |
| 332 |
officially becomes a string when the function |make_string| is called. |
| 333 |
This function returns a pointer to the new string as its value. |
| 334 |
|
| 335 |
@<Definitions@>= |
| 336 |
mp_string mp_make_string (MP mp); |
| 337 |
|
| 338 |
@ @c |
| 339 |
mp_string mp_make_string (MP mp) { /* current string enters the pool */ |
| 340 |
mp_string str; |
| 341 |
mp_lstring tmp; |
| 342 |
tmp.str = mp->cur_string; |
| 343 |
tmp.len = mp->cur_length; |
| 344 |
str = (mp_string) avl_find (&tmp, mp->strings); |
| 345 |
if (str == NULL) { /* not yet known */ |
| 346 |
str = mp_xmalloc (mp, 1, sizeof (mp_lstring)); |
| 347 |
str->str = mp->cur_string; |
| 348 |
str->len = tmp.len; |
| 349 |
assert (avl_ins (str, mp->strings, avl_false) > 0); |
| 350 |
str = (mp_string) avl_find (&tmp, mp->strings); |
| 351 |
mp->pool_in_use = mp->pool_in_use + (integer) str->len; |
| 352 |
if (mp->pool_in_use > mp->max_pl_used) |
| 353 |
mp->max_pl_used = mp->pool_in_use; |
| 354 |
mp->strs_in_use++; |
| 355 |
if (mp->strs_in_use > mp->max_strs_used) |
| 356 |
mp->max_strs_used = mp->strs_in_use; |
| 357 |
} |
| 358 |
add_str_ref(str); |
| 359 |
mp_reset_cur_string (mp); |
| 360 |
return str; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
@ Here is a routine that compares two strings in the string pool, |
| 365 |
and it does not assume that they have the same length. If the first string |
| 366 |
is lexicographically greater than, less than, or equal to the second, |
| 367 |
the result is respectively positive, negative, or zero. |
| 368 |
|
| 369 |
@<Definitions@>= |
| 370 |
integer mp_str_vs_str (MP mp, mp_string s, mp_string t); |
| 371 |
|
| 372 |
@ @c |
| 373 |
integer mp_str_vs_str (MP mp, mp_string s, mp_string t) { |
| 374 |
(void) mp; |
| 375 |
return comp_strings_entry (NULL, (const void *) s, (const void *) t); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
@ @<Definitions@>= |
| 381 |
mp_string mp_cat (MP mp, mp_string a, mp_string b); |
| 382 |
|
| 383 |
@ @c |
| 384 |
mp_string mp_cat (MP mp, mp_string a, mp_string b) { |
| 385 |
mp_string str; |
| 386 |
size_t needed; |
| 387 |
size_t saved_cur_length = mp->cur_length; |
| 388 |
unsigned char *saved_cur_string = mp->cur_string; |
| 389 |
size_t saved_cur_string_size = mp->cur_string_size; |
| 390 |
needed = a->len + b->len; |
| 391 |
mp->cur_length = 0; |
| 392 |
/*| mp->cur_string = NULL;| */ /* needs malloc, spotted by clang */ |
| 393 |
mp->cur_string = (unsigned char *) mp_xmalloc (mp, needed+1, sizeof (unsigned char)); |
| 394 |
mp->cur_string_size = 0; |
| 395 |
str_room (needed+1); |
| 396 |
(void) memcpy (mp->cur_string, a->str, a->len); |
| 397 |
(void) memcpy (mp->cur_string + a->len, b->str, b->len); |
| 398 |
mp->cur_length = needed; |
| 399 |
mp->cur_string[needed] = '\0'; |
| 400 |
str = mp_make_string (mp); |
| 401 |
mp_xfree(mp->cur_string); /* created by |mp_make_string| */ |
| 402 |
mp->cur_length = saved_cur_length; |
| 403 |
mp->cur_string = saved_cur_string; |
| 404 |
mp->cur_string_size = saved_cur_string_size; |
| 405 |
return str; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
@ @<Definitions@>= |
| 410 |
mp_string mp_chop_string (MP mp, mp_string s, integer a, integer b); |
| 411 |
|
| 412 |
@ @c |
| 413 |
mp_string mp_chop_string (MP mp, mp_string s, integer a, integer b) { |
| 414 |
integer l; /* length of the original string */ |
| 415 |
integer k; /* runs from |a| to |b| */ |
| 416 |
boolean reversed; /* was |a>b|? */ |
| 417 |
if (a <= b) |
| 418 |
reversed = false; |
| 419 |
else { |
| 420 |
reversed = true; |
| 421 |
k = a; |
| 422 |
a = b; |
| 423 |
b = k; |
| 424 |
} |
| 425 |
l = (integer) s->len; |
| 426 |
if (a < 0) { |
| 427 |
a = 0; |
| 428 |
if (b < 0) |
| 429 |
b = 0; |
| 430 |
} |
| 431 |
if (b > l) { |
| 432 |
b = l; |
| 433 |
if (a > l) |
| 434 |
a = l; |
| 435 |
} |
| 436 |
str_room ((size_t) (b - a)); |
| 437 |
if (reversed) { |
| 438 |
for (k = b - 1; k >= a; k--) { |
| 439 |
append_char (*(s->str + k)); |
| 440 |
} |
| 441 |
} else { |
| 442 |
for (k = a; k < b; k++) { |
| 443 |
append_char (*(s->str + k)); |
| 444 |
} |
| 445 |
} |
| 446 |
return mp_make_string (mp); |
| 447 |
} |
| 448 |
|