How to determine Multibyte string length.

O

Owner

How to determine Multibyte string length?

So that I can allocate memory size equal to mulibyte string length.

I found there is mblen but it's only work for single character.

any help wouldbe appreciated
 
B

Barry Schwarz

How to determine Multibyte string length?

So that I can allocate memory size equal to mulibyte string length.

I found there is mblen but it's only work for single character.

any help wouldbe appreciated

Do the functions is section 7.24 of n1256, particularly wcslen, help?
 
B

Ben Bacarisse

Owner said:
How to determine Multibyte string length?

So that I can allocate memory size equal to mulibyte string length.

I found there is mblen but it's only work for single character.

any help wouldbe appreciated

The question is a little under specified -- in particular about what to
do when the string is malformed. Assuming that we can do anything at all
in this case one answer wold be to use the mbsrtowcs function (that's
"multi-byte string, restartable, to wide character string").

Apart from having rather too many parameters, the second must be a
pointer to a pointer so it really helps to wrap this function like this:

size_t mbslen(const char *s)
{
return mbsrtowcs(NULL, &s, 0, &(mbstate_t){0});
}

No doubt you have worked out that one could also just iterate calls to
mblen, but I am not sure there is anything to be gained from that
method.
 
B

BartC

Apart from having rather too many parameters, the second must be a
pointer to a pointer so it really helps to wrap this function like this:

size_t mbslen(const char *s)
{
return mbsrtowcs(NULL, &s, 0, &(mbstate_t){0});
}

No doubt you have worked out that one could also just iterate calls to
mblen, but I am not sure there is anything to be gained from that
method.

Would strlen()+1 work?
 
B

Ben Bacarisse

BartC said:
Would strlen()+1 work?

Yes, that might be what the OP needs which is why I said the question
was rather underspecified. The literal interpretation of the question
would have strlen(s) + 1 as the answer, but since that is a
comparatively obvious answer (known to anyone copying even plain
strings) I decided the OP might have been asking for the number of
characters represented.

In effect I assumed the clarification meant "so that I can allocate
memory size [for wide characters] equal to mulibyte string length". I
should have made that clear.
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top