Espaços nominais
Variantes
Acções

std::mblen

Da cppreference.com
< cpp‎ | string‎ | multibyte

 
 
Biblioteca cordas
Strings terminadas
Original:
Null-terminated strings
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Cadeias de bytes
Multibyte cordas
Cordas de largura
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
basic_string
char_traits
 
Strings terminadas multibyte
Wide / multibyte conversões
Original:
Wide/multibyte conversions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
mbsinit
Tipos
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
mbstate_t
 
Defined in header <cstdlib>
int mblen( const char* s, std::size_t n );
Determina o tamanho, em bytes, do caráter de multibyte cujo primeiro byte é apontado por s.
Original:
Determines the size, in bytes, of the multibyte character whose first byte is pointed to by s.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Se s é um ponteiro nulo, redefine o estado de conversão global e determinado se as sequências de deslocamento são usados ​​.
Original:
If s is a null pointer, resets the global conversion state and determined whether shift sequences are used.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Esta função é equivalente à std::mbtowc((wchar_t*)0, s, n) chamada, com excepção de que a conversão do estado std::mbtowc não é afectada.
Original:
This function is equivalent to the call std::mbtowc((wchar_t*)0, s, n), except that conversion state of std::mbtowc is unaffected.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Índice

[editar] Notas

Cada chamada para atualizações mblen do estado conversão interna global (um objeto estático de std::mbstate_t tipo, conhecida apenas para esta função). Se a codificação multibyte usa os estados de mudança, os cuidados devem ser tomados para evitar varreduras retrocesso ou múltipla. Em todo o caso, vários segmentos não deve chamar mblen sem sincronização: std::mbrlen pode ser usado em vez.
Original:
Each call to mblen updates the internal global conversion state (a static object of type std::mbstate_t, only known to this function). If the multibyte encoding uses shift states, care must be taken to avoid backtracking or multiple scans. In any case, multiple threads should not call mblen without synchronization: std::mbrlen may be used instead.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Parâmetros

s -
ponteiro para o caractere multibyte
Original:
pointer to the multibyte character
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
n -
limitar o número de bytes em s que podem ser examinados
Original:
limit on the number of bytes in s that can be examined
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Valor de retorno

Se s não é um ponteiro nulo, retorna o número de bytes que estão contidos no caráter multibyte ou -1 se os primeiros bytes apontado por s não formam um caractere multibyte válido ou 0 se s está apontando para a charcter nulo '\0'.
Original:
If s is not a null pointer, returns the number of bytes that are contained in the multibyte character or -1 if the first bytes pointed to by s do not form a valid multibyte character or 0 if s is pointing at the null charcter '\0'.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Se s é um ponteiro nulo, redefine seu estado conversão interna para representar o estado deslocamento inicial e 0 retorna se a codificação multibyte atual não é dependente do estado (não usar seqüências de turnos) ou um valor diferente de zero se a codificação multibyte atual é estado-dependente (utiliza seqüências turnos).
Original:
If s is a null pointer, resets its internal conversion state to represent the initial shift state and returns 0 if the current multibyte encoding is not state-dependent (does not use shift sequences) or a non-zero value if the current multibyte encoding is state-dependent (uses shift sequences).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Exemplo

#include <clocale>
#include <string>
#include <iostream>
#include <cstdlib>
#include <stdexcept>
 
// the number of characters in a multibyte string is the sum of mblen()'s
// note: the simpler approach is std::mbstowcs(NULL, s.c_str(), s.size())
std::size_t strlen_mb(const std::string& s)
{
    std::size_t result = 0;
    const char* ptr = &s[0];
    const char* end = ptr + s.size();
    std::mblen(NULL, 0); // reset the conversion state
    while (ptr < end) {
        int next = std::mblen(ptr, end-ptr);
        if (next == -1) {
            throw std::runtime_error("strlen_mb(): conversion error");
        }
        ptr += next;
        ++result;
    }
    return result;
}
 
int main()
{
    // allow mblen() to work with UTF-8 multibyte encoding
    std::setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    std::string str = u8"z\u00df\u6c34\U0001d10b"; // or u8"zß水𝄋"
                      // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    std::cout << str << " is " << str.size() << " bytes, but only "
              << strlen_mb(str) << " characters\n";
}

Output:

zß水𝄋 is 10 bytes, but only 4 characters

[editar] Veja também

converte o caractere multibyte ao lado de caracteres largos
Original:
converts the next multibyte character to wide character
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(função) [edit]
retorna o número de bytes no caractere multibyte seguinte, determinado estado
Original:
returns the number of bytes in the next multibyte character, given state
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(função) [edit]
C documentation for mblen