public void Defaults () { EncoderReplacementFallback f = new EncoderReplacementFallback (); Assert.AreEqual ("?", f.DefaultString, "#1"); Assert.AreEqual (1, f.MaxCharCount, "#2"); f = new EncoderReplacementFallback (String.Empty); Assert.AreEqual (String.Empty, f.DefaultString, "#3"); Assert.AreEqual (0, f.MaxCharCount, "#4"); f = Encoding.UTF8.EncoderFallback as EncoderReplacementFallback; Assert.IsNotNull (f, "#5"); Assert.AreEqual ("\uFFFD", f.DefaultString, "#6"); Assert.AreEqual (1, f.MaxCharCount, "#7"); // after beta2 this test became invalid. //f = new MyEncoding ().EncoderFallback as EncoderReplacementFallback; //Assert.IsNotNull (f, "#8"); //Assert.AreEqual (String.Empty, f.DefaultString, "#9"); //Assert.AreEqual (0, f.MaxCharCount, "#10"); f = EncoderFallback.ReplacementFallback as EncoderReplacementFallback; Assert.AreEqual ("?", f.DefaultString, "#11"); Assert.AreEqual (1, f.MaxCharCount, "#12"); }
// // End of standard methods copied from EncodingNLS.cs // // GetByteCount // Note: We start by assuming that the output will be the same as count. Having // an encoder or fallback may change that assumption internal override unsafe int GetByteCount(char *chars, int charCount, EncoderNLS encoder) { char charLeftOver = (char)0; EncoderReplacementFallback fallback = null; // Start by assuming default count, then +/- for fallback characters char *charEnd = chars + charCount; // For fallback we may need a fallback buffer, we know we aren't default fallback. EncoderFallbackBuffer fallbackBuffer = null; if (encoder != null) { charLeftOver = encoder.charLeftOver; fallback = encoder.Fallback as EncoderReplacementFallback; // We mustn't have left over fallback data when counting if (encoder.InternalHasFallbackBuffer) { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException("EncoderFallbackNotEmpty"); } // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); } // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert } else { fallback = this.EncoderFallback as EncoderReplacementFallback; } // If we have an encoder AND we aren't using default fallback, // then we may have a complicated count. if (fallback != null && fallback.MaxCharCount == 1) { // Replacement fallback encodes surrogate pairs as two ?? (or two whatever), so return size is always // same as input size. // Note that no existing SBCS code pages map code points to supplimentary characters, so this is easy. // We could however have 1 extra byte if the last call had an encoder and a funky fallback and // if we don't use the funky fallback this time. // Do we have an extra char left over from last time? if (charLeftOver > 0) { charCount++; } return(charCount); } // Count is more complicated if you have a funky fallback // For fallback we may need a fallback buffer, we know we're not default fallback int byteCount = 0; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); // This will fallback a pair if *chars is a low surrogate fallbackBuffer.InternalFallback(charLeftOver, ref chars); } // Now we may have fallback char[] already from the encoder // Go ahead and do it, including the fallback. char ch; while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // Check for fallback, this'll catch surrogate pairs too. // no chars >= 0x80 are allowed. if (ch > 0x7f) { if (fallbackBuffer == null) { // Initialize the buffer if (encoder == null) { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, false); } // Get Fallback fallbackBuffer.InternalFallback(ch, ref chars); continue; } // We'll use this one byteCount++; } return(byteCount); }
/// <summary>Initializes a new instance of the <see cref="T:System.Text.EncoderReplacementFallbackBuffer" /> class using the value of a <see cref="T:System.Text.EncoderReplacementFallback" /> object.</summary> /// <param name="fallback">A <see cref="T:System.Text.EncoderReplacementFallback" /> object. </param> // Token: 0x060066FD RID: 26365 RVA: 0x0015AD43 File Offset: 0x00158F43 public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { this.strDefault = fallback.DefaultString + fallback.DefaultString; }
public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { Contract.Requires(fallback != null); }
internal 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 Debug.Assert(bytes != null, "[ASCIIEncoding.GetBytes]bytes is null"); Debug.Assert(byteCount >= 0, "[ASCIIEncoding.GetBytes]byteCount is negative"); Debug.Assert(chars != null, "[ASCIIEncoding.GetBytes]chars is null"); Debug.Assert(charCount >= 0, "[ASCIIEncoding.GetBytes]charCount is negative"); // Assert because we shouldn't be able to have a null encoder. Debug.Assert(encoderFallback != null, "[ASCIIEncoding.GetBytes]Attempting to use null encoder fallback"); // Get any left over characters char charLeftOver = (char)0; EncoderReplacementFallback fallback = null; // For fallback we may need a fallback buffer, we know we aren't default fallback. EncoderFallbackBuffer fallbackBuffer = null; char *charsForFallback; // prepare our end char *charEnd = chars + charCount; byte *byteStart = bytes; char *charStart = chars; if (encoder != null) { charLeftOver = encoder.charLeftOver; fallback = encoder.Fallback as EncoderReplacementFallback; // We mustn't have left over fallback data when counting if (encoder.InternalHasFallbackBuffer) { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); } // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } Debug.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[ASCIIEncoding.GetBytes]leftover character should be high surrogate"); // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[ASCIICodePageEncoding.GetBytes]Expected empty fallback buffer"); } else { fallback = this.EncoderFallback as EncoderReplacementFallback; } // See if we do the fast default or slightly slower fallback if (fallback != null && fallback.MaxCharCount == 1) { // Fast version char cReplacement = fallback.DefaultString[0]; // Check for replacements in range, otherwise fall back to slow version. if (cReplacement <= (char)0x7f) { // We should have exactly as many output bytes as input bytes, unless there's a left // over character, in which case we may need one more. // If we had a left over character will have to add a ? (This happens if they had a funky // fallback last time, but not this time.) (We can't spit any out though // because with fallback encoder each surrogate is treated as a seperate code point) if (charLeftOver > 0) { // Have to have room // Throw even if doing no throw version because this is just 1 char, // so buffer will never be big enough if (byteCount == 0) { ThrowBytesOverflow(encoder, true); } // This'll make sure we still have more room and also make sure our return value is correct. *(bytes++) = (byte)cReplacement; byteCount--; // We used one of the ones we were counting. } // This keeps us from overrunning our output buffer if (byteCount < charCount) { // Throw or make buffer smaller? ThrowBytesOverflow(encoder, byteCount < 1); // Just use what we can charEnd = chars + byteCount; } // We just do a quick copy while (chars < charEnd) { char ch2 = *(chars++); if (ch2 >= 0x0080) { *(bytes++) = (byte)cReplacement; } else { *(bytes++) = unchecked ((byte)(ch2)); } } // Clear encoder if (encoder != null) { encoder.charLeftOver = (char)0; encoder.m_charsUsed = (int)(chars - charStart); } return((int)(bytes - byteStart)); } } // Slower version, have to do real fallback. // prepare our end byte *byteEnd = bytes + byteCount; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Initialize the buffer Debug.Assert(encoder != null, "[ASCIIEncoding.GetBytes]Expected non null encoder if we have surrogate left over"); fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback // This will fallback a pair if *chars is a low surrogate charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; } // Now we may have fallback char[] already from the encoder // Go ahead and do it, including the fallback. char ch; while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // Check for fallback, this'll catch surrogate pairs too. // All characters >= 0x80 must fall back. if (ch > 0x7f) { // Initialize the buffer if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } // Get Fallback charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered fallbackBuffer.InternalFallback(ch, ref charsForFallback); chars = charsForFallback; // Go ahead & continue (& do the fallback) continue; } // We'll use this one // Bounds check if (bytes >= byteEnd) { // didn't use this char, we'll throw or use buffer if (fallbackBuffer == null || fallbackBuffer.bFallingBack == false) { Debug.Assert(chars > charStart || bytes == byteStart, "[ASCIIEncoding.GetBytes]Expected chars to have advanced already."); chars--; // don't use last char } else { fallbackBuffer.MovePrevious(); } // Are we throwing or using buffer? ThrowBytesOverflow(encoder, bytes == byteStart); // throw? break; // don't throw, stop } // Go ahead and add it *bytes = unchecked ((byte)ch); bytes++; } // Need to do encoder stuff if (encoder != null) { // Fallback stuck it in encoder if necessary, but we have to clear MustFlush cases if (fallbackBuffer != null && !fallbackBuffer.bUsedEncoder) { // Clear it in case of MustFlush encoder.charLeftOver = (char)0; } // Set our chars used count encoder.m_charsUsed = (int)(chars - charStart); } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0 || (encoder != null && !encoder.m_throwOnOverflow), "[ASCIIEncoding.GetBytes]Expected Empty fallback buffer at end"); return((int)(bytes - byteStart)); }
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 Debug.Assert(bytes != null, "[SBCSCodePageEncoding.GetBytes]bytes is null"); Debug.Assert(byteCount >= 0, "[SBCSCodePageEncoding.GetBytes]byteCount is negative"); Debug.Assert(chars != null, "[SBCSCodePageEncoding.GetBytes]chars is null"); Debug.Assert(charCount >= 0, "[SBCSCodePageEncoding.GetBytes]charCount is negative"); // Assert because we shouldn't be able to have a null encoder. Debug.Assert(EncoderFallback != null, "[SBCSCodePageEncoding.GetBytes]Attempting to use null encoder fallback"); CheckMemorySection(); // Need to test fallback EncoderReplacementFallback fallback = null; // Get any left over characters char charLeftOver = (char)0; if (encoder != null) { charLeftOver = encoder.charLeftOver; Debug.Assert(charLeftOver == 0 || char.IsHighSurrogate(charLeftOver), "[SBCSCodePageEncoding.GetBytes]leftover character should be high surrogate"); fallback = encoder.Fallback as EncoderReplacementFallback; // Verify that we have no fallbackbuffer, for SBCS its always empty, so just assert Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[SBCSCodePageEncoding.GetBytes]Expected empty fallback buffer at start"); // if (encoder.m_throwOnOverflow && encoder.InternalHasFallbackBuffer && // encoder.FallbackBuffer.Remaining > 0) // throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", // EncodingName, encoder.Fallback.GetType())); } else { // If we aren't using default fallback then we may have a complicated count. fallback = EncoderFallback as EncoderReplacementFallback; } // prepare our end char *charEnd = chars + charCount; byte *byteStart = bytes; char *charStart = chars; // See if we do the fast default or slightly slower fallback if (fallback != null && fallback.MaxCharCount == 1) { // Make sure our fallback character is valid first byte bReplacement = _mapUnicodeToBytes[fallback.DefaultString[0]]; // Check for replacements in range, otherwise fall back to slow version. if (bReplacement != 0) { // We should have exactly as many output bytes as input bytes, unless there's a leftover // character, in which case we may need one more. // If we had a leftover character we will have to add a ? (This happens if they had a funky // fallback last time, but not this time. We can't spit any out though, // because with fallback encoder each surrogate is treated as a separate code point) if (charLeftOver > 0) { // Have to have room // Throw even if doing no throw version because this is just 1 char, // so buffer will never be big enough if (byteCount == 0) { ThrowBytesOverflow(encoder, true); } // This'll make sure we still have more room and also make sure our return value is correct. *(bytes++) = bReplacement; byteCount--; // We used one of the ones we were counting. } // This keeps us from overrunning our output buffer if (byteCount < charCount) { // Throw or make buffer smaller? ThrowBytesOverflow(encoder, byteCount < 1); // Just use what we can charEnd = chars + byteCount; } // Simple way while (chars < charEnd) { char ch2 = *chars; chars++; byte bTemp = _mapUnicodeToBytes[ch2]; // Check for fallback if (bTemp == 0 && ch2 != (char)0) { *bytes = bReplacement; } else { *bytes = bTemp; } bytes++; } // Clear encoder if (encoder != null) { encoder.charLeftOver = (char)0; encoder.m_charsUsed = (int)(chars - charStart); } return((int)(bytes - byteStart)); } } // Slower version, have to do real fallback. // For fallback we may need a fallback buffer, we know we aren't default fallback EncoderFallbackBuffer fallbackBuffer = null; // prepare our end byte *byteEnd = bytes + byteCount; EncoderFallbackBufferHelper fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback Debug.Assert(encoder != null, "[SBCSCodePageEncoding.GetBytes]Expect to have encoder if we have a charLeftOver"); fallbackBuffer = encoder.FallbackBuffer; fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); fallbackHelper.InternalInitialize(chars, charEnd, encoder, true); // This will fallback a pair if *chars is a low surrogate fallbackHelper.InternalFallback(charLeftOver, ref chars); if (fallbackBuffer.Remaining > byteEnd - bytes) { // Throw it, if we don't have enough for this we never will ThrowBytesOverflow(encoder, true); } } // Now we may have fallback char[] already from the encoder fallback above // Go ahead and do it, including the fallback. 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 byte bTemp = _mapUnicodeToBytes[ch]; // Check for fallback, this'll catch surrogate pairs too. if (bTemp == 0 && ch != (char)0) { // Get Fallback if (fallbackBuffer == null) { // Create & init fallback buffer if (encoder == null) { fallbackBuffer = EncoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); // chars has moved so we need to remember figure it out so Exception fallback // index will be correct fallbackHelper.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } // Make sure we have enough room. Each fallback char will be 1 output char // (or recursion exception will be thrown) fallbackHelper.InternalFallback(ch, ref chars); if (fallbackBuffer.Remaining > byteEnd - bytes) { // Didn't use this char, reset it Debug.Assert(chars > charStart, "[SBCSCodePageEncoding.GetBytes]Expected chars to have advanced (fallback)"); chars--; fallbackHelper.InternalReset(); // Throw it & drop this data ThrowBytesOverflow(encoder, chars == charStart); break; } continue; } // We'll use this one // Bounds check if (bytes >= byteEnd) { // didn't use this char, we'll throw or use buffer Debug.Assert(fallbackBuffer == null || fallbackHelper.bFallingBack == false, "[SBCSCodePageEncoding.GetBytes]Expected to NOT be falling back"); if (fallbackBuffer == null || fallbackHelper.bFallingBack == false) { Debug.Assert(chars > charStart, "[SBCSCodePageEncoding.GetBytes]Expected chars to have advanced (normal)"); chars--; // don't use last char } ThrowBytesOverflow(encoder, chars == charStart); // throw ? break; // don't throw, stop } // Go ahead and add it *bytes = bTemp; bytes++; } // encoder stuff if we have one if (encoder != null) { // Fallback stuck it in encoder if necessary, but we have to clear MustFlush cases if (fallbackBuffer != null && !fallbackHelper.bUsedEncoder) { // Clear it in case of MustFlush encoder.charLeftOver = (char)0; } // Set our chars used count encoder.m_charsUsed = (int)(chars - charStart); } // Expect Empty fallback buffer for SBCS Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[SBCSEncoding.GetBytes]Expected Empty fallback buffer at end"); return((int)(bytes - byteStart)); }
/// <summary>Initializes a new instance of the <see cref="T:System.Text.EncoderReplacementFallbackBuffer" /> class using the value of a <see cref="T:System.Text.EncoderReplacementFallback" /> object.</summary><param name="fallback">A <see cref="T:System.Text.EncoderReplacementFallback" /> object. </param> public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { throw new NotImplementedException(); }
public override bool Equals(object value) { EncoderReplacementFallback fallback = value as EncoderReplacementFallback; return((fallback != null) && (this.strDefault == fallback.strDefault)); }
public override bool Equals(object value) { EncoderReplacementFallback f = value as EncoderReplacementFallback; return(f != null && replacement == f.replacement); }
public static Encoding GetEncoding(int codepage, string fallback) { if (fallback == null) throw new ArgumentNullException ("fallback"); var encoderFallback = new EncoderReplacementFallback (fallback); var decoderFallback = new DecoderReplacementFallback (fallback); return Encoding.GetEncoding (codepage, encoderFallback, decoderFallback); }
public static Encoding GetEncoding(string charset, string fallback) { int codepage; if (charset == null) throw new ArgumentNullException ("charset"); if (fallback == null) throw new ArgumentNullException ("fallback"); if ((codepage = GetCodePage (charset)) == -1) throw new NotSupportedException (); var encoderFallback = new EncoderReplacementFallback (fallback); var decoderFallback = new DecoderReplacementFallback (fallback); return Encoding.GetEncoding (codepage, encoderFallback, decoderFallback); }
static void Main(string[] args) { if (args.Length == 0) { Console.Write( "use command like this:\n" + "echo <scp>[-dcp[-fstr]] | qcpc <input> [output]\n" + "example:\n" + "echo 932 - 1200 | qcpc in.txt out.txt\n" + "echo 932 - 936 - % | qcpc conv.txt" ); return; } //============================================== string srcFile = null; string dstFile = null; try { srcFile = args[0]; } catch { } try { dstFile = srcFile; } catch { } try { dstFile = args[1]; } catch { } if (srcFile == null) return; string[] CPPair = Console.ReadLine().Split('-'); var srcCP = Encoding.GetEncoding(0); var dstCP = Encoding.GetEncoding(1200); var erfb = EncoderFallback.ReplacementFallback; var drfb = DecoderFallback.ReplacementFallback; try { srcCP = Encoding.GetEncoding(int.Parse(CPPair[0])); } catch { } try { erfb = new EncoderReplacementFallback(CPPair[2].Trim()); } catch { } try { dstCP = Encoding.GetEncoding(int.Parse(CPPair[1]), erfb, drfb); } catch { } FileStream fr = null; try { fr = new FileStream(srcFile, FileMode.Open); } catch { Console.WriteLine("import file error"); return; } var ibuf = new byte[fr.Length]; fr.Read(ibuf, 0, ibuf.Length); fr.Dispose(); var obuf = Encoding.Convert(srcCP, dstCP, ibuf); var bom = new byte[] { }; switch (dstCP.CodePage) { case 1200: bom = new byte[] { (byte)'\xFF', (byte)'\xFE' }; break; case 1201: bom = new byte[] { (byte)'\xFE', (byte)'\xFF' }; break; case 65001: bom = new byte[] { (byte)'\xEF', (byte)'\xBB', (byte)'\xBF' }; break; default: break; } obuf = bom.Concat(obuf).ToArray(); FileStream fw = null; try { fw = new FileStream(dstFile, FileMode.OpenOrCreate); } catch { Console.WriteLine("export file error"); return; } fw.SetLength(0); fw.Write(obuf, 0, obuf.Length); fw.Dispose(); }
public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { this.strDefault = fallback.DefaultString + fallback.DefaultString; }
public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { // TODO-NULLABLE: NullReferenceException // 2X in case we're a surrogate pair _strDefault = fallback.DefaultString + fallback.DefaultString; }
public override bool Equals(object value) { EncoderReplacementFallback encoderReplacementFallback = value as EncoderReplacementFallback; return(encoderReplacementFallback != null && this.strDefault == encoderReplacementFallback.strDefault); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char ch3; base.CheckMemorySection(); EncoderReplacementFallback encoderFallback = null; char charLeftOver = '\0'; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } char *charEnd = chars + charCount; byte *numPtr = bytes; char *chPtr2 = chars; if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { byte num = this.mapUnicodeToBytes[(int)((byte *)encoderFallback.DefaultString[0])]; if (num != 0) { if (charLeftOver > '\0') { if (byteCount == 0) { base.ThrowBytesOverflow(encoder, true); } bytes++; bytes[0] = num; byteCount--; } if (byteCount < charCount) { base.ThrowBytesOverflow(encoder, byteCount < 1); charEnd = chars + byteCount; } while (chars < charEnd) { char ch2 = chars[0]; chars++; byte num2 = this.mapUnicodeToBytes[(int)((byte *)ch2)]; if ((num2 == 0) && (ch2 != '\0')) { bytes[0] = num; } else { bytes[0] = num2; } bytes++; } if (encoder != null) { encoder.charLeftOver = '\0'; encoder.m_charsUsed = (int)((long)((chars - chPtr2) / 2)); } return((int)((long)((bytes - numPtr) / 1))); } } EncoderFallbackBuffer fallbackBuffer = null; byte *numPtr2 = bytes + byteCount; if (charLeftOver > '\0') { fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); fallbackBuffer.InternalFallback(charLeftOver, ref chars); if (fallbackBuffer.Remaining > ((long)((numPtr2 - bytes) / 1))) { base.ThrowBytesOverflow(encoder, true); } } while (((ch3 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch3 == '\0') { ch3 = chars[0]; chars++; } byte num3 = this.mapUnicodeToBytes[(int)((byte *)ch3)]; if ((num3 == 0) && (ch3 != '\0')) { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } fallbackBuffer.InternalFallback(ch3, ref chars); if (fallbackBuffer.Remaining <= ((long)((numPtr2 - bytes) / 1))) { continue; } chars--; fallbackBuffer.InternalReset(); base.ThrowBytesOverflow(encoder, chars == chPtr2); break; } if (bytes >= numPtr2) { if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack) { chars--; } base.ThrowBytesOverflow(encoder, chars == chPtr2); break; } bytes[0] = num3; bytes++; } if (encoder != null) { if ((fallbackBuffer != null) && !fallbackBuffer.bUsedEncoder) { encoder.charLeftOver = '\0'; } encoder.m_charsUsed = (int)((long)((chars - chPtr2) / 2)); } return((int)((long)((bytes - numPtr) / 1))); }
// Construction public EncoderReplacementFallbackBuffer(EncoderReplacementFallback fallback) { // 2X in case we're a surrogate pair _strDefault = fallback.DefaultString + fallback.DefaultString; }
internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS encoder) { char ch2; base.CheckMemorySection(); EncoderReplacementFallback encoderFallback = null; char charLeftOver = '\0'; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { if (charLeftOver > '\0') { count++; } return(count); } EncoderFallbackBuffer fallbackBuffer = null; int num = 0; char *charEnd = chars + count; 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++; } if ((this.mapUnicodeToBytes[ch2] == 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++; } } return(num); }
// GetByteCount // Note: We start by assuming that the output will be the same as count. Having // an encoder or fallback may change that assumption 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, "[SBCSCodePageEncoding.GetByteCount]count is negative"); Debug.Assert(chars != null, "[SBCSCodePageEncoding.GetByteCount]chars is null"); // Assert because we shouldn't be able to have a null encoder. Debug.Assert(EncoderFallback != null, "[SBCSCodePageEncoding.GetByteCount]Attempting to use null fallback"); CheckMemorySection(); // Need to test fallback EncoderReplacementFallback fallback = null; // Get any left over characters char charLeftOver = (char)0; if (encoder != null) { charLeftOver = encoder.charLeftOver; Debug.Assert(charLeftOver == 0 || char.IsHighSurrogate(charLeftOver), "[SBCSCodePageEncoding.GetByteCount]leftover character should be high surrogate"); fallback = encoder.Fallback as EncoderReplacementFallback; // Verify that we have no fallbackbuffer, actually for SBCS this is always empty, so just assert Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[SBCSCodePageEncoding.GetByteCount]Expected empty fallback buffer at start"); } else { // If we aren't using default fallback then we may have a complicated count. fallback = EncoderFallback as EncoderReplacementFallback; } if ((fallback != null && fallback.MaxCharCount == 1) /* || bIsBestFit*/) { // Replacement fallback encodes surrogate pairs as two ?? (or two whatever), so return size is always // same as input size. // Note that no existing SBCS code pages map code points to supplementary characters, so this is easy. // We could however have 1 extra byte if the last call had an encoder and a funky fallback and // if we don't use the funky fallback this time. // Do we have an extra char left over from last time? if (charLeftOver > 0) { count++; } return(count); } // It had a funky fallback, so it's more complicated // May need buffer later EncoderFallbackBuffer fallbackBuffer = null; // prepare our end int byteCount = 0; char *charEnd = chars + count; EncoderFallbackBufferHelper fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Since leftover char was a surrogate, it'll have to be fallen back. // Get fallback Debug.Assert(encoder != null, "[SBCSCodePageEncoding.GetByteCount]Expect to have encoder if we have a charLeftOver"); 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 // Go ahead and do it, including the fallback. 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 byte bTemp = _mapUnicodeToBytes[ch]; // Check for fallback, this'll catch surrogate pairs too. if (bTemp == 0 && ch != (char)0) { if (fallbackBuffer == null) { // Create & init fallback buffer if (encoder == null) { fallbackBuffer = EncoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); // chars has moved so we need to remember figure it out so Exception fallback // index will be correct fallbackHelper.InternalInitialize(charEnd - count, charEnd, encoder, false); } // Get Fallback fallbackHelper.InternalFallback(ch, ref chars); continue; } // We'll use this one byteCount++; } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[SBCSEncoding.GetByteCount]Expected Empty fallback buffer at end"); return((int)byteCount); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char ch4; char charLeftOver = '\0'; EncoderReplacementFallback encoderFallback = null; EncoderFallbackBuffer fallbackBuffer = null; char *charEnd = chars + charCount; byte *numPtr = bytes; char *charStart = chars; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; if (encoder.InternalHasFallbackBuffer) { fallbackBuffer = encoder.FallbackBuffer; if ((fallbackBuffer.Remaining > 0) && encoder.m_throwOnOverflow) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { char ch2 = encoderFallback.DefaultString[0]; if (ch2 <= '\x007f') { if (charLeftOver > '\0') { if (byteCount == 0) { base.ThrowBytesOverflow(encoder, true); } bytes++; bytes[0] = (byte)ch2; byteCount--; } if (byteCount < charCount) { base.ThrowBytesOverflow(encoder, byteCount < 1); charEnd = chars + byteCount; } while (chars < charEnd) { chars++; char ch3 = chars[0]; if (ch3 >= '\x0080') { bytes++; bytes[0] = (byte)ch2; } else { bytes++; bytes[0] = (byte)ch3; } } if (encoder != null) { encoder.charLeftOver = '\0'; encoder.m_charsUsed = (int)((long)((chars - charStart) / 2)); } return((int)((long)((bytes - numPtr) / 1))); } } byte *numPtr2 = bytes + byteCount; if (charLeftOver > '\0') { fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); fallbackBuffer.InternalFallback(charLeftOver, ref chars); } while (((ch4 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch4 == '\0') { ch4 = chars[0]; chars++; } if (ch4 > '\x007f') { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } fallbackBuffer.InternalFallback(ch4, ref chars); } else { if (bytes >= numPtr2) { if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack) { chars--; } else { fallbackBuffer.MovePrevious(); } base.ThrowBytesOverflow(encoder, bytes == numPtr); break; } bytes[0] = (byte)ch4; bytes++; } } if (encoder != null) { if ((fallbackBuffer != null) && !fallbackBuffer.bUsedEncoder) { encoder.charLeftOver = '\0'; } encoder.m_charsUsed = (int)((long)((chars - charStart) / 2)); } return((int)((long)((bytes - numPtr) / 1))); }
// // End of standard methods copied from EncodingNLS.cs // // GetByteCount // Note: We start by assuming that the output will be the same as count. Having // an encoder or fallback may change that assumption internal override unsafe int GetByteCount(char *chars, int charCount, EncoderNLS encoder) { // Just need to ASSERT, this is called by something else internal that checked parameters already Debug.Assert(charCount >= 0, "[ASCIIEncoding.GetByteCount]count is negative"); Debug.Assert(chars != null, "[ASCIIEncoding.GetByteCount]chars is null"); // Assert because we shouldn't be able to have a null encoder. Debug.Assert(encoderFallback != null, "[ASCIIEncoding.GetByteCount]Attempting to use null fallback encoder"); char charLeftOver = (char)0; EncoderReplacementFallback fallback = null; // Start by assuming default count, then +/- for fallback characters char *charEnd = chars + charCount; // For fallback we may need a fallback buffer, we know we aren't default fallback. EncoderFallbackBuffer fallbackBuffer = null; char *charsForFallback; if (encoder != null) { charLeftOver = encoder.charLeftOver; Debug.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[ASCIIEncoding.GetByteCount]leftover character should be high surrogate"); fallback = encoder.Fallback as EncoderReplacementFallback; // We mustn't have left over fallback data when counting if (encoder.InternalHasFallbackBuffer) { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); } // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); } // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[ASCIICodePageEncoding.GetByteCount]Expected empty fallback buffer"); } else { fallback = this.EncoderFallback as EncoderReplacementFallback; } // If we have an encoder AND we aren't using default fallback, // then we may have a complicated count. if (fallback != null && fallback.MaxCharCount == 1) { // Replacement fallback encodes surrogate pairs as two ?? (or two whatever), so return size is always // same as input size. // Note that no existing SBCS code pages map code points to supplimentary characters, so this is easy. // We could however have 1 extra byte if the last call had an encoder and a funky fallback and // if we don't use the funky fallback this time. // Do we have an extra char left over from last time? if (charLeftOver > 0) { charCount++; } return(charCount); } // Count is more complicated if you have a funky fallback // For fallback we may need a fallback buffer, we know we're not default fallback int byteCount = 0; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { Debug.Assert(Char.IsHighSurrogate(charLeftOver), "[ASCIIEncoding.GetByteCount]leftover character should be high surrogate"); Debug.Assert(encoder != null, "[ASCIIEncoding.GetByteCount]Expected encoder"); // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); // This will fallback a pair if *chars is a low surrogate charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; } // Now we may have fallback char[] already from the encoder // Go ahead and do it, including the fallback. char ch; while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // Check for fallback, this'll catch surrogate pairs too. // no chars >= 0x80 are allowed. if (ch > 0x7f) { if (fallbackBuffer == null) { // Initialize the buffer if (encoder == null) { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, false); } // Get Fallback charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered fallbackBuffer.InternalFallback(ch, ref charsForFallback); chars = charsForFallback; continue; } // We'll use this one byteCount++; } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[ASCIIEncoding.GetByteCount]Expected Empty fallback buffer"); return(byteCount); }
internal override unsafe int GetByteCount(char *chars, int charCount, EncoderNLS encoder) { char ch2; char charLeftOver = '\0'; EncoderReplacementFallback encoderFallback = null; char *charEnd = chars + charCount; EncoderFallbackBuffer fallbackBuffer = null; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; if (encoder.InternalHasFallbackBuffer) { fallbackBuffer = encoder.FallbackBuffer; if ((fallbackBuffer.Remaining > 0) && encoder.m_throwOnOverflow) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); } } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { if (charLeftOver > '\0') { charCount++; } return(charCount); } int num = 0; 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++; } if (ch2 > '\x007f') { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, false); } fallbackBuffer.InternalFallback(ch2, ref chars); } else { num++; } } return(num); }
public EncoderReplacementFallbackBuffer( EncoderReplacementFallback fallback) { if (fallback == null) throw new ArgumentNullException("fallback"); replacement = fallback.DefaultString; current = 0; }