Printf "%0x"

printf e$B$Ne(B %0x [email protected]?t$rM?$($k$H!"CM$K$h$C$Fe(B …
e$B$,$D$$$?$je(B
e$B$D$+$J$+$C$?$j$7$^$9!#e(B

% ./ruby -ve ‘[-0x40000000, -0x40000001].each {|v| printf “%0x\n”, v }’
ruby 1.9.0 (2008-01-19 revision 0) [i686-linux]
…fc0000000
fbfffffff

[email protected]$G$9!#e(B

At Mon, 21 Jan 2008 02:50:50 +0900,
Tanaka A. wrote in [ruby-dev:33224]:

printf e$B$Ne(B %0x [email protected]?t$rM?$($k$H!"CM$K$h$C$Fe(B … e$B$,$D$$$?$je(B
e$B$D$+$J$+$C$?$j$7$^$9!#e(B

% ./ruby -ve ‘[-0x40000000, -0x40000001].each {|v| printf “%0x\n”, v }’
ruby 1.9.0 (2008-01-19 revision 0) [i686-linux]
…fc0000000
fbfffffff

e$B$=$l0J30$K$b!"@:EY$r;XDj$9$k$HL/$J46$8$G$9!#e(B

$ ruby19 -ve ‘printf “|%1$.4x|%1$04x|%1$6.4x|\n”, -1’
ruby 1.9.0 (2008-01-19 revision 15119) [i686-linux]
|f…f|f…f| f…f|

test/ruby/test_sprintf.rbe$B$K$Oe(B%be$B$N%F%9%H$7$+$J$$$N$G$9$,!“Ii$NCMe(B
e$B$Ge(B”…"e$B$,$D$+$J$$$H$$$&$N$O%F%9%H$N$[$&$,$*$+$7$/$J$$$G$7$g$&$+!#e(B

  • sprintf.c (remove_sign_bits): returns pointer to the first char to
    be used, instead of copying.

  • sprintf.c (rb_str_format): negative indicator dots should come
    before sign digits always. [ruby-dev:33224]

Index: sprintf.c

— sprintf.c (revision 15146)
+++ sprintf.c (working copy)
@@ -47,10 +47,6 @@ remove_sign_bits(char *str, int base)
}
}

  • if (t > s) {

  • while (*t) *s++ = *t++;

  • *s = ‘\0’;

  • }

  • return str;

  • return t;
    }

@@ -509,8 +505,9 @@ rb_str_format(int argc, const VALUE *arg
case ‘u’:
{

  • volatile VALUE tmp1;
    volatile VALUE val = GETARG();
  • char fbuf[32], nbuf[64], *s, *t;
  • char fbuf[32], nbuf[64], *s;
    const char *prefix = 0;
  • int sign = 0;
  • int sign = 0, dots = 0;
    char sc = 0;
    long v = 0;
    @@ -613,17 +610,17 @@ rb_str_format(int argc, const VALUE *arg
    sprintf(fbuf, “%%l%c”, c);
    sprintf(nbuf, fbuf, v);
  •  s = nbuf;
       }
       else {
     s = nbuf;
     if (v < 0) {
    
  •      strcpy(s, "..");
    
  •      s += 2;
    
  •      dots = 1;
     }
     sprintf(fbuf, "%%l%c", *p == 'X' ? 'x' : *p);
    
  •  sprintf(s, fbuf, v);
    
  •  sprintf(++s, fbuf, v);
     if (v < 0) {
         char d = 0;
    
  •      remove_sign_bits(s, base);
    
  •      s = remove_sign_bits(s, base);
         switch (base) {
           case 16:
    

@@ -633,10 +630,8 @@ rb_str_format(int argc, const VALUE *arg
}
if (d && *s != d) {

  •    memmove(s+1, s, strlen(s)+1);
    
  •    *s = d;
    
  •    *--s = d;
         }
     }
       }
    
  •    s = nbuf;
    
    }
    else {
    @@ -659,5 +654,4 @@ rb_str_format(int argc, const VALUE *arg
    }
    else {
  •                    volatile VALUE tmp1;
     if (!RBIGNUM_SIGN(val)) {
         val = rb_big_clone(val);
    

@@ -667,24 +661,17 @@ rb_str_format(int argc, const VALUE *arg
s = RSTRING_PTR(tmp);
if (*s == ‘-’) {

  •      dots = 1;
         if (base == 10) {
       rb_warning("negative number for %%u specifier");
         }
    
  •      remove_sign_bits(++s, base);
    
  •      tmp = rb_str_new(0, 3+strlen(s));
    
  •      t = RSTRING_PTR(tmp);
    
  •      if (!(flags&(FPREC|FZERO))) {
    
  •    strcpy(t, "..");
    
  •    t += 2;
    
  •      }
    
  •      s = remove_sign_bits(++s, base);
         switch (base) {
           case 16:
    
  •    if (s[0] != 'f') strcpy(t++, "f"); break;
    
  •    if (s[0] != 'f') *--s = 'f'; break;
           case 8:
    
  •    if (s[0] != '7') strcpy(t++, "7"); break;
    
  •    if (s[0] != '7') *--s = '7'; break;
           case 2:
    
  •    if (s[0] != '1') strcpy(t++, "1"); break;
    
  •    if (s[0] != '1') *--s = '1'; break;
         }
    
  •      strcpy(t, s);
    
  •      s  = RSTRING_PTR(tmp);
     }
       }
    

@@ -693,4 +680,8 @@ rb_str_format(int argc, const VALUE *arg
pos = -1;
len = strlen(s);

  • if (dots) {

  •    prec -= 2;
    
  •    width -= 2;
    
  • }

    if (*p == ‘X’) {
    @@ -722,4 +713,8 @@ rb_str_format(int argc, const VALUE *arg
    }
    CHECK(prec - len);

  • if (dots) {

  •    memcpy(&buf[blen], "..", 2);
    
  •    blen += 2;
    
  • }
    if (!bignum && v < 0) {
    char c = sign_bits(base, p);
    Index: test/ruby/test_sprintf.rb
    ===================================================================
    — test/ruby/test_sprintf.rb (revision 15146)
    +++ test/ruby/test_sprintf.rb (working copy)
    @@ -20,15 +20,15 @@ class TestSprintf < Test::Unit::TestCase
    assert_equal(“0001”, sprintf("%04b", 1))
    assert_equal(“0010”, sprintf("%04b", 2))

  • assert_equal(“1111”, sprintf("%04b", -1))
  • assert_equal("…11", sprintf("%04b", -1))

    assert_equal(“0000”, sprintf("%.4b", 0))
    assert_equal(“0001”, sprintf("%.4b", 1))
    assert_equal(“0010”, sprintf("%.4b", 2))

  • assert_equal(“1111”, sprintf("%.4b", -1))
  • assert_equal("…11", sprintf("%.4b", -1))

    assert_equal(" 0000", sprintf("%6.4b", 0))
    assert_equal(" 0001", sprintf("%6.4b", 1))
    assert_equal(" 0010", sprintf("%6.4b", 2))

  • assert_equal(" 1111", sprintf("%6.4b", -1))
  • assert_equal(" …11", sprintf("%6.4b", -1))

    assert_equal(" 0b0", sprintf("%#4b", 0))
    @@ -40,15 +40,15 @@ class TestSprintf < Test::Unit::TestCase
    assert_equal(“0b01”, sprintf("%#04b", 1))
    assert_equal(“0b10”, sprintf("%#04b", 2))

  • assert_equal(“0b11”, sprintf("%#04b", -1))
  • assert_equal(“0b…1”, sprintf("%#04b", -1))

    assert_equal(“0b0000”, sprintf("%#.4b", 0))
    assert_equal(“0b0001”, sprintf("%#.4b", 1))
    assert_equal(“0b0010”, sprintf("%#.4b", 2))

  • assert_equal(“0b1111”, sprintf("%#.4b", -1))
  • assert_equal(“0b…11”, sprintf("%#.4b", -1))

    assert_equal(“0b0000”, sprintf("%#6.4b", 0))
    assert_equal(“0b0001”, sprintf("%#6.4b", 1))
    assert_equal(“0b0010”, sprintf("%#6.4b", 2))

  • assert_equal(“0b1111”, sprintf("%#6.4b", -1))
  • assert_equal(“0b…11”, sprintf("%#6.4b", -1))

    assert_equal("+0", sprintf("%+b", 0))

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:33225] Re: printf “%0x””
on Mon, 21 Jan 2008 05:45:17 +0900, Nobuyoshi N.
[email protected] writes:

|At Mon, 21 Jan 2008 02:50:50 +0900,
|Tanaka A. wrote in [ruby-dev:33224]:
|> printf e$B$Ne(B %0x [email protected]?t$rM?$($k$H!“CM$K$h$C$Fe(B … e$B$,$D$$$?$je(B
|> e$B$D$+$J$+$C$?$j$7$^$9!#e(B
|>
|> % ./ruby -ve ‘[-0x40000000, -0x40000001].each {|v| printf “%0x\n”, v }’
|> ruby 1.9.0 (2008-01-19 revision 0) [i686-linux]
|> …fc0000000
|> fbfffffff
|
|e$B$=$l0J30$K$b!”@:EY$r;XDj$9$k$HL/$J46$8$G$9!#e(B
|
|$ ruby19 -ve ‘printf “|%1$.4x|%1$04x|%1$6.4x|\n”, -1’
|ruby 1.9.0 (2008-01-19 revision 15119) [i686-linux]
||f…f|f…f| f…f|

e$B%3%_%C%H$7$F$/[email protected]$5$$!#e(B

In article [email protected],
Nobuyoshi N. [email protected] writes:

test/ruby/test_sprintf.rbe$B$K$Oe(B%be$B$N%F%9%H$7$+$J$$$N$G$9$,!“Ii$NCMe(B
e$B$Ge(B”…"e$B$,$D$+$J$$$H$$$&$N$O%F%9%H$N$[$&$,$*$+$7$/$J$$$G$7$g$&$+!#e(B

e$B$?$7$+$K$=$s$J5$$b$7$^$9!#e(B

[email protected]!“e(B%0x e$B$NOC$O$b$H$b$He(B URI e$B$Ne(B inspect
e$B$G8+$D$1$?$b$N$G!”$=e(B
e$B$3$G$Oe(B … e$B$,$D$+$J$$$3$H$r0U?^$7$F$$$k$h$&$G$9!#e(B

e$B:#$Oe(B
% ./ruby -ruri -e ‘p URI("")’
#<URI::Generic:0x…fdbceff44 URL:>
e$B$H$$$&$h$&$Ke(B … e$B$,$D$$$F$A$g$C$HJQ$J$+$s$8$G$9!#e(B

e$B$^$!!“e(BURI e$B$Ne(B inspect e$B$G$O%”%I%l%9e(B
(e$B$G$O$J$$$1$l$I$=$l$C$]$$e(B
e$B==O;?J?te(B) e$B$O=P$5$J$$$N$,$$$$$s$8$c$J$$$+$H$$$&5$$b$7$J$/$O$"e(B
e$B$j$^$;$s$,!#e(B

[email protected]$G$9!#e(B

At Thu, 24 Jan 2008 15:54:38 +0900,
Tanaka A. wrote in [ruby-dev:33347]:

#<URI::Generic:0x…fdbceff44 URL:>
e$B$H$$$&$h$&$Ke(B … e$B$,$D$$$F$A$g$C$HJQ$J$+$s$8$G$9!#e(B

e$B$3$l$Oe(BKernel#to_se$B$r;H$($P$$$$$s$8$c$J$$$G$9$+$M!#e(B

e$B$^$!!“e(BURI e$B$Ne(B inspect e$B$G$O%”%I%l%9e(B (e$B$G$O$J$$$1$l$I$=$l$C$]$$e(B
e$B==O;?J?te(B) e$B$O=P$5$J$$$N$,$$$$$s$8$c$J$$$+$H$$$&5$$b$7$J$/$O$"e(B
e$B$j$^$;$s$,!#e(B

e$B%G%P%C%0$N$H$-$H$+$K$O!"$J$s$+$7$i=P$F$/$l$?$[$&$,$"$j$,$?$$$s$8$ce(B
e$B$J$$$G$7$g$&$+!#e(B

Index: lib/uri/generic.rb

— lib/uri/generic.rb (revision 15202)
+++ lib/uri/generic.rb (working copy)
@@ -1104,6 +1104,7 @@ module URI
end

  • @@to_s = Kernel.instance_method(:to_s)
    def inspect
  •  sprintf("#<%s:%#0x URL:%s>", self.class.to_s, self.object_id, 
    

self.to_s)

  •  @@to_s.bind(self).call.sub!(/>\z/) {" URL:#{self}>"}
    
    end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs