デコード3

 懲りずにまたちょっと違うものを書いてみる。が、あまり美しくない。

!String methodsFor: 'converting'!

urlDecode

    | highBit hex asciiVal str |
    str := ReadWriteStream on: ''.
    highBit := hex := false.

    self do: [:each |
        (each ~= $% and: [hex = false])
            ifTrue: [
                (each = $+)
                    ifTrue: [str nextPut: (Character value: 16r20)]
                    ifFalse: [str nextPut: each].
            ]
            ifFalse: [
                (each = $%)
                    ifTrue: [hex := highBit := true]
                    ifFalse: [
                        (highBit)
                            ifTrue: [
                                asciiVal := each asUppercase digitValue * 16.
                                highBit := false.
                            ]
                            ifFalse: [
                                asciiVal := each asUppercase digitValue + asciiVal.
                                asciiVal > 255 ifTrue: [^self].
                                str nextPut: (Character value: asciiVal).
                                hex := false.
                            ].
                    ].
            ].
    ].

    ^str contents.
!

前の記事で書いたやつよりはちょっとだけ速くなった。
最近、「コレクション型であればとにかく do: [〜] したがる病」にかかってる気がする・・・ 年の瀬だというのに、何をしているんだ俺は。