Ejemplo n.º 1
0
 private void WriteRegex(RubyRegex /*!*/ value)
 {
     WriteSubclassData(value, typeof(RubyRegex));
     _writer.Write((byte)'/');
     WriteStringValue(value.Pattern);
     _writer.Write((byte)value.Options);
 }
            public override Content /*!*/ EscapeRegularExpression()
            {
                // TODO:
                StringBuilder sb = RubyRegex.EscapeToStringBuilder(ToString());

                return((sb != null) ? new CharArrayContent(sb.ToString().ToCharArray(), _owner) : this);
            }
Ejemplo n.º 3
0
        public static MutableString /*!*/ ToS(RubyRegex /*!*/ self)
        {
            // Ruby: doesn't wrap if there is a single embedded expression that evaluates to non-nil:
            // puts(/#{nil}#{/a/}#{nil}/)
            // We don't do that.

            return(self.ToMutableString());
        }
Ejemplo n.º 4
0
            private void WriteRegex(RubyRegex /*!*/ value)
            {
                SubclassData instanceWriter = new SubclassData(this, value, typeof(RubyRegex));

                _writer.Write((byte)'/');
                WriteStringValue(value.GetPattern());
                _writer.Write((byte)value.Options);
            }
Ejemplo n.º 5
0
        public static bool CaseCompare(RubyContext /*!*/ context, RubyRegex /*!*/ self, object str)
        {
            MutableString asString = Protocols.AsString(context, str);

            if (asString == null)
            {
                return(false);
            }
            return(CaseCompare(context, self, asString));
        }
Ejemplo n.º 6
0
        public static MutableString /*!*/ Inspect(RubyRegex /*!*/ self)
        {
            MutableString result = MutableString.CreateMutable();

            result.Append('/');
            AppendEscapeForwardSlash(result, self.GetPattern());
            result.Append('/');
            AppendOptionString(result, self.Options, true, true);
            return(result);
        }
            public override Content /*!*/ EscapeRegularExpression()
            {
                // TODO:
                var a = ToByteArray();

                return(Create(
                           BinaryEncoding.Instance.GetBytes(RubyRegex.Escape(BinaryEncoding.Instance.GetString(a, 0, a.Length))),
                           _owner
                           ));
            }
Ejemplo n.º 8
0
        private static MutableString /*!*/ Append(RubyRegex /*!*/ self, MutableString /*!*/ result)
        {
            Assert.NotNull(self, result);

            result.Append("(?");
            if (AppendOptionString(result, self.Options, true, false) < 3)
            {
                result.Append('-');
            }
            AppendOptionString(result, self.Options, false, false);
            result.Append(':');
            AppendEscapeForwardSlash(result, self.GetPattern());
            result.Append(')');
            return(result);
        }
Ejemplo n.º 9
0
        public static MutableString GetKCode(RubyRegex /*!*/ self)
        {
            switch (self.Options & RubyRegexOptions.EncodingMask)
            {
            case RubyRegexOptions.NONE: return(null);

            case RubyRegexOptions.EUC: return(MutableString.CreateAscii("euc"));

            case RubyRegexOptions.FIXED: return(MutableString.CreateAscii("none"));

            case RubyRegexOptions.UTF8: return(MutableString.CreateAscii("utf8"));

            case RubyRegexOptions.SJIS: return(MutableString.CreateAscii("sjis"));

            default: throw Assert.Unreachable;
            }
        }
Ejemplo n.º 10
0
        public static string GetSubstring(RubyScope /*!*/ scope, string /*!*/ self, [NotNull] RubyRegex /*!*/ regex, [DefaultProtocol] int occurrance)
        {
            if (regex.IsEmpty)
            {
                return(String.Empty);
            }

            MatchData match = RegexpOps.Match(scope, regex, MutableString.Create(self, RubyEncoding.UTF8));

            if (match == null || !RegexpOps.NormalizeGroupIndex(ref occurrance, match.GroupCount))
            {
                return(null);
            }

            MutableString result = match.GetGroupValue(occurrance);

            return(result != null?result.ToString() : null);
        }
Ejemplo n.º 11
0
        public static RubyRegex /*!*/ Union(ConversionStorage <MutableString> /*!*/ stringCast, ConversionStorage <IList> /*!*/ toAry, RubyClass /*!*/ self, [NotNull] object /*!*/ obj)
        {
            IList list = Protocols.TryCastToArray(toAry, obj);

            if (list != null)
            {
                return(Union(stringCast, list));
            }

            // TODO: to_regexp
            RubyRegex regex = obj as RubyRegex;

            if (regex != null)
            {
                return(regex);
            }

            return(new RubyRegex(RubyRegex.Escape(Protocols.CastToString(stringCast, obj)), RubyRegexOptions.NONE));
        }
Ejemplo n.º 12
0
        internal protected StringRegex(string /*!*/ pattern, RubyRegexOptions options)
            : base(options)
        {
            Assert.NotNull(pattern);
            _pattern = pattern;

            string transformed = TransformPattern(pattern, options);

            try {
                _regex = new Regex(transformed, RubyRegex.ToClrOptions(options));
            } catch (ArgumentException e) {
                Utils.Log("-- original ---" + new String('-', 50), "REGEX_ERROR");
                Utils.Log(pattern, "REGEX_ERROR");
                Utils.Log("-- transformed " + new String('-', 50), "REGEX_ERROR");
                Utils.Log(transformed, "REGEX_ERROR");
                Utils.Log("---------------" + new String('-', 50), "REGEX_ERROR");
                throw new RegexpError(e.Message, e);
            }
        }
Ejemplo n.º 13
0
        public static string GetSubstring(RubyScope /*!*/ scope, string /*!*/ self, [NotNull] RubyRegex /*!*/ regex)
        {
            if (regex.IsEmpty)
            {
                return(String.Empty);
            }

            // TODO (opt): don't create a new mutable string:
            MatchData match = RegexpOps.Match(scope, regex, MutableString.Create(self, RubyEncoding.UTF8));

            if (match == null)
            {
                return(null);
            }

            var result = match.GetValue();

            return(result != null?result.ToString() : null);
        }
Ejemplo n.º 14
0
        private static RubyRegex /*!*/ Union(ConversionStorage <MutableString> /*!*/ stringCast, ICollection /*!*/ objs)
        {
            if (objs.Count == 0)
            {
                return(new RubyRegex(MutableString.CreateAscii("(?!)"), RubyRegexOptions.NONE));
            }

            MutableString result = MutableString.CreateMutable(RubyEncoding.Binary);
            int           i      = 0;

            foreach (var obj in objs)
            {
                if (i > 0)
                {
                    result.Append('|');
                }

                // TODO: to_regexp
                RubyRegex regex = obj as RubyRegex;
                if (regex != null)
                {
                    if (objs.Count == 1)
                    {
                        return(regex);
                    }

                    regex.AppendTo(result);
                }
                else
                {
                    result.Append(RubyRegex.Escape(Protocols.CastToString(stringCast, obj)));
                }

                i++;
            }

            return(new RubyRegex(result, RubyRegexOptions.NONE));
        }
Ejemplo n.º 15
0
        public static RubyRegex /*!*/ Union(ConversionStorage <MutableString> /*!*/ stringCast, RubyClass /*!*/ self, params object[] /*!*/ strings)
        {
            if (strings.Length == 0)
            {
                return(new RubyRegex(MutableString.CreateAscii("(?!)"), RubyRegexOptions.NONE));
            }

            MutableString result = MutableString.CreateMutable(RubyEncoding.Binary);

            for (int i = 0; i < strings.Length; i++)
            {
                if (i > 0)
                {
                    result.Append('|');
                }

                RubyRegex regex = strings[i] as RubyRegex;
                if (regex != null)
                {
                    if (strings.Length == 1)
                    {
                        return(regex);
                    }

                    regex.AppendTo(result);
                }
                else
                {
                    result.Append(RubyRegex.Escape(Protocols.CastToString(stringCast, strings[i])));
                }
            }

            // TODO:
            //RubyClass regexClass = RubyUtils.GetExecutionContext(context).GetClass(typeof(RubyRegex));
            //return NewCallSite3.Invoke(context, regexClass, result, null, null);
            return(new RubyRegex(result, RubyRegexOptions.NONE));
        }
Ejemplo n.º 16
0
 public static RubyRegex /*!*/ Create(RubyClass /*!*/ self,
                                      [NotNull] RubyRegex /*!*/ other, [DefaultParameterValue(null)] object options, [Optional] object encoding)
 {
     ReportParametersIgnoredWarning(self.Context, encoding);
     return(new RubyRegex(other));
 }
Ejemplo n.º 17
0
 public static RubyRegex /*!*/ Create(RubyClass /*!*/ self,
                                      [NotNull] RubyRegex /*!*/ other, int options, [Optional] object encoding)
 {
     return(Create(self, other, (object)options, encoding));
 }
Ejemplo n.º 18
0
 public static RubyRegex /*!*/ Create(RubyClass /*!*/ self,
                                      [NotNull] RubyRegex /*!*/ other)
 {
     return(new RubyRegex(other));
 }
Ejemplo n.º 19
0
 public static MutableString /*!*/ Escape(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ str)
 {
     return(RubyRegex.Escape(str).TaintBy(str));
 }
Ejemplo n.º 20
0
 public static MutableString /*!*/ Source(RubyRegex /*!*/ self)
 {
     return(self.Pattern.Clone());
 }
Ejemplo n.º 21
0
 public static object ImplicitMatch(ConversionStorage <MutableString> /*!*/ stringCast, RubyScope /*!*/ scope, RubyRegex /*!*/ self)
 {
     return(MatchIndex(scope, self, Protocols.CastToString(stringCast, scope.GetInnerMostClosureScope().LastInputLine)));
 }
Ejemplo n.º 22
0
 public static RubyRegex /*!*/ Reinitialize(RubyRegex /*!*/ self, [NotNull] RubyRegex /*!*/ other)
 {
     self.Set(other.Pattern, other.Options);
     return(self);
 }
Ejemplo n.º 23
0
            public override Content /*!*/ EscapeRegularExpression()
            {
                StringBuilder sb = RubyRegex.EscapeToStringBuilder(_data);

                return((sb != null) ? new StringContent(sb.ToString(), _owner) : this);
            }
Ejemplo n.º 24
0
 public static int GetHash(RubyRegex /*!*/ self)
 {
     return(self.GetHashCode());
 }
Ejemplo n.º 25
0
 public static bool Equals(RubyRegex /*!*/ self, object other)
 {
     return(false);
 }
Ejemplo n.º 26
0
        public static bool CaseCompare(ConversionStorage <MutableString> /*!*/ stringTryCast, RubyScope /*!*/ scope, RubyRegex /*!*/ self, object obj)
        {
            MutableString str = Protocols.TryCastToString(stringTryCast, obj);

            return(str != null && Match(scope, self, str) != null);
        }
Ejemplo n.º 27
0
 public static object Match(BinaryOpStorageWithScope /*!*/ storage, RubyScope /*!*/ scope, ClrName /*!*/ self, [NotNull] RubyRegex /*!*/ regex)
 {
     return(MutableStringOps.Match(storage, scope, GetRubyName(scope.RubyContext, self), regex));
 }
Ejemplo n.º 28
0
 public static bool Equals(RubyContext /*!*/ context, RubyRegex /*!*/ self, [NotNull] RubyRegex /*!*/ other)
 {
     return(self.Equals(other));
 }
Ejemplo n.º 29
0
            private object /*!*/ ReadUserClass()
            {
                object obj      = UnmarshalNewObject();
                bool   loaded   = false;
                int    typeFlag = _reader.ReadByte();

                switch (typeFlag)
                {
                case '"':
                    MutableString msc = (obj as MutableString);
                    if (msc != null)
                    {
                        msc.Replace(0, msc.Length, ReadString());
                        loaded = true;
                    }
                    break;

                case '/':
                    RubyRegex rsc = (obj as RubyRegex);
                    if (rsc != null)
                    {
                        RubyRegex regex = ReadRegex();
                        rsc.Set(regex.Pattern, regex.Options);
                        loaded = true;
                    }
                    break;

                case '[':
                    RubyArray asc = (obj as RubyArray);
                    if (asc != null)
                    {
                        asc.AddRange(ReadArray());
                        loaded = true;
                    }
                    break;

                case '{':
                case '}':
                    Hash hsc = (obj as Hash);
                    if (hsc != null)
                    {
                        Hash hash = ReadHash(typeFlag);
                        hsc.DefaultProc  = hash.DefaultProc;
                        hsc.DefaultValue = hash.DefaultValue;
                        foreach (var pair in hash)
                        {
                            hsc.Add(pair.Key, pair.Value);
                        }
                        loaded = true;
                    }
                    break;

                default:
                    break;
                }
                if (!loaded)
                {
                    throw RubyExceptions.CreateArgumentError("incompatible base type");
                }
                return(obj);
            }
Ejemplo n.º 30
0
        public static object MatchIndex(RubyScope /*!*/ scope, RubyRegex /*!*/ self, [DefaultProtocol] MutableString /*!*/ str)
        {
            MatchData match = RubyRegex.SetCurrentMatchData(scope, self, str);

            return((match != null) ? ScriptingRuntimeHelpers.Int32ToObject(match.Index) : null);
        }