improve my substring function?

J

Jeff

Namely I'd like to silence this compiler warning:

util.c: In function 'substring':
util.c:50: warning: value computed is not used
util.c:51: warning: value computed is not used

But I'm open to all suggestions....

char *
substring(char *str, int start, int len)
{
int i;
char *ret;
char *res;
char *ptr;
char *end;

if ((len < 1) || (start < 0) || (start > (int)strlen (str)) || start
+len > (int)strlen(str))
return NULL;

ret = xmalloc(len+1);
res = ret;
ptr = str;
end = str;

for(i = 0; i < start; i++, *ptr++) ; /* util.c:50 */
for(i = 0; i < start+len; i++, *end++) ; /* util.c:51 */
while(ptr < end)
*res++ = *ptr++;

*res = 0;
return ret;
}
 
B

Ben Pfaff

Jeff said:
util.c: In function 'substring':
util.c:50: warning: value computed is not used
util.c:51: warning: value computed is not used
....

for(i = 0; i < start; i++, *ptr++) ; /* util.c:50 */
for(i = 0; i < start+len; i++, *end++) ; /* util.c:51 */

for(i = 0; i < start; i++, ptr++) ;
for(i = 0; i < start+len; i++, end++) ;

or
i = start;
ptr += start;

i = start+len;
end += len;
 
M

mfhaigh

But I'm open to all suggestions....


You've got to think it through a little bit better. What is the
correct behavior when the input string is empty? Should that be
allowed? How does one extract the remainder of a string without
having to compute the size?

What can cause NULL to be returned? What about if you're dealing
with very long strings that you already know the size of? You most
likely wouldn't want excess strlen() calls in that case.

Here's some basic tips: First, use size_t to refer to the sizes of
things. When you pass an input string into a function that doesn't
modify it, use 'const char *'; it's much more polite.

The following is some code to point you in the right direction. It's
not exhaustively tested, so don't just cut and paste it into something
important, but I believe it's correct.

Mark F. Haigh
(e-mail address removed)



/*
* Extract a substring and return a malloc-ed copy of it. The source
* string and its length are 'str' and 'len', respectively. The zero-
* based starting index of the substring and the number of characters
* to copy are 'index' and 'count', respectively.
*
* If 'count' is larger than the available number of characters, then
* the remainder of 'str' will be copied. Use SIZE_MAX as 'count' to
* unconditionally cause this behavior.
*
* If the input string is empty, if the number of characters to copy
* is zero, or if a memory allocation error occurred, then the return
* value is NULL. Otherwise, the return value is a newly malloc-ed
* copy of the extracted substring.
*/

char * substr_len(const char *str, size_t len,
size_t index, size_t count)
{
size_t max_count;
char *ret = NULL;

if(len && count && index < len) {
max_count = len - index;
if(count > max_count)
count = max_count;
ret = malloc(count + 1);
if(ret) {
memcpy(ret, str + index, count);
ret[count] = '\0';
}
}

return ret;
}


/*
* Wrapper for substr_len() when the size of the input string is not
* known. See substr_len() for more information.
*/

char * substr(const char *str, size_t index, size_t count)
{
if(str)
return substr_len(str, strlen(str), index, count);
return NULL;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top