Beispiel #1
0
        public void JavaScriptStringEncode_AllRangesAllowed_StillEncodesForbiddenChars_Extended()
        {
            // Arrange
            JavaScriptStringEncoder encoder = new JavaScriptStringEncoder(UnicodeRanges.All);

            // Act & assert - BMP chars
            for (int i = 0; i <= 0xFFFF; i++)
            {
                string input = new String((char)i, 1);
                string expected;
                if (IsSurrogateCodePoint(i))
                {
                    expected = "\uFFFD"; // unpaired surrogate -> Unicode replacement char
                }
                else
                {
                    if (input == "\b")
                    {
                        expected = @"\b";
                    }
                    else if (input == "\t")
                    {
                        expected = @"\t";
                    }
                    else if (input == "\n")
                    {
                        expected = @"\n";
                    }
                    else if (input == "\f")
                    {
                        expected = @"\f";
                    }
                    else if (input == "\r")
                    {
                        expected = @"\r";
                    }
                    else if (input == "\\")
                    {
                        expected = @"\\";
                    }
                    else if (input == "/")
                    {
                        expected = @"\/";
                    }
                    else
                    {
                        bool mustEncode = false;
                        switch (i)
                        {
                        case '<':
                        case '>':
                        case '&':
                        case '\"':
                        case '\'':
                        case '+':
                            mustEncode = true;
                            break;
                        }

                        if (i <= 0x001F || (0x007F <= i && i <= 0x9F))
                        {
                            mustEncode = true; // control char
                        }
                        else if (!UnicodeHelpers.IsCharacterDefined((char)i))
                        {
                            mustEncode = true; // undefined (or otherwise disallowed) char
                        }

                        if (mustEncode)
                        {
                            expected = String.Format(CultureInfo.InvariantCulture, @"\u{0:X4}", i);
                        }
                        else
                        {
                            expected = input; // no encoding
                        }
                    }
                }

                string retVal = encoder.JavaScriptStringEncode(input);
                Assert.Equal(expected, retVal);
            }

            // Act & assert - astral chars
            for (int i = 0x10000; i <= 0x10FFFF; i++)
            {
                string input    = Char.ConvertFromUtf32(i);
                string expected = String.Format(CultureInfo.InvariantCulture, @"\u{0:X4}\u{1:X4}", (uint)input[0], (uint)input[1]);
                string retVal   = encoder.JavaScriptStringEncode(input);
                Assert.Equal(expected, retVal);
            }
        }
Beispiel #2
0
        protected static string ParseString(char[] json, ref int index, ref bool success)
        {
            StringBuilder s = new StringBuilder(BUILDER_CAPACITY);
            char          c;

            EatWhitespace(json, ref index);

            // "
            c = json[index++];

            bool complete = false;

            while (!complete)
            {
                if (index == json.Length)
                {
                    break;
                }

                c = json[index++];
                if (c == '"')
                {
                    complete = true;
                    break;
                }
                else if (c == '\\')
                {
                    if (index == json.Length)
                    {
                        break;
                    }
                    c = json[index++];
                    if (c == '"')
                    {
                        s.Append('"');
                    }
                    else if (c == '\\')
                    {
                        s.Append('\\');
                    }
                    else if (c == '/')
                    {
                        s.Append('/');
                    }
                    else if (c == 'b')
                    {
                        s.Append('\b');
                    }
                    else if (c == 'f')
                    {
                        s.Append('\f');
                    }
                    else if (c == 'n')
                    {
                        s.Append('\n');
                    }
                    else if (c == 'r')
                    {
                        s.Append('\r');
                    }
                    else if (c == 't')
                    {
                        s.Append('\t');
                    }
                    else if (c == 'u')
                    {
                        int remainingLength = json.Length - index;
                        if (remainingLength >= 4)
                        {
                            // parse the 32 bit hex into an integer codepoint
                            uint codePoint;
                            if (!(success = UInt32.TryParse(new string(json, index, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out codePoint)))
                            {
                                return("");
                            }
                            // convert the integer codepoint to a unicode char and add to string
                            s.Append(Char.ConvertFromUtf32((int)codePoint));
                            // skip 4 chars
                            index += 4;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    s.Append(c);
                }
            }

            if (!complete)
            {
                success = false;
                return(null);
            }

            return(s.ToString());
        }
Beispiel #3
0
 public bool CheckIfUtf32Letter()
 {
     return(Char.IsLetter(Char.ConvertFromUtf32(Char.ConvertToUtf32((char)InputStream.LA(-2), (char)InputStream.LA(-1))).Substring(0)[0]));
 }
 public void TestConvertFromUtf32Fail1()
 {
     Char.ConvertFromUtf32(-1);
 }
 public void TestConvertFromUtf32Fail3()
 {
     Char.ConvertFromUtf32(0xD800);
 }
Beispiel #6
0
        public static void Game_OnGameWndProc(WndEventArgs args)
        {
            if (Game.IsChatOpen && Menu.Item("EnableColor").GetValue <bool>())
            {
                if (args.Msg == 0x0101 && args.WParam == 0x0D)
                {
                    if (Game.IsKeyDown(0x10))
                    {
                        team = false;
                    }
                    else
                    {
                        team = true;
                    }
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Olive)
                {
                    modifier = "10";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Pink)
                {
                    modifier = "11";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Red)
                {
                    modifier = "12";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Orange)
                {
                    modifier = "13";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.DarkYellow)
                {
                    modifier = "14";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.LightGreen)
                {
                    modifier = "15";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Purple)
                {
                    modifier = "16";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Grey)
                {
                    modifier = "17";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Green)
                {
                    modifier = "18";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Blue)
                {
                    modifier = "19";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.White)
                {
                    modifier = "";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Rainbow)
                {
                    modifier = rand();
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.HotPink)
                {
                    modifier = "0E";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.VibrantOrange)
                {
                    modifier = "0F";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Violet)
                {
                    modifier = "1A";
                }
                if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.RedishPink)
                {
                    modifier = "1C";
                }
                if (args.Msg == 256)
                {
                    if (args.WParam == 13)
                    {
                        int    hexnum         = Int32.Parse(modifier, System.Globalization.NumberStyles.HexNumber);
                        string stringmodifier = Char.ConvertFromUtf32(hexnum);
                        if (Command == "")
                        {
                            return;
                        }
                        if (Menu.Item("Russia").GetValue <bool>())
                        {
                            Command = Romanize(Command);
                        }
                        if (Menu.Item("Color").GetValue <StringList>().SelectedIndex == (int)color.Rainbow)
                        {
                            Command = space(Command);
                        }
                        Game.ExecuteCommand(((team) ? "say_team " : "say ") + stringmodifier + Command);
                        Command = "";

                        return;
                    }
                    if (args.WParam == 8)
                    {
                        Command = Command.Substring(0, Command.Length - 1);
                        return;
                    }
                    if (args.WParam == 32)
                    {
                        Command += " ";
                        return;
                    }
                    if (args.WParam == 186)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += ":";
                            return;
                        }
                        else
                        {
                            Command += ";";
                        }
                        return;
                    }
                    if (args.WParam == 222)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "\"";
                            return;
                        }
                        else
                        {
                            Command += "'";
                        }
                        return;
                    }
                    if (args.WParam == 191 || args.WParam == 193)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "?";
                            return;
                        }
                        else
                        {
                            Command += "/";
                        }
                        return;
                    }
                    if (args.WParam == 190)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += ">";
                            return;
                        }
                        else
                        {
                            Command += ".";
                        }
                        return;
                    }
                    if (args.WParam == 188)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "<";
                            return;
                        }
                        else
                        {
                            Command += ",";
                        }
                        return;
                    }
                    if (args.WParam == 49)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "!";
                            return;
                        }
                        else
                        {
                            Command += "1";
                        }
                        return;
                    }
                    if (args.WParam == 50)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "@";
                            return;
                        }
                        else
                        {
                            Command += "2";
                        }
                        return;
                    }
                    if (args.WParam == 51)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "#";
                            return;
                        }
                        else
                        {
                            Command += "3";
                        }
                        return;
                    }
                    if (args.WParam == 52)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "$";
                            return;
                        }
                        else
                        {
                            Command += "4";
                        }
                        return;
                    }
                    if (args.WParam == 53)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "%";
                            return;
                        }
                        else
                        {
                            Command += "5";
                        }
                        return;
                    }
                    if (args.WParam == 54)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "^";
                            return;
                        }
                        else
                        {
                            Command += "6";
                        }
                        return;
                    }
                    if (args.WParam == 55)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "&";
                            return;
                        }
                        else
                        {
                            Command += "7";
                        }
                        return;
                    }
                    if (args.WParam == 56)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "*";
                            return;
                        }
                        else
                        {
                            Command += "8";
                        }
                        return;
                    }
                    if (args.WParam == 57)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "(";
                            return;
                        }
                        else
                        {
                            Command += "9";
                        }
                        return;
                    }
                    if (args.WParam == 48)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += ")";
                            return;
                        }
                        else
                        {
                            Command += "0";
                        }
                        return;
                    }
                    if (args.WParam == 189)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "_";
                            return;
                        }
                        else
                        {
                            Command += "-";
                        }
                        return;
                    }
                    if (args.WParam == 187)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "+";
                            return;
                        }
                        else
                        {
                            Command += "=";
                        }
                        return;
                    }
                    if (args.WParam == 192)
                    {
                        if (Game.IsKeyDown(16))
                        {
                            Command += "~";
                            return;
                        }
                        else
                        {
                            Command += "`";
                        }
                        return;
                    }

                    if (args.WParam == 45 || args.WParam == 46 || args.WParam == 19 || args.WParam == 36 || args.WParam == 35 || args.WParam == 34 || args.WParam == 33 || args.WParam == 93 || args.WParam == 91 || args.WParam == 17 || args.WParam == 9 || args.WParam == 16 || args.WParam == 112 || args.WParam == 113 || args.WParam == 114 || args.WParam == 115 || args.WParam == 116 || args.WParam == 117 || args.WParam == 118 || args.WParam == 119 || args.WParam == 120 || args.WParam == 121 || args.WParam == 122 || args.WParam == 123)
                    {
                        return;
                    }
                    if (args.WParam == 27)
                    {
                        Command = "";
                        return;
                    }

                    if (args.WParam == 107)
                    {
                        Command = "+";
                        return;
                    }
                    if (args.WParam == 109)
                    {
                        Command = "-";
                        return;
                    }
                    if (args.WParam == 106)
                    {
                        Command = "*";
                        return;
                    }
                    if (args.WParam == 219)
                    {
                        Command = "é";
                        return;
                    }
                    if (Game.IsKeyDown(16))
                    {
                        Command += Utils.KeyToText((uint)args.WParam).ToUpper();

                        return;
                    }
                    else
                    {
                        Command += Utils.KeyToText((uint)args.WParam).ToLower();
                    }



                    return;
                }
            }
        }
        //file 선택 후 binary stream 읽고 parsing
        public void eventlog_parsing_max_xt()
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title    = "file open";
            ofd.FileName = "test";
            ofd.Filter   = "event log 파일|*.evl";

            DialogResult dr = ofd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                dt       = new DataTable();
                fileName = ofd.SafeFileName;
                filePath = ofd.FileName;
                FileInfo fi = new FileInfo(filePath);
                fileLength = fi.Length;
                if (!fileName.Contains("MX"))
                {
                    MessageBox.Show("Wrong file!");
                }

                else
                {
                    BinaryReader rdr = new BinaryReader(File.Open(filePath, FileMode.Open));

                    byte[] Header_bytes = new byte[2];
                    byte[] info_bytes   = new byte[18];

                    rdr.BaseStream.Position = 0; int i = 0;


                    SetUpData();

                    while (rdr.BaseStream.Position < fileLength)
                    {
                        do
                        {
                            Header_bytes[i] = rdr.ReadByte();
                        } while (Header_bytes[i] == 0xff);

                        if (i > 0 && Char.ConvertFromUtf32(Header_bytes[i - 1]) == "S" && Char.ConvertFromUtf32(Header_bytes[i]) == "N")
                        {
                            string sn = ""; int h_cnt = 0; string size = "";
                            byte   s;

                            //header
                            while (rdr.BaseStream.Position < fileLength)
                            {
                                s = rdr.ReadByte();
                                //serial number
                                if (h_cnt == 0 && s == 0x3a)
                                {
                                    s = rdr.ReadByte();
                                    while (s != 13)
                                    {
                                        sn += Char.ConvertFromUtf32(s);
                                        s   = rdr.ReadByte();
                                    }
                                }
                                //size
                                if (h_cnt == 4 && s == 0x3a)
                                {
                                    s = rdr.ReadByte();
                                    while (s != 13)
                                    {
                                        size += Char.ConvertFromUtf32(s);
                                        s     = rdr.ReadByte();
                                    }
                                }
                                if (s == 0x0d && rdr.ReadByte() == 0x0a)
                                {
                                    h_cnt++;
                                }
                                if (h_cnt == 5)
                                {
                                    break;
                                }
                            }
                            //eventlog parsing
                            int sz = Convert.ToInt32(size);

                            do
                            {
                                info_bytes = rdr.ReadBytes(18);
                                sz        -= 18;
                                if (info_bytes[0] != 0xff)
                                {
                                    string[] reverse = new string[18];
                                    string   date_bytes;
                                    BitArray bits = new BitArray(info_bytes);
                                    for (int n = 0; n < 144; n++)
                                    {
                                        reverse[n / 8] = make_bit(bits[n]) + reverse[n / 8];
                                    }

                                    date_bytes = reverse[0] + reverse[1] + reverse[2] + reverse[3];
                                    int hour = Convert.ToInt32(date_bytes.Substring(16, 4), 2);

                                    if (date_bytes.Substring(0, 1).Equals("1"))
                                    {
                                        hour += 12;
                                    }

                                    DateTime date_time = new DateTime((2000 + Convert.ToInt32(date_bytes.Substring(1, 6), 2)), Convert.ToInt32(date_bytes.Substring(7, 4), 2), Convert.ToInt32(date_bytes.Substring(11, 5), 2), hour, Convert.ToInt32(date_bytes.Substring(20, 6), 2), Convert.ToInt32(date_bytes.Substring(26, 6), 2));
                                    int      duration  = Convert.ToInt32((reverse[4] + reverse[5]).Substring(1, 15), 2);

                                    //string bump = chk_bump(reverse[4].Substring(0, 1));
                                    string H2S_status = status(reverse[6]);
                                    float  H2S_Peak   = gas_reading(reverse[6], reverse[7], reverse[8]);
                                    string CO_status  = status(reverse[9]);
                                    float  CO_Peak    = gas_reading(reverse[9], reverse[10], reverse[11]);
                                    string O2_status  = status(reverse[12]);
                                    float  O2_Peak    = gas_reading(reverse[12], reverse[13], reverse[14]);
                                    string LEL_status = status(reverse[15]);
                                    float  LEL_Peak   = gas_reading(reverse[15], reverse[16], reverse[17]);

                                    dt.Rows.Add(sn, date_time.ToString("yyyy/MM/dd HH:mm:ss"), "Peak Exposure", duration, H2S_status, H2S_Peak, CO_status, CO_Peak, O2_status, O2_Peak, LEL_status, LEL_Peak);
                                }
                            } while (sz >= 18);
                        }
                        i += 1;
                        i  = i % 2;
                    }
                    rdr.Close();
                }
            }
        }
    void __clickEmoji2(EventContext context)
    {
        GButton item = (GButton)context.data;

        _input2.ReplaceSelection(Char.ConvertFromUtf32(Convert.ToInt32(UIPackage.GetItemByURL(item.icon).name, 16)));
    }
Beispiel #9
0
 static string fromCharCode(int c)
 {
     return(Char.ConvertFromUtf32(c));
 }
Beispiel #10
0
        static void Main(string[] args)
        {
            Console.WriteLine("Wklej adres strony:");
            string        url, folder = Directory.GetCurrentDirectory() + @"\images\";
            List <string> exts = new List <string>()
            {
                ".jpg", ".png"
            };
            List <string> attributes = new List <string>()
            {
                "data-media-s4", "data-media-s3", "data-media-s2", "data-media-s1", "data-src-mobile", "srcset", "data-src-pc", "src"
            };

            try
            {
                Directory.CreateDirectory(folder);
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("Brak uprawnień\r\nUruchom jako administrator albo zmień lokalizację programu");
                return;
            }

            while (!String.IsNullOrEmpty(url = Console.ReadLine()))
            {
                if (url[0] == '\n')
                {
                    break;
                }
                Uri uriResult;
                if (Uri.TryCreate(url, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
                {
                    string subFolder = folder + uriResult.Host + @"\";
                    for (int i = 0; i < uriResult.Segments.Length; i++)
                    {
                        subFolder += uriResult.Segments[i] + @"\";
                    }
                    Directory.CreateDirectory(subFolder);

                    using (ExcelPackage excel = new ExcelPackage())
                    {
                        if (excel.Workbook.Worksheets["Images"] == null)
                        {
                            excel.Workbook.Worksheets.Add("Images");
                        }
                        var             worksheet = excel.Workbook.Worksheets["Images"];
                        List <string[]> rows      = new List <string[]>()
                        {
                            new string[] { "Lp", "Url", "File name", "File extension", "Size(kB)", "HTML tag", "attribute", "Alt" }
                        };

                        HtmlNode html = LoadHtmlDocument(uriResult);

                        List <HtmlNode> nodes = new List <HtmlNode>();
                        if (html != null)
                        {
                            nodes.Add(html);
                        }
                        List <string> fileNames = new List <string>();

                        List <ImageInfo> images = GetImagesInfo(nodes, exts, attributes, subFolder);
                        int i = 1, duplicates = 0;
                        foreach (ImageInfo image in images)
                        {
                            string[] row = new string[8];

                            row[0] = i.ToString();
                            row[1] = image.Uri.ToString();
                            row[2] = image.FileInfo.Name.Substring(0, image.FileInfo.Name.LastIndexOf("."));
                            row[3] = image.FileInfo.Extension;
                            row[5] = image.Name;
                            row[6] = image.Attribute;
                            row[7] = image.Alt;
                            if (fileNames.Contains(image.FileInfo.Name))
                            {
                                duplicates++;
                            }
                            else
                            {
                                Console.WriteLine($"Saving as {image.FileInfo.FullName}");
                                if (!DownloadImage(image.FileInfo.Directory.ToString(), image.Uri, new WebClient()))
                                {
                                    continue;
                                }
                            }
                            if (File.Exists(image.FileInfo.FullName))
                            {
                                row[4] = Math.Round(image.FileInfo.Length / 1024f).ToString();
                                fileNames.Add(image.FileInfo.Name);
                            }
                            rows.Add(row);
                            i++;
                        }
                        ;

                        string headerRange = "A1:" + Char.ConvertFromUtf32(rows[0].Length + 64) + "1";
                        worksheet.Cells[headerRange].LoadFromArrays(rows);
                        worksheet.Cells[$"A1:{'A'+ Char.ConvertFromUtf32(rows[0].Length + 64)}{i+1}"].AutoFitColumns();

                        int groupIndex = 0;
                        foreach (var group in images.GroupBy(x => x.Parent))
                        {
                            if (groupIndex % 2 == 1)
                            {
                                int min = 0, max = 0;
                                min = group.Min(x => images.IndexOf(x)) + 2;
                                max = group.Max(x => images.IndexOf(x)) + 2;
                                var test = $"A{min}:{'A' + Char.ConvertFromUtf32(rows[0].Length + 64)}{max}";
                                worksheet.Cells[$"A{min}:{'A' + Char.ConvertFromUtf32(rows[0].Length + 64)}{max}"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                                worksheet.Cells[$"A{min}:{'A' + Char.ConvertFromUtf32(rows[0].Length + 64)}{max}"].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);
                            }
                            groupIndex += 1;
                        }

                        string fileName = "";
                        for (int j = 0; j < uriResult.Segments.Length; j++)
                        {
                            fileName += uriResult.Segments[j];
                        }
                        fileName = fileName.Replace("/", "-");
                        if (fileName.Length > 100)
                        {
                            fileName = fileName.Substring(0, 100);
                        }

                        try
                        {
                            excel.SaveAs(new FileInfo(Directory.GetCurrentDirectory() + "/Images-cc" + fileName + ".xlsx"));
                            Console.WriteLine($"\r\n\r\nLiczba obrazków na stronie(bez <noscript></noscript>): {images.Count}");
                            Console.WriteLine($"Liczba tagów <picture>: {images.Where(x => x.Name == "source").GroupBy(x => x.Parent).Select(x => x.First()).Count()}");
                            Console.WriteLine($"Liczba tagów <img>: {images.Where(x => x.Name == "img" && x.Parent.Name != "picture").GroupBy(x => x.Parent).Select(x => x.First()).Count()}");

                            Console.WriteLine($"\r\nZapisano do .xlsx: { i - 1 }");
                            Console.WriteLine($"Duplikatów: {duplicates}\r\n");
                            Console.WriteLine($"Pobrano plików: {fileNames.GroupBy(x => x).Select(x => x.First()).Count()}\r\n\r\nKoniec\r\n\r\nWklej kolejny adres strony");
                        }
                        catch
                        {
                            Console.WriteLine("Nie można nadpisać pliku xlsx, prawdopodobnie jest używany przez inny proces");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Zły adres(tylko http/https)");
                }
            }
        }
Beispiel #11
0
        private static void imprimePlanilhaConcatenada(IEnumerable <PlanilhaCoordenadas> planilhaJoin, IEnumerable <LojaImportacao> lojas, IEnumerable <LojaImportacao> lojaSemFim)
        {
            using (ExcelPackage excel = new ExcelPackage())
            {
                excel.Workbook.Worksheets.Add("Planilha comparada com KML");
                excel.Workbook.Worksheets.Add("Worksheet2");
                excel.Workbook.Worksheets.Add("Lojas Sem Fim no Mapa");

                List <string[]> headerRowPlanilha1 = new List <string[]>()
                {
                    new string[] {
                        "Loja",
                        "Cidade",
                        "coordenada KML",
                        "coordenanda Poligono",
                        "coordenada Mapa",
                        "potencial",
                        "loja",
                        "cidade",
                        "regional",
                        "nome reduzido"
                    }
                };

                List <string[]> headerRowPlanilha2 = new List <string[]>()
                {
                    new string[] {
                        "Loja",
                        "Cidade",
                        "coordenada KML",
                        "coordenanda Poligono",
                        "coordenada Mapa"
                    }
                };

                // Determine the header range (e.g. A1:D1)
                string headerRange1 = "A1:" + Char.ConvertFromUtf32(headerRowPlanilha1[0].Length + 64) + "1";
                string headerRange2 = "A1:" + Char.ConvertFromUtf32(headerRowPlanilha2[0].Length + 64) + "1";

                // Target a worksheet
                ExcelWorksheet worksheet1 = excel.Workbook.Worksheets["Planilha comparada com KML"];
                ExcelWorksheet worksheet2 = excel.Workbook.Worksheets["Worksheet2"];
                ExcelWorksheet worksheet3 = excel.Workbook.Worksheets["Lojas Sem Fim no Mapa"];

                // Popular header row data
                worksheet1.Cells[headerRange1].LoadFromArrays(headerRowPlanilha1);
                worksheet2.Cells[headerRange2].LoadFromArrays(headerRowPlanilha2);


                worksheet1.Cells[2, 1].LoadFromCollection <PlanilhaCoordenadas>(planilhaJoin);
                worksheet2.Cells[2, 1].LoadFromCollection <LojaImportacao>(lojas);

                if (lojaSemFim.Count() > 0)
                {
                    worksheet3.Cells[headerRange2].LoadFromArrays(headerRowPlanilha2);
                    worksheet3.Cells[2, 1].LoadFromCollection <LojaImportacao>(lojaSemFim);
                }

                FileInfo excelFile = new FileInfo(@"C:\Users\thiago.hpereira\Documents\Visual Studio 2017\Projects\Teco\Lojas18-03-19.xlsx");
                excel.SaveAs(excelFile);
            }
        }
Beispiel #12
0
        protected static string ParseString(char[] json, bool sanitarizeHtml, ref int index, ref bool success)
        {
            var s = new StringBuilder(BuilderCapacity);

            EatWhitespace(json, ref index);

            // "
            char c = json[index++];

            bool complete = false;

            while (!complete)
            {
                if (index == json.Length)
                {
                    break;
                }

                c = json[index++];
                if (c == '"')
                {
                    complete = true;
                    break;
                }

                if (c == '\\')
                {
                    if (index == json.Length)
                    {
                        break;
                    }
                    c = json[index++];
                    if (c == '"')
                    {
                        s.Append('"');
                    }
                    else if (c == '\\')
                    {
                        s.Append('\\');
                    }
                    else if (c == '/')
                    {
                        s.Append('/');
                    }
                    else if (c == 'b')
                    {
                        s.Append('\b');
                    }
                    else if (c == 'f')
                    {
                        s.Append('\f');
                    }
                    else if (c == 'n')
                    {
                        s.Append('\n');
                    }
                    else if (c == 'r')
                    {
                        s.Append('\r');
                    }
                    else if (c == 't')
                    {
                        s.Append('\t');
                    }
                    else if (c == 'u')
                    {
                        int remainingLength = json.Length - index;
                        if (remainingLength >= 4)
                        {
                            // parse the 32 bit hex into an integer codepoint
                            uint codePoint;
                            if (!(success = UInt32.TryParse(new string(json, index, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out codePoint)))
                            {
                                return("");
                            }
                            // convert the integer codepoint to a unicode char and add to string
                            s.Append(Char.ConvertFromUtf32((int)codePoint));
                            // skip 4 chars
                            index += 4;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    s.Append(c);
                }
            }

            if (!complete)
            {
                success = false;
                CommonFunctions.CatchExceptions(new Exception("JSON parser error Decode JSON. ParseString"), string.Format("json={0} index={1}", CommonFunctions.CharToString(json), index));
                return(null);
            }

            return(sanitarizeHtml ? HttpUtility.HtmlDecode(Sanitizer.GetSafeHtmlFragment(s.ToString().Replace(" ", "&nbsp;")).Replace("&nbsp;", " ")) : s.ToString());
        }
Beispiel #13
0
        public static string FromRStringLiteral(this string s)
        {
            if (s.Length < 2)
            {
                throw new FormatException("Not a quoted R string literal");
            }

            char quote = s[0];

            if (s[s.Length - 1] != quote)
            {
                throw new FormatException("Mismatching quotes");
            }

            var  sb     = new StringBuilder();
            bool escape = false;

            for (int i = 1; i < s.Length - 1; ++i)
            {
                char c = s[i];
                if (escape)
                {
                    // https://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html
                    switch (c)
                    {
                    case 'n':
                        sb.Append("\n");
                        break;

                    case 'r':
                        sb.Append("\r");
                        break;

                    case 't':
                        sb.Append("\t");
                        break;

                    case 'b':
                        sb.Append("\b");
                        break;

                    case 'a':
                        sb.Append("\a");
                        break;

                    case 'f':
                        sb.Append("\f");
                        break;

                    case 'v':
                        sb.Append("\v");
                        break;

                    case 'x':
                        // 1 to 2 hex digits, lowercase or uppercase
                        if (i < s.Length - 1)
                        {
                            int val;
                            if (HexCharToDecimal(s[i + 1], out val))
                            {
                                i++;
                                if (i < s.Length - 1)
                                {
                                    int nextVal;
                                    if (HexCharToDecimal(s[i + 1], out nextVal))
                                    {
                                        i++;
                                        val = (val << 4) | nextVal;
                                    }
                                }

                                sb.Append(Char.ConvertFromUtf32(val));
                            }
                            else
                            {
                                throw new FormatException("Expected hex character");
                            }
                        }
                        else
                        {
                            throw new FormatException("Unexpected end of string");
                        }
                        break;

                    case '\\':
                        sb.Append(c);
                        break;

                    case '"':
                        sb.Append(c);
                        break;

                    case '\'':
                        sb.Append(c);
                        break;

                    default:
                        if (c >= '0' && c <= '7')
                        {
                            // 1 to 3 octal digits
                            int val = c - '0';
                            if (i < s.Length - 1)
                            {
                                char next = s[i + 1];
                                if (next >= '0' && next <= '7')
                                {
                                    i++;
                                    val = (val << 3) | (next - '0');
                                    if (i < s.Length - 1)
                                    {
                                        next = s[i + 1];
                                        if (next >= '0' && next <= '7')
                                        {
                                            i++;
                                            val = (val << 3) | (next - '0');
                                        }
                                    }
                                }
                            }

                            sb.Append(char.ConvertFromUtf32(val));
                            break;
                        }

                        throw new FormatException("Unrecognized escape sequence");
                    }
                    escape = false;
                }
                else
                {
                    if (c == quote)
                    {
                        throw new FormatException("Unescaped embedded quote");
                    }
                    else if (c == '\\')
                    {
                        escape = true;
                    }
                    else
                    {
                        sb.Append(c);
                    }
                }
            }

            return(sb.ToString());
        }
Beispiel #14
0
        public void JavaScriptStringEncode_DoesNotOutputHtmlSensitiveCharacters()
        {
            // Per the design document, we provide additional defense-in-depth
            // by never emitting HTML-sensitive characters unescaped.

            // Arrange
            JavaScriptStringEncoder javaScriptStringEncoder = new JavaScriptStringEncoder(UnicodeRanges.All);
            HtmlEncoder             htmlEncoder             = new HtmlEncoder(UnicodeRanges.All);

            // Act & assert
            for (int i = 0; i <= 0x10FFFF; i++)
            {
                if (IsSurrogateCodePoint(i))
                {
                    continue; // surrogates don't matter here
                }

                string javaScriptStringEncoded = javaScriptStringEncoder.JavaScriptStringEncode(Char.ConvertFromUtf32(i));
                string thenHtmlEncoded         = htmlEncoder.HtmlEncode(javaScriptStringEncoded);
                Assert.Equal(javaScriptStringEncoded, thenHtmlEncoded); // should have contained no HTML-sensitive characters
            }
        }
        /// <summary>
        /// Strip RTF Tags from RTF Text
        /// </summary>
        /// <param name="inputRtf">RTF formatted text</param>
        /// <returns>Plain text from RTF</returns>
        public static string StripRichTextFormat(string inputRtf)
        {
            if (inputRtf == null)
            {
                return(null);
            }

            string returnString;

            var  stack     = new Stack <StackEntry>();
            bool ignorable = false;               // Whether this group (and all inside it) are "ignorable".
            int  ucskip    = 1;                   // Number of ASCII characters to skip after a unicode character.
            int  curskip   = 0;                   // Number of ASCII characters left to skip
            var  outList   = new List <string>(); // Output buffer.

            Match match = _rtfRegex.Match(inputRtf);

            if (!match.Success)
            {
                // Didn't match the regex
                return(inputRtf);
            }

            while (match.Success)
            {
                string word      = match.Groups[1].Value;
                string arg       = match.Groups[2].Value;
                string hex       = match.Groups[3].Value;
                string character = match.Groups[4].Value;
                string brace     = match.Groups[5].Value;
                string tchar     = match.Groups[6].Value;

                if (!String.IsNullOrEmpty(brace))
                {
                    curskip = 0;
                    if (brace == "{")
                    {
                        // Push state
                        stack.Push(new StackEntry(ucskip, ignorable));
                    }
                    else if (brace == "}")
                    {
                        // Pop state
                        StackEntry entry = stack.Pop();
                        ucskip    = entry.NumberOfCharactersToSkip;
                        ignorable = entry.Ignorable;
                    }
                }
                else if (!String.IsNullOrEmpty(character)) // \x (not a letter)
                {
                    curskip = 0;
                    if (character == "~")
                    {
                        if (!ignorable)
                        {
                            outList.Add("\xA0");
                        }
                    }
                    else if ("{}\\".Contains(character))
                    {
                        if (!ignorable)
                        {
                            outList.Add(character);
                        }
                    }
                    else if (character == "*")
                    {
                        ignorable = true;
                    }
                }
                else if (!String.IsNullOrEmpty(word)) // \foo
                {
                    curskip = 0;
                    if (destinations.Contains(word))
                    {
                        ignorable = true;
                    }
                    else if (ignorable)
                    {
                    }
                    else if (specialCharacters.ContainsKey(word))
                    {
                        outList.Add(specialCharacters[word]);
                    }
                    else if (word == "uc")
                    {
                        ucskip = Int32.Parse(arg);
                    }
                    else if (word == "u")
                    {
                        int c = Int32.Parse(arg);
                        if (c < 0)
                        {
                            c += 0x10000;
                        }
                        outList.Add(Char.ConvertFromUtf32(c));
                        curskip = ucskip;
                    }
                }
                else if (!String.IsNullOrEmpty(hex)) // \'xx
                {
                    if (curskip > 0)
                    {
                        curskip -= 1;
                    }
                    else if (!ignorable)
                    {
                        int c = Int32.Parse(hex, System.Globalization.NumberStyles.HexNumber);
                        outList.Add(Char.ConvertFromUtf32(c));
                    }
                }
                else if (!String.IsNullOrEmpty(tchar))
                {
                    if (curskip > 0)
                    {
                        curskip -= 1;
                    }
                    else if (!ignorable)
                    {
                        outList.Add(tchar);
                    }
                }

                // Get the next match
                match = match.NextMatch();
            }

            returnString = String.Join(String.Empty, outList.ToArray());

            return(returnString);
        }
Beispiel #16
0
	protected static string parseString( char[] json, ref int index )
	{
		string s = "";
		char c;

		eatWhitespace( json, ref index );

		// "
		c = json[index++];

		bool complete = false;
		while( !complete )
		{
			if( index == json.Length )
				break;

			c = json[index++];
			if( c == '"' )
			{
				complete = true;
				break;
			}
			else if( c == '\\' )
			{
				if( index == json.Length )
					break;

				c = json[index++];
				if( c == '"' )
				{
					s += '"';
				}
				else if( c == '\\' )
				{
					s += '\\';
				}
				else if( c == '/' )
				{
					s += '/';
				}
				else if( c == 'b' )
				{
					s += '\b';
				}
				else if( c == 'f' )
				{
					s += '\f';
				}
				else if( c == 'n' )
				{
					s += '\n';
				}
				else if( c == 'r' )
				{
					s += '\r';
				}
				else if( c == 't' )
				{
					s += '\t';
				}
				else if( c == 'u' )
				{
					int remainingLength = json.Length - index;
					if( remainingLength >= 4 )
					{
						char[] unicodeCharArray = new char[4];
						Array.Copy( json, index, unicodeCharArray, 0, 4 );

						uint codePoint = UInt32.Parse( new string( unicodeCharArray ), System.Globalization.NumberStyles.HexNumber );

						// convert the integer codepoint to a unicode char and add to string
						s += Char.ConvertFromUtf32( (int)codePoint );

						// skip 4 chars
						index += 4;
					}
					else
					{
						break;
					}
				}
			}
			else
			{
				s += c;
			}

		}

		if( !complete )
			return null;

		return s;
	}
Beispiel #17
0
        public static string UnescapeString(string str)
        {
            if (str.IndexOf('\\') == -1)
            {
                return(str);                //20131215 DKh 6.3% speed improvement in JSON Integration test - similar problem
            }
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];
                if ((i < str.Length - 1) && (c == '\\'))
                {
                    i++;
                    char n = str[i];

                    switch (n)
                    {
                    case '\\': sb.Append('\\'); break;

                    case '0': sb.Append((char)CharCodes.Char0); break;

                    case 'a': sb.Append((char)CharCodes.AlertBell); break;

                    case 'b': sb.Append((char)CharCodes.Backspace); break;

                    case 'f': sb.Append((char)CharCodes.Formfeed); break;

                    case 'n': sb.Append((char)CharCodes.LF); break;

                    case 'r': sb.Append((char)CharCodes.CR); break;

                    case 't': sb.Append((char)CharCodes.Tab); break;

                    case 'v': sb.Append((char)CharCodes.VerticalQuote); break;

                    case 'u': //  \uFFFF
                        string hex = string.Empty;
                        int    cnt = 0;
                        //loop through UNICODE hex number chars
                        while ((i < str.Length - 1) && (cnt < 4))
                        {
                            i++;
                            hex += str[i];
                            cnt++;
                        }

                        try
                        {
                            sb.Append(Char.ConvertFromUtf32(Convert.ToInt32(hex, 16)));
                        }
                        catch
                        {
                            throw new StringEscapeErrorException(hex);
                        }

                        break;

                    default:
                        throw new StringEscapeErrorException(String.Format("{0}{1}", c, n));
                    }
                }
                else
                {
                    sb.Append(c);
                }
            }//for

            return(sb.ToString());
        }
Beispiel #18
0
        //file 선택
        public void eventlog_parsing_bwc()
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title    = "file open";
            ofd.FileName = "test";
            ofd.Filter   = "event log 파일|*.evl|bin 파일|*.bin";
            byte[]       binfile;
            DialogResult dr = ofd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                dt       = new DataTable();
                fileName = ofd.SafeFileName;
                filePath = ofd.FileName;
                FileInfo fi = new FileInfo(filePath);
                fileLength = fi.Length;
                DataRow workRow;

                //.bin 파일 parsing
                if (Path.GetExtension(ofd.FileName) == ".bin")
                {
                    SetUpData_bin();
                    int cnt = 0;
                    binfile = File.ReadAllBytes(filePath);
                    string sn = "";
                    int    sz = binfile.Length; sz -= 40; cnt += 40;
                    for (int i = 0; i < 18; i++)
                    {
                        sn += Convert.ToChar(binfile[i]);
                    }
                    while (sz >= 21)
                    {
                        workRow = dt.NewRow();

                        workRow[0] = sn;
                        workRow[1] = bwTime(BitConverter.ToUInt32(binfile, 4 + cnt));
                        workRow[2] = bwTime(BitConverter.ToUInt32(binfile, 8 + cnt));
                        int mins      = Convert.ToUInt16(binfile[14 + cnt]);
                        int RemainMin = BitConverter.ToUInt16(binfile, 12 + cnt) * 30 + mins;
                        workRow[3] = RemainMin;
                        int days = RemainMin / 1440;
                        int hrs  = (RemainMin - (days * 1440)) / 60;

                        workRow[4] = days + " days, " + hrs + " hrs, " + mins + " mins";
                        workRow[5] = "SO2";

                        string eventType = "";
                        switch (binfile[20 + cnt])
                        {
                        case 0xa0:
                            eventType = ("Peak Exposure");
                            break;

                        case 0xa1:
                            eventType = ("Bump Test");
                            break;

                        case 0xa2:
                            eventType = ("Zero Cal");
                            break;

                        case 0xa3:
                            eventType = ("Span Cal");
                            break;
                        }
                        workRow[6] = eventType;


                        workRow[7]  = Math.Round((((float)BitConverter.ToInt32(binfile, cnt) / 256)), 1);
                        workRow[8]  = BitConverter.ToInt16(binfile, cnt + 18);
                        workRow[9]  = Math.Round(((float)BitConverter.ToInt32(binfile, 18) / 256), 1);
                        workRow[10] = Math.Round(((float)BitConverter.ToInt32(binfile, 22) / 256), 1);
                        workRow[11] = BitConverter.ToUInt16(binfile, 30);
                        workRow[12] = BitConverter.ToUInt32(binfile, 36) / 86400 + " days";
                        bool check_overlap = false;
                        foreach (DataRow d in dt.Rows)
                        {
                            if (d[2].Equals(workRow[2]))
                            {
                                check_overlap = true;
                            }
                        }
                        if (!check_overlap && !workRow[2].ToString().Contains("0/0/0"))
                        {
                            dt.Rows.Add(workRow);
                        }
                        sz  -= 21;
                        cnt += 21;
                    }
                }

                //.evl 파일 parsing
                else if (Path.GetExtension(ofd.FileName) == ".evl")
                {
                    if (filePath.Contains("BC"))
                    {
                        string[]     log_distinct_bits = new string[fileLength / 8];
                        BinaryReader rdr          = new BinaryReader(File.Open(filePath, FileMode.Open));
                        byte[]       Header_bytes = new byte[40];
                        byte[]       info_bytes   = new byte[21];

                        int i = 0;

                        SetUpData();

                        while (rdr.BaseStream.Position < fileLength)
                        {
                            do
                            {
                                Header_bytes[i] = rdr.ReadByte();
                            } while (Header_bytes[i] == 0x00);

                            if (i > 0 && Header_bytes[i - 1] == 0x53 && Header_bytes[i] == 0x4e) //find "SN"
                            {
                                string   sn = ""; int header_cnt = 0; string s_size = ""; string dock_time = ""; string FW = "";
                                DateTime DockTime = new DateTime();
                                byte     s;

                                //Header(IDX)
                                while (rdr.BaseStream.Position < fileLength)
                                {
                                    s = rdr.ReadByte();
                                    //serial number
                                    if (header_cnt == 0 && s == 0x3a)
                                    {
                                        s = rdr.ReadByte();
                                        while (s != 0x0d)
                                        {
                                            sn += Char.ConvertFromUtf32(s);
                                            s   = rdr.ReadByte();
                                        }
                                    }
                                    //DockTime
                                    if (header_cnt == 1 && s == 0x3a)
                                    {
                                        s = rdr.ReadByte();
                                        while (s != 0x0d)
                                        {
                                            dock_time += Char.ConvertFromUtf32(s);
                                            s          = rdr.ReadByte();
                                        }
                                        string[] logtime = dock_time.Split(':', ' ', '-');
                                        DockTime = new DateTime(int.Parse(logtime[0]), int.Parse(logtime[1]), int.Parse(logtime[2]), int.Parse(logtime[3]), int.Parse(logtime[4]), int.Parse(logtime[5]));
                                    }
                                    //FW
                                    if (header_cnt == 2 && s == 0x3a)
                                    {
                                        s = rdr.ReadByte();
                                        while (s != 0x0d)
                                        {
                                            FW += Char.ConvertFromUtf32(s);
                                            s   = rdr.ReadByte();
                                        }
                                    }
                                    //Size
                                    if (header_cnt == 4 && s == 0x3a)
                                    {
                                        s = rdr.ReadByte();
                                        while (s != 0x0d)
                                        {
                                            s_size += Char.ConvertFromUtf32(s);
                                            s       = rdr.ReadByte();
                                        }
                                    }
                                    if (s == 0x0d && rdr.ReadByte() == 0x0a)
                                    {
                                        header_cnt++;
                                    }
                                    if (header_cnt == 5)
                                    {
                                        break;
                                    }
                                }

                                int sz = Convert.ToInt32(s_size);

                                //Header(log)

                                Header_bytes = rdr.ReadBytes(40); sz -= 40;

                                //Data(log)
                                while (sz >= 21)
                                {
                                    workRow = dt.NewRow();

                                    info_bytes = rdr.ReadBytes(21);
                                    sz        -= 21;
                                    workRow[0] = sn;
                                    workRow[1] = FW;
                                    workRow[2] = bwTime(BitConverter.ToUInt32(info_bytes, 4));
                                    workRow[3] = bwTime(BitConverter.ToUInt32(info_bytes, 8));
                                    int mins      = Convert.ToUInt16(info_bytes[14]);
                                    int RemainMin = BitConverter.ToUInt16(info_bytes, 12) * 30 + mins;
                                    workRow[4] = RemainMin;
                                    int days = RemainMin / 1440;
                                    int hrs  = (RemainMin - (days * 1440)) / 60;

                                    workRow[5] = days + " days, " + hrs + " hrs, " + mins + " mins";
                                    workRow[6] = "SO2";

                                    //get event type
                                    string eventType = "";
                                    switch (info_bytes[20])
                                    {
                                    case 0xa0:
                                        eventType = ("Peak Exposure");
                                        break;

                                    case 0xa1:
                                        eventType = ("Bump Test");
                                        break;

                                    case 0xa2:
                                        eventType = ("Zero Cal");
                                        break;

                                    case 0xa3:
                                        eventType = ("Span Cal");
                                        break;
                                    }
                                    workRow[7] = eventType;

                                    workRow[8]  = Math.Round((((float)BitConverter.ToInt32(info_bytes, 0) / 256)), 1);      //Peak Gas Reading
                                    workRow[9]  = BitConverter.ToInt16(info_bytes, 18);                                     //Duration of Alarm (seconds)
                                    workRow[10] = Math.Round(((float)BitConverter.ToInt32(Header_bytes, 18) / 256), 1);     //Low Alarm Pt
                                    workRow[11] = Math.Round(((float)BitConverter.ToInt32(Header_bytes, 22) / 256), 1);     //High Alarm Pt
                                    workRow[12] = BitConverter.ToUInt16(Header_bytes, 30);                                  //Total Unit Alarm Time (minutes)
                                    workRow[13] = BitConverter.ToUInt32(Header_bytes, 36) / 86400 + " days";                //Remaining Hibernate time

                                    bool check_overlap = false;
                                    foreach (DataRow d in dt.Rows)
                                    {
                                        if (d[2].Equals(workRow[2]))
                                        {
                                            check_overlap = true;
                                        }
                                    }
                                    if (!check_overlap && !workRow[2].ToString().Contains("0/0/0"))
                                    {
                                        dt.Rows.Add(workRow);
                                    }
                                }
                            }
                            i++;
                            i = i % 2;
                        }
                        rdr.Close();
                    }
                }

                else
                {
                    MessageBox.Show("Wrong File");
                }
            }
        }
Beispiel #19
0
 public override string ToString()
 {
     return(Char.ConvertFromUtf32(m_Unicode));
 }
        public string ParseString(char[] jsonChars, ref int index)
        {
            //  "   find end  " ... add chars to string builder ... escape \", \t, \n , etc. => \\", \\t, \\n
            // return string formatted
            var  result = new StringBuilder();
            char c;

            // move to first "
            var stringToken = this.MoveAndGetNextToken(jsonChars, ref index);

            if (stringToken != JsonToken.String)
            {
                throw new JsonLibException("Invalid Json. Expected double quotes");
            }

            bool stringClosed = false;

            while (!stringClosed)
            {
                if (index == jsonChars.Length)
                {
                    break;
                }

                c = jsonChars[index];

                index++;

                if (c == '"')
                {
                    stringClosed = true;
                    break;
                }
                else if (c == '\\')
                {
                    if (index == jsonChars.Length)
                    {
                        break;
                    }

                    c = jsonChars[index];

                    index++;

                    if (c == '"')
                    {
                        result.Append('"');
                    }
                    else if (c == '\\')
                    {
                        result.Append('\\');
                    }
                    else if (c == '/')
                    {
                        result.Append('/');
                    }
                    else if (c == 'b')
                    {
                        result.Append('\b');
                    }
                    else if (c == 'f')
                    {
                        result.Append('\f');
                    }
                    else if (c == 'n')
                    {
                        result.Append('\n');
                    }
                    else if (c == 'r')
                    {
                        result.Append('\r');
                    }
                    else if (c == 't')
                    {
                        result.Append('\t');
                    }
                    else if (c == 'u')
                    {
                        int remainingLength = jsonChars.Length - index;
                        if (remainingLength >= 4)
                        {
                            if (!UInt32.TryParse(new string(jsonChars, index, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out uint codePoint))
                            {
                                throw new JsonLibException("Invalid Json at index " + index);
                            }
                            result.Append(Char.ConvertFromUtf32((int)codePoint));
                            index += 4;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    result.Append(c);
                }
            }

            if (!stringClosed)
            {
                throw new JsonLibException("Invalid Json.String not closed at index " + index);
            }

            return(result.ToString());
        }
 public void TestConvertFromUtf32()
 {
     Assert.AreEqual("A", Char.ConvertFromUtf32(0x41), "#1");
     Assert.AreEqual("\uD800\uDC00", Char.ConvertFromUtf32(0x10000), "#2");
 }
Beispiel #22
0
 ////////////////////////////////////////////////////////////
 /// <summary>
 /// Construct the text arguments from a text event
 /// </summary>
 /// <param name="e">Text event</param>
 ////////////////////////////////////////////////////////////
 public TextEventArgs(TextEvent e)
 {
     Unicode = Char.ConvertFromUtf32((int)e.Unicode);
 }
 public void TestConvertFromUtf32Fail2()
 {
     Char.ConvertFromUtf32(0x110001);
 }
Beispiel #24
0
 public static String ConvertFromUtf32(this Int32 utf32)
 {
     return(Char.ConvertFromUtf32(utf32));
 }
Beispiel #25
0
        // Perform lexical analysis using a classical deterministic finite state machine.
        private void Lex(TextReader reader)
        {
            var tempbuf   = "";
            var token     = new StringBuilder();
            var state     = 0;
            var c         = reader.Read();
            var lastc     = -1;
            var line      = 1;
            var column    = 0;
            var updatepos = true;

            while (c != -1)
            {
                var ch = (char)c;
                if (updatepos)
                {
                    if (ch == '\n')
                    {
                        line++;
                        column = 0;
                    }
                    else
                    {
                        column++;
                    }
                }
                switch (state)
                {
                case 0:     // Eat whitespace
                    if (ch == '#')
                    {
                        state = 9;
                    }
                    else if ("+-*%".IndexOf(ch) != -1)
                    {
                        token.Append(ch);
                        state = 10;
                    }
                    else if ("()[],;".IndexOf(ch) != -1)
                    {
                        tokens.Add(new Token(ch.ToString(), TokenType.Interpunction, new SourcePos(filename, line, column)));
                    }
                    else if (ch == '/')
                    {
                        state = 5;
                    }
                    else if ("<>=!".IndexOf(ch) != -1)
                    {
                        token.Append(ch);
                        state = 2;
                    }
                    else if (ch == '"')
                    {
                        state = 3;
                    }
                    else if (ch == '\'')
                    {
                        state = 4;
                    }
                    else if (ch == '0')
                    {
                        state = 70;
                    }
                    else if ("123456789".IndexOf(ch) != -1)
                    {
                        token.Append(ch);
                        state = 7;
                    }
                    else if (" \t\r\n".IndexOf(ch) == -1)
                    {
                        token.Append(ch);
                        state = 1;
                    }

                    break;

                case 1:     // normal token
                    if ("()+-*/%[]<>=,;!\"' \t\r\n#".IndexOf(ch) != -1)
                    {
                        if (token.Length > 0)
                        {
                            var t = token.ToString();
                            if (t == "TRUE")
                            {
                                tokens.Add(new Token("TRUE", TokenType.Boolean, new SourcePos(filename, line, column - "TRUE".Length)));
                            }
                            else if (t == "FALSE")
                            {
                                tokens.Add(new Token("FALSE", TokenType.Boolean, new SourcePos(filename, line, column - "FALSE".Length)));
                            }
                            else if (Keywords.IsKeyword(t))
                            {
                                tokens.Add(new Token(t, TokenType.Keyword, new SourcePos(filename, line, column - t.Length)));
                            }
                            else
                            {
                                tokens.Add(new Token(t, TokenType.Identifier, new SourcePos(filename, line, column - t.Length)));
                            }
                            token = new StringBuilder();
                        }

                        lastc = c;
                        state = 0;
                    }
                    else
                    {
                        token.Append(ch);
                        if (token.ToString() == "...")
                        {
                            tokens.Add(new Token(token.ToString(), TokenType.Interpunction, new SourcePos(filename, line, column - token.Length)));
                            token = new StringBuilder();
                            state = 0;
                        }
                    }

                    break;

                case 2:     // <>, <=, >=, ==, <<, >>, <<<, >>>, =>
                    if (ch == '=')
                    {
                        token.Append(ch);
                        tokens.Add(new Token(token.ToString(), TokenType.Operator, new SourcePos(filename, line, column - token.Length + 1)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (ch == '>' && token.ToString() == "=")
                    {
                        token.Append(ch);
                        tokens.Add(new Token(token.ToString(), TokenType.Interpunction, new SourcePos(filename, line, column - token.Length + 1)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (ch == '>' && token.ToString() == "<")
                    {
                        tokens.Add(new Token("<>", TokenType.Operator, new SourcePos(filename, line, column - 1)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (ch == '*' && token.ToString() == "<")
                    {
                        tokens.Add(new Token("<*", TokenType.Interpunction, new SourcePos(filename, line, column - 1)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (ch == '>' && token.ToString() == "!")
                    {
                        tokens.Add(new Token("!>", TokenType.Operator, new SourcePos(filename, line, column - 1)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (ch == '<' && token.ToString() == "<")
                    {
                        token.Append(ch);
                        state = 21;
                    }
                    else if (ch == '>' && token.ToString() == ">")
                    {
                        token.Append(ch);
                        state = 21;
                    }
                    else
                    {
                        tokens.Add(new Token(token.ToString(), TokenType.Operator, new SourcePos(filename, line, column - token.Length)));
                        token = new StringBuilder();
                        lastc = c;
                        state = 0;
                    }

                    break;

                case 21:     // <<, >>, <<<, >>>
                    if (ch == '<' && token.ToString() == "<<")
                    {
                        tokens.Add(new Token("<<<", TokenType.Interpunction, new SourcePos(filename, line, column - 3)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (ch == '>' && token.ToString() == ">>")
                    {
                        tokens.Add(new Token(">>>", TokenType.Interpunction, new SourcePos(filename, line, column - 3)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else
                    {
                        tokens.Add(new Token(token.ToString(), TokenType.Interpunction, new SourcePos(filename, line, column - token.Length)));
                        token = new StringBuilder();
                        lastc = c;
                        state = 0;
                    }

                    break;

                case 3:     // double quotes
                    if (ch == '"')
                    {
                        tokens.Add(new Token(token.ToString(), TokenType.String, new SourcePos(filename, line, column - token.Length - 2 + 1)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (ch == '\\')
                    {
                        state = 31;
                    }
                    else
                    {
                        token.Append(ch);
                    }

                    break;

                case 31:     // double quotes escapes
                    if (ch == 'n')
                    {
                        token.Append("\n");
                        state = 3;
                    }
                    else if (ch == 'r')
                    {
                        token.Append('\r');
                        state = 3;
                    }
                    else if (ch == 't')
                    {
                        token.Append('\t');
                        state = 3;
                    }
                    else if (ch == 'x')
                    {
                        state = 311;
                    }
                    else
                    {
                        token.Append(ch);
                        state = 3;
                    }

                    break;

                case 311:
                    tempbuf = ch.ToString();
                    state   = 312;

                    break;

                case 312:
                    tempbuf += ch.ToString();
                    token.Append(Char.ConvertFromUtf32(Convert.ToInt32(tempbuf, 16)));
                    state = 3;

                    break;

                case 4:     // single quote
                    if (ch == '\'')
                    {
                        tokens.Add(new Token(token.ToString(), TokenType.String, new SourcePos(filename, line, column - token.Length - 2 + 1)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (ch == '\\')
                    {
                        state = 41;
                    }
                    else
                    {
                        token.Append(ch);
                    }

                    break;

                case 41:     // single quotes escapes
                    if (ch == 'n')
                    {
                        token.Append("\n");
                        state = 4;
                    }
                    else if (ch == 'r')
                    {
                        token.Append('\r');
                        state = 4;
                    }
                    else if (ch == 't')
                    {
                        token.Append('\t');
                        state = 4;
                    }
                    else if (ch == 'x')
                    {
                        state = 411;
                    }
                    else
                    {
                        token.Append(ch);
                        state = 4;
                    }

                    break;

                case 411:
                    tempbuf = ch.ToString();
                    state   = 412;

                    break;

                case 412:
                    tempbuf += ch.ToString();
                    token.Append(Char.ConvertFromUtf32(Convert.ToInt32(tempbuf, 16)));
                    state = 4;

                    break;

                case 5:     // check for pattern
                    if (ch == '/')
                    {
                        token.Append("//");
                        state = 6;
                    }
                    else if (ch == '=')
                    {
                        tokens.Add(new Token("/=", TokenType.Operator, new SourcePos(filename, line, column - 1)));
                        state = 0;
                    }
                    else
                    {
                        tokens.Add(new Token("/", TokenType.Operator, new SourcePos(filename, line, column - 1)));
                        lastc = c;
                        state = 0;
                    }

                    break;

                case 6:     // pattern
                    token.Append(ch);
                    if (token.ToString().EndsWith("//"))
                    {
                        tokens.Add(new Token(token.ToString(), TokenType.Pattern, new SourcePos(filename, line, column - token.Length - 4 + 1)));
                        token = new StringBuilder();
                        state = 0;
                    }

                    break;

                case 7:     // int or decimal
                    if (ch == '.')
                    {
                        token.Append(ch);
                        state = 8;
                    }
                    else if ("0123456789_".IndexOf(ch) != -1)
                    {
                        token.Append(ch);
                    }
                    else if ("()[]<>=! \t\n\r+-*/%,;#".IndexOf(ch) != -1)
                    {
                        tokens.Add(new Token(token.ToString().Replace("_", ""), TokenType.Int, new SourcePos(filename, line, column - token.Length)));
                        token = new StringBuilder();
                        lastc = c;
                        state = 0;
                    }
                    else
                    {
                        token.Append(ch);
                        state = 1;
                    }

                    break;

                case 70:     // int, decimal or hex/binary int literal
                    if (ch == 'x')
                    {
                        state = 71;     // hex int literal
                    }
                    else if (ch == 'b')
                    {
                        state = 72;     // binary int literal
                    }
                    else
                    {
                        token.Append('0');
                        lastc = c;
                        state = 7;
                    }

                    break;

                case 71:     // hex int literal
                    if ("0123456789abcdefABCDEF_".IndexOf(ch) != -1)
                    {
                        token.Append(ch);
                    }
                    else if ("()[]<>=! \t\n\r+-*/%,;#".IndexOf(ch) != -1)
                    {
                        tokens.Add(new Token(Convert.ToUInt64(token.ToString().Replace("_", ""), 16).ToString(), TokenType.Int, new SourcePos(filename, line, column - token.Length)));
                        token = new StringBuilder();
                        lastc = c;
                        state = 0;
                    }
                    else
                    {
                        token.Append(ch);
                        state = 1;
                    }

                    break;

                case 72:     // binary int literal
                    if ("01_".IndexOf(ch) != -1)
                    {
                        token.Append(ch);
                    }
                    else if ("()[]<>=! \t\n\r+-*/%,;#".IndexOf(ch) != -1)
                    {
                        tokens.Add(new Token(Convert.ToUInt64(token.ToString().Replace("_", ""), 2).ToString(), TokenType.Int, new SourcePos(filename, line, column - token.Length)));
                        token = new StringBuilder();
                        lastc = c;
                        state = 0;
                    }
                    else
                    {
                        token.Append(ch);
                        state = 1;
                    }

                    break;

                case 8:     // decimal
                    if ("0123456789_".IndexOf(ch) != -1)
                    {
                        token.Append(ch);
                    }
                    else if ("()[]<>=! \t\n\r+-*/%,;#".IndexOf(ch) != -1)
                    {
                        tokens.Add(new Token(token.ToString().Replace("_", ""), TokenType.Decimal, new SourcePos(filename, line, column - token.Length)));
                        token = new StringBuilder();
                        lastc = c;
                        state = 0;
                    }
                    else
                    {
                        token.Append(ch);
                        state = 1;
                    }

                    break;

                case 9:     // comment
                    if (ch == '\n')
                    {
                        state = 0;
                    }

                    break;

                case 10:     // potentially composite assign, ->, *>
                    if (ch == '=')
                    {
                        token.Append(ch);
                        tokens.Add(new Token(token.ToString(), TokenType.Operator,
                                             new SourcePos(filename, line, column)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (token.ToString() == "-" && ch == '>')
                    {
                        tokens.Add(new Token("->", TokenType.Operator, new SourcePos(filename, line, column)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else if (token.ToString() == "*" && ch == '>')
                    {
                        tokens.Add(new Token("*>", TokenType.Interpunction, new SourcePos(filename, line, column)));
                        token = new StringBuilder();
                        state = 0;
                    }
                    else
                    {
                        tokens.Add(new Token(token.ToString(), TokenType.Operator, new SourcePos(filename, line, column)));
                        token = new StringBuilder();
                        lastc = c;
                        state = 0;
                    }

                    break;
                }

                if (lastc != -1)
                {
                    c         = lastc;
                    lastc     = -1;
                    updatepos = false;
                }
                else
                {
                    c         = reader.Read();
                    updatepos = true;
                }
            }

            if (token.Length > 0)
            {
                var t = token.ToString();
                switch (state)
                {
                case 1:
                    if (t == "TRUE")
                    {
                        tokens.Add(new Token("TRUE", TokenType.Boolean, new SourcePos(filename, line, column - "TRUE".Length)));
                    }
                    else if (t == "FALSE")
                    {
                        tokens.Add(new Token("FALSE", TokenType.Boolean, new SourcePos(filename, line, column - "FALSE".Length)));
                    }
                    else if (Keywords.IsKeyword(t))
                    {
                        tokens.Add(new Token(t, TokenType.Keyword, new SourcePos(filename, line, column - t.Length)));
                    }
                    else if (Operators.IsOperator(t))
                    {
                        tokens.Add(new Token(t, TokenType.Operator, new SourcePos(filename, line, column - t.Length)));
                    }
                    else
                    {
                        tokens.Add(new Token(t, TokenType.Identifier, new SourcePos(filename, line, column - t.Length)));
                    }
                    break;

                case 21:
                    tokens.Add(new Token(t, TokenType.Interpunction, new SourcePos(filename, line, column - t.Length)));
                    break;

                case 3:
                case 4:
                    tokens.Add(new Token(t, TokenType.String, new SourcePos(filename, line, column - t.Length - 2)));
                    break;

                case 7:
                    tokens.Add(new Token(t.Replace("_", ""), TokenType.Int, new SourcePos(filename, line, column - t.Length)));
                    break;

                case 71:
                    tokens.Add(new Token(Convert.ToUInt64(t.Replace("_", ""), 16).ToString(), TokenType.Int, new SourcePos(filename, line, column - t.Length)));
                    break;

                case 72:
                    tokens.Add(new Token(Convert.ToUInt64(t.Replace("_", ""), 2).ToString(), TokenType.Int, new SourcePos(filename, line, column - t.Length)));
                    break;

                case 8:
                    tokens.Add(new Token(t.Replace("_", ""), TokenType.Decimal, new SourcePos(filename, line, column - t.Length)));
                    break;

                case 9:     // ignore comment
                    break;

                default:
                    if (Keywords.IsKeyword(t))
                    {
                        tokens.Add(new Token(t, TokenType.Keyword, new SourcePos(filename, line, column - t.Length)));
                    }
                    else if (Operators.IsOperator(t))
                    {
                        tokens.Add(new Token(t, TokenType.Operator, new SourcePos(filename, line, column - t.Length)));
                    }
                    else
                    {
                        tokens.Add(new Token(t, TokenType.Identifier, new SourcePos(filename, line, column - t.Length)));
                    }
                    break;
                }
            }
            else if (state == 70)
            {
                tokens.Add(new Token("0", TokenType.Int, new SourcePos(filename, line, column - 1)));
            }
        }
Beispiel #26
0
            /// <summary>
            /// Reads a JSON string
            /// </summary>
            /// <param name="expectedType"></param>
            /// <returns>string or value which is represented as a string in JSON</returns>
            private object ReadString(Type expectedType)
            {
                if (this.Source[this.index] != JsonReader.OperatorStringDelim &&
                    this.Source[this.index] != JsonReader.OperatorStringDelimAlt)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorExpectedString, this.index);
                }

                char startStringDelim = this.Source[this.index];

                // consume opening quote
                this.index++;
                if (this.index >= this.SourceLength)
                {
                    throw new JsonDeserializationException(JsonReader.ErrorUnterminatedString, this.index);
                }

                int           start   = this.index;
                StringBuilder builder = new StringBuilder();

                while (this.Source[this.index] != startStringDelim)
                {
                    if (this.Source[this.index] == JsonReader.OperatorCharEscape)
                    {
                        // copy chunk before decoding
                        builder.Append(this.Source, start, this.index - start);

                        // consume escape char
                        this.index++;
                        if (this.index >= this.SourceLength)
                        {
                            throw new JsonDeserializationException(JsonReader.ErrorUnterminatedString, this.index);
                        }

                        // decode
                        switch (this.Source[this.index])
                        {
                        case '0':
                        {
                            // don't allow NULL char '\0'
                            // causes CStrings to terminate
                            break;
                        }

                        case 'b':
                        {
                            // backspace
                            builder.Append('\b');
                            break;
                        }

                        case 'f':
                        {
                            // formfeed
                            builder.Append('\f');
                            break;
                        }

                        case 'n':
                        {
                            // newline
                            builder.Append('\n');
                            break;
                        }

                        case 'r':
                        {
                            // carriage return
                            builder.Append('\r');
                            break;
                        }

                        case 't':
                        {
                            // tab
                            builder.Append('\t');
                            break;
                        }

                        case 'u':
                        {
                            // Unicode escape sequence
                            // e.g. Copyright: "\u00A9"
                            try
                            {
                                // unicode ordinal
                                int utf16;
                                if (this.index + 4 < this.SourceLength &&
                                    Int32.TryParse(
                                        this.Source.Substring(this.index + 1, 4),
                                        NumberStyles.AllowHexSpecifier,
                                        NumberFormatInfo.InvariantInfo,
                                        out utf16))
                                {
                                    builder.Append(Char.ConvertFromUtf32(utf16));
                                    this.index += 4;
                                }
                                else
                                {
                                    // using FireFox style recovery, if not a valid hex
                                    // escape sequence then treat as single escaped 'u'
                                    // followed by rest of string
                                    builder.Append(this.Source[this.index]);
                                }
                            }
                            //Adding emoji support on reading.
                            catch (ArgumentOutOfRangeException)
                            {
                                String cleanHex = Source.Substring(index - 1, 12).Replace("\\u", "");

                                var stringBuilder = new StringBuilder();
                                for (int i = 0; i < cleanHex.Length; i += 4)
                                {
                                    string temp      = cleanHex.Substring(i, 4);
                                    char   character = (char)Convert.ToInt16(temp, 16);
                                    stringBuilder.Append(character);
                                }

                                builder.Append(stringBuilder);

                                index += 10;
                            }

                            break;
                        }

                        default:
                        {
                            builder.Append(this.Source[this.index]);
                            break;
                        }
                        }

                        this.index++;
                        if (this.index >= this.SourceLength)
                        {
                            throw new JsonDeserializationException(JsonReader.ErrorUnterminatedString, this.index);
                        }

                        start = this.index;
                    }
                    else
                    {
                        // next char
                        this.index++;
                        if (this.index >= this.SourceLength)
                        {
                            throw new JsonDeserializationException(JsonReader.ErrorUnterminatedString, this.index);
                        }
                    }
                }

                // copy rest of string
                builder.Append(this.Source, start, this.index - start);

                // consume closing quote
                this.index++;

                if (expectedType != null && expectedType != typeof(String))
                {
                    return(this.Settings.Coercion.CoerceType(expectedType, builder.ToString()));
                }

                return(builder.ToString());
            }
Beispiel #27
0
        public void TestStringLarge()
        {
            var    sw     = Stopwatch.StartNew();
            var    avg    = 0.0;
            Random random = new Random();
            var    sb     = new StringBuilder(1000 * 1000 * 200);

            // large size string
            for (int i = 0; i < 10; i++)
            {
                sb.Length = 0;
                int len = ( int )random.Next() % 100 + (1 << 24);
                for (int j = 0; j < len; j++)
                {
                    sb.Append('a' + (( int )random.Next()) & 26);
                }
                avg = (avg + sb.Length) / 2.0;
                TestString(sb.ToString());
            }
            sw.Stop();
            Console.WriteLine("Large String ({1:#,###.0} chars): {0:0.###} msec/object", sw.ElapsedMilliseconds / 10.0, avg);
            sw.Reset();

            GC.Collect();

            sw.Start();

            avg = 0.0;

            // large size string
            for (int i = 0; i < 10; i++)
            {
                sb.Length = 0;
                int len = ( int )random.Next() % 100 + (1 << 24);
                for (int j = 0; j < len; j++)
                {
                    sb.Append('a' + (( int )random.Next()) & 26);
                }
                avg = (avg + sb.Length) / 2.0;
                TestString(sb.ToString(), Encoding.Unicode);
            }
            sw.Stop();
            Console.WriteLine("Large String (UTF-16LE) ({1:#,###.0}): {0:0.###} msec/object", sw.ElapsedMilliseconds / 10.0, avg);
            sw.Reset();

            GC.Collect();

            sw.Start();

            // Non-ASCII
            avg = 0.0;
            // medium size string
            for (int i = 0; i < 100; i++)
            {
                sb.Length = 0;
                int len = ( int )random.Next() % 100 + (1 << 8);
                for (int j = 0; j < len; j++)
                {
                    var cp = random.Next(0x10ffff);
                    if (0xd800 <= cp && cp <= 0xdfff)
                    {
                        cp /= 2;
                    }

                    sb.Append(Char.ConvertFromUtf32(cp));
                }
                avg = (avg + sb.Length) / 2.0;
                TestString(sb.ToString());
            }
            sw.Stop();
            Console.WriteLine("Medium String (Non-ASCII) ({1:#.0}): {0:0.###} msec/object", sw.ElapsedMilliseconds / 100.0, avg);
        }
Beispiel #28
0
    public static void DisplayRange(uint start, uint end)
    {
        const uint upperRange     = 0x10FFFF;
        const uint surrogateStart = 0xD800;
        const uint surrogateEnd   = 0xDFFF;

        if (end <= start)
        {
            uint t = start;
            start = end;
            end   = t;
        }

        // Check whether the start or end range is outside of last plane.
        if (start > upperRange)
        {
            throw new ArgumentException(String.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{1:X5})",
                                                      start, upperRange));
        }
        if (end > upperRange)
        {
            throw new ArgumentException(String.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{0:X5})",
                                                      end, upperRange));
        }

        // Since we're using 21-bit code points, we can't use U+D800 to U+DFFF.
        if ((start <surrogateStart& end> surrogateStart) || (start >= surrogateStart & start <= surrogateEnd))
        {
            throw new ArgumentException(String.Format("0x{0:X5}-0x{1:X5} includes the surrogate pair range 0x{2:X5}-0x{3:X5}",
                                                      start, end, surrogateStart, surrogateEnd));
        }
        uint last  = RoundUpToMultipleOf(0x10, end);
        uint first = RoundDownToMultipleOf(0x10, start);

        uint rows = (last - first) / 0x10;

        for (uint r = 0; r < rows; ++r)
        {
            // Display the row header.
            Console.Write("{0:x5} ", first + 0x10 * r);

            for (uint c = 0; c < 0x10; ++c)
            {
                uint cur = (first + 0x10 * r + c);
                if (cur < start)
                {
                    Console.Write(" {0} ", Convert.ToChar(0x20));
                }
                else if (end < cur)
                {
                    Console.Write(" {0} ", Convert.ToChar(0x20));
                }
                else
                {
                    // the cast to int is safe, since we know that val <= upperRange.
                    String chars = Char.ConvertFromUtf32((int)cur);
                    // Display a space for code points that are not valid characters.
                    if (CharUnicodeInfo.GetUnicodeCategory(chars[0]) ==
                        UnicodeCategory.OtherNotAssigned)
                    {
                        Console.Write(" {0} ", Convert.ToChar(0x20));
                    }
                    // Display a space for code points in the private use area.
                    else if (CharUnicodeInfo.GetUnicodeCategory(chars[0]) ==
                             UnicodeCategory.PrivateUse)
                    {
                        Console.Write(" {0} ", Convert.ToChar(0x20));
                    }
                    // Is surrogate pair a valid character?
                    // Note that the console will interpret the high and low surrogate
                    // as separate (and unrecognizable) characters.
                    else if (chars.Length > 1 && CharUnicodeInfo.GetUnicodeCategory(chars, 0) ==
                             UnicodeCategory.OtherNotAssigned)
                    {
                        Console.Write(" {0} ", Convert.ToChar(0x20));
                    }
                    else
                    {
                        Console.Write(" {0} ", chars);
                    }
                }

                switch (c)
                {
                case 3:
                case 11:
                    Console.Write("-");
                    break;

                case 7:
                    Console.Write("--");
                    break;
                }
            }

            Console.WriteLine();
            if (0 < r && r % 0x10 == 0)
            {
                Console.WriteLine();
            }
        }
    }
Beispiel #29
0
        protected string ParseString(char[] json, ref int index)
        {
            string s = "";
            char   c;

            EatWhitespace(json, ref index);

            // "
            c = json[index++];

            bool complete = false;

            while (!complete)
            {
                if (index == json.Length)
                {
                    break;
                }

                c = json[index++];
                if (c == '"')
                {
                    complete = true;
                    break;
                }
                else if (c == '\\')
                {
                    if (index == json.Length)
                    {
                        break;
                    }
                    c = json[index++];
                    if (c == '"')
                    {
                        s += '"';
                    }
                    else if (c == '\\')
                    {
                        s += '\\';
                    }
                    else if (c == '/')
                    {
                        s += '/';
                    }
                    else if (c == 'b')
                    {
                        s += '\b';
                    }
                    else if (c == 'f')
                    {
                        s += '\f';
                    }
                    else if (c == 'n')
                    {
                        s += '\n';
                    }
                    else if (c == 'r')
                    {
                        s += '\r';
                    }
                    else if (c == 't')
                    {
                        s += '\t';
                    }
                    else if (c == 'u')
                    {
                        int remainingLength = json.Length - index;
                        if (remainingLength >= 4)
                        {
                            // fetch the next 4 chars
                            char[] unicodeCharArray = new char[4];
                            Array.Copy(json, index, unicodeCharArray, 0, 4);
                            // parse the 32 bit hex into an integer codepoint
                            uint codePoint = UInt32.Parse(new string(unicodeCharArray), NumberStyles.HexNumber);
                            // convert the integer codepoint to a unicode char and add to string
                            s += Char.ConvertFromUtf32((int)codePoint);
                            // skip 4 chars
                            index += 4;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    s += c;
                }
            }

            if (!complete)
            {
                return(null);
            }

            return(s);
        }
Beispiel #30
0
        public void Export()
        {
            var workbook = new XLWorkbook();
            var ws       = workbook.Worksheets.Add("PO Details");

            ws.Cell("A1").Value = "PO Number";
            ws.Cell("A1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("A1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("A1:A2").Merge();

            ws.Cell("B1").Value = "Manufacturer";
            ws.Cell("B1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("B1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("B1:B2").Merge();

            ws.Cell("C1").Value = "Season code";
            ws.Cell("C1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("C1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("C1:C2").Merge();

            ws.Cell("D1").Value = "Material";
            ws.Cell("D1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("D1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("D1:D2").Merge();

            ws.Cell("E1").Value = "Material Description";
            ws.Cell("E1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("E1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("E1:E2").Merge();

            ws.Cell("F1").Value = "Plan Ex-fty";
            ws.Cell("F1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("F1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            //ws.Cell("F1").Style.NumberFormat.Format = "dd-mmm-yy";
            ws.Column(6).Style.NumberFormat.Format = "dd-mmm-yy";
            ws.Range("F1:F2").Merge();

            ws.Cell("G1").Value = "Original Ex-fty";
            ws.Cell("G1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("G1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("G1:G2").Merge();

            ws.Cell("H1").Value = "Total PO qty";
            ws.Cell("H1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("H1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("H1:H2").Merge();

            ws.Cell("I1").Value = "PO unit price";
            ws.Cell("I1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("I1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("I1:I2").Merge();

            ws.Cell("J1").Value = "Delivery Address";
            ws.Cell("J1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("J1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("J1:J2").Merge();

            ws.Cell("K1").Value = "Trans Mode";
            ws.Cell("K1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("K1").Style.Alignment.Vertical   = XLAlignmentVerticalValues.Center;
            ws.Range("K1:K2").Merge();

            ws.Cell("L1").Value = "Size";

            Dictionary <string, int> sizeMapping = new Dictionary <string, int>();
            int sizeKeyIndex = 76;

            var row = 3;

            foreach (var key in ExtractedLines.Keys)
            {
                var line = ExtractedLines[key];

                if (!line.HasErrors)
                {
                    string cellName = "A" + row;
                    ws.Cell(cellName).Value = line.PONumber;

                    cellName = "B" + row;
                    ws.Cell(cellName).Value = line.Manufacturer;

                    cellName = "C" + row;
                    ws.Cell(cellName).Value = line.SeasonCode;

                    cellName = "D" + row;
                    ws.Cell(cellName).Value = line.Material;

                    cellName = "E" + row;
                    ws.Cell(cellName).Value = line.MaterialDescription;

                    cellName = "F" + row;
                    ws.Cell(cellName).Value = line.Plan_ExFTY;

                    cellName = "G" + row;
                    ws.Cell(cellName).Value = line.Original_ExFTY;

                    cellName = "H" + row;
                    ws.Cell(cellName).Value = line.TotalPOQty;

                    cellName = "I" + row;
                    ws.Cell(cellName).Value = line.POUnitPrice;

                    cellName = "J" + row;
                    ws.Cell(cellName).Value = line.DeliveryAddress;

                    cellName = "K" + row;
                    ws.Cell(cellName).Value = line.TransMode;

                    foreach (var sizeKey in line.Sizes.Keys)
                    {
                        int sizeColumnNo = 0;
                        if (sizeMapping.ContainsKey(sizeKey))
                        {
                            sizeColumnNo = sizeMapping[sizeKey];
                        }
                        else
                        {
                            sizeMapping.Add(sizeKey, sizeKeyIndex);

                            string newSizeColumnName = Char.ConvertFromUtf32(sizeKeyIndex) + "2";
                            ws.Cell(newSizeColumnName).Value = sizeKey;
                            ws.Cell(newSizeColumnName).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;

                            sizeColumnNo = sizeKeyIndex;
                            sizeKeyIndex++;
                        }

                        var    sizeValue           = line.Sizes[sizeKey];
                        string sizeValueColumnName = Char.ConvertFromUtf32(sizeColumnNo) + row;
                        ws.Cell(sizeValueColumnName).Value = sizeValue;
                    }

                    row++;
                }
            }

            string sizeRangeColumn = "L1:" + Char.ConvertFromUtf32(sizeKeyIndex - 1) + "1";

            ws.Cell("L1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range(sizeRangeColumn).Merge();

            //Adjust column widths to their content
            ws.Columns().AdjustToContents();

            ws.RangeUsed().Style.Border.OutsideBorder = XLBorderStyleValues.Thick;

            string headerRange = "A1:" + Char.ConvertFromUtf32(sizeKeyIndex - 1) + "2";
            var    rngTable    = ws.Range(headerRange);
            var    rngHeaders  = rngTable.Range(headerRange); // The address is relative to rngTable (NOT the worksheet)

            rngHeaders.Style.Font.Bold = true;

            string destination = string.Format(@"{0}\{1}", this.Destination, "PODetails_" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_tt")) + ".xlsx";;

            workbook.SaveAs(destination);
        }