Hello i have the following piece of code that i want to transform in nim:
    
    
    int utf8str_codepoint_len( char const* s, int utf8len ) {
            int codepointLen = 0;
            unsigned char m4 = 128 + 64 + 32 + 16;
            unsigned char m3 = 128 + 64 + 32;
            unsigned char m2 = 128 + 64;
            for ( int i = 0; i < utf8len; ++ i, ++ codepointLen ) {
                    char c = s[i];
                    if ( ( c & m4 ) == m4 ) {
                            i += 3;
                    } else if ( ( c & m3 ) == m3 ) {
                            i += 2;
                    } else if ( ( c & m2 ) == m2 ) {
                            i += 1;
                    }
            }
            return ( codepointLen );
    }
    
    
    Run

I have this for the moment but it's doesn't compile:
    
    
    proc utf8str_codepoint_len*(s: cstring; utf8len: cint): cint =
      var codepointLen: cint = 0
      var m4: cuchar = 128 + 64 + 32 + 16
      var m3: cuchar = 128 + 64 + 32
      var m2: cuchar = 128 + 64
      var i: cint = 0
      while i < utf8len:
        var c: char = s[i]
        if (c and m4) == m4:
          inc(i, 3)
        elif (c and m3) == m3:
          inc(i, 2)
        elif (c and m2) == m2:
          inc(i, 1)
        inc(i)
        inc(codepointLen)
      return codepointLen
    
    
    Run

Should i keep it in c and import it nim ?

Reply via email to