Author: wyoung
Date: Tue Mar 18 08:15:22 2008
New Revision: 2248

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2248&view=rev
Log:
- Added String::operator ==() and operator !=() for mysqlpp::null_type
  and for any T for which we have String::compare()
- String::compare() now returns 0 ("equal") when one of the strings is
  an uninitialized String() (no refcounted buffer) and the other is
  empty.  It used to consider any initialized string greater than an
  uninitted one.

Modified:
    trunk/lib/mystring.cpp
    trunk/lib/mystring.h
    trunk/test/string.cpp

Modified: trunk/lib/mystring.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/mystring.cpp?rev=2248&r1=2247&r2=2248&view=diff
==============================================================================
--- trunk/lib/mystring.cpp (original)
+++ trunk/lib/mystring.cpp Tue Mar 18 08:15:22 2008
@@ -52,9 +52,9 @@
                return compare(0, length(), other.buffer_->data());
        }
        else {
-               // Consider ourselves equal to other if our buffer is also
-               // uninitted, else we are greater because we are initted.
-               return buffer_ ? 1 : 0; 
+               // Other object has no buffer, so we are greater unless empty or
+               // we also have no buffer.
+               return length() > 0 ? 1 : 0;    
        }
 }
 
@@ -88,10 +88,13 @@
                return strncmp(data() + pos, other, num);
        }
        else if (!other) {
-               return 1;                               // initted is "greater 
than" uninitted
-       }
-       else {
-               return other ? -1 : 0;  // "less than" unless other also unitted
+               // Initted and non-empty is "greater than" uninitted
+               return length() > 0 ? 1 : 0;
+       }
+       else {
+               // This object has no buffer, so we are less than other object
+               // unless it is empty.
+               return other[0] == '\0' ? 0 : -1;
        }
 }
 

Modified: trunk/lib/mystring.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/mystring.h?rev=2248&r1=2247&r2=2248&view=diff
==============================================================================
--- trunk/lib/mystring.h (original)
+++ trunk/lib/mystring.h Tue Mar 18 08:15:22 2008
@@ -470,6 +470,42 @@
                buffer_ = other.buffer_;
 
                return *this;
+       }
+
+       /// \brief Equality comparison operator
+       ///
+       /// For comparing this object to any of the data types we have a
+       /// compare() overload for.
+       template <typename T>
+       bool operator ==(const T& rhs) const
+       {
+               return compare(rhs) == 0;
+       }
+
+       /// \brief Equality comparison operator
+       ///
+       /// For checking object against MySQL++'s global \c null constant
+       bool operator ==(const mysqlpp::null_type&) const
+       {
+               return is_null();
+       }
+
+       /// \brief Inequality comparison operator
+       ///
+       /// For comparing this object to any of the data types we have a
+       /// compare() overload for.
+       template <typename T>
+       bool operator !=(const T& rhs) const
+       {
+               return compare(rhs) != 0;
+       }
+
+       /// \brief Inequality comparison operator
+       ///
+       /// For checking object against MySQL++'s global \c null constant
+       bool operator !=(const mysqlpp::null_type&) const
+       {
+               return !is_null();
        }
 
        /// \brief Return a character within the string.

Modified: trunk/test/string.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/test/string.cpp?rev=2248&r1=2247&r2=2248&view=diff
==============================================================================
--- trunk/test/string.cpp (original)
+++ trunk/test/string.cpp Tue Mar 18 08:15:22 2008
@@ -132,13 +132,31 @@
 }
 
 
-// Ensures that the program's locale doesn't affect our floating-point
-// conversions.  ('.' vs. ',' stuff.)
-static bool
-test_locale()
-{
-
-       return true;
+// Checks that String's null comparison methods work right
+static bool
+test_null()
+{
+       mysqlpp::String not_null("", mysqlpp::mysql_type_info::string_type, 
false);
+       mysqlpp::String is_null("", mysqlpp::mysql_type_info::string_type, 
true);
+       if (not_null.is_null() == true) {
+               std::cerr << "not_null.is_null() == true!" << std::endl;
+               return false;
+       }
+       else if (not_null == mysqlpp::null) {
+               std::cerr << "not_null == mysqlpp:null!" << std::endl;
+               return false;
+       }
+       else if (is_null.is_null() == false) {
+               std::cerr << "is_null.is_null() == false!" << std::endl;
+               return false;
+       }
+       else if (is_null != mysqlpp::null) {
+               std::cerr << "is_null != mysqlpp:null!" << std::endl;
+               return false;
+       }
+       else {
+               return true;
+       }
 }
 
 
@@ -178,6 +196,29 @@
 }
 
 
+// Similar to test_equality, but only works with std::string
+// comparisons, which uses String::operator ==()
+static bool
+test_string_equality(const mysqlpp::String& s, std::string value)
+{
+       if (s == value) {
+               if (s != value) {
+                       std::cerr << "String(\"" << s << "\") != 
std::string(\"" <<
+                                       value << "\"), case 2!" << std::endl;
+                       return false;
+               }
+               else {
+                       return true;
+               }
+       }
+       else {
+               std::cerr << "String(\"" << s << "\") != std::string(\"" <<
+                               value << "\"), case 1!" << std::endl;
+               return false;
+       }
+}
+
+
 int
 main(int, char* argv[])
 {
@@ -202,7 +243,6 @@
                failures += test_quote_q(empty, true) == false;
                failures += test_quote_q(mysqlpp::String("1", typeid(int)),
                                false) == false;
-               failures += test_locale() == false;
                failures += test_float_conversion() == false;
                failures += test_float_conversion() == false;
                failures += test_int_conversion(empty, false) == false;
@@ -211,6 +251,9 @@
                failures += test_int_conversion(intable1, false) == false;
                failures += test_int_conversion(intable2, false) == false;
                failures += test_int_conversion(nonint, true) == false;
+               failures += test_null() == false;
+               failures += test_string_equality(empty, "") == false;
+               failures += test_string_equality(zero, "0") == false;
                
                return failures;
        }


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to