| View previous topic :: View next topic |
| Author |
Message |
Siemel Naran Guest
|
Posted: Thu Feb 26, 2004 9:08 am Post subject: replacement for stristr |
|
|
There is a function stricmp that compares two strings without case
sensitivity.
We have strstr that searches for a substring, taking case into account.
Is there a function stristr? If not, how to write one?
Thanks.
--
+++++++++++
Siemel Naran
|
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Thu Feb 26, 2004 10:26 am Post subject: Re: replacement for stristr |
|
|
"Siemel Naran" <SiemelNaran (AT) REMOVE (DOT) att.net> wrote
| Quote: | There is a function stricmp that compares two strings without case
sensitivity.
We have strstr that searches for a substring, taking case into
account.
Is there a function stristr? If not, how to write one?
Thanks.
|
stricmp is a non-standard (presumably Microsoft) extension (now designated
_stricmp to indicate that fact).
I think that you will need to convert the string to all uppercase or all
lowercase first, and then call strstr (there are Microsoft functions for
this --- called _strupr and _strlwr --- but beware that they will overwrite
the original string, so you may need to first make a copy).
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
Posted: Thu Feb 26, 2004 3:39 pm Post subject: Re: replacement for stristr |
|
|
John Carson wrote:
| Quote: | "Siemel Naran" <SiemelNaran (AT) REMOVE (DOT) att.net> wrote in message
news:%Hi%b.96313$hR.1910806 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net
There is a function stricmp that compares two strings without case
sensitivity.
We have strstr that searches for a substring, taking case into
account.
Is there a function stristr? If not, how to write one?
Thanks.
stricmp is a non-standard (presumably Microsoft) extension (now designated
_stricmp to indicate that fact).
I think that you will need to convert the string to all uppercase or all
lowercase first, and then call strstr (there are Microsoft functions for
this --- called _strupr and _strlwr --- but beware that they will overwrite
the original string, so you may need to first make a copy).
|
One could compare each character in a C-style character
using the std::toupper or std::tolower functions to make
sure you are comparing case insensitive:
int my_stricmp(const char * a
const char * b)
{
while (*a != ' ' && *b != ' ')
{
if (toupper(*a) != toupper(*b))
return (toupper(*a) < toupper(*b)) ? -1 : 1;
++a;
++b;
}
if (*a == *b)
return 0;
return (*a == ' ') ? -1 : 1;
}
One could use traits and locale to create an
uppercase only (or lowercase only) string type
using std::basic_string<>.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Thu Feb 26, 2004 4:56 pm Post subject: Re: replacement for stristr |
|
|
Siemel Naran wrote:
| Quote: |
There is a function stricmp that compares two strings without case
sensitivity.
We have strstr that searches for a substring, taking case into account.
Is there a function stristr? If not, how to write one?
|
Always a good bet is snippets.org:
http://c.snippets.org/snip_lister.php?fname=stristr.c
Brian Rodenborn
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Thu Feb 26, 2004 4:58 pm Post subject: Re: replacement for stristr |
|
|
Thomas Matthews wrote:
| Quote: |
John Carson wrote:
One could compare each character in a C-style character
using the std::toupper or std::tolower functions to make
sure you are comparing case insensitive:
int my_stricmp(const char * a
const char * b)
|
He wanted a case-insensitive strstr(), not strcmp().
Brian Rodenborn
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Fri Feb 27, 2004 7:38 am Post subject: Re: replacement for stristr |
|
|
"Thomas Matthews" <Thomas_MatthewsSpitsOnSpamBots (AT) sbcglobal (DOT) net> wrote in
message
Thanks. I can extend this idea to write my own stristr.
| Quote: | return (toupper(*a) < toupper(*b)) ? -1 : 1;
|
Out of curiosity, which is faster -- the one above or
return toupper(*a) - toupper(*b);
I imagine it varies platform by platform?
--
+++++++++++
Siemel Naran
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Fri Feb 27, 2004 7:53 am Post subject: Re: replacement for stristr |
|
|
"Default User" <first.last (AT) boeing (DOT) com.invalid> wrote in message
Thanks. This is a nice link. So I have to write my own? Searching in the
standard I find there is a std::search.
template <class charT>
truct eiq {
bool operator()(charT c1, charT c2) const {
return toupper(c1)==toupper(c2);
}
};
const char * stristr(const char * s1, const char * s2) {
const char * s1end = s1+strlen(s1);
const char * out = std::search(s1, s1end, s2, s2+strlen(s2),
eiq<char>());
if (out == s1end) return NULL;
return out;
}
I think the calls to std::strlen may make lose efficiency because we scan
each of the entire strings once to find the length.
I wrote the following which seems to work, but would like confirmation if it
is correct.
template <class charT>
const charT * enhanced::stristr(const charT * s1, const charT *const
s2start)
{
using std::toupper;
const charT * s2startplus1 = s2start + 1u;
const charT * s2 = s2startplus1;
const charT c2start = toupper(*s2start);
charT c2 = c2start;
for ( ; ; ++s1)
{
const charT c1 = toupper(*s1);
if (!c1) break;
if (c1 == c2)
{
c2 = toupper(*s2);
if (!c2) return s1-(s2-s2start-1u);
++s2;
}
else if (s2 != s2startplus1)
{
s2 = s2startplus1;
c2 = c2start;
}
}
return NULL;
}
--
+++++++++++
Siemel Naran
|
|
| Back to top |
|
 |
lilburne Guest
|
Posted: Fri Feb 27, 2004 9:22 am Post subject: Re: replacement for stristr |
|
|
Siemel Naran wrote:
| Quote: |
I wrote the following which seems to work, but would like confirmation if it
is correct.
|
Write a test. Things to test are what happens if either input are an
empty string "". The substring length is greater then the string to be
searched. That the substring "aaa" is in the middle of the string
"baaabbb". That a false start can resync "abc" "bababcd".
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Sat Feb 28, 2004 7:14 am Post subject: Re: replacement for stristr |
|
|
"lilburne" <lilburne (AT) godzilla (DOT) com> wrote
| Quote: | Siemel Naran wrote:
I wrote the following which seems to work, but would like confirmation
if it
is correct.
Write a test. Things to test are what happens if either input are an
empty string "". The substring length is greater then the string to be
searched. That the substring "aaa" is in the middle of the string
"baaabbb". That a false start can resync "abc" "bababcd".
|
Thanks. Anything else you can think of?
--
+++++++++++
Siemel Naran
|
|
| Back to top |
|
 |
Les Matheson Guest
|
Posted: Mon Mar 01, 2004 3:19 am Post subject: Re: replacement for stristr |
|
|
Siemel,
Here's a version I wrote many years ago and still use. It has a lot
of miles on it, so I'm reasonably confident it won't blow up:
LPSTR stristr(const char *pszText, const char *pszSub)
{
if (!pszText || !pszSub || !*pszText || !*pszSub)
return 0;
int nLen(int(strlen(pszSub)));
const char test(toupper(*pszSub));
while (*pszText)
{
if (toupper(*pszText)==test)
{
if (strnicmp(pszText,pszSub,nLen)==0)
return LPSTR(pszText);
}
pszText++;
}
return 0;
}
Good luck,
Les Matheson
Integral Concepts, Inc.
http://www.ivsds.com
"Siemel Naran" <SiemelNaran (AT) REMOVE (DOT) att.net> wrote
| Quote: | "lilburne" <lilburne (AT) godzilla (DOT) com> wrote
Siemel Naran wrote:
I wrote the following which seems to work, but would like confirmation
if it
is correct.
Write a test. Things to test are what happens if either input are an
empty string "". The substring length is greater then the string to be
searched. That the substring "aaa" is in the middle of the string
"baaabbb". That a false start can resync "abc" "bababcd".
Thanks. Anything else you can think of?
|
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Mon Mar 01, 2004 7:54 pm Post subject: Re: replacement for stristr |
|
|
Les Matheson wrote:
| Quote: |
Siemel,
Here's a version I wrote many years ago and still use. It has a lot
of miles on it, so I'm reasonably confident it won't blow up:
LPSTR stristr(const char *pszText, const char *pszSub)
{
if (!pszText || !pszSub || !*pszText || !*pszSub)
return 0;
int nLen(int(strlen(pszSub)));
const char test(toupper(*pszSub));
while (*pszText)
{
if (toupper(*pszText)==test)
{
if (strnicmp(pszText,pszSub,nLen)==0)
return LPSTR(pszText);
}
pszText++;
}
return 0;
}
|
I realize that this is cross-posted to a windows newsgroup, but in
general it's better to use standard constructs whenever possible to
improve portability. The problem at hand can be solved without use of
non-standard functions like strincmp() or proprietary types like LPSTR.
Also stristr is a reserved identifier.
Brian Rodenborn
|
|
| Back to top |
|
 |
Makhno Guest
|
Posted: Mon Mar 01, 2004 8:34 pm Post subject: Re: replacement for stristr |
|
|
| Quote: | Also stristr is a reserved identifier.
|
reserved where?
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Mon Mar 01, 2004 9:17 pm Post subject: Re: replacement for stristr |
|
|
"Makhno" <root (AT) 127 (DOT) 0.0.1> wrote
| Quote: | Also stristr is a reserved identifier.
reserved where?
The C standard reserves identifiers that begin with str and another lowercase letter |
in the global namespace.
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Mon Mar 01, 2004 11:03 pm Post subject: Re: replacement for stristr |
|
|
Makhno wrote:
| Quote: |
Also stristr is a reserved identifier.
reserved where?
|
In the ISO C and C++ standards. All identifiers starting with str and
followed by a lowercase letter are reserved for future library
directions.
From the C99 draft standard:
7.26 Future library directions
[#1] The following names are grouped under individual
headers for convenience. All external names described below
are reserved no matter what headers are included by the
program.
7.26.10 General utilities <stdlib.h>
[#1] Function names that begin with str and a lowercase
letter (possibly followed by any combination of digits,
letters, and underscore) may be added to the declarations in
the <stdlib.h> header.
Brian Rodenborn
|
|
| Back to top |
|
 |
Les Matheson Guest
|
Posted: Tue Mar 02, 2004 10:54 pm Post subject: Re: replacement for stristr |
|
|
| Quote: | I realize that this is cross-posted to a windows newsgroup, but in
general it's better to use standard constructs whenever possible to
improve portability. The problem at hand can be solved without use of
non-standard functions like strincmp() or proprietary types like LPSTR.
Also stristr is a reserved identifier.
|
Good point, and thanks for the follow-up.
Les Matheson
Integral Concepts, Inc.
http://www.ivsds.com
|
|
| Back to top |
|
 |
|