Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebCmdlets parse XML declaration to get encoding value, if present. #18748

Merged
merged 10 commits into from
Jan 21, 2023
Prev Previous commit
Next Next commit
remove useless do-while
  • Loading branch information
CarloToso committed Dec 8, 2022
commit 6db83db2517552f08f1f687825b581c401f2d8be
Original file line number Diff line number Diff line change
Expand Up @@ -441,27 +441,24 @@ internal static string DecodeStream(Stream stream, ref Encoding encoding)
string content = StreamToString(stream, encoding);
if (isDefaultEncoding)
{
do
// check for a charset attribute on the meta element to override the default
// we only look within the first 1k characters as the meta tag is in the head
// tag which is at the start of the document
Match match = s_metaRegex.Match(content.Substring(0, Math.Min(content.Length, 1024)));
Match match2 = s_xmlRegex.Match(content.Substring(0, Math.Min(content.Length, 256)));
iSazonov marked this conversation as resolved.
Show resolved Hide resolved
if (match.Success || match2.Success)
{
// check for a charset attribute on the meta element to override the default
// we only look within the first 1k characters as the meta tag is in the head
// tag which is at the start of the document
Match match = s_metaRegex.Match(content.Substring(0, Math.Min(content.Length, 1024)));
Match match2 = s_xmlRegex.Match(content.Substring(0, Math.Min(content.Length, 256)));
if (match.Success || match2.Success)
{
Encoding localEncoding = null;
string characterSet = (string.IsNullOrEmpty(match.Groups["charset"].Value)) ? match2.Groups["encoding"].Value : match.Groups["charset"].Value;
Encoding localEncoding = null;
string characterSet = (string.IsNullOrEmpty(match.Groups["charset"].Value)) ? match2.Groups["encoding"].Value : match.Groups["charset"].Value;

if (TryGetEncoding(characterSet, out localEncoding))
{
stream.Seek(0, SeekOrigin.Begin);
content = StreamToString(stream, localEncoding);
// report the encoding used.
encoding = localEncoding;
}
if (TryGetEncoding(characterSet, out localEncoding))
{
stream.Seek(0, SeekOrigin.Begin);
content = StreamToString(stream, localEncoding);
// report the encoding used.
iSazonov marked this conversation as resolved.
Show resolved Hide resolved
encoding = localEncoding;
}
} while (false);
}
}

return content;
Expand Down