Exemplo n.º 1
0
        public static Iconv /*!*/ Create(RubyClass /*!*/ self,
                                         [DefaultProtocol, NotNull] MutableString /*!*/ toEncoding, [DefaultProtocol, NotNull] MutableString /*!*/ fromEncoding)
        {
            Iconv converter = new Iconv();

            return(Initialize(converter, toEncoding, fromEncoding));
        }
Exemplo n.º 2
0
        private static MutableString[] /*!*/ Convert(
            MutableString /*!*/ toEncoding,
            MutableString /*!*/ fromEncoding,
            MutableString[] /*!*/ strings)
        {
            Iconv conveter = Create(null, toEncoding, fromEncoding);

            MutableString[] convertedStrings = new MutableString[strings.Length];
            for (int i = 0; i < strings.Length; i++)
            {
                convertedStrings[i] = iconv(conveter, strings[i], 0, -1);
            }
            MutableString closingString = conveter.Close(false);

            if (closingString.IsEmpty)
            {
                return(convertedStrings);
            }
            else
            {
                Array.Resize(ref convertedStrings, strings.Length + 1);
                convertedStrings[strings.Length] = closingString;
            }
            return(convertedStrings);
        }
Exemplo n.º 3
0
 public static MutableString Close(Iconv/*!*/ self)
 {
     if (!self._isClosed) {
         return self.Close(false);
     } else {
         return null;
     }
 }
Exemplo n.º 4
0
        public static Iconv/*!*/ Initialize(Iconv/*!*/ self,
            [DefaultProtocol]MutableString/*!*/ toEncoding, [DefaultProtocol]MutableString/*!*/ fromEncoding) {

            self._toEncoding = RubyEncoding.GetEncodingByRubyName(toEncoding.ConvertToString()).GetEncoder();
            self._fromEncoding = RubyEncoding.GetEncodingByRubyName(fromEncoding.ConvertToString()).GetDecoder();

            return self;
        }
Exemplo n.º 5
0
        public static Iconv /*!*/ Initialize(Iconv /*!*/ self,
                                             [DefaultProtocol] MutableString /*!*/ toEncoding, [DefaultProtocol] MutableString /*!*/ fromEncoding)
        {
            self._toEncoding   = RubyEncoding.GetEncodingByRubyName(toEncoding.ConvertToString()).GetEncoder();
            self._fromEncoding = RubyEncoding.GetEncodingByRubyName(fromEncoding.ConvertToString()).GetDecoder();

            return(self);
        }
Exemplo n.º 6
0
        public static MutableString/*!*/ iconv(Iconv/*!*/ self,
            [DefaultProtocol]MutableString str,
            [DefaultProtocol, DefaultParameterValue(0)]int startIndex,
            object length) {

            if (length == null) {
                return iconv(self, str, startIndex, -1);
            }
            throw new ArgumentException();
        }
Exemplo n.º 7
0
 public static MutableString /*!*/ Close(Iconv /*!*/ self)
 {
     if (!self._isClosed)
     {
         return(self.Close(false));
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 8
0
 public static MutableString /*!*/ iconv(Iconv /*!*/ self,
                                         [DefaultProtocol] MutableString str,
                                         [DefaultProtocol, DefaultParameterValue(0)] int startIndex,
                                         object length)
 {
     if (length == null)
     {
         return(iconv(self, str, startIndex, -1));
     }
     throw new ArgumentException();
 }
Exemplo n.º 9
0
        public static MutableString/*!*/ iconv(Iconv/*!*/ self, [DefaultProtocol]MutableString/*!*/ str,
            [DefaultProtocol, DefaultParameterValue(0)]int startIndex, [DefaultProtocol, DefaultParameterValue(-1)]int endIndex) {

            // TODO:
            int bytesUsed, charsUsed;
            bool completed;

            byte[] source = str.ConvertToBytes();
            char[] buffer = new char[self._fromEncoding.GetCharCount(source, 0, source.Length)];
            self._fromEncoding.Convert(source, 0, source.Length, buffer, 0, buffer.Length, false, out bytesUsed, out charsUsed, out completed);
            Debug.Assert(charsUsed == buffer.Length && bytesUsed == source.Length);
            
            byte[] result = new byte[self._toEncoding.GetByteCount(buffer, 0, buffer.Length, false)];
            int bytesEncoded = self._toEncoding.GetBytes(buffer, 0, buffer.Length, result, 0, false);
            Debug.Assert(bytesEncoded == result.Length);

            return MutableString.CreateBinary(result);
        }
Exemplo n.º 10
0
        public static MutableString /*!*/ iconv(Iconv /*!*/ self, [DefaultProtocol] MutableString /*!*/ str,
                                                [DefaultProtocol, DefaultParameterValue(0)] int startIndex, [DefaultProtocol, DefaultParameterValue(-1)] int endIndex)
        {
            // TODO:
            int  bytesUsed, charsUsed;
            bool completed;

            byte[] source = str.ConvertToBytes();
            char[] buffer = new char[self._fromEncoding.GetCharCount(source, 0, source.Length)];
            self._fromEncoding.Convert(source, 0, source.Length, buffer, 0, buffer.Length, false, out bytesUsed, out charsUsed, out completed);
            Debug.Assert(charsUsed == buffer.Length && bytesUsed == source.Length);

            byte[] result       = new byte[self._toEncoding.GetByteCount(buffer, 0, buffer.Length, false)];
            int    bytesEncoded = self._toEncoding.GetBytes(buffer, 0, buffer.Length, result, 0, false);

            Debug.Assert(bytesEncoded == result.Length);

            return(MutableString.CreateBinary(result));
        }
Exemplo n.º 11
0
        public static object Open([NotNull] BlockParam /*!*/ block, RubyClass /*!*/ self,
                                  [DefaultProtocol, NotNull] MutableString /*!*/ toEncoding, [DefaultProtocol, NotNull] MutableString /*!*/ fromEncoding)
        {
            Iconv converter = Create(self, toEncoding, fromEncoding);

            if (block == null)
            {
                return(converter);
            }
            else
            {
                try {
                    object blockResult;
                    block.Yield(converter, out blockResult);
                    return(blockResult);
                } finally {
                    Close(converter);
                }
            }
        }
Exemplo n.º 12
0
        public static Iconv/*!*/ Initialize(Iconv/*!*/ self,
            [DefaultProtocol, NotNull]MutableString/*!*/ toEncoding, [DefaultProtocol, NotNull]MutableString/*!*/ fromEncoding) {

            self._toEncodingString = toEncoding.ConvertToString().ToUpperInvariant();

            try {
                self._toEncoding = RubyEncoding.GetEncodingByRubyName(self._toEncodingString).GetEncoder();
            } catch (ArgumentException e) {
                throw new InvalidEncoding(self._toEncodingString, e);
            }

            try {
                self._fromEncoding = RubyEncoding.GetEncodingByRubyName(fromEncoding.ConvertToString()).GetDecoder();
            } catch (ArgumentException e) {
                throw new InvalidEncoding(fromEncoding.ConvertToString(), e);
            }

            self.ResetByteOrderMark();

            return self;
        }
Exemplo n.º 13
0
        public static Iconv /*!*/ Initialize(Iconv /*!*/ self,
                                             [DefaultProtocol, NotNull] MutableString /*!*/ toEncoding, [DefaultProtocol, NotNull] MutableString /*!*/ fromEncoding)
        {
            self._toEncodingString = toEncoding.ConvertToString().ToUpperInvariant();

            try {
                self._toEncoding = RubyEncoding.GetEncodingByRubyName(self._toEncodingString).GetEncoder();
            } catch (ArgumentException e) {
                throw new InvalidEncoding(self._toEncodingString, e);
            }

            try {
                self._fromEncoding = RubyEncoding.GetEncodingByRubyName(fromEncoding.ConvertToString()).GetDecoder();
            } catch (ArgumentException e) {
                throw new InvalidEncoding(fromEncoding.ConvertToString(), e);
            }

            self.ResetByteOrderMark();

            return(self);
        }
Exemplo n.º 14
0
        public static MutableString/*!*/ iconv(Iconv/*!*/ self, 
            [DefaultProtocol]MutableString str,
            [DefaultProtocol, DefaultParameterValue(0)]int startIndex, 
            [DefaultProtocol, NotNull, DefaultParameterValue(-1)]int length) {

            if (self._isClosed) {
                throw RubyExceptions.CreateArgumentError("closed stream");
            }

            if (str == null) {
                return self.Close(true);
            }

            // TODO:
            int bytesUsed, charsUsed;
            bool completed;

            byte[] source = str.ConvertToBytes();
            if (startIndex < 0) {
                startIndex = source.Length + startIndex;
                if (startIndex < 0) {
                    //throw new IllegalSequence("start index is too large of a negative number");
                    startIndex = 0;
                    length = 0;
                }
            } else if (startIndex > source.Length) {
                startIndex = 0;
                length = 0;
            }

            if ((length < 0) || (startIndex + length > source.Length)) {
                length = source.Length - startIndex;
            }

            char[] buffer = new char[self._fromEncoding.GetCharCount(source, startIndex, length)];
            self._fromEncoding.Convert(source, startIndex, length, buffer, 0, buffer.Length, false, out bytesUsed, out charsUsed, out completed);
            Debug.Assert(charsUsed == buffer.Length && bytesUsed == length);
            
            byte[] result = new byte[self._toEncoding.GetByteCount(buffer, 0, buffer.Length, false)];
            int bytesEncoded = self._toEncoding.GetBytes(buffer, 0, buffer.Length, result, 0, false);
            Debug.Assert(bytesEncoded == result.Length);

            if (self._emitBom && result.Length > 0) {
                byte[] resultWithBom = new byte[2 + result.Length];
                resultWithBom[0] = 0xff;
                resultWithBom[1] = 0xfe;
                Array.Copy(result, 0, resultWithBom, 2, result.Length);
                result = resultWithBom;
                self._emitBom = false;
            }

            return MutableString.CreateBinary(result);
        }
Exemplo n.º 15
0
 public static MutableString /*!*/ Close(Iconv /*!*/ self)
 {
     return(null);
 }
Exemplo n.º 16
0
        public static Iconv/*!*/ Create(RubyClass/*!*/ self,
            [DefaultProtocol, NotNull]MutableString/*!*/ toEncoding, [DefaultProtocol, NotNull]MutableString/*!*/ fromEncoding) {

            Iconv converter = new Iconv();
            return Initialize(converter, toEncoding, fromEncoding);
        }
Exemplo n.º 17
0
        public static MutableString /*!*/ iconv(Iconv /*!*/ self,
                                                [DefaultProtocol] MutableString str,
                                                [DefaultProtocol, DefaultParameterValue(0)] int startIndex,
                                                [DefaultProtocol, NotNull, DefaultParameterValue(-1)] int length)
        {
            if (self._isClosed)
            {
                throw RubyExceptions.CreateArgumentError("closed stream");
            }

            if (str == null)
            {
                return(self.Close(true));
            }

            // TODO:
            int  bytesUsed, charsUsed;
            bool completed;

            byte[] source = str.ConvertToBytes();
            if (startIndex < 0)
            {
                startIndex = source.Length + startIndex;
                if (startIndex < 0)
                {
                    //throw new IllegalSequence("start index is too large of a negative number");
                    startIndex = 0;
                    length     = 0;
                }
            }
            else if (startIndex > source.Length)
            {
                startIndex = 0;
                length     = 0;
            }

            if ((length < 0) || (startIndex + length > source.Length))
            {
                length = source.Length - startIndex;
            }

            char[] buffer = new char[self._fromEncoding.GetCharCount(source, startIndex, length)];
            self._fromEncoding.Convert(source, startIndex, length, buffer, 0, buffer.Length, false, out bytesUsed, out charsUsed, out completed);
            Debug.Assert(charsUsed == buffer.Length && bytesUsed == length);

            byte[] result       = new byte[self._toEncoding.GetByteCount(buffer, 0, buffer.Length, false)];
            int    bytesEncoded = self._toEncoding.GetBytes(buffer, 0, buffer.Length, result, 0, false);

            Debug.Assert(bytesEncoded == result.Length);

            if (self._emitBom && result.Length > 0)
            {
                byte[] resultWithBom = new byte[2 + result.Length];
                resultWithBom[0] = 0xff;
                resultWithBom[1] = 0xfe;
                Array.Copy(result, 0, resultWithBom, 2, result.Length);
                result        = resultWithBom;
                self._emitBom = false;
            }

            return(MutableString.CreateBinary(result));
        }
Exemplo n.º 18
0
 public static MutableString/*!*/ Close(Iconv/*!*/ self) {
     return null;
 }