Пример #1
0
        private static int LexicographicalOrder(IStringObject str1, IStringObject str2)
        {
            // If both strings are small use the 'toString' method to compare the
            // strings.  This saves the overhead of having to store very large string
            // objects in memory for all comparisons.
            long str1Size = str1.Length;
            long str2Size = str2.Length;

            if (str1Size < 32 * 1024 &&
                str2Size < 32 * 1024)
            {
                return(str1.ToString().CompareTo(str2.ToString()));
            }

            // The minimum size
            long       size = System.Math.Min(str1Size, str2Size);
            TextReader r1   = str1.GetInput();
            TextReader r2   = str2.GetInput();

            try {
                try {
                    while (size > 0)
                    {
                        int c1 = r1.Read();
                        int c2 = r2.Read();
                        if (c1 != c2)
                        {
                            return(c1 - c2);
                        }
                        --size;
                    }
                    // They compare equally up to the limit, so now compare sizes,
                    if (str1Size > str2Size)
                    {
                        // If str1 is larger
                        return(1);
                    }
                    else if (str1Size < str2Size)
                    {
                        // If str1 is smaller
                        return(-1);
                    }
                    // Must be equal
                    return(0);
                } finally {
                    r1.Close();
                    r2.Close();
                }
            } catch (IOException e) {
                throw new Exception("IO Error: " + e.Message);
            }
        }
Пример #2
0
        public static IStringObject Concat(this IStringObject obj, IStringObject other)
        {
            if (obj == null && other == null)
            {
                return(null);
            }

            if (obj == null)
            {
                return(other);
            }
            if (other == null)
            {
                return(obj);
            }

            var length = obj.Length + other.Length;

            // TODO: Support bigger strings ( <= Int64.MaxValue )
            if (length > Int32.MaxValue)
            {
                throw new InvalidOperationException(
                          String.Format("The concatenation of the two strings would exceed the maximum size supported ( {0} + {1} > {2} )",
                                        obj.Length,
                                        other.Length,
                                        Int32.MaxValue));
            }

            var sb = new StringBuilder(length);

            using (var reader = obj.GetInput()) {
                var buffer = new char[254];
                int readCount;
                while ((readCount = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, readCount);
                }
            }

            using (var reader = other.GetInput()) {
                var buffer = new char[254];
                int readCount;
                while ((readCount = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, readCount);
                }
            }

            return(new StringObject(sb.ToString()));
        }
Пример #3
0
        public static string ToString(this IStringObject obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var sb = new StringBuilder(obj.Length);

            using (var reader = obj.GetInput()) {
                var buffer = new char[254];
                int readCount;
                while ((readCount = reader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    sb.Append(buffer, 0, readCount);
                }
            }

            return(sb.ToString());
        }
        public static IStringObject Concat(this IStringObject obj, IStringObject other)
        {
            if (obj == null && other == null)
                return null;

            if (obj == null)
                return other;
            if (other == null)
                return obj;

            var length = obj.Length + other.Length;

            // TODO: Support bigger strings ( <= Int64.MaxValue )
            if (length > Int32.MaxValue)
                throw new InvalidOperationException(
                    String.Format("The concatenation of the two strings would exceed the maximum size supported ( {0} + {1} > {2} )",
                        obj.Length,
                        other.Length,
                        Int32.MaxValue));

            var sb = new StringBuilder(length);
            using (var reader = obj.GetInput()) {
                var buffer = new char[254];
                int readCount;
                while ((readCount = reader.Read(buffer, 0, buffer.Length)) > 0) {
                    sb.Append(buffer, 0, readCount);
                }
            }

            using (var reader = other.GetInput()) {
                var buffer = new char[254];
                int readCount;
                while ((readCount = reader.Read(buffer, 0, buffer.Length)) > 0) {
                    sb.Append(buffer, 0, readCount);
                }
            }

            return new StringObject(sb.ToString());
        }
Пример #5
0
 public static DataObject String(IStringObject s)
 {
     return(new DataObject(PrimitiveTypes.String(), s));
 }
Пример #6
0
        private static int LexicographicalOrder(IStringObject str1, IStringObject str2)
        {
            // If both strings are small use the 'toString' method to compare the
            // strings.  This saves the overhead of having to store very large string
            // objects in memory for all comparisons.
            long str1Size = str1.Length;
            long str2Size = str2.Length;
            if (str1Size < 32 * 1024 &&
                str2Size < 32 * 1024) {
                return str1.ToString().CompareTo(str2.ToString());
            }

            // The minimum size
            long size = System.Math.Min(str1Size, str2Size);
            TextReader r1 = str1.GetInput();
            TextReader r2 = str2.GetInput();
            try {
                try {
                    while (size > 0) {
                        int c1 = r1.Read();
                        int c2 = r2.Read();
                        if (c1 != c2) {
                            return c1 - c2;
                        }
                        --size;
                    }
                    // They compare equally up to the limit, so now compare sizes,
                    if (str1Size > str2Size) {
                        // If str1 is larger
                        return 1;
                    } else if (str1Size < str2Size) {
                        // If str1 is smaller
                        return -1;
                    }
                    // Must be equal
                    return 0;
                } finally {
                    r1.Close();
                    r2.Close();
                }
            } catch (IOException e) {
                throw new Exception("IO Error: " + e.Message);
            }
        }