2
* Copyright (c) 2010 Jiri Svoboda
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
/** @file String builtin binding. */
34
#include "../bigint.h"
35
#include "../builtin.h"
37
#include "../mytypes.h"
41
#include "../strtab.h"
43
#include "bi_string.h"
45
static void bi_string_length(run_t *run);
46
static void bi_string_slice(run_t *run);
48
/** Declare String builtin.
50
* @param bi Builtin object
52
void bi_string_declare(builtin_t *bi)
57
/** Bind String builtin.
59
* @param bi Builtin object
61
void bi_string_bind(builtin_t *bi)
63
builtin_fun_bind(bi, "String", "get_length", bi_string_length);
64
builtin_fun_bind(bi, "String", "Slice", bi_string_slice);
67
/** Return length of the string.
69
* @param run Runner object
71
static void bi_string_length(run_t *run)
73
rdata_var_t *self_value_var;
82
run_proc_ar_t *proc_ar;
84
/* Extract self.Value */
85
self_value_var = builtin_get_self_mbr_var(run, "Value");
86
assert(self_value_var->vc == vc_string);
87
str = self_value_var->u.string_v->value;
88
str_l = os_str_length(str);
90
#ifdef DEBUG_RUN_TRACE
91
printf("Get length of string '%s'.\n", str);
94
/* Construct return value. */
95
rint = rdata_int_new();
96
bigint_init(&rint->value, (int) str_l);
98
rvar = rdata_var_new(vc_int);
100
rval = rdata_value_new();
103
ritem = rdata_item_new(ic_value);
104
ritem->u.value = rval;
106
proc_ar = run_get_current_proc_ar(run);
107
proc_ar->retval = ritem;
110
/** Return slice (substring) of the string.
112
* @param run Runner object
114
static void bi_string_slice(run_t *run)
116
rdata_var_t *self_value_var;
120
rdata_var_t *start_var;
123
rdata_var_t *length_var;
128
rdata_string_t *rstring;
133
run_proc_ar_t *proc_ar;
135
/* Extract self.Value */
136
self_value_var = builtin_get_self_mbr_var(run, "Value");
137
assert(self_value_var->vc == vc_string);
138
str = self_value_var->u.string_v->value;
139
str_l = os_str_length(str);
141
/* Get argument @a start. */
142
start_var = run_local_vars_lookup(run, strtab_get_sid("start"));
144
assert(start_var->vc == vc_int);
146
rc = bigint_get_value_int(&start_var->u.int_v->value, &start);
147
if (rc != EOK || start < 0 || (size_t) start > str_l) {
148
printf("Error: Parameter 'start' to Slice() out of range.\n");
152
/* Get argument @a length. */
153
length_var = run_local_vars_lookup(run, strtab_get_sid("length"));
155
assert(length_var->vc == vc_int);
157
rc = bigint_get_value_int(&length_var->u.int_v->value, &length);
158
if (rc != EOK || length < 0 || (size_t) (start + length) > str_l) {
159
printf("Error: Parameter 'length' to Slice() out of range.\n");
163
#ifdef DEBUG_RUN_TRACE
164
printf("Construct Slice(%d, %d) from string '%s'.\n",
167
/* Construct return value. */
168
rstring = rdata_string_new();
169
rstring->value = os_str_aslice(str, start, length);
171
rvar = rdata_var_new(vc_string);
172
rvar->u.string_v = rstring;
173
rval = rdata_value_new();
176
ritem = rdata_item_new(ic_value);
177
ritem->u.value = rval;
179
proc_ar = run_get_current_proc_ar(run);
180
proc_ar->retval = ritem;