Esempio n. 1
0
        private static string SubString(object str, int start, int length, getEncoding encodingGetter)
        {
            // get the Unicode representation of the string
            string ustr = ObjectToString(str, encodingGetter);

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

            // start counting from the end of the string
            if (start < 0)
            {
                start = ustr.Length + start;    // can result in negative start again -> invalid
            }
            if (length == -1)
            {
                length = ustr.Length;
            }

            // check boundaries
            if (start >= ustr.Length || length < 0 || start < 0)
            {
                return(null);
            }

            if (length == 0)
            {
                return(string.Empty);
            }

            // return the substring
            return((start + length > ustr.Length) ? ustr.Substring(start) : ustr.Substring(start, length));
        }
Esempio n. 2
0
        /// <summary>
        /// Counts characters in a Unicode string or multi-byte string in PhpBytes.
        /// </summary>
        /// <param name="str">String or PhpBytes to use.</param>
        /// <param name="encodingGetter">Encoding used to encode PhpBytes</param>
        /// <returns>Number of unicode characters in given object.</returns>
        private static int StrLen(object str, getEncoding encodingGetter)
        {
            if (str == null)
            {
                return(0);
            }

            if (str.GetType() == typeof(string))
            {
                return(((string)str).Length);
            }
            else if (str.GetType() == typeof(PhpBytes))
            {
                Encoding encoding = encodingGetter();
                if (encoding == null)
                {
                    throw new NotImplementedException();
                }

                return(encoding.GetCharCount(((PhpBytes)str).ReadonlyData));
            }
            else
            {
                return((ObjectToString(str, encodingGetter) ?? string.Empty).Length);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Implementation of <c>mb_str[i]pos</c> functions.
        /// </summary>
        private static int Strpos(object haystack, object needle, int offset, getEncoding encodingGetter, bool ignoreCase)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            string uneedle   = ObjectToString(needle, encodingGetter);

            if (uhaystack == null || uneedle == null)
            {
                return(-1);
            }

            if (offset < 0 || offset >= uhaystack.Length)
            {
                if (offset != uhaystack.Length)
                {
                    PhpException.InvalidArgument("offset", LibResources.GetString("arg:out_of_bounds"));
                }
                return(-1);
            }

            if (uneedle == String.Empty)
            {
                PhpException.InvalidArgument("needle", LibResources.GetString("arg:empty"));
                return(-1);
            }

            if (ignoreCase)
            {
                return(uhaystack.ToLower().IndexOf(uneedle.ToLower(), offset));
            }
            else
            {
                return(uhaystack.IndexOf(uneedle, offset));
            }
        }
Esempio n. 4
0
        private static int SubStringCount(string haystack, string needle, getEncoding encodingGetter)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            string uneedle   = ObjectToString(needle, encodingGetter);

            if (uhaystack == null || uneedle == null)
            {
                return(0);
            }

            return(PhpStrings.SubstringCount(uhaystack, uneedle));
        }
Esempio n. 5
0
        /// <summary>
        /// Converts PhpBytes using specified encoding. If any other object is provided, encoding is not performed.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encodingGetter"></param>
        /// <returns></returns>
        private static string ObjectToString(object str, getEncoding encodingGetter)
        {
            if (str is PhpBytes)
            {
                PhpBytes bytes    = (PhpBytes)str;
                Encoding encoding = encodingGetter();
                if (encoding == null)
                {
                    return(null);
                }

                return(encoding.GetString(bytes.ReadonlyData, 0, bytes.Length));
            }
            else
            {
                // .NET String should be always UTF-16, given encoding is irrelevant
                return(PHP.Core.Convert.ObjectToString(str));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Implementation of <c>mb_strr[i]pos</c> functions.
        /// </summary>
        private static int Strrpos(object haystack, object needle, int offset, getEncoding encodingGetter, bool ignoreCase)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            string uneedle   = ObjectToString(needle, encodingGetter);

            if (uhaystack == null || uneedle == null)
            {
                return(-1);
            }

            int end = uhaystack.Length - 1;

            if (offset > end || offset < -end - 1)
            {
                PhpException.InvalidArgument("offset", LibResources.GetString("arg:out_of_bounds"));
                return(-1);
            }

            if (offset < 0)
            {
                end   += uneedle.Length + offset;
                offset = 0;
            }

            if (uneedle.Length == 0)
            {
                PhpException.InvalidArgument("needle", LibResources.GetString("arg:empty"));
                return(-1);
            }

            if (ignoreCase)
            {
                return(uhaystack.ToLower().LastIndexOf(uneedle.ToLower(), end, end - offset + 1));
            }
            else
            {
                return(uhaystack.LastIndexOf(uneedle, end, end - offset + 1));
            }
        }
Esempio n. 7
0
        private static string StrrChr(object haystack, object needle, bool beforeNeedle /*=false*/, getEncoding encodingGetter, bool ignoreCase)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            char   cneedle;
            {
                string uneedle;

                if (needle is string)
                {
                    uneedle = (string)needle;
                }
                else if (needle is PhpString)
                {
                    uneedle = ((IPhpConvertible)needle).ToString();
                }
                else if (needle is PhpBytes)
                {
                    Encoding encoding = encodingGetter();
                    if (encoding == null)
                    {
                        return(null);
                    }

                    PhpBytes bytes = (PhpBytes)needle;
                    uneedle = encoding.GetString(bytes.ReadonlyData, 0, bytes.Length);
                }
                else
                {   // needle as a character number
                    Encoding encoding = encodingGetter();
                    if (encoding == null)
                    {
                        return(null);
                    }

                    uneedle = encoding.GetString(new byte[] { unchecked ((byte)Core.Convert.ObjectToInteger(needle)) }, 0, 1);
                }

                if (string.IsNullOrEmpty(uneedle))
                {
                    return(null);
                }

                cneedle = uneedle[0];
            }

            int index = (ignoreCase) ? uhaystack.ToLower().LastIndexOf(char.ToLower(cneedle)) : uhaystack.LastIndexOf(cneedle);

            if (index < 0)
            {
                return(null);
            }

            return((beforeNeedle) ? uhaystack.Remove(index) : uhaystack.Substring(index));
        }
Esempio n. 8
0
        /// <summary>
        /// Implementation of <c>mb_strr[i]pos</c> functions.
        /// </summary>
        private static int Strrpos(object haystack, object needle, int offset, getEncoding encodingGetter, bool ignoreCase)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            string uneedle = ObjectToString(needle, encodingGetter);

            if (uhaystack == null || uneedle == null)
                return -1;

            int end = uhaystack.Length - 1;
            if (offset > end || offset < -end - 1)
            {
                PhpException.InvalidArgument("offset", LibResources.GetString("arg:out_of_bounds"));
                return -1;
            }

            if (offset < 0)
            {
                end += uneedle.Length + offset;
                offset = 0;
            }

                if (uneedle.Length == 0)
                {
                    PhpException.InvalidArgument("needle", LibResources.GetString("arg:empty"));
                    return -1;
                }

                if (ignoreCase)
                    return uhaystack.ToLower().LastIndexOf(uneedle.ToLower(), end, end - offset + 1);
                else
                    return uhaystack.LastIndexOf(uneedle, end, end - offset + 1);
        }
Esempio n. 9
0
        private static string StringTrimByWidth(object str, int start, int width, string trimmarker, getEncoding encodingGetter)
        {
            string ustr = ObjectToString(str, encodingGetter);

            if (start >= ustr.Length)
            {
                return(string.Empty);
            }

            ustr = ustr.Substring(start);
            int ustrWidth = StringWidth(ustr);

            if (ustrWidth <= width)
            {
                return(ustr);
            }

            // trim the string
            int trimmarkerWidth = StringWidth(trimmarker);

            width -= trimmarkerWidth;
            string trimmedStr = StringTrimByWidth(ustr, ref width);

            width += trimmarkerWidth;
            string trimmedTrimMarker = StringTrimByWidth(trimmarker, ref width);

            //
            return(trimmedStr + trimmedTrimMarker);
        }
Esempio n. 10
0
        private static string StringTrimByWidth(object str, int start, int width, string trimmarker, getEncoding encodingGetter)
        {
            string ustr = ObjectToString(str, encodingGetter);
            
            if (start >= ustr.Length)
                return string.Empty;

            ustr = ustr.Substring(start);
            int ustrWidth = StringWidth(ustr);

            if (ustrWidth <= width)
                return ustr;

            // trim the string
            int trimmarkerWidth = StringWidth(trimmarker);

            width -= trimmarkerWidth;
            string trimmedStr = StringTrimByWidth(ustr, ref width);
            width += trimmarkerWidth;
            string trimmedTrimMarker = StringTrimByWidth(trimmarker, ref width);

            //
            return trimmedStr + trimmedTrimMarker;
        }
Esempio n. 11
0
 private static int StringWidth(object str, getEncoding encodingGetter)
 {
     return StringWidth(ObjectToString(str, encodingGetter));
 }
Esempio n. 12
0
        private static int SubStringCount(string haystack, string needle, getEncoding encodingGetter)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            string uneedle = ObjectToString(needle, encodingGetter);

            if (uhaystack == null || uneedle == null)
                return 0;

            return PhpStrings.SubstringCount(uhaystack, uneedle);
        }
Esempio n. 13
0
        private static string SubString(object str, int start, int length, getEncoding encodingGetter)
        {
            // get the Unicode representation of the string
            string ustr = ObjectToString(str, encodingGetter);

            if (ustr == null)
                return null;

            // start counting from the end of the string
            if (start < 0)
                start = ustr.Length + start;    // can result in negative start again -> invalid

            if (length == -1)
                length = ustr.Length;

            // check boundaries
            if (start >= ustr.Length || length < 0 || start < 0)
                return null;

            if (length == 0)
                return string.Empty;

            // return the substring
            return (start + length > ustr.Length) ? ustr.Substring(start) : ustr.Substring(start, length);
        }
Esempio n. 14
0
        /// <summary>
        /// Converts PhpBytes using specified encoding. If any other object is provided, encoding is not performed.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encodingGetter"></param>
        /// <returns></returns>
        private static string ObjectToString(object str, getEncoding encodingGetter)
        {
            if (str is PhpBytes)
            {
                PhpBytes bytes = (PhpBytes)str;
                Encoding encoding = encodingGetter();
                if (encoding == null)
                    return null;

                return encoding.GetString(bytes.ReadonlyData, 0, bytes.Length);
            }
            else
            {
                // .NET String should be always UTF-16, given encoding is irrelevant
                return PHP.Core.Convert.ObjectToString(str);
            }
        }
Esempio n. 15
0
        private static string StrrChr(object haystack, object needle, bool beforeNeedle/*=false*/, getEncoding encodingGetter, bool ignoreCase)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            char cneedle;
            {
                string uneedle;

                if (needle is string) uneedle = (string)needle;
                else if (needle is PhpString) uneedle = ((IPhpConvertible)needle).ToString();
                else if (needle is PhpBytes)
                {
                    Encoding encoding = encodingGetter();
                    if (encoding == null)
                        return null;

                    PhpBytes bytes = (PhpBytes)needle;
                    uneedle = encoding.GetString(bytes.ReadonlyData, 0, bytes.Length);
                }
                else
                {   // needle as a character number
                    Encoding encoding = encodingGetter();
                    if (encoding == null)
                        return null;

                    uneedle = encoding.GetString(new byte[] { unchecked((byte)Core.Convert.ObjectToInteger(needle)) }, 0, 1);
                }

                if (string.IsNullOrEmpty(uneedle))
                    return null;

                cneedle = uneedle[0];
            }

            int index = (ignoreCase) ? uhaystack.ToLower().LastIndexOf(char.ToLower(cneedle)) : uhaystack.LastIndexOf(cneedle);
            if (index < 0)
                return null;

            return (beforeNeedle) ? uhaystack.Remove(index) : uhaystack.Substring(index);

        }
Esempio n. 16
0
        /// <summary>
        /// Counts characters in a Unicode string or multi-byte string in PhpBytes.
        /// </summary>
        /// <param name="str">String or PhpBytes to use.</param>
        /// <param name="encodingGetter">Encoding used to encode PhpBytes</param>
        /// <returns>Number of unicode characters in given object.</returns>
        private static int StrLen(object str, getEncoding encodingGetter)
        {
            if (str == null)
                return 0;

            if (str.GetType() == typeof(string))
            {
                return ((string)str).Length;
            }
            else if (str.GetType() == typeof(PhpBytes))
            {
                Encoding encoding = encodingGetter();
                if (encoding == null)
                    throw new NotImplementedException();

                return encoding.GetCharCount(((PhpBytes)str).ReadonlyData);
            }
            else
            {
                return (ObjectToString(str, encodingGetter) ?? string.Empty).Length;
            }
        }
Esempio n. 17
0
 private static string StrToLower(object str, getEncoding encodingGetter)
 {
     string ustr = ObjectToString(str, encodingGetter);
     return ustr.ToLowerInvariant();
 }
Esempio n. 18
0
        /// <summary>
        /// mb_strstr() finds the first occurrence of needle in haystack  and returns the portion of haystack. If needle is not found, it returns FALSE. 
        /// </summary>
        /// <param name="haystack">The string from which to get the first occurrence of needle</param>
        /// <param name="needle">The string to find in haystack</param>
        /// <param name="part">Determines which portion of haystack  this function returns. If set to TRUE, it returns all of haystack  from the beginning to the first occurrence of needle. If set to FALSE, it returns all of haystack  from the first occurrence of needle to the end.</param>
        /// <param name="encodingGetter">Character encoding name to use. If it is omitted, internal character encoding is used. </param>
        /// <param name="ignoreCase">Case insensitive.</param>
        /// <returns>Returns the portion of haystack, or FALSE (-1) if needle is not found.</returns>
        private static string StrStr(object haystack, object needle, bool part/* = false*/  , getEncoding encodingGetter, bool ignoreCase)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            string uneedle = ObjectToString(needle, encodingGetter);

            if (uhaystack == null || uneedle == null)   // never happen
                return null;

            if (uneedle == String.Empty)
            {
                PhpException.InvalidArgument("needle", LibResources.GetString("arg:empty"));
                return null;
            }

            int index = (ignoreCase) ? uhaystack.ToLower().IndexOf(uneedle.ToLower()) : uhaystack.IndexOf(uneedle);
            return (index == -1) ? null : (part ? uhaystack.Substring(0, index) : uhaystack.Substring(index));
        }
Esempio n. 19
0
 private static int StringWidth(object str, getEncoding encodingGetter)
 {
     return(StringWidth(ObjectToString(str, encodingGetter)));
 }
Esempio n. 20
0
        /// <summary>
        /// mb_strstr() finds the first occurrence of needle in haystack  and returns the portion of haystack. If needle is not found, it returns FALSE.
        /// </summary>
        /// <param name="haystack">The string from which to get the first occurrence of needle</param>
        /// <param name="needle">The string to find in haystack</param>
        /// <param name="part">Determines which portion of haystack  this function returns. If set to TRUE, it returns all of haystack  from the beginning to the first occurrence of needle. If set to FALSE, it returns all of haystack  from the first occurrence of needle to the end.</param>
        /// <param name="encodingGetter">Character encoding name to use. If it is omitted, internal character encoding is used. </param>
        /// <param name="ignoreCase">Case insensitive.</param>
        /// <returns>Returns the portion of haystack, or FALSE (-1) if needle is not found.</returns>
        private static string StrStr(object haystack, object needle, bool part /* = false*/, getEncoding encodingGetter, bool ignoreCase)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            string uneedle   = ObjectToString(needle, encodingGetter);

            if (uhaystack == null || uneedle == null)   // never happen
            {
                return(null);
            }

            if (uneedle == String.Empty)
            {
                PhpException.InvalidArgument("needle", LibResources.GetString("arg:empty"));
                return(null);
            }

            int index = (ignoreCase) ? uhaystack.ToLower().IndexOf(uneedle.ToLower()) : uhaystack.IndexOf(uneedle);

            return((index == -1) ? null : (part ? uhaystack.Substring(0, index) : uhaystack.Substring(index)));
        }
Esempio n. 21
0
        private static string StrToLower(object str, getEncoding encodingGetter)
        {
            string ustr = ObjectToString(str, encodingGetter);

            return(ustr.ToLowerInvariant());
        }
Esempio n. 22
0
        /// <summary>
        /// Implementation of <c>mb_str[i]pos</c> functions.
        /// </summary>
        private static int Strpos(object haystack, object needle, int offset, getEncoding encodingGetter, bool ignoreCase)
        {
            string uhaystack = ObjectToString(haystack, encodingGetter);
            string uneedle = ObjectToString(needle, encodingGetter);

            if (uhaystack == null || uneedle == null)
                return -1;

            if (offset < 0 || offset >= uhaystack.Length)
            {
                if (offset != uhaystack.Length)
                    PhpException.InvalidArgument("offset", LibResources.GetString("arg:out_of_bounds"));
                return -1;
            }

                if (uneedle == String.Empty)
                {
                    PhpException.InvalidArgument("needle", LibResources.GetString("arg:empty"));
                    return -1;
                }

            if (ignoreCase)
                return uhaystack.ToLower().IndexOf(uneedle.ToLower(), offset);
            else
                return uhaystack.IndexOf(uneedle, offset);
        }