Code reformat.

This commit is contained in:
2019-11-25 00:54:39 +00:00
parent 6937468728
commit a7d7c673a1
13 changed files with 356 additions and 392 deletions

View File

@@ -37,16 +37,12 @@ namespace DiscImageChef
{
public static class StringHandlers
{
/// <summary>
/// Converts a null-terminated (aka C string) ASCII byte array to a C# string
/// </summary>
/// <summary>Converts a null-terminated (aka C string) ASCII byte array to a C# string</summary>
/// <returns>The corresponding C# string</returns>
/// <param name="CString">A null-terminated (aka C string) ASCII byte array</param>
public static string CToString(byte[] CString) => CToString(CString, Encoding.ASCII);
/// <summary>
/// Converts a null-terminated (aka C string) byte array with the specified encoding to a C# string
/// </summary>
/// <summary>Converts a null-terminated (aka C string) byte array with the specified encoding to a C# string</summary>
/// <returns>The corresponding C# string</returns>
/// <param name="CString">A null-terminated (aka C string) byte array in the specified encoding</param>
/// <param name="encoding">Encoding.</param>
@@ -54,7 +50,8 @@ namespace DiscImageChef
/// <param name="start">Start decodint at this position</param>
public static string CToString(byte[] CString, Encoding encoding, bool twoBytes = false, int start = 0)
{
if(CString == null) return null;
if(CString == null)
return null;
int len = 0;
@@ -63,9 +60,11 @@ namespace DiscImageChef
if(CString[i] == 0)
if(twoBytes)
{
if(i + 1 < CString.Length && CString[i + 1] == 0)
if(i + 1 < CString.Length &&
CString[i + 1] == 0)
{
len++;
break;
}
}
@@ -75,7 +74,8 @@ namespace DiscImageChef
len++;
}
if(twoBytes && len % 2 > 0) len--;
if(twoBytes && len % 2 > 0)
len--;
byte[] dest = new byte[len];
Array.Copy(CString, start, dest, 0, len);
@@ -83,30 +83,28 @@ namespace DiscImageChef
return len == 0 ? "" : encoding.GetString(dest);
}
/// <summary>
/// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string
/// </summary>
/// <summary>Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string</summary>
/// <returns>The corresponding C# string</returns>
/// <param name="PascalString">A length-prefixed (aka Pascal string) ASCII byte array</param>
public static string PascalToString(byte[] PascalString) => PascalToString(PascalString, Encoding.ASCII);
/// <summary>
/// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string
/// </summary>
/// <summary>Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string</summary>
/// <returns>The corresponding C# string</returns>
/// <param name="PascalString">A length-prefixed (aka Pascal string) ASCII byte array</param>
/// <param name="encoding">Encoding.</param>
/// <param name="start">Start decodint at this position</param>
public static string PascalToString(byte[] PascalString, Encoding encoding, int start = 0)
{
if(PascalString == null) return null;
if(PascalString == null)
return null;
byte length = PascalString[start];
int len = 0;
for(int i = start + 1; i < length + 1 && i < PascalString.Length; i++)
{
if(PascalString[i] == 0) break;
if(PascalString[i] == 0)
break;
len++;
}
@@ -117,43 +115,41 @@ namespace DiscImageChef
return len == 0 ? "" : encoding.GetString(dest);
}
/// <summary>
/// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string
/// </summary>
/// <summary>Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string</summary>
/// <returns>The corresponding C# string</returns>
/// <param name="SpacePaddedString">A space (' ', 0x20, ASCII SPACE) padded ASCII byte array</param>
public static string SpacePaddedToString(byte[] SpacePaddedString) =>
SpacePaddedToString(SpacePaddedString, Encoding.ASCII);
/// <summary>
/// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string
/// </summary>
/// <summary>Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string</summary>
/// <returns>The corresponding C# string</returns>
/// <param name="SpacePaddedString">A space (' ', 0x20, ASCII SPACE) padded ASCII byte array</param>
/// <param name="encoding">Encoding.</param>
/// <param name="start">Start decodint at this position</param>
public static string SpacePaddedToString(byte[] SpacePaddedString, Encoding encoding, int start = 0)
{
if(SpacePaddedString == null) return null;
if(SpacePaddedString == null)
return null;
int len = start;
for(int i = SpacePaddedString.Length; i >= start; i--)
{
if(i == start) return "";
if(i == start)
return"";
if(SpacePaddedString[i - 1] == 0x20) continue;
if(SpacePaddedString[i - 1] == 0x20)
continue;
len = i;
break;
}
return len == 0 ? "" : encoding.GetString(SpacePaddedString, start, len);
}
/// <summary>
/// Converts an OSTA compressed unicode byte array to a C# string
/// </summary>
/// <summary>Converts an OSTA compressed unicode byte array to a C# string</summary>
/// <returns>The C# string.</returns>
/// <param name="dstring">OSTA compressed unicode byte array.</param>
public static string DecompressUnicode(byte[] dstring)
@@ -162,16 +158,22 @@ namespace DiscImageChef
byte compId = dstring[0];
string temp = "";
if(compId != 8 && compId != 16) return null;
if(compId != 8 &&
compId != 16)
return null;
for(int byteIndex = 1; byteIndex < dstring.Length;)
{
if(compId == 16) unicode = (ushort)(dstring[byteIndex++] << 8);
else unicode = 0;
if(compId == 16)
unicode = (ushort)(dstring[byteIndex++] << 8);
else
unicode = 0;
if(byteIndex < dstring.Length) unicode |= dstring[byteIndex++];
if(byteIndex < dstring.Length)
unicode |= dstring[byteIndex++];
if(unicode == 0) break;
if(unicode == 0)
break;
temp += Encoding.Unicode.GetString(BitConverter.GetBytes(unicode));
}