2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
hash.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Mon Nov 22 18:51:18 JST 1993
|
|
|
|
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-25 07:29:39 +04:00
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 08:53:16 +04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2020-05-08 12:31:09 +03:00
|
|
|
#include "ruby/internal/config.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
|
2010-01-12 04:00:34 +03:00
|
|
|
#include <errno.h>
|
2019-12-04 11:16:30 +03:00
|
|
|
|
2002-10-17 20:13:44 +04:00
|
|
|
#ifdef __APPLE__
|
2012-12-28 18:23:25 +04:00
|
|
|
# ifdef HAVE_CRT_EXTERNS_H
|
|
|
|
# include <crt_externs.h>
|
|
|
|
# else
|
|
|
|
# include "missing/crt_externs.h"
|
|
|
|
# endif
|
2002-10-17 20:13:44 +04:00
|
|
|
#endif
|
|
|
|
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "debug_counter.h"
|
|
|
|
#include "id.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "internal/array.h"
|
|
|
|
#include "internal/bignum.h"
|
|
|
|
#include "internal/class.h"
|
|
|
|
#include "internal/cont.h"
|
|
|
|
#include "internal/error.h"
|
|
|
|
#include "internal/hash.h"
|
|
|
|
#include "internal/object.h"
|
|
|
|
#include "internal/proc.h"
|
|
|
|
#include "internal/symbol.h"
|
2020-01-28 17:40:25 +03:00
|
|
|
#include "internal/time.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/vm.h"
|
|
|
|
#include "probes.h"
|
|
|
|
#include "ruby/st.h"
|
|
|
|
#include "ruby/util.h"
|
|
|
|
#include "ruby_assert.h"
|
|
|
|
#include "symbol.h"
|
|
|
|
#include "transient_heap.h"
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
#ifndef HASH_DEBUG
|
|
|
|
#define HASH_DEBUG 0
|
|
|
|
#endif
|
|
|
|
|
2019-08-07 16:54:38 +03:00
|
|
|
#if HASH_DEBUG
|
|
|
|
#include "gc.h"
|
|
|
|
#endif
|
|
|
|
|
2013-12-23 10:53:17 +04:00
|
|
|
#define HAS_EXTRA_STATES(hash, klass) ( \
|
2013-12-23 11:00:01 +04:00
|
|
|
((klass = has_extra_methods(rb_obj_class(hash))) != 0) || \
|
2019-09-25 06:59:12 +03:00
|
|
|
FL_TEST((hash), FL_EXIVAR|RHASH_PROC_DEFAULT) || \
|
2013-12-23 11:00:01 +04:00
|
|
|
!NIL_P(RHASH_IFNONE(hash)))
|
2013-12-16 17:40:04 +04:00
|
|
|
|
2016-03-09 10:17:03 +03:00
|
|
|
#define SET_DEFAULT(hash, ifnone) ( \
|
2019-01-17 19:53:10 +03:00
|
|
|
FL_UNSET_RAW(hash, RHASH_PROC_DEFAULT), \
|
2016-03-09 10:17:03 +03:00
|
|
|
RHASH_SET_IFNONE(hash, ifnone))
|
|
|
|
|
2016-03-09 10:17:03 +03:00
|
|
|
#define SET_PROC_DEFAULT(hash, proc) set_proc_default(hash, proc)
|
|
|
|
|
2016-03-09 10:17:04 +03:00
|
|
|
#define COPY_DEFAULT(hash, hash2) copy_default(RHASH(hash), RHASH(hash2))
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
copy_default(struct RHash *hash, const struct RHash *hash2)
|
|
|
|
{
|
2019-01-17 19:53:10 +03:00
|
|
|
hash->basic.flags &= ~RHASH_PROC_DEFAULT;
|
|
|
|
hash->basic.flags |= hash2->basic.flags & RHASH_PROC_DEFAULT;
|
2019-12-03 06:37:15 +03:00
|
|
|
RHASH_SET_IFNONE(hash, RHASH_IFNONE((VALUE)hash2));
|
2016-03-09 10:17:04 +03:00
|
|
|
}
|
|
|
|
|
2013-12-23 10:53:51 +04:00
|
|
|
static VALUE
|
|
|
|
has_extra_methods(VALUE klass)
|
|
|
|
{
|
|
|
|
const VALUE base = rb_cHash;
|
|
|
|
VALUE c = klass;
|
|
|
|
while (c != base) {
|
2015-05-31 22:17:18 +03:00
|
|
|
if (rb_class_has_methods(c)) return klass;
|
2013-12-23 13:01:05 +04:00
|
|
|
c = RCLASS_SUPER(c);
|
2013-12-23 10:53:51 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-25 21:05:09 +04:00
|
|
|
static VALUE rb_hash_s_try_convert(VALUE, VALUE);
|
|
|
|
|
2013-05-26 16:37:11 +04:00
|
|
|
/*
|
|
|
|
* Hash WB strategy:
|
|
|
|
* 1. Check mutate st_* functions
|
|
|
|
* * st_insert()
|
|
|
|
* * st_insert2()
|
|
|
|
* * st_update()
|
|
|
|
* * st_add_direct()
|
|
|
|
* 2. Insert WBs
|
|
|
|
*/
|
|
|
|
|
2000-02-17 10:11:22 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_freeze(VALUE hash)
|
2000-02-17 10:11:22 +03:00
|
|
|
{
|
|
|
|
return rb_obj_freeze(hash);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_cHash;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
static VALUE envtbl;
|
2020-01-08 03:19:26 +03:00
|
|
|
static ID id_hash, id_default, id_flatten_bang;
|
2019-08-01 05:20:37 +03:00
|
|
|
static ID id_hash_iter_lev;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-05-26 16:37:11 +04:00
|
|
|
VALUE
|
|
|
|
rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
|
|
|
|
{
|
* include/ruby/ruby.h: rename OBJ_WRITE and OBJ_WRITTEN into
RB_OBJ_WRITE and RB_OBJ_WRITTEN.
* array.c, class.c, compile.c, hash.c, internal.h, iseq.c,
proc.c, process.c, re.c, string.c, variable.c, vm.c,
vm_eval.c, vm_insnhelper.c, vm_insnhelper.h,
vm_method.c: catch up this change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44299 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-12-20 12:07:47 +04:00
|
|
|
RB_OBJ_WRITE(hash, (&RHASH(hash)->ifnone), ifnone);
|
2013-05-26 16:37:11 +04:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_any_cmp(VALUE a, VALUE b)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2003-03-04 10:04:11 +03:00
|
|
|
if (a == b) return 0;
|
2011-09-29 15:07:45 +04:00
|
|
|
if (RB_TYPE_P(a, T_STRING) && RBASIC(a)->klass == rb_cString &&
|
2012-05-23 11:13:21 +04:00
|
|
|
RB_TYPE_P(b, T_STRING) && RBASIC(b)->klass == rb_cString) {
|
2008-02-12 06:17:43 +03:00
|
|
|
return rb_str_hash_cmp(a, b);
|
2000-12-25 09:29:27 +03:00
|
|
|
}
|
2003-03-04 10:04:11 +03:00
|
|
|
if (a == Qundef || b == Qundef) return -1;
|
2000-12-25 09:29:27 +03:00
|
|
|
if (SYMBOL_P(a) && SYMBOL_P(b)) {
|
|
|
|
return a != b;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
* include/ruby/signal.h: removed.
* common.mk, class.c, eval.c, eval_intern.h, file.c, gc.c, hash.c,
io.c, process.c, signal.c: vm_core.h: ditto.
Some unused external global variables are also removed.
(rb_prohibit_interrupt, rb_trap_immediate, rb_trap_pending,
rb_thread_critical)
* ext/openssl/ossl_ssl.c, ext/openssl/ossl_x509store.c,
ext/readline/readline.c, ext/socket/depend,
ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19080 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-03 12:00:05 +04:00
|
|
|
return !rb_eql(a, b);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2013-11-26 17:43:40 +04:00
|
|
|
static VALUE
|
|
|
|
hash_recursive(VALUE obj, VALUE arg, int recurse)
|
|
|
|
{
|
2013-12-05 09:40:43 +04:00
|
|
|
if (recurse) return INT2FIX(0);
|
2013-11-26 17:43:40 +04:00
|
|
|
return rb_funcallv(obj, id_hash, 0, 0);
|
|
|
|
}
|
|
|
|
|
2003-12-22 12:37:13 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash(VALUE obj)
|
2003-12-22 12:37:13 +03:00
|
|
|
{
|
2013-12-05 09:40:43 +04:00
|
|
|
VALUE hval = rb_exec_recursive_outer(hash_recursive, obj, 0);
|
2009-02-14 21:53:40 +03:00
|
|
|
|
2013-12-16 17:40:02 +04:00
|
|
|
while (!FIXNUM_P(hval)) {
|
|
|
|
if (RB_TYPE_P(hval, T_BIGNUM)) {
|
2013-06-08 20:00:16 +04:00
|
|
|
int sign;
|
|
|
|
unsigned long ul;
|
2013-06-10 14:37:39 +04:00
|
|
|
sign = rb_integer_pack(hval, &ul, 1, sizeof(ul), 0,
|
2013-06-11 18:40:16 +04:00
|
|
|
INTEGER_PACK_NATIVE_BYTE_ORDER);
|
2020-04-08 07:28:13 +03:00
|
|
|
if (sign < 0) {
|
|
|
|
hval = LONG2FIX(ul | FIXNUM_MIN);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hval = LONG2FIX(ul & FIXNUM_MAX);
|
|
|
|
}
|
2013-06-08 20:00:16 +04:00
|
|
|
}
|
2009-02-14 21:53:40 +03:00
|
|
|
hval = rb_to_int(hval);
|
|
|
|
}
|
2013-12-16 17:40:02 +04:00
|
|
|
return hval;
|
2003-12-22 12:37:13 +03:00
|
|
|
}
|
|
|
|
|
2019-11-18 06:13:08 +03:00
|
|
|
static long rb_objid_hash(st_index_t index);
|
2014-01-08 10:55:24 +04:00
|
|
|
|
2019-01-30 07:54:01 +03:00
|
|
|
static st_index_t
|
|
|
|
dbl_to_index(double d)
|
|
|
|
{
|
|
|
|
union {double d; st_index_t i;} u;
|
|
|
|
u.d = d;
|
|
|
|
return u.i;
|
|
|
|
}
|
|
|
|
|
2016-11-07 03:45:00 +03:00
|
|
|
long
|
|
|
|
rb_dbl_long_hash(double d)
|
|
|
|
{
|
|
|
|
/* normalize -0.0 to 0.0 */
|
|
|
|
if (d == 0.0) d = 0.0;
|
|
|
|
#if SIZEOF_INT == SIZEOF_VOIDP
|
|
|
|
return rb_memhash(&d, sizeof(d));
|
|
|
|
#else
|
2019-01-30 07:54:01 +03:00
|
|
|
return rb_objid_hash(dbl_to_index(d));
|
2016-11-07 03:45:00 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-12-07 07:25:50 +03:00
|
|
|
static inline long
|
2016-12-06 07:43:48 +03:00
|
|
|
any_hash(VALUE a, st_index_t (*other_func)(VALUE))
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-02-13 08:09:11 +03:00
|
|
|
VALUE hval;
|
2009-09-08 17:10:04 +04:00
|
|
|
st_index_t hnum;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2020-06-15 09:02:53 +03:00
|
|
|
switch (TYPE(a)) {
|
|
|
|
case T_SYMBOL:
|
2015-01-22 10:48:59 +03:00
|
|
|
if (STATIC_SYM_P(a)) {
|
2019-01-17 19:53:10 +03:00
|
|
|
hnum = a >> (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
|
|
|
|
hnum = rb_hash_start(hnum);
|
|
|
|
}
|
2020-06-15 09:02:53 +03:00
|
|
|
else {
|
|
|
|
hnum = RSYMBOL(a)->hashval;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_FIXNUM:
|
|
|
|
case T_TRUE:
|
|
|
|
case T_FALSE:
|
|
|
|
case T_NIL:
|
2014-01-09 15:34:17 +04:00
|
|
|
hnum = rb_objid_hash((st_index_t)a);
|
2020-06-15 09:02:53 +03:00
|
|
|
break;
|
|
|
|
case T_STRING:
|
2016-12-06 07:43:48 +03:00
|
|
|
hnum = rb_str_hash(a);
|
2020-06-15 09:02:53 +03:00
|
|
|
break;
|
|
|
|
case T_BIGNUM:
|
2016-03-18 04:22:38 +03:00
|
|
|
hval = rb_big_hash(a);
|
|
|
|
hnum = FIX2LONG(hval);
|
2020-06-15 09:02:53 +03:00
|
|
|
break;
|
|
|
|
case T_FLOAT: /* prevent pathological behavior: [Bug #10761] */
|
2016-11-07 03:45:00 +03:00
|
|
|
hnum = rb_dbl_long_hash(rb_float_value(a));
|
2020-06-15 09:02:53 +03:00
|
|
|
break;
|
|
|
|
default:
|
2015-07-29 11:25:49 +03:00
|
|
|
hnum = other_func(a);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2017-12-22 11:52:11 +03:00
|
|
|
#if SIZEOF_LONG < SIZEOF_ST_INDEX_T
|
|
|
|
if (hnum > 0)
|
|
|
|
hnum &= (unsigned long)-1 >> 2;
|
|
|
|
else
|
|
|
|
hnum |= ~((unsigned long)-1 >> 2);
|
|
|
|
#else
|
2008-03-03 10:14:27 +03:00
|
|
|
hnum <<= 1;
|
2017-12-22 11:52:11 +03:00
|
|
|
hnum = RSHIFT(hnum, 1);
|
|
|
|
#endif
|
|
|
|
return (long)hnum;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2015-07-29 11:25:49 +03:00
|
|
|
static st_index_t
|
|
|
|
obj_any_hash(VALUE obj)
|
|
|
|
{
|
|
|
|
obj = rb_hash(obj);
|
|
|
|
return FIX2LONG(obj);
|
|
|
|
}
|
|
|
|
|
2015-08-14 22:52:06 +03:00
|
|
|
static st_index_t
|
2016-12-06 06:36:52 +03:00
|
|
|
rb_any_hash(VALUE a)
|
|
|
|
{
|
2016-11-07 03:45:00 +03:00
|
|
|
return any_hash(a, obj_any_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Here is a hash function for 64-bit key. It is about 5 times faster
|
|
|
|
(2 times faster when uint128 type is absent) on Haswell than
|
|
|
|
tailored Spooky or City hash function can be. */
|
|
|
|
|
|
|
|
/* Here we two primes with random bit generation. */
|
2016-12-24 16:21:30 +03:00
|
|
|
static const uint64_t prime1 = ((uint64_t)0x2e0bb864 << 32) | 0xe9ea7df5;
|
2017-12-25 15:41:52 +03:00
|
|
|
static const uint32_t prime2 = 0x830fcab9;
|
2016-11-07 03:45:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
static inline uint64_t
|
2016-12-06 06:36:52 +03:00
|
|
|
mult_and_mix(uint64_t m1, uint64_t m2)
|
2015-08-14 22:52:06 +03:00
|
|
|
{
|
2017-12-25 15:32:09 +03:00
|
|
|
#if defined HAVE_UINT128_T
|
2017-12-25 15:32:10 +03:00
|
|
|
uint128_t r = (uint128_t) m1 * (uint128_t) m2;
|
2016-11-07 03:45:00 +03:00
|
|
|
return (uint64_t) (r >> 64) ^ (uint64_t) r;
|
|
|
|
#else
|
|
|
|
uint64_t hm1 = m1 >> 32, hm2 = m2 >> 32;
|
|
|
|
uint64_t lm1 = m1, lm2 = m2;
|
|
|
|
uint64_t v64_128 = hm1 * hm2;
|
|
|
|
uint64_t v32_96 = hm1 * lm2 + lm1 * hm2;
|
|
|
|
uint64_t v1_32 = lm1 * lm2;
|
|
|
|
|
|
|
|
return (v64_128 + (v32_96 >> 32)) ^ ((v32_96 << 32) + v1_32);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t
|
2016-12-06 06:36:52 +03:00
|
|
|
key64_hash(uint64_t key, uint32_t seed)
|
2016-11-07 03:45:00 +03:00
|
|
|
{
|
|
|
|
return mult_and_mix(key + seed, prime1);
|
2015-08-14 22:52:06 +03:00
|
|
|
}
|
|
|
|
|
2019-01-30 08:08:36 +03:00
|
|
|
/* Should cast down the result for each purpose */
|
|
|
|
#define st_index_hash(index) key64_hash(rb_hash_start(index), prime2)
|
|
|
|
|
2019-11-18 06:13:08 +03:00
|
|
|
static long
|
2014-01-08 10:55:24 +04:00
|
|
|
rb_objid_hash(st_index_t index)
|
|
|
|
{
|
2019-01-30 08:08:36 +03:00
|
|
|
return (long)st_index_hash(index);
|
2014-01-08 10:55:24 +04:00
|
|
|
}
|
|
|
|
|
2015-07-29 11:25:49 +03:00
|
|
|
static st_index_t
|
|
|
|
objid_hash(VALUE obj)
|
|
|
|
{
|
2019-11-01 03:21:01 +03:00
|
|
|
VALUE object_id = rb_obj_id(obj);
|
|
|
|
if (!FIXNUM_P(object_id))
|
|
|
|
object_id = rb_big_hash(object_id);
|
|
|
|
|
2019-04-23 22:10:52 +03:00
|
|
|
#if SIZEOF_LONG == SIZEOF_VOIDP
|
2019-11-01 03:21:01 +03:00
|
|
|
return (st_index_t)st_index_hash((st_index_t)NUM2LONG(object_id));
|
2019-04-23 22:10:52 +03:00
|
|
|
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
|
2019-11-01 03:21:01 +03:00
|
|
|
return (st_index_t)st_index_hash((st_index_t)NUM2LL(object_id));
|
2019-04-23 22:10:52 +03:00
|
|
|
#endif
|
2015-07-29 11:25:49 +03:00
|
|
|
}
|
|
|
|
|
2019-08-24 19:09:53 +03:00
|
|
|
/**
|
|
|
|
* call-seq:
|
|
|
|
* obj.hash -> integer
|
|
|
|
*
|
|
|
|
* Generates an Integer hash value for this object. This function must have the
|
|
|
|
* property that <code>a.eql?(b)</code> implies <code>a.hash == b.hash</code>.
|
|
|
|
*
|
|
|
|
* The hash value is used along with #eql? by the Hash class to determine if
|
|
|
|
* two objects reference the same hash key. Any hash value that exceeds the
|
|
|
|
* capacity of an Integer will be truncated before being used.
|
|
|
|
*
|
|
|
|
* The hash value for an object may not be identical across invocations or
|
|
|
|
* implementations of Ruby. If you need a stable identifier across Ruby
|
|
|
|
* invocations and implementations you will need to generate one with a custom
|
|
|
|
* method.
|
2020-05-13 04:01:16 +03:00
|
|
|
*
|
|
|
|
* Certain core classes such as Integer use built-in hash calculations and
|
|
|
|
* do not call the #hash method when used as a hash key.
|
2019-08-24 19:09:53 +03:00
|
|
|
*--
|
|
|
|
* \private
|
|
|
|
*++
|
|
|
|
*/
|
2015-07-29 11:25:49 +03:00
|
|
|
VALUE
|
|
|
|
rb_obj_hash(VALUE obj)
|
|
|
|
{
|
2016-12-07 07:25:50 +03:00
|
|
|
long hnum = any_hash(obj, objid_hash);
|
2016-10-04 19:25:01 +03:00
|
|
|
return ST2FIX(hnum);
|
2015-07-29 11:25:49 +03:00
|
|
|
}
|
|
|
|
|
2007-07-05 05:06:49 +04:00
|
|
|
static const struct st_hash_type objhash = {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_any_cmp,
|
|
|
|
rb_any_hash,
|
1998-01-16 15:13:05 +03:00
|
|
|
};
|
|
|
|
|
2015-01-23 05:36:50 +03:00
|
|
|
#define rb_ident_cmp st_numcmp
|
|
|
|
|
|
|
|
static st_index_t
|
|
|
|
rb_ident_hash(st_data_t n)
|
|
|
|
{
|
2015-08-14 22:52:06 +03:00
|
|
|
#ifdef USE_FLONUM /* RUBY */
|
2015-01-23 05:36:50 +03:00
|
|
|
/*
|
|
|
|
* - flonum (on 64-bit) is pathologically bad, mix the actual
|
|
|
|
* float value in, but do not use the float value as-is since
|
|
|
|
* many integers get interpreted as 2.0 or -2.0 [Bug #10761]
|
|
|
|
*/
|
|
|
|
if (FLONUM_P(n)) {
|
2019-01-30 07:54:01 +03:00
|
|
|
n ^= dbl_to_index(rb_float_value(n));
|
2015-01-23 05:36:50 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-30 08:08:36 +03:00
|
|
|
return (st_index_t)st_index_hash((st_index_t)n);
|
2015-01-23 05:36:50 +03:00
|
|
|
}
|
|
|
|
|
2019-10-17 20:51:15 +03:00
|
|
|
#define identhash rb_hashtype_ident
|
|
|
|
const struct st_hash_type rb_hashtype_ident = {
|
2015-01-23 05:36:50 +03:00
|
|
|
rb_ident_cmp,
|
|
|
|
rb_ident_hash,
|
|
|
|
};
|
2008-03-04 13:14:40 +03:00
|
|
|
|
2019-01-04 04:59:49 +03:00
|
|
|
typedef st_index_t st_hash_t;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-01-04 04:59:49 +03:00
|
|
|
/*
|
|
|
|
* RHASH_AR_TABLE_P(h):
|
|
|
|
* * as.ar == NULL or
|
|
|
|
* as.ar points ar_table.
|
|
|
|
* * as.ar is allocated by transient heap or xmalloc.
|
|
|
|
*
|
|
|
|
* !RHASH_AR_TABLE_P(h):
|
|
|
|
* * as.st points st_table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define RHASH_AR_TABLE_MAX_BOUND RHASH_AR_TABLE_MAX_SIZE
|
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
#define RHASH_AR_TABLE_REF(hash, n) (&RHASH_AR_TABLE(hash)->pairs[n])
|
|
|
|
#define RHASH_AR_CLEARED_HINT 0xff
|
|
|
|
|
|
|
|
typedef struct ar_table_pair_struct {
|
2019-01-04 04:59:49 +03:00
|
|
|
VALUE key;
|
2019-01-17 19:53:10 +03:00
|
|
|
VALUE val;
|
|
|
|
} ar_table_pair;
|
2019-01-04 04:59:49 +03:00
|
|
|
|
|
|
|
typedef struct ar_table_struct {
|
2019-01-17 19:53:10 +03:00
|
|
|
/* 64bit CPU: 8B * 2 * 8 = 128B */
|
|
|
|
ar_table_pair pairs[RHASH_AR_TABLE_MAX_SIZE];
|
2019-01-04 04:59:49 +03:00
|
|
|
} ar_table;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
rb_hash_ar_table_size(void)
|
|
|
|
{
|
|
|
|
return sizeof(ar_table);
|
|
|
|
}
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
static inline st_hash_t
|
2019-01-06 01:23:54 +03:00
|
|
|
ar_do_hash(st_data_t key)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2019-01-17 19:53:10 +03:00
|
|
|
return (st_hash_t)rb_any_hash(key);
|
|
|
|
}
|
|
|
|
|
2019-08-01 10:04:40 +03:00
|
|
|
static inline ar_hint_t
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_do_hash_hint(st_hash_t hash_value)
|
|
|
|
{
|
2019-08-01 10:04:40 +03:00
|
|
|
return (ar_hint_t)hash_value;
|
2019-01-17 19:53:10 +03:00
|
|
|
}
|
|
|
|
|
2019-08-01 10:04:40 +03:00
|
|
|
static inline ar_hint_t
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_hint(VALUE hash, unsigned int index)
|
|
|
|
{
|
|
|
|
return RHASH(hash)->ar_hint.ary[index];
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2019-08-01 10:04:40 +03:00
|
|
|
ar_hint_set_hint(VALUE hash, unsigned int index, ar_hint_t hint)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2019-01-17 19:53:10 +03:00
|
|
|
RHASH(hash)->ar_hint.ary[index] = hint;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_hint_set(VALUE hash, unsigned int index, st_hash_t hash_value)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_hint_set_hint(hash, index, ar_do_hash_hint(hash_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
ar_clear_entry(VALUE hash, unsigned int index)
|
|
|
|
{
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
|
|
|
|
pair->key = Qundef;
|
|
|
|
ar_hint_set_hint(hash, index, RHASH_AR_CLEARED_HINT);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_cleared_entry(VALUE hash, unsigned int index)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2019-01-17 19:53:10 +03:00
|
|
|
if (ar_hint(hash, index) == RHASH_AR_CLEARED_HINT) {
|
|
|
|
/* RHASH_AR_CLEARED_HINT is only a hint, not mean cleared entry,
|
|
|
|
* so you need to check key == Qundef
|
|
|
|
*/
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
|
|
|
|
return pair->key == Qundef;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
ar_set_entry(VALUE hash, unsigned int index, st_data_t key, st_data_t val, st_hash_t hash_value)
|
|
|
|
{
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, index);
|
|
|
|
pair->key = key;
|
|
|
|
pair->val = val;
|
|
|
|
ar_hint_set(hash, index, hash_value);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
#define RHASH_AR_TABLE_SIZE(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
|
2019-07-31 23:59:04 +03:00
|
|
|
RHASH_AR_TABLE_SIZE_RAW(h))
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
#define RHASH_AR_TABLE_BOUND_RAW(h) \
|
|
|
|
((unsigned int)((RBASIC(h)->flags >> RHASH_AR_TABLE_BOUND_SHIFT) & \
|
|
|
|
(RHASH_AR_TABLE_BOUND_MASK >> RHASH_AR_TABLE_BOUND_SHIFT)))
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
#define RHASH_AR_TABLE_BOUND(h) (HASH_ASSERT(RHASH_AR_TABLE_P(h)), \
|
2019-07-31 23:59:04 +03:00
|
|
|
RHASH_AR_TABLE_BOUND_RAW(h))
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
#define RHASH_ST_TABLE_SET(h, s) rb_hash_st_table_set(h, s)
|
2018-12-14 04:10:15 +03:00
|
|
|
#define RHASH_TYPE(hash) (RHASH_AR_TABLE_P(hash) ? &objhash : RHASH_ST_TABLE(hash)->type)
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-07-31 23:48:22 +03:00
|
|
|
#define HASH_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(HASH_DEBUG, expr, #expr)
|
2019-07-15 05:27:38 +03:00
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
#if HASH_DEBUG
|
|
|
|
#define hash_verify(hash) hash_verify_(hash, __FILE__, __LINE__)
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_hash_dump(VALUE hash)
|
|
|
|
{
|
|
|
|
rb_obj_info_dump(hash);
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-11-03 10:58:56 +03:00
|
|
|
fprintf(stderr, " size:%u bound:%u\n",
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_SIZE(hash), RHASH_AR_TABLE_BOUND(hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
for (i=0; i<bound; i++) {
|
|
|
|
st_data_t k, v;
|
2018-10-31 01:12:12 +03:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
if (!ar_cleared_entry(hash, i)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
char b1[0x100], b2[0x100];
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
k = pair->key;
|
|
|
|
v = pair->val;
|
|
|
|
fprintf(stderr, " %d key:%s val:%s hint:%02x\n", i,
|
2018-10-31 01:11:51 +03:00
|
|
|
rb_raw_obj_info(b1, 0x100, k),
|
2019-01-17 19:53:10 +03:00
|
|
|
rb_raw_obj_info(b2, 0x100, v),
|
|
|
|
ar_hint(hash, i));
|
2018-10-31 01:11:51 +03:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, " %d empty\n", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
hash_verify_(VALUE hash, const char *file, int line)
|
|
|
|
{
|
|
|
|
HASH_ASSERT(RB_TYPE_P(hash, T_HASH));
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
unsigned i, n = 0, bound = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
for (i=0; i<bound; i++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
st_data_t k, v;
|
|
|
|
if (!ar_cleared_entry(hash, i)) {
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
k = pair->key;
|
|
|
|
v = pair->val;
|
2018-10-31 01:11:51 +03:00
|
|
|
HASH_ASSERT(k != Qundef);
|
|
|
|
HASH_ASSERT(v != Qundef);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
if (n != RHASH_AR_TABLE_SIZE(hash)) {
|
|
|
|
rb_bug("n:%u, RHASH_AR_TABLE_SIZE:%u", n, RHASH_AR_TABLE_SIZE(hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
HASH_ASSERT(RHASH_ST_TABLE(hash) != NULL);
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_SIZE_RAW(hash) == 0);
|
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_BOUND_RAW(hash) == 0);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 06:42:00 +03:00
|
|
|
#if USE_TRANSIENT_HEP
|
2018-10-31 01:11:51 +03:00
|
|
|
if (RHASH_TRANSIENT_P(hash)) {
|
2018-12-14 04:10:15 +03:00
|
|
|
volatile st_data_t MAYBE_UNUSED(key) = RHASH_AR_TABLE_REF(hash, 0)->key; /* read */
|
|
|
|
HASH_ASSERT(RHASH_AR_TABLE(hash) != NULL);
|
|
|
|
HASH_ASSERT(rb_transient_heap_managed_ptr_p(RHASH_AR_TABLE(hash)));
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2020-02-25 06:42:00 +03:00
|
|
|
#endif
|
2018-10-31 01:11:51 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define hash_verify(h) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline int
|
2018-12-14 05:27:23 +03:00
|
|
|
RHASH_TABLE_NULL_P(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH(hash)->as.ar == NULL) {
|
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 05:27:23 +03:00
|
|
|
static inline int
|
|
|
|
RHASH_TABLE_EMPTY_P(VALUE hash)
|
|
|
|
{
|
|
|
|
return RHASH_SIZE(hash) == 0;
|
|
|
|
}
|
|
|
|
|
2019-11-13 06:41:39 +03:00
|
|
|
int
|
2018-12-14 04:10:15 +03:00
|
|
|
rb_hash_ar_table_p(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
|
|
|
if (FL_TEST_RAW((hash), RHASH_ST_TABLE_FLAG)) {
|
|
|
|
HASH_ASSERT(RHASH(hash)->as.st != NULL);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_table *
|
|
|
|
rb_hash_ar_table(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
|
|
|
|
return RHASH(hash)->as.ar;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:41:39 +03:00
|
|
|
st_table *
|
2018-10-31 01:11:51 +03:00
|
|
|
rb_hash_st_table(VALUE hash)
|
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(!RHASH_AR_TABLE_P(hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
return RHASH(hash)->as.st;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_hash_st_table_set(VALUE hash, st_table *st)
|
|
|
|
{
|
|
|
|
HASH_ASSERT(st != NULL);
|
|
|
|
FL_SET_RAW((hash), RHASH_ST_TABLE_FLAG);
|
|
|
|
RHASH(hash)->as.st = st;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-12-14 04:10:15 +03:00
|
|
|
hash_ar_table_set(VALUE hash, ar_table *ar)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_P(hash));
|
|
|
|
HASH_ASSERT((RHASH_TRANSIENT_P(hash) && ar == NULL) ? FALSE : TRUE);
|
|
|
|
RHASH(hash)->as.ar = ar;
|
2018-10-31 01:11:51 +03:00
|
|
|
hash_verify(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RHASH_SET_ST_FLAG(h) FL_SET_RAW(h, RHASH_ST_TABLE_FLAG)
|
|
|
|
#define RHASH_UNSET_ST_FLAG(h) FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG)
|
|
|
|
|
2019-07-31 23:59:04 +03:00
|
|
|
static inline void
|
|
|
|
RHASH_AR_TABLE_BOUND_SET(VALUE h, st_index_t n)
|
|
|
|
{
|
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_P(h));
|
|
|
|
HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_BOUND);
|
|
|
|
|
|
|
|
RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
|
|
|
|
RBASIC(h)->flags |= n << RHASH_AR_TABLE_BOUND_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
RHASH_AR_TABLE_SIZE_SET(VALUE h, st_index_t n)
|
|
|
|
{
|
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_P(h));
|
|
|
|
HASH_ASSERT(n <= RHASH_AR_TABLE_MAX_SIZE);
|
|
|
|
|
|
|
|
RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
|
|
|
|
RBASIC(h)->flags |= n << RHASH_AR_TABLE_SIZE_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
HASH_AR_TABLE_SIZE_ADD(VALUE h, st_index_t n)
|
|
|
|
{
|
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_P(h));
|
|
|
|
|
|
|
|
RHASH_AR_TABLE_SIZE_SET(h, RHASH_AR_TABLE_SIZE(h) + n);
|
|
|
|
|
|
|
|
hash_verify(h);
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
#define RHASH_AR_TABLE_SIZE_INC(h) HASH_AR_TABLE_SIZE_ADD(h, 1)
|
2019-01-17 10:52:47 +03:00
|
|
|
|
|
|
|
static inline void
|
2019-07-19 00:15:47 +03:00
|
|
|
RHASH_AR_TABLE_SIZE_DEC(VALUE h)
|
|
|
|
{
|
2019-01-17 10:52:47 +03:00
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_P(h));
|
|
|
|
int new_size = RHASH_AR_TABLE_SIZE(h) - 1;
|
|
|
|
|
|
|
|
if (new_size != 0) {
|
|
|
|
RHASH_AR_TABLE_SIZE_SET(h, new_size);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RHASH_AR_TABLE_SIZE_SET(h, 0);
|
|
|
|
RHASH_AR_TABLE_BOUND_SET(h, 0);
|
|
|
|
}
|
|
|
|
hash_verify(h);
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-07-31 23:59:04 +03:00
|
|
|
static inline void
|
|
|
|
RHASH_AR_TABLE_CLEAR(VALUE h)
|
|
|
|
{
|
|
|
|
RBASIC(h)->flags &= ~RHASH_AR_TABLE_SIZE_MASK;
|
|
|
|
RBASIC(h)->flags &= ~RHASH_AR_TABLE_BOUND_MASK;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-07-31 23:59:04 +03:00
|
|
|
hash_ar_table_set(h, NULL);
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
static ar_table*
|
|
|
|
ar_alloc_table(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_table *tab = (ar_table*)rb_transient_heap_alloc(hash, sizeof(ar_table));
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
if (tab != NULL) {
|
2018-10-31 01:12:12 +03:00
|
|
|
RHASH_SET_TRANSIENT_FLAG(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-10-31 01:12:12 +03:00
|
|
|
RHASH_UNSET_TRANSIENT_FLAG(hash);
|
2018-12-14 04:10:15 +03:00
|
|
|
tab = (ar_table*)ruby_xmalloc(sizeof(ar_table));
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_SIZE_SET(hash, 0);
|
|
|
|
RHASH_AR_TABLE_BOUND_SET(hash, 0);
|
2019-07-31 23:50:58 +03:00
|
|
|
hash_ar_table_set(hash, tab);
|
2018-12-14 04:10:15 +03:00
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
return tab;
|
2019-01-06 01:40:32 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
NOINLINE(static int ar_equal(VALUE x, VALUE y));
|
2019-01-06 01:40:32 +03:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
static int
|
|
|
|
ar_equal(VALUE x, VALUE y)
|
2019-01-06 01:40:32 +03:00
|
|
|
{
|
2019-01-17 19:53:10 +03:00
|
|
|
return rb_any_cmp(x, y) == 0;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
2018-11-03 10:58:56 +03:00
|
|
|
static unsigned
|
2019-08-01 10:04:40 +03:00
|
|
|
ar_find_entry_hint(VALUE hash, ar_hint_t hint, st_data_t key)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
|
2019-08-01 10:04:40 +03:00
|
|
|
const ar_hint_t *hints = RHASH(hash)->ar_hint.ary;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
/* if table is NULL, then bound also should be 0 */
|
|
|
|
|
|
|
|
for (i = 0; i < bound; i++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (hints[i] == hint) {
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
if (ar_equal(key, pair->key)) {
|
|
|
|
RB_DEBUG_COUNTER_INC(artable_hint_hit);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if 0
|
|
|
|
static int pid;
|
|
|
|
static char fname[256];
|
|
|
|
static FILE *fp;
|
|
|
|
|
|
|
|
if (pid != getpid()) {
|
|
|
|
snprintf(fname, sizeof(fname), "/tmp/ruby-armiss.%d", pid = getpid());
|
|
|
|
if ((fp = fopen(fname, "w")) == NULL) rb_bug("fopen");
|
|
|
|
}
|
|
|
|
|
|
|
|
st_hash_t h1 = ar_do_hash(key);
|
|
|
|
st_hash_t h2 = ar_do_hash(pair->key);
|
|
|
|
|
|
|
|
fprintf(fp, "miss: hash_eq:%d hints[%d]:%02x hint:%02x\n"
|
|
|
|
" key :%016lx %s\n"
|
|
|
|
" pair->key:%016lx %s\n",
|
|
|
|
h1 == h2, i, hints[i], hint,
|
|
|
|
h1, rb_obj_info(key), h2, rb_obj_info(pair->key));
|
|
|
|
#endif
|
|
|
|
RB_DEBUG_COUNTER_INC(artable_hint_miss);
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-17 19:53:10 +03:00
|
|
|
RB_DEBUG_COUNTER_INC(artable_hint_notfound);
|
2018-12-14 04:10:15 +03:00
|
|
|
return RHASH_AR_TABLE_MAX_BOUND;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
static unsigned
|
|
|
|
ar_find_entry(VALUE hash, st_hash_t hash_value, st_data_t key)
|
|
|
|
{
|
2019-08-01 10:04:40 +03:00
|
|
|
ar_hint_t hint = ar_do_hash_hint(hash_value);
|
2019-01-17 19:53:10 +03:00
|
|
|
return ar_find_entry_hint(hash, hint, key);
|
|
|
|
}
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
static inline void
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_free_and_clear_table(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_table *tab = RHASH_AR_TABLE(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
if (tab) {
|
|
|
|
if (RHASH_TRANSIENT_P(hash)) {
|
|
|
|
RHASH_UNSET_TRANSIENT_FLAG(hash);
|
2018-10-31 01:12:12 +03:00
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
else {
|
2018-12-14 04:10:15 +03:00
|
|
|
ruby_xfree(RHASH_AR_TABLE(hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_CLEAR(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
|
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
|
2018-10-31 01:11:51 +03:00
|
|
|
HASH_ASSERT(RHASH_TRANSIENT_P(hash) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_try_convert_table(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2020-03-06 21:55:54 +03:00
|
|
|
if (!RHASH_AR_TABLE_P(hash)) return;
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
const unsigned size = RHASH_AR_TABLE_SIZE(hash);
|
2020-03-06 21:55:54 +03:00
|
|
|
|
|
|
|
st_table *new_tab;
|
2018-10-31 01:11:51 +03:00
|
|
|
st_index_t i;
|
|
|
|
|
2020-03-06 21:55:54 +03:00
|
|
|
if (size < RHASH_AR_TABLE_MAX_SIZE) {
|
2018-10-31 01:11:51 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_tab = st_init_table_with_size(&objhash, size * 2);
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
for (i = 0; i < RHASH_AR_TABLE_MAX_BOUND; i++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
st_add_direct(new_tab, pair->key, pair->val);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_free_and_clear_table(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
RHASH_ST_TABLE_SET(hash, new_tab);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static st_table *
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_force_convert_table(VALUE hash, const char *file, int line)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
|
|
|
st_table *new_tab;
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_ST_TABLE_P(hash)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
return RHASH_ST_TABLE(hash);
|
|
|
|
}
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE(hash)) {
|
|
|
|
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
#if RHASH_CONVERT_TABLE_DEBUG
|
|
|
|
rb_obj_info_dump(hash);
|
|
|
|
fprintf(stderr, "force_convert: %s:%d\n", file, line);
|
|
|
|
RB_DEBUG_COUNTER_INC(obj_hash_force_convert);
|
|
|
|
#endif
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
new_tab = st_init_table_with_size(&objhash, RHASH_AR_TABLE_SIZE(hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
for (i = 0; i < bound; i++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (ar_cleared_entry(hash, i)) continue;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
st_add_direct(new_tab, pair->key, pair->val);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_free_and_clear_table(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_tab = st_init_table(&objhash);
|
|
|
|
}
|
|
|
|
RHASH_ST_TABLE_SET(hash, new_tab);
|
|
|
|
|
|
|
|
return new_tab;
|
|
|
|
}
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
static ar_table *
|
|
|
|
hash_ar_table(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 05:27:23 +03:00
|
|
|
if (RHASH_TABLE_NULL_P(hash)) {
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_alloc_table(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
return RHASH_AR_TABLE(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_compact_table(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
const unsigned bound = RHASH_AR_TABLE_BOUND(hash);
|
|
|
|
const unsigned size = RHASH_AR_TABLE_SIZE(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
if (size == bound) {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
else {
|
2018-11-03 10:58:56 +03:00
|
|
|
unsigned i, j=0;
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_table_pair *pairs = RHASH_AR_TABLE(hash)->pairs;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
for (i=0; i<bound; i++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (ar_cleared_entry(hash, i)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
if (j <= i) j = i+1;
|
|
|
|
for (; j<bound; j++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (!ar_cleared_entry(hash, j)) {
|
|
|
|
pairs[i] = pairs[j];
|
|
|
|
ar_hint_set_hint(hash, i, (st_hash_t)ar_hint(hash, j));
|
|
|
|
ar_clear_entry(hash, j);
|
2018-10-31 01:11:51 +03:00
|
|
|
j++;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* non-empty is not found */
|
|
|
|
goto done;
|
|
|
|
found:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
HASH_ASSERT(i<=bound);
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_BOUND_SET(hash, size);
|
2018-10-31 01:11:51 +03:00
|
|
|
hash_verify(hash);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_add_direct_with_hash(VALUE hash, st_data_t key, st_data_t val, st_hash_t hash_value)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
unsigned bin = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
|
2018-10-31 01:12:12 +03:00
|
|
|
return 1;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-12-14 04:10:15 +03:00
|
|
|
if (UNLIKELY(bin >= RHASH_AR_TABLE_MAX_BOUND)) {
|
|
|
|
bin = ar_compact_table(hash);
|
|
|
|
hash_ar_table(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_set_entry(hash, bin, key, val, hash_value);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
|
|
|
|
RHASH_AR_TABLE_SIZE_INC(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
ar_general_foreach(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
|
|
|
|
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
for (i = 0; i < bound; i++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (ar_cleared_entry(hash, i)) continue;
|
|
|
|
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
enum st_retval retval = (*func)(pair->key, pair->val, arg, 0);
|
|
|
|
/* pair may be not valid here because of theap */
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
switch (retval) {
|
|
|
|
case ST_CONTINUE:
|
|
|
|
break;
|
|
|
|
case ST_CHECK:
|
|
|
|
case ST_STOP:
|
|
|
|
return 0;
|
2019-04-20 04:19:47 +03:00
|
|
|
case ST_REPLACE:
|
|
|
|
if (replace) {
|
2019-01-17 19:53:10 +03:00
|
|
|
VALUE key = pair->key;
|
|
|
|
VALUE val = pair->val;
|
|
|
|
retval = (*replace)(&key, &val, arg, TRUE);
|
|
|
|
|
|
|
|
// TODO: pair should be same as pair before.
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
pair->key = key;
|
|
|
|
pair->val = val;
|
2019-04-20 04:19:47 +03:00
|
|
|
}
|
|
|
|
break;
|
2018-10-31 01:11:51 +03:00
|
|
|
case ST_DELETE:
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_clear_entry(hash, i);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_SIZE_DEC(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:19:47 +03:00
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
ar_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
|
2019-04-20 04:19:47 +03:00
|
|
|
{
|
|
|
|
return ar_general_foreach(hash, func, replace, arg);
|
|
|
|
}
|
|
|
|
|
2019-08-27 05:53:39 +03:00
|
|
|
struct functor {
|
|
|
|
st_foreach_callback_func *func;
|
|
|
|
st_data_t arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
apply_functor(st_data_t k, st_data_t v, st_data_t d, int _)
|
|
|
|
{
|
|
|
|
const struct functor *f = (void *)d;
|
|
|
|
return f->func(k, v, f->arg);
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:19:47 +03:00
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
ar_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
|
2019-04-20 04:19:47 +03:00
|
|
|
{
|
2019-08-27 05:53:39 +03:00
|
|
|
const struct functor f = { func, arg };
|
|
|
|
return ar_general_foreach(hash, apply_functor, NULL, (st_data_t)&f);
|
2019-04-20 04:19:47 +03:00
|
|
|
}
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
ar_foreach_check(VALUE hash, st_foreach_check_callback_func *func, st_data_t arg,
|
2018-10-31 01:11:51 +03:00
|
|
|
st_data_t never)
|
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
|
|
|
|
unsigned i, ret = 0, bound = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
enum st_retval retval;
|
|
|
|
st_data_t key;
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_table_pair *pair;
|
2019-08-01 10:04:40 +03:00
|
|
|
ar_hint_t hint;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
for (i = 0; i < bound; i++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (ar_cleared_entry(hash, i)) continue;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
key = pair->key;
|
|
|
|
hint = ar_hint(hash, i);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
retval = (*func)(key, pair->val, arg, 0);
|
|
|
|
hash_verify(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
switch (retval) {
|
|
|
|
case ST_CHECK: {
|
2020-01-12 21:36:47 +03:00
|
|
|
pair = RHASH_AR_TABLE_REF(hash, i);
|
2019-01-17 19:53:10 +03:00
|
|
|
if (pair->key == never) break;
|
|
|
|
ret = ar_find_entry_hint(hash, hint, key);
|
2018-12-14 04:10:15 +03:00
|
|
|
if (ret == RHASH_AR_TABLE_MAX_BOUND) {
|
2018-10-31 01:11:51 +03:00
|
|
|
retval = (*func)(0, 0, arg, 1);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ST_CONTINUE:
|
|
|
|
break;
|
|
|
|
case ST_STOP:
|
2019-04-20 04:19:47 +03:00
|
|
|
case ST_REPLACE:
|
2018-10-31 01:11:51 +03:00
|
|
|
return 0;
|
|
|
|
case ST_DELETE: {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (!ar_cleared_entry(hash, i)) {
|
|
|
|
ar_clear_entry(hash, i);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_SIZE_DEC(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_update(VALUE hash, st_data_t key,
|
2018-10-31 01:12:12 +03:00
|
|
|
st_update_callback_func *func, st_data_t arg)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
|
|
|
int retval, existing;
|
2018-12-14 04:10:15 +03:00
|
|
|
unsigned bin = RHASH_AR_TABLE_MAX_BOUND;
|
2018-10-31 01:11:51 +03:00
|
|
|
st_data_t value = 0, old_key;
|
2019-01-06 01:23:54 +03:00
|
|
|
st_hash_t hash_value = ar_do_hash(key);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2020-03-06 21:32:15 +03:00
|
|
|
if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
|
|
|
|
// `#hash` changes ar_table -> st_table
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
|
2019-01-06 01:23:54 +03:00
|
|
|
bin = ar_find_entry(hash, hash_value, key);
|
2018-12-14 04:10:15 +03:00
|
|
|
existing = (bin != RHASH_AR_TABLE_MAX_BOUND) ? TRUE : FALSE;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-12-14 04:10:15 +03:00
|
|
|
hash_ar_table(hash); /* allocate ltbl if needed */
|
2018-10-31 01:11:51 +03:00
|
|
|
existing = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existing) {
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
|
|
|
|
key = pair->key;
|
|
|
|
value = pair->val;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
old_key = key;
|
|
|
|
retval = (*func)(&key, &value, arg, existing);
|
2019-01-17 19:53:10 +03:00
|
|
|
/* pair can be invalid here because of theap */
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
switch (retval) {
|
|
|
|
case ST_CONTINUE:
|
|
|
|
if (!existing) {
|
2018-12-14 04:10:15 +03:00
|
|
|
if (ar_add_direct_with_hash(hash, key, value, hash_value)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
|
2018-10-31 01:11:51 +03:00
|
|
|
if (old_key != key) {
|
2019-01-17 19:53:10 +03:00
|
|
|
pair->key = key;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2019-01-17 19:53:10 +03:00
|
|
|
pair->val = value;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ST_DELETE:
|
|
|
|
if (existing) {
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_clear_entry(hash, bin);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_SIZE_DEC(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_insert(VALUE hash, st_data_t key, st_data_t value)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
unsigned bin = RHASH_AR_TABLE_BOUND(hash);
|
2019-01-06 01:23:54 +03:00
|
|
|
st_hash_t hash_value = ar_do_hash(key);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2020-03-06 21:32:15 +03:00
|
|
|
if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
|
|
|
|
// `#hash` changes ar_table -> st_table
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
hash_ar_table(hash); /* prepare ltbl */
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-01-06 01:23:54 +03:00
|
|
|
bin = ar_find_entry(hash, hash_value, key);
|
2018-12-14 04:10:15 +03:00
|
|
|
if (bin == RHASH_AR_TABLE_MAX_BOUND) {
|
|
|
|
if (RHASH_AR_TABLE_SIZE(hash) >= RHASH_AR_TABLE_MAX_SIZE) {
|
2018-10-31 01:11:51 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
else if (bin >= RHASH_AR_TABLE_MAX_BOUND) {
|
|
|
|
bin = ar_compact_table(hash);
|
|
|
|
hash_ar_table(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
ar_set_entry(hash, bin, key, value, hash_value);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_BOUND_SET(hash, bin+1);
|
|
|
|
RHASH_AR_TABLE_SIZE_INC(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
2019-01-17 19:53:10 +03:00
|
|
|
RHASH_AR_TABLE_REF(hash, bin)->val = value;
|
2018-10-31 01:11:51 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_lookup(VALUE hash, st_data_t key, st_data_t *value)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2019-01-04 10:49:00 +03:00
|
|
|
if (RHASH_AR_TABLE_SIZE(hash) == 0) {
|
2018-10-31 01:11:51 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
2019-01-06 01:23:54 +03:00
|
|
|
st_hash_t hash_value = ar_do_hash(key);
|
2020-03-06 21:32:15 +03:00
|
|
|
if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
|
|
|
|
// `#hash` changes ar_table -> st_table
|
|
|
|
return st_lookup(RHASH_ST_TABLE(hash), key, value);
|
|
|
|
}
|
2019-01-06 01:23:54 +03:00
|
|
|
unsigned bin = ar_find_entry(hash, hash_value, key);
|
2019-01-04 10:49:00 +03:00
|
|
|
|
|
|
|
if (bin == RHASH_AR_TABLE_MAX_BOUND) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
HASH_ASSERT(bin < RHASH_AR_TABLE_MAX_BOUND);
|
|
|
|
if (value != NULL) {
|
2019-01-17 19:53:10 +03:00
|
|
|
*value = RHASH_AR_TABLE_REF(hash, bin)->val;
|
2019-01-04 10:49:00 +03:00
|
|
|
}
|
|
|
|
return 1;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_delete(VALUE hash, st_data_t *key, st_data_t *value)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-11-03 10:58:56 +03:00
|
|
|
unsigned bin;
|
2019-01-06 01:23:54 +03:00
|
|
|
st_hash_t hash_value = ar_do_hash(*key);
|
2018-10-31 01:12:12 +03:00
|
|
|
|
2020-03-06 21:32:15 +03:00
|
|
|
if (UNLIKELY(!RHASH_AR_TABLE_P(hash))) {
|
|
|
|
// `#hash` changes ar_table -> st_table
|
|
|
|
return st_delete(RHASH_ST_TABLE(hash), key, value);
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2019-01-06 01:23:54 +03:00
|
|
|
bin = ar_find_entry(hash, hash_value, *key);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (bin == RHASH_AR_TABLE_MAX_BOUND) {
|
2018-10-31 01:11:51 +03:00
|
|
|
if (value != 0) *value = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (value != 0) {
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, bin);
|
|
|
|
*value = pair->val;
|
|
|
|
}
|
|
|
|
ar_clear_entry(hash, bin);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_SIZE_DEC(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_shift(VALUE hash, st_data_t *key, st_data_t *value)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_SIZE(hash) > 0) {
|
|
|
|
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
for (i = 0; i < bound; i++) {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (!ar_cleared_entry(hash, i)) {
|
|
|
|
ar_table_pair *pair = RHASH_AR_TABLE_REF(hash, i);
|
|
|
|
if (value != 0) *value = pair->val;
|
|
|
|
*key = pair->key;
|
|
|
|
ar_clear_entry(hash, i);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_SIZE_DEC(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 19:53:10 +03:00
|
|
|
if (value != NULL) *value = 0;
|
2018-10-31 01:11:51 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-31 01:55:31 +03:00
|
|
|
static long
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_keys(VALUE hash, st_data_t *keys, st_index_t size)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:55:31 +03:00
|
|
|
st_data_t *keys_start = keys, *keys_end = keys + size;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
for (i = 0; i < bound; i++) {
|
|
|
|
if (keys == keys_end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (!ar_cleared_entry(hash, i)) {
|
|
|
|
*keys++ = RHASH_AR_TABLE_REF(hash, i)->key;
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys - keys_start;
|
|
|
|
}
|
|
|
|
|
2018-10-31 01:55:31 +03:00
|
|
|
static long
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_values(VALUE hash, st_data_t *values, st_index_t size)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
unsigned i, bound = RHASH_AR_TABLE_BOUND(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
st_data_t *values_start = values, *values_end = values + size;
|
|
|
|
|
|
|
|
for (i = 0; i < bound; i++) {
|
|
|
|
if (values == values_end) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2019-01-17 19:53:10 +03:00
|
|
|
if (!ar_cleared_entry(hash, i)) {
|
|
|
|
*values++ = RHASH_AR_TABLE_REF(hash, i)->val;
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values - values_start;
|
|
|
|
}
|
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
static ar_table*
|
|
|
|
ar_copy(VALUE hash1, VALUE hash2)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_table *old_tab = RHASH_AR_TABLE(hash2);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-11-29 11:03:55 +03:00
|
|
|
if (old_tab != NULL) {
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_table *new_tab = RHASH_AR_TABLE(hash1);
|
2018-11-29 11:03:55 +03:00
|
|
|
if (new_tab == NULL) {
|
2018-12-14 04:10:15 +03:00
|
|
|
new_tab = (ar_table*) rb_transient_heap_alloc(hash1, sizeof(ar_table));
|
2018-11-29 11:03:55 +03:00
|
|
|
if (new_tab != NULL) {
|
|
|
|
RHASH_SET_TRANSIENT_FLAG(hash1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RHASH_UNSET_TRANSIENT_FLAG(hash1);
|
2018-12-14 04:10:15 +03:00
|
|
|
new_tab = (ar_table*)ruby_xmalloc(sizeof(ar_table));
|
2018-11-29 11:03:55 +03:00
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
*new_tab = *old_tab;
|
2019-01-17 19:53:10 +03:00
|
|
|
RHASH(hash1)->ar_hint.word = RHASH(hash2)->ar_hint.word;
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
|
|
|
|
RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
|
2019-07-31 23:50:58 +03:00
|
|
|
hash_ar_table_set(hash1, new_tab);
|
2018-11-01 05:50:35 +03:00
|
|
|
|
|
|
|
rb_gc_writebarrier_remember(hash1);
|
2018-10-31 01:11:51 +03:00
|
|
|
return new_tab;
|
|
|
|
}
|
|
|
|
else {
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_AR_TABLE_BOUND_SET(hash1, RHASH_AR_TABLE_BOUND(hash2));
|
|
|
|
RHASH_AR_TABLE_SIZE_SET(hash1, RHASH_AR_TABLE_SIZE(hash2));
|
2018-11-29 11:03:55 +03:00
|
|
|
|
|
|
|
if (RHASH_TRANSIENT_P(hash1)) {
|
|
|
|
RHASH_UNSET_TRANSIENT_FLAG(hash1);
|
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
else if (RHASH_AR_TABLE(hash1)) {
|
|
|
|
ruby_xfree(RHASH_AR_TABLE(hash1));
|
2018-11-29 11:03:55 +03:00
|
|
|
}
|
|
|
|
|
2019-07-31 23:50:58 +03:00
|
|
|
hash_ar_table_set(hash1, NULL);
|
2018-11-01 05:50:35 +03:00
|
|
|
|
|
|
|
rb_gc_writebarrier_remember(hash1);
|
2018-10-31 01:11:51 +03:00
|
|
|
return old_tab;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_clear(VALUE hash)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE(hash) != NULL) {
|
|
|
|
RHASH_AR_TABLE_SIZE_SET(hash, 0);
|
|
|
|
RHASH_AR_TABLE_BOUND_SET(hash, 0);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_SIZE(hash) == 0);
|
|
|
|
HASH_ASSERT(RHASH_AR_TABLE_BOUND(hash) == 0);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 11:53:44 +03:00
|
|
|
#if USE_TRANSIENT_HEAP
|
2018-10-31 01:11:51 +03:00
|
|
|
void
|
|
|
|
rb_hash_transient_heap_evacuate(VALUE hash, int promote)
|
|
|
|
{
|
|
|
|
if (RHASH_TRANSIENT_P(hash)) {
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_table *new_tab;
|
|
|
|
ar_table *old_tab = RHASH_AR_TABLE(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
if (UNLIKELY(old_tab == NULL)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
rb_gc_force_recycle(hash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HASH_ASSERT(old_tab != NULL);
|
2020-06-15 09:12:54 +03:00
|
|
|
if (! promote) {
|
|
|
|
new_tab = rb_transient_heap_alloc(hash, sizeof(ar_table));
|
|
|
|
if (new_tab == NULL) promote = true;
|
|
|
|
}
|
2018-10-31 01:12:12 +03:00
|
|
|
if (promote) {
|
2018-12-14 04:10:15 +03:00
|
|
|
new_tab = ruby_xmalloc(sizeof(ar_table));
|
2018-10-31 01:12:12 +03:00
|
|
|
RHASH_UNSET_TRANSIENT_FLAG(hash);
|
|
|
|
}
|
|
|
|
*new_tab = *old_tab;
|
2019-07-31 23:50:58 +03:00
|
|
|
hash_ar_table_set(hash, new_tab);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
hash_verify(hash);
|
|
|
|
}
|
2018-11-01 11:53:44 +03:00
|
|
|
#endif
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2007-08-21 08:57:06 +04:00
|
|
|
typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
struct foreach_safe_arg {
|
|
|
|
st_table *tbl;
|
2007-08-21 08:57:06 +04:00
|
|
|
st_foreach_func *func;
|
2004-09-29 09:15:33 +04:00
|
|
|
st_data_t arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2013-11-14 06:33:50 +04:00
|
|
|
foreach_safe_i(st_data_t key, st_data_t value, st_data_t args, int error)
|
2004-09-29 09:15:33 +04:00
|
|
|
{
|
|
|
|
int status;
|
2013-11-14 06:33:50 +04:00
|
|
|
struct foreach_safe_arg *arg = (void *)args;
|
2004-09-29 09:15:33 +04:00
|
|
|
|
2013-11-14 06:33:50 +04:00
|
|
|
if (error) return ST_STOP;
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
status = (*arg->func)(key, value, arg->arg);
|
2004-09-29 09:15:33 +04:00
|
|
|
if (status == ST_CONTINUE) {
|
|
|
|
return ST_CHECK;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-08-27 05:53:39 +03:00
|
|
|
st_foreach_safe(st_table *table, st_foreach_func *func, st_data_t a)
|
2004-09-29 09:15:33 +04:00
|
|
|
{
|
|
|
|
struct foreach_safe_arg arg;
|
|
|
|
|
|
|
|
arg.tbl = table;
|
2007-08-21 08:57:06 +04:00
|
|
|
arg.func = (st_foreach_func *)func;
|
2004-09-29 09:15:33 +04:00
|
|
|
arg.arg = a;
|
2012-03-31 02:47:33 +04:00
|
|
|
if (st_foreach_check(table, foreach_safe_i, (st_data_t)&arg, 0)) {
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
rb_raise(rb_eRuntimeError, "hash modified during iteration");
|
|
|
|
}
|
2004-09-29 09:15:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 08:57:06 +04:00
|
|
|
typedef int rb_foreach_func(VALUE, VALUE, VALUE);
|
|
|
|
|
2004-09-23 04:51:32 +04:00
|
|
|
struct hash_foreach_arg {
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE hash;
|
2007-08-21 08:57:06 +04:00
|
|
|
rb_foreach_func *func;
|
2002-12-29 17:51:22 +03:00
|
|
|
VALUE arg;
|
1998-01-16 15:13:05 +03:00
|
|
|
};
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
static int
|
2018-12-14 04:10:15 +03:00
|
|
|
hash_ar_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
|
|
|
struct hash_foreach_arg *arg = (struct hash_foreach_arg *)argp;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (error) return ST_STOP;
|
|
|
|
status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
|
|
|
|
/* TODO: rehash check? rb_raise(rb_eRuntimeError, "rehash occurred during iteration"); */
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case ST_DELETE:
|
2018-10-31 01:12:12 +03:00
|
|
|
return ST_DELETE;
|
2018-10-31 01:11:51 +03:00
|
|
|
case ST_CONTINUE:
|
2018-10-31 01:12:12 +03:00
|
|
|
break;
|
2018-10-31 01:11:51 +03:00
|
|
|
case ST_STOP:
|
2018-10-31 01:12:12 +03:00
|
|
|
return ST_STOP;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
return ST_CHECK;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static int
|
2013-11-14 06:33:50 +04:00
|
|
|
hash_foreach_iter(st_data_t key, st_data_t value, st_data_t argp, int error)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-02-21 15:22:49 +04:00
|
|
|
struct hash_foreach_arg *arg = (struct hash_foreach_arg *)argp;
|
1998-01-16 15:13:05 +03:00
|
|
|
int status;
|
2004-09-22 08:48:52 +04:00
|
|
|
st_table *tbl;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-11-14 06:33:50 +04:00
|
|
|
if (error) return ST_STOP;
|
2018-10-31 01:11:51 +03:00
|
|
|
tbl = RHASH_ST_TABLE(arg->hash);
|
2009-02-26 13:29:13 +03:00
|
|
|
status = (*arg->func)((VALUE)key, (VALUE)value, arg->arg);
|
2018-10-31 01:11:51 +03:00
|
|
|
if (RHASH_ST_TABLE(arg->hash) != tbl) {
|
|
|
|
rb_raise(rb_eRuntimeError, "rehash occurred during iteration");
|
2004-09-22 08:48:52 +04:00
|
|
|
}
|
|
|
|
switch (status) {
|
|
|
|
case ST_DELETE:
|
2012-03-31 02:53:26 +04:00
|
|
|
return ST_DELETE;
|
2004-09-22 08:48:52 +04:00
|
|
|
case ST_CONTINUE:
|
|
|
|
break;
|
|
|
|
case ST_STOP:
|
|
|
|
return ST_STOP;
|
2004-09-21 07:08:33 +04:00
|
|
|
}
|
2004-09-22 08:48:52 +04:00
|
|
|
return ST_CHECK;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:48:30 +03:00
|
|
|
static int
|
|
|
|
iter_lev_in_ivar(VALUE hash)
|
|
|
|
{
|
2019-08-01 05:20:37 +03:00
|
|
|
VALUE levval = rb_ivar_get(hash, id_hash_iter_lev);
|
2019-01-16 13:48:30 +03:00
|
|
|
HASH_ASSERT(FIXNUM_P(levval));
|
|
|
|
return FIX2INT(levval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rb_ivar_set_internal(VALUE obj, ID id, VALUE val);
|
|
|
|
|
|
|
|
static void
|
|
|
|
iter_lev_in_ivar_set(VALUE hash, int lev)
|
|
|
|
{
|
2019-08-01 05:20:37 +03:00
|
|
|
rb_ivar_set_internal(hash, id_hash_iter_lev, INT2FIX(lev));
|
2019-01-16 13:48:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iter_lev_in_flags(VALUE hash)
|
|
|
|
{
|
2019-07-31 05:08:05 +03:00
|
|
|
unsigned int u = (unsigned int)((RBASIC(hash)->flags >> RHASH_LEV_SHIFT) & RHASH_LEV_MAX);
|
2019-01-16 13:48:30 +03:00
|
|
|
return (int)u;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
RHASH_ITER_LEV(VALUE hash)
|
|
|
|
{
|
|
|
|
int lev = iter_lev_in_flags(hash);
|
|
|
|
|
|
|
|
if (lev == RHASH_LEV_MAX) {
|
|
|
|
return iter_lev_in_ivar(hash);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return lev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 22:11:32 +03:00
|
|
|
static void
|
|
|
|
hash_iter_lev_inc(VALUE hash)
|
|
|
|
{
|
2019-01-16 13:48:30 +03:00
|
|
|
int lev = iter_lev_in_flags(hash);
|
|
|
|
if (lev == RHASH_LEV_MAX) {
|
|
|
|
lev = iter_lev_in_ivar(hash);
|
|
|
|
iter_lev_in_ivar_set(hash, lev+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lev += 1;
|
2020-03-08 20:53:46 +03:00
|
|
|
RBASIC(hash)->flags = ((RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((VALUE)lev << RHASH_LEV_SHIFT));
|
2019-01-16 13:48:30 +03:00
|
|
|
if (lev == RHASH_LEV_MAX) {
|
|
|
|
iter_lev_in_ivar_set(hash, lev);
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 22:11:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hash_iter_lev_dec(VALUE hash)
|
|
|
|
{
|
2019-01-16 13:48:30 +03:00
|
|
|
int lev = iter_lev_in_flags(hash);
|
|
|
|
if (lev == RHASH_LEV_MAX) {
|
|
|
|
lev = iter_lev_in_ivar(hash);
|
|
|
|
HASH_ASSERT(lev > 0);
|
|
|
|
iter_lev_in_ivar_set(hash, lev-1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
HASH_ASSERT(lev > 0);
|
|
|
|
RBASIC(hash)->flags = ((RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((lev-1) << RHASH_LEV_SHIFT));
|
|
|
|
}
|
2019-01-09 22:11:32 +03:00
|
|
|
}
|
|
|
|
|
2013-11-15 21:15:31 +04:00
|
|
|
static VALUE
|
|
|
|
hash_foreach_ensure_rollback(VALUE hash)
|
|
|
|
{
|
2019-01-09 22:11:32 +03:00
|
|
|
hash_iter_lev_inc(hash);
|
2013-11-15 21:15:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2013-11-15 21:08:52 +04:00
|
|
|
hash_foreach_ensure(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2019-01-09 22:11:32 +03:00
|
|
|
hash_iter_lev_dec(hash);
|
1999-01-20 07:59:39 +03:00
|
|
|
return 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
int
|
2019-08-27 05:53:39 +03:00
|
|
|
rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
return ar_foreach(hash, func, arg);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return st_foreach(RHASH_ST_TABLE(hash), func, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:19:47 +03:00
|
|
|
int
|
2019-08-27 05:53:39 +03:00
|
|
|
rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
|
2019-04-20 04:19:47 +03:00
|
|
|
{
|
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
return ar_foreach_with_replace(hash, func, replace, arg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return st_foreach_with_replace(RHASH_ST_TABLE(hash), func, replace, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-23 04:51:32 +04:00
|
|
|
static VALUE
|
2012-03-31 02:40:54 +04:00
|
|
|
hash_foreach_call(VALUE arg)
|
2004-09-23 04:51:32 +04:00
|
|
|
{
|
2012-03-31 02:40:54 +04:00
|
|
|
VALUE hash = ((struct hash_foreach_arg *)arg)->hash;
|
2018-10-31 01:11:51 +03:00
|
|
|
int ret = 0;
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
ret = ar_foreach_check(hash, hash_ar_foreach_iter,
|
2018-10-31 01:11:51 +03:00
|
|
|
(st_data_t)arg, (st_data_t)Qundef);
|
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
else if (RHASH_ST_TABLE_P(hash)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
ret = st_foreach_check(RHASH_ST_TABLE(hash), hash_foreach_iter,
|
|
|
|
(st_data_t)arg, (st_data_t)Qundef);
|
|
|
|
}
|
|
|
|
if (ret) {
|
2018-10-31 01:12:12 +03:00
|
|
|
rb_raise(rb_eRuntimeError, "ret: %d, hash modified during iteration", ret);
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 09:47:45 +03:00
|
|
|
}
|
2004-09-23 04:51:32 +04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
void
|
2019-08-27 05:53:39 +03:00
|
|
|
rb_hash_foreach(VALUE hash, rb_foreach_func *func, VALUE farg)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2004-09-23 04:51:32 +04:00
|
|
|
struct hash_foreach_arg arg;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2018-12-14 05:27:23 +03:00
|
|
|
if (RHASH_TABLE_EMPTY_P(hash))
|
2007-08-30 03:12:21 +04:00
|
|
|
return;
|
2019-01-09 22:11:32 +03:00
|
|
|
hash_iter_lev_inc(hash);
|
1998-01-16 15:13:05 +03:00
|
|
|
arg.hash = hash;
|
2007-08-21 08:57:06 +04:00
|
|
|
arg.func = (rb_foreach_func *)func;
|
1998-01-16 15:13:05 +03:00
|
|
|
arg.arg = farg;
|
2013-11-15 21:08:52 +04:00
|
|
|
rb_ensure(hash_foreach_call, (VALUE)&arg, hash_foreach_ensure, hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
hash_verify(hash);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2016-03-09 10:17:01 +03:00
|
|
|
hash_alloc_flags(VALUE klass, VALUE flags, VALUE ifnone)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2016-03-09 10:17:01 +03:00
|
|
|
const VALUE wb = (RGENGC_WB_PROTECTED_HASH ? FL_WB_PROTECTED : 0);
|
|
|
|
NEWOBJ_OF(hash, struct RHash, klass, T_HASH | wb | flags);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2016-03-09 10:17:01 +03:00
|
|
|
RHASH_SET_IFNONE((VALUE)hash, ifnone);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return (VALUE)hash;
|
|
|
|
}
|
|
|
|
|
2016-03-09 10:17:01 +03:00
|
|
|
static VALUE
|
|
|
|
hash_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
return hash_alloc_flags(klass, 0, Qnil);
|
|
|
|
}
|
|
|
|
|
* probes.d: add DTrace probe declarations. [ruby-core:27448]
* array.c (empty_ary_alloc, ary_new): added array create DTrace probe.
* compile.c (rb_insns_name): allowing DTrace probes to access
instruction sequence name.
* Makefile.in: translate probes.d file to appropriate header file.
* common.mk: declare dependencies on the DTrace header.
* configure.in: add a test for existence of DTrace.
* eval.c (setup_exception): add a probe for when an exception is
raised.
* gc.c: Add DTrace probes for mark begin and end, and sweep begin and
end.
* hash.c (empty_hash_alloc): Add a probe for hash allocation.
* insns.def: Add probes for function entry and return.
* internal.h: function declaration for compile.c change.
* load.c (rb_f_load): add probes for `load` entry and exit, require
entry and exit, and wrapping search_required for load path search.
* object.c (rb_obj_alloc): added a probe for general object creation.
* parse.y (yycompile0): added a probe around parse and compile phase.
* string.c (empty_str_alloc, str_new): DTrace probes for string
allocation.
* test/dtrace/*: tests for DTrace probes.
* vm.c (vm_invoke_proc): add probes for function return on exception
raise, hash create, and instruction sequence execution.
* vm_core.h: add probe declarations for function entry and exit.
* vm_dump.c: add probes header file.
* vm_eval.c (vm_call0_cfunc, vm_call0_cfunc_with_frame): add probe on
function entry and return.
* vm_exec.c: expose instruction number to instruction name function.
* vm_insnshelper.c: add function entry and exit probes for cfunc
methods.
* vm_insnhelper.h: vm usage information is always collected, so
uncomment the functions.
12 19:14:50 2012 Akinori MUSHA <knu@iDaemons.org>
* configure.in (isinf, isnan): isinf() and isnan() are macros on
DragonFly which cannot be found by AC_REPLACE_FUNCS(). This
workaround enforces the fact that they exist on DragonFly.
12 15:59:38 2012 Shugo Maeda <shugo@ruby-lang.org>
* vm_core.h (rb_call_info_t::refinements), compile.c (new_callinfo),
vm_insnhelper.c (vm_search_method): revert r37616 because it's too
slow. [ruby-dev:46477]
* test/ruby/test_refinement.rb (test_inline_method_cache): skip
the test until the bug is fixed efficiently.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37631 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-11-13 01:52:12 +04:00
|
|
|
static VALUE
|
|
|
|
empty_hash_alloc(VALUE klass)
|
|
|
|
{
|
2015-10-29 08:32:57 +03:00
|
|
|
RUBY_DTRACE_CREATE_HOOK(HASH, 0);
|
* probes.d: add DTrace probe declarations. [ruby-core:27448]
* array.c (empty_ary_alloc, ary_new): added array create DTrace probe.
* compile.c (rb_insns_name): allowing DTrace probes to access
instruction sequence name.
* Makefile.in: translate probes.d file to appropriate header file.
* common.mk: declare dependencies on the DTrace header.
* configure.in: add a test for existence of DTrace.
* eval.c (setup_exception): add a probe for when an exception is
raised.
* gc.c: Add DTrace probes for mark begin and end, and sweep begin and
end.
* hash.c (empty_hash_alloc): Add a probe for hash allocation.
* insns.def: Add probes for function entry and return.
* internal.h: function declaration for compile.c change.
* load.c (rb_f_load): add probes for `load` entry and exit, require
entry and exit, and wrapping search_required for load path search.
* object.c (rb_obj_alloc): added a probe for general object creation.
* parse.y (yycompile0): added a probe around parse and compile phase.
* string.c (empty_str_alloc, str_new): DTrace probes for string
allocation.
* test/dtrace/*: tests for DTrace probes.
* vm.c (vm_invoke_proc): add probes for function return on exception
raise, hash create, and instruction sequence execution.
* vm_core.h: add probe declarations for function entry and exit.
* vm_dump.c: add probes header file.
* vm_eval.c (vm_call0_cfunc, vm_call0_cfunc_with_frame): add probe on
function entry and return.
* vm_exec.c: expose instruction number to instruction name function.
* vm_insnshelper.c: add function entry and exit probes for cfunc
methods.
* vm_insnhelper.h: vm usage information is always collected, so
uncomment the functions.
12 19:14:50 2012 Akinori MUSHA <knu@iDaemons.org>
* configure.in (isinf, isnan): isinf() and isnan() are macros on
DragonFly which cannot be found by AC_REPLACE_FUNCS(). This
workaround enforces the fact that they exist on DragonFly.
12 15:59:38 2012 Shugo Maeda <shugo@ruby-lang.org>
* vm_core.h (rb_call_info_t::refinements), compile.c (new_callinfo),
vm_insnhelper.c (vm_search_method): revert r37616 because it's too
slow. [ruby-dev:46477]
* test/ruby/test_refinement.rb (test_inline_method_cache): skip
the test until the bug is fixed efficiently.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37631 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-11-13 01:52:12 +04:00
|
|
|
|
|
|
|
return hash_alloc(klass);
|
|
|
|
}
|
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_new(void)
|
2000-02-29 11:05:32 +03:00
|
|
|
{
|
2002-12-20 11:33:17 +03:00
|
|
|
return hash_alloc(rb_cHash);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 14:22:28 +03:00
|
|
|
MJIT_FUNC_EXPORTED VALUE
|
2017-09-05 07:48:19 +03:00
|
|
|
rb_hash_new_with_size(st_index_t size)
|
|
|
|
{
|
|
|
|
VALUE ret = rb_hash_new();
|
2018-12-14 04:10:15 +03:00
|
|
|
if (size == 0) {
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
else if (size <= RHASH_AR_TABLE_MAX_SIZE) {
|
|
|
|
ar_alloc_table(ret);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RHASH_ST_TABLE_SET(ret, st_init_table_with_size(&objhash, size));
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2017-09-05 07:48:19 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-09 10:17:01 +03:00
|
|
|
static VALUE
|
2020-01-10 15:26:43 +03:00
|
|
|
hash_copy(VALUE ret, VALUE hash)
|
2008-04-26 16:52:25 +04:00
|
|
|
{
|
2018-10-31 01:11:51 +03:00
|
|
|
if (!RHASH_EMPTY_P(hash)) {
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash))
|
|
|
|
ar_copy(ret, hash);
|
|
|
|
else if (RHASH_ST_TABLE_P(hash))
|
2018-10-31 01:11:51 +03:00
|
|
|
RHASH_ST_TABLE_SET(ret, st_copy(RHASH_ST_TABLE(hash)));
|
|
|
|
}
|
2016-03-09 10:17:01 +03:00
|
|
|
return ret;
|
2008-04-26 16:52:25 +04:00
|
|
|
}
|
|
|
|
|
2020-01-10 15:26:43 +03:00
|
|
|
static VALUE
|
|
|
|
hash_dup(VALUE hash, VALUE klass, VALUE flags)
|
|
|
|
{
|
|
|
|
return hash_copy(hash_alloc_flags(klass, flags, RHASH_IFNONE(hash)),
|
|
|
|
hash);
|
|
|
|
}
|
|
|
|
|
2013-12-16 17:40:04 +04:00
|
|
|
VALUE
|
|
|
|
rb_hash_dup(VALUE hash)
|
|
|
|
{
|
2016-03-09 10:17:01 +03:00
|
|
|
const VALUE flags = RBASIC(hash)->flags;
|
|
|
|
VALUE ret = hash_dup(hash, rb_obj_class(hash),
|
2019-09-25 06:59:12 +03:00
|
|
|
flags & (FL_EXIVAR|RHASH_PROC_DEFAULT));
|
2016-03-09 10:17:01 +03:00
|
|
|
if (flags & FL_EXIVAR)
|
|
|
|
rb_copy_generic_ivar(ret, hash);
|
2013-12-16 17:40:04 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-20 10:17:55 +03:00
|
|
|
MJIT_FUNC_EXPORTED VALUE
|
|
|
|
rb_hash_resurrect(VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE ret = hash_dup(hash, rb_cHash, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-09-22 08:48:52 +04:00
|
|
|
static void
|
2007-08-30 03:12:21 +04:00
|
|
|
rb_hash_modify_check(VALUE hash)
|
2004-09-22 08:48:52 +04:00
|
|
|
{
|
* array.c, gc.c, hash.c, object.c, string.c, struct.c,
transcode.c, variable.c, vm.c, vm_insnhelper.c, vm_method.c:
replace calls to rb_error_frozen() with rb_check_frozen(). a
patch from Run Paint Run Run at [ruby-core:32014]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-24 12:14:05 +04:00
|
|
|
rb_check_frozen(hash);
|
2004-09-22 08:48:52 +04:00
|
|
|
}
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
MJIT_FUNC_EXPORTED struct st_table *
|
|
|
|
rb_hash_tbl_raw(VALUE hash, const char *file, int line)
|
2007-08-30 03:12:21 +04:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
return ar_force_convert_table(hash, file, line);
|
2007-08-30 03:12:21 +04:00
|
|
|
}
|
2013-05-26 16:37:11 +04:00
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
struct st_table *
|
|
|
|
rb_hash_tbl(VALUE hash, const char *file, int line)
|
2013-05-26 20:19:04 +04:00
|
|
|
{
|
2018-10-31 01:11:51 +03:00
|
|
|
OBJ_WB_UNPROTECT(hash);
|
2020-08-17 05:12:23 +03:00
|
|
|
return rb_hash_tbl_raw(hash, file, line);
|
2013-05-26 20:19:04 +04:00
|
|
|
}
|
|
|
|
|
2007-08-30 03:12:21 +04:00
|
|
|
static void
|
|
|
|
rb_hash_modify(VALUE hash)
|
|
|
|
{
|
|
|
|
rb_hash_modify_check(hash);
|
|
|
|
}
|
|
|
|
|
2012-04-13 15:30:39 +04:00
|
|
|
NORETURN(static void no_new_key(void));
|
2010-02-16 19:20:18 +03:00
|
|
|
static void
|
2012-04-13 15:30:39 +04:00
|
|
|
no_new_key(void)
|
2010-02-16 19:20:18 +03:00
|
|
|
{
|
2012-04-13 15:30:39 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "can't add a new key into hash during iteration");
|
2010-02-16 19:20:18 +03:00
|
|
|
}
|
|
|
|
|
2013-05-26 16:37:11 +04:00
|
|
|
struct update_callback_arg {
|
|
|
|
VALUE hash;
|
|
|
|
st_data_t arg;
|
|
|
|
};
|
2012-04-13 10:26:53 +04:00
|
|
|
|
2013-05-26 16:37:11 +04:00
|
|
|
#define NOINSERT_UPDATE_CALLBACK(func) \
|
2013-11-17 18:34:27 +04:00
|
|
|
static int \
|
2013-05-26 16:37:11 +04:00
|
|
|
func##_noinsert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
|
|
|
|
{ \
|
|
|
|
if (!existing) no_new_key(); \
|
2013-06-19 01:29:30 +04:00
|
|
|
return func(key, val, (struct update_arg *)arg, existing); \
|
2013-05-26 16:37:11 +04:00
|
|
|
} \
|
2013-06-19 01:29:30 +04:00
|
|
|
\
|
2013-11-17 18:34:27 +04:00
|
|
|
static int \
|
2013-05-26 16:37:11 +04:00
|
|
|
func##_insert(st_data_t *key, st_data_t *val, st_data_t arg, int existing) \
|
|
|
|
{ \
|
2013-06-19 01:29:30 +04:00
|
|
|
return func(key, val, (struct update_arg *)arg, existing); \
|
|
|
|
}
|
|
|
|
|
|
|
|
struct update_arg {
|
|
|
|
st_data_t arg;
|
|
|
|
VALUE hash;
|
|
|
|
VALUE new_key;
|
|
|
|
VALUE old_key;
|
|
|
|
VALUE new_value;
|
|
|
|
VALUE old_value;
|
|
|
|
};
|
|
|
|
|
2016-03-09 07:45:27 +03:00
|
|
|
typedef int (*tbl_update_func)(st_data_t *, st_data_t *, st_data_t, int);
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
int
|
2019-12-03 06:37:15 +03:00
|
|
|
rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
int result = ar_update(hash, (st_data_t)key, func, arg);
|
2018-10-31 01:11:51 +03:00
|
|
|
if (result == -1) {
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_try_convert_table(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return st_update(RHASH_ST_TABLE(hash), (st_data_t)key, func, arg);
|
|
|
|
}
|
|
|
|
|
2013-06-19 01:29:30 +04:00
|
|
|
static int
|
2016-03-09 07:45:27 +03:00
|
|
|
tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
|
2013-06-19 01:29:30 +04:00
|
|
|
{
|
|
|
|
struct update_arg arg;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
arg.arg = optional_arg;
|
|
|
|
arg.hash = hash;
|
|
|
|
arg.new_key = 0;
|
|
|
|
arg.old_key = Qundef;
|
|
|
|
arg.new_value = 0;
|
|
|
|
arg.old_value = Qundef;
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
result = rb_hash_stlike_update(hash, key, func, (st_data_t)&arg);
|
2013-06-19 01:29:30 +04:00
|
|
|
|
|
|
|
/* write barrier */
|
* include/ruby/ruby.h: rename OBJ_WRITE and OBJ_WRITTEN into
RB_OBJ_WRITE and RB_OBJ_WRITTEN.
* array.c, class.c, compile.c, hash.c, internal.h, iseq.c,
proc.c, process.c, re.c, string.c, variable.c, vm.c,
vm_eval.c, vm_insnhelper.c, vm_insnhelper.h,
vm_method.c: catch up this change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44299 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-12-20 12:07:47 +04:00
|
|
|
if (arg.new_key) RB_OBJ_WRITTEN(hash, arg.old_key, arg.new_key);
|
|
|
|
if (arg.new_value) RB_OBJ_WRITTEN(hash, arg.old_value, arg.new_value);
|
2013-06-19 01:29:30 +04:00
|
|
|
|
|
|
|
return result;
|
2013-05-26 16:37:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func##_insert)
|
|
|
|
|
2013-06-19 01:29:30 +04:00
|
|
|
#define RHASH_UPDATE_ITER(h, iter_lev, key, func, a) do { \
|
|
|
|
tbl_update((h), (key), UPDATE_CALLBACK((iter_lev), func), (st_data_t)(a)); \
|
2013-05-26 16:37:11 +04:00
|
|
|
} while (0)
|
2012-04-13 10:26:53 +04:00
|
|
|
|
|
|
|
#define RHASH_UPDATE(hash, key, func, arg) \
|
|
|
|
RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
|
|
|
|
|
2009-10-26 13:30:15 +03:00
|
|
|
static void
|
2016-03-09 10:17:03 +03:00
|
|
|
set_proc_default(VALUE hash, VALUE proc)
|
2009-10-26 13:30:15 +03:00
|
|
|
{
|
2016-03-09 10:17:03 +03:00
|
|
|
if (rb_proc_lambda_p(proc)) {
|
|
|
|
int n = rb_proc_arity(proc);
|
2009-10-26 13:30:15 +03:00
|
|
|
|
2016-03-09 10:17:03 +03:00
|
|
|
if (n != 2 && (n >= 0 || n < -3)) {
|
|
|
|
if (n < 0) n = -n-1;
|
|
|
|
rb_raise(rb_eTypeError, "default_proc takes two arguments (2 for %d)", n);
|
|
|
|
}
|
2009-10-26 13:30:15 +03:00
|
|
|
}
|
2016-03-09 10:17:03 +03:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
FL_SET_RAW(hash, RHASH_PROC_DEFAULT);
|
2016-03-09 10:17:03 +03:00
|
|
|
RHASH_SET_IFNONE(hash, proc);
|
2009-10-26 13:30:15 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-08-25 18:47:23 +03:00
|
|
|
* Hash.new(default_value = nil) -> new_hash
|
|
|
|
* Hash.new {|hash, key| ... } -> new_hash
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-06-07 21:46:43 +03:00
|
|
|
* Returns a new empty \Hash object.
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-04-13 16:57:10 +03:00
|
|
|
* The initial default value and initial default proc for the new hash
|
|
|
|
* depend on which form above was used. See {Default Values}[#class-Hash-label-Default+Values].
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* If neither an argument nor a block given,
|
2020-04-13 16:57:10 +03:00
|
|
|
* initializes both the default value and the default proc to <tt>nil</tt>:
|
|
|
|
* h = Hash.new
|
|
|
|
* h.default # => nil
|
|
|
|
* h.default_proc # => nil
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-04-13 16:57:10 +03:00
|
|
|
* If argument <tt>default_value</tt> given but no block given,
|
|
|
|
* initializes the default value to the given <tt>default_value</tt>
|
|
|
|
* and the default proc to <tt>nil</tt>:
|
|
|
|
* h = Hash.new(false)
|
|
|
|
* h.default # => false
|
|
|
|
* h.default_proc # => nil
|
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* If a block given but no argument, stores the block as the default proc
|
2020-04-13 16:57:10 +03:00
|
|
|
* and sets the default value to <tt>nil</tt>:
|
2020-08-25 18:47:23 +03:00
|
|
|
* h = Hash.new {|hash, key| "Default value for #{key}" }
|
2020-04-13 16:57:10 +03:00
|
|
|
* h.default # => nil
|
|
|
|
* h.default_proc.class # => Proc
|
|
|
|
* h[:nosuch] # => "Default value for nosuch"
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_initialize(int argc, VALUE *argv, VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-02-29 11:05:32 +03:00
|
|
|
VALUE ifnone;
|
|
|
|
|
|
|
|
rb_hash_modify(hash);
|
2001-12-10 10:18:16 +03:00
|
|
|
if (rb_block_given_p()) {
|
2012-03-15 01:10:34 +04:00
|
|
|
rb_check_arity(argc, 0, 0);
|
2009-10-26 13:30:15 +03:00
|
|
|
ifnone = rb_block_proc();
|
2016-03-09 10:17:03 +03:00
|
|
|
SET_PROC_DEFAULT(hash, ifnone);
|
2001-12-10 10:18:16 +03:00
|
|
|
}
|
|
|
|
else {
|
2014-07-02 12:25:21 +04:00
|
|
|
rb_check_arity(argc, 0, 1);
|
|
|
|
ifnone = argc == 0 ? Qnil : argv[0];
|
2013-05-26 16:37:11 +04:00
|
|
|
RHASH_SET_IFNONE(hash, ifnone);
|
2001-12-10 10:18:16 +03:00
|
|
|
}
|
2000-02-29 11:05:32 +03:00
|
|
|
|
|
|
|
return hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-04-23 11:46:20 +03:00
|
|
|
* Hash[] -> new_empty_hash
|
2020-08-25 18:47:23 +03:00
|
|
|
* Hash[hash] -> new_hash
|
2020-04-23 11:46:20 +03:00
|
|
|
* Hash[ [*2_element_arrays] ] -> new_hash
|
|
|
|
* Hash[*objects] -> new_hash
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-04-23 11:46:20 +03:00
|
|
|
* Returns a new \Hash object populated with the given objects, if any.
|
2020-08-25 18:47:23 +03:00
|
|
|
* See Hash::new.
|
2013-05-13 05:31:27 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* With no argument, returns a new empty \Hash.
|
2013-05-13 05:31:27 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* When the single given argument is a \Hash,
|
|
|
|
* returns a new \Hash populated with the entries from the given \Hash.
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* Hash[h] # => {:foo=>0, :bar=>1, :baz=>2}
|
2020-04-23 11:46:20 +03:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* When the single given argument is an \Array of 2-element Arrays,
|
2020-04-23 11:46:20 +03:00
|
|
|
* returns a new \Hash object wherein each 2-element array forms a key-value entry:
|
|
|
|
* Hash[ [ [:foo, 0], [:bar, 1] ] ] # => {:foo=>0, :bar=>1}
|
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* When the argument count is an even number;
|
|
|
|
* returns a new \Hash object wherein each successive pair of arguments
|
|
|
|
* has become a key-value entry:
|
2020-04-23 11:46:20 +03:00
|
|
|
* Hash[:foo, 0, :bar, 1] # => {:foo=>0, :bar=>1}
|
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Raises an exception if the argument list does not conform to any of the above.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2017-04-24 04:40:51 +03:00
|
|
|
VALUE hash, tmp;
|
|
|
|
|
|
|
|
if (argc == 1) {
|
2018-10-31 05:06:33 +03:00
|
|
|
tmp = rb_hash_s_try_convert(Qnil, argv[0]);
|
2017-04-24 04:40:51 +03:00
|
|
|
if (!NIL_P(tmp)) {
|
|
|
|
hash = hash_alloc(klass);
|
2020-01-10 15:48:20 +03:00
|
|
|
hash_copy(hash, tmp);
|
2017-04-24 04:40:51 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = rb_check_array_type(argv[0]);
|
|
|
|
if (!NIL_P(tmp)) {
|
|
|
|
long i;
|
|
|
|
|
|
|
|
hash = hash_alloc(klass);
|
|
|
|
for (i = 0; i < RARRAY_LEN(tmp); ++i) {
|
|
|
|
VALUE e = RARRAY_AREF(tmp, i);
|
|
|
|
VALUE v = rb_check_array_type(e);
|
|
|
|
VALUE key, val = Qnil;
|
|
|
|
|
|
|
|
if (NIL_P(v)) {
|
|
|
|
rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)",
|
|
|
|
rb_builtin_class_name(e), i);
|
|
|
|
}
|
|
|
|
switch (RARRAY_LEN(v)) {
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eArgError, "invalid number of elements (%ld for 1..2)",
|
|
|
|
RARRAY_LEN(v));
|
|
|
|
case 2:
|
|
|
|
val = RARRAY_AREF(v, 1);
|
|
|
|
case 1:
|
|
|
|
key = RARRAY_AREF(v, 0);
|
|
|
|
rb_hash_aset(hash, key, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2017-04-24 04:40:51 +03:00
|
|
|
if (argc % 2 != 0) {
|
|
|
|
rb_raise(rb_eArgError, "odd number of arguments for Hash");
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = hash_alloc(klass);
|
2017-04-27 07:21:04 +03:00
|
|
|
rb_hash_bulk_insert(argc, argv, hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
hash_verify(hash);
|
2017-04-24 04:40:51 +03:00
|
|
|
return hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
Reduce allocations for keyword argument hashes
Previously, passing a keyword splat to a method always allocated
a hash on the caller side, and accepting arbitrary keywords in
a method allocated a separate hash on the callee side. Passing
explicit keywords to a method that accepted a keyword splat
did not allocate a hash on the caller side, but resulted in two
hashes allocated on the callee side.
This commit makes passing a single keyword splat to a method not
allocate a hash on the caller side. Passing multiple keyword
splats or a mix of explicit keywords and a keyword splat still
generates a hash on the caller side. On the callee side,
if arbitrary keywords are not accepted, it does not allocate a
hash. If arbitrary keywords are accepted, it will allocate a
hash, but this commit uses a callinfo flag to indicate whether
the caller already allocated a hash, and if so, the callee can
use the passed hash without duplicating it. So this commit
should make it so that a maximum of a single hash is allocated
during method calls.
To set the callinfo flag appropriately, method call argument
compilation checks if only a single keyword splat is given.
If only one keyword splat is given, the VM_CALL_KW_SPLAT_MUT
callinfo flag is not set, since in that case the keyword
splat is passed directly and not mutable. If more than one
splat is used, a new hash needs to be generated on the caller
side, and in that case the callinfo flag is set, indicating
the keyword splat is mutable by the callee.
In compile_hash, used for both hash and keyword argument
compilation, if compiling keyword arguments and only a
single keyword splat is used, pass the argument directly.
On the caller side, in vm_args.c, the callinfo flag needs to
be recognized and handled. Because the keyword splat
argument may not be a hash, it needs to be converted to a
hash first if not. Then, unless the callinfo flag is set,
the hash needs to be duplicated. The temporary copy of the
callinfo flag, kw_flag, is updated if a hash was duplicated,
to prevent the need to duplicate it again. If we are
converting to a hash or duplicating a hash, we need to update
the argument array, which can including duplicating the
positional splat array if one was passed. CALLER_SETUP_ARG
and a couple other places needs to be modified to handle
similar issues for other types of calls.
This includes fairly comprehensive tests for different ways
keywords are handled internally, checking that you get equal
results but that keyword splats on the caller side result in
distinct objects for keyword rest parameters.
Included are benchmarks for keyword argument calls.
Brief results when compiled without optimization:
def kw(a: 1) a end
def kws(**kw) kw end
h = {a: 1}
kw(a: 1) # about same
kw(**h) # 2.37x faster
kws(a: 1) # 1.30x faster
kws(**h) # 2.19x faster
kw(a: 1, **h) # 1.03x slower
kw(**h, **h) # about same
kws(a: 1, **h) # 1.16x faster
kws(**h, **h) # 1.14x faster
2020-02-24 23:05:07 +03:00
|
|
|
MJIT_FUNC_EXPORTED VALUE
|
2017-10-26 10:23:23 +03:00
|
|
|
rb_to_hash_type(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2017-05-31 15:30:57 +03:00
|
|
|
return rb_convert_type_with_id(hash, T_HASH, "Hash", idTo_hash);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2017-10-26 10:23:23 +03:00
|
|
|
#define to_hash rb_to_hash_type
|
1998-01-16 15:19:22 +03:00
|
|
|
|
2010-08-03 16:01:13 +04:00
|
|
|
VALUE
|
|
|
|
rb_check_hash_type(VALUE hash)
|
|
|
|
{
|
2017-05-31 15:30:57 +03:00
|
|
|
return rb_check_convert_type_with_id(hash, T_HASH, "Hash", idTo_hash);
|
2010-08-03 16:01:13 +04:00
|
|
|
}
|
|
|
|
|
2007-08-24 21:47:09 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-08-25 18:47:23 +03:00
|
|
|
* Hash.try_convert(obj) -> obj, new_hash, or nil
|
2020-04-13 16:57:10 +03:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* If +obj+ is a \Hash object, returns +obj+.
|
2007-08-24 21:47:09 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Otherwise if +obj+ responds to <tt>:to_hash</tt>,
|
|
|
|
* calls <tt>obj.to_hash</tt> and returns the result.
|
2007-08-24 21:47:09 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Returns +nil+ if +obj+ does not respond to <tt>:to_hash</tt>
|
2020-05-21 01:57:38 +03:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Raises an exception unless <tt>obj.to_hash</tt> returns a \Hash object.
|
2007-08-24 21:47:09 +04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_hash_s_try_convert(VALUE dummy, VALUE hash)
|
|
|
|
{
|
2010-08-03 16:01:13 +04:00
|
|
|
return rb_check_hash_type(hash);
|
2007-08-24 21:47:09 +04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:22:43 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Hash.ruby2_keywords_hash?(hash) -> true or false
|
|
|
|
*
|
|
|
|
* Checks if a given hash is flagged by Module#ruby2_keywords (or
|
|
|
|
* Proc#ruby2_keywords).
|
|
|
|
* This method is not for casual use; debugging, researching, and
|
|
|
|
* some truly necessary cases like serialization of arguments.
|
|
|
|
*
|
|
|
|
* ruby2_keywords def foo(*args)
|
|
|
|
* Hash.ruby2_keywords_hash?(args.last)
|
|
|
|
* end
|
|
|
|
* foo(k: 1) #=> true
|
|
|
|
* foo({k: 1}) #=> false
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_hash_s_ruby2_keywords_hash_p(VALUE dummy, VALUE hash)
|
|
|
|
{
|
|
|
|
Check_Type(hash, T_HASH);
|
|
|
|
return (RHASH(hash)->basic.flags & RHASH_PASS_AS_KEYWORDS) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Hash.ruby2_keywords_hash(hash) -> hash
|
|
|
|
*
|
|
|
|
* Duplicates a given hash and adds a ruby2_keywords flag.
|
|
|
|
* This method is not for casual use; debugging, researching, and
|
|
|
|
* some truly necessary cases like deserialization of arguments.
|
|
|
|
*
|
|
|
|
* h = {k: 1}
|
|
|
|
* h = Hash.ruby2_keywords_hash(h)
|
|
|
|
* def foo(k: 42)
|
|
|
|
* k
|
|
|
|
* end
|
|
|
|
* foo(*[h]) #=> 1 with neither a warning or an error
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_hash_s_ruby2_keywords_hash(VALUE dummy, VALUE hash)
|
|
|
|
{
|
|
|
|
Check_Type(hash, T_HASH);
|
|
|
|
hash = rb_hash_dup(hash);
|
|
|
|
RHASH(hash)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2013-12-01 09:28:54 +04:00
|
|
|
struct rehash_arg {
|
|
|
|
VALUE hash;
|
|
|
|
st_table *tbl;
|
|
|
|
};
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2009-05-09 07:19:52 +04:00
|
|
|
rb_hash_rehash_i(VALUE key, VALUE value, VALUE arg)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(arg)) {
|
|
|
|
ar_insert(arg, (st_data_t)key, (st_data_t)value);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-10-31 01:12:12 +03:00
|
|
|
st_insert(RHASH_ST_TABLE(arg), (st_data_t)key, (st_data_t)value);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-21 01:57:38 +03:00
|
|
|
* hash.rehash -> self
|
2020-05-16 00:11:42 +03:00
|
|
|
*
|
|
|
|
* Rebuilds the hash table by recomputing the hash index for each key;
|
|
|
|
* returns <tt>self</tt>.
|
|
|
|
*
|
2020-07-29 21:45:16 +03:00
|
|
|
* The hash table becomes invalid if the hash value of a key
|
|
|
|
* has changed after the entry was created.
|
2020-05-16 00:11:42 +03:00
|
|
|
* See {Modifying an Active Hash Key}[#class-Hash-label-Modifying+an+Active+Hash+Key].
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2015-12-11 05:38:20 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_rehash(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-12-02 16:59:31 +04:00
|
|
|
VALUE tmp;
|
|
|
|
st_table *tbl;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-04-12 10:10:31 +04:00
|
|
|
if (RHASH_ITER_LEV(hash) > 0) {
|
2004-09-22 08:48:52 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "rehash during iteration");
|
|
|
|
}
|
2007-08-30 03:12:21 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
tmp = hash_alloc(0);
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_alloc_table(tmp);
|
2018-10-31 01:12:12 +03:00
|
|
|
rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_free_and_clear_table(hash);
|
|
|
|
ar_copy(hash, tmp);
|
|
|
|
ar_free_and_clear_table(tmp);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
else if (RHASH_ST_TABLE_P(hash)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
st_table *old_tab = RHASH_ST_TABLE(hash);
|
|
|
|
tmp = hash_alloc(0);
|
|
|
|
tbl = st_init_table_with_size(old_tab->type, old_tab->num_entries);
|
2018-10-31 01:11:51 +03:00
|
|
|
RHASH_ST_TABLE_SET(tmp, tbl);
|
2018-10-31 01:12:12 +03:00
|
|
|
rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
|
|
|
|
st_free_table(old_tab);
|
|
|
|
RHASH_ST_TABLE_SET(hash, tbl);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_ST_CLEAR(tmp);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
hash_verify(hash);
|
1999-01-20 07:59:39 +03:00
|
|
|
return hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2020-01-08 12:13:35 +03:00
|
|
|
static VALUE
|
|
|
|
call_default_proc(VALUE proc, VALUE hash, VALUE key)
|
|
|
|
{
|
|
|
|
VALUE args[2] = {hash, key};
|
|
|
|
return rb_proc_call_with_block(proc, 2, args, Qnil);
|
|
|
|
}
|
|
|
|
|
2015-11-11 12:30:31 +03:00
|
|
|
VALUE
|
|
|
|
rb_hash_default_value(VALUE hash, VALUE key)
|
2012-03-31 09:23:01 +04:00
|
|
|
{
|
2020-01-08 03:19:26 +03:00
|
|
|
if (LIKELY(rb_method_basic_definition_p(CLASS_OF(hash), id_default))) {
|
2012-03-31 09:23:01 +04:00
|
|
|
VALUE ifnone = RHASH_IFNONE(hash);
|
2019-07-31 04:22:47 +03:00
|
|
|
if (!FL_TEST(hash, RHASH_PROC_DEFAULT)) return ifnone;
|
2012-03-31 09:23:01 +04:00
|
|
|
if (key == Qundef) return Qnil;
|
2020-01-08 12:13:35 +03:00
|
|
|
return call_default_proc(ifnone, hash, key);
|
2012-03-31 09:23:01 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_funcall(hash, id_default, 1, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 02:48:33 +03:00
|
|
|
static inline int
|
|
|
|
hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
|
|
|
|
{
|
|
|
|
hash_verify(hash);
|
|
|
|
|
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
return ar_lookup(hash, key, pval);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return st_lookup(RHASH_ST_TABLE(hash), key, pval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MJIT_FUNC_EXPORTED int
|
|
|
|
rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
|
|
|
|
{
|
|
|
|
return hash_stlike_lookup(hash, key, pval);
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-21 01:57:38 +03:00
|
|
|
* hash[key] -> value
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-21 01:57:38 +03:00
|
|
|
* Returns the value associated with the given +key+, if found:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h[:foo] # => 0
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-21 01:57:38 +03:00
|
|
|
* If +key+ is not found, returns a default value
|
|
|
|
* (see {Default Values}[#class-Hash-label-Default+Values]):
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h[:nosuch] # => nil
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_aref(VALUE hash, VALUE key)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 18:47:23 +04:00
|
|
|
st_data_t val;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2019-01-05 02:48:33 +03:00
|
|
|
if (hash_stlike_lookup(hash, key, &val)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
return (VALUE)val;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-01-05 02:48:33 +03:00
|
|
|
return rb_hash_default_value(hash, key);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-10 14:39:39 +04:00
|
|
|
VALUE
|
2008-12-19 11:22:45 +03:00
|
|
|
rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
|
2007-07-10 14:39:39 +04:00
|
|
|
{
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 18:47:23 +04:00
|
|
|
st_data_t val;
|
2007-07-10 14:39:39 +04:00
|
|
|
|
2019-01-17 19:53:10 +03:00
|
|
|
if (hash_stlike_lookup(hash, key, &val)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
return (VALUE)val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return def; /* without Hash#default */
|
2007-07-10 14:39:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-19 11:22:45 +03:00
|
|
|
VALUE
|
|
|
|
rb_hash_lookup(VALUE hash, VALUE key)
|
|
|
|
{
|
|
|
|
return rb_hash_lookup2(hash, key, Qnil);
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-08-26 00:09:31 +03:00
|
|
|
* hash.fetch(key) -> object
|
|
|
|
* hash.fetch(key, default_value) -> object
|
|
|
|
* hash.fetch(key) {|key| ... } -> object
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Returns the value for the given +key+, if found.
|
2020-07-29 23:04:55 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.fetch(:bar) # => 1
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* If +key+ is not found and no block was given,
|
|
|
|
* returns +default_value+:
|
2020-07-29 23:04:55 +03:00
|
|
|
* {}.fetch(:nosuch, :default) # => :default
|
2020-08-25 18:47:23 +03:00
|
|
|
* {}.fetch(:nosuch) # => nil
|
2020-05-21 01:57:38 +03:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* If +key+ is not found and a block was given,
|
|
|
|
* yields +key+ to the block and returns the block's return value:
|
|
|
|
* {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
|
2020-05-21 01:57:38 +03:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Raises KeyError if neither +default_value+ nor a block was given.
|
2020-05-21 01:57:38 +03:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Note that this method does not use the values of either #default or #default_proc.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2008-09-30 12:01:11 +04:00
|
|
|
rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2014-07-02 12:25:21 +04:00
|
|
|
VALUE key;
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 18:47:23 +04:00
|
|
|
st_data_t val;
|
2003-11-06 10:22:39 +03:00
|
|
|
long block_given;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2014-07-02 12:25:21 +04:00
|
|
|
rb_check_arity(argc, 1, 2);
|
|
|
|
key = argv[0];
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2003-11-06 10:22:39 +03:00
|
|
|
block_given = rb_block_given_p();
|
|
|
|
if (block_given && argc == 2) {
|
|
|
|
rb_warn("block supersedes default value argument");
|
|
|
|
}
|
2019-01-05 02:48:33 +03:00
|
|
|
|
|
|
|
if (hash_stlike_lookup(hash, key, &val)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
return (VALUE)val;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2019-01-05 02:48:33 +03:00
|
|
|
else {
|
2019-01-05 03:31:54 +03:00
|
|
|
if (block_given) {
|
|
|
|
return rb_yield(key);
|
|
|
|
}
|
|
|
|
else if (argc == 1) {
|
2019-01-05 02:48:33 +03:00
|
|
|
VALUE desc = rb_protect(rb_inspect, key, 0);
|
|
|
|
if (NIL_P(desc)) {
|
|
|
|
desc = rb_any_to_s(key);
|
|
|
|
}
|
|
|
|
desc = rb_str_ellipsize(desc, 65);
|
|
|
|
rb_key_err_raise(rb_sprintf("key not found: %"PRIsVALUE, desc), hash, key);
|
|
|
|
}
|
2019-01-05 03:31:54 +03:00
|
|
|
else {
|
|
|
|
return argv[1];
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 12:01:11 +04:00
|
|
|
VALUE
|
|
|
|
rb_hash_fetch(VALUE hash, VALUE key)
|
|
|
|
{
|
|
|
|
return rb_hash_fetch_m(1, &key, hash);
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-08-26 00:09:31 +03:00
|
|
|
* hash.default -> object
|
|
|
|
* hash.default(key) -> object
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Returns the default value for the given +key+.
|
|
|
|
* The returned value will be determined either by the default proc or by the default value.
|
|
|
|
* See {Default Values}[#class-Hash-label-Default+Values].
|
|
|
|
*
|
2020-05-21 01:57:38 +03:00
|
|
|
* With no argument, returns the current default value:
|
|
|
|
* h = {}
|
|
|
|
* h.default # => nil
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* If +key+ is given, returns the default value for +key+,
|
2020-05-21 01:57:38 +03:00
|
|
|
* regardless of whether that key exists:
|
2020-08-25 18:47:23 +03:00
|
|
|
* h = Hash.new { |hash, key| hash[key] = "No key #{key}"}
|
|
|
|
* h[:foo] = "Hello"
|
|
|
|
* h.default(:foo) # => "No key foo"
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_default(int argc, VALUE *argv, VALUE hash)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2020-01-08 12:13:35 +03:00
|
|
|
VALUE ifnone;
|
2001-12-10 10:18:16 +03:00
|
|
|
|
2014-07-02 12:25:21 +04:00
|
|
|
rb_check_arity(argc, 0, 1);
|
2009-11-08 12:13:17 +03:00
|
|
|
ifnone = RHASH_IFNONE(hash);
|
2019-01-17 19:53:10 +03:00
|
|
|
if (FL_TEST(hash, RHASH_PROC_DEFAULT)) {
|
2006-07-10 05:08:15 +04:00
|
|
|
if (argc == 0) return Qnil;
|
2020-01-08 12:13:35 +03:00
|
|
|
return call_default_proc(ifnone, hash, argv[0]);
|
2001-12-10 10:18:16 +03:00
|
|
|
}
|
2009-11-08 12:13:17 +03:00
|
|
|
return ifnone;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-08-26 00:09:31 +03:00
|
|
|
* hash.default = value -> object
|
2020-05-21 01:57:38 +03:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Sets the default value to +value+; returns +value+:
|
2020-05-21 01:57:38 +03:00
|
|
|
* h = {}
|
|
|
|
* h.default # => nil
|
|
|
|
* h.default = false # => false
|
|
|
|
* h.default # => false
|
|
|
|
*
|
|
|
|
* See {Default Values}[#class-Hash-label-Default+Values].
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_set_default(VALUE hash, VALUE ifnone)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2012-03-31 09:16:04 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2016-03-09 10:17:03 +03:00
|
|
|
SET_DEFAULT(hash, ifnone);
|
2002-01-11 12:18:54 +03:00
|
|
|
return ifnone;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-22 06:05:19 +03:00
|
|
|
* hash.default_proc -> proc or nil
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Returns the default proc for +self+
|
|
|
|
* (see {Default Values}[#class-Hash-label-Default+Values]):
|
2020-05-22 06:05:19 +03:00
|
|
|
* h = {}
|
|
|
|
* h.default_proc # => nil
|
2020-08-25 18:47:23 +03:00
|
|
|
* h.default_proc = proc {|hash, key| "Default value for #{key}" }
|
2020-05-22 06:05:19 +03:00
|
|
|
* h.default_proc.class # => Proc
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2002-08-13 13:21:18 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_default_proc(VALUE hash)
|
2002-08-13 13:21:18 +04:00
|
|
|
{
|
2019-01-17 19:53:10 +03:00
|
|
|
if (FL_TEST(hash, RHASH_PROC_DEFAULT)) {
|
2009-11-08 12:13:17 +03:00
|
|
|
return RHASH_IFNONE(hash);
|
2002-08-13 13:21:18 +04:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2008-08-13 12:44:17 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-22 06:05:19 +03:00
|
|
|
* hash.default_proc = proc -> proc
|
|
|
|
*
|
2020-08-25 18:47:23 +03:00
|
|
|
* Sets the default proc for +self+ to +proc+:
|
|
|
|
* (see {Default Values}[#class-Hash-label-Default+Values]):
|
2020-05-22 06:05:19 +03:00
|
|
|
* h = {}
|
|
|
|
* h.default_proc # => nil
|
|
|
|
* h.default_proc = proc { |hash, key| "Default value for #{key}" }
|
|
|
|
* h.default_proc.class # => Proc
|
|
|
|
* h.default_proc = nil
|
|
|
|
* h.default_proc # => nil
|
2008-08-13 12:44:17 +04:00
|
|
|
*/
|
|
|
|
|
2014-11-14 10:29:33 +03:00
|
|
|
VALUE
|
2008-08-13 12:44:17 +04:00
|
|
|
rb_hash_set_default_proc(VALUE hash, VALUE proc)
|
|
|
|
{
|
|
|
|
VALUE b;
|
|
|
|
|
2012-03-31 09:16:04 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2012-04-09 08:07:53 +04:00
|
|
|
if (NIL_P(proc)) {
|
2016-03-09 10:17:03 +03:00
|
|
|
SET_DEFAULT(hash, proc);
|
2012-04-09 08:07:53 +04:00
|
|
|
return proc;
|
|
|
|
}
|
2017-05-31 15:30:57 +03:00
|
|
|
b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc);
|
2008-08-13 12:44:17 +04:00
|
|
|
if (NIL_P(b) || !rb_obj_is_proc(b)) {
|
|
|
|
rb_raise(rb_eTypeError,
|
|
|
|
"wrong default_proc type %s (expected Proc)",
|
|
|
|
rb_obj_classname(proc));
|
|
|
|
}
|
|
|
|
proc = b;
|
2016-03-09 10:17:03 +03:00
|
|
|
SET_PROC_DEFAULT(hash, proc);
|
2008-08-13 12:44:17 +04:00
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2009-05-09 07:19:52 +04:00
|
|
|
key_i(VALUE key, VALUE value, VALUE arg)
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
2009-05-09 07:19:52 +04:00
|
|
|
VALUE *args = (VALUE *)arg;
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (rb_equal(value, args[0])) {
|
|
|
|
args[1] = key;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-22 06:05:19 +03:00
|
|
|
* hash.key(value) -> key or nil
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-22 06:05:19 +03:00
|
|
|
* Returns the key for the first-found entry with the given +value+
|
|
|
|
* (see {Entry Order}[#class-Hash-label-Entry+Order]):
|
|
|
|
* h = {foo: 0, bar: 2, baz: 2}
|
|
|
|
* h.key(0) # => :foo
|
|
|
|
* h.key(2) # => :bar
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if so such value is found.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_key(VALUE hash, VALUE value)
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[0] = value;
|
2001-07-31 10:24:45 +04:00
|
|
|
args[1] = Qnil;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2009-05-09 07:19:52 +04:00
|
|
|
rb_hash_foreach(hash, key_i, (VALUE)args);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
return args[1];
|
|
|
|
}
|
|
|
|
|
2004-10-20 05:38:04 +04:00
|
|
|
/* :nodoc: */
|
2004-09-22 08:48:52 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_index(VALUE hash, VALUE value)
|
2004-09-22 08:48:52 +04:00
|
|
|
{
|
2019-12-17 11:17:07 +03:00
|
|
|
rb_warn_deprecated("Hash#index", "Hash#key");
|
2004-09-22 08:48:52 +04:00
|
|
|
return rb_hash_key(hash, value);
|
|
|
|
}
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
int
|
|
|
|
rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval)
|
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
return ar_delete(hash, pkey, pval);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return st_delete(RHASH_ST_TABLE(hash), pkey, pval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 05:53:37 +03:00
|
|
|
/*
|
|
|
|
* delete a specified entry a given key.
|
|
|
|
* if there is the corresponding entry, return a value of the entry.
|
|
|
|
* if there is no corresponding entry, return Qundef.
|
|
|
|
*/
|
2014-10-23 16:42:57 +04:00
|
|
|
VALUE
|
2014-12-24 05:53:37 +03:00
|
|
|
rb_hash_delete_entry(VALUE hash, VALUE key)
|
2007-08-15 08:50:12 +04:00
|
|
|
{
|
|
|
|
st_data_t ktmp = (st_data_t)key, val;
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
if (rb_hash_stlike_delete(hash, &ktmp, &val)) {
|
|
|
|
return (VALUE)val;
|
2014-12-24 05:53:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-10-31 01:11:51 +03:00
|
|
|
return Qundef;
|
2014-12-24 05:53:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* delete a specified entry by a given key.
|
|
|
|
* if there is the corresponding entry, return a value of the entry.
|
|
|
|
* if there is no corresponding entry, return Qnil.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_hash_delete(VALUE hash, VALUE key)
|
|
|
|
{
|
|
|
|
VALUE deleted_value = rb_hash_delete_entry(hash, key);
|
|
|
|
|
|
|
|
if (deleted_value != Qundef) { /* likely pass */
|
|
|
|
return deleted_value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2007-08-15 08:50:12 +04:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-22 23:40:01 +03:00
|
|
|
* hash.delete(key) -> value or nil
|
2020-08-26 00:09:31 +03:00
|
|
|
* hash.delete(key) {|key| ... } -> object
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-26 00:09:31 +03:00
|
|
|
* Deletes the entry for the given +key+ and returns its associated value.
|
2020-05-22 23:40:01 +03:00
|
|
|
*
|
|
|
|
* If no block is given and +key+ is found, deletes the entry and returns the associated value:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.delete(:bar) # => 1
|
|
|
|
* h # => {:foo=>0, :baz=>2}
|
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* If no block given and +key+ is not found, returns +nil+.
|
2020-05-22 23:40:01 +03:00
|
|
|
*
|
|
|
|
* If a block is given and +key+ is found, ignores the block,
|
|
|
|
* deletes the entry, and returns the associated value:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-06-10 14:45:29 +03:00
|
|
|
* h.delete(:baz) { |key| raise 'Will never happen'} # => 2
|
2020-05-22 23:40:01 +03:00
|
|
|
* h # => {:foo=>0, :bar=>1}
|
|
|
|
*
|
|
|
|
* If a block is given and +key+ is not found,
|
|
|
|
* calls the block and returns the block's return value:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2014-10-23 16:42:57 +04:00
|
|
|
static VALUE
|
|
|
|
rb_hash_delete_m(VALUE hash, VALUE key)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE val;
|
|
|
|
|
2012-03-31 09:16:04 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2014-12-24 05:53:37 +03:00
|
|
|
val = rb_hash_delete_entry(hash, key);
|
|
|
|
|
|
|
|
if (val != Qundef) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (rb_block_given_p()) {
|
|
|
|
return rb_yield(key);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct shift_var {
|
|
|
|
VALUE key;
|
|
|
|
VALUE val;
|
|
|
|
};
|
|
|
|
|
2007-08-15 08:50:12 +04:00
|
|
|
static int
|
2009-05-09 07:19:52 +04:00
|
|
|
shift_i_safe(VALUE key, VALUE value, VALUE arg)
|
2007-08-15 08:50:12 +04:00
|
|
|
{
|
2009-05-09 07:19:52 +04:00
|
|
|
struct shift_var *var = (struct shift_var *)arg;
|
|
|
|
|
2007-08-15 08:50:12 +04:00
|
|
|
var->key = key;
|
|
|
|
var->val = value;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-22 23:40:01 +03:00
|
|
|
* hash.shift -> [key, value] or default_value
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-22 23:40:01 +03:00
|
|
|
* Removes the first hash entry
|
|
|
|
* (see {Entry Order}[#class-Hash-label-Entry+Order]);
|
2020-05-29 21:53:11 +03:00
|
|
|
* returns a 2-element \Array containing the removed key and value:
|
2020-05-22 23:40:01 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.shift # => [:foo, 0]
|
|
|
|
* h # => {:bar=>1, :baz=>2}
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-22 23:40:01 +03:00
|
|
|
* Returns the default value if the hash is empty
|
2020-08-26 00:09:31 +03:00
|
|
|
* (see {Default Values}[#class-Hash-label-Default+Values]).
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_shift(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
struct shift_var var;
|
|
|
|
|
2012-03-31 09:16:04 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
2012-03-31 09:16:04 +04:00
|
|
|
var.key = Qundef;
|
2013-04-25 09:03:30 +04:00
|
|
|
if (RHASH_ITER_LEV(hash) == 0) {
|
2018-12-14 04:10:15 +03:00
|
|
|
if (ar_shift(hash, &var.key, &var.val)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
return rb_assoc_new(var.key, var.val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
|
2018-10-31 01:12:12 +03:00
|
|
|
if (var.key != Qundef) {
|
|
|
|
rb_hash_delete_entry(hash, var.key);
|
|
|
|
return rb_assoc_new(var.key, var.val);
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_ST_TABLE_P(hash)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
var.key = Qundef;
|
|
|
|
if (RHASH_ITER_LEV(hash) == 0) {
|
|
|
|
if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
|
|
|
|
return rb_assoc_new(var.key, var.val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-04-25 09:03:30 +04:00
|
|
|
rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
|
|
|
|
if (var.key != Qundef) {
|
2014-12-24 05:53:37 +03:00
|
|
|
rb_hash_delete_entry(hash, var.key);
|
2013-04-25 09:03:30 +04:00
|
|
|
return rb_assoc_new(var.key, var.val);
|
2012-03-31 09:16:04 +04:00
|
|
|
}
|
2007-08-15 08:50:12 +04:00
|
|
|
}
|
2002-08-13 13:21:18 +04:00
|
|
|
}
|
2015-11-11 12:30:31 +03:00
|
|
|
return rb_hash_default_value(hash, Qnil);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
delete_if_i(VALUE key, VALUE value, VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2004-02-26 20:15:00 +03:00
|
|
|
if (RTEST(rb_yield_values(2, key, value))) {
|
2013-07-17 17:59:53 +04:00
|
|
|
return ST_DELETE;
|
2004-02-26 20:15:00 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2013-06-26 17:43:22 +04:00
|
|
|
static VALUE
|
|
|
|
hash_enum_size(VALUE hash, VALUE args, VALUE eobj)
|
|
|
|
{
|
|
|
|
return rb_hash_size(hash);
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-22 23:40:01 +03:00
|
|
|
* hash.delete_if {|key, value| ... } -> self
|
|
|
|
* hash.delete_if -> new_enumerator
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-22 23:40:01 +03:00
|
|
|
* If a block given, calls the block with each key-value pair;
|
|
|
|
* deletes each entry for which the block returns a truthy value;
|
|
|
|
* returns +self+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-26 00:09:31 +03:00
|
|
|
* h.delete_if {|key, value| value > 0 } # => {:foo=>0}
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-22 23:40:01 +03:00
|
|
|
* If no block given, returns a new \Enumerator:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.delete_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:delete_if>
|
2020-08-23 20:10:01 +03:00
|
|
|
* e.each { |key, value| value > 0 } # => {:foo=>0}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2000-07-04 11:04:42 +04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_delete_if(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-06-26 17:43:22 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2012-03-31 09:16:04 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2018-12-14 05:27:23 +03:00
|
|
|
if (!RHASH_TABLE_EMPTY_P(hash)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
rb_hash_foreach(hash, delete_if_i, hash);
|
|
|
|
}
|
2000-08-07 09:05:04 +04:00
|
|
|
return hash;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-22 23:40:01 +03:00
|
|
|
* hash.reject! {|key, value| ... } -> self or nil
|
|
|
|
* hash.reject! -> new_enumerator
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-22 23:40:01 +03:00
|
|
|
* Returns +self+, whose remaining entries are those
|
|
|
|
* for which the block returns +false+ or +nil+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.reject! {|key, value| value < 2 } # => {:baz=>2}
|
2020-05-22 23:40:01 +03:00
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if no entries are removed.
|
2020-05-22 23:40:01 +03:00
|
|
|
*
|
|
|
|
* Returns a new \Enumerator if no block given:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.reject! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject!>
|
2020-08-23 20:10:01 +03:00
|
|
|
* e.each {|key, value| key.start_with?('b') } # => {:foo=>0}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2000-08-07 09:05:04 +04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_reject_bang(VALUE hash)
|
2000-08-07 09:05:04 +04:00
|
|
|
{
|
2009-05-09 07:19:52 +04:00
|
|
|
st_index_t n;
|
2008-04-14 12:31:38 +04:00
|
|
|
|
2013-06-26 17:43:22 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2009-06-08 09:44:17 +04:00
|
|
|
rb_hash_modify(hash);
|
2013-07-12 06:35:16 +04:00
|
|
|
n = RHASH_SIZE(hash);
|
|
|
|
if (!n) return Qnil;
|
2009-06-08 09:44:17 +04:00
|
|
|
rb_hash_foreach(hash, delete_if_i, hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
if (n == RHASH_SIZE(hash)) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
return hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2013-12-07 06:16:04 +04:00
|
|
|
static int
|
2013-12-11 11:01:29 +04:00
|
|
|
reject_i(VALUE key, VALUE value, VALUE result)
|
2013-12-07 06:16:04 +04:00
|
|
|
{
|
|
|
|
if (!RTEST(rb_yield_values(2, key, value))) {
|
2013-12-11 11:01:29 +04:00
|
|
|
rb_hash_aset(result, key, value);
|
2013-12-07 06:16:04 +04:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-22 23:40:01 +03:00
|
|
|
* hash.reject {|key, value| ... } -> new_hash
|
|
|
|
* hash.reject -> new_enumerator
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-22 23:40:01 +03:00
|
|
|
* Returns a new \Hash object whose entries are all those
|
|
|
|
* from +self+ for which the block returns +false+ or +nil+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = h.reject {|key, value| key.start_with?('b') }
|
|
|
|
* h1 # => {:foo=>0}
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-24 04:35:05 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
2020-05-22 23:40:01 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject>
|
|
|
|
* h1 = e.each {|key, value| key.start_with?('b') }
|
|
|
|
* h1 # => {:foo=>0}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2013-12-11 11:01:29 +04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_reject(VALUE hash)
|
2000-02-08 11:54:01 +03:00
|
|
|
{
|
2013-12-11 11:01:29 +04:00
|
|
|
VALUE result;
|
2013-12-07 06:16:04 +04:00
|
|
|
|
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2013-12-17 15:01:37 +04:00
|
|
|
if (RTEST(ruby_verbose)) {
|
|
|
|
VALUE klass;
|
2013-12-23 10:53:17 +04:00
|
|
|
if (HAS_EXTRA_STATES(hash, klass)) {
|
|
|
|
rb_warn("extra states are no longer copied: %+"PRIsVALUE, hash);
|
2013-12-17 15:01:37 +04:00
|
|
|
}
|
2013-12-16 17:40:04 +04:00
|
|
|
}
|
2013-12-11 11:01:29 +04:00
|
|
|
result = rb_hash_new();
|
2013-12-07 06:16:04 +04:00
|
|
|
if (!RHASH_EMPTY_P(hash)) {
|
2013-12-11 11:01:29 +04:00
|
|
|
rb_hash_foreach(hash, reject_i, result);
|
2013-12-07 06:16:04 +04:00
|
|
|
}
|
2013-12-11 11:01:29 +04:00
|
|
|
return result;
|
2000-02-08 11:54:01 +03:00
|
|
|
}
|
|
|
|
|
2017-10-21 09:08:33 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-24 04:35:05 +03:00
|
|
|
* hash.slice(*keys) -> new_hash
|
|
|
|
*
|
|
|
|
* Returns a new \Hash object containing the entries for the given +keys+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.slice(:baz, :foo) # => {:baz=>2, :foo=>0}
|
2020-07-29 23:04:55 +03:00
|
|
|
*
|
2020-08-26 00:09:31 +03:00
|
|
|
* Any given +keys+ that are not found are ignored.
|
2017-10-21 09:08:33 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_slice(int argc, VALUE *argv, VALUE hash)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
VALUE key, value, result;
|
|
|
|
|
|
|
|
if (argc == 0 || RHASH_EMPTY_P(hash)) {
|
|
|
|
return rb_hash_new();
|
|
|
|
}
|
|
|
|
result = rb_hash_new_with_size(argc);
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
key = argv[i];
|
|
|
|
value = rb_hash_lookup2(hash, key, Qundef);
|
|
|
|
if (value != Qundef)
|
|
|
|
rb_hash_aset(result, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-04 16:37:38 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* hsh.except(*keys) -> a_hash
|
|
|
|
*
|
2020-08-26 00:09:31 +03:00
|
|
|
* Returns a new \Hash excluding entries for the given +keys+:
|
2019-05-04 16:37:38 +03:00
|
|
|
* h = { a: 100, b: 200, c: 300 }
|
|
|
|
* h.except(:a) #=> {:b=>200, :c=>300}
|
2020-07-29 23:04:55 +03:00
|
|
|
*
|
2020-08-26 00:09:31 +03:00
|
|
|
* Any given +keys+ that are not found are ignored.
|
2019-05-04 16:37:38 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_except(int argc, VALUE *argv, VALUE hash)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
VALUE key, result;
|
|
|
|
|
|
|
|
result = rb_obj_dup(hash);
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
key = argv[i];
|
|
|
|
rb_hash_delete(result, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2020-05-24 04:35:05 +03:00
|
|
|
* call-seq:
|
|
|
|
* hash.values_at(*keys) -> new_array
|
|
|
|
*
|
|
|
|
* Returns a new \Array containing values for the given +keys+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-26 00:09:31 +03:00
|
|
|
* h.values_at(:baz, :foo) # => [2, 0]
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2020-08-26 00:09:31 +03:00
|
|
|
* The {default values}[#class-Hash-label-Default+Values] are returned
|
|
|
|
* for any keys that are not found:
|
2020-07-29 23:04:55 +03:00
|
|
|
* h.values_at(:hello, :foo) # => [nil, 0]
|
2008-03-09 04:04:46 +03:00
|
|
|
*/
|
2003-12-23 19:21:17 +03:00
|
|
|
|
2001-12-11 06:48:08 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_values_at(int argc, VALUE *argv, VALUE hash)
|
2001-12-11 06:48:08 +03:00
|
|
|
{
|
2004-06-11 17:33:47 +04:00
|
|
|
VALUE result = rb_ary_new2(argc);
|
2001-12-11 06:48:08 +03:00
|
|
|
long i;
|
|
|
|
|
2003-05-04 20:03:24 +04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_ary_push(result, rb_hash_aref(hash, argv[i]));
|
2001-12-11 06:48:08 +03:00
|
|
|
}
|
2003-05-04 20:03:24 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-06-12 11:34:17 +03:00
|
|
|
/*
|
2020-05-24 04:35:05 +03:00
|
|
|
* call-seq:
|
|
|
|
* hash.fetch_values(*keys) -> new_array
|
|
|
|
* hash.fetch_values(*keys) {|key| ... } -> new_array
|
|
|
|
*
|
|
|
|
* Returns a new \Array containing the values associated with the given keys *keys:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.fetch_values(:baz, :foo) # => [2, 0]
|
|
|
|
*
|
2020-08-23 20:10:01 +03:00
|
|
|
* Returns a new empty \Array if no arguments given.
|
2015-06-12 11:34:17 +03:00
|
|
|
*
|
2020-08-26 00:09:31 +03:00
|
|
|
* When a block is given, calls the block with each missing key,
|
2020-05-24 04:35:05 +03:00
|
|
|
* treating the block's return value as the value for that key:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* values = h.fetch_values(:bar, :foo, :bad, :bam) {|key| key.to_s}
|
|
|
|
* values # => [1, 0, "bad", "bam"]
|
2015-06-12 11:34:17 +03:00
|
|
|
*
|
2020-08-26 00:09:31 +03:00
|
|
|
* When no block is given, raises an exception if any given key is not found.
|
2015-06-12 11:34:17 +03:00
|
|
|
*/
|
|
|
|
|
2020-05-20 14:27:47 +03:00
|
|
|
static VALUE
|
2015-06-12 11:34:17 +03:00
|
|
|
rb_hash_fetch_values(int argc, VALUE *argv, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE result = rb_ary_new2(argc);
|
|
|
|
long i;
|
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_ary_push(result, rb_hash_fetch(hash, argv[i]));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-06-21 18:50:14 +04:00
|
|
|
static int
|
|
|
|
select_i(VALUE key, VALUE value, VALUE result)
|
|
|
|
{
|
2013-12-11 11:01:29 +04:00
|
|
|
if (RTEST(rb_yield_values(2, key, value))) {
|
2007-06-21 18:50:14 +04:00
|
|
|
rb_hash_aset(result, key, value);
|
2013-12-11 11:01:29 +04:00
|
|
|
}
|
2007-06-21 18:50:14 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-24 04:35:05 +03:00
|
|
|
* hash.select {|key, value| ... } -> new_hash
|
|
|
|
* hash.select -> new_enumerator
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-24 04:35:05 +03:00
|
|
|
* Hash#filter is an alias for Hash#select.
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-24 04:35:05 +03:00
|
|
|
* Returns a new \Hash object whose entries are those for which the block returns a truthy value:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.select {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
|
2010-05-13 09:49:55 +04:00
|
|
|
*
|
2020-05-24 04:35:05 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.select # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select>
|
2020-08-23 20:10:01 +03:00
|
|
|
* e.each {|key, value| value < 2 } # => {:foo=>0, :bar=>1}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2020-05-21 21:14:40 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_select(VALUE hash)
|
2003-05-04 20:03:24 +04:00
|
|
|
{
|
|
|
|
VALUE result;
|
|
|
|
|
2013-06-26 17:43:22 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2007-06-21 18:50:14 +04:00
|
|
|
result = rb_hash_new();
|
2013-12-11 11:01:29 +04:00
|
|
|
if (!RHASH_EMPTY_P(hash)) {
|
|
|
|
rb_hash_foreach(hash, select_i, result);
|
|
|
|
}
|
2001-12-11 06:48:08 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-03-03 12:44:44 +03:00
|
|
|
static int
|
|
|
|
keep_if_i(VALUE key, VALUE value, VALUE hash)
|
|
|
|
{
|
|
|
|
if (!RTEST(rb_yield_values(2, key, value))) {
|
|
|
|
return ST_DELETE;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-24 04:35:05 +03:00
|
|
|
* hash.select! {|key, value| ... } -> self or nil
|
|
|
|
* hash.select! -> new_enumerator
|
2018-11-04 14:43:09 +03:00
|
|
|
*
|
|
|
|
* Hash#filter! is an alias for Hash#select!.
|
2020-05-24 04:35:05 +03:00
|
|
|
*
|
|
|
|
* Returns +self+, whose entries are those for which the block returns a truthy value:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
|
2020-05-24 04:35:05 +03:00
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if no entries were removed.
|
2020-05-24 04:35:05 +03:00
|
|
|
*
|
|
|
|
* Returns a new \Enumerator if no block given:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!>
|
2020-08-23 20:10:01 +03:00
|
|
|
* e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}
|
2010-03-03 12:44:44 +03:00
|
|
|
*/
|
|
|
|
|
2020-05-21 21:15:22 +03:00
|
|
|
static VALUE
|
2010-03-03 12:44:44 +03:00
|
|
|
rb_hash_select_bang(VALUE hash)
|
|
|
|
{
|
|
|
|
st_index_t n;
|
|
|
|
|
2013-06-26 17:43:22 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2012-03-31 09:16:04 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
n = RHASH_SIZE(hash);
|
|
|
|
if (!n) return Qnil;
|
2010-03-03 12:44:44 +03:00
|
|
|
rb_hash_foreach(hash, keep_if_i, hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
if (n == RHASH_SIZE(hash)) return Qnil;
|
2010-03-03 12:44:44 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-27 17:31:22 +03:00
|
|
|
* hash.keep_if {|key, value| ... } -> self
|
|
|
|
* hash.keep_if -> new_enumerator
|
2010-03-03 12:44:44 +03:00
|
|
|
*
|
2020-05-27 17:31:22 +03:00
|
|
|
* Calls the block for each key-value pair;
|
|
|
|
* retains the entry if the block returns a truthy value;
|
2020-08-26 00:09:31 +03:00
|
|
|
* otherwise deletes the entry; returns +self+.
|
2020-05-27 17:31:22 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.keep_if { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
|
2010-05-13 09:49:55 +04:00
|
|
|
*
|
2020-05-27 17:31:22 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.keep_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:keep_if>
|
2020-08-23 20:10:01 +03:00
|
|
|
* e.each { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}
|
2010-03-03 12:44:44 +03:00
|
|
|
*/
|
|
|
|
|
2020-05-21 21:15:42 +03:00
|
|
|
static VALUE
|
2010-03-03 12:44:44 +03:00
|
|
|
rb_hash_keep_if(VALUE hash)
|
|
|
|
{
|
2013-06-26 17:43:22 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2012-03-31 09:16:04 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2018-12-14 05:27:23 +03:00
|
|
|
if (!RHASH_TABLE_EMPTY_P(hash)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
rb_hash_foreach(hash, keep_if_i, hash);
|
|
|
|
}
|
2010-03-03 12:44:44 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
clear_i(VALUE key, VALUE value, VALUE dummy)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
return ST_DELETE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-27 17:31:22 +03:00
|
|
|
* hash.clear -> self
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-27 16:31:32 +03:00
|
|
|
* Removes all hash entries; returns +self+.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2012-11-05 19:27:01 +04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_clear(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2007-08-30 03:12:21 +04:00
|
|
|
rb_hash_modify_check(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
if (RHASH_ITER_LEV(hash) > 0) {
|
|
|
|
rb_hash_foreach(hash, clear_i, 0);
|
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
else if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
ar_clear(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
st_clear(RHASH_ST_TABLE(hash));
|
2004-09-22 08:48:52 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
return hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2012-04-13 10:26:53 +04:00
|
|
|
static int
|
2013-06-19 01:29:30 +04:00
|
|
|
hash_aset(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
|
2012-04-13 10:26:53 +04:00
|
|
|
{
|
2013-05-29 05:38:52 +04:00
|
|
|
if (existing) {
|
2013-06-19 01:29:30 +04:00
|
|
|
arg->new_value = arg->arg;
|
|
|
|
arg->old_value = *val;
|
2013-05-29 05:38:52 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-06-19 01:29:30 +04:00
|
|
|
arg->new_key = *key;
|
|
|
|
arg->new_value = arg->arg;
|
2013-05-29 05:38:52 +04:00
|
|
|
}
|
2013-06-19 01:29:30 +04:00
|
|
|
*val = arg->arg;
|
2012-04-13 10:26:53 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-12-30 03:00:23 +03:00
|
|
|
VALUE
|
|
|
|
rb_hash_key_str(VALUE key)
|
|
|
|
{
|
2019-09-25 06:59:12 +03:00
|
|
|
if (!RB_FL_ANY_RAW(key, FL_EXIVAR) && RBASIC_CLASS(key) == rb_cString) {
|
2018-10-26 09:41:34 +03:00
|
|
|
return rb_fstring(key);
|
2018-10-26 08:32:47 +03:00
|
|
|
}
|
2017-12-30 03:00:23 +03:00
|
|
|
else {
|
|
|
|
return rb_str_new_frozen(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 10:26:53 +04:00
|
|
|
static int
|
2013-06-19 01:29:30 +04:00
|
|
|
hash_aset_str(st_data_t *key, st_data_t *val, struct update_arg *arg, int existing)
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 18:47:23 +04:00
|
|
|
{
|
2017-07-10 02:04:43 +03:00
|
|
|
if (!existing && !RB_OBJ_FROZEN(*key)) {
|
2017-12-30 03:00:23 +03:00
|
|
|
*key = rb_hash_key_str(*key);
|
2013-05-29 05:38:52 +04:00
|
|
|
}
|
2013-06-19 01:29:30 +04:00
|
|
|
return hash_aset(key, val, arg, existing);
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 18:47:23 +04:00
|
|
|
}
|
|
|
|
|
2015-12-15 17:20:27 +03:00
|
|
|
NOINSERT_UPDATE_CALLBACK(hash_aset)
|
|
|
|
NOINSERT_UPDATE_CALLBACK(hash_aset_str)
|
2012-04-13 10:26:53 +04:00
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-27 17:31:22 +03:00
|
|
|
* hash[key] = value -> value
|
|
|
|
* hash.store(key, value)
|
2013-08-14 21:34:40 +04:00
|
|
|
*
|
2020-08-27 16:31:32 +03:00
|
|
|
* Hash#store is an alias for Hash#[]=.
|
|
|
|
|
|
|
|
* Associates the given +value+ with the given +key+; returns +value+.
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-27 17:31:22 +03:00
|
|
|
* If the given +key+ exists, replaces its value with the given +value+;
|
|
|
|
* the ordering is not affected
|
|
|
|
* (see {Entry Order}[#class-Hash-label-Entry+Order]):
|
|
|
|
* h = {foo: 0, bar: 1}
|
|
|
|
* h[:foo] = 2 # => 2
|
|
|
|
* h.store(:bar, 3) # => 3
|
|
|
|
* h # => {:foo=>2, :bar=>3}
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-27 17:31:22 +03:00
|
|
|
* If +key+ does not exist, adds the +key+ and +value+;
|
|
|
|
* the new entry is last in the order
|
|
|
|
* (see {Entry Order}[#class-Hash-label-Entry+Order]):
|
|
|
|
* h = {foo: 0, bar: 1}
|
|
|
|
* h[:baz] = 2 # => 2
|
|
|
|
* h.store(:bat, 3) # => 3
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_aset(VALUE hash, VALUE key, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2012-04-13 10:26:53 +04:00
|
|
|
int iter_lev = RHASH_ITER_LEV(hash);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_hash_modify(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 05:27:23 +03:00
|
|
|
if (RHASH_TABLE_NULL_P(hash)) {
|
2012-04-13 15:30:39 +04:00
|
|
|
if (iter_lev > 0) no_new_key();
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_alloc_table(hash);
|
2012-04-13 10:26:53 +04:00
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
if (RHASH_TYPE(hash) == &identhash || rb_obj_class(key) != rb_cString) {
|
2012-04-13 10:26:53 +04:00
|
|
|
RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
else {
|
2012-04-13 10:26:53 +04:00
|
|
|
RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-27 17:31:22 +03:00
|
|
|
* hash.replace(other_hash) -> self
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-27 17:31:22 +03:00
|
|
|
* Replaces the entire contents of +self+ with the contents of +other_hash+;
|
|
|
|
* returns +self+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.replace({bat: 3, bam: 4}) # => {:bat=>3, :bam=>4}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_replace(VALUE hash, VALUE hash2)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2010-02-10 10:31:06 +03:00
|
|
|
rb_hash_modify_check(hash);
|
2002-09-28 08:21:31 +04:00
|
|
|
if (hash == hash2) return hash;
|
2019-10-21 11:29:21 +03:00
|
|
|
if (RHASH_ITER_LEV(hash) > 0) {
|
|
|
|
rb_raise(rb_eRuntimeError, "can't replace hash during iteration");
|
|
|
|
}
|
2013-07-17 17:17:01 +04:00
|
|
|
hash2 = to_hash(hash2);
|
|
|
|
|
2016-03-09 10:17:04 +03:00
|
|
|
COPY_DEFAULT(hash, hash2);
|
2013-07-17 17:17:01 +04:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
2019-10-21 11:45:27 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash2)) {
|
|
|
|
ar_clear(hash);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ar_free_and_clear_table(hash);
|
|
|
|
RHASH_ST_TABLE_SET(hash, st_init_table_with_size(RHASH_TYPE(hash2), RHASH_SIZE(hash2)));
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-10-21 11:45:27 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash2)) {
|
|
|
|
st_free_table(RHASH_ST_TABLE(hash));
|
|
|
|
RHASH_ST_CLEAR(hash);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
st_clear(RHASH_ST_TABLE(hash));
|
|
|
|
RHASH_TBL_RAW(hash)->type = RHASH_ST_TABLE(hash2)->type;
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2019-10-21 11:29:21 +03:00
|
|
|
rb_hash_foreach(hash2, rb_hash_rehash_i, (VALUE)hash);
|
|
|
|
|
|
|
|
rb_gc_writebarrier_remember(hash);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-27 17:31:22 +03:00
|
|
|
* hash.length -> integer
|
|
|
|
* hash.size -> integer
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-27 17:31:22 +03:00
|
|
|
* Returns the count of entries in +self+:
|
2020-08-27 16:31:32 +03:00
|
|
|
* {foo: 0, bar: 1, baz: 2}.length # => 3
|
2018-10-12 22:56:53 +03:00
|
|
|
*
|
|
|
|
* Hash#length is an alias for Hash#size.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2014-06-23 11:26:03 +04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_size(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-07-11 19:25:31 +04:00
|
|
|
return INT2FIX(RHASH_SIZE(hash));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
size_t
|
|
|
|
rb_hash_size_num(VALUE hash)
|
|
|
|
{
|
|
|
|
return (long)RHASH_SIZE(hash);
|
|
|
|
}
|
2003-12-23 19:21:17 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-27 17:31:22 +03:00
|
|
|
* hash.empty? -> true or false
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-27 17:31:22 +03:00
|
|
|
* Returns +true+ if there are no hash entries, +false+ otherwise:
|
|
|
|
* {}.empty? # => true
|
|
|
|
* {foo: 0, bar: 1, baz: 2}.empty? # => false
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_empty_p(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2007-08-30 03:12:21 +04:00
|
|
|
return RHASH_EMPTY_P(hash) ? Qtrue : Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
each_value_i(VALUE key, VALUE value, VALUE _)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
rb_yield(value);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-28 14:21:48 +03:00
|
|
|
* hash.each_value {|value| ... } -> self
|
|
|
|
* hash.each_value -> new_enumerator
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Calls the given block with each value; returns +self+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.each_value {|value| puts value } # => {:foo=>0, :bar=>1, :baz=>2}
|
2020-05-28 14:21:48 +03:00
|
|
|
* Output:
|
|
|
|
* 0
|
|
|
|
* 1
|
|
|
|
* 2
|
2010-05-13 09:49:55 +04:00
|
|
|
*
|
2020-08-27 16:31:32 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
2020-05-28 14:21:48 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.each_value # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_value>
|
|
|
|
* h1 = e.each {|value| puts value }
|
|
|
|
* h1 # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
* Output:
|
|
|
|
* 0
|
|
|
|
* 1
|
|
|
|
* 2
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_each_value(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-06-26 17:43:22 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2004-09-29 09:15:33 +04:00
|
|
|
rb_hash_foreach(hash, each_value_i, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
return hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
each_key_i(VALUE key, VALUE value, VALUE _)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
rb_yield(key);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-28 14:21:48 +03:00
|
|
|
* hash.each_key {|key| ... } -> self
|
|
|
|
* hash.each_key -> new_enumerator
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Calls the given block with each key; returns +self+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.each_key {|key| puts key } # => {:foo=>0, :bar=>1, :baz=>2}
|
2020-05-28 14:21:48 +03:00
|
|
|
* Output:
|
|
|
|
* foo
|
|
|
|
* bar
|
|
|
|
* baz
|
2010-05-13 09:49:55 +04:00
|
|
|
*
|
2020-08-27 16:31:32 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
2020-05-28 14:21:48 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.each_key # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_key>
|
|
|
|
* h1 = e.each {|key| puts key }
|
|
|
|
* h1 # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
* Output:
|
|
|
|
* foo
|
|
|
|
* bar
|
|
|
|
* baz
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_each_key(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-06-26 17:43:22 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2004-09-29 09:15:33 +04:00
|
|
|
rb_hash_foreach(hash, each_key_i, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
return hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
each_pair_i(VALUE key, VALUE value, VALUE _)
|
2003-10-24 18:31:14 +04:00
|
|
|
{
|
2017-10-02 10:51:27 +03:00
|
|
|
rb_yield(rb_assoc_new(key, value));
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
each_pair_i_fast(VALUE key, VALUE value, VALUE _)
|
2017-10-02 10:51:27 +03:00
|
|
|
{
|
|
|
|
VALUE argv[2];
|
|
|
|
argv[0] = key;
|
|
|
|
argv[1] = value;
|
|
|
|
rb_yield_values2(2, argv);
|
2013-07-15 08:38:50 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-28 14:21:48 +03:00
|
|
|
* hash.each {|key, value| ... } -> self
|
|
|
|
* hash.each_pair {|key, value| ... } -> self
|
|
|
|
* hash.each -> new_enumerator
|
|
|
|
* hash.each_pair -> new_enumerator
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-27 16:31:32 +03:00
|
|
|
* Hash#each is an alias for Hash#each_pair.
|
|
|
|
|
|
|
|
* Calls the given block with each key-value pair; returns +self+:
|
2020-05-28 14:21:48 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.each_pair {|key, value| puts "#{key}: #{value}"} # => {:foo=>0, :bar=>1, :baz=>2}
|
2020-05-28 14:21:48 +03:00
|
|
|
* Output:
|
|
|
|
* foo: 0
|
|
|
|
* bar: 1
|
|
|
|
* baz: 2
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-27 16:31:32 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
2020-05-28 14:21:48 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.each_pair # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_pair>
|
|
|
|
* h1 = e.each {|key, value| puts "#{key}: #{value}"}
|
|
|
|
* h1 # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
* Output:
|
|
|
|
* foo: 0
|
|
|
|
* bar: 1
|
|
|
|
* baz: 2
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2003-10-24 18:31:14 +04:00
|
|
|
static VALUE
|
2007-11-03 17:07:48 +03:00
|
|
|
rb_hash_each_pair(VALUE hash)
|
2003-10-24 18:31:14 +04:00
|
|
|
{
|
2013-06-26 17:43:22 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2020-03-16 17:03:22 +03:00
|
|
|
if (rb_block_pair_yield_optimizable())
|
2017-10-02 10:51:27 +03:00
|
|
|
rb_hash_foreach(hash, each_pair_i_fast, 0);
|
|
|
|
else
|
|
|
|
rb_hash_foreach(hash, each_pair_i, 0);
|
2003-10-24 18:31:14 +04:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2019-12-26 09:50:34 +03:00
|
|
|
struct transform_keys_args{
|
|
|
|
VALUE trans;
|
|
|
|
VALUE result;
|
|
|
|
int block_given;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
transform_keys_hash_i(VALUE key, VALUE value, VALUE transarg)
|
|
|
|
{
|
|
|
|
struct transform_keys_args *p = (void *)transarg;
|
|
|
|
VALUE trans = p->trans, result = p->result;
|
|
|
|
VALUE new_key = rb_hash_lookup2(trans, key, Qundef);
|
|
|
|
if (new_key == Qundef) {
|
|
|
|
if (p->block_given)
|
|
|
|
new_key = rb_yield(key);
|
|
|
|
else
|
|
|
|
new_key = key;
|
|
|
|
}
|
|
|
|
rb_hash_aset(result, new_key, value);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-07-14 09:44:00 +03:00
|
|
|
static int
|
|
|
|
transform_keys_i(VALUE key, VALUE value, VALUE result)
|
|
|
|
{
|
|
|
|
VALUE new_key = rb_yield(key);
|
|
|
|
rb_hash_aset(result, new_key, value);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-28 14:21:48 +03:00
|
|
|
* hash.transform_keys {|key| ... } -> new_hash
|
|
|
|
* hash.transform_keys -> new_enumerator
|
|
|
|
*
|
|
|
|
* Returns a new \Hash object; each entry has:
|
|
|
|
* * A key provided by the block.
|
|
|
|
* * The value from +self+.
|
|
|
|
*
|
|
|
|
* Transform keys:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = h.transform_keys {|key| key.to_s }
|
|
|
|
* h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
|
|
|
|
*
|
|
|
|
* Overwrites values for duplicate keys:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = h.transform_keys {|key| :bat }
|
|
|
|
* h1 # => {:bat=>2}
|
2017-07-14 09:44:00 +03:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.transform_keys # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_keys>
|
|
|
|
* h1 = e.each { |key| key.to_s }
|
|
|
|
* h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
|
2017-07-14 09:44:00 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2019-12-26 09:50:34 +03:00
|
|
|
rb_hash_transform_keys(int argc, VALUE *argv, VALUE hash)
|
2017-07-14 09:44:00 +03:00
|
|
|
{
|
|
|
|
VALUE result;
|
2019-12-26 09:50:34 +03:00
|
|
|
struct transform_keys_args transarg = {0};
|
2017-07-14 09:44:00 +03:00
|
|
|
|
2019-12-26 09:50:34 +03:00
|
|
|
argc = rb_check_arity(argc, 0, 1);
|
|
|
|
if (argc > 0) {
|
|
|
|
transarg.trans = to_hash(argv[0]);
|
|
|
|
transarg.block_given = rb_block_given_p();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
|
|
|
}
|
2017-07-14 09:44:00 +03:00
|
|
|
result = rb_hash_new();
|
|
|
|
if (!RHASH_EMPTY_P(hash)) {
|
2019-12-26 09:50:34 +03:00
|
|
|
if (transarg.trans) {
|
|
|
|
transarg.result = result;
|
|
|
|
rb_hash_foreach(hash, transform_keys_hash_i, (VALUE)&transarg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_hash_foreach(hash, transform_keys_i, result);
|
|
|
|
}
|
2017-07-14 09:44:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-26 04:33:45 +03:00
|
|
|
static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash);
|
|
|
|
|
2017-07-14 09:44:00 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-28 14:21:48 +03:00
|
|
|
* hash.transform_keys! {|key| ... } -> self
|
|
|
|
* hash.transform_keys! -> new_enumerator
|
|
|
|
*
|
|
|
|
* Returns +self+ with new keys provided by the block:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.transform_keys! {|key| key.to_s } # => {"foo"=>0, "bar"=>1, "baz"=>2}
|
2020-05-28 14:21:48 +03:00
|
|
|
*
|
|
|
|
* Overwrites values for duplicate keys:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = h.transform_keys! {|key| :bat }
|
|
|
|
* h1 # => {:bat=>2}
|
2017-07-14 09:44:00 +03:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.transform_keys! # => #<Enumerator: {"foo"=>0, "bar"=>1, "baz"=>2}:transform_keys!>
|
|
|
|
* h1 = e.each { |key| key.to_s }
|
|
|
|
* h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
|
2017-07-14 09:44:00 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2019-12-26 09:50:34 +03:00
|
|
|
rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash)
|
2017-07-14 09:44:00 +03:00
|
|
|
{
|
2019-12-26 09:50:34 +03:00
|
|
|
VALUE trans = 0;
|
|
|
|
int block_given = 0;
|
|
|
|
|
|
|
|
argc = rb_check_arity(argc, 0, 1);
|
|
|
|
if (argc > 0) {
|
|
|
|
trans = to_hash(argv[0]);
|
|
|
|
block_given = rb_block_given_p();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
|
|
|
}
|
2017-07-14 09:44:00 +03:00
|
|
|
rb_hash_modify_check(hash);
|
2018-12-14 05:27:23 +03:00
|
|
|
if (!RHASH_TABLE_EMPTY_P(hash)) {
|
2018-01-26 04:33:45 +03:00
|
|
|
long i;
|
|
|
|
VALUE pairs = rb_hash_flatten(0, NULL, hash);
|
|
|
|
rb_hash_clear(hash);
|
|
|
|
for (i = 0; i < RARRAY_LEN(pairs); i += 2) {
|
2019-12-26 09:50:34 +03:00
|
|
|
VALUE key = RARRAY_AREF(pairs, i), new_key, val;
|
|
|
|
|
|
|
|
if (!trans) {
|
|
|
|
new_key = rb_yield(key);
|
|
|
|
}
|
|
|
|
else if ((new_key = rb_hash_lookup2(trans, key, Qundef)) != Qundef) {
|
|
|
|
/* use the transformed key */
|
|
|
|
}
|
|
|
|
else if (block_given) {
|
|
|
|
new_key = rb_yield(key);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_key = key;
|
|
|
|
}
|
|
|
|
val = RARRAY_AREF(pairs, i+1);
|
2018-01-26 04:33:45 +03:00
|
|
|
rb_hash_aset(hash, new_key, val);
|
|
|
|
}
|
2017-07-14 09:44:00 +03:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2016-08-09 11:54:15 +03:00
|
|
|
static int
|
2019-09-11 19:02:22 +03:00
|
|
|
transform_values_foreach_func(st_data_t key, st_data_t value, st_data_t argp, int error)
|
2016-08-09 11:54:15 +03:00
|
|
|
{
|
2019-09-11 19:02:22 +03:00
|
|
|
return ST_REPLACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t argp, int existing)
|
|
|
|
{
|
|
|
|
VALUE new_value = rb_yield((VALUE)*value);
|
2020-03-14 03:59:55 +03:00
|
|
|
VALUE hash = (VALUE)argp;
|
|
|
|
RB_OBJ_WRITE(hash, value, new_value);
|
2016-08-09 11:54:15 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-28 14:21:48 +03:00
|
|
|
* hash.transform_values {|value| ... } -> new_hash
|
|
|
|
* hash.transform_values -> new_enumerator
|
2016-08-09 11:54:15 +03:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Returns a new \Hash object; each entry has:
|
|
|
|
* * A key from +self+.
|
|
|
|
* * A value provided by the block.
|
2016-08-09 11:54:15 +03:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Transform values:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = h.transform_values {|value| value * 100}
|
|
|
|
* h1 # => {:foo=>0, :bar=>100, :baz=>200}
|
2016-08-09 11:54:15 +03:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.transform_values # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_values>
|
|
|
|
* h1 = e.each { |value| value * 100}
|
|
|
|
* h1 # => {:foo=>0, :bar=>100, :baz=>200}
|
2016-08-09 11:54:15 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2016-09-08 05:33:18 +03:00
|
|
|
rb_hash_transform_values(VALUE hash)
|
2016-08-09 11:54:15 +03:00
|
|
|
{
|
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
2020-01-10 15:26:43 +03:00
|
|
|
result = hash_copy(hash_alloc(rb_cHash), hash);
|
2019-09-11 19:02:22 +03:00
|
|
|
|
2016-08-09 11:54:15 +03:00
|
|
|
if (!RHASH_EMPTY_P(hash)) {
|
2020-03-14 03:59:55 +03:00
|
|
|
rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, result);
|
2016-08-09 11:54:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-28 14:21:48 +03:00
|
|
|
* hash.transform_values! {|value| ... } -> self
|
|
|
|
* hash.transform_values! -> new_enumerator
|
2016-08-09 11:54:15 +03:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Returns +self+, whose keys are unchanged, and whose values are determined by the given block.
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.transform_values! {|value| value * 100} # => {:foo=>0, :bar=>100, :baz=>200}
|
2016-08-09 11:54:15 +03:00
|
|
|
*
|
2020-05-28 14:21:48 +03:00
|
|
|
* Returns a new \Enumerator if no block given:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* e = h.transform_values! # => #<Enumerator: {:foo=>0, :bar=>100, :baz=>200}:transform_values!>
|
|
|
|
* h1 = e.each {|value| value * 100}
|
|
|
|
* h1 # => {:foo=>0, :bar=>100, :baz=>200}
|
2016-08-09 11:54:15 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2016-09-08 05:33:18 +03:00
|
|
|
rb_hash_transform_values_bang(VALUE hash)
|
2016-08-09 11:54:15 +03:00
|
|
|
{
|
|
|
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
|
|
|
rb_hash_modify_check(hash);
|
2019-09-11 19:02:22 +03:00
|
|
|
|
|
|
|
if (!RHASH_TABLE_EMPTY_P(hash)) {
|
2020-03-14 03:59:55 +03:00
|
|
|
rb_hash_stlike_foreach_with_replace(hash, transform_values_foreach_func, transform_values_foreach_replace, hash);
|
2019-09-11 19:02:22 +03:00
|
|
|
}
|
|
|
|
|
2016-08-09 11:54:15 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
to_a_i(VALUE key, VALUE value, VALUE ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, rb_assoc_new(key, value));
|
1998-01-16 15:13:05 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-29 21:53:11 +03:00
|
|
|
* hash.to_a -> new_array
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Returns a new \Array of 2-element \Array objects;
|
|
|
|
* each nested \Array contains the key and value for a hash entry:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_to_a(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE ary;
|
|
|
|
|
2013-12-01 09:39:38 +04:00
|
|
|
ary = rb_ary_new_capa(RHASH_SIZE(hash));
|
2004-09-29 09:15:33 +04:00
|
|
|
rb_hash_foreach(hash, to_a_i, ary);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2009-05-29 12:08:25 +04:00
|
|
|
inspect_i(VALUE key, VALUE value, VALUE str)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-05-29 12:08:25 +04:00
|
|
|
VALUE str2;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2011-02-15 10:08:20 +03:00
|
|
|
str2 = rb_inspect(key);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(str) > 1) {
|
2012-04-13 12:39:13 +04:00
|
|
|
rb_str_buf_cat_ascii(str, ", ");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2011-02-15 10:08:20 +03:00
|
|
|
else {
|
|
|
|
rb_enc_copy(str, str2);
|
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_append(str, str2);
|
2012-04-13 12:39:13 +04:00
|
|
|
rb_str_buf_cat_ascii(str, "=>");
|
2009-05-29 12:08:25 +04:00
|
|
|
str2 = rb_inspect(value);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_append(str, str2);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2009-05-29 12:08:25 +04:00
|
|
|
inspect_hash(VALUE hash, VALUE dummy, int recur)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-05-29 12:08:25 +04:00
|
|
|
VALUE str;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
if (recur) return rb_usascii_str_new2("{...}");
|
2001-05-30 13:12:34 +04:00
|
|
|
str = rb_str_buf_new2("{");
|
2009-05-29 12:08:25 +04:00
|
|
|
rb_hash_foreach(hash, inspect_i, str);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "}");
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2020-05-29 21:53:11 +03:00
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash.inspect -> new_string
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Returns a new \String containing the hash entries:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-06-02 18:53:25 +03:00
|
|
|
* h.inspect # => "{:foo=>0, :bar=>1, :baz=>2}"
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Hash#to_s is an alias for Hash#inspect.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_inspect(VALUE hash)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2007-08-30 03:12:21 +04:00
|
|
|
if (RHASH_EMPTY_P(hash))
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
return rb_usascii_str_new2("{}");
|
2009-05-29 12:08:25 +04:00
|
|
|
return rb_exec_recursive(inspect_hash, hash, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2020-05-29 21:53:11 +03:00
|
|
|
* call-seq:
|
|
|
|
* hash.to_hash -> self
|
2003-12-23 19:21:17 +03:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Returns +self+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.to_hash # => {:foo=>0, :bar=>1, :baz=>2}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_to_hash(VALUE hash)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
return hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2018-09-20 18:06:56 +03:00
|
|
|
VALUE
|
|
|
|
rb_hash_set_pair(VALUE hash, VALUE arg)
|
|
|
|
{
|
|
|
|
VALUE pair;
|
|
|
|
|
|
|
|
pair = rb_check_array_type(arg);
|
|
|
|
if (NIL_P(pair)) {
|
|
|
|
rb_raise(rb_eTypeError, "wrong element type %s (expected array)",
|
|
|
|
rb_builtin_class_name(arg));
|
|
|
|
}
|
|
|
|
if (RARRAY_LEN(pair) != 2) {
|
|
|
|
rb_raise(rb_eArgError, "element has wrong array length (expected 2, was %ld)",
|
|
|
|
RARRAY_LEN(pair));
|
|
|
|
}
|
|
|
|
rb_hash_aset(hash, RARRAY_AREF(pair, 0), RARRAY_AREF(pair, 1));
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
to_h_i(VALUE key, VALUE value, VALUE hash)
|
|
|
|
{
|
|
|
|
rb_hash_set_pair(hash, rb_yield_values(2, key, value));
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_to_h_block(VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE h = rb_hash_new_with_size(RHASH_SIZE(hash));
|
|
|
|
rb_hash_foreach(hash, to_h_i, h);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2012-04-24 07:46:55 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-29 21:53:11 +03:00
|
|
|
* hash.to_h -> self or new_hash
|
|
|
|
* hash.to_h {|key, value| ... } -> new_hash
|
|
|
|
*
|
|
|
|
* For an instance of \Hash, returns +self+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.to_h # => {:foo=>0, :bar=>1, :baz=>2}
|
2020-05-29 21:53:11 +03:00
|
|
|
*
|
|
|
|
* For a subclass of \Hash, returns a new \Hash
|
|
|
|
* containing the content of +self+:
|
|
|
|
* class MyHash < Hash; end
|
|
|
|
* h = MyHash[foo: 0, bar: 1, baz: 2]
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
* h.class # => MyHash
|
|
|
|
* h1 = h.to_h
|
|
|
|
* h1 # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
* h1.class # => Hash
|
|
|
|
*
|
|
|
|
* When a block is given, returns a new \Hash object
|
|
|
|
* whose content is based on the block;
|
2020-06-07 21:46:43 +03:00
|
|
|
* the block should return a 2-element \Array object
|
|
|
|
* specifying the key-value pair to be included in the returned \Array:
|
2020-05-29 21:53:11 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = h.to_h {|key, value| [value, key] }
|
|
|
|
* h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
|
|
|
|
*
|
|
|
|
* ---
|
2012-04-24 07:46:55 +04:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Raises an exception if the block does not return an \Array:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* # Raises TypeError (wrong element type Symbol (expected array)):
|
|
|
|
* h1 = h.to_h {|key, value| :array }
|
|
|
|
*
|
|
|
|
* Raises an exception if the block returns an \Array of size different from 2:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* # Raises ArgumentError (element has wrong array length (expected 2, was 3)):
|
|
|
|
* h1 = h.to_h {|key, value| [0, 1, 2] }
|
2018-09-20 18:06:56 +03:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Raises an exception if the block attempts to add a new key:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* # Raises RuntimeError (can't add a new key into hash during iteration):
|
|
|
|
* h.to_h {|key, value| h[:new_key] = 3 }
|
2012-04-24 07:46:55 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_to_h(VALUE hash)
|
|
|
|
{
|
2018-09-20 18:06:56 +03:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
return rb_hash_to_h_block(hash);
|
|
|
|
}
|
2012-04-24 07:46:55 +04:00
|
|
|
if (rb_obj_class(hash) != rb_cHash) {
|
2016-03-09 10:17:02 +03:00
|
|
|
const VALUE flags = RBASIC(hash)->flags;
|
2019-07-31 04:22:47 +03:00
|
|
|
hash = hash_dup(hash, rb_cHash, flags & RHASH_PROC_DEFAULT);
|
2012-04-24 07:46:55 +04:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2013-10-10 20:28:56 +04:00
|
|
|
static int
|
|
|
|
keys_i(VALUE key, VALUE value, VALUE ary)
|
|
|
|
{
|
|
|
|
rb_ary_push(ary, key);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-29 21:53:11 +03:00
|
|
|
* hash.keys -> new_array
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Returns a new \Array containing all keys in +self+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.keys # => [:foo, :bar, :baz]
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 14:22:28 +03:00
|
|
|
MJIT_FUNC_EXPORTED VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_keys(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-11-27 20:36:44 +04:00
|
|
|
st_index_t size = RHASH_SIZE(hash);
|
2018-12-03 15:36:39 +03:00
|
|
|
VALUE keys = rb_ary_new_capa(size);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-11-27 20:07:10 +04:00
|
|
|
if (size == 0) return keys;
|
2013-10-10 20:28:56 +04:00
|
|
|
|
2013-11-27 20:07:10 +04:00
|
|
|
if (ST_DATA_COMPATIBLE_P(VALUE)) {
|
2018-12-03 15:36:39 +03:00
|
|
|
RARRAY_PTR_USE_TRANSIENT(keys, ptr, {
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
size = ar_keys(hash, ptr, size);
|
2018-12-03 15:36:39 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
st_table *table = RHASH_ST_TABLE(hash);
|
|
|
|
size = st_keys(table, ptr, size);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
rb_gc_writebarrier_remember(keys);
|
2013-11-27 20:07:10 +04:00
|
|
|
rb_ary_set_len(keys, size);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_hash_foreach(hash, keys_i, keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
values_i(VALUE key, VALUE value, VALUE ary)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, value);
|
1998-01-16 15:13:05 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-29 21:53:11 +03:00
|
|
|
* hash.values -> new_array
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Returns a new \Array containing all values in +self+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.values # => [0, 1, 2]
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2013-10-08 11:11:15 +04:00
|
|
|
VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_values(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2013-11-28 12:39:16 +04:00
|
|
|
VALUE values;
|
|
|
|
st_index_t size = RHASH_SIZE(hash);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-11-28 12:39:16 +04:00
|
|
|
values = rb_ary_new_capa(size);
|
|
|
|
if (size == 0) return values;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-11-28 12:39:16 +04:00
|
|
|
if (ST_DATA_COMPATIBLE_P(VALUE)) {
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
rb_gc_writebarrier_remember(values);
|
2018-12-03 15:36:39 +03:00
|
|
|
RARRAY_PTR_USE_TRANSIENT(values, ptr, {
|
2018-12-14 04:10:15 +03:00
|
|
|
size = ar_values(hash, ptr, size);
|
2018-10-31 01:12:12 +03:00
|
|
|
});
|
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
else if (RHASH_ST_TABLE_P(hash)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
st_table *table = RHASH_ST_TABLE(hash);
|
|
|
|
rb_gc_writebarrier_remember(values);
|
2018-12-03 15:36:39 +03:00
|
|
|
RARRAY_PTR_USE_TRANSIENT(values, ptr, {
|
2018-10-31 01:12:12 +03:00
|
|
|
size = st_values(table, ptr, size);
|
|
|
|
});
|
|
|
|
}
|
2013-11-28 12:39:16 +04:00
|
|
|
rb_ary_set_len(values, size);
|
|
|
|
}
|
2020-05-29 21:53:11 +03:00
|
|
|
|
2013-11-28 12:39:16 +04:00
|
|
|
else {
|
|
|
|
rb_hash_foreach(hash, values_i, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-29 21:53:11 +03:00
|
|
|
* hash.include?(key) -> true or false
|
|
|
|
* hash.has_key?(key) -> true or false
|
|
|
|
* hash.key?(key) -> true or false
|
|
|
|
* hash.member?(key) -> true or false
|
|
|
|
|
|
|
|
* Methods #has_key?, #key?, and #member? are aliases for \#include?.
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Returns +true+ if +key+ is a key in +self+, otherwise +false+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.include?(:bar) # => true
|
|
|
|
* h.include?(:nosuch) # => false
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 14:22:28 +03:00
|
|
|
MJIT_FUNC_EXPORTED VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_has_key(VALUE hash, VALUE key)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2019-01-05 02:48:33 +03:00
|
|
|
if (hash_stlike_lookup(hash, key, NULL)) {
|
2018-10-31 01:11:51 +03:00
|
|
|
return Qtrue;
|
|
|
|
}
|
2019-01-05 02:48:33 +03:00
|
|
|
else {
|
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2009-05-09 07:19:52 +04:00
|
|
|
rb_hash_search_value(VALUE key, VALUE value, VALUE arg)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-05-09 07:19:52 +04:00
|
|
|
VALUE *data = (VALUE *)arg;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (rb_equal(value, data[1])) {
|
1999-01-20 07:59:39 +03:00
|
|
|
data[0] = Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-05-29 21:53:11 +03:00
|
|
|
* hash.has_value?(value) -> true or false
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Returns +true+ if +value+ is a value in +self+, otherwise +false+:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.has_value?(1) # => true
|
|
|
|
* h.has_value?(123) # => false
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_has_value(VALUE hash, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE data[2];
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
data[0] = Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
data[1] = val;
|
2009-05-09 07:19:52 +04:00
|
|
|
rb_hash_foreach(hash, rb_hash_search_value, (VALUE)data);
|
1998-01-16 15:13:05 +03:00
|
|
|
return data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
struct equal_data {
|
2007-09-24 17:43:58 +04:00
|
|
|
VALUE result;
|
2018-10-31 01:11:51 +03:00
|
|
|
VALUE hash;
|
2007-09-24 17:43:58 +04:00
|
|
|
int eql;
|
1998-01-16 15:13:05 +03:00
|
|
|
};
|
|
|
|
|
2006-09-22 12:36:02 +04:00
|
|
|
static int
|
2009-05-09 07:19:52 +04:00
|
|
|
eql_i(VALUE key, VALUE val1, VALUE arg)
|
2006-09-22 12:36:02 +04:00
|
|
|
{
|
2009-05-09 07:19:52 +04:00
|
|
|
struct equal_data *data = (struct equal_data *)arg;
|
* compile.c (iseq_build_body), error.c (set_syserr, get_syserr),
(syserr_initialize), gc.c (define_final, rb_gc_copy_finalizer),
(run_final), hash.c (rb_hash_aref, rb_hash_lookup2),
(rb_hash_fetch_m, rb_hash_clear, rb_hash_aset, eql_i),
iseq.c (iseq_load, iseq_data_to_ary), marshal.c (r_symlink),
thread.c (rb_thread_local_aref),
variable.c (generic_ivar_remove, ivar_get, rb_const_get_0),
(rb_cvar_get), vm.c (rb_vm_check_redefinition_opt_method),
vm_insnhelper.c (vm_get_ev_const), vm_method.c (remove_method),
ext/iconv/iconv.c (map_charset): use st_data_t.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-10-12 18:47:23 +04:00
|
|
|
st_data_t val2;
|
2006-09-22 12:36:02 +04:00
|
|
|
|
2019-01-05 02:48:33 +03:00
|
|
|
if (!hash_stlike_lookup(data->hash, key, &val2)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
data->result = Qfalse;
|
|
|
|
return ST_STOP;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2019-01-05 02:48:33 +03:00
|
|
|
else {
|
|
|
|
if (!(data->eql ? rb_eql(val1, (VALUE)val2) : (int)rb_equal(val1, (VALUE)val2))) {
|
|
|
|
data->result = Qfalse;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
2006-09-22 12:36:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-24 17:43:58 +04:00
|
|
|
static VALUE
|
|
|
|
recursive_eql(VALUE hash, VALUE dt, int recur)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2007-09-24 17:43:58 +04:00
|
|
|
struct equal_data *data;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2009-05-24 17:48:23 +04:00
|
|
|
if (recur) return Qtrue; /* Subtle! */
|
2007-09-24 17:43:58 +04:00
|
|
|
data = (struct equal_data*)dt;
|
|
|
|
data->result = Qtrue;
|
2009-05-09 07:19:52 +04:00
|
|
|
rb_hash_foreach(hash, eql_i, dt);
|
2007-09-24 17:43:58 +04:00
|
|
|
|
|
|
|
return data->result;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2004-05-07 12:44:24 +04:00
|
|
|
static VALUE
|
2006-09-22 12:36:02 +04:00
|
|
|
hash_equal(VALUE hash1, VALUE hash2, int eql)
|
2004-05-07 12:44:24 +04:00
|
|
|
{
|
|
|
|
struct equal_data data;
|
|
|
|
|
|
|
|
if (hash1 == hash2) return Qtrue;
|
2011-09-29 15:07:45 +04:00
|
|
|
if (!RB_TYPE_P(hash2, T_HASH)) {
|
2014-05-20 12:28:31 +04:00
|
|
|
if (!rb_respond_to(hash2, idTo_hash)) {
|
2004-05-07 12:44:24 +04:00
|
|
|
return Qfalse;
|
|
|
|
}
|
2014-12-19 09:36:05 +03:00
|
|
|
if (eql) {
|
|
|
|
if (rb_eql(hash2, hash1)) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-09-20 21:03:41 +04:00
|
|
|
return rb_equal(hash2, hash1);
|
2014-12-19 09:36:05 +03:00
|
|
|
}
|
2004-05-07 12:44:24 +04:00
|
|
|
}
|
2007-08-30 03:12:21 +04:00
|
|
|
if (RHASH_SIZE(hash1) != RHASH_SIZE(hash2))
|
2004-05-07 12:44:24 +04:00
|
|
|
return Qfalse;
|
2018-12-14 05:27:23 +03:00
|
|
|
if (!RHASH_TABLE_EMPTY_P(hash1) && !RHASH_TABLE_EMPTY_P(hash2)) {
|
|
|
|
if (RHASH_TYPE(hash1) != RHASH_TYPE(hash2)) {
|
2018-10-31 01:12:12 +03:00
|
|
|
return Qfalse;
|
2018-12-14 05:27:23 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
data.hash = hash2;
|
|
|
|
data.eql = eql;
|
|
|
|
return rb_exec_recursive_paired(recursive_eql, hash1, hash2, (VALUE)&data);
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-10-31 01:12:12 +03:00
|
|
|
|
2006-09-22 12:36:02 +04:00
|
|
|
#if 0
|
2009-11-08 12:13:17 +03:00
|
|
|
if (!(rb_equal(RHASH_IFNONE(hash1), RHASH_IFNONE(hash2)) &&
|
2019-07-31 04:22:47 +03:00
|
|
|
FL_TEST(hash1, RHASH_PROC_DEFAULT) == FL_TEST(hash2, RHASH_PROC_DEFAULT)))
|
2006-09-22 12:36:02 +04:00
|
|
|
return Qfalse;
|
|
|
|
#endif
|
2018-10-31 01:11:51 +03:00
|
|
|
return Qtrue;
|
2004-05-07 12:44:24 +04:00
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash == object -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if all of the following are true:
|
|
|
|
* * +object+ is a \Hash object.
|
|
|
|
* * +hash+ and +object+ have the same keys (regardless of order).
|
|
|
|
* * For each key +key+, <tt>hash[key] == object[key]</tt>.
|
|
|
|
*
|
|
|
|
* Otherwise, returns +false+.
|
|
|
|
*
|
|
|
|
* Equal:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 == h2 # => true
|
|
|
|
* h3 = {baz: 2, bar: 1, foo: 0}
|
|
|
|
* h1 == h3 # => true
|
|
|
|
*
|
|
|
|
* Not equal because of class:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 == 1 # false
|
|
|
|
*
|
|
|
|
* Not equal because of different keys:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {foo: 0, bar: 1, zab: 2}
|
|
|
|
* h1 == h2 # => false
|
|
|
|
*
|
|
|
|
* Not equal because of different values:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {foo: 0, bar: 1, baz: 3}
|
|
|
|
* h1 == h2 # => false
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_equal(VALUE hash1, VALUE hash2)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2009-07-18 12:05:32 +04:00
|
|
|
return hash_equal(hash1, hash2, FALSE);
|
2004-05-07 12:44:24 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2006-09-22 12:36:02 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash.eql? object -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if all of the following are true:
|
|
|
|
* * +object+ is a \Hash object.
|
|
|
|
* * +hash+ and +object+ have the same keys (regardless of order).
|
|
|
|
* * For each key +key+, <tt>h[key] eql? object[key]</tt>.
|
|
|
|
*
|
|
|
|
* Otherwise, returns +false+.
|
|
|
|
*
|
|
|
|
* Equal:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1.eql? h2 # => true
|
|
|
|
* h3 = {baz: 2, bar: 1, foo: 0}
|
|
|
|
* h1.eql? h3 # => true
|
|
|
|
*
|
|
|
|
* Not equal because of class:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1.eql? 1 # false
|
|
|
|
*
|
|
|
|
* Not equal because of different keys:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {foo: 0, bar: 1, zab: 2}
|
|
|
|
* h1.eql? h2 # => false
|
|
|
|
*
|
|
|
|
* Not equal because of different values:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {foo: 0, bar: 1, baz: 3}
|
|
|
|
* h1.eql? h2 # => false
|
2006-09-22 12:36:02 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_eql(VALUE hash1, VALUE hash2)
|
|
|
|
{
|
2009-07-18 12:05:32 +04:00
|
|
|
return hash_equal(hash1, hash2, TRUE);
|
2006-09-22 12:36:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-05-09 07:19:52 +04:00
|
|
|
hash_i(VALUE key, VALUE val, VALUE arg)
|
2006-09-22 12:36:02 +04:00
|
|
|
{
|
2009-09-08 17:10:04 +04:00
|
|
|
st_index_t *hval = (st_index_t *)arg;
|
2011-01-12 02:37:15 +03:00
|
|
|
st_index_t hdata[2];
|
2009-05-09 07:19:52 +04:00
|
|
|
|
2011-01-12 02:37:15 +03:00
|
|
|
hdata[0] = rb_hash(key);
|
|
|
|
hdata[1] = rb_hash(val);
|
|
|
|
*hval ^= st_hash(hdata, sizeof(hdata), 0);
|
2006-09-22 12:36:02 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash.hash -> an_integer
|
|
|
|
*
|
|
|
|
* Returns the \Integer hash-code for the hash:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1.hash.class # => Integer
|
|
|
|
*
|
|
|
|
* Two \Hash objects have the same hash-code if their content is the same
|
|
|
|
* (regardless or order):
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {baz: 2, bar: 1, foo: 0}
|
|
|
|
* h2.hash == h1.hash # => true
|
|
|
|
* h2.eql? h1 # => true
|
2006-09-22 12:36:02 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_hash(VALUE hash)
|
|
|
|
{
|
2013-12-10 18:44:51 +04:00
|
|
|
st_index_t size = RHASH_SIZE(hash);
|
|
|
|
st_index_t hval = rb_hash_start(size);
|
|
|
|
hval = rb_hash_uint(hval, (st_index_t)rb_hash_hash);
|
|
|
|
if (size) {
|
|
|
|
rb_hash_foreach(hash, hash_i, (VALUE)&hval);
|
|
|
|
}
|
2013-12-03 17:18:30 +04:00
|
|
|
hval = rb_hash_end(hval);
|
2016-10-04 19:25:01 +03:00
|
|
|
return ST2FIX(hval);
|
2006-09-22 12:36:02 +04:00
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_invert_i(VALUE key, VALUE value, VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_hash_aset(hash, value, key);
|
1998-01-16 15:13:05 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash.invert -> new_hash
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-06-02 18:53:25 +03:00
|
|
|
* Returns a new \Hash object with the each key-value pair inverted:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = h.invert
|
|
|
|
* h1 # => {0=>:foo, 1=>:bar, 2=>:baz}
|
2016-02-13 15:45:59 +03:00
|
|
|
*
|
2020-06-02 18:53:25 +03:00
|
|
|
* Overwrites any repeated new keys:
|
|
|
|
* (see {Entry Order}[#class-Hash-label-Entry+Order]):
|
|
|
|
* h = {foo: 0, bar: 0, baz: 0}
|
|
|
|
* h.invert # => {0=>:baz}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_invert(VALUE hash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2017-10-01 07:26:25 +03:00
|
|
|
VALUE h = rb_hash_new_with_size(RHASH_SIZE(hash));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
rb_hash_foreach(hash, rb_hash_invert_i, h);
|
1998-01-16 15:13:05 +03:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2012-04-13 10:26:53 +04:00
|
|
|
static int
|
2013-06-19 01:29:30 +04:00
|
|
|
rb_hash_update_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
|
2012-04-13 10:26:53 +04:00
|
|
|
{
|
2013-05-29 05:38:52 +04:00
|
|
|
if (existing) {
|
2013-06-19 01:29:30 +04:00
|
|
|
arg->old_value = *value;
|
|
|
|
arg->new_value = arg->arg;
|
2013-05-29 05:38:52 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-06-19 01:29:30 +04:00
|
|
|
arg->new_key = *key;
|
|
|
|
arg->new_value = arg->arg;
|
2013-05-29 05:38:52 +04:00
|
|
|
}
|
2013-06-19 01:29:30 +04:00
|
|
|
*value = arg->arg;
|
2012-04-13 10:26:53 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:20:27 +03:00
|
|
|
NOINSERT_UPDATE_CALLBACK(rb_hash_update_callback)
|
2012-04-13 10:26:53 +04:00
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_hash_update_i(VALUE key, VALUE value, VALUE hash)
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
2012-04-13 10:26:53 +04:00
|
|
|
RHASH_UPDATE(hash, key, rb_hash_update_callback, value);
|
1998-01-16 15:19:22 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2013-06-19 01:29:30 +04:00
|
|
|
rb_hash_update_block_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
|
2002-01-25 11:22:11 +03:00
|
|
|
{
|
2013-06-19 01:29:30 +04:00
|
|
|
VALUE newvalue = (VALUE)arg->arg;
|
2013-05-29 05:38:52 +04:00
|
|
|
|
2012-04-13 10:26:53 +04:00
|
|
|
if (existing) {
|
|
|
|
newvalue = rb_yield_values(3, (VALUE)*key, (VALUE)*value, newvalue);
|
2013-06-19 01:29:30 +04:00
|
|
|
arg->old_value = *value;
|
2002-01-25 11:22:11 +03:00
|
|
|
}
|
2013-05-29 05:38:52 +04:00
|
|
|
else {
|
2013-06-19 01:29:30 +04:00
|
|
|
arg->new_key = *key;
|
2013-05-29 05:38:52 +04:00
|
|
|
}
|
2016-04-28 11:03:19 +03:00
|
|
|
arg->new_value = newvalue;
|
2013-05-29 05:38:52 +04:00
|
|
|
*value = newvalue;
|
2012-04-13 10:26:53 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:20:27 +03:00
|
|
|
NOINSERT_UPDATE_CALLBACK(rb_hash_update_block_callback)
|
2012-04-13 10:26:53 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
rb_hash_update_block_i(VALUE key, VALUE value, VALUE hash)
|
|
|
|
{
|
|
|
|
RHASH_UPDATE(hash, key, rb_hash_update_block_callback, value);
|
2002-01-25 11:22:11 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash.merge! -> self
|
|
|
|
* hash.merge!(*other_hashes) -> self
|
|
|
|
* hash.merge!(*other_hashes) { |key, old_value, new_value| ... } -> self
|
|
|
|
*
|
|
|
|
* Merges each of +other_hashes+ into +self+; returns +self+.
|
|
|
|
*
|
2020-08-20 15:34:24 +03:00
|
|
|
* Each argument in +other_hashes+ must be a \Hash.
|
2020-06-02 18:53:25 +03:00
|
|
|
*
|
|
|
|
* \Method #update is an alias for \#merge!.
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* With arguments and no block:
|
|
|
|
* * Returns +self+, after the given hashes are merged into it.
|
|
|
|
* * The given hashes are merged left to right.
|
|
|
|
* * Each new entry is added at the end.
|
|
|
|
* * Each duplicate-key entry's value overwrites the previous value.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = {bat: 3, bar: 4}
|
|
|
|
* h2 = {bam: 5, bat:6}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.merge!(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
|
2020-06-02 18:53:25 +03:00
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* With arguments and a block:
|
|
|
|
* * Returns +self+, after the given hashes are merged.
|
|
|
|
* * The given hashes are merged left to right.
|
|
|
|
* * Each new-key entry is added at the end.
|
|
|
|
* * For each duplicate key:
|
|
|
|
* * Calls the block with the key and the old and new values.
|
|
|
|
* * The block's return value becomes the new value for the entry.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = {bat: 3, bar: 4}
|
|
|
|
* h2 = {bam: 5, bat:6}
|
|
|
|
* h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value }
|
|
|
|
* h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
|
|
|
|
*
|
|
|
|
* Allows the block to add a new key:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = {bat: 3, bar: 4}
|
|
|
|
* h2 = {bam: 5, bat:6}
|
|
|
|
* h3 = h.merge!(h1, h2) { |key, old_value, new_value| h[:new_key] = 10 }
|
|
|
|
* h3 # => {:foo=>0, :bar=>10, :baz=>2, :bat=>10, :new_key=>10, :bam=>5}
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* With no arguments:
|
|
|
|
* * Returns +self+, unmodified.
|
|
|
|
* * The block, if given, is ignored.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.merge # => {:foo=>0, :bar=>1, :baz=>2}
|
2020-06-10 14:45:29 +03:00
|
|
|
* h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' }
|
2020-06-02 18:53:25 +03:00
|
|
|
* h1 # => {:foo=>0, :bar=>1, :baz=>2}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2018-09-19 04:59:26 +03:00
|
|
|
rb_hash_update(int argc, VALUE *argv, VALUE self)
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
2018-09-19 04:59:26 +03:00
|
|
|
int i;
|
|
|
|
bool block_given = rb_block_given_p();
|
|
|
|
|
|
|
|
rb_hash_modify(self);
|
|
|
|
for (i = 0; i < argc; i++){
|
|
|
|
VALUE hash = to_hash(argv[i]);
|
|
|
|
if (block_given) {
|
2018-09-19 05:10:58 +03:00
|
|
|
rb_hash_foreach(hash, rb_hash_update_block_i, self);
|
2018-09-19 04:59:26 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-09-19 05:10:58 +03:00
|
|
|
rb_hash_foreach(hash, rb_hash_update_i, self);
|
2018-09-19 04:59:26 +03:00
|
|
|
}
|
2010-12-04 05:21:53 +03:00
|
|
|
}
|
2018-09-19 04:59:26 +03:00
|
|
|
return self;
|
2010-12-04 05:21:53 +03:00
|
|
|
}
|
|
|
|
|
2013-06-19 01:29:30 +04:00
|
|
|
struct update_func_arg {
|
2010-12-04 05:21:53 +03:00
|
|
|
VALUE hash;
|
2012-04-13 10:26:53 +04:00
|
|
|
VALUE value;
|
2010-12-04 05:21:53 +03:00
|
|
|
rb_hash_update_func *func;
|
|
|
|
};
|
|
|
|
|
2012-04-13 10:26:53 +04:00
|
|
|
static int
|
2013-06-19 01:29:30 +04:00
|
|
|
rb_hash_update_func_callback(st_data_t *key, st_data_t *value, struct update_arg *arg, int existing)
|
2012-04-13 10:26:53 +04:00
|
|
|
{
|
2013-06-19 01:29:30 +04:00
|
|
|
struct update_func_arg *uf_arg = (struct update_func_arg *)arg->arg;
|
|
|
|
VALUE newvalue = uf_arg->value;
|
|
|
|
|
2012-04-13 10:26:53 +04:00
|
|
|
if (existing) {
|
2013-06-19 01:29:30 +04:00
|
|
|
newvalue = (*uf_arg->func)((VALUE)*key, (VALUE)*value, newvalue);
|
|
|
|
arg->old_value = *value;
|
2013-05-29 05:38:52 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-06-19 01:29:30 +04:00
|
|
|
arg->new_key = *key;
|
2012-04-13 10:26:53 +04:00
|
|
|
}
|
2016-04-28 11:03:19 +03:00
|
|
|
arg->new_value = newvalue;
|
2013-05-29 05:38:52 +04:00
|
|
|
*value = newvalue;
|
2012-04-13 10:26:53 +04:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:20:27 +03:00
|
|
|
NOINSERT_UPDATE_CALLBACK(rb_hash_update_func_callback)
|
2012-04-13 10:26:53 +04:00
|
|
|
|
2010-12-04 05:21:53 +03:00
|
|
|
static int
|
|
|
|
rb_hash_update_func_i(VALUE key, VALUE value, VALUE arg0)
|
|
|
|
{
|
2013-06-19 01:29:30 +04:00
|
|
|
struct update_func_arg *arg = (struct update_func_arg *)arg0;
|
2010-12-04 05:21:53 +03:00
|
|
|
VALUE hash = arg->hash;
|
|
|
|
|
2012-04-13 10:26:53 +04:00
|
|
|
arg->value = value;
|
2013-05-26 16:37:11 +04:00
|
|
|
RHASH_UPDATE(hash, key, rb_hash_update_func_callback, (VALUE)arg);
|
2010-12-04 05:21:53 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_hash_update_by(VALUE hash1, VALUE hash2, rb_hash_update_func *func)
|
|
|
|
{
|
|
|
|
rb_hash_modify(hash1);
|
|
|
|
hash2 = to_hash(hash2);
|
|
|
|
if (func) {
|
2013-06-19 01:29:30 +04:00
|
|
|
struct update_func_arg arg;
|
2010-12-04 05:21:53 +03:00
|
|
|
arg.hash = hash1;
|
|
|
|
arg.func = func;
|
|
|
|
rb_hash_foreach(hash2, rb_hash_update_func_i, (VALUE)&arg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_hash_foreach(hash2, rb_hash_update_i, hash1);
|
2002-01-25 11:22:11 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
return hash1;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash.merge -> copy_of_self
|
|
|
|
* hash.merge(*other_hashes) -> new_hash
|
|
|
|
* hash.merge(*other_hashes) { |key, old_value, new_value| ... } -> new_hash
|
|
|
|
*
|
|
|
|
* Returns the new \Hash formed by merging each of +other_hashes+
|
|
|
|
* into a copy of +self+.
|
|
|
|
*
|
2020-08-20 15:34:24 +03:00
|
|
|
* Each argument in +other_hashes+ must be a \Hash.
|
2020-06-02 18:53:25 +03:00
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* With arguments and no block:
|
|
|
|
* * Returns the new \Hash object formed by merging each successive
|
|
|
|
* \Hash in +other_hashes+ into +self+.
|
|
|
|
* * Each new-key entry is added at the end.
|
|
|
|
* * Each duplicate-key entry's value overwrites the previous value.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = {bat: 3, bar: 4}
|
|
|
|
* h2 = {bam: 5, bat:6}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.merge(h1, h2) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}
|
2020-06-02 18:53:25 +03:00
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* With arguments and a block:
|
|
|
|
* * Returns a new \Hash object that is the merge of +self+ and each given hash.
|
|
|
|
* * The given hashes are merged left to right.
|
|
|
|
* * Each new-key entry is added at the end.
|
|
|
|
* * For each duplicate key:
|
|
|
|
* * Calls the block with the key and the old and new values.
|
|
|
|
* * The block's return value becomes the new value for the entry.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = {bat: 3, bar: 4}
|
|
|
|
* h2 = {bam: 5, bat:6}
|
|
|
|
* h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
|
|
|
|
* h3 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}
|
|
|
|
*
|
|
|
|
* Ignores an attempt in the block to add a new key:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 = {bat: 3, bar: 4}
|
|
|
|
* h2 = {bam: 5, bat:6}
|
|
|
|
* h3 = h.merge(h1, h2) { |key, old_value, new_value| h[:new_key] = 10 }
|
|
|
|
* h3 # => {:foo=>0, :bar=>10, :baz=>2, :bat=>10, :bam=>5}
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-06-02 18:53:25 +03:00
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* With no arguments:
|
|
|
|
* * Returns a copy of +self+.
|
|
|
|
* * The block, if given, is ignored.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.merge # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
* h1 = h.merge { |key, old_value, new_value| raise 'Cannot happen' }
|
2020-06-02 18:53:25 +03:00
|
|
|
* h1 # => {:foo=>0, :bar=>1, :baz=>2}
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
2003-02-03 08:34:16 +03:00
|
|
|
static VALUE
|
2018-09-19 04:59:26 +03:00
|
|
|
rb_hash_merge(int argc, VALUE *argv, VALUE self)
|
2003-02-03 08:34:16 +03:00
|
|
|
{
|
2018-09-19 04:59:26 +03:00
|
|
|
return rb_hash_update(argc, argv, rb_hash_dup(self));
|
2003-02-03 08:34:16 +03:00
|
|
|
}
|
|
|
|
|
2013-07-29 16:39:21 +04:00
|
|
|
static int
|
|
|
|
assoc_cmp(VALUE a, VALUE b)
|
|
|
|
{
|
|
|
|
return !RTEST(rb_equal(a, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
lookup2_call(VALUE arg)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)arg;
|
|
|
|
return rb_hash_lookup2(args[0], args[1], Qundef);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct reset_hash_type_arg {
|
|
|
|
VALUE hash;
|
|
|
|
const struct st_hash_type *orighash;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
reset_hash_type(VALUE arg)
|
|
|
|
{
|
|
|
|
struct reset_hash_type_arg *p = (struct reset_hash_type_arg *)arg;
|
2018-12-14 04:10:15 +03:00
|
|
|
HASH_ASSERT(RHASH_ST_TABLE_P(p->hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
RHASH_ST_TABLE(p->hash)->type = p->orighash;
|
2013-07-29 16:39:21 +04:00
|
|
|
return Qundef;
|
|
|
|
}
|
|
|
|
|
2007-06-22 20:26:07 +04:00
|
|
|
static int
|
2013-07-29 15:24:08 +04:00
|
|
|
assoc_i(VALUE key, VALUE val, VALUE arg)
|
2007-06-22 20:26:07 +04:00
|
|
|
{
|
2013-07-29 15:24:08 +04:00
|
|
|
VALUE *args = (VALUE *)arg;
|
2013-07-29 09:58:36 +04:00
|
|
|
|
2013-07-29 15:24:08 +04:00
|
|
|
if (RTEST(rb_equal(args[0], key))) {
|
|
|
|
args[1] = rb_assoc_new(key, val);
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
2007-06-22 20:26:07 +04:00
|
|
|
}
|
|
|
|
|
2007-06-29 09:15:46 +04:00
|
|
|
/*
|
2007-06-22 20:26:07 +04:00
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash.assoc(key) -> new_array or nil
|
2007-06-22 20:26:07 +04:00
|
|
|
*
|
2020-06-02 18:53:25 +03:00
|
|
|
* If the given +key+ is found, returns a 2-element \Array containing that key and its value:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.assoc(:bar) # => [:bar, 1]
|
2007-06-22 20:26:07 +04:00
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if key +key+ is not found.
|
2007-06-22 20:26:07 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
2013-07-29 16:39:21 +04:00
|
|
|
rb_hash_assoc(VALUE hash, VALUE key)
|
2007-06-22 20:26:07 +04:00
|
|
|
{
|
2013-07-29 16:39:21 +04:00
|
|
|
st_table *table;
|
|
|
|
const struct st_hash_type *orighash;
|
2013-07-29 15:24:08 +04:00
|
|
|
VALUE args[2];
|
|
|
|
|
2013-07-29 16:39:21 +04:00
|
|
|
if (RHASH_EMPTY_P(hash)) return Qnil;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_force_convert_table(hash, __FILE__, __LINE__);
|
|
|
|
HASH_ASSERT(RHASH_ST_TABLE_P(hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
table = RHASH_ST_TABLE(hash);
|
2013-07-29 16:39:21 +04:00
|
|
|
orighash = table->type;
|
|
|
|
|
|
|
|
if (orighash != &identhash) {
|
|
|
|
VALUE value;
|
|
|
|
struct reset_hash_type_arg ensure_arg;
|
|
|
|
struct st_hash_type assochash;
|
|
|
|
|
|
|
|
assochash.compare = assoc_cmp;
|
|
|
|
assochash.hash = orighash->hash;
|
2018-10-31 01:11:51 +03:00
|
|
|
table->type = &assochash;
|
2013-07-29 16:39:21 +04:00
|
|
|
args[0] = hash;
|
|
|
|
args[1] = key;
|
|
|
|
ensure_arg.hash = hash;
|
|
|
|
ensure_arg.orighash = orighash;
|
|
|
|
value = rb_ensure(lookup2_call, (VALUE)&args, reset_hash_type, (VALUE)&ensure_arg);
|
|
|
|
if (value != Qundef) return rb_assoc_new(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
args[0] = key;
|
2013-07-29 15:24:08 +04:00
|
|
|
args[1] = Qnil;
|
|
|
|
rb_hash_foreach(hash, assoc_i, (VALUE)args);
|
|
|
|
return args[1];
|
2007-06-22 20:26:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-05-09 07:19:52 +04:00
|
|
|
rassoc_i(VALUE key, VALUE val, VALUE arg)
|
2007-06-22 20:26:07 +04:00
|
|
|
{
|
2009-05-09 07:19:52 +04:00
|
|
|
VALUE *args = (VALUE *)arg;
|
|
|
|
|
2007-06-22 20:26:07 +04:00
|
|
|
if (RTEST(rb_equal(args[0], val))) {
|
|
|
|
args[1] = rb_assoc_new(key, val);
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-02 18:53:25 +03:00
|
|
|
* hash.rassoc(value) -> new_array or nil
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-06-02 18:53:25 +03:00
|
|
|
* Returns a new 2-element \Array consisting of the key and value
|
|
|
|
* of the first-found entry whose value is <tt>==</tt> to value
|
|
|
|
* (see {Entry Order}[#class-Hash-label-Entry+Order]):
|
|
|
|
* h = {foo: 0, bar: 1, baz: 1}
|
|
|
|
* h.rassoc(1) # => [:bar, 1]
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if no such value found.
|
2007-06-22 20:26:07 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_hash_rassoc(VALUE hash, VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE args[2];
|
|
|
|
|
|
|
|
args[0] = obj;
|
|
|
|
args[1] = Qnil;
|
2009-05-09 07:19:52 +04:00
|
|
|
rb_hash_foreach(hash, rassoc_i, (VALUE)args);
|
2007-06-22 20:26:07 +04:00
|
|
|
return args[1];
|
|
|
|
}
|
|
|
|
|
2013-07-18 12:48:12 +04:00
|
|
|
static int
|
|
|
|
flatten_i(VALUE key, VALUE val, VALUE ary)
|
|
|
|
{
|
|
|
|
VALUE pair[2];
|
|
|
|
|
|
|
|
pair[0] = key;
|
|
|
|
pair[1] = val;
|
|
|
|
rb_ary_cat(ary, pair, 2);
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2007-06-22 20:26:07 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-04 02:53:56 +03:00
|
|
|
* hash.flatten -> new_array
|
|
|
|
* hash.flatten(level) -> new_array
|
|
|
|
*
|
2020-08-20 15:34:24 +03:00
|
|
|
* Argument +level+, if given, must be an \Integer.
|
2020-06-04 02:53:56 +03:00
|
|
|
*
|
|
|
|
* Returns a new \Array object that is a 1-dimensional flattening of +self+.
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* By default, nested Arrays are not flattened:
|
|
|
|
* h = {foo: 0, bar: [:bat, 3], baz: 2}
|
|
|
|
* h.flatten # => [:foo, 0, :bar, [:bat, 3], :baz, 2]
|
|
|
|
*
|
|
|
|
* Takes the depth of recursive flattening from argument +level+:
|
|
|
|
* h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
|
|
|
|
* h.flatten(1) # => [:foo, 0, :bar, [:bat, [:baz, [:bat]]]]
|
|
|
|
* h.flatten(2) # => [:foo, 0, :bar, :bat, [:baz, [:bat]]]
|
|
|
|
* h.flatten(3) # => [:foo, 0, :bar, :bat, :baz, [:bat]]
|
|
|
|
* h.flatten(4) # => [:foo, 0, :bar, :bat, :baz, :bat]
|
|
|
|
*
|
|
|
|
* When +level+ is negative, flattens all nested Arrays:
|
|
|
|
* h = {foo: 0, bar: [:bat, [:baz, [:bat, ]]]}
|
|
|
|
* h.flatten(-1) # => [:foo, 0, :bar, :bat, :baz, :bat]
|
|
|
|
* h.flatten(-2) # => [:foo, 0, :bar, :bat, :baz, :bat]
|
|
|
|
*
|
|
|
|
* When +level+ is zero, returns the equivalent of #to_a :
|
|
|
|
* h = {foo: 0, bar: [:bat, 3], baz: 2}
|
|
|
|
* h.flatten(0) # => [[:foo, 0], [:bar, [:bat, 3]], [:baz, 2]]
|
|
|
|
* h.flatten(0) == h.to_a # => true
|
2007-06-22 20:26:07 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
|
|
|
|
{
|
2013-07-18 12:48:12 +04:00
|
|
|
VALUE ary;
|
2007-06-22 20:26:07 +04:00
|
|
|
|
2017-10-22 03:38:05 +03:00
|
|
|
rb_check_arity(argc, 0, 1);
|
|
|
|
|
2013-07-18 12:48:12 +04:00
|
|
|
if (argc) {
|
2017-10-22 03:38:05 +03:00
|
|
|
int level = NUM2INT(argv[0]);
|
|
|
|
|
2014-02-20 07:09:13 +04:00
|
|
|
if (level == 0) return rb_hash_to_a(hash);
|
|
|
|
|
|
|
|
ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
|
|
|
|
rb_hash_foreach(hash, flatten_i, ary);
|
2017-10-22 03:38:05 +03:00
|
|
|
level--;
|
|
|
|
|
|
|
|
if (level > 0) {
|
|
|
|
VALUE ary_flatten_level = INT2FIX(level);
|
|
|
|
rb_funcallv(ary, id_flatten_bang, 1, &ary_flatten_level);
|
2014-02-20 06:27:30 +04:00
|
|
|
}
|
2014-02-20 07:09:13 +04:00
|
|
|
else if (level < 0) {
|
2017-10-22 03:38:05 +03:00
|
|
|
/* flatten recursively */
|
2016-07-29 14:57:14 +03:00
|
|
|
rb_funcallv(ary, id_flatten_bang, 0, 0);
|
2013-07-18 12:48:12 +04:00
|
|
|
}
|
2008-02-05 19:51:30 +03:00
|
|
|
}
|
2014-02-20 07:09:13 +04:00
|
|
|
else {
|
|
|
|
ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
|
|
|
|
rb_hash_foreach(hash, flatten_i, ary);
|
|
|
|
}
|
|
|
|
|
2007-06-22 20:26:07 +04:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2016-10-13 11:06:00 +03:00
|
|
|
static int
|
|
|
|
delete_if_nil(VALUE key, VALUE value, VALUE hash)
|
|
|
|
{
|
|
|
|
if (NIL_P(value)) {
|
|
|
|
return ST_DELETE;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_if_not_nil(VALUE key, VALUE value, VALUE hash)
|
|
|
|
{
|
|
|
|
if (!NIL_P(value)) {
|
|
|
|
rb_hash_aset(hash, key, value);
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-04 02:53:56 +03:00
|
|
|
* hash.compact -> new_hash
|
2016-10-13 11:06:00 +03:00
|
|
|
*
|
2020-06-04 02:53:56 +03:00
|
|
|
* Returns a copy of +self+ with all +nil+-valued entries removed:
|
|
|
|
* h = {foo: 0, bar: nil, baz: 2, bat: nil}
|
|
|
|
* h1 = h.compact
|
|
|
|
* h1 # => {:foo=>0, :baz=>2}
|
2016-10-13 11:06:00 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_compact(VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE result = rb_hash_new();
|
|
|
|
if (!RHASH_EMPTY_P(hash)) {
|
|
|
|
rb_hash_foreach(hash, set_if_not_nil, result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-04 02:53:56 +03:00
|
|
|
* hash.compact! -> self or nil
|
2016-10-13 11:06:00 +03:00
|
|
|
*
|
2020-06-04 02:53:56 +03:00
|
|
|
* Returns +self+ with all its +nil+-valued entries removed (in place):
|
|
|
|
* h = {foo: 0, bar: nil, baz: 2, bat: nil}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.compact! # => {:foo=>0, :baz=>2}
|
2016-10-13 11:06:00 +03:00
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if no entries were removed.
|
2016-10-13 11:06:00 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_hash_compact_bang(VALUE hash)
|
|
|
|
{
|
2018-10-31 01:11:51 +03:00
|
|
|
st_index_t n;
|
2016-10-13 11:06:00 +03:00
|
|
|
rb_hash_modify_check(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
n = RHASH_SIZE(hash);
|
|
|
|
if (n) {
|
2016-10-13 11:06:00 +03:00
|
|
|
rb_hash_foreach(hash, delete_if_nil, hash);
|
2018-10-31 01:12:12 +03:00
|
|
|
if (n != RHASH_SIZE(hash))
|
2016-10-22 12:52:35 +03:00
|
|
|
return hash;
|
2016-10-13 11:06:00 +03:00
|
|
|
}
|
2016-10-22 12:52:35 +03:00
|
|
|
return Qnil;
|
2016-10-13 11:06:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-18 06:13:08 +03:00
|
|
|
static st_table *rb_init_identtable_with_size(st_index_t size);
|
|
|
|
|
2006-09-11 12:09:19 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-04 02:53:56 +03:00
|
|
|
* hash.compare_by_identity -> self
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-06-04 02:53:56 +03:00
|
|
|
* Sets +self+ to consider only identity in comparing keys;
|
|
|
|
* two keys are considered the same only if they are the same object;
|
|
|
|
* returns +self+.
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-06-04 02:53:56 +03:00
|
|
|
* By default, these two object are considered to be the same key,
|
|
|
|
* so +s1+ will overwrite +s0+:
|
|
|
|
* s0 = 'x'
|
|
|
|
* s1 = 'x'
|
|
|
|
* h = {}
|
|
|
|
* h.compare_by_identity? # => false
|
|
|
|
* h[s0] = 0
|
|
|
|
* h[s1] = 1
|
|
|
|
* h # => {"x"=>1}
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-06-04 02:53:56 +03:00
|
|
|
* After calling \#compare_by_identity, the keys are considered to be different,
|
|
|
|
* and therefore do not overwrite each other:
|
|
|
|
* h = {}
|
2020-08-23 20:10:01 +03:00
|
|
|
* h.compare_by_identity # => {}
|
2020-06-04 02:53:56 +03:00
|
|
|
* h.compare_by_identity? # => true
|
|
|
|
* h[s0] = 0
|
|
|
|
* h[s1] = 1
|
|
|
|
* h # => {"x"=>0, "x"=>1}
|
2006-09-11 12:09:19 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2006-10-12 18:05:17 +04:00
|
|
|
rb_hash_compare_by_id(VALUE hash)
|
2006-09-11 12:09:19 +04:00
|
|
|
{
|
2018-10-31 01:11:51 +03:00
|
|
|
VALUE tmp;
|
2017-10-22 03:57:46 +03:00
|
|
|
st_table *identtable;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2013-07-29 16:57:40 +04:00
|
|
|
if (rb_hash_compare_by_id_p(hash)) return hash;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2017-10-22 03:57:46 +03:00
|
|
|
rb_hash_modify_check(hash);
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_force_convert_table(hash, __FILE__, __LINE__);
|
|
|
|
HASH_ASSERT(RHASH_ST_TABLE_P(hash));
|
2017-10-22 03:57:46 +03:00
|
|
|
|
2018-10-31 01:11:51 +03:00
|
|
|
tmp = hash_alloc(0);
|
2017-10-22 03:57:46 +03:00
|
|
|
identtable = rb_init_identtable_with_size(RHASH_SIZE(hash));
|
2018-10-31 01:11:51 +03:00
|
|
|
RHASH_ST_TABLE_SET(tmp, identtable);
|
|
|
|
rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
|
|
|
|
st_free_table(RHASH_ST_TABLE(hash));
|
|
|
|
RHASH_ST_TABLE_SET(hash, identtable);
|
2018-12-14 04:10:15 +03:00
|
|
|
RHASH_ST_CLEAR(tmp);
|
2018-10-31 01:11:51 +03:00
|
|
|
rb_gc_force_recycle(tmp);
|
2017-10-22 03:57:46 +03:00
|
|
|
|
2006-09-11 12:09:19 +04:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-04 02:53:56 +03:00
|
|
|
* hash.compare_by_identity? -> true or false
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-06-04 02:53:56 +03:00
|
|
|
* Returns +true+ if #compare_by_identity has been called, +false+ otherwise:
|
|
|
|
* h = {}
|
|
|
|
* h.compare_by_identity? # false
|
|
|
|
* h.compare_by_identity
|
|
|
|
* h.compare_by_identity? # true
|
2006-09-11 12:09:19 +04:00
|
|
|
*/
|
|
|
|
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 14:22:28 +03:00
|
|
|
MJIT_FUNC_EXPORTED VALUE
|
2006-10-12 18:05:17 +04:00
|
|
|
rb_hash_compare_by_id_p(VALUE hash)
|
2006-09-11 12:09:19 +04:00
|
|
|
{
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_ST_TABLE_P(hash) && RHASH_ST_TABLE(hash)->type == &identhash) {
|
2006-09-11 12:09:19 +04:00
|
|
|
return Qtrue;
|
|
|
|
}
|
2018-10-31 01:11:51 +03:00
|
|
|
else {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2006-09-11 12:09:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-23 05:36:50 +03:00
|
|
|
VALUE
|
|
|
|
rb_ident_hash_new(void)
|
|
|
|
{
|
|
|
|
VALUE hash = rb_hash_new();
|
2018-10-31 01:11:51 +03:00
|
|
|
RHASH_ST_TABLE_SET(hash, st_init_table(&identhash));
|
2015-01-23 05:36:50 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2015-01-23 14:01:02 +03:00
|
|
|
st_table *
|
|
|
|
rb_init_identtable(void)
|
|
|
|
{
|
|
|
|
return st_init_table(&identhash);
|
|
|
|
}
|
|
|
|
|
2019-11-18 06:13:08 +03:00
|
|
|
static st_table *
|
2015-01-23 14:01:02 +03:00
|
|
|
rb_init_identtable_with_size(st_index_t size)
|
|
|
|
{
|
|
|
|
return st_init_table_with_size(&identhash, size);
|
|
|
|
}
|
|
|
|
|
2014-07-18 17:16:48 +04:00
|
|
|
static int
|
|
|
|
any_p_i(VALUE key, VALUE value, VALUE arg)
|
|
|
|
{
|
2017-10-02 10:51:27 +03:00
|
|
|
VALUE ret = rb_yield(rb_assoc_new(key, value));
|
|
|
|
if (RTEST(ret)) {
|
|
|
|
*(VALUE *)arg = Qtrue;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
any_p_i_fast(VALUE key, VALUE value, VALUE arg)
|
|
|
|
{
|
|
|
|
VALUE ret = rb_yield_values(2, key, value);
|
2014-07-18 17:16:48 +04:00
|
|
|
if (RTEST(ret)) {
|
|
|
|
*(VALUE *)arg = Qtrue;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-12-11 01:36:28 +03:00
|
|
|
static int
|
|
|
|
any_p_i_pattern(VALUE key, VALUE value, VALUE arg)
|
|
|
|
{
|
|
|
|
VALUE ret = rb_funcall(((VALUE *)arg)[1], idEqq, 1, rb_assoc_new(key, value));
|
|
|
|
if (RTEST(ret)) {
|
|
|
|
*(VALUE *)arg = Qtrue;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2014-07-18 17:16:48 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-06-04 02:53:56 +03:00
|
|
|
* hash.any? -> true or false
|
|
|
|
* hash.any?(object) -> true or false
|
|
|
|
* hash.any? {|key, value| ... } -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if any element satisfies a given criterion;
|
|
|
|
* +false+ otherwise.
|
|
|
|
*
|
|
|
|
* ---
|
2014-07-18 17:16:48 +04:00
|
|
|
*
|
2020-06-04 02:53:56 +03:00
|
|
|
* With no argument and no block,
|
|
|
|
* returns +true+ if +self+ is non-empty; +false+ if empty:
|
|
|
|
* {}.any? # => false
|
|
|
|
* {nil => false}.any? # => true
|
|
|
|
*
|
|
|
|
* With argument +object+ and no block,
|
|
|
|
* returns +true+ if for any key +key+
|
|
|
|
* <tt>h.assoc(key) == object</tt>:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.any?([:bar, 1]) # => true
|
|
|
|
* h.any?([:bar, 0]) # => false
|
|
|
|
* h.any?([:baz, 1]) # => false
|
|
|
|
*
|
|
|
|
* With no argument and a block,
|
|
|
|
* calls the block with each key-value pair;
|
|
|
|
* returns +true+ if the block returns any truthy value,
|
|
|
|
* +false+ otherwise:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.any? {|key, value| value < 3 } # => true
|
|
|
|
* h.any? {|key, value| value > 3 } # => false
|
|
|
|
*
|
|
|
|
* With argument +object+ and a block,
|
|
|
|
* issues a warning ('given block not used') and ignores the block:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.any?([:bar, 1]) # => true
|
|
|
|
* h.any?([:bar, 0]) # => false
|
|
|
|
* h.any?([:baz, 1]) # => false
|
2014-07-18 17:16:48 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
2017-12-11 01:36:28 +03:00
|
|
|
rb_hash_any_p(int argc, VALUE *argv, VALUE hash)
|
2014-07-18 17:16:48 +04:00
|
|
|
{
|
2017-12-11 01:36:28 +03:00
|
|
|
VALUE args[2];
|
|
|
|
args[0] = Qfalse;
|
2014-07-18 17:16:48 +04:00
|
|
|
|
2017-12-11 01:36:28 +03:00
|
|
|
rb_check_arity(argc, 0, 1);
|
2014-07-18 17:16:48 +04:00
|
|
|
if (RHASH_EMPTY_P(hash)) return Qfalse;
|
2017-12-11 01:36:28 +03:00
|
|
|
if (argc) {
|
2018-09-13 14:10:24 +03:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
rb_warn("given block not used");
|
|
|
|
}
|
2017-12-11 01:36:28 +03:00
|
|
|
args[1] = argv[0];
|
|
|
|
|
|
|
|
rb_hash_foreach(hash, any_p_i_pattern, (VALUE)args);
|
2014-07-18 17:16:48 +04:00
|
|
|
}
|
2017-12-11 01:36:28 +03:00
|
|
|
else {
|
|
|
|
if (!rb_block_given_p()) {
|
|
|
|
/* yields pairs, never false */
|
|
|
|
return Qtrue;
|
|
|
|
}
|
2020-03-16 17:03:22 +03:00
|
|
|
if (rb_block_pair_yield_optimizable())
|
2017-12-11 01:36:28 +03:00
|
|
|
rb_hash_foreach(hash, any_p_i_fast, (VALUE)args);
|
|
|
|
else
|
|
|
|
rb_hash_foreach(hash, any_p_i, (VALUE)args);
|
|
|
|
}
|
|
|
|
return args[0];
|
2014-07-18 17:16:48 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 15:27:26 +03:00
|
|
|
/*
|
2020-06-04 02:53:56 +03:00
|
|
|
* call-seq:
|
2020-08-13 21:16:27 +03:00
|
|
|
* hash.dig(key, *identifiers) -> object
|
2020-06-04 02:53:56 +03:00
|
|
|
*
|
2020-08-13 21:16:27 +03:00
|
|
|
* Finds and returns the object in nested objects
|
|
|
|
* that is specified by +key+ and +identifiers+.
|
|
|
|
* The nested objects may be instances of various classes.
|
2020-08-15 02:55:04 +03:00
|
|
|
* See {Dig Methods}[rdoc-ref:doc/dig_methods.rdoc].
|
2015-11-09 15:27:26 +03:00
|
|
|
*
|
2020-06-04 02:53:56 +03:00
|
|
|
* Examples:
|
|
|
|
* h = {foo: {bar: {baz: 2}}}
|
2020-08-13 21:16:27 +03:00
|
|
|
* h.dig(:foo) # => {:bar=>{:baz=>2}}
|
|
|
|
* h.dig(:foo, :bar) # => {:bar=>{:baz=>2}}
|
2020-06-04 02:53:56 +03:00
|
|
|
* h.dig(:foo, :bar, :baz) # => 2
|
2020-08-13 21:16:27 +03:00
|
|
|
* h.dig(:foo, :bar, :BAZ) # => nil
|
2020-07-29 21:45:16 +03:00
|
|
|
*
|
|
|
|
* The nested objects may include any that respond to \#dig. See:
|
|
|
|
* - Hash#dig
|
|
|
|
* - Array#dig
|
|
|
|
* - Struct#dig
|
|
|
|
* - OpenStruct#dig
|
|
|
|
* - CSV::Table#dig
|
|
|
|
* - CSV::Row#dig
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* h = {foo: {bar: [:a, :b, :c]}}
|
|
|
|
* h.dig(:foo, :bar, 2) # => :c
|
2020-07-29 23:04:55 +03:00
|
|
|
*
|
|
|
|
* This method will use the {default values}[#class-Hash-label-Default+Values]
|
|
|
|
* for keys that are not present:
|
|
|
|
* h = {foo: {bar: [:a, :b, :c]}}
|
|
|
|
* h.dig(:hello) # => nil
|
|
|
|
* h.default_proc = -> (hash, _key) { hash }
|
|
|
|
* h.dig(:hello, :world) # => h
|
|
|
|
* h.dig(:hello, :world, :foo, :bar, 2) # => :c
|
2015-11-09 15:27:26 +03:00
|
|
|
*/
|
|
|
|
|
2018-05-08 12:09:12 +03:00
|
|
|
static VALUE
|
2015-11-09 15:27:26 +03:00
|
|
|
rb_hash_dig(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
|
|
|
|
self = rb_hash_aref(self, *argv);
|
|
|
|
if (!--argc) return self;
|
|
|
|
++argv;
|
|
|
|
return rb_obj_dig(argc, argv, self, Qnil);
|
|
|
|
}
|
|
|
|
|
2015-11-10 08:02:02 +03:00
|
|
|
static int
|
|
|
|
hash_le_i(VALUE key, VALUE value, VALUE arg)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)arg;
|
|
|
|
VALUE v = rb_hash_lookup2(args[0], key, Qundef);
|
|
|
|
if (v != Qundef && rb_equal(value, v)) return ST_CONTINUE;
|
|
|
|
args[1] = Qfalse;
|
|
|
|
return ST_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
hash_le(VALUE hash1, VALUE hash2)
|
|
|
|
{
|
|
|
|
VALUE args[2];
|
|
|
|
args[0] = hash2;
|
|
|
|
args[1] = Qtrue;
|
|
|
|
rb_hash_foreach(hash1, hash_le_i, (VALUE)args);
|
|
|
|
return args[1];
|
|
|
|
}
|
|
|
|
|
2015-11-14 11:13:11 +03:00
|
|
|
/*
|
2020-06-07 21:46:43 +03:00
|
|
|
* call-seq:
|
|
|
|
* hash <= other_hash -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +hash+ is a subset of +other_hash+, +false+ otherwise:
|
|
|
|
* h1 = {foo: 0, bar: 1}
|
|
|
|
* h2 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 <= h2 # => true
|
|
|
|
* h2 <= h1 # => false
|
|
|
|
* h1 <= h1 # => true
|
2015-11-14 11:13:11 +03:00
|
|
|
*/
|
2015-11-10 08:02:02 +03:00
|
|
|
static VALUE
|
|
|
|
rb_hash_le(VALUE hash, VALUE other)
|
|
|
|
{
|
|
|
|
other = to_hash(other);
|
|
|
|
if (RHASH_SIZE(hash) > RHASH_SIZE(other)) return Qfalse;
|
|
|
|
return hash_le(hash, other);
|
|
|
|
}
|
|
|
|
|
2015-11-14 11:13:11 +03:00
|
|
|
/*
|
2020-06-07 21:46:43 +03:00
|
|
|
* call-seq:
|
|
|
|
* hash < other_hash -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +hash+ is a proper subset of +other_hash+, +false+ otherwise:
|
|
|
|
* h1 = {foo: 0, bar: 1}
|
|
|
|
* h2 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h1 < h2 # => true
|
|
|
|
* h2 < h1 # => false
|
|
|
|
* h1 < h1 # => false
|
2015-11-14 11:13:11 +03:00
|
|
|
*/
|
2015-11-10 08:02:02 +03:00
|
|
|
static VALUE
|
|
|
|
rb_hash_lt(VALUE hash, VALUE other)
|
|
|
|
{
|
|
|
|
other = to_hash(other);
|
|
|
|
if (RHASH_SIZE(hash) >= RHASH_SIZE(other)) return Qfalse;
|
|
|
|
return hash_le(hash, other);
|
|
|
|
}
|
|
|
|
|
2015-11-14 11:13:11 +03:00
|
|
|
/*
|
2020-06-07 21:46:43 +03:00
|
|
|
* call-seq:
|
|
|
|
* hash >= other_hash -> true or false
|
2015-11-14 11:13:11 +03:00
|
|
|
*
|
2020-06-07 21:46:43 +03:00
|
|
|
* Returns +true+ if +hash+ is a superset of +other_hash+, +false+ otherwise:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {foo: 0, bar: 1}
|
|
|
|
* h1 >= h2 # => true
|
|
|
|
* h2 >= h1 # => false
|
|
|
|
* h1 >= h1 # => true
|
2015-11-14 11:13:11 +03:00
|
|
|
*/
|
2015-11-10 08:02:02 +03:00
|
|
|
static VALUE
|
|
|
|
rb_hash_ge(VALUE hash, VALUE other)
|
|
|
|
{
|
|
|
|
other = to_hash(other);
|
|
|
|
if (RHASH_SIZE(hash) < RHASH_SIZE(other)) return Qfalse;
|
|
|
|
return hash_le(other, hash);
|
|
|
|
}
|
|
|
|
|
2015-11-14 11:13:11 +03:00
|
|
|
/*
|
2020-06-07 21:46:43 +03:00
|
|
|
* call-seq:
|
|
|
|
* hash > other_hash -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if +hash+ is a proper superset of +other_hash+, +false+ otherwise:
|
|
|
|
* h1 = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h2 = {foo: 0, bar: 1}
|
|
|
|
* h1 > h2 # => true
|
|
|
|
* h2 > h1 # => false
|
|
|
|
* h1 > h1 # => false
|
2015-11-14 11:13:11 +03:00
|
|
|
*/
|
2015-11-10 08:02:02 +03:00
|
|
|
static VALUE
|
|
|
|
rb_hash_gt(VALUE hash, VALUE other)
|
|
|
|
{
|
|
|
|
other = to_hash(other);
|
|
|
|
if (RHASH_SIZE(hash) <= RHASH_SIZE(other)) return Qfalse;
|
|
|
|
return hash_le(other, hash);
|
|
|
|
}
|
|
|
|
|
2015-11-10 10:57:17 +03:00
|
|
|
static VALUE
|
2019-09-27 03:25:54 +03:00
|
|
|
hash_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(key, hash))
|
2015-11-10 10:57:17 +03:00
|
|
|
{
|
2015-11-10 12:25:58 +03:00
|
|
|
rb_check_arity(argc, 1, 1);
|
|
|
|
return rb_hash_aref(hash, *argv);
|
2015-11-10 10:57:17 +03:00
|
|
|
}
|
|
|
|
|
2018-02-23 05:18:52 +03:00
|
|
|
/*
|
2020-06-07 21:46:43 +03:00
|
|
|
* call-seq:
|
|
|
|
* hash.to_proc -> proc
|
2018-02-23 05:18:52 +03:00
|
|
|
*
|
2020-06-07 21:46:43 +03:00
|
|
|
* Returns a \Proc object that maps a key to its value:
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* proc = h.to_proc
|
|
|
|
* proc.class # => Proc
|
|
|
|
* proc.call(:foo) # => 0
|
|
|
|
* proc.call(:bar) # => 1
|
|
|
|
* proc.call(:nosuch) # => nil
|
2018-02-23 05:18:52 +03:00
|
|
|
*/
|
2015-11-10 10:57:17 +03:00
|
|
|
static VALUE
|
|
|
|
rb_hash_to_proc(VALUE hash)
|
|
|
|
{
|
2020-07-28 21:39:27 +03:00
|
|
|
return rb_func_lambda_new(hash_proc_call, hash, 1, 1);
|
2015-11-10 10:57:17 +03:00
|
|
|
}
|
|
|
|
|
2019-04-17 09:48:03 +03:00
|
|
|
static VALUE
|
|
|
|
rb_hash_deconstruct_keys(VALUE hash, VALUE keys)
|
|
|
|
{
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2016-07-20 11:35:25 +03:00
|
|
|
static int
|
|
|
|
add_new_i(st_data_t *key, st_data_t *val, st_data_t arg, int existing)
|
|
|
|
{
|
|
|
|
VALUE *args = (VALUE *)arg;
|
|
|
|
if (existing) return ST_STOP;
|
|
|
|
RB_OBJ_WRITTEN(args[0], Qundef, (VALUE)*key);
|
|
|
|
RB_OBJ_WRITE(args[0], (VALUE *)val, args[1]);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add +key+ to +val+ pair if +hash+ does not contain +key+.
|
|
|
|
* returns non-zero if +key+ was contained.
|
|
|
|
*/
|
|
|
|
int
|
2018-10-31 05:06:33 +03:00
|
|
|
rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val)
|
2016-07-20 11:35:25 +03:00
|
|
|
{
|
2018-10-31 01:11:51 +03:00
|
|
|
st_table *tbl;
|
|
|
|
int ret = 0;
|
2016-07-20 11:35:25 +03:00
|
|
|
VALUE args[2];
|
|
|
|
args[0] = hash;
|
|
|
|
args[1] = val;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
if (RHASH_AR_TABLE_P(hash)) {
|
|
|
|
hash_ar_table(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 04:10:15 +03:00
|
|
|
ret = ar_update(hash, (st_data_t)key, add_new_i, (st_data_t)args);
|
2018-10-31 01:11:51 +03:00
|
|
|
if (ret != -1) {
|
2018-10-31 01:12:12 +03:00
|
|
|
return ret;
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_try_convert_table(hash);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
tbl = RHASH_TBL_RAW(hash);
|
2016-07-20 11:35:25 +03:00
|
|
|
return st_update(tbl, (st_data_t)key, add_new_i, (st_data_t)args);
|
2018-10-31 01:11:51 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static st_data_t
|
|
|
|
key_stringify(VALUE key)
|
|
|
|
{
|
|
|
|
return (rb_obj_class(key) == rb_cString && !RB_OBJ_FROZEN(key)) ?
|
|
|
|
rb_hash_key_str(key) : key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_bulk_insert(VALUE hash, long argc, const VALUE *argv)
|
2018-10-31 01:11:51 +03:00
|
|
|
{
|
|
|
|
long i;
|
|
|
|
for (i = 0; i < argc; ) {
|
2018-10-31 01:12:12 +03:00
|
|
|
st_data_t k = key_stringify(argv[i++]);
|
|
|
|
st_data_t v = argv[i++];
|
2018-12-14 04:10:15 +03:00
|
|
|
ar_insert(hash, k, v);
|
2018-10-31 01:12:12 +03:00
|
|
|
RB_OBJ_WRITTEN(hash, Qundef, k);
|
|
|
|
RB_OBJ_WRITTEN(hash, Qundef, v);
|
2018-10-31 01:11:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 11:34:37 +03:00
|
|
|
void
|
2018-10-31 01:11:51 +03:00
|
|
|
rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
|
|
|
|
{
|
|
|
|
HASH_ASSERT(argc % 2 == 0);
|
2018-12-14 05:27:23 +03:00
|
|
|
if (argc > 0) {
|
|
|
|
st_index_t size = argc / 2;
|
2018-10-31 01:11:51 +03:00
|
|
|
|
2018-12-14 05:27:23 +03:00
|
|
|
if (RHASH_TABLE_NULL_P(hash)) {
|
|
|
|
if (size <= RHASH_AR_TABLE_MAX_SIZE) {
|
|
|
|
hash_ar_table(hash);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RHASH_TBL_RAW(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RHASH_AR_TABLE_P(hash) &&
|
|
|
|
(RHASH_AR_TABLE_SIZE(hash) + size <= RHASH_AR_TABLE_MAX_SIZE)) {
|
|
|
|
ar_bulk_insert(hash, argc, argv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_hash_bulk_insert_into_st_table(argc, argv, hash);
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 11:35:25 +03:00
|
|
|
}
|
|
|
|
|
2001-11-13 06:59:20 +03:00
|
|
|
static char **origenviron;
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#ifdef _WIN32
|
2010-12-15 01:18:39 +03:00
|
|
|
#define GET_ENVIRON(e) ((e) = rb_w32_get_environ())
|
2002-05-29 14:22:19 +04:00
|
|
|
#define FREE_ENVIRON(e) rb_w32_free_environ(e)
|
2001-11-13 06:59:20 +03:00
|
|
|
static char **my_environ;
|
|
|
|
#undef environ
|
|
|
|
#define environ my_environ
|
2012-03-15 07:57:02 +04:00
|
|
|
#undef getenv
|
2016-08-05 11:04:04 +03:00
|
|
|
static char *(*w32_getenv)(const char*);
|
|
|
|
static char *
|
|
|
|
w32_getenv_unknown(const char *name)
|
2013-12-01 19:55:56 +04:00
|
|
|
{
|
2016-08-05 11:04:04 +03:00
|
|
|
char *(*func)(const char*);
|
|
|
|
if (rb_locale_encindex() == rb_ascii8bit_encindex()) {
|
|
|
|
func = rb_w32_getenv;
|
2013-12-01 19:55:56 +04:00
|
|
|
}
|
2016-08-05 11:04:04 +03:00
|
|
|
else {
|
|
|
|
func = rb_w32_ugetenv;
|
|
|
|
}
|
|
|
|
/* atomic assignment in flat memory model */
|
|
|
|
return (w32_getenv = func)(name);
|
2013-12-01 19:55:56 +04:00
|
|
|
}
|
2016-08-05 11:04:04 +03:00
|
|
|
static char *(*w32_getenv)(const char*) = w32_getenv_unknown;
|
2013-12-02 17:31:14 +04:00
|
|
|
#define getenv(n) w32_getenv(n)
|
2002-10-17 20:13:44 +04:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#undef environ
|
|
|
|
#define environ (*_NSGetEnviron())
|
|
|
|
#define GET_ENVIRON(e) (e)
|
|
|
|
#define FREE_ENVIRON(e)
|
2001-11-13 06:59:20 +03:00
|
|
|
#else
|
1998-01-16 15:13:05 +03:00
|
|
|
extern char **environ;
|
2001-11-13 06:59:20 +03:00
|
|
|
#define GET_ENVIRON(e) (e)
|
|
|
|
#define FREE_ENVIRON(e)
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
2008-09-23 11:59:48 +04:00
|
|
|
#ifdef ENV_IGNORECASE
|
2010-12-15 01:18:39 +03:00
|
|
|
#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
|
|
|
|
#define ENVNMATCH(s1, s2, n) (STRNCASECMP((s1), (s2), (n)) == 0)
|
2008-09-23 11:59:48 +04:00
|
|
|
#else
|
2010-12-15 01:18:39 +03:00
|
|
|
#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
|
|
|
|
#define ENVNMATCH(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
|
2008-09-23 11:59:48 +04:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2003-06-06 13:24:59 +04:00
|
|
|
static VALUE
|
2016-08-05 18:39:46 +03:00
|
|
|
env_enc_str_new(const char *ptr, long len, rb_encoding *enc)
|
2003-06-06 13:24:59 +04:00
|
|
|
{
|
2012-03-15 07:57:02 +04:00
|
|
|
#ifdef _WIN32
|
2017-07-30 05:56:30 +03:00
|
|
|
rb_encoding *internal = rb_default_internal_encoding();
|
|
|
|
const int ecflags = ECONV_INVALID_REPLACE | ECONV_UNDEF_REPLACE;
|
|
|
|
rb_encoding *utf8 = rb_utf8_encoding();
|
|
|
|
VALUE str = rb_enc_str_new(NULL, 0, (internal ? internal : enc));
|
|
|
|
if (NIL_P(rb_str_cat_conv_enc_opts(str, 0, ptr, len, utf8, ecflags, Qnil))) {
|
2018-09-26 20:24:00 +03:00
|
|
|
rb_str_initialize(str, ptr, len, NULL);
|
2017-07-30 05:56:30 +03:00
|
|
|
}
|
2012-03-15 07:57:02 +04:00
|
|
|
#else
|
2016-08-05 18:39:46 +03:00
|
|
|
VALUE str = rb_external_str_new_with_enc(ptr, len, enc);
|
2012-03-15 07:57:02 +04:00
|
|
|
#endif
|
2003-06-06 13:24:59 +04:00
|
|
|
|
|
|
|
rb_obj_freeze(str);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2014-06-24 05:50:09 +04:00
|
|
|
static VALUE
|
2016-08-05 18:39:46 +03:00
|
|
|
env_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
|
2014-06-24 05:50:09 +04:00
|
|
|
{
|
2016-08-05 18:39:46 +03:00
|
|
|
return env_enc_str_new(ptr, strlen(ptr), enc);
|
|
|
|
}
|
2014-06-24 05:50:09 +04:00
|
|
|
|
2016-08-05 18:39:46 +03:00
|
|
|
static VALUE
|
|
|
|
env_str_new(const char *ptr, long len)
|
|
|
|
{
|
|
|
|
return env_enc_str_new(ptr, len, rb_locale_encoding());
|
2014-06-24 05:50:09 +04:00
|
|
|
}
|
|
|
|
|
2003-06-06 13:24:59 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_str_new2(const char *ptr)
|
2003-06-06 13:24:59 +04:00
|
|
|
{
|
2003-06-23 10:52:39 +04:00
|
|
|
if (!ptr) return Qnil;
|
2003-06-06 13:24:59 +04:00
|
|
|
return env_str_new(ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
2018-07-18 13:30:41 +03:00
|
|
|
static const char TZ_ENV[] = "TZ";
|
|
|
|
|
2016-08-05 18:39:46 +03:00
|
|
|
static rb_encoding *
|
|
|
|
env_encoding_for(const char *name, const char *ptr)
|
|
|
|
{
|
2019-09-25 06:59:12 +03:00
|
|
|
if (ENVMATCH(name, PATH_ENV)) {
|
2016-08-05 18:39:46 +03:00
|
|
|
return rb_filesystem_encoding();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_locale_encoding();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
env_name_new(const char *name, const char *ptr)
|
|
|
|
{
|
|
|
|
return env_enc_str_new_cstr(ptr, env_encoding_for(name, ptr));
|
|
|
|
}
|
|
|
|
|
2015-04-19 04:42:57 +03:00
|
|
|
static void *
|
2015-12-05 11:26:26 +03:00
|
|
|
get_env_cstr(
|
|
|
|
#ifdef _WIN32
|
|
|
|
volatile VALUE *pstr,
|
|
|
|
#else
|
|
|
|
VALUE str,
|
|
|
|
#endif
|
|
|
|
const char *name)
|
2015-04-19 04:42:57 +03:00
|
|
|
{
|
2015-12-05 11:26:26 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
VALUE str = *pstr;
|
|
|
|
#endif
|
2015-04-19 04:42:57 +03:00
|
|
|
char *var;
|
|
|
|
rb_encoding *enc = rb_enc_get(str);
|
|
|
|
if (!rb_enc_asciicompat(enc)) {
|
|
|
|
rb_raise(rb_eArgError, "bad environment variable %s: ASCII incompatible encoding: %s",
|
|
|
|
name, rb_enc_name(enc));
|
|
|
|
}
|
2015-12-05 11:26:26 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (!rb_enc_str_asciionly_p(str)) {
|
|
|
|
*pstr = str = rb_str_conv_enc(str, NULL, rb_utf8_encoding());
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-19 04:42:57 +03:00
|
|
|
var = RSTRING_PTR(str);
|
|
|
|
if (memchr(var, '\0', RSTRING_LEN(str))) {
|
|
|
|
rb_raise(rb_eArgError, "bad environment variable %s: contains null byte", name);
|
|
|
|
}
|
2016-06-10 08:48:38 +03:00
|
|
|
return rb_str_fill_terminator(str, 1); /* ASCII compatible */
|
2015-04-19 04:42:57 +03:00
|
|
|
}
|
|
|
|
|
2015-12-05 11:26:26 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define get_env_ptr(var, val) \
|
|
|
|
(var = get_env_cstr(&(val), #var))
|
|
|
|
#else
|
2014-06-25 16:18:53 +04:00
|
|
|
#define get_env_ptr(var, val) \
|
2015-04-19 04:42:57 +03:00
|
|
|
(var = get_env_cstr(val, #var))
|
2015-12-05 11:26:26 +03:00
|
|
|
#endif
|
2014-06-25 16:18:53 +04:00
|
|
|
|
|
|
|
static inline const char *
|
2014-06-26 02:43:23 +04:00
|
|
|
env_name(volatile VALUE *s)
|
2014-06-25 16:18:53 +04:00
|
|
|
{
|
|
|
|
const char *name;
|
2014-06-26 02:43:23 +04:00
|
|
|
SafeStringValue(*s);
|
|
|
|
get_env_ptr(name, *s);
|
2014-06-25 16:18:53 +04:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2014-06-26 02:43:23 +04:00
|
|
|
#define env_name(s) env_name(&(s))
|
|
|
|
|
2019-02-27 06:26:05 +03:00
|
|
|
static VALUE env_aset(VALUE nm, VALUE val);
|
|
|
|
|
2020-04-18 14:58:35 +03:00
|
|
|
static void
|
|
|
|
reset_by_modified_env(const char *nam)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2019-12-01 19:21:05 +03:00
|
|
|
/*
|
|
|
|
* ENV['TZ'] = nil has a special meaning.
|
|
|
|
* TZ is no longer considered up-to-date and ruby call tzset() as needed.
|
|
|
|
* It could be useful if sysadmin change /etc/localtime.
|
|
|
|
* This hack might works only on Linux glibc.
|
|
|
|
*/
|
|
|
|
if (ENVMATCH(nam, TZ_ENV)) {
|
2020-01-28 17:40:25 +03:00
|
|
|
ruby_reset_timezone();
|
2019-12-01 19:21:05 +03:00
|
|
|
}
|
2020-04-18 14:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
env_delete(VALUE name)
|
|
|
|
{
|
|
|
|
const char *nam = env_name(name);
|
|
|
|
const char *val = getenv(nam);
|
|
|
|
|
|
|
|
reset_by_modified_env(nam);
|
2019-12-01 19:21:05 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (val) {
|
2003-06-06 13:24:59 +04:00
|
|
|
VALUE value = env_str_new2(val);
|
2000-01-05 07:41:21 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
ruby_setenv(nam, 0);
|
2008-09-23 11:59:48 +04:00
|
|
|
if (ENVMATCH(nam, PATH_ENV)) {
|
2015-09-25 10:08:45 +03:00
|
|
|
RB_GC_GUARD(name);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2000-01-05 07:41:21 +03:00
|
|
|
return value;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-10-01 06:29:48 +03:00
|
|
|
* ENV.delete(name) -> value
|
|
|
|
* ENV.delete(name) { |name| block } -> value
|
|
|
|
* ENV.delete(missing_name) -> nil
|
|
|
|
* ENV.delete(missing_name) { |name| block } -> block_value
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2019-10-13 03:48:20 +03:00
|
|
|
* Deletes the environment variable with +name+ if it exists and returns its value:
|
|
|
|
* ENV['foo'] = '0'
|
|
|
|
* ENV.delete('foo') # => '0'
|
2020-08-21 19:42:02 +03:00
|
|
|
*
|
|
|
|
* If a block is not given and the named environment variable does not exist, returns +nil+.
|
|
|
|
*
|
2019-10-13 03:48:20 +03:00
|
|
|
* If a block given and the environment variable does not exist,
|
2019-10-01 06:29:48 +03:00
|
|
|
* yields +name+ to the block and returns the value of the block:
|
|
|
|
* ENV.delete('foo') { |name| name * 2 } # => "foofoo"
|
2020-08-21 19:42:02 +03:00
|
|
|
*
|
2019-10-13 03:48:20 +03:00
|
|
|
* If a block given and the environment variable exists,
|
|
|
|
* deletes the environment variable and returns its value (ignoring the block):
|
|
|
|
* ENV['foo'] = '0'
|
2020-06-10 14:45:29 +03:00
|
|
|
* ENV.delete('foo') { |name| raise 'ignored' } # => "0"
|
2020-08-21 19:42:02 +03:00
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* Raises an exception if +name+ is invalid.
|
2020-02-22 04:25:54 +03:00
|
|
|
* See {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values].
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_delete_m(VALUE obj, VALUE name)
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
2003-03-31 06:06:23 +04:00
|
|
|
VALUE val;
|
|
|
|
|
2019-02-27 06:26:05 +03:00
|
|
|
val = env_delete(name);
|
2019-10-01 06:29:48 +03:00
|
|
|
if (NIL_P(val) && rb_block_given_p()) val = rb_yield(name);
|
1998-01-16 15:19:22 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ENV[name] -> value
|
|
|
|
*
|
2019-10-20 20:28:22 +03:00
|
|
|
* Returns the value for the environment variable +name+ if it exists:
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV['foo'] = '0'
|
|
|
|
* ENV['foo'] # => "0"
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if the named variable does not exist.
|
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* Raises an exception if +name+ is invalid.
|
2020-02-22 04:25:54 +03:00
|
|
|
* See {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values].
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_f_getenv(VALUE obj, VALUE name)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2014-06-25 16:18:53 +04:00
|
|
|
const char *nam, *env;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2014-06-25 16:18:53 +04:00
|
|
|
nam = env_name(name);
|
1999-01-20 07:59:39 +03:00
|
|
|
env = getenv(nam);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (env) {
|
2016-08-05 18:39:46 +03:00
|
|
|
return env_name_new(nam, env);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-10-20 20:28:22 +03:00
|
|
|
* ENV.fetch(name) -> value
|
|
|
|
* ENV.fetch(name, default) -> value
|
|
|
|
* ENV.fetch(name) { |name| block } -> value
|
|
|
|
*
|
|
|
|
* If +name+ is the name of an environment variable, returns its value:
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV['foo'] = '0'
|
|
|
|
* ENV.fetch('foo') # => '0'
|
2019-10-20 20:28:22 +03:00
|
|
|
* Otherwise if a block is given (but not a default value),
|
|
|
|
* yields +name+ to the block and returns the block's return value:
|
|
|
|
* ENV.fetch('foo') { |name| :need_not_return_a_string } # => :need_not_return_a_string
|
|
|
|
* Otherwise if a default value is given (but not a block), returns the default value:
|
|
|
|
* ENV.delete('foo')
|
|
|
|
* ENV.fetch('foo', :default_need_not_be_a_string) # => :default_need_not_be_a_string
|
|
|
|
* If the environment variable does not exist and both default and block are given,
|
2019-10-24 02:04:45 +03:00
|
|
|
* issues a warning ("warning: block supersedes default value argument"),
|
2019-10-20 20:28:22 +03:00
|
|
|
* yields +name+ to the block, and returns the block's return value:
|
|
|
|
* ENV.fetch('foo', :default) { |name| :block_return } # => :block_return
|
2019-12-13 22:46:54 +03:00
|
|
|
* Raises KeyError if +name+ is valid, but not found,
|
2019-10-20 20:28:22 +03:00
|
|
|
* and neither default value nor block is given:
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.fetch('foo') # Raises KeyError (key not found: "foo")
|
|
|
|
* Raises an exception if +name+ is invalid.
|
2020-02-22 04:25:54 +03:00
|
|
|
* See {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values].
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2000-04-12 09:06:23 +04:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_fetch(int argc, VALUE *argv, VALUE _)
|
2000-04-12 09:06:23 +04:00
|
|
|
{
|
2015-12-06 15:13:25 +03:00
|
|
|
VALUE key;
|
2003-11-06 10:22:39 +03:00
|
|
|
long block_given;
|
2014-06-25 16:18:53 +04:00
|
|
|
const char *nam, *env;
|
2000-04-12 09:06:23 +04:00
|
|
|
|
2014-07-02 12:25:21 +04:00
|
|
|
rb_check_arity(argc, 1, 2);
|
|
|
|
key = argv[0];
|
2003-11-06 10:22:39 +03:00
|
|
|
block_given = rb_block_given_p();
|
|
|
|
if (block_given && argc == 2) {
|
|
|
|
rb_warn("block supersedes default value argument");
|
|
|
|
}
|
2015-12-06 15:13:25 +03:00
|
|
|
nam = env_name(key);
|
2000-04-12 09:06:23 +04:00
|
|
|
env = getenv(nam);
|
|
|
|
if (!env) {
|
2003-11-06 10:22:39 +03:00
|
|
|
if (block_given) return rb_yield(key);
|
2000-04-12 09:06:23 +04:00
|
|
|
if (argc == 1) {
|
2017-09-18 11:05:53 +03:00
|
|
|
rb_key_err_raise(rb_sprintf("key not found: \"%"PRIsVALUE"\"", key), envtbl, key);
|
2000-04-12 09:06:23 +04:00
|
|
|
}
|
2014-07-02 12:25:21 +04:00
|
|
|
return argv[1];
|
2000-04-12 09:06:23 +04:00
|
|
|
}
|
2016-08-05 18:39:46 +03:00
|
|
|
return env_name_new(nam, env);
|
2000-04-12 09:06:23 +04:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_env_path_tainted(void)
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
2020-01-23 15:42:05 +03:00
|
|
|
rb_warn_deprecated_to_remove("rb_env_path_tainted", "3.2");
|
2019-09-25 06:59:12 +03:00
|
|
|
return 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
2009-11-14 10:43:26 +03:00
|
|
|
#if defined(_WIN32) || (defined(HAVE_SETENV) && defined(HAVE_UNSETENV))
|
2011-10-24 18:57:08 +04:00
|
|
|
#elif defined __sun
|
2009-11-14 10:43:26 +03:00
|
|
|
static int
|
|
|
|
in_origenv(const char *str)
|
|
|
|
{
|
|
|
|
char **env;
|
|
|
|
for (env = origenviron; *env; ++env) {
|
|
|
|
if (*env == str) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
1999-01-20 07:59:39 +03:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
envix(const char *nam)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
register int i, len = strlen(nam);
|
2001-11-13 06:59:20 +03:00
|
|
|
char **env;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
|
|
|
for (i = 0; env[i]; i++) {
|
2008-09-23 11:59:48 +04:00
|
|
|
if (ENVNMATCH(env[i],nam,len) && env[i][len] == '=')
|
1999-01-20 07:59:39 +03:00
|
|
|
break; /* memcmp must come first to avoid */
|
|
|
|
} /* potential SEGV's */
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1999-01-20 07:59:39 +03:00
|
|
|
return i;
|
|
|
|
}
|
2006-08-29 14:11:27 +04:00
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2011-03-19 12:28:49 +03:00
|
|
|
#if defined(_WIN32)
|
2011-03-19 19:33:59 +03:00
|
|
|
static size_t
|
2015-12-05 11:26:26 +03:00
|
|
|
getenvsize(const WCHAR* p)
|
2011-03-19 12:28:49 +03:00
|
|
|
{
|
2015-12-05 11:26:26 +03:00
|
|
|
const WCHAR* porg = p;
|
|
|
|
while (*p++) p += lstrlenW(p) + 1;
|
2011-03-19 13:00:51 +03:00
|
|
|
return p - porg + 1;
|
2011-03-19 12:28:49 +03:00
|
|
|
}
|
2018-08-11 16:18:55 +03:00
|
|
|
|
2011-03-19 19:33:59 +03:00
|
|
|
static size_t
|
2014-09-30 09:25:32 +04:00
|
|
|
getenvblocksize(void)
|
2011-03-19 19:33:59 +03:00
|
|
|
{
|
2018-08-11 16:18:55 +03:00
|
|
|
#ifdef _MAX_ENV
|
|
|
|
return _MAX_ENV;
|
|
|
|
#else
|
2016-05-01 14:42:41 +03:00
|
|
|
return 32767;
|
2018-08-11 16:18:55 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_envsize(size_t n)
|
|
|
|
{
|
|
|
|
if (_WIN32_WINNT < 0x0600 && rb_w32_osver() < 6) {
|
|
|
|
/* https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx */
|
|
|
|
/* Windows Server 2003 and Windows XP: The maximum size of the
|
|
|
|
* environment block for the process is 32,767 characters. */
|
|
|
|
WCHAR* p = GetEnvironmentStringsW();
|
|
|
|
if (!p) return -1; /* never happen */
|
|
|
|
n += getenvsize(p);
|
|
|
|
FreeEnvironmentStringsW(p);
|
|
|
|
if (n >= getenvblocksize()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2011-03-19 19:33:59 +03:00
|
|
|
}
|
2014-07-10 19:22:07 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN32) || \
|
|
|
|
(defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV)))
|
2011-03-19 12:28:49 +03:00
|
|
|
|
2013-09-13 11:28:25 +04:00
|
|
|
NORETURN(static void invalid_envname(const char *name));
|
|
|
|
|
|
|
|
static void
|
|
|
|
invalid_envname(const char *name)
|
|
|
|
{
|
|
|
|
rb_syserr_fail_str(EINVAL, rb_sprintf("ruby_setenv(%s)", name));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
check_envname(const char *name)
|
|
|
|
{
|
|
|
|
if (strchr(name, '=')) {
|
|
|
|
invalid_envname(name);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
ruby_setenv(const char *name, const char *value)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2003-06-20 10:22:50 +04:00
|
|
|
#if defined(_WIN32)
|
2015-12-05 11:26:26 +03:00
|
|
|
# if defined(MINGW_HAS_SECURE_API) || RUBY_MSVCRT_VERSION >= 80
|
|
|
|
# define HAVE__WPUTENV_S 1
|
|
|
|
# endif
|
2010-09-11 11:47:44 +04:00
|
|
|
VALUE buf;
|
2015-12-05 11:26:26 +03:00
|
|
|
WCHAR *wname;
|
|
|
|
WCHAR *wvalue = 0;
|
2010-09-11 11:47:44 +04:00
|
|
|
int failed = 0;
|
2015-12-05 11:26:26 +03:00
|
|
|
int len;
|
2013-09-13 11:28:25 +04:00
|
|
|
check_envname(name);
|
2015-12-05 11:26:26 +03:00
|
|
|
len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
|
2009-11-11 06:20:21 +03:00
|
|
|
if (value) {
|
2015-12-05 11:26:26 +03:00
|
|
|
int len2;
|
2018-08-11 16:18:55 +03:00
|
|
|
len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
|
|
|
|
if (check_envsize((size_t)len + len2)) { /* len and len2 include '\0' */
|
2011-03-19 19:33:59 +03:00
|
|
|
goto fail; /* 2 for '=' & '\0' */
|
2011-03-19 11:10:08 +03:00
|
|
|
}
|
2015-12-05 11:26:26 +03:00
|
|
|
wname = ALLOCV_N(WCHAR, buf, len + len2);
|
|
|
|
wvalue = wname + len;
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, value, -1, wvalue, len2);
|
|
|
|
#ifndef HAVE__WPUTENV_S
|
|
|
|
wname[len-1] = L'=';
|
|
|
|
#endif
|
2009-11-11 06:20:21 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-12-05 11:26:26 +03:00
|
|
|
wname = ALLOCV_N(WCHAR, buf, len + 1);
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);
|
|
|
|
wvalue = wname + len;
|
|
|
|
*wvalue = L'\0';
|
|
|
|
#ifndef HAVE__WPUTENV_S
|
|
|
|
wname[len-1] = L'=';
|
|
|
|
#endif
|
2010-09-11 11:47:44 +04:00
|
|
|
}
|
2015-12-05 11:26:26 +03:00
|
|
|
#ifndef HAVE__WPUTENV_S
|
|
|
|
failed = _wputenv(wname);
|
|
|
|
#else
|
|
|
|
failed = _wputenv_s(wname, wvalue);
|
|
|
|
#endif
|
|
|
|
ALLOCV_END(buf);
|
2010-09-11 11:47:44 +04:00
|
|
|
/* even if putenv() failed, clean up and try to delete the
|
|
|
|
* variable from the system area. */
|
|
|
|
if (!value || !*value) {
|
|
|
|
/* putenv() doesn't handle empty value */
|
2010-09-30 05:57:17 +04:00
|
|
|
if (!SetEnvironmentVariable(name, value) &&
|
|
|
|
GetLastError() != ERROR_ENVVAR_NOT_FOUND) goto fail;
|
2009-11-11 06:20:21 +03:00
|
|
|
}
|
2013-09-13 11:28:25 +04:00
|
|
|
if (failed) {
|
|
|
|
fail:
|
|
|
|
invalid_envname(name);
|
|
|
|
}
|
2006-02-01 16:27:47 +03:00
|
|
|
#elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
|
2010-01-11 06:58:39 +03:00
|
|
|
if (value) {
|
|
|
|
if (setenv(name, value, 1))
|
2013-06-21 09:31:41 +04:00
|
|
|
rb_sys_fail_str(rb_sprintf("setenv(%s)", name));
|
2013-09-13 11:28:25 +04:00
|
|
|
}
|
|
|
|
else {
|
2010-01-26 15:41:43 +03:00
|
|
|
#ifdef VOID_UNSETENV
|
|
|
|
unsetenv(name);
|
|
|
|
#else
|
2010-01-11 06:58:39 +03:00
|
|
|
if (unsetenv(name))
|
2013-06-21 09:31:41 +04:00
|
|
|
rb_sys_fail_str(rb_sprintf("unsetenv(%s)", name));
|
2010-01-26 15:41:43 +03:00
|
|
|
#endif
|
2010-01-11 06:58:39 +03:00
|
|
|
}
|
2011-10-24 18:57:08 +04:00
|
|
|
#elif defined __sun
|
2014-07-10 19:22:07 +04:00
|
|
|
/* Solaris 9 (or earlier) does not have setenv(3C) and unsetenv(3C). */
|
|
|
|
/* The below code was tested on Solaris 10 by:
|
|
|
|
% ./configure ac_cv_func_setenv=no ac_cv_func_unsetenv=no
|
|
|
|
*/
|
|
|
|
size_t len, mem_size;
|
|
|
|
char **env_ptr, *str, *mem_ptr;
|
2013-09-13 11:28:25 +04:00
|
|
|
|
2014-07-10 19:22:07 +04:00
|
|
|
check_envname(name);
|
2010-01-11 16:58:59 +03:00
|
|
|
len = strlen(name);
|
2014-07-10 19:22:07 +04:00
|
|
|
if (value) {
|
|
|
|
mem_size = len + strlen(value) + 2;
|
|
|
|
mem_ptr = malloc(mem_size);
|
|
|
|
if (mem_ptr == NULL)
|
|
|
|
rb_sys_fail_str(rb_sprintf("malloc("PRIuSIZE")", mem_size));
|
|
|
|
snprintf(mem_ptr, mem_size, "%s=%s", name, value);
|
|
|
|
}
|
2009-11-14 10:43:26 +03:00
|
|
|
for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
|
|
|
|
if (!strncmp(str, name, len) && str[len] == '=') {
|
|
|
|
if (!in_origenv(str)) free(str);
|
|
|
|
while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value) {
|
2014-07-10 19:22:07 +04:00
|
|
|
if (putenv(mem_ptr)) {
|
|
|
|
free(mem_ptr);
|
2013-06-21 09:31:41 +04:00
|
|
|
rb_sys_fail_str(rb_sprintf("putenv(%s)", name));
|
2014-07-10 19:22:07 +04:00
|
|
|
}
|
2009-11-14 10:43:26 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
#else /* WIN32 */
|
2005-07-19 12:31:04 +04:00
|
|
|
size_t len;
|
2010-01-11 16:58:59 +03:00
|
|
|
int i;
|
2013-09-13 11:28:25 +04:00
|
|
|
|
2010-01-11 16:58:59 +03:00
|
|
|
i=envix(name); /* where does it go? */
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
if (environ == origenviron) { /* need we copy environment? */
|
|
|
|
int j;
|
|
|
|
int max;
|
|
|
|
char **tmpenv;
|
|
|
|
|
|
|
|
for (max = i; environ[max]; max++) ;
|
|
|
|
tmpenv = ALLOC_N(char*, max+2);
|
|
|
|
for (j=0; j<max; j++) /* copy environment */
|
2009-11-11 06:54:04 +03:00
|
|
|
tmpenv[j] = ruby_strdup(environ[j]);
|
1999-01-20 07:59:39 +03:00
|
|
|
tmpenv[max] = 0;
|
|
|
|
environ = tmpenv; /* tell exec where it is now */
|
|
|
|
}
|
2005-06-13 08:04:33 +04:00
|
|
|
if (environ[i]) {
|
2005-06-12 20:56:06 +04:00
|
|
|
char **envp = origenviron;
|
|
|
|
while (*envp && *envp != environ[i]) envp++;
|
|
|
|
if (!*envp)
|
* array.c, bignum.c, cont.c, dir.c, dln.c, encoding.c, enumerator.c,
enumerator.c (enumerator_allocate), eval_jump.c, file.c, hash.c,
io.c, load.c, pack.c, proc.c, random.c, re.c, ruby.c, st.c,
string.c, thread.c, thread_pthread.c, time.c, util.c, variable.c,
vm.c, gc.c:
allocated memory objects by xmalloc (ruby_xmalloc) should be
freed by xfree (ruby_xfree).
* ext/curses/curses.c, ext/dbm/dbm.c, ext/digest/digest.c,
ext/gdbm/gdbm.c, ext/json/ext/parser/parser.c,
ext/json/ext/parser/unicode.c, ext/openssl/ossl_cipher.c,
ext/openssl/ossl_hmac.c, ext/openssl/ossl_pkey_ec.c,
ext/sdbm/init.c, ext/strscan/strscan.c, ext/zlib/zlib.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-08 14:01:40 +04:00
|
|
|
xfree(environ[i]);
|
2005-06-13 08:04:33 +04:00
|
|
|
if (!value) {
|
2005-06-12 20:56:06 +04:00
|
|
|
while (environ[i]) {
|
|
|
|
environ[i] = environ[i+1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2005-06-13 08:04:33 +04:00
|
|
|
}
|
|
|
|
else { /* does not exist yet */
|
|
|
|
if (!value) return;
|
1999-01-20 07:59:39 +03:00
|
|
|
REALLOC_N(environ, char*, i+2); /* just expand it a bit */
|
|
|
|
environ[i+1] = 0; /* make sure it's null terminated */
|
|
|
|
}
|
2005-07-19 12:31:04 +04:00
|
|
|
len = strlen(name) + strlen(value) + 2;
|
|
|
|
environ[i] = ALLOC_N(char, len);
|
|
|
|
snprintf(environ[i],len,"%s=%s",name,value); /* all that work just for this */
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif /* WIN32 */
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
ruby_unsetenv(const char *name)
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
|
|
|
ruby_setenv(name, 0);
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV[name] = value -> value
|
2011-06-30 04:20:15 +04:00
|
|
|
* ENV.store(name, value) -> value
|
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.store is an alias for ENV.[]=.
|
|
|
|
*
|
|
|
|
* Creates, updates, or deletes the named environment variable, returning the value.
|
|
|
|
* Both +name+ and +value+ may be instances of String.
|
|
|
|
* See {Valid Names and Values}[#class-ENV-label-Valid+Names+and+Values].
|
|
|
|
*
|
|
|
|
* - If the named environment variable does not exist:
|
|
|
|
* - If +value+ is +nil+, does nothing.
|
|
|
|
* ENV.clear
|
|
|
|
* ENV['foo'] = nil # => nil
|
|
|
|
* ENV.include?('foo') # => false
|
|
|
|
* ENV.store('bar', nil) # => nil
|
|
|
|
* ENV.include?('bar') # => false
|
|
|
|
* - If +value+ is not +nil+, creates the environment variable with +name+ and +value+:
|
|
|
|
* # Create 'foo' using ENV.[]=.
|
|
|
|
* ENV['foo'] = '0' # => '0'
|
|
|
|
* ENV['foo'] # => '0'
|
|
|
|
* # Create 'bar' using ENV.store.
|
|
|
|
* ENV.store('bar', '1') # => '1'
|
|
|
|
* ENV['bar'] # => '1'
|
|
|
|
* - If the named environment variable exists:
|
|
|
|
* - If +value+ is not +nil+, updates the environment variable with value +value+:
|
|
|
|
* # Update 'foo' using ENV.[]=.
|
|
|
|
* ENV['foo'] = '2' # => '2'
|
|
|
|
* ENV['foo'] # => '2'
|
|
|
|
* # Update 'bar' using ENV.store.
|
|
|
|
* ENV.store('bar', '3') # => '3'
|
|
|
|
* ENV['bar'] # => '3'
|
|
|
|
* - If +value+ is +nil+, deletes the environment variable:
|
|
|
|
* # Delete 'foo' using ENV.[]=.
|
|
|
|
* ENV['foo'] = nil # => nil
|
|
|
|
* ENV.include?('foo') # => false
|
|
|
|
* # Delete 'bar' using ENV.store.
|
|
|
|
* ENV.store('bar', nil) # => nil
|
|
|
|
* ENV.include?('bar') # => false
|
|
|
|
*
|
|
|
|
* Raises an exception if +name+ or +value+ is invalid.
|
|
|
|
* See {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values].
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2019-02-27 06:26:05 +03:00
|
|
|
env_aset_m(VALUE obj, VALUE nm, VALUE val)
|
|
|
|
{
|
|
|
|
return env_aset(nm, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
env_aset(VALUE nm, VALUE val)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
char *name, *value;
|
|
|
|
|
2006-06-17 18:50:04 +04:00
|
|
|
if (NIL_P(val)) {
|
2019-02-27 06:26:09 +03:00
|
|
|
env_delete(nm);
|
2008-09-23 03:27:33 +04:00
|
|
|
return Qnil;
|
2006-06-17 18:50:04 +04:00
|
|
|
}
|
2014-06-25 05:20:01 +04:00
|
|
|
SafeStringValue(nm);
|
|
|
|
SafeStringValue(val);
|
2014-06-25 16:18:53 +04:00
|
|
|
/* nm can be modified in `val.to_str`, don't get `name` before
|
|
|
|
* check for `val` */
|
|
|
|
get_env_ptr(name, nm);
|
|
|
|
get_env_ptr(value, val);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
ruby_setenv(name, value);
|
2008-09-23 11:59:48 +04:00
|
|
|
if (ENVMATCH(name, PATH_ENV)) {
|
2015-09-25 10:08:45 +03:00
|
|
|
RB_GC_GUARD(nm);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2020-04-18 14:58:35 +03:00
|
|
|
reset_by_modified_env(name);
|
2001-10-29 08:07:26 +03:00
|
|
|
return val;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2020-04-18 15:30:11 +03:00
|
|
|
env_keys(int raw)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
char **env;
|
2004-11-18 11:11:12 +03:00
|
|
|
VALUE ary;
|
2020-04-18 15:30:11 +03:00
|
|
|
rb_encoding *enc = raw ? 0 : rb_locale_encoding();
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2004-11-18 11:11:12 +03:00
|
|
|
ary = rb_ary_new();
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (*env) {
|
|
|
|
char *s = strchr(*env, '=');
|
1999-01-20 07:59:39 +03:00
|
|
|
if (s) {
|
2020-04-18 15:30:11 +03:00
|
|
|
const char *p = *env;
|
|
|
|
size_t l = s - p;
|
|
|
|
VALUE e = raw ? rb_utf8_str_new(p, l) : env_enc_str_new(p, l, enc);
|
|
|
|
rb_ary_push(ary, e);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
env++;
|
|
|
|
}
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1998-01-16 15:13:05 +03:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2019-08-29 05:47:20 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.keys -> array of names
|
2019-08-29 05:47:20 +03:00
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* Returns all variable names in an Array:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.keys # => ['bar', 'foo']
|
|
|
|
* The order of the names is OS-dependent.
|
|
|
|
* See {About Ordering}[#class-ENV-label-About+Ordering].
|
|
|
|
*
|
2020-08-23 20:10:01 +03:00
|
|
|
* Returns the empty Array if ENV is empty.
|
2019-08-29 05:47:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
env_f_keys(VALUE _)
|
|
|
|
{
|
2020-04-18 15:30:11 +03:00
|
|
|
return env_keys(FALSE);
|
2019-08-29 05:47:20 +03:00
|
|
|
}
|
|
|
|
|
2012-11-06 21:13:48 +04:00
|
|
|
static VALUE
|
2013-06-26 17:43:22 +04:00
|
|
|
rb_env_size(VALUE ehash, VALUE args, VALUE eobj)
|
2012-11-06 21:13:48 +04:00
|
|
|
{
|
|
|
|
char **env;
|
|
|
|
long cnt = 0;
|
|
|
|
|
|
|
|
env = GET_ENVIRON(environ);
|
|
|
|
for (; *env ; ++env) {
|
|
|
|
if (strchr(*env, '=')) {
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FREE_ENVIRON(environ);
|
|
|
|
return LONG2FIX(cnt);
|
|
|
|
}
|
|
|
|
|
2013-01-26 07:58:33 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-11-02 09:32:49 +03:00
|
|
|
* ENV.each_key { |name| block } -> ENV
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.each_key -> an_enumerator
|
2013-01-26 07:58:33 +04:00
|
|
|
*
|
2019-12-17 01:58:53 +03:00
|
|
|
* Yields each environment variable name:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1') # => ENV
|
|
|
|
* names = []
|
|
|
|
* ENV.each_key { |name| names.push(name) } # => ENV
|
|
|
|
* names # => ["bar", "foo"]
|
|
|
|
*
|
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
* e = ENV.each_key # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_key>
|
|
|
|
* names = []
|
|
|
|
* e.each { |name| names.push(name) } # => ENV
|
|
|
|
* names # => ["bar", "foo"]
|
2013-01-26 07:58:33 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_each_key(VALUE ehash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2004-11-18 11:11:12 +03:00
|
|
|
VALUE keys;
|
2003-08-02 00:16:53 +04:00
|
|
|
long i;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2012-11-06 21:13:48 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
2020-04-18 15:30:11 +03:00
|
|
|
keys = env_keys(FALSE);
|
2006-09-02 18:42:08 +04:00
|
|
|
for (i=0; i<RARRAY_LEN(keys); i++) {
|
2013-05-13 13:56:22 +04:00
|
|
|
rb_yield(RARRAY_AREF(keys, i));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2003-08-02 00:16:53 +04:00
|
|
|
return ehash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_values(void)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2004-11-18 11:11:12 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
char **env;
|
|
|
|
|
2004-11-18 11:11:12 +03:00
|
|
|
ary = rb_ary_new();
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (*env) {
|
|
|
|
char *s = strchr(*env, '=');
|
1999-01-20 07:59:39 +03:00
|
|
|
if (s) {
|
2003-06-06 13:24:59 +04:00
|
|
|
rb_ary_push(ary, env_str_new2(s+1));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
env++;
|
|
|
|
}
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1998-01-16 15:13:05 +03:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2019-08-29 05:47:20 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.values -> array of values
|
2019-08-29 05:47:20 +03:00
|
|
|
*
|
2019-12-17 01:58:53 +03:00
|
|
|
* Returns all environment variable values in an Array:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.values # => ['1', '0']
|
|
|
|
* The order of the values is OS-dependent.
|
|
|
|
* See {About Ordering}[#class-ENV-label-About+Ordering].
|
|
|
|
*
|
2020-08-23 20:10:01 +03:00
|
|
|
* Returns the empty Array if ENV is empty.
|
2019-08-29 05:47:20 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
env_f_values(VALUE _)
|
|
|
|
{
|
|
|
|
return env_values();
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-11-02 09:32:49 +03:00
|
|
|
* ENV.each_value { |value| block } -> ENV
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.each_value -> an_enumerator
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2019-12-17 01:58:53 +03:00
|
|
|
* Yields each environment variable value:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1') # => ENV
|
|
|
|
* values = []
|
|
|
|
* ENV.each_value { |value| values.push(value) } # => ENV
|
|
|
|
* values # => ["1", "0"]
|
|
|
|
*
|
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
* e = ENV.each_value # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_value>
|
|
|
|
* values = []
|
|
|
|
* e.each { |value| values.push(value) } # => ENV
|
|
|
|
* values # => ["1", "0"]
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_each_value(VALUE ehash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-05-25 19:42:14 +04:00
|
|
|
VALUE values;
|
2003-08-02 00:16:53 +04:00
|
|
|
long i;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2012-11-06 21:13:48 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
* safe.c (rb_set_safe_level, safe_setter): raise an ArgumentError
when $SAFE is set to 4. $SAFE=4 is now obsolete.
[ruby-core:55222] [Feature #8468]
* object.c (rb_obj_untrusted, rb_obj_untrust, rb_obj_trust):
Kernel#untrusted?, untrust, and trust are now deprecated.
Their behavior is same as tainted?, taint, and untaint,
respectively.
* include/ruby/ruby.h (OBJ_UNTRUSTED, OBJ_UNTRUST): OBJ_UNTRUSTED()
and OBJ_UNTRUST() are aliases of OBJ_TAINTED() and OBJ_TAINT(),
respectively.
* array.c, class.c, debug.c, dir.c, encoding.c, error.c, eval.c,
ext/curses/curses.c, ext/dbm/dbm.c, ext/dl/cfunc.c,
ext/dl/cptr.c, ext/dl/dl.c, ext/etc/etc.c, ext/fiddle/fiddle.c,
ext/fiddle/pointer.c, ext/gdbm/gdbm.c, ext/readline/readline.c,
ext/sdbm/init.c, ext/socket/ancdata.c, ext/socket/basicsocket.c,
ext/socket/socket.c, ext/socket/udpsocket.c,
ext/stringio/stringio.c, ext/syslog/syslog.c, ext/tk/tcltklib.c,
ext/win32ole/win32ole.c, file.c, gc.c, hash.c, io.c, iseq.c,
load.c, marshal.c, object.c, proc.c, process.c, random.c, re.c,
safe.c, string.c, thread.c, transcode.c, variable.c,
vm_insnhelper.c, vm_method.c, vm_trace.c: remove code for
$SAFE=4.
* test/dl/test_dl2.rb, test/erb/test_erb.rb,
test/readline/test_readline.rb,
test/readline/test_readline_history.rb, test/ruby/test_alias.rb,
test/ruby/test_array.rb, test/ruby/test_dir.rb,
test/ruby/test_encoding.rb, test/ruby/test_env.rb,
test/ruby/test_eval.rb, test/ruby/test_exception.rb,
test/ruby/test_file_exhaustive.rb, test/ruby/test_hash.rb,
test/ruby/test_io.rb, test/ruby/test_method.rb,
test/ruby/test_module.rb, test/ruby/test_object.rb,
test/ruby/test_pack.rb, test/ruby/test_rand.rb,
test/ruby/test_regexp.rb, test/ruby/test_settracefunc.rb,
test/ruby/test_struct.rb, test/ruby/test_thread.rb,
test/ruby/test_time.rb: remove tests for $SAFE=4.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41259 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-06-12 18:20:51 +04:00
|
|
|
values = env_values();
|
2006-09-02 18:42:08 +04:00
|
|
|
for (i=0; i<RARRAY_LEN(values); i++) {
|
2013-05-13 13:56:22 +04:00
|
|
|
rb_yield(RARRAY_AREF(values, i));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2003-08-02 00:16:53 +04:00
|
|
|
return ehash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-09-20 22:18:07 +03:00
|
|
|
* ENV.each { |name, value| block } -> ENV
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.each -> an_enumerator
|
2019-09-20 22:18:07 +03:00
|
|
|
* ENV.each_pair { |name, value| block } -> ENV
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.each_pair -> an_enumerator
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-05-29 21:53:11 +03:00
|
|
|
* Yields each environment variable name and its value as a 2-element \Array:
|
2019-12-17 01:58:53 +03:00
|
|
|
* h = {}
|
|
|
|
* ENV.each_pair { |name, value| h[name] = value } # => ENV
|
|
|
|
* h # => {"bar"=>"1", "foo"=>"0"}
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2019-12-17 01:58:53 +03:00
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
* h = {}
|
|
|
|
* e = ENV.each_pair # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_pair>
|
|
|
|
* e.each { |name, value| h[name] = value } # => ENV
|
|
|
|
* h # => {"bar"=>"1", "foo"=>"0"}
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2007-11-03 17:07:48 +03:00
|
|
|
env_each_pair(VALUE ehash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
char **env;
|
2004-11-18 11:11:12 +03:00
|
|
|
VALUE ary;
|
2003-08-02 00:16:53 +04:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2012-11-06 21:13:48 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
2007-11-03 17:07:48 +03:00
|
|
|
|
2004-11-18 11:11:12 +03:00
|
|
|
ary = rb_ary_new();
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
1999-01-20 07:59:39 +03:00
|
|
|
while (*env) {
|
|
|
|
char *s = strchr(*env, '=');
|
|
|
|
if (s) {
|
2003-08-02 00:16:53 +04:00
|
|
|
rb_ary_push(ary, env_str_new(*env, s-*env));
|
|
|
|
rb_ary_push(ary, env_str_new2(s+1));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
env++;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
2003-08-02 00:16:53 +04:00
|
|
|
|
2020-03-16 17:03:22 +03:00
|
|
|
if (rb_block_pair_yield_optimizable()) {
|
2017-10-02 10:51:27 +03:00
|
|
|
for (i=0; i<RARRAY_LEN(ary); i+=2) {
|
|
|
|
rb_yield_values(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (i=0; i<RARRAY_LEN(ary); i+=2) {
|
|
|
|
rb_yield(rb_assoc_new(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)));
|
|
|
|
}
|
2003-08-02 00:16:53 +04:00
|
|
|
}
|
|
|
|
return ehash;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2018-11-04 14:45:11 +03:00
|
|
|
* ENV.reject! { |name, value| block } -> ENV or nil
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.reject! -> an_enumerator
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2019-12-17 01:58:53 +03:00
|
|
|
* Similar to ENV.delete_if, but returns +nil+ if no changes were made.
|
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* Yields each environment variable name and its value as a 2-element Array,
|
|
|
|
* deleting each environment variable for which the block returns a truthy value,
|
|
|
|
* and returning ENV (if any deletions) or +nil+ (if not):
|
2019-12-17 01:58:53 +03:00
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* ENV.reject! { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"foo"=>"0"}
|
|
|
|
* ENV.reject! { |name, value| name.start_with?('b') } # => nil
|
|
|
|
*
|
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* e = ENV.reject! # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:reject!>
|
|
|
|
* e.each { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"foo"=>"0"}
|
|
|
|
* e.each { |name, value| name.start_with?('b') } # => nil
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2008-04-14 12:31:38 +04:00
|
|
|
env_reject_bang(VALUE ehash)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2015-06-26 02:06:48 +03:00
|
|
|
VALUE keys;
|
2003-08-01 06:52:21 +04:00
|
|
|
long i;
|
2002-08-21 19:47:54 +04:00
|
|
|
int del = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2012-11-06 21:13:48 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
2020-04-18 15:30:11 +03:00
|
|
|
keys = env_keys(FALSE);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 14:49:11 +04:00
|
|
|
RBASIC_CLEAR_CLASS(keys);
|
2006-09-02 18:42:08 +04:00
|
|
|
for (i=0; i<RARRAY_LEN(keys); i++) {
|
2013-05-13 13:56:22 +04:00
|
|
|
VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
|
1998-01-16 15:13:05 +03:00
|
|
|
if (!NIL_P(val)) {
|
2013-05-13 13:56:22 +04:00
|
|
|
if (RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
|
2019-02-27 06:26:09 +03:00
|
|
|
env_delete(RARRAY_AREF(keys, i));
|
2000-08-07 09:05:04 +04:00
|
|
|
del++;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-26 02:06:48 +03:00
|
|
|
RB_GC_GUARD(keys);
|
2000-08-07 09:05:04 +04:00
|
|
|
if (del == 0) return Qnil;
|
|
|
|
return envtbl;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-11-02 09:32:49 +03:00
|
|
|
* ENV.delete_if { |name, value| block } -> ENV
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.delete_if -> an_enumerator
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* Yields each environment variable name and its value as a 2-element Array,
|
|
|
|
* deleting each environment variable for which the block returns a truthy value,
|
2020-02-19 22:59:21 +03:00
|
|
|
* and returning ENV (regardless of whether any deletions):
|
2020-02-09 09:59:55 +03:00
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* ENV.delete_if { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"foo"=>"0"}
|
|
|
|
* ENV.delete_if { |name, value| name.start_with?('b') } # => ENV
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* e = ENV.delete_if # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:delete_if!>
|
|
|
|
* e.each { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"foo"=>"0"}
|
|
|
|
* e.each { |name, value| name.start_with?('b') } # => ENV
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2000-08-07 09:05:04 +04:00
|
|
|
static VALUE
|
2008-04-14 12:31:38 +04:00
|
|
|
env_delete_if(VALUE ehash)
|
2000-08-07 09:05:04 +04:00
|
|
|
{
|
2012-11-06 21:13:48 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
2008-04-14 12:31:38 +04:00
|
|
|
env_reject_bang(ehash);
|
1998-01-16 15:13:05 +03:00
|
|
|
return envtbl;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.values_at(*names) -> array of values
|
2020-02-09 09:59:55 +03:00
|
|
|
*
|
|
|
|
* Returns an Array containing the environment variable values associated with
|
|
|
|
* the given names:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* ENV.values_at('foo', 'baz') # => ["0", "2"]
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* Returns +nil+ in the Array for each name that is not an ENV name:
|
|
|
|
* ENV.values_at('foo', 'bat', 'bar', 'bam') # => ["0", nil, "1", nil]
|
|
|
|
*
|
2020-08-23 20:10:01 +03:00
|
|
|
* Returns an empty \Array if no names given.
|
2020-02-09 09:59:55 +03:00
|
|
|
*
|
|
|
|
* Raises an exception if any name is invalid.
|
|
|
|
* See {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values].
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2001-12-11 06:48:08 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_values_at(int argc, VALUE *argv, VALUE _)
|
2001-12-11 06:48:08 +03:00
|
|
|
{
|
2004-11-18 11:11:12 +03:00
|
|
|
VALUE result;
|
2001-12-11 06:48:08 +03:00
|
|
|
long i;
|
|
|
|
|
2004-11-18 11:11:12 +03:00
|
|
|
result = rb_ary_new();
|
2003-05-04 20:03:24 +04:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2001-12-11 06:48:08 +03:00
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.select { |name, value| block } -> hash of name/value pairs
|
|
|
|
* ENV.select -> an_enumerator
|
|
|
|
* ENV.filter { |name, value| block } -> hash of name/value pairs
|
|
|
|
* ENV.filter -> an_enumerator
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* ENV.filter is an alias for ENV.select.
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* Yields each environment variable name and its value as a 2-element Array,
|
|
|
|
* returning a Hash of the names and values for which the block returns a truthy value:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* ENV.select { |name, value| name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
|
|
|
|
* ENV.filter { |name, value| name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
|
2018-11-04 14:46:49 +03:00
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
* e = ENV.select # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:select>
|
|
|
|
* e.each { |name, value | name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
|
|
|
|
* e = ENV.filter # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:filter>
|
|
|
|
* e.each { |name, value | name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2003-05-04 20:03:24 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_select(VALUE ehash)
|
2003-05-04 20:03:24 +04:00
|
|
|
{
|
|
|
|
VALUE result;
|
2014-06-26 03:28:04 +04:00
|
|
|
VALUE keys;
|
|
|
|
long i;
|
2003-05-04 20:03:24 +04:00
|
|
|
|
2012-11-06 21:13:48 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
2007-06-21 18:50:14 +04:00
|
|
|
result = rb_hash_new();
|
2020-04-18 15:30:11 +03:00
|
|
|
keys = env_keys(FALSE);
|
2014-06-26 03:28:04 +04:00
|
|
|
for (i = 0; i < RARRAY_LEN(keys); ++i) {
|
|
|
|
VALUE key = RARRAY_AREF(keys, i);
|
|
|
|
VALUE val = rb_f_getenv(Qnil, key);
|
|
|
|
if (!NIL_P(val)) {
|
|
|
|
if (RTEST(rb_yield_values(2, key, val))) {
|
|
|
|
rb_hash_aset(result, key, val);
|
2003-05-04 20:03:24 +04:00
|
|
|
}
|
2001-12-11 06:48:08 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 02:06:48 +03:00
|
|
|
RB_GC_GUARD(keys);
|
2003-05-04 20:03:24 +04:00
|
|
|
|
2001-12-11 06:48:08 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2018-11-04 14:45:11 +03:00
|
|
|
* ENV.select! { |name, value| block } -> ENV or nil
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.select! -> an_enumerator
|
2018-11-04 14:46:49 +03:00
|
|
|
* ENV.filter! { |name, value| block } -> ENV or nil
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.filter! -> an_enumerator
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2018-11-04 14:46:49 +03:00
|
|
|
* ENV.filter! is an alias for ENV.select!.
|
2020-02-09 09:59:55 +03:00
|
|
|
*
|
|
|
|
* Yields each environment variable name and its value as a 2-element Array,
|
|
|
|
* deleting each entry for which the block returns +false+ or +nil+,
|
|
|
|
* and returning ENV if any deletions made, or +nil+ otherwise:
|
|
|
|
*
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* ENV.select! { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"bar"=>"1", "baz"=>"2"}
|
|
|
|
* ENV.select! { |name, value| true } # => nil
|
|
|
|
*
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* ENV.filter! { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"bar"=>"1", "baz"=>"2"}
|
|
|
|
* ENV.filter! { |name, value| true } # => nil
|
|
|
|
*
|
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
*
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* e = ENV.select! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:select!>
|
|
|
|
* e.each { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"bar"=>"1", "baz"=>"2"}
|
|
|
|
* e.each { |name, value| true } # => nil
|
|
|
|
*
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* e = ENV.filter! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:filter!>
|
|
|
|
* e.each { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"bar"=>"1", "baz"=>"2"}
|
|
|
|
* e.each { |name, value| true } # => nil
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2010-03-03 12:44:44 +03:00
|
|
|
static VALUE
|
|
|
|
env_select_bang(VALUE ehash)
|
|
|
|
{
|
2015-06-26 02:06:48 +03:00
|
|
|
VALUE keys;
|
2010-03-03 12:44:44 +03:00
|
|
|
long i;
|
|
|
|
int del = 0;
|
|
|
|
|
2012-11-06 21:13:48 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
2020-04-18 15:30:11 +03:00
|
|
|
keys = env_keys(FALSE);
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 14:49:11 +04:00
|
|
|
RBASIC_CLEAR_CLASS(keys);
|
2010-03-03 12:44:44 +03:00
|
|
|
for (i=0; i<RARRAY_LEN(keys); i++) {
|
2013-05-13 13:56:22 +04:00
|
|
|
VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
|
2010-03-03 12:44:44 +03:00
|
|
|
if (!NIL_P(val)) {
|
2013-05-13 13:56:22 +04:00
|
|
|
if (!RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
|
2019-02-27 06:26:09 +03:00
|
|
|
env_delete(RARRAY_AREF(keys, i));
|
2010-03-03 12:44:44 +03:00
|
|
|
del++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-26 02:06:48 +03:00
|
|
|
RB_GC_GUARD(keys);
|
2010-03-03 12:44:44 +03:00
|
|
|
if (del == 0) return Qnil;
|
|
|
|
return envtbl;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-11-02 09:32:49 +03:00
|
|
|
* ENV.keep_if { |name, value| block } -> ENV
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.keep_if -> an_enumerator
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* Yields each environment variable name and its value as a 2-element Array,
|
|
|
|
* deleting each environment variable for which the block returns +false+ or +nil+,
|
|
|
|
* and returning ENV:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* ENV.keep_if { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"bar"=>"1", "baz"=>"2"}
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-09 09:59:55 +03:00
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* e = ENV.keep_if # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:keep_if>
|
|
|
|
* e.each { |name, value| name.start_with?('b') } # => ENV
|
|
|
|
* ENV # => {"bar"=>"1", "baz"=>"2"}
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2010-03-03 12:44:44 +03:00
|
|
|
static VALUE
|
|
|
|
env_keep_if(VALUE ehash)
|
|
|
|
{
|
2012-11-06 21:13:48 +04:00
|
|
|
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
|
2010-03-03 12:44:44 +03:00
|
|
|
env_select_bang(ehash);
|
|
|
|
return envtbl;
|
|
|
|
}
|
|
|
|
|
2018-04-19 08:55:42 +03:00
|
|
|
/*
|
2020-02-22 04:25:54 +03:00
|
|
|
* call-seq:
|
|
|
|
* ENV.slice(*names) -> hash of name/value pairs
|
|
|
|
*
|
|
|
|
* Returns a Hash of the given ENV names and their corresponding values:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2', 'bat' => '3')
|
|
|
|
* ENV.slice('foo', 'baz') # => {"foo"=>"0", "baz"=>"2"}
|
|
|
|
* ENV.slice('baz', 'foo') # => {"baz"=>"2", "foo"=>"0"}
|
|
|
|
* Raises an exception if any of the +names+ is invalid
|
|
|
|
* (see {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values]):
|
|
|
|
* ENV.slice('foo', 'bar', :bat) # Raises TypeError (no implicit conversion of Symbol into String)
|
2018-04-19 08:55:42 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_slice(int argc, VALUE *argv, VALUE _)
|
2018-04-19 08:55:42 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
VALUE key, value, result;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
return rb_hash_new();
|
|
|
|
}
|
|
|
|
result = rb_hash_new_with_size(argc);
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
key = argv[i];
|
|
|
|
value = rb_f_getenv(Qnil, key);
|
|
|
|
if (value != Qnil)
|
|
|
|
rb_hash_aset(result, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-04-24 18:46:39 +04:00
|
|
|
VALUE
|
|
|
|
rb_env_clear(void)
|
2003-01-07 10:36:40 +03:00
|
|
|
{
|
2015-06-26 02:06:48 +03:00
|
|
|
VALUE keys;
|
2003-08-01 06:52:21 +04:00
|
|
|
long i;
|
2007-06-29 09:15:46 +04:00
|
|
|
|
2020-04-18 15:30:11 +03:00
|
|
|
keys = env_keys(TRUE);
|
2006-09-02 18:42:08 +04:00
|
|
|
for (i=0; i<RARRAY_LEN(keys); i++) {
|
2020-04-18 15:30:53 +03:00
|
|
|
VALUE key = RARRAY_AREF(keys, i);
|
|
|
|
const char *nam = RSTRING_PTR(key);
|
|
|
|
ruby_setenv(nam, 0);
|
2003-01-07 10:36:40 +03:00
|
|
|
}
|
2015-06-26 02:06:48 +03:00
|
|
|
RB_GC_GUARD(keys);
|
2003-01-07 10:36:40 +03:00
|
|
|
return envtbl;
|
|
|
|
}
|
|
|
|
|
2019-08-29 05:47:20 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.clear -> ENV
|
2019-08-29 05:47:20 +03:00
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* Removes every environment variable; returns ENV:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.size # => 2
|
|
|
|
* ENV.clear # => ENV
|
|
|
|
* ENV.size # => 0
|
2019-08-29 05:47:20 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
env_clear(VALUE _)
|
|
|
|
{
|
|
|
|
return rb_env_clear();
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ENV.to_s -> "ENV"
|
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Returns String 'ENV':
|
|
|
|
* ENV.to_s # => "ENV"
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_to_s(VALUE _)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
* string.c (rb_str_usascii_new{,2}: defined.
(rb_str_new): set US-ASCII and ENC_CODERANGE_7BIT when empty
string.
* encoding.c (rb_usascii_encoding, rb_usascii_encindex): defined.
(rb_enc_inspect, enc_name, rb_locale_charmap, rb_enc_name_list_i):
use rb_str_ascii_new.
* array.c (recursive_join, inspect_ary): ditto.
* object.c (nil_to_s, nil_inspect, true_to_s, false_to_s,
rb_mod_to_s): ditto.
* hash.c (inspect_hash, rb_hash_inspect, rb_f_getenv, env_fetch,
env_clear, env_to_s, env_inspect): ditto.
* numeric.c (flo_to_s, int_chr, rb_fix2str): ditto.
* bignum.c (rb_big2str): ditto.
* file.c (rb_file_ftype, rb_file_s_dirname, rb_file_s_extname,
file_inspect_join, Init_file): ditto.
* test/ruby/test_ruby_m17n.rb: add checks for encoding of string.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15244 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-01-25 19:40:02 +03:00
|
|
|
return rb_usascii_str_new2("ENV");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.inspect -> a_string
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Returns the contents of the environment as a String:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.inspect # => "{\"bar\"=>\"1\", \"foo\"=>\"0\"}"
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2000-12-05 12:36:54 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_inspect(VALUE _)
|
2000-12-05 12:36:54 +03:00
|
|
|
{
|
|
|
|
char **env;
|
2004-11-18 11:11:12 +03:00
|
|
|
VALUE str, i;
|
2000-12-05 12:36:54 +03:00
|
|
|
|
2004-11-18 11:11:12 +03:00
|
|
|
str = rb_str_buf_new2("{");
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
2000-12-05 12:36:54 +03:00
|
|
|
while (*env) {
|
|
|
|
char *s = strchr(*env, '=');
|
|
|
|
|
|
|
|
if (env != environ) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, ", ");
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
|
|
|
if (s) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "\"");
|
|
|
|
rb_str_buf_cat(str, *env, s-*env);
|
|
|
|
rb_str_buf_cat2(str, "\"=>");
|
2008-02-28 21:13:45 +03:00
|
|
|
i = rb_inspect(rb_str_new2(s+1));
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_append(str, i);
|
2000-12-05 12:36:54 +03:00
|
|
|
}
|
|
|
|
env++;
|
|
|
|
}
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "}");
|
2000-12-05 12:36:54 +03:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.to_a -> array of 2-element arrays
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Returns the contents of ENV as an Array of 2-element Arrays,
|
|
|
|
* each of which is a name/value pair:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.to_a # => [["bar", "1"], ["foo", "0"]]
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_to_a(VALUE _)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
char **env;
|
2004-11-18 11:11:12 +03:00
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2004-11-18 11:11:12 +03:00
|
|
|
ary = rb_ary_new();
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (*env) {
|
|
|
|
char *s = strchr(*env, '=');
|
1999-01-20 07:59:39 +03:00
|
|
|
if (s) {
|
2003-06-06 13:24:59 +04:00
|
|
|
rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
|
|
|
|
env_str_new2(s+1)));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
env++;
|
|
|
|
}
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1998-01-16 15:13:05 +03:00
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-14 08:18:48 +03:00
|
|
|
* ENV.rehash -> nil
|
|
|
|
*
|
|
|
|
* (Provided for compatibility with Hash.)
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Does not modify ENV; returns +nil+.
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_none(VALUE _)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.length -> an_integer
|
|
|
|
* ENV.size -> an_integer
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Returns the count of environment variables:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.length # => 2
|
|
|
|
* ENV.size # => 2
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_size(VALUE _)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int i;
|
2001-11-13 06:59:20 +03:00
|
|
|
char **env;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
2012-12-29 16:22:04 +04:00
|
|
|
for (i=0; env[i]; i++)
|
1998-01-16 15:13:05 +03:00
|
|
|
;
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1998-01-16 15:13:05 +03:00
|
|
|
return INT2FIX(i);
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ENV.empty? -> true or false
|
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Returns +true+ when there are no environment variables, +false+ otherwise:
|
|
|
|
* ENV.clear
|
2020-02-14 08:26:19 +03:00
|
|
|
* ENV.empty? # => true
|
2020-02-14 08:18:48 +03:00
|
|
|
* ENV['foo'] = '0'
|
|
|
|
* ENV.empty? # => false
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_empty_p(VALUE _)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-11-13 06:59:20 +03:00
|
|
|
char **env;
|
|
|
|
|
|
|
|
env = GET_ENVIRON(environ);
|
|
|
|
if (env[0] == 0) {
|
|
|
|
FREE_ENVIRON(environ);
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
FREE_ENVIRON(environ);
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ENV.include?(name) -> true or false
|
|
|
|
* ENV.has_key?(name) -> true or false
|
|
|
|
* ENV.member?(name) -> true or false
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.key?(name) -> true or false
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.has_key?, ENV.member?, and ENV.key? are aliases for ENV.include?.
|
|
|
|
*
|
|
|
|
* Returns +true+ if there is an environment variable with the given +name+:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.include?('foo') # => true
|
|
|
|
* Returns +false+ if +name+ is a valid String and there is no such environment variable:
|
|
|
|
* ENV.include?('baz') # => false
|
|
|
|
* Returns +false+ if +name+ is the empty String or is a String containing character <code>'='</code>:
|
|
|
|
* ENV.include?('') # => false
|
|
|
|
* ENV.include?('=') # => false
|
|
|
|
* Raises an exception if +name+ is a String containing the NUL character <code>"\0"</code>:
|
|
|
|
* ENV.include?("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
|
|
|
|
* Raises an exception if +name+ has an encoding that is not ASCII-compatible:
|
|
|
|
* ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
|
|
|
|
* # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
|
|
|
|
* Raises an exception if +name+ is not a String:
|
|
|
|
* ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_has_key(VALUE env, VALUE key)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2014-06-25 16:18:53 +04:00
|
|
|
const char *s;
|
2001-05-02 08:22:21 +04:00
|
|
|
|
2014-06-25 16:18:53 +04:00
|
|
|
s = env_name(key);
|
2001-05-02 08:22:21 +04:00
|
|
|
if (getenv(s)) return Qtrue;
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.assoc(name) -> [name, value] or nil
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* Returns a 2-element Array containing the name and value of the environment variable
|
|
|
|
* for +name+ if it exists:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
2020-02-14 08:18:48 +03:00
|
|
|
* ENV.assoc('foo') # => ['foo', '0']
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if +name+ is a valid String and there is no such environment variable.
|
|
|
|
*
|
|
|
|
* Returns +nil+ if +name+ is the empty String or is a String containing character <code>'='</code>.
|
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* Raises an exception if +name+ is a String containing the NUL character <code>"\0"</code>:
|
|
|
|
* ENV.assoc("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
|
|
|
|
* Raises an exception if +name+ has an encoding that is not ASCII-compatible:
|
|
|
|
* ENV.assoc("\xa1\xa1".force_encoding(Encoding::UTF_16LE))
|
|
|
|
* # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
|
|
|
|
* Raises an exception if +name+ is not a String:
|
|
|
|
* ENV.assoc(Object.new) # TypeError (no implicit conversion of Object into String)
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2007-06-22 20:26:07 +04:00
|
|
|
env_assoc(VALUE env, VALUE key)
|
|
|
|
{
|
2014-06-25 16:18:53 +04:00
|
|
|
const char *s, *e;
|
2007-06-22 20:26:07 +04:00
|
|
|
|
2015-12-06 15:13:25 +03:00
|
|
|
s = env_name(key);
|
2007-06-22 20:26:07 +04:00
|
|
|
e = getenv(s);
|
2016-08-04 05:54:32 +03:00
|
|
|
if (e) return rb_assoc_new(key, env_str_new2(e));
|
2007-06-22 20:26:07 +04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.value?(value) -> true or false
|
2011-06-30 04:20:15 +04:00
|
|
|
* ENV.has_value?(value) -> true or false
|
|
|
|
*
|
2020-02-19 22:59:21 +03:00
|
|
|
* Returns +true+ if +value+ is the value for some environment variable name, +false+ otherwise:
|
2020-02-14 08:18:48 +03:00
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.value?('0') # => true
|
|
|
|
* ENV.has_value?('0') # => true
|
|
|
|
* ENV.value?('2') # => false
|
|
|
|
* ENV.has_value?('2') # => false
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2007-06-22 20:26:07 +04:00
|
|
|
static VALUE
|
|
|
|
env_has_value(VALUE dmy, VALUE obj)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
char **env;
|
|
|
|
|
2007-06-22 20:26:07 +04:00
|
|
|
obj = rb_check_string_type(obj);
|
|
|
|
if (NIL_P(obj)) return Qnil;
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (*env) {
|
2003-06-23 12:41:07 +04:00
|
|
|
char *s = strchr(*env, '=');
|
|
|
|
if (s++) {
|
2003-11-28 13:28:21 +03:00
|
|
|
long len = strlen(s);
|
2007-06-22 20:26:07 +04:00
|
|
|
if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
2001-11-13 06:59:20 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
env++;
|
|
|
|
}
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.rassoc(value) -> [name, value] or nil
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-20 02:43:26 +03:00
|
|
|
* Returns a 2-element Array containing the name and value of the
|
|
|
|
* *first* *found* environment variable that has value +value+, if one
|
|
|
|
* exists:
|
2020-02-14 08:18:48 +03:00
|
|
|
* ENV.replace('foo' => '0', 'bar' => '0')
|
|
|
|
* ENV.rassoc('0') # => ["bar", "0"]
|
|
|
|
* The order in which environment variables are examined is OS-dependent.
|
|
|
|
* See {About Ordering}[#class-ENV-label-About+Ordering].
|
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if there is no such environment variable.
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2007-06-22 20:26:07 +04:00
|
|
|
static VALUE
|
|
|
|
env_rassoc(VALUE dmy, VALUE obj)
|
|
|
|
{
|
|
|
|
char **env;
|
|
|
|
|
|
|
|
obj = rb_check_string_type(obj);
|
|
|
|
if (NIL_P(obj)) return Qnil;
|
|
|
|
env = GET_ENVIRON(environ);
|
|
|
|
while (*env) {
|
|
|
|
char *s = strchr(*env, '=');
|
|
|
|
if (s++) {
|
|
|
|
long len = strlen(s);
|
|
|
|
if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
|
2019-09-25 06:59:12 +03:00
|
|
|
VALUE result = rb_assoc_new(rb_str_new(*env, s-*env-1), obj);
|
2007-06-22 20:26:07 +04:00
|
|
|
FREE_ENVIRON(environ);
|
2008-02-05 18:25:35 +03:00
|
|
|
return result;
|
2007-06-22 20:26:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
env++;
|
|
|
|
}
|
|
|
|
FREE_ENVIRON(environ);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.key(value) -> name or nil
|
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Returns the name of the first environment variable with +value+, if it exists:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '0')
|
2020-02-14 08:26:19 +03:00
|
|
|
* ENV.key('0') # => "foo"
|
2019-12-13 22:46:54 +03:00
|
|
|
* The order in which environment variables are examined is OS-dependent.
|
|
|
|
* See {About Ordering}[#class-ENV-label-About+Ordering].
|
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if there is no such value.
|
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Raises an exception if +value+ is invalid:
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.key(Object.new) # raises TypeError (no implicit conversion of Object into String)
|
2020-02-22 04:25:54 +03:00
|
|
|
* See {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values].
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_key(VALUE dmy, VALUE value)
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
|
|
|
char **env;
|
2001-11-13 06:59:20 +03:00
|
|
|
VALUE str;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2014-06-25 05:20:01 +04:00
|
|
|
SafeStringValue(value);
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
1999-08-13 09:45:20 +04:00
|
|
|
while (*env) {
|
2003-06-23 12:41:07 +04:00
|
|
|
char *s = strchr(*env, '=');
|
|
|
|
if (s++) {
|
2003-11-28 13:28:21 +03:00
|
|
|
long len = strlen(s);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) {
|
2003-06-06 13:24:59 +04:00
|
|
|
str = env_str_new(*env, s-*env-1);
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
|
|
|
return str;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
env++;
|
|
|
|
}
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1999-08-13 09:45:20 +04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.index(value) -> name
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Deprecated method that is equivalent to ENV.key.
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2005-04-02 08:23:56 +04:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_index(VALUE dmy, VALUE value)
|
2005-04-02 08:23:56 +04:00
|
|
|
{
|
2019-12-17 11:17:07 +03:00
|
|
|
rb_warn_deprecated("ENV.index", "ENV.key");
|
2005-04-02 08:23:56 +04:00
|
|
|
return env_key(dmy, value);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_to_hash(void)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
char **env;
|
2004-11-18 11:11:12 +03:00
|
|
|
VALUE hash;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2004-11-18 11:11:12 +03:00
|
|
|
hash = rb_hash_new();
|
2001-11-13 06:59:20 +03:00
|
|
|
env = GET_ENVIRON(environ);
|
1999-01-20 07:59:39 +03:00
|
|
|
while (*env) {
|
|
|
|
char *s = strchr(*env, '=');
|
|
|
|
if (s) {
|
2003-06-06 13:24:59 +04:00
|
|
|
rb_hash_aset(hash, env_str_new(*env, s-*env),
|
|
|
|
env_str_new2(s+1));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
env++;
|
|
|
|
}
|
2001-11-13 06:59:20 +03:00
|
|
|
FREE_ENVIRON(environ);
|
1999-01-20 07:59:39 +03:00
|
|
|
return hash;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2019-08-29 05:47:20 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.to_hash -> hash of name/value pairs
|
2019-08-29 05:47:20 +03:00
|
|
|
*
|
2020-02-14 08:18:48 +03:00
|
|
|
* Returns a Hash containing all name/value pairs from ENV:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
2020-02-14 08:26:19 +03:00
|
|
|
* ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
|
2019-08-29 05:47:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
env_f_to_hash(VALUE _)
|
|
|
|
{
|
|
|
|
return env_to_hash();
|
|
|
|
}
|
|
|
|
|
2018-09-20 18:06:56 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.to_h -> hash of name/value pairs
|
|
|
|
* ENV.to_h {|name, value| block } -> hash of name/value pairs
|
2018-09-20 18:06:56 +03:00
|
|
|
*
|
2020-02-22 04:25:54 +03:00
|
|
|
* With no block, returns a Hash containing all name/value pairs from ENV:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.to_h # => {"bar"=>"1", "foo"=>"0"}
|
|
|
|
* With a block, returns a Hash whose items are determined by the block.
|
|
|
|
* Each name/value pair in ENV is yielded to the block.
|
|
|
|
* The block must return a 2-element Array (name/value pair)
|
|
|
|
* that is added to the return Hash as a key and value:
|
|
|
|
* ENV.to_h { |name, value| [name.to_sym, value.to_i] } # => {:bar=>1, :foo=>0}
|
|
|
|
* Raises an exception if the block does not return an Array:
|
|
|
|
* ENV.to_h { |name, value| name } # Raises TypeError (wrong element type String (expected array))
|
|
|
|
* Raises an exception if the block returns an Array of the wrong size:
|
|
|
|
* ENV.to_h { |name, value| [name] } # Raises ArgumentError (element has wrong array length (expected 2, was 1))
|
2018-09-20 18:06:56 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_to_h(VALUE _)
|
2018-09-20 18:06:56 +03:00
|
|
|
{
|
|
|
|
VALUE hash = env_to_hash();
|
|
|
|
if (rb_block_given_p()) {
|
|
|
|
hash = rb_hash_to_h_block(hash);
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2019-05-04 16:37:38 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ENV.except(*keys) -> a_hash
|
|
|
|
*
|
|
|
|
* Returns a hash except the given keys from ENV and their values.
|
|
|
|
*
|
|
|
|
* ENV #=> {"LANG"="en_US.UTF-8", "TERM"=>"xterm-256color", "HOME"=>"/Users/rhc"}
|
|
|
|
* ENV.except("TERM","HOME") #=> {"LANG"="en_US.UTF-8"}
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
env_except(int argc, VALUE *argv, VALUE _)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
VALUE key, hash = env_to_hash();
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
key = argv[i];
|
|
|
|
rb_hash_delete(hash, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.reject { |name, value| block } -> hash of name/value pairs
|
|
|
|
* ENV.reject -> an_enumerator
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-22 04:25:54 +03:00
|
|
|
* Yields each environment variable name and its value as a 2-element Array.
|
|
|
|
* Returns a Hash whose items are determined by the block.
|
|
|
|
* When the block returns a truthy value, the name/value pair is added to the return Hash;
|
|
|
|
* otherwise the pair is ignored:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
|
|
|
|
* ENV.reject { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
|
|
|
|
* Returns an Enumerator if no block given:
|
|
|
|
* e = ENV.reject
|
|
|
|
* e.each { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2000-02-08 11:54:02 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_reject(VALUE _)
|
2000-02-08 11:54:02 +03:00
|
|
|
{
|
|
|
|
return rb_hash_delete_if(env_to_hash());
|
|
|
|
}
|
|
|
|
|
2020-05-10 18:24:14 +03:00
|
|
|
NORETURN(static VALUE env_freeze(VALUE self));
|
2019-06-15 02:53:42 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.freeze
|
2019-06-15 02:53:42 +03:00
|
|
|
*
|
2020-02-22 04:25:54 +03:00
|
|
|
* Raises an exception:
|
|
|
|
* ENV.freeze # Raises TypeError (cannot freeze ENV)
|
2019-06-15 02:53:42 +03:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
env_freeze(VALUE self)
|
|
|
|
{
|
|
|
|
rb_raise(rb_eTypeError, "cannot freeze ENV");
|
2020-05-10 18:24:14 +03:00
|
|
|
UNREACHABLE_RETURN(self);
|
2019-06-15 02:53:42 +03:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.shift -> [name, value] or nil
|
|
|
|
*
|
|
|
|
* Removes the first environment variable from ENV and returns
|
|
|
|
* a 2-element Array containing its name and value:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.to_hash # => {'bar' => '1', 'foo' => '0'}
|
|
|
|
* ENV.shift # => ['bar', '1']
|
|
|
|
* ENV.to_hash # => {'foo' => '0'}
|
|
|
|
* Exactly which environment variable is "first" is OS-dependent.
|
|
|
|
* See {About Ordering}[#class-ENV-label-About+Ordering].
|
|
|
|
*
|
2020-08-21 19:42:02 +03:00
|
|
|
* Returns +nil+ if the environment is empty.
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2003-01-07 10:36:40 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_shift(VALUE _)
|
2003-01-07 10:36:40 +03:00
|
|
|
{
|
|
|
|
char **env;
|
2014-06-27 00:33:34 +04:00
|
|
|
VALUE result = Qnil;
|
2003-01-07 10:36:40 +03:00
|
|
|
|
|
|
|
env = GET_ENVIRON(environ);
|
|
|
|
if (*env) {
|
|
|
|
char *s = strchr(*env, '=');
|
|
|
|
if (s) {
|
2003-06-06 13:24:59 +04:00
|
|
|
VALUE key = env_str_new(*env, s-*env);
|
2006-08-31 14:47:44 +04:00
|
|
|
VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
|
2019-02-27 06:26:09 +03:00
|
|
|
env_delete(key);
|
2014-06-27 00:33:34 +04:00
|
|
|
result = rb_assoc_new(key, val);
|
2003-01-07 10:36:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
FREE_ENVIRON(environ);
|
2014-06-27 00:33:34 +04:00
|
|
|
return result;
|
2003-01-07 10:36:40 +03:00
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.invert -> hash of value/name pairs
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2020-02-22 04:25:54 +03:00
|
|
|
* Returns a Hash whose keys are the ENV values,
|
|
|
|
* and whose values are the corresponding ENV names:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.invert # => {"1"=>"bar", "0"=>"foo"}
|
|
|
|
* For a duplicate ENV value, overwrites the hash entry:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '0')
|
|
|
|
* ENV.invert # => {"0"=>"foo"}
|
|
|
|
* Note that the order of the ENV processing is OS-dependent,
|
|
|
|
* which means that the order of overwriting is also OS-dependent.
|
|
|
|
* See {About Ordering}[#class-ENV-label-About+Ordering].
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2003-01-07 10:36:40 +03:00
|
|
|
static VALUE
|
2019-08-29 05:47:20 +03:00
|
|
|
env_invert(VALUE _)
|
2003-01-07 10:36:40 +03:00
|
|
|
{
|
|
|
|
return rb_hash_invert(env_to_hash());
|
|
|
|
}
|
|
|
|
|
2020-04-18 14:39:07 +03:00
|
|
|
static void
|
|
|
|
keylist_delete(VALUE keys, VALUE key)
|
|
|
|
{
|
|
|
|
long keylen, elen;
|
|
|
|
const char *keyptr, *eptr;
|
|
|
|
RSTRING_GETMEM(key, keyptr, keylen);
|
|
|
|
for (long i=0; i<RARRAY_LEN(keys); i++) {
|
|
|
|
VALUE e = RARRAY_AREF(keys, i);
|
|
|
|
RSTRING_GETMEM(e, eptr, elen);
|
|
|
|
if (elen != keylen) continue;
|
|
|
|
if (!ENVNMATCH(keyptr, eptr, elen)) continue;
|
|
|
|
rb_ary_delete_at(keys, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_replace_i(VALUE key, VALUE val, VALUE keys)
|
2003-01-07 10:36:40 +03:00
|
|
|
{
|
2020-04-18 14:39:07 +03:00
|
|
|
env_name(key);
|
2019-02-27 06:26:05 +03:00
|
|
|
env_aset(key, val);
|
2020-04-18 14:39:07 +03:00
|
|
|
|
|
|
|
keylist_delete(keys, key);
|
2003-01-07 10:36:40 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2019-12-13 22:46:54 +03:00
|
|
|
* ENV.replace(hash) -> ENV
|
|
|
|
*
|
|
|
|
* Replaces the entire content of the environment variables
|
|
|
|
* with the name/value pairs in the given +hash+;
|
|
|
|
* returns ENV.
|
2011-06-30 04:20:15 +04:00
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* Replaces the content of ENV with the given pairs:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1') # => ENV
|
|
|
|
* ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
|
|
|
|
*
|
2020-02-22 04:25:54 +03:00
|
|
|
* Raises an exception if a name or value is invalid
|
|
|
|
* (see {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values]):
|
|
|
|
* ENV.replace('foo' => '0', :bar => '1') # Raises TypeError (no implicit conversion of Symbol into String)
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => 1) # Raises TypeError (no implicit conversion of Integer into String)
|
|
|
|
* ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2003-01-07 10:36:40 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_replace(VALUE env, VALUE hash)
|
2003-01-07 10:36:40 +03:00
|
|
|
{
|
2015-06-26 02:06:48 +03:00
|
|
|
VALUE keys;
|
2003-08-01 06:52:21 +04:00
|
|
|
long i;
|
2003-01-07 10:36:40 +03:00
|
|
|
|
2020-04-18 15:30:11 +03:00
|
|
|
keys = env_keys(TRUE);
|
2003-01-07 10:36:40 +03:00
|
|
|
if (env == hash) return env;
|
|
|
|
hash = to_hash(hash);
|
2004-09-29 09:15:33 +04:00
|
|
|
rb_hash_foreach(hash, env_replace_i, keys);
|
2003-01-07 10:36:40 +03:00
|
|
|
|
2006-09-02 18:42:08 +04:00
|
|
|
for (i=0; i<RARRAY_LEN(keys); i++) {
|
2019-02-27 06:26:09 +03:00
|
|
|
env_delete(RARRAY_AREF(keys, i));
|
2003-01-07 10:36:40 +03:00
|
|
|
}
|
2015-06-26 02:06:48 +03:00
|
|
|
RB_GC_GUARD(keys);
|
2003-01-07 10:36:40 +03:00
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2004-09-29 09:15:33 +04:00
|
|
|
static int
|
2019-08-27 05:53:39 +03:00
|
|
|
env_update_i(VALUE key, VALUE val, VALUE _)
|
2003-01-07 10:36:40 +03:00
|
|
|
{
|
2019-10-01 06:23:25 +03:00
|
|
|
env_aset(key, val);
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
env_update_block_i(VALUE key, VALUE val, VALUE _)
|
|
|
|
{
|
|
|
|
VALUE oldval = rb_f_getenv(Qnil, key);
|
|
|
|
if (!NIL_P(oldval)) {
|
|
|
|
val = rb_yield_values(3, key, oldval, val);
|
2003-01-07 10:36:40 +03:00
|
|
|
}
|
2019-02-27 06:26:05 +03:00
|
|
|
env_aset(key, val);
|
2003-01-07 10:36:40 +03:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2011-06-30 04:20:15 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.update(hash) -> ENV
|
|
|
|
* ENV.update(hash) { |name, env_val, hash_val| block } -> ENV
|
|
|
|
* ENV.merge!(hash) -> ENV
|
|
|
|
* ENV.merge!(hash) { |name, env_val, hash_val| block } -> ENV
|
|
|
|
*
|
|
|
|
* ENV.update is an alias for ENV.merge!.
|
|
|
|
*
|
|
|
|
* Adds to ENV each key/value pair in the given +hash+; returns ENV:
|
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
|
|
|
* ENV.merge!('baz' => '2', 'bat' => '3') # => {"bar"=>"1", "bat"=>"3", "baz"=>"2", "foo"=>"0"}
|
|
|
|
* Deletes the ENV entry for a hash value that is +nil+:
|
|
|
|
* ENV.merge!('baz' => nil, 'bat' => nil) # => {"bar"=>"1", "foo"=>"0"}
|
|
|
|
* For an already-existing name, if no block given, overwrites the ENV value:
|
|
|
|
* ENV.merge!('foo' => '4') # => {"bar"=>"1", "foo"=>"4"}
|
|
|
|
* For an already-existing name, if block given,
|
|
|
|
* yields the name, its ENV value, and its hash value;
|
2020-02-22 18:32:37 +03:00
|
|
|
* the block's return value becomes the new name:
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.merge!('foo' => '5') { |name, env_val, hash_val | env_val + hash_val } # => {"bar"=>"1", "foo"=>"45"}
|
|
|
|
* Raises an exception if a name or value is invalid
|
|
|
|
* (see {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values]);
|
2020-02-22 18:32:37 +03:00
|
|
|
* ENV.replace('foo' => '0', 'bar' => '1')
|
2020-02-22 04:25:54 +03:00
|
|
|
* ENV.merge!('foo' => '6', :bar => '7', 'baz' => '9') # Raises TypeError (no implicit conversion of Symbol into String)
|
2020-02-22 18:32:37 +03:00
|
|
|
* ENV # => {"bar"=>"1", "foo"=>"6"}
|
|
|
|
* ENV.merge!('foo' => '7', 'bar' => 8, 'baz' => '9') # Raises TypeError (no implicit conversion of Integer into String)
|
|
|
|
* ENV # => {"bar"=>"1", "foo"=>"7"}
|
2020-02-22 04:25:54 +03:00
|
|
|
* Raises an exception if the block returns an invalid name:
|
|
|
|
* (see {Invalid Names and Values}[#class-ENV-label-Invalid+Names+and+Values]):
|
|
|
|
* ENV.merge!('bat' => '8', 'foo' => '9') { |name, env_val, hash_val | 10 } # Raises TypeError (no implicit conversion of Integer into String)
|
2020-02-22 18:32:37 +03:00
|
|
|
* ENV # => {"bar"=>"1", "bat"=>"8", "foo"=>"7"}
|
2020-02-22 04:25:54 +03:00
|
|
|
*
|
|
|
|
* Note that for the exceptions above,
|
|
|
|
* hash pairs preceding an invalid name or value are processed normally;
|
|
|
|
* those following are ignored.
|
2011-06-30 04:20:15 +04:00
|
|
|
*/
|
2003-01-07 10:36:40 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
env_update(VALUE env, VALUE hash)
|
2003-01-07 10:36:40 +03:00
|
|
|
{
|
|
|
|
if (env == hash) return env;
|
|
|
|
hash = to_hash(hash);
|
2019-10-01 06:23:25 +03:00
|
|
|
rb_foreach_func *func = rb_block_given_p() ?
|
|
|
|
env_update_block_i : env_update_i;
|
|
|
|
rb_hash_foreach(hash, func, 0);
|
2003-01-07 10:36:40 +03:00
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2003-12-23 19:21:17 +03:00
|
|
|
/*
|
2020-05-16 00:11:42 +03:00
|
|
|
* A \Hash maps each of its unique keys to a specific value.
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* A \Hash has certain similarities to an \Array, but:
|
|
|
|
* - An \Array index is always an \Integer.
|
|
|
|
* - A \Hash key can be (almost) any object.
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* === \Hash \Data Syntax
|
2011-11-04 11:19:23 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* The older syntax for \Hash data uses the "hash rocket," <tt>=></tt>:
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* h = {:foo => 0, :bar => 1, :baz => 2}
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
2011-11-04 11:19:23 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* Alternatively, but only for a \Hash key that's a \Symbol,
|
|
|
|
* you can use a newer JSON-style syntax,
|
|
|
|
* where each bareword becomes a \Symbol:
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
2011-11-04 11:19:23 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* You can also use a \String in place of a bareword:
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* h = {'foo': 0, 'bar': 1, 'baz': 2}
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* And you can mix the styles:
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* h = {foo: 0, :bar => 1, 'baz': 2}
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* But it's an error to try the JSON-style syntax
|
|
|
|
* for a key that's not a bareword or a String:
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* # Raises SyntaxError (syntax error, unexpected ':', expecting =>):
|
|
|
|
* h = {0: 'zero'}
|
2011-11-04 11:19:23 +04:00
|
|
|
*
|
2011-10-05 00:57:07 +04:00
|
|
|
* === Common Uses
|
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* You can use a \Hash to give names to objects:
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* person = {name: 'Matz', language: 'Ruby'}
|
|
|
|
* person # => {:name=>"Matz", :language=>"Ruby"}
|
2011-11-04 11:19:23 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* You can use a \Hash to give names to method arguments:
|
|
|
|
*
|
|
|
|
* def some_method(hash)
|
|
|
|
* p hash
|
|
|
|
* end
|
|
|
|
* some_method({foo: 0, bar: 1, baz: 2}) # => {:foo=>0, :bar=>1, :baz=>2}
|
2011-11-04 11:19:23 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* Note: when the last argument in a method call is a \Hash,
|
|
|
|
* the curly braces may be omitted:
|
2011-10-05 00:57:07 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* some_method(foo: 0, bar: 1, baz: 2) # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
*
|
2020-06-07 21:46:43 +03:00
|
|
|
* You can use a \Hash to initialize an object:
|
2020-05-16 00:11:42 +03:00
|
|
|
*
|
|
|
|
* class Dev
|
|
|
|
* attr_accessor :name, :language
|
|
|
|
* def initialize(hash)
|
|
|
|
* self.name = hash[:name]
|
|
|
|
* self.language = hash[:language]
|
|
|
|
* end
|
2011-10-05 00:57:07 +04:00
|
|
|
* end
|
2020-05-16 00:11:42 +03:00
|
|
|
* matz = Dev.new(name: 'Matz', language: 'Ruby')
|
|
|
|
* matz # => #<Dev: @name="Matz", @language="Ruby">
|
|
|
|
*
|
|
|
|
* === Creating a \Hash
|
|
|
|
*
|
|
|
|
* Here are three ways to create a \Hash:
|
|
|
|
*
|
|
|
|
* - \Method <tt>Hash.new</tt>
|
|
|
|
* - \Method <tt>Hash[]</tt>
|
|
|
|
* - Literal form: <tt>{}</tt>.
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* You can create a \Hash by calling method Hash.new.
|
|
|
|
*
|
|
|
|
* Create an empty Hash:
|
|
|
|
*
|
|
|
|
* h = Hash.new
|
|
|
|
* h # => {}
|
|
|
|
* h.class # => Hash
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* You can create a \Hash by calling method Hash.[].
|
|
|
|
*
|
|
|
|
* Create an empty Hash:
|
|
|
|
*
|
|
|
|
* h = Hash[]
|
|
|
|
* h # => {}
|
|
|
|
*
|
|
|
|
* Create a \Hash with initial entries:
|
|
|
|
*
|
|
|
|
* h = Hash[foo: 0, bar: 1, baz: 2]
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
*
|
|
|
|
* ---
|
|
|
|
*
|
|
|
|
* You can create a \Hash by using its literal form (curly braces).
|
|
|
|
*
|
|
|
|
* Create an empty \Hash:
|
|
|
|
*
|
|
|
|
* h = {}
|
|
|
|
* h # => {}
|
2007-06-29 09:15:46 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* Create a \Hash with initial entries:
|
2012-02-24 03:03:39 +04:00
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* === \Hash Value Basics
|
|
|
|
*
|
|
|
|
* The simplest way to retrieve a \Hash value (instance method #[]):
|
|
|
|
*
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h[:foo] # => 0
|
|
|
|
*
|
|
|
|
* The simplest way to create or update a \Hash value (instance method #[]=):
|
|
|
|
*
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h[:bat] = 3 # => 3
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
|
|
|
|
* h[:foo] = 4 # => 4
|
|
|
|
* h # => {:foo=>4, :bar=>1, :baz=>2, :bat=>3}
|
|
|
|
*
|
2020-06-07 21:46:43 +03:00
|
|
|
* The simplest way to delete a \Hash entry (instance method #delete):
|
2020-05-16 00:11:42 +03:00
|
|
|
*
|
|
|
|
* h = {foo: 0, bar: 1, baz: 2}
|
|
|
|
* h.delete(:bar) # => 1
|
|
|
|
* h # => {:foo=>0, :baz=>2}
|
|
|
|
*
|
|
|
|
* === Entry Order
|
|
|
|
*
|
2020-06-07 21:46:43 +03:00
|
|
|
* A \Hash object presents its entries in the order of their creation. This is seen in:
|
2020-05-16 00:11:42 +03:00
|
|
|
*
|
|
|
|
* - Iterative methods such as <tt>each</tt>, <tt>each_key</tt>, <tt>each_pair</tt>, <tt>each_value</tt>.
|
|
|
|
* - Other order-sensitive methods such as <tt>shift</tt>, <tt>keys</tt>, <tt>values</tt>.
|
2020-06-07 21:46:43 +03:00
|
|
|
* - The \String returned by method <tt>inspect</tt>.
|
2020-05-16 00:11:42 +03:00
|
|
|
*
|
2020-05-24 04:35:05 +03:00
|
|
|
* A new \Hash has its initial ordering per the given entries:
|
2020-05-16 00:11:42 +03:00
|
|
|
*
|
|
|
|
* h = Hash[foo: 0, bar: 1]
|
|
|
|
* h # => {:foo=>0, :bar=>1}
|
|
|
|
*
|
|
|
|
* New entries are added at the end:
|
|
|
|
*
|
|
|
|
* h[:baz] = 2
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>2}
|
|
|
|
*
|
|
|
|
* Updating a value does not affect the order:
|
|
|
|
*
|
|
|
|
* h[:baz] = 3
|
|
|
|
* h # => {:foo=>0, :bar=>1, :baz=>3}
|
|
|
|
*
|
|
|
|
* But re-creating a deleted entry can affect the order:
|
|
|
|
*
|
|
|
|
* h.delete(:foo)
|
|
|
|
* h[:foo] = 5
|
|
|
|
* h # => {:bar=>1, :baz=>3, :foo=>5}
|
|
|
|
*
|
|
|
|
* === \Hash Keys
|
|
|
|
*
|
|
|
|
* ==== \Hash Key Equivalence
|
|
|
|
*
|
2020-06-07 21:46:43 +03:00
|
|
|
* Two objects are treated as the same \hash key when their <code>hash</code> value
|
2012-02-24 03:03:39 +04:00
|
|
|
* is identical and the two objects are <code>eql?</code> to each other.
|
|
|
|
*
|
2020-05-16 00:11:42 +03:00
|
|
|
* ==== Modifying an Active \Hash Key
|
|
|
|
*
|
|
|
|
* Modifying a \Hash key while it is in use damages the hash's index.
|
|
|
|
*
|
|
|
|
* This \Hash has keys that are Arrays:
|
|
|
|
*
|
|
|
|
* a0 = [ :foo, :bar ]
|
|
|
|
* a1 = [ :baz, :bat ]
|
|
|
|
* h = {a0 => 0, a1 => 1}
|
|
|
|
* h.include?(a0) # => true
|
|
|
|
* h[a0] # => 0
|
|
|
|
* a0.hash # => 110002110
|
|
|
|
*
|
|
|
|
* Modifying array element <tt>a0[0]</tt> changes its hash value:
|
|
|
|
*
|
|
|
|
* a0[0] = :bam
|
|
|
|
* a0.hash # => 1069447059
|
|
|
|
*
|
|
|
|
* And damages the \Hash index:
|
|
|
|
*
|
|
|
|
* h.include?(a0) # => false
|
|
|
|
* h[a0] # => nil
|
|
|
|
*
|
|
|
|
* You can repair the hash index using method +rehash+:
|
|
|
|
*
|
|
|
|
* h.rehash # => {[:bam, :bar]=>0, [:baz, :bat]=>1}
|
|
|
|
* h.include?(a0) # => true
|
|
|
|
* h[a0] # => 0
|
|
|
|
*
|
|
|
|
* A \String key is always safe.
|
2020-06-07 21:46:43 +03:00
|
|
|
* That's because an unfrozen \String
|
2020-05-16 00:11:42 +03:00
|
|
|
* passed as a key will be replaced by a duplicated and frozen \String:
|
|
|
|
*
|
|
|
|
* s = 'foo'
|
|
|
|
* s.frozen? # => false
|
|
|
|
* h = {s => 0}
|
|
|
|
* first_key = h.keys.first
|
|
|
|
* first_key.frozen? # => true
|
|
|
|
*
|
|
|
|
* ==== User-Defined \Hash Keys
|
|
|
|
*
|
2020-07-29 21:45:16 +03:00
|
|
|
* To be useable as a \Hash key, objects must implement the methods <code>hash</code> and <code>eql?</code>.
|
|
|
|
* Note: this requirement does not apply if the \Hash uses #compare_by_id since comparison will then rely on
|
|
|
|
* the keys' object id instead of <code>hash</code> and <code>eql?</code>.
|
|
|
|
*
|
|
|
|
* \Object defines basic implementation for <code>hash</code> and <code>eq?</code> that makes each object
|
|
|
|
* a distinct key. Typically, user-defined classes will want to override these methods to provide meaningful
|
|
|
|
* behavior, or for example inherit \Struct that has useful definitions for these.
|
2012-02-24 03:03:39 +04:00
|
|
|
*
|
2012-03-04 17:59:23 +04:00
|
|
|
* A typical implementation of <code>hash</code> is based on the
|
2012-02-24 03:03:39 +04:00
|
|
|
* object's data while <code>eql?</code> is usually aliased to the overridden
|
|
|
|
* <code>==</code> method:
|
|
|
|
*
|
|
|
|
* class Book
|
|
|
|
* attr_reader :author, :title
|
|
|
|
*
|
|
|
|
* def initialize(author, title)
|
|
|
|
* @author = author
|
|
|
|
* @title = title
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* def ==(other)
|
2020-07-29 21:45:16 +03:00
|
|
|
* self.class === other &&
|
|
|
|
* other.author == @author &&
|
2012-02-24 03:03:39 +04:00
|
|
|
* other.title == @title
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* alias eql? ==
|
|
|
|
*
|
|
|
|
* def hash
|
|
|
|
* @author.hash ^ @title.hash # XOR
|
|
|
|
* end
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* book1 = Book.new 'matz', 'Ruby in a Nutshell'
|
|
|
|
* book2 = Book.new 'matz', 'Ruby in a Nutshell'
|
|
|
|
*
|
|
|
|
* reviews = {}
|
|
|
|
*
|
|
|
|
* reviews[book1] = 'Great reference!'
|
|
|
|
* reviews[book2] = 'Nice and compact!'
|
|
|
|
*
|
|
|
|
* reviews.length #=> 1
|
|
|
|
*
|
2020-03-27 06:33:39 +03:00
|
|
|
* === Default Values
|
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* The methods #[], #values_at and #dig need to return the value associated to a certain key
|
|
|
|
* When that key is not found, that value will be determined by its default proc (if any)
|
|
|
|
* or else its default (initially `nil`).
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
|
|
|
* You can retrieve the default value with method #default:
|
|
|
|
*
|
|
|
|
* h = Hash.new
|
|
|
|
* h.default # => nil
|
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* You can set the default value by passing an argument to method Hash.new or
|
|
|
|
* with method #default=
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* h = Hash.new(-1)
|
|
|
|
* h.default # => -1
|
|
|
|
* h.default = 0
|
|
|
|
* h.default # => 0
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* This default value is returned for #[], #values_at and #dig when a key is
|
|
|
|
* not found:
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* counts = {foo: 42}
|
|
|
|
* counts.default # => nil (default)
|
|
|
|
* counts[:foo] = 42
|
|
|
|
* counts[:bar] # => nil
|
|
|
|
* counts.default = 0
|
|
|
|
* counts[:bar] # => 0
|
|
|
|
* counts.values_at(:foo, :bar, :baz) # => [42, 0, 0]
|
|
|
|
* counts.dig(:bar) # => 0
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* Note that the default value is used without being duplicated. It is not advised to set
|
|
|
|
* the default value to a mutable object:
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* synonyms = Hash.new([])
|
|
|
|
* synonyms[:hello] # => []
|
|
|
|
* synonyms[:hello] << :hi # => [:hi], but this mutates the default!
|
|
|
|
* synonyms.default # => [:hi]
|
|
|
|
* synonyms[:world] << :universe
|
|
|
|
* synonyms[:world] # => [:hi, :universe], oops
|
|
|
|
* synonyms.keys # => [], oops
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* To use a mutable object as default, it is recommended to use a default proc
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
|
|
|
* ==== Default \Proc
|
|
|
|
*
|
|
|
|
* When the default proc for a \Hash is set (i.e., not +nil+),
|
|
|
|
* the default value returned by method #[] is determined by the default proc alone.
|
|
|
|
*
|
|
|
|
* You can retrieve the default proc with method #default_proc:
|
|
|
|
*
|
|
|
|
* h = Hash.new
|
|
|
|
* h.default_proc # => nil
|
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* You can set the default proc by calling Hash.new with a block or
|
|
|
|
* calling the method #default_proc=
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
|
|
|
* h = Hash.new { |hash, key| "Default value for #{key}" }
|
|
|
|
* h.default_proc.class # => Proc
|
2020-07-29 23:04:55 +03:00
|
|
|
* h.default_proc = proc { |hash, key| "Default value for #{key.inspect}" }
|
2020-03-27 06:33:39 +03:00
|
|
|
* h.default_proc.class # => Proc
|
|
|
|
*
|
|
|
|
* When the default proc is set (i.e., not +nil+)
|
|
|
|
* and method #[] is called with with a non-existent key,
|
|
|
|
* #[] calls the default proc with both the \Hash object itself and the missing key,
|
|
|
|
* then returns the proc's return value:
|
|
|
|
*
|
|
|
|
* h = Hash.new { |hash, key| "Default value for #{key}" }
|
|
|
|
* h[:nosuch] # => "Default value for nosuch"
|
|
|
|
*
|
|
|
|
* Note that in the example above no entry for key +:nosuch+ is created:
|
|
|
|
*
|
|
|
|
* h.include?(:nosuch) # => false
|
|
|
|
*
|
|
|
|
* However, the proc itself can add a new entry:
|
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* synonyms = Hash.new { |hash, key| hash[key] = [] }
|
|
|
|
* synonyms.include?(:hello) # => false
|
|
|
|
* synonyms[:hello] << :hi # => [:hi]
|
|
|
|
* synonyms[:world] << :universe # => [:universe]
|
|
|
|
* synonyms.keys # => [:hello, :world]
|
2020-03-27 06:33:39 +03:00
|
|
|
*
|
2020-07-29 23:04:55 +03:00
|
|
|
* Note that setting the default proc will clear the default value and vice versa.
|
2003-12-23 19:21:17 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
Init_Hash(void)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2008-06-09 13:25:32 +04:00
|
|
|
#undef rb_intern
|
2008-08-16 04:20:31 +04:00
|
|
|
#define rb_intern(str) rb_intern_const(str)
|
2001-12-10 10:18:16 +03:00
|
|
|
id_hash = rb_intern("hash");
|
|
|
|
id_default = rb_intern("default");
|
2014-02-20 06:27:30 +04:00
|
|
|
id_flatten_bang = rb_intern("flatten!");
|
2019-08-01 05:20:37 +03:00
|
|
|
id_hash_iter_lev = rb_make_internal_id();
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_cHash = rb_define_class("Hash", rb_cObject);
|
|
|
|
|
|
|
|
rb_include_module(rb_cHash, rb_mEnumerable);
|
|
|
|
|
* probes.d: add DTrace probe declarations. [ruby-core:27448]
* array.c (empty_ary_alloc, ary_new): added array create DTrace probe.
* compile.c (rb_insns_name): allowing DTrace probes to access
instruction sequence name.
* Makefile.in: translate probes.d file to appropriate header file.
* common.mk: declare dependencies on the DTrace header.
* configure.in: add a test for existence of DTrace.
* eval.c (setup_exception): add a probe for when an exception is
raised.
* gc.c: Add DTrace probes for mark begin and end, and sweep begin and
end.
* hash.c (empty_hash_alloc): Add a probe for hash allocation.
* insns.def: Add probes for function entry and return.
* internal.h: function declaration for compile.c change.
* load.c (rb_f_load): add probes for `load` entry and exit, require
entry and exit, and wrapping search_required for load path search.
* object.c (rb_obj_alloc): added a probe for general object creation.
* parse.y (yycompile0): added a probe around parse and compile phase.
* string.c (empty_str_alloc, str_new): DTrace probes for string
allocation.
* test/dtrace/*: tests for DTrace probes.
* vm.c (vm_invoke_proc): add probes for function return on exception
raise, hash create, and instruction sequence execution.
* vm_core.h: add probe declarations for function entry and exit.
* vm_dump.c: add probes header file.
* vm_eval.c (vm_call0_cfunc, vm_call0_cfunc_with_frame): add probe on
function entry and return.
* vm_exec.c: expose instruction number to instruction name function.
* vm_insnshelper.c: add function entry and exit probes for cfunc
methods.
* vm_insnhelper.h: vm usage information is always collected, so
uncomment the functions.
12 19:14:50 2012 Akinori MUSHA <knu@iDaemons.org>
* configure.in (isinf, isnan): isinf() and isnan() are macros on
DragonFly which cannot be found by AC_REPLACE_FUNCS(). This
workaround enforces the fact that they exist on DragonFly.
12 15:59:38 2012 Shugo Maeda <shugo@ruby-lang.org>
* vm_core.h (rb_call_info_t::refinements), compile.c (new_callinfo),
vm_insnhelper.c (vm_search_method): revert r37616 because it's too
slow. [ruby-dev:46477]
* test/ruby/test_refinement.rb (test_inline_method_cache): skip
the test until the bug is fixed efficiently.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37631 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-11-13 01:52:12 +04:00
|
|
|
rb_define_alloc_func(rb_cHash, empty_hash_alloc);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_singleton_method(rb_cHash, "[]", rb_hash_s_create, -1);
|
2007-08-24 21:47:09 +04:00
|
|
|
rb_define_singleton_method(rb_cHash, "try_convert", rb_hash_s_try_convert, 1);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "initialize", rb_hash_initialize, -1);
|
2019-10-21 11:29:21 +03:00
|
|
|
rb_define_method(rb_cHash, "initialize_copy", rb_hash_replace, 1);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "rehash", rb_hash_rehash, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cHash, "to_hash", rb_hash_to_hash, 0);
|
|
|
|
rb_define_method(rb_cHash, "to_h", rb_hash_to_h, 0);
|
|
|
|
rb_define_method(rb_cHash, "to_a", rb_hash_to_a, 0);
|
|
|
|
rb_define_method(rb_cHash, "inspect", rb_hash_inspect, 0);
|
2009-05-29 12:08:25 +04:00
|
|
|
rb_define_alias(rb_cHash, "to_s", "inspect");
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "to_proc", rb_hash_to_proc, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cHash, "==", rb_hash_equal, 1);
|
|
|
|
rb_define_method(rb_cHash, "[]", rb_hash_aref, 1);
|
|
|
|
rb_define_method(rb_cHash, "hash", rb_hash_hash, 0);
|
|
|
|
rb_define_method(rb_cHash, "eql?", rb_hash_eql, 1);
|
|
|
|
rb_define_method(rb_cHash, "fetch", rb_hash_fetch_m, -1);
|
|
|
|
rb_define_method(rb_cHash, "[]=", rb_hash_aset, 2);
|
|
|
|
rb_define_method(rb_cHash, "store", rb_hash_aset, 2);
|
|
|
|
rb_define_method(rb_cHash, "default", rb_hash_default, -1);
|
|
|
|
rb_define_method(rb_cHash, "default=", rb_hash_set_default, 1);
|
|
|
|
rb_define_method(rb_cHash, "default_proc", rb_hash_default_proc, 0);
|
|
|
|
rb_define_method(rb_cHash, "default_proc=", rb_hash_set_default_proc, 1);
|
|
|
|
rb_define_method(rb_cHash, "key", rb_hash_key, 1);
|
|
|
|
rb_define_method(rb_cHash, "index", rb_hash_index, 1);
|
|
|
|
rb_define_method(rb_cHash, "size", rb_hash_size, 0);
|
|
|
|
rb_define_method(rb_cHash, "length", rb_hash_size, 0);
|
|
|
|
rb_define_method(rb_cHash, "empty?", rb_hash_empty_p, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cHash, "each_value", rb_hash_each_value, 0);
|
|
|
|
rb_define_method(rb_cHash, "each_key", rb_hash_each_key, 0);
|
|
|
|
rb_define_method(rb_cHash, "each_pair", rb_hash_each_pair, 0);
|
|
|
|
rb_define_method(rb_cHash, "each", rb_hash_each_pair, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2019-12-26 09:50:34 +03:00
|
|
|
rb_define_method(rb_cHash, "transform_keys", rb_hash_transform_keys, -1);
|
|
|
|
rb_define_method(rb_cHash, "transform_keys!", rb_hash_transform_keys_bang, -1);
|
2016-09-08 05:33:18 +03:00
|
|
|
rb_define_method(rb_cHash, "transform_values", rb_hash_transform_values, 0);
|
|
|
|
rb_define_method(rb_cHash, "transform_values!", rb_hash_transform_values_bang, 0);
|
2016-08-09 11:54:15 +03:00
|
|
|
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "keys", rb_hash_keys, 0);
|
|
|
|
rb_define_method(rb_cHash, "values", rb_hash_values, 0);
|
|
|
|
rb_define_method(rb_cHash, "values_at", rb_hash_values_at, -1);
|
|
|
|
rb_define_method(rb_cHash, "fetch_values", rb_hash_fetch_values, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cHash, "shift", rb_hash_shift, 0);
|
|
|
|
rb_define_method(rb_cHash, "delete", rb_hash_delete_m, 1);
|
|
|
|
rb_define_method(rb_cHash, "delete_if", rb_hash_delete_if, 0);
|
|
|
|
rb_define_method(rb_cHash, "keep_if", rb_hash_keep_if, 0);
|
|
|
|
rb_define_method(rb_cHash, "select", rb_hash_select, 0);
|
|
|
|
rb_define_method(rb_cHash, "select!", rb_hash_select_bang, 0);
|
2018-02-25 16:52:07 +03:00
|
|
|
rb_define_method(rb_cHash, "filter", rb_hash_select, 0);
|
|
|
|
rb_define_method(rb_cHash, "filter!", rb_hash_select_bang, 0);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "reject", rb_hash_reject, 0);
|
|
|
|
rb_define_method(rb_cHash, "reject!", rb_hash_reject_bang, 0);
|
2017-10-21 09:08:33 +03:00
|
|
|
rb_define_method(rb_cHash, "slice", rb_hash_slice, -1);
|
2019-05-04 16:37:38 +03:00
|
|
|
rb_define_method(rb_cHash, "except", rb_hash_except, -1);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "clear", rb_hash_clear, 0);
|
|
|
|
rb_define_method(rb_cHash, "invert", rb_hash_invert, 0);
|
2018-09-19 04:59:26 +03:00
|
|
|
rb_define_method(rb_cHash, "update", rb_hash_update, -1);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "replace", rb_hash_replace, 1);
|
2018-09-19 04:59:26 +03:00
|
|
|
rb_define_method(rb_cHash, "merge!", rb_hash_update, -1);
|
|
|
|
rb_define_method(rb_cHash, "merge", rb_hash_merge, -1);
|
2007-06-22 20:26:07 +04:00
|
|
|
rb_define_method(rb_cHash, "assoc", rb_hash_assoc, 1);
|
|
|
|
rb_define_method(rb_cHash, "rassoc", rb_hash_rassoc, 1);
|
|
|
|
rb_define_method(rb_cHash, "flatten", rb_hash_flatten, -1);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "compact", rb_hash_compact, 0);
|
|
|
|
rb_define_method(rb_cHash, "compact!", rb_hash_compact_bang, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "include?", rb_hash_has_key, 1);
|
|
|
|
rb_define_method(rb_cHash, "member?", rb_hash_has_key, 1);
|
|
|
|
rb_define_method(rb_cHash, "has_key?", rb_hash_has_key, 1);
|
|
|
|
rb_define_method(rb_cHash, "has_value?", rb_hash_has_value, 1);
|
|
|
|
rb_define_method(rb_cHash, "key?", rb_hash_has_key, 1);
|
|
|
|
rb_define_method(rb_cHash, "value?", rb_hash_has_value, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_method(rb_cHash, "compare_by_identity", rb_hash_compare_by_id, 0);
|
|
|
|
rb_define_method(rb_cHash, "compare_by_identity?", rb_hash_compare_by_id_p, 0);
|
2006-09-11 12:09:19 +04:00
|
|
|
|
2017-12-11 01:36:28 +03:00
|
|
|
rb_define_method(rb_cHash, "any?", rb_hash_any_p, -1);
|
2015-11-09 15:27:26 +03:00
|
|
|
rb_define_method(rb_cHash, "dig", rb_hash_dig, -1);
|
2014-07-18 17:16:48 +04:00
|
|
|
|
2015-11-10 08:02:02 +03:00
|
|
|
rb_define_method(rb_cHash, "<=", rb_hash_le, 1);
|
|
|
|
rb_define_method(rb_cHash, "<", rb_hash_lt, 1);
|
|
|
|
rb_define_method(rb_cHash, ">=", rb_hash_ge, 1);
|
|
|
|
rb_define_method(rb_cHash, ">", rb_hash_gt, 1);
|
|
|
|
|
2019-04-17 09:48:03 +03:00
|
|
|
rb_define_method(rb_cHash, "deconstruct_keys", rb_hash_deconstruct_keys, 1);
|
|
|
|
|
2020-01-06 12:22:43 +03:00
|
|
|
rb_define_singleton_method(rb_cHash, "ruby2_keywords_hash?", rb_hash_s_ruby2_keywords_hash_p, 1);
|
|
|
|
rb_define_singleton_method(rb_cHash, "ruby2_keywords_hash", rb_hash_s_ruby2_keywords_hash, 1);
|
|
|
|
|
2011-06-29 07:09:34 +04:00
|
|
|
/* Document-class: ENV
|
|
|
|
*
|
|
|
|
* ENV is a hash-like accessor for environment variables.
|
2019-11-05 02:03:01 +03:00
|
|
|
*
|
|
|
|
* === Interaction with the Operating System
|
2019-12-13 22:46:54 +03:00
|
|
|
*
|
2019-11-05 02:03:01 +03:00
|
|
|
* The ENV object interacts with the operating system's environment variables:
|
2019-12-13 22:46:54 +03:00
|
|
|
*
|
2019-11-05 02:03:01 +03:00
|
|
|
* - When you get the value for a name in ENV, the value is retrieved from among the current environment variables.
|
|
|
|
* - When you create or set a name-value pair in ENV, the name and value are immediately set in the environment variables.
|
|
|
|
* - When you delete a name-value pair in ENV, it is immediately deleted from the environment variables.
|
2019-11-05 02:07:59 +03:00
|
|
|
*
|
2019-11-05 02:03:01 +03:00
|
|
|
* === Names and Values
|
|
|
|
*
|
2019-12-13 22:46:54 +03:00
|
|
|
* Generally, a name or value is a String.
|
|
|
|
*
|
|
|
|
* ==== Valid Names and Values
|
|
|
|
*
|
|
|
|
* Each name or value must be one of the following:
|
|
|
|
*
|
|
|
|
* - A String.
|
|
|
|
* - An object that responds to \#to_str by returning a String, in which case that String will be used as the name or value.
|
|
|
|
*
|
|
|
|
* ==== Invalid Names and Values
|
|
|
|
*
|
|
|
|
* A new name:
|
|
|
|
*
|
|
|
|
* - May not be the empty string:
|
|
|
|
* ENV[''] = '0'
|
|
|
|
* # Raises Errno::EINVAL (Invalid argument - ruby_setenv())
|
|
|
|
*
|
|
|
|
* - May not contain character <code>"="</code>:
|
|
|
|
* ENV['='] = '0'
|
|
|
|
* # Raises Errno::EINVAL (Invalid argument - ruby_setenv(=))
|
|
|
|
*
|
|
|
|
* A new name or value:
|
|
|
|
*
|
|
|
|
* - May not be a non-String that does not respond to \#to_str:
|
|
|
|
*
|
|
|
|
* ENV['foo'] = Object.new
|
|
|
|
* # Raises TypeError (no implicit conversion of Object into String)
|
|
|
|
* ENV[Object.new] = '0'
|
|
|
|
* # Raises TypeError (no implicit conversion of Object into String)
|
|
|
|
*
|
|
|
|
* - May not contain the NUL character <code>"\0"</code>:
|
|
|
|
*
|
|
|
|
* ENV['foo'] = "\0"
|
|
|
|
* # Raises ArgumentError (bad environment variable value: contains null byte)
|
|
|
|
* ENV["\0"] == '0'
|
|
|
|
* # Raises ArgumentError (bad environment variable name: contains null byte)
|
|
|
|
*
|
|
|
|
* - May not have an ASCII-incompatible encoding such as UTF-16LE or ISO-2022-JP:
|
|
|
|
*
|
|
|
|
* ENV['foo'] = '0'.force_encoding(Encoding::ISO_2022_JP)
|
|
|
|
* # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: ISO-2022-JP)
|
|
|
|
* ENV["foo".force_encoding(Encoding::ISO_2022_JP)] = '0'
|
|
|
|
* # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: ISO-2022-JP)
|
2019-11-05 02:07:59 +03:00
|
|
|
*
|
2019-11-05 02:03:01 +03:00
|
|
|
* === About Ordering
|
2019-12-13 22:46:54 +03:00
|
|
|
*
|
|
|
|
* ENV enumerates its name/value pairs in the order found
|
2019-11-05 02:03:01 +03:00
|
|
|
* in the operating system's environment variables.
|
|
|
|
* Therefore the ordering of ENV content is OS-dependent, and may be indeterminate.
|
|
|
|
*
|
|
|
|
* This will be seen in:
|
|
|
|
* - A Hash returned by an ENV method.
|
|
|
|
* - An Enumerator returned by an ENV method.
|
|
|
|
* - An Array returned by ENV.keys, ENV.values, or ENV.to_a.
|
|
|
|
* - The String returned by ENV.inspect.
|
2019-12-13 22:46:54 +03:00
|
|
|
* - The Array returned by ENV.shift.
|
|
|
|
* - The name returned by ENV.key.
|
|
|
|
*
|
|
|
|
* === About the Examples
|
|
|
|
* Some methods in ENV return ENV itself. Typically, there are many environment variables.
|
|
|
|
* It's not useful to display a large ENV in the examples here,
|
|
|
|
* so most example snippets begin by resetting the contents of ENV:
|
|
|
|
* - ENV.replace replaces ENV with a new collection of entries.
|
|
|
|
* - ENV.clear empties ENV.
|
2011-06-29 07:09:34 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hack to get RDoc to regard ENV as a class:
|
|
|
|
* envtbl = rb_define_class("ENV", rb_cObject);
|
|
|
|
*/
|
1999-01-20 07:59:39 +03:00
|
|
|
origenviron = environ;
|
|
|
|
envtbl = rb_obj_alloc(rb_cObject);
|
|
|
|
rb_extend_object(envtbl, rb_mEnumerable);
|
|
|
|
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_singleton_method(envtbl, "[]", rb_f_getenv, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "fetch", env_fetch, -1);
|
2019-02-27 06:26:05 +03:00
|
|
|
rb_define_singleton_method(envtbl, "[]=", env_aset_m, 2);
|
|
|
|
rb_define_singleton_method(envtbl, "store", env_aset_m, 2);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_singleton_method(envtbl, "each", env_each_pair, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "each_pair", env_each_pair, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "each_key", env_each_key, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "each_value", env_each_value, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "delete", env_delete_m, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "delete_if", env_delete_if, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "keep_if", env_keep_if, 0);
|
2018-04-19 08:55:42 +03:00
|
|
|
rb_define_singleton_method(envtbl, "slice", env_slice, -1);
|
2019-05-04 16:37:38 +03:00
|
|
|
rb_define_singleton_method(envtbl, "except", env_except, -1);
|
2019-08-29 05:47:20 +03:00
|
|
|
rb_define_singleton_method(envtbl, "clear", env_clear, 0);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_singleton_method(envtbl, "reject", env_reject, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "reject!", env_reject_bang, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "select", env_select, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "select!", env_select_bang, 0);
|
2018-02-25 16:52:07 +03:00
|
|
|
rb_define_singleton_method(envtbl, "filter", env_select, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "filter!", env_select_bang, 0);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_singleton_method(envtbl, "shift", env_shift, 0);
|
2019-06-15 02:53:42 +03:00
|
|
|
rb_define_singleton_method(envtbl, "freeze", env_freeze, 0);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_singleton_method(envtbl, "invert", env_invert, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "replace", env_replace, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "update", env_update, 1);
|
2019-06-21 06:28:28 +03:00
|
|
|
rb_define_singleton_method(envtbl, "merge!", env_update, 1);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_singleton_method(envtbl, "inspect", env_inspect, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "rehash", env_none, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "to_a", env_to_a, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "to_s", env_to_s, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "key", env_key, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "index", env_index, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "size", env_size, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "length", env_size, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "empty?", env_empty_p, 0);
|
2019-08-29 05:47:20 +03:00
|
|
|
rb_define_singleton_method(envtbl, "keys", env_f_keys, 0);
|
|
|
|
rb_define_singleton_method(envtbl, "values", env_f_values, 0);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_singleton_method(envtbl, "values_at", env_values_at, -1);
|
|
|
|
rb_define_singleton_method(envtbl, "include?", env_has_key, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "member?", env_has_key, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "has_key?", env_has_key, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "has_value?", env_has_value, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "key?", env_has_key, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "value?", env_has_value, 1);
|
2019-08-29 05:47:20 +03:00
|
|
|
rb_define_singleton_method(envtbl, "to_hash", env_f_to_hash, 0);
|
2018-09-20 18:06:56 +03:00
|
|
|
rb_define_singleton_method(envtbl, "to_h", env_to_h, 0);
|
2016-12-03 14:18:44 +03:00
|
|
|
rb_define_singleton_method(envtbl, "assoc", env_assoc, 1);
|
|
|
|
rb_define_singleton_method(envtbl, "rassoc", env_rassoc, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2011-06-29 07:09:34 +04:00
|
|
|
/*
|
|
|
|
* ENV is a Hash-like accessor for environment variables.
|
|
|
|
*
|
|
|
|
* See ENV (the class) for more details.
|
|
|
|
*/
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_define_global_const("ENV", envtbl);
|
2013-11-15 21:15:31 +04:00
|
|
|
|
|
|
|
/* for callcc */
|
|
|
|
ruby_register_rollback_func_for_ensure(hash_foreach_ensure, hash_foreach_ensure_rollback);
|
2019-08-01 10:04:40 +03:00
|
|
|
|
|
|
|
HASH_ASSERT(sizeof(ar_hint_t) * RHASH_AR_TABLE_MAX_SIZE == sizeof(VALUE));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|