/// <summary>Convert UTF8 bytes into UTF16 characters. If offset /// is non-zero, conversion starts at that starting point /// in utf8, re-using the results from the previous call /// up until offset. /// </summary> public static void UTF8toUTF16(byte[] utf8, int offset, int length, UTF16Result result) { int end = offset + length; char[] out_Renamed = result.result; if (result.offsets.Length <= end) { int[] newOffsets = new int[2 * end]; Array.Copy(result.offsets, 0, newOffsets, 0, result.offsets.Length); result.offsets = newOffsets; } int[] offsets = result.offsets; // If incremental decoding fell in the middle of a // single unicode character, rollback to its start: int upto = offset; while (offsets[upto] == - 1) upto--; int outUpto = offsets[upto]; // Pre-allocate for worst case 1-for-1 if (outUpto + length >= out_Renamed.Length) { char[] newOut = new char[2 * (outUpto + length)]; Array.Copy(out_Renamed, 0, newOut, 0, outUpto); result.result = out_Renamed = newOut; } while (upto < end) { int b = utf8[upto] & 0xff; int ch; offsets[upto++] = outUpto; if (b < 0xc0) { System.Diagnostics.Debug.Assert(b < 0x80); ch = b; } else if (b < 0xe0) { ch = ((b & 0x1f) << 6) + (utf8[upto] & 0x3f); offsets[upto++] = - 1; } else if (b < 0xf0) { ch = ((b & 0xf) << 12) + ((utf8[upto] & 0x3f) << 6) + (utf8[upto + 1] & 0x3f); offsets[upto++] = - 1; offsets[upto++] = - 1; } else { System.Diagnostics.Debug.Assert(b < 0xf8); ch = ((b & 0x7) << 18) + ((utf8[upto] & 0x3f) << 12) + ((utf8[upto + 1] & 0x3f) << 6) + (utf8[upto + 2] & 0x3f); offsets[upto++] = - 1; offsets[upto++] = - 1; offsets[upto++] = - 1; } if (ch <= UNI_MAX_BMP) { // target is a character <= 0xFFFF out_Renamed[outUpto++] = (char) ch; } else { // target is a character in range 0xFFFF - 0x10FFFF int chHalf = ch - HALF_BASE; out_Renamed[outUpto++] = (char) ((chHalf >> (int) HALF_SHIFT) + UNI_SUR_HIGH_START); out_Renamed[outUpto++] = (char) ((chHalf & HALF_MASK) + UNI_SUR_LOW_START); } } offsets[upto] = outUpto; result.length = outUpto; }
public void CopyText(UTF16Result other) { SetLength(other.length); Array.Copy(other.result, 0, result, 0, length); }
/** Convert UTF8 bytes into UTF16 characters. If offset * is non-zero, conversion starts at that starting point * in utf8, re-using the results from the previous call * up until offset. */ public static void UTF8toUTF16(/* in */ byte[] utf8, /* in */ int offset, /* in */ int length, /* in */ UTF16Result result) { int end = offset + length; char[] out_Renamed = result.result; if (result.offsets.Length <= end) { int[] newOffsets = new int[2 * end]; System.Array.Copy(result.offsets, 0, newOffsets, 0, result.offsets.Length); result.offsets = newOffsets; } int[] offsets = result.offsets; // If incremental decoding fell in the middle of a // single unicode character, rollback to its start: int upto = offset; while (offsets[upto] == -1) { upto--; } int outUpto = offsets[upto]; // Pre-allocate for worst case 1-for-1 if (outUpto + length >= out_Renamed.Length) { char[] newOut = new char[2 * (outUpto + length)]; System.Array.Copy(out_Renamed, 0, newOut, 0, outUpto); result.result = out_Renamed = newOut; } while (upto < end) { int b = utf8[upto] & 0xff; int ch; offsets[upto++] = outUpto; if (b < 0xc0) { System.Diagnostics.Debug.Assert(b < 0x80); ch = b; } else if (b < 0xe0) { ch = ((b & 0x1f) << 6) + (utf8[upto] & 0x3f); offsets[upto++] = -1; } else if (b < 0xf0) { ch = ((b & 0xf) << 12) + ((utf8[upto] & 0x3f) << 6) + (utf8[upto + 1] & 0x3f); offsets[upto++] = -1; offsets[upto++] = -1; } else { System.Diagnostics.Debug.Assert(b < 0xf8); ch = ((b & 0x7) << 18) + ((utf8[upto] & 0x3f) << 12) + ((utf8[upto + 1] & 0x3f) << 6) + (utf8[upto + 2] & 0x3f); offsets[upto++] = -1; offsets[upto++] = -1; offsets[upto++] = -1; } if (ch <= UNI_MAX_BMP) { // target is a character <= 0xFFFF out_Renamed[outUpto++] = (char)ch; } else { // target is a character in range 0xFFFF - 0x10FFFF int chHalf = ch - HALF_BASE; out_Renamed[outUpto++] = (char)((chHalf >> (int)HALF_SHIFT) + UNI_SUR_HIGH_START); out_Renamed[outUpto++] = (char)((chHalf & HALF_MASK) + UNI_SUR_LOW_START); } } offsets[upto] = outUpto; result.length = outUpto; }
public void copyText(UTF16Result other) { setLength(other.length); System.Array.Copy(other.result, 0, result, 0, length); }