/// <summary> This implements only the most basic checking for an email address's validity -- that it contains /// an '@' contains no characters disallowed by RFC 2822. This is an overly lenient definition of /// validity. We want to generally be lenient here since this class is only intended to encapsulate what's /// in a barcode, not "judge" it. /// </summary> internal static bool isBasicallyValidEmailAddress(String email) { if (email == null) { return false; } bool atFound = false; for (int i = 0; i < email.Length; i++) { char c = email.CharAt(i); if ((CharExtend.ToInt32(c) < CharExtend.ToInt32('a') || CharExtend.ToInt32(c) > CharExtend.ToInt32('z')) && (CharExtend.ToInt32(c) < CharExtend.ToInt32('A') || CharExtend.ToInt32(c) > CharExtend.ToInt32('Z')) && (CharExtend.ToInt32(c) < CharExtend.ToInt32('0') || CharExtend.ToInt32(c) > CharExtend.ToInt32('9')) && !isAtextSymbol(c)) { return false; } if (c == '@') { if (atFound) { return false; } atFound = true; } } return atFound; }
/*******************************/ /// <summary> /// Copies an array of chars obtained from a String into a specified array of chars /// </summary> /// <param name="sourceString">The String to get the chars from</param> /// <param name="sourceStart">Position of the String to start getting the chars</param> /// <param name="sourceEnd">Position of the String to end getting the chars</param> /// <param name="destinationArray">Array to return the chars</param> /// <param name="destinationStart">Position of the destination array of chars to start storing the chars</param> /// <returns>An array of chars</returns> public static void GetCharsFromString(String sourceString, int sourceStart, int sourceEnd, char[] destinationArray, int destinationStart) { int sourceCounter; int destinationCounter; sourceCounter = sourceStart; destinationCounter = destinationStart; while (sourceCounter < sourceEnd) { destinationArray[destinationCounter] = (char) sourceString.CharAt(sourceCounter); sourceCounter++; destinationCounter++; } }
protected internal static String unescapeBackslash(String escaped) { if (escaped != null) { int backslash = escaped.IndexOf('\\'); if (backslash >= 0) { int max = escaped.Length; StringBuilder unescaped = new StringBuilder(); unescaped.Append(escaped.Substr(0, backslash)); bool nextIsEscaped = false; for (int i = backslash; i < max; i++) { char c = escaped.CharAt(i); if (nextIsEscaped || c != '\\') { unescaped.Append(c); nextIsEscaped = false; } else { nextIsEscaped = true; } } return unescaped.ToString(); } } return escaped; }
internal static String[] matchPrefixedField(String prefix, String rawText, char endChar, bool trim) { ArrayList matches = null; int i = 0; int max = rawText.Length; while (i < max) { //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'" i = rawText.IndexOf(prefix, i); if (i < 0) { break; } i += prefix.Length; // Skip past this prefix we found to start int start = i; // Found the start of a match here bool done = false; while (!done) { //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'" i = rawText.IndexOf((Char) endChar, i); if (i < 0) { // No terminating end character? uh, done. Set i such that loop terminates and break i = rawText.Length; done = true; } else if (rawText.CharAt(i - 1) == '\\') { // semicolon was escaped so continue i++; } else { // found a match if (matches == null) { matches = new ArrayList(); // lazy init } String element = unescapeBackslash(rawText.Substr(start, (i) - (start))); if (trim) { element = element.Trim(); } matches.Add(element); i++; done = true; } } } if (matches == null || (matches.Count == 0)) { return null; } return toStringArray(matches); }
protected internal static bool isSubstringOfDigits(String value_Renamed, int offset, int length) { if (value_Renamed == null) { return false; } int stringLength = value_Renamed.Length; int max = offset + length; if (stringLength < max) { return false; } for (int i = offset; i < max; i++) { char c = value_Renamed.CharAt(i); if (CharExtend.ToInt32(c) < CharExtend.ToInt32('0') || CharExtend.ToInt32(c) > CharExtend.ToInt32('9')) { return false; } } return true; }
protected internal static bool isStringOfDigits(String value_Renamed, int length) { if (value_Renamed == null) { return false; } int stringLength = value_Renamed.Length; if (length != stringLength) { return false; } for (int i = 0; i < length; i++) { char c = value_Renamed.CharAt(i); if (CharExtend.ToInt32(c) < CharExtend.ToInt32('0') || CharExtend.ToInt32(c) > CharExtend.ToInt32('9')) { return false; } } return true; }
private static String formatAddress(String address) { if (address == null) { return null; } int length = address.Length; StringBuilder newAddress = new StringBuilder(); for (int j = 0; j < length; j++) { char c = address.CharAt(j); if (c == ';') { newAddress.Append(' '); } else { newAddress.Append(c); } } return newAddress.ToString().Trim(); }
private static String[] matchVCardPrefixedField(String prefix, String rawText, bool trim) { ArrayList matches = null; int i = 0; int max = rawText.Length; while (i < max) { //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'" i = rawText.IndexOf(prefix, i); if (i < 0) { break; } if (i > 0 && rawText.CharAt(i - 1) != '\n') { // then this didn't start a new token, we matched in the middle of something i++; continue; } i += prefix.Length; // Skip past this prefix we found to start if (rawText.CharAt(i) != ':' && rawText.CharAt(i) != ';') { continue; } while (rawText.CharAt(i) != ':') { // Skip until a colon i++; } i++; // skip colon int start = i; // Found the start of a match here //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'" i = rawText.IndexOf('\n', i); // Really, ends in \r\n if (i < 0) { // No terminating end character? uh, done. Set i such that loop terminates and break i = max; } else if (i > start) { // found a match if (matches == null) { matches = new ArrayList(); // lazy init } String element = rawText.Substr(start, (i) - (start)); if (trim) { element = element.Trim(); } matches.Add(element); i++; } else { i++; } } if (matches == null || (matches.Count == 0)) { return null; } return toStringArray(matches); }
private static bool isLikeVCardDate(String value_Renamed) { if (value_Renamed == null) { return true; } // Not really sure this is true but matches practice // Mach YYYYMMDD if (isStringOfDigits(value_Renamed, 8)) { return true; } // or YYYY-MM-DD return value_Renamed.Length == 10 && value_Renamed.CharAt(4) == '-' && value_Renamed.CharAt(7) == '-' && isSubstringOfDigits(value_Renamed, 0, 4) && isSubstringOfDigits(value_Renamed, 5, 2) && isSubstringOfDigits(value_Renamed, 8, 2); }
/// <summary> Choose the best mode by examining the content. Note that 'encoding' is used as a hint; /// if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. /// </summary> public static Mode chooseMode2(String content, String encoding) { if ("Shift_JIS" == encoding) { // Choose Kanji mode if all input are double-byte characters return isOnlyDoubleByteKanji(content)?Mode.KANJI:Mode.BYTE; } bool hasNumeric = false; bool hasAlphanumeric = false; for (int i = 0; i < content.Length; ++i) { char c = content.CharAt(i); if (CharExtend.ToInt32(c) >= CharExtend.ToInt32('0') && CharExtend.ToInt32(c) <= CharExtend.ToInt32('9')) { hasNumeric = true; } else if (getAlphanumericCode(CharExtend.ToInt32(c)) != - 1) { hasAlphanumeric = true; } else { return Mode.BYTE; } } if (hasAlphanumeric) { return Mode.ALPHANUMERIC; } else if (hasNumeric) { return Mode.NUMERIC; } return Mode.BYTE; }
private static bool isColonFollowedByPortNumber(String uri, int protocolEnd) { //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'" int nextSlash = uri.IndexOf('/', protocolEnd + 1); if (nextSlash < 0) { nextSlash = uri.Length; } if (nextSlash <= protocolEnd + 1) { return false; } for (int x = protocolEnd + 1; x < nextSlash; x++) { if (uri.CharAt(x) < '0' || uri.CharAt(x) > '9') { return false; } } return true; }
/// <summary> Determines whether a string is not obviously not a URI. This implements crude checks; this class does not /// intend to strictly check URIs as its only function is to represent what is in a barcode, but, it does /// need to know when a string is obviously not a URI. /// </summary> internal static bool isBasicallyValidURI(String uri) { if (uri == null || uri.IndexOf(' ') >= 0 || uri.IndexOf('\n') >= 0) { return false; } // Look for period in a domain but followed by at least a two-char TLD // Forget strings that don't have a valid-looking protocol int period = uri.IndexOf('.'); if (period >= uri.Length - 2) { return false; } int colon = uri.IndexOf(':'); if (period < 0 && colon < 0) { return false; } if (colon >= 0) { if (period < 0 || period > colon) { // colon ends the protocol for (int i = 0; i < colon; i++) { char c = uri.CharAt(i); if ((CharExtend.ToInt32(c) < CharExtend.ToInt32('a') || CharExtend.ToInt32(c) > CharExtend.ToInt32('z')) && (CharExtend.ToInt32(c) < CharExtend.ToInt32('A') || CharExtend.ToInt32(c) > CharExtend.ToInt32('Z'))) { return false; } } } else { // colon starts the port; crudely look for at least two numbers if (colon >= uri.Length - 2) { return false; } for (int i = colon + 1; i < colon + 3; i++) { char c = uri.CharAt(i); if (CharExtend.ToInt32(c) < CharExtend.ToInt32('0') || CharExtend.ToInt32(c) > CharExtend.ToInt32('9')) { return false; } } } } return true; }
/// <summary> RFC 2445 allows the start and end fields to be of type DATE (e.g. 20081021) or DATE-TIME /// (e.g. 20081021T123000 for local time, or 20081021T123000Z for UTC). /// /// </summary> /// <param name="date">The string to validate /// </param> private static void validateDate(String date) { if (date != null) { int length = date.Length; if (length != 8 && length != 15 && length != 16) { throw new Exception("ArgumentException"); } for (int i = 0; i < 8; i++) { if (!CharExtend.IsDigit(date.CharAt(i))) { throw new Exception("ArgumentException"); } } if (length > 8) { if (date.CharAt(8) != 'T') { throw new Exception("ArgumentException"); } for (int i = 9; i < 15; i++) { if (!CharExtend.IsDigit(date.CharAt(i))) { throw new Exception("ArgumentException"); } } if (length == 16 && date.CharAt(15) != 'Z') { throw new Exception("ArgumentException"); } } } }
/// <summary> Expands a UPC-E value back into its full, equivalent UPC-A code value. /// /// </summary> /// <param name="upce">UPC-E code as string of digits /// </param> /// <returns> equivalent UPC-A code as string of digits /// </returns> public static String convertUPCEtoUPCA(String upce) { char[] upceChars = new char[6]; SupportClass.GetCharsFromString(upce, 1, 7, upceChars, 0); StringBuilder result = new StringBuilder(); result.Append(upce.CharAt(0)); char lastChar = upceChars[5]; switch (lastChar) { case '0': case '1': case '2': result.Append(upceChars.Extract(0, 2).Join()); result.Append(lastChar); result.Append("0000"); result.Append(upceChars.Extract(2, 3).Join()); break; case '3': result.Append(upceChars.Extract(0, 3).Join()); result.Append("00000"); result.Append(upceChars.Extract(3, 2).Join()); break; case '4': result.Append(upceChars.Extract(0, 4).Join()); result.Append("00000"); result.Append(upceChars[4]); break; default: result.Append(upceChars.Extract(0, 5).Join()); result.Append("0000"); result.Append(lastChar); break; } result.Append(upce.CharAt(7)); return result.ToString(); }
/// <summary> Add two numbers which are represented as strings. /// /// </summary> /// <param name="value1"> /// </param> /// <param name="value2"> /// </param> /// <returns> the result of value1 + value2 /// </returns> private static StringBuilder add(String value1, String value2) { StringBuilder temp1 = new StringBuilder(); StringBuilder temp2 = new StringBuilder(); //System.StringBuilder result = new System.StringBuilder(); char[] result = new char[value1.Length]; for (int i = 0; i < value1.Length; i++) { // Put zeros into the result. result[i] = '0'; } int carry = 0; for (int i = value1.Length - 3; i > - 1; i -= 3) { temp1.Clear(); temp1.Append(value1.CharAt(i)); temp1.Append(value1.CharAt(i + 1)); temp1.Append(value1.CharAt(i + 2)); temp2.Clear(); temp2.Append(value2.CharAt(i)); temp2.Append(value2.CharAt(i + 1)); temp2.Append(value2.CharAt(i + 2)); int intValue1 = Int32.Parse(temp1.ToString()); int intValue2 = Int32.Parse(temp2.ToString()); int sumval = (intValue1 + intValue2 + carry) % 1000; carry = Math.Floor((intValue1 + intValue2 + carry) / 1000); result[i + 2] = Int32Extend.ToChar((sumval % 10) + CharExtend.ToInt32('0')); result[i + 1] = Int32Extend.ToChar((Math.Floor(sumval / 10) % 10) + CharExtend.ToInt32('0')); result[i] = Int32Extend.ToChar(Math.Floor(sumval / 100) + CharExtend.ToInt32('0')); } return new StringBuilder(result.Join()); }
private static String decodeExtended(String encoded) { int length = encoded.Length; StringBuilder decoded = new StringBuilder(); for (int i = 0; i < length; i++) { char c = encoded.CharAt(i); if (c == '+' || c == '$' || c == '%' || c == '/') { char next = encoded.CharAt(i + 1); char decodedChar = '\x0000'; switch (c) { case '+': // +A to +Z map to a to z if (CharExtend.ToInt32(next) >= CharExtend.ToInt32('A') && CharExtend.ToInt32(next) <= CharExtend.ToInt32('Z')) { decodedChar = (char) (next + 32); } else { throw new Exception("ReaderException"); } break; case '$': // $A to $Z map to control codes SH to SB if (CharExtend.ToInt32(next) >= CharExtend.ToInt32('A') && CharExtend.ToInt32(next) <= CharExtend.ToInt32('Z')) { decodedChar = Int32Extend.ToChar(CharExtend.ToInt32(next) - 64); } else { throw new Exception("ReaderException"); } break; case '%': // %A to %E map to control codes ESC to US if (CharExtend.ToInt32(next) >= CharExtend.ToInt32('A') && CharExtend.ToInt32(next) <= CharExtend.ToInt32('E')) { decodedChar = Int32Extend.ToChar(CharExtend.ToInt32(next) - 38); } else if (CharExtend.ToInt32(next) >= CharExtend.ToInt32('F') && CharExtend.ToInt32(next) <= CharExtend.ToInt32('W')) { decodedChar = Int32Extend.ToChar(CharExtend.ToInt32(next) - 11); } else { throw new Exception("ReaderException"); } break; case '/': // /A to /O map to ! to , and /Z maps to : if (CharExtend.ToInt32(next) >= CharExtend.ToInt32('A') && CharExtend.ToInt32(next) <= CharExtend.ToInt32('O')) { decodedChar = Int32Extend.ToChar(CharExtend.ToInt32(next) - 32); } else if (next == 'Z') { decodedChar = ':'; } else { throw new Exception("ReaderException"); } break; } decoded.Append(decodedChar); // bump up i again since we read two characters i++; } else { decoded.Append(c); } } return decoded.ToString(); }