public static String UnescapeString(String Value) { if (Value.Length < 2) throw(new Exception("Invalid String [1]")); if (Value[0] != '\'' && Value[0] != '"') throw (new Exception("Invalid String [2]")); if (Value.Substr(0, 1) != Value.Substr(-1, 1)) throw (new Exception("Invalid String [3]")); String RetString = ""; Value = Value.Substr(1, -1); for (int n = 0; n < Value.Length; n++) { if (Value[n] == '\\') { switch (Value[++n]) { case 'n': RetString += '\n'; break; case 'r': RetString += '\r'; break; case 't': RetString += '\t'; break; default: throw(new Exception("Unknown Escape Sequence")); } } else { RetString += Value[n]; } } return RetString; }
/// <returns> a byte array of horizontal pixels (0 = white, 1 = black) /// </returns> public override sbyte[] encode2(String contents) { if (contents.Length != 8) { throw new Exception("ArgumentException: Requested contents should be 8 digits long, but got " + contents.Length); } sbyte[] result = new sbyte[codeWidth]; int pos = 0; pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1); for (int i = 0; i <= 3; i++) { int digit = Int32.Parse(contents.Substr(i, (i + 1) - (i))); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 0); } pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, 0); for (int i = 4; i <= 7; i++) { int digit = Int32.Parse(contents.Substr(i, (i + 1) - (i))); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 1); } pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1); return result; }
public static int ParseIntegerConstant(String Value, int DefaultBase = 10) { try { Value = Value.Replace("_", ""); if (Value.Substr(0, 1) == "-") return -ParseIntegerConstant(Value.Substr(1)); if (Value.Substr(0, 1) == "+") return +ParseIntegerConstant(Value.Substr(1)); if (Value.Substr(0, 2) == "0x") return Convert.ToInt32(Value.Substr(2), 16); if (Value.Substr(0, 2) == "0b") return Convert.ToInt32(Value.Substr(2), 2); return Convert.ToInt32(Value, DefaultBase); } catch (FormatException FormatException) { throw (new FormatException("Can't parse the string '" + Value + "'", FormatException)); } }
public override sbyte[] encode2(String contents) { if (contents.Length != 13) { throw new Exception("ArgumentException: Requested contents should be 13 digits long, but got " + contents.Length); } int firstDigit = Int32.Parse(contents.Substr(0, (1) - (0))); int parities = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit]; sbyte[] result = new sbyte[codeWidth]; int pos = 0; pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1); // See {@link #EAN13Reader} for a description of how the first digit & left bars are encoded for (int i = 1; i <= 6; i++) { int digit = Int32.Parse(contents.Substr(i, (i + 1) - (i))); if ((parities >> (6 - i) & 1) == 1) { digit += 10; } pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], 0); } pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, 0); for (int i = 7; i <= 12; i++) { int digit = Int32.Parse(contents.Substr(i, (i + 1) - (i))); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], 1); } pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, 1); return result; }
/* static public IEnumerable<String> TokenizeRegex(String Line) { var Matches = new Regex(@"(\+\d+|-\d+|[%\w]+|\S)", RegexOptions.Compiled).Matches(Line); var Ret = new String[Matches.Count]; for (int n = 0; n < Matches.Count; n++) Ret[n] = Matches[n].Value; return Ret; } */ public static IEnumerable<String> TokenizeFast(String Line) { var Parts = new List<String>(); for (int n = 0; n < Line.Length; n++) { if (IsIdent(Line[n])) { int m = n; for (; n < Line.Length && IsIdent(Line[n]); n++) { } Parts.Add(Line.Substr(m, n - m)); n--; } else { if (!IsSpace(Line[n])) { Parts.Add("" + Line[n]); } } } return Parts; }
private static String urlDecode(String escaped) { // No we can't use java.net.URLDecoder here. JavaME doesn't have it. if (escaped == null) { return null; } char[] escapedArray = StringExtend.ToCharArray(escaped); int first = findFirstEscape(escapedArray); if (first < 0) { return escaped; } int max = escapedArray.Length; // final length is at most 2 less than original due to at least 1 unescaping StringBuilder unescaped = new StringBuilder(); // Can append everything up to first escape character unescaped.Append(escaped.Substr(0, first)); for (int i = first; i < max; i++) { char c = escapedArray[i]; if (c == '+') { // + is translated directly into a space unescaped.Append(' '); } else if (c == '%') { // Are there even two more chars? if not we will just copy the escaped sequence and be done if (i >= max - 2) { unescaped.Append('%'); // append that % and move on } else { int firstDigitValue = parseHexDigit(escapedArray[++i]); int secondDigitValue = parseHexDigit(escapedArray[++i]); if (firstDigitValue < 0 || secondDigitValue < 0) { // bad digit, just move on unescaped.Append('%'); unescaped.Append(escapedArray[i - 1]); unescaped.Append(escapedArray[i]); } unescaped.Append((char) ((firstDigitValue << 4) + secondDigitValue)); } } else { unescaped.Append(c); } } return unescaped.ToString(); }
private static void appendKeyValue(String uri, int paramStart, int paramEnd, Dictionary result) { //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 separator = uri.IndexOf('=', paramStart); if (separator >= 0) { // key = value String key = uri.Substr(paramStart, (separator) - (paramStart)); String value_Renamed = uri.Substr(separator + 1, (paramEnd) - (separator + 1)); value_Renamed = urlDecode(value_Renamed); result[key] = value_Renamed; } // Can't put key, null into a hashtable }
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); }
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 String parseName(String name) { int comma = name.IndexOf(','); if (comma >= 0) { // Format may be last,first; switch it around return name.Substr(comma + 1) + ' ' + name.Substr(0, (comma) - (0)); } return name; }