473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

basic_string with unsigned short

Hi
I want to know how to use basic_string with unsigned short (I have
mentioned below why i have to do this). Could any tell me some good
references in this topic. I am new to creating a new basic_string
class.

#include <string>
#include<iostream>
using namespace std;

struct unsigned_short_traits
{
typedef unsigned short _E;
typedef _E char_type;
typedef int int_type;
typedef std::streampos pos_type;
typedef std::streamoff off_type;
typedef std::mbstate_t state_type;
static void assign(_E& _X, const _E& _Y)
{_X = _Y; }
static bool eq(const _E& _X, const _E& _Y)
{return (_X == _Y); }
static bool lt(const _E& _X, const _E& _Y)
{return (_X < _Y); }
static int compare(const _E *_U, const _E *_V, size_t _N)
{return (memcmp(_U, _V, _N)); }
static size_t length(const _E *_U)
{return (strlen((const char *)_U)); }
static _E * copy(_E *_U, const _E *_V, size_t _N)
{return ((_E *)memcpy(_U, _V, _N)); }
static const _E * find(const _E *_U, size_t _N, const _E& _C)
{return ((const _E *)memchr(_U, _C, _N)); }
static _E * move(_E *_U, const _E *_V, size_t _N)
{return ((_E *)memmove(_U, _V, _N)); }
static _E * assign(_E *_U, size_t _N, const _E& _C)
{return ((_E *)memset(_U, _C, _N)); }
static _E to_char_type(const int_type& _C)
{return ((_E)_C); }
static int_type to_int_type(const _E& _C)
{return ((int_type)(_C)); }
static bool eq_int_type(const int_type& _X, const int_type& _Y)
{return (_X == _Y); }
static int_type eof()
{return (EOF); }
static int_type not_eof(const int_type& _C)
{return (_C != eof() ? _C : !eof()); }
};

typedef std::basic_string<unsigned short, unsigned_short_traits>
utf16string;

int main()
{
char *a = "abc";
utf16string str(reinterpret_cast<unsigned short*>(a));
cout<<str<<endl;
return 0;
}

REASON TO CREATE THIS utf16string

I am using xerces parser which uses a XMLCh ( typedef unsigned short
XMLCh) as the basic character. Most of xerces functions have XMLCh
pointers as input. But since my application has to be unicode supported
and at the same time i cannot use std::wstring I cannot use
std::wstring since wchar_t is 32 bit in linux and XMLCh is 16 bit. So
conversion between std::wstring and XMLCh will not work. So i thought
of defining basic_string with unsigned short.
I know this group is not for solving issues in c++ isses regarding any
platform (linux). But i am just asking how to use basic_string with
unsigned char.

Thanks in Advance
Kiran.

Oct 28 '06 #1
5 5078
On 27 Oct 2006 22:52:28 -0700, "wolverine" <ki*********@gmail.com>
wrote:
I want to know how to use basic_string with unsigned short (I have
mentioned below why i have to do this). Could any tell me some good
references in this topic. I am new to creating a new basic_string
class.

#include <string>
#include<iostream>
using namespace std;
there should be no 'using namespace ...' in header files
>struct unsigned_short_traits
Specialize the char_traits template

template<>
struct char_traits<XMLCh>
>{
typedef unsigned short _E;
typedef _E char_type;
typedef int int_type;
typedef std::streampos pos_type;
typedef std::streamoff off_type;
// may not be correct
> typedef std::mbstate_t state_type;
static void assign(_E& _X, const _E& _Y)
{_X = _Y; }
static bool eq(const _E& _X, const _E& _Y)
{return (_X == _Y); }
static bool lt(const _E& _X, const _E& _Y)
{return (_X < _Y); }
static int compare(const _E *_U, const _E *_V, size_t _N)
{return (memcmp(_U, _V, _N)); }
static size_t length(const _E *_U)
{return (strlen((const char *)_U)); }
// strlen doesn't work for unsigned long
> static _E * copy(_E *_U, const _E *_V, size_t _N)
{return ((_E *)memcpy(_U, _V, _N)); }
static const _E * find(const _E *_U, size_t _N, const _E& _C)
{return ((const _E *)memchr(_U, _C, _N)); }
static _E * move(_E *_U, const _E *_V, size_t _N)
{return ((_E *)memmove(_U, _V, _N)); }
static _E * assign(_E *_U, size_t _N, const _E& _C)
{return ((_E *)memset(_U, _C, _N)); }
static _E to_char_type(const int_type& _C)
{return ((_E)_C); }
static int_type to_int_type(const _E& _C)
{return ((int_type)(_C)); }
static bool eq_int_type(const int_type& _X, const int_type& _Y)
{return (_X == _Y); }
static int_type eof()
{return (EOF); }
static int_type not_eof(const int_type& _C)
{return (_C != eof() ? _C : !eof()); }
};
You need to test those traits. Probably there are some more issues
with them.
>typedef std::basic_string<unsigned short, unsigned_short_traits>
utf16string;
typedef std::basic_string<XMLChXMLstring;

It's not an UTF16 string because characters in UTF16 can be longer
then unsigned long.
>int main()
{
char *a = "abc";
utf16string str(reinterpret_cast<unsigned short*>(a));
Oops, you need to write a function, that converts char* into XMLCh*
(probably available in Xerces).
cout<<str<<endl;
cout is for char. It doesn't work with XMLCh.
return 0;
}

REASON TO CREATE THIS utf16string

I am using xerces parser which uses a XMLCh ( typedef unsigned short
XMLCh) as the basic character.
Best wishes,
Roland Pibinger
Oct 28 '06 #2
Hi
Thanks for your reply. It helped. Thanks for spending your valuable
time in helping novices like me.

Thanks
Kiran Pradeep

Roland Pibinger wrote:
On 27 Oct 2006 22:52:28 -0700, "wolverine" <ki*********@gmail.com>
wrote:
I want to know how to use basic_string with unsigned short (I have
mentioned below why i have to do this). Could any tell me some good
references in this topic. I am new to creating a new basic_string
class.

#include <string>
#include<iostream>
using namespace std;

there should be no 'using namespace ...' in header files
struct unsigned_short_traits

Specialize the char_traits template

template<>
struct char_traits<XMLCh>
{
typedef unsigned short _E;
typedef _E char_type;
typedef int int_type;
typedef std::streampos pos_type;
typedef std::streamoff off_type;

// may not be correct
typedef std::mbstate_t state_type;
static void assign(_E& _X, const _E& _Y)
{_X = _Y; }
static bool eq(const _E& _X, const _E& _Y)
{return (_X == _Y); }
static bool lt(const _E& _X, const _E& _Y)
{return (_X < _Y); }
static int compare(const _E *_U, const _E *_V, size_t _N)
{return (memcmp(_U, _V, _N)); }
static size_t length(const _E *_U)
{return (strlen((const char *)_U)); }

// strlen doesn't work for unsigned long
static _E * copy(_E *_U, const _E *_V, size_t _N)
{return ((_E *)memcpy(_U, _V, _N)); }
static const _E * find(const _E *_U, size_t _N, const _E& _C)
{return ((const _E *)memchr(_U, _C, _N)); }
static _E * move(_E *_U, const _E *_V, size_t _N)
{return ((_E *)memmove(_U, _V, _N)); }
static _E * assign(_E *_U, size_t _N, const _E& _C)
{return ((_E *)memset(_U, _C, _N)); }
static _E to_char_type(const int_type& _C)
{return ((_E)_C); }
static int_type to_int_type(const _E& _C)
{return ((int_type)(_C)); }
static bool eq_int_type(const int_type& _X, const int_type& _Y)
{return (_X == _Y); }
static int_type eof()
{return (EOF); }
static int_type not_eof(const int_type& _C)
{return (_C != eof() ? _C : !eof()); }
};

You need to test those traits. Probably there are some more issues
with them.
typedef std::basic_string<unsigned short, unsigned_short_traits>
utf16string;

typedef std::basic_string<XMLChXMLstring;

It's not an UTF16 string because characters in UTF16 can be longer
then unsigned long.
int main()
{
char *a = "abc";
utf16string str(reinterpret_cast<unsigned short*>(a));

Oops, you need to write a function, that converts char* into XMLCh*
(probably available in Xerces).
cout<<str<<endl;

cout is for char. It doesn't work with XMLCh.
return 0;
}

REASON TO CREATE THIS utf16string

I am using xerces parser which uses a XMLCh ( typedef unsigned short
XMLCh) as the basic character.

Best wishes,
Roland Pibinger
Oct 28 '06 #3
On 28 Oct 2006 04:51:08 -0700, "wolverine" <ki*********@gmail.com>
wrote:
Thanks for your reply. It helped. Thanks for spending your valuable
time in helping novices like me.
I forgot to mention that since the char_traits template is in
namespace std also the specialization needs to be in namespace std.
This looks strange because normally you are not allowed to put
anything into std. Alternatively you can define your traits as 'struct
unsigned_short_traits' and provide it as a second template paramerer
as you have done in your code. Actually I'd prefer your solution.

Best wishes,
Roland Pibinger
Oct 28 '06 #4
Hi
Thanks for your reply. It helped. Thanks for spending your valuable
time in helping novices like me.

Thanks
Kiran Pradeep

Roland Pibinger wrote:
On 27 Oct 2006 22:52:28 -0700, "wolverine" <ki*********@gmail.com>
wrote:
I want to know how to use basic_string with unsigned short (I have
mentioned below why i have to do this). Could any tell me some good
references in this topic. I am new to creating a new basic_string
class.

#include <string>
#include<iostream>
using namespace std;

there should be no 'using namespace ...' in header files
struct unsigned_short_traits

Specialize the char_traits template

template<>
struct char_traits<XMLCh>
{
typedef unsigned short _E;
typedef _E char_type;
typedef int int_type;
typedef std::streampos pos_type;
typedef std::streamoff off_type;

// may not be correct
typedef std::mbstate_t state_type;
static void assign(_E& _X, const _E& _Y)
{_X = _Y; }
static bool eq(const _E& _X, const _E& _Y)
{return (_X == _Y); }
static bool lt(const _E& _X, const _E& _Y)
{return (_X < _Y); }
static int compare(const _E *_U, const _E *_V, size_t _N)
{return (memcmp(_U, _V, _N)); }
static size_t length(const _E *_U)
{return (strlen((const char *)_U)); }

// strlen doesn't work for unsigned long
static _E * copy(_E *_U, const _E *_V, size_t _N)
{return ((_E *)memcpy(_U, _V, _N)); }
static const _E * find(const _E *_U, size_t _N, const _E& _C)
{return ((const _E *)memchr(_U, _C, _N)); }
static _E * move(_E *_U, const _E *_V, size_t _N)
{return ((_E *)memmove(_U, _V, _N)); }
static _E * assign(_E *_U, size_t _N, const _E& _C)
{return ((_E *)memset(_U, _C, _N)); }
static _E to_char_type(const int_type& _C)
{return ((_E)_C); }
static int_type to_int_type(const _E& _C)
{return ((int_type)(_C)); }
static bool eq_int_type(const int_type& _X, const int_type& _Y)
{return (_X == _Y); }
static int_type eof()
{return (EOF); }
static int_type not_eof(const int_type& _C)
{return (_C != eof() ? _C : !eof()); }
};

You need to test those traits. Probably there are some more issues
with them.
typedef std::basic_string<unsigned short, unsigned_short_traits>
utf16string;

typedef std::basic_string<XMLChXMLstring;

It's not an UTF16 string because characters in UTF16 can be longer
then unsigned long.
int main()
{
char *a = "abc";
utf16string str(reinterpret_cast<unsigned short*>(a));

Oops, you need to write a function, that converts char* into XMLCh*
(probably available in Xerces).
cout<<str<<endl;

cout is for char. It doesn't work with XMLCh.
return 0;
}

REASON TO CREATE THIS utf16string

I am using xerces parser which uses a XMLCh ( typedef unsigned short
XMLCh) as the basic character.

Best wishes,
Roland Pibinger
Oct 28 '06 #5

Roland Pibinger wrote:
On 27 Oct 2006 22:52:28 -0700, "wolverine" <ki*********@gmail.com>
wrote:
typedef std::basic_string<unsigned short, unsigned_short_traits>
utf16string;

typedef std::basic_string<XMLChXMLstring;

It's not an UTF16 string because characters in UTF16 can be longer
then unsigned long.
Might be worth clarifying this a little - I'm not totally sure what
point you're trying to make, but it's probably the following.

The semantics of std::basic_string are such that they assume each
character type (XMLCh in your case) holds one character. Of course for
UTF-16 sequences this isn't true.

Now this doesn't mean that you can't use std::basic_string in this
circumstance, but it does mean that you must be careful what operations
you perform on it. For example, parsing the string is Ok and chopping
the string at a boundary you find by looking for certain characters is
safe.

What isn't safe though is things like:

myXMLstring.substr( 0, 100 );

This may chop the last character (give you only one half of a surrogate
pair) and in any case will return you a string with between 100 and 50
Unicode characters. You will also have some dissonance with iterators
and operators like [] and the member at().

Things like this are deeply suspicous:

std::transform( myXMLstring.begin(), myXMLstring.end(), std::towupper
);
For our FOST.3 framework we went down a slightly different route. We
took the std::basic_string and copied the concepts of the interface,
but used UTF-16 sequences for all character sequences and UTF-32 for
all character operations.

So there is a constructor that does wstring( const wchar_t * ) and
another which is wstring( size_type, utf32 ), using utf32 as a typedef
for a 32 bit int. Dereferencing an iterator returns a UTF-32 character
as does at() and operator [], but c_str() returns a UTF-16 sequence.
size() and length() both return the number of UTF-32 characters (there
are seperate members for fetching the UTF-16 length).

This seems to give us the best of both worlds - normal UTF-16 handling
for the Windows API, but correct Unicode handling for string
operations.

If you are currently exploring new std::basic_string character types
for your needs you may also want to think about this alternative
implementation.

Of course if you use std::basic_string with a UTF-32 character type
then all this complication goes away because there the one-to-one
mapping is maintained.
K

Oct 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rémi Peyronnet | last post by:
Hello, I have some link problems while using basic_string with another type (unsigned int in my case) This is a sample source : " #include <string>
7
by: Heinz Ozwirk | last post by:
Does the standard require that 1. the type of basic_string::npos is an unsigned type? 2. the value of basic_string::npos is the largest possible value of that type? And - only if both are...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
1
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error:...
0
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
0
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
5
by: George2 | last post by:
Hello everyone, I would like to learn some experiences about when should we use std::basic_string and when should we use std::string? I learned some Hello World level samples and now want to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.