Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #43582 > unrolled thread

A case insensitive substring search?

Started byJames Moe <jimoeDESPAM@sohnen-moe.com>
First post2016-06-28 13:16 -0700
Last post2016-07-01 07:53 +0100
Articles 12 — 6 participants

Back to article view | Back to comp.lang.c++


Contents

  A case insensitive substring search? James Moe <jimoeDESPAM@sohnen-moe.com> - 2016-06-28 13:16 -0700
    Re: A case insensitive substring search? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-28 23:48 +0100
      Re: A case insensitive substring search? James Moe <jimoeDESPAM@sohnen-moe.com> - 2016-06-28 22:25 -0700
        Re: A case insensitive substring search? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-06-29 10:56 +0100
          Re: A case insensitive substring search? Öö Tiib <ootiib@hot.ee> - 2016-06-29 04:19 -0700
        Re: A case insensitive substring search? James Moe <jimoeDESPAM@sohnen-moe.com> - 2016-06-29 10:48 -0700
    Re: A case insensitive substring search? Nobody <nobody@nowhere.invalid> - 2016-06-29 00:39 +0100
      Re: A case insensitive substring search? Öö Tiib <ootiib@hot.ee> - 2016-06-29 06:22 -0700
        Re: A case insensitive substring search? Ralf Goertz <me@myprovider.invalid> - 2016-06-29 17:16 +0200
          Re: A case insensitive substring search? Öö Tiib <ootiib@hot.ee> - 2016-07-01 01:46 -0700
      Re: A case insensitive substring search? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-06-29 21:32 +0200
      Re: A case insensitive substring search? Nobody <nobody@nowhere.invalid> - 2016-07-01 07:53 +0100

#43582 — A case insensitive substring search?

FromJames Moe <jimoeDESPAM@sohnen-moe.com>
Date2016-06-28 13:16 -0700
SubjectA case insensitive substring search?
Message-ID<C6mdnaL0a8umRu_KnZ2dnUU7-cHNnZ2d@giganews.com>
Hello,
  A case insensitive substring search: How hard can it be?.
  I searched for it. There are lots of case insensitive string searches,
but very few case insensitive substring searches. Nothing that I could
understand.
  I came up with the following. It seemed that search_n() was perfect
for a substring search; I just had to supply an appropriate comparison
function. which is where I stalled.

bool icompare (unsigned char a, unsigned char b)
{
    return (toupper(a) == toupper(b));
}

bool strstricmp (string & s1, string & sub2)
{
    string::iterator pos;

    pos = search_n(s1.begin, s1.end(),
		   1,
		   sub2,
		   icompare());
    return (pos != s1.end());
}

  The main problem is what to supply icompare() as parameters? Probably
the wrong choice.

  This was a cakewalk in C. Creating a C++-worthy function is not so easy.

-- 
James Moe
jmm-list at sohnen-moe dot com
Think.

[toc] | [next] | [standalone]


#43583

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-06-28 23:48 +0100
Message-ID<877fd9gcav.fsf@bsb.me.uk>
In reply to#43582
James Moe <jimoeDESPAM@sohnen-moe.com> writes:

>   A case insensitive substring search: How hard can it be?.
>   I searched for it. There are lots of case insensitive string searches,
> but very few case insensitive substring searches. Nothing that I could
> understand.
>   I came up with the following. It seemed that search_n() was perfect
> for a substring search; I just had to supply an appropriate comparison
> function. which is where I stalled.

You need search, not search_n.  And if you are using modern C++ you can
pass a lambda for the comparison function so there is no need for an
extra named function (though that's not a big deal).

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#43588

FromJames Moe <jimoeDESPAM@sohnen-moe.com>
Date2016-06-28 22:25 -0700
Message-ID<8tadndSUROtJxu7KnZ2dnUU7-X3NnZ2d@giganews.com>
In reply to#43583
On 06/28/2016 03:48 PM, Ben Bacarisse wrote:
> You need search, not search_n.
>
  Tried that. No luck.
bool strstricmp (string & s1, string & sub2)
{
    string::iterator pos;

    pos = search(s1.begin, s1.end(),
		   sub2.begin(), sub2.end(),
		   icompare);
    return (pos != s1.end());
}
  Complains that there is no match for the predicate.

> And if you are using modern C++ you can [...]
> 
  Alas, no. g++ 4.7.3.

-- 
James Moe
jmm-list at sohnen-moe dot com
Think.

[toc] | [prev] | [next] | [standalone]


#43589

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-06-29 10:56 +0100
Message-ID<871t3ggvy2.fsf@bsb.me.uk>
In reply to#43588
James Moe <jimoeDESPAM@sohnen-moe.com> writes:

> On 06/28/2016 03:48 PM, Ben Bacarisse wrote:
>> You need search, not search_n.
>>
>   Tried that. No luck.
> bool strstricmp (string & s1, string & sub2)
> {
>     string::iterator pos;
>
>     pos = search(s1.begin, s1.end(),
> 		   sub2.begin(), sub2.end(),
> 		   icompare);
>     return (pos != s1.end());
> }
>   Complains that there is no match for the predicate.

Seems odd.  I'd expect more problems to come from the missing () after
s1.begin.  When I fix that, it works here.

You should consider making s1 and sub2 references to const.

>> And if you are using modern C++ you can [...]
>> 
>   Alas, no. g++ 4.7.3.

The [...] went on to suggest using an anonymous function.  I thought
these were introduced in gcc 4.5.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#43590

FromÖö Tiib <ootiib@hot.ee>
Date2016-06-29 04:19 -0700
Message-ID<7334b0ad-5f58-40d4-a9e8-d150b949a0c0@googlegroups.com>
In reply to#43589
On Wednesday, 29 June 2016 12:56:32 UTC+3, Ben Bacarisse  wrote:
> James Moe <jimoeDESPAM@sohnen-moe.com> writes:
> 
> > On 06/28/2016 03:48 PM, Ben Bacarisse wrote:
> >> You need search, not search_n.
> >>
> >   Tried that. No luck.
> > bool strstricmp (string & s1, string & sub2)
> > {
> >     string::iterator pos;
> >
> >     pos = search(s1.begin, s1.end(),
> > 		   sub2.begin(), sub2.end(),
> > 		   icompare);
> >     return (pos != s1.end());
> > }
> >   Complains that there is no match for the predicate.
> 
> Seems odd.  I'd expect more problems to come from the missing () after
> s1.begin.  When I fix that, it works here.

May be that James has some user-defined things named 'search'
and/or 'string'. With 'std::string' and 'std::search' his code will be
rejected because of missing '()' after 's1.begin'.

> 
> You should consider making s1 and sub2 references to const.
> 
> >> And if you are using modern C++ you can [...]
> >> 
> >   Alas, no. g++ 4.7.3.
> 
> The [...] went on to suggest using an anonymous function.  I thought
> these were introduced in gcc 4.5.

Yes, lambdas were available since gcc 4.5. 

[toc] | [prev] | [next] | [standalone]


#43594

FromJames Moe <jimoeDESPAM@sohnen-moe.com>
Date2016-06-29 10:48 -0700
Message-ID<atqdnT-rzblClOnKnZ2dnUU7-f-dnZ2d@giganews.com>
In reply to#43588
On 06/28/2016 10:25 PM, James Moe wrote:
>     pos = search(s1.begin, s1.end(),
>
  Bzzt! User error.
  That should be "s1.begin()", not "s1.begin".

-- 
James Moe
jmm-list at sohnen-moe dot com
Think.

[toc] | [prev] | [next] | [standalone]


#43584

FromNobody <nobody@nowhere.invalid>
Date2016-06-29 00:39 +0100
Message-ID<pan.2016.06.28.23.41.22.74000@nowhere.invalid>
In reply to#43582
On Tue, 28 Jun 2016 13:16:55 -0700, James Moe wrote:

>   A case insensitive substring search: How hard can it be?.

Substring search is provided by the std::basic_string::find() method.
Comparisons are performed using the eq() member of the traits class,
so if you use a traits class where eq() is case-insensitive, you get a
case-insensitive substring comparison.

But that requires using an explicit specialisation of std::basic_string
rather than just using std::string or std::wstring (which use
std::char_traits implicitly). A cast would almost certainly work, but
(AFAIK) it's not guaranteed.

Whatever method you choose, there's the issue that it isn't always
possible to perform a case-insensitive comparison character-by-character.

The classic example of where this fails is that the German "sharp s"
character ("ß") doesn't have an equivalent upper-case character; the
upper-case equivalent is "SS". There's no way to handle this situation
using only the standard library, where case conversion is assumed to map
one character to one character.

[toc] | [prev] | [next] | [standalone]


#43591

FromÖö Tiib <ootiib@hot.ee>
Date2016-06-29 06:22 -0700
Message-ID<ab5ca1d0-9343-4558-80ed-236e9c34f86d@googlegroups.com>
In reply to#43584
On Wednesday, 29 June 2016 02:38:24 UTC+3, Nobody  wrote:
> On Tue, 28 Jun 2016 13:16:55 -0700, James Moe wrote:
> 
> >   A case insensitive substring search: How hard can it be?.
> 
> Substring search is provided by the std::basic_string::find() method.
> Comparisons are performed using the eq() member of the traits class,
> so if you use a traits class where eq() is case-insensitive, you get a
> case-insensitive substring comparison.
> 
> But that requires using an explicit specialisation of std::basic_string
> rather than just using std::string or std::wstring (which use
> std::char_traits implicitly). A cast would almost certainly work, but
> (AFAIK) it's not guaranteed.
> 
> Whatever method you choose, there's the issue that it isn't always
> possible to perform a case-insensitive comparison character-by-character.
> 
> The classic example of where this fails is that the German "sharp s"
> character ("ß") doesn't have an equivalent upper-case character; the
> upper-case equivalent is "SS". 

So some capitalized word in text (say "RUSSEN") may be one German
word ("rußen") or other German word ("Russen") or same-looking
word or name from some other language? 

> There's no way to handle this situation using only the standard
> library, where case conversion is assumed to map one character
> to one character.

If the issue is like I described above then it can't be handled with
whatever software and even for real human operator it is possible
to construct ambiguous input that the operator can't decide. If
treating "ß" and "ss" as equivalents is good enough hack around
it then however it is doable.

[toc] | [prev] | [next] | [standalone]


#43592

FromRalf Goertz <me@myprovider.invalid>
Date2016-06-29 17:16 +0200
Message-ID<20160629171607.2f78f7c8@delli.home.local>
In reply to#43591
Am Wed, 29 Jun 2016 06:22:48 -0700 (PDT)
schrieb Öö Tiib <ootiib@hot.ee>:

> So some capitalized word in text (say "RUSSEN") may be one German word
> ("rußen") or other German word ("Russen") or same-looking word or name
> from some other language? 

If there is going to be an ambiguity like in your example a capitalized
ß becomes SZ which is what it really is. AFAIK the origin of the ß is a
ligature of ſ (long s) and z. Another more common example of this
ambiguity would be „Maße“ (measures). It will become „MASZE“ not „MASSE“
(mass).

[toc] | [prev] | [next] | [standalone]


#43605

FromÖö Tiib <ootiib@hot.ee>
Date2016-07-01 01:46 -0700
Message-ID<91f867ee-b246-478d-9742-7e946172eadd@googlegroups.com>
In reply to#43592
On Wednesday, 29 June 2016 18:16:20 UTC+3, Ralf Goertz  wrote:
> Am Wed, 29 Jun 2016 06:22:48 -0700 (PDT)
> schrieb Öö Tiib <ootiib@hot.ee>:
> 
> > So some capitalized word in text (say "RUSSEN") may be one German word
> > ("rußen") or other German word ("Russen") or same-looking word or name
> > from some other language? 
> 
> If there is going to be an ambiguity like in your example a capitalized
> ß becomes SZ which is what it really is. AFAIK the origin of the ß is a
> ligature of ſ (long s) and z. Another more common example of this
> ambiguity would be „Maße“ (measures). It will become „MASZE“ not „MASSE“
> (mass).

Thanks for explaining. Seemingly it still remains possible to prepare
a "test case" about what even human operator can not be 100%
sure if it is German „Maße“ or name "Masze" or word "masze"
from some other language in all caps.

[toc] | [prev] | [next] | [standalone]


#43595

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2016-06-29 21:32 +0200
Message-ID<nl17ra$cih$1@dont-email.me>
In reply to#43584
On 29.06.2016 19:24, Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> Nobody <nobody@nowhere.invalid> writes:
>>>                               There's no way to handle this situation
>>> using only the standard library, where case conversion is assumed to map
>>> one character to one character.
>> It was always possible to use a custom character encoding, where
>> »SS« is represented by a single code point (character); it still
>> can be rendered »SS«, which is a question of I/O.
>
>   And there even is a precedent for it: »"\n"« (one character),
>   which sometimes is output as »"\015\012"« (two characters).

But that would break the Unix-land convention for narrow streams, that 
(in Unix-land) they only shuffle bytes, with no interpretation.

Also keep in mind that in Unix-land the narrow streams are now usually 
UTF-8 encoded, with a variable number of bytes per Unicode code point.

But I like the idea of the uppercase sharp S. I didn't know about it 
until your posting. Thanks!


Cheers!,

- Alf

[toc] | [prev] | [next] | [standalone]


#43604

FromNobody <nobody@nowhere.invalid>
Date2016-07-01 07:53 +0100
Message-ID<pan.2016.07.01.06.53.19.656000@nowhere.invalid>
In reply to#43584
On Wed, 29 Jun 2016 04:33:05 +0000, Stefan Ram wrote:

> Nobody <nobody@nowhere.invalid> writes:
>>                               There's no way to handle this situation
>>using only the standard library, where case conversion is assumed to map
>>one character to one character.
> 
>   No way?
> 
>   It was always possible to use a custom character encoding

I should have been more clear: there's no way to handle this situation
using only the standard library's notion of case (either the std::ctype
facet or the features inherited from C in <cctype>) for an arbitrary
locale.

I daresay that you can create custom locales or just avoid using anything
related to locales and create your own alternatives. But that's likely to
create as many problems as it solves.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web