Skip to content

Commit 85f67c5

Browse files
committed
Optimize negative sign handling in __str_base10()
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.
1 parent 4979668 commit 85f67c5

File tree

3 files changed

+4
-9
lines changed

3 files changed

+4
-9
lines changed

lib/c.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,8 @@ void __str_base10(char *pb, int val)
158158
i--;
159159
}
160160

161-
if (neg == 1) {
162-
int c = 0;
163-
while (pb[c] == '0')
164-
c++;
165-
if (c > 0)
166-
pb[c - 1] = '-';
167-
}
161+
if (neg == 1)
162+
pb[i] = '-';
168163
}
169164

170165
void __str_base8(char *pb, int val)

tests/snapshots/fib.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/hello.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)