diff --git a/.gitignore b/.gitignore index 5a57766..4c9f02b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,248 @@ -*.userprefs -bin -obj +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +[Xx]64/ +[Xx]86/ +[Bb]uild/ +bld/ +[Bb]in/ +[Oo]bj/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml + +# TODO: Un-comment the next line if you do not want to checkin +# your web deploy settings because they may include unencrypted +# passwords +#*.pubxml +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Microsoft Azure ApplicationInsights config file +ApplicationInsights.config + +# Windows Store app package directory +AppPackages/ +BundleArtifacts/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# LightSwitch generated files +GeneratedArtifacts/ +ModelManifest.xml + +# Paket dependency manager +.paket/paket.exe + +# FAKE - F# Make +.fake/ +/src/presentations/web/wwwroot +/src/services/webjob/lucene +appsettings.Development.json diff --git a/Portable.Text.Encoding.sln b/Portable.Text.Encoding.sln index 9b4c113..18308ae 100644 --- a/Portable.Text.Encoding.sln +++ b/Portable.Text.Encoding.sln @@ -1,12 +1,10 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 -VisualStudioVersion = 12.0.30324.0 +# Visual Studio 15 +VisualStudioVersion = 15.0.27625.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Portable.Text.Encoding", "Portable.Text.Encoding\Portable.Text.Encoding.csproj", "{EEE48C75-11BE-4B50-B759-F85B5052D473}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Portable.Text.Encoding.WindowsUniversal81", "Portable.Text.Encoding\Portable.Text.Encoding.WindowsUniversal81.csproj", "{B76A64F9-B00E-4243-AE89-5D024CA3B436}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -17,14 +15,13 @@ Global {EEE48C75-11BE-4B50-B759-F85B5052D473}.Debug|Any CPU.Build.0 = Debug|Any CPU {EEE48C75-11BE-4B50-B759-F85B5052D473}.Release|Any CPU.ActiveCfg = Release|Any CPU {EEE48C75-11BE-4B50-B759-F85B5052D473}.Release|Any CPU.Build.0 = Release|Any CPU - {B76A64F9-B00E-4243-AE89-5D024CA3B436}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B76A64F9-B00E-4243-AE89-5D024CA3B436}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B76A64F9-B00E-4243-AE89-5D024CA3B436}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B76A64F9-B00E-4243-AE89-5D024CA3B436}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EAE04A38-0F28-42ED-BF19-028BB9CF3BE1} + EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution StartupItem = Portable.Text.Encoding\Portable.Text.Encoding.csproj EndGlobalSection diff --git a/Portable.Text.Encoding/CJK/CodeTable.cs b/Portable.Text.Encoding/CJK/CodeTable.cs index 63fa3bb..d54dbfa 100644 --- a/Portable.Text.Encoding/CJK/CodeTable.cs +++ b/Portable.Text.Encoding/CJK/CodeTable.cs @@ -26,7 +26,8 @@ using System.IO; using System.Reflection; -namespace Portable.Text { +namespace Portable.Text +{ // This class assists encoding classes for the large CJK character // sets by providing pointer access to table data in the resource // section of the current assembly. @@ -41,55 +42,62 @@ sealed class CodeTable : IDisposable Stream stream; // Load a code table from the resource section of this assembly. - public CodeTable (string name) + public CodeTable(string name) { +#if NETFX_40 + stream = typeof(CodeTable).Assembly.GetManifestResourceStream(name); +#else stream = typeof (CodeTable).GetTypeInfo ().Assembly.GetManifestResourceStream (name); +#endif if (stream == null) - throw new NotSupportedException ("Encoding not supported."); + throw new NotSupportedException("Encoding not supported."); } // Implement the IDisposable interface. - public void Dispose () + public void Dispose() { - if (stream != null) { - stream.Dispose (); + if (stream != null) + { + stream.Dispose(); stream = null; } } - public byte[] GetSection (int num) + public byte[] GetSection(int num) { // If the table has been disposed, then bail out. if (stream == null) return null; // Scan through the stream looking for the section. - byte[] header = new byte [8]; + byte[] header = new byte[8]; long length = stream.Length; int sectNum, sectLen; long posn = 0; - while ((posn + 8) <= length) { + while ((posn + 8) <= length) + { // Read the next header block. stream.Position = posn; - if (stream.Read (header, 0, 8) != 8) + if (stream.Read(header, 0, 8) != 8) break; // Decode the fields in the header block. - sectNum = ((int)(header [0])) | - (((int)(header [1])) << 8) | - (((int)(header [2])) << 16) | - (((int)(header [3])) << 24); - sectLen = ((int)(header [4])) | - (((int)(header [5])) << 8) | - (((int)(header [6])) << 16) | - (((int)(header [7])) << 24); + sectNum = ((int)(header[0])) | + (((int)(header[1])) << 8) | + (((int)(header[2])) << 16) | + (((int)(header[3])) << 24); + sectLen = ((int)(header[4])) | + (((int)(header[5])) << 8) | + (((int)(header[6])) << 16) | + (((int)(header[7])) << 24); // Is this the section we are looking for? - if (sectNum == num) { - byte[] buf = new byte [sectLen]; - if (stream.Read (buf, 0, sectLen) != sectLen) + if (sectNum == num) + { + byte[] buf = new byte[sectLen]; + if (stream.Read(buf, 0, sectLen) != sectLen) break; return buf; diff --git a/Portable.Text.Encoding/CJK/GB18030Source.cs b/Portable.Text.Encoding/CJK/GB18030Source.cs index 9ca22e0..fc54bdf 100644 --- a/Portable.Text.Encoding/CJK/GB18030Source.cs +++ b/Portable.Text.Encoding/CJK/GB18030Source.cs @@ -19,7 +19,7 @@ class GB18030Map public readonly long GEnd; public readonly bool Dummy; // This range is actually not usable. - public GB18030Map (int ustart, int uend, long gstart, long gend, bool dummy) + public GB18030Map(int ustart, int uend, long gstart, long gend, bool dummy) { this.UStart = ustart; this.UEnd = uend; @@ -29,41 +29,46 @@ public GB18030Map (int ustart, int uend, long gstart, long gend, bool dummy) } } - GB18030Source () + GB18030Source() { } static readonly byte[] gbx2uni; static readonly byte[] uni2gbx; - static GB18030Source () + static GB18030Source() { +#if NETFX_40 + var assembly = typeof(GB18030Source).Assembly; +#else var assembly = typeof (GB18030Source).GetTypeInfo ().Assembly; +#endif - using (var stream = assembly.GetManifestResourceStream ("gb18030.table")) { + using (var stream = assembly.GetManifestResourceStream("gb18030.table")) + { var buf = new byte[4]; int size; - stream.Read (buf, 0, buf.Length); + stream.Read(buf, 0, buf.Length); size = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3]); gbx2uni = new byte[size]; - stream.Read (gbx2uni, 0, size); + stream.Read(gbx2uni, 0, size); - stream.Read (buf, 0, buf.Length); + stream.Read(buf, 0, buf.Length); size = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3]); uni2gbx = new byte[size]; - stream.Read (uni2gbx, 0, size); + stream.Read(uni2gbx, 0, size); } } - static readonly long gbxBase = FromGBXRaw (0x81, 0x30, 0x81, 0x30, false); - static readonly long gbxSuppBase = FromGBXRaw (0x90, 0x30, 0x81, 0x30, false); + static readonly long gbxBase = FromGBXRaw(0x81, 0x30, 0x81, 0x30, false); + static readonly long gbxSuppBase = FromGBXRaw(0x90, 0x30, 0x81, 0x30, false); // See http://icu.sourceforge.net/docs/papers/gb18030.html // and referenced XML mapping table. - static readonly GB18030Map [] ranges = { + static readonly GB18030Map[] ranges = { // rawmap: 0x0080-0x0451 new GB18030Map (0x0452, 0x200F, FromGBXRaw (0x81, 0x30, 0xD3, 0x30, false), FromGBXRaw (0x81, 0x36, 0xA5, 0x31, false), false), // rawmap: 0x2010-0x2642 @@ -99,31 +104,32 @@ static GB18030Source () new GB18030Map (0xFFE6, 0xFFFF, FromGBXRaw (0x84, 0x31, 0xA2, 0x34, false), FromGBXRaw (0x84, 0x31, 0xA4, 0x39, false), false), }; - public static void Unlinear (byte [] bytes, int start, long gbx) + public static void Unlinear(byte[] bytes, int start, long gbx) { - fixed (byte* bptr = bytes) { - Unlinear (bptr + start, gbx); + fixed (byte* bptr = bytes) + { + Unlinear(bptr + start, gbx); } } - public static unsafe void Unlinear (byte* bytes, long gbx) + public static unsafe void Unlinear(byte* bytes, long gbx) { - bytes [3] = (byte) (gbx % 10 + 0x30); + bytes[3] = (byte)(gbx % 10 + 0x30); gbx /= 10; - bytes [2] = (byte) (gbx % 126 + 0x81); + bytes[2] = (byte)(gbx % 126 + 0x81); gbx /= 126; - bytes [1] = (byte) (gbx % 10 + 0x30); + bytes[1] = (byte)(gbx % 10 + 0x30); gbx /= 10; - bytes [0] = (byte) (gbx + 0x81); + bytes[0] = (byte)(gbx + 0x81); } // negative (invalid) or positive (valid) - public static long FromGBX (byte [] bytes, int start) + public static long FromGBX(byte[] bytes, int start) { - byte b1 = bytes [start]; - byte b2 = bytes [start + 1]; - byte b3 = bytes [start + 2]; - byte b4 = bytes [start + 3]; + byte b1 = bytes[start]; + byte b2 = bytes[start + 1]; + byte b3 = bytes[start + 2]; + byte b4 = bytes[start + 3]; if (b1 < 0x81 || b1 == 0xFF) return -1; @@ -135,57 +141,61 @@ public static long FromGBX (byte [] bytes, int start) return -4; if (b1 >= 0x90) - return FromGBXRaw (b1, b2, b3, b4, true); + return FromGBXRaw(b1, b2, b3, b4, true); - long linear = FromGBXRaw (b1, b2, b3, b4, false); + long linear = FromGBXRaw(b1, b2, b3, b4, false); long rawOffset = 0; long startIgnore = 0; - foreach (var range in ranges) { + foreach (var range in ranges) + { if (linear < range.GStart) - return ToUcsRaw ((int)(linear - startIgnore + rawOffset)); + return ToUcsRaw((int)(linear - startIgnore + rawOffset)); if (linear <= range.GEnd) return linear - gbxBase - range.GStart + range.UStart; - if (range.GStart != 0) { + if (range.GStart != 0) + { rawOffset += range.GStart - startIgnore; startIgnore = range.GEnd + 1; } } -// return ToUcsRaw ((int) (linear - gbxBase)); - throw new Exception (string.Format ("GB18030 INTERNAL ERROR (should not happen): GBX {0:x02} {1:x02} {2:x02} {3:x02}", b1, b2, b3, b4)); + // return ToUcsRaw ((int) (linear - gbxBase)); + throw new Exception(string.Format("GB18030 INTERNAL ERROR (should not happen): GBX {0:x02} {1:x02} {2:x02} {3:x02}", b1, b2, b3, b4)); } - public static long FromUCSSurrogate (int cp) + public static long FromUCSSurrogate(int cp) { return cp + gbxSuppBase; } - public static long FromUCS (int cp) + public static long FromUCS(int cp) { long startIgnore = 0x80; long rawOffset = 0; - foreach (var range in ranges) { + foreach (var range in ranges) + { if (cp < range.UStart) - return ToGbxRaw ((int) (cp - startIgnore + rawOffset)); + return ToGbxRaw((int)(cp - startIgnore + rawOffset)); if (cp <= range.UEnd) return cp - range.UStart + range.GStart; - if (range.GStart != 0) { + if (range.GStart != 0) + { rawOffset += range.UStart - startIgnore; startIgnore = range.UEnd + 1; } } - throw new Exception (String.Format ("GB18030 INTERNAL ERROR (should not happen): UCS {0:x06}", cp)); + throw new Exception(String.Format("GB18030 INTERNAL ERROR (should not happen): UCS {0:x06}", cp)); } - static long FromGBXRaw (byte b1, byte b2, byte b3, byte b4, bool supp) + static long FromGBXRaw(byte b1, byte b2, byte b3, byte b4, bool supp) { // 126 = 0xFE - 0x80 return (((b1 - (supp ? 0x90 : 0x81)) * 10 + @@ -194,12 +204,12 @@ static long FromGBXRaw (byte b1, byte b2, byte b3, byte b4, bool supp) b4 - 0x30 + (supp ? 0x10000 : 0); } - static int ToUcsRaw (int idx) + static int ToUcsRaw(int idx) { return gbx2uni[idx * 2] * 0x100 + gbx2uni[idx * 2 + 1]; } - static long ToGbxRaw (int idx) + static long ToGbxRaw(int idx) { if (idx < 0 || idx * 2 + 1 >= uni2gbx.Length) return -1; diff --git a/Portable.Text.Encoding/Encoding.cs b/Portable.Text.Encoding/Encoding.cs index a2dbf37..a2304f1 100644 --- a/Portable.Text.Encoding/Encoding.cs +++ b/Portable.Text.Encoding/Encoding.cs @@ -30,9 +30,9 @@ namespace Portable.Text { public abstract class Encoding - #if !STANDALONE +#if !STANDALONE : System.Text.Encoding - #endif +#endif { // Code page used by this encoding. internal readonly int codePage; @@ -40,76 +40,83 @@ public abstract class Encoding bool is_readonly = true; // Constructor. - protected Encoding () + protected Encoding() { } - protected Encoding (int codePage) + protected Encoding(int codePage) { this.codePage = windows_code_page = codePage; - switch (codePage) { - default: - // MS has "InternalBestFit{Decoder|Encoder}Fallback - // here, but we dunno what they are for. - decoder_fallback = DecoderFallback.ReplacementFallback; - encoder_fallback = EncoderFallback.ReplacementFallback; - break; - case 20127: // ASCII - case 54936: // GB18030 - decoder_fallback = DecoderFallback.ReplacementFallback; - encoder_fallback = EncoderFallback.ReplacementFallback; - break; - case 1200: // UTF16 - case 1201: // UTF16 - case 12000: // UTF32 - case 12001: // UTF32 - case 65000: // UTF7 - case 65001: // UTF8 - decoder_fallback = DecoderFallback.StandardSafeFallback; - encoder_fallback = EncoderFallback.StandardSafeFallback; - break; + switch (codePage) + { + default: + // MS has "InternalBestFit{Decoder|Encoder}Fallback + // here, but we dunno what they are for. + decoder_fallback = DecoderFallback.ReplacementFallback; + encoder_fallback = EncoderFallback.ReplacementFallback; + break; + case 20127: // ASCII + case 54936: // GB18030 + decoder_fallback = DecoderFallback.ReplacementFallback; + encoder_fallback = EncoderFallback.ReplacementFallback; + break; + case 1200: // UTF16 + case 1201: // UTF16 + case 12000: // UTF32 + case 12001: // UTF32 + case 65000: // UTF7 + case 65001: // UTF8 + decoder_fallback = DecoderFallback.StandardSafeFallback; + encoder_fallback = EncoderFallback.StandardSafeFallback; + break; } } DecoderFallback decoder_fallback; EncoderFallback encoder_fallback; - public bool IsReadOnly { + public bool IsReadOnly + { get { return is_readonly; } } - public virtual bool IsSingleByte { + public virtual bool IsSingleByte + { get { return false; } } - public DecoderFallback DecoderFallback { + public DecoderFallback DecoderFallback + { get { return decoder_fallback; } - set { + set + { if (IsReadOnly) - throw new InvalidOperationException ("This Encoding is readonly."); + throw new InvalidOperationException("This Encoding is readonly."); if (value == null) - throw new ArgumentNullException (); + throw new ArgumentNullException(); decoder_fallback = value; } } - public EncoderFallback EncoderFallback { + public EncoderFallback EncoderFallback + { get { return encoder_fallback; } - set { + set + { if (IsReadOnly) - throw new InvalidOperationException ("This Encoding is readonly."); + throw new InvalidOperationException("This Encoding is readonly."); if (value == null) - throw new ArgumentNullException (); + throw new ArgumentNullException(); encoder_fallback = value; } } - internal void SetFallbackInternal (EncoderFallback e, DecoderFallback d) + internal void SetFallbackInternal(EncoderFallback e, DecoderFallback d) { if (e != null) encoder_fallback = e; @@ -119,276 +126,285 @@ internal void SetFallbackInternal (EncoderFallback e, DecoderFallback d) } // Convert between two encodings. - public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding, byte[] bytes) + public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, byte[] bytes) { if (srcEncoding == null) - throw new ArgumentNullException ("srcEncoding"); + throw new ArgumentNullException("srcEncoding"); if (dstEncoding == null) - throw new ArgumentNullException ("dstEncoding"); + throw new ArgumentNullException("dstEncoding"); if (bytes == null) - throw new ArgumentNullException ("bytes"); + throw new ArgumentNullException("bytes"); - return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, 0, bytes.Length)); + return dstEncoding.GetBytes(srcEncoding.GetChars(bytes, 0, bytes.Length)); } - public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding, byte[] bytes, int index, int count) + public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, byte[] bytes, int index, int count) { if (srcEncoding == null) - throw new ArgumentNullException ("srcEncoding"); + throw new ArgumentNullException("srcEncoding"); if (dstEncoding == null) - throw new ArgumentNullException ("dstEncoding"); + throw new ArgumentNullException("dstEncoding"); if (bytes == null) - throw new ArgumentNullException ("bytes"); + throw new ArgumentNullException("bytes"); if (index < 0 || index > bytes.Length) - throw new ArgumentOutOfRangeException ("index"); + throw new ArgumentOutOfRangeException("index"); if (count < 0 || (bytes.Length - index) < count) - throw new ArgumentOutOfRangeException ("count"); + throw new ArgumentOutOfRangeException("count"); - return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, index, count)); + return dstEncoding.GetBytes(srcEncoding.GetChars(bytes, index, count)); } // Determine if two Encoding objects are equal. - public override bool Equals (object obj) + public override bool Equals(object obj) { var encoding = obj as Encoding; - return encoding != null && codePage == encoding.codePage && DecoderFallback.Equals (encoding.DecoderFallback) && EncoderFallback.Equals (encoding.EncoderFallback); + return encoding != null && codePage == encoding.codePage && DecoderFallback.Equals(encoding.DecoderFallback) && EncoderFallback.Equals(encoding.EncoderFallback); } - #if STANDALONE +#if STANDALONE // Get the number of characters needed to encode a character buffer. public abstract int GetByteCount (char[] chars, int index, int count); - #endif +#endif // Convenience wrappers for "GetByteCount". - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - int GetByteCount (string s) +#endif + int GetByteCount(string s) { if (s == null) - throw new ArgumentNullException ("s"); + throw new ArgumentNullException("s"); if (s.Length == 0) return 0; - unsafe { - fixed (char* cptr = s) { - return GetByteCount (cptr, s.Length); + unsafe + { + fixed (char* cptr = s) + { + return GetByteCount(cptr, s.Length); } } } - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - int GetByteCount (char[] chars) +#endif + int GetByteCount(char[] chars) { if (chars == null) - throw new ArgumentNullException ("chars"); + throw new ArgumentNullException("chars"); - return GetByteCount (chars, 0, chars.Length); + return GetByteCount(chars, 0, chars.Length); } - #if STANDALONE +#if STANDALONE // Get the bytes that result from encoding a character buffer. public abstract int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex); - #endif +#endif // Convenience wrappers for "GetBytes". - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - int GetBytes (string s, int charIndex, int charCount, byte[] bytes, int byteIndex) +#endif + int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) { if (s == null) - throw new ArgumentNullException ("s"); + throw new ArgumentNullException("s"); if (charIndex < 0 || charIndex > s.Length) - throw new ArgumentOutOfRangeException ("charIndex"); + throw new ArgumentOutOfRangeException("charIndex"); if (charCount < 0 || charIndex > (s.Length - charCount)) - throw new ArgumentOutOfRangeException ("charCount"); + throw new ArgumentOutOfRangeException("charCount"); if (byteIndex < 0 || byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException ("byteIndex"); + throw new ArgumentOutOfRangeException("byteIndex"); if (charCount == 0 || bytes.Length == byteIndex) return 0; - unsafe { - fixed (char* cptr = s) { - fixed (byte* bptr = bytes) { - return GetBytes (cptr + charIndex, charCount, bptr + byteIndex, bytes.Length - byteIndex); + unsafe + { + fixed (char* cptr = s) + { + fixed (byte* bptr = bytes) + { + return GetBytes(cptr + charIndex, charCount, bptr + byteIndex, bytes.Length - byteIndex); } } } } - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - byte[] GetBytes (string s) +#endif + byte[] GetBytes(string s) { if (s == null) - throw new ArgumentNullException ("s"); + throw new ArgumentNullException("s"); if (s.Length == 0) return new byte[0]; - int byteCount = GetByteCount (s); + int byteCount = GetByteCount(s); if (byteCount == 0) return new byte[0]; - unsafe { - fixed (char* cptr = s) { - var bytes = new byte [byteCount]; + unsafe + { + fixed (char* cptr = s) + { + var bytes = new byte[byteCount]; - fixed (byte* bptr = bytes) { - GetBytes (cptr, s.Length, bptr, byteCount); + fixed (byte* bptr = bytes) + { + GetBytes(cptr, s.Length, bptr, byteCount); return bytes; } } } } - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - byte[] GetBytes (char[] chars, int index, int count) +#endif + byte[] GetBytes(char[] chars, int index, int count) { - int numBytes = GetByteCount (chars, index, count); - var bytes = new byte [numBytes]; + int numBytes = GetByteCount(chars, index, count); + var bytes = new byte[numBytes]; - GetBytes (chars, index, count, bytes, 0); + GetBytes(chars, index, count, bytes, 0); return bytes; } - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - byte[] GetBytes (char[] chars) +#endif + byte[] GetBytes(char[] chars) { - int numBytes = GetByteCount (chars, 0, chars.Length); - var bytes = new byte [numBytes]; + int numBytes = GetByteCount(chars, 0, chars.Length); + var bytes = new byte[numBytes]; - GetBytes (chars, 0, chars.Length, bytes, 0); + GetBytes(chars, 0, chars.Length, bytes, 0); return bytes; } - #if STANDALONE +#if STANDALONE // Get the number of characters needed to decode a byte buffer. public abstract int GetCharCount (byte[] bytes, int index, int count); - #endif +#endif // Convenience wrappers for "GetCharCount". - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - int GetCharCount (byte[] bytes) +#endif + int GetCharCount(byte[] bytes) { if (bytes == null) - throw new ArgumentNullException ("bytes"); + throw new ArgumentNullException("bytes"); - return GetCharCount (bytes, 0, bytes.Length); + return GetCharCount(bytes, 0, bytes.Length); } - #if STANDALONE +#if STANDALONE // Get the characters that result from decoding a byte buffer. public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex); - #endif +#endif // Convenience wrappers for "GetChars". - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - char[] GetChars (byte[] bytes, int index, int count) +#endif + char[] GetChars(byte[] bytes, int index, int count) { - int numChars = GetCharCount (bytes, index, count); - char[] chars = new char [numChars]; - GetChars (bytes, index, count, chars, 0); + int numChars = GetCharCount(bytes, index, count); + char[] chars = new char[numChars]; + GetChars(bytes, index, count, chars, 0); return chars; } - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - char[] GetChars (byte[] bytes) +#endif + char[] GetChars(byte[] bytes) { if (bytes == null) - throw new ArgumentNullException ("bytes"); + throw new ArgumentNullException("bytes"); - int numChars = GetCharCount (bytes, 0, bytes.Length); - char[] chars = new char [numChars]; - GetChars (bytes, 0, bytes.Length, chars, 0); + int numChars = GetCharCount(bytes, 0, bytes.Length); + char[] chars = new char[numChars]; + GetChars(bytes, 0, bytes.Length, chars, 0); return chars; } // Get a decoder that forwards requests to this object. - #if STANDALONE +#if STANDALONE public virtual Decoder - #else +#else public override System.Text.Decoder - #endif - GetDecoder () +#endif + GetDecoder() { - return new ForwardingDecoder (this); + return new ForwardingDecoder(this); } // Get an encoder that forwards requests to this object. - #if STANDALONE +#if STANDALONE public virtual Encoder - #else +#else public override System.Text.Encoder - #endif - GetEncoder () +#endif + GetEncoder() { - return new ForwardingEncoder (this); + return new ForwardingEncoder(this); } // Get an encoder for a specific code page. - public static Encoding GetEncoding (int codepage) + public static Encoding GetEncoding(int codepage) { if (codepage < 0 || codepage > 0xffff) - throw new ArgumentOutOfRangeException ("codepage", "Valid values are between 0 and 65535, inclusive."); + throw new ArgumentOutOfRangeException("codepage", "Valid values are between 0 and 65535, inclusive."); // Check for the builtin code pages first. - switch (codepage) { - case ASCIIEncoding.ASCII_CODE_PAGE: return ASCII; - case UTF7Encoding.UTF7_CODE_PAGE: return UTF7; - case UTF8Encoding.UTF8_CODE_PAGE: return UTF8; - case UTF32Encoding.UTF32_CODE_PAGE: return UTF32; - case UTF32Encoding.BIG_UTF32_CODE_PAGE: return BigEndianUTF32; - case UnicodeEncoding.UNICODE_CODE_PAGE: return Unicode; - case UnicodeEncoding.BIG_UNICODE_CODE_PAGE: return BigEndianUnicode; - case Latin1Encoding.ISOLATIN_CODE_PAGE: return ISOLatin1; - case 0: return Default; + switch (codepage) + { + case ASCIIEncoding.ASCII_CODE_PAGE: return ASCII; + case UTF7Encoding.UTF7_CODE_PAGE: return UTF7; + case UTF8Encoding.UTF8_CODE_PAGE: return UTF8; + case UTF32Encoding.UTF32_CODE_PAGE: return UTF32; + case UTF32Encoding.BIG_UTF32_CODE_PAGE: return BigEndianUTF32; + case UnicodeEncoding.UNICODE_CODE_PAGE: return Unicode; + case UnicodeEncoding.BIG_UNICODE_CODE_PAGE: return BigEndianUnicode; + case Latin1Encoding.ISOLATIN_CODE_PAGE: return ISOLatin1; + case 0: return Default; } // Build a code page class name. @@ -396,78 +412,85 @@ public static Encoding GetEncoding (int codepage) Encoding encoding; // Look for a code page converter in this assembly. - var assembly = typeof (Encoding).GetTypeInfo ().Assembly; - var type = assembly.GetType (className); +#if NETFX_40 + var assembly = typeof(Encoding).Assembly; +#else + var assembly = typeof(Encoding).GetTypeInfo().Assembly; +#endif + var type = assembly.GetType(className); - if (type != null) { - encoding = (Encoding) Activator.CreateInstance (type); + if (type != null) + { + encoding = (Encoding)Activator.CreateInstance(type); encoding.is_readonly = true; return encoding; } // Look in any assembly, in case the application // has provided its own code page handler. - type = Type.GetType (className); - if (type != null) { - encoding = (Encoding) Activator.CreateInstance (type); + type = Type.GetType(className); + if (type != null) + { + encoding = (Encoding)Activator.CreateInstance(type); encoding.is_readonly = true; return encoding; } // We have no idea how to handle this code page. - throw new NotSupportedException (string.Format ("CodePage {0} not supported", codepage)); + throw new NotSupportedException(string.Format("CodePage {0} not supported", codepage)); } - public virtual Encoding Clone () + public new virtual Encoding Clone() { - var encoding = (Encoding) MemberwiseClone (); + var encoding = (Encoding)MemberwiseClone(); encoding.is_readonly = false; return encoding; } - public static Encoding GetEncoding (int codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback) + public static Encoding GetEncoding(int codepage, EncoderFallback encoderFallback, DecoderFallback decoderFallback) { if (encoderFallback == null) - throw new ArgumentNullException ("encoderFallback"); + throw new ArgumentNullException("encoderFallback"); if (decoderFallback == null) - throw new ArgumentNullException ("decoderFallback"); + throw new ArgumentNullException("decoderFallback"); - var encoding = GetEncoding (codepage).Clone (); + var encoding = GetEncoding(codepage).Clone(); encoding.is_readonly = false; encoding.encoder_fallback = encoderFallback; encoding.decoder_fallback = decoderFallback; return encoding; } - public static Encoding GetEncoding (string name, EncoderFallback encoderFallback, DecoderFallback decoderFallback) + public static Encoding GetEncoding(string name, EncoderFallback encoderFallback, DecoderFallback decoderFallback) { if (encoderFallback == null) - throw new ArgumentNullException ("encoderFallback"); + throw new ArgumentNullException("encoderFallback"); if (decoderFallback == null) - throw new ArgumentNullException ("decoderFallback"); + throw new ArgumentNullException("decoderFallback"); - var encoding = GetEncoding (name).Clone (); + var encoding = GetEncoding(name).Clone(); encoding.is_readonly = false; encoding.encoder_fallback = encoderFallback; encoding.decoder_fallback = decoderFallback; return encoding; } - static EncodingInfo [] encoding_infos; + static EncodingInfo[] encoding_infos; // FIXME: As everyone would agree, this implementation is so *hacky* // and could be very easily broken. But since there is a test for // this method to make sure that this method always returns // the same number and content of encoding infos, this won't // matter practically. - public static EncodingInfo[] GetEncodings () + public static EncodingInfo[] GetEncodings() { - if (encoding_infos == null) { - var codepages = new [] { + if (encoding_infos == null) + { + var codepages = new[] { 37, 437, 500, 708, - 850, 852, 855, 857, 858, 860, 861, 862, 863, + 850, 852, 855, 857, 858, 860, 861, 862, 863, 864, 865, 866, 869, 870, 874, 875, 932, 936, 949, 950, 1026, 1047, 1140, 1141, 1142, 1143, 1144, @@ -486,19 +509,19 @@ public static EncodingInfo[] GetEncodings () 65000, 65001 }; - encoding_infos = new EncodingInfo [codepages.Length]; + encoding_infos = new EncodingInfo[codepages.Length]; for (int i = 0; i < codepages.Length; i++) - encoding_infos [i] = new EncodingInfo (codepages [i]); + encoding_infos[i] = new EncodingInfo(codepages[i]); } return encoding_infos; } - public bool IsAlwaysNormalized () + public bool IsAlwaysNormalized() { - return IsAlwaysNormalized (NormalizationForm.FormC); + return IsAlwaysNormalized(NormalizationForm.FormC); } - public virtual bool IsAlwaysNormalized (NormalizationForm form) + public virtual bool IsAlwaysNormalized(NormalizationForm form) { // umm, ASCIIEncoding should have overriden this method, no? return form == NormalizationForm.FormC && this is ASCIIEncoding; @@ -506,98 +529,103 @@ public virtual bool IsAlwaysNormalized (NormalizationForm form) // Get an encoding object for a specific web encoding name. public static - #if !STANDALONE +#if !STANDALONE new - #endif - Encoding GetEncoding (string name) +#endif + Encoding GetEncoding(string name) { // Validate the parameters. if (name == null) - throw new ArgumentNullException ("name"); + throw new ArgumentNullException("name"); - string converted = name.ToLowerInvariant ().Replace ('-', '_'); + string converted = name.ToLowerInvariant().Replace('-', '_'); // Builtin web encoding names and the corresponding code pages. - switch (converted) { - case "ascii": - case "us_ascii": - case "us": - case "ansi_x3.4_1968": - case "ansi_x3.4_1986": - case "cp367": - case "csascii": - case "ibm367": - case "iso_ir_6": - case "iso646_us": - case "iso_646.irv:1991": - return GetEncoding (ASCIIEncoding.ASCII_CODE_PAGE); - - case "utf_7": - case "csunicode11utf7": - case "unicode_1_1_utf_7": - case "unicode_2_0_utf_7": - case "x_unicode_1_1_utf_7": - case "x_unicode_2_0_utf_7": - return GetEncoding (UTF7Encoding.UTF7_CODE_PAGE); - - case "utf_8": - case "unicode_1_1_utf_8": - case "unicode_2_0_utf_8": - case "x_unicode_1_1_utf_8": - case "x_unicode_2_0_utf_8": - return GetEncoding (UTF8Encoding.UTF8_CODE_PAGE); - - case "utf_16": - case "utf_16le": - case "ucs_2": - case "unicode": - case "iso_10646_ucs2": - return GetEncoding (UnicodeEncoding.UNICODE_CODE_PAGE); - - case "unicodefffe": - case "utf_16be": - return GetEncoding (UnicodeEncoding.BIG_UNICODE_CODE_PAGE); - - case "utf_32": - case "utf_32le": - case "ucs_4": - return GetEncoding (UTF32Encoding.UTF32_CODE_PAGE); - - case "utf_32be": - return GetEncoding (UTF32Encoding.BIG_UTF32_CODE_PAGE); - - case "iso_8859_1": - case "latin1": - return GetEncoding (Latin1Encoding.ISOLATIN_CODE_PAGE); + switch (converted) + { + case "ascii": + case "us_ascii": + case "us": + case "ansi_x3.4_1968": + case "ansi_x3.4_1986": + case "cp367": + case "csascii": + case "ibm367": + case "iso_ir_6": + case "iso646_us": + case "iso_646.irv:1991": + return GetEncoding(ASCIIEncoding.ASCII_CODE_PAGE); + + case "utf_7": + case "csunicode11utf7": + case "unicode_1_1_utf_7": + case "unicode_2_0_utf_7": + case "x_unicode_1_1_utf_7": + case "x_unicode_2_0_utf_7": + return GetEncoding(UTF7Encoding.UTF7_CODE_PAGE); + + case "utf_8": + case "unicode_1_1_utf_8": + case "unicode_2_0_utf_8": + case "x_unicode_1_1_utf_8": + case "x_unicode_2_0_utf_8": + return GetEncoding(UTF8Encoding.UTF8_CODE_PAGE); + + case "utf_16": + case "utf_16le": + case "ucs_2": + case "unicode": + case "iso_10646_ucs2": + return GetEncoding(UnicodeEncoding.UNICODE_CODE_PAGE); + + case "unicodefffe": + case "utf_16be": + return GetEncoding(UnicodeEncoding.BIG_UNICODE_CODE_PAGE); + + case "utf_32": + case "utf_32le": + case "ucs_4": + return GetEncoding(UTF32Encoding.UTF32_CODE_PAGE); + + case "utf_32be": + return GetEncoding(UTF32Encoding.BIG_UTF32_CODE_PAGE); + + case "iso_8859_1": + case "latin1": + return GetEncoding(Latin1Encoding.ISOLATIN_CODE_PAGE); } // Build a web encoding class name. - string encodingName = "Portable.Text.ENC" + converted; + string encodingName = "Portable.Text.ENC" + converted; // Look for a code page converter in this assembly. - var assembly = typeof (Encoding).GetTypeInfo ().Assembly; - var type = assembly.GetType (encodingName); +#if NETFX_40 + var assembly = typeof(Encoding).Assembly; +#else + var assembly = typeof(Encoding).GetTypeInfo().Assembly; +#endif + var type = assembly.GetType(encodingName); if (type != null) - return (Encoding) Activator.CreateInstance (type); + return (Encoding)Activator.CreateInstance(type); // Look in any assembly, in case the application // has provided its own code page handler. - type = Type.GetType (encodingName); + type = Type.GetType(encodingName); if (type != null) - return (Encoding) Activator.CreateInstance (type); + return (Encoding)Activator.CreateInstance(type); // We have no idea how to handle this encoding name. - throw new ArgumentException (string.Format ("Encoding name '{0}' not supported", name), "name"); + throw new ArgumentException(string.Format("Encoding name '{0}' not supported", name), "name"); } // Get a hash code for this instance. - public override int GetHashCode () + public override int GetHashCode() { - return DecoderFallback.GetHashCode () << 24 + EncoderFallback.GetHashCode () << 16 + codePage; + return DecoderFallback.GetHashCode() << 24 + EncoderFallback.GetHashCode() << 16 + codePage; } - #if STANDALONE +#if STANDALONE // Get the maximum number of bytes needed to encode a // specified number of characters. public abstract int GetMaxByteCount (int charCount); @@ -605,36 +633,36 @@ public override int GetHashCode () // Get the maximum number of characters needed to decode a // specified number of bytes. public abstract int GetMaxCharCount (int byteCount); - #endif +#endif // Get the identifying preamble for this encoding. - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - byte[] GetPreamble () +#endif + byte[] GetPreamble() { return new byte[0]; } // Decode a buffer of bytes into a string. - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - string GetString (byte[] bytes, int index, int count) +#endif + string GetString(byte[] bytes, int index, int count) { - return new string (GetChars (bytes, index, count)); + return new string(GetChars(bytes, index, count)); } - public virtual string GetString (byte[] bytes) + public virtual string GetString(byte[] bytes) { if (bytes == null) - throw new ArgumentNullException ("bytes"); + throw new ArgumentNullException("bytes"); - return GetString (bytes, 0, bytes.Length); + return GetString(bytes, 0, bytes.Length); } internal bool is_mail_news_display; @@ -647,58 +675,69 @@ public virtual string GetString (byte[] bytes) internal string web_name; // Get the mail body name for this encoding. - public virtual string BodyName { + public virtual string BodyName + { get { return body_name; } } // Get the code page represented by this object. - public virtual int CodePage { + public virtual int CodePage + { get { return codePage; } } // Get the human-readable name for this encoding. - public virtual string EncodingName { + public virtual string EncodingName + { get { return encoding_name; } } // Get the mail agent header name for this encoding. - public virtual string HeaderName { + public virtual string HeaderName + { get { return header_name; } } // Determine if this encoding can be displayed in a Web browser. - public virtual bool IsBrowserDisplay { + public virtual bool IsBrowserDisplay + { get { return is_browser_display; } } // Determine if this encoding can be saved from a Web browser. - public virtual bool IsBrowserSave { + public virtual bool IsBrowserSave + { get { return is_browser_save; } } // Determine if this encoding can be displayed in a mail/news agent. - public virtual bool IsMailNewsDisplay { + public virtual bool IsMailNewsDisplay + { get { return is_mail_news_display; } } // Determine if this encoding can be saved from a mail/news agent. - public virtual bool IsMailNewsSave { + public virtual bool IsMailNewsSave + { get { return is_mail_news_save; } } // Get the IANA-preferred Web name for this encoding. - #if STANDALONE +#if STANDALONE public virtual - #else +#else public override - #endif - string WebName { +#endif + string WebName + { get { return web_name; } } // Get the Windows code page represented by this object. - public virtual int WindowsCodePage { - get { + public virtual int WindowsCodePage + { + get + { // We make no distinction between normal and // Windows code pages in this implementation. return windows_code_page; @@ -715,15 +754,20 @@ public virtual int WindowsCodePage { static volatile Encoding utf32Encoding; static volatile Encoding bigEndianUTF32Encoding; - static readonly object lockobj = new object (); + static readonly object lockobj = new object(); // Get the standard ASCII encoding object. - public static Encoding ASCII { - get { - if (asciiEncoding == null) { - lock (lockobj) { - if (asciiEncoding == null) { - asciiEncoding = new ASCIIEncoding (); + public static Encoding ASCII + { + get + { + if (asciiEncoding == null) + { + lock (lockobj) + { + if (asciiEncoding == null) + { + asciiEncoding = new ASCIIEncoding(); //asciiEncoding.is_readonly = true; } } @@ -735,15 +779,20 @@ public static Encoding ASCII { // Get the standard big-endian Unicode encoding object. public static - #if !STANDALONE +#if !STANDALONE new - #endif - Encoding BigEndianUnicode { - get { - if (bigEndianEncoding == null) { - lock (lockobj) { - if (bigEndianEncoding == null) { - bigEndianEncoding = new UnicodeEncoding (true, true); +#endif + Encoding BigEndianUnicode + { + get + { + if (bigEndianEncoding == null) + { + lock (lockobj) + { + if (bigEndianEncoding == null) + { + bigEndianEncoding = new UnicodeEncoding(true, true); //bigEndianEncoding.is_readonly = true; } } @@ -754,17 +803,23 @@ Encoding BigEndianUnicode { } // Get the default encoding object. - public static Encoding Default { + public static Encoding Default + { get { return UTF8; } } // Get the ISO Latin1 encoding object. - static Encoding ISOLatin1 { - get { - if (isoLatin1Encoding == null) { - lock (lockobj) { - if (isoLatin1Encoding == null) { - isoLatin1Encoding = new Latin1Encoding (); + static Encoding ISOLatin1 + { + get + { + if (isoLatin1Encoding == null) + { + lock (lockobj) + { + if (isoLatin1Encoding == null) + { + isoLatin1Encoding = new Latin1Encoding(); //isoLatin1Encoding.is_readonly = true; } } @@ -775,12 +830,17 @@ static Encoding ISOLatin1 { } // Get the standard UTF-7 encoding object. - public static Encoding UTF7 { - get { - if (utf7Encoding == null) { - lock (lockobj) { - if (utf7Encoding == null) { - utf7Encoding = new UTF7Encoding (); + public static Encoding UTF7 + { + get + { + if (utf7Encoding == null) + { + lock (lockobj) + { + if (utf7Encoding == null) + { + utf7Encoding = new UTF7Encoding(); //utf7Encoding.is_readonly = true; } } @@ -792,15 +852,20 @@ public static Encoding UTF7 { // Get the standard UTF-8 encoding object. public static - #if !STANDALONE +#if !STANDALONE new - #endif - Encoding UTF8 { - get { - if (utf8EncodingWithMarkers == null) { - lock (lockobj) { - if (utf8EncodingWithMarkers == null) { - utf8EncodingWithMarkers = new UTF8Encoding (true); +#endif + Encoding UTF8 + { + get + { + if (utf8EncodingWithMarkers == null) + { + lock (lockobj) + { + if (utf8EncodingWithMarkers == null) + { + utf8EncodingWithMarkers = new UTF8Encoding(true); //utf8EncodingWithMarkers.is_readonly = true; } } @@ -812,15 +877,20 @@ Encoding UTF8 { // Get the standard little-endian Unicode encoding object. public static - #if !STANDALONE +#if !STANDALONE new - #endif - Encoding Unicode { - get { - if (unicodeEncoding == null) { - lock (lockobj) { - if (unicodeEncoding == null) { - unicodeEncoding = new UnicodeEncoding (false, true); +#endif + Encoding Unicode + { + get + { + if (unicodeEncoding == null) + { + lock (lockobj) + { + if (unicodeEncoding == null) + { + unicodeEncoding = new UnicodeEncoding(false, true); //unicodeEncoding.is_readonly = true; } } @@ -831,12 +901,17 @@ Encoding Unicode { } // Get the standard little-endian UTF-32 encoding object. - public static Encoding UTF32 { - get { - if (utf32Encoding == null) { - lock (lockobj) { - if (utf32Encoding == null) { - utf32Encoding = new UTF32Encoding (false, true); + public static Encoding UTF32 + { + get + { + if (utf32Encoding == null) + { + lock (lockobj) + { + if (utf32Encoding == null) + { + utf32Encoding = new UTF32Encoding(false, true); //utf32Encoding.is_readonly = true; } } @@ -847,12 +922,17 @@ public static Encoding UTF32 { } // Get the standard big-endian UTF-32 encoding object. - internal static Encoding BigEndianUTF32 { - get { - if (bigEndianUTF32Encoding == null) { - lock (lockobj) { - if (bigEndianUTF32Encoding == null) { - bigEndianUTF32Encoding = new UTF32Encoding (true, true); + internal static Encoding BigEndianUTF32 + { + get + { + if (bigEndianUTF32Encoding == null) + { + lock (lockobj) + { + if (bigEndianUTF32Encoding == null) + { + bigEndianUTF32Encoding = new UTF32Encoding(true, true); //bigEndianUTF32Encoding.is_readonly = true; } } @@ -867,7 +947,7 @@ sealed class ForwardingDecoder : Decoder { readonly Encoding encoding; - public ForwardingDecoder (Encoding enc) + public ForwardingDecoder(Encoding enc) { var fallback = enc.DecoderFallback; @@ -877,14 +957,14 @@ public ForwardingDecoder (Encoding enc) encoding = enc; } - public override int GetCharCount (byte[] bytes, int index, int count) + public override int GetCharCount(byte[] bytes, int index, int count) { - return encoding.GetCharCount (bytes, index, count); + return encoding.GetCharCount(bytes, index, count); } - public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { - return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex); + return encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex); } } @@ -893,7 +973,7 @@ sealed class ForwardingEncoder : Encoder { readonly Encoding encoding; - public ForwardingEncoder (Encoding enc) + public ForwardingEncoder(Encoding enc) { var fallback = enc.EncoderFallback; @@ -903,104 +983,104 @@ public ForwardingEncoder (Encoding enc) encoding = enc; } - public override int GetByteCount (char[] chars, int index, int count, bool flush) + public override int GetByteCount(char[] chars, int index, int count, bool flush) { - return encoding.GetByteCount (chars, index, count); + return encoding.GetByteCount(chars, index, count); } - public override int GetBytes (char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush) + public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush) { - return encoding.GetBytes (chars, charIndex, charCount, bytes, byteIndex); + return encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex); } } - public unsafe virtual int GetByteCount (char *chars, int count) + public unsafe virtual int GetByteCount(char* chars, int count) { if (chars == null) - throw new ArgumentNullException ("chars"); + throw new ArgumentNullException("chars"); if (count < 0) - throw new ArgumentOutOfRangeException ("count"); + throw new ArgumentOutOfRangeException("count"); - var c = new char [count]; + var c = new char[count]; for (int p = 0; p < count; p++) - c [p] = chars [p]; + c[p] = chars[p]; - return GetByteCount (c); + return GetByteCount(c); } - public unsafe virtual int GetCharCount (byte *bytes, int count) + public unsafe virtual int GetCharCount(byte* bytes, int count) { if (bytes == null) - throw new ArgumentNullException ("bytes"); + throw new ArgumentNullException("bytes"); if (count < 0) - throw new ArgumentOutOfRangeException ("count"); + throw new ArgumentOutOfRangeException("count"); - var ba = new byte [count]; + var ba = new byte[count]; for (int i = 0; i < count; i++) - ba [i] = bytes [i]; + ba[i] = bytes[i]; - return GetCharCount (ba, 0, count); + return GetCharCount(ba, 0, count); } - public unsafe virtual int GetChars (byte *bytes, int byteCount, char *chars, int charCount) + public unsafe virtual int GetChars(byte* bytes, int byteCount, char* chars, int charCount) { if (bytes == null) - throw new ArgumentNullException ("bytes"); + throw new ArgumentNullException("bytes"); if (chars == null) - throw new ArgumentNullException ("chars"); + throw new ArgumentNullException("chars"); if (charCount < 0) - throw new ArgumentOutOfRangeException ("charCount"); + throw new ArgumentOutOfRangeException("charCount"); if (byteCount < 0) - throw new ArgumentOutOfRangeException ("byteCount"); + throw new ArgumentOutOfRangeException("byteCount"); - var ba = new byte [byteCount]; + var ba = new byte[byteCount]; for (int i = 0; i < byteCount; i++) - ba [i] = bytes [i]; + ba[i] = bytes[i]; - var ret = GetChars (ba, 0, byteCount); + var ret = GetChars(ba, 0, byteCount); int top = ret.Length; if (top > charCount) - throw new ArgumentException ("charCount is less than the number of characters produced", "charCount"); + throw new ArgumentException("charCount is less than the number of characters produced", "charCount"); for (int i = 0; i < top; i++) - chars [i] = ret [i]; + chars[i] = ret[i]; return top; } - public unsafe virtual int GetBytes (char *chars, int charCount, byte *bytes, int byteCount) + public unsafe virtual int GetBytes(char* chars, int charCount, byte* bytes, int byteCount) { if (bytes == null) - throw new ArgumentNullException ("bytes"); + throw new ArgumentNullException("bytes"); if (chars == null) - throw new ArgumentNullException ("chars"); + throw new ArgumentNullException("chars"); if (charCount < 0) - throw new ArgumentOutOfRangeException ("charCount"); + throw new ArgumentOutOfRangeException("charCount"); if (byteCount < 0) - throw new ArgumentOutOfRangeException ("byteCount"); + throw new ArgumentOutOfRangeException("byteCount"); - var c = new char [charCount]; + var c = new char[charCount]; for (int i = 0; i < charCount; i++) - c [i] = chars [i]; + c[i] = chars[i]; - var b = GetBytes (c, 0, charCount); + var b = GetBytes(c, 0, charCount); int top = b.Length; if (top > byteCount) - throw new ArgumentException ("byteCount is less that the number of bytes produced", "byteCount"); + throw new ArgumentException("byteCount is less that the number of bytes produced", "byteCount"); for (int i = 0; i < top; i++) - bytes [i] = b [i]; + bytes[i] = b[i]; return b.Length; } diff --git a/Portable.Text.Encoding/Portable.Text.Encoding.WindowsUniversal81.csproj b/Portable.Text.Encoding/Portable.Text.Encoding.WindowsUniversal81.csproj deleted file mode 100644 index 2e6bba9..0000000 --- a/Portable.Text.Encoding/Portable.Text.Encoding.WindowsUniversal81.csproj +++ /dev/null @@ -1,174 +0,0 @@ - - - - Debug - AnyCPU - {B76A64F9-B00E-4243-AE89-5D024CA3B436} - {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Library - Portable.Text - Portable.Text.Encoding - v4.5 - Profile111 - 10.0 - - - true - full - false - ..\bin\Debug\Profile111 - obj\Debug\Profile111 - DEBUG;TRACE - prompt - 4 - false - true - - - pdbonly - true - ..\bin\Release\Profile111 - obj\Release\Profile111 - TRACE - prompt - 4 - false - true - - - true - - - portable.text.encoding.snk - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - big5.table - - - gb18030.table - - - gb2312.table - - - jis.table - - - ks.table - - - - - diff --git a/Portable.Text.Encoding/Portable.Text.Encoding.csproj b/Portable.Text.Encoding/Portable.Text.Encoding.csproj index 5485f88..4c2fc84 100644 --- a/Portable.Text.Encoding/Portable.Text.Encoding.csproj +++ b/Portable.Text.Encoding/Portable.Text.Encoding.csproj @@ -1,174 +1,70 @@ - - - - Debug - AnyCPU - {EEE48C75-11BE-4B50-B759-F85B5052D473} - {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Library - Portable.Text - Portable.Text.Encoding - v4.5 - Profile7 - 10.0 - - - true - full - false - ..\bin\Debug\Profile7 - obj\Debug\Profile7 - DEBUG;TRACE - prompt - 4 - false - true - - - pdbonly - true - ..\bin\Release\Profile7 - obj\Release\Profile7 - TRACE - prompt - 4 - false - true - - - true - - - portable.text.encoding.snk - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - big5.table - - - gb18030.table - - - gb2312.table - - - jis.table - - - ks.table - - - - - \ No newline at end of file + + + + net40;netstandard1.0;netstandard1.6;portable-net45+win8+wpa81+wp8 + Portable.Text.Encoding: A Portable Implementation of System.Text.Encoding. + + The main goal of this project is to supply a portable alternative to System.Text.Encoding that can be used from other PCL libraries. + + Platforms such as Windows Phone currently do not provide support for text encodings other than a limited subset of the Unicode encodings. This can be a real show-stopper when you need to work with legacy text encodings. + + * Updated to work with Xamarin.iOS. + Xamarin Inc. + Jeffrey Stedfast,Atsushi Enomoto,and Mono contributors + Copyright ©Xamarin Inc. 2016 + http://opensource.org/licenses/MIT + http://github.com/jstedfast/Portable.Text.Encoding + 0.8.4 + 0.8.4 + pcl net45 win8 win81 wp80 wpa81 monodroid monotouch xamarin + True + True + True + 0.8.4 + True + True + portable.text.encoding.snk + + + + PROFILE_111 + + + + NETFX_40 + + + + + + + + + + + + + + + + + + big5.table + + + gb18030.table + + + gb2312.table + + + jis.table + + + ks.table + + + + + + diff --git a/Portable.Text.Encoding/Properties/AssemblyInfo.cs b/Portable.Text.Encoding/Properties/AssemblyInfo.cs index f94088c..c6199de 100644 --- a/Portable.Text.Encoding/Properties/AssemblyInfo.cs +++ b/Portable.Text.Encoding/Properties/AssemblyInfo.cs @@ -1,68 +1,68 @@ -// -// AssemblyInfo.cs -// -// Author: Jeffrey Stedfast -// -// Copyright (c) 2014-2015 Xamarin Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// +//// +//// AssemblyInfo.cs +//// +//// Author: Jeffrey Stedfast +//// +//// Copyright (c) 2014-2015 Xamarin Inc. +//// +//// Permission is hereby granted, free of charge, to any person obtaining a copy +//// of this software and associated documentation files (the "Software"), to deal +//// in the Software without restriction, including without limitation the rights +//// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +//// copies of the Software, and to permit persons to whom the Software is +//// furnished to do so, subject to the following conditions: +//// +//// The above copyright notice and this permission notice shall be included in +//// all copies or substantial portions of the Software. +//// +//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +//// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +//// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +//// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +//// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +//// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +//// THE SOFTWARE. +//// -using System.Reflection; +//using System.Reflection; -// Information about this assembly is defined by the following attributes. -// Change them to the values specific to your project. -[assembly: AssemblyTitle ("Portable.Text.Encoding")] -[assembly: AssemblyDescription ("A Portable Implementation of System.Text.Encoding.")] -[assembly: AssemblyConfiguration ("")] -[assembly: AssemblyCompany ("Xamarin Inc.")] -[assembly: AssemblyProduct ("")] -[assembly: AssemblyCopyright ("Copyright © 2014-2015 Xamarin Inc. (www.xamarin.com)")] -[assembly: AssemblyTrademark ("Xamarin Inc.")] -[assembly: AssemblyCulture ("")] +//// Information about this assembly is defined by the following attributes. +//// Change them to the values specific to your project. +//[assembly: AssemblyTitle ("Portable.Text.Encoding")] +//[assembly: AssemblyDescription ("A Portable Implementation of System.Text.Encoding.")] +//[assembly: AssemblyConfiguration ("")] +//[assembly: AssemblyCompany ("Xamarin Inc.")] +//[assembly: AssemblyProduct ("")] +//[assembly: AssemblyCopyright ("Copyright © 2014-2015 Xamarin Inc. (www.xamarin.com)")] +//[assembly: AssemblyTrademark ("Xamarin Inc.")] +//[assembly: AssemblyCulture ("")] -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Micro Version -// Build Number -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -// -// Note: AssemblyVersion is what the CLR matches against at runtime, so be careful -// about updating it. The AssemblyFileVersion is the official release version while -// the AssemblyInformationalVersion is just used as a display version. -// -// Based on my current understanding, AssemblyVersion is essentially the "API Version" -// and so should only be updated when the API changes. The other 2 Version attributes -// represent the "Release Version". -// -// Making releases: -// -// If any classes, methods, or enum values have been added, bump the Micro Version -// in all version attributes and set the Build Number back to 0. -// -// If there have only been bug fixes, bump the Micro Version and/or the Build Number -// in the AssemblyFileVersion attribute. -[assembly: AssemblyInformationalVersion ("0.8.3")] -[assembly: AssemblyFileVersion ("0.8.3.0")] -[assembly: AssemblyVersion ("0.8.3.0")] +//// Version information for an assembly consists of the following four values: +//// +//// Major Version +//// Minor Version +//// Micro Version +//// Build Number +//// +//// You can specify all the values or you can default the Build and Revision Numbers +//// by using the '*' as shown below: +//// [assembly: AssemblyVersion("1.0.*")] +//// +//// Note: AssemblyVersion is what the CLR matches against at runtime, so be careful +//// about updating it. The AssemblyFileVersion is the official release version while +//// the AssemblyInformationalVersion is just used as a display version. +//// +//// Based on my current understanding, AssemblyVersion is essentially the "API Version" +//// and so should only be updated when the API changes. The other 2 Version attributes +//// represent the "Release Version". +//// +//// Making releases: +//// +//// If any classes, methods, or enum values have been added, bump the Micro Version +//// in all version attributes and set the Build Number back to 0. +//// +//// If there have only been bug fixes, bump the Micro Version and/or the Build Number +//// in the AssemblyFileVersion attribute. +//[assembly: AssemblyInformationalVersion ("0.8.3")] +//[assembly: AssemblyFileVersion ("0.8.3.0")] +//[assembly: AssemblyVersion ("0.8.3.0")]