开发者

Is there an elegant, industry standard way of implementing substr in C?

开发者 https://www.devze.com 2022-12-31 09:18 出处:网络
Is there an elegant, cross-platform, industry standard way of implementing substr() in C? 开发者_Python百科or is it a case of every developer reinventing the wheel?

Is there an elegant, cross-platform, industry standard way of implementing substr() in C?

开发者_Python百科or is it a case of every developer reinventing the wheel?

EDIT: Added 'cross-platform'.


My comment to the original question notwithstanding, the problem is that such a function needs a buffer to store the result, and it is not clear whether this should be provided by the caller or instantiated by the function (leaving the question of how it is later destroyed), or even created in place by modifying the original string. In different situations you may want different behaviour, so rolling your own may be beneficial (and is trivial in any case).

// Caller supplied destination
char* substr( const char* source, size_t start, size_t end, char* dest )
{
    memmove( dest, &source[start], end - start ) ;
    dest[end - start] = 0 ;
    return dest ;
}

// Automatically instantiated destination (and a memory leak!)
char* substr( const char* source, size_t start, size_t end )
{
    char* dest = malloc( end - start + 1) ;
    memcpy( dest, &source[start], end - start ) ;
    dest[end - start] = 0 ;
    return dest ;
}

// Modify in-place (original string truncated)
char* substr( char* source, size_t start, size_t end )
{
    source[end+1] = 0 ;
    return &source[start] ;
}

Note in all the above, the validity of the arguments such as bounds checking and determining that start < end is left to the caller, and in this respect they reflect the philosophy of the standard string library. In most cases I'd prefer the first as having the greatest utility and being more in-keeping with the design of the standard library. memmove() is used over memcpy(), in order that the source and destination may be the same location, if you do not need that, it may be more efficient to use memcpy().


its like that:

#include <string.h>
char* substr(int offset, int len, const char* input)
{
    /* this one assumes that strlen(input) >= offset */
    return strndup(input+offset, len);
}

EDIT: added the handling of offset>strlen and removed the strndup usage

#include <string.h>
#include <stdlib.h>

char* substr(size_t offset, size_t len, const char* input)
{
    const size_t inputLength = strlen(input);
    if(offset <= inputLength)
    {
        size_t resultLength = inputLength-offset;
        char* result = NULL;
        if (len < resultLength)
        {
            resultLength = len;
        }
        result = malloc(resultLength+1);
        if(NULL != result)
        {
            strncpy(result, input+offset, resultLength);
            result[resultLength] = 0;
        }
        return result;
    }
    else
    {
        /* Offset is larger than the string length */
        return NULL;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消