Skip to content

Commit

Permalink
Optimize negative sign handling in __str_base10()
Browse files Browse the repository at this point in the history
Modify the handling of negative numbers in the __str_base10().
Previously, adding a negative sign required searching for the first '0'
in the result and replacing it with a '-'. The updated approach allows
for directly appending the negative sign to pb[i] if needed,
simplifying the implementation.
  • Loading branch information
visitorckw committed Aug 9, 2024
1 parent bc25eff commit f111a2d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 9 deletions.
9 changes: 2 additions & 7 deletions lib/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,8 @@ void __str_base10(char *pb, int val)
i--;
}

if (neg == 1) {
int c = 0;
while (pb[c] == '0')
c++;
if (c > 0)
pb[c - 1] = '-';
}
if (neg == 1)
pb[i] = '-';
}

void __str_base8(char *pb, int val)
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/fib.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/snapshots/hello.json

Large diffs are not rendered by default.

0 comments on commit f111a2d

Please sign in to comment.