Skip to content

Commit

Permalink
Generate Index and Range
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Dec 11, 2023
1 parent ddafe42 commit 157539a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
15 changes: 14 additions & 1 deletion AdvancedSharpAdbClient/AdvancedSharpAdbClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,26 @@
<DefineConstants>$(DefineConstants);HAS_RUNTIMEINFORMATION</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' != 'net2.0-client'
and '$(TargetFramework)' != 'net3.0-client'
and '$(TargetFramework)' != 'net3.5-client'
and '$(TargetFramework)' != 'net4.0-client'
and '$(TargetFramework)' != 'net4.5.2'
and '$(TargetFramework)' != 'net4.6.2'
and '$(TargetFramework)' != 'netstandard1.3'
and '$(TargetFramework)' != 'netcore5.0'
and '$(TargetFramework)' != 'uap10.0'">
<DefineConstants>$(DefineConstants);HAS_RANGE</DefineConstants>
<PolySharpIncludeGeneratedTypes>$(PolySharpIncludeGeneratedTypes);System.Range</PolySharpIncludeGeneratedTypes>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'
or '$(TargetFramework)' == 'net6.0-windows10.0.17763.0'
or '$(TargetFramework)' == 'net8.0'
or '$(TargetFramework)' == 'net8.0-windows10.0.17763.0'
or '$(TargetFramework)' == 'netcoreapp3.1'
or '$(TargetFramework)' == 'netstandard2.1'">
<DefineConstants>$(DefineConstants);HAS_BUFFERS;HAS_INDEXRANGE</DefineConstants>
<DefineConstants>$(DefineConstants);HAS_BUFFERS</DefineConstants>
<PolySharpIncludeGeneratedTypes>$(PolySharpIncludeGeneratedTypes);System.Runtime.CompilerServices.CollectionBuilderAttribute</PolySharpIncludeGeneratedTypes>
</PropertyGroup>

Expand Down
20 changes: 8 additions & 12 deletions AdvancedSharpAdbClient/DeviceCommands/LinuxPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ public static string Combine(params string[] paths)
{
capacity += paths[i].Length;
}
#if HAS_INDEXRANGE
char ch = paths[i][^1];
#else
char ch = paths[i][paths[i].Length - 1];
#endif
if (ch != DirectorySeparatorChar)
{
capacity++;
Expand All @@ -81,16 +77,11 @@ public static string Combine(params string[] paths)
}
else
{
#if HAS_INDEXRANGE
char ch2 = builder[^1];
#else
char ch2 = builder[builder.Length - 1];
#endif
if (ch2 != DirectorySeparatorChar)
{
_ = builder.Append(DirectorySeparatorChar);
}

_ = builder.Append(paths[j]);
}
}
Expand Down Expand Up @@ -125,11 +116,16 @@ public static string Combine(params string[] paths)
{
return tpath;
}
#if HAS_INDEXRANGE
tpath = tpath[..(tpath.LastIndexOf(DirectorySeparatorChar) + 1)];

tpath =
#if HAS_BUFFERS
tpath.AsSpan(0, tpath.LastIndexOf(DirectorySeparatorChar) + 1).ToString();
#elif HAS_RANGE
tpath[..(tpath.LastIndexOf(DirectorySeparatorChar) + 1)];
#else
tpath = tpath.Substring(0, tpath.LastIndexOf(DirectorySeparatorChar) + 1);
tpath.Substring(0, tpath.LastIndexOf(DirectorySeparatorChar) + 1);
#endif

return FixupPath(tpath);
}
else if (tpath.Length == 1)
Expand Down
28 changes: 14 additions & 14 deletions AdvancedSharpAdbClient/DeviceCommands/Models/AndroidProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public AndroidProcess(string line, bool cmdLinePrefix = false)

if (cmdLinePrefix)
{
#if HAS_INDEXRANGE
string[] cmdLineParts = line[..processNameStart]
string[] cmdLineParts =
#if HAS_RANGE
line[..processNameStart]
#else

string[] cmdLineParts = line.Substring(0, processNameStart)
line.Substring(0, processNameStart)
#endif
.Split('\0');

Expand All @@ -57,11 +57,7 @@ public AndroidProcess(string line, bool cmdLinePrefix = false)
}
else
{
#if HAS_INDEXRANGE
pid = int.Parse(cmdLineParts[^1]);
#else
pid = int.Parse(cmdLineParts[cmdLineParts.Length - 1]);
#endif
ProcessId = pid;

comm = cmdLineParts[0];
Expand All @@ -74,21 +70,25 @@ public AndroidProcess(string line, bool cmdLinePrefix = false)

if (!parsedCmdLinePrefix)
{
#if HAS_INDEXRANGE
pid = int.Parse(line[..processNameStart]);
pid =
#if HAS_BUFFERS
int.Parse(line.AsSpan(0, processNameStart));
#elif HAS_RANGE
int.Parse(line[..processNameStart]);
#else
pid = int.Parse(line.Substring(0, processNameStart));
int.Parse(line.Substring(0, processNameStart));
#endif
ProcessId = pid;

comm = line.Substring(processNameStart + 1, processNameEnd - processNameStart - 1);
Name = comm;
}

#if HAS_INDEXRANGE
string[] parts = line[(processNameEnd + 1)..]
string[] parts =
#if HAS_RANGE
line[(processNameEnd + 1)..]
#else
string[] parts = line.Substring(processNameEnd + 1)
line.Substring(processNameEnd + 1)
#endif
.Split(' ', StringSplitOptions.RemoveEmptyEntries);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere. All rights reserved.
// </copyright>

using System;
using System.Collections.Generic;

namespace AdvancedSharpAdbClient.DeviceCommands.Receivers
Expand Down Expand Up @@ -36,10 +37,13 @@ protected override void ProcessNewLines(IEnumerable<string> lines)
// package:mwc2015.be

// Remove the "package:" prefix
#if HAS_INDEXRANGE
string package = line[8..];
string package =
#if HAS_BUFFERS
line.AsSpan(8).ToString();
#elif HAS_RANGE
line[8..];
#else
string package = line.Substring(8);
line.Substring(8);
#endif
//// If there's a '=' included, use the last instance,
//// to accommodate for values like
Expand All @@ -54,7 +58,10 @@ protected override void ProcessNewLines(IEnumerable<string> lines)
}
else
{
#if HAS_INDEXRANGE
#if HAS_BUFFERS
string path = package.AsSpan(0, separator).ToString();
string name = package.AsSpan(separator + 1).ToString();
#elif HAS_RANGE
string path = package[..separator];
string name = package[(separator + 1)..];
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ private void CheckPackagesSection(string line)

return !inPackagesSection ? null
: line != null && line.Trim().StartsWith("versionName=")
#if HAS_INDEXRANGE
#if HAS_BUFFERS
? line.Trim().AsSpan(12).ToString().Trim() : null;
#elif HAS_RANGE
? line.Trim()[12..].Trim() : null;
#else
? line.Trim().Substring(12).Trim() : null;
Expand Down

0 comments on commit 157539a

Please sign in to comment.