summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-02 06:41:33 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-02 06:41:33 +0000
commit776e2693e738975b2bdbeb1ec43e4993a5520665 (patch)
tree7cfe6e46681a24e91c1cfa1d81a017eff06fcb1c /string.c
parentf701792f6f5d21a3762aec4a23a2b134e52ab94e (diff)
* string.c (rb_str_ljust): now takes optional argument to specify
pad string. [ruby-talk:70482] * string.c (rb_str_rjust): ditto. * string.c (rb_str_center): ditto. * string.c (rb_str_justify): utility function. * eval.c (rb_add_method): call singleton_method_added or method_added for every method definition (after ruby_running). [ruby-talk:70471] * array.c (rb_ary_reverse_bang): Array#reverse! should not return nil even for arrays sized less than 2. * io.c (argf_eof): should not block after reading all argument files. (ruby-bugs-ja PR#449) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3742 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c124
1 files changed, 77 insertions, 47 deletions
diff --git a/string.c b/string.c
index 42177887a8..ee3dc6b774 100644
--- a/string.c
+++ b/string.c
@@ -3121,69 +3121,99 @@ rb_str_sum(argc, argv, str)
}
static VALUE
-rb_str_ljust(str, w)
+rb_str_justify(argc, argv, str, jflag)
+ int argc;
+ VALUE *argv;
VALUE str;
- VALUE w;
+ char jflag;
{
- long width = NUM2LONG(w);
+ VALUE w;
+ long width, flen = 0;
VALUE res;
- char *p, *pend;
-
+ char *p, *pend, *f = " ";
+ long n;
+ VALUE pad;
+
+ if (rb_scan_args(argc, argv, "11", &w, &pad) == 2) {
+ if (!NIL_P(pad)) {
+ StringValue(pad);
+ if (RSTRING(pad)->len > 0) {
+ f = RSTRING(pad)->ptr;
+ flen = RSTRING(pad)->len;
+ }
+ }
+ }
+ width = NUM2LONG(w);
if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
res = rb_str_new5(str, 0, width);
- memcpy(RSTRING(res)->ptr, RSTRING(str)->ptr, RSTRING(str)->len);
- p = RSTRING(res)->ptr + RSTRING(str)->len; pend = RSTRING(res)->ptr + width;
- while (p < pend) {
- *p++ = ' ';
+ p = RSTRING(res)->ptr;
+ if (jflag != 'l') {
+ n = width - RSTRING(str)->len;
+ pend = p + ((jflag == 'r') ? n : n/2);
+ if (flen <= 1) {
+ while (p < pend) {
+ *p++ = *f;
+ }
+ }
+ else {
+ char *q = f;
+ while (p + flen <= pend) {
+ memcpy(p,f,flen);
+ p += flen;
+ }
+ while (p < pend) {
+ *p++ = *q++;
+ }
+ }
+ }
+ memcpy(p, RSTRING(str)->ptr, RSTRING(str)->len);
+ if (jflag != 'r') {
+ p += RSTRING(str)->len; pend = RSTRING(res)->ptr + width;
+ if (flen <= 1) {
+ while (p < pend) {
+ *p++ = *f;
+ }
+ }
+ else {
+ while (p + flen <= pend) {
+ memcpy(p,f,flen);
+ p += flen;
+ }
+ while (p < pend) {
+ *p++ = *f++;
+ }
+ }
}
OBJ_INFECT(res, str);
+ if (flen > 0) OBJ_INFECT(res, pad);
return res;
}
static VALUE
-rb_str_rjust(str, w)
+rb_str_ljust(argc, argv, str)
+ int argc;
+ VALUE *argv;
VALUE str;
- VALUE w;
{
- long width = NUM2LONG(w);
- VALUE res;
- char *p, *pend;
-
- if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
- res = rb_str_new5(str, 0, width);
- p = RSTRING(res)->ptr; pend = p + width - RSTRING(str)->len;
- while (p < pend) {
- *p++ = ' ';
- }
- memcpy(pend, RSTRING(str)->ptr, RSTRING(str)->len);
- OBJ_INFECT(res, str);
- return res;
+ return rb_str_justify(argc, argv, str, 'l');
}
static VALUE
-rb_str_center(str, w)
+rb_str_rjust(argc, argv, str)
+ int argc;
+ VALUE *argv;
VALUE str;
- VALUE w;
{
- long width = NUM2LONG(w);
- VALUE res;
- char *p, *pend;
- long n;
+ return rb_str_justify(argc, argv, str, 'r');
+}
- if (width < 0 || RSTRING(str)->len >= width) return rb_str_dup(str);
- res = rb_str_new5(str, 0, width);
- n = (width - RSTRING(str)->len)/2;
- p = RSTRING(res)->ptr; pend = p + n;
- while (p < pend) {
- *p++ = ' ';
- }
- memcpy(pend, RSTRING(str)->ptr, RSTRING(str)->len);
- p = pend + RSTRING(str)->len; pend = RSTRING(res)->ptr + width;
- while (p < pend) {
- *p++ = ' ';
- }
- OBJ_INFECT(res, str);
- return res;
+static VALUE
+rb_str_center(argc, argv, str)
+ int argc;
+ VALUE *argv;
+ VALUE str;
+{
+ return rb_str_justify(argc, argv, str, 'c');
}
void
@@ -3265,9 +3295,9 @@ Init_String()
rb_define_method(rb_cString, "scan", rb_str_scan, 1);
- rb_define_method(rb_cString, "ljust", rb_str_ljust, 1);
- rb_define_method(rb_cString, "rjust", rb_str_rjust, 1);
- rb_define_method(rb_cString, "center", rb_str_center, 1);
+ rb_define_method(rb_cString, "ljust", rb_str_ljust, -1);
+ rb_define_method(rb_cString, "rjust", rb_str_rjust, -1);
+ rb_define_method(rb_cString, "center", rb_str_center, -1);
rb_define_method(rb_cString, "sub", rb_str_sub, -1);
rb_define_method(rb_cString, "gsub", rb_str_gsub, -1);