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
279
280
281
|
/*
* Copyright 2007-2018 Adrian Thurston <thurston@colm.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <colm/pool.h>
#include <colm/pdarun.h>
#include <colm/bytecode.h>
str_t *string_prefix( program_t *prg, str_t *str, long len )
{
head_t *head = string_alloc_full( prg, str->value->data, len );
return (str_t*)construct_string( prg, head );
}
str_t *string_suffix( program_t *prg, str_t *str, long pos )
{
long len = str->value->length - pos;
head_t *head = string_alloc_full( prg, str->value->data + pos, len );
return (str_t*)construct_string( prg, head );
}
tree_t *construct_string( program_t *prg, head_t *s )
{
str_t *str = (str_t*) tree_allocate( prg );
str->id = LEL_ID_STR;
str->value = s;
return (tree_t*)str;
}
/*
* In this system strings are not null terminated. Often strings come from a
* parse, in which case the string is just a pointer into the the data stream.
* A block in a parsed stream can hold many tokens and there is no room
* allocated for nulls.
*/
head_t *string_copy( program_t *prg, head_t *head )
{
head_t *result = 0;
if ( head != 0 ) {
if ( (char*)(head+1) == head->data )
result = string_alloc_full( prg, head->data, head->length );
else
result = colm_string_alloc_pointer( prg, head->data, head->length );
if ( head->location != 0 ) {
result->location = location_allocate( prg );
result->location->name = head->location->name;
result->location->line = head->location->line;
result->location->column = head->location->column;
result->location->byte = head->location->byte;
}
}
return result;
}
void string_free( program_t *prg, head_t *head )
{
if ( head != 0 ) {
if ( head->location != 0 )
location_free( prg, head->location );
if ( (char*)(head+1) == head->data ) {
/* Full string allocation. */
free( head );
}
else {
/* Just a string head. */
head_free( prg, head );
}
}
}
const char *string_data( head_t *head )
{
if ( head == 0 )
return 0;
return head->data;
}
long string_length( head_t *head )
{
if ( head == 0 )
return 0;
return head->length;
}
void string_shorten( head_t *head, long newlen )
{
assert( newlen <= head->length );
head->length = newlen;
}
head_t *init_str_space( long length )
{
/* Find the length and allocate the space for the shared string. */
head_t *head = (head_t*) malloc( sizeof(head_t) + length );
/* Init the header. */
head->data = (char*)(head+1);
head->length = length;
head->location = 0;
/* Save the pointer to the data. */
return head;
}
/* Create from a c-style string. */
head_t *string_alloc_full( program_t *prg, const char *data, long length )
{
/* Init space for the data. */
head_t *head = init_str_space( length );
/* Copy in the data. */
memcpy( (head+1), data, length );
return head;
}
/* Create from a c-style string. */
head_t *colm_string_alloc_pointer( program_t *prg, const char *data, long length )
{
/* Find the length and allocate the space for the shared string. */
head_t *head = head_allocate( prg );
/* Init the header. */
head->data = data;
head->length = length;
return head;
}
head_t *concat_str( head_t *s1, head_t *s2 )
{
long s1Len = s1->length;
long s2Len = s2->length;
/* Init space for the data. */
head_t *head = init_str_space( s1Len + s2Len );
/* Copy in the data. */
memcpy( (head+1), s1->data, s1Len );
memcpy( (char*)(head+1) + s1Len, s2->data, s2Len );
return head;
}
head_t *string_to_upper( head_t *s )
{
/* Init space for the data. */
long len = s->length;
head_t *head = init_str_space( len );
/* Copy in the data. */
const char *src = s->data;
char *dst = (char*)(head+1);
int i;
for ( i = 0; i < len; i++ )
*dst++ = toupper( *src++ );
return head;
}
head_t *string_to_lower( head_t *s )
{
/* Init space for the data. */
long len = s->length;
head_t *head = init_str_space( len );
/* Copy in the data. */
const char *src = s->data;
char *dst = (char*)(head+1);
int i;
for ( i = 0; i < len; i++ )
*dst++ = tolower( *src++ );
return head;
}
/* Compare two strings. If identical returns 1, otherwise 0. */
word_t cmp_string( head_t *s1, head_t *s2 )
{
if ( s1->length < s2->length )
return -1;
else if ( s1->length > s2->length )
return 1;
else {
char *d1 = (char*)(s1->data);
char *d2 = (char*)(s2->data);
return memcmp( d1, d2, s1->length );
}
}
word_t str_atoi( head_t *str )
{
/* FIXME: need to implement this by hand. There is no null terminator. */
char *nulled = (char*)malloc( str->length + 1 );
memcpy( nulled, str->data, str->length );
nulled[str->length] = 0;
int res = atoi( nulled );
free( nulled );
return res;
}
word_t str_atoo( head_t *str )
{
/* FIXME: need to implement this by hand. There is no null terminator. */
char *nulled = (char*)malloc( str->length + 1 );
memcpy( nulled, str->data, str->length );
nulled[str->length] = 0;
int res = strtol( nulled, 0, 8 );
free( nulled );
return res;
}
head_t *int_to_str( program_t *prg, word_t i )
{
char data[20];
sprintf( data, "%ld", i );
return string_alloc_full( prg, data, strlen(data) );
}
word_t str_uord16( head_t *head )
{
uchar *data = (uchar*)(head->data);
ulong res;
res = (ulong)data[1];
res |= ((ulong)data[0]) << 8;
return res;
}
word_t str_uord8( head_t *head )
{
uchar *data = (uchar*)(head->data);
ulong res = (ulong)data[0];
return res;
}
head_t *make_literal( program_t *prg, long offset )
{
return colm_string_alloc_pointer( prg,
prg->rtd->litdata[offset],
prg->rtd->litlen[offset] );
}
head_t *string_sprintf( program_t *prg, str_t *format, long integer )
{
head_t *format_head = format->value;
long written = snprintf( 0, 0, string_data(format_head), integer );
head_t *head = init_str_space( written+1 );
written = snprintf( (char*)head->data, written+1, string_data(format_head), integer );
head->length -= 1;
return head;
}
|