示例#1
0
        public String Generate(String content)
        {
            StringTokenizer             tokenizer         = new StringTokenizer(content);
            StringBuilder               output            = new StringBuilder();
            Dictionary <String, String> localReplacements = new Dictionary <String, String>();

            Boolean inReplacement = false;
            Boolean inIndex       = false;
            String  replacer      = null;
            String  index         = null;

            foreach (StringToken token in tokenizer)
            {
                if (token.Type == StringTokenType.Punctuation && token.String == "[")
                {
                    inReplacement = true;
                }
                else if (token.Type == StringTokenType.Punctuation && token.String == "]")
                {
                    if (String.IsNullOrEmpty(index))
                    {
                        output.Append(MadLib.GetRandomItem(replacer));
                    }
                    else
                    {
                        String key = replacer + "-" + index;
                        if (!localReplacements.ContainsKey(key))
                        {
                            localReplacements.Add(key, MadLib.GetRandomItem(replacer));
                        }
                        output.Append(localReplacements[key]);
                    }

                    replacer      = null;
                    inReplacement = false;
                    inIndex       = false;
                    index         = null;
                }
                else if (inReplacement)
                {
                    if (inIndex)
                    {
                        index += token.String.ToUpperInvariant();
                    }
                    else
                    {
                        if (token.Type != StringTokenType.Punctuation || token.String == "_" || token.String == ":")
                        {
                            replacer += token.String.ToUpperInvariant();
                        }
                        else if (token.String == "-")
                        {
                            inIndex = true;
                        }
                    }
                }
                else
                {
                    output.Append(token.String);
                }
            }

            return(output.ToString());
        }
示例#2
0
 static MadLib()
 {
     MadLib.Initialize();
 }
示例#3
0
        public static String GetString(StringType type, ulong length)
        {
            String  ans       = null;
            String  charset   = null;
            Boolean useMadLib = false;

            switch (type)
            {
            case StringType.AaZz:
            case StringType.Digits:
            case StringType.AlphaNumeric:
            case StringType.ANSI:
            case StringType.ASCII:
            case StringType.Hex:
            case StringType.UpperCase:
            case StringType.LowerCase:
                charset = GetCharSet(type);
                break;

            case StringType.Unicode:
            case StringType.Random:
                ans = GetUnicodeString(length);
                break;

            case StringType.EmailSimple:
                ans = GetString(StringType.Name, length).Replace(' ', '.') + "@" + GetString(StringType.Domain, length);
                ans = ans.ToLowerInvariant();
                break;

            case StringType.Email:
                ans = GetString(StringType.ASCII, length) + "@" + GetString(StringType.Domain, length);
                break;

            case StringType.Domain:
                String tld = "." + GetString(StringType.TLD);
                ulong  l   = (Int64)length < (Int64)tld.Length ? 3 : length - (ulong)tld.Length;
                if (l < 3)
                {
                    l = 3;
                }
                ans = GetString(StringType.AaZz, l) + tld;
                ans = ans.ConvertWhitespaceToSpaces().Replace(" ", "");
                break;

            case StringType.TLD:
                ans = GetTLD();
                break;

            case StringType.Name:
                useMadLib = true;
                charset   = Rando.RandomBoolean() ? "[boyname]" : "[girlname]";
                charset  += " [lastname]";
                break;

            case StringType.Word:
                useMadLib = true;
                charset   = "[top5000]";
                break;

            case StringType.Sentence:
                useMadLib = true;
                charset   = GetSimpleSentenceStructure();
                break;

            case StringType.Uri:
                ans = GenerateUri();
                break;

            case StringType.IPAddress:
                if (Rando.RandomBoolean())
                {
                    ans = GetString(StringType.IPv4);
                }
                else
                {
                    ans = GetString(StringType.IPv6);
                }
                break;

            case StringType.IPv4:
                ans = (new System.Net.IPAddress((long)Rando.RandomInt(0, int.MaxValue))).ToString();
                break;

            case StringType.IPv6:
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 8; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(":");
                    }

                    if (!Rando.RandomBoolean(10))
                    {
                        sb.Append(GetString(StringType.Hex, 4));
                    }
                }

                ans = sb.ToString();
            }
            break;
            }
            if (useMadLib)
            {
                var m = new MadLib();
                ans = m.Generate(charset);
            }
            else if (!String.IsNullOrEmpty(charset))
            {
                StringBuilder sb = new StringBuilder();
                for (ulong i = 0; i < length; i++)
                {
                    sb.Append(Rando.RandomPick(charset));
                }
                ans = sb.ToString();
            }

            return(ans);
        }