Are there any padding methods in Collection classes?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Are there any padding methods in Collection classes?

Tim M
I keep looking for ways to pad a String or any Collection - and I don't see
any methods for this?

The best I could come up with was:

padded := aCollection , (aCollection class new: desiredSize - aCollection
size - 1 withAll: paddingChar)

But there must be a more elegent way to do this?

Tim


Reply | Threaded
Open this post in threaded view
|

Re: Are there any padding methods in Collection classes?

Ian Bartholomew-21
Tim,

>I keep looking for ways to pad a String or any Collection - and I don't see
>any methods for this?

You can pad Strings with spaces using #sprintf:

'%30s' sprintfWith: 'left padded'
'%-30s' sprintfWith: 'right padded'

or with $0 instead of a space

'%030s' sprintfWith: 'left padded'

>But there must be a more elegent way to do this?

Define elegant :-)

You could maybe use a #copyReplace (probably the most efficient?) ....

aCollection := #[1 2 3 4 5].

newCollection := ((aCollection species new: 50) atAllPut: 0)
        copyReplaceFrom: 1
        to: aCollection size
        with: aCollection.

or maybe a Stream...

aCollection := #[1 2 3 4 5].

newCollection := aCollection species writeStream
        nextPutAll: aCollection;
        nextPutAll: ((aCollection species new: (50 - aCollection
size)) atAllPut: 0);
        contents.

and no doubt there are other ways....
--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: Are there any padding methods in Collection classes?

Bill Dargel
In reply to this post by Tim M
Tim M wrote:
> I keep looking for ways to pad a String or any Collection - and I don't
> see any methods for this?

Me too.

> The best I could come up with was:
>
> padded := aCollection , (aCollection class new: desiredSize -
> aCollection size - 1 withAll: paddingChar)
>
> But there must be a more elegent way to do this?

How about:
     padded := aCollection paddedTo: desiredSize.
:-)
At least given Space as the paddingChar.

Seems I added the following extension to String some time ago:

!String methodsFor!
paddedTo: anInteger
        ^self , (self species new: (anInteger - self size max: 0)
                                withAll: Character space)! !

I also have a similar #rightJustify: that pads on the left with spaces.

BTW, I think the "-1" in your example must be a bug.
Another good reason to make the code into its own method, which can then
be easily tested. I found I also have:

!StringExtensionsTest methodsFor!
testPaddedTo
        self assert: ('abc' paddedTo: 4) = 'abc '.
        self assert: ('abc' paddedTo: 3) = 'abc'.
        self assert: ('abc' paddedTo: 2) = 'abc'.
        self assert: ('abc' paddedTo: 0) = 'abc'.
        self assert: ('' paddedTo: 0) = ''.
        self assert: ('' paddedTo: 2) = '  '! !

--
Bill Dargel            [hidden email]
Shoshana Technologies
100 West Joy Road, Ann Arbor, MI 48105  USA


Reply | Threaded
Open this post in threaded view
|

Re: Are there any padding methods in Collection classes?

Howard Oh
Bill Dargel 작성:



My SequenceableCollection has following methods to do the thing.
They don't use species new: but fast enough for me so far.

leftPad: anObject toBecomeSize: anInteger

        | writeStream |

        writeStream := WriteStream on: self class new.
        [ writeStream size + self size < anInteger ] whileTrue:
                [ writeStream nextPut: anObject ].
        writeStream nextPutAll: self.
        ^writeStream contents

rightPad: anObject toBecomeSize: anInteger

        | writeStream |

        writeStream := WriteStream on: self class new.
        writeStream nextPutAll: self.
        [ writeStream size < anInteger ] whileTrue:
                [ writeStream nextPut: anObject ].
        ^writeStream contents


Reply | Threaded
Open this post in threaded view
|

Re: Are there any padding methods in Collection classes?

Jochen Riekhof-7
In reply to this post by Tim M
Hi...

If I am sure my padded string is not exceeding a certain length I use

(' ' copyFrom: str size), str

where the leading string must be one character shorter than the total pad
size due to smalltalk indexing starting from 1. Of courde zou can use any
string here.

Ciao

...Jochen