Beispiel #1
0
 private static bool NeedsTruncation(string s)
 {
     // Optimization: only count the actual number of bytes if there is the possibility
     // of the string exceeding maxStringBytes
     return(encoding.GetMaxByteCount(s.Length) > maxStringBytes &&
            encoding.GetByteCount(s) > maxStringBytes);
 }
Beispiel #2
0
        protected virtual void OnPaste(System.EventArgs e)
        {
            object clipboardText = Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.Text);

            if (clipboardText == null)
            {
                return;
            }

            System.Text.Encoding sjisEncoding = System.Text.Encoding.GetEncoding("Shift_JIS");
            string inputText             = clipboardText.ToString();
            int    textByteCount         = sjisEncoding.GetByteCount(this.Text);
            int    inputByteCount        = sjisEncoding.GetByteCount(inputText);
            int    selectedTextByteCount = sjisEncoding.GetByteCount(this.SelectedText);
            int    remainByteCount       = this.MaxByteLength - (textByteCount - selectedTextByteCount);

            if (remainByteCount <= 0)
            {
                return;
            }

            if (remainByteCount >= inputByteCount)
            {
                this.SelectedText = inputText;
            }
            else
            {
                this.SelectedText = inputText.Substring(0, remainByteCount);
            }
        }
Beispiel #3
0
        private void txtboxInput_TextChanged(object sender, System.EventArgs e)
        {
            if (this._maxLength > 0)
            {
                System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
                string text = this.txtboxInput.Text;

                if (encoding.GetByteCount(text) > _maxLength)
                {
                    while (encoding.GetByteCount(text) > _maxLength)
                    {
                        text = text.Substring(0, text.Length - 1);
                    }

                    this.txtboxInput.Text = this.txtboxInput.Text.Substring(0, this.txtboxInput.Text.Length - 1);
                    System.Windows.Forms.SendKeys.Send("^{END}");
                }
            }

            if (InnerTextChanged != null)
            {
                InnerTextChanged(sender, e);
            }
            //			switch (_editType)
            //			{
            //				case EditTypes.String :
            //
            //					break;
            //				case EditTypes.Number:
            //
            //					break;
            //			}
        }
        static int _m_GetByteCount(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.Text.Encoding gen_to_be_invoked = (System.Text.Encoding)translator.FastGetCSObj(L, 1);


                int gen_param_count = LuaAPI.lua_gettop(L);

                if (gen_param_count == 2 && translator.Assignable <char[]>(L, 2))
                {
                    char[] _chars = (char[])translator.GetObject(L, 2, typeof(char[]));

                    int gen_ret = gen_to_be_invoked.GetByteCount(
                        _chars);
                    LuaAPI.xlua_pushinteger(L, gen_ret);



                    return(1);
                }
                if (gen_param_count == 2 && (LuaAPI.lua_isnil(L, 2) || LuaAPI.lua_type(L, 2) == LuaTypes.LUA_TSTRING))
                {
                    string _s = LuaAPI.lua_tostring(L, 2);

                    int gen_ret = gen_to_be_invoked.GetByteCount(
                        _s);
                    LuaAPI.xlua_pushinteger(L, gen_ret);



                    return(1);
                }
                if (gen_param_count == 4 && translator.Assignable <char[]>(L, 2) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 4))
                {
                    char[] _chars = (char[])translator.GetObject(L, 2, typeof(char[]));
                    int    _index = LuaAPI.xlua_tointeger(L, 3);
                    int    _count = LuaAPI.xlua_tointeger(L, 4);

                    int gen_ret = gen_to_be_invoked.GetByteCount(
                        _chars,
                        _index,
                        _count);
                    LuaAPI.xlua_pushinteger(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }

            return(LuaAPI.luaL_error(L, "invalid arguments to System.Text.Encoding.GetByteCount!"));
        }
Beispiel #5
0
        /// <summary>
        /// Add a header and footer to the byte[] to construct a MultiPart/Form-data.
        /// </summary>
        /// <returns>The file for multi part form data.</returns>
        /// <param name="requestContentType">Request content type.</param>
        /// <param name="fileName">File name.</param>
        /// <param name="uploadFile">Upload file.</param>
        private byte[] FormatFileForMultiPartFormData(out string requestContentType, string fileName, byte[] uploadFile)
        {
            // Construct boundary and content type. Use Guild to generate a random number attached to the form
            string moBackFormatDataBoundary = string.Format("--MoBackFormData{0:N}", System.Guid.NewGuid());

            // Construct a ContentType for the request. It's not only multipart/form-data, but also need the boundary append after it.
            requestContentType = string.Format("multipart/form-data; boundary={0}", moBackFormatDataBoundary);

            // Create a new Stream to write a new file header.
            System.IO.Stream formStream = new System.IO.MemoryStream();

            // Create encoding type
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;

            /*
             * // Construct a header for the upload file:
             * // Result:
             * --MoBackFormData{RandomNumber}
             * Content-Disposition: form-data; name="file"; filename={fileName}
             * Content-Type: <-- this could be empty?
             * // Note: we do need the Content-Disposition. Not sure about the Content-Type, left it blank for now.
             */
            string fileUploadheader = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                                                    moBackFormatDataBoundary,
                                                    "file",
                                                    fileName,
                                                    string.Empty);

            // Write the header to stream.
            formStream.Write(encoding.GetBytes(fileUploadheader), 0, encoding.GetByteCount(fileUploadheader));

            // Write the upload file to stream
            formStream.Write(uploadFile, 0, uploadFile.Length);

            // Create footer for the upload file.
            string fileUploadFooter = string.Format("\r\n--{0}--\r\n", moBackFormatDataBoundary);

            // Write the footer to stream
            formStream.Write(encoding.GetBytes(fileUploadFooter), 0, encoding.GetByteCount(fileUploadFooter));

            // Construct a new byte array after finish append header and footer.
            formStream.Position = 0;
            byte[] postData = new byte[formStream.Length];
            formStream.Read(postData, 0, postData.Length);
            formStream.Close();

            return(postData);
        }
Beispiel #6
0
        private static sbyte[] GetSBytesForEncoding(System.Text.Encoding encoding, string s)
        {
            var sbytes = new sbyte[encoding.GetByteCount(s)];

            encoding.GetBytes(s, 0, s.Length, (byte[])(object)sbytes, 0);
            return(sbytes);
        }
Beispiel #7
0
        public void GetByteCount()
        {
            System.Text.Encoding enc = GetEncoding();
            var byteCount            = enc.GetByteCount("abc");

            Assert.Equal <int>(5, byteCount);
        }
 public unsafe static int GetByteCount(this System.Text.Encoding encoding, ReadOnlySpan <char> chars)
 {
     fixed(char *p = chars)
     {
         return(encoding.GetByteCount(p, chars.Length));
     }
 }
        public static string gFun_MidB(string pStr_String, int pInt_Start, int pInt_Len = 0)
        {
            int nInt_Pos = default(int);

            System.Text.Encoding nObj_Encod = System.Text.Encoding.GetEncoding(949);
            byte[] nByt_Temp   = null;
            string nStr_Return = "";

            try
            {
                nInt_Pos = nObj_Encod.GetByteCount(pStr_String) - pInt_Start + 1;
                pInt_Len = pInt_Len == 0 ? nInt_Pos : pInt_Len;
                pInt_Len = pInt_Len > nInt_Pos ? nInt_Pos : pInt_Len;

                nByt_Temp   = nObj_Encod.GetBytes(pStr_String);
                nStr_Return = (string)(nObj_Encod.GetString(nByt_Temp, pInt_Start - 1, pInt_Len));

                return(nStr_Return);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n gFun_MidB Function Error !", "확인");
                return("");
            }
            finally
            {
            }
        }
Beispiel #10
0
        public static string gFun_RightB(string pStr_String, int pInt_Len)
        {
            int nInt_Pos = default(int);

            byte[] nByt_Temp = null;
            System.Text.Encoding nObj_Encod = System.Text.Encoding.GetEncoding(949);
            //string nStr_Return = "";

            try
            {
                nInt_Pos = System.Convert.ToInt32(nObj_Encod.GetByteCount(pStr_String));
                pInt_Len = pInt_Len > nInt_Pos ? nInt_Pos : pInt_Len;

                nByt_Temp = nObj_Encod.GetBytes(pStr_String);
                return(nObj_Encod.GetString(nByt_Temp, nByt_Temp.Length - pInt_Len, pInt_Len));
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n gFun_RightB Function Error !", "확인");
                return("");
            }
            finally
            {
            }
        }
Beispiel #11
0
        public void WriteString(string value)
        {
            if (value == null)
            {
                WriteVarInt32(0);
            }
            else
            {
                int length = Utf8Encoding.GetByteCount(value);
                WriteVarInt32(length);
                ensureCapacity(length);

                if (length == value.Length) // Must be all ASCII...
                {
                    for (int i = 0; i < length; i++)
                    {
                        mBuffer[mWritePos + i] = (byte)value[i];
                    }
                }
                else
                {
                    Utf8Encoding.GetBytes(value, 0, value.Length, mBuffer, mWritePos);
                }
                mWritePos += length;
            }
        }
Beispiel #12
0
        protected virtual void OnChar(System.Windows.Forms.KeyPressEventArgs e)
        {
            if (char.IsControl(e.KeyChar))
            {
                return;
            }

            System.Text.Encoding sjisEncoding = System.Text.Encoding.GetEncoding("Shift_JIS");
            int textByteCount         = sjisEncoding.GetByteCount(this.Text);
            int inputByteCount        = sjisEncoding.GetByteCount(e.KeyChar.ToString());
            int selectedTextByteCount = sjisEncoding.GetByteCount(this.SelectedText);

            if ((textByteCount + inputByteCount - selectedTextByteCount) > this.MaxByteLength)
            {
                e.Handled = true;
            }
        }
 public static byte[] StringToByteArray(string str)
 {
     byte[] ByteArray = new byte[str.Length + 1];
     System.Text.Encoding Encoding = System.Text.Encoding.ASCII;
     ByteArray = new byte[Encoding.GetByteCount(str) + 1];
     ByteArray = Encoding.GetBytes(str + " ");
     ByteArray[ByteArray.Length - 1] = 0;
     return(ByteArray);
 }
Beispiel #14
0
        public static int GetByteCount(this System.Text.Encoding encoding, params char[] chars)
        {
            if (encoding == null)
            {
                encoding = System.Text.Encoding.Default;
            }

            return(encoding.GetByteCount(chars));
        }
        public void AddField(string fieldName, string value, System.Text.Encoding encoding)
        {
            var byteCount = encoding.GetByteCount(value);
            var buffer    = BufferPool.Get(byteCount, true);
            var stream    = new BufferPoolMemoryStream(buffer, 0, byteCount);

            encoding.GetBytes(value, 0, value.Length, buffer, 0);

            AddStreamField(stream, fieldName, null, "text/plain; charset=" + encoding.WebName);
        }
Beispiel #16
0
        protected override long GetSize(string value)
        {
            var size = m_encoding.GetByteCount(value);

            if (m_buf == null || m_buf.Length < size)
            {
                m_buf = new byte[Math.Max(MIN_BUF_SIZE, size + BUFFER_OVERSHOOT)];
            }
            return(size);
        }
        /// <summary>
        /// 计算所有的字符串的最大的字节长度,并可设置所有的字符串为相同的字节长度
        /// </summary>
        /// <param name="myList">保存字符串的数组</param>
        /// <param name="FixStyle">修正模式 0:不修正, 1:在原始字符产前面添加填充字符 2:在字符串后面添加填充字符</param>
        /// <param name="FillChar">修正时填充的字符,其Asc码从0到255</param>
        /// <returns>字符串最大的字节长度</returns>
        public static int FixStringByteLength(System.Collections.ArrayList myList, int FixStyle, char FillChar)
        {
            System.Text.Encoding myEncode = System.Text.Encoding.GetEncoding(936);
            int MaxLen = 0;

            foreach (string strItem in myList)
            {
                if (strItem != null)
                {
                    int len = myEncode.GetByteCount(strItem);
                    if (MaxLen < len)
                    {
                        MaxLen = len;
                    }
                }
            }
            if (FixStyle == 1 || FixStyle == 2)
            {
                for (int iCount = 0; iCount < myList.Count; iCount++)
                {
                    string strItem = (string)myList[iCount];
                    if (strItem == null)
                    {
                        strItem = "";
                    }
                    int len = myEncode.GetByteCount(strItem);
                    if (len != MaxLen)
                    {
                        if (FixStyle == 1)
                        {
                            strItem = new string( FillChar, MaxLen - len ) + strItem;
                        }
                        else
                        {
                            strItem = strItem + new string( FillChar, MaxLen - len );
                        }
                        myList[iCount] = strItem;
                    }
                }                //for
            }
            return(MaxLen);
        }        //public static int FixStringByteLength( System.Collections.ArrayList myList , int FixStyle, char FillChar)
Beispiel #18
0
        private byte[] GetMultipartFormData(IDictionary <string, object> postParameters, string boundary)
        {
            System.Text.Encoding encoding = System.Text.Encoding.ASCII;
            Stream formDataStream         = new System.IO.MemoryStream();
            bool   needsCLRF = false;

            foreach (var param in postParameters)
            {
                if (needsCLRF)
                {
                    formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));
                }

                needsCLRF = true;
                if (param.Value is FileParameter)
                {
                    FileParameter fileToUpload = (FileParameter)param.Value;
                    string        header       = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\";" + "filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n", boundary, param.Key, fileToUpload.FileName ?? param.Key, fileToUpload.ContentType ?? "application/octet-stream");

                    formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));
                    formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
                }
                else
                {
                    string postData = string.Format("--{0}\r\nContent-Disposition: form-data;" + "name=\"{1}\"\r\n\r\n{2}", boundary, param.Key, param.Value);

                    formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
                }
            }

            string footer = "\r\n--" + boundary + "--\r\n";

            formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));

            formDataStream.Position = 0;
            byte[] formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);
            formDataStream.Close();

            return(formData);
        }
Beispiel #19
0
        /// <summary>Gets the encoded bytes for a string including an optional null terminator.</summary>
        /// <param name="value">The string value to convert.</param>
        /// <param name="enc">The character encoding.</param>
        /// <param name="nullTerm">if set to <c>true</c> include a null terminator at the end of the string in the resulting byte array.</param>
        /// <returns>A byte array including <paramref name="value"/> encoded as per <paramref name="enc"/> and the optional null terminator.</returns>
        public static byte[] GetBytes(this string value, System.Text.Encoding enc, bool nullTerm = true)
        {
            var chSz = GetCharSize(enc);
            var ret  = new byte[enc.GetByteCount(value) + (nullTerm ? chSz : 0)];

            enc.GetBytes(value, 0, value.Length, ret, 0);
            if (nullTerm)
            {
                enc.GetBytes(new[] { '\0' }, 0, 1, ret, ret.Length - chSz);
            }
            return(ret);
        }
        private void MaxByteLengthTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                return;
            }

            if (char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                return;
            }

            System.Text.Encoding sJis = System.Text.Encoding.GetEncoding("Shift_JIS");
            int textByteCount         = sJis.GetByteCount(this.Text);
            int inputByteCount        = sJis.GetByteCount(e.KeyChar.ToString());
            int selectedTextByteCount = sJis.GetByteCount(this.SelectedText);

            if ((textByteCount + inputByteCount - selectedTextByteCount) > this.MaxLength)
            {
                e.Handled = true;
                return;
            }
        }
Beispiel #21
0
        private void WriteAuthor(Stream binary_file, string author)
        {
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            // Get buffer size required for conversion
            int buffersize = encoding.GetByteCount(author);

            if (buffersize < 30)
            {
                buffersize = 30;
            }
            // Write string into binary file with UTF8 encoding
            byte[] buffer = new byte[buffersize];
            encoding.GetBytes(author, 0, author.Length, buffer, 0);
            binary_file.Write(buffer, 0, 30);
        }
Beispiel #22
0
 /// <summary>
 /// 根据当前iPlat4C.xml中的字符编码获取字符串长度
 /// </summary>
 /// <param name="strValue"></param>
 /// <returns></returns>
 static public int GetByteLength(string strValue)
 {
     //编码问题:
     //默认是gbk
     //当是gbk    时: 汉字对应2 位,其他为1位
     //当是unicode时: 全为2位
     //当是utf-8  时: 中文为3位,其他为1位
     System.Text.Encoding encoding = System.Text.Encoding.Default;
     try
     {
         encoding = System.Text.Encoding.GetEncoding("EC.ProjectConfig.Instance.CurrentServers[0].CharSet");
     }
     catch {
         encoding = System.Text.Encoding.Default;
     }
     return(encoding.GetByteCount(strValue));
 }
Beispiel #23
0
        public static int gFun_LenB(string pStr_String)
        {
            System.Text.Encoding nObj_Encod = System.Text.Encoding.GetEncoding(949);
            int nInt_Len = 0;

            try
            {
                nInt_Len = System.Convert.ToInt32(nObj_Encod.GetByteCount(pStr_String));
                return(nInt_Len);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n gFun_LenB Function Error !", "확인");
                return(0);
            }
            finally
            {
            }
        }
Beispiel #24
0
        public byte[] WriteToScreen(uint deviceAddress, string text)
        {
            if (text.Length > 40)
            {
                text = text.Remove(40, text.Length - 40);
            }
            System.Text.Encoding encoding = System.Text.Encoding.ASCII;
            var mBytes = new byte[encoding.GetByteCount(text) * 8];
            int i      = 0;

            foreach (char ch in text)
            {
                byte mScrPosition = Convert.ToByte(i / 8);
                byte mAsciIchr    = Convert.ToByte(ch);
                System.Array.Copy(Message.GetMessage(deviceAddress, 0x00, 0x15, mScrPosition, mAsciIchr), 0, mBytes, i, 8);
                i = i + 8;
            }

            return(mBytes);
        }
Beispiel #25
0
        static StackObject *GetByteCount_2(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 2);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            System.String @s = (System.String) typeof(System.String).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 2);
            System.Text.Encoding instance_of_this_method = (System.Text.Encoding) typeof(System.Text.Encoding).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            var result_of_this_method = instance_of_this_method.GetByteCount(@s);

            __ret->ObjectType = ObjectTypes.Integer;
            __ret->Value      = result_of_this_method;
            return(__ret + 1);
        }
Beispiel #26
0
        public static byte[] Hash(string In)
        {
            const int SaltLength = 16;

            using (RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider())
            {
                using (SHA512 Provider = new SHA512Managed())
                {
                    System.Text.Encoding Encoder = System.Text.Encoding.UTF8;

                    byte[] Plain = new byte[SaltLength + Encoder.GetByteCount(In)];

                    byte[] Salt = new byte[SaltLength];
                    Gen.GetBytes(Salt);
                    Array.Copy(Salt, Plain, Salt.Length);

                    Encoder.GetBytes(In, 0, In.Length, Plain, SaltLength);

                    return(Provider.ComputeHash(Plain));
                }
            }
        }
Beispiel #27
0
        public byte[] WriteToScreen(uint deviceAddress, string text)
        {
            if (text.Length > 40)
            {
                text = text.Remove(40, text.Length - 40);
            }
            System.Text.Encoding encoding = System.Text.Encoding.ASCII;
            byte[] m_bytes = new byte[encoding.GetByteCount(text) * 7];
            int    i       = 0;
            byte   m_scrPosition;
            byte   m_ASCIIchr;

            foreach (char ch in text)
            {
                m_scrPosition = Convert.ToByte(i / 7);
                m_ASCIIchr    = Convert.ToByte(ch);
                Array.Copy(Message.GetMessage(deviceAddress, 0x00, 0x15, m_scrPosition, m_ASCIIchr), 0, m_bytes, i, 7);
                i = i + 7;
            }

            return(m_bytes);
        }
Beispiel #28
0
 /// <summary>
 /// Writes a UTF-8 encoded string to the model file.
 /// </summary>
 /// <param name="data">
 /// The string data to be persisted.
 /// </param>
 protected override void WriteString(string data)
 {
     _output.WriteByte((byte)_encoding.GetByteCount(data));
     _output.Write(_encoding.GetBytes(data), 0, _encoding.GetByteCount(data));
 }
Beispiel #29
0
        public static bool ReadDelimitedDataFrom(this System.Text.Encoding encoding, byte[] buffer, char[] delimits, int offset, int count, out string result, out int read, out System.Exception any, bool includeDelimits = true)
        {
            read = Common.Binary.Zero;

            any = null;

            result = string.Empty;

            //Todo, check for large delemits and use a hash or always use a hash.
            //System.Collections.Generic.HashSet<char> delimitsC = new System.Collections.Generic.HashSet<char>(delimits);

            if (delimits == null)
            {
                delimits = EmptyChar;
            }

            int max;

            if (count.Equals(Common.Binary.Zero) || ArrayExtensions.IsNullOrEmpty(buffer, out max))
            {
                result = null;

                return(false);
            }

            //Account for the position
            max -= offset;

            //The smaller of the two, max and count
            if ((count = Common.Binary.Min(ref max, ref count)).Equals(0))
            {
                return(false);
            }

            bool sawDelimit = false;

            //Make the builder
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            //Use default..
            if (encoding == null)
            {
                encoding = System.Text.Encoding.Default;
            }

            System.Text.Decoder decoder = encoding.GetDecoder();

            //int charCount = decoder.GetCharCount(buffer, offset, count);

            //if(charCount == 0) return true;

            int toRead, delemitsLength = delimits.Length;

            toRead = delemitsLength <= Common.Binary.Zero ? count : Common.Binary.Max(1, delemitsLength);

            bool complete;

            int charsUsed;

            int justRead;

            //Could use Pool to save allocatios until StringBuilder can handle char*
            char[] results = new char[toRead];

            do
            {
                //Convert to utf16 from the encoding
#if UNSAFE
                unsafe { decoder.Convert((byte *)System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <byte>(buffer, offset), count, (char *)System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <char>(results, 0), toRead, count <= 0, out justRead, out charsUsed, out complete); }
#else
                decoder.Convert(buffer, offset, count, results, 0, toRead, count <= 0, out justRead, out charsUsed, out complete);
#endif

                //If there are not enough bytes to decode the char
                if (justRead.Equals(Common.Binary.Zero))
                {
                    break;
                }

                //Move the offsets and count for what was converted from the decoder
                offset += justRead;

                count -= justRead;

                //Iterate the decoded characters looking for a delemit
                if (delemitsLength > Common.Binary.Zero)
                {
                    for (int c = 0, e = charsUsed; c < e; ++c)
                    {
                        //Compare the char decoded to the delimits, if encountered set sawDelimit and either include the delemit or not.
#if NATIVE || UNSAFE
                        if (System.Array.IndexOf <char>(delimits, (char)System.Runtime.InteropServices.Marshal.ReadInt16(System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement <char>(results, c))) >= 0)
#else
                        if (System.Array.IndexOf <char>(delimits, results[c]) >= 0)
#endif
                        {
                            sawDelimit = true;

                            if (false.Equals(includeDelimits))
                            {
                                charsUsed = c;
                            }
                            else
                            {
                                charsUsed = ++c;
                            }

                            break;
                        }
                    }
                }

                if (charsUsed > 0)
                {
                    builder.Append(results, 0, charsUsed);
                }
            } while (count > 0 && false.Equals(sawDelimit));

            if (builder == null)
            {
                result = null;

                return(sawDelimit);
            }

            result = builder.Length.Equals(Common.Binary.Zero) ? string.Empty : builder.ToString();

            //Take the amount of bytes in the string as what was read.
            read = encoding.GetByteCount(result);

            builder = null;

            return(sawDelimit);
        }
Beispiel #30
0
        /// <summary>
        /// Finds the index in the given buffer of a newline character, either the first or the last (based on the parameters).
        /// If a combined newline (\r\n), the index returned is that of the last character in the sequence.
        /// </summary>
        /// <param name="buffer">The buffer to search in.</param>
        /// <param name="startOffset">The index of the first byte to start searching at.</param>
        /// <param name="length">The number of bytes to search, starting from the given startOffset.</param>
        /// <param name="reverse">If true, searches from the startOffset down to the beginning of the buffer. If false, searches upwards.</param>
        /// <returns>The index of the closest newline character in the sequence (based on direction) that was found. Returns -1 if not found. </returns>
        public static int FindNewline(byte[] buffer, int startOffset, int length, bool reverse, System.Text.Encoding encoding, string delimiter = null)
        {
            if (buffer.Length == 0 || length == 0)
            {
                return(-1);
            }

            // define the bytes per character to use
            int bytesPerChar;

            switch (encoding.CodePage)
            {
            // Big Endian Unicode (UTF-16)
            case 1201:
            // Unicode (UTF-16)
            case 1200:
                bytesPerChar = 2;
                break;

            // UTF-32
            case 12000:
                bytesPerChar = 4;
                break;

            // ASCII
            case 20127:
            // UTF-8
            case 65001:
            // UTF-7
            case 65000:
            // Default to UTF-8
            default:
                bytesPerChar = 1;
                break;
            }

            if (!string.IsNullOrEmpty(delimiter) && delimiter.Length > 1)
            {
                throw new ArgumentException("delimiter", "The delimiter must only be a single character or unspecified to represent the CRLF delimiter");
            }

            if (!string.IsNullOrEmpty(delimiter))
            {
                // convert the byte array back to a string
                var startOfSegment = reverse ? startOffset - length + 1 : startOffset;
                var bytesToString  = encoding.GetString(buffer, startOfSegment, length);
                if (!bytesToString.Contains(delimiter))
                {
                    // didn't find the delimiter.
                    return(-1);
                }

                // the index is returned, which is 0 based, so our loop must include the zero case.
                var numCharsToDelim = reverse ? bytesToString.LastIndexOf(delimiter) : bytesToString.IndexOf(delimiter);
                var toReturn        = 0;
                for (int i = 0; i <= numCharsToDelim; i++)
                {
                    toReturn += encoding.GetByteCount(bytesToString[startOfSegment + i].ToString());
                }

                // we get the total number of bytes, but we want to return the index (which starts at 0)
                // so we subtract 1 from the total number of bytes to get the final byte index.
                return(toReturn - 1);
            }

            //endOffset is a 'sentinel' value; we use that to figure out when to stop searching
            int endOffset = reverse ? startOffset - length : startOffset + length;

            // if we are starting at the end, we need to move toward the front enough to grab the right number of bytes
            startOffset = reverse ? startOffset - (bytesPerChar - 1) : startOffset;

            if (startOffset < 0 || startOffset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("startOffset", "Given start offset is outside the bounds of the given buffer. In reverse cases, the start offset is modified to ensure we check the full size of the last character");
            }

            // make sure that the length we are traversing is at least as long as a single character
            if (length < bytesPerChar)
            {
                throw new ArgumentOutOfRangeException("length", "Length must be at least as long as the length, in bytes, of a single character");
            }

            if (endOffset < -1 || endOffset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("length", "Given combination of startOffset and length would execute the search outside the bounds of the given buffer.");
            }

            int bufferEndOffset = reverse ? startOffset : startOffset + length;
            int result          = -1;

            for (int charPos = startOffset; reverse?charPos != endOffset : charPos + bytesPerChar - 1 < endOffset; charPos = reverse ? charPos - 1 : charPos + 1)
            {
                char c = bytesPerChar == 1 ? (char)buffer[charPos] : encoding.GetString(buffer, charPos, bytesPerChar).ToCharArray()[0];
                if (IsNewline(c, delimiter))
                {
                    result = charPos + bytesPerChar - 1;
                    break;
                }
            }

            if (string.IsNullOrEmpty(delimiter) && !reverse && result < bufferEndOffset - bytesPerChar && IsNewline(bytesPerChar == 1 ? (char)buffer[result + bytesPerChar] : encoding.GetString(buffer, result + 1, bytesPerChar).ToCharArray()[0]))
            {
                //we originally landed on a \r character; if we have a \r\n character, advance one position to include that
                result += bytesPerChar;
            }

            return(result);
        }