What do you think of a symbol with a multibite line?



  • There's a multibite line, the length of its measures in the megabytes, the cycle needs to be viewed as a symbol with a multibite line?



  • We'll taking another line. Presuming this is the first utf-8 symbol

    unsigned char b=...; // тут проверяемый байт (обязательно unsigned !!!)
    if(b<=0x7F) это однобайтовый символ, можем выдавать его и проверять следующий
    else if(b>=0xF8) ... это не unicode символ, или более новый стандарт с более чем 4х байтовыми символами
    else if(b>=0xF0) ... это 4х байтовый символ. т.е. берем еще следующие 3 байта
    else if(b>=0xE0) ... это 3х байтовый символ. т.е. берем еще следующие 2 байта
    else if(b>=0xC0) ... это 2х байтовый символ. т.е. берем еще следующий байт
    else ... b - совершенно точно один из серединных байт символа, мы потеряли первый байт
    

    If the symbol is more than 1 bayth length, then all subsequent baytes of the symbol must be within range b>=0x80 && b<0xC0

    I wrote it on the standard. https://www.rfc-editor.org/rfc/rfc3629 I hope there's no mistake. We can watch the library code.

    Oh. I love bicycles, so I gave you an example, with symbol codes. Codes for Russian letters have been completed with the function given Javascript charCodeAt - matched. Attention: collected on the knee, no checks are made that the line ended in the middle of the symbol, with the wrong unicode line possible to go beyond the range! The three-bite and four-bite symbols are also not checked, because I don't know Chinese. There's also no check on the 2nd and the next Bate unicode to the correctness, bluntly drop two bats and use it.

    #include <stdio.h>
    #include <string.h>
    

    unsigned char *s="Test string. Тестовая строка";
    int main()
    {
    int len=strlen(s);
    int i;
    int sim_code; // Сюда собираем код очередного символа
    for(i=0;i<len;i++)
    {
    if(s[i]<=0x7F) { sim_code=s[i]; }
    else if(s[i]>=0xF8) { printf("Не unicode 6.0 (RFC3629) символ %X",s[i]); continue; }
    else if(s[i]>=0xF0) { sim_code=( ((s[i]&0x07)<<18) | ((s[i+1]&0x3F)<<12) | ((s[i+2]&0x3F)<<6) | (s[i+3]&0x3F) ); i+=3; }
    else if(s[i]>=0xE0) { sim_code=( ((s[i]&0x0F)<<12) | ((s[i+1]&0x3F)<<6) | (s[i+2]&0x3F) ); i+=2; }
    else if(s[i]>=0xC0) { sim_code=( ((s[i]&0x1F)<<6) | (s[i+1] & 0x3F) ); i++; }
    else { printf("Пропущен первый байт unicode символа !!! (%X)",s[i]); continue; }
    printf("%d ",sim_code);
    }
    }

    Universal method of obtaining the length of the current symbol:

    unsigned char b=s[i]; // Берем первый байт символа
    int n; // длина символа
    if(b<=0x7F) n=1; ... это однобайтовый символ, работаем с ним как есть
    else
    for(n=0; b & 0x80; n++) b<<=1;
    // после цикла n=полной длине в байтах, включая текущий байт




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2