Пример #1
0
        internal RubyMethodBody(MethodDeclaration/*!*/ ast, MSA.SymbolDocumentInfo document, RubyEncoding/*!*/ encoding) {
            Assert.NotNull(ast, encoding);

            _ast = ast;
            _document = document;
            _encoding = encoding;
        }
Пример #2
0
        public static RubyArray GetAllNames(RubyContext/*!*/ context, RubyEncoding/*!*/ self)
        {
            var result = new RubyArray();

            string name = self.Name;
            result.Add(MutableString.Create(name));

            foreach (var alias in RubyEncoding.Aliases) {
                if (StringComparer.OrdinalIgnoreCase.Equals(alias.Value, name)) {
                    result.Add(MutableString.CreateAscii(alias.Key));
                }
            }

            if (self == context.RubyOptions.LocaleEncoding) {
                result.Add(MutableString.CreateAscii("locale"));
            }

            if (self == context.DefaultExternalEncoding) {
                result.Add(MutableString.CreateAscii("external"));
            }

            if (self == context.GetPathEncoding()) {
                result.Add(MutableString.CreateAscii("filesystem"));
            }

            return result;
        }
Пример #3
0
        private MutableString /*!*/ ConsumeLine(RubyEncoding /*!*/ encoding, int lineLength, int consume, bool bufferResized)
        {
            Debug.Assert(consume >= lineLength);
            Debug.Assert(consume <= _bufferCount);

            MutableString line;

            if (bufferResized || _bufferStart == 0 && !Utils.IsSparse(lineLength, _buffer.Length))
            {
                Debug.Assert(_bufferStart == 0);
                line = new MutableString(_buffer, lineLength, encoding);

                if (_bufferCount > consume)
                {
                    var newBuffer = new byte[Math.Max(_defaultBufferSize, _bufferCount - consume)];
                    Buffer.BlockCopy(_buffer, consume, newBuffer, 0, _bufferCount - consume);
                    _buffer = newBuffer;
                }
                else
                {
                    _buffer = null;
                }

                // consume as if we kept the same buffer and then adjust start:
                ConsumeBuffered(consume);
                _bufferStart = 0;
            }
            else
            {
                line = MutableString.CreateBinary(encoding).Append(_buffer, _bufferStart, lineLength);
                ConsumeBuffered(consume);
            }
            return(line);
        }
Пример #4
0
        /// <summary>
        /// Start is a number of bytes if kcode is given, otherwise it's a number of characters.
        /// </summary>
        public MatchData Match(RubyEncoding kcode, MutableString /*!*/ input, int start, bool freezeInput)
        {
            string str;
            Regex  regex = Transform(ref kcode, input, start, out str);

            Match match;

            if (kcode != null)
            {
                if (str == null)
                {
                    return(null);
                }
                match = regex.Match(str, 0);
            }
            else
            {
                if (start < 0)
                {
                    start += str.Length;
                }
                if (start < 0 || start > str.Length)
                {
                    return(null);
                }
                match = regex.Match(str, start);
            }

            return(MatchData.Create(match, input, freezeInput, str, kcode, start));
        }
Пример #5
0
        private static RubyEncoding/*!*/ CreateKCoding(int codepage, RubyEncoding/*!*/ realEncoding) {
#if SILVERLIGHT
            return new RubyEncoding(new UTF8Encoding(false, false), new UTF8Encoding(false, true), realEncoding);
#else
            return new RubyEncoding(KCoding.Create(codepage, false), KCoding.Create(codepage, true), realEncoding);
#endif
        }
Пример #6
0
        private Regex /*!*/ TransformPattern(RubyEncoding encoding, RubyRegexOptions kc)
        {
            // We can reuse cached CLR regex if it was created for the same k-coding:
            if (_cachedRegex != null && kc == _cachedKCode)
            {
                return(_cachedRegex);
            }

            string pattern;

            if (kc != 0 || encoding == RubyEncoding.Binary)
            {
                pattern = _pattern.ToString(encoding.Encoding);
            }
            else
            {
                pattern = _pattern.ConvertToString();
            }

            Regex result;

            try {
                result = new Regex(RegexpTransformer.Transform(pattern, _options, out _hasGAnchor), ToClrOptions(_options));
            } catch (Exception e) {
                throw new RegexpError(e.Message);
            }

            _cachedKCode = kc;
            _cachedRegex = result;
            return(result);
        }
Пример #7
0
        public static RubyArray /*!*/ GetAllNames(RubyContext /*!*/ context, RubyEncoding /*!*/ self)
        {
            var result = new RubyArray();

            string name = self.Name;

            result.Add(MutableString.Create(name));

            foreach (var alias in RubyEncoding.Aliases)
            {
                if (StringComparer.OrdinalIgnoreCase.Equals(alias.Value, name))
                {
                    result.Add(MutableString.CreateAscii(alias.Key));
                }
            }

            if (self == context.RubyOptions.LocaleEncoding)
            {
                result.Add(MutableString.CreateAscii("locale"));
            }

            if (self == context.DefaultExternalEncoding)
            {
                result.Add(MutableString.CreateAscii("external"));
            }

            if (self == context.GetPathEncoding())
            {
                result.Add(MutableString.CreateAscii("filesystem"));
            }

            return(result);
        }
Пример #8
0
        public MutableString ReadLine(MutableString /*!*/ separator, RubyEncoding /*!*/ encoding, bool preserveEndOfLines)
        {
            int b = ReadByteNormalizeEoln(preserveEndOfLines);

            if (b == -1)
            {
                return(null);
            }

            int           separatorOffset = 0;
            int           separatorLength = separator.GetByteCount();
            MutableString result          = MutableString.CreateBinary(encoding);

            do
            {
                result.Append((byte)b);

                if (b == separator.GetByte(separatorOffset))
                {
                    if (separatorOffset == separatorLength - 1)
                    {
                        break;
                    }
                    separatorOffset++;
                }
                else if (separatorOffset > 0)
                {
                    separatorOffset = 0;
                }

                b = ReadByteNormalizeEoln(preserveEndOfLines);
            } while (b != -1);

            return(result);
        }
Пример #9
0
        public MutableString ReadLineOrParagraph(MutableString separator, RubyEncoding /*!*/ encoding, bool preserveEndOfLines, int limit)
        {
            ContractUtils.Requires(limit >= 0);

            if (limit == 0)
            {
                return(MutableString.CreateEmpty());
            }
            else if (separator == null)
            {
                var result = MutableString.CreateBinary();
                return(AppendBytes(result, limit, preserveEndOfLines) == 0 ? null : result);
            }
            else if (separator.StartsWith('\n') && separator.GetLength() == 1)
            {
                return(ReadLine(encoding, preserveEndOfLines, limit));
            }
            else if (separator.IsEmpty)
            {
                return(ReadParagraph(encoding, preserveEndOfLines, limit));
            }
            else
            {
                return(ReadLine(separator, encoding, preserveEndOfLines, limit));
            }
        }
Пример #10
0
        private Regex /*!*/ Transform(ref RubyEncoding encoding, MutableString /*!*/ input, int start, out string strInput)
        {
            ContractUtils.RequiresNotNull(input, "input");

            // K-coding of the current operation (the current KCODE gets preference over the KCODE regex option):
            RubyRegexOptions kc = _options & RubyRegexOptions.EncodingMask;

            if (kc != 0)
            {
                encoding = _pattern.Encoding;
            }
            else
            {
                kc = RubyEncoding.ToRegexOption(encoding);
            }

            // Convert input to a string. Force k-coding if necessary.
            if (kc != 0)
            {
                // Handling multi-byte K-coded characters is not entirely correct here.
                // Three cases to be considered:
                // 1) Multi-byte character is explicitly contained in the pattern: /�*/
                // 2) Subsequent escapes form a complete character: /\342\202\254*/ or /\xe2\x82\xac*/
                // 3) Subsequent escapes form an incomplete character: /[\x7f-\xff]{1,3}/
                //
                // In the first two cases we want to "group" the byte triplet so that regex operators like *, +, ? and {n,m} operate on
                // the entire character, not just the last byte. We could unescape the bytes and replace them with complete Unicode characters.
                // Then we could encode the input using the same K-coding and we would get a match.
                // However, case 3) requires the opposite: to match the bytes we need to encode the input using binary encoding.
                // Using this encoding makes *+? operators operate on the last byte (encoded as UTF16 character).
                //
                // The right solution would require the regex engine to handle multi-byte escaped characters, which it doesn't.
                //
                // TODO:
                // A correct workaround would be to wrap the byte sequence that forms a character into a non-capturing group,
                // for example transform /\342\202\254*/ to /(?:\342\202\254)*/ and use binary encoding on both input and pattern.
                // For now, we just detect if there are any non-ascii character escapes. If so we use a binary encoding accomodating case 3),
                // but breaking cases 1 and 2. Otherwise we encode using k-coding to make case 1 match.
                if (HasEscapedNonAsciiBytes(_pattern))
                {
                    encoding = RubyEncoding.Binary;
                    kc       = 0;
                }

                strInput = ForceEncoding(input, encoding.Encoding, start);
            }
            else if (input.Encoding.IsKCoding)
            {
                strInput = input.ToString(BinaryEncoding.Instance);
            }
            else
            {
                _pattern.RequireCompatibleEncoding(input);
                input.PrepareForCharacterRead();
                strInput = input.ConvertToString();
            }

            return(TransformPattern(encoding, kc));
        }
Пример #11
0
 public MutableString/*!*/ GetMutableString(RubyEncoding/*!*/ encoding) {
     string str = _value as string;
     if (str != null) {
         return MutableString.Create(str, encoding);
     } else {
         return MutableString.CreateBinary((byte[])_value, encoding);
     }
 }
Пример #12
0
 internal StringLiteral(object/*!*/ value, RubyEncoding/*!*/ encoding, SourceSpan location)
     : base(location)
 {
     Debug.Assert(value is string || value is byte[]);
     Debug.Assert(encoding != null);
     _value = value;
     _encoding = encoding;
 }
Пример #13
0
        public RubyIO(RubyContext/*!*/ context) {
            ContractUtils.RequiresNotNull(context, "context");

            _context = context;
            _fileDescriptor = -1;
            _stream = null;
            _externalEncoding = context.DefaultExternalEncoding;
            _internalEncoding = context.DefaultInternalEncoding;
        }
Пример #14
0
        internal static MutableString /*!*/ ToChr(RubyEncoding /*!*/ encoding, RubyEncoding /*!*/ resultEncoding, int codepoint)
        {
            if (codepoint < 0)
            {
                throw RubyExceptions.CreateRangeError("{0} out of char range", codepoint);
            }

            switch (encoding.CodePage)
            {
            case RubyEncoding.CodePageUTF7:
            case RubyEncoding.CodePageUTF8:
            case RubyEncoding.CodePageUTF16BE:
            case RubyEncoding.CodePageUTF16LE:
            case RubyEncoding.CodePageUTF32BE:
            case RubyEncoding.CodePageUTF32LE:
                if (codepoint > 0x10ffff)
                {
                    throw RubyExceptions.CreateRangeError("{0} is not a valid Unicode code point (0..0x10ffff)", codepoint);
                }
                return(MutableString.CreateMutable(Tokenizer.UnicodeCodePointToString(codepoint), resultEncoding));

            case RubyEncoding.CodePageSJIS:
                if (codepoint >= 0x81 && codepoint <= 0x9f || codepoint >= 0xe0 && codepoint <= 0xfc)
                {
                    throw RubyExceptions.CreateArgumentError("invalid codepoint 0x{0:x2} in Shift_JIS", codepoint);
                }
                goto default;

            case RubyEncoding.CodePageEUCJP:
                // MRI's bahavior is strange - bug?
                if (codepoint >= 0x80)
                {
                    throw RubyExceptions.CreateRangeError("{0} out of char range", codepoint);
                }
                goto default;

            default:
                if (codepoint <= 0xff)
                {
                    return(MutableString.CreateBinary(new[] { (byte)codepoint }, resultEncoding));
                }
                if (encoding.IsDoubleByteCharacterSet)
                {
                    if (codepoint <= 0xffff)
                    {
                        return(MutableString.CreateBinary(new[] { (byte)(codepoint >> 8), (byte)(codepoint & 0xff) }, resultEncoding));
                    }
                    throw RubyExceptions.CreateRangeError("{0} out of char range", codepoint);
                }
                if (encoding.IsSingleByteCharacterSet)
                {
                    throw RubyExceptions.CreateRangeError("{0} out of char range", codepoint);
                }
                throw new NotSupportedException(RubyExceptions.FormatMessage("Encoding {0} code points not supported", encoding));
            }
        }
Пример #15
0
 public static MutableString/*!*/ Inspect(RubyContext/*!*/ context, RubyEncoding/*!*/ self) {
     // TODO: to_s overridden
     MutableString result = MutableString.CreateMutable(context.GetIdentifierEncoding());
     result.Append("#<");
     result.Append(context.GetClassDisplayName(self));
     result.Append(':');
     result.Append(self.Name);
     result.Append('>');
     return result;
 }
Пример #16
0
        public RubyIO(RubyContext /*!*/ context)
        {
            ContractUtils.RequiresNotNull(context, "context");

            _context          = context;
            _fileDescriptor   = -1;
            _stream           = null;
            _externalEncoding = context.DefaultExternalEncoding;
            _internalEncoding = context.DefaultInternalEncoding;
        }
Пример #17
0
 public static MutableString/*!*/ Inspect(RubyContext/*!*/ context, RubyEncoding/*!*/ self) {
     // TODO: to_s overridden
     MutableString result = MutableString.CreateMutable();
     result.Append("#<");
     result.Append(RubyUtils.GetClassName(context, self));
     result.Append(':');
     result.Append(self.Name);
     result.Append(">");
     return result;
 }
Пример #18
0
        // TODO: remove
        internal StringFormatter(RubyContext /*!*/ context, string /*!*/ format, RubyEncoding /*!*/ encoding, IList /*!*/ data)
        {
            Assert.NotNull(context, format, data, encoding);

            _context = context;
            _format  = format;
            _data    = data;

            // TODO (encoding):
            _encoding = encoding;
        }
Пример #19
0
        public RubyIO(RubyContext/*!*/ context) {
            ContractUtils.RequiresNotNull(context, "context");

            _context = context;
            _fileDescriptor = -1;
            _stream = null;

            // TODO (encoding): enable setting
            _externalEncoding = RubyEncoding.Binary;
            _internalEncoding = null;
        }
Пример #20
0
        public static RubyArray /*!*/ GetNameList(RubyClass /*!*/ self)
        {
            var result = new RubyArray();

            foreach (string name in RubyEncoding.GetEncodingNames())
            {
                result.Add(MutableString.Create(name));
            }

            return(result);
        }
Пример #21
0
 private RubyEncoding(Encoding /*!*/ encoding, Encoding /*!*/ strictEncoding, RubyEncoding realEncoding, int ordinal)
 {
     Assert.NotNull(encoding, strictEncoding);
     Debug.Assert(encoding is KCoding == strictEncoding is KCoding);
     _isKCoding       = encoding is KCoding;
     _ordinal         = ordinal;
     _encoding        = encoding;
     _strictEncoding  = strictEncoding;
     _realEncoding    = realEncoding ?? this;
     _maxBytesPerChar = strictEncoding.GetMaxByteCount(1);
 }
Пример #22
0
        public static RubyArray /*!*/ GetAvailableEncodings(RubyClass /*!*/ self)
        {
            var result = new RubyArray();

            foreach (int codePage in RubyEncoding.GetEncodingCodePages())
            {
                result.Add(RubyEncoding.GetRubyEncoding(codePage));
            }

            return(result);
        }
Пример #23
0
        public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyEncoding /*!*/ self)
        {
            // TODO: to_s overridden
            MutableString result = MutableString.CreateMutable(context.GetIdentifierEncoding());

            result.Append("#<");
            result.Append(context.GetClassDisplayName(self));
            result.Append(':');
            result.Append(self.Name);
            result.Append('>');
            return(result);
        }
Пример #24
0
        public MutableString ReadLine(RubyEncoding /*!*/ encoding, bool preserveEndOfLines, int limit)
        {
            // TODO: limit

            if (_bufferCount == 0)
            {
                if (LoadBuffer(_defaultBufferSize) == 0)
                {
                    return(null);
                }
            }

            bool bufferResized = false;
            int  lf            = Array.IndexOf(_buffer, LF, _bufferStart, _bufferCount);

            while (lf < 0)
            {
                int s = _bufferCount;
                LoadBuffer(_buffer.Length - _bufferCount);
                Debug.Assert(_bufferStart == 0);

                lf = Array.IndexOf(_buffer, LF, s, _bufferCount - s);
                if (lf >= 0)
                {
                    break;
                }

                // end of stream:
                if (_bufferCount < _buffer.Length)
                {
                    return(ConsumeLine(encoding, _bufferCount, _bufferCount, bufferResized));
                }

                Array.Resize(ref _buffer, _buffer.Length << 1);
                bufferResized = true;
                _bufferStart  = 0;
            }

            int lineLength;
            int consume = lf + 1 - _bufferStart;

            if (!preserveEndOfLines && lf - 1 >= _bufferStart && _buffer[lf - 1] == CR)
            {
                _buffer[lf - 1] = LF;
                lineLength      = consume - 1;
            }
            else
            {
                lineLength = consume;
            }

            return(ConsumeLine(encoding, lineLength, consume, bufferResized));
        }
Пример #25
0
        public RubyIO(RubyContext /*!*/ context)
        {
            ContractUtils.RequiresNotNull(context, "context");

            _context        = context;
            _fileDescriptor = -1;
            _stream         = null;

            // TODO (encoding): enable setting
            _externalEncoding = RubyEncoding.Binary;
            _internalEncoding = null;
        }
Пример #26
0
        public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyEncoding /*!*/ self)
        {
            // TODO: to_s overridden
            MutableString result = MutableString.CreateMutable();

            result.Append("#<");
            result.Append(RubyUtils.GetClassName(context, self));
            result.Append(':');
            result.Append(self.Name);
            result.Append(">");
            return(result);
        }
Пример #27
0
        /// <summary>
        /// Finds the last match whose index is less than or equal to "start".
        /// Captures are ordered in the same way as with forward match. This is different from .NET reverse matching.
        /// Start is a number of bytes if kcode is given, otherwise it's a number of characters.
        /// </summary>
        public MatchData LastMatch(MutableString /*!*/ input, int start)
        {
            string       str;
            RubyEncoding kcode = null;
            Regex        regex = Transform(ref kcode, input, 0, out str);

            Debug.Assert(str != null);

            if (kcode != null)
            {
                int    byteCount;
                byte[] bytes = input.GetByteArray(out byteCount);

                if (start < 0)
                {
                    start += byteCount;
                }

                // GetCharCount returns the number of whole characters:
                start = (start >= byteCount) ? str.Length : kcode.Encoding.GetCharCount(bytes, 0, start + 1) - 1;
            }
            else
            {
                if (start < 0)
                {
                    start += str.Length;
                }

                if (start > str.Length)
                {
                    start = str.Length;
                }
            }

            Match match;

            if (_hasGAnchor)
            {
                // This only makes some \G anchors work. It seems that CLR doesn't support \G if preceeded by some characters.
                // For example, this works in MRI but doesn't in CLR: "abcabczzz".rindex(/.+\G.+/, 3)
                match = regex.Match(str, start);
            }
            else
            {
                match = LastMatch(regex, str, start);
                if (match == null)
                {
                    return(null);
                }
            }
            return(MatchData.Create(match, input, true, str));
        }
Пример #28
0
        public RubyConstructor(RubyGlobalScope/*!*/ scope, NodeProvider/*!*/ nodeProvider)
            : base(nodeProvider, scope) {

            _encoding = RubyEncoding.GetRubyEncoding(nodeProvider.Encoding);

            _newSite = CallSite<Func<CallSite, RubyModule, object, object, object, object>>.Create(
                RubyCallAction.Make(scope.Context, "new", RubyCallSignature.WithImplicitSelf(3))
            ); 

            _yamlInitializeSite = CallSite<Func<CallSite, object, object, Hash, object>>.Create(
                RubyCallAction.Make(scope.Context, "yaml_initialize", RubyCallSignature.WithImplicitSelf(3))
            );
        }
Пример #29
0
        public static RubyArray /*!*/ GetAvailableEncodings(RubyClass /*!*/ self)
        {
            var infos  = Encoding.GetEncodings();
            var result = new RubyArray(1 + infos.Length);

            // Ruby specific:
            result.Add(RubyEncoding.Binary);

            foreach (var info in infos)
            {
                result.Add(RubyEncoding.GetRubyEncoding(info.CodePage));
            }
            return(result);
        }
Пример #30
0
        internal RubyInputProvider(RubyContext/*!*/ context, ICollection<string>/*!*/ arguments, RubyEncoding/*!*/ encoding) {
            Assert.NotNull(context, encoding);
            Assert.NotNullItems(arguments);
            _context = context;

            var args = new RubyArray();
            foreach (var arg in arguments) {
                ExpandArgument(args, arg, encoding);
            }

            _commandLineArguments = args;
            _lastInputLineNumber = 1;
            _singleton = new object();
        }
Пример #31
0
        private MSA.Expression _sourcePathConstant; // lazy

        internal AstGenerator(RubyContext/*!*/ context, RubyCompilerOptions/*!*/ options, MSA.SymbolDocumentInfo document, RubyEncoding/*!*/ encoding,
            bool printInteractiveResult) {

            Assert.NotNull(context, options, encoding);
            _context = context;
            _compilerOptions = options;
            _debugCompiler = Snippets.Shared.SaveSnippets;
            _debugMode = context.DomainManager.Configuration.DebugMode;
            _traceEnabled = context.RubyOptions.EnableTracing;
            _document = document;
            _encoding = encoding;
            _profiler = context.RubyOptions.Profile ? Profiler.Instance : null;
            _savingToDisk = context.RubyOptions.SavePath != null;
            _printInteractiveResult = printInteractiveResult;
        }
Пример #32
0
        internal AstGenerator(RubyCompilerOptions/*!*/ options, SourceUnit/*!*/ sourceUnit, RubyEncoding/*!*/ encoding,
            bool debugCompiler, bool debugMode, bool traceEnabled, bool profilerEnabled, bool savingToDisk) {

            Assert.NotNull(options, encoding, sourceUnit);
            _context = (RubyContext)sourceUnit.LanguageContext;
            _compilerOptions = options;
            _debugCompiler = debugCompiler;
            _debugMode = debugMode;
            _traceEnabled = traceEnabled;
            _sourceUnit = sourceUnit;
            _document = sourceUnit.Document;
            _encoding = encoding;
            _profiler = profilerEnabled ? Profiler.Instance : null;
            _savingToDisk = savingToDisk;
        }
Пример #33
0
        private void ExpandArgument(RubyArray/*!*/ args, string/*!*/ arg, RubyEncoding/*!*/ encoding) {
            if (arg.IndexOf('*') != -1 || arg.IndexOf('?') != -1) {
                bool added = false;
                foreach (string path in Glob.GlobResults(_context.DomainManager.Platform, arg, 0)) {
                    args.Add(MutableString.Create(path, encoding));
                    added = true;
                }

                if (!added) {
                    args.Add(MutableString.Create(arg, encoding));
                }
            } else {
                args.Add(MutableString.Create(arg, encoding));
            }
        }
Пример #34
0
        public static RubyArray /*!*/ GetAvailableEncodings(RubyClass /*!*/ self)
        {
            // TODO: loads all encodings, we should be lazy with encoding creation

            var infos  = Encoding.GetEncodings();
            var result = new RubyArray(1 + infos.Length);

            // Ruby specific:
            result.Add(RubyEncoding.Binary);

            foreach (var info in infos)
            {
                result.Add(RubyEncoding.GetRubyEncoding(info.GetEncoding()));
            }
            return(result);
        }
Пример #35
0
            private void WriteSymbol(string /*!*/ value, RubyEncoding /*!*/ encoding)
            {
                int position;

                if (_symbols.TryGetValue(value, out position))
                {
                    _writer.Write((byte)';');
                    WriteInt32(position);
                }
                else
                {
                    position        = _symbols.Count;
                    _symbols[value] = position;
                    _writer.Write((byte)':');
                    WriteStringValue(value, encoding);
                }
            }
Пример #36
0
        public MutableString ReadParagraph(RubyEncoding /*!*/ encoding, bool preserveEndOfLines)
        {
            var result = ReadLine(MutableString.CreateAscii("\n\n"), encoding, preserveEndOfLines);

            int c;

            while ((c = PeekByteNormalizeEoln(preserveEndOfLines)) != -1)
            {
                if (c != '\n')
                {
                    break;
                }
                ReadByteNormalizeEoln(preserveEndOfLines);
            }

            return(result);
        }
Пример #37
0
        public static RubyIO /*!*/ SetEncodings(ConversionStorage <IDictionary <object, object> > /*!*/ toHash, ConversionStorage <MutableString> /*!*/ toStr,
                                                RubyIO /*!*/ self, object external, [Optional] object @internal, [Optional] IDictionary <object, object> options)
        {
            TryConvertToOptions(toHash, ref options, ref external, ref @internal);

            // TODO: options

            RubyEncoding externalEncoding = null, internalEncoding = null;

            if (external != Missing.Value)
            {
                externalEncoding = ToEncoding(toStr, external);
            }
            if (@internal != Missing.Value)
            {
                internalEncoding = ToEncoding(toStr, @internal);
            }
            return(SetEncodings(self, externalEncoding, internalEncoding));
        }
Пример #38
0
        /// <summary>
        /// Returns a collection of fresh MatchData objects.
        /// </summary>
        public IList <MatchData> /*!*/ Matches(RubyEncoding kcode, MutableString /*!*/ input, bool inputMayMutate)
        {
            string          str;
            MatchCollection matches = Transform(ref kcode, input, 0, out str).Matches(str);

            var result = new MatchData[matches.Count];

            if (result.Length > 0 && inputMayMutate)
            {
                // clone and freeze the string once so that it can be shared by all the MatchData objects
                input = input.Clone().Freeze();
            }

            for (int i = 0; i < result.Length; i++)
            {
                result[i] = MatchData.Create(matches[i], input, false, str, kcode, 0);
            }

            return(result);
        }
Пример #39
0
        public RubyOptions(IDictionary<string, object>/*!*/ options)
            : base(options)
        {
            _arguments = GetStringCollectionOption(options, "Arguments") ?? EmptyStringCollection;
            _localeEncoding = GetOption(options, "LocaleEncoding", RubyEncoding.UTF8);
            _defaultEncoding = GetOption<RubyEncoding>(options, "DefaultEncoding", null);

            _mainFile = GetOption(options, "MainFile", (string)null);
            _verbosity = GetOption(options, "Verbosity", 1);
            _debugVariable = GetOption(options, "DebugVariable", false);
            _enableTracing = GetOption(options, "EnableTracing", false);
            _savePath = GetOption(options, "SavePath", (string)null);
            _loadFromDisk = GetOption(options, "LoadFromDisk", false);
            _profile = GetOption(options, "Profile", false);
            _noAssemblyResolveHook = GetOption(options, "NoAssemblyResolveHook", false);
            _requirePaths = GetStringCollectionOption(options, "RequiredPaths", ';', ',');
            _hasSearchPaths = GetOption<object>(options, "SearchPaths", null) != null;
            _standardLibraryPath = GetOption(options, "StandardLibrary", (string)null);
            _applicationBase = GetOption(options, "ApplicationBase", (string)null);
        }
Пример #40
0
 public MutableString ReadLineOrParagraph(MutableString separator, RubyEncoding /*!*/ encoding, bool preserveEndOfLines)
 {
     if (separator == null)
     {
         var result = MutableString.CreateBinary();
         return(AppendBytes(result, Int32.MaxValue, preserveEndOfLines) == 0 ? null : result);
     }
     else if (separator.StartsWith('\n') && separator.GetLength() == 1)
     {
         return(ReadLine(encoding, preserveEndOfLines));
     }
     else if (separator.IsEmpty)
     {
         return(ReadParagraph(encoding, preserveEndOfLines));
     }
     else
     {
         return(ReadLine(separator, encoding, preserveEndOfLines));
     }
 }
Пример #41
0
        internal AstGenerator(RubyContext/*!*/ context, RubyCompilerOptions/*!*/ options, MSA.SymbolDocumentInfo document, RubyEncoding/*!*/ encoding,
            bool printInteractiveResult) {

            Assert.NotNull(context, options, encoding);
            _context = context;
            _compilerOptions = options;
            _debugMode = context.DomainManager.Configuration.DebugMode;
            _traceEnabled = context.RubyOptions.EnableTracing;
            _document = document;
            _sequencePointClearance = (document != null) ? Ast.ClearDebugInfo(document) : null;
            _encoding = encoding;
            _encodingConstant = Ast.Constant(encoding);
            _profiler = context.RubyOptions.Profile ? Profiler.Instance : null;
            _savingToDisk = context.RubyOptions.SavePath != null;
            _printInteractiveResult = printInteractiveResult;
#if SILVERLIGHT
            _debugCompiler = false;
#else
            _debugCompiler = Snippets.Shared.SaveSnippets;
#endif
        }
Пример #42
0
        public void Set(MutableString /*!*/ pattern, RubyRegexOptions options)
        {
            ContractUtils.RequiresNotNull(pattern, "pattern");

            // RubyRegexOptions.Once is only used to determine how the Regexp object should be created and cached.
            // It is not a property of the final object. /foo/ should compare equal with /foo/o.
            _options = options & ~RubyRegexOptions.Once;

            RubyEncoding encoding = RubyEncoding.GetRegexEncoding(options);

            if (encoding != null || pattern.Encoding.IsKCoding)
            {
                _pattern = MutableString.CreateBinary(pattern.ToByteArray(), encoding ?? RubyEncoding.Binary).Freeze();
            }
            else
            {
                _pattern = pattern.PrepareForCharacterRead().Clone().Freeze();
            }

            TransformPattern(encoding, options & RubyRegexOptions.EncodingMask);
        }
Пример #43
0
        private void ExpandArgument(RubyArray /*!*/ args, string /*!*/ arg, RubyEncoding /*!*/ encoding)
        {
            if (arg.IndexOf('*') != -1 || arg.IndexOf('?') != -1)
            {
                bool added = false;
                foreach (string path in Glob.GetMatches(_context.DomainManager.Platform, arg, 0))
                {
                    args.Add(MutableString.Create(path, encoding));
                    added = true;
                }

                if (!added)
                {
                    args.Add(MutableString.Create(arg, encoding));
                }
            }
            else
            {
                args.Add(MutableString.Create(arg, encoding));
            }
        }
Пример #44
0
        /// <summary>
        /// Returns Ruby names of all available encodings including their aliases.
        /// </summary>
        public static IEnumerable <string> GetEncodingNames()
        {
            // Ruby specific:
            yield return(RubyEncoding.Binary.Name);

            foreach (var info in Encoding.GetEncodings())
            {
                yield return(RubyEncoding.GetRubySpecificName(info.CodePage) ?? info.Name);
            }

            foreach (var alias in RubyEncoding.Aliases.Keys)
            {
                yield return(alias);
            }

            yield return("locale");

            yield return("external");

            yield return("filesystem");
        }
Пример #45
0
        internal static RubyEncoding /*!*/ GetRubyEncoding(int codepage)
        {
            switch (codepage)
            {
            case CodePageBinary: return(Binary);

            case CodePageUTF8: return(UTF8);
            }

            if (_Encodings == null)
            {
                Interlocked.CompareExchange(ref _Encodings, new Dictionary <int, RubyEncoding>(), null);
            }

            RubyEncoding result;

            lock (_Encodings) {
                if (!_Encodings.TryGetValue(codepage, out result))
                {
                    result = new RubyEncoding(
                        CreateEncoding(codepage, false),
                        CreateEncoding(codepage, true),
                        null,
                        codepage
                        );

                    // Some MutableString operations (GetHashCode, SetChar, etc.) assume that the encoding maps each and every
                    // character \u0000..\u007f to a corresponding byte 0..0x7f and back.
                    if (!IsAsciiIdentity(result.Encoding))
                    {
                        throw new NotSupportedException(String.Format("Encoding code page {0} is not supported", codepage));
                    }

                    _Encodings.Add(codepage, result);
                }
            }

            return(result);
        }
Пример #46
0
        public static RubyEncoding /*!*/ GetRubyEncoding(int codepage)
        {
            switch (codepage)
            {
            case CodePageBinary: return(Binary);

            case CodePageAscii: return(Ascii);

            case CodePageUTF8: return(UTF8);

#if FEATURE_ENCODING
            case CodePageSJIS: return(SJIS);

            case CodePageEUCJP: return(EUCJP);
#endif
            }

            if (_Encodings == null)
            {
                Interlocked.CompareExchange(ref _Encodings, new Dictionary <int, RubyEncoding>(), null);
            }

            RubyEncoding result;
            lock (_Encodings) {
                if (!_Encodings.TryGetValue(codepage, out result))
                {
                    result = new RubyEncoding(
                        CreateEncoding(codepage, false),
                        CreateEncoding(codepage, true),
                        codepage
                        );

                    _Encodings.Add(codepage, result);
                }
            }

            return(result);
        }
Пример #47
0
 public static Exception/*!*/ CreateArgumentError(DecoderFallbackException/*!*/ e, RubyEncoding/*!*/ encoding) {
     return RubyExceptions.CreateArgumentError(String.Format("invalid byte sequence {0} in {1}",
         BitConverter.ToString(e.BytesUnknown), encoding));
 }
Пример #48
0
 public static Exception/*!*/ CreateArgumentError(EncoderFallbackException/*!*/ e, RubyEncoding/*!*/ encoding) {
     return RubyExceptions.CreateArgumentError(String.Format("character U+{0:X4} can't be encoded in {1}",
         e.CharUnknownHigh != '\0' ? Tokenizer.ToCodePoint(e.CharUnknownHigh, e.CharUnknownLow) : (int)e.CharUnknown, encoding));
 }
Пример #49
0
 public static bool IsDummy(RubyEncoding/*!*/ self) {
     return false;
 }
Пример #50
0
 public static RubyEncoding BasedEncoding(RubyEncoding/*!*/ self) {
     return null;
 }
Пример #51
0
 public IOInfo(IOMode? mode, RubyEncoding externalEncoding, RubyEncoding internalEncoding) {
     _mode = mode;
     _externalEncoding = externalEncoding;
     _internalEncoding = internalEncoding;
 }
Пример #52
0
        private void Test_Translate(
            byte[]/*!*/ bself, RubyEncoding/*!*/ eself,
            byte[]/*!*/ bfrom, RubyEncoding/*!*/ efrom,
            byte[]/*!*/ bto, RubyEncoding/*!*/ eto, 
            byte[]/*!*/ expected, RubyEncoding/*!*/ expectedEncoding) {

            var self = MutableString.CreateBinary(bself, eself);
            var from = MutableString.CreateBinary(bfrom, efrom);
            var to = MutableString.CreateBinary(bto, eto);

            var result = MutableStringOps.GetTranslated(self, from, to);
            Assert(result.Encoding == expectedEncoding);
            var b = result.ToByteArray();
            Assert(b.ValueEquals(expected));
        }
Пример #53
0
 public void Test_Reverse(byte[]/*!*/ b, RubyEncoding/*!*/ e, byte[]/*!*/ expected) {
     var s = MutableString.CreateBinary(b, e);
     MutableStringOps.Reverse(s);
     var actual = s.ToByteArray();
     Assert(actual.ValueEquals(expected));
 }
Пример #54
0
        private void Test_Concatenate(byte[]/*!*/ b1, RubyEncoding/*!*/ e1, byte[]/*!*/ b2, RubyEncoding/*!*/ e2, RubyEncoding/*!*/ resultEncoding) {
            var s1 = MutableString.CreateBinary(b1, e1).PrepareForCharacterRead();
            var s2 = MutableString.CreateBinary(b2, e2).PrepareForCharacterRead();

            var s = MutableStringOps.Concatenate(s1, s2);
            Assert(s.Encoding == resultEncoding);
            var b = s.ToByteArray();
            Assert(b.ValueCompareTo(b.Length, Utils.Concatenate(b1, b2)) == 0);
        }
Пример #55
0
 private MutableString/*!*/ MS(byte[]/*!*/ data, RubyEncoding/*!*/ e) {
     return MutableString.CreateBinary(data.Length * 3, e).Append(data);
 }
Пример #56
0
 public static Exception/*!*/ CreateEncodingCompatibilityError(RubyEncoding/*!*/ encoding1, RubyEncoding/*!*/ encoding2) {
     return new EncodingCompatibilityError(
         FormatMessage("incompatible character encodings: {0}{1} and {2}{3}",
             encoding1.Name, encoding1.IsKCoding ? " (KCODE)" : null, encoding2.Name, encoding2.IsKCoding ? " (KCODE)" : null
         )
     );
 }
Пример #57
0
 public static RubyEncoding/*!*/ SetDefaultInternalEncoding(RubyClass/*!*/ self, RubyEncoding encoding) {
     // TODO:
     return RubyEncoding.Default;
 }
Пример #58
0
 public static MutableString/*!*/ ToS(RubyEncoding/*!*/ self) {
     return MutableString.CreateAscii(self.Name);
 }
Пример #59
0
 public static RubyIO/*!*/ SetEncodings(RubyIO/*!*/ self, RubyEncoding external, [DefaultParameterValue(null)]RubyEncoding @internal) {
     self.ExternalEncoding = external ?? self.Context.RubyOptions.LocaleEncoding;
     self.InternalEncoding = @internal;
     return self;
 }
Пример #60
0
 /*!*/
 MSA.Expression StringConstructor.IFactory.CreateExpression(AstGenerator/*!*/ gen, byte[]/*!*/ literal, RubyEncoding/*!*/ encoding)
 {
     // TODO: create the regex here, not at runtime:
     return Methods.CreateRegexB.OpCall(
         Ast.Constant(literal), encoding.Expression, AstUtils.Constant(_options), AstUtils.Constant(new StrongBox<RubyRegex>(null))
     );
 }