🆕 格式化输出一个字符串

This commit is contained in:
fslongjin 2022-01-22 23:01:12 +08:00
parent a79315a31c
commit 1afa20dc55
2 changed files with 51 additions and 0 deletions

View File

@ -114,6 +114,7 @@ int vsprintf(char *buf, const char *fmt, va_list args)
field_width = skip_and_atoi(&fmt);
//获取小数精度
precision = -1;
if (*fmt == '.')
{
++fmt;
@ -162,6 +163,46 @@ int vsprintf(char *buf, const char *fmt, va_list args)
++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;
default:

View File

@ -32,3 +32,13 @@ struct screen_info
extern unsigned char font_ascii[256][16]; //导出ascii字体的bitmap8*16大小
char buf[4096]; //vsprintf()的缓冲区
/**
* fmt和args中的内容进行格式化buf中
* @param buf
* @param fmt
* @param args
* @return
*/
int vsprintf(char *buf, const char *fmt, va_list args);