Browse Source

Revert "Simplify the code for encodings when marshalling"

This reverts commit 3a93f8f575.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1446/head
Dimitar Dobrev 5 years ago
parent
commit
be227c2c18
  1. 45
      src/Generator/Types/Std/Stdlib.cs

45
src/Generator/Types/Std/Stdlib.cs

@ -155,7 +155,7 @@ namespace CppSharp.Types.Std
return new CustomType(typePrinter.IntPtrType); return new CustomType(typePrinter.IntPtrType);
} }
Encoding enconding = GetEncoding(); var (enconding, _) = GetEncoding();
if (enconding == Encoding.ASCII) if (enconding == Encoding.ASCII)
return new CustomType("[MarshalAs(UnmanagedType.LPStr)] string"); return new CustomType("[MarshalAs(UnmanagedType.LPStr)] string");
@ -225,8 +225,7 @@ namespace CppSharp.Types.Std
string bytes = $"__bytes{ctx.ParameterIndex}"; string bytes = $"__bytes{ctx.ParameterIndex}";
string bytePtr = $"__bytePtr{ctx.ParameterIndex}"; string bytePtr = $"__bytePtr{ctx.ParameterIndex}";
ctx.Before.WriteLine($@"byte[] {bytes} = global::System.Text.Encoding.{ ctx.Before.WriteLine($@"byte[] {bytes} = global::System.Text.Encoding.{
GetEncoding().GetType().Name.Replace( GetEncoding().Name}.GetBytes({param});");
nameof(Encoding), string.Empty)}.GetBytes({param});");
ctx.Before.WriteLine($"fixed (byte* {bytePtr} = {bytes})"); ctx.Before.WriteLine($"fixed (byte* {bytePtr} = {bytes})");
ctx.HasCodeBlock = true; ctx.HasCodeBlock = true;
ctx.Before.WriteOpenBraceAndIndent(); ctx.Before.WriteOpenBraceAndIndent();
@ -272,7 +271,7 @@ namespace CppSharp.Types.Std
textGenerator.UnindentAndWriteCloseBrace(); textGenerator.UnindentAndWriteCloseBrace();
} }
Encoding encoding = GetEncoding(); string encoding = GetEncoding().Name;
string type = GetTypeForCodePoint(encoding); string type = GetTypeForCodePoint(encoding);
var retPtr = Generator.GeneratedIdentifier($"retPtr{ctx.ParameterIndex}"); var retPtr = Generator.GeneratedIdentifier($"retPtr{ctx.ParameterIndex}");
var length = Generator.GeneratedIdentifier($"length{ctx.ParameterIndex}"); var length = Generator.GeneratedIdentifier($"length{ctx.ParameterIndex}");
@ -281,31 +280,49 @@ namespace CppSharp.Types.Std
textGenerator.WriteLine($"while (*({retPtr}++) != 0) {length} += sizeof({type});"); textGenerator.WriteLine($"while (*({retPtr}++) != 0) {length} += sizeof({type});");
ctx.Return.Write($@"global::System.Text.Encoding.{ ctx.Return.Write($@"global::System.Text.Encoding.{
encoding.GetType().Name.Replace( encoding}.GetString((byte*) {returnVarName}, {length})");
nameof(Encoding), string.Empty)}.GetString((byte*) {returnVarName}, {length})");
} }
private Encoding GetEncoding() private (Encoding Encoding, string Name) GetEncoding()
{ {
switch (GetCharWidth()) switch (GetCharWidth())
{ {
case 8: case 8:
return Context.Options.Encoding; if (Context.Options.Encoding == Encoding.ASCII)
return (Context.Options.Encoding, nameof(Encoding.ASCII));
if (Context.Options.Encoding == Encoding.UTF8)
return (Context.Options.Encoding, nameof(Encoding.UTF8));
if (Context.Options.Encoding == Encoding.UTF7)
return (Context.Options.Encoding, nameof(Encoding.UTF7));
if (Context.Options.Encoding == Encoding.BigEndianUnicode)
return (Context.Options.Encoding, nameof(Encoding.BigEndianUnicode));
if (Context.Options.Encoding == Encoding.Unicode)
return (Context.Options.Encoding, nameof(Encoding.Unicode));
if (Context.Options.Encoding == Encoding.UTF32)
return (Context.Options.Encoding, nameof(Encoding.UTF32));
break;
case 16: case 16:
return Encoding.Unicode; return (Encoding.Unicode, nameof(Encoding.Unicode));
case 32: case 32:
return Encoding.UTF32; return (Encoding.UTF32, nameof(Encoding.UTF32));
} }
throw new System.NotSupportedException( throw new System.NotSupportedException(
$"{Context.Options.Encoding.EncodingName} is not supported yet."); $"{Context.Options.Encoding.EncodingName} is not supported yet.");
} }
private static string GetTypeForCodePoint(Encoding encoding) private static string GetTypeForCodePoint(string encoding)
{ {
return encoding == Encoding.UTF32 ? "int" : switch (encoding)
encoding == Encoding.Unicode || encoding == Encoding.BigEndianUnicode ? {
"short" : "byte"; case nameof(Encoding.UTF32):
return "int";
case nameof(Encoding.Unicode):
case nameof(Encoding.BigEndianUnicode):
return "short";
default:
return "byte";
}
} }
} }

Loading…
Cancel
Save