473,795 Members | 3,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
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(co nst int_type& _C)
{return ((_E)_C); }
static int_type to_int_type(con st _E& _C)
{return ((int_type)(_C) ); }
static bool eq_int_type(con st 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_stri ng<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 5141
On 27 Oct 2006 22:52:28 -0700, "wolverine" <ki*********@gm ail.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<iostr eam>
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<XML Ch>
>{
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(co nst int_type& _C)
{return ((_E)_C); }
static int_type to_int_type(con st _E& _C)
{return ((int_type)(_C) ); }
static bool eq_int_type(con st 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_stri ng<unsigned short, unsigned_short_ traits>
utf16string;
typedef std::basic_stri ng<XMLChXMLstri ng;

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*********@gm ail.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<iostre am>
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<XML Ch>
{
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(co nst int_type& _C)
{return ((_E)_C); }
static int_type to_int_type(con st _E& _C)
{return ((int_type)(_C) ); }
static bool eq_int_type(con st 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_stri ng<unsigned short, unsigned_short_ traits>
utf16string;

typedef std::basic_stri ng<XMLChXMLstri ng;

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*********@gm ail.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*********@gm ail.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<iostre am>
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<XML Ch>
{
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(co nst int_type& _C)
{return ((_E)_C); }
static int_type to_int_type(con st _E& _C)
{return ((int_type)(_C) ); }
static bool eq_int_type(con st 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_stri ng<unsigned short, unsigned_short_ traits>
utf16string;

typedef std::basic_stri ng<XMLChXMLstri ng;

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*********@gm ail.com>
wrote:
typedef std::basic_stri ng<unsigned short, unsigned_short_ traits>
utf16string;

typedef std::basic_stri ng<XMLChXMLstri ng;

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_stri ng 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_stri ng 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.sub str( 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.beg in(), myXMLstring.end (), std::towupper
);
For our FOST.3 framework we went down a slightly different route. We
took the std::basic_stri ng 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_stri ng character types
for your needs you may also want to think about this alternative
implementation.

Of course if you use std::basic_stri ng 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
1570
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
2628
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 true - basic_string::npos + 1 == 0 ? Tnx Heinz
34
16693
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 - 3;
16
5144
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
20
5367
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 <stdlib.h>
1
7298
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: Creating library Release/fnimqcmd.lib and object Release/fnimqcmd.exp CoIMQCmd.obj : error LNK2001: unresolved external symbol
0
328
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 basic_string class. #include <string> #include<iostream> using namespace std;
0
1753
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 basic_string class. #include <string> #include<iostream> using namespace std;
10
5650
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; } else
5
3053
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 listen to your practical experiences. :-)
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.