From ffa450acddb7aec6a440ae3fe6032c109805d176 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 15 Sep 1998 01:38:10 +0000 Subject: Removed hideous inefficiencies in old trim_string code. This was making calls to strlen() a profiling hotspot. Jeremy. --- source/lib/util.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/source/lib/util.c b/source/lib/util.c index 668857f004e..002b31d027d 100644 --- a/source/lib/util.c +++ b/source/lib/util.c @@ -1089,23 +1089,30 @@ trim the specified elements off the front and back of a string BOOL trim_string(char *s,char *front,char *back) { BOOL ret = False; - while (front && *front && strncmp(s,front,strlen(front)) == 0) - { - char *p = s; - ret = True; - while (1) - { - if (!(*p = p[strlen(front)])) - break; - p++; - } - } - while (back && *back && strlen(s) >= strlen(back) && - (strncmp(s+strlen(s)-strlen(back),back,strlen(back))==0)) + int front_len = (front && *front) ? strlen(front) : 0; + int back_len = (back && *back) ? strlen(back) : 0; + int s_len; + + while (front_len && strncmp(s, front, front_len) == 0) + { + char *p = s; + ret = True; + while (1) { - ret = True; - s[strlen(s)-strlen(back)] = 0; + if (!(*p = p[front_len])) + break; + p++; } + } + + s_len = strlen(s); + while (back_len && s_len >= back_len && + (strncmp(s + s_len - back_len, back, back_len)==0)) + { + ret = True; + s[s_len - back_len] = 0; + s_len = strlen(s); + } return(ret); } -- cgit