internal static object tr_trans(object str, Frame caller, Proc block, object src, object repl, bool sflag) { Tr trSrc, trRepl; bool cflag = false; int[] trans = new int[256]; int i, c; bool modify = false; int s, send; System.Text.StringBuilder t = new System.Text.StringBuilder(); string sString = StringValue(str, caller); string srcString = StringValue(src, caller); string replString = StringValue(repl, caller); if (((String)str).value == null || ((String)str).value.Length == 0) { return null; } trSrc = new Tr(srcString); if (srcString.Length >= 2 && srcString[0] == '^') { cflag = true; trSrc.MoveNext(); } if (replString.Length == 0) { return Ruby.Methods.rb_str_delete_bang.singleton.Call(null, str, caller, block, new Array(src)); } trRepl = new Tr(replString); if (cflag) { for (i = 0; i < 256; i++) { trans[i] = 1; } while (trSrc.HasMore()) { c = trSrc.Next(); trans[c] = -1; } while (trRepl.HasMore()) { /* retrieve last replacer */ c = trRepl.Next(); } for (i = 0; i < 256; i++) { if (trans[i] >= 0) { trans[i] = trRepl.Now; } } } else { int r; for (i = 0; i < 256; i++) { trans[i] = -1; } while (trSrc.HasMore()) { c = trSrc.Next(); r = trRepl.Next(); if (r == -1) { r = trRepl.Now; } trans[c] = r; } } s = 0; send = s + sString.Length; if (sflag) { int c0, last = -1; while (s < send) { c0 = sString[s++]; if ((c = trans[c0]) >= 0) { if (last == c) continue; last = c; t.Append((char)c); modify = true; } else { last = -1; t.Append((char)c0); } } } else { while (s < send) { if ((c = trans[(int)sString[s]]) >= 0) { t.Append((char)c); modify = true; } else { t.Append((char)sString[s]); } s++; } } if (modify) { ((String)str).value = t.ToString(); return str; } else { return null; } }
// tr_setup_table internal static void SetupTable(string str, char[] table, bool init) { char[] buf = new char[256]; Tr tr = new Tr(str); int i, c; int cflag = 0; if (str.Length > 1 && str[0] == '^') { cflag = 1; tr.MoveNext(); } if (init) { for (i = 0; i < 256; i++) { table[i] = (char)1; } } for (i = 0; i < 256; i++) { buf[i] = (char)cflag; } while (tr.HasMore()) { c = tr.Next(); if (cflag == 1) { buf[c] = (char)0; } else { buf[c] = (char)1; } } for (i = 0; i < 256; i++) { char test = (char)(table[i] & buf[i]); table[i] = (char)(table[i] & buf[i]); } }