From bd36fb5111d39f86f372829714bfdb06238f8809 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Fri, 20 Jul 2007 14:25:46 +0200 Subject: Improved numdigits(), isnumber() and strreplace() --- tools.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'tools.c') diff --git a/tools.c b/tools.c index 46b691b9..dc91d11a 100644 --- a/tools.c +++ b/tools.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.c 1.132 2007/07/20 13:17:40 kls Exp $ + * $Id: tools.c 1.133 2007/07/20 14:25:46 kls Exp $ */ #include "tools.h" @@ -158,10 +158,11 @@ char *strreplace(char *s, const char *s1, const char *s2) int l1 = strlen(s1); int l2 = strlen(s2); if (l2 > l1) - s = (char *)realloc(s, strlen(s) + l2 - l1 + 1); + s = (char *)realloc(s, l + l2 - l1 + 1); + char *sof = s + of; if (l2 != l1) - memmove(s + of + l2, s + of + l1, l - of - l1 + 1); - strncpy(s + of, s2, l2); + memmove(sof + l2, sof + l1, l - of - l1 + 1); + strncpy(sof, s2, l2); } return s; } @@ -252,20 +253,22 @@ bool isempty(const char *s) int numdigits(int n) { - char buf[16]; - snprintf(buf, sizeof(buf), "%d", n); - return strlen(buf); + int res = 1; + while (n >= 10) { + n /= 10; + res++; + } + return res; } bool isnumber(const char *s) { if (!*s) return false; - while (*s) { - if (!isdigit(*s)) - return false; - s++; - } + do { + if (!isdigit(*s)) + return false; + } while (*++s); return true; } -- cgit v1.2.3