System.Net.WebHeaderEncoding.DecodeUtf8FromString C# (CSharp) Method

DecodeUtf8FromString() static private method

static private DecodeUtf8FromString ( string input ) : string
input string
return string
        internal static string DecodeUtf8FromString(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return input;
            }

            bool possibleUtf8 = false;
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] > (char)255)
                {
                    return input; // This couldn't have come from the wire, someone assigned it directly.
                }
                else if (input[i] > (char)127)
                {
                    possibleUtf8 = true;
                    break;
                }
            }
            if (possibleUtf8)
            {
                byte[] rawBytes = new byte[input.Length];
                for (int i = 0; i < input.Length; i++)
                {
                    if (input[i] > (char)255)
                    {
                        return input; // This couldn't have come from the wire, someone assigned it directly.
                    }
                    rawBytes[i] = (byte)input[i];
                }
                try
                {
                    return s_utf8Decoder.GetString(rawBytes);
                }
                catch (ArgumentException) { } // Not actually Utf-8
            }
            return input;
        }
    }