Skip to content

Commit 03c3780

Browse files
authored
First class span types (#2039)
* Rely on first-class span conversions of C# 14 * Optimize use of Span in Bytes
1 parent c4324c9 commit 03c3780

5 files changed

Lines changed: 11 additions & 15 deletions

File tree

src/core/IronPython/Hosting/PythonOptionsParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ protected override void ParseArgument(string/*!*/ arg) {
150150
}
151151

152152
if (arg == "/?" || arg == "--help") {
153-
HandleOptions("h".AsSpan());
153+
HandleOptions("h");
154154
return;
155155
}
156156

157157
if (arg == "--version") {
158-
HandleOptions("V".AsSpan());
158+
HandleOptions("V");
159159
return;
160160
}
161161

src/core/IronPython/Modules/_io/StringIO.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private string readline(int limit) {
114114
span = span.Slice(0, idx + 1);
115115
}
116116
} else if (_newline == string.Empty) {
117-
var idx = span.IndexOfAny("\r\n".AsSpan());
117+
var idx = span.IndexOfAny("\r\n");
118118
if (idx != -1) {
119119
if (span[idx++] == '\r') {
120120
// ensure we don't split \r\n
@@ -377,7 +377,7 @@ private int DoWrite(string str) {
377377

378378
if (_newline is null) {
379379
while (true) {
380-
var idx = span.IndexOfAny("\r\n".AsSpan());
380+
var idx = span.IndexOfAny("\r\n");
381381
if (idx == -1)
382382
break;
383383

src/core/IronPython/Runtime/Bytes.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ public static object fromhex(CodeContext context, [NotNone] PythonType cls, [Not
375375
return PythonTypeOps.CallParams(context, cls, new Bytes(hex));
376376
}
377377

378-
public string hex() => ToHex(_bytes.AsSpan()); // new in CPython 3.5
378+
public string hex() => ToHex(_bytes); // new in CPython 3.5
379379

380380
internal static string ToHex(ReadOnlySpan<byte> bytes) {
381381
if (bytes.Length == 0) return string.Empty;
@@ -395,13 +395,13 @@ static char ToAscii(int b) {
395395
// new in CPython 3.8
396396
public string hex([NotNone] string sep, int bytes_per_sep = 1) {
397397
if (sep.Length != 1) throw PythonOps.ValueError($"{nameof(sep)} must be length 1");
398-
return ToHex(_bytes.AsSpan(), sep[0], bytes_per_sep);
398+
return ToHex(_bytes, sep[0], bytes_per_sep);
399399
}
400400

401401
// new in CPython 3.8
402402
public string hex([BytesLike, NotNone] IList<byte> sep, int bytes_per_sep = 1) {
403403
if (sep.Count != 1) throw PythonOps.ValueError($"{nameof(sep)} must be length 1");
404-
return ToHex(_bytes.AsSpan(), (char)sep[0], bytes_per_sep);
404+
return ToHex(_bytes, (char)sep[0], bytes_per_sep);
405405
}
406406

407407
internal static string ToHex(ReadOnlySpan<byte> bytes, char sep, int bytes_per_sep) {
@@ -1077,9 +1077,9 @@ public int this[object? index] {
10771077

10781078
#region Implementation Details
10791079

1080-
internal ReadOnlyMemory<byte> AsMemory() => _bytes.AsMemory();
1080+
internal ReadOnlyMemory<byte> AsMemory() => _bytes;
10811081

1082-
internal ReadOnlySpan<byte> AsSpan() => _bytes.AsSpan();
1082+
internal ReadOnlySpan<byte> AsSpan() => _bytes;
10831083

10841084
internal static bool TryInvokeBytesOperator(CodeContext context, object? obj, [NotNullWhen(true)] out Bytes? bytes) {
10851085
if (PythonTypeOps.TryInvokeUnaryOperator(context, obj, "__bytes__", out object? res)) {

src/core/IronPython/Runtime/LiteralParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void handleError(in ReadOnlySpan<T> data, int start, int end, string reason) {
219219
#nullable enable
220220

221221
private static bool TryParseExpression(Parser parser, ReadOnlySpan<char> data, [NotNullWhen(true)] out Expression? expression, [NotNullWhen(false)] out string? error) {
222-
if (data.TrimStart(" \t\f\r\n".AsSpan()).Length == 0) {
222+
if (data.TrimStart(" \t\f\r\n").Length == 0) {
223223
expression = null;
224224
error = "f-string: empty expression not allowed";
225225
return false;
@@ -649,7 +649,7 @@ private static bool TryParseInt<T>(in ReadOnlySpan<T> text, int start, int lengt
649649
}
650650

651651
public static object ParseInteger(string text, int b) {
652-
if (TryParseInteger(text.AsSpan(), b, false, out object val)) {
652+
if (TryParseInteger(text, b, false, out object val)) {
653653
return val;
654654
}
655655
throw new ValueErrorException($"invalid literal with base {b}: {text}");

src/core/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,10 +3689,6 @@ internal static string MakeString(this IList<byte> bytes, int startIdx, int maxB
36893689
return StringOps.Latin1Encoding.GetString(byteArr, startIdx, maxBytes);
36903690
}
36913691

3692-
internal static string MakeString(this Span<byte> bytes) {
3693-
return MakeString((ReadOnlySpan<byte>)bytes);
3694-
}
3695-
36963692
internal static string MakeString(this ReadOnlySpan<byte> bytes) {
36973693
if (bytes.IsEmpty) {
36983694
return string.Empty;

0 commit comments

Comments
 (0)