in-built C++ function that makes case insensitive comparision of two strings - c++

Is there any in-built function in C++ library that is able to make case insensitive comparision of two strings ? I am aware of simple approaches like using toupper/tolower,writing function ourselves.I want to know if there is anything in string.h library or other which is able to meet above objective.Here,
strcasecmp of C don't support strings,so not much of help in C++.It only works with char *.
Any help would be very thankful.
P.S. Boost libraries won't be of much help.
Thanks.

If you are willing to call strcasecmp, then you can call it in C++ too:
int cmp_result = strcasecmp(s1.c_str(), s2.c_str());

There is none. C++ does not provide any meaningful textual support beyond simply storing it. You will have to look to ICU.

Related

Complex Digamma Function

Do you know a c++ library where the Digamma Function (http://en.wikipedia.org/wiki/Digamma_function) is implemented with complex arguments?
I did not find a library, but google found this for me:
GammaFunctions.c++
However I did not check the validity of this code.
Try one of these:
GSL - GNU Scientific Library contains gsl_sf_complex_psi_e.
http://www.ensta-paristech.fr/~lunevill/doc_melina++_v039/src/special_functions/GammaFunctions.c++.html
http://www.mathworks.com/matlabcentral/fileexchange/978-special-functions-math-library/content/psi.m - You'll need to convert this to C++.
http://www.netlib.no/netlib/slatec/fnlib/cpsi.f - You'll need to convert this to C++.

Replacing CString in mingw compiler for eclipse

I have some code which is relatively big, but there is one part of it which uses something called CString (http://msdn.microsoft.com/en-us/library/aa300688%28v=vs.60%29.aspx) . Right now I am using the mingw compiler on eclipse and it doesnt seem to recognize it which makes sense. But I need to use the other part of the code. I know I can replace the CString with a const char * but there are several function like append which the code uses which are not defined. I was wondering whats the best way to port this code? Also there are other stdafx.h dependencies also like _T(). Whats the best approach I should take now?
There is a CString replacement over on CodeProject. I just googled for "CString alternative". I haven't looked at it but it should be easier to use that instead of std::string.

Can I include/import a single function from a library in C++

I only need to use the iequals case-insensitive string comparison function from the BOOST library.
I am using #include <boost/algorithm/string.hpp> to import it.
Is there any way that I could only import the iequals function by itself?
The reason I even care (I am really just curious.) is because the compiled DLL is about 230kB if I don't #include it and about 1.1MB if I do. It doesn't really make much difference in this particular case how large the file is, but it seems like there is a lot of stuff that gets imported and never used. What if the library was a few GB and I only needed one of the functions? Then it would become an issue, I imagine.
I am admittedly naive when it comes to just about anything cpp-related, but I feel like it isn't very efficient to include some 750kB of code when probably 90% of it isn't used. It could be that the iequals function uses all of that 750kB, I have no idea.
Then again, if the iequals function includes many of the same libraries, the file would still be just as large.
Thoughts?
Thanks in advance for any advice.
EDIT:
Thanks for the responses. I am doing my best to understand them.
I am a chemical engineer who is rewriting a bunch of horribly slow and poorly optimized VBA macros into a C++ DLL. So far the results have been outstanding and everything functions correctly. I just don't see the need for the extra file size if I only need to do a single type of comparison between two strings.
An example of the comparison I need to do is as follows:
if (SomeBSTR == "SomeTextHere") {
// do stuff
}
or more exactly:
if (Gas == "Methane" or
Gas == "CH4" or
Gas == "C1") return 1;
if (Gas == "Ethane" or
Gas == "C2H6" or
Gas == "C2") return 2;
If this is the ONLY type of comparison that I have to do, can I do it in a more simple way than:
int wStrCmp(const BSTR Str1, const wstring Str2) {
wstring wStr1(Str1, SysStringLen(Str1));
return boost::iequals(Str1, Str2);
}
which is called via:
if (wStrCmp(Gas, L"Methane") or
wStrCmp(Gas, L"CH4") or
wStrCmp(Gas, L"C1")) return 1;
Those last 2 blocks are practically pasted from my code.
Thanks again, guys.
believe me you already just include boost::algorithm::iequals but it use boost::range and std::locale that possibly you don't use them in other places of your code, so this make your code a lot bigger, so I guess for your case there is no other way to do that unless you use some non-standard function like stricmp or strcasecmp.
If you want to compare wide strings on Windows(for example BSTR) you can use _wcsicmp from CRT or lstrcmpiW from Windows runtime(declared in Kernel32.lib that possibly you already linked with it).

let the user use a function in c++ [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Dynamic source code in C++
is it possible to let the user type in a function and then run that function without using a lot of if's or a huge switch?
It is not possible to execute arbitrary c++ code in your program, since you than need a c++ compiler inside your program. But you could try to embed Python to your program. Boost python makes this relatively easy. The user can than write a python function that is executed and can interact with the classes and functions of your program. You need to make your functions explicitely visible to python.
What ever a user types in will be text, or a string. The only way I know to have it get mapped to a function is to use if/else or switch statements. That or the cringe inducing option of mapping each of your functions to a UI widget.
The end of the story, is it's your code. You have to write, and live with it. Just be careful, your program may be wildly successful, and you may not write code anymore, and then someone else will have to maintain your code. So be nice to the maintenance programmer who may follow you, and write code that isn't too tricky to figure out.
I assume you want something like eval from php.
You can try to play with command design pattern, but I doubt it will be an easy task. Basically you need to write simple C++ interpreter.
What type of function do you mean? A C++ function? If so, then you will have to either (1)interpret it or (2)compile and execute it. Interpretation would be the more likely choice here. I'm not sure if there are libraries out there already to do this but I'd assume there are.
If you don't like mega-if's or huge switches, you may be SoL on any solution for anything ever, but then again there is seldom one perfect way to do things. Consider looking in to various logic structures and algorithms to see how to do something that would normally be the job of a 23-case switch could be done another way. Like I said initially, however, sometimes you really do just need a million nested if's to do what you want to.
No, in C++ this is not possible. C++ is a compiled language. When the program runs, the compiler doesn't need to be accessible, or even installed on the machine that runs the program.
If you want to do this in C++, you need to write your own interpreter that parses whatever the user enters.
Here is my best idea, but it is a tad memory intensive.
First, create a class, lets call it MyFuncPtr to store a union of several different types of pointers to functions and an integer to tell which type it is. Overload the () operator to call the function stored with a variable length argument list. Make sure to include some sort of run-time argument checking.
Finally create a map of strings to MyFuncPtrs. Store your functions in this map along with their names. Then all you need to do is feed the name into the [] command to get a function that can be easily called. Templates could probably be used to aid in the making of MyFuncPtr instances.
This would be the easiest if it were plain C functions and no name mangling is performed on the symbols (use extern "C" { ... })
With some platform-specific code you can get the address of a function by its name. Then you cast the address as a function pointer which you can use to call the function.
On windows you must be using GetProcAddress and dlsym on Posix compliant platforms.

using boost string algorithm with MFC CString to check for the end of a string

I need to check whether my CString object in MFC ends with a specific string.
I know that boost::algorithm has many functions meant for string manipulation and that in the header boost/algorithm/string/predicate.hpp could it be used for that purpose.
I usually use this library with std::string. Do you know a convenient way to use this library also with CString?
I know that the library is generic that can be used also with other string libraries used as template arguments, but it is not clear (and whether is possible) to apply this feature to CString.
Can you help me with that in case it is possible?
According to Boost String Algorithms Library, "consult the design chapter to see precise specification of supported string types", which says amongst other things, "first requirement of string-type is that it must [be] accessible using Boost.Range", and note at the bottom the MFC/ATL implementation written by Shunsuke Sogame which should allow you to combine libraries.
Edit: Since you mention regex in the comments below, this is all you really need to do (assuming a unicode build):
CString inputString;
wcmatch matchGroups;
wregex yourRegex(L"^(.*)$"), regex::icase);
if (regex_search(static_cast<LPCWSTR>(inputString), matchGroups, yourRegex))
{
CString firstCapture = matchGroups[1].str().c_str();
}
Note how we reduce the different string types to raw pointers to pass them between libraries. Replace my contrived yourRegex with your requirements, including whether or not you ignore case or are explicit about anchors.
Why don't you save yourself the trouble and just use CStringT::Right?