It would be helpful to others to indicate your exact problem, like why does it 
not compile and why you can not solve this? Your above snippet has problems 
with converting types, because you are trying to mix Nim integers and C 
unsigned chars. Fixing this is not too hard:
    
    
    proc utf8str_codepoint_len*(s: cstring, utf8len: cint): cint =
      var codepointLen: cint = 0
      let m4 = 128 + 64 + 32 + 16
      let m3 = 128 + 64 + 32
      let m2 = 128 + 64
      var i: cint = 0
      while i < utf8len:
        var c = s[i].int
        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

As an alternative you might look into the nim unicode module which offers the 
runelen() proc which does almost exactly what your above code does.

Reply via email to