Skip to content

Commit d1e8821

Browse files
committed
feat: Put ArrayPool usage into try-finally
1 parent a72916b commit d1e8821

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

src/GettingThingsDone.ApplicationCore/Services/LegacyExchangeActionFormat.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,23 @@ public static string ToLegacyFormat(this Action action)
5959
{
6060
var buffer = ArrayPool<char>.Shared.Rent(TotalWidth);
6161

62-
Array.Fill(buffer, ' ');
63-
64-
action.Title?.CopyTo(0, buffer, TitleStart, Math.Min(TitleWidth, action.Title.Length));
65-
66-
action.DueDate?.ToString(DueDateFormat).CopyTo(0, buffer, DueDateStart, DueDateWidth);
62+
string result;
63+
try
64+
{
65+
Array.Fill(buffer, ' ');
6766

68-
var result = new string(buffer);
67+
action.Title?.CopyTo(0, buffer, TitleStart, Math.Min(TitleWidth, action.Title.Length));
6968

70-
ArrayPool<char>.Shared.Return(buffer);
69+
action.DueDate?.ToString(DueDateFormat).CopyTo(0, buffer, DueDateStart, DueDateWidth);
7170

71+
result = new string(buffer);
72+
}
73+
finally
74+
{
75+
ArrayPool<char>.Shared.Return(buffer);
76+
}
77+
7278
return result;
7379
}
7480
}
75-
}
81+
}

src/GettingThingsDone.ApplicationCore/Services/LegacyExchangeActionListFormat.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ public static string ToLegacyFormat(this ActionList actionList)
3939
{
4040
var buffer = ArrayPool<char>.Shared.Rent(TotalWidth);
4141

42-
Array.Fill(buffer, ' ');
43-
44-
actionList.Name?.CopyTo(0, buffer, TitleStart, Math.Min(TitleWidth, actionList.Name.Length));
45-
46-
var result = new string(buffer);
42+
string result;
43+
try
44+
{
45+
Array.Fill(buffer, ' ');
4746

48-
ArrayPool<char>.Shared.Return(buffer);
47+
actionList.Name?.CopyTo(0, buffer, TitleStart, Math.Min(TitleWidth, actionList.Name.Length));
4948

49+
result = new string(buffer);
50+
}
51+
finally
52+
{
53+
ArrayPool<char>.Shared.Return(buffer);
54+
}
55+
5056
return result;
5157
}
5258
}

src/GettingThingsDone.ApplicationCore/Services/LegacyExchangeProjectFormat.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ public static string ToLegacyFormat(this Project project)
3939
{
4040
var buffer = ArrayPool<char>.Shared.Rent(TotalWidth);
4141

42-
Array.Fill(buffer, ' ');
43-
44-
project.Name?.CopyTo(0, buffer, TitleStart, Math.Min(TitleWidth, project.Name.Length));
42+
string result;
43+
try
44+
{
45+
Array.Fill(buffer, ' ');
4546

46-
var result = new string(buffer);
47+
project.Name?.CopyTo(0, buffer, TitleStart, Math.Min(TitleWidth, project.Name.Length));
4748

48-
ArrayPool<char>.Shared.Return(buffer);
49+
result = new string(buffer);
50+
}
51+
finally
52+
{
53+
ArrayPool<char>.Shared.Return(buffer);
54+
}
4955

5056
return result;
5157
}

0 commit comments

Comments
 (0)