-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add snprintf implementation #170
base: master
Are you sure you want to change the base?
Changes from all commits
af03db2
be3a312
4db8d7e
0db0e9c
73ce0cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -724,3 +724,69 @@ void free(void *ptr) | |
__freelist_head->prev = cur; | ||
__freelist_head = cur; | ||
} | ||
|
||
void snprintf(char *buffer, int n, char *str, ...) | ||
{ | ||
int *var_args = &str + 4; | ||
int si = 0, bi = 0, pi = 0; | ||
|
||
while (str[si]) { | ||
if (bi >= n - 1) { | ||
break; | ||
} else if (str[si] != '%') { | ||
buffer[bi] = str[si]; | ||
bi++; | ||
si++; | ||
} else { | ||
int w = 0, zp = 0, pp = 0; | ||
|
||
si++; | ||
if (str[si] == '#') { | ||
pp = 1; | ||
si++; | ||
} | ||
if (str[si] == '0') { | ||
zp = 1; | ||
si++; | ||
} | ||
if (str[si] >= '1' && str[si] <= '9') { | ||
w = str[si] - '0'; | ||
si++; | ||
if (str[si] >= '0' && str[si] <= '9') { | ||
w = w * 10; | ||
w += str[si] - '0'; | ||
si++; | ||
} | ||
} | ||
switch (str[si]) { | ||
case 37: /* % */ | ||
buffer[bi++] = '%'; | ||
si++; | ||
continue; | ||
case 99: /* c */ | ||
buffer[bi++] = var_args[pi]; | ||
break; | ||
case 115: /* s */ | ||
strcpy(buffer + bi, var_args[pi]); | ||
bi += strlen(var_args[pi]); | ||
break; | ||
case 111: /* o */ | ||
bi += __format(buffer + bi, var_args[pi], w, zp, 8, pp); | ||
break; | ||
case 100: /* d */ | ||
bi += __format(buffer + bi, var_args[pi], w, zp, 10, 0); | ||
break; | ||
case 120: /* x */ | ||
bi += __format(buffer + bi, var_args[pi], w, zp, 16, pp); | ||
break; | ||
default: | ||
abort(); | ||
break; | ||
} | ||
pi++; | ||
si++; | ||
} | ||
} | ||
if (bi < n) | ||
buffer[bi] = '\0'; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using git for version control, it is recommended to add a newline at the end of files. This is because git compares files line by line, and the end of each line is identified by a newline character. For more details, refer to the following link: |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1085,4 +1085,23 @@ int main() | |
return 0; | ||
} | ||
EOF | ||
|
||
try_output "Hello World 1123" << EOF | ||
int main() { | ||
char buffer[50]; | ||
snprintf(buffer, sizeof(buffer), "Hello %s %d", "World", 1123); | ||
printf("%s", buffer); | ||
return 0; | ||
} | ||
EOF | ||
|
||
try_output "Number: -37" << EOF | ||
int main() { | ||
char buffer[20]; | ||
snprintf(buffer, sizeof(buffer), "Number: %d", -37); | ||
printf("%s", buffer); | ||
return 0; | ||
} | ||
EOF | ||
Comment on lines
+1089
to
+1105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't to see any edge case here, please add an edge case that covers when given formatted string's length exceeds |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you provide more tests such as string truncation? |
||
echo OK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we reuse the code in
sprintf()
?