Example #1
0
        // Cf. EncodeDataToArgs
        internal static string DecodeArgsToData(string strArgs)
        {
            if (strArgs == null)
            {
                Debug.Assert(false); return(string.Empty);
            }

            Debug.Assert(StrUtil.Count(strArgs, "\"") == StrUtil.Count(strArgs, "\\\""));

            if (MonoWorkarounds.IsRequired(3471228285U) && IsUnix())
            {
                string str = strArgs;

                str = str.Replace("\\\"", "\"");
                str = str.Replace("\\\\", "\\");

                return(str);
            }

            StringBuilder sb = new StringBuilder();
            int           i  = 0;

            while (i < strArgs.Length)
            {
                char ch = strArgs[i++];

                if (ch == '\\')
                {
                    int cBackslashes = 1;
                    while ((i < strArgs.Length) && (strArgs[i] == '\\'))
                    {
                        ++cBackslashes;
                        ++i;
                    }

                    if (i == strArgs.Length)
                    {
                        sb.Append('\\', cBackslashes);                         // Assume no quote follows
                    }
                    else if (strArgs[i] == '\"')
                    {
                        Debug.Assert((cBackslashes & 1) == 1);
                        sb.Append('\\', (cBackslashes - 1) / 2);
                        sb.Append('\"');
                        ++i;
                    }
                    else
                    {
                        sb.Append('\\', cBackslashes);
                    }
                }
                else
                {
                    sb.Append(ch);
                }
            }

            return(sb.ToString());
        }
Example #2
0
        private static void TestStrUtil()
        {
#if DEBUG
            string[]      vSeps = new string[] { "ax", "b", "c" };
            const string  str1  = "axbqrstcdeax";
            List <string> v1    = StrUtil.SplitWithSep(str1, vSeps, true);

            if (v1.Count != 9)
            {
                throw new InvalidOperationException("StrUtil-1");
            }
            if (v1[0].Length > 0)
            {
                throw new InvalidOperationException("StrUtil-2");
            }
            if (!v1[1].Equals("ax"))
            {
                throw new InvalidOperationException("StrUtil-3");
            }
            if (v1[2].Length > 0)
            {
                throw new InvalidOperationException("StrUtil-4");
            }
            if (!v1[3].Equals("b"))
            {
                throw new InvalidOperationException("StrUtil-5");
            }
            if (!v1[4].Equals("qrst"))
            {
                throw new InvalidOperationException("StrUtil-6");
            }
            if (!v1[5].Equals("c"))
            {
                throw new InvalidOperationException("StrUtil-7");
            }
            if (!v1[6].Equals("de"))
            {
                throw new InvalidOperationException("StrUtil-8");
            }
            if (!v1[7].Equals("ax"))
            {
                throw new InvalidOperationException("StrUtil-9");
            }
            if (v1[8].Length > 0)
            {
                throw new InvalidOperationException("StrUtil-10");
            }

            const string  str2 = "12ab56";
            List <string> v2   = StrUtil.SplitWithSep(str2, new string[] { "AB" }, false);
            if (v2.Count != 3)
            {
                throw new InvalidOperationException("StrUtil-11");
            }
            if (!v2[0].Equals("12"))
            {
                throw new InvalidOperationException("StrUtil-12");
            }
            if (!v2[1].Equals("AB"))
            {
                throw new InvalidOperationException("StrUtil-13");
            }
            if (!v2[2].Equals("56"))
            {
                throw new InvalidOperationException("StrUtil-14");
            }

            List <string> v3 = StrUtil.SplitWithSep("pqrs", vSeps, false);
            if (v3.Count != 1)
            {
                throw new InvalidOperationException("StrUtil-15");
            }
            if (!v3[0].Equals("pqrs"))
            {
                throw new InvalidOperationException("StrUtil-16");
            }

            if (StrUtil.VersionToString(0x000F000E000D000CUL) != "15.14.13.12")
            {
                throw new InvalidOperationException("StrUtil-V1");
            }
            if (StrUtil.VersionToString(0x00FF000E00010000UL) != "255.14.1")
            {
                throw new InvalidOperationException("StrUtil-V2");
            }
            if (StrUtil.VersionToString(0x000F00FF00000000UL) != "15.255")
            {
                throw new InvalidOperationException("StrUtil-V3");
            }
            if (StrUtil.VersionToString(0x00FF000000000000UL) != "255")
            {
                throw new InvalidOperationException("StrUtil-V4");
            }
            if (StrUtil.VersionToString(0x00FF000000000000UL, 2) != "255.0")
            {
                throw new InvalidOperationException("StrUtil-V5");
            }
            if (StrUtil.VersionToString(0x0000000000070000UL) != "0.0.7")
            {
                throw new InvalidOperationException("StrUtil-V6");
            }
            if (StrUtil.VersionToString(0x0000000000000000UL) != "0")
            {
                throw new InvalidOperationException("StrUtil-V7");
            }
            if (StrUtil.VersionToString(0x00000000FFFF0000UL, 4) != "0.0.65535.0")
            {
                throw new InvalidOperationException("StrUtil-V8");
            }
            if (StrUtil.VersionToString(0x0000000000000000UL, 4) != "0.0.0.0")
            {
                throw new InvalidOperationException("StrUtil-V9");
            }

            if (StrUtil.RtfEncodeChar('\u0000') != "\\u0?")
            {
                throw new InvalidOperationException("StrUtil-Rtf1");
            }
            if (StrUtil.RtfEncodeChar('\u7FFF') != "\\u32767?")
            {
                throw new InvalidOperationException("StrUtil-Rtf2");
            }
            if (StrUtil.RtfEncodeChar('\u8000') != "\\u-32768?")
            {
                throw new InvalidOperationException("StrUtil-Rtf3");
            }
            if (StrUtil.RtfEncodeChar('\uFFFF') != "\\u-1?")
            {
                throw new InvalidOperationException("StrUtil-Rtf4");
            }

            if (!StrUtil.StringToBool(Boolean.TrueString))
            {
                throw new InvalidOperationException("StrUtil-Bool1");
            }
            if (StrUtil.StringToBool(Boolean.FalseString))
            {
                throw new InvalidOperationException("StrUtil-Bool2");
            }

            if (StrUtil.Count("Abracadabra", "a") != 4)
            {
                throw new InvalidOperationException("StrUtil-Count1");
            }
            if (StrUtil.Count("Bla", "U") != 0)
            {
                throw new InvalidOperationException("StrUtil-Count2");
            }
            if (StrUtil.Count("AAAAA", "AA") != 4)
            {
                throw new InvalidOperationException("StrUtil-Count3");
            }

            const string sU = "data:mytype;base64,";
            if (!StrUtil.IsDataUri(sU))
            {
                throw new InvalidOperationException("StrUtil-DataUri1");
            }
            if (!StrUtil.IsDataUri(sU, "mytype"))
            {
                throw new InvalidOperationException("StrUtil-DataUri2");
            }
            if (StrUtil.IsDataUri(sU, "notmytype"))
            {
                throw new InvalidOperationException("StrUtil-DataUri3");
            }

            uint u = 0x7FFFFFFFU;
            if (u.ToString(NumberFormatInfo.InvariantInfo) != "2147483647")
            {
                throw new InvalidOperationException("StrUtil-Inv1");
            }
            if (uint.MaxValue.ToString(NumberFormatInfo.InvariantInfo) !=
                "4294967295")
            {
                throw new InvalidOperationException("StrUtil-Inv2");
            }
            if (long.MinValue.ToString(NumberFormatInfo.InvariantInfo) !=
                "-9223372036854775808")
            {
                throw new InvalidOperationException("StrUtil-Inv3");
            }
            if (short.MinValue.ToString(NumberFormatInfo.InvariantInfo) !=
                "-32768")
            {
                throw new InvalidOperationException("StrUtil-Inv4");
            }
#endif
        }