44 static const char ws[] =
" \t\r\n\f\v";
46 for(
int i = 0; i <
sizeof(ws); i++ ) {
47 if( ch == ws[i] )
return 1;
70 if( !h )
return value;
72 uint8_t *str = mrbc_alloc(vm, len+1);
88 memcpy( str, src, len );
113 if( !h )
return value;
148#if defined(MRBC_ALLOC_VMID)
154 mrbc_set_vm_id( str->
string, 0 );
172 if( value.
string == NULL )
return value;
194 if( value.
string == NULL )
return value;
219 memcpy(str + len1, s2->
string->
data, len2 + 1);
248 memcpy(str + len1, s2, len2);
249 str[len1 + len2] = 0;
251 memset(str + len1, 0, len2 + 1);
275 while( try_cnt >= 0 ) {
302 if( *p1 ==
'\0' )
break;
315 int new_size = p2 - p1 + 1;
319 if( p1 != buf ) memmove( buf, p1, new_size );
320 buf[new_size] =
'\0';
346 int new_size = p2 - p1 + 1;
350 buf[new_size] =
'\0';
370 if (
'a' <= data[len] && data[len] <=
'z') {
371 data[len] = data[len] - (
'a' -
'A');
392 if (
'A' <= data[len] && data[len] <=
'Z') {
393 data[len] = data[len] + (
'a' -
'A');
404static void c_string_new(
struct VM *vm,
mrbc_value v[],
int argc)
428static void c_string_add(
struct VM *vm,
mrbc_value v[],
int argc)
444static void c_string_mul(
struct VM *vm,
mrbc_value v[],
int argc)
458 if( value.
string == NULL )
return;
461 for(
int i = 0; i < v[1].
i; i++ ) {
475static void c_string_size(
struct VM *vm,
mrbc_value v[],
int argc)
487static void c_string_to_i(
struct VM *vm,
mrbc_value v[],
int argc)
492 if( base < 2 || base > 36 ) {
508static void c_string_to_f(
struct VM *vm,
mrbc_value v[],
int argc)
520static void c_string_to_s(
struct VM *vm,
mrbc_value v[],
int argc)
532static void c_string_append(
struct VM *vm,
mrbc_value v[],
int argc)
543static void c_string_slice(
struct VM *vm,
mrbc_value v[],
int argc)
551 if( pos < 0 ) pos += target_len;
552 if( pos >= target_len )
goto RETURN_NIL;
560 if( pos < 0 ) pos += target_len;
574 if( pos < 0 ) pos += target_len;
576 if( pos2 < 0 ) pos2 += target_len;
587 if( pos < 0 || pos > target_len )
goto RETURN_NIL;
588 if( len > target_len - pos ) len = target_len - pos;
590 if( len < 0 )
goto RETURN_NIL;
593 if( !ret.
string )
goto RETURN_NIL;
606static void c_string_insert(
struct VM *vm,
mrbc_value v[],
int argc)
649 if( len < 0 ) len = 0;
661 if( pos < 0 ) pos = len1 + pos;
662 if( len > len1 - pos ) len = len1 - pos;
663 if( pos < 0 || pos > len1 || len < 0) {
668 int len3 = len1 + len2 - len;
671 str = mrbc_realloc(vm, str, len3+1);
675 memmove( str + pos + len2, str + pos + len, len1 - pos - len + 1 );
679 str = mrbc_realloc(vm, str, len3+1);
695static void c_string_chomp(
struct VM *vm,
mrbc_value v[],
int argc)
708static void c_string_clear(
struct VM *vm,
mrbc_value v[],
int argc)
717static void c_string_chomp_self(
struct VM *vm,
mrbc_value v[],
int argc)
728static void c_string_dup(
struct VM *vm,
mrbc_value v[],
int argc)
739static void c_string_empty(
struct VM *vm,
mrbc_value v[],
int argc)
748static void c_string_getbyte(
struct VM *vm,
mrbc_value v[],
int argc)
754 if( idx >= len ) idx = -1;
769static void c_string_setbyte(
struct VM *vm,
mrbc_value v[],
int argc)
783 if( idx < 0 || idx >= len ) {
797static void c_string_index(
struct VM *vm,
mrbc_value v[],
int argc)
808 if( offset < 0 )
goto NIL_RETURN;
816 if( index < 0 )
goto NIL_RETURN;
829static void c_string_inspect(
struct VM *vm,
mrbc_value v[],
int argc)
831 char buf[10] =
"\\x";
836 if( s[i] <
' ' || 0x7f <= s[i] ) {
837 buf[2] =
"0123456789ABCDEF"[s[i] >> 4];
838 buf[3] =
"0123456789ABCDEF"[s[i] & 0x0f];
854static void c_string_ord(
struct VM *vm,
mrbc_value v[],
int argc)
870static void c_string_slice_self(
struct VM *vm,
mrbc_value v[],
int argc)
891 if( pos < 0 ) pos += target_len;
892 if( pos < 0 )
goto RETURN_NIL;
893 if( len > (target_len - pos) ) len = target_len - pos;
894 if( len < 0 )
goto RETURN_NIL;
895 if( argc == 1 && len <= 0 )
goto RETURN_NIL;
898 if( !ret.
string )
goto RETURN_NIL;
918static void c_string_split(
struct VM *vm,
mrbc_value v[],
int argc)
957 if( sep_len == 0 ) sep_len++;
1003 if( pos < 0 )
break;
1004 offset = pos + sep_len;
1011 if( idx < 0 )
break;
1033static void c_string_lstrip(
struct VM *vm,
mrbc_value v[],
int argc)
1046static void c_string_lstrip_self(
struct VM *vm,
mrbc_value v[],
int argc)
1057static void c_string_rstrip(
struct VM *vm,
mrbc_value v[],
int argc)
1070static void c_string_rstrip_self(
struct VM *vm,
mrbc_value v[],
int argc)
1081static void c_string_strip(
struct VM *vm,
mrbc_value v[],
int argc)
1094static void c_string_strip_self(
struct VM *vm,
mrbc_value v[],
int argc)
1105static void c_string_to_sym(
struct VM *vm,
mrbc_value v[],
int argc)
1148 if( flag_reverse_enable && pattern_length >= 2 && pattern[i] ==
'^' ) {
1154 while( i < pattern_length ) {
1156 if( (i+2) < pattern_length && pattern[i+1] ==
'-' ) {
1157 pat1 = mrbc_alloc( vm,
sizeof(
struct tr_pattern) + 2 );
1158 if( pat1 != NULL ) {
1161 pat1->
n = pattern[i+2] - pattern[i] + 1;
1163 pat1->
ch[0] = pattern[i];
1164 pat1->
ch[1] = pattern[i+2];
1170 int start_pos = i++;
1171 while( i < pattern_length ) {
1172 if( (i+2) < pattern_length && pattern[i+1] ==
'-' )
break;
1176 int len = i - start_pos;
1177 pat1 = mrbc_alloc( vm,
sizeof(
struct tr_pattern) + len );
1178 if( pat1 != NULL ) {
1183 memcpy( pat1->
ch, &pattern[start_pos], len );
1192 while( p->
next != NULL ) { p = p->
next; }
1206 while( pat != NULL ) {
1207 if( pat->
type == 1 ) {
1208 for(
int i = 0; i < pat->
n; i++ ) {
1209 if( pat->
ch[i] ==
ch ) ret = n_sum + i;
1219 return (ret < 0) ? INT_MAX : -1;
1227 while( pat != NULL ) {
1228 if( n_th < (n_sum + pat->
n) ) {
1229 int i = (n_th - n_sum);
1230 return (pat->
type == 1) ? pat->
ch[i] :pat->
ch[0] + i;
1232 if( pat->
next == NULL ) {
1233 return (pat->
type == 1) ? pat->
ch[pat->
n - 1] : pat->
ch[1];
1251 if( pat == NULL )
return 0;
1255 int flag_changed = 0;
1259 for(
int i = 0; i < len; i++ ) {
1261 if(
n < 0 )
continue;
1265 memmove( s + i, s + i + 1, len - i );
1279 return flag_changed;
1282static void c_string_tr(
struct VM *vm,
mrbc_value v[],
int argc)
1293static void c_string_tr_self(
struct VM *vm,
mrbc_value v[],
int argc)
1295 int flag_changed =
tr_main(vm, v, argc);
1297 if( !flag_changed ) {
1306static void c_string_start_with(
struct VM *vm,
mrbc_value v[],
int argc)
1328static void c_string_end_with(
struct VM *vm,
mrbc_value v[],
int argc)
1351static void c_string_include(
struct VM *vm,
mrbc_value v[],
int argc)
1366static void c_string_bytes(
struct VM *vm,
mrbc_value v[],
int argc)
1375 for(
int i = 0; i < len; i++ ) {
1385static void c_string_upcase(
struct VM *vm,
mrbc_value v[],
int argc)
1395static void c_string_upcase_self(
struct VM *vm,
mrbc_value v[],
int argc)
1406static void c_string_downcase(
struct VM *vm,
mrbc_value v[],
int argc)
1417static void c_string_downcase_self(
struct VM *vm,
mrbc_value v[],
int argc)
1477#include "_autogen_class_string.h"
void * mrbc_raw_realloc(void *ptr, unsigned int size)
void mrbc_raw_free(void *ptr)
int mrbc_array_push(mrbc_value *ary, mrbc_value *set_val)
int mrbc_array_set(mrbc_value *ary, int idx, mrbc_value *set_val)
mrbc_value mrbc_array_get(const mrbc_value *ary, int idx)
mrbc_value mrbc_array_new(struct VM *vm, int size)
mrbc_value mrbc_array_remove(mrbc_value *ary, int idx)
static int mrbc_array_size(const mrbc_value *ary)
static mrbc_value mrbc_range_last(const mrbc_value *v)
static int mrbc_range_exclude_end(const mrbc_value *v)
static mrbc_value mrbc_range_first(const mrbc_value *v)
int mrbc_string_index(const mrbc_value *src, const mrbc_value *pattern, int offset)
void mrbc_string_delete(mrbc_value *str)
static void tr_free_pattern(struct tr_pattern *pat)
static int tr_get_character(const struct tr_pattern *pat, int n_th)
static int tr_find_character(const struct tr_pattern *pat, int ch)
mrbc_value mrbc_string_dup(struct VM *vm, mrbc_value *s1)
static int tr_main(struct VM *vm, mrbc_value v[], int argc)
static int is_space(int ch)
static struct tr_pattern * tr_parse_pattern(struct VM *vm, const mrbc_value *v_pattern, int flag_reverse_enable)
void mrbc_string_clear(mrbc_value *str)
mrbc_value mrbc_string_add(struct VM *vm, const mrbc_value *s1, const mrbc_value *s2)
int mrbc_string_chomp(mrbc_value *src)
int mrbc_string_append_cbuf(mrbc_value *s1, const void *s2, int len2)
int mrbc_string_upcase(mrbc_value *str)
int mrbc_string_downcase(mrbc_value *str)
int mrbc_string_append(mrbc_value *s1, const mrbc_value *s2)
mrbc_value mrbc_string_new(struct VM *vm, const void *src, int len)
mrbc_value mrbc_string_new_alloc(struct VM *vm, void *buf, int len)
int mrbc_string_strip(mrbc_value *src, int mode)
struct RString mrbc_string
String object.
static char * mrbc_string_cstr(const mrbc_value *v)
static mrbc_value mrbc_string_new_cstr(struct VM *vm, const char *src)
static int mrbc_string_append_cstr(mrbc_value *s1, const char *s2)
static int mrbc_string_size(const mrbc_value *str)
void mrbc_raise(struct VM *vm, struct RClass *exc_cls, const char *msg)
void mrbc_raisef(struct VM *vm, struct RClass *exc_cls, const char *fstr,...)
Include at once the necessary header files.
uint8_t * data
pointer to allocated buffer.
MRBC_STRING_SIZE_T size
string length.
mrbc_value mrbc_symbol_new(struct VM *vm, const char *str)
const char * mrbc_symid_to_str(mrbc_sym sym_id)
mrbc_int_t mrbc_atoi(const char *s, int base)
static void mrbc_decref(mrbc_value *v)
#define MRBC_INIT_OBJECT_HEADER(p, t)
#define SET_BOOL_RETURN(n)
#define SET_INT_RETURN(n)
static void mrbc_incref(mrbc_value *v)
@ MRBC_TT_INTEGER
Integer.
struct RObject mrbc_value
#define mrbc_integer_value(n)
#define SET_FLOAT_RETURN(n)
Global configuration of mruby/c VM's.