mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-25 04:55:04 +02:00
Added error handling to substring
This commit is contained in:
parent
fcf9158d1c
commit
1d72873117
1 changed files with 18 additions and 5 deletions
19
runtime.c
19
runtime.c
|
@ -857,13 +857,26 @@ integer_type Cyc_string_length(object str) {
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
TODO: need error checking below, this is much too dangerous without!
|
|
||||||
string_type Cyc_substring(object str, object start, object end) {
|
string_type Cyc_substring(object str, object start, object end) {
|
||||||
int s = integer_value(start),
|
|
||||||
e = integer_value(end);
|
|
||||||
const char *raw = string_str(str);
|
const char *raw = string_str(str);
|
||||||
|
int s = integer_value(start),
|
||||||
|
e = integer_value(end),
|
||||||
|
len = strlen(raw);
|
||||||
|
|
||||||
|
if (s > e) {
|
||||||
|
Cyc_rt_raise2("substring - start cannot be greater than end", start);
|
||||||
|
}
|
||||||
|
if (s > len) {
|
||||||
|
Cyc_rt_raise2("substring - start cannot be greater than string length", start);
|
||||||
|
}
|
||||||
|
if (e > len) {
|
||||||
|
e = len;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
make_stringn(sub, raw + s, e - s);
|
make_stringn(sub, raw + s, e - s);
|
||||||
return sub;
|
return sub;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
integer_type Cyc_system(object cmd) {
|
integer_type Cyc_system(object cmd) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue