• R/O
  • SSH
  • HTTPS

cstl: Commit


Commit MetaInfo

Revision404 (tree)
Time2010-08-26 19:54:06
Authorkatono

Log Message

LibcImpl_sprintfの機能拡張。

Change Summary

Incremental Difference

--- branches/try-vfunc/unittest/LibcImpl.c (revision 403)
+++ branches/try-vfunc/unittest/LibcImpl.c (revision 404)
@@ -42,51 +42,85 @@
4242 return 0;
4343 }
4444
45-static size_t dec2ascii(char *ascii, unsigned int dec, size_t width, int left_flag)
45+static size_t set_ascii(char *ascii, const char *tmp, size_t size, size_t width, int left_flag, int zero_flag)
4646 {
4747 size_t i;
48- size_t ret;
49- char tmp[16];
50- for (i = 0; dec > 0 && i < 16; i++) {
51- tmp[i] = dec % 10;
52- dec /= 10;
53- }
54- ret = i;
55-
5648 if (width > 1) {
5749 if (left_flag) {
58- for (i = 0; i < ret; i++) {
59- ascii[i] = tmp[ret - i - 1] + '0';
50+ for (i = 0; i < size; i++) {
51+ ascii[i] = tmp[size - i - 1];
6052 }
61- if (width > ret) {
62- for (i = 0; i < width - ret; i++) {
63- ascii[ret + i] = ' ';
53+ if (width > size) {
54+ for (i = 0; i < width - size; i++) {
55+ ascii[size + i] = ' ';
6456 }
65- ret += width - ret;
57+ size += width - size;
6658 }
6759 } else {
68- if (width > ret) {
69- for (i = 0; i < width - ret; i++) {
70- ascii[i] = ' ';
60+ if (width > size) {
61+ for (i = 0; i < width - size; i++) {
62+ ascii[i] = zero_flag ? '0' : ' ';
7163 }
72- for (i = 0; i < ret; i++) {
73- ascii[width - ret + i] = tmp[ret - i - 1] + '0';
64+ for (i = 0; i < size; i++) {
65+ ascii[width - size + i] = tmp[size - i - 1];
7466 }
75- ret += width - ret;
67+ size += width - size;
7668 } else {
77- for (i = 0; i < ret; i++) {
78- ascii[i] = tmp[ret - i - 1] + '0';
69+ for (i = 0; i < size; i++) {
70+ ascii[i] = tmp[size - i - 1];
7971 }
8072 }
8173 }
8274 } else {
83- for (i = 0; i < ret; i++) {
84- ascii[i] = tmp[ret - i - 1] + '0';
75+ for (i = 0; i < size; i++) {
76+ ascii[i] = tmp[size - i - 1];
8577 }
8678 }
87- return ret;
79+ return size;
8880 }
8981
82+static size_t dec2ascii(char *ascii, unsigned int dec, size_t width, int left_flag, int zero_flag, int signed_flag)
83+{
84+ size_t i;
85+ char tmp[16];
86+ const char *num_str = "0123456789";
87+ int signed_dec = (int) dec;
88+ if (dec == 0) {
89+ tmp[0] = '0';
90+ i = 1;
91+ } else if (signed_flag && signed_dec < 0) {
92+ signed_dec = -signed_dec;
93+ for (i = 0; signed_dec > 0 && i < 15; i++) {
94+ tmp[i] = num_str[signed_dec % 10];
95+ signed_dec /= 10;
96+ }
97+ tmp[i++] = '-';
98+ } else {
99+ for (i = 0; dec > 0 && i < 16; i++) {
100+ tmp[i] = num_str[dec % 10];
101+ dec /= 10;
102+ }
103+ }
104+ return set_ascii(ascii, tmp, i, width, left_flag, zero_flag);
105+}
106+
107+static size_t hex2ascii(char *ascii, unsigned int hex, size_t width, int left_flag, int zero_flag, int case_char)
108+{
109+ size_t i;
110+ char tmp[16];
111+ const char *num_str = (case_char == 'X') ? "0123456789ABCDEF" : "0123456789abcdef";
112+ if (hex == 0) {
113+ tmp[0] = '0';
114+ i = 1;
115+ } else {
116+ for (i = 0; hex > 0 && i < 16; i++) {
117+ tmp[i] = num_str[hex & 0xf];
118+ hex >>= 4;
119+ }
120+ }
121+ return set_ascii(ascii, tmp, i, width, left_flag, zero_flag);
122+}
123+
90124 int LibcImpl_sprintf(char *buf, const char *format, void *arg1, void *arg2, void *arg3, void *arg4)
91125 {
92126 size_t i;
@@ -96,6 +130,7 @@
96130 const char *tmp_str;
97131 unsigned int tmp_val;
98132 int left_flag;
133+ int zero_flag;
99134 size_t width;
100135 size_t inc;
101136
@@ -106,47 +141,73 @@
106141
107142 i = 0;
108143 while (*p != '\0') {
144+ if (*p != '%') {
145+ buf[i++] = *(p++);
146+ continue;
147+ }
148+ p++;
109149 if (*p == '%') {
150+ buf[i++] = *(p++);
151+ continue;
152+ }
153+ if (*p == '-') {
154+ left_flag = 1;
110155 p++;
111- if (*p == '%') {
112- buf[i++] = *(p++);
113- } else {
114- if (*p == '-') {
115- left_flag = 1;
116- p++;
117- } else {
118- left_flag = 0;
119- }
120- if ('1' <= *p && *p <= '9') {
121- width = *p - '0';
122- p++;
123- } else {
124- width = 0;
125- }
126- switch (*p) {
127- case 'd':
128- tmp_val = (unsigned int) arg_list[arg_idx];
129- inc = dec2ascii(&buf[i], tmp_val, width, left_flag);
130- i += inc;
131- p++;
132- arg_idx++;
133- break;
134- case 's':
135- tmp_str = (const char *) arg_list[arg_idx];
136- while (*tmp_str != '\0') {
137- buf[i++] = *(tmp_str++);
138- }
139- p++;
140- arg_idx++;
141- break;
142- default:
143- p++;
144- break;
145- }
156+ } else {
157+ left_flag = 0;
158+ }
159+ if (*p == '0') {
160+ zero_flag = 1;
161+ p++;
162+ } else {
163+ zero_flag = 0;
164+ }
165+ if ('1' <= *p && *p <= '9') {
166+ width = *p - '0';
167+ p++;
168+ if ('0' <= *p && *p <= '9') {
169+ width = 10 * width + (*p - '0');
170+ p++;
146171 }
147172 } else {
148- buf[i++] = *(p++);
173+ width = 0;
149174 }
175+ switch (*p) {
176+ case 'c':
177+ buf[i++] = (int) arg_list[arg_idx++];
178+ p++;
179+ break;
180+ case 'd':
181+ case 'i':
182+ case 'u':
183+ tmp_val = (unsigned int) arg_list[arg_idx++];
184+ inc = dec2ascii(&buf[i], tmp_val, width, left_flag, zero_flag, (*p == 'u') ? 0 : 1);
185+ i += inc;
186+ p++;
187+ break;
188+ case 'p':
189+ buf[i++] = '0';
190+ buf[i++] = 'x';
191+ zero_flag = 1;
192+ width = 8;
193+ case 'x':
194+ case 'X':
195+ tmp_val = (unsigned int) arg_list[arg_idx++];
196+ inc = hex2ascii(&buf[i], tmp_val, width, left_flag, zero_flag, *p);
197+ i += inc;
198+ p++;
199+ break;
200+ case 's':
201+ tmp_str = (const char *) arg_list[arg_idx++];
202+ while (*tmp_str != '\0') {
203+ buf[i++] = *(tmp_str++);
204+ }
205+ p++;
206+ break;
207+ default:
208+ p++;
209+ break;
210+ }
150211 }
151212 buf[i] = '\0';
152213 return (int) i;
--- branches/try-vfunc/unittest/test/test_LibcImpl.c (revision 403)
+++ branches/try-vfunc/unittest/test/test_LibcImpl.c (revision 404)
@@ -25,11 +25,16 @@
2525 printf("%s\n", buf);
2626 assert(strcmp(buf, sp_buf) == 0);
2727
28- sprintf(sp_buf, "%d", 9999);
29- LibcImpl_sprintf(buf, "%d", (void *)9999, 0, 0, 0);
28+ sprintf(sp_buf, "%d, %d, %d, \"%8d\"", 9999, 0, -1, -1234);
29+ LibcImpl_sprintf(buf, "%d, %d, %d, \"%8d\"", (void *)9999, (void*)0, (void*)-1, (void*)-1234);
3030 printf("%s\n", buf);
3131 assert(strcmp(buf, sp_buf) == 0);
3232
33+ sprintf(sp_buf, "%u, %u, %-d, \"%-8d\"", 12345, 0, -123, -111);
34+ LibcImpl_sprintf(buf, "%u, %u, %-d, \"%-8d\"", (void *)12345, (void*)0, (void*)-123, (void*)-111);
35+ printf("%s\n", buf);
36+ assert(strcmp(buf, sp_buf) == 0);
37+
3338 sprintf(sp_buf, "\"%d\", \"%8d\", \"%-8d\", \"%s\"", 123456789, 123456, 789, "abcd");
3439 LibcImpl_sprintf(buf, "\"%d\", \"%8d\", \"%-8d\", \"%s\"", (void *)123456789, (void *)123456, (void *)789, "abcd");
3540 printf("%s\n", buf);
@@ -40,6 +45,26 @@
4045 printf("%s\n", buf);
4146 assert(strcmp(buf, sp_buf) == 0);
4247
48+ sprintf(sp_buf, "%x, \"%8x\", \"%08X\", \"%10d\"", 0x1234, 0x5678, 0xABCD, 12345);
49+ LibcImpl_sprintf(buf, "%x, \"%8x\", \"%08X\", \"%10d\"", (void *)0x1234, (void*)0x5678, (void*)0xABCD, (void*)12345);
50+ printf("%s\n", buf);
51+ assert(strcmp(buf, sp_buf) == 0);
52+
53+ sprintf(sp_buf, "%x, \"%8x\", \"%-8X\", \"%-10d\"", 0x0, 0x5678, 0xABCD, 12345);
54+ LibcImpl_sprintf(buf, "%x, \"%8x\", \"%-8X\", \"%-10d\"", (void *)0x0, (void*)0x5678, (void*)0xABCD, (void*)12345);
55+ printf("%s\n", buf);
56+ assert(strcmp(buf, sp_buf) == 0);
57+
58+ sprintf(sp_buf, "%c, %c, %c, %c", 'a', '#', '\\', '9');
59+ LibcImpl_sprintf(buf, "%c, %c, %c, %c", (void *)'a', (void*)'#', (void*)'\\', (void*)'9');
60+ printf("%s\n", buf);
61+ assert(strcmp(buf, sp_buf) == 0);
62+
63+ sprintf(sp_buf, "%p, %p, %p, %p", (void*)0, (void*)buf, (void*)sp_buf, (void*)0);
64+ LibcImpl_sprintf(buf, "%p, %p, %p, %p", (void *)0, (void*)buf, (void*)sp_buf, (void*)0);
65+ printf("%s\n", sp_buf);
66+ printf("%s\n", buf);
67+
4368 }
4469
4570 int main(void)
Show on old repository browser