Ejemplo n.º 1
0
    private List <string> getTokens(string expression)
    {
        string        operators  = "()*/+-";
        List <string> tokens     = new List <string>();
        StringMaker   makeString = new StringMaker();

        foreach (char c in expression.Replace(" ", string.Empty))
        {
            if (operators.IndexOf(c) >= 0)
            {
                if ((makeString.Length > 0))
                {
                    tokens.Add(makeString.ToString());
                    makeString.Length = 0;
                }
                tokens.Add(c);
            }
            else
            {
                makeString.Append(c);
            }
        }

        if ((makeString.Length > 0))
        {
            tokens.Add(makeString.ToString());
        }
        return(tokens);
    }
Ejemplo n.º 2
0
    private string getSubExpression(List <string> tokens, ref int i)
    {
        StringMaker subExpression = new StringMaker();
        int         banyakKurung  = 1;

        i += 1;
        while (i < tokens.Count && banyakKurung > 0)
        {
            string token = tokens[index];

            if (tokens[index] == "(")
            {
                banyakKurung += 1;
            }

            if (tokens[index] == ")")
            {
                banyakKurung -= 1;
            }

            if (banyakKurung > 0)
            {
                subExpression.Append(token);
            }
            i += 1;
        }

        if ((banyakKurung > 0))
        {
            throw new ArgumentException("Error Kurung");
        }
        return(subExpression.ToString());
    }
Ejemplo n.º 3
0
        public void TestExpansion_Int64()
        {
            StringMaker sm;
            string      expected;
            string      actual;

            long o = 123456;

            for (int capacity = 0; capacity < 20; capacity++)
            {
#if PUBLIC_STRINGMAKER
                sm = new StringMaker(capacity);
                sm.Append(o);
                actual   = sm.ExtractString();
                expected = string.Format("{0}", o);
                Assert.Equal(expected, actual);
                sm.Dispose();

                sm = new StringMaker(capacity);
                sm.Append(o);
                actual = sm.ExtractSpan().ToString();
                Assert.Equal(expected, actual);
                sm.Dispose();
#endif
                for (int width = -10; width < 10; width++)
                {
                    sm = new StringMaker(capacity);
                    sm.Append(o, "", null, width);
                    actual   = sm.ExtractString();
                    expected = string.Format(string.Format("{{0,{0}}}", width), o);
                    Assert.Equal(expected, actual);
                    sm.Dispose();

                    sm = new StringMaker(capacity);
                    sm.Append(o, "", null, width);
                    actual = sm.ExtractSpan().ToString();
                    Assert.Equal(expected, actual);
                    sm.Dispose();
                }
            }

#if PUBLIC_STRINGMAKER
            sm = new StringMaker(Array.Empty <char>(), true);
            sm.Append(o);
            Assert.True(sm.Overflowed);
            Assert.Equal(string.Empty, sm.ExtractString());
            sm.Dispose();
#endif

            sm = new StringMaker(Array.Empty <char>(), true);
            sm.Append(o, string.Empty, null, 0);
            Assert.True(sm.Overflowed);
            Assert.Equal(string.Empty, sm.ExtractSpan().ToString());
            Assert.Equal(string.Empty, sm.ExtractString());
            sm.Dispose();
        }
Ejemplo n.º 4
0
        public void TestExpansion_Char()
        {
            StringMaker sm;
            string      expected;
            string      actual;

            char o = 'x';

            for (int capacity = 0; capacity < 20; capacity++)
            {
                sm = new StringMaker(capacity);
                sm.Append(o);
                actual   = sm.ExtractString();
                expected = string.Format("{0}", o);
                Assert.Equal(expected, actual);
                sm.Dispose();

                for (int width = -10; width < 10; width++)
                {
                    sm = new StringMaker(capacity);
                    sm.Append(o, width);
                    actual   = sm.ExtractString();
                    expected = string.Format(string.Format("{{0,{0}}}", width), o);
                    Assert.Equal(expected, actual);
                    sm.Dispose();
                }
            }

            sm = new StringMaker(Array.Empty <char>(), true);
            sm.Append(o);
            Assert.True(sm.Overflowed);
            Assert.Equal(string.Empty, sm.ExtractString());
            sm.Dispose();

            sm = new StringMaker(Array.Empty <char>(), true);
            sm.Append(o, 0);
            Assert.True(sm.Overflowed);
            Assert.Equal(string.Empty, sm.ExtractSpan().ToString());
            Assert.Equal(string.Empty, sm.ExtractString());
            sm.Dispose();

            sm = new StringMaker(Array.Empty <char>(), true);
            sm.Append(o, 2);
            Assert.True(sm.Overflowed);
            Assert.Equal(string.Empty, sm.ExtractString());
            sm.Dispose();

            sm = new StringMaker(Array.Empty <char>(), true);
            sm.Append(o, -2);
            Assert.True(sm.Overflowed);
            Assert.Equal(string.Empty, sm.ExtractString());
            sm.Dispose();
        }