Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tools] Enable nullability on StringUtils. #14944

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public override bool Execute ()

var arguments = new List<string> ();
if (!StringUtils.TryParseArguments (aotArguments, out var parsedArguments, out var ex)) {
Log.LogError (MSBStrings.E7071, /* Unable to parse the AOT compiler arguments: {0} ({1}) */ aotArguments, ex.Message);
Log.LogError (MSBStrings.E7071, /* Unable to parse the AOT compiler arguments: {0} ({1}) */ aotArguments, ex!.Message);
return false;
}
if (!StringUtils.TryParseArguments (processArguments, out var parsedProcessArguments, out var ex2)) {
Log.LogError (MSBStrings.E7071, /* Unable to parse the AOT compiler arguments: {0} ({1}) */ processArguments, ex2.Message);
Log.LogError (MSBStrings.E7071, /* Unable to parse the AOT compiler arguments: {0} ({1}) */ processArguments, ex2!.Message);
return false;
}
arguments.Add ($"{string.Join (",", parsedArguments)}");
Expand Down
4 changes: 2 additions & 2 deletions tools/common/Execution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Task<Execution> RunAsync ()
try {
var p = new Process ();
p.StartInfo.FileName = FileName;
p.StartInfo.Arguments = StringUtils.FormatArguments (Arguments);
p.StartInfo.Arguments = Arguments is not null ? StringUtils.FormatArguments (Arguments) : "";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false;
p.StartInfo.RedirectStandardOutput = true;
Expand Down Expand Up @@ -96,7 +96,7 @@ public Task<Execution> RunAsync ()
try {
if (Log != null) {
if (!string.IsNullOrEmpty (p.StartInfo.WorkingDirectory))
Log.Write ($"cd {StringUtils.Quote (p.StartInfo.WorkingDirectory)} && ");
Log.Write ($"cd {StringUtils.Quote (p.StartInfo.WorkingDirectory!)} && ");
Log.WriteLine ("{0} {1}", p.StartInfo.FileName, p.StartInfo.Arguments);
}

Expand Down
50 changes: 25 additions & 25 deletions tools/common/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Text;

#nullable disable
#nullable enable

namespace Xamarin.Utils {
internal class StringUtils {
Expand All @@ -20,9 +20,9 @@ static StringUtils ()
static char[] mustQuoteCharacters = new char [] { ' ', '\'', ',', '$', '\\' };
static char [] mustQuoteCharactersProcess = { ' ', '\\', '"', '\'' };

public static string[] Quote (params string[] array)
public static string[]? Quote (params string[] array)
{
if (array == null || array.Length == 0)
if (array is null || array.Length == 0)
return array;

var rv = new string [array.Length];
Expand Down Expand Up @@ -53,16 +53,16 @@ public static string Quote (string f)
return s.ToString ();
}

public static string [] QuoteForProcess (IList<string> arguments)
public static string[]? QuoteForProcess (IList<string> arguments)
{
if (arguments == null)
if (arguments is null)
return Array.Empty<string> ();
return QuoteForProcess (arguments.ToArray ());
}

public static string [] QuoteForProcess (params string [] array)
public static string[]? QuoteForProcess (params string [] array)
{
if (array == null || array.Length == 0)
if (array is null || array.Length == 0)
return array;

var rv = new string [array.Length];
Expand Down Expand Up @@ -106,9 +106,9 @@ public static string FormatArguments (IList<string> arguments)
return string.Join (" ", QuoteForProcess (arguments));
}

public static string Unquote (string input)
public static string? Unquote (string input)
{
if (input == null || input.Length == 0 || input [0] != shellQuoteChar)
if (input is null || input.Length == 0 || input [0] != shellQuoteChar)
return input;

var builder = new StringBuilder ();
Expand All @@ -124,24 +124,23 @@ public static string Unquote (string input)
return builder.ToString ();
}

public static bool TryParseArguments (string quotedArguments, out string [] argv, out Exception ex)
public static bool TryParseArguments (string quotedArguments, out string []? argv, out Exception? ex)
{
var builder = new StringBuilder ();
var args = new List<string> ();
string argument;
int i = 0, j;
char c;

while (i < quotedArguments.Length) {
c = quotedArguments [i];
if (c != ' ' && c != '\t') {
if ((argument = GetArgument (builder, quotedArguments, i, out j, out ex)) == null) {
if (GetArgument (builder, quotedArguments, i, out j, out ex) is string argument) {
args.Add (argument);
i = j;
} else {
argv = null;
return false;
}

args.Add (argument);
i = j;
}

i++;
Expand All @@ -153,7 +152,7 @@ public static bool TryParseArguments (string quotedArguments, out string [] argv
return true;
}

static string GetArgument (StringBuilder builder, string buf, int startIndex, out int endIndex, out Exception ex)
static string? GetArgument (StringBuilder builder, string buf, int startIndex, out int endIndex, out Exception? ex)
{
bool escaped = false;
char qchar, c = '\0';
Expand Down Expand Up @@ -184,16 +183,17 @@ static string GetArgument (StringBuilder builder, string buf, int startIndex, ou
break;
} else if (qchar == '\0' && (c == '\'' || c == '"')) {
string sofar = builder.ToString ();
string embedded;

if ((embedded = GetArgument (builder, buf, i, out endIndex, out ex)) == null)
return null;
if (GetArgument (builder, buf, i, out endIndex, out ex) is string embedded ) {
i = endIndex;
builder.Clear ();
builder.Append (sofar);
builder.Append (embedded);
continue;
}

return null;

i = endIndex;
builder.Clear ();
builder.Append (sofar);
builder.Append (embedded);
continue;
} else {
builder.Append (c);
}
Expand Down Expand Up @@ -233,7 +233,7 @@ static class StringExtensions
// If the original array is null, a new array is also created, with just the new value.
internal static T [] CopyAndAdd<T>(this T[] array, T value)
{
if (array == null || array.Length == 0)
if (array is null || array.Length == 0)
return new T [] { value };
var tmpArray = array;
Array.Resize (ref array, array.Length + 1);
Expand Down