Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions lib/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,68 @@ void free(void *ptr)
__freelist_head->prev = cur;
__freelist_head = cur;
}

void snprintf(char *buffer, size_t n, char *str, ...)
jserv marked this conversation as resolved.
Show resolved Hide resolved
{
int *var_args = &str + 4;
int si = 0, bi = 0, pi = 0;

while (str[si]) {
Copy link
Collaborator

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() ?

if (bi >= n-1) {
jserv marked this conversation as resolved.
Show resolved Hide resolved
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';
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
https://medium.com/@alexey.inkin/how-to-force-newline-at-end-of-files-and-why-you-should-do-it-fdf76d1d090e

19 changes: 19 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 n.


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide more tests such as string truncation?

echo OK
Loading