STK++ 0.9.13
STK_String.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: STKernel::Base
27 * Purpose: Define the fundamental type String.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
36#ifndef STK_STRING_H
37#define STK_STRING_H
38
39#include <ctype.h>
40#include <algorithm>
41
42namespace STK
43{
50template<>
51struct Arithmetic<String>: public std::numeric_limits<String>
52{
54 static String NA() throw() { return String();}
56 static const bool hasNA = true;
61 static bool isNA(String const& x) throw() { return (x == NA());}
66 static bool isInfinite(String const& x) throw() { return false; }
71 static bool isFinite(String const& x) throw() { return (!isNA(x));}
72};
73
79template<>
81{
84};
85
86#ifndef IS_RTKPP_LIB
96extern String stringNa;
97
103extern int stringNaSize;
104
109inline void setStringNa(String const& na)
110{ stringNa = na; stringNaSize = na.size();}
111
112#else /* is rtkpp lib */
113
120static const String stringNa = _T("NA");
124static const int stringNaSize = 2;
125
126#endif
127
134inline String const& toUpperString( String& s)
135{
136 // iterate along the String
137 for (String::iterator it = s.begin(); it != s.end(); ++it)
138 { *it = std::toupper(*it);}
139 // return upper cased string
140 return s;
141}
142
149inline String toUpperString( String const& s)
150{
151 // iterate along the String
152 String str = s;
153 for (String::iterator it = str.begin(); it != str.end(); ++it)
154 { *it = std::toupper(*it);}
155 // return upper cased string
156 return str;
157}
158
166{
167 // iterate along the String
168 String str = s;
169 str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
170 return str;
171}
172
173}
174
175#include "STK_Proxy.h"
176
177namespace STK
178{
194template <class Type>
195bool stringToType( Type &t, String const& s
196 , std::ios_base& (*f)(std::ios_base&) = std::dec
197 )
198{ istringstream iss(s);
199 return !( iss >> f >> Proxy<Type>(t)).fail();
200}
201
215template <class Type>
216Type stringToType( String const& s)
217{
218 Type t;
220 iss >> Proxy<Type>(t);
221 return(t);
222}
223
234template <class Type>
235String typeToString( Type const& t, std::ios_base& (*f)(std::ios_base&) = std::dec)
236{
237 if (Arithmetic<Type>::isNA(t)) return stringNa;
239 return static_cast<ostringstream&>(oss << f << Proxy<Type>(t)).str();
240}
241
242} // namespace STK
243
244#endif /*STK_STRING_H*/
In this file we define the proxy classes for wrapping the STKpp fundamental types.
#define _T(x)
Let x unmodified.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
bool stringToType(Type &t, String const &s, std::ios_base &(*f)(std::ios_base &)=std::dec)
convert a String to Type
Definition STK_String.h:195
void setStringNa(String const &na)
Set a new value to the na String representation and modify stringNaSize accordingly.
Definition STK_String.h:109
int stringNaSize
Size (in number of Char) of a Not Available value.
String const & toUpperString(String &s)
convert the characters of the String to upper case
Definition STK_String.h:134
String removeWhiteSpaces(String const &s)
convert the characters of the String to upper case
Definition STK_String.h:165
String typeToString(Type const &t, std::ios_base &(*f)(std::ios_base &)=std::dec)
convert a Type to String
Definition STK_String.h:235
String stringNa
Representation of a Not Available value.
IdType
Id for the Type of a variable.
@ string_
String type.
std::basic_string< Char > String
STK fundamental type of a String.
std::basic_ostringstream< Char > ostringstream
ostringstream (ostringstream) for Char
Definition STK_Stream.h:65
std::basic_istringstream< Char > istringstream
istringstream for Char
Definition STK_Stream.h:63
The namespace STK is the main domain space of the Statistical ToolKit project.
static String NA()
Adding a Non Available (NA) special String (the empty String)
Definition STK_String.h:54
static bool isInfinite(String const &x)
Test if x is infinite.
Definition STK_String.h:66
static bool isNA(String const &x)
Test if x is a Non Available (NA) String.
Definition STK_String.h:61
static bool isFinite(String const &x)
Test if x is finite.
Definition STK_String.h:71
Arithmetic properties of STK fundamental types.
static const bool hasNA
True if the type has a representation for a "Not Available."
static Type NA()
Adding a Non Available (NA) special number.
static bool isNA(Type const &x)
static Base::IdType returnType()
Definition STK_String.h:83
Implementation of the return type.