rt-thread icon indicating copy to clipboard operation
rt-thread copied to clipboard

[Feature] 是否可以增加一个rt_atoi函数呢

Open illustriousness opened this issue 1 month ago • 6 comments

Describe problem solved by the proposed feature

atoi函数用的还是蛮多的 希望增加一个

Describe your preferred solution

No response

Describe possible alternatives

No response

illustriousness avatar Nov 19 '25 05:11 illustriousness

int rt_atoi(const char* s)
{
    int n = 0, sign = 1;

    if (*s == '-')
    {
        sign = -1;
        s++;
    }
    else if (*s == '+')
        s++;

    while (*s >= '0' && *s <= '9') n = n * 10 + (*s++ - '0');

    return sign * n;
}

illustriousness avatar Nov 19 '25 05:11 illustriousness

这种纯函数直接用标准库提供的就行,一般不会有问题的。

a1012112796 avatar Nov 24 '25 09:11 a1012112796

会引入额外的库 增加flash占用

这种纯函数直接用标准库提供的就行,一般不会有问题的。

illustriousness avatar Nov 24 '25 09:11 illustriousness

感觉有这类需求时,还是使用c库中的比较好,否则 rt_* 类的c库实现会越来越多。

BernardXiong avatar Nov 24 '25 21:11 BernardXiong

感觉有这类需求时,还是使用c库中的比较好,否则 rt_* 类的c库实现会越来越多。

最好有一个列表,哪些使用 c库实现即可, atoi 是其中之一

lygstate avatar Nov 26 '25 10:11 lygstate

主要来说是libc中非常有限的函数,内核中用到的函数。

kerrno.c

  • rt_strerror(rt_err_t error) – convert an error code to its string form.

kstdio.c

  • rt_snprintf(char *buf, size_t size, const char *fmt, ...) – bounded formatted print.
  • rt_sprintf(char *buf, const char *format, ...) – unbounded formatted print.
  • rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) – core formatter; uses the libc version or the implementations in rt_vsnprintf_std.c / rt_vsnprintf_tiny.c depending on config.
  • rt_vsprintf(char *buf, const char *format, va_list arg_ptr) – varargs helper that uses rt_vsnprintf.

kstring.c

  • rt_memset(void *s, int c, size_t count) – fill memory with a byte value.
  • rt_memcpy(void *dst, const void *src, size_t count) – copy memory.
  • rt_memmove(void *dest, const void *src, size_t n) – move memory safely over overlaps.
  • rt_memcmp(const void *cs, const void *ct, size_t count) – compare memory blocks.
  • rt_strstr(const char *s1, const char *s2) – find substring.
  • rt_strcasecmp(const char *a, const char *b) – case-insensitive string compare.
  • rt_strncpy(char *dst, const char *src, size_t n) – copy string with length bound.
  • rt_strcpy(char *dst, const char *src) – copy NUL-terminated string.
  • rt_strncmp(const char *cs, const char *ct, size_t count) – compare strings with length bound.
  • rt_strcmp(const char *cs, const char *ct) – compare NUL-terminated strings.
  • rt_strlen(const char *s) – string length.
  • rt_strnlen(const char *s, size_t maxlen) – bounded string length.
  • rt_strdup(const char *s) – duplicate a string (only when RT_USING_HEAP is enabled).

BernardXiong avatar Dec 06 '25 16:12 BernardXiong