mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-09 07:06:47 +00:00
🆕 格式化输出一个字符串
This commit is contained in:
parent
a79315a31c
commit
1afa20dc55
@ -114,6 +114,7 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
field_width = skip_and_atoi(&fmt);
|
field_width = skip_and_atoi(&fmt);
|
||||||
|
|
||||||
//获取小数精度
|
//获取小数精度
|
||||||
|
precision = -1;
|
||||||
if (*fmt == '.')
|
if (*fmt == '.')
|
||||||
{
|
{
|
||||||
++fmt;
|
++fmt;
|
||||||
@ -162,6 +163,46 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
++str;
|
++str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
//显示一个字符串
|
||||||
|
case 's':
|
||||||
|
s = va_arg(args, char *);
|
||||||
|
if (!s)
|
||||||
|
s = '\0';
|
||||||
|
len = strlen(s);
|
||||||
|
if (precision < 0)
|
||||||
|
{
|
||||||
|
//未指定精度
|
||||||
|
precision = len;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (len > precision)
|
||||||
|
{
|
||||||
|
len = precision;
|
||||||
|
}
|
||||||
|
|
||||||
|
//靠右对齐
|
||||||
|
if (!(flags & LEFT))
|
||||||
|
while (len < field_width--)
|
||||||
|
{
|
||||||
|
*str = ' ';
|
||||||
|
++str;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
*str = *s;
|
||||||
|
++s;
|
||||||
|
++str;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (len<field_width--)
|
||||||
|
{
|
||||||
|
*str = ' ';
|
||||||
|
++str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -32,3 +32,13 @@ struct screen_info
|
|||||||
extern unsigned char font_ascii[256][16]; //导出ascii字体的bitmap(8*16大小)
|
extern unsigned char font_ascii[256][16]; //导出ascii字体的bitmap(8*16大小)
|
||||||
|
|
||||||
char buf[4096]; //vsprintf()的缓冲区
|
char buf[4096]; //vsprintf()的缓冲区
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将字符串按照fmt和args中的内容进行格式化,然后保存到buf中
|
||||||
|
* @param buf 结果缓冲区
|
||||||
|
* @param fmt 格式化字符串
|
||||||
|
* @param args 内容
|
||||||
|
* @return 最终字符串的长度
|
||||||
|
*/
|
||||||
|
int vsprintf(char *buf, const char *fmt, va_list args);
|
Loading…
x
Reference in New Issue
Block a user