Changeset 1499


Ignore:
Timestamp:
Aug 27, 2012, 11:54:38 PM (9 years ago)
Author:
Peter Johansson
Message:

refs #519. Update to latest yat/utility.h. Limit the fetch to one file only as a full fetch seemed intrusive (and belong in trunk).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/0.9-stable/yat/utility.h

    r1490 r1499  
    22#define _theplu_yat_utility_utility_
    33
    4 // $Id: utility.h 2273 2010-06-15 02:11:47Z peter $
     4// $Id: utility.h 2673 2011-12-03 00:30:12Z peter $
    55
    66/*
     
    88  Copyright (C) 2006 Jari Häkkinen
    99  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    10   Copyright (C) 2009, 2010 Peter Johansson
     10  Copyright (C) 2009, 2010, 2011 Peter Johansson
    1111
    1212  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    3535#include "Exception.h"
    3636
     37#include <algorithm>
     38#include <cctype>
    3739#include <cmath>
     40#include <functional>
    3841#include <limits>
     42#include <locale>
    3943#include <istream>
    4044#include <string>
     
    6266                     OutputIterator result);
    6367
     68  /**
     69     \brief convert T to a string
     70
     71     T is supposed to be a numerical type.
     72
     73     \since new in yat 0.8
     74   */
     75  template<typename T>
     76  std::string convert(T input);
    6477
    6578  /**
     
    7285
    7386  /**
    74      \brief check if string is convertible to (numerical) type
     87     \brief check if string is convertible to (numerical) type \c T
    7588
    7689     \since New in yat 0.5
     
    259272
    260273  // template implementations
     274  template<typename T>
     275  std::string convert(T input)
     276  {
     277    std::ostringstream ss;
     278    ss << input;
     279    return ss.str();
     280  }
     281
     282
    261283  template<typename T>
    262284  T convert(const std::string& s)
     
    300322      if (rectangle && nof_columns && v.size()!=nof_columns) {
    301323        std::ostringstream s;
    302         s << "load data file error: "
     324        s << "load stream error: "
    303325          << "line " << matrix.size() << " has " << v.size()
    304326          << " columns; expected " << nof_columns << " columns.";
     
    313335
    314336  template<typename T>
    315   void load(std::istream& is, std::vector<T>& vec, char sep='\0')
     337  void load(std::istream& is, std::vector<T>& vec, char sep)
    316338  {
    317339    detail::VectorPusher<T> pusher;
     
    334356  bool convert(const std::string& s, T& result)
    335357  {
     358    if (!std::numeric_limits<T>::is_signed) {
     359      // first non-whitespace character
     360      std::string::const_iterator iter = s.begin();
     361      while (iter!=s.end() && std::isspace(*iter))
     362        ++iter;
     363      // unsigned int cannot start with a '-' and with some compilers
     364      // operation ss >> result won't fail so catch it like this instead.
     365      if (iter==s.end() || *iter=='-')
     366        return false;
     367    }
    336368    std::istringstream ss(s);
    337369    ss >> result;
Note: See TracChangeset for help on using the changeset viewer.