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

Loading…
Cancel
Save