Author: wyoung
Date: Thu Nov 15 20:40:18 2007
New Revision: 1859
URL: http://svn.gna.org/viewcvs/mysqlpp?rev=1859&view=rev
Log:
- Added several compare() methods to SQLTypeAdapter and String classes
to partially replicate what's in std::string. (We left out a few of
the less useful ones.)
- Added an at() method to SQLTypeAdapter
Modified:
trunk/lib/mystring.cpp
trunk/lib/mystring.h
trunk/lib/stadapter.cpp
trunk/lib/stadapter.h
Modified: trunk/lib/mystring.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/mystring.cpp?rev=1859&r1=1858&r2=1859&view=diff
==============================================================================
--- trunk/lib/mystring.cpp (original)
+++ trunk/lib/mystring.cpp Thu Nov 15 20:40:18 2007
@@ -53,35 +53,48 @@
int
String::compare(const String& other) const
{
- if (buffer_) {
- if (other.buffer_) {
- const char* ptb = buffer_->data();
// ptr to this buffer
- const char* pob = other.buffer_->data(); // ptr
to other buffer
- const size_type short_len = std::min(length(),
other.length());
- for (size_type i = 0; i < short_len; ++i) {
- if (ptb[i] != pob[i]) {
- return ptb[i] - pob[i];
- }
- }
-
- return length() - other.length();
- }
- else {
- // Arbitrarily consider a String that has a buffer to be
- // "greater than" one that is default-constructed.
- return 1;
- }
- }
- else if (other.buffer_) {
- // Reverse of above rule
- return -1;
+ if (other.buffer_) {
+ return compare(0, length(), other.buffer_->data());
}
else {
- // Neither String has a buffer, so consider them to be equal.
- return 0;
+ // Consider ourselves equal to other if our buffer is also
+ // uninitted, else we are greater because we are initted.
+ return buffer_ ? 1 : 0;
}
}
+int
+String::compare(const std::string& other) const
+{
+ return compare(0, length(), other.data());
+}
+
+int
+String::compare(size_type pos, size_type num, std::string& other) const
+{
+ return compare(pos, num, other.data());
+}
+
+int
+String::compare(const char* other) const
+{
+ return compare(0, length(), other);
+}
+
+int
+String::compare(size_type pos, size_type num,
+ const char* other) const
+{
+ if (buffer_ && other) {
+ 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
+ }
+}
template <>
String
Modified: trunk/lib/mystring.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/mystring.h?rev=1859&r1=1858&r2=1859&view=diff
==============================================================================
--- trunk/lib/mystring.h (original)
+++ trunk/lib/mystring.h Thu Nov 15 20:40:18 2007
@@ -250,10 +250,44 @@
///
/// \param other string to compare against this one
///
- /// \retval <0 if str1 is lexically "less than" str2
- /// \retval 0 if str1 is equal to str2
- /// \retval >0 if str1 is lexically "greater than" str2
+ /// \see compare(size_type, size_type, const char*)
int compare(const String& other) const;
+
+ /// \brief Lexically compare this string to another.
+ ///
+ /// \param other string to compare against this one
+ ///
+ /// \see compare(size_type, size_type, const char*)
+ int compare(const std::string& other) const;
+
+ /// \brief Lexically compare this string to another.
+ ///
+ /// \param pos position within this string to begin comparison
+ /// \param num maximum number of characters within this string to
+ /// use in comparison
+ /// \param other string to compare against this one
+ ///
+ /// \see compare(size_type, size_type, const char*)
+ int compare(size_type pos, size_type num, std::string& other) const;
+
+ /// \brief Lexically compare this string to another.
+ ///
+ /// \param other string to compare against this one
+ ///
+ /// \see compare(size_type, size_type, const char*)
+ int compare(const char* other) const;
+
+ /// \brief Lexically compare this string to another.
+ ///
+ /// \param pos position within this string to begin comparison
+ /// \param num maximum number of characters within this string to
+ /// use in comparison
+ /// \param other string to compare against this one
+ ///
+ /// \retval < 0 if this string is lexically "less than" other
+ /// \retval 0 if this string is equal to other
+ /// \retval > 0 if this string is lexically "greater than" other
+ int compare(size_type pos, size_type num, const char* other) const;
/// \brief Raw access to the underlying buffer, with no C string
/// interpretation.
Modified: trunk/lib/stadapter.cpp
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/stadapter.cpp?rev=1859&r1=1858&r2=1859&view=diff
==============================================================================
--- trunk/lib/stadapter.cpp (original)
+++ trunk/lib/stadapter.cpp Thu Nov 15 20:40:18 2007
@@ -311,6 +311,66 @@
buffer_(new RefCountedBuffer(null_str, typeid(void), true)),
is_processed_(false)
{
+}
+
+char
+SQLTypeAdapter::at(size_type i) const throw(std::out_of_range)
+{
+ if (buffer_) {
+ if (i <= length()) {
+ return *(buffer_->data() + i);
+ }
+ else {
+ throw out_of_range("Not enough chars in
SQLTypeAdapter");
+ }
+ }
+ else {
+ throw out_of_range("SQLTypeAdapter buffer not initialized");
+ }
+}
+
+int
+SQLTypeAdapter::compare(const SQLTypeAdapter& other) const
+{
+ if (other.buffer_) {
+ return compare(0, length(), other.buffer_->data());
+ }
+ else {
+ return buffer_ ? 1 : 0;
+ }
+}
+
+int
+SQLTypeAdapter::compare(const string& other) const
+{
+ return compare(0, length(), other.data());
+}
+
+int
+SQLTypeAdapter::compare(size_type pos, size_type num, string& other) const
+{
+ return compare(pos, num, other.data());
+}
+
+int
+SQLTypeAdapter::compare(const char* other) const
+{
+ return compare(0, length(), other);
+}
+
+int
+SQLTypeAdapter::compare(size_type pos, size_type num,
+ const char* other) const
+{
+ if (buffer_ && other) {
+ 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
+ }
}
const char*
Modified: trunk/lib/stadapter.h
URL:
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/stadapter.h?rev=1859&r1=1858&r2=1859&view=diff
==============================================================================
--- trunk/lib/stadapter.h (original)
+++ trunk/lib/stadapter.h Thu Nov 15 20:40:18 2007
@@ -34,6 +34,7 @@
#include "null.h"
#include "sql_types.h"
+#include <stdexcept>
#include <string>
namespace mysqlpp {
@@ -161,6 +162,41 @@
/// \brief Copy a C++ \c string into this object
SQLTypeAdapter& operator =(const std::string& str);
+ /// \brief Returns the character at a given position within the
+ /// string buffer.
+ ///
+ /// \throw out_of_range if the internal buffer is not initialized
+ /// (default ctor called, and no assignment operator subsequently)
+ /// or if there are not at least i + 1 characters in the buffer
+ char at(size_type i) const throw(std::out_of_range);
+
+ /// \brief Compare the internal buffer to the given string
+ ///
+ /// Works just like string::compare(const std::string&).
+ int compare(const SQLTypeAdapter& other) const;
+
+ /// \brief Compare the internal buffer to the given string
+ ///
+ /// Works just like string::compare(const std::string&).
+ int compare(const std::string& other) const;
+
+ /// \brief Compare the internal buffer to the given string
+ ///
+ /// Works just like string::compare(size_type, size_type,
+ /// std::string&).
+ int compare(size_type pos, size_type num, std::string& other) const;
+
+ /// \brief Compare the internal buffer to the given string
+ ///
+ /// Works just like string::compare(const char*).
+ int compare(const char* other) const;
+
+ /// \brief Compare the internal buffer to the given string
+ ///
+ /// Works just like string::compare(size_type, size_type,
+ /// const char*).
+ int compare(size_type pos, size_type num, const char* other) const;
+
/// \brief Return pointer to raw data buffer
const char* data() const;
_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits