-
Notifications
You must be signed in to change notification settings - Fork 65
/
PrintfFormatDescriptor.class.st
193 lines (163 loc) · 4.52 KB
/
PrintfFormatDescriptor.class.st
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Class {
#name : #PrintfFormatDescriptor,
#superclass : #Object,
#instVars : [
'flush',
'width',
'precision'
],
#classVars : [
'Flags',
'Operators'
],
#category : #Printf
}
{ #category : #'class initialization' }
PrintfFormatDescriptor class >> initialize [
"PrintfFormatDescriptor initialize"
Operators := Dictionary new.
Operators at: $P put: #PrintStringFormatDescriptor.
Operators at: $c put: #PrintfCharacterFormatDescriptor.
Operators at: $s put: #PrintfStringFormatDescriptor.
#($d $o $u $x $X $p)
do: [:operator | Operators at: operator put: #PrintfNumberFormatDescriptor].
#($e $E $f $g $G $a $A)
do: [:operator | Operators at: operator put: #PrintfFloatFormatDescriptor].
Flags := Dictionary new.
Flags at: $- put: #leftFlush.
Flags at: $+ put: #rightFlush.
Flags at: $ put: #space.
Flags at: $# put: #radix.
Flags at: $0 put: #zero
]
{ #category : #'instance creation' }
PrintfFormatDescriptor class >> new [
^ super new initialize
]
{ #category : #'instance creation' }
PrintfFormatDescriptor class >> newFrom: desc [
| myself |
myself := self new.
myself perform: desc flush.
myself width: desc width.
myself precision: desc precision.
^ myself
]
{ #category : #'instance creation' }
PrintfFormatDescriptor class >> scanFrom: stream [
| desc |
desc := self new.
[Flags includesKey: stream peek]
whileTrue: [desc := desc perform: (Flags at: stream next)].
stream peek isDigit ifTrue: [desc width: (Integer readFrom: stream)].
stream peek == $. ifTrue: [stream next. desc precision: (Integer readFrom: stream)].
stream peek == $l ifTrue: [stream next.
stream peek == $l ifTrue: [stream next]
].
desc := desc operator: stream next.
^ desc
]
{ #category : #rendering }
PrintfFormatDescriptor >> applyOperator: object [
self subclassResponsibility
]
{ #category : #private }
PrintfFormatDescriptor >> flush [
^ flush
]
{ #category : #'initialize-release' }
PrintfFormatDescriptor >> initialize [
flush := #rightFlush.
width := 0
]
{ #category : #scanning }
PrintfFormatDescriptor >> leftFlush [
flush := #leftFlush
]
{ #category : #private }
PrintfFormatDescriptor >> operator: char [
| myself |
myself := (Smalltalk at: (Operators at: char)) newFrom: self.
myself setOperator: char.
^ myself
]
{ #category : #private }
PrintfFormatDescriptor >> padding [
^ Character space
]
{ #category : #private }
PrintfFormatDescriptor >> precision [
^ precision
]
{ #category : #private }
PrintfFormatDescriptor >> precision: anInteger [
precision := anInteger
]
{ #category : #printing }
PrintfFormatDescriptor >> printOn: aStream [
aStream nextPut: $%.
flush == #leftFlush ifTrue: [aStream nextPut: $-]
]
{ #category : #printing }
PrintfFormatDescriptor >> printWidthOn: aStream [
width ~= 0 ifTrue: [width printOn: aStream].
precision isNil ifFalse: [aStream nextPut: $.. precision printOn: aStream]
]
{ #category : #scanning }
PrintfFormatDescriptor >> radix [
^ (PrintfNumberFormatDescriptor newFrom: self) radix
]
{ #category : #rendering }
PrintfFormatDescriptor >> render: object [
| string |
string := self applyOperator: object.
self stringLength ~= 0
ifTrue: [ string := string copyFrom: 1 to: (self stringLength min: string size) ].
width == 0
ifTrue: [ ^ string ].
^ (String new: width withAll: self padding)
copyReplaceFrom: (self startIndexOfCopyReplaceWithStringSize: string size)
to: (self stopIndexOfCopyReplaceWithStringSize: string size)
with: string
]
{ #category : #scanning }
PrintfFormatDescriptor >> rightFlush [
flush := #rightFlush
]
{ #category : #private }
PrintfFormatDescriptor >> setOperator: char [
]
{ #category : #scanning }
PrintfFormatDescriptor >> space [
^ (PrintfNumberFormatDescriptor newFrom: self) space
]
{ #category : #rendering }
PrintfFormatDescriptor >> startIndexOfCopyReplaceWithStringSize: anInteger [
|start|
flush == #leftFlush ifTrue: [start := 1].
flush == #rightFlush ifTrue: [start := width - anInteger + 1].
^(start max: 1)
]
{ #category : #rendering }
PrintfFormatDescriptor >> stopIndexOfCopyReplaceWithStringSize: anInteger [
| stop |
flush == #leftFlush ifTrue: [stop := anInteger].
flush == #rightFlush ifTrue: [stop := width].
^stop min: width
]
{ #category : #private }
PrintfFormatDescriptor >> stringLength [
^ precision isNil ifTrue: [0] ifFalse: [precision]
]
{ #category : #private }
PrintfFormatDescriptor >> width [
^ width
]
{ #category : #private }
PrintfFormatDescriptor >> width: anInteger [
width := anInteger
]
{ #category : #scanning }
PrintfFormatDescriptor >> zero [
^ (PrintfNumberFormatDescriptor newFrom: self) zero
]