From 2cc8fe18a0eb808e60e9f51de1189aa9b1f4e183 Mon Sep 17 00:00:00 2001 From: Iristyle Date: Thu, 9 May 2013 14:06:35 -0400 Subject: [PATCH] feat(core): EncodingHelpers from posh-git - Used to ensure we read/write text files properly --- core/EncodingHelpers.ps1 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 core/EncodingHelpers.ps1 diff --git a/core/EncodingHelpers.ps1 b/core/EncodingHelpers.ps1 new file mode 100644 index 0000000..90528e8 --- /dev/null +++ b/core/EncodingHelpers.ps1 @@ -0,0 +1,17 @@ +# Adapted from http://www.west-wind.com/Weblog/posts/197245.aspx +function Get-FileEncoding($Path) +{ + $bytes = [byte[]](Get-Content $Path -Encoding byte -ReadCount 4 -TotalCount 4) + + if (!$bytes) { return 'utf8' } + + switch -regex ('{0:x2}{1:x2}{2:x2}{3:x2}' -f $bytes[0..3]) + { + '^efbbbf' { return 'utf8' } + '^2b2f76' { return 'utf7' } + '^fffe' { return 'unicode' } + '^feff' { return 'bigendianunicode' } + '^0000feff' { return 'utf32' } + default { return 'ascii' } + } +}