X-Git-Url: https://git.llucax.com/z.facultad/75.42/string.git/blobdiff_plain/91ae14ba4499be590813ab84a75f5c027ac12c11..7bda03b390c8a38ec5a61386ae034e29bf87da4a:/unicode.cpp diff --git a/unicode.cpp b/unicode.cpp index f1a0ad7..85512f7 100644 --- a/unicode.cpp +++ b/unicode.cpp @@ -20,7 +20,7 @@ # include #endif -Unicode::Unicode(short c = 0): caracter(c) { +Unicode::Unicode(short c): caracter(c) { #ifdef DEBUG std::cerr << "En constructor de Unicode." << std::endl; #endif @@ -46,40 +46,50 @@ Unicode& Unicode::operator=(const Unicode& unicode) { return *this; } -bool Unicode::operator<(const Unicode& unicode) { +bool Unicode::operator<(const Unicode& unicode) const { #ifdef DEBUG std::cerr << "En operator< de Unicode." << std::endl; #endif return caracter < unicode.caracter; } -bool Unicode::operator==(const Unicode& unicode) { +bool Unicode::operator==(const Unicode& unicode) const { #ifdef DEBUG std::cerr << "En operator== de Unicode." << std::endl; #endif return caracter == unicode.caracter; } -char Unicode::operator char(void) { +Unicode::operator char(void) const { #ifdef DEBUG std::cerr << "En cast de Unicode a char." << std::endl; #endif return static_cast(caracter); } -/// Volcado a un stream de salida. std::ostream& operator<<(std::ostream& out, const Unicode& unicode) { #ifdef DEBUG std::cerr << "En operator<< de Unicode." << std::endl; #endif - return out << unicode.caracter; + // Imprime el caracter como 2 caracteres ASCII. + // Si el primer caracter (bits mas significativos) es cero, entonces no lo + // imprime (es ASCII "puro"). + if (unicode.caracter >> 8) { + out << static_cast(unicode.caracter >> 8); + } + return out << static_cast(unicode.caracter); } -/// Captura desde un stream de entrada. -std::istream& operator>>(std::istream& in, const Unicode& unicode) { +std::istream& operator>>(std::istream& in, Unicode& unicode) { #ifdef DEBUG std::cerr << "En operator>> de Unicode." << std::endl; #endif - return in >> unicode.caracter; + char c; + // Lee el primer caracter ASCII. + in >> c; + // Lo asigno como un short cuyos bits menos significativos son los del + // caracter leĆ­do. + unicode.caracter = static_cast(c); + return in; }