本文整理汇总了C#中System.Text.DecoderFallbackBuffer类的典型用法代码示例。如果您正苦于以下问题:C# DecoderFallbackBuffer类的具体用法?C# DecoderFallbackBuffer怎么用?C# DecoderFallbackBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DecoderFallbackBuffer类属于System.Text命名空间,在下文中一共展示了DecoderFallbackBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EncodingCharBuffer
[System.Security.SecurityCritical] // auto-generated
internal unsafe EncodingCharBuffer(EncodingNLS enc, DecoderNLS decoder, char* charStart, int charCount, byte* byteStart, int byteCount)
{
_enc = enc;
_decoder = decoder;
_chars = charStart;
_charStart = charStart;
_charEnd = charStart + charCount;
_byteStart = byteStart;
_bytes = byteStart;
_byteEnd = byteStart + byteCount;
if (_decoder == null)
_fallbackBuffer = enc.DecoderFallback.CreateFallbackBuffer();
else
_fallbackBuffer = _decoder.FallbackBuffer;
// If we're getting chars or getting char count we don't expect to have
// to remember fallbacks between calls (so it should be empty)
Debug.Assert(_fallbackBuffer.Remaining == 0,
"[Encoding.EncodingCharBuffer.EncodingCharBuffer]Expected empty fallback buffer for getchars/charcount");
_fallbackBufferHelper = new DecoderFallbackBufferHelper(_fallbackBuffer);
_fallbackBufferHelper.InternalInitialize(_bytes, _charEnd);
}
示例2: FallbackInvalidByteSequence
private unsafe bool FallbackInvalidByteSequence(ref byte* pSrc, int ch, DecoderFallbackBuffer fallback, ref char* pTarget)
{
byte* numPtr = pSrc;
byte[] bytesUnknown = this.GetBytesUnknown(ref numPtr, ch);
if (!fallback.InternalFallback(bytesUnknown, pSrc, ref pTarget))
{
pSrc = numPtr;
return false;
}
return true;
}
示例3: InternalGetCharCount
private unsafe static int InternalGetCharCount (
byte* bytes, int count, uint leftOverBits,
uint leftOverCount, object provider,
ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
{
int index = 0;
int length = 0;
if (leftOverCount == 0) {
int end = index + count;
for (; index < end; index++, count--) {
if (bytes [index] < 0x80)
length++;
else
break;
}
}
// Determine the number of characters that we have.
uint ch;
uint leftBits = leftOverBits;
uint leftSoFar = (leftOverCount & (uint)0x0F);
uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
while (count > 0) {
ch = (uint)(bytes[index++]);
--count;
if (leftSize == 0) {
// Process a UTF-8 start character.
if (ch < (uint)0x0080) {
// Single-byte UTF-8 character.
++length;
} else if ((ch & (uint)0xE0) == (uint)0xC0) {
// Double-byte UTF-8 character.
leftBits = (ch & (uint)0x1F);
leftSoFar = 1;
leftSize = 2;
} else if ((ch & (uint)0xF0) == (uint)0xE0) {
// Three-byte UTF-8 character.
leftBits = (ch & (uint)0x0F);
leftSoFar = 1;
leftSize = 3;
} else if ((ch & (uint)0xF8) == (uint)0xF0) {
// Four-byte UTF-8 character.
leftBits = (ch & (uint)0x07);
leftSoFar = 1;
leftSize = 4;
} else if ((ch & (uint)0xFC) == (uint)0xF8) {
// Five-byte UTF-8 character.
leftBits = (ch & (uint)0x03);
leftSoFar = 1;
leftSize = 5;
} else if ((ch & (uint)0xFE) == (uint)0xFC) {
// Six-byte UTF-8 character.
leftBits = (ch & (uint)0x03);
leftSoFar = 1;
leftSize = 6;
} else {
// Invalid UTF-8 start character.
length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - 1, 1);
}
} else {
// Process an extra byte in a multi-byte sequence.
if ((ch & (uint)0xC0) == (uint)0x80) {
leftBits = ((leftBits << 6) | (ch & (uint)0x3F));
if (++leftSoFar >= leftSize) {
// We have a complete character now.
if (leftBits < (uint)0x10000) {
// is it an overlong ?
bool overlong = false;
switch (leftSize) {
case 2:
overlong = (leftBits <= 0x7F);
break;
case 3:
overlong = (leftBits <= 0x07FF);
break;
case 4:
overlong = (leftBits <= 0xFFFF);
break;
case 5:
overlong = (leftBits <= 0x1FFFFF);
break;
case 6:
overlong = (leftBits <= 0x03FFFFFF);
break;
}
if (overlong) {
length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
}
else if ((leftBits & 0xF800) == 0xD800) {
// UTF-8 doesn't use surrogate characters
length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
}
else
++length;
} else if (leftBits < (uint)0x110000) {
length += 2;
} else {
length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
//.........这里部分代码省略.........
示例4: GetChars
int GetChars (byte[] bytes, int byteIndex, int byteCount,
char[] chars, int charIndex,
ref DecoderFallbackBuffer buffer)
{
if (bytes == null)
throw new ArgumentNullException ("bytes");
if (chars == null)
throw new ArgumentNullException ("chars");
if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
if (charIndex < 0 || charIndex > chars.Length)
throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
if ((chars.Length - charIndex) < byteCount)
throw new ArgumentException (_("Arg_InsufficientSpace"));
int count = byteCount;
while (count-- > 0) {
char c = (char) bytes [byteIndex++];
if (c < '\x80')
chars [charIndex++] = c;
else {
if (buffer == null)
buffer = DecoderFallback.CreateFallbackBuffer ();
var thisByte = new byte[] { bytes [byteIndex-1] };
buffer.Fallback (thisByte, 0);
while (buffer.Remaining > 0) {
if (charIndex < chars.Length) {
chars [charIndex++] = buffer.GetNextChar ();
continue;
}
throw new ArgumentException (
"The output char buffer is too small to contain the " +
"decoded characters.");
}
}
}
return byteCount;
}
示例5: FallbackInvalidByteSequence
[System.Security.SecurityCritical] // auto-generated
private unsafe bool FallbackInvalidByteSequence(
ref byte* pSrc, int ch, DecoderFallbackBuffer fallback, ref char* pTarget)
{
// Get our byte[]
byte *pStart = pSrc;
byte[] bytesUnknown = GetBytesUnknown(ref pStart, ch);
// Do the actual fallback
if (!fallback.InternalFallback(bytesUnknown, pSrc, ref pTarget))
{
// Oops, it failed, back up to pStart
pSrc = pStart;
return false;
}
// It worked
return true;
}
示例6: InternalGetChars
// Get the characters that result from decoding a byte buffer.
private unsafe static int InternalGetChars (
byte[] bytes, int byteIndex, int byteCount, char[] chars,
int charIndex, ref uint leftOverBits, ref uint leftOverCount,
object provider,
ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
{
// Validate the parameters.
if (bytes == null) {
throw new ArgumentNullException ("bytes");
}
if (chars == null) {
throw new ArgumentNullException ("chars");
}
if (byteIndex < 0 || byteIndex > bytes.Length) {
throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
}
if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
}
if (charIndex < 0 || charIndex > chars.Length) {
throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
}
if (charIndex == chars.Length)
return 0;
fixed (char* cptr = chars) {
if (byteCount == 0 || byteIndex == bytes.Length)
return InternalGetChars (null, 0, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, provider, ref fallbackBuffer, ref bufferArg, flush);
// otherwise...
fixed (byte* bptr = bytes)
return InternalGetChars (bptr + byteIndex, byteCount, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, provider, ref fallbackBuffer, ref bufferArg, flush);
}
}
示例7: InternalGetCharCount
// Internal version of "GetCharCount" which can handle a rolling
// state between multiple calls to this method.
#if NET_2_0
private unsafe static int InternalGetCharCount (
byte[] bytes, int index, int count, uint leftOverBits,
uint leftOverCount, object provider,
ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
示例8: InternalGetCharsCount
internal unsafe static DecoderStatus InternalGetCharsCount (
byte[] bytes, int byteIndex, int byteCount,
DecoderFallbackBuffer fallbackBuffer,
out int bytesProcessed, out int charsProcessed,
ref uint leftBytes, ref uint leftBits, ref uint procBytes,
bool flush)
{
if (bytes == null)
throw new ArgumentNullException ("bytes");
if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
fixed (byte* bptr = bytes) {
return InternalGetChars (
bptr + byteIndex, byteCount,
null, -1,
fallbackBuffer,
out bytesProcessed, out charsProcessed,
ref leftBytes, ref leftBits, ref procBytes,
flush);
}
}
示例9: InternalGetChars
// InternalGetChars processor. Can decode or count space needed for
// decoding, depending on the enabled mode:
// - decoder
// enabled when charCount >= 0 (but chars may be null)
// - counter
// enabled when chars == null && charCount < 0
internal unsafe static DecoderStatus InternalGetChars (
byte* bytes, int byteCount,
char* chars, int charCount,
DecoderFallbackBuffer fallbackBuffer,
out int bytesProcessed, out int charsProcessed,
ref uint leftBytes, ref uint leftBits, ref uint procBytes,
bool flush)
{
DecoderStatus s;
int t_bytesProcessed, t_charsProcessed;
// Validate parameters
if (byteCount < 0)
throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
else
if (byteCount > 0 && bytes == null)
throw new ArgumentNullException ("bytes");
if (chars == null) {
if (charCount > 0)
throw new ArgumentNullException ("chars");
} else {
if (charCount < 0)
throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
}
// reset counters
charsProcessed = 0;
bytesProcessed = 0;
// byte processing loop
while (byteCount - bytesProcessed > 0) {
// fetch a char from the input byte array
s = chars != null
? InternalGetChar (
bytes + bytesProcessed, byteCount - bytesProcessed,
chars + charsProcessed, charCount - charsProcessed,
out t_bytesProcessed, out t_charsProcessed,
ref leftBytes, ref leftBits, ref procBytes)
: InternalGetChar (
bytes + bytesProcessed, byteCount - bytesProcessed,
null, charCount,
out t_bytesProcessed, out t_charsProcessed,
ref leftBytes, ref leftBits, ref procBytes);
// if not enough space return here
// NOTE: maybe we should restore the original encoder
// state ... we should check what ms do in this case
if(s == DecoderStatus.InsufficientSpace)
return DecoderStatus.InsufficientSpace;
// update counters
charsProcessed += t_charsProcessed;
bytesProcessed += t_bytesProcessed;
switch (s) {
case DecoderStatus.Ok:
break; // everything OK :D
case DecoderStatus.Overlong:
case DecoderStatus.InvalidSequence:
case DecoderStatus.InvalidStart:
case DecoderStatus.InvalidChar:
case DecoderStatus.SurrogateFound:
s = InternalGetCharsFlush (
chars, charCount,
fallbackBuffer,
s,
bytesProcessed, ref charsProcessed,
ref leftBytes, ref leftBits, ref procBytes);
if (s != DecoderStatus.Ok)
return s;
break;
case DecoderStatus.InputRunOut:
return flush
? InternalGetCharsFlush (
chars, charCount,
fallbackBuffer,
s,
bytesProcessed, ref charsProcessed,
ref leftBytes, ref leftBits, ref procBytes)
: DecoderStatus.InputRunOut;
}
}
return flush
? InternalGetCharsFlush (
chars, charCount,
fallbackBuffer,
DecoderStatus.Ok,
bytesProcessed, ref charsProcessed,
ref leftBytes, ref leftBits, ref procBytes)
: DecoderStatus.Ok;
}
示例10: InternalGetCharsFlush
// This function is called when we want to flush the decoder state
// (i.e. in case of invalid UTF-8 characters or interrupted sequences)
internal unsafe static DecoderStatus InternalGetCharsFlush (
char* chars, int charCount,
DecoderFallbackBuffer fallbackBuffer,
DecoderStatus s,
int bytesProcessed, ref int charsProcessed,
ref uint leftBytes, ref uint leftBits, ref uint procBytes)
{
// if there is nothing to flush, then exit silently
if(procBytes == 0)
return DecoderStatus.Ok;
// now we build a 'bytesUnknown' array with the
// stored bytes in 'procBytes'.
int extra = 0;
for (uint t = procBytes; t != 0; extra++)
t = t >> 8;
byte [] bytesUnknown = new byte [extra];
for (int i = extra; i > 0; i--)
bytesUnknown [i - 1] = (byte) ((procBytes >> (8 * (extra - i))) & 0xff);
// partial reset: this condition avoids infinite loops
if (s == DecoderStatus.InvalidSequence)
leftBytes = 0;
// call the fallback and cross fingers
fallbackBuffer.Fallback (bytesUnknown, bytesProcessed - extra);
if (chars != null) {
while (fallbackBuffer.Remaining > 0) {
if (charsProcessed >= charCount)
return DecoderStatus.InsufficientSpace;
chars [charsProcessed++] = fallbackBuffer.GetNextChar ();
}
} else
charsProcessed += fallbackBuffer.Remaining;
fallbackBuffer.Reset ();
// recovery was succesful, flush decoder state
leftBits = leftBytes = procBytes = 0;
return DecoderStatus.Ok;
}
示例11: Fallback
// for GetCharCount()
static unsafe int Fallback (object provider, ref DecoderFallbackBuffer buffer, ref byte [] bufferArg, byte* bytes, long index, uint size)
{
if (buffer == null) {
DecoderFallback fb = provider as DecoderFallback;
if (fb != null)
buffer = fb.CreateFallbackBuffer ();
else
buffer = ((Decoder) provider).FallbackBuffer;
}
if (bufferArg == null)
bufferArg = new byte [1];
int ret = 0;
for (int i = 0; i < size; i++) {
bufferArg [0] = bytes [(int) index + i];
buffer.Fallback (bufferArg, 0);
ret += buffer.Remaining;
buffer.Reset ();
}
return ret;
}
示例12: InternalGetChars
private unsafe static int InternalGetChars (
byte* bytes, int byteCount, char* chars, int charCount,
ref uint leftOverBits, ref uint leftOverCount,
object provider,
ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
示例13: DecoderFallbackBufferHelper
public DecoderFallbackBufferHelper(DecoderFallbackBuffer fallbackBuffer)
{
_fallbackBuffer = fallbackBuffer;
byteStart = null;
charEnd = null;
}
示例14: GetChars
int GetChars (byte[] bytes, int byteIndex, int byteCount,
char[] chars, int charIndex,
ref DecoderFallbackBuffer buffer)
{
if (bytes == null)
throw new ArgumentNullException ("bytes");
if (chars == null)
throw new ArgumentNullException ("chars");
if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
if (charIndex < 0 || charIndex > chars.Length)
throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
if ((chars.Length - charIndex) < byteCount)
throw new ArgumentException (_("Arg_InsufficientSpace"));
int count = byteCount;
while (count-- > 0) {
char c = (char) bytes [byteIndex++];
if (c < '\x80')
chars [charIndex++] = c;
else {
if (buffer == null)
buffer = DecoderFallback.CreateFallbackBuffer ();
buffer.Fallback (bytes, byteIndex);
while (buffer.Remaining > 0)
chars [charIndex++] = buffer.GetNextChar ();
}
}
return byteCount;
}
示例15: EncodingCharBuffer
[System.Security.SecurityCritical] // auto-generated
internal unsafe EncodingCharBuffer(Encoding enc, DecoderNLS decoder, char* charStart, int charCount,
byte* byteStart, int byteCount)
{
this.enc = enc;
this.decoder = decoder;
this.chars = charStart;
this.charStart = charStart;
this.charEnd = charStart + charCount;
this.byteStart = byteStart;
this.bytes = byteStart;
this.byteEnd = byteStart + byteCount;
if (this.decoder == null)
this.fallbackBuffer = enc.DecoderFallback.CreateFallbackBuffer();
else
this.fallbackBuffer = this.decoder.FallbackBuffer;
// If we're getting chars or getting char count we don't expect to have
// to remember fallbacks between calls (so it should be empty)
Contract.Assert(fallbackBuffer.Remaining == 0,
"[Encoding.EncodingCharBuffer.EncodingCharBuffer]Expected empty fallback buffer for getchars/charcount");
fallbackBuffer.InternalInitialize(bytes, charEnd);
}