improve my substring function?

Discussion in 'C Programming' started by Jeff, May 8, 2009.

  1. Jeff

    Jeff Guest

    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;
    }
    Jeff, May 8, 2009
    #1
    1. Advertising

  2. Jeff

    Ben Pfaff Guest

    Jeff <> 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,0xa67f6aaa,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 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, May 8, 2009
    #2
    1. Advertising

  3. Jeff

    Guest

    On May 8, 12:03 pm, Jeff <> 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




    /*
    * 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;
    }
    , May 9, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pembed2003
    Replies:
    10
    Views:
    562
    Siemel Naran
    Jun 23, 2004
  2. gbattine
    Replies:
    8
    Views:
    428
    Philipp Leitner
    Jul 16, 2006
  3. Matthew Wilson

    Need advice on how to improve this function

    Matthew Wilson, Aug 20, 2006, in forum: Python
    Replies:
    3
    Views:
    287
    Gabriel Genellina
    Aug 22, 2006
  4. Bowlderster

    How to improve my function?

    Bowlderster, Oct 30, 2007, in forum: C Programming
    Replies:
    2
    Views:
    283
    Charlie Gordon
    Oct 30, 2007
  5. Replies:
    3
    Views:
    200
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page