Click here to Skip to main content
16,017,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have methods that currently take const char* and an encoding. Like so:
C++
foo(const char* text,text_encoding::utf8)

That's fine for utf8 and latin1, etc
But what if you want utf16 (wchar_t?) or utf32 (no intrinsic type, but i use int32_t)

What if there are others, like some kind of binary businesses that's utf8? My encoders don't care - they take a void*

Should I take a const void* here? I really hate it. But I also hate the idea of limiting the types it can be via a union because I just don't know what types of encodings people may want to use down the road.

I mean, I could do like typedef void* encoded_text_t; (I don't like that name, but I'd take suggestions).

Does anyone have a better idea?

Keep in mind, the STL is off limits to me. I'm on embedded.

What I have tried:

not really applicable here.

I've put everything in the first box.
Posted
Comments
PIEBALDconsult 16-Aug-24 14:00pm    
No matter what you do, someone will still try to pass you a wombat. :D
honey the codewitch 16-Aug-24 14:03pm    
Right? If this was LVGL I'd just take void* and call it a day, because that's how they handle every situation like this, but that's straight C, not C++, and also it squicks me out. I just don't like taking void* for what is essentially text. I could live with typedefing it if there are no other good options, but I need a name. text_t? nah. string_t? too confusing (std::string exists) meh.

To store text that can be any charset in C++, you should use std::string.
This data type is designed to handle strings more flexibly compared to the char data type which can only store one character.
 
Share this answer
 
Comments
honey the codewitch 17-Aug-24 10:22am    
A) Keep in mind, the STL is off limits to me. I'm on embedded.

C) can std::string produce utf32 codepoints? No it cannot

D) Just no
Richard MacCutchan 17-Aug-24 10:24am    
OP clearly stated that she cannot use STL.
honey the codewitch 17-Aug-24 11:05am    
I've settled on the typedef for now. It's less than ideal. I'm considering a union, but that gets weirder to use than a cast. A void pointer theoretically works, and obviates the need for a cast when passing in the argument - I just hate it.
Richard MacCutchan 17-Aug-24 11:12am    
Do you ever get that feeling that just as you reach what you thought was the end of the rabbit hole, there is a bend which leads down into deeper darkness? :(
honey the codewitch 17-Aug-24 11:12am    
I'm literally right there now, and I just posted in the lounge about it. LOL
Depending on your library and compiler you should use some macro for the character identifier. Microsoft is using therefor the
C++
TCHAR
macro which can resolve to char or wchar_t depending on the compiler settings.
That need some change in most string functions like using
C++
_tcscpy
instead of strcpy.

So some string library which can handle it would be very handy.
 
Share this answer
 
Comments
honey the codewitch 17-Aug-24 16:42pm    
oof. I'm trying to keep things C++ wherever possible. I do use the preprocessor, but seldomly. I could use a macro but I don't think that puts me in a better position than a typedef, TBH.
KarstenK 19-Aug-24 3:42am    
this is some typedef but it also is some typedef for the API like the strcpy. So it works!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900