-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathprintf.gsf
126 lines (106 loc) · 2.11 KB
/
printf.gsf
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
*
* currently, only support %0*[0-9]d
*
function printf( format, arg.1, arg.2, arg.3, arg.4, arg.5, arg.6, arg.7, arg.8, arg.9, arg.10 )
ret = ''
buf.1 = ''
length = math_strlen( format )
i = 1
a = 1
stat = 0
buf.1 = ''
buf.2 = ''
buf.3 = ''
while( i <= length )
*say 'stat = ' % stat
tmp = substr( format, i, 1 )
*say tmp
***** stat=0: normal string *****
* 0 -> 1
if( stat = 0 & tmp = '%' )
buf.1 = tmp
stat = 1
i = i + 1 ; continue
endif
* 0 -> 0
if( stat = 0 )
ret = ret % tmp
i = i + 1 ; continue
endif
***** 1: buf.1='%' *****
* 1 -> 2
if( stat = 1 & tmp = '0' )
buf.2 = tmp
stat = 2
i = i + 1 ; continue
endif
* 1 -> 3
if( stat = 1 & valnum(tmp) = 1 )
buf.2 = ' '
buf.3 = tmp
stat = 3
i = i + 1 ; continue
endif
* 1 -> 0
if( stat = 1 & tmp = 'd' )
ret = ret % math_int(arg.a)
buf.1 = ''
stat = 0
a = a + 1
i = i + 1 ; continue
endif
* 1 -> 0
if( stat = 1 )
ret = ret % buf.1 % tmp
buf.1 = ''
stat = 0
i = i + 1 ; continue
endif
***** 2: buf.1='%', buf.2='0' *****
* 2 -> 3
if( stat = 2 & valnum(tmp) = 1 )
buf.3 = tmp
stat = 3
i = i + 1 ; continue
endif
* 2 -> 0
if( stat = 2 )
ret = ret % buf.1 % buf.2 % tmp
buf.1 = ''
buf.2 = ''
stat = 0
i = i + 1 ; continue
endif
***** 3: buf.1='%', buf.2='0' or ' ', buf.3=[0-9] *****
* 3 -> 0
if( stat = 3 & tmp = 'd' )
argc = math_int( arg.a )
j = math_strlen( argc ) + 1
while( j <= buf.3 )
ret = ret % buf.2
j = j + 1
endwhile
ret = ret % argc
buf.3 = ''
buf.2 = ''
buf.1 = ''
stat = 0
a = a + 1
i = i + 1 ; continue
endif
* 3 -> 0
if( stat = 3 )
ret = ret % buf.1 % buf.2 % buf.3 % tmp
buf.3 = ''
buf.2 = ''
buf.1 = ''
stat = 0
i = i + 1 ; continue
endif
* say tmp
i = i + 1
endwhile
* flush buffer
ret = ret % buf.1 % buf.2 % buf.3
return ret
end function