public Match(String path) {
// IF DEBUG: System.out.print("\n[" + path + "]");
params = new HashMap<>();
- if(path!=null) {
+ if (path!=null) {
String[] pa = path.split("/");
values = new byte[pa.length][];
vars = new Integer[pa.length];
int val = 0;
String key;
- for(int i=0;i<pa.length && !wildcard;++i) {
- if(pa[i].startsWith(":")) {
- if(pa[i].endsWith("*")) {
+ for (int i=0;i<pa.length && !wildcard;++i) {
+ if (pa[i].startsWith(":")) {
+ if (pa[i].endsWith("*")) {
val = i | pa.length<<16; // load end value in high order bits
key = pa[i].substring(0, pa[i].length()-1);// remove *
wildcard = true;
vars[i]=val;
} else {
values[i]=pa[i].getBytes();
- if(pa[i].endsWith("*")) {
+ if (pa[i].endsWith("*")) {
wildcard = true;
- if(pa[i].length()>1) {
+ if (pa[i].length()>1) {
/* remove * from value */
int newlength = values[i].length-1;
byte[] real = new byte[newlength];
*/
public String param(String path,String key) {
Integer val = params.get(key); // :key or key
- if(val!=null) {
+ if (val!=null) {
int start = val & 0xFFFF;
int end = (val >> 16) & 0xFFFF;
int idx = -1;
int i;
- for(i=0;i<start;++i) {
+ for (i=0;i<start;++i) {
idx = path.indexOf('/',idx+1);
- if(idx<0)break;
+ if (idx<0)break;
}
- if(i==start) {
+ if (i==start) {
++idx;
- if(end==0) {
+ if (end==0) {
end = path.indexOf('/',idx);
- if(end<0)end=path.length();
+ if (end<0)end=path.length();
} else {
end=path.length();
}
return path.substring(idx,end);
- } else if(i==start-1) { // if last spot was left blank, i.e. :key*
+ } else if (i==start-1) { // if last spot was left blank, i.e. :key*
return "";
}
}
}
public boolean match(String path) {
- if(path==null|| path.length()==0 || "/".equals(path) ) {
- if(values==null)return true;
+ if (path==null|| path.length()==0 || "/".equals(path) ) {
+ if (values==null)return true;
switch(values.length) {
case 0: return true;
case 1: return values[0].length==0;
int lastByte = pabytes.length;
boolean fieldMatched = false; // = lastByte>0?(pabytes[0]=='/'):false;
// IF DEBUG: System.out.println("\n -- " + path + " --");
- for(int i=0;rv && i<lastByte;++i) {
- if(field>=lastField) { // checking here allows there to be a non-functional ending /
+ for (int i=0;rv && i<lastByte;++i) {
+ if (field>=lastField) { // checking here allows there to be a non-functional ending /
rv = false;
break;
}
- if(values[field]==null) { // it's a variable, just look for /s
- if(wildcard && field==lastField-1) return true;// we've made it this far. We accept all remaining characters
+ if (values[field]==null) { // it's a variable, just look for /s
+ if (wildcard && field==lastField-1) return true;// we've made it this far. We accept all remaining characters
Integer val = vars[field];
int start = val & 0xFFFF;
int end = (val >> 16) & 0xFFFF;
- if(end==0)end=start+1;
+ if (end==0)end=start+1;
int k = i;
- for(int j=start; j<end && k<lastByte; ++k) {
+ for (int j=start; j<end && k<lastByte; ++k) {
// IF DEBUG: System.out.print((char)pabytes[k]);
- if(pabytes[k]=='/') {
+ if (pabytes[k]=='/') {
++field;
++j;
}
}
- if(k==lastByte && pabytes[k-1]!='/')++field;
- if(k>i)i=k-1; // if we've incremented, have to accommodate the outer for loop incrementing as well
+ if (k==lastByte && pabytes[k-1]!='/')++field;
+ if (k>i)i=k-1; // if we've incremented, have to accommodate the outer for loop incrementing as well
fieldMatched = false; // reset
fieldIdx = 0;
} else {
// IF DEBUG: System.out.print((char)pabytes[i]);
- if(pabytes[i]=='/') { // end of field, eval if Field is matched
+ if (pabytes[i]=='/') { // end of field, eval if Field is matched
// if double slash, check if supposed to be empty
- if(fieldIdx==0 && values[field].length==0) {
+ if (fieldIdx==0 && values[field].length==0) {
fieldMatched = true;
}
rv = fieldMatched && ++field<lastField;
// reset
fieldMatched = false;
fieldIdx = 0;
- } else if(values[field].length==0) {
+ } else if (values[field].length==0) {
// double slash in path, but content in field. We check specially here to avoid
// Array out of bounds issues.
rv = false;
} else {
- if(fieldMatched) {
+ if (fieldMatched) {
rv =false; // field is already matched, now there's too many bytes
} else {
rv = pabytes[i]==values[field][fieldIdx++]; // compare expected (pabytes[i]) with value for particular field
fieldMatched=values[field].length==fieldIdx; // are all the bytes match in the field?
- if(fieldMatched && (i==lastByte-1 || (wildcard && field==lastField-1)))
+ if (fieldMatched && (i==lastByte-1 || (wildcard && field==lastField-1)))
return true; // last field info
}
}
}
}
- if(field!=lastField || pabytes.length!=lastByte) rv = false; // have we matched all the fields and all the bytes?
+ if (field!=lastField || pabytes.length!=lastByte) rv = false; // have we matched all the fields and all the bytes?
return rv;
}