예제 #1
0
        private unsafe string GetRelativeSerializationString(UriFormat format)
        {
            if (format == UriFormat.UriEscaped)
            {
                return(UriHelper.EscapeString(_string, checkExistingEscaped: true, UriHelper.UnreservedReservedTable));
            }
            else if (format == UriFormat.Unescaped)
            {
                return(UnescapeDataString(_string));
            }
            else if (format == UriFormat.SafeUnescaped)
            {
                if (_string.Length == 0)
                {
                    return(string.Empty);
                }

                char[] dest     = new char[_string.Length];
                int    position = 0;
                dest = UriHelper.UnescapeString(_string, 0, _string.Length, dest, ref position, c_DummyChar,
                                                c_DummyChar, c_DummyChar, UnescapeMode.EscapeUnescape, null, false);
                return(new string(dest, 0, position));
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(format));
            }
        }
예제 #2
0
        public static string UnescapeDataString(string stringToUnescape)
        {
            if (stringToUnescape is null)
            {
                throw new ArgumentNullException(nameof(stringToUnescape));
            }

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

            int position = stringToUnescape.IndexOf('%');

            if (position == -1)
            {
                return(stringToUnescape);
            }

            var vsb = new ValueStringBuilder(stackalloc char[256]);

            vsb.EnsureCapacity(stringToUnescape.Length);

            vsb.Append(stringToUnescape.AsSpan(0, position));
            UriHelper.UnescapeString(
                stringToUnescape, position, stringToUnescape.Length, ref vsb,
                c_DummyChar, c_DummyChar, c_DummyChar,
                UnescapeMode.Unescape | UnescapeMode.UnescapeAll,
                syntax: null, isQuery: false);

            return(vsb.ToString());
        }
예제 #3
0
        private unsafe string GetRelativeSerializationString(UriFormat format)
        {
            if (format == UriFormat.UriEscaped)
            {
                return(UriHelper.EscapeString(_string, checkExistingEscaped: true, UriHelper.UnreservedReservedTable));
            }
            else if (format == UriFormat.Unescaped)
            {
                return(UnescapeDataString(_string));
            }
            else if (format == UriFormat.SafeUnescaped)
            {
                if (_string.Length == 0)
                {
                    return(string.Empty);
                }

                var vsb = new ValueStringBuilder(stackalloc char[StackallocThreshold]);
                UriHelper.UnescapeString(_string, ref vsb, c_DummyChar, c_DummyChar, c_DummyChar, UnescapeMode.EscapeUnescape, null, false);
                return(vsb.ToString());
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(format));
            }
        }
예제 #4
0
        public static string UnescapeDataString(string stringToUnescape)
        {
            if ((object)stringToUnescape == null)
            {
                throw new ArgumentNullException(nameof(stringToUnescape));
            }

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

            unsafe
            {
                fixed(char *pStr = stringToUnescape)
                {
                    int position;

                    for (position = 0; position < stringToUnescape.Length; ++position)
                    {
                        if (pStr[position] == '%')
                        {
                            break;
                        }
                    }

                    if (position == stringToUnescape.Length)
                    {
                        return(stringToUnescape);
                    }

                    UnescapeMode unescapeMode = UnescapeMode.Unescape | UnescapeMode.UnescapeAll;

                    position = 0;
                    ValueStringBuilder vsb = new ValueStringBuilder(stringToUnescape.Length);

                    UriHelper.UnescapeString(stringToUnescape, 0, stringToUnescape.Length, ref vsb, ref position,
                                             c_DummyChar, c_DummyChar, c_DummyChar, unescapeMode, null, false);

                    ReadOnlySpan <char> resultSpan = vsb.AsSpan(0, position);
                    string result = resultSpan.SequenceEqual(stringToUnescape) ? stringToUnescape : resultSpan.ToString();

                    vsb.Dispose();
                    return(result);
                }
            }
        }
예제 #5
0
        private unsafe string GetRelativeSerializationString(UriFormat format)
        {
            if (format == UriFormat.UriEscaped)
            {
                if (_string.Length == 0)
                {
                    return(string.Empty);
                }
                int    position = 0;
                char[] dest     = UriHelper.EscapeString(_string, 0, _string.Length, null, ref position, true,
                                                         c_DummyChar, c_DummyChar, '%');
                if ((object)dest == null)
                {
                    return(_string);
                }
                return(new string(dest, 0, position));
            }

            else if (format == UriFormat.Unescaped)
            {
                return(UnescapeDataString(_string));
            }

            else if (format == UriFormat.SafeUnescaped)
            {
                if (_string.Length == 0)
                {
                    return(string.Empty);
                }

                char[] dest     = new char[_string.Length];
                int    position = 0;
                dest = UriHelper.UnescapeString(_string, 0, _string.Length, dest, ref position, c_DummyChar,
                                                c_DummyChar, c_DummyChar, UnescapeMode.EscapeUnescape, null, false);
                return(new string(dest, 0, position));
            }
            else
            {
                throw new ArgumentOutOfRangeException("format");
            }
        }
예제 #6
0
        public static string UnescapeDataString(string stringToUnescape)
        {
            if ((object)stringToUnescape == null)
            {
                throw new ArgumentNullException("stringToUnescape");
            }

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

            unsafe
            {
                fixed(char *pStr = stringToUnescape)
                {
                    int position;

                    for (position = 0; position < stringToUnescape.Length; ++position)
                    {
                        if (pStr[position] == '%')
                        {
                            break;
                        }
                    }

                    if (position == stringToUnescape.Length)
                    {
                        return(stringToUnescape);
                    }

                    UnescapeMode unescapeMode = UnescapeMode.Unescape | UnescapeMode.UnescapeAll;

                    position = 0;
                    char[] dest = new char[stringToUnescape.Length];
                    dest = UriHelper.UnescapeString(stringToUnescape, 0, stringToUnescape.Length, dest, ref position,
                                                    c_DummyChar, c_DummyChar, c_DummyChar, unescapeMode, null, false);
                    return(new string(dest, 0, position));
                }
            }
        }
예제 #7
0
        //
        // Unescapes entire string and checks if it has unicode chars
        //
        private bool CheckForUnicode(string data)
        {
            bool hasUnicode = false;

            char[] chars = new char[data.Length];
            int    count = 0;

            chars = UriHelper.UnescapeString(data, 0, data.Length, chars, ref count, c_DummyChar, c_DummyChar,
                                             c_DummyChar, UnescapeMode.Unescape | UnescapeMode.UnescapeAll, null, false);

            for (int i = 0; i < count; ++i)
            {
                if (chars[i] > '\x7f')
                {
                    // Unicode
                    hasUnicode = true;
                    break;
                }
            }
            return(hasUnicode);
        }