Caseless String
Hello,
I must decide relatively quickly whether to add a caseless string class
to my library. I envision such a string would relax the signifcance of
case when used as an argument in a boolean operation:
String<char> s1 = "c++";
Caseless_String<char> s2 = "C++";
s1 == s1; // true - bool operator == (const String<char> &, const
String<char> &);
s1 == s2; // true - bool operator == (const String<char> &, const
Caseless_String<char> &);
s2 == s1; // true - bool operator == (const Caseless_String<char> &,
const String<char> &);
s2 == s2; // true - bool operator == (const Caseless_String<char> &,
const CaselessString<char> &);
I cannot use STL's string, and I do not particularly enjoy making my
own string. I have read some previous postings on the problem of case
in language, and needless to say, it's not a trivial issue. However, I
cannot wait any longer to choose a model, and I'd like to get your
opinion on whether what I intend to do is dumb.
What I intend to do:
template <class C> class Caseless_String { } ;
Then, I will derive from Caseless_String a regular string:
template <class C> class String : public Caseless_String<C> {} ;
I will use the global form of boolean operators (== >= , etc.) so that
I only have to define them once for Caseless_String. Doing this will
allow Caseless_String to appear as one argument on either side of the
operator with whatever type of string as the other argument. A normal
String will be implicitly casted to the base if necessary, and since a
regular String will be derived from Caseless_String, I can override
these operators in String to force case significance when both
arguments are of class String.
What concerns me is that I feel there is an error that I am not
forseeing, butI cannot afford to be ambivalent about a strategy.
However, there are many places in my code where I would like case
restriction on strings to be *automatically* relaxed. If I have a
complex data structure where Strings are contained deep within, but
case does not matter, I would like to be able to supply
Caseless_Strings as arguments, and have all operations work
intutitively without any extra effort.
Is this wrong? Bad? It doesn't feel quite right, but I don't see any
other options.
-Le Chaud Lapin-
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]