-
Notifications
You must be signed in to change notification settings - Fork 0
/
PercentEncoder.cls
72 lines (61 loc) · 2.06 KB
/
PercentEncoder.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"Filed out from Dolphin Smalltalk 7"!
Object subclass: #PercentEncoder
instanceVariableNames: 'reserved'
classVariableNames: 'Current'
poolDictionaries: ''
classInstanceVariableNames: ''!
PercentEncoder guid: (GUID fromString: '{183707c5-6b54-4780-b5d8-71fa30c46297}')!
PercentEncoder comment: ''!
!PercentEncoder categoriesForClass!Unclassified! !
!PercentEncoder methodsFor!
decode: aString
"
PercentEncoder decode: 'this%20is%20AT+%40'
"
| reader raw |
reader := aString readStream.
raw := String streamContents: [:strm |
[reader atEnd] whileFalse: [| char |
char := reader next.
(reader position > 1 and: [char = $+]) ifTrue: [strm space] ifFalse: [| code |
char = $%
ifTrue: [
code := reader next digitValue * 16 + reader next digitValue.
char := Character utf8Value: code].
strm nextPut: char]]].
^String fromUTF8: raw!
encode: aString
"
PercentEncoder encode: 'this is AT @'
"
^String streamContents: [:strm |
aString do: [:char |
((reserved includes: char) or: [char codePoint > 127])
ifTrue: [
char asUtf8String asByteArray do: [:byte | | hex |
hex := byte printStringBase: 16.
strm nextPut: $%; nextPutAll: hex]]
ifFalse: [strm nextPut: char]]]!
initialize
super initialize.
self initializeReserved!
initializeReserved
reserved := ' ?:@&=+$#;%/\!!'! !
!PercentEncoder categoriesFor: #decode:!public!services! !
!PercentEncoder categoriesFor: #encode:!public!services! !
!PercentEncoder categoriesFor: #initialize!initializing!public! !
!PercentEncoder categoriesFor: #initializeReserved!initializing!public! !
!PercentEncoder class methodsFor!
current
Current isNil ifTrue: [Current := self new].
^Current!
decode: aString
^self current decode: aString!
encode: aString
^self current encode: aString!
new
^super new initialize! !
!PercentEncoder class categoriesFor: #current!accessing!public! !
!PercentEncoder class categoriesFor: #decode:!public!services! !
!PercentEncoder class categoriesFor: #encode:!public!services! !
!PercentEncoder class categoriesFor: #new!instance creation!public! !