Skip to content

Instantly share code, notes, and snippets.

@if1live
Created April 20, 2015 11:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save if1live/3e8f24a4b8bdcc15a3e9 to your computer and use it in GitHub Desktop.
Save if1live/3e8f24a4b8bdcc15a3e9 to your computer and use it in GitHub Desktop.
custom strcmp
#include <cstdio>
#include <cstring>
#include <cassert>
typedef enum {
MyOrderedAscending = -1,
MyOrderSame,
MyOrderedDescending
} MyComparisonResult;
MyComparisonResult my_strcmp_enum(const char * str1, const char * str2) {
int val = strcmp(str1, str2);
if(val < 0) {
return MyOrderedAscending;
} else if(val > 0) {
return MyOrderedDescending;
} else {
return MyOrderSame;
}
}
bool operator==(MyComparisonResult a, int b) {
return ((int)a == b);
}
bool operator==(MyComparisonResult a, bool b) {
return !((bool)a == b);
}
bool operator!=(MyComparisonResult a, int b) { return !(a == b); }
bool operator!=(MyComparisonResult a, bool b) { return !(a == b); }
// compile error
// error: conversion function must be a non-static member function
// operator bool(MyComparisonResult a) { return true; }
class ComparisonResult {
public:
enum {
ASC = -1,
EQUAL,
DESC
};
ComparisonResult(int val) : val(val) {}
bool operator==(int x) {
return ((int)val == x);
}
bool operator==(bool x) {
return !((bool)val == x);
}
bool operator!=(int x) { return !(*this == x); }
bool operator!=(bool x) { return !(*this == x); }
operator bool() {
return (val == EQUAL);
}
static ComparisonResult strcmp(const char *str1, const char *str2) {
int raw = ::strcmp(str1, str2);
if(raw < 0) {
return ComparisonResult(ASC);
} else if(raw > 0) {
return ComparisonResult(DESC);
} else {
return ComparisonResult(EQUAL);
}
}
private:
int val;
};
ComparisonResult my_strcmp(const char *str1, const char *str2)
{
return ComparisonResult::strcmp(str1, str2);
}
void test_enum_version()
{
assert(my_strcmp_enum("foo", "foo") == 0);
assert(my_strcmp_enum("foo", "foo") == true);
assert(my_strcmp_enum("foo", "bar") != 0);
assert(my_strcmp_enum("foo", "bar") != true);
// compatible with strcmp
assert(my_strcmp_enum("1", "1") == strcmp("1", "1"));
assert(my_strcmp_enum("1", "2") == strcmp("1", "2"));
assert(my_strcmp_enum("2", "1") == strcmp("2", "1"));
// if(my_strcmp...)
printf("same : %d\n", my_strcmp_enum("foo", "foo"));
printf("different : %d\n", !my_strcmp_enum("foo", "foo"));
}
void test_class_version()
{
assert(my_strcmp("foo", "foo") == 0);
assert(my_strcmp("foo", "foo") == true);
assert(my_strcmp("foo", "foo"));
assert(my_strcmp("foo", "bar") != 0);
assert(my_strcmp("foo", "bar") != true);
assert(!my_strcmp("foo", "bar"));
// compatible with strcmp
assert(my_strcmp("1", "1") == strcmp("1", "1"));
assert(my_strcmp("1", "2") == strcmp("1", "2"));
assert(my_strcmp("2", "1") == strcmp("2", "1"));
}
int main()
{
test_enum_version();
test_class_version();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment