本文整理汇总了C#中System.Text.EncoderNLS类的典型用法代码示例。如果您正苦于以下问题:C# EncoderNLS类的具体用法?C# EncoderNLS怎么用?C# EncoderNLS使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EncoderNLS类属于System.Text命名空间,在下文中一共展示了EncoderNLS类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetByteCount
internal unsafe int GetByteCount(char* chars, int count, EncoderNLS encoder)
{
Contract.Requires(count >= 0);
Contract.Requires(Contract.WritableBytes(chars) >= (uint)(sizeof(char) * count));
return this.GetByteCount(chars, count);
}
示例2: EncodingByteBuffer
internal unsafe EncodingByteBuffer(EncodingNLS inEncoding, EncoderNLS inEncoder, byte* inByteStart, int inByteCount, char* inCharStart, int inCharCount)
{
_enc = inEncoding;
_encoder = inEncoder;
_charStart = inCharStart;
_chars = inCharStart;
_charEnd = inCharStart + inCharCount;
_bytes = inByteStart;
_byteStart = inByteStart;
_byteEnd = inByteStart + inByteCount;
if (_encoder == null)
fallbackBuffer = _enc.EncoderFallback.CreateFallbackBuffer();
else
{
fallbackBuffer = _encoder.FallbackBuffer;
// If we're not converting we must not have data in our fallback buffer
if (_encoder.m_throwOnOverflow && _encoder.InternalHasFallbackBuffer && fallbackBuffer.Remaining > 0)
throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, _encoder.Encoding.EncodingName, _encoder.Fallback.GetType()));
}
fallbackBufferHelper = new EncoderFallbackBufferHelper(fallbackBuffer);
fallbackBufferHelper.InternalInitialize(_chars, _charEnd, _encoder, _bytes != null);
}
示例3: GetByteCount
internal override unsafe int GetByteCount(char* chars, int count, EncoderNLS encoder)
{
char ch2;
base.CheckMemorySection();
char charLeftOver = '\0';
if (encoder != null)
{
charLeftOver = encoder.charLeftOver;
if (encoder.InternalHasFallbackBuffer && (encoder.FallbackBuffer.Remaining > 0))
{
throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() }));
}
}
int num = 0;
char* charEnd = chars + count;
EncoderFallbackBuffer fallbackBuffer = null;
if (charLeftOver > '\0')
{
fallbackBuffer = encoder.FallbackBuffer;
fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false);
fallbackBuffer.InternalFallback(charLeftOver, ref chars);
}
while (((ch2 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd))
{
if (ch2 == '\0')
{
ch2 = chars[0];
chars++;
}
ushort num2 = this.mapUnicodeToBytes[ch2];
if ((num2 == 0) && (ch2 != '\0'))
{
if (fallbackBuffer == null)
{
if (encoder == null)
{
fallbackBuffer = base.encoderFallback.CreateFallbackBuffer();
}
else
{
fallbackBuffer = encoder.FallbackBuffer;
}
fallbackBuffer.InternalInitialize(charEnd - count, charEnd, encoder, false);
}
fallbackBuffer.InternalFallback(ch2, ref chars);
}
else
{
num++;
if (num2 >= 0x100)
{
num++;
}
}
}
return num;
}
示例4: InternalInitialize
[System.Security.SecurityCritical] // auto-generated
internal unsafe void InternalInitialize(char* _charStart, char* _charEnd, EncoderNLS _encoder, bool _setEncoder)
{
charStart = _charStart;
charEnd = _charEnd;
encoder = _encoder;
setEncoder = _setEncoder;
bUsedEncoder = false;
bFallingBack = false;
iRecursionCount = 0;
}
示例5: GetByteCount
// For NLS Encodings, workhorse takes an encoder (may be null)
// Always validate parameters before calling internal version, which will only assert.
internal virtual unsafe int GetByteCount(char* chars, int count, EncoderNLS encoder)
{
Debug.Assert(chars != null);
Debug.Assert(count >= 0);
return GetByteCount(chars, count);
}
示例6: GetByteCount
// Our workhorse version
internal override unsafe int GetByteCount(char* chars, int count, EncoderNLS baseEncoder)
{
// Use null pointer to ask GetBytes for count
return GetBytes(chars, count, null, 0, baseEncoder);
}
示例7: GetBytes
[System.Security.SecurityCritical] // auto-generated
internal override unsafe int GetBytes(char* chars, int charCount,
byte* bytes, int byteCount, EncoderNLS baseEncoder)
{
// Just need to ASSERT, this is called by something else internal that checked parameters already
Contract.Assert(chars != null, "[ISO2022Encoding.GetBytes]chars is null");
Contract.Assert(byteCount >= 0, "[ISO2022Encoding.GetBytes]byteCount is negative");
Contract.Assert(charCount >= 0, "[ISO2022Encoding.GetBytes]charCount is negative");
// Assert because we shouldn't be able to have a null encoder.
Contract.Assert(encoderFallback != null, "[ISO2022Encoding.GetBytes]Attempting to use null encoder fallback");
// Fix our encoder
ISO2022Encoder encoder = (ISO2022Encoder)baseEncoder;
// Our return value
int iCount = 0;
switch(CodePage)
{
case 50220:
case 50221:
case 50222:
iCount = GetBytesCP5022xJP( chars, charCount, bytes, byteCount, encoder );
break;
case 50225:
iCount = GetBytesCP50225KR( chars, charCount, bytes, byteCount, encoder );
break;
// Everett had 50227 the same as 936
/* case 50227:
iCount = GetBytesCP50227CN( chars, charCount, bytes, byteCount, encoder );
break;
*/
case 52936:
iCount = GetBytesCP52936( chars, charCount, bytes, byteCount, encoder );
break;
}
return iCount;
}
示例8: GetByteCount
//
// End of standard methods copied from EncodingNLS.cs
//
internal override unsafe int GetByteCount(char* chars, int count, EncoderNLS encoder)
{
// Start by assuming each char gets 2 bytes
int byteCount = count << 1;
// Check for overflow in byteCount
// (If they were all invalid chars, this would actually be wrong,
// but that's a ridiculously large # so we're not concerned about that case)
if (byteCount < 0)
throw new ArgumentOutOfRangeException("count", "GetByteCountOverflow");
char* charStart = chars;
char* charEnd = chars + count;
char charLeftOver = (char)0;
bool wasHereBefore = false;
// Need -1 to check 2 at a time. If we have an even #, longChars will go
// from longEnd - 1/2 long to longEnd + 1/2 long. If we're odd, longChars
// will go from longEnd - 1 long to longEnd. (Might not get to use this)
ulong* longEnd = (ulong*)(charEnd - 3);
// For fallback we may need a fallback buffer
EncoderFallbackBuffer fallbackBuffer = null;
if (encoder != null)
{
charLeftOver = encoder.charLeftOver;
// Assume extra bytes to encode charLeftOver if it existed
if (charLeftOver > 0)
byteCount += 2;
// We mustn't have left over fallback data when counting
if (encoder.InternalHasFallbackBuffer)
{
fallbackBuffer = encoder.FallbackBuffer;
if (fallbackBuffer.Remaining > 0)
throw new ArgumentException("EncoderFallbackNotEmpty");
// Set our internal fallback interesting things.
fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, false);
}
}
char ch;
TryAgain:
while (((ch = (fallbackBuffer == null) ? (char)0 : fallbackBuffer.InternalGetNextChar()) != 0) || chars < charEnd)
{
// First unwind any fallback
if (ch == 0)
{
// No fallback, maybe we can do it fast
#if !NO_FAST_UNICODE_LOOP
#if BIGENDIAN // If endianess is backwards then each pair of bytes would be backwards.
if ( bigEndian &&
#else
if (!bigEndian &&
#endif // BIGENDIAN
#if WIN64 // 64 bit CPU needs to be long aligned for this to work.
charLeftOver == 0 && (unchecked((long)chars) & 7) == 0)
#else
charLeftOver == 0 && (unchecked((int)chars) & 3) == 0)
#endif
{
// Need new char* so we can check 4 at a time
ulong* longChars = (ulong*)chars;
while (longChars < longEnd)
{
// See if we potentially have surrogates (0x8000 bit set)
// (We're either big endian on a big endian machine or little endian on
// a little endian machine so this'll work)
if ((0x8000800080008000 & *longChars) != 0)
{
// See if any of these are high or low surrogates (0xd800 - 0xdfff). If the high
// 5 bits looks like 11011, then its a high or low surrogate.
// We do the & f800 to filter the 5 bits, then ^ d800 to ensure the 0 isn't set.
// Note that we expect BMP characters to be more common than surrogates
// & each char with 11111... then ^ with 11011. Zeroes then indicate surrogates
ulong uTemp = (0xf800f800f800f800 & *longChars) ^ 0xd800d800d800d800;
// Check each of the 4 chars. 0 for those 16 bits means it was a surrogate
// but no clue if they're high or low.
// If each of the 4 characters are non-zero, then none are surrogates.
if ((uTemp & 0xFFFF000000000000) == 0 ||
(uTemp & 0x0000FFFF00000000) == 0 ||
(uTemp & 0x00000000FFFF0000) == 0 ||
(uTemp & 0x000000000000FFFF) == 0)
{
// It has at least 1 surrogate, but we don't know if they're high or low surrogates,
// or if there's 1 or 4 surrogates
//.........这里部分代码省略.........
示例9: GetByteCount
[System.Security.SecurityCritical] // auto-generated
public override unsafe int GetByteCount(char* chars, int count, EncoderNLS encoder)
{
// Just need to ASSERT, this is called by something else internal that checked parameters already
Debug.Assert(count >= 0, "[DBCSCodePageEncoding.GetByteCount]count is negative");
Debug.Assert(chars != null, "[DBCSCodePageEncoding.GetByteCount]chars is null");
// Assert because we shouldn't be able to have a null encoder.
Debug.Assert(EncoderFallback != null, "[DBCSCodePageEncoding.GetByteCount]Attempting to use null fallback");
CheckMemorySection();
// Get any left over characters
char charLeftOver = (char)0;
if (encoder != null)
{
charLeftOver = encoder.charLeftOver;
// Only count if encoder.m_throwOnOverflow
if (encoder.InternalHasFallbackBuffer && encoder.FallbackBuffer.Remaining > 0)
throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, EncodingName, encoder.Fallback.GetType()));
}
// prepare our end
int byteCount = 0;
char* charEnd = chars + count;
// For fallback we will need a fallback buffer
EncoderFallbackBuffer fallbackBuffer = null;
EncoderFallbackBufferHelper fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer);
// We may have a left over character from last time, try and process it.
if (charLeftOver > 0)
{
Debug.Assert(Char.IsHighSurrogate(charLeftOver), "[DBCSCodePageEncoding.GetByteCount]leftover character should be high surrogate");
Debug.Assert(encoder != null,
"[DBCSCodePageEncoding.GetByteCount]Expect to have encoder if we have a charLeftOver");
// Since left over char was a surrogate, it'll have to be fallen back.
// Get Fallback
fallbackBuffer = encoder.FallbackBuffer;
fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer);
fallbackHelper.InternalInitialize(chars, charEnd, encoder, false);
// This will fallback a pair if *chars is a low surrogate
fallbackHelper.InternalFallback(charLeftOver, ref chars);
}
// Now we may have fallback char[] already (from the encoder)
// We have to use fallback method.
char ch;
while ((ch = (fallbackBuffer == null) ? '\0' : fallbackHelper.InternalGetNextChar()) != 0 ||
chars < charEnd)
{
// First unwind any fallback
if (ch == 0)
{
// No fallback, just get next char
ch = *chars;
chars++;
}
// get byte for this char
ushort sTemp = mapUnicodeToBytes[ch];
// Check for fallback, this'll catch surrogate pairs too.
if (sTemp == 0 && ch != (char)0)
{
if (fallbackBuffer == null)
{
// Initialize the buffer
if (encoder == null)
fallbackBuffer = EncoderFallback.CreateFallbackBuffer();
else
fallbackBuffer = encoder.FallbackBuffer;
fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer);
fallbackHelper.InternalInitialize(charEnd - count, charEnd, encoder, false);
}
// Get Fallback
fallbackHelper.InternalFallback(ch, ref chars);
continue;
}
// We'll use this one
byteCount++;
if (sTemp >= 0x100)
byteCount++;
}
return (int)byteCount;
}
示例10: GetBytes
internal virtual unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS encoder)
{
return this.GetBytes(chars, charCount, bytes, byteCount);
}
示例11: GetBytes
[System.Security.SecurityCritical] // auto-generated
internal override unsafe int GetBytes(char* chars, int charCount,
byte* bytes, int byteCount, EncoderNLS baseEncoder)
{
Contract.Assert(byteCount >=0, "[UTF7Encoding.GetBytes]byteCount >=0");
Contract.Assert(chars!=null, "[UTF7Encoding.GetBytes]chars!=null");
Contract.Assert(charCount >=0, "[UTF7Encoding.GetBytes]charCount >=0");
// Get encoder info
UTF7Encoding.Encoder encoder = (UTF7Encoding.Encoder)baseEncoder;
// Default bits & count
int bits = 0;
int bitCount = -1;
// prepare our helpers
Encoding.EncodingByteBuffer buffer = new Encoding.EncodingByteBuffer(
this, encoder, bytes, byteCount, chars, charCount);
if (encoder != null)
{
bits = encoder.bits;
bitCount = encoder.bitCount;
// May have had too many left over
while (bitCount >= 6)
{
bitCount -= 6;
// If we fail we'll never really have enough room
if (!buffer.AddByte(base64Bytes[(bits >> bitCount) & 0x3F]))
ThrowBytesOverflow(encoder, buffer.Count == 0);
}
}
while (buffer.MoreData)
{
char currentChar = buffer.GetNextChar();
if (currentChar < 0x80 && directEncode[currentChar])
{
if (bitCount >= 0)
{
if (bitCount > 0)
{
// Try to add the next byte
if (!buffer.AddByte(base64Bytes[bits << 6 - bitCount & 0x3F]))
break; // Stop here, didn't throw
bitCount = 0;
}
// Need to get emit '-' and our char, 2 bytes total
if (!buffer.AddByte((byte)'-'))
break; // Stop here, didn't throw
bitCount = -1;
}
// Need to emit our char
if (!buffer.AddByte((byte)currentChar))
break; // Stop here, didn't throw
}
else if (bitCount < 0 && currentChar == '+')
{
if (!buffer.AddByte((byte)'+', (byte)'-'))
break; // Stop here, didn't throw
}
else
{
if (bitCount < 0)
{
// Need to emit a + and 12 bits (3 bytes)
// Only 12 of the 16 bits will be emitted this time, the other 4 wait 'til next time
if (!buffer.AddByte((byte)'+'))
break; // Stop here, didn't throw
// We're now in bit mode, but haven't stored data yet
bitCount = 0;
}
// Add our bits
bits = bits << 16 | currentChar;
bitCount += 16;
while (bitCount >= 6)
{
bitCount -= 6;
if (!buffer.AddByte(base64Bytes[(bits >> bitCount) & 0x3F]))
{
bitCount += 6; // We didn't use these bits
currentChar = buffer.GetNextChar(); // We're processing this char still, but AddByte
// --'d it when we ran out of space
break; // Stop here, not enough room for bytes
}
}
if (bitCount >= 6)
break; // Didn't have room to encode enough bits
}
}
//.........这里部分代码省略.........
示例12: GetByteCount
[System.Security.SecurityCritical] // auto-generated
public override unsafe int GetByteCount(char* chars, int count, EncoderNLS encoder)
{
// Just call GetBytes() with null bytes
return GetBytes(chars, count, null, 0, encoder);
}
示例13: GetBytes
[System.Security.SecurityCritical] // auto-generated
public override unsafe int GetBytes(char* chars, int charCount,
byte* bytes, int byteCount, EncoderNLS encoder)
{
// Just need to ASSERT, this is called by something else internal that checked parameters already
// We'll allow null bytes as a count
// Debug.Assert(bytes != null, "[GB18030Encoding.GetBytes]bytes is null");
Debug.Assert(byteCount >= 0, "[GB18030Encoding.GetBytes]byteCount is negative");
Debug.Assert(chars != null, "[GB18030Encoding.GetBytes]chars is null");
Debug.Assert(charCount >= 0, "[GB18030Encoding.GetBytes]charCount is negative");
// Assert because we shouldn't be able to have a null encoder.
Debug.Assert(EncoderFallback != null, "[GB18030Encoding.GetBytes]Attempting to use null encoder fallback");
// Get any left over characters
char charLeftOver = (char)0;
if (encoder != null)
charLeftOver = encoder.charLeftOver;
// prepare our helpers
EncodingByteBuffer buffer = new EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
// Try again if we were MustFlush
TryAgain:
// Go ahead and do it, including the fallback.
while (buffer.MoreData)
{
// Get next char
char ch = buffer.GetNextChar();
// Have to check for charLeftOver
if (charLeftOver != 0)
{
Debug.Assert(Char.IsHighSurrogate(charLeftOver),
"[GB18030Encoding.GetBytes] leftover character should be high surrogate, not 0x" + ((int)charLeftOver).ToString("X4", CultureInfo.InvariantCulture));
// If our next char isn't a low surrogate, then we need to do fallback.
if (!Char.IsLowSurrogate(ch))
{
// No low surrogate, fallback high surrogate & try this one again
buffer.MovePrevious(false); // (Ignoring this character, don't throw)
if (!buffer.Fallback(charLeftOver))
{
charLeftOver = (char)0;
break;
}
charLeftOver = (char)0;
continue;
}
else
{
// Next is a surrogate, add it as surrogate pair
// Need 4 bytes for surrogates
// Get our offset
int offset = ((charLeftOver - 0xd800) << 10) + (ch - 0xdc00);
byte byte4 = (byte)((offset % 0x0a) + 0x30);
offset /= 0x0a;
byte byte3 = (byte)((offset % 0x7e) + 0x81);
offset /= 0x7e;
byte byte2 = (byte)((offset % 0x0a) + 0x30);
offset /= 0x0a;
Debug.Assert(offset < 0x6f,
"[GB18030Encoding.GetBytes](1) Expected offset < 0x6f, not 0x" + offset.ToString("X2", CultureInfo.InvariantCulture));
charLeftOver = (char)0;
if (!buffer.AddByte((byte)(offset + 0x90), byte2, byte3, byte4))
{
// Didn't work, need to back up for both surrogates (AddByte already backed up one)
buffer.MovePrevious(false); // (don't throw)
break;
}
}
charLeftOver = '\0';
}
// ASCII's easiest
else if (ch <= 0x7f)
{
// Need a byte
if (!buffer.AddByte((byte)ch))
break;
}
// See if its a surrogate pair
else if (Char.IsHighSurrogate(ch))
{
// Remember it for next time
charLeftOver = ch;
}
else if (Char.IsLowSurrogate(ch))
{
// Low surrogates should've been found already
if (!buffer.Fallback(ch))
break;
}
else
{
// Not surrogate or ASCII, get value
ushort iBytes = mapUnicodeToBytes[ch];
//.........这里部分代码省略.........
示例14: InternalInitialize
// Set the above values
// This can't be part of the constructor because EncoderFallbacks would have to know how to impliment these.
internal unsafe void InternalInitialize(char* charStart, char* charEnd, EncoderNLS encoder, bool setEncoder)
{
this.charStart = charStart;
this.charEnd = charEnd;
this.encoder = encoder;
this.setEncoder = setEncoder;
this.bUsedEncoder = false;
this.bFallingBack = false;
this.iRecursionCount = 0;
}
示例15: GetBytes
internal override unsafe int GetBytes(char *chars, int charCount,
byte* bytes, int byteCount, EncoderNLS encoder)
{
BCLDebug.Assert(chars!=null, "[UTF32Encoding.GetBytes]chars!=null");
BCLDebug.Assert(bytes!=null, "[UTF32Encoding.GetBytes]bytes!=null");
BCLDebug.Assert(byteCount >=0, "[UTF32Encoding.GetBytes]byteCount >=0");
BCLDebug.Assert(charCount >=0, "[UTF32Encoding.GetBytes]charCount >=0");
char* charStart = chars;
char* charEnd = chars + charCount;
byte* byteStart = bytes;
byte* byteEnd = bytes + byteCount;
char highSurrogate = '\0';
// For fallback we may need a fallback buffer
EncoderFallbackBuffer fallbackBuffer = null;
if (encoder != null)
{
highSurrogate = encoder.charLeftOver;
fallbackBuffer = encoder.FallbackBuffer;
// We mustn't have left over fallback data when not converting
if (encoder.m_throwOnOverflow && fallbackBuffer.Remaining > 0)
throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty",
this.EncodingName, encoder.Fallback.GetType()));
}
else
{
fallbackBuffer = this.encoderFallback.CreateFallbackBuffer();
}
// Set our internal fallback interesting things.
fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true);
char ch;
TryAgain:
while (((ch = fallbackBuffer.InternalGetNextChar()) != 0) || chars < charEnd)
{
// First unwind any fallback
if (ch == 0)
{
// No fallback, just get next char
ch = *chars;
chars++;
}
// Do we need a low surrogate?
if (highSurrogate != '\0')
{
//
// In previous char, we encountered a high surrogate, so we are expecting a low surrogate here.
//
if (Char.IsLowSurrogate(ch))
{
// Is it a legal one?
uint iTemp = GetSurrogate(highSurrogate, ch);
highSurrogate = '\0';
//
// One surrogate pair will be translated into 4 bytes UTF32.
//
if (bytes+3 >= byteEnd)
{
// Don't have 4 bytes
if (fallbackBuffer.bFallingBack)
{
fallbackBuffer.MovePrevious(); // Aren't using these 2 fallback chars
fallbackBuffer.MovePrevious();
}
else
{
// If we don't have enough room, then either we should've advanced a while
// or we should have bytes==byteStart and throw below
BCLDebug.Assert(chars > charStart + 1 || bytes == byteStart,
"[UnicodeEncoding.GetBytes]Expected chars to have when no room to add surrogate pair");
chars-=2; // Aren't using those 2 chars
}
ThrowBytesOverflow(encoder, bytes == byteStart); // Throw maybe (if no bytes written)
highSurrogate = (char)0; // Nothing left over (we backed up to start of pair if supplimentary)
break;
}
if (bigEndian)
{
*(bytes++) = (byte)(0x00);
*(bytes++) = (byte)(iTemp >> 16); // Implies & 0xFF, which isn't needed cause high are all 0
*(bytes++) = (byte)(iTemp >> 8); // Implies & 0xFF
*(bytes++) = (byte)(iTemp); // Implies & 0xFF
}
else
{
*(bytes++) = (byte)(iTemp); // Implies & 0xFF
*(bytes++) = (byte)(iTemp >> 8); // Implies & 0xFF
*(bytes++) = (byte)(iTemp >> 16); // Implies & 0xFF, which isn't needed cause high are all 0
*(bytes++) = (byte)(0x00);
}
continue;
}
//.........这里部分代码省略.........