[051bc69a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 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.
|
---|
| 16 | *
|
---|
| 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.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file String builtin binding. */
|
---|
| 30 |
|
---|
| 31 | #include <stdio.h>
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 | #include <assert.h>
|
---|
| 34 | #include "../bigint.h"
|
---|
| 35 | #include "../builtin.h"
|
---|
| 36 | #include "../debug.h"
|
---|
| 37 | #include "../mytypes.h"
|
---|
| 38 | #include "../os/os.h"
|
---|
| 39 | #include "../rdata.h"
|
---|
| 40 | #include "../run.h"
|
---|
| 41 | #include "../strtab.h"
|
---|
| 42 |
|
---|
| 43 | #include "bi_string.h"
|
---|
| 44 |
|
---|
| 45 | static void bi_string_length(run_t *run);
|
---|
| 46 | static void bi_string_slice(run_t *run);
|
---|
| 47 |
|
---|
| 48 | /** Declare String builtin.
|
---|
| 49 | *
|
---|
| 50 | * @param bi Builtin object
|
---|
| 51 | */
|
---|
| 52 | void bi_string_declare(builtin_t *bi)
|
---|
| 53 | {
|
---|
| 54 | (void) bi;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /** Bind String builtin.
|
---|
| 58 | *
|
---|
| 59 | * @param bi Builtin object
|
---|
| 60 | */
|
---|
| 61 | void bi_string_bind(builtin_t *bi)
|
---|
| 62 | {
|
---|
| 63 | builtin_fun_bind(bi, "String", "get_length", bi_string_length);
|
---|
| 64 | builtin_fun_bind(bi, "String", "Slice", bi_string_slice);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /** Return length of the string.
|
---|
| 68 | *
|
---|
| 69 | * @param run Runner object
|
---|
| 70 | */
|
---|
| 71 | static void bi_string_length(run_t *run)
|
---|
| 72 | {
|
---|
[1433ecda] | 73 | rdata_var_t *self_value_var;
|
---|
| 74 | const char *str;
|
---|
| 75 | size_t str_l;
|
---|
[051bc69a] | 76 |
|
---|
| 77 | rdata_int_t *rint;
|
---|
| 78 | rdata_var_t *rvar;
|
---|
| 79 | rdata_value_t *rval;
|
---|
| 80 | rdata_item_t *ritem;
|
---|
| 81 |
|
---|
| 82 | run_proc_ar_t *proc_ar;
|
---|
| 83 |
|
---|
| 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);
|
---|
| 89 |
|
---|
| 90 | #ifdef DEBUG_RUN_TRACE
|
---|
| 91 | printf("Get length of string '%s'.\n", str);
|
---|
| 92 | #endif
|
---|
| 93 |
|
---|
| 94 | /* Construct return value. */
|
---|
| 95 | rint = rdata_int_new();
|
---|
| 96 | bigint_init(&rint->value, (int) str_l);
|
---|
| 97 |
|
---|
| 98 | rvar = rdata_var_new(vc_int);
|
---|
| 99 | rvar->u.int_v = rint;
|
---|
| 100 | rval = rdata_value_new();
|
---|
| 101 | rval->var = rvar;
|
---|
| 102 |
|
---|
| 103 | ritem = rdata_item_new(ic_value);
|
---|
| 104 | ritem->u.value = rval;
|
---|
| 105 |
|
---|
| 106 | proc_ar = run_get_current_proc_ar(run);
|
---|
| 107 | proc_ar->retval = ritem;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /** Return slice (substring) of the string.
|
---|
| 111 | *
|
---|
| 112 | * @param run Runner object
|
---|
| 113 | */
|
---|
| 114 | static void bi_string_slice(run_t *run)
|
---|
| 115 | {
|
---|
[1433ecda] | 116 | rdata_var_t *self_value_var;
|
---|
| 117 | const char *str;
|
---|
| 118 | const char *slice;
|
---|
| 119 | size_t str_l;
|
---|
[051bc69a] | 120 |
|
---|
| 121 | rdata_var_t *start_var;
|
---|
| 122 | int start;
|
---|
| 123 |
|
---|
| 124 | rdata_var_t *length_var;
|
---|
| 125 | int length;
|
---|
| 126 |
|
---|
[b7fd2a0] | 127 | errno_t rc;
|
---|
[051bc69a] | 128 |
|
---|
| 129 | /* Extract self.Value */
|
---|
| 130 | self_value_var = builtin_get_self_mbr_var(run, "Value");
|
---|
| 131 | assert(self_value_var->vc == vc_string);
|
---|
| 132 | str = self_value_var->u.string_v->value;
|
---|
| 133 | str_l = os_str_length(str);
|
---|
| 134 |
|
---|
| 135 | /* Get argument @a start. */
|
---|
| 136 | start_var = run_local_vars_lookup(run, strtab_get_sid("start"));
|
---|
| 137 | assert(start_var);
|
---|
| 138 | assert(start_var->vc == vc_int);
|
---|
| 139 |
|
---|
| 140 | rc = bigint_get_value_int(&start_var->u.int_v->value, &start);
|
---|
| 141 | if (rc != EOK || start < 0 || (size_t) start > str_l) {
|
---|
| 142 | printf("Error: Parameter 'start' to Slice() out of range.\n");
|
---|
| 143 | exit(1);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /* Get argument @a length. */
|
---|
| 147 | length_var = run_local_vars_lookup(run, strtab_get_sid("length"));
|
---|
| 148 | assert(length_var);
|
---|
| 149 | assert(length_var->vc == vc_int);
|
---|
| 150 |
|
---|
| 151 | rc = bigint_get_value_int(&length_var->u.int_v->value, &length);
|
---|
| 152 | if (rc != EOK || length < 0 || (size_t) (start + length) > str_l) {
|
---|
| 153 | printf("Error: Parameter 'length' to Slice() out of range.\n");
|
---|
| 154 | exit(1);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | #ifdef DEBUG_RUN_TRACE
|
---|
| 158 | printf("Construct Slice(%d, %d) from string '%s'.\n",
|
---|
| 159 | start, length, str);
|
---|
| 160 | #endif
|
---|
[c5cb943d] | 161 | slice = os_str_aslice(str, start, length);
|
---|
[051bc69a] | 162 |
|
---|
[c5cb943d] | 163 | /* Ownership of slice is transferred. */
|
---|
| 164 | builtin_return_string(run, slice);
|
---|
[051bc69a] | 165 | }
|
---|