Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* numeric.c (num_step): better iteration condition for float

  values;  suggested by Masahiro TANAKA <masa@ir.isas.ac.jp>.

* range.c (range_step): step (for Range#step method) <= 0 makes no
  sence, thus ArgError will be raised.

* range.c (range_each): Range#each method is special case for
  Range#step(1)

* file.c (rb_find_file): load must be done from an abolute path if
  $SAFE >= 4.

* enum.c (enum_partition): new method. [new]

* re.c (rb_reg_s_quote): quote whitespaces for /x cases.


git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@2420 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
commit b6b8afad4a603190ac3b3376b413a40588c84246 1 parent 93195b1
authored May 01, 2002
26  ChangeLog
... ...
@@ -1,7 +1,33 @@
  1
+Tue Apr 30 09:23:05 2002  Yukihiro Matsumoto  <matz@ruby-lang.org>
  2
+
  3
+	* numeric.c (num_step): better iteration condition for float
  4
+	  values;  suggested by Masahiro TANAKA <masa@ir.isas.ac.jp>.
  5
+
  6
+Tue Apr 30 05:59:42 2002  Michal Rokos  <m.rokos@sh.cvut.cz>
  7
+
  8
+	* range.c (range_step): step (for Range#step method) <= 0 makes no
  9
+	  sence, thus ArgError will be raised.
  10
+
  11
+	* range.c (range_each): Range#each method is special case for
  12
+	  Range#step(1)
  13
+
  14
+Mon Apr 29 18:46:42 2002  Yukihiro Matsumoto  <matz@ruby-lang.org>
  15
+
  16
+	* file.c (rb_find_file): load must be done from an abolute path if
  17
+	  $SAFE >= 4.
  18
+
1 19
 Sun Apr 28 17:01:56 2002  WATANABE Hirofumi  <eban@ruby-lang.org>
2 20
 
3 21
 	* win32/win32.c (insert): fix prototype for ANSI C.
4 22
 
  23
+Fri Apr 26 13:47:15 2002  Yukihiro Matsumoto  <matz@ruby-lang.org>
  24
+
  25
+	* enum.c (enum_partition): new method. [new]
  26
+
  27
+Fri Apr 26 13:41:00 2002  Yukihiro Matsumoto  <matz@ruby-lang.org>
  28
+
  29
+	* re.c (rb_reg_s_quote): quote whitespaces for /x cases.
  30
+
5 31
 Fri Apr 26 06:48:23 2002  Takaaki Tateishi <ttate@kt.jaist.ac.jp>
6 32
 
7 33
 	* ext/dl/ptr.c (cary2ary): missing break in switch statements.
27  enum.c
@@ -217,6 +217,32 @@ enum_inject(argc, argv, obj)
217 217
 }
218 218
 
219 219
 static VALUE
  220
+partition_i(i, ary)
  221
+    VALUE i, *ary;
  222
+{
  223
+    if (RTEST(rb_yield(i))) {
  224
+	rb_ary_push(ary[0], i);
  225
+    }
  226
+    else {
  227
+	rb_ary_push(ary[1], i);
  228
+    }
  229
+    return Qnil;
  230
+}
  231
+
  232
+static VALUE
  233
+enum_partition(obj)
  234
+    VALUE obj;
  235
+{
  236
+    VALUE ary[2];
  237
+
  238
+    ary[0] = rb_ary_new();
  239
+    ary[1]  =  rb_ary_new();
  240
+    rb_iterate(rb_each, obj, partition_i, (VALUE)ary);
  241
+
  242
+    return rb_assoc_new(ary[0], ary[1]);
  243
+}
  244
+
  245
+static VALUE
220 246
 enum_sort(obj)
221 247
     VALUE obj;
222 248
 {
@@ -475,6 +501,7 @@ Init_Enumerable()
475 501
     rb_define_method(rb_mEnumerable,"collect", enum_collect, 0);
476 502
     rb_define_method(rb_mEnumerable,"map", enum_collect, 0);
477 503
     rb_define_method(rb_mEnumerable,"inject", enum_inject, -1);
  504
+    rb_define_method(rb_mEnumerable,"partition", enum_partition, 0);
478 505
     rb_define_method(rb_mEnumerable,"all?", enum_all, 0);
479 506
     rb_define_method(rb_mEnumerable,"any?", enum_any, 0);
480 507
     rb_define_method(rb_mEnumerable,"min", enum_min, 0);
32  eval.c
@@ -227,7 +227,7 @@ rb_add_method(klass, mid, node, noex)
227 227
     NODE *body;
228 228
 
229 229
     if (NIL_P(klass)) klass = rb_cObject;
230  
-    if (rb_safe_level() >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
  230
+    if (ruby_safe_level >= 4 && (klass == rb_cObject || !OBJ_TAINTED(klass))) {
231 231
 	rb_raise(rb_eSecurityError, "Insecure: can't define method");
232 232
     }
233 233
     if (OBJ_FROZEN(klass)) rb_error_frozen("class/module");
@@ -310,7 +310,7 @@ remove_method(klass, mid)
310 310
     if (klass == rb_cObject) {
311 311
 	rb_secure(4);
312 312
     }
313  
-    if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
  313
+    if (ruby_safe_level >= 4 && !OBJ_TAINTED(klass)) {
314 314
 	rb_raise(rb_eSecurityError, "Insecure: can't remove method");
315 315
     }
316 316
     if (OBJ_FROZEN(klass)) rb_error_frozen("class/module");
@@ -1588,7 +1588,7 @@ rb_undef(klass, id)
1588 1588
     if (ruby_class == rb_cObject) {
1589 1589
 	rb_secure(4);
1590 1590
     }
1591  
-    if (rb_safe_level() >= 4 && !OBJ_TAINTED(klass)) {
  1591
+    if (ruby_safe_level >= 4 && !OBJ_TAINTED(klass)) {
1592 1592
 	rb_raise(rb_eSecurityError, "Insecure: can't undef");
1593 1593
 	if (id == __id__ || id == __send__ || id == init) {
1594 1594
 	    rb_name_error(id, "undefining `%s' prohibited", rb_id2name(id));
@@ -3166,7 +3166,7 @@ rb_eval(self, n)
3166 3166
 	    VALUE klass;
3167 3167
 	    NODE *body = 0, *defn;
3168 3168
 
3169  
-	    if (rb_safe_level() >= 4 && !OBJ_TAINTED(recv)) {
  3169
+	    if (ruby_safe_level >= 4 && !OBJ_TAINTED(recv)) {
3170 3170
 		rb_raise(rb_eSecurityError, "Insecure: can't define singleton method");
3171 3171
 	    }
3172 3172
 	    if (FIXNUM_P(recv) || SYMBOL_P(recv)) {
@@ -3179,7 +3179,7 @@ rb_eval(self, n)
3179 3179
 	    if (OBJ_FROZEN(recv)) rb_error_frozen("object");
3180 3180
 	    klass = rb_singleton_class(recv);
3181 3181
 	    if (st_lookup(RCLASS(klass)->m_tbl, node->nd_mid, &body)) {
3182  
-		if (rb_safe_level() >= 4) {
  3182
+		if (ruby_safe_level >= 4) {
3183 3183
 		    rb_raise(rb_eSecurityError, "redefining method prohibited");
3184 3184
 		}
3185 3185
 		if (RTEST(ruby_verbose)) {
@@ -3248,7 +3248,7 @@ rb_eval(self, n)
3248 3248
 			goto override_class;
3249 3249
 		    }
3250 3250
 		}
3251  
-		if (rb_safe_level() >= 4) {
  3251
+		if (ruby_safe_level >= 4) {
3252 3252
 		    rb_raise(rb_eSecurityError, "extending class prohibited");
3253 3253
 		}
3254 3254
 	    }
@@ -3288,7 +3288,7 @@ rb_eval(self, n)
3288 3288
 		    rb_raise(rb_eTypeError, "%s is not a module",
3289 3289
 			     rb_id2name(node->nd_cname));
3290 3290
 		}
3291  
-		if (rb_safe_level() >= 4) {
  3291
+		if (ruby_safe_level >= 4) {
3292 3292
 		    rb_raise(rb_eSecurityError, "extending module prohibited");
3293 3293
 		}
3294 3294
 	    }
@@ -3315,7 +3315,7 @@ rb_eval(self, n)
3315 3315
 		rb_raise(rb_eTypeError, "no virtual class for %s",
3316 3316
 			 rb_class2name(CLASS_OF(result)));
3317 3317
 	    }
3318  
-	    if (rb_safe_level() >= 4 && !OBJ_TAINTED(result))
  3318
+	    if (ruby_safe_level >= 4 && !OBJ_TAINTED(result))
3319 3319
 		rb_raise(rb_eSecurityError, "Insecure: can't extend object");
3320 3320
 	    klass = rb_singleton_class(result);
3321 3321
 	    
@@ -5603,7 +5603,7 @@ static void
5603 5603
 secure_visibility(self)
5604 5604
     VALUE self;
5605 5605
 {
5606  
-    if (rb_safe_level() >= 4 && !OBJ_TAINTED(self)) {
  5606
+    if (ruby_safe_level >= 4 && !OBJ_TAINTED(self)) {
5607 5607
 	rb_raise(rb_eSecurityError, "Insecure: can't change method visibility");
5608 5608
     }
5609 5609
 }
@@ -6315,7 +6315,7 @@ proc_save_safe_level(data)
6315 6315
     VALUE data;
6316 6316
 {
6317 6317
     if (OBJ_TAINTED(data)) {
6318  
-	switch (rb_safe_level()) {
  6318
+	switch (ruby_safe_level) {
6319 6319
 	  case 3:
6320 6320
 	    FL_SET(data, PROC_T3);
6321 6321
 	    break;
@@ -6323,7 +6323,7 @@ proc_save_safe_level(data)
6323 6323
 	    FL_SET(data, PROC_T4);
6324 6324
 	    break;
6325 6325
 	  default:
6326  
-	    if (rb_safe_level() > 4) {
  6326
+	    if (ruby_safe_level > 4) {
6327 6327
 		FL_SET(data, PROC_TMAX);
6328 6328
 	    }
6329 6329
 	    break;
@@ -6621,8 +6621,8 @@ block_pass(self, node)
6621 6621
 	block = b;
6622 6622
     }
6623 6623
 
6624  
-    if (rb_safe_level() >= 1 && OBJ_TAINTED(block)) {
6625  
-	if (rb_safe_level() > proc_get_safe_level(block)) {
  6624
+    if (ruby_safe_level >= 1 && OBJ_TAINTED(block)) {
  6625
+	if (ruby_safe_level > proc_get_safe_level(block)) {
6626 6626
 	    rb_raise(rb_eSecurityError, "Insecure: tainted block value");
6627 6627
 	}
6628 6628
     }
@@ -8316,7 +8316,7 @@ rb_thread_safe_level(thread)
8316 8316
 
8317 8317
     th = rb_thread_check(thread);
8318 8318
     if (th == curr_thread) {
8319  
-	return INT2NUM(rb_safe_level());
  8319
+	return INT2NUM(ruby_safe_level);
8320 8320
     }
8321 8321
     return INT2NUM(th->safe);
8322 8322
 }
@@ -8859,7 +8859,7 @@ rb_thread_local_aref(thread, id)
8859 8859
     VALUE val;
8860 8860
 
8861 8861
     th = rb_thread_check(thread);
8862  
-    if (rb_safe_level() >= 4 && th != curr_thread) {
  8862
+    if (ruby_safe_level >= 4 && th != curr_thread) {
8863 8863
 	rb_raise(rb_eSecurityError, "Insecure: thread locals");
8864 8864
     }
8865 8865
     if (!th->locals) return Qnil;
@@ -8884,7 +8884,7 @@ rb_thread_local_aset(thread, id, val)
8884 8884
 {
8885 8885
     rb_thread_t th = rb_thread_check(thread);
8886 8886
 
8887  
-    if (rb_safe_level() >= 4 && th != curr_thread) {
  8887
+    if (ruby_safe_level >= 4 && th != curr_thread) {
8888 8888
 	rb_raise(rb_eSecurityError, "Insecure: can't modify thread locals");
8889 8889
     }
8890 8890
     if (OBJ_FROZEN(thread)) rb_error_frozen("thread locals");
4  file.c
@@ -2482,6 +2482,10 @@ rb_find_file(path)
2482 2482
 	if (file_load_ok(f)) return path;
2483 2483
     }
2484 2484
 
  2485
+    if (rb_safe_level() >= 4) {
  2486
+	rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f);
  2487
+    }
  2488
+
2485 2489
     if (rb_load_path) {
2486 2490
 	int i;
2487 2491
 
18  numeric.c
@@ -788,6 +788,7 @@ num_step(argc, argv, from)
788 788
 	    while (i <= end) {
789 789
 		rb_yield(INT2FIX(i));
790 790
 		i += diff;
  791
+		printf("<<%g>>\n", i - end);
791 792
 	    }
792 793
 	}
793 794
 	else {
@@ -798,21 +799,16 @@ num_step(argc, argv, from)
798 799
 	}
799 800
     }
800 801
     else if (TYPE(from) == T_FLOAT || TYPE(to) == T_FLOAT || TYPE(step) == T_FLOAT) {
  802
+	const double epsilon = 2.2204460492503131E-16;
801 803
 	double beg = NUM2DBL(from);
802 804
 	double end = NUM2DBL(to);
803 805
 	double unit = NUM2DBL(step);
804  
-	double n = beg;
805  
-	long i = 0;
  806
+	double n = (end - beg)/unit;
  807
+	long i;
806 808
 
807  
-	if (unit > 0) {
808  
-	    for (i=0; n<=end; i++, n=beg+unit*i) {
809  
-		rb_yield(rb_float_new(n));
810  
-	    }
811  
-	}
812  
-	else {
813  
-	    for (i=0; n>=end; i++, n=beg+unit*i) {
814  
-		rb_yield(rb_float_new(n));
815  
-	    }
  809
+	n = floor(n + n*epsilon) + 1;
  810
+	for (i=0; i<n; i++) {
  811
+	    rb_yield(rb_float_new(i*unit+beg));
816 812
 	}
817 813
     }
818 814
     else {
111  range.c
@@ -75,7 +75,7 @@ range_initialize(argc, argv, obj)
75 75
     rb_scan_args(argc, argv, "21", &beg, &end, &flags);
76 76
     /* Ranges are immutable, so that they should be initialized only once. */
77 77
     if (rb_ivar_defined(obj, id_beg)) {
78  
-	rb_name_error(rb_intern("initialized"), "`initialize' called twice");
  78
+	rb_name_error(rb_intern("initialize"), "`initialize' called twice");
79 79
     }
80 80
     range_init(obj, beg, end, RTEST(flags));
81 81
     return Qnil;
@@ -231,59 +231,6 @@ range_hash(range, obj)
231 231
 }
232 232
 
233 233
 static VALUE
234  
-range_each(range)
235  
-    VALUE range;
236  
-{
237  
-    VALUE b, e;
238  
-
239  
-    b = rb_ivar_get(range, id_beg);
240  
-    e = rb_ivar_get(range, id_end);
241  
-
242  
-    if (FIXNUM_P(b) && FIXNUM_P(e)) { /* fixnums are special */
243  
-	long end = FIX2LONG(e);
244  
-	long i;
245  
-
246  
-	if (!EXCL(range)) end += 1;
247  
-	for (i=FIX2LONG(b); i<end; i++) {
248  
-	    rb_yield(INT2NUM(i));
249  
-	}
250  
-    }
251  
-    else if (TYPE(b) == T_STRING) {
252  
-	rb_str_upto(b, e, EXCL(range));
253  
-    }
254  
-    else if (rb_obj_is_kind_of(b, rb_cNumeric)) {
255  
-	b = rb_Integer(b);
256  
-	e = rb_Integer(e);
257  
-
258  
-	if (!EXCL(range)) e = rb_funcall(e, '+', 1, INT2FIX(1));
259  
-	while (RTEST(rb_funcall(b, '<', 1, e))) {
260  
-	    rb_yield(b);
261  
-	    b = rb_funcall(b, '+', 1, INT2FIX(1));
262  
-	}
263  
-    }
264  
-    else {			/* generic each */
265  
-	VALUE v = b;
266  
-
267  
-	if (EXCL(range)) {
268  
-	    while (r_lt(v, e)) {
269  
-		if (r_eq(v, e)) break;
270  
-		rb_yield(v);
271  
-		v = rb_funcall(v, id_succ, 0, 0);
272  
-	    }
273  
-	}
274  
-	else {
275  
-	    while (r_le(v, e)) {
276  
-		rb_yield(v);
277  
-		if (r_eq(v, e)) break;
278  
-		v = rb_funcall(v, id_succ, 0, 0);
279  
-	    }
280  
-	}
281  
-    }
282  
-
283  
-    return range;
284  
-}
285  
-
286  
-static VALUE
287 234
 r_step_str(args)
288 235
     VALUE *args;
289 236
 {
@@ -313,26 +260,52 @@ range_step(argc, argv, range)
313 260
 
314 261
     b = rb_ivar_get(range, id_beg);
315 262
     e = rb_ivar_get(range, id_end);
316  
-    rb_scan_args(argc, argv, "01", &step);
  263
+    if (rb_scan_args(argc, argv, "01", &step) == 0) {
  264
+	step = INT2FIX(1);
  265
+    }
317 266
 
318 267
     if (FIXNUM_P(b) && FIXNUM_P(e)) { /* fixnums are special */
319  
-	long end = FIX2LONG(e);
320  
-	long i, s = (argc == 0) ? 1 : NUM2LONG(step);
321  
-
322  
-	if (!EXCL(range)) end += 1;
323  
-	for (i=FIX2LONG(b); i<end; i+=s) {
324  
-	    rb_yield(INT2NUM(i));
  268
+	long beg = FIX2LONG(b), end = FIX2LONG(e), s = NUM2LONG(step);
  269
+	long i;
  270
+	if (s <= 0) {
  271
+	    rb_raise(rb_eArgError, "step can't be <= 0");
  272
+	}
  273
+	if ((end - beg) < 0) {
  274
+	    if (!EXCL(range)) end -= 1;
  275
+	    for (i=beg; i>end; i-=s) {
  276
+		rb_yield(LONG2NUM(i));
  277
+	    }
  278
+	}
  279
+	else {
  280
+	    if (!EXCL(range)) end += 1;
  281
+	    for (i=beg; i<end; i+=s) {
  282
+		rb_yield(INT2NUM(i));
  283
+	    }
325 284
 	}
326 285
     }
327 286
     else if (rb_obj_is_kind_of(b, rb_cNumeric)) {
  287
+	VALUE diff;
328 288
 	b = rb_Integer(b);
329 289
 	e = rb_Integer(e);
330 290
 	step = rb_Integer(step);
331 291
 
332  
-	if (!EXCL(range)) e = rb_funcall(e, '+', 1, INT2FIX(1));
333  
-	while (RTEST(rb_funcall(b, '<', 1, e))) {
334  
-	    rb_yield(b);
335  
-	    b = rb_funcall(b, '+', 1, step);
  292
+	if (RTEST(rb_funcall(step, rb_intern("<="), 1, INT2FIX(0)))) {
  293
+	    rb_raise(rb_eArgError, "step can't be <= 0");
  294
+	}
  295
+	diff = rb_funcall(e, '-', 1, b);
  296
+	if (RTEST(rb_funcall(diff, '<', 1, INT2FIX(0)))) {
  297
+	    if (!EXCL(range)) e = rb_funcall(e, '-', 1, INT2FIX(1));
  298
+	    while (RTEST(rb_funcall(b, '>', 1, e))) {
  299
+		rb_yield(b);
  300
+		b = rb_funcall(b, '-', 1, step);
  301
+	    }
  302
+	}
  303
+	else {
  304
+	    if (!EXCL(range)) e = rb_funcall(e, '+', 1, INT2FIX(1));
  305
+	    while (RTEST(rb_funcall(b, '<', 1, e))) {
  306
+		rb_yield(b);
  307
+		b = rb_funcall(b, '+', 1, step);
  308
+	    }
336 309
 	}
337 310
     }
338 311
     else if (TYPE(b) == T_STRING) {
@@ -366,11 +339,17 @@ range_step(argc, argv, range)
366 339
 	    }
367 340
 	}
368 341
     }
369  
-
370 342
     return range;
371 343
 }
372 344
 
373 345
 static VALUE
  346
+range_each(range)
  347
+    VALUE range;
  348
+{
  349
+    return range_step(0, NULL, range);
  350
+}
  351
+
  352
+static VALUE
374 353
 range_first(obj)
375 354
     VALUE obj;
376 355
 {
41  re.c
@@ -861,7 +861,8 @@ match_to_a(match)
861 861
     for (i=0; i<regs->num_regs; i++) {
862 862
 	if (regs->beg[i] == -1) {
863 863
 	    rb_ary_push(ary, Qnil);
864  
-	} else {
  864
+	}
  865
+	else {
865 866
 	    VALUE str = rb_str_new(ptr+regs->beg[i], regs->end[i]-regs->beg[i]);
866 867
 	    if (taint) OBJ_TAINT(str);
867 868
 	    rb_ary_push(ary, str);
@@ -1170,6 +1171,7 @@ rb_reg_s_quote(argc, argv)
1170 1171
     int kcode_saved = reg_kcode;
1171 1172
     char *s, *send, *t;
1172 1173
     VALUE tmp;
  1174
+    int c;
1173 1175
 
1174 1176
     rb_scan_args(argc, argv, "11", &str, &kcode);
1175 1177
     if (!NIL_P(kcode)) {
@@ -1184,24 +1186,41 @@ rb_reg_s_quote(argc, argv)
1184 1186
     t = RSTRING(tmp)->ptr;
1185 1187
 
1186 1188
     for (; s < send; s++) {
1187  
-	if (ismbchar(*s)) {
1188  
-	    int n = mbclen(*s);
  1189
+	c = *s;
  1190
+	if (ismbchar(c)) {
  1191
+	    int n = mbclen(c);
1189 1192
 
1190 1193
 	    while (n-- && s < send)
1191 1194
 		*t++ = *s++;
1192 1195
 	    s--;
1193 1196
 	    continue;
1194 1197
 	}
1195  
-	if (*s == '[' || *s == ']'
1196  
-	    || *s == '{' || *s == '}'
1197  
-	    || *s == '(' || *s == ')'
1198  
-	    || *s == '|' || *s == '-'
1199  
-	    || *s == '*' || *s == '.' || *s == '\\'
1200  
-	    || *s == '?' || *s == '+'
1201  
-	    || *s == '^' || *s == '$') {
  1198
+	switch (c) {
  1199
+	  case '\t':
  1200
+	    c = 't';
1202 1201
 	    *t++ = '\\';
  1202
+	    break;
  1203
+	  case '\f':
  1204
+	    c = 'f';
  1205
+	    *t++ = '\\';
  1206
+	    break;
  1207
+	  case '\r':
  1208
+	    c = 'r';
  1209
+	    *t++ = '\\';
  1210
+	    break;
  1211
+	  case '\n':
  1212
+	    c = 'n';
  1213
+	    *t++ = '\\';
  1214
+	    break;
  1215
+	  case '[': case ']': case '{': case '}':
  1216
+	  case '(': case ')': case '|': case '-':
  1217
+	  case '*': case '.': case '\\':
  1218
+	  case '?': case '+': case '^': case '$':
  1219
+	  case ' ':
  1220
+	    *t++ = '\\';
  1221
+	    break;
1203 1222
 	}
1204  
-	*t++ = *s;
  1223
+	*t++ = c;
1205 1224
     }
1206 1225
     kcode_reset_option();
1207 1226
     rb_str_resize(tmp, t - RSTRING(tmp)->ptr);

0 notes on commit b6b8afa

Please sign in to comment.
Something went wrong with that request. Please try again.