Skip to content

Commit 78eb071

Browse files
committed
Add Utf8Marshaler.FromManaged() and FromNative()
1 parent f23436b commit 78eb071

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

LibGit2Sharp/ContentChanges.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace LibGit2Sharp
1010
public class ContentChanges
1111
{
1212
private readonly StringBuilder patchBuilder = new StringBuilder();
13-
private static readonly Utf8Marshaler marshaler = (Utf8Marshaler)Utf8Marshaler.GetInstance(string.Empty);
1413

1514
protected ContentChanges()
1615
{
@@ -47,15 +46,15 @@ internal static bool IsBinaryDelta(GitDiffDelta delta)
4746

4847
private int HunkCallback(IntPtr data, GitDiffDelta delta, GitDiffRange range, IntPtr header, uint headerlen)
4948
{
50-
string decodedContent = marshaler.NativeToString(header, headerlen);
49+
string decodedContent = Utf8Marshaler.FromNative(header, headerlen);
5150

5251
PatchBuilder.AppendFormat("{0}", decodedContent);
5352
return 0;
5453
}
5554

5655
private int LineCallback(IntPtr data, GitDiffDelta delta, GitDiffRange range, GitDiffLineOrigin lineorigin, IntPtr content, uint contentlen)
5756
{
58-
string decodedContent = marshaler.NativeToString(content, contentlen);
57+
string decodedContent = Utf8Marshaler.FromNative(content, contentlen);
5958

6059
string prefix;
6160

LibGit2Sharp/Core/Ensure.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace LibGit2Sharp.Core
1010
[DebuggerStepThrough]
1111
internal static class Ensure
1212
{
13-
private static readonly Utf8Marshaler marshaler = (Utf8Marshaler)Utf8Marshaler.GetInstance(string.Empty);
14-
1513
/// <summary>
1614
/// Checks an argument to ensure it isn't null.
1715
/// </summary>
@@ -75,7 +73,7 @@ public static void Success(int result, bool allowPositiveResult = false)
7573
}
7674
else
7775
{
78-
errorMessage = (string)marshaler.MarshalNativeToManaged(error.Message);
76+
errorMessage = Utf8Marshaler.FromNative(error.Message);
7977
}
8078

8179
throw new LibGit2SharpException(

LibGit2Sharp/Core/Libgit2UnsafeHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ namespace LibGit2Sharp.Core
66
{
77
internal static unsafe class Libgit2UnsafeHelper
88
{
9-
private static readonly Utf8Marshaler marshaler = (Utf8Marshaler)Utf8Marshaler.GetInstance(string.Empty);
10-
119
public static IList<string> ListAllBranchNames(RepositorySafeHandle repo, GitBranchType types)
1210
{
1311
UnSafeNativeMethods.git_strarray strArray;
@@ -55,7 +53,7 @@ private static IList<string> BuildListOf(UnSafeNativeMethods.git_strarray strArr
5553
uint numberOfEntries = gitStrArray->size;
5654
for (uint i = 0; i < numberOfEntries; i++)
5755
{
58-
var name = (string)marshaler.MarshalNativeToManaged((IntPtr)gitStrArray->strings[i]);
56+
var name = Utf8Marshaler.FromNative((IntPtr)gitStrArray->strings[i]);
5957
list.Add(name);
6058
}
6159

LibGit2Sharp/Core/Utf8Marshaler.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ protected unsafe string NativeToString(IntPtr pNativeData)
6262

6363
var length = (uint)(walk - (byte*)pNativeData);
6464

65-
return NativeToString(pNativeData, length);
65+
return FromNative(pNativeData, length);
6666
}
6767

68-
public string NativeToString(IntPtr pNativeData, uint length)
68+
public static string FromNative(IntPtr pNativeData, uint length)
6969
{
7070
// should not be null terminated
7171
var strbuf = new byte[length];
@@ -93,6 +93,16 @@ public int GetNativeDataSize()
9393

9494
#endregion
9595

96+
public static IntPtr FromManaged(string managedObj)
97+
{
98+
return staticInstance.MarshalManagedToNative(managedObj);
99+
}
100+
101+
public static string FromNative(IntPtr pNativeData)
102+
{
103+
return (string)staticInstance.MarshalNativeToManaged(pNativeData);
104+
}
105+
96106
public static ICustomMarshaler GetInstance(string cookie)
97107
{
98108
return staticInstance;

LibGit2Sharp/Signature.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ public class Signature
1414
private readonly string name;
1515
private readonly string email;
1616

17-
private static readonly Utf8Marshaler marshaler = (Utf8Marshaler)Utf8Marshaler.GetInstance(string.Empty);
18-
1917
internal Signature(IntPtr signaturePtr)
2018
{
2119
var handle = new GitSignature();
2220
Marshal.PtrToStructure(signaturePtr, handle);
2321

24-
// XXX: This is unbelievably hacky, but I can't get the
25-
// Utf8Marshaller to work properly.
26-
name = (string)marshaler.MarshalNativeToManaged(handle.Name);
27-
email = (string)marshaler.MarshalNativeToManaged(handle.Email);
22+
name = Utf8Marshaler.FromNative(handle.Name);
23+
email = Utf8Marshaler.FromNative(handle.Email);
2824
when = Epoch.ToDateTimeOffset(handle.When.Time, handle.When.Offset);
2925
}
3026

LibGit2Sharp/TreeChanges.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ namespace LibGit2Sharp
1313
/// </summary>
1414
public class TreeChanges : IEnumerable<TreeEntryChanges>
1515
{
16-
private static readonly Utf8Marshaler marshaler = (Utf8Marshaler)Utf8Marshaler.GetInstance(string.Empty);
17-
1816
private readonly IDictionary<string, TreeEntryChanges> changes = new Dictionary<string, TreeEntryChanges>();
1917
private readonly List<TreeEntryChanges> added = new List<TreeEntryChanges>();
2018
private readonly List<TreeEntryChanges> deleted = new List<TreeEntryChanges>();
@@ -43,8 +41,8 @@ internal TreeChanges(DiffListSafeHandle diff)
4341

4442
private int PrintCallBack(IntPtr data, GitDiffDelta delta, GitDiffRange range, GitDiffLineOrigin lineorigin, IntPtr content, uint contentlen)
4543
{
46-
string formattedoutput = marshaler.NativeToString(content, contentlen);
47-
var currentFilePath = (string)marshaler.MarshalNativeToManaged(delta.NewFile.Path);
44+
var formattedoutput = Utf8Marshaler.FromNative(content, contentlen);
45+
var currentFilePath = Utf8Marshaler.FromNative(delta.NewFile.Path);
4846

4947
AddLineChange(currentFilePath, lineorigin);
5048

@@ -87,8 +85,8 @@ private void IncrementLinesAdded(string filePath)
8785

8886
private void AddFileChange(GitDiffDelta delta)
8987
{
90-
var newFilePath = (string)marshaler.MarshalNativeToManaged(delta.NewFile.Path);
91-
var oldFilePath = (string)marshaler.MarshalNativeToManaged(delta.OldFile.Path);
88+
var newFilePath = Utf8Marshaler.FromNative(delta.NewFile.Path);
89+
var oldFilePath = Utf8Marshaler.FromNative(delta.OldFile.Path);
9290
var newMode = (Mode)delta.NewFile.Mode;
9391
var oldMode = (Mode)delta.OldFile.Mode;
9492
var newOid = new ObjectId(delta.NewFile.Oid);

0 commit comments

Comments
 (0)