String#pad ?

J

Jani Monoses

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 :)


Jani
 
A

Andreas Schwarz

Jani said:
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 :)

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

Martin DeMello

Jani Monoses said:
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 :)

And doesn't even work :)

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
 
R

Robert Klemme

Jani Monoses said:
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 :)

:)

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 ) )

:)

robert
 
J

Jani Monoses

Martin said:
And doesn't even work :)

It does work the power of two is 4.That's why I said for example :)
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 :(

Jani
 
J

Jani Monoses

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 :)

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
 
R

Robert Klemme

Jani Monoses said:
definitely my bad wording since I mislead both of you :)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top