]> Dogcows Code - chaz/vimcoder/blobdiff - src/com/dogcows/resources/C++Driver
separate whitespace handling into its own function
[chaz/vimcoder] / src / com / dogcows / resources / C++Driver
index 9ca857968510527b6388536028a3a67e6dbf0a2d..24ab333609ffa5ed8a51c9c80f0b32943fe2fb19 100644 (file)
@@ -22,7 +22,7 @@ static void __timer_start()
        struct timeval tv;
        if (gettimeofday(&tv, NULL) == 0)
        {
-               __time = (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
+               __time = double(tv.tv_sec) + double(tv.tv_usec) * 0.000001;
        }
 }
 
@@ -34,6 +34,12 @@ static double __timer_stop()
 }
 
 
+static void __eat_whitespace(std::istream& in)
+{
+       while (in.good() && std::isspace(in.peek())) in.get();
+}
+
+
 std::ostream& operator << (std::ostream& out, const std::string& str)
 {
        out << '"' << str.c_str() << '"';
@@ -55,7 +61,7 @@ std::ostream& operator << (std::ostream& out, const std::vector<T>& vec)
 
 std::istream& operator >> (std::istream& in, std::string& str)
 {
-       while (in.good() && std::isspace(in.peek())) in.get();
+       __eat_whitespace(in);
 
        int c;
        if (in.good() && (c = in.get()) == '"')
@@ -67,10 +73,6 @@ std::istream& operator >> (std::istream& in, std::string& str)
                }
                str = s.str();
        }
-       else
-       {
-               in.putback(c);
-       }
 
        return in;
 }
@@ -78,30 +80,29 @@ std::istream& operator >> (std::istream& in, std::string& str)
 template <class T>
 std::istream& operator >> (std::istream& in, std::vector<T>& vec)
 {
-       while (in.good() && std::isspace(in.peek())) in.get();
+       __eat_whitespace(in);
 
        int c;
        if (in.good() && (c = in.get()) == '{')
        {
-               while (in.good() && std::isspace(in.peek())) in.get();
-               T t;
+               __eat_whitespace(in);
                vec.clear();
                while (in.good() && (c = in.get()) != '}')
                {
                        if (c != ',') in.putback(c);
+
+                       T t;
                        in >> t;
+                       __eat_whitespace(in);
+
                        vec.push_back(t);
-                       while (in.good() && std::isspace(in.peek())) in.get();
                }
        }
-       else
-       {
-               in.putback(c);
-       }
 
        return in;
 }
 
+
 template <class T>
 bool __equals(const T& actual, const T& expected)
 {
@@ -140,9 +141,9 @@ int main(int argc, char* argv[])
        bool    __exit_on_fail = false;
        int     __pass = 0;
        int     __fail = 0;
-       
+
        if (1 < argc) __exit_on_fail = true;
-       
+
        std::ifstream __in("testcases.txt");
        for(;;)
        {
@@ -150,18 +151,18 @@ int main(int argc, char* argv[])
                $METHODPARAMDECLARES$
                __in >> __expected >> $METHODPARAMSTREAMIN$;
                if (!__in.good()) break;
-               
+
                std::cout << "----------------------------------------" << std::endl
                          << "Test " << (__pass + __fail) << ": ";
                std::cout.flush();
-               
+
                __timer_start();
-               
+
                $CLASSNAME$ object;
                $RETURNTYPE$ __actual = object.$METHODNAME$($METHODPARAMNAMES$);
-               
+
                double __t = __timer_stop();
-               
+
                if (__equals(__actual, __expected))
                {
                        std::cout << "[PASS] in " << __t << " seconds." << std::endl;
This page took 0.024602 seconds and 4 git commands to generate.