Example #1
0
        public void Append_Constructor(string data)
        {
            var sb = new MyStringBuilder(data);

            Assert.AreEqual(data.Length, sb.Count);
            Assert.AreEqual(data, sb.ToString());
            Assert.AreEqual(data.Length, sb.ToString().Length);
        }
Example #2
0
        public void Clear(string data)
        {
            var sb = new MyStringBuilder();

            sb.Append(data);
            sb.Clear();

            Assert.AreEqual(0, sb.Count);
            Assert.AreEqual(string.Empty, sb.ToString());
            Assert.AreEqual(0, sb.ToString().Length);
        }
Example #3
0
        public void AppendLine(string data)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine(data);

            var newLineLength = Environment.NewLine.Length;

            Assert.AreEqual(data.Length + newLineLength, sb.Count);
            Assert.AreEqual(data + Environment.NewLine, sb.ToString());
            Assert.AreEqual(data.Length + newLineLength, sb.ToString().Length);
        }
        public void PlusOperator_AddTwoStrings_ReturnsConcatenatedResult()
        {
            MyStringBuilder msb = "hello";

            msb += " world";
            Assert.That(msb.ToString(), Is.EqualTo("hello world"));
        }
        public void V2_MyStringBuilder_ToString(string data)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine(data);
            var result = sb.ToString();
        }
Example #6
0
        private static bool asmstuff(string code, out string result)
        {
            if (File.Exists(mydir + "/f.s"))
            {
                File.Delete(mydir + "/f.s");
            }
            if (File.Exists(mydir + "/f.o"))
            {
                File.Delete(mydir + "/f.o");
            }
            StreamWriter file = new StreamWriter(mydir + "/f.s");

            file.WriteLine(".intel_syntax noprefix\n_main:\n" + code);
            file.Close();
            var  o   = new MyStringBuilder();
            bool res = false;

            if (exec(o, gcc, "-m32 -c f.s -o f.o"))
            {
                o.Clear();
                res = exec(o, objdump, "-z -M intel -d f.o");
            }
            result = o.ToString();
            if (File.Exists(mydir + "/f.s"))
            {
                File.Delete(mydir + "/f.s");
            }
            if (File.Exists(mydir + "/f.o"))
            {
                File.Delete(mydir + "/f.o");
            }
            return(res);
        }
Example #7
0
        private static string stripcomments(string[] lines)
        {
            check_base_accesses = 0;
            check_data_accesses = 0;
            var sb           = new MyStringBuilder();
            var replacements = new Dictionary <string, string>();

            replacements.Add("__BASEADDR", "0xEEEFEEFF");
            var varRegex = new Regex("_var(..)");

            foreach (var line in lines)
            {
                if (line.Trim().Length == 0)
                {
                    continue;
                }

                string l = line;

                int i = l.IndexOf(';');
                if (i != -1)
                {
                    if (i < l.Length - 1)
                    {
                        string comment = l.Substring(i + 1).Trim();
                        if (comment.StartsWith("_DEFINE:"))
                        {
                            string[] parts = comment.Substring("_DEFINE:".Length).Split(new char[] { '=' }, 2);
                            if (parts[0].Length != 0)
                            {
                                string value = parts[1];
                                int    idx   = value.IndexOf(';');
                                if (idx > -1)
                                {
                                    value = value.Substring(0, idx);
                                }
                                value = preproc(value, replacements);
                                if (replacements.ContainsKey(parts[0]))
                                {
                                    replacements.Remove(parts[0]);
                                }
                                replacements.Add(parts[0], value);
                            }
                        }
                    }
                    l = l.Substring(0, i).Trim();
                    if (l.Length == 0)
                    {
                        continue;
                    }
                }

                l = preproc(l, replacements);

                check_data_accesses += varRegex.Matches(l).Count;
                l = varRegex.Replace(l, "0xEE${1}FFEE");
                sb.Append(l).AppendLine();
            }
            return(sb.ToString());
        }
Example #8
0
    public static string valueDestroyBuilder(MyStringBuilder builder, bool destroyReally = false)
    {
        string str = builder.ToString();

        mStringBuilderPool?.destroyBuilder(builder, destroyReally);
        return(str);
    }
        private static OrderReport GenerateReportForOrder(
            Order order,
            OrderReportFormattingSettings orderReportFormattingSettings)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("Order date: " + FormatDate(order.OrderDate));

            if (!orderReportFormattingSettings.DontIncludeNumberOrOrderLines)
            {
                sb.AppendLine("Number of order lines: " + order.OrderLines.Length.AsString());
            }

            decimal total = 0m;

            foreach (var orderLine in order.OrderLines)
            {
                var lineTotal = orderLine.ItemCount * orderLine.ItemPrice;

                total += lineTotal;

                sb.AppendLine(orderLine.ProductName + ": " + orderLine.ItemCount.AsString() + " * " + orderLine.ItemPrice.AsString() + "$ = " + lineTotal.AsString() + "$");
            }

            sb.AppendLine("Total: " + total.AsString() + "$");

            return(new OrderReport(sb.ToString()));
        }
Example #10
0
        public static void AdapterDecoratorExample()
        {
            MyStringBuilder msb = "Hello ";

            msb += "World";

            WriteLine(msb.ToString());
        }
Example #11
0
        private static string b2h(byte[] b, int offset, int len)
        {
            var sb = new MyStringBuilder();

            for (int i = offset, j = offset + len; i < j; i++)
            {
                sb.Append(b[i].ToString("X2"));
            }
            return(sb.ToString());
        }
Example #12
0
        public string Invoke(string[] args)
        {
            MyStringBuilder s = args[0];

            for (int i = 1; i < args.Length; i++)
            {
                s += args[i]; //when implicit conversion created from MyStringBuilder to string, + operator works like append in strings
            }
            return(s.ToString());
        }
Example #13
0
        public void Should_Get_SetIndexer()
        {
            //arrange
            string          str     = "hello";
            MyStringBuilder builder = new MyStringBuilder(str);

            //act
            builder[0] = 'H';


            //assert
            builder.ToString()[0].ShouldBeEquivalentTo('H');
        }
Example #14
0
        public void Should_Create_My_String_Builder_With_String()
        {
            //arrange
            string str = "hello";

            //act
            MyStringBuilder builder = new MyStringBuilder(str);

            //assert
            builder.Length.ShouldBeEquivalentTo(str.Length);
            builder.Capacity.ShouldBeEquivalentTo(str.Length);
            builder.ToString().ShouldBeEquivalentTo(str);
        }
Example #15
0
        private static Report GenerateReport(ImmutableArray <City> cities)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("Number of cities: " + cities.Length.AsString());
            sb.AppendLine();

            foreach (var subReport in cities.Select(GenerateReportForCity))
            {
                sb.AppendLine(subReport.Value);
            }

            return(new Report(sb.ToString()));
        }
Example #16
0
        private static Report GenerateReport(ImmutableArray <City> cities, Func <Customer, ImmutableArray <Order> > getOrdersForCustomer)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("Number of cities: " + cities.Length.AsString());
            sb.AppendLine();

            foreach (var subReport in cities.Select(x => GenerateReportForCity(x, getOrdersForCustomer)))
            {
                sb.AppendLine(subReport.Value);
            }

            return(new Report(sb.ToString()));
        }
Example #17
0
        public void Should_Check_Clear()
        {
            //arrange
            string str       = "Hello";
            string resultStr = "";

            //act
            MyStringBuilder builder = new MyStringBuilder(str);

            builder.Clear();
            var result = builder.ToString();

            //assert
            result.ShouldBeEquivalentTo(resultStr);
        }
Example #18
0
        private static CustomerReport GenerateReportForCustomer(Customer customer)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("Customer name: " + customer.Name);

            sb.AppendLine("Number of orders for customer: " + customer.Orders.Length.AsString());

            foreach (var subReport in customer.Orders.Select(GenerateReportForOrder))
            {
                sb.AppendLine(subReport.Value);
            }

            return(new CustomerReport(sb.ToString()));
        }
Example #19
0
        public void Should_Check_Append()
        {
            //arrange
            string str       = "Hello";
            string str2      = "World!";
            string resultStr = "HelloWorld!";

            //act
            MyStringBuilder builder = new MyStringBuilder(str);

            builder.Append(str2);
            var result = builder.ToString();

            //assert
            result.ShouldBeEquivalentTo(resultStr);
        }
        public static Report GenerateReport(
            Func <City, CityReport> generateCityReport,
            ImmutableArray <City> cities)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("Number of cities: " + cities.Length.AsString());
            sb.AppendLine();

            foreach (var subReport in cities.Select(x => generateCityReport(x)))
            {
                sb.AppendLine(subReport.Value);
            }

            return(new Report(sb.ToString()));
        }
Example #21
0
        public void VeryLongStringsCanBeAddedToStringBuilder()
        {
            var stringBuilder = new MyStringBuilder();

            var actualString = string.Empty;

            for (int i = 0; i < 200; i++)
            {
                var str = new string('a', 10000);
                actualString += str;
                stringBuilder.Append(str);

                Assert.AreEqual(actualString.Length, stringBuilder.GetLength());
                Assert.AreEqual(actualString, stringBuilder.ToString());
            }
        }
Example #22
0
        private static CityReport GenerateReportForCity(City city, Func <Customer, ImmutableArray <Order> > getOrdersForCustomer)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("City name: " + city.Name);

            sb.AppendLine("Number of customers in the city: " + city.Customers.Length.AsString());
            sb.AppendLine();

            foreach (var subReport in city.Customers.Select(x => GenerateReportForCustomer(x, getOrdersForCustomer)))
            {
                sb.AppendLine(subReport.Value);
            }

            return(new CityReport(sb.ToString()));
        }
Example #23
0
        public bool CheckSubtree(MyBinarySearchTree <int> biggerTree,
                                 MyBinarySearchTree <int> smallerTree)
        {
            if (biggerTree == null || smallerTree == null)
            {
                throw new ArgumentNullException();
            }

            var biggerBuilder  = new MyStringBuilder();
            var smallerBuilder = new MyStringBuilder();

            GetMarkedPreOrderTraversal(biggerTree.Root, biggerBuilder);
            GetMarkedPreOrderTraversal(smallerTree.Root, smallerBuilder);

            return(biggerBuilder.ToString().Contains(smallerBuilder.ToString()));
        }
Example #24
0
        public CityReport Generate(City city)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("City name: " + city.Name);

            sb.AppendLine("Number of customers in the city: " + city.Customers.Length.AsString());
            sb.AppendLine();

            foreach (var subReport in city.Customers.Select(x => customerReportGenerator.Generate(x)))
            {
                sb.AppendLine(subReport.Value);
            }

            return(new CityReport(sb.ToString()));
        }
Example #25
0
        public void StringsCanBeAddedToStringBuilder()
        {
            var strings       = new string[] { "Some", "string", "long string - in NetCF arguments pretty much don't matter as long as count is 0", "", null };
            var stringBuilder = new MyStringBuilder();

            var actualString = string.Empty;

            for (int i = 0; i < 200; i++)
            {
                actualString += strings[i % strings.Length];
                stringBuilder.Append(strings[i % strings.Length]);

                Assert.AreEqual(actualString.Length, stringBuilder.GetLength());
                Assert.AreEqual(actualString, stringBuilder.ToString());
            }
        }
Example #26
0
        protected override string CreateContent(FormInfo formInfo)
        {
            MyStringBuilder fieldBuilder = new MyStringBuilder(256);

            foreach (TableInfo tableInfo in _tableInfos)
            {
                string columnDescription = tableInfo.ColumnDescription;
                string keyString         = null;
                string typeName          = ChangeToCSharpType(tableInfo.ColumnType);
                string name = tableInfo.ColumnName;
                name = name.Substring(0, 1).ToUpper() + name.Substring(1);
                string isNullable = tableInfo.IsNullable == "1" && typeName != "string" ? "?" : "";
                if (tableInfo.IsIncrement == "1")
                {
                    keyString = "[Key]";
                }
                fieldBuilder.AppendLine();
                fieldBuilder.AppendLine(2, "/// <summary>");
                fieldBuilder.AppendLine(2, $"/// {columnDescription}");
                fieldBuilder.AppendLine(2, "/// </summary>");
                if (string.IsNullOrWhiteSpace(keyString) == false)
                {
                    fieldBuilder.AppendLine(2, keyString);
                }
                fieldBuilder.AppendLine(2, "public " + typeName + isNullable + " " + name + " { " + "get; set;" + " }");
                fieldBuilder.AppendLine();
            }

            string[]        textArray = File.ReadAllLines(GetTemplateFromPath());
            MyStringBuilder sb        = new MyStringBuilder(128);

            foreach (string text in textArray)
            {
                string item = text.Replace(TemplatePlaceholder.NameSpaceName, formInfo.NameSpaceName).Replace(TemplatePlaceholder.TableName, formInfo.TableName);
                if (item.Trim() == TemplatePlaceholder.FieldArea)
                {
                    string temp = item.Replace(item, fieldBuilder.ToString());
                    sb.AppendLine(temp);
                }
                else
                {
                    sb.AppendLine(item);
                }
            }

            return(sb.ToString());
        }
Example #27
0
        private static CustomerReport GenerateReportForCustomer(Customer customer, Func <Customer, ImmutableArray <Order> > getOrdersForCustomer)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("Customer name: " + customer.Name);

            var orders = getOrdersForCustomer(customer);

            sb.AppendLine("Number of orders for customer: " + orders.Length.AsString());

            foreach (var subReport in orders.Select(GenerateReportForOrder))
            {
                sb.AppendLine(subReport.Value);
            }

            return(new CustomerReport(sb.ToString()));
        }
Example #28
0
        public CustomerReport Generate(Customer customer)
        {
            var sb = new MyStringBuilder();

            sb.AppendLine("Customer name: " + customer.Name);

            var orders = getOrdersForCustomer(customer);

            sb.AppendLine("Number of orders for customer: " + orders.Length.AsString());

            foreach (var subReport in orders.Select(order => orderReportGenerator.Generate(order)))
            {
                sb.AppendLine(subReport.Value);
            }

            return(new CustomerReport(sb.ToString()));
        }
Example #29
0
        private static string cleandisasm(string[] lines)
        {
            var sb = new MyStringBuilder();

            string[] instr = new string[14];
            int      instrc;
            int      instrs             = 0;
            int      real_data_accesses = 0;
            int      real_base_accesses = 0;

            DATA.lvars.Clear();
            DATA.cache.Clear();
            SortedDictionary <int, A> stuff           = new SortedDictionary <int, A>();
            List <BASEADDRPATCH>      baseaddrpatches = new List <BASEADDRPATCH>();

            sb.Append(":ENTRY").AppendLine();
            sb.Append("hex").AppendLine();
            int _ = 0;

            if (lines[_].Trim().Replace("\r", "").Length == 0)
            {
                do
                {
                    _++;
                } while (!lines[_ - 1].StartsWith("00000000 <_main>"));
            }
            for (; _ < lines.Length; _++)
            {
                string _line = lines[_];
                try {
                    string line = _line.Trim().Replace("\r", "");
                    if (line.Length == 0)
                    {
                        continue;
                    }
                    if (line.EndsWith(":"))
                    {
                        if (comments)
                        {
                            sb.Append("// ").Append(line.Trim()).AppendLine();
                        }
                        continue;
                    }
                    instrc = 0;
                    int c = 1;
                    while (line[c - 1] != ':')
                    {
                        c++;
                    }
                    while (line[c] == ' ' || line[c] == '\t')
                    {
                        c++;
                    }
                    do
                    {
                        instr[instrc++] = new string(new char[] { line[c], line[c + 1] } );
                        c += 3;
                    } while (line[c] != ' ' && line [c] != '\t');
                    while (line[c] == ' ' || line[c] == '\t')
                    {
                        c++;
                    }
                    string comment = line.Substring(c);

                    // >>>>>>> nextline
                    // try to include next line if necessary
                    // for example:
                    //   test   DWORD PTR [esp+0x11223344],0x44332211
                    // results in:
                    // 0:  f7 84 24 44 33 22 11    test   DWORD PTR [esp+0x11223344],0x44332211
                    // 7:  11 22 33 44

tryparsenextline:
                    if (_ + 1 >= lines.Length)
                    {
                        goto skipnextline;
                    }

                    string nextline = lines[_ + 1].Replace("\r", "").Trim();
                    nextline = new Regex("^[0-9a-fA-F]+:\\s+").Replace(nextline, "");
                    string[] opcodes = nextline.Split(' ');
                    foreach (string oc in opcodes)
                    {
                        if (oc.Length != 2)
                        {
                            goto skipnextline;
                        }
                        try {
                            int.Parse(oc, NumberStyles.HexNumber);
                        } catch (Exception) {
                            goto skipnextline;
                        }
                    }

                    foreach (string oc in opcodes)
                    {
                        instr[instrc++] = new string(new char[] { oc[0], oc[1] } );
                    }

                    _++;
                    goto tryparsenextline;

skipnextline:
                    // <<<<<<< nextline

                    int si = 0;
                    if (instrc == 5 &&
                        (comment.StartsWith("call") || comment.StartsWith("jmp") || comment.StartsWith("je ") || comment.StartsWith("jne ")) &&
                        instraddr(instr) > 0x40000)
                    {
                        si = 1;
                        sb.Append(instr[0]).AppendLine();
                        instrs++;
                        stuff.Add(instrs, new JMPCALL());
                        int idx = comment.IndexOf(' ') + 1;
                        comment = comment.Substring(0, idx) + "0x" + realaddr(instr);
                        if (correctoffsets)
                        {
                            patchinstr(instr, 2 * instrs + 4);
                        }
                        else
                        {
                            patchinstr(instr, instrs);
                        }
                    }
                    for (int i = 1; i <= instrc - 4; i++)
                    {
                        int       type      = 0;
                        const int TYPE_DATA = 1;
                        const int TYPE_BASE = 2;
                        if (instr[i] == "ee" && instr[i + 1] == "ff" && instr[i + 3] == "ee")
                        {
                            type = TYPE_DATA;
                        }
                        else if (instr[i] == "ff" && instr[i + 1] == "ee" && instr[i + 2] == "ef" && instr[i + 3] == "ee")
                        {
                            type = TYPE_BASE;
                        }
                        else
                        {
                            continue;
                        }
                        for (; si < i; si++)
                        {
                            sb.Append(instr[si]).Append(' ');
                            instrs++;
                        }
                        switch (type)
                        {
                        case TYPE_DATA:
                            //stuff.Add(instrs, new DATA(instr[i + 2], (int.Parse(instr[i + 1], NumberStyles.HexNumber) << 8) | int.Parse(instr[i], NumberStyles.HexNumber)));
                            stuff.Add(instrs, new DATA(instr[i + 2], 0));
                            sb.AppendLine();
                            sb.Append("00 00 00 00 // DATA").Append(instr[i + 2]);
                            real_data_accesses++;
                            break;

                        case TYPE_BASE:
                            baseaddrpatches.Add(new BASEADDRPATCH(instrs));
                            sb.AppendLine();
                            sb.Append("00 00 00 00 // __BASEADDR");
                            real_base_accesses++;
                            break;
                        }
                        instrs += 4;
                        i      += 3;
                        si     += 4;
                        if (si < instrc)
                        {
                            sb.AppendLine();
                        }
                    }
                    for (; si < instrc; si++)
                    {
                        sb.Append(instr[si]).Append(' ');
                        instrs++;
                    }
                    sb.AppendLine();
                    if (comments)
                    {
                        sb.Append("// ").Append(comment.Trim()).AppendLine();
                    }
                } catch (Exception er) {
                    string result = er.ToString() + "\r\n" + _line;
                    MessageBox.Show(result);
                    return(result);
                }
            }
            sb.Append("end").AppendLine();
            var sb2 = new MyStringBuilder();

            sb2.Append("// this file is generated by asmtool").AppendLine();
            sb2.Append("// https://github.com/yugecin/scmcleoscripts/tree/master/tools/asm").AppendLine();
            sb2.AppendLine();
            sb2.Append(":HOOKER").AppendLine();
            sb2.Append("0AC6: 0@ = label @ENTRY offset").AppendLine();
            sb2.AppendLine();
            sb2.Append("0085: 1@ = 0@ // (int)").AppendLine();
            string[] hookadrs = new string[] {
                "",
                hookaddr.Substring(6, 2),
                hookaddr.Substring(4, 2),
                hookaddr.Substring(2, 2),
                hookaddr.Substring(0, 2),
            };
            sb2.Append("000E: 1@ -= 0x").Append(realaddr(hookadrs)).AppendLine();
            sb2.Append("0A8C: write_memory 0x").Append(i2hs(int.Parse(hookaddr, NumberStyles.HexNumber) - 1)).Append(" size 1 value 0xE9 vp 0").AppendLine();
            sb2.Append("0A8C: write_memory 0x").Append(hookaddr).Append(" size 4 value 1@ vp 0").AppendLine();
            sb2.AppendLine();
            sb2.Append("0AC6: 1@ = label @ENTRY offset").AppendLine();
            sb2.AppendLine();
            if (baseaddrpatches.Count > 0)
            {
                sb2.Append("0085: 2@ = 1@").AppendLine();
                foreach (BASEADDRPATCH p in baseaddrpatches)
                {
                    p.dostuff(sb2);
                }
                sb2.AppendLine();
            }
            int offset = 0;

            foreach (KeyValuePair <int, A> s in stuff)
            {
                int diff = s.Key - offset;
                sb2.Append("000A: 1@ += ").Append(diff).AppendLine();
                offset += diff;
                s.Value.dostuff(sb2);
            }
            sb2.AppendLine();
            sb2.Append("0002: jump @NOMOREHOOKER").AppendLine().AppendLine();
            sb.Insert(0, sb2.ToString());

            check(check_data_accesses, real_data_accesses, "data accesses");
            check(check_base_accesses, real_base_accesses, "base addr accesses");

            return(sb.ToString());
        }