Skip to content

Commit 49327b8

Browse files
committed
Refactored more IMAP code to reduce ! usage
1 parent 5c022a3 commit 49327b8

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

MailKit/Net/Imap/ImapCommand.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,9 @@ public bool Step ()
597597
var text = Engine.ReadLine (CancellationToken).Trim ();
598598

599599
// if we've got a Literal pending, the '+' means we can send it now...
600-
if (!supportsLiteralPlus && parts[current].Literal != null) {
601-
parts[current].Literal!.WriteTo (Engine.Stream, CancellationToken);
600+
var literal = parts[current].Literal;
601+
if (!supportsLiteralPlus && literal != null) {
602+
literal.WriteTo (Engine.Stream, CancellationToken);
602603
break;
603604
}
604605

@@ -739,8 +740,9 @@ public async Task<bool> StepAsync ()
739740
var text = (await Engine.ReadLineAsync (CancellationToken).ConfigureAwait (false)).Trim ();
740741

741742
// if we've got a Literal pending, the '+' means we can send it now...
742-
if (!supportsLiteralPlus && parts[current].Literal != null) {
743-
await parts[current].Literal!.WriteToAsync (Engine.Stream, CancellationToken).ConfigureAwait (false);
743+
var literal = parts[current].Literal;
744+
if (!supportsLiteralPlus && literal != null) {
745+
await literal.WriteToAsync (Engine.Stream, CancellationToken).ConfigureAwait (false);
744746
break;
745747
}
746748

MailKit/Net/Imap/ImapEngine.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,7 +2847,7 @@ internal void ProcessUntaggedResponse (CancellationToken cancellationToken)
28472847
} else if (atom.Equals ("FLAGS", StringComparison.OrdinalIgnoreCase)) {
28482848
var keywords = new HashSet<string> (StringComparer.Ordinal);
28492849
var flags = ImapUtils.ParseFlagsList (this, atom, keywords, cancellationToken);
2850-
folder!.UpdateAcceptedFlags (flags, keywords);
2850+
folder?.UpdateAcceptedFlags (flags, keywords);
28512851
token = ReadToken (cancellationToken);
28522852

28532853
AssertToken (token, ImapTokenType.Eoln, GenericUntaggedResponseSyntaxErrorFormat, atom, token);
@@ -3000,7 +3000,7 @@ internal async Task ProcessUntaggedResponseAsync (CancellationToken cancellation
30003000
} else if (atom.Equals ("FLAGS", StringComparison.OrdinalIgnoreCase)) {
30013001
var keywords = new HashSet<string> (StringComparer.Ordinal);
30023002
var flags = await ImapUtils.ParseFlagsListAsync (this, atom, keywords, cancellationToken).ConfigureAwait (false);
3003-
folder!.UpdateAcceptedFlags (flags, keywords);
3003+
folder?.UpdateAcceptedFlags (flags, keywords);
30043004
token = await ReadTokenAsync (cancellationToken).ConfigureAwait (false);
30053005

30063006
AssertToken (token, ImapTokenType.Eoln, GenericUntaggedResponseSyntaxErrorFormat, atom, token);
@@ -3082,6 +3082,7 @@ internal async Task ProcessUntaggedResponseAsync (CancellationToken cancellation
30823082
}
30833083
}
30843084

3085+
[MemberNotNull (nameof (current))]
30853086
void PopNextCommand ()
30863087
{
30873088
lock (queue) {
@@ -3133,7 +3134,7 @@ void Iterate ()
31333134
{
31343135
PopNextCommand ();
31353136

3136-
current!.Status = ImapCommandStatus.Active;
3137+
current.Status = ImapCommandStatus.Active;
31373138

31383139
try {
31393140
while (current.Step ()) {
@@ -3160,7 +3161,7 @@ async Task IterateAsync ()
31603161
{
31613162
PopNextCommand ();
31623163

3163-
current!.Status = ImapCommandStatus.Active;
3164+
current.Status = ImapCommandStatus.Active;
31643165

31653166
try {
31663167
while (await current.StepAsync ().ConfigureAwait (false)) {

MailKit/Net/Imap/ImapFolderSearch.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ static void ParseESearchResults (ImapEngine engine, ImapCommand ic, SearchResult
461461
{
462462
var token = engine.ReadToken (ic.CancellationToken);
463463
UniqueId? minValue = null, maxValue = null;
464+
var folder = ic.Folder!;
464465
bool hasCount = false;
465466
int parenDepth = 0;
466467
//bool uid = false;
@@ -558,18 +559,17 @@ static void ParseESearchResults (ImapEngine engine, ImapCommand ic, SearchResult
558559

559560
var min = ImapEngine.ParseNumber (token, true, ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
560561

561-
results.Min = new UniqueId (ic.Folder!.UidValidity, min);
562+
results.Min = new UniqueId (folder.UidValidity, min);
562563
} else if (atom.Equals ("MAX", StringComparison.OrdinalIgnoreCase)) {
563564
ImapEngine.AssertToken (token, ImapTokenType.Atom, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token);
564565

565566
var max = ImapEngine.ParseNumber (token, true, ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
566567

567-
results.Max = new UniqueId (ic.Folder!.UidValidity, max);
568+
results.Max = new UniqueId (folder.UidValidity, max);
568569
} else if (atom.Equals ("ALL", StringComparison.OrdinalIgnoreCase)) {
569570
ImapEngine.AssertToken (token, ImapTokenType.Atom, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token);
570571

571-
var uids = ImapEngine.ParseUidSet (token, ic.Folder!.UidValidity, out minValue, out maxValue, ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
572-
572+
var uids = ImapEngine.ParseUidSet (token, folder.UidValidity, out minValue, out maxValue, ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
573573
if (!hasCount)
574574
results.Count = uids.Count;
575575

@@ -592,6 +592,7 @@ static async Task ParseESearchResultsAsync (ImapEngine engine, ImapCommand ic, S
592592
{
593593
var token = await engine.ReadTokenAsync (ic.CancellationToken).ConfigureAwait (false);
594594
UniqueId? minValue = null, maxValue = null;
595+
var folder = ic.Folder!;
595596
bool hasCount = false;
596597
int parenDepth = 0;
597598
//bool uid = false;
@@ -689,18 +690,17 @@ static async Task ParseESearchResultsAsync (ImapEngine engine, ImapCommand ic, S
689690

690691
var min = ImapEngine.ParseNumber (token, true, ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
691692

692-
results.Min = new UniqueId (ic.Folder!.UidValidity, min);
693+
results.Min = new UniqueId (folder.UidValidity, min);
693694
} else if (atom.Equals ("MAX", StringComparison.OrdinalIgnoreCase)) {
694695
ImapEngine.AssertToken (token, ImapTokenType.Atom, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token);
695696

696697
var max = ImapEngine.ParseNumber (token, true, ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
697698

698-
results.Max = new UniqueId (ic.Folder!.UidValidity, max);
699+
results.Max = new UniqueId (folder.UidValidity, max);
699700
} else if (atom.Equals ("ALL", StringComparison.OrdinalIgnoreCase)) {
700701
ImapEngine.AssertToken (token, ImapTokenType.Atom, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token);
701702

702-
var uids = ImapEngine.ParseUidSet (token, ic.Folder!.UidValidity, out minValue, out maxValue, ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
703-
703+
var uids = ImapEngine.ParseUidSet (token, folder.UidValidity, out minValue, out maxValue, ImapEngine.GenericItemSyntaxErrorFormat, atom, token);
704704
if (!hasCount)
705705
results.Count = uids.Count;
706706

@@ -733,6 +733,7 @@ static Task UntaggedESearchHandler (ImapEngine engine, ImapCommand ic, int index
733733

734734
static void ParseSearchResults (ImapEngine engine, ImapCommand ic, SearchResults results)
735735
{
736+
var folder = ic.Folder!;
736737
var uids = results.UniqueIds;
737738
uint min = uint.MaxValue;
738739
uint uid, max = 0;
@@ -748,7 +749,7 @@ static void ParseSearchResults (ImapEngine engine, ImapCommand ic, SearchResults
748749
token = engine.ReadToken (ic.CancellationToken);
749750

750751
uid = ImapEngine.ParseNumber (token, true, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "SEARCH", token);
751-
uids.Add (new UniqueId (ic.Folder!.UidValidity, uid));
752+
uids.Add (new UniqueId (folder.UidValidity, uid));
752753
min = Math.Min (min, uid);
753754
max = Math.Max (max, uid);
754755
} while (true);
@@ -779,13 +780,14 @@ static void ParseSearchResults (ImapEngine engine, ImapCommand ic, SearchResults
779780
results.UniqueIds = uids;
780781
results.Count = uids.Count;
781782
if (uids.Count > 0) {
782-
results.Min = new UniqueId (ic.Folder!.UidValidity, min);
783-
results.Max = new UniqueId (ic.Folder!.UidValidity, max);
783+
results.Min = new UniqueId (folder.UidValidity, min);
784+
results.Max = new UniqueId (folder.UidValidity, max);
784785
}
785786
}
786787

787788
static async Task ParseSearchResultsAsync (ImapEngine engine, ImapCommand ic, SearchResults results)
788789
{
790+
var folder = ic.Folder!;
789791
var uids = results.UniqueIds;
790792
uint min = uint.MaxValue;
791793
uint uid, max = 0;
@@ -801,7 +803,7 @@ static async Task ParseSearchResultsAsync (ImapEngine engine, ImapCommand ic, Se
801803
token = await engine.ReadTokenAsync (ic.CancellationToken).ConfigureAwait (false);
802804

803805
uid = ImapEngine.ParseNumber (token, true, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "SEARCH", token);
804-
uids.Add (new UniqueId (ic.Folder!.UidValidity, uid));
806+
uids.Add (new UniqueId (folder.UidValidity, uid));
805807
min = Math.Min (min, uid);
806808
max = Math.Max (max, uid);
807809
} while (true);
@@ -832,8 +834,8 @@ static async Task ParseSearchResultsAsync (ImapEngine engine, ImapCommand ic, Se
832834
results.UniqueIds = uids;
833835
results.Count = uids.Count;
834836
if (uids.Count > 0) {
835-
results.Min = new UniqueId (ic.Folder!.UidValidity, min);
836-
results.Max = new UniqueId (ic.Folder!.UidValidity, max);
837+
results.Min = new UniqueId (folder.UidValidity, min);
838+
results.Max = new UniqueId (folder.UidValidity, max);
837839
}
838840
}
839841

0 commit comments

Comments
 (0)