473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Rolling my own memicmp()

KPB
I'm currently reading Herb Sutter's Exceptional C++ book, specifically
the two items pertaining to the case insensitive string.

Basically, he achieves this by deriving from the std::char_traits<char>
class and inserting it into basic_string<>.

In one of the char_traits<> functions he implements (compare()), he
calls a function called memicmp(char const*, char const*, size_t);

I decided to roll my own based on the stdC++ libs, specifically
std::mismatch() and std::pair.

It seems to work based on my whole 5 minutes of testing.

Assuming that the function works correctly, I'm looking to see if anyone
can come up with a more elegant solution. For example, did I have to
actually create a functor (see ci_equal_to) or could I have performed
some binding operation?

Thanks,
KPB
--- BEGIN CODE ---

#include <iostream>
#include <utility>
#include <functional>
#include <cctype>
#include <string>
#include <cassert>

// My own version of memicmp that Herb Sutter uses
// in Exceptional C++
int memIcmp(char const* p1, char const* p2, size_t n);

int main()
{
using namespace std;

string s1 = "deTroit";
string s2 = "DetRoiT";

// current implementation of memIcmp
// assumes that both strings are exactly
// the same size.
assert(s1.size() == s2.size());

// memIcmp test
int i = memIcmp(s1.c_str(), s2.c_str(), s1.size());

// print result of string comparison
if(0 == i)
{
cout << "strings match" << endl;
}
else
{
cout << "strings do not match" << endl;
}

return 0;
}

// Should I have just derived this class from
// binary_function instead of equal_to?
struct ci_equal_to : public std::equal_to<char>
{
bool operator () (char left, char right)
{
return toupper(left) == toupper(right);
}
};

// Assumption: assume that both p1 and p2 are of size n
int memIcmp(char const* p1, char const* p2, size_t n)
{
using namespace std;

typedef pair<char const*, char const*> ci_diff_pair;
ci_diff_pair p = mismatch(p1, p1 + n, p2, ci_equal_to());

// both characters match exactly (case insensitive)
if (p.first == p1 + n && p.second == p2 + n)
{
return 0;
}

return *(p.first) < *(p.second) ? -1 : 1;
}

--- END CODE ---
Jul 22 '05 #1
0 1299

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: traga_2_whiskys | last post by:
Weel i want do make a box with text rolling down to up, i've make-it wiht <marquee> in html, byt the text isn't always rolling. can anibody help me?
3
by: DB2 Convert | last post by:
Hi, Correct me if I am wrong? Why should I specify at the restore statement such as Restore Database ABC ... "WITHOUT ROLLING FOWARD"? My understand is when I am using circular logging,...
3
by: db2group88 | last post by:
we are using db2 udb v8.2 on windows. All our tables are created with "not logged initially" parameter. Our application with auto commit on. I would like to do an online backup and rolling forward...
0
by: PeteCresswell | last post by:
The one I would up was a pivot table presentation" Fund Year ReturnForYear ReturnForYearPlusPreceedingYear, ReturnForYearPlusTwoPreceedingYears.....and so-forth. But that design compromise...
101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
17
by: Jose Durazo | last post by:
Hello, I'm doing an exercise to simulate rolling a pair of dice 36,000 times, then count and display how many times the simulation rolls each possible sum. For some reason each time I run my...
2
by: BurtonBach | last post by:
I have a small database that mostly keeps track of material quantities going into jobs we do. I have been able to make this work well for me. I now want to add the tracking of a rolling balance of...
3
by: farhadtarapore | last post by:
I have a very large C++ application that has been converted into a windows service. This application writes a lot of statements to the console i.e. cout and cerr. I have used std::ofstream...
4
by: brendan.wong | last post by:
hello. i'm trying to incorporate error handling into my application, but i've run into a dilemma. i've already performed 10 successful INSERTS, but on the 11th INSERT, the application fails for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.