add nameparser parameter support

parameter = {n} for entries i, ra, rn.
This commit is contained in:
David Ruhmann 2015-08-19 17:17:50 -05:00
parent 49bb9f7e80
commit 59abcded74
5 changed files with 57 additions and 6 deletions

View file

@ -24,6 +24,7 @@
#endregion License Information (GPL v3)
using System;
using System.Collections.Generic;
using System.Text;
namespace ShareX.HelpersLib
@ -266,5 +267,19 @@ public static string[] Lines(this string text)
{
return text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
}
public static IEnumerable<Tuple<string, string>> ForEachBetween(this string text, string front, string back)
{
int f = 0;
int b = 0;
while (text.Length > f
&& 0 < (f = text.IndexOf(front, f))
&& 0 < (b = text.IndexOf(back, f + front.Length)))
{
string result = text.Substring(f, (b + back.Length) - f);
yield return new Tuple<string, string>(result, result.Substring(front.Length, (result.Length - back.Length) - front.Length));
f += front.Length;
}
}
}
}

View file

@ -881,5 +881,15 @@ public static bool IsAdministrator()
{
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
}
public static string RepeatGenerator(int count, Func<string> generator)
{
string result = "";
for (int x = count; x > 0; x--)
{
result += generator();
}
return result;
}
}
}

View file

@ -25,6 +25,7 @@
using ShareX.HelpersLib.Properties;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Text;
@ -177,6 +178,10 @@ public string Parse(string pattern)
if (sb.ToString().Contains(ReplCodeMenuEntry.i.ToPrefixString()))
{
AutoIncrementNumber++;
foreach (Tuple<string, int> entry in ListEntryWithValue(sb.ToString(), ReplCodeMenuEntry.i.ToPrefixString()))
{
sb.Replace(entry.Item1, Helpers.AddZeroes(AutoIncrementNumber, Math.Max(0, entry.Item2)));
}
sb.Replace(ReplCodeMenuEntry.i.ToPrefixString(), AutoIncrementNumber.ToString());
}
@ -191,6 +196,15 @@ public string Parse(string pattern)
string result = sb.ToString();
foreach (Tuple<string, int> entry in ListEntryWithValue(result, ReplCodeMenuEntry.rn.ToPrefixString()))
{
result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Numbers).ToString()));
}
foreach (Tuple<string, int> entry in ListEntryWithValue(result, ReplCodeMenuEntry.ra.ToPrefixString()))
{
result = result.ReplaceAll(entry.Item1, () => Helpers.RepeatGenerator(entry.Item2, () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString()));
}
result = result.ReplaceAll(ReplCodeMenuEntry.rn.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Numbers).ToString());
result = result.ReplaceAll(ReplCodeMenuEntry.ra.ToPrefixString(), () => Helpers.GetRandomChar(Helpers.Alphanumeric).ToString());
@ -218,5 +232,17 @@ public string Parse(string pattern)
return result;
}
private IEnumerable<Tuple<string, int>> ListEntryWithValue(string text, string entry)
{
foreach (Tuple<string, string> s in text.ForEachBetween(entry + "{", "}"))
{
int n = 0;
if (int.TryParse(s.Item2, out n))
{
yield return new Tuple<string, int>(s.Item1, n);
}
}
}
}
}

View file

@ -1673,7 +1673,7 @@ internal class Resources {
}
/// <summary>
/// Looks up a localized string similar to Auto increment number.
/// Looks up a localized string similar to Auto increment number. 0 pad left using {n}.
/// </summary>
internal static string ReplCodeMenuEntry_i_Auto_increment_number {
get {
@ -1754,7 +1754,7 @@ internal class Resources {
}
/// <summary>
/// Looks up a localized string similar to Random alphanumeric char.
/// Looks up a localized string similar to Random alphanumeric char. Repeat using {n}.
/// </summary>
internal static string ReplCodeMenuEntry_ra_Random_alphanumeric_char {
get {
@ -1763,7 +1763,7 @@ internal class Resources {
}
/// <summary>
/// Looks up a localized string similar to Random number 0 to 9.
/// Looks up a localized string similar to Random number 0 to 9. Repeat using {n}.
/// </summary>
internal static string ReplCodeMenuEntry_rn_Random_number_0_to_9 {
get {

View file

@ -197,13 +197,13 @@
<value>Unix timestamp</value>
</data>
<data name="ReplCodeMenuEntry_i_Auto_increment_number" xml:space="preserve">
<value>Auto increment number</value>
<value>Auto increment number. 0 pad left using {n}</value>
</data>
<data name="ReplCodeMenuEntry_rn_Random_number_0_to_9" xml:space="preserve">
<value>Random number 0 to 9</value>
<value>Random number 0 to 9. Repeat using {n}</value>
</data>
<data name="ReplCodeMenuEntry_ra_Random_alphanumeric_char" xml:space="preserve">
<value>Random alphanumeric char</value>
<value>Random alphanumeric char. Repeat using {n}</value>
</data>
<data name="ReplCodeMenuEntry_width_Gets_image_width" xml:space="preserve">
<value>Image width</value>