a// =================================================================================================================

//        xs_UTF16Decode

// =================================================================================================================

int32 xs_UTF16Decode (uint32 &code, const xs_utf16* str, int32 len, bool strict)

{

          if (str==0||len==0)          {code=0; return 0;}

 

          uint32 c1 = str[0];

 

          //note: many implementations test from HighStart to HighEnd,

          //                 this may be a partial code point, and is incorrect(?)

          //                 trivial checking should exclude the WHOLE surrogate range

          if (c1<xs_UTF16_HighStart || c1>xs_UTF16_LowEnd)          return 1;

                             //really an error if we're starting in the low range

                  

          //surrogate pair

          if (len<=1 || str[1]==0)                                  {code=xs_UTF_Replace; return strict ? 0 : 1;} //error

          uint32 c2 = str[1];

          code = ((c1-xs_UTF16_HighStart)<<xs_UTF16_HalfShift) + (c2-xs_UTF16_LowStart) + xs_UTF16_HalfBase;

 

          if (strict==false)                                        return 2;

 

          //check for errors

          if (c1>=xs_UTF16_LowStart && c1<=xs_UTF16_LowEnd)         {code=xs_UTF_Replace; return 0;} //error

          if (c2<xs_UTF16_LowStart  || c2>xs_UTF16_LowEnd)          {code=xs_UTF_Replace; return 0;} //error

          if (code>xs_UTF_Max)                                      {code=xs_UTF_Replace; return 0;} //error

 

          //success

          return 2;

}