refactored GetArrayDimensions method, ParseFSize now returns null if not found

This commit is contained in:
mm00
2025-03-12 18:59:47 +01:00
parent c48d84bb7c
commit 6533511a4a
2 changed files with 24 additions and 21 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace CodeGenerator {
@@ -28,7 +29,7 @@ namespace CodeGenerator {
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ParseFSize(this string str) {
public static int? ParseFSize(this string str) {
return str switch {
"char" => sizeof(sbyte),
"short" => sizeof(short),
@@ -40,8 +41,26 @@ namespace CodeGenerator {
"int64_t" => sizeof(long),
"int8_t" => sizeof(sbyte),
"uint64_t" => sizeof(ulong),
_ => throw new("Unknown type")
_ => null
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static List<int> GetArrayDimensions(this string name) {
var dimensions = new List<int>();
int startIndex = 0;
// Get all array dimensions
while ((startIndex = name.IndexOf('[', startIndex)) != -1) {
int endIndex = name.IndexOf(']', startIndex);
string sizeStr = name.Substring(startIndex + 1, endIndex - startIndex - 1);
if (int.TryParse(sizeStr, out int size)) {
dimensions.Add(size);
}
startIndex = endIndex + 1;
}
return dimensions;
}
}
}