1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: zstring.c 8250 2007-09-25 13:31:24Z giles $ */
15
/* String operators */
25
/* The generic operators (copy, get, put, getinterval, putinterval, */
26
/* length, and forall) are implemented in zgeneric.c. */
28
/* <int> .bytestring <bytestring> */
30
zbytestring(i_ctx_t *i_ctx_p)
36
check_int_leu(*op, max_int);
37
size = (uint)op->value.intval;
38
sbody = ialloc_bytes(size, ".bytestring");
40
return_error(e_VMerror);
41
make_astruct(op, a_all | icurrent_space, sbody);
42
memset(sbody, 0, size);
46
/* <int> string <string> */
48
zstring(i_ctx_t *i_ctx_p)
54
check_type(*op, t_integer);
55
if (op->value.intval < 0 )
56
return_error(e_rangecheck);
57
if (op->value.intval > max_string_size )
58
return_error(e_limitcheck); /* to match Distiller */
59
size = op->value.intval;
60
sbody = ialloc_string(size, "string");
62
return_error(e_VMerror);
63
make_string(op, a_all | icurrent_space, size, sbody);
64
memset(sbody, 0, size);
68
/* <name> .namestring <string> */
70
znamestring(i_ctx_t *i_ctx_p)
74
check_type(*op, t_name);
75
name_string_ref(imemory, op, op);
79
/* <string> <pattern> anchorsearch <post> <match> -true- */
80
/* <string> <pattern> anchorsearch <string> -false- */
82
zanchorsearch(i_ctx_t *i_ctx_p)
86
uint size = r_size(op);
88
check_read_type(*op, t_string);
89
check_read_type(*op1, t_string);
90
if (size <= r_size(op1) && !memcmp(op1->value.bytes, op->value.bytes, size)) {
95
r_set_size(op0, size);
96
op1->value.bytes += size;
97
r_dec_size(op1, size);
104
/* <string> <pattern> search <post> <match> <pre> -true- */
105
/* <string> <pattern> search <string> -false- */
107
zsearch(i_ctx_t *i_ctx_p)
111
uint size = r_size(op);
117
check_read_type(*op1, t_string);
118
check_read_type(*op, t_string);
119
if (size > r_size(op1)) { /* can't match */
123
count = r_size(op1) - size;
124
ptr = op1->value.bytes;
127
pat = op->value.bytes;
130
if (*ptr == ch && (size == 1 || !memcmp(ptr, pat, size)))
139
op->tas.type_attrs = op1->tas.type_attrs;
140
op->value.bytes = ptr;
141
r_set_size(op, size);
144
r_set_size(op - 1, ptr - op[-1].value.bytes);
145
op1->value.bytes = ptr + size;
146
r_set_size(op1, count);
151
/* <string> <charstring> .stringbreak <int|null> */
153
zstringbreak(i_ctx_t *i_ctx_p)
158
check_read_type(op[-1], t_string);
159
check_read_type(*op, t_string);
160
/* We can't use strpbrk here, because C doesn't allow nulls in strings. */
161
for (i = 0; i < r_size(op - 1); ++i)
162
for (j = 0; j < r_size(op); ++j)
163
if (op[-1].value.const_bytes[i] == op->value.const_bytes[j]) {
173
/* <obj> <pattern> .stringmatch <bool> */
175
zstringmatch(i_ctx_t *i_ctx_p)
181
check_read_type(*op, t_string);
182
switch (r_type(op1)) {
187
name_string_ref(imemory, op1, op1); /* can't fail */
189
result = string_match(op1->value.const_bytes, r_size(op1),
190
op->value.const_bytes, r_size(op),
194
result = (r_size(op) == 1 && *op->value.bytes == '*');
196
make_bool(op1, result);
201
/* ------ Initialization procedure ------ */
203
const op_def zstring_op_defs[] =
205
{"1.bytestring", zbytestring},
206
{"2anchorsearch", zanchorsearch},
207
{"1.namestring", znamestring},
208
{"2search", zsearch},
209
{"1string", zstring},
210
{"2.stringbreak", zstringbreak},
211
{"2.stringmatch", zstringmatch},