public static DreamValue NativeProc_Find(DreamObject instance, DreamObject usr, DreamProcArguments arguments) { DreamRegex dreamRegex = DreamMetaObjectRegex.ObjectToDreamRegex[instance]; DreamValue haystack = arguments.GetArgument(0, "haystack"); int next = GetNext(instance, arguments.GetArgument(1, "Start"), dreamRegex.IsGlobal); int end = arguments.GetArgument(2, "End").GetValueAsInteger(); instance.SetVariable("text", haystack); string haystackString; if (!haystack.TryGetValueAsString(out haystackString)) { haystackString = String.Empty; } if (end == 0) { end = haystackString.Length; } if (haystackString.Length == next - 1) { return(new DreamValue(0)); } Match match = dreamRegex.Regex.Match(haystackString, next - 1, end - next); if (match.Success) { instance.SetVariable("index", new DreamValue(match.Index + 1)); instance.SetVariable("match", new DreamValue(match.Value)); if (match.Groups.Count > 0) { DreamList groupList = new DreamList(); for (int i = 1; i < match.Groups.Count; i++) { groupList.AddValue(new DreamValue(match.Groups[i].Value)); } instance.SetVariable("group", new DreamValue(groupList)); } if (dreamRegex.IsGlobal) { instance.SetVariable("next", new DreamValue(match.Index + match.Length)); } return(new DreamValue(match.Index + 1)); } else { return(new DreamValue(0)); } }
public static DreamValue NativeProc_Replace(DreamObject instance, DreamObject usr, DreamProcArguments arguments) { DreamRegex dreamRegex = DreamMetaObjectRegex.ObjectToDreamRegex[instance]; if (!dreamRegex.IsGlobal) { throw new NotImplementedException("Non-global regex replaces are not implemented"); } DreamValue haystack = arguments.GetArgument(0, "haystack"); DreamValue replace = arguments.GetArgument(1, "replacement"); int start = arguments.GetArgument(2, "Start").GetValueAsInteger(); int end = arguments.GetArgument(3, "End").GetValueAsInteger(); string haystackString = haystack.GetValueAsString(); if (end == 0) { end = haystackString.Length; } if (replace.TryGetValueAsProc(out DreamProc replaceProc)) { throw new NotImplementedException("Proc replacements are not implemented"); } else if (replace.TryGetValueAsString(out string replaceString)) { string replaced = dreamRegex.Regex.Replace(haystackString, replaceString, end - start, start - 1); instance.SetVariable("text", new DreamValue(replaced)); return(new DreamValue(replaced)); } else { throw new ArgumentException("Replacement argument must be a string"); } }