public override unsafe int GetByteCount(char *chars, int count, bool flush) { ArgumentNullException.ThrowIfNull(chars); if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); } bool excludeLastChar = count > 0 && !flush && char.IsHighSurrogate(chars[count - 1]); if (excludeLastChar) { count--; } if (_charLeftOver == NULL_CHAR) { if (count <= 0) { return(0); } return(OSEncoding.WideCharToMultiByte(_encoding.CodePage, chars, count, null, 0)); } // we have left over character if (count == 0 && !excludeLastChar && !flush) { return(0); } return(ConvertWithLeftOverChar(chars, count, null, 0)); }
private unsafe int ConvertWithLeftOverChar(char *chars, int count, byte *bytes, int byteCount) { Debug.Assert(_charLeftOver != NULL_CHAR); char *pTempBuffer = stackalloc char[2]; pTempBuffer[0] = _charLeftOver; int index = 0; if (count > 0 && char.IsLowSurrogate(chars[0])) { pTempBuffer[1] = chars[0]; index++; } int result = OSEncoding.WideCharToMultiByte(_encoding.CodePage, pTempBuffer, index + 1, bytes, byteCount); if (count - index > 0) { result += OSEncoding.WideCharToMultiByte( _encoding.CodePage, chars + index, count - index, bytes == null ? null : bytes + result, bytes == null ? 0 : byteCount - result); } return(result); }
private unsafe int ConvertWithLeftOverByte(byte *bytes, int count, char *chars, int charCount) { Debug.Assert(_leftOverLeadByte != 0); byte *pTempBuffer = stackalloc byte[2]; pTempBuffer[0] = _leftOverLeadByte; int index = 0; if (count > 0) { pTempBuffer[1] = bytes[0]; index++; } int result = OSEncoding.MultiByteToWideChar(_encoding.CodePage, pTempBuffer, index + 1, chars, charCount); if (count - index > 0) { result += OSEncoding.MultiByteToWideChar( _encoding.CodePage, bytes + index, count - index, chars == null ? null : chars + result, chars == null ? 0 : charCount - result); } return(result); }
public unsafe override int GetCharCount(byte *bytes, int count, bool flush) { ArgumentNullException.ThrowIfNull(bytes); if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); } bool excludeLastByte = count > 0 && !flush && IsLastByteALeadByte(bytes, count); if (excludeLastByte) { count--; } if (_leftOverLeadByte == 0) { if (count <= 0) { return(0); } return(OSEncoding.MultiByteToWideChar(_encoding.CodePage, bytes, count, null, 0)); } if (count == 0 && !excludeLastByte && !flush) { return(0); } return(ConvertWithLeftOverByte(bytes, count, null, 0)); }
public unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, bool flush) { if (chars == null || bytes == null) { throw new ArgumentNullException(chars == null ? nameof(chars) : nameof(bytes), SR.ArgumentNull_Array); } if (byteCount < 0 || charCount < 0) { throw new ArgumentOutOfRangeException(byteCount < 0 ? nameof(byteCount) : nameof(charCount), SR.ArgumentOutOfRange_NeedNonNegNum); } if (charCount == 0) { return(0); } byte lastByte = byteCount > 0 && !flush && IsLastByteALeadByte(bytes, byteCount) ? bytes[byteCount - 1] : (byte)0; if (lastByte != 0) { byteCount--; } if (_leftOverLeadByte == 0) { if (byteCount <= 0) { _leftOverLeadByte = lastByte; return(0); } int result = OSEncoding.MultiByteToWideChar(_encoding.CodePage, bytes, byteCount, chars, charCount); _leftOverLeadByte = lastByte; return(result); } // we have left over lead byte if (byteCount == 0 && lastByte == 0 && !flush) { return(0); } int res = ConvertWithLeftOverByte(bytes, byteCount, chars, charCount); _leftOverLeadByte = lastByte; return(res); }
public unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, bool flush) { if (chars == null || bytes == null) { throw new ArgumentNullException(chars == null ? nameof(chars) : nameof(bytes), SR.ArgumentNull_Array); } if (byteCount < 0 || charCount < 0) { throw new ArgumentOutOfRangeException(byteCount < 0 ? nameof(byteCount) : nameof(charCount), SR.ArgumentOutOfRange_NeedNonNegNum); } if (byteCount == 0) { return(0); } char lastChar = charCount > 0 && !flush && Char.IsHighSurrogate(chars[charCount - 1]) ? chars[charCount - 1] : NULL_CHAR; if (lastChar != NULL_CHAR) { charCount--; } if (_charLeftOver == NULL_CHAR) { if (charCount <= 0) { _charLeftOver = lastChar; return(0); } int result = OSEncoding.WideCharToMultiByte(_encoding.CodePage, chars, charCount, bytes, byteCount); _charLeftOver = lastChar; return(result); } // we have left over character if (charCount == 0 && lastChar == NULL_CHAR && !flush) { return(0); } int res = ConvertWithLeftOverChar(chars, charCount, bytes, byteCount); _charLeftOver = lastChar; return(res); }