package org.das2.util;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Apply an operation to a String to make it a String with similar meaning. This is needed for
* new code which indicates the orbit number, but the orbit number context is provided elsewhere
* on the plot.
*
*
* - https://calvinandhobbes.fandom.com/wiki/Transmogrifier_Gun?file=CalvinTransmogrifierGunWeb.jpg
*
*
* @author jbf
*/
public class StringTransmogrifier {
Pattern p;
String result;
int nf;
String[] byName;
int[] byIndex;
/**
* Create a regex-based transmografier. The result is the template which is
* populated from groups of the regular expression. The groups can be referenced
* by index or by name, with
*
* - $<num> by index, where 1 is the first group number
*
- $<letter> by 1-letter named group
*
- $<{name}> by named group
* @param regex the regular expression with
* @param result the result
*/
public StringTransmogrifier( String regex, String result ) {
p= Pattern.compile(regex);
String[] ss= result.split("\\$");
nf= ss.length-1;
byIndex= new int[nf];
byName= new String[nf];
StringBuilder resultb= new StringBuilder(ss[0]);
int fieldNum=0;
for ( int i=1; i1 && Character.isDigit(s.charAt(1)) ) {
byIndex[fieldNum]= ( s.charAt(0) - 48 ) * 10 + ( s.charAt(1) - 48 );
resultb.append(s.substring(2));
} else {
byIndex[fieldNum]= ( s.charAt(0) - 48 );
resultb.append(s.substring(1));
}
} else if ( s.charAt(0)=='{' ) {
int i2= s.indexOf("}");
byName[fieldNum]= s.substring(1,i2);
resultb.append("%s");
resultb.append(s.substring(i2+1));
} else {
byName[fieldNum]= s.substring(0,1);
resultb.append("%s");
resultb.append(s.substring(1));
}
fieldNum++;
}
}
if ( fieldNum