Author: sebor
Date: Mon Feb 27 18:33:17 2006
New Revision: 381544
URL: http://svn.apache.org/viewcvs?rev=381544&view=rev
Log:
2006-02-27 Martin Sebor <[EMAIL PROTECTED]>
* alg_test.h (mismatch): New helper function.
(BinaryPredicate): Modified ctor to take an argument describing
the operation performed by the member operator(), removed copy
ctor and assignment operator.
(X::Less): New class.
* alg_test.cpp (mismatch): Defined.
(compare): Implemented in terms of mismatch.
(BinaryPredicate): Removed copy ctor and assignment operator.
(_rw_fmtxarrayv): Recognized '@' in addition to '*' as a way
to specify the position of the cursor (as the pointer rather
than the index of the element designated by the cursor).
Modified:
incubator/stdcxx/trunk/tests/include/alg_test.h
incubator/stdcxx/trunk/tests/src/alg_test.cpp
Modified: incubator/stdcxx/trunk/tests/include/alg_test.h
URL:
http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/include/alg_test.h?rev=381544&r1=381543&r2=381544&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/include/alg_test.h (original)
+++ incubator/stdcxx/trunk/tests/include/alg_test.h Mon Feb 27 18:33:17 2006
@@ -189,6 +189,13 @@
// array of X objects is greater than the second array
static int compare (const X*, const X*, _RWSTD_SIZE_T);
+ // returns a pointer to the first element in the sequence of X
+ // whose value is not equal to the corresponding element of
+ // the character string or 0 when no such element exists
+ static const X* mismatch (const X*, const char*, _RWSTD_SIZE_T);
+
+ struct Less;
+
private:
enum assign_op {
@@ -520,17 +527,36 @@
// total number of times operator() was invoked
static _RWSTD_SIZE_T n_total_op_fcall_;
- bool ignore_case_;
+ enum binary_op {
+ op_equals,
+ op_not_equals,
+ op_less,
+ op_less_equal,
+ op_greater,
+ op_greater_equal
+ };
- BinaryPredicate (bool = false);
+ BinaryPredicate (binary_op);
- BinaryPredicate (const BinaryPredicate&);
+ virtual ~BinaryPredicate ();
- BinaryPredicate& operator= (const BinaryPredicate&);
+ virtual conv_to_bool operator()(const X&, const X&) /* non-const */;
- virtual ~BinaryPredicate ();
+private:
- virtual conv_to_bool operator()(const X&, const X&) const;
+ // not assignable
+ void operator= (const BinaryPredicate&);
+
+ binary_op op_;
+};
+
+
+struct X::Less: BinaryPredicate
+{
+ // dummy arguments provided to prevent the class from being
+ // default constructible and implicit conversion from int
+ Less (int /* dummy */, int /* dummy */)
+ : BinaryPredicate (BinaryPredicate::op_less) { /* no-op */ }
};
/**************************************************************************/
Modified: incubator/stdcxx/trunk/tests/src/alg_test.cpp
URL:
http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/tests/src/alg_test.cpp?rev=381544&r1=381543&r2=381544&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/alg_test.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/alg_test.cpp Mon Feb 27 18:33:17 2006
@@ -520,11 +520,11 @@
}
-/* static */ int
-X::compare (const X *x, const char *str, size_t len /* = -1 */)
+/* static */ const X*
+X::mismatch (const X *xarray, const char *str, size_t len /* = -1 */)
{
if (!str)
- return x == 0;
+ return xarray;
if (size_t (-1) == len)
len = strlen (str);
@@ -533,8 +533,23 @@
const int val = UChar (str [i]);
- if (val != x [i].val_)
- return x [i].val_ - val;
+ if (val != xarray [i].val_)
+ return xarray + i;
+ }
+
+ return 0;
+}
+
+
+/* static */ int
+X::compare (const X *xarray, const char *str, size_t len /* = -1 */)
+{
+ const X* const px = mismatch (xarray, str, len);
+
+ if (px) {
+ RW_ASSERT (size_t (px - xarray) < len);
+
+ return px->val_ - int (UChar (str [px - xarray]));
}
return 0;
@@ -542,9 +557,9 @@
/* static */ int
-X::compare (const char *str, const X *x, size_t len /* = -1 */)
+X::compare (const char *str, const X *xarray, size_t len /* = -1 */)
{
- return -X::compare (x, str, len);
+ return -X::compare (xarray, str, len);
}
@@ -603,29 +618,12 @@
BinaryPredicate::
-BinaryPredicate (bool ignore_case)
- : ignore_case_ (ignore_case)
-{
- // no-op
-}
-
-
-BinaryPredicate::
-BinaryPredicate (const BinaryPredicate &rhs)
- : ignore_case_ (rhs.ignore_case_)
+BinaryPredicate (binary_op op): op_ (op)
{
// no-op
}
-BinaryPredicate& BinaryPredicate::
-operator= (const BinaryPredicate &rhs)
-{
- ignore_case_ = rhs.ignore_case_;
- return *this;
-}
-
-
/* virtual */ BinaryPredicate::~BinaryPredicate ()
{
// no-op
@@ -633,29 +631,22 @@
/* virtual */ conv_to_bool BinaryPredicate::
-operator()(const X &lhs, const X &rhs) const
+operator()(const X &lhs, const X &rhs) /* non-const */
{
++n_total_op_fcall_;
- if (lhs == rhs)
- return conv_to_bool::make (true);
-
- if (ignore_case_) {
-
- const int lval = lhs.val_;
- const int rval = rhs.val_;
-
- if ( lval < 0 || lval > int (_RWSTD_UCHAR_MAX)
- || rval < 0 || rval > int (_RWSTD_UCHAR_MAX))
- return conv_to_bool::make (false);
+ bool result;
- const int lup = toupper (lval);
- const int rup = toupper (rval);
-
- return conv_to_bool::make (_RWSTD_EOF != lup && lup == rup);
+ switch (op_) {
+ case op_equals: result = lhs.val_ == rhs.val_; break;
+ case op_not_equals: result = !(lhs.val_ == rhs.val_); break;
+ case op_less: result = lhs.val_ < rhs.val_; break;
+ case op_less_equal: result = !(rhs.val_ < lhs.val_); break;
+ case op_greater: result = rhs.val_ < lhs.val_; break;
+ case op_greater_equal: result = !(rhs.val_ < lhs.val_); break;
}
- return conv_to_bool::make (false);
+ return conv_to_bool::make (result);
}
@@ -732,9 +723,21 @@
int paramno = -1;
int cursor = -1;
- // directive syntax:
- // "X=" [ '#' ] [ '+' ] [ '*' | <n> ] [ '.' [ '*' | <n> ] ]
+ const X* pelem = 0;
+ // directive syntax:
+ // "X=" [ '#' ] [ '+' ] [ '*' | <n> ] [ '.' [ '*' | '@' | <n> ] ]
+ // where
+ // '#' causes X::id_ to be included in output
+ // '+' forces X::val_ to be formatted as an integer (otherwise
+ // it is formatted as an (optionally escaped) character
+ // '*' or <n> is the number of elements in the sequence (the
+ // first occurrence)
+ // '*', <n> is the offset of the cursor within the sequence
+ // (where the cursor is a pair of pointy brackets
+ // surrounding the element, e.g., >123<)
+ // '@' is the pointer to the element to be surrended by the
+ // pair of pointy brackets
if ('X' != fmt [0] || '=' != fmt [1])
return _RWSTD_INT_MIN;
@@ -785,10 +788,22 @@
RW_ASSERT (0 != pva);
// extract the width from rw_snprintfa's variable argument
- // list pass through to us by the caller
+ // list passed through to us by the caller
cursor = va_arg (*pva, int);
++fmt;
}
+ else if ('@' == *fmt) {
+ if (0 == pva)
+ pva = va_arg (va, va_list*);
+
+ RW_ASSERT (0 != pva);
+
+ // extract the pointer from rw_snprintfa's variable argument
+ // list passed through to us by the caller
+ pelem = va_arg (*pva, X*);
+
+ ++fmt;
+ }
else if (isdigit (*fmt)) {
char* end = 0;
cursor = strtol (fmt, &end, 10);
@@ -809,6 +824,13 @@
// list pass through to us by the caller
const X* const xbeg = va_arg (*pva, X*);
+ if (-1 != cursor) {
+ RW_ASSERT (-1 < cursor);
+ RW_ASSERT (0 == pelem);
+
+ pelem = xbeg + cursor;
+ }
+
// extract the address where to store the extracted argument
// for use by any subsequent positional paramaters
const X** const pparam = va_arg (va, const X**);
@@ -836,12 +858,12 @@
"%{?}%d:%{;}"
"%{?}%d%{?},%{;}%{:}%{lc}%{;}"
"%{?}<%{;}",
- px - xbeg == cursor, // '>'
+ px == pelem, // '>'
fl_pound, px->id_, // "<id>:"
fl_plus, px->val_, // <val>
px + 1 < xbeg + nelems, // ','
px->val_, // <val>
- px - xbeg == cursor); // '<'
+ px == pelem); // '<'
if (n < 0)
return n;