Skip to main content
added 430 characters in body
Source Link
Fallen
  • 207
  • 2
  • 6

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && epostfix > postfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

Update This is the newer version of mine:

bool strends(const char *str, const char *postfix)
{
        register const char *end = str + strlen(str) - strlen(postfix);
        if (strlen(str) < strlen(postfix))
                return false;

        while (*end)
                if (*end++ != *postfix++)
                        return false;
        return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && epostfix > postfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && epostfix > postfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

Update This is the newer version of mine:

bool strends(const char *str, const char *postfix)
{
        register const char *end = str + strlen(str) - strlen(postfix);
        if (strlen(str) < strlen(postfix))
                return false;

        while (*end)
                if (*end++ != *postfix++)
                        return false;
        return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

edited body
Source Link
Fallen
  • 207
  • 2
  • 6

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && postfixepostfix > epostfixpostfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && postfix > epostfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && epostfix > postfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

Tweeted twitter.com/#!/StackCodeReview/status/296706362832982016
added 4 characters in body
Source Link
Fallen
  • 207
  • 2
  • 6

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && endpostfix <> epostfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && end < epostfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

I wrote my own version of endswith just like in high-level programming languages in C which I would like reviews on.

There are 2 versions. One is my own and the other is from ccan. For some reason, I feel that ccan's version is better than mine until proven wrong.

ccan/str's version:

static inline bool strends(const char *str, const char *postfix)
{
    if (strlen(str) < strlen(postfix))
        return false;

    return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
}

My version:

bool strends(const char *str, const char *postfix)
{
    register const char *end = str + strlen(str) - 1;
    register const char *epostfix = postfix + strlen(postfix) - 1;

    while (end > str && postfix > epostfix)
        if (*end-- != *epostfix--)
            return false;
    return true;
}

I wonder which one is better and why (in terms of readability, performance and so on). Please be specific and provide your research if possible.

Thanks in advance.

deleted 6 characters in body
Source Link
Fallen
  • 207
  • 2
  • 6
Loading
Added a reference, because I originally thought it was a misspelling
Source Link
Loading
Source Link
Fallen
  • 207
  • 2
  • 6
Loading