diff options
author | Klaus Schmidinger <vdr@tvdr.de> | 2007-07-20 14:25:46 +0200 |
---|---|---|
committer | Klaus Schmidinger <vdr@tvdr.de> | 2007-07-20 14:25:46 +0200 |
commit | bd36fb5111d39f86f372829714bfdb06238f8809 (patch) | |
tree | 7afb78ed0108ad4a41dc1c34f972fc124090ecff /tools.c | |
parent | 57c2b211e4c600a35dab470b9319d9c025736437 (diff) | |
download | vdr-bd36fb5111d39f86f372829714bfdb06238f8809.tar.gz vdr-bd36fb5111d39f86f372829714bfdb06238f8809.tar.bz2 |
Improved numdigits(), isnumber() and strreplace()
Diffstat (limited to 'tools.c')
-rw-r--r-- | tools.c | 27 |
1 files changed, 15 insertions, 12 deletions
@@ -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; } |