Hi All.
I have converted a Gradient RegEx Fractal code from Java to CSharp in which you type in a RegEx string like 1*2*0 and it creates a fancy gradient pattern, saved to a png file. It used a nuget package called Fare which implements dk.briks.automation.
The Java version was originally here:
GitHub - SSODelta/GradientRegexImages: Creating pretty images from regular expressions using edit distance
and the Fare nuget for CSharp is here:
The
Fare did not have implemented GetStrings, but in the Java version of dk briks automation it was there.
In Java version:
I tried to make a C# version of GetStrings that matches the Java code but I get the error message "Index and length must refer to a location within the string".
Googling suggested: The error occurs in C# when you are trying to perform an operation (like accessing or removing characters) on a string or StringBuilder and the provided index and/or length is out of bounds for the current string or string-like object, but I still cannot find where that error is (getting really frustrated). Any help or hints would be greatly appreciated. I would really like to get it working in CSharp. At present the program compiles to an exe and is run in the console with say a test RegEx string 1*2*0 but I get the error message "Index and length must refer to a location within the string".
The problem might be too hard to solve, but I thought I would ask for help.
I have converted a Gradient RegEx Fractal code from Java to CSharp in which you type in a RegEx string like 1*2*0 and it creates a fancy gradient pattern, saved to a png file. It used a nuget package called Fare which implements dk.briks.automation.
The Java version was originally here:
GitHub - SSODelta/GradientRegexImages: Creating pretty images from regular expressions using edit distance
and the Fare nuget for CSharp is here:
GitHub - moodmosaic/Fare: Port of Java dk.brics.automaton and xeger, mostly used for generating strings that match a specific regular expression.
Port of Java dk.brics.automaton and xeger, mostly used for generating strings that match a specific regular expression. - moodmosaic/Fare
github.com
The
Fare did not have implemented GetStrings, but in the Java version of dk briks automation it was there.
In Java version:
Java GetString Implementation:
private static void getStrings(State s, Set<String> strings, StringBuilder path, int length) {
if (length == 0) {
if (s.accept)
strings.add(path.toString());
} else
for (Transition t : s.transitions)
for (int n = t.min; n <= t.max; n++) {
path.append((char)n);
getStrings(t.to, strings, path, length - 1);
path.deleteCharAt(path.length() - 1);
}
}
and also
public static Set<String> getStrings(Automaton a, int length) {
HashSet<String> strings = new HashSet<String>();
if (a.isSingleton && a.singleton.length() == length)
strings.add(a.singleton);
else if (length >= 0)
getStrings(a.initial, strings, new StringBuilder(), length);
return strings;
I tried to make a C# version of GetStrings that matches the Java code but I get the error message "Index and length must refer to a location within the string".
Googling suggested: The error occurs in C# when you are trying to perform an operation (like accessing or removing characters) on a string or StringBuilder and the provided index and/or length is out of bounds for the current string or string-like object, but I still cannot find where that error is (getting really frustrated). Any help or hints would be greatly appreciated. I would really like to get it working in CSharp. At present the program compiles to an exe and is run in the console with say a test RegEx string 1*2*0 but I get the error message "Index and length must refer to a location within the string".
CSHarp GetString:
public static HashSet<string> GetStrings(Automaton automaton, int length)
{
HashSet<string> strings = new HashSet<string>();
if (automaton.IsSingleton && automaton.Singleton.Length == length)
strings.Add(automaton.Singleton);
else if (length >= 0)
GetStrings(automaton.Initial, strings, new StringBuilder(), length);
return strings;
}
private static void GetStrings(State s, HashSet<string> strings, StringBuilder path, int length)
{
if (length == 0)
{
if (s.Accept)
strings.Add(path.ToString());
}
else
{
foreach (Transition t in s.Transitions)
{
for (int n = t.Min; n <= t.Max; n++)
{
path.Append((char)n);
GetStrings(t.To, strings, path, length - 1);
// Only remove the last character if the path is not empty
if (path.Length > 0)
path.Remove(path.Length - 1, 1);
}
}
}
}
The problem might be too hard to solve, but I thought I would ask for help.