aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Baptiste Queru <jbq@google.com>2009-11-15 12:05:41 -0800
committerJean-Baptiste Queru <jbq@google.com>2009-11-15 12:05:41 -0800
commit992df477e264540d244af44dd49e0e2a481eaf97 (patch)
tree6cba51557eba418b540029ad96aeffe03859722a
parent9cb0478662a7c988146fff0d868bba2839ea80f2 (diff)
parent1ebb2f779283ea40603d9a60c0366c1cb59698a6 (diff)
downloadastl-992df477e264540d244af44dd49e0e2a481eaf97.tar.gz
merge from eclair
-rw-r--r--include/functional72
-rw-r--r--include/string12
-rw-r--r--include/type_traits.h4
-rw-r--r--src/string.cpp61
-rw-r--r--tests/Android.mk3
-rw-r--r--tests/test_functional.cpp61
-rw-r--r--tests/test_string.cpp38
7 files changed, 181 insertions, 70 deletions
diff --git a/include/functional b/include/functional
new file mode 100644
index 0000000..2f4ea3d
--- /dev/null
+++ b/include/functional
@@ -0,0 +1,72 @@
+/* -*- c++ -*- */
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef ANDROID_ASTL_FUNCTIONAL__
+#define ANDROID_ASTL_FUNCTIONAL__
+
+#if defined(_T) || defined(_Arg) || defined(_Arg1) || defined(_Arg2) || \
+ defined(_Result) || defined(_Name) || defined(_Op)
+#error "Macro(s) already defined."
+#endif
+
+namespace std {
+
+template <class _Arg, class _Result>
+struct unary_function {
+ typedef _Arg argument_type;
+ typedef _Result result_type;
+};
+
+template <class _Arg1, class _Arg2, class _Result>
+struct binary_function {
+ typedef _Arg1 first_argument_type;
+ typedef _Arg2 second_argument_type;
+ typedef _Result result_type;
+};
+
+// Comparaison
+
+#define FUNCTIONAL_BINARY_COMPARAISON(_Name, _Op) \
+ template <typename _T> \
+ struct _Name : public binary_function<_T, _T, bool> \
+ { \
+ bool operator()(const _T& left, const _T& right) const \
+ { return left _Op right; } \
+ };
+
+FUNCTIONAL_BINARY_COMPARAISON(equal_to, ==)
+FUNCTIONAL_BINARY_COMPARAISON(not_equal_to, !=)
+FUNCTIONAL_BINARY_COMPARAISON(greater, >)
+FUNCTIONAL_BINARY_COMPARAISON(less, <)
+FUNCTIONAL_BINARY_COMPARAISON(greater_equal, >=)
+FUNCTIONAL_BINARY_COMPARAISON(less_equal, <=)
+
+} // namespace std
+
+#endif // ANDROID_ASTL_FUNCTIONAL__
diff --git a/include/string b/include/string
index 552c37b..f1cfe93 100644
--- a/include/string
+++ b/include/string
@@ -61,12 +61,17 @@ class string
// Constructors
string();
+ string(const string& str);
+
// Construct a string from a source's substring.
// @param str The source string.
// @param pos The index of the character to start the copy at.
// @param n The number of characters to copy. Use string::npos for the
// remainder.
- string(const string& str, size_t pos = 0, size_type n = npos);
+ string(const string& str, size_t pos, size_type n);
+
+ // Same as above but implicitly copy from pos to the end of the str.
+ string(const string& str, size_type pos);
// Construct a string from a C string.
// @param str The source string, must be '\0' terminated.
@@ -236,11 +241,6 @@ class string
// starting position.
size_type find(const value_type *str, size_type pos = 0) const;
- // @return a substring of this one.
- string substr(size_type pos = 0, size_type n = npos) const {
- return string(*this, pos, n);
- }
-
private:
bool SafeMalloc(size_type n);
void SafeRealloc(size_type n);
diff --git a/include/type_traits.h b/include/type_traits.h
index 14a3664..bc7cc0b 100644
--- a/include/type_traits.h
+++ b/include/type_traits.h
@@ -153,8 +153,8 @@ struct sfinae_types
// Only classes will match the first declaration (pointer to member).
// TODO: newer version of gcc have these is_class built in.
-template<typename _Tp> sfinae_types::one test_pod_type(int _Tp::*);
-template<typename _Tp> sfinae_types::two& test_pod_type(...);
+template<typename _T> sfinae_types::one test_pod_type(int _T::*);
+template<typename _T> sfinae_types::two& test_pod_type(...);
template<typename _T>
struct is_pod: public integral_constant<bool, sizeof(test_pod_type<_T>(0)) != sizeof(sfinae_types::one)> { };
diff --git a/src/string.cpp b/src/string.cpp
index b80fd78..bb9e35a 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -136,6 +136,12 @@ void string::ConstructEmptyString()
mCapacity = 0;
}
+void string::Constructor(const value_type *str, size_type n)
+{
+ Constructor(str, 0, n);
+}
+
+
void string::Constructor(const value_type *str, size_type pos, size_type n)
{
// Enough data and no overflow
@@ -168,29 +174,28 @@ string::string()
ConstructEmptyString();
}
+string::string(const string& str)
+{
+ Constructor(str.mData, str.mLength);
+}
+
string::string(const string& str, size_type pos, size_type n)
{
- if (pos < str.mLength)
+ if (pos < str.mLength && n <= (str.mLength - pos))
{
- if (npos == n)
- {
- Constructor(str.mData, pos , str.mLength - pos);
- return;
- }
- else if (n <= (str.mLength - pos))
- {
- Constructor(str.mData, pos , n);
- return;
- }
+ Constructor(str.mData + pos , n);
+ }
+ else
+ {
+ ConstructEmptyString();
}
- ConstructEmptyString();
}
-string::string(const value_type *str)
+string::string(const string& str, size_type pos)
{
- if (NULL != str)
+ if (pos < str.mLength)
{
- Constructor(str, 0, strlen(str));
+ Constructor(str.mData, pos, str.mLength - pos);
}
else
{
@@ -198,18 +203,23 @@ string::string(const value_type *str)
}
}
-string::string(const value_type *str, size_type n)
+string::string(const value_type *str)
{
- if (npos != n)
+ if (NULL != str)
{
- Constructor(str, 0, n);
+ Constructor(str, strlen(str));
}
else
{
- ConstructEmptyString(); // standard requires we throw length_error here.
+ ConstructEmptyString();
}
}
+string::string(const value_type *str, size_type n)
+{
+ Constructor(str, n);
+}
+
// Char repeat constructor.
string::string(size_type n, char c)
{
@@ -220,7 +230,7 @@ string::string(const value_type *begin, const value_type *end)
{
if (begin < end)
{
- Constructor(begin, 0, end - begin);
+ Constructor(begin, end - begin);
}
else
{
@@ -445,7 +455,7 @@ char& string::operator[](const size_type pos)
string& string::assign(const string& str)
{
clear();
- Constructor(str.mData, 0, str.mLength);
+ Constructor(str.mData, str.mLength);
return *this;
}
@@ -470,7 +480,7 @@ string& string::assign(const value_type *str)
return *this;
}
clear();
- Constructor(str, 0, strlen(str));
+ Constructor(str, strlen(str));
return *this;
}
@@ -481,7 +491,7 @@ string& string::assign(const value_type *array, size_type n)
return *this;
}
clear();
- Constructor(array, 0, n);
+ Constructor(array, n);
return *this;
}
@@ -519,7 +529,10 @@ string::size_type string::find(const value_type *str, size_type pos) const
{
return string::npos;
}
- return static_cast<size_type>(idx - mData);
+
+ const std::ptrdiff_t delta = idx - mData;
+
+ return static_cast<size_type>(delta);
}
} // namespace std
diff --git a/tests/Android.mk b/tests/Android.mk
index 432dece..825d1eb 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -62,9 +62,10 @@ endef
sources := \
test_algorithm.cpp \
+ test_functional.cpp \
test_string.cpp \
test_type_traits.cpp \
- test_uninitialized.cpp \
+ test_uninitialized.cpp \
test_vector.cpp
# Disable all optimization for the host target to help test tools (valgrind...)
diff --git a/tests/test_functional.cpp b/tests/test_functional.cpp
new file mode 100644
index 0000000..ba72f80
--- /dev/null
+++ b/tests/test_functional.cpp
@@ -0,0 +1,61 @@
+/* -*- c++ -*- */
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "../include/functional"
+#ifndef ANDROID_ASTL_FUNCTIONAL__
+#error "Wrong header included!!"
+#endif
+
+#include "common.h"
+
+
+namespace android {
+
+
+bool testInteger() {
+ EXPECT_TRUE(std::equal_to<int>()(10, 10));
+ EXPECT_TRUE(std::not_equal_to<int>()(10, 11));
+
+ EXPECT_TRUE(std::greater<int>()(10, 5));
+ EXPECT_TRUE(std::less<int>()(5, 10));
+
+ EXPECT_TRUE(std::greater_equal<int>()(10, 5));
+ EXPECT_TRUE(std::less_equal<int>()(5, 10));
+
+ EXPECT_TRUE(std::greater_equal<int>()(10, 10));
+ EXPECT_TRUE(std::less_equal<int>()(10, 10));
+ return true;
+}
+
+} // namespace android
+
+int main(int argc, char **argv)
+{
+ FAIL_UNLESS(testInteger);
+}
diff --git a/tests/test_string.cpp b/tests/test_string.cpp
index ecff20e..bd69a46 100644
--- a/tests/test_string.cpp
+++ b/tests/test_string.cpp
@@ -96,10 +96,7 @@ bool testConstructorString()
string str2 (str1);
EXPECT_TRUE(str1.size() == 21);
- const char literal[] = "scott mills cracks me up";
- const string str3(literal);
- EXPECT_TRUE(str3 == literal);
-
+ const string str3("scott mills cracks me up");
string str4(str3, 12);
EXPECT_TRUE(strcmp("cracks me up", str4.c_str()) == 0);
@@ -118,12 +115,6 @@ bool testConstructorString()
string str9(str3, 24, 1);
EXPECT_TRUE(strcmp("", str9.c_str()) == 0);
- string str10(str3, 0);
- EXPECT_TRUE(strcmp(literal, str10.c_str()) == 0);
-
- string str11(str3, 6);
- EXPECT_TRUE(strcmp("mills cracks me up", str11.c_str()) == 0);
-
return true;
}
@@ -876,32 +867,6 @@ bool testErase()
return true;
}
-bool testSubstr()
-{
- const char literal[] = "basement jaxx";
- const string str01(literal);
- string str02;
-
- str02 = str01.substr(0, 5);
- EXPECT_TRUE(str02 == "basem");
-
- str02 = str01.substr(0, 8);
- EXPECT_TRUE(str02 == "basement");
-
- str02 = str01.substr(0, string::npos);
- EXPECT_TRUE(str02 == "basement jaxx");
-
- str02 = str01.substr();
- EXPECT_TRUE(str02 == "basement jaxx");
-
- str02 = str01.substr(9);
- EXPECT_TRUE(str02 == "jaxx");
-
- str02 = str01.substr(9, string::npos);
- EXPECT_TRUE(str02 == "jaxx");
- return true;
-}
-
} // namespace android
int main(int argc, char **argv)
@@ -926,6 +891,5 @@ int main(int argc, char **argv)
FAIL_UNLESS(testCapacity);
FAIL_UNLESS(testClear);
FAIL_UNLESS(testErase);
- FAIL_UNLESS(testSubstr);
return kPassed;
}