Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC-30988 Clean up implementation of stristr() #18124

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions system/jlib/jstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2869,36 +2869,24 @@ const char * stristr (const char *haystack, const char *needle)
if (isEmptyString(haystack) || isEmptyString(needle))
return nullptr;

const char *pptr = needle; /* Pattern to search for */
const char *start = haystack; /* Start with a bowl of hay */
const char *sptr; /* Substring pointer */
int slen = strlen(haystack); /* Total size of haystack */
int plen = strlen(needle); /* Length of our needle */

/* while string length not shorter than pattern length */
for (; slen >= plen; start++, slen--)
const char * start = haystack;
size_t slen = strlen(haystack); /* Total size of haystack */
size_t nlen = strlen(needle); /* Length of our needle */
char needle0 = tolower(*needle);
for (; slen >= nlen; start++, slen--)
{
/* find start of pattern in string */
while (tolower(*start) != tolower(*needle))
{
start++;
slen--;
/* if pattern longer than string */
if (slen < plen)
return nullptr;
}

sptr = start;
pptr = (char *) needle;
while (tolower(*sptr) == tolower(*pptr))
if (tolower(*start) == needle0)
{
sptr++;
pptr++;
/* if end of pattern then pattern was found */
if (!*pptr)
return start;
size_t i = 1;
for (;;)
{
if (i == nlen)
return start;
if (tolower(start[i]) != tolower(needle[i]))
break;
i++;
}
}
}

return nullptr;
}
29 changes: 28 additions & 1 deletion testing/unittests/jlibtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

#include "unittests.hpp"

#define CPPUNIT_ASSERT_EQUAL_STR(x, y) CPPUNIT_ASSERT_EQUAL(std::string(x),std::string(y))
#define CPPUNIT_ASSERT_EQUAL_STR(x, y) CPPUNIT_ASSERT_EQUAL(std::string(x ? x : ""),std::string(y ? y : ""))

static const unsigned oneMinute = 60000; // msec

Expand Down Expand Up @@ -3830,4 +3830,31 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JLibSecretsTest, "JLibSecretsTest" );



class JLibStringTest : public CppUnit::TestFixture
{
public:
CPPUNIT_TEST_SUITE(JLibStringTest);
CPPUNIT_TEST(testStristr);
CPPUNIT_TEST_SUITE_END();

void testStristr()
{
CPPUNIT_ASSERT_EQUAL_STR(stristr("abc", "abc"), "abc");
CPPUNIT_ASSERT_EQUAL_STR(stristr("abc", "ABC"), "abc");
CPPUNIT_ASSERT_EQUAL_STR(stristr("x", "ABC"), "");
CPPUNIT_ASSERT_EQUAL_STR(stristr("abcdefgh", "A"), "abcdefgh");
CPPUNIT_ASSERT_EQUAL_STR(stristr("ABCDEFGH", "a"), "ABCDEFGH");
CPPUNIT_ASSERT_EQUAL_STR(stristr("abcdefgh", "E"), "efgh");
CPPUNIT_ASSERT_EQUAL_STR(stristr("ABCDEFGH", "e"), "EFGH");
CPPUNIT_ASSERT_EQUAL_STR(stristr("abcdefgh", "FGH"), "fgh");
CPPUNIT_ASSERT_EQUAL_STR(stristr("aabcdefgh", "ABC"), "abcdefgh");
CPPUNIT_ASSERT_EQUAL_STR(stristr("ababacz", "ABAC"), "abacz");
CPPUNIT_ASSERT_EQUAL_STR(stristr("", "ABC"), "");
CPPUNIT_ASSERT_EQUAL_STR(stristr("ABC", ""), "");
}
};

CPPUNIT_TEST_SUITE_REGISTRATION( JLibStringTest );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JLibStringTest, "JLibStringTest" );

#endif // _USE_CPPUNIT
Loading