Ignore:
Timestamp:
Mar 16, 2013 9:31:43 AM (11 years ago)
Author:
gah
Message:

fixes for lack of string trim for numbers and unit conversion

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lang/tcl/src/RpUnitsTclInterface.cc

    r3177 r3511  
    3838 */
    3939
     40#include <algorithm>
     41#include <functional>
     42#include <cctype>
     43
     44// Trim from start
     45static inline std::string &ltrim(std::string &s)
     46{
     47    s.erase(s.begin(),
     48        std::find_if(s.begin(), s.end(),
     49                     std::not1(std::ptr_fun<int, int>(std::isspace))));
     50    return s;
     51}
     52
     53// Trim from end
     54static inline std::string &rtrim(std::string &s)
     55{
     56    s.erase(std::find_if(s.rbegin(), s.rend(),
     57        std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
     58    return s;
     59}
     60
     61// Trim from both ends
     62static inline std::string &trim(std::string &s)
     63{
     64    return ltrim(rtrim(s));
     65}
     66
    4067extern "C" int
    4168RpUnits_Init(Tcl_Interp *interp)
     
    7198 * provided in -context option.
    7299 */
     100
    73101
    74102int
     
    208236    // or if we should use those provided in -context option
    209237
     238
     239    // Trim away white space from the value. 
     240    trim(inValue);
     241
    210242    double value;
    211     value = strtod(inValue.c_str(),&endptr);
     243    value = strtod(inValue.c_str(), &endptr);
    212244    if (endptr == inValue.c_str()) {
    213245        // there was no numeric value that could be pulled from inValue
Note: See TracChangeset for help on using the changeset viewer.