help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] What's wrong with this script??


From: Paolo Bonzini
Subject: Re: [Help-smalltalk] What's wrong with this script??
Date: Thu, 25 Feb 2010 20:56:54 +0100
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.7) Gecko/20100120 Fedora/3.0.1-1.fc12 Lightning/1.0b2pre Thunderbird/3.0.1

On 02/25/2010 08:03 PM, Mehul Sanghvi wrote:
Being new to Smalltalk, and learning it on and off when I get time
here and there, what is "new" syntax if the "old" syntax is the "!"
?

New syntax is like this:


Object subclass: Base64 [
    Base64 class >> encode: aString [
        | i j outSize c1 c2 c3 out b64string chars |
        chars := ##('ABCDEFGHIJKLMNOPQRSTUVWXYZ',
                    'abcdefghijklmnopqrstuvwxyz0123456789+/=').
        outSize := aString size // 3 * 4.
        (aString size \\ 3) = 0 ifFalse: [ outSize := outSize + 4 ].
        b64string := String new: outSize.

        i := 1.
        1 to: outSize by: 4 do: [ :j |
            c1 := aString valueAt: i ifAbsent: [0].
            c2 := aString valueAt: i+1 ifAbsent: [0].
            c3 := aString valueAt: i+2 ifAbsent: [0].

            out := c1 bitShift: -2.
            b64string at: j put: (chars at: out + 1).

            out := ((c1 bitAnd: 3) bitShift: 4) bitOr: (c2 bitShift: -4).
            b64string at: j+1 put: (chars at: out + 1).

            out := ((c2 bitAnd: 15) bitShift: 2) bitOr: (c3 bitShift: -6).
            b64string at: j+2 put: (chars at: out + 1).

            out := c3 bitAnd: 16r13F.
            b64string at: j+3 put: (chars at: out + 1).

            i := i + 3.
        ].

        b64string
            replaceFrom: outSize - (i - aString size) + 2
            to: outSize withObject: $=.

       ^b64string
   ]
]




reply via email to

[Prev in Thread] Current Thread [Next in Thread]