Changeset 1312 for trunk/yat/utility


Ignore:
Timestamp:
May 19, 2008, 6:30:52 PM (13 years ago)
Author:
Peter
Message:

closes ticket:360 a template function to check if a string is of type
T is_int, is_float, and is_double use this template, which implies a
change. For instance, is_int("inf") will only return true if
numerical_limits<int>::has_infinity is true. This was not the case
before. Similar for "-inf" and "NaN", and alos for is_float. For
is_double this should not be an issue because double is required to
support both inf and NaN (checked in configure).

Location:
trunk/yat/utility
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/utility.cc

    r1304 r1312  
    3939  bool is_double(const std::string& s)
    4040  {
    41     if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf"))
    42       return true;
    43     std::stringstream ss(s);
    44     double a;
    45     ss>>a;
    46     if(ss.fail())
    47       return false;
    48     // Check that nothing is left on stream
    49     std::string b;
    50     ss >> b;
    51     return (b.size() ? false : true);
     41    return is<double>(s);
    5242  }
    5343 
     
    7060  bool is_float(const std::string& s)
    7161  {
    72     if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf"))
    73       return true;
    74     std::stringstream ss(s);
    75     float a;
    76     ss>>a;
    77     if(ss.fail())
    78       return false;
    79     // Check that nothing is left on stream
    80     std::string b;
    81     ss >> b;
    82     return (b.size() ? false : true);
     62    return is<float>(s);
    8363  }
    8464
     
    8666  bool is_int(const std::string& s)
    8767  {
    88     if (is_nan(s) || is_equal(s, "inf") || is_equal(s, "-inf"))
    89       return true;
    90     std::stringstream ss(s);
    91     int a;
    92     ss >> a;
    93     if(ss.fail())
    94       return false;
    95     // Check that nothing is left on stream
    96     std::string b;
    97     ss >> b;
    98     return (b.size() ? false : true);
     68    return is<int>(s);
    9969  }
    10070
  • trunk/yat/utility/utility.h

    r1304 r1312  
    5454  T convert(const std::string& s);
    5555
     56  /**
     57     \brief check if string is convertible to (numerical) type
     58   */
     59  template<typename T>
     60  bool is(const std::string& s);
     61
    5662  ///
    5763  /// @return true if string is a double
     64  ///
     65  /// \deprecated use is<double>(const std::string&)
    5866  ///
    5967  bool is_double(const std::string&);
     
    6977  /// @return true if string is a float
    7078  ///
     79  /// \deprecated use is<float>(const std::string&)
     80  ///
    7181  bool is_float(const std::string&);
    7282
    7383  ///
    7484  /// @return true if string is an int
     85  ///
     86  /// \deprecated use is<int>(const std::string&)
    7587  ///
    7688  bool is_int(const std::string&);
     
    114126  }
    115127
     128  template<typename T>
     129  bool is(const std::string& s)
     130  {
     131    if (is_nan(s))
     132      return std::numeric_limits<T>::has_quiet_NaN;
     133    if (is_equal(s, "inf"))
     134      return std::numeric_limits<T>::has_infinity;
     135    if (is_equal(s, "-inf"))
     136      return std::numeric_limits<T>::has_infinity &&
     137        std::numeric_limits<T>::is_signed;
     138    std::stringstream ss(s);
     139    T a;
     140    ss >> a;
     141    if(ss.fail())
     142      return false;
     143    // Check that nothing is left on stream
     144    std::string b;
     145    ss >> b;
     146    return b.empty();
     147  }
     148
    116149}}} // of namespace utility, yat, and theplu
    117150
Note: See TracChangeset for help on using the changeset viewer.