Advertisement
timeshifter

new parser

Oct 19th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1.         if (true) {
  2.  
  3.  
  4.             bool hasMatch = true;
  5.             Regex regToken = new Regex("{([^{}]+)}"),
  6.                 regDefault = new Regex(@"{(\d):([^{}]+)}"),
  7.                 regUrl = new Regex(@"{(\d):url}"),
  8.                 regArg = new Regex(@"{(\d+)}"),
  9.                 regRnd = new Regex(@"{rnd:([^{}]+)}"),
  10.                 regRng = new Regex(@"{rng:([^{}]+)}");
  11.  
  12.  
  13.             int matchIdx = -1, matchLen = -1;
  14.             string matchReplace = "";
  15.  
  16.             string testOut = msg;
  17.             int iterations=4;
  18.             while (hasMatch && iterations>0) {
  19.  
  20.                 if (regToken.IsMatch(testOut)) {
  21.                     int offset = 0;
  22.                     foreach (Match mToken in regToken.Matches(testOut)) {
  23.                         matchReplace = "";
  24.  
  25.                         string matchOriginal = mToken.Value;
  26.  
  27.                         if (regUrl.IsMatch(matchOriginal)) {
  28.                             Match mUrl = regUrl.Matches(matchOriginal)[0];
  29.  
  30.                             if (prms.Length > int.Parse(mUrl.Groups[1].Value)) {
  31.                                 matchReplace = HttpUtility.UrlEncode(prms[int.Parse(mUrl.Groups[1].Value)]);
  32.                             }
  33.                             else {
  34.                                 matchReplace = matchOriginal;
  35.                             }
  36.                            
  37.                         }
  38.                         else if (regDefault.IsMatch(matchOriginal)) {
  39.                             Match mDefault = regDefault.Matches(matchOriginal)[0];
  40.                            
  41.                             if (prms.Length > int.Parse(mDefault.Groups[1].Value)) {
  42.                                 matchReplace = prms[int.Parse(mDefault.Groups[1].Value)];
  43.                             }
  44.                             else {
  45.                                 matchReplace = mDefault.Groups[2].Value;
  46.                             }
  47.  
  48.                         }
  49.                         else if (regArg.IsMatch(matchOriginal)) {
  50.                             Match mArg = regArg.Matches(matchOriginal)[0];
  51.  
  52.                             if (prms.Length > int.Parse(mArg.Groups[1].Value)) {
  53.                                 matchReplace = prms[int.Parse(mArg.Groups[1].Value)];
  54.                             }
  55.                             else {
  56.                                 matchReplace = matchOriginal;
  57.                             }
  58.  
  59.                         }
  60.                         else if (regRnd.IsMatch(matchOriginal)) {
  61.                             Match mRnd = regRnd.Matches(matchOriginal)[0];
  62.  
  63.                             if (mRnd.Groups[1].Value.ToLower() == "args") {
  64.                                 string[] parts = msgText.Split(',');
  65.                                 matchReplace = parts[R.Next(parts.Length)];
  66.                             }
  67.                             else {
  68.                                 string[] parts = mRnd.Groups[1].Value.Split('|');
  69.                                 matchReplace = parts[R.Next(parts.Length)];
  70.                             }
  71.                         }
  72.                         else if (regRng.IsMatch(matchOriginal)) {
  73.                             Match mRng = regRng.Matches(matchOriginal)[0];
  74.  
  75.                             string[] parts = mRng.Groups[1].Value.Split(',');
  76.                             int p1 = 0, p2 = 0;
  77.                             if (parts[0].ToLower().Trim() == "min") {
  78.                                 p1 = int.MinValue;
  79.                             }
  80.                             else {
  81.                                 p1 = int.Parse(parts[0]);
  82.                             }
  83.                             if (parts[1].ToLower().Trim() == "max") {
  84.                                 p2 = int.MaxValue-1;
  85.                             }
  86.                             else {
  87.                                 p2 = int.Parse(parts[1]);
  88.                             }
  89.  
  90.                             matchReplace = R.Next(Math.Min(p1, p2), Math.Max(p1, p2) + 1).ToString();
  91.                         }
  92.  
  93.  
  94.  
  95.                         if (matchReplace != "") {
  96.                             testOut = testOut.Remove(mToken.Index-offset, mToken.Length).Insert(mToken.Index-offset, matchReplace);
  97.                             offset += mToken.Length - matchReplace.Length;
  98.                         }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.                     }
  105.  
  106.  
  107.  
  108.  
  109.                 }
  110.                 else {
  111.                     hasMatch = false;
  112.                 }
  113.  
  114.  
  115.                 iterations--;
  116.             }
  117.  
  118.             if (target == "#timebot") {
  119.                 PendingOutput.Add(SendMessage(target, "new parser result: " + testOut));
  120.             }
  121.             else {
  122.                 /*PendingOutput.Add(SendMessage(admin, testOut));*/
  123.             }
  124.            
  125.             /*return msgText;*/
  126.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement