sprint: replace sprintf usage

This commit is contained in:
corey 2024-01-15 15:59:50 -06:00
parent bcba92f1b0
commit 8140ff2f37
4 changed files with 98 additions and 86 deletions

View File

@ -4,7 +4,7 @@
CFLAGS= -Wfatal-errors -Wall -Wextra -Werror=discarded-qualifiers -Werror=ignored-qualifiers
LDFLAGS= -g3 -no-pie -z noexecstack -lm
ASFLAGS=
OBJS= str.o tok.o vec.o lex.o pnode.o reg.o mem.o state.o err.o x86_64.o run.o ir.o gen.o i386.o
OBJS= str.o tok.o vec.o lex.o pnode.o reg.o mem.o state.o err.o x86_64.o run.o ir.o gen.o i386.o sprint.o
LIB= libpar.a
TESTS_i386= $(notdir $(basename $(shell find tests/i386/ -name '*.par'))) $(notdir $(basename $(shell find tests/all/ -name '*.par')))
TESTS_x86_64= $(notdir $(basename $(shell find tests/x86_64/ -name '*.par'))) $(notdir $(basename $(shell find tests/all/ -name '*.par')))

20
err.c
View File

@ -1,8 +1,10 @@
#include<stdio.h>
#include<stdarg.h>
#include<unistd.h>
#include"err.h"
#include"vec.h"
#include"str.h"
#include"sprint.h"
void err_log(char*fmt,...)
{
@ -29,13 +31,13 @@ void err_log(char*fmt,...)
case 'd':
case 'u':
ibuffer=va_arg(va,uint32_t);
sprintf(sbuffer,"%u",ibuffer);
sprint(sbuffer,"%u",ibuffer);
str_append(&str,sbuffer);
break;
case 'x':
ibuffer=va_arg(va,uint32_t);
sprintf(sbuffer,"%#x",ibuffer);
sprint(sbuffer,"%x",ibuffer);
str_append(&str,sbuffer);
break;
@ -65,11 +67,15 @@ void err_log(char*fmt,...)
va_end(va);
fprintf(stderr,"%serror%s: %s\n",
"\033[31m",
"\033[0m",
str.buffer
);
{
char b[1024];
int n=sprint(b,"%serror%s: %s\n",
"\033[31m",
"\033[0m",
str.buffer
);
write(2,b,n);
}
str_free(&str);
}

1
err.h
View File

@ -6,5 +6,6 @@
#include<string.h>
#include"str.h"
#include"vec.h"
#include"sprint.h"
void err_log(char*fmt,...);

161
sprint.c
View File

@ -11,86 +11,12 @@
#define __r8(x) asm("movq %%r8,%0":"=m"(x))
#define __r9(x) asm("movq %%r9,%0":"=m"(x))
int sprinti16(char*s,int d)
{
static char g[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char b[32]={0};
int bp=0;
int i=0;
if(!s)return 0;
if(d<0)
{
b[bp++]='-';
d=-d;
}
// Get each digit as character
while(d>0)
{
int m=d%16;
d/=16;
b[i++]=g[m];
}
b[i]=0;
int max=i;
// Print reversed string
for(;i>=0;--i)
{
if(b[i]>31)
s[max-i]=b[i];
else
s[max-i]=1;
}
s[max+1]=0;
return max;
}
int sprinti(char*s,int d)
{
char b[32]={0};
int bp=0;
int i=0;
if(!s)return 0;
if(d<0)
{
b[bp++]='-';
d=-d;
}
// Get each digit as character
while(d>0)
{
int m=d%10;
d/=10;
b[i++]=m+'0';
}
b[i]=0;
int max=i;
// Print reversed string
for(;i>=0;--i)
{
if(b[i]>31)
s[max-i]=b[i];
else
s[max-i]=1;
}
s[max+1]=0;
return max;
}
static int sprinti(char*s,int d);
static int sprinti16(char*s,int d);
// Print formatted string with variable number
// of arguments into destination string dest
char*sprint(char*dest,char*fmt,...)
int sprint(char*dest,char*fmt,...)
{
bool special=false;
typedef union type {int64_t i64;int32_t i32;char c;char*s;}type;
@ -141,6 +67,8 @@ char*sprint(char*dest,char*fmt,...)
{
case 'd':
case 'i':
case 'u':
{
char tmp[1024];
int n;
@ -185,5 +113,82 @@ char*sprint(char*dest,char*fmt,...)
}
dest[bpos]=0;
return dest;
return bpos;
}
static int sprinti16(char*s,int d)
{
static char g[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char b[32]={0};
int bp=0;
int i=0;
if(!s)return 0;
if(d<0)
{
b[bp++]='-';
d=-d;
}
// Get each digit as character
while(d>0)
{
int m=d%16;
d/=16;
b[i++]=g[m];
}
b[i]=0;
int max=i;
// Print reversed string
for(;i>=0;--i)
{
if(b[i]>31)
s[max-i]=b[i];
else
s[max-i]=1;
}
s[max+1]=0;
return max;
}
static int sprinti(char*s,int d)
{
char b[32]={0};
int bp=0;
int i=0;
if(!s)return 0;
if(d<0)
{
b[bp++]='-';
d=-d;
}
// Get each digit as character
while(d>0)
{
int m=d%10;
d/=10;
b[i++]=m+'0';
}
b[i]=0;
int max=i;
// Print reversed string
for(;i>=0;--i)
{
if(b[i]>31)
s[max-i]=b[i];
else
s[max-i]=1;
}
s[max+1]=0;
return max;
}