supplement 2.28 → 2.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/supplement.c +120 -63
  4. data/lib/supplement.h +4 -2
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f85d589269b733a85677e23b294181ffe0c589b85b4a4d635d4071cfb11f9dee
4
- data.tar.gz: 629cb0a6f688289737682b4483ce56b2d48ebc59329427e526b8422cc8dbb7f0
3
+ metadata.gz: 9d98b193bc4d1cc8ede4e690103285660b92acbef10bc2c8c9fbe9774968b55e
4
+ data.tar.gz: ce38aef42ac4a5aceb953eee5090d62b09da647c6badd39eb13fcf6ae75124d7
5
5
  SHA512:
6
- metadata.gz: 35b071bcdc5240496f66ab0e288c3be3f9d22166f5fe817f01cd69ba64e19af7fc78c9a76fe10fc78d85addb8703861b87b9082c903a21a45c22e63033a6e8a5
7
- data.tar.gz: 8d2d97d762991063639a8fd33d2d84ab876542e71b005c11744f74dfc3ab30d2412490b1dc9e9f699ce4334f0579ce10bdb4ad0f9fcacaa860c9ffcf67cbc119
6
+ metadata.gz: c8a1ca6542111abec4919e6b0fafbc665e88baf3de003ccb696586060b4508f0d8c95c5a85d5b2b2bbab85aad0f2c7132bee0458dd64f799d6825c8f404e6754
7
+ data.tar.gz: 30703341388fdd2895f68879925eef1d4a1e94c22e177470cc7689b5e7a0addd5693bab20969afa8b127e6a137e5794dba323bdf2d1e2824adfe73b813103dc5
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # supplement 2.28 -- Useful Ruby enhancements
1
+ # supplement 2.29 -- Useful Ruby enhancements
2
2
 
3
3
  Some simple Ruby extensions.
4
4
 
data/lib/supplement.c CHANGED
@@ -393,10 +393,53 @@ rb_str_tail( int argc, VALUE *argv, VALUE str)
393
393
 
394
394
  /*
395
395
  * call-seq:
396
- * starts_with?( oth) -> nil or int
396
+ * axe( n = 80) -> str
397
397
  *
398
- * Checks whether the head is <code>oth</code>. Returns length of
399
- * <code>oth</code> when matching.
398
+ * Cut off everthing beyond then <code>n</code>th character. Replace the
399
+ * last bytes by ellipses.
400
+ *
401
+ * a = "Redistribution and use in source and binary forms, with or without"
402
+ * a.axe( 16) #=> "Redistributio..."
403
+ */
404
+
405
+ VALUE
406
+ rb_str_axe( int argc, VALUE *argv, VALUE str)
407
+ {
408
+ VALUE n;
409
+ VALUE ret;
410
+ long newlen, oldlen;
411
+
412
+ if (rb_scan_args( argc, argv, "01", &n) == 1 && !NIL_P( n))
413
+ newlen = NUM2LONG( n);
414
+ else
415
+ newlen = 80;
416
+ if (newlen < 0)
417
+ return Qnil;
418
+
419
+ oldlen = rb_str_strlen( str);
420
+ if (newlen < oldlen) {
421
+ VALUE ell;
422
+ long e;
423
+
424
+ ell = rb_str_new2( "...");
425
+ e = rb_str_strlen( ell);
426
+ if (newlen > e) {
427
+ ret = rb_str_substr( str, 0, newlen - e);
428
+ rb_str_append( ret, ell);
429
+ } else
430
+ ret = rb_str_substr( str, 0, newlen);
431
+ } else
432
+ ret = str;
433
+ return ret;
434
+ }
435
+
436
+
437
+ /*
438
+ * call-seq:
439
+ * starts_with?( *oth) -> nil or int
440
+ *
441
+ * Checks whether the head is one of <code>oth</code>. Returns the position
442
+ * where the match ends.
400
443
  *
401
444
  * "sys-apps".starts_with?( "sys-") #=> 4
402
445
  *
@@ -406,32 +449,39 @@ rb_str_tail( int argc, VALUE *argv, VALUE str)
406
449
  */
407
450
 
408
451
  VALUE
409
- rb_str_starts_with_p( VALUE str, VALUE oth)
452
+ rb_str_starts_with_p( int argc, VALUE *argv, VALUE str)
410
453
  {
411
- long i;
412
- char *s, *o;
413
- VALUE ost;
454
+ int j;
414
455
 
415
- ost = rb_string_value( &oth);
416
- i = RSTRING_LEN( ost);
417
- if (i > RSTRING_LEN( str))
418
- return Qnil;
419
- s = RSTRING_PTR( str);
420
- o = RSTRING_PTR( ost);
421
- for (; i; i--, s++, o++) {
422
- if (*s != *o)
456
+ for (j = 0; j < argc; j++) {
457
+ long i;
458
+ char *s, *o;
459
+ VALUE ost;
460
+ VALUE oth = argv[ j];
461
+
462
+ ost = rb_string_value( &oth);
463
+ i = RSTRING_LEN( ost);
464
+ if (i > RSTRING_LEN( str))
423
465
  return Qnil;
466
+ s = RSTRING_PTR( str);
467
+ o = RSTRING_PTR( ost);
468
+ for (; i; i--, s++, o++) {
469
+ if (*s != *o)
470
+ break;
471
+ }
472
+ if (!i)
473
+ return INT2FIX( rb_str_strlen( ost));
424
474
  }
425
- return INT2FIX( rb_str_strlen( ost));
475
+ return Qnil;
426
476
  }
427
477
 
428
478
 
429
479
  /*
430
480
  * call-seq:
431
- * ends_with?( oth) -> nil or int
481
+ * ends_with?( *oth) -> nil or int
432
482
  *
433
- * Checks whether the tail is <code>oth</code>. Returns the position
434
- * where <code>oth</code> starts when matching.
483
+ * Checks whether the tail is one of <code>oth</code>. Returns the position
484
+ * where the match starts.
435
485
  *
436
486
  * "sys-apps".ends_with?( "-apps") #=> 3
437
487
  *
@@ -440,65 +490,70 @@ rb_str_starts_with_p( VALUE str, VALUE oth)
440
490
  */
441
491
 
442
492
  VALUE
443
- rb_str_ends_with_p( VALUE str, VALUE oth)
493
+ rb_str_ends_with_p( int argc, VALUE *argv, VALUE str)
444
494
  {
445
- long i;
446
- char *s, *o;
447
- VALUE ost;
495
+ int j;
448
496
 
449
- ost = rb_string_value( &oth);
450
- i = RSTRING_LEN( ost);
451
- if (i > RSTRING_LEN( str))
452
- return Qnil;
453
- s = RSTRING_END( str);
454
- o = RSTRING_END( ost);
455
- for (; i; i--)
456
- if (*--s != *--o)
497
+ for (j = 0; j < argc; j++) {
498
+ long i;
499
+ char *s, *o;
500
+ VALUE ost;
501
+ VALUE oth = argv[ j];
502
+
503
+ ost = rb_string_value( &oth);
504
+ i = RSTRING_LEN( ost);
505
+ if (i > RSTRING_LEN( str))
457
506
  return Qnil;
458
- return INT2FIX( rb_str_strlen( str) - rb_str_strlen( ost));
507
+ s = RSTRING_END( str);
508
+ o = RSTRING_END( ost);
509
+ for (; i; i--)
510
+ if (*--s != *--o)
511
+ break;
512
+ if (!i)
513
+ return INT2FIX( rb_str_strlen( str) - rb_str_strlen( ost));
514
+ }
515
+ return Qnil;
459
516
  }
460
517
 
461
518
 
462
519
  /*
463
520
  * call-seq:
464
- * axe( n = 80) -> str
521
+ * starts_with?( oth) -> nil or int
465
522
  *
466
- * Cut off everthing beyond then <code>n</code>th character. Replace the
467
- * last bytes by ellipses.
523
+ * Checks whether the head is <code>oth</code>. Returns length of
524
+ * <code>oth</code> when matching.
468
525
  *
469
- * a = "Redistribution and use in source and binary forms, with or without"
470
- * a.axe( 16) #=> "Redistributio..."
526
+ * :"sys-apps".starts_with?( "sys-") #=> 4
527
+ *
528
+ * Caution! The Ruby 1.9.3 method #start_with? (notice the missing s)
529
+ * just returns +true+ or +false+. Mnemonics: "s" = prepare for
530
+ * <code>#slice</code>.
471
531
  */
472
532
 
473
533
  VALUE
474
- rb_str_axe( int argc, VALUE *argv, VALUE str)
534
+ rb_sym_starts_with_p( int argc, VALUE *argv, VALUE sym)
475
535
  {
476
- VALUE n;
477
- VALUE ret;
478
- long newlen, oldlen;
536
+ return rb_str_starts_with_p(argc, argv, rb_sym2str(sym));
537
+ }
479
538
 
480
- if (rb_scan_args( argc, argv, "01", &n) == 1 && !NIL_P( n))
481
- newlen = NUM2LONG( n);
482
- else
483
- newlen = 80;
484
- if (newlen < 0)
485
- return Qnil;
486
539
 
487
- oldlen = rb_str_strlen( str);
488
- if (newlen < oldlen) {
489
- VALUE ell;
490
- long e;
540
+ /*
541
+ * call-seq:
542
+ * ends_with?( oth) -> nil or int
543
+ *
544
+ * Checks whether the tail is <code>oth</code>. Returns the position
545
+ * where <code>oth</code> starts when matching.
546
+ *
547
+ * :"sys-apps".ends_with?( "-apps") #=> 3
548
+ *
549
+ * Caution! The Ruby 1.9.3 method #start_with? (notice the missing s)
550
+ * just returns +true+ or +false+.
551
+ */
491
552
 
492
- ell = rb_str_new2( "...");
493
- e = rb_str_strlen( ell);
494
- if (newlen > e) {
495
- ret = rb_str_substr( str, 0, newlen - e);
496
- rb_str_append( ret, ell);
497
- } else
498
- ret = rb_str_substr( str, 0, newlen);
499
- } else
500
- ret = str;
501
- return ret;
553
+ VALUE
554
+ rb_sym_ends_with_p( int argc, VALUE *argv, VALUE sym)
555
+ {
556
+ return rb_str_ends_with_p(argc, argv, rb_sym2str(sym));
502
557
  }
503
558
 
504
559
 
@@ -1125,9 +1180,11 @@ void Init_supplement( void)
1125
1180
  rb_define_method( rb_cString, "head", rb_str_head, -1);
1126
1181
  rb_define_method( rb_cString, "rest", rb_str_rest, -1);
1127
1182
  rb_define_method( rb_cString, "tail", rb_str_tail, -1);
1128
- rb_define_method( rb_cString, "starts_with?", rb_str_starts_with_p, 1);
1129
- rb_define_method( rb_cString, "ends_with?", rb_str_ends_with_p, 1);
1130
1183
  rb_define_method( rb_cString, "axe", rb_str_axe, -1);
1184
+ rb_define_method( rb_cString, "starts_with?", rb_str_starts_with_p, -1);
1185
+ rb_define_method( rb_cString, "ends_with?", rb_str_ends_with_p, -1);
1186
+ rb_define_method( rb_cSymbol, "starts_with?", rb_sym_starts_with_p, -1);
1187
+ rb_define_method( rb_cSymbol, "ends_with?", rb_sym_ends_with_p, -1);
1131
1188
 
1132
1189
  rb_define_method( rb_cNumeric, "grammatical", rb_num_grammatical, 2);
1133
1190
  rb_define_method( rb_cNumeric, "sqrt", rb_num_sqrt, 0);
data/lib/supplement.h CHANGED
@@ -26,9 +26,11 @@ extern VALUE rb_str_cut_bang( VALUE, VALUE);
26
26
  extern VALUE rb_str_head( int, VALUE *, VALUE);
27
27
  extern VALUE rb_str_rest( int, VALUE *, VALUE);
28
28
  extern VALUE rb_str_tail( int, VALUE *, VALUE);
29
- extern VALUE rb_str_starts_with_p( VALUE, VALUE);
30
- extern VALUE rb_str_ends_with_p( VALUE, VALUE);
31
29
  extern VALUE rb_str_axe( int, VALUE *, VALUE);
30
+ extern VALUE rb_str_starts_with_p( int argc, VALUE *argv, VALUE);
31
+ extern VALUE rb_str_ends_with_p( int argc, VALUE *argv, VALUE);
32
+ extern VALUE rb_sym_starts_with_p( int argc, VALUE *argv, VALUE);
33
+ extern VALUE rb_sym_ends_with_p( int argc, VALUE *argv, VALUE);
32
34
 
33
35
  extern VALUE rb_num_grammatical( VALUE, VALUE, VALUE);
34
36
  extern VALUE rb_num_sqrt( VALUE);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supplement
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.28'
4
+ version: '2.29'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertram Scharpf