How to use the "unsigned" representation of a number

F

FabioAng

Assuming I have this function (it's not complete):

template<typename InputType, typename OutputIterator>
void to_utf8(InputType input, OutputIterator result)
{
// trivial conversion
if (input < (1 << 7)) {
*result = input; ++result;
}else {
*result = ((input >> 6) | 0xc0); ++result;
*result = ((input & 0x3f) | 0x80); ++result;
}
}

Everything works fine if InputType is "unsigned" but it could not work if
InputType is "signed".
This problem can be generalized in every template function where a numbering
compare is applied.

template<typename InputType>
bool is_valid_xml_range(InputType input)
{
if ( ( input >= 0x20 ) && ( input <= 0xD7FF ) )
return true;
else
return false;
}

Is there a way to use always the "unsigned" representation, e.g.

template<typename InputType>
bool is_valid_xml_range(InputType input)
{
UnsignedInputType uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}

Regards,
Fabio
 
M

Michael DOUBEZ

FabioAng a écrit :
Assuming I have this function (it's not complete):

template<typename InputType, typename OutputIterator>
void to_utf8(InputType input, OutputIterator result)
{
// trivial conversion
if (input < (1 << 7)) {
*result = input; ++result;
}else {
*result = ((input >> 6) | 0xc0); ++result;
*result = ((input & 0x3f) | 0x80); ++result;
}
}

Everything works fine if InputType is "unsigned" but it could not work if
InputType is "signed".
This problem can be generalized in every template function where a numbering
compare is applied.
[snip]

Is there a way to use always the "unsigned" representation, e.g.
[snip]

What you want to known is in fact if there is ones above 7th bit:
if( (input>>7) != 0 ) ...

Michael
 
F

FabioAng

Michael DOUBEZ said:
What you want to known is in fact if there is ones above 7th bit:
if( (input>>7) != 0 ) ...

Michael

I would like to solve the generic problem not the only one I posted

Fabio
 
S

Sylvester Hesp

FabioAng said:
Is there a way to use always the "unsigned" representation, e.g.

template<typename InputType>
bool is_valid_xml_range(InputType input)
{
UnsignedInputType uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}

You can do it via template specialization

template<class T> struct unsigned_type { typedef T type; };
template<> struct unsigned_type<char> { typedef unsigned char type; };
template<> struct unsigned_type<signed char> { typedef unsigned char
type; };
template<> struct unsigned_type<short> { typedef unsigned short type; };
template<> struct unsigned_type<int> { typedef unsigned int type; };
template<> struct unsigned_type<long> { typedef unsigned long type; };


template<typename InputType>
bool is_valid_xml_range(InputType input)
{
typedef unsigned_type<InputType>::type UnsignedInputType;
UnsignedInputType uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}


- Sylvester
 
F

FabioAng

I found a solution in an old boost thread

#include <boost/type_traits.hpp>
#include <boost/integer.hpp>

namespace detail
{
template <class T, bool is_signed>
struct remove_signedness_helper
{
typedef T type;
};

template <class T>
struct remove_signedness_helper<T, true>
{
typedef typename boost::uint_t<sizeof(T) * 8>::least type;
};
}

template <class T>
struct remove_signedness
{
typedef typename
detail::remove_signedness_helper<T,boost::is_signed<T>::value>::type type;
};

template<typename InputType, typename OutputIterator>
void to_utf8(InputType input, OutputIterator result)
{
remove_signedness<InputType>::type uinput;
uinput = input;
// trivial conversion
if (uinput < (1 << 7)) {
*result = uinput; ++result;
} else {
*result = ((uinput >> 6) | 0xc0); ++result;
*result = ((uinput & 0x3f) | 0x80); ++result;
}
}

Is there anybody who sees something wrong in this piece if code ?

Regards
Fabio
 
F

FabioAng

Sylvester Hesp said:
You can do it via template specialization

template<class T> struct unsigned_type { typedef T type; };
template<> struct unsigned_type<char> { typedef unsigned char type; };
template<> struct unsigned_type<signed char> { typedef unsigned char
type; };
template<> struct unsigned_type<short> { typedef unsigned short type; };
template<> struct unsigned_type<int> { typedef unsigned int type; };
template<> struct unsigned_type<long> { typedef unsigned long type; };


template<typename InputType>
bool is_valid_xml_range(InputType input)
{
typedef unsigned_type<InputType>::type UnsignedInputType;
UnsignedInputType uinput = input;

if ( ( uinput >= 0x20 ) && ( uinput <= 0xD7FF ) )
return true;
else
return false;
}


- Sylvester

Great idea...the same which I found using boost library

Thanks
Fabio
 
G

Gianni Mariani

FabioAng said:
I found a solution in an old boost thread
....
Is there anybody who sees something wrong in this piece if code ?

It's wrong ? It only works for is8859-1 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top