static void set_state_ivars(VALUE hash, VALUE state) { VALUE ivars = rb_obj_instance_variables(state); int i = 0; for (i = 0; i < RARRAY_LEN(ivars); i++) { VALUE key = rb_funcall(rb_ary_entry(ivars, i), i_to_s, 0); long key_len = RSTRING_LEN(key); VALUE value = rb_iv_get(state, StringValueCStr(key)); rb_hash_aset(hash, rb_str_intern(rb_str_substr(key, 1, key_len - 1)), value); } }
static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, VALUE* o) { if(u->source == Qnil || l <= COW_MIN_SIZE) { *o = rb_str_new(p, l); } else { *o = rb_str_substr(u->source, p - b, l); } #ifdef COMPAT_HAVE_ENCODING ENCODING_SET(*o, s_enc_utf8); #endif return 0; }
VALUE rb_str_axe( int argc, VALUE *argv, VALUE str) { VALUE n; VALUE ret; long newlen, oldlen; if (rb_scan_args( argc, argv, "01", &n) == 1 && !NIL_P( n)) newlen = NUM2LONG( n); else newlen = 80; if (newlen < 0) return Qnil; #ifdef HAVE_HEADER_RUBY_H oldlen = RSTRING_LEN( str); #else oldlen = rb_str_strlen( str); #endif if (newlen < oldlen) { VALUE ell; long e; ell = rb_str_new2( "..."); #ifdef HAVE_HEADER_RUBY_H e = RSTRING_LEN( ell); #else e = rb_str_strlen( ell); #endif if (newlen > e) { ret = rb_str_substr( str, 0, newlen - e); rb_str_append( ret, ell); } else ret = rb_str_substr( str, 0, newlen); OBJ_INFECT( ret, str); } else ret = str; return ret; }
void header_done(void *data, const char *at, size_t length) { VALUE req = (VALUE)data; VALUE temp = Qnil; VALUE ctype = Qnil; VALUE clen = Qnil; char *colon = NULL; clen = rb_hash_aref(req, global_http_content_length); if(clen != Qnil) { rb_hash_aset(req, global_content_length, clen); } ctype = rb_hash_aref(req, global_http_content_type); if(ctype != Qnil) { rb_hash_aset(req, global_content_type, ctype); } rb_hash_aset(req, global_gateway_interface, global_gateway_interface_value); if((temp = rb_hash_aref(req, global_http_host)) != Qnil) { /* ruby better close strings off with a '\0' dammit */ colon = strchr(RSTRING(temp)->ptr, ':'); if(colon != NULL) { rb_hash_aset(req, global_server_name, rb_str_substr(temp, 0, colon - RSTRING(temp)->ptr)); rb_hash_aset(req, global_server_port, rb_str_substr(temp, colon - RSTRING(temp)->ptr+1, RSTRING(temp)->len)); } else { rb_hash_aset(req, global_server_name, temp); rb_hash_aset(req, global_server_port, global_port_80); } } /* grab the initial body and stuff it into an ivar */ rb_ivar_set(req, id_http_body, rb_str_new(at, length)); rb_hash_aset(req, global_server_protocol, global_server_protocol_value); rb_hash_aset(req, global_server_software, global_mongrel_version); }
VALUE rb_str_eat( int argc, VALUE *argv, VALUE str) { VALUE val; int n; int l; int r; #ifdef HAVE_HEADER_RUBY_H n = l = RSTRING_LEN( str); #else n = l = rb_str_strlen( str); #endif if (rb_scan_args( argc, argv, "01", &val) == 1) { if (!NIL_P( val)) { int v = NUM2INT( val); if (v >= 0) { if (n >= v) n = v; } else { n = -n; if (n <= v) n = v; } } } rb_str_modify( str); #ifdef HAVE_HEADER_RUBY_H if (n > 0) { r = l - n; val = rb_str_new5( str, RSTRING_PTR( str), n); memmove( RSTRING_PTR( str), RSTRING_PTR( str) + n, r); } else { r = l + n; val = rb_str_new5( str, RSTRING_PTR( str) + r, -n); } RSTRING_LEN( str) = r; OBJ_INFECT( val, str); #else if (n > 0) { r = 0; } else if (n < 0) { r = l + n; n = -n; } else return Qnil; val = rb_str_substr( str, r, n); if (!NIL_P(val)) rb_str_update( str, r, n, rb_str_new( NULL, 0)); #endif return val; }
VALUE rb_str_rest( int argc, VALUE *argv, VALUE str) { VALUE n; long l, beg, len; beg = rb_scan_args( argc, argv, "01", &n) == 1 ? NUM2LONG( n) : 1; if (beg < 0) beg = 0; #ifdef HAVE_HEADER_RUBY_H l = RSTRING_LEN( str); #else l = rb_str_strlen( str); #endif return rb_str_substr( str, beg, l - beg); }
VALUE rb_str_tail( int argc, VALUE *argv, VALUE str) { VALUE n; long l, beg, len; len = rb_scan_args( argc, argv, "01", &n) == 1 ? NUM2LONG( n) : 1; #ifdef HAVE_HEADER_RUBY_H l = RSTRING_LEN( str); #else l = rb_str_strlen( str); #endif beg = l - len; if (beg < 0) beg = 0, len = l; return rb_str_substr( str, beg, len); }
static VALUE apply_actions(VALUE field, VALUE actions) { long j, actions_len = RARRAY_LEN(actions); long beg, len; VALUE num = 0, modi = 0; for (j = 0; j < actions_len; j++) { VALUE action = rb_ary_entry(actions, j); VALUE klass = rb_class_of(action); if (klass == rb_cRange) { /* copied from rb_str_aref */ len = rb_str_strlen(field); if (RTEST(rb_range_beg_len(action, &beg, &len, len, 0))) field = rb_str_substr(field, beg, len); } else if (klass == rb_cArray) { num = rb_str_to_inum(field, 10, 0); modi = rb_ary_entry(action, 1); if ( (FIXNUM_P(num) || TYPE(num) == T_BIGNUM && RBIGNUM_LEN(num) <= (SIZEOF_LONG/SIZEOF_BDIGITS) ) && FIXNUM_P(modi) && FIX2LONG(modi)) { long modl = NUM2LONG(modi); long numl = (NUM2LONG(num) / modl) * modl; char buf[30]; int wrtn = snprintf(buf, 30, RSTRING_PTR(rb_ary_entry(action, 0)), numl); if (wrtn < 30) { field = rb_str_new(buf, wrtn); continue; } } else { num = rb_funcall2(num, idDiv, 1, &modi); num = rb_funcall2(num, idMul, 1, &modi); } field = rb_str_format(1, &num, rb_ary_entry(action, 0)); } } return field; }
static VALUE check_app_file_exist(VALUE dir, VALUE fname1, const char* szPlatform) { VALUE res = rb_str_dup(dir); //RAWLOG_INFO1("find_file: check dir %s", RSTRING_PTR(dir)); #ifdef __SYMBIAN32__ if(*RSTRING_PTR(res) == '/') res = rb_str_substr(res,1,RSTRING_LEN(res) - 1); #endif rb_str_cat(res,"/",1); rb_str_cat(res,RSTRING_PTR(fname1),RSTRING_LEN(fname1)); if (szPlatform) { rb_str_cat(res,".",1); rb_str_cat(res,szPlatform,strlen(szPlatform)); } rb_str_cat(res,RHO_RB_EXT,strlen(RHO_RB_EXT)); //RAWLOG_INFO1("find_file: check file: %s", RSTRING_PTR(res)); return eaccess(RSTRING_PTR(res), R_OK) == 0 ? res : 0; }
static VALUE minimize_yield(VALUE rb_oid, VALUE *data) { rb_funcall(data[0], rb_intern("call"), 1, rb_str_substr(rb_oid, 0, FIX2INT(data[1]))); return Qnil; }
VALUE string_spec_rb_str_substr(VALUE self, VALUE str, VALUE beg, VALUE len) { return rb_str_substr(str, FIX2INT(beg), FIX2INT(len)); }
static ID rb_intern_uchars(const UChar *chars, const size_t chars_len, VALUE str) { const unsigned long name_hash = rb_str_hash_uchars(chars, chars_len); LOCK(); ID id = (ID)CFDictionaryGetValue(sym_id, (const void *)name_hash); UNLOCK(); if (id != 0) { goto return_id; } if (str == Qnil) { str = rb_unicode_str_new(chars, chars_len); } rb_sym_t *sym = NULL; long pos = 0; if (chars_len > 0) { UChar c = chars[0]; switch (c) { case '$': id = ID_GLOBAL; goto new_id; case '@': if (chars_len > 1 && chars[1] == '@') { pos++; id = ID_CLASS; } else { id = ID_INSTANCE; } pos++; break; default: if (chars_len > 1 && chars[chars_len - 1] == '=') { // Attribute assignment. id = rb_intern_str(rb_str_substr(str, 0, chars_len - 1)); if (!is_attrset_id(id)) { id = rb_id_attrset(id); goto id_register; } id = ID_ATTRSET; } else if (iswupper(c)) { id = ID_CONST; } else { id = ID_LOCAL; } break; } } if (pos < chars_len && !isdigit(chars[pos])) { for (; pos < chars_len; pos++) { if (!is_identchar(chars[pos])) { break; } } } if (pos < chars_len) { id = ID_JUNK; } new_id: id |= ++last_id << ID_SCOPE_SHIFT; id_register: //printf("register %s hash %ld id %ld\n", RSTRING_PTR(str), name_hash, id); sym = sym_alloc(str, id); LOCK(); CFDictionarySetValue(sym_id, (const void *)name_hash, (const void *)id); CFDictionarySetValue(id_str, (const void *)id, (const void *)sym); UNLOCK(); return_id: return id; }
static VALUE tre_traverse(VALUE pattern, VALUE string, long char_offset, VALUE params, VALUE ignore_case, VALUE multi_line, int num_captures, VALUE repeat) { // Compile once regex_t preg; tre_compile_regex(&preg, pattern, ignore_case, multi_line); // Build regaparams regaparams_t aparams; tre_build_aparams(&aparams, params); // Match data regamatch_t match; regmatch_t pmatch[num_captures + 1]; // memset(&match, 0, sizeof(match)); match.nmatch = num_captures + 1; match.pmatch = pmatch; // Scan VALUE arr = rb_ary_new(); long char_offset_acc = char_offset; // rb_global_variable(&arr); while (1) { // Get substring to start with long char_len = CHAR_LENGTH(string) - char_offset; if (char_len <= 0) break; string = rb_str_substr(string, char_offset, char_len); int result = tre_reganexec(&preg, StringValuePtr(string), RSTRING_LEN(string), &match, aparams, 0); if (result == REG_NOMATCH) break; // Fill in array with ranges VALUE subarr; if (match.nmatch == 1) subarr = arr; // Faking.. kind of. else { subarr = rb_ary_new(); // rb_global_variable(&subarr); } unsigned int i; for (i = 0; i < match.nmatch; ++i) // No match if (match.pmatch[i].rm_so == -1) rb_ary_push(subarr, Qnil); // Match => Range else { VALUE range = rb_range_new( LONG2NUM( char_offset_acc + BYTE_TO_CHAR(string, match.pmatch[i].rm_so) ), LONG2NUM( char_offset_acc + BYTE_TO_CHAR(string, match.pmatch[i].rm_eo) ), 1); // rb_global_variable(&range); rb_ary_push(subarr, range); } if (match.nmatch > 1) rb_ary_push(arr, subarr); // Stop or proceed if (repeat == Qfalse) break; else { char_offset = BYTE_TO_CHAR(string, match.pmatch[0].rm_eo); if (char_offset == 0) char_offset = 1; // Weird case char_offset_acc += char_offset; } } // Free once tre_regfree(&preg); return arr; }