Your code is mostly fine, the reason it's only compiling, is due to Nim having 
a much stricter type system. For other visitors this is the error message is:
    
    
    in.nim(3, 34) Error: type mismatch: got but expected 'cuchar = Char'
    
    
    Run

You're probably better off not using the types prefixed by C, those are mostly 
meant for compability.

This is how I would translate your function into more idiomatic Nim: 
    
    
    proc utf8str_codepoint_len*(s: string, utf8len: int): int =
      const
        m4 = 128'u8 + 64 + 32 + 16
        m3 = 128'u8 + 64 + 32
        m2 = 128'u8 + 64
      var i = 0
      while i < utf8len:
        # in Nim char and uint8/byte are separate types, with the former being 
only used to represent 8 bit
        # characters and thus arithmetic functions aren't defined for neither 
it nor cchar.
        var c = uint8(s[i])
        if (c and m4) == m4:
          i += 3
        elif (c and m3) == m3:
          i += 2
        elif (c and m2) == m2:
          i += 1
        inc result # result is the implicit return value that is like all 
unassigned variables in Nim set to 0 in the beginning
    
    
    Run

Reply via email to