supplement 2.30 → 2.31.1
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.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +2 -2
- data/lib/supplement.c +71 -15
- data/lib/supplement.h +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dff470072491d922eb1edfa3e305fad6323caa366f83379f10065de3c8ce5a84
|
|
4
|
+
data.tar.gz: a69fc76a288c99a345e583029fac4678ec591aaa6edd50cf896e77a498147cef
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cabaaa436d3f1e3a7bfae57e47c2df42980e8011a1f2a745be79489bbbdc164e8ef7a89c420369a0128e743618303649b396f045351009c5e3b85cca1e60e740
|
|
7
|
+
data.tar.gz: e5de3d55afe67a240d8efbf35198bed3ca114ef143c1e5a91d221b63ee3c5eea367b76548a250010bcc8eca420cc287f0305f612fb075c746ad267fd3a5ca168
|
data/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# BSD-2-clause license, extended by language use conditions
|
|
2
2
|
|
|
3
|
-
Copyright (C) 2009-
|
|
3
|
+
Copyright (C) 2009-2026, Bertram Scharpf <software@bertram-scharpf.de>.
|
|
4
4
|
All rights reserved.
|
|
5
5
|
|
|
6
6
|
Redistribution and use in source and binary forms, with or without
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# supplement 2.
|
|
1
|
+
# supplement 2.31.1 -- Useful Ruby enhancements
|
|
2
2
|
|
|
3
3
|
Some simple Ruby extensions.
|
|
4
4
|
|
|
@@ -48,7 +48,7 @@ one of them to be included into the Ruby standard.
|
|
|
48
48
|
|
|
49
49
|
## Copyright
|
|
50
50
|
|
|
51
|
-
* (C) 2009-
|
|
51
|
+
* (C) 2009-2026 Bertram Scharpf <software@bertram-scharpf.de>
|
|
52
52
|
* License: [BSD-2-Clause+](./LICENSE)
|
|
53
53
|
* Repository: [ruby-supplement](https://github.com/BertramScharpf/ruby-supplement)
|
|
54
54
|
|
data/lib/supplement.c
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
#include <ruby/re.h>
|
|
12
12
|
|
|
13
13
|
|
|
14
|
+
static long supplement_args_len_default( int, VALUE *);
|
|
14
15
|
static void supplement_ary_assure_notempty( VALUE);
|
|
15
16
|
static VALUE supplement_index_blk( VALUE);
|
|
16
17
|
static VALUE supplement_index_ref( VALUE, VALUE);
|
|
@@ -19,6 +20,8 @@ static VALUE supplement_rindex_ref( VALUE, VALUE);
|
|
|
19
20
|
static VALUE supplement_do_unumask( VALUE);
|
|
20
21
|
|
|
21
22
|
|
|
23
|
+
static const char *supplement_ellipse = "...";
|
|
24
|
+
|
|
22
25
|
static ID id_delete_at = 0;
|
|
23
26
|
static ID id_cmp = 0;
|
|
24
27
|
static ID id_eqq = 0;
|
|
@@ -395,49 +398,101 @@ rb_str_tail( int argc, VALUE *argv, VALUE str)
|
|
|
395
398
|
}
|
|
396
399
|
|
|
397
400
|
|
|
401
|
+
long
|
|
402
|
+
supplement_args_len_default( int argc, VALUE *argv)
|
|
403
|
+
{
|
|
404
|
+
VALUE n;
|
|
405
|
+
long r;
|
|
406
|
+
|
|
407
|
+
if (rb_scan_args( argc, argv, "01", &n) == 1 && !NIL_P( n))
|
|
408
|
+
r = NUM2LONG( n);
|
|
409
|
+
else
|
|
410
|
+
r = 80;
|
|
411
|
+
return r;
|
|
412
|
+
}
|
|
413
|
+
|
|
398
414
|
/*
|
|
399
415
|
* call-seq:
|
|
400
416
|
* axe( n = 80) -> str
|
|
401
417
|
*
|
|
402
|
-
* Cut off everthing beyond
|
|
418
|
+
* Cut off everthing beyond the <code>n</code>th character. Replace the
|
|
403
419
|
* last bytes by ellipses.
|
|
404
420
|
*
|
|
405
|
-
* a = "
|
|
406
|
-
* a.axe( 16) #=> "
|
|
421
|
+
* a = "Now is the time for all good men to come to the aid of their country etc."
|
|
422
|
+
* a.axe( 16) #=> "Now is the ti..."
|
|
407
423
|
*/
|
|
408
424
|
|
|
409
425
|
VALUE
|
|
410
426
|
rb_str_axe( int argc, VALUE *argv, VALUE str)
|
|
411
427
|
{
|
|
412
|
-
VALUE n;
|
|
413
428
|
VALUE ret;
|
|
414
429
|
long newlen, oldlen;
|
|
415
430
|
|
|
416
|
-
|
|
417
|
-
newlen = NUM2LONG( n);
|
|
418
|
-
else
|
|
419
|
-
newlen = 80;
|
|
431
|
+
newlen = supplement_args_len_default( argc, argv);
|
|
420
432
|
if (newlen < 0)
|
|
421
433
|
return Qnil;
|
|
422
434
|
|
|
435
|
+
|
|
423
436
|
oldlen = rb_str_strlen( str);
|
|
424
437
|
if (newlen < oldlen) {
|
|
425
438
|
VALUE ell;
|
|
426
|
-
long
|
|
439
|
+
long le;
|
|
427
440
|
|
|
428
|
-
ell = rb_str_new2(
|
|
429
|
-
|
|
430
|
-
if (newlen
|
|
431
|
-
ret = rb_str_substr( str, 0, newlen -
|
|
441
|
+
ell = rb_str_new2( supplement_ellipse);
|
|
442
|
+
le = rb_str_strlen( ell);
|
|
443
|
+
if (newlen >= le) {
|
|
444
|
+
ret = rb_str_substr( str, 0, newlen - le);
|
|
432
445
|
rb_str_append( ret, ell);
|
|
433
446
|
} else
|
|
434
|
-
ret = rb_str_substr(
|
|
447
|
+
ret = rb_str_substr( ell, 0, newlen);
|
|
435
448
|
} else
|
|
436
449
|
ret = str;
|
|
437
450
|
return ret;
|
|
438
451
|
}
|
|
439
452
|
|
|
440
453
|
|
|
454
|
+
/*
|
|
455
|
+
* call-seq:
|
|
456
|
+
* axe!( n = 80) -> str
|
|
457
|
+
*
|
|
458
|
+
* Cut off everthing beyond the <code>n</code>th character. Replace the
|
|
459
|
+
* last bytes by ellipses.
|
|
460
|
+
*
|
|
461
|
+
* a = "Now is the time for all good men to come to the aid of their country etc."
|
|
462
|
+
* a.axe!( 16) #=> "Now is the ti..."
|
|
463
|
+
* a #=> "Now is the ti..."
|
|
464
|
+
*
|
|
465
|
+
* If nothing was removed, <code>nil</code> is returned.
|
|
466
|
+
*/
|
|
467
|
+
|
|
468
|
+
VALUE
|
|
469
|
+
rb_str_axe_bang( int argc, VALUE *argv, VALUE str)
|
|
470
|
+
{
|
|
471
|
+
long newlen, oldlen;
|
|
472
|
+
|
|
473
|
+
newlen = supplement_args_len_default( argc, argv);
|
|
474
|
+
if (newlen < 0)
|
|
475
|
+
return Qnil;
|
|
476
|
+
|
|
477
|
+
rb_str_modify( str);
|
|
478
|
+
|
|
479
|
+
oldlen = rb_str_strlen( str);
|
|
480
|
+
if (newlen < oldlen) {
|
|
481
|
+
VALUE ell;
|
|
482
|
+
long le;
|
|
483
|
+
long re;
|
|
484
|
+
|
|
485
|
+
rb_str_update( str, newlen, oldlen - newlen, rb_str_new( NULL, 0));
|
|
486
|
+
ell = rb_str_new2( supplement_ellipse);
|
|
487
|
+
le = rb_str_strlen( ell);
|
|
488
|
+
re = newlen < le ? newlen : le;
|
|
489
|
+
rb_str_update( str, newlen - re, re, rb_str_substr( ell, 0, re));
|
|
490
|
+
return str;
|
|
491
|
+
} else
|
|
492
|
+
return Qnil;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
|
|
441
496
|
/*
|
|
442
497
|
* call-seq:
|
|
443
498
|
* starts_with?( *oth) -> nil or int
|
|
@@ -1185,6 +1240,7 @@ void Init_supplement( void)
|
|
|
1185
1240
|
rb_define_method( rb_cString, "rest", rb_str_rest, -1);
|
|
1186
1241
|
rb_define_method( rb_cString, "tail", rb_str_tail, -1);
|
|
1187
1242
|
rb_define_method( rb_cString, "axe", rb_str_axe, -1);
|
|
1243
|
+
rb_define_method( rb_cString, "axe!", rb_str_axe_bang, -1);
|
|
1188
1244
|
rb_define_method( rb_cString, "starts_with?", rb_str_starts_with_p, -1);
|
|
1189
1245
|
rb_define_method( rb_cString, "ends_with?", rb_str_ends_with_p, -1);
|
|
1190
1246
|
rb_define_method( rb_cSymbol, "starts_with?", rb_sym_starts_with_p, -1);
|
|
@@ -1206,7 +1262,7 @@ void Init_supplement( void)
|
|
|
1206
1262
|
|
|
1207
1263
|
rb_define_method( rb_cHash, "notempty?", rb_hash_notempty_p, 0);
|
|
1208
1264
|
|
|
1209
|
-
rb_undef_method(
|
|
1265
|
+
rb_undef_method( CLASS_OF( rb_cFile), "umask");
|
|
1210
1266
|
rb_define_singleton_method( rb_cFile, "umask", rb_file_s_umask, -1);
|
|
1211
1267
|
|
|
1212
1268
|
rb_define_singleton_method( rb_cDir, "current", rb_dir_s_current, 0);
|
data/lib/supplement.h
CHANGED
|
@@ -27,6 +27,7 @@ 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
29
|
extern VALUE rb_str_axe( int, VALUE *, VALUE);
|
|
30
|
+
extern VALUE rb_str_axe_bang( int, VALUE *, VALUE);
|
|
30
31
|
extern VALUE rb_str_starts_with_p( int argc, VALUE *argv, VALUE);
|
|
31
32
|
extern VALUE rb_str_ends_with_p( int argc, VALUE *argv, VALUE);
|
|
32
33
|
extern VALUE rb_sym_starts_with_p( int argc, VALUE *argv, VALUE);
|