Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > improve my substring function?

Reply
Thread Tools

improve my substring function?

 
 
Jeff
Guest
Posts: n/a
 
      05-08-2009
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;
}
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      05-08-2009
Jeff <(E-Mail Removed)> writes:

> 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;
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
 
 
 
mfhaigh@sbcglobal.net
Guest
Posts: n/a
 
      05-09-2009
On May 8, 12:03*pm, Jeff <(E-Mail Removed)> wrote:
<snip>
>
> 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
http://www.velocityreviews.com/forums/(E-Mail 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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding a SubString within a String Badass Scotsman ASP .Net 2 03-31-2006 04:00 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
find if there is a given substring inside a string juli ASP .Net 3 12-06-2004 11:52 AM
web sddress substring =?Utf-8?B?Sm9u?= ASP .Net 2 11-30-2004 01:31 PM
"Substring" for images? How do I determine if an image is contained within another? Sean Java 0 05-04-2004 05:50 PM



Advertisments