1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_util_String_hpp
38 #define anna_core_util_String_hpp
44 #include <anna/core/util/defines.hpp>
51 * Clase String que optimiza el uso y la conversi�n de tipos usados habitualmente.
53 class String : public std::string {
56 * Indicadores que permiten ajustar el funcionamiento de la clase anna::String.
60 struct Flag { enum _v { None, ShowNull }; };
64 * \param flag Indicadores que permiten ajustar el funcionamiento de la clase anna::String.
66 String(const Flag::_v flag = Flag::None) : a_flags(flag) {;}
70 * \param str Cadena con la que iniciar el contenido de esta instancia. Puede ser NULL.
71 * \param flag Indicadores que permiten ajustar el funcionamiento de la clase anna::String.
73 explicit String(const char* str, const Flag::_v flag = Flag::None) :
74 std::string((str == NULL) ? ((flag & Flag::ShowNull) ? "<null>" : "") : str),
80 * \param other Instancia con la iniciar el contenido de esta instancia.
82 String(const String &other) : std::string(other), a_flags(other.a_flags) {;}
87 * \param other Instancia con la iniciar el contenido de esta instancia.
88 * \param flag Indicadores que permiten ajustar el funcionamiento de la clase anna::String.
90 explicit String(const std::string& other, const Flag::_v flag = Flag::None) : std::string(other), a_flags(flag) {;}
93 * Convierte a may�sculas el contenido de esta instancia.
95 void toUpper() throw();
98 * Convierte a min�sculas el contenido de esta instancia.
100 void toLower() throw();
102 String& operator = (const char* vv) throw() { std::string::clear(); return operator<< (vv); }
103 String& operator = (const int vv) throw() { std::string::clear(); return operator<< (vv); }
104 String& operator = (const unsigned int vv) throw() { std::string::clear(); return operator<< (vv); }
105 String& operator = (const bool vv) throw() { std::string::clear(); return operator<< (vv); }
106 String& operator = (const S64 vv) throw() { std::string::clear(); return operator<< (vv); }
107 String& operator = (const U64 vv) throw() { std::string::clear(); return operator<< (vv); }
108 String& operator = (const float vv) throw() { std::string::clear(); return operator<< (vv); }
109 String& operator = (const double vv) throw() { std::string::clear(); return operator<< (vv); }
110 String& operator = (const std::string& vv) throw() { std::string::operator= (vv); return *this; }
111 String& operator = (const DataBlock& vv) throw() { std::string::clear(); return operator<< (vv); }
113 String& operator += (const char* vv) throw() { return operator<< (vv); }
114 String& operator += (const int vv) throw() { return operator<< (vv); }
115 String& operator += (const unsigned int vv) throw() { return operator<< (vv); }
116 String& operator += (const bool vv) throw() { return operator<< (vv); }
117 String& operator += (const S64 vv) throw() { return operator<< (vv); }
118 String& operator += (const U64 vv) throw() { return operator<< (vv); }
119 String& operator += (const float vv) throw() { return operator<< (vv); }
120 String& operator += (const double vv) throw() { return operator<< (vv); }
121 String& operator += (const std::string& vv) throw() { return *this << vv; }
122 String& operator += (const DataBlock& vv) throw() { return *this << vv; }
124 String& operator << (const char* vv) throw();
125 String& operator << (const int vv) throw();
126 String& operator << (const unsigned int vv) throw();
127 String& operator << (const bool vv) throw() { std::string::operator+= ((vv == true) ? "true" : "false"); return *this; }
128 String& operator << (const S64 vv) throw();
129 String& operator << (const U64 vv) throw();
130 String& operator << (const float vv) throw();
131 String& operator << (const double vv) throw();
132 String& operator << (const std::string& vv) throw() { std::string::operator+= (vv); return *this; }
133 String& operator << (const DataBlock& vv) throw();
136 * Formatea el contenido del \em float con la cadena recibida como par�metro.
137 * La cadena de forma se interpreta de la misma forma que en el m�todo \em 'sprintf'
138 * \param vv Valor a interpretar.
139 * \param format Cadena usada para interpretar el valor (ver sprintf).
140 * \return La cadena con el contenido interpretado.
142 static String format(const float vv, const char* format) throw() { return __format(vv, format); }
145 * Formatea el contenido del \em double con la cadena recibida como par�metro.
146 * La cadena de forma se interpreta de la misma forma que en el m�todo \em 'sprintf'
147 * \param vv Valor a interpretar.
148 * \param format Cadena usada para interpretar el valor (ver sprintf).
149 * \return La cadena con el contenido interpretado.
151 static String format(const double vv, const char* format) throw() { return __format(vv, format); }
154 * Formatea el contenido del DataBlock con el n�mero de caracteres por l�nea recibida como par�metro.
155 * \param vv Valor a interpretar.
156 * \param characterByLine N�mero de caracters por l�nea.
157 * \return La cadena con el contenido interpretado.
159 static String format(const DataBlock& vv, const int characterByLine) throw();
162 * Formatea el valor del par�metro como un n�mero en hexadecimal.
163 * \param vv Valor a interpretar.
164 * \return La cadena con el contenido interpretado.
166 static String hex(const int vv) { return __format(vv, "0x%x"); }
169 * Formatea el valor del par�metro como un n�mero en hexadecimal.
170 * \param vv Valor a interpretar.
171 * \return La cadena con el contenido interpretado.
173 static String hex(const unsigned int vv) { return __format(vv, "0x%x"); }
176 * Formatea el valor del par�metro como un n�mero en hexadecimal.
177 * \param vv Valor a interpretar.
178 * \return La cadena con el contenido interpretado.
180 static String hex(const S64 vv) {
181 return __format(vv, "0x%llx");
183 return __format (vv, "0x%lx");
185 return __format (vv, "0x%llx");
193 template <typename T> static String __format(const T vv, const char* format) {
195 sprintf(aux, format, vv);