コード例 #1
0
        private static void SubSubSteps(string input, Encoding selectedEncoding, ref StringBuilder result)
        {
            var enumerator = new CharacterEnumerator(input);

            while (enumerator.MoveNext())
            {
                var c         = enumerator.Current;
                var codePoint = enumerator.GetCodePoint();

                if (c == " ")
                {
                    result.Append('+');
                }
                else if (!Regex.IsMatch(c, "[\u0020\u002A\u002D\u002E\u0030-\u0039\u0041-\u005A\u005F\u0061-\007A]"))
                {
                    string s = string.Empty;
                    foreach (var b in selectedEncoding.GetBytes(s))
                    {
                        SubSubSubStep(b, ref s);
                    }
                }
                else
                {
                    result.Append(c);
                }
            }
        }
コード例 #2
0
        private static string InternalEncode(string str)
        {
            var sb         = new StringBuilder();
            var enumerator = new CharacterEnumerator(str);

            var lastWasCr = false;

            while (enumerator.MoveNext())
            {
                var c         = enumerator.Current;
                var codePoint = enumerator.GetCodePoint();

                if (c == " ")
                {
                    sb.Append('+');
                }
                else if (c == "\u2029" || c == "\u2028" || c == "\u0085" || c == "\u000D" || (c == "\u000A" && !lastWasCr))
                {
                    sb.Append("%0D%0A");
                }
                else if (Regex.IsMatch(c, @"[A-Za-z0-9]"))
                {
                    sb.Append(c);
                }
                else if (codePoint > 127)
                {
                    throw new FormatException("The non-alphanumeric character '" + c + "' cannot be represented as ASCII coded character.");
                }
                else if (c != "\u000A")
                {
                    sb.Append("%" + ((byte)codePoint).ToString("X2"));
                }

                lastWasCr = c == "\u000D";
            }

            return(sb.ToString());
        }
コード例 #3
0
        /* private static string InternalEncode(string str)
         * {
         *  StringBuilder sb = new StringBuilder();
         *  TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator(str);
         *
         *  bool lastWasCr = false;
         *  foreach (char c in str)
         *  {
         *      if (lastWasCr && c != '\u000A')
         *          sb.Append("%0D%0A");
         *
         *      if (c == ' ')
         *          sb.Append('+');
         *      if (c == '\u2028' || c == '\u000A' || c == '\u0085')
         *          sb.Append("%0D%0A");
         *      else if (c > 127 || ":/?#[]@!$&'()*+,;=".IndexOf(c) != -1)
         *          foreach (byte b in Encoding.UTF8.GetBytes(new char[] {c}))
         *          {
         *              sb.Append("%");
         *              sb.Append(b.ToString("X2"));
         *          }
         *      else if (!(lastWasCr = c == '\u000D'))
         *          sb.Append(c);
         *  }
         *
         *  if (lastWasCr)
         *      sb.Append("%0D%0A");
         *
         *  return sb.ToString();
         * } */

        private static string InternalEncode(string str)
        {
            var sb         = new StringBuilder();
            var enumerator = new CharacterEnumerator(str);

            var lastWasCr = false;

            while (enumerator.MoveNext())
            {
                var c = enumerator.Current;

                if (c == " ")
                {
                    sb.Append('+');
                }
                else if (c == "\u2028" || c == "\u0085" || c == "\u000D" || (c == "\u000A" && !lastWasCr))
                {
                    sb.Append("%0D%0A");
                }
                else if (enumerator.GetCodePoint() > 127 || ":/?#[]@!$&'()*+,;=".IndexOf(c, StringComparison.Ordinal) != -1)
                {
                    foreach (var b in Encoding.UTF8.GetBytes(c))
                    {
                        sb.Append("%");
                        sb.Append(b.ToString("X2"));
                    }
                }
                else if (c != "\u000A")
                {
                    sb.Append(c);
                }

                lastWasCr = c == "\u000D";
            }

            return(sb.ToString());
        }