String#pad?

Hello

is there a nicer way of padding a string to a power of two length than for example

data = data.ljust((data.length+3) & ~3)

this looks too much like C :slight_smile:

Jani

Jani Monoses wrote:

Hello

is there a nicer way of padding a string to a power of two length than for example

data = data.ljust((data.length+3) & ~3)

this looks too much like C :slight_smile:

At least a bit more readable:
data.ljust(data.size + data.size % 2)

And doesn't even work :slight_smile:

class Fixnum
  def lg
    self.to_s(2).length - 1
  end
end

class String
  def ljust_pow2
    self.ljust(2 ** (self.length.lg + 1))
  end
end

p "hello world".ljust_pow2

martin

路路路

Jani Monoses <jani@iv.ro> wrote:

Hello

is there a nicer way of padding a string to a power of two length than for example

data = data.ljust((data.length+3) & ~3)

this looks too much like C :slight_smile:

"Jani Monoses" <jani@iv.ro> schrieb im Newsbeitrag
news:4152A02D.8090501@iv.ro...

Hello

is there a nicer way of padding a string to a power of two length than

for example

Did you mean to say "multiple of two"? 'Cause that's what your code seems
to be doing.

data = data.ljust((data.length+3) & ~3)

this looks too much like C :slight_smile:

:slight_smile:

Different story but not necessarily nicer:

data << (" " * (data.length % 2))
data << (" " * ((data.length + 1) % 2))

If you really meant "power of two" a somewhat weired version:

data.ljust( ( "1" + data.length.to_s(2).gsub(/1/, '0') ).to_i( 2 ) )

:slight_smile:

    robert

Martin DeMello wrote:

Hello

is there a nicer way of padding a string to a power of two length than for example

data = data.ljust((data.length+3) & ~3)

this looks too much like C :slight_smile:

And doesn't even work :slight_smile:

It does work the power of two is 4.That's why I said for example :slight_smile:
Ok my question meant a generic power of two but pasted the line from an actual program, my bad.

class Fixnum
  def lg
    self.to_s(2).length - 1
  end
end

class String
  def ljust_pow2
    self.ljust(2 ** (self.length.lg + 1))
  end
end

p "hello world".ljust_pow2

This is even more work that the line above :frowning:

Jani

路路路

Jani Monoses <jani@iv.ro> wrote:

Did you mean to say "multiple of two"? 'Cause that's what your code seems
to be doing.

definitely my bad wording since I mislead both of you :slight_smile:

I just thought that something out of the box to do this would be nice

String#pad and String#pad!

data.pad!(n) #pad data to next 2^n boundary

or maybe powers of two are two restrictive than make n actually mean that make the strings length a multiple of n.

Jani

"Jani Monoses" <jani@iv.ro> schrieb im Newsbeitrag
news:4152AEEF.1060208@iv.ro...

> Did you mean to say "multiple of two"? 'Cause that's what your code

seems

> to be doing.

definitely my bad wording since I mislead both of you :slight_smile:

I just thought that something out of the box to do this would be nice

String#pad and String#pad!

data.pad!(n) #pad data to next 2^n boundary

or maybe powers of two are two restrictive than make n actually mean

that make the strings

length a multiple of n.

IMHO this application is not general enough. Also, the name #pad does by
no means imply that the length will be n*m after invocation. For the
general case there is already #ljust and #rjust. I'd vote against having
these methods with the functionality you propose in the std lib.

Regards

    robert