NumberIstream< T > Class Template Reference
Collaboration diagram for NumberIstream< T >:

Public Member Functions

 NumberIstream (std::istream &i)
 
NumberIstreamoperator>> (T &x)
 

Private Member Functions

bool RemainderIsOnlySpaces ()
 
NumberIstreamParseOnFail (T *x)
 

Private Attributes

std::istream & in_
 

Detailed Description

template<class T>
class kaldi::NumberIstream< T >

Definition at line 166 of file text-utils.cc.

Constructor & Destructor Documentation

◆ NumberIstream()

NumberIstream ( std::istream &  i)
inlineexplicit

Definition at line 168 of file text-utils.cc.

168 : in_(i) {}
std::istream & in_
Definition: text-utils.cc:178

Member Function Documentation

◆ operator>>()

NumberIstream& operator>> ( T &  x)
inline

Definition at line 170 of file text-utils.cc.

170  {
171  if (!in_.good()) return *this;
172  in_ >> x;
173  if (!in_.fail() && RemainderIsOnlySpaces()) return *this;
174  return ParseOnFail(&x);
175  }
bool RemainderIsOnlySpaces()
Definition: text-utils.cc:180
std::istream & in_
Definition: text-utils.cc:178
NumberIstream & ParseOnFail(T *x)
Definition: text-utils.cc:195

◆ ParseOnFail()

NumberIstream& ParseOnFail ( T *  x)
inlineprivate

Definition at line 195 of file text-utils.cc.

195  {
196  std::string str;
197  in_.clear();
198  in_.seekg(0);
199  // If the stream is broken even before trying
200  // to read from it or if there are many tokens,
201  // it's pointless to try.
202  if (!(in_ >> str) || !RemainderIsOnlySpaces()) {
203  in_.setstate(std::ios_base::failbit);
204  return *this;
205  }
206 
207  std::map<std::string, T> inf_nan_map;
208  // we'll keep just uppercase values.
209  inf_nan_map["INF"] = std::numeric_limits<T>::infinity();
210  inf_nan_map["+INF"] = std::numeric_limits<T>::infinity();
211  inf_nan_map["-INF"] = - std::numeric_limits<T>::infinity();
212  inf_nan_map["INFINITY"] = std::numeric_limits<T>::infinity();
213  inf_nan_map["+INFINITY"] = std::numeric_limits<T>::infinity();
214  inf_nan_map["-INFINITY"] = - std::numeric_limits<T>::infinity();
215  inf_nan_map["NAN"] = std::numeric_limits<T>::quiet_NaN();
216  inf_nan_map["+NAN"] = std::numeric_limits<T>::quiet_NaN();
217  inf_nan_map["-NAN"] = - std::numeric_limits<T>::quiet_NaN();
218  // MSVC
219  inf_nan_map["1.#INF"] = std::numeric_limits<T>::infinity();
220  inf_nan_map["-1.#INF"] = - std::numeric_limits<T>::infinity();
221  inf_nan_map["1.#QNAN"] = std::numeric_limits<T>::quiet_NaN();
222  inf_nan_map["-1.#QNAN"] = - std::numeric_limits<T>::quiet_NaN();
223 
224  std::transform(str.begin(), str.end(), str.begin(), ::toupper);
225 
226  if (inf_nan_map.find(str) != inf_nan_map.end()) {
227  *x = inf_nan_map[str];
228  } else {
229  in_.setstate(std::ios_base::failbit);
230  }
231 
232  return *this;
233  }
bool RemainderIsOnlySpaces()
Definition: text-utils.cc:180
std::istream & in_
Definition: text-utils.cc:178

◆ RemainderIsOnlySpaces()

bool RemainderIsOnlySpaces ( )
inlineprivate

Definition at line 180 of file text-utils.cc.

180  {
181  if (in_.tellg() != std::istream::pos_type(-1)) {
182  std::string rem;
183  in_ >> rem;
184 
185  if (rem.find_first_not_of(' ') != std::string::npos) {
186  // there is not only spaces
187  return false;
188  }
189  }
190 
191  in_.clear();
192  return true;
193  }
std::istream & in_
Definition: text-utils.cc:178

Member Data Documentation

◆ in_

std::istream& in_
private

Definition at line 178 of file text-utils.cc.


The documentation for this class was generated from the following file: