GetCharCount() public method

public GetCharCount ( byte bytes ) : int
bytes byte
return int
Esempio n. 1
0
        /// <summary>
        /// Applies the clang-format replacements (xml) to the current view
        /// </summary>
        private static void ApplyClangFormatReplacements(string replacements, IWpfTextView view)
        {
            // clang-format returns no replacements if input text is empty
            if (replacements.Length == 0)
            {
                return;
            }

            var root              = XElement.Parse(replacements);
            var edit              = view.TextBuffer.CreateEdit();
            int last_offset_utf8  = 0;
            int last_offset_utf16 = 0;

            foreach (XElement replacement in root.Descendants("replacement"))
            {
                int offset_utf8 = int.Parse(replacement.Attribute("offset").Value);
                int length_utf8 = int.Parse(replacement.Attribute("length").Value);
                // convert the multi bytes index to utf16 index
                // assume that offsets is in order.
                int offset_utf16 = enc.GetCharCount(buffer, last_offset_utf8, offset_utf8 - last_offset_utf8) + last_offset_utf16;
                int length_utf16 = enc.GetCharCount(buffer, offset_utf8, length_utf8);
                edit.Replace(offset_utf16, length_utf16, replacement.Value);
                last_offset_utf8  = offset_utf8;
                last_offset_utf16 = offset_utf16;
            }
            edit.Apply();
            view.Selection.Clear();
        }
Esempio n. 2
0
        public void GetCharCount_InvalidLength()
        {
            var strBytes = new[]
            {
                (byte)0
            };

            System.Text.Encoding enc = GetEncoding();
            Assert.Throws <ArgumentException>(() => enc.GetCharCount(strBytes));
        }
Esempio n. 3
0
 private void TestEncoding(Encoding enc, int byteCount, int maxByteCount, byte[] bytes)
 {
     Assert.Equal(byteCount, enc.GetByteCount(s_myChars));
     Assert.Equal(maxByteCount, enc.GetMaxByteCount(s_myChars.Length));
     Assert.Equal(enc.GetBytes(s_myChars), bytes);
     Assert.Equal(enc.GetCharCount(bytes), s_myChars.Length);
     Assert.Equal(enc.GetChars(bytes), s_myChars);
     Assert.Equal(enc.GetString(bytes, 0, bytes.Length), s_myString);
     Assert.NotEqual(0, enc.GetHashCode());
 }
 private char[]/*!*/ DataToChars(int additionalCapacity, Encoding/*!*/ encoding) {
     if (_count == 0) {
         return (additionalCapacity == 0) ? Utils.EmptyChars : new char[additionalCapacity];
     } else if (additionalCapacity == 0) {
         return encoding.GetChars(_data, 0, _count);
     } else {
         var result = new char[encoding.GetCharCount(_data, 0, _count) + additionalCapacity];
         encoding.GetChars(_data, 0, _count, result, 0);
         return result;
     }
 }
    /// <summary>
    /// shift-JIS文字列をUTF-8文字列に変換
    /// </summary>
    /// <param name="shiftStrings"></param>
    /// <returns></returns>
    private string ConvertShiftJIStoUTF8(string shiftStrings)
    {
        System.Text.Encoding shiftJIS = System.Text.Encoding.GetEncoding("shift_jis");
        byte[] shiftBytes             = shiftJIS.GetBytes(shiftStrings);

        System.Text.Encoding utf = System.Text.Encoding.UTF8;
        byte[] convStringData    = System.Text.Encoding.Convert(shiftJIS, utf, shiftBytes);
        char[] convCharData      = new char[utf.GetCharCount(convStringData, 0, convStringData.Length)];

        utf.GetChars(convStringData, 0, convStringData.Length, convCharData, 0);

        return(new string(convCharData));
    }
Esempio n. 6
0
        private string ReEncode(
            System.Text.Encoding fromEncoding,
            System.Text.Encoding toEncoding,
            string text)
        {
            byte[] frombyte = fromEncoding.GetBytes(text);
            byte[] tobyte   = Encoding.Convert(fromEncoding, toEncoding,
                                               frombyte);
            char[] tochar = new char[toEncoding.GetCharCount(tobyte, 0, tobyte.Length)];
            toEncoding.GetChars(tobyte, 0, tobyte.Length, tochar, 0);
            string tostr = new string(tochar);

            return(tostr);
        }
Esempio n. 7
0
        public static String ToEncoding(this String psValue, Encoding poSourceEncoding, Encoding poDestinationEncoding)
        {
            if (String.IsNullOrEmpty(psValue))
            {
                return psValue;
            }

            var loSourceBytes = poSourceEncoding.GetBytes(psValue);
            var loDestinationBytes = Encoding.Convert(poSourceEncoding, poDestinationEncoding, loSourceBytes);
            var loDestinationChars = new char[poDestinationEncoding.GetCharCount(loDestinationBytes, 0, loDestinationBytes.Length)];

            poDestinationEncoding.GetChars(loDestinationBytes, 0, loDestinationChars.Length, loDestinationChars, 0);

            return new string(loDestinationChars);
        }
Esempio n. 8
0
        public unsafe static int GetCharCount(Encoding encoding, byte *bytes, int count)
        {
            Debug.Assert(encoding != null);
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes), Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            Contract.EndContractBlock();

            return(encoding.GetCharCount(bytes, count, decoder: null));
        }
Esempio n. 9
0
 static public int GetCharCount__A_Byte(IntPtr l)
 {
     try {
         System.Text.Encoding self = (System.Text.Encoding)checkSelf(l);
         System.Byte[]        a1;
         checkArray(l, 2, out a1);
         var ret = self.GetCharCount(a1);
         pushValue(l, true);
         pushValue(l, ret);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Esempio n. 10
0
    public static string ConvertAsciiToUtf8(string asciiString)
    {
        System.Text.Encoding ascii = System.Text.Encoding.ASCII;
        System.Text.Encoding utf8  = System.Text.Encoding.UTF8;

        byte[] asciiBytes = ascii.GetBytes(asciiString);
        byte[] utf8Bytes  = System.Text.Encoding.Convert(ascii, utf8,
                                                         asciiBytes);

        char[] utf8Chars = new char[utf8.GetCharCount(utf8Bytes, 0,
                                                      utf8Bytes.Length)];
        utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, utf8Chars, 0);
        string utf8String = new string(utf8Chars);

        return(utf8String);
    }
Esempio n. 11
0
        public void GetCharCount_ValidLength()
        {
            var strBytes = new[]
            {
                (byte)0,
                (byte)3,
                (byte)'a',
                (byte)'b',
                (byte)'c'
            };

            System.Text.Encoding enc = GetEncoding();
            var count = enc.GetCharCount(strBytes);

            Assert.Equal <int>(3, count);
        }
Esempio n. 12
0
        //============
        // 指定コードバイト型配列 ---> ユニコードstring 変換メソッド
        //
        // 内容: 指定コード(例: シフトJISコード)のバイト型配列をユニコードの文字列に変換する。
        //
        // 第1引数: 変換前の文字列を入力指定(byte型配列のシフトJISコードの文字列)
        // 第2引数: 変換前のエンコードを入力指定(シフトJISコード時は、"shift_jis")
        // 第3引数: 変換後の文字列を出力(string型のユニコードの文字列)
        //
        // 戻り値: エラー情報(0から1: 成功、2以上: 失敗)
        // _______________(1: 警告(フォールバック発生))
        //
        // エンコーディングを示すコードページ名(第2引数指定値)
        //   シフトJIS: "shift_jis"
        //   JIS: "iso-2022-jp"
        //   日本語EUC: "euc-jp"
        //   Unicode (UTF-8): "utf-8"
        public static int ChangeBinaryToUnicodeStr(byte[] BeforeBytes, string BeforeEncod, out string UnicodeText)
        {
            // 初期化
            UnicodeText = "";
            int ErrorInfo = 0;             // エラー情報

            // 入力がnull時のエラー処理
            if (BeforeBytes == null || BeforeEncod == null)
            {
                return(2);
            }

            // 文字コードを示すエンコードを生成
            System.Text.Encoding AfterEncodObj  = System.Text.Encoding.Unicode;                  // 変換後エンコード
            System.Text.Encoding BeforeEncodObj = System.Text.Encoding.GetEncoding(BeforeEncod); // 変換前エンコード

            // 指定コードをユニコードに変換
            byte[] AfterBytes = null;
            try
            {
                AfterBytes = System.Text.Encoding.Convert(BeforeEncodObj, AfterEncodObj, BeforeBytes);
            }
            catch (DecoderFallbackException)
            {
                // フォールバックが発生
                ErrorInfo = 1;
            }
            catch (EncoderFallbackException)
            {
                // フォールバックが発生
                ErrorInfo = 1;
            }
            catch (ArgumentNullException)
            {
                // 入出力文字列格納用オブジェクト または encoding が null を参照し、失敗
                ErrorInfo = 2;
            }


            // バイト型配列のユニコード文字列をstring型に設定
            char[] AfterChars = new char[AfterEncodObj.GetCharCount(AfterBytes, 0, AfterBytes.Length)];
            AfterEncodObj.GetChars(AfterBytes, 0, AfterBytes.Length, AfterChars, 0);
            UnicodeText = new string(AfterChars);

            return(ErrorInfo);
        }
Esempio n. 13
0
        private static void ConvertEncoding(Encoding unicode, Encoding ascii)
        {
            string storyline = "Cobb&#x27;s rare ability";
            var sourceBytes = ascii.GetBytes(storyline);
            var destBytes = Encoding.Convert(ascii, unicode, sourceBytes);

            var destChars = new char[unicode.GetCharCount(destBytes, 0, destBytes.Length)];
            unicode.GetChars(destBytes, 0, destBytes.Length, destChars, 0);
            var newString = new string(destChars);

            Console.WriteLine("\n-------------------------------");
            Console.WriteLine(DateTime.Now);
            Console.WriteLine(unicode.EncodingName);
            Console.WriteLine(ascii.EncodingName);
            Console.WriteLine("===============================");
            Console.WriteLine(newString);
        }
Esempio n. 14
0
        public override unsafe int GetCharCount(byte *bytes, int count, bool flush)
        {
            ArgumentNullException.ThrowIfNull(bytes);

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count),
                                                      SR.ArgumentOutOfRange_NeedNonNegNum);
            }

            // Remember the flush
            _mustFlush       = flush;
            _throwOnOverflow = true;

            // By default just call the encoding version, no flush by default
            Debug.Assert(_encoding is not null);
            return(_encoding.GetCharCount(bytes, count, this));
        }
Esempio n. 15
0
        public override unsafe int GetCharCount(byte* bytes, int count, bool flush)
        {
            // Validate parameters
            if (bytes == null)
                throw new ArgumentNullException(nameof(bytes),
                      SR.ArgumentNull_Array);

            if (count < 0)
                throw new ArgumentOutOfRangeException(nameof(count),
                      SR.ArgumentOutOfRange_NeedNonNegNum);

            // Remember the flush
            _mustFlush = flush;
            _throwOnOverflow = true;

            // By default just call the encoding version, no flush by default
            Debug.Assert(_encoding != null);
            return _encoding.GetCharCount(bytes, count, this);
        }
        public static int GetCharCount(this Encoding encoding, ReadOnlySpan <byte> bytes)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (_getCharCountShim != null)
            {
                return(_getCharCountShim(encoding, bytes));
            }

            unsafe
            {
                fixed(byte *bytesPtr = bytes)
                {
                    return(encoding.GetCharCount(bytesPtr, bytes.Length));
                }
            }
        }
Esempio n. 17
0
        public static unsafe byte[] ByteToCharStyles(byte* styles, byte* text, int length, Encoding encoding)
        {
            // This is used by annotations and margins to get all the styles in one call.
            // It converts an array of styles where each element corresponds to a BYTE
            // to an array of styles where each element corresponds to a CHARACTER.

            var bytePos = 0; // Position within text BYTES and style BYTES (should be the same)
            var charPos = 0; // Position within style CHARACTERS
            var decoder = encoding.GetDecoder();
            var result = new byte[encoding.GetCharCount(text, length)];

            while (bytePos < length)
            {
                if (decoder.GetCharCount(text + bytePos, 1, false) > 0)
                    result[charPos++] = *(styles + bytePos); // New char

                bytePos++;
            }

            return result;
        }
        public unsafe override int GetCharCount(byte *bytes, int count, bool flush)
        {
            // Validate parameters
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes",
                                                Environment.GetResourceString("ArgumentNull_Array"));
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count",
                                                      Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }

            // Remember the flush
            this.m_mustFlush       = flush;
            this.m_throwOnOverflow = true;

            // By default just call the encoding version, no flush by default
            return(m_encoding.GetCharCount(bytes, count, this));
        }
Esempio n. 19
0
        public unsafe override int GetCharCount(byte *bytes, int count, bool flush)
        {
            // Validate parameters
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes),
                                                SR.ArgumentNull_Array);
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count),
                                                      SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            Contract.EndContractBlock();

            // Remember the flush
            this.m_mustFlush       = flush;
            this.m_throwOnOverflow = true;

            // By default just call the encoding version, no flush by default
            return(m_encoding.GetCharCount(bytes, count, this));
        }
Esempio n. 20
0
 public virtual int GetCharCount(byte[] bytes, int index, int count)
 {
     CheckParams(bytes, index, count);
     return(encoding.GetCharCount(bytes, index, count));
 }
        public unsafe static int GetCharCount(Encoding encoding, byte[] bytes, int index, int count)
        {
            Contract.Assert(encoding != null);
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (index < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException(index < 0 ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (bytes.Length - index < count)
            {
                throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
            }
            Contract.EndContractBlock();

            // If no input just return 0, fixed doesn't like 0 length arrays.
            if (count == 0)
                return 0;

            // Just call pointer version
            fixed (byte* pBytes = bytes)
                return encoding.GetCharCount(pBytes + index, count, decoder: null);
        }
        public unsafe static int GetCharCount(Encoding encoding, byte* bytes, int count)
        {
            Contract.Assert(encoding != null);
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            Contract.EndContractBlock();

            return encoding.GetCharCount(bytes, count, decoder: null);
        }
Esempio n. 23
0
            public static unsafe string ReadString2(BufferSegment buf, ref int offset, Encoding encoding)
            {
                int len = ReadUShort(buf, ref offset);

                if (len == 0) {
                    return string.Empty;
                }
                if (encoding.GetCharCount(buf.Buffer.Array, buf.Offset + offset, len) == 0) {
                    return string.Empty;
                }
                string str = new string(encoding.GetChars(buf.Buffer.Array, buf.Offset + offset, len));
                offset += len;
                return str;
            }
Esempio n. 24
0
        unsafe static internal String CreateStringFromEncoding(
            byte* bytes, int byteLength, Encoding encoding)
        {
            // Get our string length
            int stringLength = encoding.GetCharCount(bytes, byteLength, null);

            // They gave us an empty string if they needed one
            // 0 bytelength might be possible if there's something in an encoder
            if (stringLength == 0)
                return String.Empty;

            String s = FastAllocateString(stringLength);
            fixed (char* pTempChars = &s.m_firstChar)
            {
                int doubleCheck = encoding.GetChars(bytes, byteLength, pTempChars, stringLength, null);
            }

            return s;
        }
Esempio n. 25
0
 public static string ByteDataToString(byte[] byteArray, int startIndex, int length, Encoding encoding)
 {
     byte[] bytes = new byte[length];
     for (int i = 0; i < length; i++)
     {
         bytes[i] = byteArray[startIndex + i];
     }
     char[] chars = new char[encoding.GetCharCount(bytes, 0, bytes.Length)];
     encoding.GetChars(byteArray, startIndex, length, chars, 0);
     return new string(chars);
 }
Esempio n. 26
0
 private int ReadChars(byte* tmpBuffer, int byteCount, ref char[] outputArray, Encoding encoding)
 {
     ReadMemory(tmpBuffer, byteCount * 8);
     int charCount = encoding.GetCharCount(tmpBuffer, byteCount);
     if (charCount > outputArray.Length)
         outputArray = new char[Math.Max(charCount, outputArray.Length * 2)];
     fixed (char* chars = &outputArray[0])
     {
         encoding.GetChars(tmpBuffer, byteCount, chars, charCount);
     }
     return charCount;
 }
Esempio n. 27
0
 /// <summary>
 /// 把 2位 的长度也读进来了
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="len"></param>
 /// <param name="encoding"></param>
 /// <returns></returns>
 public static unsafe string ReadString2(byte* buf, ref int offset, Encoding encoding)
 {
     int len = ReadUShort(buf, ref offset);
     //byte[] temp = new byte[len];
     //Marshal.Copy((IntPtr)(void*)(buf + offset), temp, 0, len);
     //offset += len;
     //string s = encoding.GetString(temp);
     //return s;
     if (len == 0) {
         return string.Empty;
     }
     int length = encoding.GetCharCount(buf + offset, len);
     if (length == 0) {
         return string.Empty;
     }
     string str = new string(' ', length);
     fixed (char* chRef = str) {
         encoding.GetChars(buf + offset, len, chRef, length);
     }
     offset += len;
     return str;
 }
Esempio n. 28
0
        public static unsafe int GetCharCount(Encoding encoding, byte* bytes, int count)
        {
            Debug.Assert(encoding != null);
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes), SR.ArgumentNull_Array);
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            Contract.EndContractBlock();

            return encoding.GetCharCount(bytes, count, decoder: null);
        }
Esempio n. 29
0
 // Override inherited methods.
 public override int GetCharCount(byte[] bytes, int index, int count)
 {
     return(encoding.GetCharCount(bytes, index, count));
 }
Esempio n. 30
0
 public static unsafe string ReadString(BufferSegment buf, ref int offset, int len, Encoding encoding)
 {
     //
     if (encoding.GetCharCount(buf.Buffer.Array, buf.Offset + offset, len) == 0) {
         return string.Empty;
     }
     // string str = new string(' ', length);
     //fixed (char* chRef = str) {
     char[] chars = encoding.GetChars(buf.Buffer.Array, buf.Offset + offset, len);
     //}
     string str = new string(chars);
     offset += len;
     return str;
 }
Esempio n. 31
0
 private string convString(string instr, Encoding inCode, Encoding outCode)
 {
     byte[] inBytes = inCode.GetBytes(instr);
     byte[] outBytes = Encoding.Convert(inCode, outCode, inBytes);
     char[] outChars = new char[outCode.GetCharCount(outBytes, 0, outBytes.Length)];
     outCode.GetChars(outBytes, 0, outBytes.Length, outChars, 0);
     return new string(outChars);
 }
Esempio n. 32
0
 /// <summary>
 /// Gets the number of CHAR in a BYTE range
 /// </summary>
 private static unsafe int GetCharCount(IntPtr text, int length, Encoding encoding)
 {
     if (text == IntPtr.Zero || length == 0)
         return 0;
     var count = encoding.GetCharCount((byte*)text, length);
     return count;
 }
Esempio n. 33
0
    private bool pLoad(ref GCDrawingfield drawing)
    {
        drawing_   = drawing;
        layerNames = new Dictionary <string, string>();
        valid      = true;
        resetModal();
        try
        {
            statusUpdateUI?.Invoke("Loading");
            progressUpdateUI?.Invoke(0);
            Stream s = File.OpenRead(filename);
            if (filename.ToLower().EndsWith("gz"))
            {
                using GZipStream gzs = new(s, CompressionMode.Decompress);
                MemoryStream ms = new();
                gzs.CopyTo(ms);
                ms.Seek(0, SeekOrigin.Begin);
                br = new EndianBinaryReader(EndianBitConverter.Big, ms);
            }
            else
            {
                br = new EndianBinaryReader(EndianBitConverter.Big, s);
            }
            System.Text.Encoding ascii = System.Text.Encoding.ASCII;

            cell_ = null;

            long pos = br.BaseStream.Position;
            long len = br.BaseStream.Length;

            long updateInterval = len / 100;

            double progress = 0;

            while (pos < len)
            {
                switch (pos % updateInterval)
                {
                case 0:
                    progressUpdateUI?.Invoke(progress);
                    progress += 0.01f;
                    break;
                }
                reclen = br.ReadUInt16();
                record = br.ReadByte();
                type   = br.ReadByte();

                switch (type)
                {
                case 0:
                    break;     // no data

                case 1:
                    items = (reclen - 4) / 2;     // bit array
                    break;

                case 2:
                    items = (reclen - 4) / 2;     // two byte signed int
                    break;

                case 3:
                    items = (reclen - 4) / 4;     // four byte signed int
                    break;

                case 4:
                    items = (reclen - 4) / 4;     // four byte real
                    break;

                case 5:
                    items = (reclen - 4) / 8;     // eight byte real
                    break;

                case 6:
                    items = reclen - 4;     // ASC
                    break;
                }

                switch (record)
                {
                case 0:     // header
                    help16 = br.ReadInt16();
                    break;

                case 1:     // bgnlib
                    drawing_.modyear  = br.ReadInt16();
                    drawing_.modmonth = br.ReadInt16();
                    drawing_.modday   = br.ReadInt16();
                    drawing_.modhour  = br.ReadInt16();
                    drawing_.modmin   = br.ReadInt16();
                    drawing_.modsec   = br.ReadInt16();
                    // drawing mod time above
                    drawing_.accyear  = br.ReadInt16();
                    drawing_.accmonth = br.ReadInt16();
                    drawing_.accday   = br.ReadInt16();
                    drawing_.acchour  = br.ReadInt16();
                    drawing_.accmin   = br.ReadInt16();
                    drawing_.accsec   = br.ReadInt16();
                    // drawing access time above
                    break;

                case 2:      // LIBNAM
                    byte[] tmp_lbnbyte = br.ReadBytes(items);

                    char[] tmplbnchars = new char[ascii.GetCharCount(tmp_lbnbyte, 0, tmp_lbnbyte.Length)];
                    ascii.GetChars(tmp_lbnbyte, 0, tmp_lbnbyte.Length, tmplbnchars, 0);

                    drawing_.libname = new string(tmplbnchars);     //  br.ReadString();
                    // Some files have null termination issues.
                    string[] lNTokens = drawing_.libname.Split(new [] { '\0' });
                    drawing_.libname = lNTokens[0];
                    break;

                case 3:     // UNITS
                    double_                = read8ByteReal();
                    drawing_.userunits     = double_;
                    double_                = read8ByteReal();
                    drawing_.databaseunits = double_;
                    break;

                case 4:     // ENDLIB
                    break;

                case 5:     // BGNSTR
                    cell_ = drawing_.addCell();

                    cell_.modyear  = br.ReadInt16();
                    cell_.modmonth = br.ReadInt16();
                    cell_.modday   = br.ReadInt16();
                    cell_.modhour  = br.ReadInt16();
                    cell_.modmin   = br.ReadInt16();
                    cell_.modsec   = br.ReadInt16();
                    cell_.accyear  = br.ReadInt16();
                    cell_.accmonth = br.ReadInt16();
                    cell_.accday   = br.ReadInt16();
                    cell_.acchour  = br.ReadInt16();
                    cell_.accmin   = br.ReadInt16();
                    cell_.accsec   = br.ReadInt16();

                    break;

                case 6:     // STRNAM
                    byte[] tmp_strnbyte = br.ReadBytes(items);

                    char[] tmpstrnchars = new char[ascii.GetCharCount(tmp_strnbyte, 0, tmp_strnbyte.Length)];
                    ascii.GetChars(tmp_strnbyte, 0, tmp_strnbyte.Length, tmpstrnchars, 0);

                    cell_.cellName = new string(tmpstrnchars);
                    // Some files have null termination issues.
                    string[] tokens = cell_.cellName.Split(new [] { '\0' });
                    cell_.cellName = tokens[0];
                    break;

                case 7:     // ENDSTR
                    break;

                case 8:     // BONDRY -> Polygon
                    modal.elementmode = 110;
                    break;

                case 9:     //PATH
                    modal.elementmode = 150;
                    modal.angle       = 0;
                    modal.mag         = 1;
                    modal.mirror_x    = false;
                    modal.width       = 0;
                    modal.cap         = 0;
                    modal.endExt      = 0;
                    modal.beginExt    = 0;
                    break;

                case 10:     //SREF
                    modal.elementmode = 120;
                    modal.angle       = 0;
                    modal.mag         = 1;
                    modal.mirror_x    = false;
                    modal.rotate      = false;
                    modal.mag_        = false;
                    break;

                case 11:     //AREF
                    modal.elementmode = 130;
                    modal.angle       = 0;
                    modal.mag         = 1;
                    modal.mirror_x    = false;
                    modal.rotate      = false;
                    modal.mag_        = false;
                    break;

                case 12:     //TEXT
                    modal.elementmode  = 140;
                    modal.angle        = 0;
                    modal.mag          = 1;
                    modal.width        = 0;
                    modal.mirror_x     = false;
                    modal.rotate       = false;
                    modal.mag_         = false;
                    modal.presentation = 0;
                    break;

                case 13:     //LAYER
                    modal.layer = br.ReadInt16();
                    break;

                case 14:     //DATATYPE
                    modal.datatype = br.ReadInt16();
                    try
                    {
                        layerNames.Add("L" + modal.layer + "D" + modal.datatype, "L" + modal.layer + "D" + modal.datatype);
                    }
                    catch (Exception)
                    {
                    }
                    break;

                case 15:     //WIDTH
                    modal.width = br.ReadInt32();
                    switch (modal.width)
                    {
                    case < 0:
                        modal.width = -modal.width;
                        break;
                    }

                    modal.width = modal.width switch
                    {
                        // Zero width is problematic for Variance and Quilt.
                        0 => 10,
                        _ => modal.width
                    };
                    break;

                case 16:     //XY
                    modal.point_array = new GeoLibPoint[items / 2];
                    for (int g = 0; g < modal.point_array.Length; g++)
                    {
                        int32x = br.ReadInt32();
                        int32y = br.ReadInt32();
                        modal.point_array[g] = new GeoLibPoint(int32x, int32y);
                    }
                    break;

                case 17:     //ENDEL
                    // Looks like some cases, we don't register the associated LD (e.g. layers with only text). Workaround for now until cause is understood.
                    try
                    {
                        layerNames.Add("L" + modal.layer + "D" + modal.datatype, "L" + modal.layer + "D" + modal.datatype);
                    }
                    catch (Exception)
                    {
                    }
                    switch (modal.elementmode)
                    {
                    case 100:
                        addBox();
                        break;

                    case 110:
                        addPolygon();
                        break;

                    case 120:
                        addCellRef();
                        break;

                    case 130:
                        addCellRefArray();
                        break;

                    case 140:
                        addText();
                        break;

                    case 150:
                        addPath();
                        break;
                    }
                    resetModal();
                    break;

                case 18:     //SNAME
                    byte[] tmp_snamebyte = br.ReadBytes(items);

                    char[] tmpnamechars = new char[ascii.GetCharCount(tmp_snamebyte, 0, tmp_snamebyte.Length)];
                    ascii.GetChars(tmp_snamebyte, 0, tmp_snamebyte.Length, tmpnamechars, 0);

                    modal.sname = new string(tmpnamechars);
                    // Some files have null termination issues.
                    string[] sNtokens = modal.sname.Split(new [] { '\0' });
                    modal.sname = sNtokens[0];
                    break;

                case 19:     //COLROW
                    modal.anzx = br.ReadInt16();
                    modal.anzy = br.ReadInt16();
                    break;

                /*case 20: //TXTNOD
                 *  break;
                 * case 21: //NODE
                 *  break;*/
                case 22:                             //TXTTYP
                    modal.textType = br.ReadInt16();
                    modal.datatype = modal.textType; // we don't treat text differently.
                    break;

                case 23:     //PRSTTN
                    modal.presentation = br.ReadInt16() & 0x000F;
                    break;

                /*case 24: //SPACNG
                 *  break;*/
                case 25:     //STRING
                    byte[] tmp_strbyte = br.ReadBytes(items);

                    char[] tmpstrchars = new char[ascii.GetCharCount(tmp_strbyte, 0, tmp_strbyte.Length)];
                    ascii.GetChars(tmp_strbyte, 0, tmp_strbyte.Length, tmpstrchars, 0);
                    modal.sname = new string(tmpstrchars);
                    // Some files have null termination issues.
                    string[] sN2tokens = modal.sname.Split(new [] { '\0' });
                    modal.sname = sN2tokens[0];
                    break;

                case 26:     //STRANS
                    int16          = br.ReadInt16();
                    modal.mirror_x = (int16 & 0x8000) != 0;
                    modal.rotate   = (int16 & 0x0002) != 0;
                    modal.mag_     = (int16 & 0x0004) != 0;
                    break;

                case 27:     //MAG
                    modal.mag = read8ByteReal();
                    break;

                case 28:     //ANGLE
                    modal.angle = read8ByteReal();
                    break;

                /*case 29: //UINTEG
                 *  break;
                 * case 30: //USTRNG
                 *  break;
                 * case 31: //REFLIB
                 *  s=readString(&ts,items);
                 *  if (layout::debug){
                 *      printf("REFLIB %s\n",s.latin1());
                 *  }
                 *  break;
                 * case 32: //FONTS
                 *  break;*/
                case 33:     //PTHTYP
                    modal.cap = br.ReadInt16();
                    break;

                /*case 34: //GENRTS
                 *  break;
                 * case 35: //ATRTBL
                 *  break;
                 * case 36: //STPTBL
                 *  break;
                 * case 37: //STRTYP
                 *  break;
                 * case 38: //EFLAGS
                 *  break;
                 * case 39: //ELKEY
                 *  break;
                 * case 40: //LNKTYP
                 *  break;
                 * case 41: //LNKKEYa
                 *  break;
                 * case 42: //NODTYP
                 *  break;
                 * case 43: //PROATR
                 *  break;
                 * case 44: //PROVAL
                 *  break;*/
                case 45:     //BOX
                    modal.elementmode = 100;
                    break;

                case 46:     //BOXTYP
                    modal.boxType = br.ReadInt16();
                    break;

                /*case 47: //PLEX
                 *  break;*/
                case 48:     //BGNEXTN
                    modal.beginExt = br.ReadInt32();
                    break;

                case 49:     //ENDEXTN
                    modal.endExt = br.ReadInt32();
                    break;

                /*case 50: //TAPNUM
                 *  break;
                 * case 51: //TAPCOD
                 *  break;
                 * case 52: //STRCLS
                 *  break;
                 * case 53: //RESRVD
                 *  break;
                 * case 54: //FORMAT
                 *  break;
                 * case 55: //MASK
                 *  break;
                 * case 56: //ENDMSK
                 *  break;
                 * case 57: //LDIRSZ
                 *  break;
                 * case 58: //SRFNAM
                 *  break;
                 * case 59: //LIBSCR
                 *  break;*/
                default:
                    for (int i = 0; i < items; i++)
                    {
                        switch (type)
                        {
                        case 0:
                            // No Data
                            break;

                        case 1:
                            // Bit Array
                            int16 = br.ReadInt16();
                            break;

                        case 2:
                            // Two Byte signed int
                            int16 = br.ReadInt16();
                            break;

                        case 3:
                            // Four Byte signed int
                            int32 = br.ReadInt32();
                            break;

                        case 4:
                            // Four byte real
                            int32 = br.ReadInt32();
                            break;

                        case 5:
                            // Eight byte real
                            int32 = br.ReadInt32();
                            break;

                        case 6:
                            // ASC
                            help = br.ReadByte();
                            break;
                        }
                    }
                    break;
                }
                pos = br.BaseStream.Position;
            }

            try
            {
                drawing_.active_cell = drawing.findCellIndex(cell_.cellName);
                drawing_.resize(drawing_.userunits / 1E-3);
            }
            catch (Exception)
            {
                const string err = "Unable to find any cells. Is this file legal GDS?";
                error_msgs.Add(err);
                throw new Exception(err);
            }

            statusUpdateUI?.Invoke("Done");
            progressUpdateUI?.Invoke(1.0f);
        }
        catch (Exception e)
        {
            valid = false;
            error_msgs.Add(e.Message);
        }
        return(valid);
    }
        /// <summary>
        /// Gets the encoded characters as a Memory from a byte Span
        /// </summary>
        /// <param name="encoding">The encoding to use</param>
        /// <param name="input">The input bytes</param>
        /// <returns>The characters that make up the text</returns>
        public static Memory <char> GetChars(this Encoding encoding, ReadOnlySpan <byte> input)
        {
            var Output = new char[encoding.GetCharCount(input)];

            return(new Memory <char>(Output, 0, encoding.GetChars(input, Output)));
        }
Esempio n. 35
0
        public string ReadPrefixLengthString(Encoding encoding)
        {
            int byteCount = (int)ReadUInt32Variant();

            if(byteCount == 0)
            {
                return String.Empty;
            }
            else if (byteCount <= 1024)
            {
                byte* bytes = stackalloc byte[byteCount];
                ReadMemory(bytes, byteCount * 8);
                int charCount = encoding.GetCharCount(bytes, byteCount);
                char* chars = stackalloc char[charCount];
                encoding.GetChars(bytes, byteCount, chars, charCount);
                return new string(chars, 0, charCount);
            }
            else
            {
                // Heap alloc
                byte[] data = new byte[byteCount];
                fixed (byte* ptr = data)
                {
                    ReadMemory(ptr, byteCount * 8);
                }
                return new string(encoding.GetChars(data));
            }
        }
Esempio n. 36
0
        private void GetCharsTestAutoBuffer(Encoding encoding, byte[] inputBuffer, int inputIndex, int inputCount, int outputIndex, string expectedOutput, int expectedOutputCount)
        {
            var outputBuffer = new char[encoding.GetCharCount(inputBuffer, inputIndex, inputCount)];
            var result = encoding.GetChars(inputBuffer, inputIndex, inputCount, outputBuffer, outputIndex == COUNT_AUTO ? 0 : outputIndex);
            var actual = new string(outputBuffer);

            Assert.Equal(expectedOutputCount, result);
            Assert.Equal(expectedOutput, actual);
        }
Esempio n. 37
0
        public static string Escape(string s, Encoding encoding, EscapeMode escapeMode)
        {
            StringBuilder accum = new StringBuilder(s.Length * 2);
            Dictionary<char, string> map = escapeMode.GetMap();

            for (int pos = 0; pos < s.Length; pos++)
            {
                char c = s[pos];

                // To replicate this line from the original code: encoder.canEncode(c), we perform the following steps:
                byte[] unicodeBytes = Encoding.Unicode.GetBytes(c.ToString()); // Get the bytes by unicode.

                byte[] encodingBytes = Encoding.Convert(Encoding.Unicode, encoding, unicodeBytes); // Convert bytes into target encoding's bytes.

                char[] encodingChars = new char[encoding.GetCharCount(encodingBytes)];
                encoding.GetChars(encodingBytes, 0, encodingBytes.Length, encodingChars, 0); // Convert target encoding bytes into chars.

                if (map.ContainsKey(c))
                {
                    accum.AppendFormat("&{0};", map[c]);
                }
                else if (encodingChars[0] == c)
                {
                    // If the char resulting from the conversion matches the original one, we can understand that the encoding can encode this char.
                    accum.Append(c); // If so, add it to the accumulator.
                }
                else
                {
                    accum.AppendFormat("&#{0};", (int)c);
                }
            }

            return accum.ToString();
        }
 public static int GetCharCount(Encoding encoding, ReadOnlySpan <byte> span) => encoding.GetCharCount(span);
Esempio n. 39
0
        public static unsafe int GetCharCount(Encoding encoding, byte[] bytes, int index, int count)
        {
            Debug.Assert(encoding != null);
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes), SR.ArgumentNull_Array);
            }
            if (index < 0 || count < 0)
            {
                throw new ArgumentOutOfRangeException(index < 0 ? nameof(index) : nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (bytes.Length - index < count)
            {
                throw new ArgumentOutOfRangeException(nameof(bytes), SR.ArgumentOutOfRange_IndexCountBuffer);
            }
            Contract.EndContractBlock();

            // If no input just return 0, fixed doesn't like 0 length arrays.
            if (count == 0)
                return 0;

            // Just call pointer version
            fixed (byte* pBytes = bytes)
                return encoding.GetCharCount(pBytes + index, count, decoder: null);
        }
Esempio n. 40
0
        private void GetCharCountTest(Encoding encoding, byte[] buffer, int startIndex, int count, int expected)
        {
            var result = encoding.GetCharCount(buffer, startIndex, count);

            Assert.Equal(expected, result);
        }
Esempio n. 41
0
            //public static string ReadString(Stream stream, int len, Encoding encoding) {
            //    byte[] buf = new byte[len];
            //    stream.Read(buf, 0, len);
            //    return encoding.GetString(buf);
            //}
            /// <summary>
            /// 把 1位 的长度也读进来了
            /// </summary>
            /// <param name="stream"></param>
            /// <param name="len"></param>
            /// <param name="encoding"></param>
            /// <returns></returns>
            public static unsafe string ReadString1(byte* buf, ref int offset, Encoding encoding)
            {
                int len = ReadByte(buf, ref offset);
                //byte[] temp = new byte[len];
                ////fixed (byte* bb = temp) {
                ////    byte* cc = bb;
                ////    for (int i = 0; i < len; i++) {
                ////        *cc = *(buf + offset);
                ////        cc++;
                ////        offset++;
                ////    }
                ////}
                //Marshal.Copy((IntPtr)(void*)(buf + offset), temp, 0, len);
                //offset += len;
                //string s = encoding.GetString(temp);
                //return s;

                if (len == 0) {
                    return string.Empty;
                }
                int length = encoding.GetCharCount(buf + offset, len);
                if (length == 0) {
                    return string.Empty;
                }
                string str = new string(' ', length);
                fixed (char* chRef = str) {
                    encoding.GetChars(buf + offset, len, chRef, length);
                }
                offset += len;
                return str;
            }
Esempio n. 42
0
 public override int GetCharCount(byte[] bytes)
 {
     return(enc.GetCharCount(bytes));
 }
 /// <summary>
 /// Determines the number of characters that result from a decoding operation
 /// </summary>
 /// <param name="encoding">The encoding to use</param>
 /// <param name="span">The input bytes</param>
 /// <returns>The number of characters that would be output</returns>
 public static unsafe int GetCharCount(this Encoding encoding, ReadOnlySpan <byte> span)
 {
     fixed(byte *pSpan = &MemoryMarshal.GetReference(span))
     return(encoding.GetCharCount(pSpan, span.Length));
 }