public bool FromNative(string pattern, char[] stringSeparators)
{
Debug.AssertNotNull(pattern, "Pattern string is null");
if(pattern.Length == 0) {
return false;
}
string[] components = pattern.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
// there should be at least four components
if(components.Length < 4) {
return false;
}
else {
int patternLength;
if(int.TryParse(components[3], out patternLength) == false) {
Debug.ReportWarning("Couldn't parse pattern length. Length field: {0}", components[0]);
return false;
}
// perform some other validation on the pattern length
if(patternLength < 0 || patternLength > 0x10000) {
Debug.ReportWarning("Invalid pattern length. Length: {0}", patternLength);
return false;
}
if(patternLength > (components.Length - 1)) {
Debug.ReportWarning("Not all pattern components present. Length: {0}, Present: {1}", patternLength, components.Length);
return false;
}
if(patternLength > 0) {
// allocate the pattern
_pattern = new byte[patternLength];
for(int i = 0; i < patternLength; i++) {
if(byte.TryParse(components[4 + i], out _pattern[i]) == false) {
Debug.ReportWarning("Failed to parse pattern component. Component field: {0}", components[4 + i]);
_pattern = null;
return false;
}
}
}
return true;
}
}