예제 #1
0
        /// <summary>
        /// Provides the path string escaped in a way which is correct for combining into the URI representation.
        /// </summary>
        /// <returns>The escaped path value</returns>
        public string ToUriComponent()
        {
            if (!HasValue)
            {
                return(string.Empty);
            }
            StringBuilder stringBuilder = null;
            int           startIndex    = 0;
            int           num           = 0;
            bool          flag          = false;
            int           num2          = 0;

            while (num2 < Value.Length)
            {
                bool flag2 = PathStringHelper.IsPercentEncodedChar(Value, num2);
                if (PathStringHelper.IsValidPathChar(Value[num2]) | flag2)
                {
                    if (flag)
                    {
                        if (stringBuilder is null)
                        {
                            stringBuilder = new StringBuilder(Value.Length * 3);
                        }
                        stringBuilder.Append(Uri.EscapeDataString(Value.Substring(startIndex, num)));
                        flag       = false;
                        startIndex = num2;
                        num        = 0;
                    }
                    if (flag2)
                    {
                        num  += 3;
                        num2 += 3;
                    }
                    else
                    {
                        num++;
                        num2++;
                    }
                    continue;
                }
                if (!flag)
                {
                    if (stringBuilder is null)
                    {
                        stringBuilder = new StringBuilder(Value.Length * 3);
                    }
                    stringBuilder.Append(Value, startIndex, num);
                    flag       = true;
                    startIndex = num2;
                    num        = 0;
                }
                num++;
                num2++;
            }
            if (num == Value.Length && !flag)
            {
                return(Value);
            }
            if (num > 0)
            {
                if (stringBuilder is null)
                {
                    stringBuilder = new StringBuilder(Value.Length * 3);
                }
                if (flag)
                {
                    stringBuilder.Append(Uri.EscapeDataString(Value.Substring(startIndex, num)));
                }
                else
                {
                    stringBuilder.Append(Value, startIndex, num);
                }
            }
            return(stringBuilder.ToString());
        }
예제 #2
0
        /// <summary>
        /// Provides the path string escaped in a way which is correct for combining into the URI representation.
        /// </summary>
        /// <returns>The escaped path value</returns>
        public string ToUriComponent()
        {
            if (!HasValue)
            {
                return(string.Empty);
            }

            StringBuilder buffer = null;

            var start            = 0;
            var count            = 0;
            var requiresEscaping = false;
            var i = 0;

            while (i < _value.Length)
            {
                var isPercentEncodedChar = PathStringHelper.IsPercentEncodedChar(_value, i);
                if (PathStringHelper.IsValidPathChar(_value[i]) || isPercentEncodedChar)
                {
                    if (requiresEscaping)
                    {
                        // the current segment requires escape
                        if (buffer == null)
                        {
                            buffer = new StringBuilder(_value.Length * 3);
                        }

                        buffer.Append(Uri.EscapeDataString(_value.Substring(start, count)));

                        requiresEscaping = false;
                        start            = i;
                        count            = 0;
                    }

                    if (isPercentEncodedChar)
                    {
                        count += 3;
                        i     += 3;
                    }
                    else
                    {
                        count++;
                        i++;
                    }
                }
                else
                {
                    if (!requiresEscaping)
                    {
                        // the current segment doesn't require escape
                        if (buffer == null)
                        {
                            buffer = new StringBuilder(_value.Length * 3);
                        }

                        buffer.Append(_value, start, count);

                        requiresEscaping = true;
                        start            = i;
                        count            = 0;
                    }

                    count++;
                    i++;
                }
            }

            if (count == _value.Length && !requiresEscaping)
            {
                return(_value);
            }
            else
            {
                if (count > 0)
                {
                    if (buffer == null)
                    {
                        buffer = new StringBuilder(_value.Length * 3);
                    }

                    if (requiresEscaping)
                    {
                        buffer.Append(Uri.EscapeDataString(_value.Substring(start, count)));
                    }
                    else
                    {
                        buffer.Append(_value, start, count);
                    }
                }

                return(buffer.ToString());
            }
        }