Bill Allombert on Thu, 01 Oct 2026 23:10:04 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

utf8 library for GP (!)


Dear PARI-dev,

I wrote a basic script to convert UTF8 strings to code word and back in GP.

? utf8("βγδζθκλµνξπρςστφχψ")
%1 = [946,947,948,950,952,954,955,181,957,958,960,961,962,963,964,966,967,968]
? unutf8(%)
%2 = "βγδζθκλµνξπρςστφχψ"


This way we can lok up characters in ut8 strings in GP.

Cheers,
Bill
utf8(s)=
{
  my(L=List(),skip=0,V=Vecsmall(s));
  for(i=1,#V,
    if(skip,skip--;next);
    my(c=V[i]);
    if (c>=240,
        skip=3;
        c = (c%8)<<18+(V[i+1]%64)<<12+(V[i+2]%64)<<6+V[i+3]%64
     ,c>=224,
        skip=2;
        c = (c%16)<<12+(V[i+1]%64)<<6+V[i+2]%64;
     ,c>=192,
        skip=1;
        c = (c%32)<<6+V[i+1]%64);
     listput(~L,c));
  Vec(L);
}

unutf8(V)=
{
  strchr(concat(vector(#V,i,
    my(c=V[i],w);
    if (c>=0x10000,
        Vecsmall([240 + c>>18, 128+(c>>12)%64, 128+(c>>6)%64, 128+c%64])
     ,c>=0x800,
        Vecsmall([224 + c>>12, 128+(c>>6)%64, 128+c%64])
     ,c>=0x80,
        Vecsmall([192 + c>>6, 128+c%64])
     ,  Vecsmall(c)))));
}