/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> private static void Compile(Variant[] param, RegExpNI _this) { string expr = param[0].AsString(); int flags; if (param.Length >= 2) { string fs = param[1].AsString(); flags = RegExpNI.GetRegExpFlagsFromString(fs); } else { flags = RegExpNI.RegExpFlagToValue((char)0, 0); } if (expr.Length == 0) { expr = "(?:)"; } // generate empty regular expression try { int pflag = (flags & ~tjsflagsmask); if (pflag != 0) { _this.RegEx = Sharpen.Pattern.Compile(expr, pflag); } else { _this.RegEx = Sharpen.Pattern.Compile(expr); } } //catch (PatternSyntaxException e) //{ // _this.RegEx = null; // throw new TJSException(e.Message); //} catch (ArgumentException e) { _this.RegEx = null; throw new TJSException(e.Message); } _this.mFlags = flags; }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> private static Dispatch2 GetResultArray(bool matched, RegExpNI _this, Matcher m) { Dispatch2 array = TJS.CreateArrayObject(); if (matched) { if (_this.RegEx == null) { Variant val = new Variant(string.Empty); array.PropSetByNum(Interface.MEMBERENSURE | Interface.IGNOREPROP, 0, val, array); } else { if (m != null) { bool isMatch = m.Matches(); Variant val; if (isMatch) { val = new Variant(m.Group(0)); array.PropSetByNum(Interface.MEMBERENSURE | Interface.IGNOREPROP, 0, val, array); } int size = m.GroupCount(); for (int i = 0; i < size; i++) { val = new Variant(m.Group(i + 1)); array.PropSetByNum(Interface.MEMBERENSURE | Interface.IGNOREPROP, i + 1, val, array ); } } } } return array; }
private static bool Match(string target, RegExpNI _this) { if (_this.RegEx == null) { return false; } int targlen = target.Length; if (_this.mStart == targlen) { // Start already reached at end return _this.RegEx == null; } else { // returns true if empty if (_this.mStart > targlen) { // Start exceeds target's length return false; } } int searchstart = _this.mStart; _this.mMatch = _this.RegEx.Matcher(Sharpen.Runtime.Substring(target, searchstart) ); return _this.mMatch.Matches(); }
/// <exception cref="Kirikiri.Tjs2.VariantException"></exception> /// <exception cref="Kirikiri.Tjs2.TJSException"></exception> private static bool Exec(string target, RegExpNI _this) { bool matched = Match(target, _this); Dispatch2 array = GetResultArray(matched, _this, _this.mMatch); _this.mArray = new Variant(array, array); _this.mInput = target; if (!matched || _this.RegEx == null) { _this.mIndex = _this.mStart; _this.mLastIndex = _this.mStart; _this.mLastMatch = string.Empty; _this.mLastParen = string.Empty; _this.mLeftContext = Sharpen.Runtime.Substring(target, 0, _this.mStart); } else { _this.mIndex = _this.mStart + _this.mMatch.Start(); _this.mLastIndex = _this.mStart + _this.mMatch.End(); _this.mLastMatch = _this.mMatch.Group(0); _this.mLastParen = _this.mMatch.Group(_this.mMatch.GroupCount() - 1); _this.mLeftContext = Sharpen.Runtime.Substring(target, _this.mIndex); _this.mRightContext = Sharpen.Runtime.Substring(target, _this.mLastIndex); if ((_this.mFlags & globalsearch) != 0) { // global search flag changes the next search starting position. int match_end = _this.mLastIndex; _this.mStart = match_end; } } return matched; }