public static P5Scalar BitAnd(Runtime runtime, P5Scalar res, P5Scalar a, P5Scalar b) { if (a.IsString(runtime) && b.IsString(runtime)) { string sa = a.AsString(runtime), sb = b.AsString(runtime); System.Text.StringBuilder t; if (sa.Length > sb.Length) { t = new System.Text.StringBuilder(sa); for (int i = 0; i < sb.Length; ++i) t[i] &= sb[i]; } else { t = new System.Text.StringBuilder(sb); for (int i = 0; i < sa.Length; ++i) t[i] &= sa[i]; } res.SetString(runtime, t.ToString()); } else { // TODO take into account signed/unsigned? res.SetInteger(runtime, a.AsInteger(runtime) & b.AsInteger(runtime)); } return res; }
public static int Transliterate(Runtime runtime, P5Scalar scalar, string match, string replacement, int flags) { bool complement = (flags & Opcode.FLAG_RX_COMPLEMENT) != 0; bool delete = (flags & Opcode.FLAG_RX_DELETE) != 0; bool squeeze = (flags & Opcode.FLAG_RX_SQUEEZE) != 0; var s = scalar.AsString(runtime); int count = 0, last_r = -1; var new_str = new System.Text.StringBuilder(); for (int i = 0; i < s.Length; ++i) { int idx = match.IndexOf(s[i]); int replace = s[i]; if (idx == -1 && complement) { if (delete) replace = -1; else if (replacement.Length > 0) replace = replacement[replacement.Length - 1]; if (last_r == replace && squeeze) replace = -1; else last_r = replace; count += 1; } else if (idx != -1 && !complement) { if (idx >= replacement.Length && delete) replace = -1; else if (idx >= replacement.Length) replace = replacement[replacement.Length - 1]; else if (idx < replacement.Length) replace = replacement[idx]; if (last_r == replace && squeeze) replace = -1; else last_r = replace; count += 1; } else last_r = -1; if (replace != -1) new_str.Append((char)replace); } scalar.SetString(runtime, new_str.ToString()); return count; }
public static void ReplaceSubstrings(Runtime runtime, P5Scalar value, string str, List<RxReplacement> substrings) { for (int i = substrings.Count - 1; i >= 0; --i) { // TODO optimize str = str.Substring(0, substrings[i].Start) + substrings[i].String + str.Substring(substrings[i].End); } value.SetString(runtime, str); }