Search code examples
unicodef#portable-class-library

F# .Net portable subset Unicode issues


OK, I've made an F# portable library project in VS2012 and I have some integers that represent Utf-32 encoded characters eg: 0x0001D538 which is a double struck A. Normally to make this into a Utf-16 surrogate pair you would use System.Char.ConvertFromUtf32(i), job done. However, Microsoft have kindly decided not to include this method in the .net portable subset. (it runs fine in the interactive window which must be running the full .net). So, what should I do instead to get my favorite surrogate pairs from these integers? They need to be integers because I do some arithmetic on them. Waiting for the next version of things to come out is a viable option.


Solution

  • Here's a quick translation of the C# from Reflector. Can you use this?

    type System.Char with
      static member ConvertFromUtf32(utf32) =
        if utf32 < 0 || utf32 > 0x10ffff || (utf32 >= 0xd800 && utf32 <= 0xdfff) then
          invalidArg "utf32" "Out of range"
        elif utf32 < 0x10000 then 
          new String(char utf32, 1)
        else
          let utf32 = utf32 - 0x10000
          new String([| char ((utf32 / 0x400) + 0xd800); char ((utf32 % 0x400) + 0xdc00) |])