M
Michael B Allen
Hello,
Early on I decided that all text (what most people call "strings" [1])
in my code would be unsigned char *. The reasoning is that the elements
of these arrays are decidedly not signed. In fact, they may not even
represent complete characters. At this point I think of text as simple
binary blobs. What charset, character encoding and termination they use
should not be exposed in the interface used to operate on them.
But now I have a dilemma. C string literals are signed char *. With GCC
4 warning about every sign mismatch, my code is spewing warnings all
over the place and I'm trying to figure out what to do about it.
My current thought is to define a Windows style _T macro:
#define _T(s) ((unsigned char *)s)
Use "text" functions like:
int
text_copy(const unsigned char *src, unsigned char *dst, int n)
{
while (n-- && *src) {
*dst++ = *src++;
...
And abolish the use of traditional string functions (at least for "text").
The code might then look like the following:
unsigned char buf[255];
text_copy(_T("hello, world"), buf, sizeof(buf));
What do you think?
If I do the above I have a lot of work to do so if someone has a better
idea I'd really like to hear about it.
Mike
PS: If you have an opinion that is unfavorable (but professional) let's
hear it.
[1] I use the term "text" to mean stuff that may actually be displayed
to a user (possibly in a foreign country). I use the term "string"
to represent traditional 8 bit zero terminated char * arrays.
Early on I decided that all text (what most people call "strings" [1])
in my code would be unsigned char *. The reasoning is that the elements
of these arrays are decidedly not signed. In fact, they may not even
represent complete characters. At this point I think of text as simple
binary blobs. What charset, character encoding and termination they use
should not be exposed in the interface used to operate on them.
But now I have a dilemma. C string literals are signed char *. With GCC
4 warning about every sign mismatch, my code is spewing warnings all
over the place and I'm trying to figure out what to do about it.
My current thought is to define a Windows style _T macro:
#define _T(s) ((unsigned char *)s)
Use "text" functions like:
int
text_copy(const unsigned char *src, unsigned char *dst, int n)
{
while (n-- && *src) {
*dst++ = *src++;
...
And abolish the use of traditional string functions (at least for "text").
The code might then look like the following:
unsigned char buf[255];
text_copy(_T("hello, world"), buf, sizeof(buf));
What do you think?
If I do the above I have a lot of work to do so if someone has a better
idea I'd really like to hear about it.
Mike
PS: If you have an opinion that is unfavorable (but professional) let's
hear it.
[1] I use the term "text" to mean stuff that may actually be displayed
to a user (possibly in a foreign country). I use the term "string"
to represent traditional 8 bit zero terminated char * arrays.