Skip to content

Commit e0b8196

Browse files
Add the rt_atoi function.
1 parent 6695599 commit e0b8196

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

include/klibc/kstring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ char *rt_strncpy(char *dest, const char *src, size_t n);
3131
int rt_strncmp(const char *cs, const char *ct, size_t count);
3232
int rt_strcmp(const char *cs, const char *ct);
3333
size_t rt_strlen(const char *src);
34-
34+
int rt_atoi(const char* s);
3535
#ifdef __cplusplus
3636
}
3737
#endif

src/klibc/kstring.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,23 @@ size_t rt_strnlen(const char *s, size_t maxlen)
533533
#endif /* RT_KLIBC_USING_USER_STRNLEN */
534534
RTM_EXPORT(rt_strnlen);
535535

536+
int rt_atoi(const char* s)
537+
{
538+
int n = 0, sign = 1;
539+
540+
if (*s == '-')
541+
{
542+
sign = -1;
543+
s++;
544+
}
545+
else if (*s == '+')
546+
s++;
547+
548+
while (*s >= '0' && *s <= '9') n = n * 10 + (*s++ - '0');
549+
550+
return sign * n;
551+
}
552+
536553
#ifdef RT_USING_HEAP
537554
/**
538555
* @brief This function will duplicate a string.

0 commit comments

Comments
 (0)